GlyphMapWidget.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. class Selection {
  20. public:
  21. Selection() = default;
  22. Selection(int start, int size)
  23. : m_start(start)
  24. , m_size(size)
  25. {
  26. }
  27. int size() const { return m_size; }
  28. void set_size(int i) { m_size = i; }
  29. int start() const { return m_start; }
  30. void set_start(int i) { m_start = i; }
  31. Selection normalized() const;
  32. bool contains(int) const;
  33. void resize_by(int i);
  34. void extend_to(int);
  35. private:
  36. int m_start { 0 };
  37. int m_size { 1 };
  38. };
  39. Selection selection() const { return m_selection; }
  40. int active_glyph() const { return m_active_glyph; }
  41. enum class ShouldResetSelection {
  42. Yes,
  43. No
  44. };
  45. void set_active_range(Unicode::CodePointRange);
  46. void set_active_glyph(int, ShouldResetSelection = ShouldResetSelection::Yes);
  47. void set_selection(int start, int size, Optional<u32> active_glyph = {});
  48. void clear_selection() { m_selection.set_size(0); }
  49. void scroll_to_glyph(int);
  50. void update_glyph(int);
  51. void select_previous_existing_glyph();
  52. void select_next_existing_glyph();
  53. int rows() const { return m_rows; }
  54. int columns() const { return m_columns; }
  55. Function<void(int)> on_active_glyph_changed;
  56. Function<void(int)> on_glyph_double_clicked;
  57. Function<void(ContextMenuEvent&)> on_context_menu_request;
  58. private:
  59. GlyphMapWidget();
  60. virtual void paint_event(PaintEvent&) override;
  61. virtual void mousedown_event(MouseEvent&) override;
  62. virtual void mouseup_event(GUI::MouseEvent&) override;
  63. virtual void mousemove_event(GUI::MouseEvent&) override;
  64. virtual void doubleclick_event(MouseEvent&) override;
  65. virtual void keydown_event(KeyEvent&) override;
  66. virtual void resize_event(ResizeEvent&) override;
  67. virtual void did_change_font() override;
  68. virtual void context_menu_event(ContextMenuEvent&) override;
  69. Gfx::IntRect get_outer_rect(int glyph) const;
  70. Optional<int> glyph_at_position(Gfx::IntPoint) const;
  71. int glyph_at_position_clamped(Gfx::IntPoint) const;
  72. void recalculate_content_size();
  73. int m_glyph_count { 0x110000 };
  74. int m_columns { 0 };
  75. int m_rows { 0 };
  76. int m_horizontal_spacing { 2 };
  77. int m_vertical_spacing { 2 };
  78. Selection m_selection;
  79. int m_active_glyph { 0 };
  80. int m_visible_glyphs { 0 };
  81. bool m_in_drag_select { false };
  82. Unicode::CodePointRange m_active_range { 0x0000, 0x10FFFF };
  83. RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
  84. Gfx::IntPoint m_last_mousemove_position;
  85. };
  86. }