GlyphMapWidget.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "GlyphMapWidget.h"
  2. #include <LibGUI/GPainter.h>
  3. GlyphMapWidget::GlyphMapWidget(Font& mutable_font, GWidget* parent)
  4. : GFrame(parent)
  5. , m_font(mutable_font)
  6. {
  7. set_frame_thickness(2);
  8. set_frame_shape(FrameShape::Container);
  9. set_frame_shadow(FrameShadow::Sunken);
  10. set_relative_rect({ 0, 0, preferred_width(), preferred_height() });
  11. }
  12. GlyphMapWidget::~GlyphMapWidget()
  13. {
  14. }
  15. int GlyphMapWidget::preferred_width() const
  16. {
  17. return columns() * (font().max_glyph_width() + m_horizontal_spacing) + 2 + frame_thickness() * 2;
  18. }
  19. int GlyphMapWidget::preferred_height() const
  20. {
  21. return rows() * (font().glyph_height() + m_vertical_spacing) + 2 + frame_thickness() * 2;
  22. }
  23. void GlyphMapWidget::set_selected_glyph(byte glyph)
  24. {
  25. if (m_selected_glyph == glyph)
  26. return;
  27. m_selected_glyph = glyph;
  28. if (on_glyph_selected)
  29. on_glyph_selected(glyph);
  30. update();
  31. }
  32. Rect GlyphMapWidget::get_outer_rect(byte glyph) const
  33. {
  34. int row = glyph / columns();
  35. int column = glyph % columns();
  36. return Rect {
  37. column * (font().max_glyph_width() + m_horizontal_spacing) + 1,
  38. row * (font().glyph_height() + m_vertical_spacing) + 1,
  39. font().max_glyph_width() + m_horizontal_spacing,
  40. font().glyph_height() + m_horizontal_spacing
  41. }
  42. .translated(frame_thickness(), frame_thickness());
  43. }
  44. void GlyphMapWidget::update_glyph(byte glyph)
  45. {
  46. update(get_outer_rect(glyph));
  47. }
  48. void GlyphMapWidget::paint_event(GPaintEvent& event)
  49. {
  50. GFrame::paint_event(event);
  51. GPainter painter(*this);
  52. painter.add_clip_rect(event.rect());
  53. painter.set_font(font());
  54. painter.fill_rect(frame_inner_rect(), Color::White);
  55. byte glyph = 0;
  56. for (int row = 0; row < rows(); ++row) {
  57. for (int column = 0; column < columns(); ++column, ++glyph) {
  58. Rect outer_rect = get_outer_rect(glyph);
  59. Rect inner_rect(
  60. outer_rect.x() + m_horizontal_spacing / 2,
  61. outer_rect.y() + m_vertical_spacing / 2,
  62. font().max_glyph_width(),
  63. font().glyph_height());
  64. if (glyph == m_selected_glyph) {
  65. painter.fill_rect(outer_rect, Color::from_rgb(0x84351a));
  66. painter.draw_glyph(inner_rect.location(), glyph, Color::White);
  67. } else {
  68. painter.draw_glyph(inner_rect.location(), glyph, Color::Black);
  69. }
  70. }
  71. }
  72. }
  73. void GlyphMapWidget::mousedown_event(GMouseEvent& event)
  74. {
  75. // FIXME: This is a silly loop.
  76. for (unsigned glyph = 0; glyph < 256; ++glyph) {
  77. if (get_outer_rect(glyph).contains(event.position())) {
  78. set_selected_glyph(glyph);
  79. break;
  80. }
  81. }
  82. }