Merge pull request #75494 from MewPurPur/line-edit-get-selected-text

Implement LineEdit.get_selected_text()
This commit is contained in:
Rémi Verschelde 2023-04-03 16:03:58 +02:00
commit a9855a3a4c
No known key found for this signature in database
GPG Key ID: C3336907360768E1
3 changed files with 21 additions and 5 deletions

View File

@ -109,6 +109,12 @@
Returns the scroll offset due to [member caret_column], as a number of characters.
</description>
</method>
<method name="get_selected_text">
<return type="String" />
<description>
Returns the text inside the selection.
</description>
</method>
<method name="get_selection_from_column" qualifiers="const">
<return type="int" />
<description>

View File

@ -335,7 +335,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
}
}
if (!pass && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
DisplayServer::get_singleton()->clipboard_set_primary(text.substr(selection.begin, selection.end - selection.begin));
DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
}
}
}
@ -355,7 +355,7 @@ void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
} else {
if (selection.enabled && !pass && b->get_button_index() == MouseButton::LEFT && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
DisplayServer::get_singleton()->clipboard_set_primary(text.substr(selection.begin, selection.end - selection.begin));
DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
}
if (!text.is_empty() && is_editable() && clear_button_enabled) {
bool press_attempt = clear_button_status.press_attempt;
@ -644,7 +644,7 @@ Variant LineEdit::get_drag_data(const Point2 &p_point) {
}
if (selection.drag_attempt && selection.enabled) {
String t = text.substr(selection.begin, selection.end - selection.begin);
String t = get_selected_text();
Label *l = memnew(Label);
l->set_text(t);
set_drag_preview(l);
@ -1168,13 +1168,13 @@ void LineEdit::_notification(int p_what) {
void LineEdit::copy_text() {
if (selection.enabled && !pass) {
DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
DisplayServer::get_singleton()->clipboard_set(get_selected_text());
}
}
void LineEdit::cut_text() {
if (editable && selection.enabled && !pass) {
DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
DisplayServer::get_singleton()->clipboard_set(get_selected_text());
selection_delete();
}
}
@ -1811,6 +1811,14 @@ bool LineEdit::has_selection() const {
return selection.enabled;
}
String LineEdit::get_selected_text() {
if (selection.enabled) {
return text.substr(selection.begin, selection.end - selection.begin);
} else {
return String();
}
}
int LineEdit::get_selection_from_column() const {
ERR_FAIL_COND_V(!selection.enabled, -1);
return selection.begin;
@ -2486,6 +2494,7 @@ void LineEdit::_bind_methods() {
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_selected_text"), &LineEdit::get_selected_text);
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);

View File

@ -274,6 +274,7 @@ public:
void selection_delete();
void deselect();
bool has_selection() const;
String get_selected_text();
int get_selection_from_column() const;
int get_selection_to_column() const;