TextEditor.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Function.h>
  28. #include <AK/NonnullOwnPtrVector.h>
  29. #include <AK/NonnullRefPtrVector.h>
  30. #include <LibCore/ElapsedTimer.h>
  31. #include <LibGUI/ScrollableWidget.h>
  32. #include <LibGUI/TextDocument.h>
  33. #include <LibGUI/TextRange.h>
  34. #include <LibGfx/TextAlignment.h>
  35. namespace GUI {
  36. class TextEditor
  37. : public ScrollableWidget
  38. , public TextDocument::Client {
  39. C_OBJECT(TextEditor)
  40. public:
  41. enum Type {
  42. MultiLine,
  43. SingleLine
  44. };
  45. enum Mode {
  46. Editable,
  47. ReadOnly,
  48. DisplayOnly
  49. };
  50. virtual ~TextEditor() override;
  51. const TextDocument& document() const { return *m_document; }
  52. TextDocument& document() { return *m_document; }
  53. virtual void set_document(TextDocument&);
  54. void set_visualize_trailing_whitespace(bool);
  55. bool visualize_trailing_whitespace() const { return m_visualize_trailing_whitespace; }
  56. bool has_visible_list() const { return m_has_visible_list; }
  57. void set_has_visible_list(bool);
  58. bool has_open_button() const { return m_has_open_button; }
  59. void set_has_open_button(bool);
  60. virtual bool is_automatic_indentation_enabled() const final { return m_automatic_indentation_enabled; }
  61. void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }
  62. virtual int soft_tab_width() const final { return m_soft_tab_width; }
  63. bool is_line_wrapping_enabled() const { return m_line_wrapping_enabled; }
  64. void set_line_wrapping_enabled(bool);
  65. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  66. void set_text_alignment(Gfx::TextAlignment);
  67. Type type() const { return m_type; }
  68. bool is_single_line() const { return m_type == SingleLine; }
  69. bool is_multi_line() const { return m_type == MultiLine; }
  70. Mode mode() const { return m_mode; }
  71. bool is_editable() const { return m_mode == Editable; }
  72. bool is_readonly() const { return m_mode == ReadOnly; }
  73. bool is_displayonly() const { return m_mode == DisplayOnly; }
  74. void set_mode(const Mode);
  75. bool is_ruler_visible() const { return m_ruler_visible; }
  76. void set_ruler_visible(bool b) { m_ruler_visible = b; }
  77. void set_icon(const Gfx::Bitmap*);
  78. const Gfx::Bitmap* icon() const { return m_icon; }
  79. Function<void()> on_cursor_change;
  80. Function<void()> on_selection_change;
  81. Function<void()> on_focusin;
  82. Function<void()> on_focusout;
  83. void set_text(const StringView&);
  84. void scroll_cursor_into_view();
  85. void scroll_position_into_view(const TextPosition&);
  86. size_t line_count() const { return document().line_count(); }
  87. int line_spacing() const { return m_line_spacing; }
  88. int line_height() const;
  89. TextPosition cursor() const { return m_cursor; }
  90. TextRange normalized_selection() const { return m_selection.normalized(); }
  91. void insert_at_cursor_or_replace_selection(const StringView&);
  92. bool write_to_file(const StringView& path);
  93. bool has_selection() const { return m_selection.is_valid(); }
  94. String selected_text() const;
  95. void set_selection(const TextRange&);
  96. void clear_selection();
  97. bool can_undo() const { return document().can_undo(); }
  98. bool can_redo() const { return document().can_redo(); }
  99. String text() const;
  100. void clear();
  101. void cut();
  102. void copy();
  103. void paste();
  104. void do_delete();
  105. void delete_current_line();
  106. void select_all();
  107. void undo() { document().undo(); }
  108. void redo() { document().redo(); }
  109. Function<void()> on_change;
  110. Function<void()> on_mousedown;
  111. Function<void()> on_return_pressed;
  112. Function<void()> on_escape_pressed;
  113. Function<void()> on_up_pressed;
  114. Function<void()> on_down_pressed;
  115. Function<void()> on_pageup_pressed;
  116. Function<void()> on_pagedown_pressed;
  117. Action& undo_action() { return *m_undo_action; }
  118. Action& redo_action() { return *m_redo_action; }
  119. Action& cut_action() { return *m_cut_action; }
  120. Action& copy_action() { return *m_copy_action; }
  121. Action& paste_action() { return *m_paste_action; }
  122. Action& delete_action() { return *m_delete_action; }
  123. Action& go_to_line_action() { return *m_go_to_line_action; }
  124. Action& select_all_action() { return *m_select_all_action; }
  125. void add_custom_context_menu_action(Action&);
  126. void set_cursor(size_t line, size_t column);
  127. void set_cursor(const TextPosition&);
  128. const SyntaxHighlighter* syntax_highlighter() const;
  129. void set_syntax_highlighter(OwnPtr<SyntaxHighlighter>);
  130. bool is_in_drag_select() const { return m_in_drag_select; }
  131. protected:
  132. explicit TextEditor(Type = Type::MultiLine);
  133. virtual void did_change_font() override;
  134. virtual void paint_event(PaintEvent&) override;
  135. virtual void mousedown_event(MouseEvent&) override;
  136. virtual void mouseup_event(MouseEvent&) override;
  137. virtual void mousemove_event(MouseEvent&) override;
  138. virtual void doubleclick_event(MouseEvent&) override;
  139. virtual void keydown_event(KeyEvent&) override;
  140. virtual void focusin_event(FocusEvent&) override;
  141. virtual void focusout_event(FocusEvent&) override;
  142. virtual void timer_event(Core::TimerEvent&) override;
  143. virtual bool accepts_focus() const override { return true; }
  144. virtual void enter_event(Core::Event&) override;
  145. virtual void leave_event(Core::Event&) override;
  146. virtual void context_menu_event(ContextMenuEvent&) override;
  147. virtual void resize_event(ResizeEvent&) override;
  148. virtual void theme_change_event(ThemeChangeEvent&) override;
  149. virtual void cursor_did_change() {}
  150. Gfx::IntRect ruler_content_rect(size_t line) const;
  151. TextPosition text_position_at(const Gfx::IntPoint&) const;
  152. bool ruler_visible() const { return m_ruler_visible; }
  153. private:
  154. friend class TextDocumentLine;
  155. // ^TextDocument::Client
  156. virtual void document_did_append_line() override;
  157. virtual void document_did_insert_line(size_t) override;
  158. virtual void document_did_remove_line(size_t) override;
  159. virtual void document_did_remove_all_lines() override;
  160. virtual void document_did_change() override;
  161. virtual void document_did_set_text() override;
  162. virtual void document_did_set_cursor(const TextPosition&) override;
  163. void create_actions();
  164. void paint_ruler(Painter&);
  165. void update_content_size();
  166. void did_change();
  167. int fixed_glyph_width() const;
  168. void defer_reflow();
  169. void undefer_reflow();
  170. int icon_size() const { return 16; }
  171. int icon_padding() const { return 2; }
  172. int button_padding() const { return m_has_open_button ? 17 : 2; }
  173. class ReflowDeferrer {
  174. public:
  175. ReflowDeferrer(TextEditor& editor)
  176. : m_editor(editor)
  177. {
  178. m_editor.defer_reflow();
  179. }
  180. ~ReflowDeferrer()
  181. {
  182. m_editor.undefer_reflow();
  183. }
  184. private:
  185. TextEditor& m_editor;
  186. };
  187. Gfx::IntRect line_content_rect(size_t item_index) const;
  188. Gfx::IntRect line_widget_rect(size_t line_index) const;
  189. Gfx::IntRect cursor_content_rect() const;
  190. Gfx::IntRect content_rect_for_position(const TextPosition&) const;
  191. void update_cursor();
  192. const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return document().lines(); }
  193. NonnullOwnPtrVector<TextDocumentLine>& lines() { return document().lines(); }
  194. TextDocumentLine& line(size_t index) { return document().line(index); }
  195. const TextDocumentLine& line(size_t index) const { return document().line(index); }
  196. TextDocumentLine& current_line() { return line(m_cursor.line()); }
  197. const TextDocumentLine& current_line() const { return line(m_cursor.line()); }
  198. int ruler_width() const;
  199. void toggle_selection_if_needed_for_event(const KeyEvent&);
  200. void delete_selection();
  201. void did_update_selection();
  202. int content_x_for_position(const TextPosition&) const;
  203. Gfx::IntRect ruler_rect_in_inner_coordinates() const;
  204. Gfx::IntRect visible_text_rect_in_inner_coordinates() const;
  205. void recompute_all_visual_lines();
  206. void ensure_cursor_is_valid();
  207. void flush_pending_change_notification_if_needed();
  208. void get_selection_line_boundaries(size_t& first_line, size_t& last_line);
  209. void move_selected_lines_up();
  210. void move_selected_lines_down();
  211. void sort_selected_lines();
  212. size_t visual_line_containing(size_t line_index, size_t column) const;
  213. void recompute_visual_lines(size_t line_index);
  214. void automatic_selection_scroll_timer_fired();
  215. template<class T, class... Args>
  216. inline void execute(Args&&... args)
  217. {
  218. auto command = make<T>(*m_document, forward<Args>(args)...);
  219. command->execute_from(*this);
  220. m_document->add_to_undo_stack(move(command));
  221. }
  222. Type m_type { MultiLine };
  223. Mode m_mode { Editable };
  224. TextPosition m_cursor;
  225. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::CenterLeft };
  226. bool m_cursor_state { true };
  227. bool m_in_drag_select { false };
  228. bool m_ruler_visible { false };
  229. bool m_has_pending_change_notification { false };
  230. bool m_automatic_indentation_enabled { false };
  231. bool m_line_wrapping_enabled { false };
  232. bool m_has_visible_list { false };
  233. bool m_has_open_button { false };
  234. bool m_visualize_trailing_whitespace { true };
  235. int m_line_spacing { 4 };
  236. size_t m_soft_tab_width { 4 };
  237. int m_horizontal_content_padding { 3 };
  238. TextRange m_selection;
  239. RefPtr<Menu> m_context_menu;
  240. RefPtr<Action> m_undo_action;
  241. RefPtr<Action> m_redo_action;
  242. RefPtr<Action> m_cut_action;
  243. RefPtr<Action> m_copy_action;
  244. RefPtr<Action> m_paste_action;
  245. RefPtr<Action> m_delete_action;
  246. RefPtr<Action> m_go_to_line_action;
  247. RefPtr<Action> m_select_all_action;
  248. Core::ElapsedTimer m_triple_click_timer;
  249. NonnullRefPtrVector<Action> m_custom_context_menu_actions;
  250. size_t m_reflow_deferred { 0 };
  251. bool m_reflow_requested { false };
  252. bool is_visual_data_up_to_date() const { return !m_reflow_requested; }
  253. RefPtr<TextDocument> m_document;
  254. template<typename Callback>
  255. void for_each_visual_line(size_t line_index, Callback) const;
  256. struct LineVisualData {
  257. Vector<size_t, 1> visual_line_breaks;
  258. Gfx::IntRect visual_rect;
  259. };
  260. NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
  261. OwnPtr<SyntaxHighlighter> m_highlighter;
  262. RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
  263. Gfx::IntPoint m_last_mousemove_position;
  264. RefPtr<Gfx::Bitmap> m_icon;
  265. };
  266. }