GTextEditor.h 9.3 KB

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