diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-03-07 14:02:10 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-03-07 14:02:10 +0100 |
commit | a21ecd440a548db117fcbff4eaf25f82c57f5855 (patch) | |
tree | e938778f0410237fa9fa0520574a696791cd2ac0 | |
parent | 9997992907d2ecb5c86f17590c63a6284643c589 (diff) | |
download | serenity-a21ecd440a548db117fcbff4eaf25f82c57f5855.zip |
GTextEditor: Allow moving the cursor by clicking.
-rw-r--r-- | LibGUI/GTextEditor.cpp | 13 | ||||
-rw-r--r-- | LibGUI/GTextEditor.h | 1 |
2 files changed, 9 insertions, 5 deletions
diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index 43ec411d4a..4a5accc041 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -88,7 +88,13 @@ int GTextEditor::content_width() const void GTextEditor::mousedown_event(GMouseEvent& event) { - (void)event; + auto position = event.position(); + position.move_by(m_horizontal_scrollbar->value(), m_vertical_scrollbar->value()); + position.move_by(-padding(), -padding()); + int line_index = position.y() / line_height(); + int column_index = position.x() / glyph_width(); + column_index = min(column_index, m_lines[line_index].length()); + set_cursor(line_index, column_index); } void GTextEditor::paint_event(GPaintEvent& event) @@ -189,10 +195,7 @@ Rect GTextEditor::cursor_content_rect() const return { }; ASSERT(!m_lines.is_empty()); ASSERT(m_cursor.column() <= (current_line().text().length() + 1)); - int x = 0; - for (int i = 0; i < m_cursor.column(); ++i) - x += font().glyph_width(current_line().text()[i]); - return { x, m_cursor.line() * line_height(), 1, line_height() }; + return { m_cursor.column() * glyph_width(), m_cursor.line() * line_height(), 1, line_height() }; } Rect GTextEditor::line_widget_rect(int line_index) const diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h index 044e253a4e..50c9942e13 100644 --- a/LibGUI/GTextEditor.h +++ b/LibGUI/GTextEditor.h @@ -45,6 +45,7 @@ public: int line_height() const { return font().glyph_height() + m_line_spacing; } int padding() const { return 3; } GTextPosition cursor() const { return m_cursor; } + int glyph_width() const { return font().glyph_width('x'); } private: virtual void paint_event(GPaintEvent&) override; |