TextEditor.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Function.h>
  9. #include <AK/NonnullOwnPtrVector.h>
  10. #include <AK/NonnullRefPtrVector.h>
  11. #include <LibCore/ElapsedTimer.h>
  12. #include <LibCore/Timer.h>
  13. #include <LibGUI/AbstractScrollableWidget.h>
  14. #include <LibGUI/Action.h>
  15. #include <LibGUI/Clipboard.h>
  16. #include <LibGUI/Forward.h>
  17. #include <LibGUI/TextDocument.h>
  18. #include <LibGUI/TextRange.h>
  19. #include <LibGfx/TextAlignment.h>
  20. #include <LibSyntax/Forward.h>
  21. #include <LibSyntax/HighlighterClient.h>
  22. namespace GUI {
  23. class TextEditor
  24. : public AbstractScrollableWidget
  25. , public TextDocument::Client
  26. , public Syntax::HighlighterClient
  27. , public Clipboard::ClipboardClient {
  28. C_OBJECT(TextEditor);
  29. public:
  30. enum Type {
  31. MultiLine,
  32. SingleLine
  33. };
  34. enum Mode {
  35. Editable,
  36. ReadOnly,
  37. DisplayOnly
  38. };
  39. enum WrappingMode {
  40. NoWrap,
  41. WrapAnywhere,
  42. WrapAtWords
  43. };
  44. virtual ~TextEditor() override;
  45. TextDocument const& document() const { return *m_document; }
  46. TextDocument& document() { return *m_document; }
  47. bool has_document() const { return !!m_document; }
  48. virtual void set_document(TextDocument&);
  49. String const& placeholder() const { return m_placeholder; }
  50. void set_placeholder(StringView placeholder) { m_placeholder = placeholder; }
  51. TextDocumentLine& current_line() { return line(m_cursor.line()); }
  52. TextDocumentLine const& current_line() const { return line(m_cursor.line()); }
  53. void set_visualize_trailing_whitespace(bool);
  54. bool visualize_trailing_whitespace() const { return m_visualize_trailing_whitespace; }
  55. void set_visualize_leading_whitespace(bool);
  56. bool visualize_leading_whitespace() const { return m_visualize_leading_whitespace; }
  57. bool is_cursor_line_highlighted() const { return m_cursor_line_highlighting; }
  58. void set_cursor_line_highlighting(bool);
  59. virtual bool is_automatic_indentation_enabled() const final { return m_automatic_indentation_enabled; }
  60. void set_automatic_indentation_enabled(bool enabled) { m_automatic_indentation_enabled = enabled; }
  61. virtual int soft_tab_width() const final { return m_soft_tab_width; }
  62. void set_soft_tab_width(int width) { m_soft_tab_width = width; };
  63. WrappingMode wrapping_mode() const { return m_wrapping_mode; }
  64. bool is_wrapping_enabled() const { return m_wrapping_mode != WrappingMode::NoWrap; }
  65. void set_wrapping_mode(WrappingMode);
  66. Gfx::TextAlignment text_alignment() const { return m_text_alignment; }
  67. void set_text_alignment(Gfx::TextAlignment);
  68. Type type() const { return m_type; }
  69. bool is_single_line() const { return m_type == SingleLine; }
  70. bool is_multi_line() const { return m_type == MultiLine; }
  71. Mode mode() const { return m_mode; }
  72. bool is_editable() const { return m_mode == Editable; }
  73. bool is_readonly() const { return m_mode == ReadOnly; }
  74. bool is_displayonly() const { return m_mode == DisplayOnly; }
  75. void set_mode(const Mode);
  76. bool is_ruler_visible() const { return m_ruler_visible; }
  77. void set_ruler_visible(bool);
  78. bool is_gutter_visible() const { return m_gutter_visible; }
  79. void set_gutter_visible(bool);
  80. void set_icon(Gfx::Bitmap const*);
  81. Gfx::Bitmap const* icon() const { return m_icon; }
  82. Function<void()> on_cursor_change;
  83. Function<void()> on_selection_change;
  84. Function<void()> on_focusin;
  85. Function<void()> on_focusout;
  86. Function<void()> on_highlighter_change;
  87. void set_text(StringView, AllowCallback = AllowCallback::Yes);
  88. void scroll_cursor_into_view();
  89. void scroll_position_into_view(TextPosition const&);
  90. size_t line_count() const { return document().line_count(); }
  91. TextDocumentLine& line(size_t index) { return document().line(index); }
  92. TextDocumentLine const& line(size_t index) const { return document().line(index); }
  93. NonnullOwnPtrVector<TextDocumentLine>& lines() { return document().lines(); }
  94. NonnullOwnPtrVector<TextDocumentLine> const& lines() const { return document().lines(); }
  95. int line_height() const;
  96. TextPosition cursor() const { return m_cursor; }
  97. TextRange normalized_selection() const { return m_selection.normalized(); }
  98. void insert_at_cursor_or_replace_selection(StringView);
  99. void replace_all_text_without_resetting_undo_stack(StringView text);
  100. bool write_to_file(String const& path);
  101. bool write_to_file(Core::File&);
  102. bool has_selection() const { return m_selection.is_valid(); }
  103. String selected_text() const;
  104. size_t number_of_words() const;
  105. size_t number_of_selected_words() const;
  106. void set_selection(TextRange const&);
  107. void clear_selection();
  108. bool can_undo() const { return document().can_undo(); }
  109. bool can_redo() const { return document().can_redo(); }
  110. String text() const;
  111. void clear();
  112. void cut();
  113. void copy();
  114. void paste();
  115. void do_delete();
  116. void delete_current_line();
  117. void delete_previous_word();
  118. void delete_previous_char();
  119. void delete_from_line_start_to_cursor();
  120. void select_all();
  121. void select_current_line();
  122. virtual void undo();
  123. virtual void redo();
  124. Function<void()> on_change;
  125. Function<void(bool modified)> on_modified_change;
  126. Function<void()> on_mousedown;
  127. Function<void()> on_return_pressed;
  128. Function<void()> on_shift_return_pressed;
  129. Function<void()> on_ctrl_return_pressed;
  130. Function<void()> on_escape_pressed;
  131. Function<void()> on_up_pressed;
  132. Function<void()> on_down_pressed;
  133. Function<void()> on_pageup_pressed;
  134. Function<void()> on_pagedown_pressed;
  135. Action& undo_action() { return *m_undo_action; }
  136. Action& redo_action() { return *m_redo_action; }
  137. Action& cut_action() { return *m_cut_action; }
  138. Action& copy_action() { return *m_copy_action; }
  139. Action& paste_action() { return *m_paste_action; }
  140. Action& go_to_line_action() { return *m_go_to_line_action; }
  141. Action& select_all_action() { return *m_select_all_action; }
  142. void add_custom_context_menu_action(Action&);
  143. void set_cursor_and_focus_line(size_t line, size_t column);
  144. void set_cursor(size_t line, size_t column);
  145. virtual void set_cursor(TextPosition const&);
  146. Syntax::Highlighter* syntax_highlighter();
  147. Syntax::Highlighter const* syntax_highlighter() const;
  148. void set_syntax_highlighter(OwnPtr<Syntax::Highlighter>);
  149. AutocompleteProvider const* autocomplete_provider() const;
  150. void set_autocomplete_provider(OwnPtr<AutocompleteProvider>&&);
  151. EditingEngine const* editing_engine() const;
  152. void set_editing_engine(OwnPtr<EditingEngine>);
  153. bool should_autocomplete_automatically() const { return m_autocomplete_timer; }
  154. void set_should_autocomplete_automatically(bool);
  155. u32 substitution_code_point() const { return m_substitution_code_point; }
  156. void set_substitution_code_point(u32 code_point);
  157. bool is_in_drag_select() const { return m_in_drag_select; }
  158. TextRange& selection() { return m_selection; };
  159. void did_update_selection();
  160. void did_change(AllowCallback = AllowCallback::Yes);
  161. void update_cursor();
  162. void add_code_point(u32 code_point);
  163. void reset_cursor_blink();
  164. void update_selection(bool is_selecting);
  165. int number_of_visible_lines() const;
  166. Gfx::IntRect cursor_content_rect() const;
  167. TextPosition text_position_at_content_position(Gfx::IntPoint const&) const;
  168. void delete_text_range(TextRange);
  169. bool text_is_secret() const { return m_text_is_secret; }
  170. void set_text_is_secret(bool text_is_secret);
  171. void force_rehighlight();
  172. enum class SearchDirection {
  173. Forward,
  174. Backward,
  175. };
  176. TextRange find_text(StringView needle, SearchDirection, GUI::TextDocument::SearchShouldWrap, bool use_regex, bool match_case);
  177. void reset_search_results();
  178. Optional<size_t> search_result_index() const { return m_search_result_index; }
  179. Vector<TextRange> const& search_results() const { return m_search_results; }
  180. protected:
  181. explicit TextEditor(Type = Type::MultiLine);
  182. virtual void did_change_font() override;
  183. virtual void paint_event(PaintEvent&) override;
  184. virtual void mousedown_event(MouseEvent&) override;
  185. virtual void mouseup_event(MouseEvent&) override;
  186. virtual void mousemove_event(MouseEvent&) override;
  187. virtual void doubleclick_event(MouseEvent&) override;
  188. virtual void keydown_event(KeyEvent&) override;
  189. virtual void focusin_event(FocusEvent&) override;
  190. virtual void focusout_event(FocusEvent&) override;
  191. virtual void timer_event(Core::TimerEvent&) override;
  192. virtual void enter_event(Core::Event&) override;
  193. virtual void leave_event(Core::Event&) override;
  194. virtual void context_menu_event(ContextMenuEvent&) override;
  195. virtual void resize_event(ResizeEvent&) override;
  196. virtual void theme_change_event(ThemeChangeEvent&) override;
  197. virtual void cursor_did_change();
  198. Gfx::IntRect ruler_content_rect(size_t line) const;
  199. Gfx::IntRect gutter_content_rect(size_t line) const;
  200. TextPosition text_position_at(Gfx::IntPoint const&) const;
  201. bool ruler_visible() const { return m_ruler_visible; }
  202. bool gutter_visible() const { return m_gutter_visible; }
  203. Gfx::IntRect content_rect_for_position(TextPosition const&) const;
  204. int ruler_width() const;
  205. int gutter_width() const;
  206. virtual void highlighter_did_set_spans(Vector<TextDocumentSpan> spans) final { document().set_spans(Syntax::HighlighterClient::span_collection_index, move(spans)); }
  207. private:
  208. friend class TextDocumentLine;
  209. // ^TextDocument::Client
  210. virtual void document_did_append_line() override;
  211. virtual void document_did_insert_line(size_t) override;
  212. virtual void document_did_remove_line(size_t) override;
  213. virtual void document_did_remove_all_lines() override;
  214. virtual void document_did_change(AllowCallback = AllowCallback::Yes) override;
  215. virtual void document_did_set_text(AllowCallback = AllowCallback::Yes) override;
  216. virtual void document_did_set_cursor(TextPosition const&) override;
  217. virtual void document_did_update_undo_stack() override;
  218. // ^Syntax::HighlighterClient
  219. virtual Vector<TextDocumentSpan>& spans() final { return document().spans(); }
  220. virtual Vector<TextDocumentSpan> const& spans() const final { return document().spans(); }
  221. virtual void set_span_at_index(size_t index, TextDocumentSpan span) final { document().set_span_at_index(index, move(span)); }
  222. virtual void highlighter_did_request_update() final { update(); }
  223. virtual String highlighter_did_request_text() const final { return text(); }
  224. virtual GUI::TextDocument& highlighter_did_request_document() final { return document(); }
  225. virtual GUI::TextPosition highlighter_did_request_cursor() const final { return m_cursor; }
  226. // ^Clipboard::ClipboardClient
  227. virtual void clipboard_content_did_change(String const& mime_type) override;
  228. void create_actions();
  229. void paint_ruler(Painter&);
  230. void update_content_size();
  231. int fixed_glyph_width() const;
  232. void defer_reflow();
  233. void undefer_reflow();
  234. enum UserRequestedAutocomplete {
  235. No,
  236. Yes
  237. };
  238. void try_show_autocomplete(UserRequestedAutocomplete);
  239. void try_update_autocomplete(Function<void()> callback = {});
  240. void force_update_autocomplete(Function<void()> callback = {});
  241. void hide_autocomplete_if_needed();
  242. void hide_autocomplete();
  243. int icon_size() const { return 16; }
  244. int icon_padding() const { return 2; }
  245. class ReflowDeferrer {
  246. public:
  247. ReflowDeferrer(TextEditor& editor)
  248. : m_editor(editor)
  249. {
  250. m_editor.defer_reflow();
  251. }
  252. ~ReflowDeferrer()
  253. {
  254. m_editor.undefer_reflow();
  255. }
  256. private:
  257. TextEditor& m_editor;
  258. };
  259. int text_width_for_font(auto const& text_view, Gfx::Font const&) const;
  260. Utf32View substitution_code_point_view(size_t length) const;
  261. Gfx::IntRect line_content_rect(size_t item_index) const;
  262. Gfx::IntRect line_widget_rect(size_t line_index) const;
  263. void delete_selection();
  264. int content_x_for_position(TextPosition const&) const;
  265. Gfx::IntRect ruler_rect_in_inner_coordinates() const;
  266. Gfx::IntRect gutter_rect_in_inner_coordinates() const;
  267. Gfx::IntRect visible_text_rect_in_inner_coordinates() const;
  268. void recompute_all_visual_lines();
  269. void ensure_cursor_is_valid();
  270. void rehighlight_if_needed();
  271. size_t visual_line_containing(size_t line_index, size_t column) const;
  272. void recompute_visual_lines(size_t line_index);
  273. void automatic_selection_scroll_timer_fired();
  274. template<class T, class... Args>
  275. inline void execute(Args&&... args)
  276. {
  277. auto command = make<T>(*m_document, forward<Args>(args)...);
  278. command->perform_formatting(*this);
  279. will_execute(*command);
  280. command->execute_from(*this);
  281. m_document->add_to_undo_stack(move(command));
  282. }
  283. virtual void will_execute(TextDocumentUndoCommand const&) { }
  284. void on_search_results(GUI::TextRange current, Vector<GUI::TextRange> all_results);
  285. static constexpr auto search_results_span_collection_index = 1;
  286. Type m_type { MultiLine };
  287. Mode m_mode { Editable };
  288. TextPosition m_cursor;
  289. Gfx::TextAlignment m_text_alignment { Gfx::TextAlignment::CenterLeft };
  290. bool m_cursor_state { true };
  291. bool m_in_drag_select { false };
  292. bool m_ruler_visible { false };
  293. bool m_gutter_visible { false };
  294. bool m_needs_rehighlight { false };
  295. bool m_has_pending_change_notification { false };
  296. bool m_automatic_indentation_enabled { false };
  297. WrappingMode m_wrapping_mode { WrappingMode::NoWrap };
  298. bool m_visualize_trailing_whitespace { true };
  299. bool m_visualize_leading_whitespace { false };
  300. bool m_cursor_line_highlighting { true };
  301. size_t m_soft_tab_width { 4 };
  302. int m_horizontal_content_padding { 3 };
  303. TextRange m_selection;
  304. // NOTE: If non-zero, all glyphs will be substituted with this one.
  305. u32 m_substitution_code_point { 0 };
  306. mutable OwnPtr<Vector<u32>> m_substitution_string_data; // Used to avoid repeated String construction.
  307. RefPtr<Menu> m_context_menu;
  308. RefPtr<Action> m_undo_action;
  309. RefPtr<Action> m_redo_action;
  310. RefPtr<Action> m_cut_action;
  311. RefPtr<Action> m_copy_action;
  312. RefPtr<Action> m_paste_action;
  313. RefPtr<Action> m_go_to_line_action;
  314. RefPtr<Action> m_select_all_action;
  315. Core::ElapsedTimer m_triple_click_timer;
  316. NonnullRefPtrVector<Action> m_custom_context_menu_actions;
  317. size_t m_reflow_deferred { 0 };
  318. bool m_reflow_requested { false };
  319. bool is_visual_data_up_to_date() const { return !m_reflow_requested; }
  320. RefPtr<TextDocument> m_document;
  321. String m_placeholder { "" };
  322. template<typename Callback>
  323. void for_each_visual_line(size_t line_index, Callback) const;
  324. struct LineVisualData {
  325. Vector<size_t, 1> visual_line_breaks;
  326. Gfx::IntRect visual_rect;
  327. };
  328. NonnullOwnPtrVector<LineVisualData> m_line_visual_data;
  329. OwnPtr<Syntax::Highlighter> m_highlighter;
  330. OwnPtr<AutocompleteProvider> m_autocomplete_provider;
  331. OwnPtr<AutocompleteBox> m_autocomplete_box;
  332. bool m_should_keep_autocomplete_box { false };
  333. size_t m_automatic_autocomplete_delay_ms { 800 };
  334. RefPtr<Core::Timer> m_automatic_selection_scroll_timer;
  335. RefPtr<Core::Timer> m_autocomplete_timer;
  336. OwnPtr<EditingEngine> m_editing_engine;
  337. Gfx::IntPoint m_last_mousemove_position;
  338. RefPtr<Gfx::Bitmap> m_icon;
  339. bool m_text_is_secret { false };
  340. Optional<size_t> m_search_result_index;
  341. Vector<GUI::TextRange> m_search_results;
  342. };
  343. }