GMenu.cpp 5.6 KB

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