GTextEditor.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. Function<void(GTextEditor&)> on_cursor_change;
  56. void set_text(const String&);
  57. void scroll_cursor_into_view();
  58. int line_count() const { return m_lines.size(); }
  59. int line_spacing() const { return m_line_spacing; }
  60. int line_height() const { return font().glyph_height() + m_line_spacing; }
  61. GTextPosition cursor() const { return m_cursor; }
  62. GTextRange normalized_selection() const { return m_selection.normalized(); }
  63. int glyph_width() const { return font().glyph_width('x'); }
  64. bool write_to_file(const String& path);
  65. bool has_selection() const { return m_selection.is_valid(); }
  66. String selected_text() const;
  67. String text() const;
  68. void clear();
  69. void cut();
  70. void copy();
  71. void paste();
  72. void do_delete();
  73. void delete_current_line();
  74. Function<void(GTextEditor&)> on_return_pressed;
  75. Function<void(GTextEditor&)> on_escape_pressed;
  76. virtual const char* class_name() const override { return "GTextEditor"; }
  77. private:
  78. virtual void paint_event(GPaintEvent&) override;
  79. virtual void mousedown_event(GMouseEvent&) override;
  80. virtual void mouseup_event(GMouseEvent&) override;
  81. virtual void mousemove_event(GMouseEvent&) override;
  82. virtual void keydown_event(GKeyEvent&) override;
  83. virtual void focusin_event(GEvent&) override;
  84. virtual void focusout_event(GEvent&) override;
  85. virtual void timer_event(GTimerEvent&) override;
  86. virtual bool accepts_focus() const override { return true; }
  87. virtual void enter_event(GEvent&) override;
  88. virtual void leave_event(GEvent&) override;
  89. void paint_ruler(Painter&);
  90. void update_content_size();
  91. class Line {
  92. friend class GTextEditor;
  93. public:
  94. Line();
  95. const char* characters() const { return m_text.data(); }
  96. int length() const { return m_text.size() - 1; }
  97. int width(const Font&) const;
  98. void set_text(const String&);
  99. void append(char);
  100. void prepend(char);
  101. void insert(int index, char);
  102. void remove(int index);
  103. void append(const char*, int);
  104. void truncate(int length);
  105. void clear();
  106. private:
  107. // NOTE: This vector is null terminated.
  108. Vector<char> m_text;
  109. };
  110. Rect line_content_rect(int item_index) const;
  111. Rect line_widget_rect(int line_index) const;
  112. Rect cursor_content_rect() const;
  113. void update_cursor();
  114. void set_cursor(int line, int column);
  115. void set_cursor(const GTextPosition&);
  116. Line& current_line() { return *m_lines[m_cursor.line()]; }
  117. const Line& current_line() const { return *m_lines[m_cursor.line()]; }
  118. GTextPosition text_position_at(const Point&) const;
  119. void insert_at_cursor(char);
  120. void insert_at_cursor(const String&);
  121. int ruler_width() const;
  122. Rect ruler_content_rect(int line) const;
  123. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  124. void insert_at_cursor_or_replace_selection(const String&);
  125. void delete_selection();
  126. Type m_type { MultiLine };
  127. Vector<OwnPtr<Line>> m_lines;
  128. GTextPosition m_cursor;
  129. bool m_cursor_state { true };
  130. bool m_in_drag_select { false };
  131. bool m_ruler_visible { true };
  132. int m_line_spacing { 4 };
  133. int m_soft_tab_width { 4 };
  134. int m_horizontal_content_padding { 2 };
  135. GTextRange m_selection;
  136. };