GMenu.cpp 5.6 KB

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