GTextEditor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. void set_cursor(int line, int column);
  130. void set_cursor(const GTextPosition&);
  131. struct Span {
  132. bool contains(const GTextPosition& position) const
  133. {
  134. if (!(position.line() > start.line() || (position.line() == start.line() && position.column() >= start.column())))
  135. return false;
  136. if (!(position.line() < end.line() || (position.line() == end.line() && position.column() <= end.column())))
  137. return false;
  138. return true;
  139. }
  140. GTextPosition start;
  141. GTextPosition end;
  142. Color color;
  143. const Font* font { nullptr };
  144. };
  145. void set_spans(const Vector<Span>& spans)
  146. {
  147. m_spans = spans;
  148. }
  149. protected:
  150. GTextEditor(Type, GWidget* parent);
  151. virtual void did_change_font() override;
  152. virtual void paint_event(GPaintEvent&) override;
  153. virtual void mousedown_event(GMouseEvent&) override;
  154. virtual void mouseup_event(GMouseEvent&) override;
  155. virtual void mousemove_event(GMouseEvent&) override;
  156. virtual void doubleclick_event(GMouseEvent&) override;
  157. virtual void keydown_event(GKeyEvent&) override;
  158. virtual void focusin_event(CEvent&) override;
  159. virtual void focusout_event(CEvent&) override;
  160. virtual void timer_event(CTimerEvent&) override;
  161. virtual bool accepts_focus() const override { return true; }
  162. virtual void enter_event(CEvent&) override;
  163. virtual void leave_event(CEvent&) override;
  164. virtual void context_menu_event(GContextMenuEvent&) override;
  165. virtual void resize_event(GResizeEvent&) override;
  166. private:
  167. void create_actions();
  168. void paint_ruler(Painter&);
  169. void update_content_size();
  170. void did_change();
  171. class Line {
  172. friend class GTextEditor;
  173. public:
  174. explicit Line(GTextEditor&);
  175. Line(GTextEditor&, const StringView&);
  176. StringView view() const { return { characters(), length() }; }
  177. const char* characters() const { return m_text.data(); }
  178. int length() const { return m_text.size() - 1; }
  179. void set_text(const StringView&);
  180. void append(char);
  181. void prepend(char);
  182. void insert(int index, char);
  183. void remove(int index);
  184. void append(const char*, int);
  185. void truncate(int length);
  186. void clear();
  187. void recompute_visual_lines();
  188. int visual_line_containing(int column) const;
  189. int first_non_whitespace_column() const;
  190. template<typename Callback>
  191. void for_each_visual_line(Callback) const;
  192. private:
  193. GTextEditor& m_editor;
  194. // NOTE: This vector is null terminated.
  195. Vector<char> m_text;
  196. Vector<int, 1> m_visual_line_breaks;
  197. Rect m_visual_rect;
  198. };
  199. Rect line_content_rect(int item_index) const;
  200. Rect line_widget_rect(int line_index) const;
  201. Rect cursor_content_rect() const;
  202. Rect content_rect_for_position(const GTextPosition&) const;
  203. void update_cursor();
  204. Line& current_line() { return m_lines[m_cursor.line()]; }
  205. const Line& current_line() const { return m_lines[m_cursor.line()]; }
  206. GTextPosition text_position_at(const Point&) const;
  207. void insert_at_cursor(char);
  208. void insert_at_cursor(const StringView&);
  209. int ruler_width() const;
  210. Rect ruler_content_rect(int line) const;
  211. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  212. void insert_at_cursor_or_replace_selection(const StringView&);
  213. void delete_selection();
  214. void did_update_selection();
  215. int content_x_for_position(const GTextPosition&) const;
  216. char character_at(const GTextPosition&) const;
  217. Rect ruler_rect_in_inner_coordinates() const;
  218. Rect visible_text_rect_in_inner_coordinates() const;
  219. void recompute_all_visual_lines();
  220. Type m_type { MultiLine };
  221. NonnullOwnPtrVector<Line> m_lines;
  222. GTextPosition m_cursor;
  223. TextAlignment m_text_alignment { TextAlignment::CenterLeft };
  224. bool m_cursor_state { true };
  225. bool m_in_drag_select { false };
  226. bool m_ruler_visible { false };
  227. bool m_have_pending_change_notification { false };
  228. bool m_automatic_indentation_enabled { false };
  229. bool m_line_wrapping_enabled { false };
  230. bool m_readonly { false };
  231. int m_line_spacing { 4 };
  232. int m_soft_tab_width { 4 };
  233. int m_horizontal_content_padding { 2 };
  234. GTextRange m_selection;
  235. OwnPtr<GMenu> m_context_menu;
  236. RefPtr<GAction> m_undo_action;
  237. RefPtr<GAction> m_redo_action;
  238. RefPtr<GAction> m_cut_action;
  239. RefPtr<GAction> m_copy_action;
  240. RefPtr<GAction> m_paste_action;
  241. RefPtr<GAction> m_delete_action;
  242. CElapsedTimer m_triple_click_timer;
  243. NonnullRefPtrVector<GAction> m_custom_context_menu_actions;
  244. Vector<Span> m_spans;
  245. };
  246. inline const LogStream& operator<<(const LogStream& stream, const GTextPosition& value)
  247. {
  248. if (!value.is_valid())
  249. return stream << "GTextPosition(Invalid)";
  250. return stream << String::format("(%d,%d)", value.line(), value.column());
  251. }
  252. inline const LogStream& operator<<(const LogStream& stream, const GTextRange& value)
  253. {
  254. if (!value.is_valid())
  255. return stream << "GTextRange(Invalid)";
  256. return stream << value.start() << '-' << value.end();
  257. }