LibGUI: Make GTextEditor::set_cursor() public

Also clamp the cursor value to the possible range instead of asserting
when trying to set a cursor past the end of the document.
This commit is contained in:
Andreas Kling 2019-10-21 18:58:27 +02:00
parent 7eed2e968c
commit 0a0dfeee8b
Notes: sideshowbarker 2024-07-19 11:35:43 +09:00
2 changed files with 13 additions and 5 deletions

View File

@ -848,11 +848,18 @@ void GTextEditor::set_cursor(int line, int column)
set_cursor({ line, column });
}
void GTextEditor::set_cursor(const GTextPosition& position)
void GTextEditor::set_cursor(const GTextPosition& a_position)
{
ASSERT(!m_lines.is_empty());
ASSERT(position.line() < m_lines.size());
ASSERT(position.column() <= m_lines[position.line()].length());
GTextPosition position = a_position;
if (position.line() >= m_lines.size())
position.set_line(m_lines.size() - 1);
if (position.column() > m_lines[position.line()].length())
position.set_column(m_lines[position.line()].length());
if (m_cursor != position) {
// NOTE: If the old cursor is no longer valid, repaint everything just in case.
auto old_cursor_line_rect = m_cursor.line() < m_lines.size()

View File

@ -164,6 +164,9 @@ public:
void add_custom_context_menu_action(GAction&);
void set_cursor(int line, int column);
void set_cursor(const GTextPosition&);
protected:
GTextEditor(Type, GWidget* parent);
@ -229,8 +232,6 @@ private:
Rect cursor_content_rect() const;
Rect content_rect_for_position(const GTextPosition&) const;
void update_cursor();
void set_cursor(int line, int column);
void set_cursor(const GTextPosition&);
Line& current_line() { return m_lines[m_cursor.line()]; }
const Line& current_line() const { return m_lines[m_cursor.line()]; }
GTextPosition text_position_at(const Point&) const;