Menu.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/HashMap.h>
  27. #include <AK/SharedBuffer.h>
  28. #include <LibGUI/Action.h>
  29. #include <LibGUI/ActionGroup.h>
  30. #include <LibGUI/Menu.h>
  31. #include <LibGUI/MenuItem.h>
  32. #include <LibGUI/WindowServerConnection.h>
  33. #include <LibGfx/Bitmap.h>
  34. //#define MENU_DEBUG
  35. namespace GUI {
  36. static HashMap<int, Menu*>& all_menus()
  37. {
  38. static HashMap<int, Menu*>* map;
  39. if (!map)
  40. map = new HashMap<int, Menu*>();
  41. return *map;
  42. }
  43. Menu* Menu::from_menu_id(int menu_id)
  44. {
  45. auto it = all_menus().find(menu_id);
  46. if (it == all_menus().end())
  47. return nullptr;
  48. return (*it).value;
  49. }
  50. Menu::Menu(const StringView& name)
  51. : m_name(name)
  52. {
  53. }
  54. Menu::~Menu()
  55. {
  56. unrealize_menu();
  57. }
  58. void Menu::set_icon(const Gfx::Bitmap* icon)
  59. {
  60. m_icon = icon;
  61. }
  62. void Menu::add_action(NonnullRefPtr<Action> action)
  63. {
  64. m_items.append(make<MenuItem>(m_menu_id, move(action)));
  65. #ifdef GMENU_DEBUG
  66. dbgln("GUI::Menu::add_action(): MenuItem Menu ID: {}", m_menu_id);
  67. #endif
  68. }
  69. Menu& Menu::add_submenu(const String& name)
  70. {
  71. auto submenu = Menu::construct(name);
  72. m_items.append(make<MenuItem>(m_menu_id, submenu));
  73. return submenu;
  74. }
  75. void Menu::add_separator()
  76. {
  77. m_items.append(make<MenuItem>(m_menu_id, MenuItem::Type::Separator));
  78. }
  79. void Menu::realize_if_needed(const RefPtr<Action>& default_action)
  80. {
  81. if (m_menu_id == -1 || m_last_default_action.ptr() != default_action)
  82. realize_menu(default_action);
  83. }
  84. void Menu::popup(const Gfx::IntPoint& screen_position, const RefPtr<Action>& default_action)
  85. {
  86. realize_if_needed(default_action);
  87. WindowServerConnection::the().post_message(Messages::WindowServer::PopupMenu(m_menu_id, screen_position));
  88. }
  89. void Menu::dismiss()
  90. {
  91. if (m_menu_id == -1)
  92. return;
  93. WindowServerConnection::the().post_message(Messages::WindowServer::DismissMenu(m_menu_id));
  94. }
  95. template<typename IconContainerType>
  96. static int ensure_realized_icon(IconContainerType& container)
  97. {
  98. int icon_buffer_id = -1;
  99. if (container.icon()) {
  100. ASSERT(container.icon()->format() == Gfx::BitmapFormat::RGBA32);
  101. ASSERT(container.icon()->size() == Gfx::IntSize(16, 16));
  102. if (container.icon()->shbuf_id() == -1) {
  103. auto shared_buffer = SharedBuffer::create_with_size(container.icon()->size_in_bytes());
  104. ASSERT(shared_buffer);
  105. auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, container.icon()->size());
  106. memcpy(shared_buffer->template data<u8>(), container.icon()->scanline_u8(0), container.icon()->size_in_bytes());
  107. shared_buffer->seal();
  108. shared_buffer->share_with(WindowServerConnection::the().server_pid());
  109. container.set_icon(shared_icon);
  110. }
  111. icon_buffer_id = container.icon()->shbuf_id();
  112. }
  113. return icon_buffer_id;
  114. }
  115. int Menu::realize_menu(RefPtr<Action> default_action)
  116. {
  117. unrealize_menu();
  118. m_menu_id = WindowServerConnection::the().send_sync<Messages::WindowServer::CreateMenu>(m_name)->menu_id();
  119. #ifdef MENU_DEBUG
  120. dbgln("GUI::Menu::realize_menu(): New menu ID: {}", m_menu_id);
  121. #endif
  122. ASSERT(m_menu_id > 0);
  123. for (size_t i = 0; i < m_items.size(); ++i) {
  124. auto& item = m_items[i];
  125. item.set_menu_id({}, m_menu_id);
  126. item.set_identifier({}, i);
  127. if (item.type() == MenuItem::Type::Separator) {
  128. WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuSeparator>(m_menu_id);
  129. continue;
  130. }
  131. if (item.type() == MenuItem::Type::Submenu) {
  132. auto& submenu = *item.submenu();
  133. submenu.realize_if_needed(default_action);
  134. int icon_buffer_id = ensure_realized_icon(submenu);
  135. WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuItem>(m_menu_id, i, submenu.menu_id(), submenu.name(), true, false, false, false, "", icon_buffer_id, false);
  136. continue;
  137. }
  138. if (item.type() == MenuItem::Type::Action) {
  139. auto& action = *item.action();
  140. int icon_buffer_id = ensure_realized_icon(action);
  141. auto shortcut_text = action.shortcut().is_valid() ? action.shortcut().to_string() : String();
  142. bool exclusive = action.group() && action.group()->is_exclusive() && action.is_checkable();
  143. bool is_default = (default_action.ptr() == &action);
  144. WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuItem>(m_menu_id, i, -1, action.text(), action.is_enabled(), action.is_checkable(), action.is_checkable() ? action.is_checked() : false, is_default, shortcut_text, icon_buffer_id, exclusive);
  145. }
  146. }
  147. all_menus().set(m_menu_id, this);
  148. m_last_default_action = default_action;
  149. return m_menu_id;
  150. }
  151. void Menu::unrealize_menu()
  152. {
  153. if (m_menu_id == -1)
  154. return;
  155. all_menus().remove(m_menu_id);
  156. WindowServerConnection::the().send_sync<Messages::WindowServer::DestroyMenu>(m_menu_id);
  157. m_menu_id = -1;
  158. }
  159. void Menu::realize_menu_if_needed()
  160. {
  161. if (menu_id() == -1)
  162. realize_menu();
  163. }
  164. Action* Menu::action_at(size_t index)
  165. {
  166. if (index >= m_items.size())
  167. return nullptr;
  168. return m_items[index].action();
  169. }
  170. }