GTextEditor.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #pragma once
  2. #include <AK/Function.h>
  3. #include <AK/HashMap.h>
  4. #include <AK/NonnullOwnPtrVector.h>
  5. #include <LibDraw/TextAlignment.h>
  6. #include <LibGUI/GScrollableWidget.h>
  7. class GAction;
  8. class GMenu;
  9. class GScrollBar;
  10. class Painter;
  11. enum class ShouldWrapAtEndOfDocument { No = 0, Yes };
  12. enum class ShouldWrapAtStartOfDocument { No = 0, Yes };
  13. class GTextPosition {
  14. public:
  15. GTextPosition() {}
  16. GTextPosition(int line, int column)
  17. : m_line(line)
  18. , m_column(column)
  19. {
  20. }
  21. bool is_valid() const { return m_line >= 0 && m_column >= 0; }
  22. int line() const { return m_line; }
  23. int column() const { return m_column; }
  24. void set_line(int line) { m_line = line; }
  25. void set_column(int column) { m_column = column; }
  26. bool operator==(const GTextPosition& other) const { return m_line == other.m_line && m_column == other.m_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_line == other.m_line && m_column < other.m_column); }
  29. private:
  30. int m_line { -1 };
  31. int m_column { -1 };
  32. };
  33. class GTextRange {
  34. public:
  35. GTextRange() {}
  36. GTextRange(const GTextPosition& start, const GTextPosition& end)
  37. : m_start(start)
  38. , m_end(end)
  39. {
  40. }
  41. bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
  42. void clear()
  43. {
  44. m_start = {};
  45. m_end = {};
  46. }
  47. GTextPosition& start() { return m_start; }
  48. GTextPosition& end() { return m_end; }
  49. const GTextPosition& start() const { return m_start; }
  50. const GTextPosition& end() const { return m_end; }
  51. GTextRange normalized() const { return GTextRange(normalized_start(), normalized_end()); }
  52. void set_start(const GTextPosition& position) { m_start = position; }
  53. void set_end(const GTextPosition& position) { m_end = position; }
  54. void set(const GTextPosition& start, const GTextPosition& end)
  55. {
  56. m_start = start;
  57. m_end = end;
  58. }
  59. bool operator==(const GTextRange& other) const
  60. {
  61. return m_start == other.m_start && m_end == other.m_end;
  62. }
  63. private:
  64. GTextPosition normalized_start() const { return m_start < m_end ? m_start : m_end; }
  65. GTextPosition normalized_end() const { return m_start < m_end ? m_end : m_start; }
  66. GTextPosition m_start;
  67. GTextPosition m_end;
  68. };
  69. class GTextEditor : public GScrollableWidget {
  70. C_OBJECT(GTextEditor)
  71. public:
  72. enum Type {
  73. MultiLine,
  74. SingleLine
  75. };
  76. GTextEditor(Type, GWidget* parent);
  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. private:
  129. virtual void paint_event(GPaintEvent&) override;
  130. virtual void mousedown_event(GMouseEvent&) override;
  131. virtual void mouseup_event(GMouseEvent&) override;
  132. virtual void mousemove_event(GMouseEvent&) override;
  133. virtual void doubleclick_event(GMouseEvent&) override;
  134. virtual void keydown_event(GKeyEvent&) override;
  135. virtual void focusin_event(CEvent&) override;
  136. virtual void focusout_event(CEvent&) override;
  137. virtual void timer_event(CTimerEvent&) override;
  138. virtual bool accepts_focus() const override { return true; }
  139. virtual void enter_event(CEvent&) override;
  140. virtual void leave_event(CEvent&) override;
  141. virtual void context_menu_event(GContextMenuEvent&) override;
  142. virtual void resize_event(GResizeEvent&) override;
  143. void create_actions();
  144. void paint_ruler(Painter&);
  145. void update_content_size();
  146. void did_change();
  147. class Line {
  148. friend class GTextEditor;
  149. public:
  150. explicit Line(GTextEditor&);
  151. Line(GTextEditor&, const StringView&);
  152. StringView view() const { return { characters(), length() }; }
  153. const char* characters() const { return m_text.data(); }
  154. int length() const { return m_text.size() - 1; }
  155. int width(const Font&) const;
  156. void set_text(const StringView&);
  157. void append(char);
  158. void prepend(char);
  159. void insert(int index, char);
  160. void remove(int index);
  161. void append(const char*, int);
  162. void truncate(int length);
  163. void clear();
  164. void recompute_visual_lines();
  165. int visual_line_containing(int column) const;
  166. template<typename Callback>
  167. void for_each_visual_line(Callback) const;
  168. private:
  169. GTextEditor& m_editor;
  170. // NOTE: This vector is null terminated.
  171. Vector<char> m_text;
  172. Vector<int, 1> m_visual_line_breaks;
  173. Rect m_visual_rect;
  174. };
  175. Rect line_content_rect(int item_index) const;
  176. Rect line_widget_rect(int line_index) const;
  177. Rect cursor_content_rect() const;
  178. Rect content_rect_for_position(const GTextPosition&) const;
  179. void update_cursor();
  180. void set_cursor(int line, int column);
  181. void set_cursor(const GTextPosition&);
  182. Line& current_line() { return m_lines[m_cursor.line()]; }
  183. const Line& current_line() const { return m_lines[m_cursor.line()]; }
  184. GTextPosition text_position_at(const Point&) const;
  185. void insert_at_cursor(char);
  186. void insert_at_cursor(const StringView&);
  187. int ruler_width() const;
  188. Rect ruler_content_rect(int line) const;
  189. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  190. void insert_at_cursor_or_replace_selection(const StringView&);
  191. void delete_selection();
  192. void did_update_selection();
  193. int content_x_for_position(const GTextPosition&) const;
  194. char character_at(const GTextPosition&) const;
  195. Rect ruler_rect_in_inner_coordinates() const;
  196. Rect visible_text_rect_in_inner_coordinates() const;
  197. void recompute_all_visual_lines();
  198. Type m_type { MultiLine };
  199. NonnullOwnPtrVector<Line> m_lines;
  200. GTextPosition m_cursor;
  201. TextAlignment m_text_alignment { TextAlignment::CenterLeft };
  202. bool m_cursor_state { true };
  203. bool m_in_drag_select { false };
  204. bool m_ruler_visible { false };
  205. bool m_have_pending_change_notification { false };
  206. bool m_automatic_indentation_enabled { false };
  207. bool m_line_wrapping_enabled { false };
  208. bool m_readonly { false };
  209. int m_line_spacing { 4 };
  210. int m_soft_tab_width { 4 };
  211. int m_horizontal_content_padding { 2 };
  212. GTextRange m_selection;
  213. OwnPtr<GMenu> m_context_menu;
  214. RefPtr<GAction> m_undo_action;
  215. RefPtr<GAction> m_redo_action;
  216. RefPtr<GAction> m_cut_action;
  217. RefPtr<GAction> m_copy_action;
  218. RefPtr<GAction> m_paste_action;
  219. RefPtr<GAction> m_delete_action;
  220. CElapsedTimer m_triple_click_timer;
  221. };
  222. inline const LogStream& operator<<(const LogStream& stream, const GTextPosition& value)
  223. {
  224. if (!value.is_valid())
  225. return stream << "GTextPosition(Invalid)";
  226. return stream << String::format("(%d,%d)", value.line(), value.column());
  227. }
  228. inline const LogStream& operator<<(const LogStream& stream, const GTextRange& value)
  229. {
  230. if (!value.is_valid())
  231. return stream << "GTextRange(Invalid)";
  232. return stream << value.start() << '-' << value.end();
  233. }