GlyphMapWidget.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 "GlyphMapWidget.h"
  27. #include <LibGUI/Painter.h>
  28. #include <LibGfx/BitmapFont.h>
  29. #include <LibGfx/Palette.h>
  30. GlyphMapWidget::GlyphMapWidget(Gfx::BitmapFont& mutable_font)
  31. : m_font(mutable_font)
  32. {
  33. m_glyph_count = mutable_font.glyph_count();
  34. set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
  35. set_focus_policy(GUI::FocusPolicy::StrongFocus);
  36. }
  37. GlyphMapWidget::~GlyphMapWidget()
  38. {
  39. }
  40. int GlyphMapWidget::preferred_width() const
  41. {
  42. return columns() * (font().max_glyph_width() + m_horizontal_spacing) + 2 + frame_thickness() * 2;
  43. }
  44. int GlyphMapWidget::preferred_height() const
  45. {
  46. return rows() * (font().glyph_height() + m_vertical_spacing) + 2 + frame_thickness() * 2;
  47. }
  48. void GlyphMapWidget::set_selected_glyph(int glyph)
  49. {
  50. if (m_selected_glyph == glyph)
  51. return;
  52. m_selected_glyph = glyph;
  53. if (on_glyph_selected)
  54. on_glyph_selected(glyph);
  55. update();
  56. }
  57. Gfx::IntRect GlyphMapWidget::get_outer_rect(int glyph) const
  58. {
  59. int row = glyph / columns();
  60. int column = glyph % columns();
  61. return Gfx::IntRect {
  62. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  63. row * (font().glyph_height() + m_vertical_spacing) + 1,
  64. font().max_glyph_width() + m_horizontal_spacing,
  65. font().glyph_height() + m_horizontal_spacing
  66. }
  67. .translated(frame_thickness(), frame_thickness());
  68. }
  69. void GlyphMapWidget::update_glyph(int glyph)
  70. {
  71. update(get_outer_rect(glyph));
  72. }
  73. void GlyphMapWidget::paint_event(GUI::PaintEvent& event)
  74. {
  75. GUI::Frame::paint_event(event);
  76. GUI::Painter painter(*this);
  77. painter.add_clip_rect(event.rect());
  78. painter.set_font(font());
  79. painter.fill_rect(frame_inner_rect(), palette().base());
  80. for (int glyph = 0; glyph < m_glyph_count; ++glyph) {
  81. Gfx::IntRect outer_rect = get_outer_rect(glyph);
  82. Gfx::IntRect inner_rect(
  83. outer_rect.x() + m_horizontal_spacing / 2,
  84. outer_rect.y() + m_vertical_spacing / 2,
  85. font().max_glyph_width(),
  86. font().glyph_height());
  87. if (glyph == m_selected_glyph) {
  88. painter.fill_rect(outer_rect, is_focused() ? palette().selection() : palette().inactive_selection());
  89. painter.draw_glyph(inner_rect.location(), glyph, is_focused() ? palette().selection_text() : palette().inactive_selection_text());
  90. } else {
  91. painter.draw_glyph(inner_rect.location(), glyph, palette().base_text());
  92. }
  93. }
  94. }
  95. void GlyphMapWidget::mousedown_event(GUI::MouseEvent& event)
  96. {
  97. GUI::Frame::mousedown_event(event);
  98. // FIXME: This is a silly loop.
  99. for (int glyph = 0; glyph < m_glyph_count; ++glyph) {
  100. if (get_outer_rect(glyph).contains(event.position())) {
  101. set_selected_glyph(glyph);
  102. break;
  103. }
  104. }
  105. }
  106. void GlyphMapWidget::keydown_event(GUI::KeyEvent& event)
  107. {
  108. GUI::Frame::keydown_event(event);
  109. if (event.key() == KeyCode::Key_Up) {
  110. if (selected_glyph() >= m_columns) {
  111. set_selected_glyph(selected_glyph() - m_columns);
  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. return;
  119. }
  120. }
  121. if (event.key() == KeyCode::Key_Left) {
  122. if (selected_glyph() > 0) {
  123. set_selected_glyph(selected_glyph() - 1);
  124. return;
  125. }
  126. }
  127. if (event.key() == KeyCode::Key_Right) {
  128. if (selected_glyph() < m_glyph_count - 1) {
  129. set_selected_glyph(selected_glyph() + 1);
  130. return;
  131. }
  132. }
  133. if (event.ctrl() && event.key() == KeyCode::Key_Home) {
  134. set_selected_glyph(0);
  135. return;
  136. }
  137. if (event.ctrl() && event.key() == KeyCode::Key_End) {
  138. set_selected_glyph(m_glyph_count - 1);
  139. return;
  140. }
  141. if (!event.ctrl() && event.key() == KeyCode::Key_Home) {
  142. set_selected_glyph(selected_glyph() / m_columns * m_columns);
  143. return;
  144. }
  145. if (!event.ctrl() && event.key() == KeyCode::Key_End) {
  146. int new_selection = selected_glyph() / m_columns * m_columns + (m_columns - 1);
  147. int max = m_glyph_count - 1;
  148. new_selection = clamp(new_selection, 0, max);
  149. set_selected_glyph(new_selection);
  150. return;
  151. }
  152. }