GlyphMapWidget.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "GlyphMapWidget.h"
  9. #include <LibGUI/Painter.h>
  10. #include <LibGfx/BitmapFont.h>
  11. #include <LibGfx/Emoji.h>
  12. #include <LibGfx/Palette.h>
  13. REGISTER_WIDGET(GUI, GlyphMapWidget);
  14. namespace GUI {
  15. GlyphMapWidget::Selection GlyphMapWidget::Selection::normalized() const
  16. {
  17. if (m_size > 0)
  18. return *this;
  19. return { m_start + m_size, -m_size + 1 };
  20. }
  21. void GlyphMapWidget::Selection::resize_by(int i)
  22. {
  23. m_size += i;
  24. if (m_size == 0) {
  25. if (i < 0)
  26. m_size--;
  27. else
  28. m_size++;
  29. }
  30. }
  31. bool GlyphMapWidget::Selection::contains(int i) const
  32. {
  33. auto this_normalized = normalized();
  34. return i >= this_normalized.m_start && i < this_normalized.m_start + this_normalized.m_size;
  35. }
  36. void GlyphMapWidget::Selection::extend_to(int glyph)
  37. {
  38. m_size = glyph - m_start;
  39. if (m_size > 0)
  40. m_size++;
  41. }
  42. GlyphMapWidget::GlyphMapWidget()
  43. {
  44. set_focus_policy(FocusPolicy::StrongFocus);
  45. horizontal_scrollbar().set_visible(false);
  46. did_change_font();
  47. }
  48. GlyphMapWidget::~GlyphMapWidget()
  49. {
  50. }
  51. void GlyphMapWidget::resize_event(ResizeEvent& event)
  52. {
  53. int event_width = event.size().width() - vertical_scrollbar().width() - (frame_thickness() * 2) - m_horizontal_spacing;
  54. int event_height = event.size().height() - (frame_thickness() * 2);
  55. m_visible_glyphs = (event_width * event_height) / (font().max_glyph_width() * font().glyph_height());
  56. m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
  57. m_rows = ceil_div(m_glyph_count, m_columns);
  58. int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
  59. int content_height = rows() * (font().glyph_height() + m_vertical_spacing) + frame_thickness();
  60. set_content_size({ content_width, content_height });
  61. scroll_to_glyph(m_active_glyph);
  62. AbstractScrollableWidget::resize_event(event);
  63. }
  64. void GlyphMapWidget::set_active_glyph(int glyph, ShouldResetSelection should_reset_selection)
  65. {
  66. if (m_active_glyph == glyph)
  67. return;
  68. m_active_glyph = glyph;
  69. if (should_reset_selection == ShouldResetSelection::Yes) {
  70. m_selection.set_start(glyph);
  71. m_selection.set_size(1);
  72. }
  73. if (on_active_glyph_changed)
  74. on_active_glyph_changed(glyph);
  75. update();
  76. }
  77. Gfx::IntRect GlyphMapWidget::get_outer_rect(int glyph) const
  78. {
  79. int row = glyph / columns();
  80. int column = glyph % columns();
  81. return Gfx::IntRect {
  82. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  83. row * (font().glyph_height() + m_vertical_spacing) + 1,
  84. font().max_glyph_width() + m_horizontal_spacing,
  85. font().glyph_height() + m_horizontal_spacing
  86. }
  87. .translated(frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value());
  88. }
  89. void GlyphMapWidget::update_glyph(int glyph)
  90. {
  91. update(get_outer_rect(glyph));
  92. }
  93. void GlyphMapWidget::paint_event(PaintEvent& event)
  94. {
  95. Frame::paint_event(event);
  96. Painter painter(*this);
  97. painter.add_clip_rect(widget_inner_rect());
  98. painter.add_clip_rect(event.rect());
  99. painter.set_font(font());
  100. painter.fill_rect(widget_inner_rect(), palette().inactive_window_title());
  101. int scroll_steps = vertical_scrollbar().value() / vertical_scrollbar().step();
  102. int first_visible_glyph = scroll_steps * columns();
  103. for (int glyph = first_visible_glyph; glyph <= first_visible_glyph + m_visible_glyphs && glyph < m_glyph_count; ++glyph) {
  104. Gfx::IntRect outer_rect = get_outer_rect(glyph);
  105. Gfx::IntRect inner_rect(
  106. outer_rect.x() + m_horizontal_spacing / 2,
  107. outer_rect.y() + m_vertical_spacing / 2,
  108. font().max_glyph_width(),
  109. font().glyph_height());
  110. if (m_selection.contains(glyph)) {
  111. painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
  112. if (font().contains_glyph(glyph))
  113. painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
  114. else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph))
  115. painter.draw_emoji(inner_rect.location(), *emoji, font());
  116. } else if (font().contains_glyph(glyph)) {
  117. painter.fill_rect(outer_rect, palette().base());
  118. painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
  119. } else if (auto* emoji = Gfx::Emoji::emoji_for_code_point(glyph)) {
  120. painter.fill_rect(outer_rect, Gfx::Color { 255, 150, 150 });
  121. painter.draw_emoji(inner_rect.location(), *emoji, font());
  122. }
  123. }
  124. painter.draw_focus_rect(get_outer_rect(m_active_glyph), Gfx::Color::Black);
  125. }
  126. void GlyphMapWidget::mousedown_event(MouseEvent& event)
  127. {
  128. Frame::mousedown_event(event);
  129. Gfx::IntPoint map_offset { frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value() };
  130. auto map_position = event.position() - map_offset;
  131. auto col = (map_position.x() - 1) / ((font().max_glyph_width() + m_horizontal_spacing));
  132. auto row = (map_position.y() - 1) / ((font().glyph_height() + m_vertical_spacing));
  133. auto glyph = row * columns() + col;
  134. if (row >= 0 && row < rows() && col >= 0 && col < columns() && glyph < m_glyph_count) {
  135. if (event.shift())
  136. m_selection.extend_to(glyph);
  137. else {
  138. m_selection.set_size(1);
  139. m_selection.set_start(glyph);
  140. }
  141. set_active_glyph(glyph, ShouldResetSelection::No);
  142. }
  143. }
  144. void GlyphMapWidget::keydown_event(KeyEvent& event)
  145. {
  146. Frame::keydown_event(event);
  147. if (!event.ctrl() && !event.shift()) {
  148. m_selection.set_size(1);
  149. m_selection.set_start(m_active_glyph);
  150. }
  151. if (event.key() == KeyCode::Key_Up) {
  152. if (m_selection.start() >= m_columns) {
  153. if (event.shift())
  154. m_selection.resize_by(-m_columns);
  155. else
  156. m_selection.set_start(m_selection.start() - m_columns);
  157. set_active_glyph(m_active_glyph - m_columns, ShouldResetSelection::No);
  158. scroll_to_glyph(m_active_glyph);
  159. return;
  160. }
  161. }
  162. if (event.key() == KeyCode::Key_Down) {
  163. if (m_selection.start() < m_glyph_count - m_columns) {
  164. if (event.shift())
  165. m_selection.resize_by(m_columns);
  166. else
  167. m_selection.set_start(m_selection.start() + m_columns);
  168. set_active_glyph(m_active_glyph + m_columns, ShouldResetSelection::No);
  169. scroll_to_glyph(m_active_glyph);
  170. return;
  171. }
  172. }
  173. if (event.key() == KeyCode::Key_Left) {
  174. if (m_selection.start() > 0) {
  175. if (event.shift())
  176. m_selection.resize_by(-1);
  177. else
  178. m_selection.set_start(m_selection.start() - 1);
  179. set_active_glyph(m_active_glyph - 1, ShouldResetSelection::No);
  180. scroll_to_glyph(m_active_glyph);
  181. return;
  182. }
  183. }
  184. if (event.key() == KeyCode::Key_Right) {
  185. if (m_selection.start() < m_glyph_count - 1) {
  186. if (event.shift())
  187. m_selection.resize_by(1);
  188. else
  189. m_selection.set_start(m_selection.start() + 1);
  190. set_active_glyph(m_active_glyph + 1, ShouldResetSelection::No);
  191. scroll_to_glyph(m_active_glyph);
  192. return;
  193. }
  194. }
  195. // FIXME: Support selection for these.
  196. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  197. set_active_glyph(0);
  198. scroll_to_glyph(m_active_glyph);
  199. return;
  200. }
  201. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  202. set_active_glyph(m_glyph_count - 1);
  203. scroll_to_glyph(m_active_glyph);
  204. return;
  205. }
  206. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  207. set_active_glyph(m_active_glyph / m_columns * m_columns);
  208. return;
  209. }
  210. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  211. int new_selection = m_active_glyph / m_columns * m_columns + (m_columns - 1);
  212. int max = m_glyph_count - 1;
  213. new_selection = clamp(new_selection, 0, max);
  214. set_active_glyph(new_selection);
  215. return;
  216. }
  217. }
  218. void GlyphMapWidget::did_change_font()
  219. {
  220. vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
  221. set_active_glyph('A');
  222. }
  223. void GlyphMapWidget::scroll_to_glyph(int glyph)
  224. {
  225. int row = glyph / columns();
  226. int column = glyph % columns();
  227. auto scroll_rect = Gfx::IntRect {
  228. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  229. row * (font().glyph_height() + m_vertical_spacing) + 1,
  230. font().max_glyph_width() + m_horizontal_spacing,
  231. font().glyph_height() + m_horizontal_spacing
  232. };
  233. scroll_into_view(scroll_rect, true, true);
  234. }
  235. }