Menu.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <LibGUI/Action.h>
  28. #include <LibGUI/ActionGroup.h>
  29. #include <LibGUI/Menu.h>
  30. #include <LibGUI/MenuItem.h>
  31. #include <LibGUI/WindowServerConnection.h>
  32. #include <LibGfx/Bitmap.h>
  33. //#define MENU_DEBUG
  34. namespace GUI {
  35. static HashMap<int, Menu*>& all_menus()
  36. {
  37. static HashMap<int, Menu*>* map;
  38. if (!map)
  39. map = new HashMap<int, Menu*>();
  40. return *map;
  41. }
  42. Menu* Menu::from_menu_id(int menu_id)
  43. {
  44. auto it = all_menus().find(menu_id);
  45. if (it == all_menus().end())
  46. return nullptr;
  47. return (*it).value;
  48. }
  49. Menu::Menu(const StringView& name)
  50. : m_name(name)
  51. {
  52. }
  53. Menu::~Menu()
  54. {
  55. unrealize_menu();
  56. }
  57. void Menu::set_icon(const Gfx::Bitmap* icon)
  58. {
  59. m_icon = icon;
  60. }
  61. void Menu::add_action(NonnullRefPtr<Action> action)
  62. {
  63. m_items.append(make<MenuItem>(m_menu_id, move(action)));
  64. #ifdef GMENU_DEBUG
  65. dbgln("GUI::Menu::add_action(): MenuItem Menu ID: {}", m_menu_id);
  66. #endif
  67. }
  68. Menu& Menu::add_submenu(const String& name)
  69. {
  70. auto submenu = Menu::construct(name);
  71. m_items.append(make<MenuItem>(m_menu_id, submenu));
  72. return submenu;
  73. }
  74. void Menu::add_separator()
  75. {
  76. m_items.append(make<MenuItem>(m_menu_id, MenuItem::Type::Separator));
  77. }
  78. void Menu::realize_if_needed(const RefPtr<Action>& default_action)
  79. {
  80. if (m_menu_id == -1 || m_last_default_action.ptr() != default_action)
  81. realize_menu(default_action);
  82. }
  83. void Menu::popup(const Gfx::IntPoint& screen_position, const RefPtr<Action>& default_action)
  84. {
  85. realize_if_needed(default_action);
  86. WindowServerConnection::the().post_message(Messages::WindowServer::PopupMenu(m_menu_id, screen_position));
  87. }
  88. void Menu::dismiss()
  89. {
  90. if (m_menu_id == -1)
  91. return;
  92. WindowServerConnection::the().post_message(Messages::WindowServer::DismissMenu(m_menu_id));
  93. }
  94. int Menu::realize_menu(RefPtr<Action> default_action)
  95. {
  96. unrealize_menu();
  97. m_menu_id = WindowServerConnection::the().send_sync<Messages::WindowServer::CreateMenu>(m_name)->menu_id();
  98. #ifdef MENU_DEBUG
  99. dbgln("GUI::Menu::realize_menu(): New menu ID: {}", m_menu_id);
  100. #endif
  101. ASSERT(m_menu_id > 0);
  102. for (size_t i = 0; i < m_items.size(); ++i) {
  103. auto& item = m_items[i];
  104. item.set_menu_id({}, m_menu_id);
  105. item.set_identifier({}, i);
  106. if (item.type() == MenuItem::Type::Separator) {
  107. WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuSeparator>(m_menu_id);
  108. continue;
  109. }
  110. if (item.type() == MenuItem::Type::Submenu) {
  111. auto& submenu = *item.submenu();
  112. submenu.realize_if_needed(default_action);
  113. auto icon = submenu.icon() ? submenu.icon()->to_shareable_bitmap() : Gfx::ShareableBitmap();
  114. WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuItem>(m_menu_id, i, submenu.menu_id(), submenu.name(), true, false, false, false, "", icon, false);
  115. continue;
  116. }
  117. if (item.type() == MenuItem::Type::Action) {
  118. auto& action = *item.action();
  119. auto shortcut_text = action.shortcut().is_valid() ? action.shortcut().to_string() : String();
  120. bool exclusive = action.group() && action.group()->is_exclusive() && action.is_checkable();
  121. bool is_default = (default_action.ptr() == &action);
  122. auto icon = action.icon() ? action.icon()->to_shareable_bitmap() : Gfx::ShareableBitmap();
  123. 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, exclusive);
  124. }
  125. }
  126. all_menus().set(m_menu_id, this);
  127. m_last_default_action = default_action;
  128. return m_menu_id;
  129. }
  130. void Menu::unrealize_menu()
  131. {
  132. if (m_menu_id == -1)
  133. return;
  134. all_menus().remove(m_menu_id);
  135. WindowServerConnection::the().send_sync<Messages::WindowServer::DestroyMenu>(m_menu_id);
  136. m_menu_id = -1;
  137. }
  138. void Menu::realize_menu_if_needed()
  139. {
  140. if (menu_id() == -1)
  141. realize_menu();
  142. }
  143. Action* Menu::action_at(size_t index)
  144. {
  145. if (index >= m_items.size())
  146. return nullptr;
  147. return m_items[index].action();
  148. }
  149. }