TextEditor.h 9.6 KB

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