GlyphMapWidget.h 2.8 KB

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