1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 04:07:44 +00:00

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.
This commit is contained in:
Andreas Kling 2019-08-21 19:32:39 +02:00
parent 0b3308f995
commit 9e5c5627d5
2 changed files with 15 additions and 11 deletions

View file

@ -24,7 +24,7 @@ GTextEditor::GTextEditor(Type type, GWidget* parent)
set_font(GFontDatabase::the().get_by_name("Csilla Thin")); set_font(GFontDatabase::the().get_by_name("Csilla Thin"));
// FIXME: Recompute vertical scrollbar step size on font change. // FIXME: Recompute vertical scrollbar step size on font change.
vertical_scrollbar().set_step(line_height()); vertical_scrollbar().set_step(line_height());
m_lines.append(make<Line>()); m_lines.append(make<Line>(*this));
m_cursor = { 0, 0 }; m_cursor = { 0, 0 };
create_actions(); create_actions();
} }
@ -77,7 +77,7 @@ void GTextEditor::set_text(const StringView& text)
auto add_line = [&](int current_position) { auto add_line = [&](int current_position) {
int line_length = current_position - start_of_current_line; int line_length = current_position - start_of_current_line;
auto line = make<Line>(); auto line = make<Line>(*this);
if (line_length) if (line_length)
line->set_text(text.substring_view(start_of_current_line, current_position - start_of_current_line)); line->set_text(text.substring_view(start_of_current_line, current_position - start_of_current_line));
m_lines.append(move(line)); m_lines.append(move(line));
@ -558,7 +558,7 @@ void GTextEditor::delete_current_line()
m_lines.remove(m_cursor.line()); m_lines.remove(m_cursor.line());
if (m_lines.is_empty()) if (m_lines.is_empty())
m_lines.append(make<Line>()); m_lines.append(make<Line>(*this));
update_content_size(); update_content_size();
update(); update();
@ -619,13 +619,13 @@ void GTextEditor::insert_at_cursor(char ch)
if (leading_spaces) if (leading_spaces)
new_line_contents = String::repeated(' ', leading_spaces); new_line_contents = String::repeated(' ', leading_spaces);
} }
m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>(new_line_contents)); m_lines.insert(m_cursor.line() + (at_tail ? 1 : 0), make<Line>(*this, new_line_contents));
update(); update();
did_change(); did_change();
set_cursor(m_cursor.line() + 1, m_lines[m_cursor.line() + 1].length()); set_cursor(m_cursor.line() + 1, m_lines[m_cursor.line() + 1].length());
return; return;
} }
auto new_line = make<Line>(); auto new_line = make<Line>(*this);
new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column()); new_line->append(current_line().characters() + m_cursor.column(), current_line().length() - m_cursor.column());
current_line().truncate(m_cursor.column()); current_line().truncate(m_cursor.column());
m_lines.insert(m_cursor.line() + 1, move(new_line)); m_lines.insert(m_cursor.line() + 1, move(new_line));
@ -764,12 +764,14 @@ void GTextEditor::timer_event(CTimerEvent&)
update_cursor(); update_cursor();
} }
GTextEditor::Line::Line() GTextEditor::Line::Line(GTextEditor& editor)
: m_editor(editor)
{ {
clear(); clear();
} }
GTextEditor::Line::Line(const StringView& text) GTextEditor::Line::Line(GTextEditor& editor, const StringView& text)
: m_editor(editor)
{ {
set_text(text); set_text(text);
} }
@ -888,7 +890,7 @@ String GTextEditor::text() const
void GTextEditor::clear() void GTextEditor::clear()
{ {
m_lines.clear(); m_lines.clear();
m_lines.append(make<Line>()); m_lines.append(make<Line>(*this));
m_selection.clear(); m_selection.clear();
did_update_selection(); did_update_selection();
set_cursor(0, 0); set_cursor(0, 0);
@ -956,7 +958,7 @@ void GTextEditor::delete_selection()
} }
if (m_lines.is_empty()) if (m_lines.is_empty())
m_lines.append(make<Line>()); m_lines.append(make<Line>(*this));
m_selection.clear(); m_selection.clear();
did_update_selection(); did_update_selection();

View file

@ -167,8 +167,8 @@ private:
friend class GTextEditor; friend class GTextEditor;
public: public:
Line(); explicit Line(GTextEditor&);
explicit Line(const StringView&); Line(GTextEditor&, const StringView&);
const char* characters() const { return m_text.data(); } const char* characters() const { return m_text.data(); }
int length() const { return m_text.size() - 1; } int length() const { return m_text.size() - 1; }
@ -183,6 +183,8 @@ private:
void clear(); void clear();
private: private:
GTextEditor& m_editor;
// NOTE: This vector is null terminated. // NOTE: This vector is null terminated.
Vector<char> m_text; Vector<char> m_text;
}; };