GTextEditor.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #pragma once
  2. #include <LibGUI/GScrollableWidget.h>
  3. #include <AK/Function.h>
  4. #include <AK/HashMap.h>
  5. #include <SharedGraphics/TextAlignment.h>
  6. class GAction;
  7. class GMenu;
  8. class GScrollBar;
  9. class Painter;
  10. class GTextPosition {
  11. public:
  12. GTextPosition() { }
  13. GTextPosition(int line, int column)
  14. : m_line(line)
  15. , m_column(column)
  16. {
  17. }
  18. bool is_valid() const { return m_line >= 0 && m_column >= 0; }
  19. int line() const { return m_line; }
  20. int column() const { return m_column; }
  21. void set_line(int line) { m_line = line; }
  22. void set_column(int column) { m_column = column; }
  23. bool operator==(const GTextPosition& other) const { return m_line == other.m_line && m_column == other.m_column; }
  24. bool operator!=(const GTextPosition& other) const { return m_line != other.m_line || m_column != other.m_column; }
  25. bool operator<(const GTextPosition& other) const { return m_line < other.m_line || (m_line == other.m_line && m_column < other.m_column); }
  26. private:
  27. int m_line { -1 };
  28. int m_column { -1 };
  29. };
  30. class GTextRange {
  31. public:
  32. GTextRange() { }
  33. GTextRange(const GTextPosition& start, const GTextPosition& end) : m_start(start) , m_end(end) { }
  34. bool is_valid() const { return m_start.is_valid() && m_end.is_valid(); }
  35. void clear() { m_start = { }; m_end = { }; }
  36. GTextPosition& start() { return m_start; }
  37. GTextPosition& end() { return m_end; }
  38. const GTextPosition& start() const { return m_start; }
  39. const GTextPosition& end() const { return m_end; }
  40. GTextRange normalized() const { return GTextRange(normalized_start(), normalized_end()); }
  41. void set_start(const GTextPosition& position) { m_start = position; }
  42. void set_end(const GTextPosition& position) { m_end = position; }
  43. void set(const GTextPosition& start, const GTextPosition& end) { m_start = start; m_end = end; }
  44. private:
  45. GTextPosition normalized_start() const { return m_start < m_end ? m_start : m_end; }
  46. GTextPosition normalized_end() const { return m_start < m_end ? m_end : m_start; }
  47. GTextPosition m_start;
  48. GTextPosition m_end;
  49. };
  50. class GTextEditor : public GScrollableWidget {
  51. public:
  52. enum Type { MultiLine, SingleLine };
  53. GTextEditor(Type, GWidget* parent);
  54. virtual ~GTextEditor() override;
  55. TextAlignment text_alignment() const { return m_text_alignment; }
  56. void set_text_alignment(TextAlignment);
  57. Type type() const { return m_type; }
  58. bool is_single_line() const { return m_type == SingleLine; }
  59. bool is_multi_line() const { return m_type == MultiLine; }
  60. bool is_ruler_visible() const { return m_ruler_visible; }
  61. void set_ruler_visible(bool b) { m_ruler_visible = b; }
  62. Function<void()> on_cursor_change;
  63. Function<void()> on_selection_change;
  64. void set_text(const String&);
  65. void scroll_cursor_into_view();
  66. int line_count() const { return m_lines.size(); }
  67. int line_spacing() const { return m_line_spacing; }
  68. int line_height() const { return font().glyph_height() + m_line_spacing; }
  69. GTextPosition cursor() const { return m_cursor; }
  70. GTextRange normalized_selection() const { return m_selection.normalized(); }
  71. // FIXME: This should take glyph spacing into account, no?
  72. int glyph_width() const { return font().glyph_width('x'); }
  73. bool write_to_file(const String& path);
  74. bool has_selection() const { return m_selection.is_valid(); }
  75. String selected_text() const;
  76. String text() const;
  77. void clear();
  78. void cut();
  79. void copy();
  80. void paste();
  81. void do_delete();
  82. void delete_current_line();
  83. Function<void()> on_change;
  84. Function<void()> on_return_pressed;
  85. Function<void()> on_escape_pressed;
  86. virtual const char* class_name() const override { return "GTextEditor"; }
  87. GAction& undo_action() { return *m_undo_action; }
  88. GAction& redo_action() { return *m_redo_action; }
  89. GAction& cut_action() { return *m_cut_action; }
  90. GAction& copy_action() { return *m_copy_action; }
  91. GAction& paste_action() { return *m_paste_action; }
  92. GAction& delete_action() { return *m_delete_action; }
  93. private:
  94. virtual void paint_event(GPaintEvent&) override;
  95. virtual void mousedown_event(GMouseEvent&) override;
  96. virtual void mouseup_event(GMouseEvent&) override;
  97. virtual void mousemove_event(GMouseEvent&) override;
  98. virtual void keydown_event(GKeyEvent&) override;
  99. virtual void focusin_event(CEvent&) override;
  100. virtual void focusout_event(CEvent&) override;
  101. virtual void timer_event(CTimerEvent&) override;
  102. virtual bool accepts_focus() const override { return true; }
  103. virtual void enter_event(CEvent&) override;
  104. virtual void leave_event(CEvent&) override;
  105. virtual void context_menu_event(GContextMenuEvent&) override;
  106. virtual void resize_event(GResizeEvent&) override;
  107. void create_actions();
  108. void paint_ruler(Painter&);
  109. void update_content_size();
  110. void did_change();
  111. class Line {
  112. friend class GTextEditor;
  113. public:
  114. Line();
  115. const char* characters() const { return m_text.data(); }
  116. int length() const { return m_text.size() - 1; }
  117. int width(const Font&) const;
  118. void set_text(const String&);
  119. void append(char);
  120. void prepend(char);
  121. void insert(int index, char);
  122. void remove(int index);
  123. void append(const char*, int);
  124. void truncate(int length);
  125. void clear();
  126. private:
  127. // NOTE: This vector is null terminated.
  128. Vector<char> m_text;
  129. };
  130. Rect line_content_rect(int item_index) const;
  131. Rect line_widget_rect(int line_index) const;
  132. Rect cursor_content_rect() const;
  133. void update_cursor();
  134. void set_cursor(int line, int column);
  135. void set_cursor(const GTextPosition&);
  136. Line& current_line() { return *m_lines[m_cursor.line()]; }
  137. const Line& current_line() const { return *m_lines[m_cursor.line()]; }
  138. GTextPosition text_position_at(const Point&) const;
  139. void insert_at_cursor(char);
  140. void insert_at_cursor(const String&);
  141. int ruler_width() const;
  142. Rect ruler_content_rect(int line) const;
  143. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  144. void insert_at_cursor_or_replace_selection(const String&);
  145. void delete_selection();
  146. void did_update_selection();
  147. int content_x_for_position(const GTextPosition&) const;
  148. Type m_type { MultiLine };
  149. Vector<OwnPtr<Line>> m_lines;
  150. GTextPosition m_cursor;
  151. TextAlignment m_text_alignment { TextAlignment::CenterLeft };
  152. bool m_cursor_state { true };
  153. bool m_in_drag_select { false };
  154. bool m_ruler_visible { false };
  155. bool m_have_pending_change_notification { false };
  156. int m_line_spacing { 4 };
  157. int m_soft_tab_width { 4 };
  158. int m_horizontal_content_padding { 2 };
  159. GTextRange m_selection;
  160. OwnPtr<GMenu> m_context_menu;
  161. RetainPtr<GAction> m_undo_action;
  162. RetainPtr<GAction> m_redo_action;
  163. RetainPtr<GAction> m_cut_action;
  164. RetainPtr<GAction> m_copy_action;
  165. RetainPtr<GAction> m_paste_action;
  166. RetainPtr<GAction> m_delete_action;
  167. };