TextEditor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. virtual ~TextEditor() override;
  46. const TextDocument& document() const { return *m_document; }
  47. TextDocument& document() { return *m_document; }
  48. void set_document(TextDocument&);
  49. bool is_readonly() const { return m_readonly; }
  50. void set_readonly(bool);
  51. virtual bool is_automatic_indentation_enabled() const final { return m_automatic_indentation_enabled; }
  52. void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }
  53. virtual int soft_tab_width() const final { return m_soft_tab_width; }
  54. bool is_line_wrapping_enabled() const { return m_line_wrapping_enabled; }
  55. void set_line_wrapping_enabled(bool);
  56. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  57. void set_text_alignment(Gfx::TextAlignment);
  58. Type type() const { return m_type; }
  59. bool is_single_line() const { return m_type == SingleLine; }
  60. bool is_multi_line() const { return m_type == MultiLine; }
  61. bool is_ruler_visible() const { return m_ruler_visible; }
  62. void set_ruler_visible(bool b) { m_ruler_visible = b; }
  63. Function<void()> on_cursor_change;
  64. Function<void()> on_selection_change;
  65. void set_text(const StringView&);
  66. void scroll_cursor_into_view();
  67. void scroll_position_into_view(const TextPosition&);
  68. size_t line_count() const { return document().line_count(); }
  69. int line_spacing() const { return m_line_spacing; }
  70. int line_height() const;
  71. TextPosition cursor() const { return m_cursor; }
  72. TextRange normalized_selection() const { return m_selection.normalized(); }
  73. void insert_at_cursor_or_replace_selection(const StringView&);
  74. bool write_to_file(const StringView& path);
  75. bool has_selection() const { return m_selection.is_valid(); }
  76. String selected_text() const;
  77. void set_selection(const TextRange&);
  78. void clear_selection();
  79. bool can_undo() const { return document().can_undo(); }
  80. bool can_redo() const { return document().can_redo(); }
  81. String text() const;
  82. void clear();
  83. void cut();
  84. void copy();
  85. void paste();
  86. void do_delete();
  87. void delete_current_line();
  88. void select_all();
  89. void undo() { document().undo(); }
  90. void redo() { document().redo(); }
  91. Function<void()> on_change;
  92. Function<void()> on_return_pressed;
  93. Function<void()> on_escape_pressed;
  94. Action& undo_action() { return *m_undo_action; }
  95. Action& redo_action() { return *m_redo_action; }
  96. Action& cut_action() { return *m_cut_action; }
  97. Action& copy_action() { return *m_copy_action; }
  98. Action& paste_action() { return *m_paste_action; }
  99. Action& delete_action() { return *m_delete_action; }
  100. Action& go_to_line_action() { return *m_go_to_line_action; }
  101. Action& select_all_action() { return *m_select_all_action; }
  102. void add_custom_context_menu_action(Action&);
  103. void set_cursor(size_t line, size_t column);
  104. void set_cursor(const TextPosition&);
  105. const SyntaxHighlighter* syntax_highlighter() const;
  106. void set_syntax_highlighter(OwnPtr<SyntaxHighlighter>);
  107. bool is_in_drag_select() const { return m_in_drag_select; }
  108. protected:
  109. explicit TextEditor(Type = Type::MultiLine);
  110. virtual void did_change_font() override;
  111. virtual void paint_event(PaintEvent&) override;
  112. virtual void mousedown_event(MouseEvent&) override;
  113. virtual void mouseup_event(MouseEvent&) override;
  114. virtual void mousemove_event(MouseEvent&) override;
  115. virtual void doubleclick_event(MouseEvent&) override;
  116. virtual void keydown_event(KeyEvent&) override;
  117. virtual void focusin_event(Core::Event&) override;
  118. virtual void focusout_event(Core::Event&) override;
  119. virtual void timer_event(Core::TimerEvent&) override;
  120. virtual bool accepts_focus() const override { return true; }
  121. virtual void enter_event(Core::Event&) override;
  122. virtual void leave_event(Core::Event&) override;
  123. virtual void context_menu_event(ContextMenuEvent&) override;
  124. virtual void resize_event(ResizeEvent&) override;
  125. virtual void theme_change_event(ThemeChangeEvent&) override;
  126. virtual void cursor_did_change() {}
  127. Gfx::IntRect ruler_content_rect(size_t line) const;
  128. TextPosition text_position_at(const Gfx::IntPoint&) const;
  129. bool ruler_visible() const { return m_ruler_visible; }
  130. private:
  131. friend class TextDocumentLine;
  132. // ^TextDocument::Client
  133. virtual void document_did_append_line() override;
  134. virtual void document_did_insert_line(size_t) override;
  135. virtual void document_did_remove_line(size_t) override;
  136. virtual void document_did_remove_all_lines() override;
  137. virtual void document_did_change() override;
  138. virtual void document_did_set_text() override;
  139. virtual void document_did_set_cursor(const TextPosition&) override;
  140. void create_actions();
  141. void paint_ruler(Painter&);
  142. void update_content_size();
  143. void did_change();
  144. int fixed_glyph_width() const;
  145. void defer_reflow();
  146. void undefer_reflow();
  147. class ReflowDeferrer {
  148. public:
  149. ReflowDeferrer(TextEditor& editor)
  150. : m_editor(editor)
  151. {
  152. m_editor.defer_reflow();
  153. }
  154. ~ReflowDeferrer()
  155. {
  156. m_editor.undefer_reflow();
  157. }
  158. private:
  159. TextEditor& m_editor;
  160. };
  161. Gfx::IntRect line_content_rect(size_t item_index) const;
  162. Gfx::IntRect line_widget_rect(size_t line_index) const;
  163. Gfx::IntRect cursor_content_rect() const;
  164. Gfx::IntRect content_rect_for_position(const TextPosition&) const;
  165. void update_cursor();
  166. const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return document().lines(); }
  167. NonnullOwnPtrVector<TextDocumentLine>& lines() { return document().lines(); }
  168. TextDocumentLine& line(size_t index) { return document().line(index); }
  169. const TextDocumentLine& line(size_t index) const { return document().line(index); }
  170. TextDocumentLine& current_line() { return line(m_cursor.line()); }
  171. const TextDocumentLine& current_line() const { return line(m_cursor.line()); }
  172. int ruler_width() const;
  173. void toggle_selection_if_needed_for_event(const KeyEvent&);
  174. void delete_selection();
  175. void did_update_selection();
  176. int content_x_for_position(const TextPosition&) const;
  177. Gfx::IntRect ruler_rect_in_inner_coordinates() const;
  178. Gfx::IntRect visible_text_rect_in_inner_coordinates() const;
  179. void recompute_all_visual_lines();
  180. void ensure_cursor_is_valid();
  181. void flush_pending_change_notification_if_needed();
  182. void get_selection_line_boundaries(size_t& first_line, size_t& last_line);
  183. void move_selected_lines_up();
  184. void move_selected_lines_down();
  185. void sort_selected_lines();
  186. size_t visual_line_containing(size_t line_index, size_t column) const;
  187. void recompute_visual_lines(size_t line_index);
  188. void automatic_selection_scroll_timer_fired();
  189. template<class T, class... Args>
  190. inline void execute(Args&&... args)
  191. {
  192. auto command = make<T>(*m_document, forward<Args>(args)...);
  193. command->execute_from(*this);
  194. m_document->add_to_undo_stack(move(command));
  195. }
  196. Type m_type { MultiLine };
  197. TextPosition m_cursor;
  198. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::CenterLeft };
  199. bool m_cursor_state { true };
  200. bool m_in_drag_select { false };
  201. bool m_ruler_visible { false };
  202. bool m_has_pending_change_notification { false };
  203. bool m_automatic_indentation_enabled { false };
  204. bool m_line_wrapping_enabled { false };
  205. bool m_readonly { false };
  206. int m_line_spacing { 4 };
  207. size_t m_soft_tab_width { 4 };
  208. int m_horizontal_content_padding { 3 };
  209. TextRange m_selection;
  210. RefPtr<Menu> m_context_menu;
  211. RefPtr<Action> m_undo_action;
  212. RefPtr<Action> m_redo_action;
  213. RefPtr<Action> m_cut_action;
  214. RefPtr<Action> m_copy_action;
  215. RefPtr<Action> m_paste_action;
  216. RefPtr<Action> m_delete_action;
  217. RefPtr<Action> m_go_to_line_action;
  218. RefPtr<Action> m_select_all_action;
  219. Core::ElapsedTimer m_triple_click_timer;
  220. NonnullRefPtrVector<Action> m_custom_context_menu_actions;
  221. size_t m_reflow_deferred { 0 };
  222. bool m_reflow_requested { false };
  223. bool is_visual_data_up_to_date() const { return !m_reflow_requested; }
  224. RefPtr<TextDocument> m_document;
  225. template<typename Callback>
  226. void for_each_visual_line(size_t line_index, Callback) const;
  227. struct LineVisualData {
  228. Vector<size_t, 1> visual_line_breaks;
  229. Gfx::IntRect visual_rect;
  230. };
  231. NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
  232. OwnPtr<SyntaxHighlighter> m_highlighter;
  233. RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
  234. Gfx::IntPoint m_last_mousemove_position;
  235. };
  236. }