GTextEditor.h 9.7 KB

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