mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 20:47:45 +00:00
GTextEditor: More work on basic line editing. Insert/remove characters.
This commit is contained in:
parent
8425ea971a
commit
6094f592a9
2 changed files with 47 additions and 0 deletions
|
@ -186,6 +186,14 @@ void GTextEditor::keydown_event(GKeyEvent& event)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!event.modifiers() && event.key() == KeyCode::Key_Backspace) {
|
||||||
|
if (m_cursor.column() > 0) {
|
||||||
|
current_line().remove(m_cursor.column() - 1);
|
||||||
|
set_cursor(m_cursor.line(), m_cursor.column() - 1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!event.text().is_empty())
|
if (!event.text().is_empty())
|
||||||
insert_at_cursor(event.text()[0]);
|
insert_at_cursor(event.text()[0]);
|
||||||
|
|
||||||
|
@ -204,7 +212,11 @@ void GTextEditor::insert_at_cursor(char ch)
|
||||||
update();
|
update();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
current_line().insert(m_cursor.column(), ch);
|
||||||
|
set_cursor(m_cursor.line(), m_cursor.column() + 1);
|
||||||
|
update_cursor();
|
||||||
}
|
}
|
||||||
|
|
||||||
Rect GTextEditor::visible_content_rect() const
|
Rect GTextEditor::visible_content_rect() const
|
||||||
|
@ -330,3 +342,33 @@ int GTextEditor::Line::width(const Font& font) const
|
||||||
{
|
{
|
||||||
return font.glyph_width('x') * length();
|
return font.glyph_width('x') * length();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GTextEditor::Line::append(char ch)
|
||||||
|
{
|
||||||
|
insert(length(), ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GTextEditor::Line::prepend(char ch)
|
||||||
|
{
|
||||||
|
insert(0, ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GTextEditor::Line::insert(int index, char ch)
|
||||||
|
{
|
||||||
|
if (index == length()) {
|
||||||
|
m_text.last() = ch;
|
||||||
|
m_text.append(0);
|
||||||
|
} else {
|
||||||
|
m_text.insert(index, move(ch));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GTextEditor::Line::remove(int index)
|
||||||
|
{
|
||||||
|
if (index == length()) {
|
||||||
|
m_text.take_last();
|
||||||
|
m_text.last() = 0;
|
||||||
|
} else {
|
||||||
|
m_text.remove(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -60,6 +60,7 @@ private:
|
||||||
virtual bool accepts_focus() const override { return true; }
|
virtual bool accepts_focus() const override { return true; }
|
||||||
|
|
||||||
class Line {
|
class Line {
|
||||||
|
friend class GTextEditor;
|
||||||
public:
|
public:
|
||||||
Line();
|
Line();
|
||||||
|
|
||||||
|
@ -67,6 +68,10 @@ private:
|
||||||
int length() const { return m_text.size() - 1; }
|
int length() const { return m_text.size() - 1; }
|
||||||
int width(const Font&) const;
|
int width(const Font&) const;
|
||||||
void set_text(const String&);
|
void set_text(const String&);
|
||||||
|
void append(char);
|
||||||
|
void prepend(char);
|
||||||
|
void insert(int index, char);
|
||||||
|
void remove(int index);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// NOTE: This vector is null terminated.
|
// NOTE: This vector is null terminated.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue