GTextEditor.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #pragma once
  2. #include <LibGUI/GWidget.h>
  3. #include <AK/Function.h>
  4. #include <AK/HashMap.h>
  5. class GScrollBar;
  6. class GTextPosition {
  7. public:
  8. GTextPosition() { }
  9. GTextPosition(int line, int column)
  10. : m_line(line)
  11. , m_column(column)
  12. {
  13. }
  14. bool is_valid() const { return m_line >= 0 && m_column >= 0; }
  15. int line() const { return m_line; }
  16. int column() const { return m_column; }
  17. void set_line(int line) { m_line = line; }
  18. void set_column(int column) { m_column = column; }
  19. bool operator==(const GTextPosition& other) const { return m_line == other.m_line && m_column == other.m_column; }
  20. bool operator!=(const GTextPosition& other) const { return m_line != other.m_line || m_column != other.m_column; }
  21. bool operator<(const GTextPosition& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }
  22. private:
  23. int m_line { -1 };
  24. int m_column { -1 };
  25. };
  26. class GTextRange {
  27. public:
  28. GTextRange() { }
  29. GTextRange(const GTextPosition& start, const GTextPosition& end) : m_start(start) , m_end(end) { }
  30. bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
  31. void clear() { m_start = { }; m_end = { }; }
  32. GTextPosition& start() { return m_start; }
  33. GTextPosition& end() { return m_end; }
  34. const GTextPosition& start() const { return m_start; }
  35. const GTextPosition& end() const { return m_end; }
  36. GTextRange normalized() const { return GTextRange(normalized_start(), normalized_end()); }
  37. void set_start(const GTextPosition& position) { m_start = position; }
  38. void set_end(const GTextPosition& position) { m_end = position; }
  39. void set(const GTextPosition& start, const GTextPosition& end) { m_start = start; m_end = end; }
  40. private:
  41. GTextPosition normalized_start() const { return m_start < m_end ? m_start : m_end; }
  42. GTextPosition normalized_end() const { return m_start < m_end ? m_end : m_start; }
  43. GTextPosition m_start;
  44. GTextPosition m_end;
  45. };
  46. class GTextEditor : public GWidget {
  47. public:
  48. explicit GTextEditor(GWidget* parent);
  49. virtual ~GTextEditor() override;
  50. Function<void(GTextEditor&)> on_cursor_change;
  51. void set_text(const String&);
  52. int content_width() const;
  53. int content_height() const;
  54. Rect visible_content_rect() const;
  55. void scroll_cursor_into_view();
  56. int line_count() const { return m_lines.size(); }
  57. int line_spacing() const { return m_line_spacing; }
  58. int line_height() const { return font().glyph_height() + m_line_spacing; }
  59. int padding() const { return 3; }
  60. GTextPosition cursor() const { return m_cursor; }
  61. GTextRange normalized_selection() const { return m_selection.normalized(); }
  62. int glyph_width() const { return font().glyph_width('x'); }
  63. bool write_to_file(const String& path);
  64. bool has_selection() const { return m_selection.is_valid(); }
  65. String selected_text() const;
  66. void cut();
  67. void copy();
  68. void paste();
  69. private:
  70. virtual void paint_event(GPaintEvent&) override;
  71. virtual void resize_event(GResizeEvent&) override;
  72. virtual void mousedown_event(GMouseEvent&) override;
  73. virtual void mouseup_event(GMouseEvent&) override;
  74. virtual void mousemove_event(GMouseEvent&) override;
  75. virtual void keydown_event(GKeyEvent&) override;
  76. virtual void focusin_event(GEvent&) override;
  77. virtual void focusout_event(GEvent&) override;
  78. virtual void timer_event(GTimerEvent&) override;
  79. virtual bool accepts_focus() const override { return true; }
  80. class Line {
  81. friend class GTextEditor;
  82. public:
  83. Line();
  84. const char* characters() const { return m_text.data(); }
  85. int length() const { return m_text.size() - 1; }
  86. int width(const Font&) const;
  87. void set_text(const String&);
  88. void append(char);
  89. void prepend(char);
  90. void insert(int index, char);
  91. void remove(int index);
  92. void append(const char*, int);
  93. void truncate(int length);
  94. void clear();
  95. private:
  96. // NOTE: This vector is null terminated.
  97. Vector<char> m_text;
  98. };
  99. void update_scrollbar_ranges();
  100. Rect line_content_rect(int item_index) const;
  101. Rect line_widget_rect(int line_index) const;
  102. Rect cursor_content_rect() const;
  103. void update_cursor();
  104. void set_cursor(int line, int column);
  105. void set_cursor(const GTextPosition&);
  106. Line& current_line() { return *m_lines[m_cursor.line()]; }
  107. const Line& current_line() const { return *m_lines[m_cursor.line()]; }
  108. GTextPosition text_position_at(const Point&) const;
  109. void insert_at_cursor(char);
  110. void insert_at_cursor(const String&);
  111. int ruler_width() const;
  112. Rect ruler_content_rect(int line) const;
  113. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  114. void insert_at_cursor_or_replace_selection(const String&);
  115. void delete_selection();
  116. GScrollBar* m_vertical_scrollbar { nullptr };
  117. GScrollBar* m_horizontal_scrollbar { nullptr };
  118. Vector<OwnPtr<Line>> m_lines;
  119. GTextPosition m_cursor;
  120. bool m_cursor_state { true };
  121. bool m_in_drag_select { false };
  122. int m_line_spacing { 2 };
  123. GTextRange m_selection;
  124. };