| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Action.h>
 | 
					
						
							|  |  |  | #include <LibGUI/Menu.h>
 | 
					
						
							|  |  |  | #include <LibGUI/MenuItem.h>
 | 
					
						
							|  |  |  | #include <LibGUI/WindowServerConnection.h>
 | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | namespace GUI { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | MenuItem::MenuItem(unsigned menu_id, Type type) | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  |     : m_type(type) | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     , m_menu_id(menu_id) | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | MenuItem::MenuItem(unsigned menu_id, NonnullRefPtr<Action>&& action) | 
					
						
							|  |  |  |     : m_type(Type::Action) | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     , m_menu_id(menu_id) | 
					
						
							| 
									
										
										
										
											2019-02-12 14:09:48 +01:00
										 |  |  |     , m_action(move(action)) | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |     m_action->register_menu_item({}, *this); | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     m_enabled = m_action->is_enabled(); | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  |     m_checkable = m_action->is_checkable(); | 
					
						
							|  |  |  |     if (m_checkable) | 
					
						
							|  |  |  |         m_checked = m_action->is_checked(); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | MenuItem::MenuItem(unsigned menu_id, NonnullRefPtr<Menu>&& submenu) | 
					
						
							|  |  |  |     : m_type(Type::Submenu) | 
					
						
							| 
									
										
										
										
											2019-08-28 21:11:53 +02:00
										 |  |  |     , m_menu_id(menu_id) | 
					
						
							|  |  |  |     , m_submenu(move(submenu)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | MenuItem::~MenuItem() | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     if (m_action) | 
					
						
							| 
									
										
										
										
											2019-06-07 11:46:02 +02:00
										 |  |  |         m_action->unregister_menu_item({}, *this); | 
					
						
							| 
									
										
										
										
											2019-02-11 14:43:43 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void MenuItem::set_enabled(bool enabled) | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     if (m_enabled == enabled) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_enabled = enabled; | 
					
						
							|  |  |  |     update_window_server(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void MenuItem::set_checked(bool checked) | 
					
						
							| 
									
										
										
										
											2019-04-26 21:09:56 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     ASSERT(is_checkable()); | 
					
						
							|  |  |  |     if (m_checked == checked) | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     m_checked = checked; | 
					
						
							|  |  |  |     update_window_server(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | void MenuItem::update_window_server() | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-05-23 19:31:00 -07:00
										 |  |  |     if (m_menu_id < 0) | 
					
						
							|  |  |  |         return; | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  |     auto& action = *m_action; | 
					
						
							| 
									
										
										
										
											2019-12-02 15:54:01 +01:00
										 |  |  |     auto shortcut_text = action.shortcut().is_valid() ? action.shortcut().to_string() : String(); | 
					
						
							| 
									
										
										
										
											2020-02-06 20:20:44 +01:00
										 |  |  |     WindowServerConnection::the().send_sync<Messages::WindowServer::UpdateMenuItem>(m_menu_id, m_identifier, -1, action.text(), action.is_enabled(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, shortcut_text); | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-15 01:56:30 +01:00
										 |  |  | void MenuItem::set_menu_id(Badge<Menu>, unsigned int menu_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_menu_id = menu_id; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void MenuItem::set_identifier(Badge<Menu>, unsigned identifier) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_identifier = identifier; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-12 02:53:27 +02:00
										 |  |  | } |