GlyphMapWidget.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <LibGUI/AbstractScrollableWidget.h>
  9. #include <LibGUI/TextRange.h>
  10. #include <LibGfx/BitmapFont.h>
  11. namespace GUI {
  12. class GlyphMapWidget final : public AbstractScrollableWidget {
  13. C_OBJECT(GlyphMapWidget)
  14. public:
  15. virtual ~GlyphMapWidget() override;
  16. class Selection {
  17. public:
  18. Selection() = default;
  19. Selection(int start, int size)
  20. : m_start(start)
  21. , m_size(size)
  22. {
  23. }
  24. int size() const { return m_size; }
  25. void set_size(int i) { m_size = i; }
  26. int start() const { return m_start; }
  27. void set_start(int i) { m_start = i; }
  28. Selection normalized() const;
  29. bool contains(int) const;
  30. void resize_by(int i);
  31. void extend_to(int);
  32. private:
  33. int m_start { 0 };
  34. int m_size { 1 };
  35. };
  36. Selection selection() const { return m_selection; }
  37. int active_glyph() const { return m_active_glyph; }
  38. enum class ShouldResetSelection {
  39. Yes,
  40. No
  41. };
  42. void set_active_glyph(int, ShouldResetSelection = ShouldResetSelection::Yes);
  43. void clear_selection() { m_selection.set_size(0); }
  44. void scroll_to_glyph(int);
  45. void update_glyph(int);
  46. void select_previous_existing_glyph();
  47. void select_next_existing_glyph();
  48. int rows() const { return m_rows; }
  49. int columns() const { return m_columns; }
  50. Function<void(int)> on_active_glyph_changed;
  51. Function<void(int)> on_glyph_double_clicked;
  52. private:
  53. GlyphMapWidget();
  54. virtual void paint_event(PaintEvent&) override;
  55. virtual void mousedown_event(MouseEvent&) override;
  56. virtual void mouseup_event(GUI::MouseEvent&) override;
  57. virtual void mousemove_event(GUI::MouseEvent&) override;
  58. virtual void doubleclick_event(MouseEvent&) override;
  59. virtual void keydown_event(KeyEvent&) override;
  60. virtual void resize_event(ResizeEvent&) override;
  61. virtual void did_change_font() override;
  62. Gfx::IntRect get_outer_rect(int glyph) const;
  63. Optional<int> glyph_at_position(Gfx::IntPoint) const;
  64. void recalculate_content_size();
  65. int m_glyph_count { 0x110000 };
  66. int m_columns { 0 };
  67. int m_rows { 0 };
  68. int m_horizontal_spacing { 2 };
  69. int m_vertical_spacing { 2 };
  70. Selection m_selection;
  71. int m_active_glyph { 0 };
  72. int m_visible_glyphs { 0 };
  73. bool m_in_drag_select { false };
  74. };
  75. }