WSMenu.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #include "WSMenu.h"
  2. #include "WSMenuItem.h"
  3. #include "WSWindow.h"
  4. #include "WSEvent.h"
  5. #include "WSEventLoop.h"
  6. #include "WSWindowManager.h"
  7. #include <WindowServer/WSAPITypes.h>
  8. #include <WindowServer/WSClientConnection.h>
  9. #include <SharedGraphics/CharacterBitmap.h>
  10. #include <SharedGraphics/Painter.h>
  11. #include <SharedGraphics/StylePainter.h>
  12. #include <SharedGraphics/Font.h>
  13. WSMenu::WSMenu(WSClientConnection* client, int menu_id, String&& name)
  14. : m_client(client)
  15. , m_menu_id(menu_id)
  16. , m_name(move(name))
  17. {
  18. }
  19. WSMenu::~WSMenu()
  20. {
  21. }
  22. const Font& WSMenu::font() const
  23. {
  24. return Font::default_font();
  25. }
  26. static const char* s_checked_bitmap_data = {
  27. " "
  28. " # "
  29. " ## "
  30. " ### "
  31. " ## ### "
  32. " ##### "
  33. " ### "
  34. " # "
  35. " "
  36. };
  37. static CharacterBitmap* s_checked_bitmap;
  38. static const int s_checked_bitmap_width = 9;
  39. static const int s_checked_bitmap_height = 9;
  40. static const int s_checked_bitmap_padding = 6;
  41. int WSMenu::width() const
  42. {
  43. int widest_text = 0;
  44. int widest_shortcut = 0;
  45. for (auto& item : m_items) {
  46. if (item->type() != WSMenuItem::Text)
  47. continue;
  48. int text_width = font().width(item->text());
  49. if (!item->shortcut_text().is_empty()) {
  50. int shortcut_width = font().width(item->shortcut_text());
  51. widest_shortcut = max(shortcut_width, widest_shortcut);
  52. }
  53. if (item->is_checkable())
  54. text_width += s_checked_bitmap_width + s_checked_bitmap_padding;
  55. widest_text = max(widest_text, text_width);
  56. }
  57. int widest_item = widest_text;
  58. if (widest_shortcut)
  59. widest_item += padding_between_text_and_shortcut() + widest_shortcut;
  60. return max(widest_item, rect_in_menubar().width()) + horizontal_padding() + frame_thickness() * 2;
  61. }
  62. int WSMenu::height() const
  63. {
  64. if (m_items.is_empty())
  65. return 0;
  66. return (m_items.last()->rect().bottom() - 1) + frame_thickness() * 2;
  67. }
  68. void WSMenu::redraw()
  69. {
  70. if (!menu_window())
  71. return;
  72. draw();
  73. menu_window()->invalidate();
  74. }
  75. WSWindow& WSMenu::ensure_menu_window()
  76. {
  77. int width = this->width();
  78. if (!m_menu_window) {
  79. Point next_item_location(frame_thickness(), frame_thickness());
  80. for (auto& item : m_items) {
  81. int height = 0;
  82. if (item->type() == WSMenuItem::Text)
  83. height = item_height();
  84. else if (item->type() == WSMenuItem::Separator)
  85. height = 8;
  86. item->set_rect({ next_item_location, { width - frame_thickness() * 2, height } });
  87. next_item_location.move_by(0, height);
  88. }
  89. auto window = make<WSWindow>(*this, WSWindowType::Menu);
  90. window->set_opacity(0.95f);
  91. window->set_rect(0, 0, width, height());
  92. m_menu_window = move(window);
  93. draw();
  94. }
  95. return *m_menu_window;
  96. }
  97. void WSMenu::draw()
  98. {
  99. ASSERT(menu_window());
  100. ASSERT(menu_window()->backing_store());
  101. Painter painter(*menu_window()->backing_store());
  102. Rect rect { { }, menu_window()->size() };
  103. painter.fill_rect(rect.shrunken(6, 6), Color::LightGray);
  104. StylePainter::paint_window_frame(painter, rect);
  105. int width = this->width();
  106. if (!s_checked_bitmap)
  107. s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref();
  108. for (auto& item : m_items) {
  109. if (item->type() == WSMenuItem::Text) {
  110. Color text_color = Color::Black;
  111. if (item == m_hovered_item) {
  112. painter.fill_rect(item->rect(), WSWindowManager::the().menu_selection_color());
  113. text_color = Color::White;
  114. }
  115. if (!item->is_enabled())
  116. text_color = Color::MidGray;
  117. Rect text_rect = item->rect().translated(left_padding(), 0);
  118. if (item->is_checkable()) {
  119. if (item->is_checked()) {
  120. Rect checkmark_rect { text_rect.location().x(), 0, s_checked_bitmap_width, s_checked_bitmap_height };
  121. checkmark_rect.center_vertically_within(text_rect);
  122. painter.draw_bitmap(checkmark_rect.location(), *s_checked_bitmap, Color::Black);
  123. }
  124. text_rect.move_by(s_checked_bitmap_width + s_checked_bitmap_padding, 0);
  125. }
  126. painter.draw_text(text_rect, item->text(), TextAlignment::CenterLeft, text_color);
  127. if (!item->shortcut_text().is_empty()) {
  128. painter.draw_text(item->rect().translated(-right_padding(), 0), item->shortcut_text(), TextAlignment::CenterRight, text_color);
  129. }
  130. } else if (item->type() == WSMenuItem::Separator) {
  131. Point p1(4, item->rect().center().y());
  132. Point p2(width - 5, item->rect().center().y());
  133. painter.draw_line(p1, p2, Color::MidGray);
  134. painter.draw_line(p1.translated(0, 1), p2.translated(0, 1), Color::White);
  135. }
  136. }
  137. }
  138. void WSMenu::event(CEvent& event)
  139. {
  140. ASSERT(menu_window());
  141. if (event.type() == WSEvent::MouseMove) {
  142. auto* item = item_at(static_cast<const WSMouseEvent&>(event).position());
  143. if (!item || m_hovered_item == item)
  144. return;
  145. m_hovered_item = item;
  146. redraw();
  147. return;
  148. }
  149. if (event.type() == WSEvent::MouseUp) {
  150. if (!m_hovered_item)
  151. return;
  152. if (m_hovered_item->is_enabled())
  153. did_activate(*m_hovered_item);
  154. clear_hovered_item();
  155. return;
  156. }
  157. CObject::event(event);
  158. }
  159. void WSMenu::clear_hovered_item()
  160. {
  161. if (!m_hovered_item)
  162. return;
  163. m_hovered_item = nullptr;
  164. redraw();
  165. }
  166. void WSMenu::did_activate(WSMenuItem& item)
  167. {
  168. if (on_item_activation)
  169. on_item_activation(item);
  170. close();
  171. WSAPI_ServerMessage message;
  172. message.type = WSAPI_ServerMessage::Type::MenuItemActivated;
  173. message.menu.menu_id = m_menu_id;
  174. message.menu.identifier = item.identifier();
  175. if (m_client)
  176. m_client->post_message(message);
  177. }
  178. WSMenuItem* WSMenu::item_with_identifier(unsigned identifer)
  179. {
  180. for (auto& item : m_items) {
  181. if (item->identifier() == identifer)
  182. return item.ptr();
  183. }
  184. return nullptr;
  185. }
  186. WSMenuItem* WSMenu::item_at(const Point& position)
  187. {
  188. for (auto& item : m_items) {
  189. if (!item->rect().contains(position))
  190. continue;
  191. return item.ptr();
  192. }
  193. return nullptr;
  194. }
  195. void WSMenu::close()
  196. {
  197. WSWindowManager::the().close_menu(*this);
  198. if (menu_window())
  199. menu_window()->set_visible(false);
  200. }
  201. void WSMenu::popup(const Point& position, bool top_anchored)
  202. {
  203. ASSERT(!is_empty());
  204. auto& window = ensure_menu_window();
  205. if (top_anchored)
  206. window.move_to(position);
  207. else
  208. window.move_to(position.translated(0, -window.height()));
  209. window.set_visible(true);
  210. WSWindowManager::the().set_current_menu(this);
  211. }