summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorConteZero <one@contezero.com>2022-04-10 18:03:10 +0200
committerConteZero <one@contezero.com>2022-04-10 18:03:10 +0200
commitaddadaaa24ea594f376df600bf7b48a549780f42 (patch)
treec7c85a3cb755bf719893af746bcf484e5f41b5e8
parentbf153b82c73755bf898df9de33ac2116ebe914b9 (diff)
Add select_all() to RichTextLabel
-rw-r--r--doc/classes/RichTextLabel.xml7
-rw-r--r--scene/gui/rich_text_label.cpp43
-rw-r--r--scene/gui/rich_text_label.h1
3 files changed, 51 insertions, 0 deletions
diff --git a/doc/classes/RichTextLabel.xml b/doc/classes/RichTextLabel.xml
index a882a6c66f..b572ad2ed8 100644
--- a/doc/classes/RichTextLabel.xml
+++ b/doc/classes/RichTextLabel.xml
@@ -376,6 +376,13 @@
Scrolls the window's top line to match first line of the [code]paragraph[/code].
</description>
</method>
+ <method name="select_all">
+ <return type="void" />
+ <description>
+ Select all the text.
+ If [member selection_enabled] is [code]false[/code], no selection will occur.
+ </description>
+ </method>
<method name="set_cell_border_color">
<return type="void" />
<argument index="0" name="color" type="Color" />
diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp
index 981766e5eb..8e26fae8c2 100644
--- a/scene/gui/rich_text_label.cpp
+++ b/scene/gui/rich_text_label.cpp
@@ -1909,6 +1909,10 @@ void RichTextLabel::gui_input(const Ref<InputEvent> &p_event) {
vscroll->set_value(vscroll->get_max());
handled = true;
}
+ if (k->is_action("ui_text_select_all")) {
+ select_all();
+ handled = true;
+ }
if (k->is_action("ui_copy")) {
selection_copy();
handled = true;
@@ -4196,6 +4200,44 @@ void RichTextLabel::selection_copy() {
}
}
+void RichTextLabel::select_all() {
+ if (!selection.enabled) {
+ return;
+ }
+
+ Item *it = main;
+ Item *from_item = nullptr;
+ Item *to_item = nullptr;
+
+ while (it) {
+ if (it->type != ITEM_FRAME) {
+ if (from_item == nullptr) {
+ from_item = it;
+ } else {
+ to_item = it;
+ }
+ }
+ it = _get_next_item(it, true);
+ }
+
+ ItemFrame *from_frame = nullptr;
+ int from_line = 0;
+ _find_frame(from_item, &from_frame, &from_line);
+ ItemFrame *to_frame = nullptr;
+ int to_line = 0;
+ _find_frame(to_item, &to_frame, &to_line);
+ selection.from_line = from_line;
+ selection.from_frame = from_frame;
+ selection.from_char = 0;
+ selection.from_item = from_item;
+ selection.to_line = to_line;
+ selection.to_frame = to_frame;
+ selection.to_char = to_frame->lines[to_line].char_count;
+ selection.to_item = to_item;
+ selection.active = true;
+ update();
+}
+
bool RichTextLabel::is_selection_enabled() const {
return selection.enabled;
}
@@ -4491,6 +4533,7 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_selection_from"), &RichTextLabel::get_selection_from);
ClassDB::bind_method(D_METHOD("get_selection_to"), &RichTextLabel::get_selection_to);
+ ClassDB::bind_method(D_METHOD("select_all"), &RichTextLabel::select_all);
ClassDB::bind_method(D_METHOD("get_selected_text"), &RichTextLabel::get_selected_text);
ClassDB::bind_method(D_METHOD("deselect"), &RichTextLabel::deselect);
diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h
index b710413987..9ee182c668 100644
--- a/scene/gui/rich_text_label.h
+++ b/scene/gui/rich_text_label.h
@@ -584,6 +584,7 @@ public:
int get_selection_from() const;
int get_selection_to() const;
String get_selected_text() const;
+ void select_all();
void selection_copy();
void set_deselect_on_focus_loss_enabled(const bool p_enabled);
bool is_deselect_on_focus_loss_enabled() const;