GTextEditor.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. Function<void(GTextEditor&)> on_return_pressed;
  73. virtual const char* class_name() const override { return "GTextEditor"; }
  74. private:
  75. virtual void paint_event(GPaintEvent&) override;
  76. virtual void mousedown_event(GMouseEvent&) override;
  77. virtual void mouseup_event(GMouseEvent&) override;
  78. virtual void mousemove_event(GMouseEvent&) override;
  79. virtual void keydown_event(GKeyEvent&) override;
  80. virtual void focusin_event(GEvent&) override;
  81. virtual void focusout_event(GEvent&) override;
  82. virtual void timer_event(GTimerEvent&) override;
  83. virtual bool accepts_focus() const override { return true; }
  84. void paint_ruler(Painter&);
  85. void update_content_size();
  86. class Line {
  87. friend class GTextEditor;
  88. public:
  89. Line();
  90. const char* characters() const { return m_text.data(); }
  91. int length() const { return m_text.size() - 1; }
  92. int width(const Font&) const;
  93. void set_text(const String&);
  94. void append(char);
  95. void prepend(char);
  96. void insert(int index, char);
  97. void remove(int index);
  98. void append(const char*, int);
  99. void truncate(int length);
  100. void clear();
  101. private:
  102. // NOTE: This vector is null terminated.
  103. Vector<char> m_text;
  104. };
  105. Rect line_content_rect(int item_index) const;
  106. Rect line_widget_rect(int line_index) const;
  107. Rect cursor_content_rect() const;
  108. void update_cursor();
  109. void set_cursor(int line, int column);
  110. void set_cursor(const GTextPosition&);
  111. Line& current_line() { return *m_lines[m_cursor.line()]; }
  112. const Line& current_line() const { return *m_lines[m_cursor.line()]; }
  113. GTextPosition text_position_at(const Point&) const;
  114. void insert_at_cursor(char);
  115. void insert_at_cursor(const String&);
  116. int ruler_width() const;
  117. Rect ruler_content_rect(int line) const;
  118. void toggle_selection_if_needed_for_event(const GKeyEvent&);
  119. void insert_at_cursor_or_replace_selection(const String&);
  120. void delete_selection();
  121. Type m_type { MultiLine };
  122. Vector<OwnPtr<Line>> m_lines;
  123. GTextPosition m_cursor;
  124. bool m_cursor_state { true };
  125. bool m_in_drag_select { false };
  126. bool m_ruler_visible { true };
  127. int m_line_spacing { 4 };
  128. int m_soft_tab_width { 4 };
  129. int m_horizontal_content_padding { 2 };
  130. GTextRange m_selection;
  131. };