TextEditor.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 <LibCore/Timer.h>
  32. #include <LibGUI/Forward.h>
  33. #include <LibGUI/ScrollableWidget.h>
  34. #include <LibGUI/TextDocument.h>
  35. #include <LibGUI/TextRange.h>
  36. #include <LibGfx/TextAlignment.h>
  37. #include <LibSyntax/Forward.h>
  38. #include <LibSyntax/HighlighterClient.h>
  39. namespace GUI {
  40. class TextEditor
  41. : public ScrollableWidget
  42. , public TextDocument::Client
  43. , public Syntax::HighlighterClient {
  44. C_OBJECT(TextEditor);
  45. public:
  46. enum Type {
  47. MultiLine,
  48. SingleLine
  49. };
  50. enum Mode {
  51. Editable,
  52. ReadOnly,
  53. DisplayOnly
  54. };
  55. enum WrappingMode {
  56. NoWrap,
  57. WrapAnywhere,
  58. WrapAtWords
  59. };
  60. virtual ~TextEditor() override;
  61. const TextDocument& document() const { return *m_document; }
  62. TextDocument& document() { return *m_document; }
  63. virtual void set_document(TextDocument&);
  64. const String& placeholder() const { return m_placeholder; }
  65. void set_placeholder(const StringView& placeholder) { m_placeholder = placeholder; }
  66. TextDocumentLine& current_line() { return line(m_cursor.line()); }
  67. const TextDocumentLine& current_line() const { return line(m_cursor.line()); }
  68. void set_visualize_trailing_whitespace(bool);
  69. bool visualize_trailing_whitespace() const { return m_visualize_trailing_whitespace; }
  70. virtual bool is_automatic_indentation_enabled() const final { return m_automatic_indentation_enabled; }
  71. void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }
  72. virtual int soft_tab_width() const final { return m_soft_tab_width; }
  73. void set_soft_tab_width(int width) { m_soft_tab_width = width; };
  74. WrappingMode wrapping_mode() const { return m_wrapping_mode; }
  75. bool is_wrapping_enabled() const { return m_wrapping_mode != WrappingMode::NoWrap; }
  76. void set_wrapping_mode(WrappingMode);
  77. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  78. void set_text_alignment(Gfx::TextAlignment);
  79. Type type() const { return m_type; }
  80. bool is_single_line() const { return m_type == SingleLine; }
  81. bool is_multi_line() const { return m_type == MultiLine; }
  82. Mode mode() const { return m_mode; }
  83. bool is_editable() const { return m_mode == Editable; }
  84. bool is_readonly() const { return m_mode == ReadOnly; }
  85. bool is_displayonly() const { return m_mode == DisplayOnly; }
  86. void set_mode(const Mode);
  87. bool is_ruler_visible() const { return m_ruler_visible; }
  88. void set_ruler_visible(bool b) { m_ruler_visible = b; }
  89. void set_icon(const Gfx::Bitmap*);
  90. const Gfx::Bitmap* icon() const { return m_icon; }
  91. Function<void()> on_cursor_change;
  92. Function<void()> on_selection_change;
  93. Function<void()> on_focusin;
  94. Function<void()> on_focusout;
  95. void set_text(const StringView&);
  96. void scroll_cursor_into_view();
  97. void scroll_position_into_view(const TextPosition&);
  98. size_t line_count() const { return document().line_count(); }
  99. TextDocumentLine& line(size_t index) { return document().line(index); }
  100. const TextDocumentLine& line(size_t index) const { return document().line(index); }
  101. NonnullOwnPtrVector<TextDocumentLine>& lines() { return document().lines(); }
  102. const NonnullOwnPtrVector<TextDocumentLine>& lines() const { return document().lines(); }
  103. int line_spacing() const { return m_line_spacing; }
  104. int line_height() const;
  105. TextPosition cursor() const { return m_cursor; }
  106. TextRange normalized_selection() const { return m_selection.normalized(); }
  107. void insert_at_cursor_or_replace_selection(const StringView&);
  108. bool write_to_file(const String& path);
  109. bool has_selection() const { return m_selection.is_valid(); }
  110. String selected_text() const;
  111. void set_selection(const TextRange&);
  112. void clear_selection();
  113. bool can_undo() const { return document().can_undo(); }
  114. bool can_redo() const { return document().can_redo(); }
  115. String text() const;
  116. void clear();
  117. void cut();
  118. void copy();
  119. void paste();
  120. void do_delete();
  121. void delete_current_line();
  122. void select_all();
  123. virtual void undo() { document().undo(); }
  124. virtual void redo() { document().redo(); }
  125. Function<void()> on_change;
  126. Function<void()> on_mousedown;
  127. Function<void()> on_return_pressed;
  128. Function<void()> on_escape_pressed;
  129. Function<void()> on_up_pressed;
  130. Function<void()> on_down_pressed;
  131. Function<void()> on_pageup_pressed;
  132. Function<void()> on_pagedown_pressed;
  133. Action& undo_action() { return *m_undo_action; }
  134. Action& redo_action() { return *m_redo_action; }
  135. Action& cut_action() { return *m_cut_action; }
  136. Action& copy_action() { return *m_copy_action; }
  137. Action& paste_action() { return *m_paste_action; }
  138. Action& delete_action() { return *m_delete_action; }
  139. Action& go_to_line_action() { return *m_go_to_line_action; }
  140. Action& select_all_action() { return *m_select_all_action; }
  141. void add_custom_context_menu_action(Action&);
  142. void set_cursor_and_focus_line(size_t line, size_t column);
  143. void set_cursor(size_t line, size_t column);
  144. virtual void set_cursor(const TextPosition&);
  145. const Syntax::Highlighter* syntax_highlighter() const;
  146. void set_syntax_highlighter(OwnPtr<Syntax::Highlighter>);
  147. const AutocompleteProvider* autocomplete_provider() const;
  148. void set_autocomplete_provider(OwnPtr<AutocompleteProvider>&&);
  149. const EditingEngine* editing_engine() const;
  150. void set_editing_engine(OwnPtr<EditingEngine>);
  151. bool should_autocomplete_automatically() const { return m_autocomplete_timer; }
  152. void set_should_autocomplete_automatically(bool);
  153. bool is_in_drag_select() const { return m_in_drag_select; }
  154. TextRange* selection() { return &m_selection; };
  155. void did_update_selection();
  156. void did_change();
  157. void update_cursor();
  158. void add_code_point(u32 code_point);
  159. void reset_cursor_blink();
  160. void toggle_selection_if_needed_for_event(bool is_selecting);
  161. int number_of_visible_lines() const;
  162. Gfx::IntRect cursor_content_rect() const;
  163. TextPosition text_position_at_content_position(const Gfx::IntPoint&) const;
  164. void delete_text_range(TextRange);
  165. protected:
  166. explicit TextEditor(Type = Type::MultiLine);
  167. virtual void did_change_font() override;
  168. virtual void paint_event(PaintEvent&) override;
  169. virtual void mousedown_event(MouseEvent&) override;
  170. virtual void mouseup_event(MouseEvent&) override;
  171. virtual void mousemove_event(MouseEvent&) override;
  172. virtual void doubleclick_event(MouseEvent&) override;
  173. virtual void keydown_event(KeyEvent&) override;
  174. virtual void focusin_event(FocusEvent&) override;
  175. virtual void focusout_event(FocusEvent&) override;
  176. virtual void timer_event(Core::TimerEvent&) override;
  177. virtual void enter_event(Core::Event&) override;
  178. virtual void leave_event(Core::Event&) override;
  179. virtual void context_menu_event(ContextMenuEvent&) override;
  180. virtual void resize_event(ResizeEvent&) override;
  181. virtual void theme_change_event(ThemeChangeEvent&) override;
  182. virtual void cursor_did_change() { }
  183. Gfx::IntRect ruler_content_rect(size_t line) const;
  184. TextPosition text_position_at(const Gfx::IntPoint&) const;
  185. bool ruler_visible() const { return m_ruler_visible; }
  186. Gfx::IntRect content_rect_for_position(const TextPosition&) const;
  187. int ruler_width() const;
  188. private:
  189. friend class TextDocumentLine;
  190. // ^TextDocument::Client
  191. virtual void document_did_append_line() override;
  192. virtual void document_did_insert_line(size_t) override;
  193. virtual void document_did_remove_line(size_t) override;
  194. virtual void document_did_remove_all_lines() override;
  195. virtual void document_did_change() override;
  196. virtual void document_did_set_text() override;
  197. virtual void document_did_set_cursor(const TextPosition&) override;
  198. // ^Syntax::HighlighterClient
  199. virtual Vector<TextDocumentSpan>& spans() final { return document().spans(); }
  200. virtual const Vector<TextDocumentSpan>& spans() const final { return document().spans(); }
  201. virtual void highlighter_did_set_spans(Vector<TextDocumentSpan> spans) final { document().set_spans(move(spans)); }
  202. virtual void set_span_at_index(size_t index, TextDocumentSpan span) final { document().set_span_at_index(index, move(span)); }
  203. virtual void highlighter_did_request_update() final { update(); }
  204. virtual String highlighter_did_request_text() const final { return text(); }
  205. virtual GUI::TextDocument& highlighter_did_request_document() final { return document(); }
  206. virtual GUI::TextPosition highlighter_did_request_cursor() const final { return m_cursor; }
  207. void create_actions();
  208. void paint_ruler(Painter&);
  209. void update_content_size();
  210. int fixed_glyph_width() const;
  211. void defer_reflow();
  212. void undefer_reflow();
  213. void try_show_autocomplete();
  214. int icon_size() const { return 16; }
  215. int icon_padding() const { return 2; }
  216. class ReflowDeferrer {
  217. public:
  218. ReflowDeferrer(TextEditor& editor)
  219. : m_editor(editor)
  220. {
  221. m_editor.defer_reflow();
  222. }
  223. ~ReflowDeferrer()
  224. {
  225. m_editor.undefer_reflow();
  226. }
  227. private:
  228. TextEditor& m_editor;
  229. };
  230. Gfx::IntRect line_content_rect(size_t item_index) const;
  231. Gfx::IntRect line_widget_rect(size_t line_index) const;
  232. void delete_selection();
  233. int content_x_for_position(const TextPosition&) const;
  234. Gfx::IntRect ruler_rect_in_inner_coordinates() const;
  235. Gfx::IntRect visible_text_rect_in_inner_coordinates() const;
  236. void recompute_all_visual_lines();
  237. void ensure_cursor_is_valid();
  238. void flush_pending_change_notification_if_needed();
  239. size_t visual_line_containing(size_t line_index, size_t column) const;
  240. void recompute_visual_lines(size_t line_index);
  241. void automatic_selection_scroll_timer_fired();
  242. template<class T, class... Args>
  243. inline void execute(Args&&... args)
  244. {
  245. auto command = make<T>(*m_document, forward<Args>(args)...);
  246. command->perform_formatting(*this);
  247. on_edit_action(*command);
  248. command->execute_from(*this);
  249. m_document->add_to_undo_stack(move(command));
  250. }
  251. virtual void on_edit_action(const Command&) { }
  252. Type m_type { MultiLine };
  253. Mode m_mode { Editable };
  254. TextPosition m_cursor;
  255. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::CenterLeft };
  256. bool m_cursor_state { true };
  257. bool m_in_drag_select { false };
  258. bool m_ruler_visible { false };
  259. bool m_has_pending_change_notification { false };
  260. bool m_automatic_indentation_enabled { false };
  261. WrappingMode m_wrapping_mode { WrappingMode::NoWrap };
  262. bool m_visualize_trailing_whitespace { true };
  263. int m_line_spacing { 4 };
  264. size_t m_soft_tab_width { 4 };
  265. int m_horizontal_content_padding { 3 };
  266. TextRange m_selection;
  267. RefPtr<Menu> m_context_menu;
  268. RefPtr<Action> m_undo_action;
  269. RefPtr<Action> m_redo_action;
  270. RefPtr<Action> m_cut_action;
  271. RefPtr<Action> m_copy_action;
  272. RefPtr<Action> m_paste_action;
  273. RefPtr<Action> m_delete_action;
  274. RefPtr<Action> m_go_to_line_action;
  275. RefPtr<Action> m_select_all_action;
  276. Core::ElapsedTimer m_triple_click_timer;
  277. NonnullRefPtrVector<Action> m_custom_context_menu_actions;
  278. size_t m_reflow_deferred { 0 };
  279. bool m_reflow_requested { false };
  280. bool is_visual_data_up_to_date() const { return !m_reflow_requested; }
  281. RefPtr<TextDocument> m_document;
  282. String m_placeholder { "" };
  283. template<typename Callback>
  284. void for_each_visual_line(size_t line_index, Callback) const;
  285. struct LineVisualData {
  286. Vector<size_t, 1> visual_line_breaks;
  287. Gfx::IntRect visual_rect;
  288. };
  289. NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
  290. OwnPtr<Syntax::Highlighter> m_highlighter;
  291. OwnPtr<AutocompleteProvider> m_autocomplete_provider;
  292. OwnPtr<AutocompleteBox> m_autocomplete_box;
  293. bool m_should_keep_autocomplete_box { false };
  294. size_t m_automatic_autocomplete_delay_ms { 800 };
  295. RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
  296. RefPtr<Core::Timer> m_autocomplete_timer;
  297. OwnPtr<EditingEngine> m_editing_engine;
  298. Gfx::IntPoint m_last_mousemove_position;
  299. RefPtr<Gfx::Bitmap> m_icon;
  300. };
  301. }