WSMenu.cpp 7.5 KB

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