summaryrefslogtreecommitdiff
path: root/LibGUI
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-03-07 14:35:32 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-03-07 14:35:32 +0100
commitce35cddb1b762bb4c21e09cfd38e33ddcae4393a (patch)
treecee92ba617f8ce1843b402c62b894f9886d1c84d /LibGUI
parenta21ecd440a548db117fcbff4eaf25f82c57f5855 (diff)
downloadserenity-ce35cddb1b762bb4c21e09cfd38e33ddcae4393a.zip
GTextEditor: Let's use a Vector for the line backing store.
I'm eventually gonna want to replace this with something more clever, like a automagically splicing vector or something, but for now, at least we move away from immutable Strings.
Diffstat (limited to 'LibGUI')
-rw-r--r--LibGUI/GTextEditor.cpp27
-rw-r--r--LibGUI/GTextEditor.h12
2 files changed, 26 insertions, 13 deletions
diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp
index 4a5accc041..77ebe37b37 100644
--- a/LibGUI/GTextEditor.cpp
+++ b/LibGUI/GTextEditor.cpp
@@ -112,7 +112,7 @@ void GTextEditor::paint_event(GPaintEvent& event)
line_rect.set_width(exposed_width);
if (i == m_cursor.line() && is_focused())
painter.fill_rect(line_rect, Color(230, 230, 230));
- painter.draw_text(line_rect, line.text(), TextAlignment::CenterLeft, Color::Black);
+ painter.draw_text(line_rect, line.characters(), line.length(), TextAlignment::CenterLeft, Color::Black);
}
if (is_focused() && m_cursor_state)
@@ -176,9 +176,17 @@ void GTextEditor::keydown_event(GKeyEvent& event)
set_cursor(line_count() - 1, m_lines[line_count() - 1].length());
return;
}
+
+ if (!event.text().is_empty())
+ insert_at_cursor(event.text()[0]);
+
return GWidget::keydown_event(event);
}
+void GTextEditor::insert_at_cursor(char ch)
+{
+}
+
Rect GTextEditor::visible_content_rect() const
{
return {
@@ -194,7 +202,7 @@ Rect GTextEditor::cursor_content_rect() const
if (!m_cursor.is_valid())
return { };
ASSERT(!m_lines.is_empty());
- ASSERT(m_cursor.column() <= (current_line().text().length() + 1));
+ ASSERT(m_cursor.column() <= (current_line().length() + 1));
return { m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() };
}
@@ -280,17 +288,20 @@ void GTextEditor::timer_event(GTimerEvent&)
update_cursor();
}
+GTextEditor::Line::Line()
+{
+ m_text.append(0);
+}
+
void GTextEditor::Line::set_text(const String& text)
{
- if (text == m_text)
+ if (text.length() == length() && !memcmp(text.characters(), characters(), length()))
return;
- m_text = text;
- m_cached_width = -1;
+ m_text.resize(text.length() + 1);
+ memcpy(m_text.data(), text.characters(), text.length() + 1);
}
int GTextEditor::Line::width(const Font& font) const
{
- if (m_cached_width < 0)
- m_cached_width = font.width(m_text);
- return m_cached_width;
+ return font.glyph_width('x') * length();
}
diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h
index 50c9942e13..d56a898a6e 100644
--- a/LibGUI/GTextEditor.h
+++ b/LibGUI/GTextEditor.h
@@ -57,18 +57,20 @@ private:
virtual void timer_event(GTimerEvent&) override;
virtual bool accepts_focus() const override { return true; }
+ void insert_at_cursor(char);
+
class Line {
public:
- Line() { }
+ Line();
- String text() const { return m_text; }
- int length() const { return m_text.length(); }
+ const char* characters() const { return m_text.data(); }
+ int length() const { return m_text.size() - 1; }
int width(const Font&) const;
void set_text(const String&);
private:
- String m_text;
- mutable int m_cached_width { -1 };
+ // NOTE: This vector is null terminated.
+ Vector<char> m_text;
};
void update_scrollbar_ranges();