From 9e5c5627d5da2fc7a08f75016d48c043af980bc4 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 21 Aug 2019 19:32:39 +0200 Subject: [PATCH] GTextEditor: Give Line objects a back-reference to the GTextEditor This will allow us to do more complicated things in Line without having to pass the editor around all the time. --- Libraries/LibGUI/GTextEditor.cpp | 20 +++++++++++--------- Libraries/LibGUI/GTextEditor.h | 6 ++++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Libraries/LibGUI/GTextEditor.cpp b/Libraries/LibGUI/GTextEditor.cpp index 60b0c703e62..85c3ccc2c46 100644 --- a/Libraries/LibGUI/GTextEditor.cpp +++ b/Libraries/LibGUI/GTextEditor.cpp @@ -24,7 +24,7 @@ GTextEditor::GTextEditor(Type type, GWidget* parent) set_font(GFontDatabase::the().get_by_name("Csilla Thin")); // FIXME: Recompute vertical scrollbar step size on font change. vertical_scrollbar().set_step(line_height()); - m_lines.append(make()); + m_lines.append(make(*this)); m_cursor = { 0, 0 }; create_actions(); } @@ -77,7 +77,7 @@ void GTextEditor::set_text(const StringView& text) auto add_line = [&](int current_position) { int line_length = current_position - start_of_current_line; - auto line = make(); + auto line = make(*this); if (line_length) line->set_text(text.substring_view(start_of_current_line, current_position - start_of_current_line)); m_lines.append(move(line)); @@ -558,7 +558,7 @@ void GTextEditor::delete_current_line() m_lines.remove(m_cursor.line()); if (m_lines.is_empty()) - m_lines.append(make()); + m_lines.append(make(*this)); update_content_size(); update(); @@ -619,13 +619,13 @@ void GTextEditor::insert_at_cursor(char ch) if (leading_spaces) new_line_contents = String::repeated(' ', leading_spaces); } - m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make(new_line_contents)); + m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make(*this, new_line_contents)); update(); did_change(); set_cursor(m_cursor.line() + 1, m_lines[m_cursor.line() + 1].length()); return; } - auto new_line = make(); + auto new_line = make(*this); new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column()); current_line().truncate(m_cursor.column()); m_lines.insert(m_cursor.line() + 1, move(new_line)); @@ -764,12 +764,14 @@ void GTextEditor::timer_event(CTimerEvent&) update_cursor(); } -GTextEditor::Line::Line() +GTextEditor::Line::Line(GTextEditor& editor) + : m_editor(editor) { clear(); } -GTextEditor::Line::Line(const StringView& text) +GTextEditor::Line::Line(GTextEditor& editor, const StringView& text) + : m_editor(editor) { set_text(text); } @@ -888,7 +890,7 @@ String GTextEditor::text() const void GTextEditor::clear() { m_lines.clear(); - m_lines.append(make()); + m_lines.append(make(*this)); m_selection.clear(); did_update_selection(); set_cursor(0, 0); @@ -956,7 +958,7 @@ void GTextEditor::delete_selection() } if (m_lines.is_empty()) - m_lines.append(make()); + m_lines.append(make(*this)); m_selection.clear(); did_update_selection(); diff --git a/Libraries/LibGUI/GTextEditor.h b/Libraries/LibGUI/GTextEditor.h index 22364e69cd6..1f37f251aa8 100644 --- a/Libraries/LibGUI/GTextEditor.h +++ b/Libraries/LibGUI/GTextEditor.h @@ -167,8 +167,8 @@ private: friend class GTextEditor; public: - Line(); - explicit Line(const StringView&); + explicit Line(GTextEditor&); + Line(GTextEditor&, const StringView&); const char* characters() const { return m_text.data(); } int length() const { return m_text.size() - 1; } @@ -183,6 +183,8 @@ private: void clear(); private: + GTextEditor& m_editor; + // NOTE: This vector is null terminated. Vector m_text; };