GlyphMapWidget.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "GlyphMapWidget.h"
  7. #include <LibGUI/Painter.h>
  8. #include <LibGfx/BitmapFont.h>
  9. #include <LibGfx/Palette.h>
  10. GlyphMapWidget::GlyphMapWidget()
  11. {
  12. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  13. horizontal_scrollbar().set_visible(false);
  14. }
  15. GlyphMapWidget::~GlyphMapWidget()
  16. {
  17. }
  18. void GlyphMapWidget::initialize(Gfx::BitmapFont& mutable_font)
  19. {
  20. if (m_font == mutable_font)
  21. return;
  22. m_font = mutable_font;
  23. m_glyph_count = mutable_font.glyph_count();
  24. m_selected_glyph = 0;
  25. vertical_scrollbar().set_step(font().glyph_height() + m_vertical_spacing);
  26. }
  27. void GlyphMapWidget::resize_event(GUI::ResizeEvent& event)
  28. {
  29. int event_width = event.size().width() - this->vertical_scrollbar().width() - (frame_thickness() * 2) - m_horizontal_spacing;
  30. m_columns = max(event_width / (font().max_glyph_width() + m_horizontal_spacing), 1);
  31. m_rows = ceil_div(m_glyph_count, m_columns);
  32. int content_width = columns() * (font().max_glyph_width() + m_horizontal_spacing);
  33. int content_height = rows() * (font().glyph_height() + m_vertical_spacing) + frame_thickness();
  34. set_content_size({ content_width, content_height });
  35. ScrollableWidget::resize_event(event);
  36. }
  37. void GlyphMapWidget::set_selected_glyph(int glyph)
  38. {
  39. if (m_selected_glyph == glyph)
  40. return;
  41. m_selected_glyph = glyph;
  42. if (on_glyph_selected)
  43. on_glyph_selected(glyph);
  44. update();
  45. }
  46. Gfx::IntRect GlyphMapWidget::get_outer_rect(int glyph) const
  47. {
  48. int row = glyph / columns();
  49. int column = glyph % columns();
  50. return Gfx::IntRect {
  51. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  52. row * (font().glyph_height() + m_vertical_spacing) + 1,
  53. font().max_glyph_width() + m_horizontal_spacing,
  54. font().glyph_height() + m_horizontal_spacing
  55. }
  56. .translated(frame_thickness() - horizontal_scrollbar().value(), frame_thickness() - vertical_scrollbar().value());
  57. }
  58. void GlyphMapWidget::update_glyph(int glyph)
  59. {
  60. update(get_outer_rect(glyph));
  61. }
  62. void GlyphMapWidget::reprobe_font()
  63. {
  64. VERIFY(m_font);
  65. m_glyph_count = m_font->glyph_count();
  66. m_selected_glyph = 0;
  67. update();
  68. }
  69. void GlyphMapWidget::paint_event(GUI::PaintEvent& event)
  70. {
  71. GUI::Frame::paint_event(event);
  72. GUI::Painter painter(*this);
  73. painter.add_clip_rect(widget_inner_rect());
  74. painter.add_clip_rect(event.rect());
  75. painter.set_font(font());
  76. painter.fill_rect(widget_inner_rect(), palette().inactive_window_title());
  77. for (int glyph = 0; glyph < m_glyph_count; ++glyph) {
  78. Gfx::IntRect outer_rect = get_outer_rect(glyph);
  79. Gfx::IntRect inner_rect(
  80. outer_rect.x() + m_horizontal_spacing / 2,
  81. outer_rect.y() + m_vertical_spacing / 2,
  82. font().max_glyph_width(),
  83. font().glyph_height());
  84. if (glyph == m_selected_glyph) {
  85. painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
  86. if (m_font->contains_glyph(glyph))
  87. painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
  88. } else if (m_font->contains_glyph(glyph)) {
  89. painter.fill_rect(outer_rect, palette().base());
  90. painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
  91. }
  92. }
  93. }
  94. void GlyphMapWidget::mousedown_event(GUI::MouseEvent& event)
  95. {
  96. GUI::Frame::mousedown_event(event);
  97. // FIXME: This is a silly loop.
  98. for (int glyph = 0; glyph < m_glyph_count; ++glyph) {
  99. if (get_outer_rect(glyph).contains(event.position())) {
  100. set_selected_glyph(glyph);
  101. break;
  102. }
  103. }
  104. }
  105. void GlyphMapWidget::keydown_event(GUI::KeyEvent& event)
  106. {
  107. GUI::Frame::keydown_event(event);
  108. if (event.key() == KeyCode::Key_Up) {
  109. if (selected_glyph() >= m_columns) {
  110. set_selected_glyph(selected_glyph() - m_columns);
  111. scroll_to_glyph(selected_glyph());
  112. return;
  113. }
  114. }
  115. if (event.key() == KeyCode::Key_Down) {
  116. if (selected_glyph() < m_glyph_count - m_columns) {
  117. set_selected_glyph(selected_glyph() + m_columns);
  118. scroll_to_glyph(selected_glyph());
  119. return;
  120. }
  121. }
  122. if (event.key() == KeyCode::Key_Left) {
  123. if (selected_glyph() > 0) {
  124. set_selected_glyph(selected_glyph() - 1);
  125. scroll_to_glyph(selected_glyph());
  126. return;
  127. }
  128. }
  129. if (event.key() == KeyCode::Key_Right) {
  130. if (selected_glyph() < m_glyph_count - 1) {
  131. set_selected_glyph(selected_glyph() + 1);
  132. scroll_to_glyph(selected_glyph());
  133. return;
  134. }
  135. }
  136. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  137. set_selected_glyph(0);
  138. scroll_to_glyph(selected_glyph());
  139. return;
  140. }
  141. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  142. set_selected_glyph(m_glyph_count - 1);
  143. scroll_to_glyph(selected_glyph());
  144. return;
  145. }
  146. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  147. set_selected_glyph(selected_glyph() / m_columns * m_columns);
  148. return;
  149. }
  150. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  151. int new_selection = selected_glyph() / m_columns * m_columns + (m_columns - 1);
  152. int max = m_glyph_count - 1;
  153. new_selection = clamp(new_selection, 0, max);
  154. set_selected_glyph(new_selection);
  155. return;
  156. }
  157. }
  158. void GlyphMapWidget::scroll_to_glyph(int glyph)
  159. {
  160. int row = glyph / columns();
  161. int column = glyph % columns();
  162. auto scroll_rect = Gfx::IntRect {
  163. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  164. row * (font().glyph_height() + m_vertical_spacing) + 1,
  165. font().max_glyph_width() + m_horizontal_spacing,
  166. font().glyph_height() + m_horizontal_spacing
  167. };
  168. scroll_into_view(scroll_rect, true, true);
  169. }