GTextEditor.h 5.8 KB

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