GTextEditor: Allow moving the cursor by clicking.

This commit is contained in:
Andreas Kling 2019-03-07 14:02:10 +01:00
parent 9997992907
commit a21ecd440a
Notes: sideshowbarker 2024-07-19 15:08:40 +09:00
2 changed files with 9 additions and 5 deletions

View File

@ -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

View File

@ -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;