GTextEditor.h 6.6 KB

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