GlyphMapWidget.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 <LibCore/Timer.h>
  10. #include <LibGUI/AbstractScrollableWidget.h>
  11. #include <LibGUI/TextRange.h>
  12. #include <LibGfx/Font/BitmapFont.h>
  13. #include <LibUnicode/CharacterTypes.h>
  14. namespace GUI {
  15. class GlyphMapWidget final : public AbstractScrollableWidget {
  16. C_OBJECT(GlyphMapWidget)
  17. public:
  18. virtual ~GlyphMapWidget() override = default;
  19. void set_font(Gfx::Font const&);
  20. class Selection {
  21. public:
  22. Selection() = default;
  23. Selection(int start, int size)
  24. : m_start(start)
  25. , m_size(size)
  26. {
  27. }
  28. int size() const { return m_size; }
  29. void set_size(int i) { m_size = i; }
  30. int start() const { return m_start; }
  31. void set_start(int i) { m_start = i; }
  32. Selection normalized() const;
  33. bool contains(int) const;
  34. void resize_by(int i);
  35. void extend_to(int);
  36. private:
  37. int m_start { 0 };
  38. int m_size { 1 };
  39. };
  40. Selection selection() const { return m_selection; }
  41. int active_glyph() const { return m_active_glyph; }
  42. enum class ShouldResetSelection {
  43. Yes,
  44. No
  45. };
  46. void set_active_range(Unicode::CodePointRange);
  47. void set_active_glyph(int, ShouldResetSelection = ShouldResetSelection::Yes);
  48. void set_selection(int start, int size, Optional<u32> active_glyph = {});
  49. void clear_selection() { m_selection.set_size(0); }
  50. void scroll_to_glyph(int);
  51. void update_glyph(int);
  52. void set_highlight_modifications(bool);
  53. void set_glyph_modified(u32 glyph, bool modified);
  54. bool glyph_is_modified(u32 glyph);
  55. void select_previous_existing_glyph();
  56. void select_next_existing_glyph();
  57. int rows() const { return m_rows; }
  58. int columns() const { return m_columns; }
  59. Function<void(int)> on_active_glyph_changed;
  60. Function<void(int)> on_glyph_double_clicked;
  61. Function<void(ContextMenuEvent&)> on_context_menu_request;
  62. private:
  63. GlyphMapWidget();
  64. virtual void paint_event(PaintEvent&) override;
  65. virtual void mousedown_event(MouseEvent&) override;
  66. virtual void mouseup_event(GUI::MouseEvent&) override;
  67. virtual void mousemove_event(GUI::MouseEvent&) override;
  68. virtual void doubleclick_event(MouseEvent&) override;
  69. virtual void keydown_event(KeyEvent&) override;
  70. virtual void resize_event(ResizeEvent&) override;
  71. virtual void did_change_font() override;
  72. virtual void context_menu_event(ContextMenuEvent&) override;
  73. Gfx::IntRect get_outer_rect(int glyph) const;
  74. Optional<int> glyph_at_position(Gfx::IntPoint) const;
  75. int glyph_at_position_clamped(Gfx::IntPoint) const;
  76. void recalculate_content_size();
  77. RefPtr<Gfx::Font> m_original_font;
  78. int m_glyph_count { 0x110000 };
  79. int m_columns { 0 };
  80. int m_rows { 0 };
  81. int m_horizontal_spacing { 2 };
  82. int m_vertical_spacing { 2 };
  83. Selection m_selection;
  84. int m_active_glyph { 0 };
  85. int m_visible_glyphs { 0 };
  86. bool m_in_drag_select { false };
  87. bool m_highlight_modifications { false };
  88. HashTable<u32> m_modified_glyphs;
  89. Unicode::CodePointRange m_active_range { 0x0000, 0x10FFFF };
  90. RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
  91. Gfx::IntPoint m_last_mousemove_position;
  92. };
  93. }