GTextEditor.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. #include <AK/Function.h>
  3. #include <AK/HashMap.h>
  4. #include <LibGUI/GScrollableWidget.h>
  5. #include <LibDraw/TextAlignment.h>
  6. class GAction;
  7. class GMenu;
  8. class GScrollBar;
  9. class Painter;
  10. class GTextPosition {
  11. public:
  12. GTextPosition() {}
  13. GTextPosition(int line, int column)
  14. : m_line(line)
  15. , m_column(column)
  16. {
  17. }
  18. bool is_valid() const { return m_line >= 0 && m_column >= 0; }
  19. int line() const { return m_line; }
  20. int column() const { return m_column; }
  21. void set_line(int line) { m_line = line; }
  22. void set_column(int column) { m_column = column; }
  23. bool operator==(const GTextPosition& other) const { return m_line == other.m_line && m_column == other.m_column; }
  24. bool operator!=(const GTextPosition& other) const { return m_line != other.m_line || m_column != other.m_column; }
  25. bool operator<(const GTextPosition& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }
  26. private:
  27. int m_line { -1 };
  28. int m_column { -1 };
  29. };
  30. class GTextRange {
  31. public:
  32. GTextRange() {}
  33. GTextRange(const GTextPosition& start, const GTextPosition& end)
  34. : m_start(start)
  35. , m_end(end)
  36. {
  37. }
  38. bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
  39. void clear()
  40. {
  41. m_start = {};
  42. m_end = {};
  43. }
  44. GTextPosition& start() { return m_start; }
  45. GTextPosition& end() { return m_end; }
  46. const GTextPosition& start() const { return m_start; }
  47. const GTextPosition& end() const { return m_end; }
  48. GTextRange normalized() const { return GTextRange(normalized_start(), normalized_end()); }
  49. void set_start(const GTextPosition& position) { m_start = position; }
  50. void set_end(const GTextPosition& position) { m_end = position; }
  51. void set(const GTextPosition& start, const GTextPosition& end)
  52. {
  53. m_start = start;
  54. m_end = end;
  55. }
  56. private:
  57. GTextPosition normalized_start() const { return m_start < m_end ? m_start : m_end; }
  58. GTextPosition normalized_end() const { return m_start < m_end ? m_end : m_start; }
  59. GTextPosition m_start;
  60. GTextPosition m_end;
  61. };
  62. class GTextEditor : public GScrollableWidget {
  63. public:
  64. enum Type {
  65. MultiLine,
  66. SingleLine
  67. };
  68. GTextEditor(Type, GWidget* parent);
  69. virtual ~GTextEditor() override;
  70. bool is_readonly() const { return m_readonly; }
  71. void set_readonly(bool);
  72. bool is_automatic_indentation() const { return m_automatic_indentation_enabled; }
  73. void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }
  74. TextAlignment text_alignment() const { return m_text_alignment; }
  75. void set_text_alignment(TextAlignment);
  76. Type type() const { return m_type; }
  77. bool is_single_line() const { return m_type == SingleLine; }
  78. bool is_multi_line() const { return m_type == MultiLine; }
  79. bool is_ruler_visible() const { return m_ruler_visible; }
  80. void set_ruler_visible(bool b) { m_ruler_visible = b; }
  81. Function<void()> on_cursor_change;
  82. Function<void()> on_selection_change;
  83. void set_text(const StringView&);
  84. void scroll_cursor_into_view();
  85. int line_count() const { return m_lines.size(); }
  86. int line_spacing() const { return m_line_spacing; }
  87. int line_height() const { return font().glyph_height() + m_line_spacing; }
  88. GTextPosition cursor() const { return m_cursor; }
  89. GTextRange normalized_selection() const { return m_selection.normalized(); }
  90. // FIXME: This should take glyph spacing into account, no?
  91. int glyph_width() const { return font().glyph_width('x'); }
  92. bool write_to_file(const StringView& path);
  93. bool has_selection() const { return m_selection.is_valid(); }
  94. String selected_text() const;
  95. String text() const;
  96. void clear();
  97. void cut();
  98. void copy();
  99. void paste();
  100. void do_delete();
  101. void delete_current_line();
  102. void select_all();
  103. Function<void()> on_change;
  104. Function<void()> on_return_pressed;
  105. Function<void()> on_escape_pressed;
  106. virtual const char* class_name() const override { return "GTextEditor"; }
  107. GAction& undo_action() { return *m_undo_action; }
  108. GAction& redo_action() { return *m_redo_action; }
  109. GAction& cut_action() { return *m_cut_action; }
  110. GAction& copy_action() { return *m_copy_action; }
  111. GAction& paste_action() { return *m_paste_action; }
  112. GAction& delete_action() { return *m_delete_action; }
  113. private:
  114. virtual void paint_event(GPaintEvent&) override;
  115. virtual void mousedown_event(GMouseEvent&) override;
  116. virtual void mouseup_event(GMouseEvent&) override;
  117. virtual void mousemove_event(GMouseEvent&) override;
  118. virtual void doubleclick_event(GMouseEvent&) override;
  119. virtual void keydown_event(GKeyEvent&) override;
  120. virtual void focusin_event(CEvent&) override;
  121. virtual void focusout_event(CEvent&) override;
  122. virtual void timer_event(CTimerEvent&) override;
  123. virtual bool accepts_focus() const override { return true; }
  124. virtual void enter_event(CEvent&) override;
  125. virtual void leave_event(CEvent&) override;
  126. virtual void context_menu_event(GContextMenuEvent&) override;
  127. virtual void resize_event(GResizeEvent&) override;
  128. void create_actions();
  129. void paint_ruler(Painter&);
  130. void update_content_size();
  131. void did_change();
  132. class Line {
  133. friend class GTextEditor;
  134. public:
  135. Line();
  136. explicit Line(const StringView&);
  137. const char* characters() const { return m_text.data(); }
  138. int length() const { return m_text.size() - 1; }
  139. int width(const Font&) const;
  140. void set_text(const StringView&);
  141. void append(char);
  142. void prepend(char);
  143. void insert(int index, char);
  144. void remove(int index);
  145. void append(const char*, int);
  146. void truncate(int length);
  147. void clear();
  148. private:
  149. // NOTE: This vector is null terminated.
  150. Vector<char> m_text;
  151. };
  152. Rect line_content_rect(int item_index) const;
  153. Rect line_widget_rect(int line_index) const;
  154. Rect cursor_content_rect() const;
  155. void update_cursor();
  156. void set_cursor(int line, int column);
  157. void set_cursor(const GTextPosition&);
  158. Line& current_line() { return *m_lines[m_cursor.line()]; }
  159. const Line& current_line() const { return *m_lines[m_cursor.line()]; }
  160. GTextPosition text_position_at(const Point&) const;
  161. void insert_at_cursor(char);
  162. void insert_at_cursor(const StringView&);
  163. int ruler_width() const;
  164. Rect ruler_content_rect(int line) const;
  165. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  166. void insert_at_cursor_or_replace_selection(const StringView&);
  167. void delete_selection();
  168. void did_update_selection();
  169. int content_x_for_position(const GTextPosition&) const;
  170. Type m_type { MultiLine };
  171. Vector<OwnPtr<Line>> m_lines;
  172. GTextPosition m_cursor;
  173. TextAlignment m_text_alignment { TextAlignment::CenterLeft };
  174. bool m_cursor_state { true };
  175. bool m_in_drag_select { false };
  176. bool m_ruler_visible { false };
  177. bool m_have_pending_change_notification { false };
  178. bool m_automatic_indentation_enabled { false };
  179. bool m_readonly { false };
  180. int m_line_spacing { 4 };
  181. int m_soft_tab_width { 4 };
  182. int m_horizontal_content_padding { 2 };
  183. GTextRange m_selection;
  184. OwnPtr<GMenu> m_context_menu;
  185. RefPtr<GAction> m_undo_action;
  186. RefPtr<GAction> m_redo_action;
  187. RefPtr<GAction> m_cut_action;
  188. RefPtr<GAction> m_copy_action;
  189. RefPtr<GAction> m_paste_action;
  190. RefPtr<GAction> m_delete_action;
  191. CElapsedTimer m_triple_click_timer;
  192. };