From 6a6bcc5daff79e4cbccb6d9a33a95a2e5a6911bf Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 7 Mar 2019 13:21:51 +0100 Subject: [PATCH] GTextEditor: Add Home/End and Ctrl+Home/Ctrl+End navigation shortcuts. For start/end of line and start/end of document respectively. --- LibGUI/GTextEditor.cpp | 31 +++++++++++++++++++++++-------- LibGUI/GTextEditor.h | 23 +++++++++++++---------- 2 files changed, 36 insertions(+), 18 deletions(-) diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index f751133161..2f35fe9424 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -121,7 +121,7 @@ void GTextEditor::paint_event(GPaintEvent& event) void GTextEditor::keydown_event(GKeyEvent& event) { - if (event.key() == KeyCode::Key_Up) { + if (!event.modifiers() && event.key() == KeyCode::Key_Up) { if (m_cursor.line() > 0) { int new_line = m_cursor.line() - 1; int new_column = min(m_cursor.column(), m_lines[new_line].length()); @@ -129,7 +129,7 @@ void GTextEditor::keydown_event(GKeyEvent& event) } return; } - if (event.key() == KeyCode::Key_Down) { + if (!event.modifiers() && event.key() == KeyCode::Key_Down) { if (m_cursor.line() < (m_lines.size() - 1)) { int new_line = m_cursor.line() + 1; int new_column = min(m_cursor.column(), m_lines[new_line].length()); @@ -137,20 +137,36 @@ void GTextEditor::keydown_event(GKeyEvent& event) } return; } - if (event.key() == KeyCode::Key_Left) { + if (!event.modifiers() && event.key() == KeyCode::Key_Left) { if (m_cursor.column() > 0) { int new_column = m_cursor.column() - 1; set_cursor(m_cursor.line(), new_column); } return; } - if (event.key() == KeyCode::Key_Right) { - if (m_cursor.column() < (m_lines[m_cursor.line()].length())) { + if (!event.modifiers() && event.key() == KeyCode::Key_Right) { + if (m_cursor.column() < current_line().length()) { int new_column = m_cursor.column() + 1; set_cursor(m_cursor.line(), new_column); } return; } + if (!event.modifiers() && event.key() == KeyCode::Key_Home) { + set_cursor(m_cursor.line(), 0); + return; + } + if (!event.modifiers() && event.key() == KeyCode::Key_End) { + set_cursor(m_cursor.line(), current_line().length()); + return; + } + if (event.ctrl() && event.key() == KeyCode::Key_Home) { + set_cursor(0, 0); + return; + } + if (event.ctrl() && event.key() == KeyCode::Key_End) { + set_cursor(line_count() - 1, m_lines[line_count() - 1].length()); + return; + } return GWidget::keydown_event(event); } @@ -169,11 +185,10 @@ Rect GTextEditor::cursor_content_rect() const if (!m_cursor.is_valid()) return { }; ASSERT(!m_lines.is_empty()); - auto& line = m_lines[m_cursor.line()]; - ASSERT(m_cursor.column() <= (line.text().length() + 1)); + 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(line.text()[i]); + x += font().glyph_width(current_line().text()[i]); return { x, m_cursor.line() * line_height(), 1, line_height() }; } diff --git a/LibGUI/GTextEditor.h b/LibGUI/GTextEditor.h index 9040e94c5c..4ecc5d3138 100644 --- a/LibGUI/GTextEditor.h +++ b/LibGUI/GTextEditor.h @@ -55,16 +55,6 @@ private: virtual void timer_event(GTimerEvent&) override; virtual bool accepts_focus() const override { return true; } - void update_scrollbar_ranges(); - Rect line_content_rect(int item_index) const; - Rect line_widget_rect(int line_index) const; - Rect cursor_content_rect() const; - void update_cursor(); - void set_cursor(int line, int column); - - GScrollBar* m_vertical_scrollbar { nullptr }; - GScrollBar* m_horizontal_scrollbar { nullptr }; - class Line { public: Line() { } @@ -78,6 +68,19 @@ private: String m_text; mutable int m_cached_width { -1 }; }; + + void update_scrollbar_ranges(); + Rect line_content_rect(int item_index) const; + Rect line_widget_rect(int line_index) const; + Rect cursor_content_rect() const; + void update_cursor(); + void set_cursor(int line, int column); + Line& current_line() { return m_lines[m_cursor.line()]; } + const Line& current_line() const { return m_lines[m_cursor.line()]; } + + GScrollBar* m_vertical_scrollbar { nullptr }; + GScrollBar* m_horizontal_scrollbar { nullptr }; + Vector m_lines; GTextPosition m_cursor; bool m_cursor_state { true };