GTextEditor.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #pragma once
  2. #include <LibGUI/GScrollableWidget.h>
  3. #include <AK/Function.h>
  4. #include <AK/HashMap.h>
  5. class GScrollBar;
  6. class Painter;
  7. class GTextPosition {
  8. public:
  9. GTextPosition() { }
  10. GTextPosition(int line, int column)
  11. : m_line(line)
  12. , m_column(column)
  13. {
  14. }
  15. bool is_valid() const { return m_line >= 0 && m_column >= 0; }
  16. int line() const { return m_line; }
  17. int column() const { return m_column; }
  18. void set_line(int line) { m_line = line; }
  19. void set_column(int column) { m_column = 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_column != other.m_column; }
  22. bool operator<(const GTextPosition& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }
  23. private:
  24. int m_line { -1 };
  25. int m_column { -1 };
  26. };
  27. class GTextRange {
  28. public:
  29. GTextRange() { }
  30. GTextRange(const GTextPosition& start, const GTextPosition& end) : m_start(start) , m_end(end) { }
  31. bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
  32. void clear() { m_start = { }; m_end = { }; }
  33. GTextPosition& start() { return m_start; }
  34. GTextPosition& end() { return m_end; }
  35. const GTextPosition& start() const { return m_start; }
  36. const GTextPosition& end() const { return m_end; }
  37. GTextRange normalized() const { return GTextRange(normalized_start(), normalized_end()); }
  38. void set_start(const GTextPosition& position) { m_start = position; }
  39. void set_end(const GTextPosition& position) { m_end = position; }
  40. void set(const GTextPosition& start, const GTextPosition& end) { m_start = start; m_end = end; }
  41. private:
  42. GTextPosition normalized_start() const { return m_start < m_end ? m_start : m_end; }
  43. GTextPosition normalized_end() const { return m_start < m_end ? m_end : m_start; }
  44. GTextPosition m_start;
  45. GTextPosition m_end;
  46. };
  47. class GTextEditor : public GScrollableWidget {
  48. public:
  49. enum Type { MultiLine, SingleLine };
  50. GTextEditor(Type, GWidget* parent);
  51. virtual ~GTextEditor() override;
  52. Type type() const { return m_type; }
  53. bool is_single_line() const { return m_type == SingleLine; }
  54. bool is_multi_line() const { return m_type == MultiLine; }
  55. bool is_ruler_visible() const { return m_ruler_visible; }
  56. void set_ruler_visible(bool b) { m_ruler_visible = b; }
  57. Function<void()> on_cursor_change;
  58. Function<void()> on_selection_change;
  59. void set_text(const String&);
  60. void scroll_cursor_into_view();
  61. int line_count() const { return m_lines.size(); }
  62. int line_spacing() const { return m_line_spacing; }
  63. int line_height() const { return font().glyph_height() + m_line_spacing; }
  64. GTextPosition cursor() const { return m_cursor; }
  65. GTextRange normalized_selection() const { return m_selection.normalized(); }
  66. int glyph_width() const { return font().glyph_width('x'); }
  67. bool write_to_file(const String& path);
  68. bool has_selection() const { return m_selection.is_valid(); }
  69. String selected_text() const;
  70. String text() const;
  71. void clear();
  72. void cut();
  73. void copy();
  74. void paste();
  75. void do_delete();
  76. void delete_current_line();
  77. Function<void()> on_change;
  78. Function<void()> on_return_pressed;
  79. Function<void()> on_escape_pressed;
  80. virtual const char* class_name() const override { return "GTextEditor"; }
  81. private:
  82. virtual void paint_event(GPaintEvent&) override;
  83. virtual void mousedown_event(GMouseEvent&) override;
  84. virtual void mouseup_event(GMouseEvent&) override;
  85. virtual void mousemove_event(GMouseEvent&) override;
  86. virtual void keydown_event(GKeyEvent&) override;
  87. virtual void focusin_event(CEvent&) override;
  88. virtual void focusout_event(CEvent&) override;
  89. virtual void timer_event(CTimerEvent&) override;
  90. virtual bool accepts_focus() const override { return true; }
  91. virtual void enter_event(CEvent&) override;
  92. virtual void leave_event(CEvent&) override;
  93. void paint_ruler(Painter&);
  94. void update_content_size();
  95. void did_change();
  96. class Line {
  97. friend class GTextEditor;
  98. public:
  99. Line();
  100. const char* characters() const { return m_text.data(); }
  101. int length() const { return m_text.size() - 1; }
  102. int width(const Font&) const;
  103. void set_text(const String&);
  104. void append(char);
  105. void prepend(char);
  106. void insert(int index, char);
  107. void remove(int index);
  108. void append(const char*, int);
  109. void truncate(int length);
  110. void clear();
  111. private:
  112. // NOTE: This vector is null terminated.
  113. Vector<char> m_text;
  114. };
  115. Rect line_content_rect(int item_index) const;
  116. Rect line_widget_rect(int line_index) const;
  117. Rect cursor_content_rect() const;
  118. void update_cursor();
  119. void set_cursor(int line, int column);
  120. void set_cursor(const GTextPosition&);
  121. Line& current_line() { return *m_lines[m_cursor.line()]; }
  122. const Line& current_line() const { return *m_lines[m_cursor.line()]; }
  123. GTextPosition text_position_at(const Point&) const;
  124. void insert_at_cursor(char);
  125. void insert_at_cursor(const String&);
  126. int ruler_width() const;
  127. Rect ruler_content_rect(int line) const;
  128. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  129. void insert_at_cursor_or_replace_selection(const String&);
  130. void delete_selection();
  131. void did_update_selection();
  132. Type m_type { MultiLine };
  133. Vector<OwnPtr<Line>> m_lines;
  134. GTextPosition m_cursor;
  135. bool m_cursor_state { true };
  136. bool m_in_drag_select { false };
  137. bool m_ruler_visible { true };
  138. bool m_have_pending_change_notification { false };
  139. int m_line_spacing { 4 };
  140. int m_soft_tab_width { 4 };
  141. int m_horizontal_content_padding { 2 };
  142. GTextRange m_selection;
  143. };