From e54a4646c771d8bd6d415118f4ec60d0c561fcb5 Mon Sep 17 00:00:00 2001 From: Yuri Roubinsky Date: Fri, 24 Sep 2021 11:00:45 +0300 Subject: [PATCH] Add selection getter methods to `LineEdit` Co-authored-by: Yuri Roubinsky --- doc/classes/LineEdit.xml | 18 ++++++++++++++++++ scene/gui/line_edit.cpp | 17 +++++++++++++++++ scene/gui/line_edit.h | 3 +++ 3 files changed, 38 insertions(+) diff --git a/doc/classes/LineEdit.xml b/doc/classes/LineEdit.xml index be8c47c79bb1..dc2d6be4f140 100644 --- a/doc/classes/LineEdit.xml +++ b/doc/classes/LineEdit.xml @@ -74,6 +74,24 @@ Returns the scroll offset due to [member caret_position], as a number of characters. + + + + Returns the selection begin column. + + + + + + Returns the selection end column. + + + + + + Returns [code]true[/code] if the user has selected text. + + diff --git a/scene/gui/line_edit.cpp b/scene/gui/line_edit.cpp index 81de06386f03..31187f81ba85 100644 --- a/scene/gui/line_edit.cpp +++ b/scene/gui/line_edit.cpp @@ -1584,6 +1584,20 @@ void LineEdit::deselect() { update(); } +bool LineEdit::has_selection() const { + return selection.enabled; +} + +int LineEdit::get_selection_from_column() const { + ERR_FAIL_COND_V(!selection.enabled, -1); + return selection.begin; +} + +int LineEdit::get_selection_to_column() const { + ERR_FAIL_COND_V(!selection.enabled, -1); + return selection.end; +} + void LineEdit::selection_delete() { if (selection.enabled) { delete_text(selection.begin, selection.end); @@ -1956,6 +1970,9 @@ void LineEdit::_bind_methods() { ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1)); ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all); ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect); + ClassDB::bind_method(D_METHOD("has_selection"), &LineEdit::has_selection); + ClassDB::bind_method(D_METHOD("get_selection_from_column"), &LineEdit::get_selection_from_column); + ClassDB::bind_method(D_METHOD("get_selection_to_column"), &LineEdit::get_selection_to_column); ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text); ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text); ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder); diff --git a/scene/gui/line_edit.h b/scene/gui/line_edit.h index 19c5c60e28ff..eec9699c007b 100644 --- a/scene/gui/line_edit.h +++ b/scene/gui/line_edit.h @@ -187,6 +187,9 @@ class LineEdit : public Control { void select_all(); void selection_delete(); void deselect(); + bool has_selection() const; + int get_selection_from_column() const; + int get_selection_to_column() const; void delete_char(); void delete_text(int p_from_column, int p_to_column);