| 1 | /**************************************************************************/ |
| 2 | /* text_edit.cpp */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #include "text_edit.h" |
| 32 | #include "text_edit.compat.inc" |
| 33 | |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/input/input.h" |
| 36 | #include "core/input/input_map.h" |
| 37 | #include "core/object/script_language.h" |
| 38 | #include "core/os/keyboard.h" |
| 39 | #include "core/os/os.h" |
| 40 | #include "core/string/alt_codes.h" |
| 41 | #include "core/string/string_builder.h" |
| 42 | #include "scene/gui/label.h" |
| 43 | #include "scene/main/window.h" |
| 44 | #include "scene/theme/theme_db.h" |
| 45 | |
| 46 | /////////////////////////////////////////////////////////////////////////////// |
| 47 | /// TEXT /// |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
| 49 | |
| 50 | void TextEdit::Text::set_font(const Ref<Font> &p_font) { |
| 51 | if (font == p_font) { |
| 52 | return; |
| 53 | } |
| 54 | font = p_font; |
| 55 | is_dirty = true; |
| 56 | } |
| 57 | |
| 58 | void TextEdit::Text::set_font_size(int p_font_size) { |
| 59 | if (font_size == p_font_size) { |
| 60 | return; |
| 61 | } |
| 62 | font_size = p_font_size; |
| 63 | is_dirty = true; |
| 64 | } |
| 65 | |
| 66 | void TextEdit::Text::set_tab_size(int p_tab_size) { |
| 67 | if (tab_size == p_tab_size) { |
| 68 | return; |
| 69 | } |
| 70 | tab_size = p_tab_size; |
| 71 | tab_size_dirty = true; |
| 72 | } |
| 73 | |
| 74 | int TextEdit::Text::get_tab_size() const { |
| 75 | return tab_size; |
| 76 | } |
| 77 | |
| 78 | void TextEdit::Text::set_indent_wrapped_lines(bool p_enabled) { |
| 79 | if (indent_wrapped_lines == p_enabled) { |
| 80 | return; |
| 81 | } |
| 82 | indent_wrapped_lines = p_enabled; |
| 83 | tab_size_dirty = true; |
| 84 | } |
| 85 | |
| 86 | bool TextEdit::Text::is_indent_wrapped_lines() const { |
| 87 | return indent_wrapped_lines; |
| 88 | } |
| 89 | |
| 90 | void TextEdit::Text::set_direction_and_language(TextServer::Direction p_direction, const String &p_language) { |
| 91 | if (direction == p_direction && language == p_language) { |
| 92 | return; |
| 93 | } |
| 94 | direction = p_direction; |
| 95 | language = p_language; |
| 96 | is_dirty = true; |
| 97 | } |
| 98 | |
| 99 | void TextEdit::Text::set_draw_control_chars(bool p_enabled) { |
| 100 | if (draw_control_chars == p_enabled) { |
| 101 | return; |
| 102 | } |
| 103 | draw_control_chars = p_enabled; |
| 104 | is_dirty = true; |
| 105 | } |
| 106 | |
| 107 | void TextEdit::Text::set_inline_object_parser(const Callable &p_parser) { |
| 108 | inline_object_parser = p_parser; |
| 109 | is_dirty = true; |
| 110 | invalidate_all(); |
| 111 | } |
| 112 | |
| 113 | int TextEdit::Text::get_line_width(int p_line, int p_wrap_index) const { |
| 114 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 115 | if (p_wrap_index != -1) { |
| 116 | return text[p_line].data_buf->get_line_width(p_wrap_index); |
| 117 | } |
| 118 | return text[p_line].data_buf->get_size().x; |
| 119 | } |
| 120 | |
| 121 | int TextEdit::Text::get_max_width() const { |
| 122 | if (max_line_width_dirty) { |
| 123 | int new_max_line_width = 0; |
| 124 | for (const Line &l : text) { |
| 125 | if (l.hidden) { |
| 126 | continue; |
| 127 | } |
| 128 | new_max_line_width = MAX(new_max_line_width, l.width); |
| 129 | } |
| 130 | max_line_width = new_max_line_width; |
| 131 | } |
| 132 | |
| 133 | return max_line_width; |
| 134 | } |
| 135 | |
| 136 | int TextEdit::Text::get_line_height() const { |
| 137 | if (max_line_height_dirty) { |
| 138 | int new_max_line_height = 0; |
| 139 | for (const Line &l : text) { |
| 140 | if (l.hidden) { |
| 141 | continue; |
| 142 | } |
| 143 | new_max_line_height = MAX(new_max_line_height, l.height); |
| 144 | } |
| 145 | max_line_height = new_max_line_height; |
| 146 | } |
| 147 | |
| 148 | return max_line_height; |
| 149 | } |
| 150 | |
| 151 | void TextEdit::Text::set_width(float p_width) { |
| 152 | width = p_width; |
| 153 | } |
| 154 | |
| 155 | float TextEdit::Text::get_width() const { |
| 156 | return width; |
| 157 | } |
| 158 | |
| 159 | void TextEdit::Text::set_brk_flags(BitField<TextServer::LineBreakFlag> p_flags) { |
| 160 | brk_flags = p_flags; |
| 161 | } |
| 162 | |
| 163 | BitField<TextServer::LineBreakFlag> TextEdit::Text::get_brk_flags() const { |
| 164 | return brk_flags; |
| 165 | } |
| 166 | |
| 167 | int TextEdit::Text::get_line_wrap_amount(int p_line) const { |
| 168 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 169 | |
| 170 | return text[p_line].line_count - 1; |
| 171 | } |
| 172 | |
| 173 | Vector<Vector2i> TextEdit::Text::get_line_wrap_ranges(int p_line) const { |
| 174 | Vector<Vector2i> ret; |
| 175 | ERR_FAIL_INDEX_V(p_line, text.size(), ret); |
| 176 | |
| 177 | Ref<TextParagraph> data_buf = text[p_line].data_buf; |
| 178 | int line_count = data_buf->get_line_count(); |
| 179 | for (int i = 0; i < line_count; i++) { |
| 180 | ret.push_back(data_buf->get_line_range(i)); |
| 181 | } |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | const Ref<TextParagraph> TextEdit::Text::get_line_data(int p_line) const { |
| 186 | ERR_FAIL_INDEX_V(p_line, text.size(), Ref<TextParagraph>()); |
| 187 | return text[p_line].data_buf; |
| 188 | } |
| 189 | |
| 190 | float TextEdit::Text::get_indent_offset(int p_line, bool p_rtl) const { |
| 191 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 192 | Line &text_line = text.write[p_line]; |
| 193 | if (text_line.indent_ofs < 0.0) { |
| 194 | int char_count = 0; |
| 195 | int line_length = text_line.data.size(); |
| 196 | for (int i = 0; i < line_length - 1; i++) { |
| 197 | if (text_line.data[i] == '\t') { |
| 198 | char_count++; |
| 199 | } else if (text_line.data[i] == ' ') { |
| 200 | char_count++; |
| 201 | } else { |
| 202 | break; |
| 203 | } |
| 204 | } |
| 205 | RID text_rid = text_line.data_buf->get_line_rid(0); |
| 206 | float offset = (p_rtl) ? TS->shaped_text_get_size(text_rid).x : 0; |
| 207 | Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, 0, char_count); |
| 208 | for (const Vector2 v : sel) { |
| 209 | if (p_rtl) { |
| 210 | offset = MIN(v.x, MIN(v.y, offset)); |
| 211 | } else { |
| 212 | offset = MAX(v.x, MAX(v.y, offset)); |
| 213 | } |
| 214 | } |
| 215 | text_line.indent_ofs = (p_rtl) ? TS->shaped_text_get_size(text_rid).x - offset : offset; |
| 216 | } |
| 217 | return text_line.indent_ofs; |
| 218 | } |
| 219 | |
| 220 | _FORCE_INLINE_ const String &TextEdit::Text::operator[](int p_line) const { |
| 221 | static const String empty; |
| 222 | ERR_FAIL_INDEX_V(p_line, text.size(), empty); |
| 223 | return text[p_line].data; |
| 224 | } |
| 225 | |
| 226 | _FORCE_INLINE_ const String &TextEdit::Text::get_text_with_ime(int p_line) const { |
| 227 | if (!text[p_line].ime_data.is_empty()) { |
| 228 | return text[p_line].ime_data; |
| 229 | } else { |
| 230 | return text[p_line].data; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | const Vector<RID> TextEdit::Text::get_accessibility_elements(int p_line) { |
| 235 | ERR_FAIL_INDEX_V(p_line, text.size(), Vector<RID>()); |
| 236 | |
| 237 | return text[p_line].accessibility_text_root_element; |
| 238 | } |
| 239 | |
| 240 | void TextEdit::Text::update_accessibility(int p_line, RID p_root) { |
| 241 | ERR_FAIL_INDEX(p_line, text.size()); |
| 242 | |
| 243 | Line &l = text.write[p_line]; |
| 244 | if (l.accessibility_text_root_element.is_empty()) { |
| 245 | for (int i = 0; i < l.data_buf->get_line_count(); i++) { |
| 246 | bool is_last_line = (p_line == text.size() - 1) && (i == l.data_buf->get_line_count() - 1); |
| 247 | RID rid = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(p_root, l.data_buf->get_line_rid(i), max_line_height, p_line, is_last_line); |
| 248 | l.accessibility_text_root_element.push_back(rid); |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | inline bool is_inline_info_valid(const Variant &p_info) { |
| 254 | if (p_info.get_type() != Variant::DICTIONARY) { |
| 255 | return false; |
| 256 | } |
| 257 | Dictionary info = p_info; |
| 258 | if (!info.get_valid("column").is_num() || !info.get_valid("width_ratio").is_num()) { |
| 259 | return false; |
| 260 | } |
| 261 | return true; |
| 262 | } |
| 263 | |
| 264 | void TextEdit::Text::invalidate_cache(int p_line, bool p_text_changed) { |
| 265 | ERR_FAIL_INDEX(p_line, text.size()); |
| 266 | |
| 267 | Line &l = text.write[p_line]; |
| 268 | for (const RID rid : l.accessibility_text_root_element) { |
| 269 | if (rid.is_valid()) { |
| 270 | DisplayServer::get_singleton()->accessibility_free_element(rid); |
| 271 | } |
| 272 | } |
| 273 | l.accessibility_text_root_element.clear(); |
| 274 | |
| 275 | if (font.is_null()) { |
| 276 | return; // Not in tree? |
| 277 | } |
| 278 | |
| 279 | Line &text_line = text.write[p_line]; |
| 280 | if (p_text_changed) { |
| 281 | text_line.data_buf->clear(); |
| 282 | } |
| 283 | |
| 284 | BitField<TextServer::LineBreakFlag> flags = brk_flags; |
| 285 | if (indent_wrapped_lines) { |
| 286 | flags.set_flag(TextServer::BREAK_TRIM_INDENT); |
| 287 | } |
| 288 | |
| 289 | text_line.data_buf->set_width(width); |
| 290 | text_line.data_buf->set_direction((TextServer::Direction)direction); |
| 291 | text_line.data_buf->set_break_flags(flags); |
| 292 | text_line.data_buf->set_preserve_control(draw_control_chars); |
| 293 | text_line.data_buf->set_custom_punctuation(get_enabled_word_separators()); |
| 294 | text_line.indent_ofs = -1.0; |
| 295 | |
| 296 | const String &text_with_ime = (!text_line.ime_data.is_empty()) ? text_line.ime_data : text_line.data; |
| 297 | const Array &bidi_override_with_ime = (!text_line.ime_data.is_empty()) ? text_line.ime_bidi_override : text_line.bidi_override; |
| 298 | |
| 299 | if (p_text_changed) { |
| 300 | int from = 0; |
| 301 | if (inline_object_parser.is_valid()) { |
| 302 | // Insert inline object. |
| 303 | Variant parsed_result = inline_object_parser.call(text_with_ime); |
| 304 | if (parsed_result.is_array()) { |
| 305 | Array object_infos = parsed_result; |
| 306 | for (Variant val : object_infos) { |
| 307 | if (!is_inline_info_valid(val)) { |
| 308 | continue; |
| 309 | } |
| 310 | Dictionary info = val; |
| 311 | int start = info["column"]; |
| 312 | float width_ratio = info["width_ratio"]; |
| 313 | String left_string = text_with_ime.substr(from, start - from); |
| 314 | text_line.data_buf->add_string(left_string, font, font_size, language); |
| 315 | text_line.data_buf->add_object(info, Vector2(font_height * width_ratio, font_height), INLINE_ALIGNMENT_CENTER, 0); |
| 316 | from = start; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | String remaining_string = text_with_ime.substr(from); |
| 321 | text_line.data_buf->add_string(remaining_string, font, font_size, language); |
| 322 | } |
| 323 | if (!bidi_override_with_ime.is_empty()) { |
| 324 | TS->shaped_text_set_bidi_override(text_line.data_buf->get_rid(), bidi_override_with_ime); |
| 325 | } |
| 326 | |
| 327 | if (!p_text_changed) { |
| 328 | // Update fonts. |
| 329 | RID r = text_line.data_buf->get_rid(); |
| 330 | int spans = TS->shaped_get_span_count(r); |
| 331 | for (int i = 0; i < spans; i++) { |
| 332 | TS->shaped_set_span_update_font(r, i, font->get_rids(), font_size, font->get_opentype_features()); |
| 333 | } |
| 334 | |
| 335 | // Update inline object sizes. |
| 336 | for (int i = 0; i < text_line.data_buf->get_line_count(); i++) { |
| 337 | for (Variant key : text_line.data_buf->get_line_objects(i)) { |
| 338 | if (!is_inline_info_valid(key)) { |
| 339 | continue; |
| 340 | } |
| 341 | Dictionary info = key; |
| 342 | float width_ratio = info["width_ratio"]; |
| 343 | text_line.data_buf->resize_object(info, Vector2(font_height * width_ratio, font_height), INLINE_ALIGNMENT_CENTER, 0); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | // Apply tab align. |
| 349 | if (tab_size > 0) { |
| 350 | Vector<float> tabs; |
| 351 | tabs.push_back(MAX(1, (font->get_char_size(' ', font_size).width + font->get_spacing(TextServer::SPACING_SPACE)) * tab_size)); |
| 352 | text_line.data_buf->tab_align(tabs); |
| 353 | } |
| 354 | |
| 355 | // Update wrap amount. |
| 356 | const int old_line_count = text_line.line_count; |
| 357 | text_line.line_count = text_line.data_buf->get_line_count(); |
| 358 | if (!text_line.hidden && text_line.line_count != old_line_count) { |
| 359 | total_visible_line_count += text_line.line_count - old_line_count; |
| 360 | } |
| 361 | |
| 362 | // Update height. |
| 363 | const int old_height = text_line.height; |
| 364 | text_line.height = font_height; |
| 365 | for (int i = 0; i < text_line.line_count; i++) { |
| 366 | text_line.height = MAX(text_line.height, text_line.data_buf->get_line_size(i).y); |
| 367 | } |
| 368 | |
| 369 | // If this line has shrunk, this may no longer be the tallest line. |
| 370 | if (!text_line.hidden) { |
| 371 | if (old_height == max_line_height && text_line.height < old_height) { |
| 372 | max_line_height_dirty = true; |
| 373 | } else { |
| 374 | max_line_height = MAX(text_line.height, max_line_height); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // Update width. |
| 379 | const int old_width = text_line.width; |
| 380 | text_line.width = get_line_width(p_line); |
| 381 | |
| 382 | if (!text_line.hidden) { |
| 383 | // If this line has shrunk, this may no longer be the longest line. |
| 384 | if (old_width == max_line_width && text_line.width < old_width) { |
| 385 | max_line_width_dirty = true; |
| 386 | } else { |
| 387 | max_line_width = MAX(text_line.width, max_line_width); |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | void TextEdit::Text::invalidate_all_lines() { |
| 393 | for (int i = 0; i < text.size(); i++) { |
| 394 | if (tab_size_dirty) { |
| 395 | if (tab_size > 0) { |
| 396 | Vector<float> tabs; |
| 397 | tabs.push_back(MAX(1, (font->get_char_size(' ', font_size).width + font->get_spacing(TextServer::SPACING_SPACE)) * tab_size)); |
| 398 | text[i].data_buf->tab_align(tabs); |
| 399 | } |
| 400 | } |
| 401 | invalidate_cache(i, false); |
| 402 | } |
| 403 | tab_size_dirty = false; |
| 404 | } |
| 405 | |
| 406 | void TextEdit::Text::invalidate_font() { |
| 407 | if (!is_dirty) { |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | max_line_width_dirty = true; |
| 412 | max_line_height_dirty = true; |
| 413 | |
| 414 | if (font.is_valid() && font_size > 0) { |
| 415 | font_height = font->get_height(font_size); |
| 416 | } |
| 417 | |
| 418 | for (int i = 0; i < text.size(); i++) { |
| 419 | invalidate_cache(i, false); |
| 420 | } |
| 421 | is_dirty = false; |
| 422 | } |
| 423 | |
| 424 | void TextEdit::Text::invalidate_all() { |
| 425 | if (!is_dirty) { |
| 426 | return; |
| 427 | } |
| 428 | |
| 429 | max_line_width_dirty = true; |
| 430 | max_line_height_dirty = true; |
| 431 | |
| 432 | if (font.is_valid() && font_size > 0) { |
| 433 | font_height = font->get_height(font_size); |
| 434 | } |
| 435 | |
| 436 | for (int i = 0; i < text.size(); i++) { |
| 437 | invalidate_cache(i, true); |
| 438 | } |
| 439 | is_dirty = false; |
| 440 | } |
| 441 | |
| 442 | void TextEdit::Text::clear() { |
| 443 | text.clear(); |
| 444 | |
| 445 | max_line_width_dirty = true; |
| 446 | max_line_height_dirty = true; |
| 447 | total_visible_line_count = 0; |
| 448 | |
| 449 | Line line; |
| 450 | line.gutters.resize(gutter_count); |
| 451 | text.insert(0, line); |
| 452 | invalidate_cache(0, true); |
| 453 | } |
| 454 | |
| 455 | int TextEdit::Text::get_total_visible_line_count() const { |
| 456 | return total_visible_line_count; |
| 457 | } |
| 458 | |
| 459 | void TextEdit::Text::set(int p_line, const String &p_text, const Array &p_bidi_override) { |
| 460 | ERR_FAIL_INDEX(p_line, text.size()); |
| 461 | |
| 462 | text.write[p_line].data = p_text; |
| 463 | text.write[p_line].ime_data = String(); |
| 464 | text.write[p_line].bidi_override = p_bidi_override; |
| 465 | text.write[p_line].ime_bidi_override.clear(); |
| 466 | invalidate_cache(p_line, true); |
| 467 | } |
| 468 | |
| 469 | void TextEdit::Text::set_ime(int p_line, const String &p_text, const Array &p_bidi_override) { |
| 470 | ERR_FAIL_INDEX(p_line, text.size()); |
| 471 | |
| 472 | text.write[p_line].ime_data = p_text; |
| 473 | text.write[p_line].ime_bidi_override = p_bidi_override; |
| 474 | invalidate_cache(p_line, true); |
| 475 | } |
| 476 | |
| 477 | void TextEdit::Text::set_hidden(int p_line, bool p_hidden) { |
| 478 | ERR_FAIL_INDEX(p_line, text.size()); |
| 479 | |
| 480 | Line &text_line = text.write[p_line]; |
| 481 | if (text_line.hidden == p_hidden) { |
| 482 | return; |
| 483 | } |
| 484 | text_line.hidden = p_hidden; |
| 485 | if (p_hidden) { |
| 486 | total_visible_line_count -= text_line.line_count; |
| 487 | if (text_line.width == max_line_width) { |
| 488 | max_line_width_dirty = true; |
| 489 | } |
| 490 | if (text_line.height == max_line_height) { |
| 491 | max_line_height_dirty = true; |
| 492 | } |
| 493 | } else { |
| 494 | total_visible_line_count += text_line.line_count; |
| 495 | max_line_width = MAX(text_line.width, max_line_width); |
| 496 | max_line_height = MAX(text_line.height, max_line_height); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | bool TextEdit::Text::is_hidden(int p_line) const { |
| 501 | ERR_FAIL_INDEX_V(p_line, text.size(), true); |
| 502 | return text[p_line].hidden; |
| 503 | } |
| 504 | |
| 505 | void TextEdit::Text::insert(int p_at, const Vector<String> &p_text, const Vector<Array> &p_bidi_override) { |
| 506 | ERR_FAIL_INDEX(p_at, text.size() + 1); |
| 507 | |
| 508 | int new_line_count = p_text.size() - 1; |
| 509 | if (new_line_count > 0) { |
| 510 | text.resize(text.size() + new_line_count); |
| 511 | for (int i = (text.size() - 1); i > p_at; i--) { |
| 512 | if ((i - new_line_count) <= 0) { |
| 513 | break; |
| 514 | } |
| 515 | text.write[i] = text[i - new_line_count]; |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | for (int i = 0; i < p_text.size(); i++) { |
| 520 | if (i == 0) { |
| 521 | set(p_at + i, p_text[i], p_bidi_override[i]); |
| 522 | continue; |
| 523 | } |
| 524 | Line line; |
| 525 | line.gutters.resize(gutter_count); |
| 526 | line.data = p_text[i]; |
| 527 | line.bidi_override = p_bidi_override[i]; |
| 528 | text.write[p_at + i] = line; |
| 529 | invalidate_cache(p_at + i, true); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | void TextEdit::Text::remove_range(int p_from_line, int p_to_line) { |
| 534 | if (p_from_line == p_to_line) { |
| 535 | return; |
| 536 | } |
| 537 | |
| 538 | for (int i = p_from_line + 1; i <= p_to_line; i++) { |
| 539 | const Line &text_line = text[i]; |
| 540 | if (text_line.hidden) { |
| 541 | continue; |
| 542 | } |
| 543 | |
| 544 | if (text_line.height == max_line_height) { |
| 545 | max_line_height_dirty = true; |
| 546 | } |
| 547 | if (text_line.width == max_line_width) { |
| 548 | max_line_width_dirty = true; |
| 549 | } |
| 550 | total_visible_line_count -= text_line.line_count; |
| 551 | } |
| 552 | |
| 553 | int diff = p_to_line - p_from_line; |
| 554 | for (int i = p_to_line + 1; i < text.size(); i++) { |
| 555 | text.write[i - diff] = text[i]; |
| 556 | } |
| 557 | text.resize(text.size() - diff); |
| 558 | |
| 559 | ERR_FAIL_COND(total_visible_line_count < 0); // BUG |
| 560 | } |
| 561 | |
| 562 | void TextEdit::Text::add_gutter(int p_at) { |
| 563 | for (int i = 0; i < text.size(); i++) { |
| 564 | if (p_at < 0 || p_at > gutter_count) { |
| 565 | text.write[i].gutters.push_back(Gutter()); |
| 566 | } else { |
| 567 | text.write[i].gutters.insert(p_at, Gutter()); |
| 568 | } |
| 569 | } |
| 570 | gutter_count++; |
| 571 | } |
| 572 | |
| 573 | void TextEdit::Text::remove_gutter(int p_gutter) { |
| 574 | ERR_FAIL_INDEX(p_gutter, text.size()); |
| 575 | |
| 576 | for (int i = 0; i < text.size(); i++) { |
| 577 | text.write[i].gutters.remove_at(p_gutter); |
| 578 | } |
| 579 | gutter_count--; |
| 580 | } |
| 581 | |
| 582 | void TextEdit::Text::move_gutters(int p_from_line, int p_to_line) { |
| 583 | ERR_FAIL_INDEX(p_from_line, text.size()); |
| 584 | ERR_FAIL_INDEX(p_to_line, text.size()); |
| 585 | |
| 586 | text.write[p_to_line].gutters = text[p_from_line].gutters; |
| 587 | text.write[p_from_line].gutters.clear(); |
| 588 | text.write[p_from_line].gutters.resize(gutter_count); |
| 589 | } |
| 590 | |
| 591 | void TextEdit::Text::set_use_default_word_separators(bool p_enabled) { |
| 592 | if (use_default_word_separators == p_enabled) { |
| 593 | return; |
| 594 | } |
| 595 | use_default_word_separators = p_enabled; |
| 596 | invalidate_all_lines(); |
| 597 | } |
| 598 | |
| 599 | void TextEdit::Text::set_use_custom_word_separators(bool p_enabled) { |
| 600 | if (use_custom_word_separators == p_enabled) { |
| 601 | return; |
| 602 | } |
| 603 | use_custom_word_separators = p_enabled; |
| 604 | invalidate_all_lines(); |
| 605 | } |
| 606 | |
| 607 | bool TextEdit::Text::is_default_word_separators_enabled() const { |
| 608 | return use_default_word_separators; |
| 609 | } |
| 610 | |
| 611 | bool TextEdit::Text::is_custom_word_separators_enabled() const { |
| 612 | return use_custom_word_separators; |
| 613 | } |
| 614 | |
| 615 | String TextEdit::Text::get_custom_word_separators() const { |
| 616 | return custom_word_separators; |
| 617 | } |
| 618 | |
| 619 | String TextEdit::Text::get_default_word_separators() const { |
| 620 | String concat_separators = "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~"; |
| 621 | for (char32_t ch = 0x2000; ch <= 0x206F; ++ch) { // General punctuation block. |
| 622 | concat_separators += ch; |
| 623 | } |
| 624 | for (char32_t ch = 0x3000; ch <= 0x303F; ++ch) { // CJK punctuation block. |
| 625 | concat_separators += ch; |
| 626 | } |
| 627 | return concat_separators; |
| 628 | } |
| 629 | |
| 630 | // Get default and/or custom word separators depending on the option enabled. |
| 631 | String TextEdit::Text::get_enabled_word_separators() const { |
| 632 | String all_separators; |
| 633 | if (use_default_word_separators) { |
| 634 | all_separators += get_default_word_separators(); |
| 635 | } |
| 636 | if (use_custom_word_separators) { |
| 637 | all_separators += get_custom_word_separators(); |
| 638 | } |
| 639 | return all_separators; |
| 640 | } |
| 641 | |
| 642 | /////////////////////////////////////////////////////////////////////////////// |
| 643 | /// TEXT EDIT /// |
| 644 | /////////////////////////////////////////////////////////////////////////////// |
| 645 | |
| 646 | void TextEdit::_accessibility_action_set_selection(const Variant &p_data) { |
| 647 | Dictionary new_selection = p_data; |
| 648 | RID sel_start = new_selection["start_element"]; |
| 649 | Vector2i sel_start_line = DisplayServer::get_singleton()->accessibility_element_get_meta(sel_start); |
| 650 | int sel_start_pos = new_selection["start_char"]; |
| 651 | |
| 652 | RID sel_end = new_selection["end_element"]; |
| 653 | Vector2i sel_end_line = DisplayServer::get_singleton()->accessibility_element_get_meta(sel_end); |
| 654 | int sel_end_pos = new_selection["end_char"]; |
| 655 | |
| 656 | remove_secondary_carets(); |
| 657 | select(sel_start_line.x, sel_start_pos, sel_end_line.x, sel_end_pos, 0); |
| 658 | } |
| 659 | |
| 660 | void TextEdit::_accessibility_action_replace_selected(const Variant &p_data) { |
| 661 | String new_text = p_data; |
| 662 | insert_text_at_caret(new_text); |
| 663 | } |
| 664 | |
| 665 | void TextEdit::_accessibility_action_set_value(const Variant &p_data) { |
| 666 | String new_text = p_data; |
| 667 | set_text(new_text); |
| 668 | } |
| 669 | |
| 670 | void TextEdit::_accessibility_action_menu(const Variant &p_data) { |
| 671 | if (context_menu_enabled) { |
| 672 | _update_context_menu(); |
| 673 | adjust_viewport_to_caret(); |
| 674 | menu->set_position(get_screen_position() + get_caret_draw_pos()); |
| 675 | menu->reset_size(); |
| 676 | menu->popup(); |
| 677 | menu->grab_focus(); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | void TextEdit::_accessibility_scroll_down(const Variant &p_data) { |
| 682 | if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) { |
| 683 | v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() / 4); |
| 684 | } else { |
| 685 | v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page()); |
| 686 | } |
| 687 | queue_accessibility_update(); |
| 688 | } |
| 689 | |
| 690 | void TextEdit::_accessibility_scroll_left(const Variant &p_data) { |
| 691 | if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) { |
| 692 | h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page() / 4); |
| 693 | } else { |
| 694 | h_scroll->set_value(h_scroll->get_value() - h_scroll->get_page()); |
| 695 | } |
| 696 | queue_accessibility_update(); |
| 697 | } |
| 698 | |
| 699 | void TextEdit::_accessibility_scroll_right(const Variant &p_data) { |
| 700 | if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) { |
| 701 | h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() / 4); |
| 702 | } else { |
| 703 | h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page()); |
| 704 | } |
| 705 | queue_accessibility_update(); |
| 706 | } |
| 707 | |
| 708 | void TextEdit::_accessibility_scroll_up(const Variant &p_data) { |
| 709 | if ((DisplayServer::AccessibilityScrollUnit)p_data == DisplayServer::SCROLL_UNIT_ITEM) { |
| 710 | v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page() / 4); |
| 711 | } else { |
| 712 | v_scroll->set_value(v_scroll->get_value() - v_scroll->get_page()); |
| 713 | } |
| 714 | queue_accessibility_update(); |
| 715 | } |
| 716 | |
| 717 | void TextEdit::_accessibility_scroll_set(const Variant &p_data) { |
| 718 | const Point2 &pos = p_data; |
| 719 | h_scroll->set_value(pos.x); |
| 720 | v_scroll->set_value(pos.y); |
| 721 | queue_accessibility_update(); |
| 722 | } |
| 723 | |
| 724 | void TextEdit::_accessibility_action_scroll_into_view(const Variant &p_data, int p_line, int p_wrap) { |
| 725 | double delta = get_scroll_pos_for_line(p_line, p_wrap) - get_v_scroll(); |
| 726 | if (delta < 0) { |
| 727 | _scroll_up(-delta, false); |
| 728 | } else { |
| 729 | _scroll_down(delta, false); |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | Ref<StyleBox> TextEdit::_get_current_stylebox() const { |
| 734 | return editable ? theme_cache.style_normal : theme_cache.style_readonly; |
| 735 | } |
| 736 | |
| 737 | void TextEdit::_notification(int p_what) { |
| 738 | switch (p_what) { |
| 739 | case NOTIFICATION_EXIT_TREE: |
| 740 | case NOTIFICATION_ACCESSIBILITY_INVALIDATE: { |
| 741 | text.clear_accessibility(); |
| 742 | } break; |
| 743 | |
| 744 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 745 | RID ae = get_accessibility_element(); |
| 746 | ERR_FAIL_COND(ae.is_null()); |
| 747 | |
| 748 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_MULTILINE_TEXT_FIELD); |
| 749 | if (text.size() == 1 && text[0].is_empty()) { |
| 750 | DisplayServer::get_singleton()->accessibility_update_set_placeholder(ae, atr(placeholder_text)); |
| 751 | } |
| 752 | if (!placeholder_text.is_empty() && get_accessibility_name().is_empty()) { |
| 753 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, atr(placeholder_text)); |
| 754 | } |
| 755 | DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_READONLY, !editable); |
| 756 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_TEXT_SELECTION, callable_mp(this, &TextEdit::_accessibility_action_set_selection)); |
| 757 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_REPLACE_SELECTED_TEXT, callable_mp(this, &TextEdit::_accessibility_action_replace_selected)); |
| 758 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &TextEdit::_accessibility_action_set_value)); |
| 759 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &TextEdit::_accessibility_action_menu)); |
| 760 | |
| 761 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_DOWN, callable_mp(this, &TextEdit::_accessibility_scroll_down)); |
| 762 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_LEFT, callable_mp(this, &TextEdit::_accessibility_scroll_left)); |
| 763 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_RIGHT, callable_mp(this, &TextEdit::_accessibility_scroll_right)); |
| 764 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SCROLL_UP, callable_mp(this, &TextEdit::_accessibility_scroll_up)); |
| 765 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_SCROLL_OFFSET, callable_mp(this, &TextEdit::_accessibility_scroll_set)); |
| 766 | |
| 767 | int first_vis_line = get_first_visible_line(); |
| 768 | int row_height = get_line_height(); |
| 769 | Ref<StyleBox> style = _get_current_stylebox(); |
| 770 | int xmargin_beg = Math::ceil(style->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding; |
| 771 | Size2 size = get_size(); |
| 772 | bool rtl = is_layout_rtl(); |
| 773 | int lines_drawn = 0; |
| 774 | |
| 775 | for (int i = 0; i < text.size(); i++) { |
| 776 | text.update_accessibility(i, ae); |
| 777 | const Ref<TextParagraph> &ac_buf = text.get_line_data(i); |
| 778 | const Vector<RID> &text_aes = text.get_accessibility_elements(i); |
| 779 | int first_indent_line = 0; |
| 780 | float indent_ofs = 0.0; |
| 781 | if (text.is_indent_wrapped_lines()) { |
| 782 | _get_wrapped_indent_level(i, first_indent_line); |
| 783 | indent_ofs = MIN(text.get_indent_offset(i, rtl), wrap_at_column * 0.6); |
| 784 | } |
| 785 | for (int j = 0; j < text_aes.size(); j++) { |
| 786 | float text_off_y = style->get_margin(SIDE_TOP); |
| 787 | |
| 788 | text_off_y += (lines_drawn + j) * row_height + theme_cache.line_spacing / 2; |
| 789 | text_off_y -= (first_vis_line + first_visible_line_wrap_ofs) * row_height; |
| 790 | text_off_y -= _get_v_scroll_offset() * row_height; |
| 791 | |
| 792 | float wrap_indent = j > first_indent_line ? indent_ofs : 0.0; |
| 793 | int char_margin = xmargin_beg - first_visible_col; |
| 794 | if (rtl) { |
| 795 | char_margin = size.width - char_margin - ac_buf->get_line_width(j) - wrap_indent; |
| 796 | } else { |
| 797 | char_margin += wrap_indent; |
| 798 | } |
| 799 | |
| 800 | DisplayServer::get_singleton()->accessibility_update_set_flag(text_aes[j], DisplayServer::AccessibilityFlags::FLAG_HIDDEN, _is_line_hidden(i)); |
| 801 | Transform2D tr; |
| 802 | tr.set_origin(Point2(char_margin, text_off_y)); |
| 803 | DisplayServer::get_singleton()->accessibility_update_set_transform(text_aes[j], tr); |
| 804 | DisplayServer::get_singleton()->accessibility_update_set_name(text_aes[j], vformat(RTR("Line %d"), i)); |
| 805 | DisplayServer::get_singleton()->accessibility_element_set_meta(text_aes[j], Vector2i(i, j)); |
| 806 | DisplayServer::get_singleton()->accessibility_update_add_action(text_aes[j], DisplayServer::AccessibilityAction::ACTION_SCROLL_INTO_VIEW, callable_mp(this, &TextEdit::_accessibility_action_scroll_into_view).bind(i, j)); |
| 807 | } |
| 808 | lines_drawn += ac_buf->get_line_count(); |
| 809 | } |
| 810 | |
| 811 | // Selection. |
| 812 | if (carets.size() > 0) { |
| 813 | if (carets[0].selection.active) { |
| 814 | int start_wrap = get_line_wrap_index_at_column(carets[0].selection.origin_line, carets[0].selection.origin_column); |
| 815 | RID start_rid = text.get_accessibility_elements(carets[0].selection.origin_line)[start_wrap]; |
| 816 | |
| 817 | int end_wrap = get_line_wrap_index_at_column(carets[0].line, carets[0].column); |
| 818 | RID end_rid = text.get_accessibility_elements(carets[0].line)[end_wrap]; |
| 819 | DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, start_rid, carets[0].selection.origin_column, end_rid, carets[0].column); |
| 820 | } else { |
| 821 | int caret_wrap = get_line_wrap_index_at_column(carets[0].line, carets[0].column); |
| 822 | RID caret_rid = text.get_accessibility_elements(carets[0].line)[caret_wrap]; |
| 823 | DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, caret_rid, carets[0].column, caret_rid, carets[0].column); |
| 824 | } |
| 825 | } |
| 826 | } break; |
| 827 | |
| 828 | case NOTIFICATION_POSTINITIALIZE: { |
| 829 | _update_caches(); |
| 830 | } break; |
| 831 | |
| 832 | case NOTIFICATION_ENTER_TREE: { |
| 833 | _update_caches(); |
| 834 | if (caret_pos_dirty) { |
| 835 | callable_mp(this, &TextEdit::_emit_caret_changed).call_deferred(); |
| 836 | } |
| 837 | if (text_changed_dirty) { |
| 838 | callable_mp(this, &TextEdit::_emit_text_changed).call_deferred(); |
| 839 | } |
| 840 | _update_wrap_at_column(true); |
| 841 | } break; |
| 842 | |
| 843 | case NOTIFICATION_RESIZED: { |
| 844 | _update_scrollbars(); |
| 845 | _update_wrap_at_column(); |
| 846 | } break; |
| 847 | |
| 848 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 849 | if (is_visible()) { |
| 850 | callable_mp(this, &TextEdit::_update_scrollbars).call_deferred(); |
| 851 | callable_mp(this, &TextEdit::_update_wrap_at_column).call_deferred(false); |
| 852 | } |
| 853 | } break; |
| 854 | |
| 855 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
| 856 | case NOTIFICATION_TRANSLATION_CHANGED: |
| 857 | case NOTIFICATION_THEME_CHANGED: { |
| 858 | if (is_inside_tree()) { |
| 859 | _update_caches(p_what == NOTIFICATION_TRANSLATION_CHANGED); |
| 860 | _update_wrap_at_column(true); |
| 861 | } |
| 862 | } break; |
| 863 | |
| 864 | case NOTIFICATION_WM_WINDOW_FOCUS_IN: { |
| 865 | window_has_focus = true; |
| 866 | draw_caret = true; |
| 867 | queue_redraw(); |
| 868 | } break; |
| 869 | |
| 870 | case NOTIFICATION_WM_WINDOW_FOCUS_OUT: { |
| 871 | window_has_focus = false; |
| 872 | draw_caret = false; |
| 873 | queue_redraw(); |
| 874 | set_selection_mode(SelectionMode::SELECTION_MODE_NONE); |
| 875 | } break; |
| 876 | |
| 877 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 878 | if (scrolling && get_v_scroll() != target_v_scroll) { |
| 879 | double target_y = target_v_scroll - get_v_scroll(); |
| 880 | double dist = std::abs(target_y); |
| 881 | // To ensure minimap is responsive override the speed setting. |
| 882 | double vel = ((target_y / dist) * ((minimap_clicked) ? 3000 : v_scroll_speed)) * get_process_delta_time(); |
| 883 | |
| 884 | // Prevent small velocities from blocking scrolling. |
| 885 | if (Math::abs(vel) < v_scroll->get_step()) { |
| 886 | vel = v_scroll->get_step() * SIGN(vel); |
| 887 | } |
| 888 | |
| 889 | if (Math::abs(vel) >= dist) { |
| 890 | set_v_scroll(target_v_scroll); |
| 891 | scrolling = false; |
| 892 | minimap_clicked = false; |
| 893 | set_process_internal(false); |
| 894 | } else { |
| 895 | set_v_scroll(get_v_scroll() + vel); |
| 896 | } |
| 897 | } else { |
| 898 | scrolling = false; |
| 899 | minimap_clicked = false; |
| 900 | set_process_internal(false); |
| 901 | } |
| 902 | } break; |
| 903 | |
| 904 | case NOTIFICATION_DRAW: { |
| 905 | if (first_draw) { |
| 906 | // Size may not be the final one, so attempts to ensure caret was visible may have failed. |
| 907 | adjust_viewport_to_caret(); |
| 908 | first_draw = false; |
| 909 | } |
| 910 | |
| 911 | RID ci = get_canvas_item(); |
| 912 | Ref<StyleBox> style = _get_current_stylebox(); |
| 913 | |
| 914 | // Draw normal/read_only style. |
| 915 | style->draw(ci, Rect2(Point2(), get_size())); |
| 916 | |
| 917 | /* Prevent the resource getting lost between the editor and game. */ |
| 918 | if (Engine::get_singleton()->is_editor_hint()) { |
| 919 | if (syntax_highlighter.is_valid() && syntax_highlighter->get_text_edit() != this) { |
| 920 | syntax_highlighter->set_text_edit(this); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | Size2 size = get_size(); |
| 925 | bool rtl = is_layout_rtl(); |
| 926 | if ((!has_focus() && !(menu && menu->has_focus())) || !window_has_focus) { |
| 927 | draw_caret = false; |
| 928 | } |
| 929 | |
| 930 | _update_scrollbars(); |
| 931 | |
| 932 | RS::get_singleton()->canvas_item_clear(text_ci); |
| 933 | RS::get_singleton()->canvas_item_set_custom_rect(text_ci, !is_visibility_clip_disabled(), Rect2(Point2(0, 0), size)); |
| 934 | RS::get_singleton()->canvas_item_set_clip(text_ci, true); |
| 935 | RS::get_singleton()->canvas_item_set_visibility_layer(text_ci, get_visibility_layer()); |
| 936 | RS::get_singleton()->canvas_item_set_default_texture_filter(text_ci, RS::CanvasItemTextureFilter(get_texture_filter_in_tree())); |
| 937 | |
| 938 | int left_margin = Math::ceil(style->get_margin(SIDE_LEFT)); |
| 939 | int xmargin_beg = left_margin + gutters_width + gutter_padding; |
| 940 | |
| 941 | int xmargin_end = size.width - Math::floor(style->get_margin(SIDE_RIGHT)); |
| 942 | if (draw_minimap) { |
| 943 | xmargin_end -= minimap_width; |
| 944 | } |
| 945 | if (!editable) { |
| 946 | draw_caret = is_drawing_caret_when_editable_disabled(); |
| 947 | } |
| 948 | if (has_focus(Engine::get_singleton()->is_editor_hint() || GLOBAL_GET_CACHED(int, "gui/common/show_focus_state_on_pointer_event") != 1)) { |
| 949 | theme_cache.style_focus->draw(ci, Rect2(Point2(), size)); |
| 950 | } |
| 951 | |
| 952 | int visible_rows = get_visible_line_count() + 1; |
| 953 | |
| 954 | #ifndef DISABLE_DEPRECATED |
| 955 | if (theme_cache.background_color.a > 0.01) { |
| 956 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(Point2i(), get_size()), theme_cache.background_color); |
| 957 | } |
| 958 | #endif // DISABLE_DEPRECATED |
| 959 | |
| 960 | Vector<BraceMatchingData> brace_matching; |
| 961 | if (highlight_matching_braces_enabled) { |
| 962 | brace_matching.resize(get_caret_count()); |
| 963 | |
| 964 | for (int caret = 0; caret < get_caret_count(); caret++) { |
| 965 | BraceMatchingData &brace_match = brace_matching.write[caret]; |
| 966 | |
| 967 | if (get_caret_line(caret) < 0 || get_caret_line(caret) >= text.size() || get_caret_column(caret) < 0) { |
| 968 | continue; |
| 969 | } |
| 970 | |
| 971 | if (get_caret_column(caret) < text[get_caret_line(caret)].length()) { |
| 972 | // Check for open. |
| 973 | char32_t c = text[get_caret_line(caret)][get_caret_column(caret)]; |
| 974 | char32_t closec = 0; |
| 975 | |
| 976 | if (c == '[') { |
| 977 | closec = ']'; |
| 978 | } else if (c == '{') { |
| 979 | closec = '}'; |
| 980 | } else if (c == '(') { |
| 981 | closec = ')'; |
| 982 | } |
| 983 | |
| 984 | if (closec != 0) { |
| 985 | int stack = 1; |
| 986 | |
| 987 | for (int i = get_caret_line(caret); i < text.size(); i++) { |
| 988 | int from = i == get_caret_line(caret) ? get_caret_column(caret) + 1 : 0; |
| 989 | for (int j = from; j < text[i].length(); j++) { |
| 990 | char32_t cc = text[i][j]; |
| 991 | // Ignore any brackets inside a string. |
| 992 | if (cc == '"' || cc == '\'') { |
| 993 | char32_t quotation = cc; |
| 994 | do { |
| 995 | j++; |
| 996 | if (!(j < text[i].length())) { |
| 997 | break; |
| 998 | } |
| 999 | cc = text[i][j]; |
| 1000 | // Skip over escaped quotation marks inside strings. |
| 1001 | if (cc == '\\') { |
| 1002 | bool escaped = true; |
| 1003 | while (j + 1 < text[i].length() && text[i][j + 1] == '\\') { |
| 1004 | escaped = !escaped; |
| 1005 | j++; |
| 1006 | } |
| 1007 | if (escaped) { |
| 1008 | j++; |
| 1009 | continue; |
| 1010 | } |
| 1011 | } |
| 1012 | } while (cc != quotation); |
| 1013 | } else if (cc == c) { |
| 1014 | stack++; |
| 1015 | } else if (cc == closec) { |
| 1016 | stack--; |
| 1017 | } |
| 1018 | |
| 1019 | if (stack == 0) { |
| 1020 | brace_match.open_match_line = i; |
| 1021 | brace_match.open_match_column = j; |
| 1022 | brace_match.open_matching = true; |
| 1023 | |
| 1024 | break; |
| 1025 | } |
| 1026 | } |
| 1027 | if (brace_match.open_match_line != -1) { |
| 1028 | break; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | if (!brace_match.open_matching) { |
| 1033 | brace_match.open_mismatch = true; |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | if (get_caret_column(caret) > 0) { |
| 1039 | char32_t c = text[get_caret_line(caret)][get_caret_column(caret) - 1]; |
| 1040 | char32_t closec = 0; |
| 1041 | |
| 1042 | if (c == ']') { |
| 1043 | closec = '['; |
| 1044 | } else if (c == '}') { |
| 1045 | closec = '{'; |
| 1046 | } else if (c == ')') { |
| 1047 | closec = '('; |
| 1048 | } |
| 1049 | |
| 1050 | if (closec != 0) { |
| 1051 | int stack = 1; |
| 1052 | |
| 1053 | for (int i = get_caret_line(caret); i >= 0; i--) { |
| 1054 | int from = i == get_caret_line(caret) ? get_caret_column(caret) - 2 : text[i].length() - 1; |
| 1055 | for (int j = from; j >= 0; j--) { |
| 1056 | char32_t cc = text[i][j]; |
| 1057 | // Ignore any brackets inside a string. |
| 1058 | if (cc == '"' || cc == '\'') { |
| 1059 | char32_t quotation = cc; |
| 1060 | do { |
| 1061 | j--; |
| 1062 | if (!(j >= 0)) { |
| 1063 | break; |
| 1064 | } |
| 1065 | cc = text[i][j]; |
| 1066 | // Skip over escaped quotation marks inside strings. |
| 1067 | if (cc == quotation) { |
| 1068 | bool escaped = false; |
| 1069 | while (j - 1 >= 0 && text[i][j - 1] == '\\') { |
| 1070 | escaped = !escaped; |
| 1071 | j--; |
| 1072 | } |
| 1073 | if (escaped) { |
| 1074 | cc = '\\'; |
| 1075 | continue; |
| 1076 | } |
| 1077 | } |
| 1078 | } while (cc != quotation); |
| 1079 | } else if (cc == c) { |
| 1080 | stack++; |
| 1081 | } else if (cc == closec) { |
| 1082 | stack--; |
| 1083 | } |
| 1084 | |
| 1085 | if (stack == 0) { |
| 1086 | brace_match.close_match_line = i; |
| 1087 | brace_match.close_match_column = j; |
| 1088 | brace_match.close_matching = true; |
| 1089 | |
| 1090 | break; |
| 1091 | } |
| 1092 | } |
| 1093 | if (brace_match.close_match_line != -1) { |
| 1094 | break; |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | if (!brace_match.close_matching) { |
| 1099 | brace_match.close_mismatch = true; |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | bool draw_placeholder = _using_placeholder(); |
| 1107 | |
| 1108 | // Get the highlighted words. |
| 1109 | String highlighted_text = get_selected_text(0); |
| 1110 | |
| 1111 | // Check if highlighted words contain only whitespaces (tabs or spaces). |
| 1112 | bool only_whitespaces_highlighted = highlighted_text.strip_edges().is_empty(); |
| 1113 | |
| 1114 | Vector<Pair<int, int>> highlighted_lines; |
| 1115 | highlighted_lines.resize(carets.size()); |
| 1116 | Vector<int> carets_wrap_index; |
| 1117 | carets_wrap_index.resize(carets.size()); |
| 1118 | for (int i = 0; i < carets.size(); i++) { |
| 1119 | carets.write[i].visible = false; |
| 1120 | int wrap_index = get_caret_wrap_index(i); |
| 1121 | highlighted_lines.write[i] = Pair<int, int>(get_caret_line(i), wrap_index); |
| 1122 | carets_wrap_index.write[i] = wrap_index; |
| 1123 | } |
| 1124 | |
| 1125 | int first_vis_line = get_first_visible_line() - 1; |
| 1126 | int draw_amount = visible_rows + 1; |
| 1127 | draw_amount += draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(first_vis_line + 1); |
| 1128 | |
| 1129 | // Draw minimap. |
| 1130 | if (draw_minimap) { |
| 1131 | int minimap_visible_lines = get_minimap_visible_lines(); |
| 1132 | int minimap_line_height = (minimap_char_size.y + minimap_line_spacing); |
| 1133 | int tab_size = text.get_tab_size(); |
| 1134 | |
| 1135 | // Calculate viewport size and y offset. |
| 1136 | int viewport_height = (draw_amount - 1) * minimap_line_height; |
| 1137 | int control_height = _get_control_height() - viewport_height; |
| 1138 | int viewport_offset_y = std::round(get_scroll_pos_for_line(first_vis_line + 1) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount)); |
| 1139 | |
| 1140 | // Calculate the first line. |
| 1141 | int num_lines_before = std::round((viewport_offset_y) / minimap_line_height); |
| 1142 | int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line; |
| 1143 | if (minimap_line >= 0) { |
| 1144 | minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x; |
| 1145 | minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0); |
| 1146 | } |
| 1147 | int minimap_draw_amount = minimap_visible_lines + get_line_wrap_count(minimap_line + 1); |
| 1148 | |
| 1149 | // Draw the minimap. |
| 1150 | |
| 1151 | // Add visual feedback when dragging or hovering the visible area rectangle. |
| 1152 | Color viewport_color = theme_cache.caret_color; |
| 1153 | if (dragging_minimap) { |
| 1154 | viewport_color.a = 0.25; |
| 1155 | } else if (hovering_minimap) { |
| 1156 | viewport_color.a = 0.175; |
| 1157 | } else { |
| 1158 | viewport_color.a = 0.1; |
| 1159 | } |
| 1160 | |
| 1161 | if (rtl) { |
| 1162 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, viewport_offset_y, minimap_width, viewport_height), viewport_color); |
| 1163 | } else { |
| 1164 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2((xmargin_end + 2), viewport_offset_y, minimap_width, viewport_height), viewport_color); |
| 1165 | } |
| 1166 | |
| 1167 | for (int i = 0; i < minimap_draw_amount; i++) { |
| 1168 | minimap_line++; |
| 1169 | |
| 1170 | if (minimap_line < 0 || minimap_line >= (int)text.size()) { |
| 1171 | break; |
| 1172 | } |
| 1173 | |
| 1174 | while (_is_line_hidden(minimap_line)) { |
| 1175 | minimap_line++; |
| 1176 | if (minimap_line < 0 || minimap_line >= (int)text.size()) { |
| 1177 | break; |
| 1178 | } |
| 1179 | } |
| 1180 | |
| 1181 | if (minimap_line < 0 || minimap_line >= (int)text.size()) { |
| 1182 | break; |
| 1183 | } |
| 1184 | |
| 1185 | const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(minimap_line); |
| 1186 | |
| 1187 | Color line_background_color = text.get_line_background_color(minimap_line); |
| 1188 | |
| 1189 | // Make non-default background colors more visible, such as error markers. |
| 1190 | // If a line background color is being applied, like in an error marker, the alpha is set to 1.0. |
| 1191 | // Else, it stays zero. |
| 1192 | line_background_color.a = 1.0 * (line_background_color != Color(0, 0, 0, 0)); |
| 1193 | |
| 1194 | Color current_color = editable ? theme_cache.font_color : theme_cache.font_readonly_color; |
| 1195 | |
| 1196 | const Vector<String> wrap_rows = get_line_wrapped_text(minimap_line); |
| 1197 | int line_wrap_amount = get_line_wrap_count(minimap_line); |
| 1198 | int last_wrap_column = 0; |
| 1199 | |
| 1200 | int first_indent_line = 0; |
| 1201 | float wrap_indent_line = 0.0; |
| 1202 | if (text.is_indent_wrapped_lines()) { |
| 1203 | wrap_indent_line = _get_wrapped_indent_level(minimap_line, first_indent_line); |
| 1204 | wrap_indent_line = MIN(wrap_indent_line, (minimap_width / minimap_char_size.x) * 0.6); |
| 1205 | } |
| 1206 | for (int line_wrap_index = 0; line_wrap_index < line_wrap_amount + 1; line_wrap_index++) { |
| 1207 | if (line_wrap_index != 0) { |
| 1208 | i++; |
| 1209 | if (i >= minimap_draw_amount) { |
| 1210 | break; |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | const String &str = wrap_rows[line_wrap_index]; |
| 1215 | int indent_px = line_wrap_index > first_indent_line ? wrap_indent_line : 0.0; |
| 1216 | if (indent_px >= wrap_at_column) { |
| 1217 | indent_px = 0; |
| 1218 | } |
| 1219 | indent_px = minimap_char_size.x * indent_px; |
| 1220 | |
| 1221 | if (line_wrap_index > 0) { |
| 1222 | last_wrap_column += wrap_rows[line_wrap_index - 1].length(); |
| 1223 | } |
| 1224 | |
| 1225 | if (highlight_current_line && highlighted_lines.has(Pair<int, int>(minimap_line, line_wrap_index))) { |
| 1226 | if (rtl) { |
| 1227 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), theme_cache.current_line_color); |
| 1228 | } else { |
| 1229 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), theme_cache.current_line_color); |
| 1230 | } |
| 1231 | } else if (line_background_color.a > 0) { |
| 1232 | if (rtl) { |
| 1233 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - (xmargin_end + 2) - minimap_width, i * 3, minimap_width, 2), line_background_color); |
| 1234 | } else { |
| 1235 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2((xmargin_end + 2), i * 3, minimap_width, 2), line_background_color); |
| 1236 | } |
| 1237 | } |
| 1238 | |
| 1239 | Color next_color = current_color; |
| 1240 | int characters = 0; |
| 1241 | int tab_alignment = 0; |
| 1242 | int xpos = xmargin_end + 2 + indent_px; |
| 1243 | for (int j = 0; j < str.length(); j++) { |
| 1244 | bool next_is_whitespace = false; |
| 1245 | bool next_is_tab = false; |
| 1246 | // Get the number of characters to draw together. |
| 1247 | for (characters = 0; j + characters < str.length(); characters++) { |
| 1248 | int next_char_index = j + characters; |
| 1249 | |
| 1250 | for (const Pair<int64_t, Color> &color_data : color_map) { |
| 1251 | if (last_wrap_column + next_char_index >= color_data.first) { |
| 1252 | next_color = color_data.second; |
| 1253 | if (!editable) { |
| 1254 | next_color.a = theme_cache.font_readonly_color.a; |
| 1255 | } |
| 1256 | next_color.a *= 0.6; |
| 1257 | } else { |
| 1258 | break; |
| 1259 | } |
| 1260 | } |
| 1261 | if (characters == 0) { |
| 1262 | current_color = next_color; |
| 1263 | } |
| 1264 | if (next_color != current_color) { |
| 1265 | break; |
| 1266 | } |
| 1267 | next_is_whitespace = is_whitespace(str[next_char_index]); |
| 1268 | if (next_is_whitespace) { |
| 1269 | if (str[next_char_index] == '\t') { |
| 1270 | next_is_tab = true; |
| 1271 | } |
| 1272 | break; |
| 1273 | } |
| 1274 | bool out_of_bounds = xpos + minimap_char_size.x * characters >= xmargin_end + minimap_width; |
| 1275 | if (out_of_bounds) { |
| 1276 | break; |
| 1277 | } |
| 1278 | } |
| 1279 | if (!next_is_whitespace && characters == 0) { |
| 1280 | break; |
| 1281 | } |
| 1282 | |
| 1283 | if (characters > 0) { |
| 1284 | if (rtl) { |
| 1285 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(Point2(size.width - xpos - minimap_char_size.x * characters, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), current_color); |
| 1286 | } else { |
| 1287 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(Point2(xpos, minimap_line_height * i), Point2(minimap_char_size.x * characters, minimap_char_size.y)), current_color); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | j += characters - 1; |
| 1292 | xpos += minimap_char_size.x * characters; |
| 1293 | tab_alignment += characters; |
| 1294 | |
| 1295 | if (next_is_whitespace) { |
| 1296 | if (next_is_tab) { |
| 1297 | tab_alignment %= tab_size; |
| 1298 | xpos += minimap_char_size.x * (tab_size - tab_alignment); |
| 1299 | tab_alignment = 0; |
| 1300 | } else { |
| 1301 | xpos += minimap_char_size.x; |
| 1302 | tab_alignment += 1; |
| 1303 | } |
| 1304 | j += 1; |
| 1305 | } |
| 1306 | |
| 1307 | if (xpos >= xmargin_end + minimap_width) { |
| 1308 | // Out of bounds. |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | int top_limit_y = style->get_margin(SIDE_TOP); |
| 1317 | int bottom_limit_y = get_size().height - style->get_margin(SIDE_BOTTOM); |
| 1318 | |
| 1319 | // Draw guidelines. |
| 1320 | _draw_guidelines(); |
| 1321 | |
| 1322 | // Draw main text. |
| 1323 | line_drawing_cache.clear(); |
| 1324 | int row_height = draw_placeholder ? placeholder_line_height + theme_cache.line_spacing : get_line_height(); |
| 1325 | int line = first_vis_line; |
| 1326 | for (int i = 0; i < draw_amount; i++) { |
| 1327 | line++; |
| 1328 | |
| 1329 | if (line < 0 || line >= (int)text.size()) { |
| 1330 | continue; |
| 1331 | } |
| 1332 | |
| 1333 | while (_is_line_hidden(line)) { |
| 1334 | line++; |
| 1335 | if (line < 0 || line >= (int)text.size()) { |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | if (line < 0 || line >= (int)text.size()) { |
| 1341 | continue; |
| 1342 | } |
| 1343 | |
| 1344 | LineDrawingCache cache_entry; |
| 1345 | |
| 1346 | const Vector<Pair<int64_t, Color>> color_map = _get_line_syntax_highlighting(line); |
| 1347 | |
| 1348 | // Ensure we at least use the font color. |
| 1349 | Color current_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color; |
| 1350 | if (draw_placeholder) { |
| 1351 | current_color = theme_cache.font_placeholder_color; |
| 1352 | } |
| 1353 | |
| 1354 | const Ref<TextParagraph> ldata = draw_placeholder ? placeholder_data_buf : text.get_line_data(line); |
| 1355 | |
| 1356 | const Vector<String> wrap_rows = draw_placeholder ? placeholder_wrapped_rows : get_line_wrapped_text(line); |
| 1357 | int line_wrap_amount = draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(line); |
| 1358 | |
| 1359 | int first_indent_line = 0; |
| 1360 | float indent_ofs = 0.0; |
| 1361 | if (text.is_indent_wrapped_lines()) { |
| 1362 | _get_wrapped_indent_level(line, first_indent_line); |
| 1363 | indent_ofs = MIN(text.get_indent_offset(line, rtl), wrap_at_column * 0.6); |
| 1364 | } |
| 1365 | for (int line_wrap_index = 0; line_wrap_index <= line_wrap_amount; line_wrap_index++) { |
| 1366 | if (line_wrap_index != 0) { |
| 1367 | i++; |
| 1368 | if (i >= draw_amount) { |
| 1369 | break; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | const String &str = wrap_rows[line_wrap_index]; |
| 1374 | int char_margin = xmargin_beg - first_visible_col; |
| 1375 | |
| 1376 | int ofs_y = style->get_margin(SIDE_TOP); |
| 1377 | |
| 1378 | ofs_y += i * row_height + theme_cache.line_spacing / 2; |
| 1379 | ofs_y -= first_visible_line_wrap_ofs * row_height; |
| 1380 | ofs_y -= _get_v_scroll_offset() * row_height; |
| 1381 | |
| 1382 | bool clipped = false; |
| 1383 | if (ofs_y + row_height < top_limit_y) { |
| 1384 | // Line is outside the top margin, clip current line. |
| 1385 | // Still need to go through the process to prepare color changes for next lines. |
| 1386 | clipped = true; |
| 1387 | } |
| 1388 | |
| 1389 | if (ofs_y > bottom_limit_y) { |
| 1390 | // Line is outside the bottom margin, clip any remaining text. |
| 1391 | i = draw_amount; |
| 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | if (text.get_line_background_color(line).a > 0.0) { |
| 1396 | if (rtl) { |
| 1397 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - xmargin_end, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line)); |
| 1398 | } else { |
| 1399 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(xmargin_beg, ofs_y, xmargin_end - xmargin_beg, row_height), text.get_line_background_color(line)); |
| 1400 | } |
| 1401 | } |
| 1402 | |
| 1403 | // Draw current line highlight. |
| 1404 | if (highlight_current_line && highlighted_lines.has(Pair<int, int>(line, line_wrap_index))) { |
| 1405 | if (rtl) { |
| 1406 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(size.width - xmargin_end, ofs_y, xmargin_end, row_height), theme_cache.current_line_color); |
| 1407 | } else { |
| 1408 | RS::get_singleton()->canvas_item_add_rect(text_ci, Rect2(0, ofs_y, xmargin_end, row_height), theme_cache.current_line_color); |
| 1409 | } |
| 1410 | } |
| 1411 | |
| 1412 | if (line_wrap_index == 0) { |
| 1413 | // Only do these if we are on the first wrapped part of a line. |
| 1414 | |
| 1415 | cache_entry.y_offset = ofs_y; |
| 1416 | |
| 1417 | int gutter_offset = left_margin; |
| 1418 | for (int g = 0; g < gutters.size(); g++) { |
| 1419 | const GutterInfo &gutter = gutters[g]; |
| 1420 | |
| 1421 | if (!gutter.draw || gutter.width <= 0) { |
| 1422 | continue; |
| 1423 | } |
| 1424 | |
| 1425 | switch (gutter.type) { |
| 1426 | case GUTTER_TYPE_STRING: { |
| 1427 | const String &txt = get_line_gutter_text(line, g); |
| 1428 | if (txt.is_empty()) { |
| 1429 | break; |
| 1430 | } |
| 1431 | |
| 1432 | Ref<TextLine> tl; |
| 1433 | tl.instantiate(); |
| 1434 | tl->add_string(txt, theme_cache.font, theme_cache.font_size, _get_locale()); |
| 1435 | |
| 1436 | int yofs = ofs_y + (row_height - tl->get_size().y) / 2; |
| 1437 | if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) { |
| 1438 | tl->draw_outline(text_ci, Point2(gutter_offset, yofs), theme_cache.outline_size, theme_cache.outline_color); |
| 1439 | } |
| 1440 | tl->draw(text_ci, Point2(gutter_offset, yofs), get_line_gutter_item_color(line, g)); |
| 1441 | } break; |
| 1442 | case GUTTER_TYPE_ICON: { |
| 1443 | const Ref<Texture2D> icon = get_line_gutter_icon(line, g); |
| 1444 | if (icon.is_null()) { |
| 1445 | break; |
| 1446 | } |
| 1447 | |
| 1448 | Rect2 gutter_rect = Rect2(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height)); |
| 1449 | |
| 1450 | int horizontal_padding = gutter_rect.size.x / 6; |
| 1451 | int vertical_padding = gutter_rect.size.y / 6; |
| 1452 | |
| 1453 | gutter_rect.position += Point2(horizontal_padding, vertical_padding); |
| 1454 | gutter_rect.size -= Point2(horizontal_padding, vertical_padding) * 2; |
| 1455 | |
| 1456 | // Correct icon aspect ratio. |
| 1457 | float icon_ratio = icon->get_width() / icon->get_height(); |
| 1458 | float gutter_ratio = gutter_rect.size.x / gutter_rect.size.y; |
| 1459 | if (gutter_ratio > icon_ratio) { |
| 1460 | gutter_rect.size.x = std::floor(icon->get_width() * (gutter_rect.size.y / icon->get_height())); |
| 1461 | } else { |
| 1462 | gutter_rect.size.y = std::floor(icon->get_height() * (gutter_rect.size.x / icon->get_width())); |
| 1463 | } |
| 1464 | if (rtl) { |
| 1465 | gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x; |
| 1466 | } |
| 1467 | |
| 1468 | icon->draw_rect(text_ci, gutter_rect, false, get_line_gutter_item_color(line, g)); |
| 1469 | } break; |
| 1470 | case GUTTER_TYPE_CUSTOM: { |
| 1471 | if (gutter.custom_draw_callback.is_valid()) { |
| 1472 | Rect2i gutter_rect = Rect2i(Point2i(gutter_offset, ofs_y), Size2i(gutter.width, row_height)); |
| 1473 | if (rtl) { |
| 1474 | gutter_rect.position.x = size.width - gutter_rect.position.x - gutter_rect.size.x; |
| 1475 | } |
| 1476 | gutter.custom_draw_callback.call(line, g, Rect2(gutter_rect)); |
| 1477 | } |
| 1478 | } break; |
| 1479 | } |
| 1480 | |
| 1481 | gutter_offset += gutter.width; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | // Draw line. |
| 1486 | RID rid = ldata->get_line_rid(line_wrap_index); |
| 1487 | float text_height = TS->shaped_text_get_size(rid).y; |
| 1488 | float wrap_indent = line_wrap_index > first_indent_line ? indent_ofs : 0.0; |
| 1489 | |
| 1490 | if (rtl) { |
| 1491 | char_margin = size.width - char_margin - TS->shaped_text_get_size(rid).x - wrap_indent; |
| 1492 | } else { |
| 1493 | char_margin += wrap_indent; |
| 1494 | } |
| 1495 | |
| 1496 | // Validate inline objects. |
| 1497 | Vector<Dictionary> object_keys; |
| 1498 | if (inline_object_drawer.is_valid()) { |
| 1499 | for (Variant k : ldata->get_line_objects(line_wrap_index)) { |
| 1500 | if (!is_inline_info_valid(k)) { |
| 1501 | continue; |
| 1502 | } |
| 1503 | object_keys.push_back(k); |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | // Draw selections. |
| 1508 | float char_w = theme_cache.font->get_char_size(' ', theme_cache.font_size).width; |
| 1509 | for (int c = 0; c < get_caret_count(); c++) { |
| 1510 | if (!clipped && has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) { |
| 1511 | int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c); |
| 1512 | int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c); |
| 1513 | Vector<Vector2> sel = TS->shaped_text_get_selection(rid, sel_from, sel_to); |
| 1514 | |
| 1515 | // Show selection at the end of line. |
| 1516 | if (line_wrap_index == line_wrap_amount && line < get_selection_to_line(c)) { |
| 1517 | if (rtl) { |
| 1518 | sel.push_back(Vector2(-char_w, 0)); |
| 1519 | } else { |
| 1520 | float line_end = TS->shaped_text_get_size(rid).width; |
| 1521 | sel.push_back(Vector2(line_end, line_end + char_w)); |
| 1522 | } |
| 1523 | } |
| 1524 | // Show selection for inline objects. |
| 1525 | for (Dictionary info : object_keys) { |
| 1526 | int info_column = info["column"]; |
| 1527 | if (info_column >= sel_from && info_column < sel_to) { |
| 1528 | Rect2 orect = TS->shaped_text_get_object_rect(rid, info); |
| 1529 | sel.push_back(Vector2(orect.position.x, orect.position.x + orect.size.x)); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | for (int j = 0; j < sel.size(); j++) { |
| 1534 | Rect2 rect = Rect2(Math::ceil(sel[j].x) + char_margin, ofs_y, Math::ceil(sel[j].y) - Math::ceil(sel[j].x), row_height); |
| 1535 | if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { |
| 1536 | continue; |
| 1537 | } |
| 1538 | if (rect.position.x < xmargin_beg) { |
| 1539 | rect.size.x -= (xmargin_beg - rect.position.x); |
| 1540 | rect.position.x = xmargin_beg; |
| 1541 | } |
| 1542 | if (rect.position.x + rect.size.x > xmargin_end) { |
| 1543 | rect.size.x = xmargin_end - rect.position.x; |
| 1544 | } |
| 1545 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, theme_cache.selection_color); |
| 1546 | } |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | int start = TS->shaped_text_get_range(rid).x; |
| 1551 | if (!clipped && !search_text.is_empty()) { // Search highlight |
| 1552 | int search_text_col = _get_column_pos_of_word(search_text, str, search_flags, 0); |
| 1553 | int search_text_len = search_text.length(); |
| 1554 | while (search_text_col != -1) { |
| 1555 | const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, search_text_col + start, search_text_col + search_text_len + start); |
| 1556 | for (int j = 0; j < sel.size(); j++) { |
| 1557 | Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, row_height); |
| 1558 | if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { |
| 1559 | continue; |
| 1560 | } |
| 1561 | if (rect.position.x < xmargin_beg) { |
| 1562 | rect.size.x -= (xmargin_beg - rect.position.x); |
| 1563 | rect.position.x = xmargin_beg; |
| 1564 | } else if (rect.position.x + rect.size.x > xmargin_end) { |
| 1565 | rect.size.x = xmargin_end - rect.position.x; |
| 1566 | } |
| 1567 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, theme_cache.search_result_color); |
| 1568 | _draw_rect_unfilled(text_ci, rect, theme_cache.search_result_border_color); |
| 1569 | } |
| 1570 | |
| 1571 | search_text_col = _get_column_pos_of_word(search_text, str, search_flags, search_text_col + search_text_len); |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | if (!clipped && highlight_all_occurrences && !only_whitespaces_highlighted && !highlighted_text.is_empty()) { // Highlight |
| 1576 | int highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0); |
| 1577 | int highlighted_text_len = highlighted_text.length(); |
| 1578 | while (highlighted_text_col != -1) { |
| 1579 | const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, highlighted_text_col + start, highlighted_text_col + highlighted_text_len + start); |
| 1580 | for (int j = 0; j < sel.size(); j++) { |
| 1581 | Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, row_height); |
| 1582 | if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { |
| 1583 | continue; |
| 1584 | } |
| 1585 | if (rect.position.x < xmargin_beg) { |
| 1586 | rect.size.x -= (xmargin_beg - rect.position.x); |
| 1587 | rect.position.x = xmargin_beg; |
| 1588 | } else if (rect.position.x + rect.size.x > xmargin_end) { |
| 1589 | rect.size.x = xmargin_end - rect.position.x; |
| 1590 | } |
| 1591 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, theme_cache.word_highlighted_color); |
| 1592 | } |
| 1593 | |
| 1594 | highlighted_text_col = _get_column_pos_of_word(highlighted_text, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, highlighted_text_col + highlighted_text_len); |
| 1595 | } |
| 1596 | } |
| 1597 | |
| 1598 | if (!clipped && lookup_symbol_word.length() != 0) { // Highlight word |
| 1599 | if (is_unicode_identifier_start(lookup_symbol_word[0]) || lookup_symbol_word[0] == '.') { |
| 1600 | Color highlight_underline_color = !editable ? theme_cache.font_readonly_color : theme_cache.font_color; |
| 1601 | int lookup_symbol_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, 0); |
| 1602 | int lookup_symbol_word_len = lookup_symbol_word.length(); |
| 1603 | while (lookup_symbol_word_col != -1) { |
| 1604 | const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, lookup_symbol_word_col + start, lookup_symbol_word_col + lookup_symbol_word_len + start); |
| 1605 | for (int j = 0; j < sel.size(); j++) { |
| 1606 | Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y + (theme_cache.line_spacing / 2), sel[j].y - sel[j].x, row_height); |
| 1607 | if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { |
| 1608 | continue; |
| 1609 | } |
| 1610 | if (rect.position.x < xmargin_beg) { |
| 1611 | rect.size.x -= (xmargin_beg - rect.position.x); |
| 1612 | rect.position.x = xmargin_beg; |
| 1613 | } else if (rect.position.x + rect.size.x > xmargin_end) { |
| 1614 | rect.size.x = xmargin_end - rect.position.x; |
| 1615 | } |
| 1616 | rect.position.y += std::ceil(TS->shaped_text_get_ascent(rid)) + std::ceil(theme_cache.font->get_underline_position(theme_cache.font_size)); |
| 1617 | rect.size.y = MAX(1, theme_cache.font->get_underline_thickness(theme_cache.font_size)); |
| 1618 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, highlight_underline_color); |
| 1619 | } |
| 1620 | |
| 1621 | lookup_symbol_word_col = _get_column_pos_of_word(lookup_symbol_word, str, SEARCH_MATCH_CASE | SEARCH_WHOLE_WORDS, lookup_symbol_word_col + lookup_symbol_word_len); |
| 1622 | } |
| 1623 | } |
| 1624 | } |
| 1625 | |
| 1626 | ofs_y += (row_height - text_height) / 2; |
| 1627 | |
| 1628 | const Glyph *glyphs = TS->shaped_text_get_glyphs(rid); |
| 1629 | int gl_size = TS->shaped_text_get_glyph_count(rid); |
| 1630 | |
| 1631 | ofs_y += ldata->get_line_ascent(line_wrap_index); |
| 1632 | |
| 1633 | int first_visible_char = TS->shaped_text_get_range(rid).y; |
| 1634 | int last_visible_char = TS->shaped_text_get_range(rid).x; |
| 1635 | |
| 1636 | float char_ofs = 0; |
| 1637 | if (theme_cache.outline_size > 0 && theme_cache.outline_color.a > 0) { |
| 1638 | for (int j = 0; j < gl_size; j++) { |
| 1639 | for (int k = 0; k < glyphs[j].repeat; k++) { |
| 1640 | if ((char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) { |
| 1641 | if (glyphs[j].font_rid != RID()) { |
| 1642 | TS->font_draw_glyph_outline(glyphs[j].font_rid, text_ci, glyphs[j].font_size, theme_cache.outline_size, Vector2(char_margin + char_ofs + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, theme_cache.outline_color); |
| 1643 | } |
| 1644 | } |
| 1645 | char_ofs += glyphs[j].advance; |
| 1646 | } |
| 1647 | if ((char_ofs + char_margin) >= xmargin_end) { |
| 1648 | break; |
| 1649 | } |
| 1650 | } |
| 1651 | char_ofs = 0; |
| 1652 | } |
| 1653 | |
| 1654 | // Draw inline objects. |
| 1655 | for (Dictionary k : object_keys) { |
| 1656 | Rect2 col_rect = TS->shaped_text_get_object_rect(rid, k); |
| 1657 | col_rect.position += Vector2(char_margin, ofs_y); |
| 1658 | if (!clipped && (col_rect.position.x) >= xmargin_beg && (col_rect.position.x + col_rect.size.x) <= xmargin_end) { |
| 1659 | inline_object_drawer.call(k, col_rect); |
| 1660 | } |
| 1661 | } |
| 1662 | |
| 1663 | for (int j = 0; j < gl_size; j++) { |
| 1664 | for (const Pair<int64_t, Color> &color_data : color_map) { |
| 1665 | if (color_data.first <= glyphs[j].start) { |
| 1666 | current_color = color_data.second; |
| 1667 | if (!editable && current_color.a > theme_cache.font_readonly_color.a) { |
| 1668 | current_color.a = theme_cache.font_readonly_color.a; |
| 1669 | } |
| 1670 | } else { |
| 1671 | break; |
| 1672 | } |
| 1673 | } |
| 1674 | Color gl_color = current_color; |
| 1675 | |
| 1676 | for (int c = 0; c < get_caret_count(); c++) { |
| 1677 | if (has_selection(c) && line >= get_selection_from_line(c) && line <= get_selection_to_line(c)) { // Selection |
| 1678 | int sel_from = (line > get_selection_from_line(c)) ? TS->shaped_text_get_range(rid).x : get_selection_from_column(c); |
| 1679 | int sel_to = (line < get_selection_to_line(c)) ? TS->shaped_text_get_range(rid).y : get_selection_to_column(c); |
| 1680 | |
| 1681 | if (glyphs[j].start >= sel_from && glyphs[j].end <= sel_to && use_selected_font_color) { |
| 1682 | gl_color = theme_cache.font_selected_color; |
| 1683 | } |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | float char_pos = char_ofs + char_margin; |
| 1688 | if (char_pos >= xmargin_beg) { |
| 1689 | if (highlight_matching_braces_enabled) { |
| 1690 | for (int c = 0; c < get_caret_count(); c++) { |
| 1691 | const BraceMatchingData &brace_match = brace_matching[c]; |
| 1692 | if ((brace_match.open_match_line == line && brace_match.open_match_column == glyphs[j].start) || |
| 1693 | (get_caret_column(c) == glyphs[j].start && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_match.open_matching || brace_match.open_mismatch))) { |
| 1694 | if (brace_match.open_mismatch) { |
| 1695 | gl_color = _get_brace_mismatch_color(); |
| 1696 | } |
| 1697 | Rect2 rect = Rect2(char_pos, ofs_y + theme_cache.font->get_underline_position(theme_cache.font_size), glyphs[j].advance * glyphs[j].repeat, MAX(theme_cache.font->get_underline_thickness(theme_cache.font_size) * theme_cache.base_scale, 1)); |
| 1698 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, gl_color); |
| 1699 | } |
| 1700 | |
| 1701 | if ((brace_match.close_match_line == line && brace_match.close_match_column == glyphs[j].start) || |
| 1702 | (get_caret_column(c) == glyphs[j].start + 1 && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index && (brace_match.close_matching || brace_match.close_mismatch))) { |
| 1703 | if (brace_match.close_mismatch) { |
| 1704 | gl_color = _get_brace_mismatch_color(); |
| 1705 | } |
| 1706 | Rect2 rect = Rect2(char_pos, ofs_y + theme_cache.font->get_underline_position(theme_cache.font_size), glyphs[j].advance * glyphs[j].repeat, MAX(theme_cache.font->get_underline_thickness(theme_cache.font_size) * theme_cache.base_scale, 1)); |
| 1707 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, gl_color); |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | if (draw_tabs && ((glyphs[j].flags & TextServer::GRAPHEME_IS_TAB) == TextServer::GRAPHEME_IS_TAB)) { |
| 1713 | int yofs = (text_height - theme_cache.tab_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index); |
| 1714 | theme_cache.tab_icon->draw(text_ci, Point2(char_pos, ofs_y + yofs), gl_color); |
| 1715 | } else if (draw_spaces && ((glyphs[j].flags & TextServer::GRAPHEME_IS_SPACE) == TextServer::GRAPHEME_IS_SPACE) && ((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL)) { |
| 1716 | int yofs = (text_height - theme_cache.space_icon->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index); |
| 1717 | int xofs = (glyphs[j].advance * glyphs[j].repeat - theme_cache.space_icon->get_width()) / 2; |
| 1718 | theme_cache.space_icon->draw(text_ci, Point2(char_pos + xofs, ofs_y + yofs), gl_color); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | bool had_glyphs_drawn = false; |
| 1723 | for (int k = 0; k < glyphs[j].repeat; k++) { |
| 1724 | if (!clipped && (char_ofs + char_margin) >= xmargin_beg && (char_ofs + glyphs[j].advance + char_margin) <= xmargin_end) { |
| 1725 | if (glyphs[j].font_rid != RID()) { |
| 1726 | TS->font_draw_glyph(glyphs[j].font_rid, text_ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, gl_color); |
| 1727 | had_glyphs_drawn = true; |
| 1728 | } else if (((glyphs[j].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((glyphs[j].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) { |
| 1729 | TS->draw_hex_code_box(text_ci, glyphs[j].font_size, Vector2(char_margin + char_ofs + glyphs[j].x_off, ofs_y + glyphs[j].y_off), glyphs[j].index, gl_color); |
| 1730 | had_glyphs_drawn = true; |
| 1731 | } |
| 1732 | } |
| 1733 | char_ofs += glyphs[j].advance; |
| 1734 | } |
| 1735 | |
| 1736 | if (had_glyphs_drawn) { |
| 1737 | if (first_visible_char > glyphs[j].start) { |
| 1738 | first_visible_char = glyphs[j].start; |
| 1739 | } |
| 1740 | if (last_visible_char < glyphs[j].end) { |
| 1741 | last_visible_char = glyphs[j].end; |
| 1742 | } |
| 1743 | } |
| 1744 | |
| 1745 | if ((char_ofs + char_margin) >= xmargin_end) { |
| 1746 | break; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | cache_entry.first_visible_chars.push_back(first_visible_char); |
| 1751 | cache_entry.last_visible_chars.push_back(last_visible_char); |
| 1752 | |
| 1753 | // is_line_folded |
| 1754 | if (line_wrap_index == line_wrap_amount && line < text.size() - 1 && _is_line_hidden(line + 1)) { |
| 1755 | int xofs = char_ofs + char_margin + (_get_folded_eol_icon()->get_width() / 2); |
| 1756 | if (xofs >= xmargin_beg && xofs < xmargin_end) { |
| 1757 | int yofs = (text_height - _get_folded_eol_icon()->get_height()) / 2 - ldata->get_line_ascent(line_wrap_index); |
| 1758 | Color eol_color = _get_code_folding_color(); |
| 1759 | eol_color.a = 1; |
| 1760 | _get_folded_eol_icon()->draw(text_ci, Point2(xofs, ofs_y + yofs), eol_color); |
| 1761 | } |
| 1762 | } |
| 1763 | |
| 1764 | // Carets. |
| 1765 | // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1). |
| 1766 | const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale); |
| 1767 | |
| 1768 | for (int c = 0; c < carets.size(); c++) { |
| 1769 | if (!clipped && get_caret_line(c) == line && carets_wrap_index[c] == line_wrap_index) { |
| 1770 | carets.write[c].draw_pos.y = ofs_y + ldata->get_line_descent(line_wrap_index); |
| 1771 | |
| 1772 | if (ime_text.is_empty() || ime_selection.y == 0) { |
| 1773 | // Normal caret. |
| 1774 | CaretInfo ts_caret; |
| 1775 | if (!str.is_empty() || !ime_text.is_empty()) { |
| 1776 | // Get carets. |
| 1777 | ts_caret = TS->shaped_text_get_carets(rid, ime_text.is_empty() ? get_caret_column(c) : get_caret_column(c) + ime_selection.x); |
| 1778 | } else { |
| 1779 | // No carets, add one at the start. |
| 1780 | int h = theme_cache.font->get_height(theme_cache.font_size); |
| 1781 | if (rtl) { |
| 1782 | ts_caret.l_dir = TextServer::DIRECTION_RTL; |
| 1783 | ts_caret.l_caret = Rect2(Vector2(TS->shaped_text_get_size(rid).x, -h / 2), Size2(caret_width * 4, h)); |
| 1784 | } else { |
| 1785 | ts_caret.l_dir = TextServer::DIRECTION_LTR; |
| 1786 | ts_caret.l_caret = Rect2(Vector2(char_ofs, -h / 2), Size2(caret_width * 4, h)); |
| 1787 | } |
| 1788 | } |
| 1789 | |
| 1790 | if ((ts_caret.l_caret != Rect2() && (ts_caret.l_dir == TextServer::DIRECTION_AUTO || ts_caret.l_dir == (TextServer::Direction)input_direction)) || (ts_caret.t_caret == Rect2())) { |
| 1791 | carets.write[c].draw_pos.x = char_margin + ts_caret.l_caret.position.x; |
| 1792 | } else { |
| 1793 | carets.write[c].draw_pos.x = char_margin + ts_caret.t_caret.position.x; |
| 1794 | } |
| 1795 | |
| 1796 | if (get_caret_draw_pos(c).x >= xmargin_beg && get_caret_draw_pos(c).x <= xmargin_end) { |
| 1797 | carets.write[c].visible = true; |
| 1798 | if (draw_caret || drag_caret_force_displayed) { |
| 1799 | if (caret_type == CaretType::CARET_TYPE_BLOCK || overtype_mode) { |
| 1800 | //Block or underline caret, draw trailing carets at full height. |
| 1801 | int h = theme_cache.font->get_height(theme_cache.font_size); |
| 1802 | |
| 1803 | if (ts_caret.t_caret != Rect2()) { |
| 1804 | if (overtype_mode) { |
| 1805 | ts_caret.t_caret.position.y = TS->shaped_text_get_descent(rid); |
| 1806 | ts_caret.t_caret.size.y = caret_width; |
| 1807 | } else { |
| 1808 | ts_caret.t_caret.position.y = -TS->shaped_text_get_ascent(rid); |
| 1809 | ts_caret.t_caret.size.y = h; |
| 1810 | } |
| 1811 | ts_caret.t_caret.position += Vector2(char_margin, ofs_y); |
| 1812 | if (overtype_mode) { |
| 1813 | RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.t_caret, theme_cache.caret_color); |
| 1814 | } else { |
| 1815 | _draw_rect_unfilled(text_ci, ts_caret.t_caret, theme_cache.caret_color); |
| 1816 | } |
| 1817 | |
| 1818 | if (ts_caret.l_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) { |
| 1819 | // Draw split caret (leading part). |
| 1820 | ts_caret.l_caret.position += Vector2(char_margin, ofs_y); |
| 1821 | ts_caret.l_caret.size.x = caret_width; |
| 1822 | RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.l_caret, theme_cache.caret_color); |
| 1823 | // Draw extra direction marker on top of split caret. |
| 1824 | float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3; |
| 1825 | Rect2 trect = Rect2(ts_caret.l_caret.position.x + d * caret_width, ts_caret.l_caret.position.y + ts_caret.l_caret.size.y - caret_width, 3 * caret_width, caret_width); |
| 1826 | RS::get_singleton()->canvas_item_add_rect(text_ci, trect, theme_cache.caret_color); |
| 1827 | } |
| 1828 | } else { // End of the line. |
| 1829 | if (gl_size > 0) { |
| 1830 | // Adjust for actual line dimensions. |
| 1831 | if (overtype_mode) { |
| 1832 | ts_caret.l_caret.position.y = TS->shaped_text_get_descent(rid); |
| 1833 | ts_caret.l_caret.size.y = caret_width; |
| 1834 | } else { |
| 1835 | ts_caret.l_caret.position.y = -TS->shaped_text_get_ascent(rid); |
| 1836 | ts_caret.l_caret.size.y = h; |
| 1837 | } |
| 1838 | } else if (overtype_mode) { |
| 1839 | ts_caret.l_caret.position.y += ts_caret.l_caret.size.y; |
| 1840 | ts_caret.l_caret.size.y = caret_width; |
| 1841 | } |
| 1842 | if (Math::ceil(ts_caret.l_caret.position.x) >= TS->shaped_text_get_size(rid).x) { |
| 1843 | ts_caret.l_caret.size.x = theme_cache.font->get_char_size('m', theme_cache.font_size).x; |
| 1844 | } else { |
| 1845 | ts_caret.l_caret.size.x = 3 * caret_width; |
| 1846 | } |
| 1847 | ts_caret.l_caret.position += Vector2(char_margin, ofs_y); |
| 1848 | if (ts_caret.l_dir == TextServer::DIRECTION_RTL) { |
| 1849 | ts_caret.l_caret.position.x -= ts_caret.l_caret.size.x; |
| 1850 | } |
| 1851 | if (overtype_mode) { |
| 1852 | RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.l_caret, theme_cache.caret_color); |
| 1853 | } else { |
| 1854 | _draw_rect_unfilled(text_ci, ts_caret.l_caret, theme_cache.caret_color); |
| 1855 | } |
| 1856 | } |
| 1857 | } else { |
| 1858 | // Normal caret. |
| 1859 | if (ts_caret.l_caret != Rect2() && ts_caret.l_dir == TextServer::DIRECTION_AUTO) { |
| 1860 | // Draw extra marker on top of mid caret. |
| 1861 | Rect2 trect = Rect2(ts_caret.l_caret.position.x - 2.5 * caret_width, ts_caret.l_caret.position.y, 6 * caret_width, caret_width); |
| 1862 | trect.position += Vector2(char_margin, ofs_y); |
| 1863 | RS::get_singleton()->canvas_item_add_rect(text_ci, trect, theme_cache.caret_color); |
| 1864 | } else if (ts_caret.l_caret != Rect2() && ts_caret.t_caret != Rect2() && ts_caret.l_dir != ts_caret.t_dir) { |
| 1865 | // Draw extra direction marker on top of split caret. |
| 1866 | float d = (ts_caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3; |
| 1867 | Rect2 trect = Rect2(ts_caret.l_caret.position.x + d * caret_width, ts_caret.l_caret.position.y + ts_caret.l_caret.size.y - caret_width, 3 * caret_width, caret_width); |
| 1868 | trect.position += Vector2(char_margin, ofs_y); |
| 1869 | RS::get_singleton()->canvas_item_add_rect(text_ci, trect, theme_cache.caret_color); |
| 1870 | |
| 1871 | d = (ts_caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3; |
| 1872 | trect = Rect2(ts_caret.t_caret.position.x + d * caret_width, ts_caret.t_caret.position.y, 3 * caret_width, caret_width); |
| 1873 | trect.position += Vector2(char_margin, ofs_y); |
| 1874 | RS::get_singleton()->canvas_item_add_rect(text_ci, trect, theme_cache.caret_color); |
| 1875 | } |
| 1876 | ts_caret.l_caret.position += Vector2(char_margin, ofs_y); |
| 1877 | ts_caret.l_caret.size.x = caret_width; |
| 1878 | |
| 1879 | RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.l_caret, theme_cache.caret_color); |
| 1880 | |
| 1881 | ts_caret.t_caret.position += Vector2(char_margin, ofs_y); |
| 1882 | ts_caret.t_caret.size.x = caret_width; |
| 1883 | |
| 1884 | RS::get_singleton()->canvas_item_add_rect(text_ci, ts_caret.t_caret, theme_cache.caret_color); |
| 1885 | } |
| 1886 | } |
| 1887 | } |
| 1888 | } |
| 1889 | if (!ime_text.is_empty()) { |
| 1890 | { |
| 1891 | // IME Intermediate text range. |
| 1892 | const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, get_caret_column(c), get_caret_column(c) + ime_text.length()); |
| 1893 | for (int j = 0; j < sel.size(); j++) { |
| 1894 | Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, text_height); |
| 1895 | if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { |
| 1896 | continue; |
| 1897 | } |
| 1898 | if (rect.position.x < xmargin_beg) { |
| 1899 | rect.size.x -= (xmargin_beg - rect.position.x); |
| 1900 | rect.position.x = xmargin_beg; |
| 1901 | } else if (rect.position.x + rect.size.x > xmargin_end) { |
| 1902 | rect.size.x = xmargin_end - rect.position.x; |
| 1903 | } |
| 1904 | rect.size.y = caret_width; |
| 1905 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, theme_cache.caret_color); |
| 1906 | carets.write[c].draw_pos.x = rect.position.x; |
| 1907 | } |
| 1908 | } |
| 1909 | if (ime_selection.y > 0) { |
| 1910 | // IME caret. |
| 1911 | const Vector<Vector2> sel = TS->shaped_text_get_selection(rid, get_caret_column(c) + ime_selection.x, get_caret_column(c) + ime_selection.x + ime_selection.y); |
| 1912 | for (int j = 0; j < sel.size(); j++) { |
| 1913 | Rect2 rect = Rect2(sel[j].x + char_margin, ofs_y, sel[j].y - sel[j].x, text_height); |
| 1914 | if (rect.position.x + rect.size.x <= xmargin_beg || rect.position.x > xmargin_end) { |
| 1915 | continue; |
| 1916 | } |
| 1917 | if (rect.position.x < xmargin_beg) { |
| 1918 | rect.size.x -= (xmargin_beg - rect.position.x); |
| 1919 | rect.position.x = xmargin_beg; |
| 1920 | } else if (rect.position.x + rect.size.x > xmargin_end) { |
| 1921 | rect.size.x = xmargin_end - rect.position.x; |
| 1922 | } |
| 1923 | rect.size.y = caret_width * 3; |
| 1924 | RS::get_singleton()->canvas_item_add_rect(text_ci, rect, theme_cache.caret_color); |
| 1925 | carets.write[c].draw_pos.x = rect.position.x; |
| 1926 | } |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | if (!draw_placeholder) { |
| 1934 | line_drawing_cache[line] = cache_entry; |
| 1935 | } |
| 1936 | } |
| 1937 | |
| 1938 | if (has_focus()) { |
| 1939 | _update_ime_window_position(); |
| 1940 | } |
| 1941 | } break; |
| 1942 | |
| 1943 | case NOTIFICATION_FOCUS_ENTER: { |
| 1944 | if (caret_blink_enabled) { |
| 1945 | caret_blink_timer->start(); |
| 1946 | } else { |
| 1947 | draw_caret = true; |
| 1948 | } |
| 1949 | |
| 1950 | if (editable && virtual_keyboard_show_on_focus) { |
| 1951 | _show_virtual_keyboard(); |
| 1952 | } |
| 1953 | } break; |
| 1954 | |
| 1955 | case NOTIFICATION_FOCUS_EXIT: { |
| 1956 | if (caret_blink_enabled) { |
| 1957 | caret_blink_timer->stop(); |
| 1958 | } |
| 1959 | |
| 1960 | apply_ime(); |
| 1961 | |
| 1962 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) { |
| 1963 | DisplayServer::get_singleton()->virtual_keyboard_hide(); |
| 1964 | } |
| 1965 | |
| 1966 | if (deselect_on_focus_loss_enabled && !selection_drag_attempt) { |
| 1967 | deselect(); |
| 1968 | } |
| 1969 | } break; |
| 1970 | |
| 1971 | case MainLoop::NOTIFICATION_OS_IME_UPDATE: { |
| 1972 | if (has_focus()) { |
| 1973 | const String &new_ime_text = DisplayServer::get_singleton()->ime_get_text(); |
| 1974 | const Vector2i &new_ime_selection = DisplayServer::get_singleton()->ime_get_selection(); |
| 1975 | if (ime_text == new_ime_text && ime_selection == new_ime_selection) { |
| 1976 | break; |
| 1977 | } |
| 1978 | if (!window_has_focus && !new_ime_text.is_empty()) { |
| 1979 | break; |
| 1980 | } |
| 1981 | |
| 1982 | bool had_ime_text = has_ime_text(); |
| 1983 | ime_text = new_ime_text; |
| 1984 | ime_selection = new_ime_selection; |
| 1985 | |
| 1986 | if (!had_ime_text && has_ime_text()) { |
| 1987 | _cancel_drag_and_drop_text(); |
| 1988 | } |
| 1989 | |
| 1990 | if (has_ime_text() && has_selection()) { |
| 1991 | set_selection_mode(SELECTION_MODE_NONE); |
| 1992 | delete_selection(); |
| 1993 | } |
| 1994 | |
| 1995 | _update_ime_text(); |
| 1996 | adjust_viewport_to_caret(0); |
| 1997 | |
| 1998 | queue_accessibility_update(); |
| 1999 | queue_redraw(); |
| 2000 | } |
| 2001 | } break; |
| 2002 | |
| 2003 | case NOTIFICATION_DRAG_BEGIN: { |
| 2004 | set_selection_mode(SelectionMode::SELECTION_MODE_NONE); |
| 2005 | drag_action = true; |
| 2006 | dragging_minimap = false; |
| 2007 | dragging_selection = false; |
| 2008 | can_drag_minimap = false; |
| 2009 | click_select_held->stop(); |
| 2010 | } break; |
| 2011 | |
| 2012 | case NOTIFICATION_DRAG_END: { |
| 2013 | remove_drag_caret(); |
| 2014 | if (selection_drag_attempt && is_drag_successful()) { |
| 2015 | // Dropped elsewhere. |
| 2016 | if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 2017 | delete_selection(); |
| 2018 | } else if (deselect_on_focus_loss_enabled) { |
| 2019 | deselect(); |
| 2020 | } |
| 2021 | } |
| 2022 | selection_drag_attempt = false; |
| 2023 | drag_action = false; |
| 2024 | drag_caret_force_displayed = false; |
| 2025 | dragging_selection = false; |
| 2026 | } break; |
| 2027 | |
| 2028 | case NOTIFICATION_MOUSE_EXIT_SELF: { |
| 2029 | if (drag_caret_force_displayed) { |
| 2030 | drag_caret_force_displayed = false; |
| 2031 | queue_redraw(); |
| 2032 | } |
| 2033 | if (hovered_gutter != Vector2i(-1, -1)) { |
| 2034 | hovered_gutter = Vector2i(-1, -1); |
| 2035 | queue_redraw(); |
| 2036 | } |
| 2037 | } break; |
| 2038 | } |
| 2039 | } |
| 2040 | |
| 2041 | void TextEdit::unhandled_key_input(const Ref<InputEvent> &p_event) { |
| 2042 | Ref<InputEventKey> k = p_event; |
| 2043 | |
| 2044 | if (k.is_valid()) { |
| 2045 | if (!k->is_pressed()) { |
| 2046 | return; |
| 2047 | } |
| 2048 | // Handle Unicode (with modifiers active, process after shortcuts). |
| 2049 | if (has_focus() && editable && (k->get_unicode() >= 32)) { |
| 2050 | handle_unicode_input(k->get_unicode()); |
| 2051 | accept_event(); |
| 2052 | } |
| 2053 | } |
| 2054 | } |
| 2055 | |
| 2056 | bool TextEdit::alt_input(const Ref<InputEvent> &p_gui_input) { |
| 2057 | Ref<InputEventKey> k = p_gui_input; |
| 2058 | if (k.is_valid()) { |
| 2059 | // Start Unicode Alt input (hold). |
| 2060 | if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) { |
| 2061 | if (has_selection()) { |
| 2062 | delete_selection(); |
| 2063 | } |
| 2064 | alt_start = true; |
| 2065 | alt_code = 0; |
| 2066 | alt_mode = ALT_INPUT_UNICODE; |
| 2067 | ime_text = "u"; |
| 2068 | ime_selection = Vector2i(0, -1); |
| 2069 | _update_ime_text(); |
| 2070 | return true; |
| 2071 | } |
| 2072 | |
| 2073 | // Start Unicode input (press). |
| 2074 | if (k->is_action("ui_unicode_start", true) && !alt_start && !alt_start_no_hold) { |
| 2075 | if (has_selection()) { |
| 2076 | delete_selection(); |
| 2077 | } |
| 2078 | alt_start_no_hold = true; |
| 2079 | alt_code = 0; |
| 2080 | alt_mode = ALT_INPUT_UNICODE; |
| 2081 | ime_text = "u"; |
| 2082 | ime_selection = Vector2i(0, -1); |
| 2083 | _update_ime_text(); |
| 2084 | return true; |
| 2085 | } |
| 2086 | |
| 2087 | // Start OEM Alt input (hold). |
| 2088 | if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) { |
| 2089 | if (has_selection()) { |
| 2090 | delete_selection(); |
| 2091 | } |
| 2092 | alt_start = true; |
| 2093 | alt_code = (uint32_t)(k->get_keycode() - Key::KP_0); |
| 2094 | alt_mode = ALT_INPUT_OEM; |
| 2095 | ime_text = vformat("o%s", String::num_int64(alt_code, 10)); |
| 2096 | ime_selection = Vector2i(0, -1); |
| 2097 | _update_ime_text(); |
| 2098 | return true; |
| 2099 | } |
| 2100 | |
| 2101 | // Start Windows Alt input (hold). |
| 2102 | if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) { |
| 2103 | if (has_selection()) { |
| 2104 | delete_selection(); |
| 2105 | } |
| 2106 | alt_start = true; |
| 2107 | alt_mode = ALT_INPUT_WIN; |
| 2108 | alt_code = 0; |
| 2109 | ime_text = "w"; |
| 2110 | ime_selection = Vector2i(0, -1); |
| 2111 | _update_ime_text(); |
| 2112 | return true; |
| 2113 | } |
| 2114 | |
| 2115 | // Update Unicode input. |
| 2116 | if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) { |
| 2117 | if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) { |
| 2118 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2119 | alt_code = alt_code << 4; |
| 2120 | } else { |
| 2121 | alt_code = alt_code * 10; |
| 2122 | } |
| 2123 | alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0); |
| 2124 | } else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) { |
| 2125 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2126 | alt_code = alt_code << 4; |
| 2127 | } else { |
| 2128 | alt_code = alt_code * 10; |
| 2129 | } |
| 2130 | alt_code += (uint32_t)(k->get_keycode() - Key::KP_0); |
| 2131 | } else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) { |
| 2132 | alt_code = alt_code << 4; |
| 2133 | alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10; |
| 2134 | } else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) { |
| 2135 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2136 | alt_code = alt_code << 4; |
| 2137 | } else { |
| 2138 | alt_code = alt_code * 10; |
| 2139 | } |
| 2140 | alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0); |
| 2141 | } else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) { |
| 2142 | alt_code = alt_code << 4; |
| 2143 | alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10; |
| 2144 | } else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) { |
| 2145 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2146 | alt_code = alt_code << 4; |
| 2147 | } else { |
| 2148 | alt_code = alt_code * 10; |
| 2149 | } |
| 2150 | alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0); |
| 2151 | } |
| 2152 | if (k->get_keycode() == Key::BACKSPACE) { |
| 2153 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2154 | alt_code = alt_code >> 4; |
| 2155 | } else { |
| 2156 | alt_code = alt_code / 10; |
| 2157 | } |
| 2158 | } |
| 2159 | if (alt_code > 0x10ffff) { |
| 2160 | alt_code = 0x10ffff; |
| 2161 | } |
| 2162 | if (alt_code > 0) { |
| 2163 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2164 | ime_text = vformat("u%s", String::num_int64(alt_code, 16, true)); |
| 2165 | } else if (alt_mode == ALT_INPUT_OEM) { |
| 2166 | ime_text = vformat("o%s", String::num_int64(alt_code, 10)); |
| 2167 | } else if (alt_mode == ALT_INPUT_WIN) { |
| 2168 | ime_text = vformat("w%s", String::num_int64(alt_code, 10)); |
| 2169 | } |
| 2170 | } else { |
| 2171 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2172 | ime_text = "u"; |
| 2173 | } else if (alt_mode == ALT_INPUT_OEM) { |
| 2174 | ime_text = "o"; |
| 2175 | } else if (alt_mode == ALT_INPUT_WIN) { |
| 2176 | ime_text = "w"; |
| 2177 | } |
| 2178 | } |
| 2179 | ime_selection = Vector2i(0, -1); |
| 2180 | _update_ime_text(); |
| 2181 | return true; |
| 2182 | } |
| 2183 | |
| 2184 | // Submit Unicode input. |
| 2185 | if ((!k->is_pressed() && alt_start && k->get_keycode() == Key::ALT) || (alt_start_no_hold && (k->is_action("ui_text_submit", true) || k->is_action("ui_accept", true)))) { |
| 2186 | alt_start = false; |
| 2187 | alt_start_no_hold = false; |
| 2188 | if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 2189 | ime_text = String(); |
| 2190 | ime_selection = Vector2i(); |
| 2191 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 2192 | if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 2193 | handle_unicode_input(alt_code); |
| 2194 | } |
| 2195 | } else if (alt_mode == ALT_INPUT_OEM) { |
| 2196 | if (alt_code > 0x00 && alt_code <= 0xff) { |
| 2197 | handle_unicode_input(alt_code_oem437[alt_code]); |
| 2198 | } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 2199 | handle_unicode_input(alt_code); |
| 2200 | } |
| 2201 | } else if (alt_mode == ALT_INPUT_WIN) { |
| 2202 | if (alt_code > 0x00 && alt_code <= 0xff) { |
| 2203 | handle_unicode_input(alt_code_cp1252[alt_code]); |
| 2204 | } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 2205 | handle_unicode_input(alt_code); |
| 2206 | } |
| 2207 | } |
| 2208 | alt_mode = ALT_INPUT_NONE; |
| 2209 | } else { |
| 2210 | ime_text = String(); |
| 2211 | ime_selection = Vector2i(); |
| 2212 | } |
| 2213 | _update_ime_text(); |
| 2214 | return true; |
| 2215 | } |
| 2216 | |
| 2217 | // Cancel Unicode input. |
| 2218 | if (alt_start_no_hold && k->is_action("ui_cancel", true)) { |
| 2219 | alt_start = false; |
| 2220 | alt_start_no_hold = false; |
| 2221 | alt_mode = ALT_INPUT_NONE; |
| 2222 | ime_text = String(); |
| 2223 | ime_selection = Vector2i(); |
| 2224 | _update_ime_text(); |
| 2225 | return true; |
| 2226 | } |
| 2227 | } |
| 2228 | return false; |
| 2229 | } |
| 2230 | |
| 2231 | void TextEdit::gui_input(const Ref<InputEvent> &p_gui_input) { |
| 2232 | ERR_FAIL_COND(p_gui_input.is_null()); |
| 2233 | |
| 2234 | double prev_v_scroll = v_scroll->get_value(); |
| 2235 | double prev_h_scroll = h_scroll->get_value(); |
| 2236 | |
| 2237 | Ref<InputEventMouseButton> mb = p_gui_input; |
| 2238 | |
| 2239 | if (mb.is_valid()) { |
| 2240 | Vector2i mpos = mb->get_position(); |
| 2241 | if (is_layout_rtl()) { |
| 2242 | mpos.x = get_size().x - mpos.x; |
| 2243 | } |
| 2244 | |
| 2245 | if (mb->is_pressed()) { |
| 2246 | if (mb->get_button_index() == MouseButton::WHEEL_UP && !mb->is_command_or_control_pressed()) { |
| 2247 | if (mb->is_shift_pressed()) { |
| 2248 | h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor())); |
| 2249 | queue_accessibility_update(); |
| 2250 | } else if (mb->is_alt_pressed()) { |
| 2251 | // Scroll 5 times as fast as normal (like in Visual Studio Code). |
| 2252 | _scroll_up(15 * mb->get_factor(), true); |
| 2253 | } else if (v_scroll->is_visible()) { |
| 2254 | // Scroll 3 lines. |
| 2255 | _scroll_up(3 * mb->get_factor(), true); |
| 2256 | } |
| 2257 | } |
| 2258 | if (mb->get_button_index() == MouseButton::WHEEL_DOWN && !mb->is_command_or_control_pressed()) { |
| 2259 | if (mb->is_shift_pressed()) { |
| 2260 | h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor())); |
| 2261 | queue_accessibility_update(); |
| 2262 | } else if (mb->is_alt_pressed()) { |
| 2263 | // Scroll 5 times as fast as normal (like in Visual Studio Code). |
| 2264 | _scroll_down(15 * mb->get_factor(), true); |
| 2265 | } else if (v_scroll->is_visible()) { |
| 2266 | // Scroll 3 lines. |
| 2267 | _scroll_down(3 * mb->get_factor(), true); |
| 2268 | } |
| 2269 | } |
| 2270 | if (mb->get_button_index() == MouseButton::WHEEL_LEFT) { |
| 2271 | h_scroll->set_value(h_scroll->get_value() - (100 * mb->get_factor())); |
| 2272 | queue_accessibility_update(); |
| 2273 | } |
| 2274 | if (mb->get_button_index() == MouseButton::WHEEL_RIGHT) { |
| 2275 | h_scroll->set_value(h_scroll->get_value() + (100 * mb->get_factor())); |
| 2276 | queue_accessibility_update(); |
| 2277 | } |
| 2278 | |
| 2279 | if (mb->get_button_index() == MouseButton::LEFT) { |
| 2280 | _reset_caret_blink_timer(); |
| 2281 | |
| 2282 | apply_ime(); |
| 2283 | |
| 2284 | Point2i pos = get_line_column_at_pos(mpos); |
| 2285 | int line = pos.y; |
| 2286 | int col = pos.x; |
| 2287 | |
| 2288 | // Gutters. |
| 2289 | Vector2i current_hovered_gutter = _get_hovered_gutter(mpos); |
| 2290 | if (current_hovered_gutter != hovered_gutter) { |
| 2291 | hovered_gutter = current_hovered_gutter; |
| 2292 | queue_redraw(); |
| 2293 | } |
| 2294 | if (hovered_gutter != Vector2i(-1, -1)) { |
| 2295 | emit_signal(SNAME("gutter_clicked"), hovered_gutter.y, hovered_gutter.x); |
| 2296 | return; |
| 2297 | } |
| 2298 | int left_margin = Math::ceil(_get_current_stylebox()->get_margin(SIDE_LEFT)); |
| 2299 | if (mpos.x < left_margin + gutters_width + gutter_padding) { |
| 2300 | return; |
| 2301 | } |
| 2302 | |
| 2303 | // Minimap. |
| 2304 | if (draw_minimap) { |
| 2305 | _update_minimap_click(); |
| 2306 | if (dragging_minimap) { |
| 2307 | return; |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | // Update caret. |
| 2312 | |
| 2313 | int caret = carets.size() - 1; |
| 2314 | int prev_col = get_caret_column(caret); |
| 2315 | int prev_line = get_caret_line(caret); |
| 2316 | |
| 2317 | int mouse_over_selection_caret = get_selection_at_line_column(line, col, true); |
| 2318 | |
| 2319 | const int triple_click_timeout = 600; |
| 2320 | const int triple_click_tolerance = 5; |
| 2321 | bool is_triple_click = (!mb->is_double_click() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < triple_click_timeout && mb->get_position().distance_to(last_dblclk_pos) < triple_click_tolerance); |
| 2322 | |
| 2323 | if (!mb->is_double_click() && !is_triple_click) { |
| 2324 | if (mb->is_alt_pressed()) { |
| 2325 | prev_line = line; |
| 2326 | prev_col = col; |
| 2327 | |
| 2328 | // Remove caret at clicked location. |
| 2329 | if (get_caret_count() > 1) { |
| 2330 | // Deselect if clicked on caret or its selection. |
| 2331 | int clicked_caret = get_selection_at_line_column(line, col, true, false); |
| 2332 | if (clicked_caret != -1) { |
| 2333 | remove_caret(clicked_caret); |
| 2334 | last_dblclk = 0; |
| 2335 | return; |
| 2336 | } |
| 2337 | } |
| 2338 | |
| 2339 | if (mouse_over_selection_caret >= 0) { |
| 2340 | // Did not remove selection under mouse, don't add a new caret. |
| 2341 | return; |
| 2342 | } |
| 2343 | |
| 2344 | // Create new caret at clicked location. |
| 2345 | caret = add_caret(line, col); |
| 2346 | if (caret == -1) { |
| 2347 | return; |
| 2348 | } |
| 2349 | |
| 2350 | queue_accessibility_update(); |
| 2351 | |
| 2352 | last_dblclk = 0; |
| 2353 | } else if (!mb->is_shift_pressed()) { |
| 2354 | if (drag_and_drop_selection_enabled && mouse_over_selection_caret >= 0) { |
| 2355 | // Try to drag and drop. |
| 2356 | set_selection_mode(SelectionMode::SELECTION_MODE_NONE); |
| 2357 | selection_drag_attempt = true; |
| 2358 | drag_and_drop_origin_caret_index = mouse_over_selection_caret; |
| 2359 | last_dblclk = 0; |
| 2360 | // Don't update caret until we know if it is not drag and drop. |
| 2361 | return; |
| 2362 | } else { |
| 2363 | // A regular click clears all other carets. |
| 2364 | caret = 0; |
| 2365 | remove_secondary_carets(); |
| 2366 | deselect(); |
| 2367 | } |
| 2368 | } |
| 2369 | |
| 2370 | _push_current_op(); |
| 2371 | set_caret_line(line, false, true, -1, caret); |
| 2372 | set_caret_column(col, false, caret); |
| 2373 | selection_drag_attempt = false; |
| 2374 | bool caret_moved = get_caret_column(caret) != prev_col || get_caret_line(caret) != prev_line; |
| 2375 | |
| 2376 | if (selecting_enabled && mb->is_shift_pressed() && !has_selection(caret) && caret_moved) { |
| 2377 | // Select from the previous caret position. |
| 2378 | select(prev_line, prev_col, line, col, caret); |
| 2379 | } |
| 2380 | |
| 2381 | // Start regular select mode. |
| 2382 | set_selection_mode(SelectionMode::SELECTION_MODE_POINTER); |
| 2383 | _update_selection_mode_pointer(true); |
| 2384 | } else if (is_triple_click) { |
| 2385 | // Start triple-click select line mode. |
| 2386 | set_selection_mode(SelectionMode::SELECTION_MODE_LINE); |
| 2387 | _update_selection_mode_line(true); |
| 2388 | last_dblclk = 0; |
| 2389 | } else if (mb->is_double_click()) { |
| 2390 | // Start double-click select word mode. |
| 2391 | set_selection_mode(SelectionMode::SELECTION_MODE_WORD); |
| 2392 | _update_selection_mode_word(true); |
| 2393 | last_dblclk = OS::get_singleton()->get_ticks_msec(); |
| 2394 | last_dblclk_pos = mb->get_position(); |
| 2395 | } |
| 2396 | |
| 2397 | // Click inline objects. |
| 2398 | if (inline_object_click_handler.is_valid()) { |
| 2399 | int xmargin_beg = left_margin + gutters_width + gutter_padding; |
| 2400 | int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x); |
| 2401 | const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl()); |
| 2402 | |
| 2403 | Ref<TextParagraph> ldata = text.get_line_data(line); |
| 2404 | for (const Variant &inline_key : ldata->get_line_objects(wrap_i)) { |
| 2405 | if (!is_inline_info_valid(inline_key)) { |
| 2406 | continue; |
| 2407 | } |
| 2408 | Dictionary info = inline_key.duplicate(); |
| 2409 | info["line"] = line; |
| 2410 | Rect2 obj_rect = ldata->get_line_object_rect(wrap_i, inline_key); |
| 2411 | obj_rect.position.x += xmargin_beg + wrap_indent - first_visible_col; |
| 2412 | |
| 2413 | if (mpos.x > obj_rect.position.x && mpos.x < obj_rect.get_end().x) { |
| 2414 | Rect2 col_rect = get_rect_at_line_column(line, col); |
| 2415 | col_rect.position += get_screen_position() + Vector2(col_rect.size.x, 0); |
| 2416 | col_rect.size = obj_rect.size; |
| 2417 | set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_NONE); |
| 2418 | inline_object_click_handler.call(info, col_rect); |
| 2419 | break; |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | |
| 2424 | queue_accessibility_update(); |
| 2425 | queue_redraw(); |
| 2426 | } |
| 2427 | |
| 2428 | if (is_middle_mouse_paste_enabled() && mb->get_button_index() == MouseButton::MIDDLE && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 2429 | apply_ime(); |
| 2430 | paste_primary_clipboard(); |
| 2431 | } |
| 2432 | |
| 2433 | if (mb->get_button_index() == MouseButton::RIGHT && (context_menu_enabled || is_move_caret_on_right_click_enabled())) { |
| 2434 | _push_current_op(); |
| 2435 | _reset_caret_blink_timer(); |
| 2436 | apply_ime(); |
| 2437 | _cancel_drag_and_drop_text(); |
| 2438 | set_selection_mode(SELECTION_MODE_NONE); |
| 2439 | |
| 2440 | Point2i pos = get_line_column_at_pos(mpos); |
| 2441 | int mouse_line = pos.y; |
| 2442 | int mouse_column = pos.x; |
| 2443 | |
| 2444 | if (is_move_caret_on_right_click_enabled()) { |
| 2445 | bool selection_clicked = get_selection_at_line_column(mouse_line, mouse_column, true) >= 0; |
| 2446 | if (!selection_clicked) { |
| 2447 | deselect(); |
| 2448 | remove_secondary_carets(); |
| 2449 | set_caret_line(mouse_line, false, false, -1); |
| 2450 | set_caret_column(mouse_column); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | if (context_menu_enabled) { |
| 2455 | _update_context_menu(); |
| 2456 | menu->set_position(get_screen_transform().xform(mpos)); |
| 2457 | menu->reset_size(); |
| 2458 | menu->popup(); |
| 2459 | grab_focus(); |
| 2460 | } |
| 2461 | } |
| 2462 | } else { |
| 2463 | if (has_ime_text()) { |
| 2464 | // Ignore mouse up in IME input mode. |
| 2465 | return; |
| 2466 | } |
| 2467 | |
| 2468 | if (mb->get_button_index() == MouseButton::LEFT) { |
| 2469 | if (!drag_action && selection_drag_attempt && is_mouse_over_selection()) { |
| 2470 | // This is not a drag and drop attempt, update the caret. |
| 2471 | selection_drag_attempt = false; |
| 2472 | remove_secondary_carets(); |
| 2473 | deselect(); |
| 2474 | |
| 2475 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 2476 | set_caret_line(pos.y, false, true, -1, 0); |
| 2477 | set_caret_column(pos.x, true, 0); |
| 2478 | } |
| 2479 | dragging_minimap = false; |
| 2480 | dragging_selection = false; |
| 2481 | can_drag_minimap = false; |
| 2482 | set_selection_mode(SelectionMode::SELECTION_MODE_NONE); |
| 2483 | click_select_held->stop(); |
| 2484 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 2485 | DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text()); |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | if (editable) { |
| 2490 | _show_virtual_keyboard(); |
| 2491 | } |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | const Ref<InputEventPanGesture> pan_gesture = p_gui_input; |
| 2496 | if (pan_gesture.is_valid()) { |
| 2497 | const real_t delta = pan_gesture->get_delta().y; |
| 2498 | if (delta < 0) { |
| 2499 | _scroll_up(-delta, false); |
| 2500 | } else { |
| 2501 | _scroll_down(delta, false); |
| 2502 | } |
| 2503 | h_scroll->set_value(h_scroll->get_value() + pan_gesture->get_delta().x * 100); |
| 2504 | if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) { |
| 2505 | accept_event(); // Accept event if scroll changed. |
| 2506 | } |
| 2507 | queue_accessibility_update(); |
| 2508 | |
| 2509 | return; |
| 2510 | } |
| 2511 | |
| 2512 | Ref<InputEventMouseMotion> mm = p_gui_input; |
| 2513 | |
| 2514 | if (mm.is_valid()) { |
| 2515 | Vector2i mpos = mm->get_position(); |
| 2516 | if (is_layout_rtl()) { |
| 2517 | mpos.x = get_size().x - mpos.x; |
| 2518 | } |
| 2519 | |
| 2520 | if (mm->get_button_mask().has_flag(MouseButtonMask::LEFT) && get_viewport()->gui_get_drag_data() == Variant()) { |
| 2521 | // Update if not in drag and drop. |
| 2522 | _reset_caret_blink_timer(); |
| 2523 | |
| 2524 | if (draw_minimap && !dragging_selection) { |
| 2525 | _update_minimap_drag(); |
| 2526 | } |
| 2527 | |
| 2528 | if (!dragging_minimap && !has_ime_text()) { |
| 2529 | switch (selecting_mode) { |
| 2530 | case SelectionMode::SELECTION_MODE_POINTER: { |
| 2531 | _update_selection_mode_pointer(); |
| 2532 | } break; |
| 2533 | case SelectionMode::SELECTION_MODE_WORD: { |
| 2534 | _update_selection_mode_word(); |
| 2535 | } break; |
| 2536 | case SelectionMode::SELECTION_MODE_LINE: { |
| 2537 | _update_selection_mode_line(); |
| 2538 | } break; |
| 2539 | default: { |
| 2540 | break; |
| 2541 | } |
| 2542 | } |
| 2543 | } |
| 2544 | } |
| 2545 | |
| 2546 | // Update hovered gutter. |
| 2547 | Vector2i current_hovered_gutter = _get_hovered_gutter(mpos); |
| 2548 | if (current_hovered_gutter != hovered_gutter) { |
| 2549 | hovered_gutter = current_hovered_gutter; |
| 2550 | queue_redraw(); |
| 2551 | } |
| 2552 | |
| 2553 | if (drag_action && can_drop_data(mpos, get_viewport()->gui_get_drag_data())) { |
| 2554 | apply_ime(); |
| 2555 | // Update drag and drop caret. |
| 2556 | drag_caret_force_displayed = true; |
| 2557 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 2558 | |
| 2559 | if (drag_caret_index == -1) { |
| 2560 | // Force create a new caret for drag and drop. |
| 2561 | carets.push_back(Caret()); |
| 2562 | drag_caret_index = carets.size() - 1; |
| 2563 | } |
| 2564 | |
| 2565 | drag_caret_force_displayed = true; |
| 2566 | set_caret_line(pos.y, false, true, -1, drag_caret_index); |
| 2567 | set_caret_column(pos.x, true, drag_caret_index); |
| 2568 | dragging_selection = true; |
| 2569 | } |
| 2570 | } |
| 2571 | |
| 2572 | if (draw_minimap && !dragging_selection) { |
| 2573 | _update_minimap_hover(); |
| 2574 | } |
| 2575 | |
| 2576 | if (v_scroll->get_value() != prev_v_scroll || h_scroll->get_value() != prev_h_scroll) { |
| 2577 | accept_event(); // Accept event if scroll changed. |
| 2578 | } |
| 2579 | |
| 2580 | Ref<InputEventKey> k = p_gui_input; |
| 2581 | |
| 2582 | if (k.is_valid()) { |
| 2583 | if (alt_input(p_gui_input)) { |
| 2584 | accept_event(); |
| 2585 | return; |
| 2586 | } |
| 2587 | if (!k->is_pressed()) { |
| 2588 | return; |
| 2589 | } |
| 2590 | |
| 2591 | // If a modifier has been pressed, and nothing else, return. |
| 2592 | if (k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::META || k->get_keycode() == Key::CAPSLOCK) { |
| 2593 | return; |
| 2594 | } |
| 2595 | |
| 2596 | _cancel_drag_and_drop_text(); |
| 2597 | |
| 2598 | _reset_caret_blink_timer(); |
| 2599 | |
| 2600 | // Allow unicode handling if: |
| 2601 | // * No modifiers are pressed (except Shift and CapsLock) |
| 2602 | bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed()); |
| 2603 | |
| 2604 | // Check and handle all built-in shortcuts. |
| 2605 | |
| 2606 | // NEWLINES. |
| 2607 | if (k->is_action("ui_text_newline_above", true)) { |
| 2608 | _new_line(false, true); |
| 2609 | accept_event(); |
| 2610 | return; |
| 2611 | } |
| 2612 | if (k->is_action("ui_text_newline_blank", true)) { |
| 2613 | _new_line(false); |
| 2614 | accept_event(); |
| 2615 | return; |
| 2616 | } |
| 2617 | if (k->is_action("ui_text_newline", true)) { |
| 2618 | _new_line(); |
| 2619 | accept_event(); |
| 2620 | return; |
| 2621 | } |
| 2622 | |
| 2623 | // BACKSPACE AND DELETE. |
| 2624 | if (k->is_action("ui_text_backspace_all_to_left", true)) { |
| 2625 | _do_backspace(false, true); |
| 2626 | accept_event(); |
| 2627 | return; |
| 2628 | } |
| 2629 | if (k->is_action("ui_text_backspace_word", true)) { |
| 2630 | _do_backspace(true); |
| 2631 | accept_event(); |
| 2632 | return; |
| 2633 | } |
| 2634 | if (k->is_action("ui_text_backspace", true)) { |
| 2635 | _do_backspace(); |
| 2636 | accept_event(); |
| 2637 | return; |
| 2638 | } |
| 2639 | if (k->is_action("ui_text_delete_all_to_right", true)) { |
| 2640 | _delete(false, true); |
| 2641 | accept_event(); |
| 2642 | return; |
| 2643 | } |
| 2644 | if (k->is_action("ui_text_delete_word", true)) { |
| 2645 | _delete(true); |
| 2646 | accept_event(); |
| 2647 | return; |
| 2648 | } |
| 2649 | if (k->is_action("ui_text_delete", true)) { |
| 2650 | _delete(); |
| 2651 | accept_event(); |
| 2652 | return; |
| 2653 | } |
| 2654 | |
| 2655 | // SCROLLING. |
| 2656 | if (k->is_action("ui_text_scroll_up", true)) { |
| 2657 | _scroll_lines_up(); |
| 2658 | accept_event(); |
| 2659 | return; |
| 2660 | } |
| 2661 | if (k->is_action("ui_text_scroll_down", true)) { |
| 2662 | _scroll_lines_down(); |
| 2663 | accept_event(); |
| 2664 | return; |
| 2665 | } |
| 2666 | |
| 2667 | if (is_shortcut_keys_enabled()) { |
| 2668 | // SELECT ALL, SELECT WORD UNDER CARET, ADD SELECTION FOR NEXT OCCURRENCE, SKIP SELECTION FOR NEXT OCCURRENCE, |
| 2669 | // CLEAR CARETS AND SELECTIONS, CUT, COPY, PASTE. |
| 2670 | if (k->is_action("ui_text_select_all", true)) { |
| 2671 | select_all(); |
| 2672 | accept_event(); |
| 2673 | return; |
| 2674 | } |
| 2675 | if (k->is_action("ui_text_select_word_under_caret", true)) { |
| 2676 | select_word_under_caret(); |
| 2677 | accept_event(); |
| 2678 | return; |
| 2679 | } |
| 2680 | if (k->is_action("ui_text_add_selection_for_next_occurrence", true)) { |
| 2681 | add_selection_for_next_occurrence(); |
| 2682 | accept_event(); |
| 2683 | return; |
| 2684 | } |
| 2685 | if (k->is_action("ui_text_skip_selection_for_next_occurrence", true)) { |
| 2686 | skip_selection_for_next_occurrence(); |
| 2687 | accept_event(); |
| 2688 | return; |
| 2689 | } |
| 2690 | if (k->is_action("ui_text_clear_carets_and_selection", true)) { |
| 2691 | // Since the default shortcut is ESC, accepts the event only if it's actually performed. |
| 2692 | if (_clear_carets_and_selection()) { |
| 2693 | accept_event(); |
| 2694 | return; |
| 2695 | } |
| 2696 | } |
| 2697 | if (k->is_action("ui_cut", true)) { |
| 2698 | cut(); |
| 2699 | accept_event(); |
| 2700 | return; |
| 2701 | } |
| 2702 | if (k->is_action("ui_copy", true)) { |
| 2703 | copy(); |
| 2704 | accept_event(); |
| 2705 | return; |
| 2706 | } |
| 2707 | if (k->is_action("ui_paste", true)) { |
| 2708 | paste(); |
| 2709 | accept_event(); |
| 2710 | return; |
| 2711 | } |
| 2712 | |
| 2713 | // UNDO/REDO. |
| 2714 | if (k->is_action("ui_undo", true)) { |
| 2715 | undo(); |
| 2716 | accept_event(); |
| 2717 | return; |
| 2718 | } |
| 2719 | if (k->is_action("ui_redo", true)) { |
| 2720 | redo(); |
| 2721 | accept_event(); |
| 2722 | return; |
| 2723 | } |
| 2724 | |
| 2725 | if (k->is_action("ui_text_caret_add_below", true)) { |
| 2726 | add_caret_at_carets(true); |
| 2727 | accept_event(); |
| 2728 | return; |
| 2729 | } |
| 2730 | if (k->is_action("ui_text_caret_add_above", true)) { |
| 2731 | add_caret_at_carets(false); |
| 2732 | accept_event(); |
| 2733 | return; |
| 2734 | } |
| 2735 | } |
| 2736 | |
| 2737 | // MISC. |
| 2738 | if (k->is_action("ui_menu", true)) { |
| 2739 | _push_current_op(); |
| 2740 | if (context_menu_enabled) { |
| 2741 | _update_context_menu(); |
| 2742 | adjust_viewport_to_caret(); |
| 2743 | menu->set_position(get_screen_transform().xform(get_caret_draw_pos())); |
| 2744 | menu->reset_size(); |
| 2745 | menu->popup(); |
| 2746 | menu->grab_focus(); |
| 2747 | } |
| 2748 | accept_event(); |
| 2749 | return; |
| 2750 | } |
| 2751 | if (k->is_action("ui_text_toggle_insert_mode", true)) { |
| 2752 | set_overtype_mode_enabled(!overtype_mode); |
| 2753 | accept_event(); |
| 2754 | return; |
| 2755 | } |
| 2756 | if (k->is_action("ui_swap_input_direction", true)) { |
| 2757 | _swap_current_input_direction(); |
| 2758 | accept_event(); |
| 2759 | return; |
| 2760 | } |
| 2761 | |
| 2762 | // CARET MOVEMENT |
| 2763 | |
| 2764 | k = k->duplicate(); |
| 2765 | bool shift_pressed = k->is_shift_pressed(); |
| 2766 | // Remove shift or else actions will not match. Use above variable for selection. |
| 2767 | k->set_shift_pressed(false); |
| 2768 | |
| 2769 | // CARET MOVEMENT - LEFT, RIGHT. |
| 2770 | if (k->is_action("ui_text_caret_word_left", true)) { |
| 2771 | _move_caret_left(shift_pressed, true); |
| 2772 | accept_event(); |
| 2773 | return; |
| 2774 | } |
| 2775 | if (k->is_action("ui_text_caret_left", true)) { |
| 2776 | _move_caret_left(shift_pressed, false); |
| 2777 | accept_event(); |
| 2778 | return; |
| 2779 | } |
| 2780 | if (k->is_action("ui_text_caret_word_right", true)) { |
| 2781 | _move_caret_right(shift_pressed, true); |
| 2782 | accept_event(); |
| 2783 | return; |
| 2784 | } |
| 2785 | if (k->is_action("ui_text_caret_right", true)) { |
| 2786 | _move_caret_right(shift_pressed, false); |
| 2787 | accept_event(); |
| 2788 | return; |
| 2789 | } |
| 2790 | |
| 2791 | // CARET MOVEMENT - UP, DOWN. |
| 2792 | if (k->is_action("ui_text_caret_up", true)) { |
| 2793 | _move_caret_up(shift_pressed); |
| 2794 | accept_event(); |
| 2795 | return; |
| 2796 | } |
| 2797 | if (k->is_action("ui_text_caret_down", true)) { |
| 2798 | _move_caret_down(shift_pressed); |
| 2799 | accept_event(); |
| 2800 | return; |
| 2801 | } |
| 2802 | |
| 2803 | // CARET MOVEMENT - DOCUMENT START/END. |
| 2804 | if (k->is_action("ui_text_caret_document_start", true)) { // && shift_pressed) { |
| 2805 | _move_caret_document_start(shift_pressed); |
| 2806 | accept_event(); |
| 2807 | return; |
| 2808 | } |
| 2809 | if (k->is_action("ui_text_caret_document_end", true)) { // && shift_pressed) { |
| 2810 | _move_caret_document_end(shift_pressed); |
| 2811 | accept_event(); |
| 2812 | return; |
| 2813 | } |
| 2814 | |
| 2815 | // CARET MOVEMENT - LINE START/END. |
| 2816 | if (k->is_action("ui_text_caret_line_start", true)) { |
| 2817 | _move_caret_to_line_start(shift_pressed); |
| 2818 | accept_event(); |
| 2819 | return; |
| 2820 | } |
| 2821 | if (k->is_action("ui_text_caret_line_end", true)) { |
| 2822 | _move_caret_to_line_end(shift_pressed); |
| 2823 | accept_event(); |
| 2824 | return; |
| 2825 | } |
| 2826 | |
| 2827 | // CARET MOVEMENT - PAGE UP/DOWN. |
| 2828 | if (k->is_action("ui_text_caret_page_up", true)) { |
| 2829 | _move_caret_page_up(shift_pressed); |
| 2830 | accept_event(); |
| 2831 | return; |
| 2832 | } |
| 2833 | if (k->is_action("ui_text_caret_page_down", true)) { |
| 2834 | _move_caret_page_down(shift_pressed); |
| 2835 | accept_event(); |
| 2836 | return; |
| 2837 | } |
| 2838 | |
| 2839 | // Toggle Tab mode. |
| 2840 | if (k->is_action("ui_focus_mode", true)) { |
| 2841 | tab_input_mode = !tab_input_mode; |
| 2842 | accept_event(); |
| 2843 | return; |
| 2844 | } |
| 2845 | |
| 2846 | // Handle tab as it has no set unicode value. |
| 2847 | if (tab_input_mode && k->is_action("ui_text_indent", true)) { |
| 2848 | if (editable) { |
| 2849 | insert_text_at_caret("\t"); |
| 2850 | } |
| 2851 | accept_event(); |
| 2852 | return; |
| 2853 | } |
| 2854 | |
| 2855 | // Handle Unicode (if no modifiers active). |
| 2856 | if (allow_unicode_handling && editable && k->get_unicode() >= 32) { |
| 2857 | handle_unicode_input(k->get_unicode()); |
| 2858 | accept_event(); |
| 2859 | return; |
| 2860 | } |
| 2861 | } |
| 2862 | } |
| 2863 | |
| 2864 | /* Input actions. */ |
| 2865 | void TextEdit::_swap_current_input_direction() { |
| 2866 | if (input_direction == TEXT_DIRECTION_LTR) { |
| 2867 | input_direction = TEXT_DIRECTION_RTL; |
| 2868 | } else { |
| 2869 | input_direction = TEXT_DIRECTION_LTR; |
| 2870 | } |
| 2871 | for (int i = 0; i < carets.size(); i++) { |
| 2872 | set_caret_column(get_caret_column(i), i == 0, i); |
| 2873 | } |
| 2874 | queue_redraw(); |
| 2875 | } |
| 2876 | |
| 2877 | void TextEdit::_new_line(bool p_split_current_line, bool p_above) { |
| 2878 | if (!editable) { |
| 2879 | return; |
| 2880 | } |
| 2881 | |
| 2882 | begin_complex_operation(); |
| 2883 | begin_multicaret_edit(); |
| 2884 | |
| 2885 | for (int i = 0; i < get_caret_count(); i++) { |
| 2886 | if (multicaret_edit_ignore_caret(i)) { |
| 2887 | continue; |
| 2888 | } |
| 2889 | if (p_split_current_line) { |
| 2890 | insert_text_at_caret("\n", i); |
| 2891 | } else { |
| 2892 | int line = get_caret_line(i); |
| 2893 | insert_text("\n", line, p_above ? 0 : text[line].length(), p_above, p_above); |
| 2894 | deselect(i); |
| 2895 | set_caret_line(p_above ? line : line + 1, false, true, -1, i); |
| 2896 | set_caret_column(0, i == 0, i); |
| 2897 | } |
| 2898 | } |
| 2899 | |
| 2900 | end_multicaret_edit(); |
| 2901 | end_complex_operation(); |
| 2902 | } |
| 2903 | |
| 2904 | void TextEdit::_move_caret_left(bool p_select, bool p_move_by_word) { |
| 2905 | _push_current_op(); |
| 2906 | for (int i = 0; i < get_caret_count(); i++) { |
| 2907 | // Handle selection. |
| 2908 | if (p_select) { |
| 2909 | _pre_shift_selection(i); |
| 2910 | } else if (has_selection(i) && !p_move_by_word) { |
| 2911 | // If a selection is active, move caret to start of selection. |
| 2912 | set_caret_line(get_selection_from_line(i), false, true, -1, i); |
| 2913 | set_caret_column(get_selection_from_column(i), i == 0, i); |
| 2914 | deselect(i); |
| 2915 | continue; |
| 2916 | } else { |
| 2917 | deselect(i); |
| 2918 | } |
| 2919 | |
| 2920 | if (get_caret_column(i) == 0) { |
| 2921 | if (get_caret_line(i) == 0) { |
| 2922 | continue; |
| 2923 | } |
| 2924 | // If the caret is at the start of the line, and not on the first line, move it up to the end of the previous line. |
| 2925 | int new_caret_line = get_caret_line(i) - get_next_visible_line_offset_from(get_caret_line(i) - 1, -1); |
| 2926 | set_caret_line(new_caret_line, false, true, -1, i); |
| 2927 | set_caret_column(text[get_caret_line(i)].length(), i == 0, i); |
| 2928 | } else if (p_move_by_word) { |
| 2929 | int caret_column = get_caret_column(i); |
| 2930 | const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid()); |
| 2931 | if (words.is_empty() || caret_column <= words[0]) { |
| 2932 | // Move to the start when there are no more words. |
| 2933 | caret_column = 0; |
| 2934 | } else { |
| 2935 | for (int j = words.size() - 2; j >= 0; j = j - 2) { |
| 2936 | if (words[j] < caret_column) { |
| 2937 | caret_column = words[j]; |
| 2938 | break; |
| 2939 | } |
| 2940 | } |
| 2941 | } |
| 2942 | set_caret_column(caret_column, i == 0, i); |
| 2943 | } else { |
| 2944 | if (caret_mid_grapheme_enabled) { |
| 2945 | set_caret_column(get_caret_column(i) - 1, i == 0, i); |
| 2946 | } else { |
| 2947 | set_caret_column(TS->shaped_text_prev_character_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i); |
| 2948 | } |
| 2949 | } |
| 2950 | } |
| 2951 | merge_overlapping_carets(); |
| 2952 | } |
| 2953 | |
| 2954 | void TextEdit::_move_caret_right(bool p_select, bool p_move_by_word) { |
| 2955 | _push_current_op(); |
| 2956 | for (int i = 0; i < get_caret_count(); i++) { |
| 2957 | // Handle selection. |
| 2958 | if (p_select) { |
| 2959 | _pre_shift_selection(i); |
| 2960 | } else if (has_selection(i) && !p_move_by_word) { |
| 2961 | // If a selection is active, move caret to end of selection. |
| 2962 | set_caret_line(get_selection_to_line(i), false, true, -1, i); |
| 2963 | set_caret_column(get_selection_to_column(i), i == 0, i); |
| 2964 | deselect(i); |
| 2965 | continue; |
| 2966 | } else { |
| 2967 | deselect(i); |
| 2968 | } |
| 2969 | |
| 2970 | if (get_caret_column(i) == text[get_caret_line(i)].length()) { |
| 2971 | if (get_caret_line(i) >= text.size() - 1 || get_caret_line(i) == get_last_unhidden_line()) { |
| 2972 | continue; |
| 2973 | } |
| 2974 | // If the caret is at the end of the line, and not on the last line, move it down to the beginning of the next line. |
| 2975 | int new_caret_line = get_caret_line(i) + get_next_visible_line_offset_from(get_caret_line(i) + 1, 1); |
| 2976 | set_caret_line(new_caret_line, false, true, -1, i); |
| 2977 | set_caret_column(0, i == 0, i); |
| 2978 | } else if (p_move_by_word) { |
| 2979 | int caret_column = get_caret_column(i); |
| 2980 | const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(i))->get_rid()); |
| 2981 | if (words.is_empty() || caret_column >= words[words.size() - 1]) { |
| 2982 | // Move to the end when there are no more words. |
| 2983 | caret_column = text[get_caret_line(i)].length(); |
| 2984 | } else { |
| 2985 | for (int j = 1; j < words.size(); j = j + 2) { |
| 2986 | if (words[j] > caret_column) { |
| 2987 | caret_column = words[j]; |
| 2988 | break; |
| 2989 | } |
| 2990 | } |
| 2991 | } |
| 2992 | set_caret_column(caret_column, i == 0, i); |
| 2993 | } else { |
| 2994 | if (caret_mid_grapheme_enabled) { |
| 2995 | set_caret_column(get_caret_column(i) + 1, i == 0, i); |
| 2996 | } else { |
| 2997 | set_caret_column(TS->shaped_text_next_character_pos(text.get_line_data(get_caret_line(i))->get_rid(), get_caret_column(i)), i == 0, i); |
| 2998 | } |
| 2999 | } |
| 3000 | } |
| 3001 | merge_overlapping_carets(); |
| 3002 | } |
| 3003 | |
| 3004 | void TextEdit::_move_caret_up(bool p_select) { |
| 3005 | _push_current_op(); |
| 3006 | for (int i = 0; i < get_caret_count(); i++) { |
| 3007 | if (p_select) { |
| 3008 | _pre_shift_selection(i); |
| 3009 | } else { |
| 3010 | deselect(i); |
| 3011 | } |
| 3012 | |
| 3013 | int cur_wrap_index = get_caret_wrap_index(i); |
| 3014 | if (cur_wrap_index > 0) { |
| 3015 | set_caret_line(get_caret_line(i), true, false, cur_wrap_index - 1, i); |
| 3016 | } else if (get_caret_line(i) == 0) { |
| 3017 | set_caret_column(0, i == 0, i); |
| 3018 | } else { |
| 3019 | int new_line = get_caret_line(i) - get_next_visible_line_offset_from(get_caret_line(i) - 1, -1); |
| 3020 | if (is_line_wrapped(new_line)) { |
| 3021 | set_caret_line(new_line, i == 0, false, get_line_wrap_count(new_line), i); |
| 3022 | } else { |
| 3023 | set_caret_line(new_line, i == 0, false, 0, i); |
| 3024 | } |
| 3025 | } |
| 3026 | } |
| 3027 | merge_overlapping_carets(); |
| 3028 | } |
| 3029 | |
| 3030 | void TextEdit::_move_caret_down(bool p_select) { |
| 3031 | _push_current_op(); |
| 3032 | for (int i = 0; i < get_caret_count(); i++) { |
| 3033 | if (p_select) { |
| 3034 | _pre_shift_selection(i); |
| 3035 | } else { |
| 3036 | deselect(i); |
| 3037 | } |
| 3038 | |
| 3039 | int cur_wrap_index = get_caret_wrap_index(i); |
| 3040 | if (cur_wrap_index < get_line_wrap_count(get_caret_line(i))) { |
| 3041 | set_caret_line(get_caret_line(i), i == 0, false, cur_wrap_index + 1, i); |
| 3042 | } else if (get_caret_line(i) == get_last_unhidden_line()) { |
| 3043 | set_caret_column(text[get_caret_line(i)].length()); |
| 3044 | } else { |
| 3045 | int new_line = get_caret_line(i) + get_next_visible_line_offset_from(CLAMP(get_caret_line(i) + 1, 0, text.size() - 1), 1); |
| 3046 | set_caret_line(new_line, i == 0, false, 0, i); |
| 3047 | } |
| 3048 | } |
| 3049 | merge_overlapping_carets(); |
| 3050 | } |
| 3051 | |
| 3052 | void TextEdit::_move_caret_to_line_start(bool p_select) { |
| 3053 | _push_current_op(); |
| 3054 | for (int i = 0; i < get_caret_count(); i++) { |
| 3055 | if (p_select) { |
| 3056 | _pre_shift_selection(i); |
| 3057 | } else { |
| 3058 | deselect(i); |
| 3059 | } |
| 3060 | |
| 3061 | // Move caret column to start of wrapped row and then to start of text. |
| 3062 | Vector<String> rows = get_line_wrapped_text(get_caret_line(i)); |
| 3063 | int wi = get_caret_wrap_index(i); |
| 3064 | int row_start_col = 0; |
| 3065 | for (int j = 0; j < wi; j++) { |
| 3066 | row_start_col += rows[j].length(); |
| 3067 | } |
| 3068 | if (get_caret_column(i) == row_start_col || wi == 0) { |
| 3069 | // Compute whitespace symbols sequence length. |
| 3070 | int current_line_whitespace_len = get_first_non_whitespace_column(get_caret_line(i)); |
| 3071 | if (get_caret_column(i) == current_line_whitespace_len) { |
| 3072 | set_caret_column(0, i == 0, i); |
| 3073 | } else { |
| 3074 | set_caret_column(current_line_whitespace_len, i == 0, i); |
| 3075 | } |
| 3076 | } else { |
| 3077 | set_caret_column(row_start_col, i == 0, i); |
| 3078 | } |
| 3079 | } |
| 3080 | merge_overlapping_carets(); |
| 3081 | } |
| 3082 | |
| 3083 | void TextEdit::_move_caret_to_line_end(bool p_select) { |
| 3084 | _push_current_op(); |
| 3085 | for (int i = 0; i < get_caret_count(); i++) { |
| 3086 | if (p_select) { |
| 3087 | _pre_shift_selection(i); |
| 3088 | } else { |
| 3089 | deselect(i); |
| 3090 | } |
| 3091 | |
| 3092 | // Move caret column to end of wrapped row and then to end of text. |
| 3093 | Vector<String> rows = get_line_wrapped_text(get_caret_line(i)); |
| 3094 | int wi = get_caret_wrap_index(i); |
| 3095 | int row_end_col = -1; |
| 3096 | for (int j = 0; j < wi + 1; j++) { |
| 3097 | row_end_col += rows[j].length(); |
| 3098 | } |
| 3099 | if (wi == rows.size() - 1 || get_caret_column(i) == row_end_col) { |
| 3100 | set_caret_column(text[get_caret_line(i)].length(), i == 0, i); |
| 3101 | } else { |
| 3102 | set_caret_column(row_end_col, i == 0, i); |
| 3103 | } |
| 3104 | } |
| 3105 | merge_overlapping_carets(); |
| 3106 | } |
| 3107 | |
| 3108 | void TextEdit::_move_caret_page_up(bool p_select) { |
| 3109 | _push_current_op(); |
| 3110 | for (int i = 0; i < get_caret_count(); i++) { |
| 3111 | if (p_select) { |
| 3112 | _pre_shift_selection(i); |
| 3113 | } else { |
| 3114 | deselect(i); |
| 3115 | } |
| 3116 | |
| 3117 | Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), -get_visible_line_count()); |
| 3118 | int n_line = get_caret_line(i) - next_line.x + 1; |
| 3119 | set_caret_line(n_line, i == 0, false, next_line.y, i); |
| 3120 | } |
| 3121 | merge_overlapping_carets(); |
| 3122 | } |
| 3123 | |
| 3124 | void TextEdit::_move_caret_page_down(bool p_select) { |
| 3125 | _push_current_op(); |
| 3126 | for (int i = 0; i < get_caret_count(); i++) { |
| 3127 | if (p_select) { |
| 3128 | _pre_shift_selection(i); |
| 3129 | } else { |
| 3130 | deselect(i); |
| 3131 | } |
| 3132 | |
| 3133 | Point2i next_line = get_next_visible_line_index_offset_from(get_caret_line(i), get_caret_wrap_index(i), get_visible_line_count()); |
| 3134 | int n_line = get_caret_line(i) + next_line.x - 1; |
| 3135 | set_caret_line(n_line, i == 0, false, next_line.y, i); |
| 3136 | } |
| 3137 | merge_overlapping_carets(); |
| 3138 | } |
| 3139 | |
| 3140 | void TextEdit::_do_backspace(bool p_word, bool p_all_to_left) { |
| 3141 | if (!editable) { |
| 3142 | return; |
| 3143 | } |
| 3144 | |
| 3145 | start_action(EditAction::ACTION_BACKSPACE); |
| 3146 | begin_multicaret_edit(); |
| 3147 | |
| 3148 | Vector<int> sorted_carets = get_sorted_carets(); |
| 3149 | sorted_carets.reverse(); |
| 3150 | for (int i = 0; i < sorted_carets.size(); i++) { |
| 3151 | int caret_index = sorted_carets[i]; |
| 3152 | if (multicaret_edit_ignore_caret(caret_index)) { |
| 3153 | continue; |
| 3154 | } |
| 3155 | |
| 3156 | if (get_caret_column(caret_index) == 0 && get_caret_line(caret_index) == 0 && !has_selection(caret_index)) { |
| 3157 | continue; |
| 3158 | } |
| 3159 | |
| 3160 | if (has_selection(caret_index) || (!p_all_to_left && !p_word) || get_caret_column(caret_index) == 0) { |
| 3161 | backspace(caret_index); |
| 3162 | continue; |
| 3163 | } |
| 3164 | |
| 3165 | if (p_all_to_left) { |
| 3166 | // Remove everything to left of caret to the start of the line. |
| 3167 | int caret_current_column = get_caret_column(caret_index); |
| 3168 | _remove_text(get_caret_line(caret_index), 0, get_caret_line(caret_index), caret_current_column); |
| 3169 | collapse_carets(get_caret_line(caret_index), 0, get_caret_line(caret_index), caret_current_column); |
| 3170 | set_caret_column(0, caret_index == 0, caret_index); |
| 3171 | _offset_carets_after(get_caret_line(caret_index), caret_current_column, get_caret_line(caret_index), 0); |
| 3172 | continue; |
| 3173 | } |
| 3174 | |
| 3175 | if (p_word) { |
| 3176 | // Remove text to the start of the word left of the caret. |
| 3177 | int from_column = get_caret_column(caret_index); |
| 3178 | int column = get_caret_column(caret_index); |
| 3179 | // Check for the case "<word><space><caret>" and ignore the space. |
| 3180 | // No need to check for column being 0 since it is checked above. |
| 3181 | if (is_whitespace(text[get_caret_line(caret_index)][get_caret_column(caret_index) - 1])) { |
| 3182 | column -= 1; |
| 3183 | } |
| 3184 | |
| 3185 | // Get a list with the indices of the word bounds of the given text line. |
| 3186 | const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(caret_index))->get_rid()); |
| 3187 | if (words.is_empty() || column <= words[0]) { |
| 3188 | // Delete to the start when there are no more words. |
| 3189 | column = 0; |
| 3190 | } else { |
| 3191 | // Otherwise search for the first word break that is smaller than the index from we're currently deleting. |
| 3192 | for (int c = words.size() - 2; c >= 0; c = c - 2) { |
| 3193 | if (words[c] < column) { |
| 3194 | column = words[c]; |
| 3195 | break; |
| 3196 | } |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | _remove_text(get_caret_line(caret_index), column, get_caret_line(caret_index), from_column); |
| 3201 | collapse_carets(get_caret_line(caret_index), column, get_caret_line(caret_index), from_column); |
| 3202 | set_caret_column(column, caret_index == 0, caret_index); |
| 3203 | _offset_carets_after(get_caret_line(caret_index), from_column, get_caret_line(caret_index), column); |
| 3204 | } |
| 3205 | } |
| 3206 | |
| 3207 | end_multicaret_edit(); |
| 3208 | end_action(); |
| 3209 | } |
| 3210 | |
| 3211 | void TextEdit::_delete(bool p_word, bool p_all_to_right) { |
| 3212 | if (!editable) { |
| 3213 | return; |
| 3214 | } |
| 3215 | |
| 3216 | start_action(EditAction::ACTION_DELETE); |
| 3217 | begin_multicaret_edit(); |
| 3218 | |
| 3219 | Vector<int> sorted_carets = get_sorted_carets(); |
| 3220 | for (int i = 0; i < sorted_carets.size(); i++) { |
| 3221 | int caret_index = sorted_carets[i]; |
| 3222 | if (multicaret_edit_ignore_caret(caret_index)) { |
| 3223 | continue; |
| 3224 | } |
| 3225 | |
| 3226 | if (has_selection(caret_index)) { |
| 3227 | delete_selection(caret_index); |
| 3228 | continue; |
| 3229 | } |
| 3230 | |
| 3231 | int curline_len = text[get_caret_line(caret_index)].length(); |
| 3232 | if (get_caret_line(caret_index) == text.size() - 1 && get_caret_column(caret_index) == curline_len) { |
| 3233 | continue; // Last line, last column: Nothing to do. |
| 3234 | } |
| 3235 | |
| 3236 | int next_line = get_caret_column(caret_index) < curline_len ? get_caret_line(caret_index) : get_caret_line(caret_index) + 1; |
| 3237 | int next_column; |
| 3238 | |
| 3239 | if (p_all_to_right) { |
| 3240 | if (get_caret_column(caret_index) == curline_len) { |
| 3241 | continue; |
| 3242 | } |
| 3243 | |
| 3244 | // Delete everything to right of caret. |
| 3245 | next_column = curline_len; |
| 3246 | next_line = get_caret_line(caret_index); |
| 3247 | } else if (p_word && get_caret_column(caret_index) < curline_len - 1) { |
| 3248 | // Delete next word to right of caret. |
| 3249 | int line = get_caret_line(caret_index); |
| 3250 | int column = get_caret_column(caret_index); |
| 3251 | |
| 3252 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid()); |
| 3253 | if (words.is_empty() || column >= words[words.size() - 1]) { |
| 3254 | // Delete to the end when there are no more words. |
| 3255 | column = text[get_caret_line(i)].length(); |
| 3256 | } else { |
| 3257 | for (int j = 1; j < words.size(); j = j + 2) { |
| 3258 | if (words[j] > column) { |
| 3259 | column = words[j]; |
| 3260 | break; |
| 3261 | } |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | next_line = line; |
| 3266 | next_column = column; |
| 3267 | } else { |
| 3268 | // Delete one character. |
| 3269 | if (caret_mid_grapheme_enabled) { |
| 3270 | next_column = get_caret_column(caret_index) < curline_len ? (get_caret_column(caret_index) + 1) : 0; |
| 3271 | } else { |
| 3272 | next_column = get_caret_column(caret_index) < curline_len ? TS->shaped_text_next_character_pos(text.get_line_data(get_caret_line(caret_index))->get_rid(), (get_caret_column(caret_index))) : 0; |
| 3273 | } |
| 3274 | } |
| 3275 | |
| 3276 | _remove_text(get_caret_line(caret_index), get_caret_column(caret_index), next_line, next_column); |
| 3277 | collapse_carets(get_caret_line(caret_index), get_caret_column(caret_index), next_line, next_column); |
| 3278 | _offset_carets_after(next_line, next_column, get_caret_line(caret_index), get_caret_column(caret_index)); |
| 3279 | } |
| 3280 | |
| 3281 | end_multicaret_edit(); |
| 3282 | end_action(); |
| 3283 | } |
| 3284 | |
| 3285 | void TextEdit::_move_caret_document_start(bool p_select) { |
| 3286 | remove_secondary_carets(); |
| 3287 | if (p_select) { |
| 3288 | _pre_shift_selection(0); |
| 3289 | } else { |
| 3290 | deselect(); |
| 3291 | } |
| 3292 | |
| 3293 | set_caret_line(0, false, true, -1); |
| 3294 | set_caret_column(0); |
| 3295 | } |
| 3296 | |
| 3297 | void TextEdit::_move_caret_document_end(bool p_select) { |
| 3298 | remove_secondary_carets(); |
| 3299 | if (p_select) { |
| 3300 | _pre_shift_selection(0); |
| 3301 | } else { |
| 3302 | deselect(); |
| 3303 | } |
| 3304 | |
| 3305 | set_caret_line(get_last_unhidden_line(), true, false, -1); |
| 3306 | set_caret_column(text[get_caret_line()].length()); |
| 3307 | } |
| 3308 | |
| 3309 | bool TextEdit::_clear_carets_and_selection() { |
| 3310 | _push_current_op(); |
| 3311 | if (get_caret_count() > 1) { |
| 3312 | remove_secondary_carets(); |
| 3313 | return true; |
| 3314 | } |
| 3315 | |
| 3316 | if (has_selection()) { |
| 3317 | deselect(); |
| 3318 | return true; |
| 3319 | } |
| 3320 | |
| 3321 | return false; |
| 3322 | } |
| 3323 | |
| 3324 | void TextEdit::_update_placeholder() { |
| 3325 | if (theme_cache.font.is_null() || theme_cache.font_size <= 0) { |
| 3326 | return; // Not in tree? |
| 3327 | } |
| 3328 | |
| 3329 | const String placeholder_translated = atr(placeholder_text); |
| 3330 | |
| 3331 | // Placeholder is generally smaller then text documents, and updates less so this should be fast enough for now. |
| 3332 | placeholder_data_buf->clear(); |
| 3333 | placeholder_data_buf->set_width(text.get_width()); |
| 3334 | BitField<TextServer::LineBreakFlag> flags = text.get_brk_flags(); |
| 3335 | if (text.is_indent_wrapped_lines()) { |
| 3336 | flags.set_flag(TextServer::BREAK_TRIM_INDENT); |
| 3337 | } |
| 3338 | placeholder_data_buf->set_break_flags(flags); |
| 3339 | if (text_direction == Control::TEXT_DIRECTION_INHERITED) { |
| 3340 | placeholder_data_buf->set_direction(is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); |
| 3341 | } else { |
| 3342 | placeholder_data_buf->set_direction((TextServer::Direction)text_direction); |
| 3343 | } |
| 3344 | placeholder_data_buf->set_preserve_control(draw_control_chars); |
| 3345 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 3346 | placeholder_data_buf->add_string(placeholder_translated, theme_cache.font, theme_cache.font_size, lang); |
| 3347 | |
| 3348 | placeholder_bidi_override = structured_text_parser(st_parser, st_args, placeholder_translated); |
| 3349 | if (placeholder_bidi_override.is_empty()) { |
| 3350 | TS->shaped_text_set_bidi_override(placeholder_data_buf->get_rid(), placeholder_bidi_override); |
| 3351 | } |
| 3352 | |
| 3353 | if (get_tab_size() > 0) { |
| 3354 | Vector<float> tabs; |
| 3355 | tabs.push_back(MAX(1, (theme_cache.font->get_char_size(' ', theme_cache.font_size).width + theme_cache.font->get_spacing(TextServer::SPACING_SPACE)) * get_tab_size())); |
| 3356 | placeholder_data_buf->tab_align(tabs); |
| 3357 | } |
| 3358 | |
| 3359 | // Update height. |
| 3360 | const int wrap_amount = placeholder_data_buf->get_line_count() - 1; |
| 3361 | placeholder_line_height = theme_cache.font->get_height(theme_cache.font_size); |
| 3362 | for (int i = 0; i <= wrap_amount; i++) { |
| 3363 | placeholder_line_height = MAX(placeholder_line_height, placeholder_data_buf->get_line_size(i).y); |
| 3364 | } |
| 3365 | |
| 3366 | // Update width. |
| 3367 | placeholder_max_width = placeholder_data_buf->get_size().x; |
| 3368 | |
| 3369 | // Update wrapped rows. |
| 3370 | placeholder_wrapped_rows.clear(); |
| 3371 | for (int i = 0; i <= wrap_amount; i++) { |
| 3372 | Vector2i line_range = placeholder_data_buf->get_line_range(i); |
| 3373 | placeholder_wrapped_rows.push_back(placeholder_translated.substr(line_range.x, line_range.y - line_range.x)); |
| 3374 | } |
| 3375 | } |
| 3376 | |
| 3377 | bool TextEdit::_using_placeholder() const { |
| 3378 | return text.size() == 1 && text[0].is_empty() && ime_text.is_empty(); |
| 3379 | } |
| 3380 | |
| 3381 | void TextEdit::_update_theme_item_cache() { |
| 3382 | Control::_update_theme_item_cache(); |
| 3383 | |
| 3384 | theme_cache.base_scale = get_theme_default_base_scale(); |
| 3385 | use_selected_font_color = theme_cache.font_selected_color != Color(0, 0, 0, 0); |
| 3386 | |
| 3387 | if (text.get_line_height() + theme_cache.line_spacing < 1) { |
| 3388 | WARN_PRINT("Line height is too small, please increase font_size and/or line_spacing"); |
| 3389 | } |
| 3390 | } |
| 3391 | |
| 3392 | void TextEdit::_update_caches(bool p_invalidate_all) { |
| 3393 | /* Text properties. */ |
| 3394 | TextServer::Direction dir; |
| 3395 | if (text_direction == Control::TEXT_DIRECTION_INHERITED) { |
| 3396 | dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR; |
| 3397 | } else { |
| 3398 | dir = (TextServer::Direction)text_direction; |
| 3399 | } |
| 3400 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 3401 | text.set_direction_and_language(dir, lang); |
| 3402 | text.set_draw_control_chars(draw_control_chars); |
| 3403 | text.set_font(theme_cache.font); |
| 3404 | text.set_font_size(theme_cache.font_size); |
| 3405 | if (p_invalidate_all) { |
| 3406 | text.invalidate_all(); |
| 3407 | } else { |
| 3408 | text.invalidate_font(); |
| 3409 | } |
| 3410 | _update_placeholder(); |
| 3411 | |
| 3412 | /* Syntax highlighting. */ |
| 3413 | if (syntax_highlighter.is_valid()) { |
| 3414 | syntax_highlighter->set_text_edit(this); |
| 3415 | } |
| 3416 | _clear_syntax_highlighting_cache(); |
| 3417 | queue_accessibility_update(); |
| 3418 | } |
| 3419 | |
| 3420 | void TextEdit::_close_ime_window() { |
| 3421 | DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID; |
| 3422 | if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) { |
| 3423 | return; |
| 3424 | } |
| 3425 | DisplayServer::get_singleton()->window_set_ime_position(Point2(), wid); |
| 3426 | DisplayServer::get_singleton()->window_set_ime_active(false, wid); |
| 3427 | } |
| 3428 | |
| 3429 | void TextEdit::_update_ime_window_position() { |
| 3430 | DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID; |
| 3431 | if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) { |
| 3432 | return; |
| 3433 | } |
| 3434 | DisplayServer::get_singleton()->window_set_ime_active(true, wid); |
| 3435 | Point2 pos = get_global_position() + get_caret_draw_pos(); |
| 3436 | if (get_window()->get_embedder()) { |
| 3437 | pos += get_viewport()->get_popup_base_transform().get_origin(); |
| 3438 | } |
| 3439 | // Take into account the window's transform. |
| 3440 | pos = get_window()->get_screen_transform().xform(pos); |
| 3441 | // The window will move to the updated position the next time the IME is updated, not immediately. |
| 3442 | DisplayServer::get_singleton()->window_set_ime_position(pos, wid); |
| 3443 | } |
| 3444 | |
| 3445 | void TextEdit::_update_ime_text() { |
| 3446 | if (has_ime_text()) { |
| 3447 | // Update text to visually include IME text. |
| 3448 | for (int i = 0; i < get_caret_count(); i++) { |
| 3449 | int l = get_caret_line(i); |
| 3450 | String text_with_ime = text[l].substr(0, get_caret_column(i)) + ime_text + text[l].substr(get_caret_column(i)); |
| 3451 | text.set_ime(l, text_with_ime, structured_text_parser(st_parser, st_args, text_with_ime)); |
| 3452 | emit_signal(SNAME("lines_edited_from"), l, l); |
| 3453 | } |
| 3454 | } else { |
| 3455 | // Reset text. |
| 3456 | for (int i = 0; i < get_caret_count(); i++) { |
| 3457 | int l = get_caret_line(i); |
| 3458 | text.set_ime(l, String(), Array()); |
| 3459 | emit_signal(SNAME("lines_edited_from"), l, l); |
| 3460 | } |
| 3461 | } |
| 3462 | _clear_syntax_highlighting_cache(); |
| 3463 | queue_accessibility_update(); |
| 3464 | queue_redraw(); |
| 3465 | } |
| 3466 | |
| 3467 | void TextEdit::_show_virtual_keyboard() { |
| 3468 | _update_ime_window_position(); |
| 3469 | |
| 3470 | if (virtual_keyboard_enabled && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD)) { |
| 3471 | int caret_start = -1; |
| 3472 | int caret_end = -1; |
| 3473 | |
| 3474 | if (!has_selection(0)) { |
| 3475 | String full_text = _base_get_text(0, 0, get_caret_line(), get_caret_column()); |
| 3476 | |
| 3477 | caret_start = full_text.length(); |
| 3478 | } else { |
| 3479 | String pre_text = _base_get_text(0, 0, get_selection_from_line(), get_selection_from_column()); |
| 3480 | String post_text = get_selected_text(0); |
| 3481 | |
| 3482 | caret_start = pre_text.length(); |
| 3483 | caret_end = caret_start + post_text.length(); |
| 3484 | } |
| 3485 | |
| 3486 | DisplayServer::get_singleton()->virtual_keyboard_show(get_text(), get_global_rect(), DisplayServer::KEYBOARD_TYPE_MULTILINE, -1, caret_start, caret_end); |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | /* General overrides. */ |
| 3491 | Size2 TextEdit::get_minimum_size() const { |
| 3492 | Size2 ms = _get_current_stylebox()->get_minimum_size(); |
| 3493 | if (fit_content_height) { |
| 3494 | ms.height += content_size_cache.height; |
| 3495 | } |
| 3496 | if (fit_content_width) { |
| 3497 | ms.width += content_size_cache.width; |
| 3498 | } |
| 3499 | return ms; |
| 3500 | } |
| 3501 | |
| 3502 | bool TextEdit::is_text_field() const { |
| 3503 | return true; |
| 3504 | } |
| 3505 | |
| 3506 | Variant TextEdit::get_drag_data(const Point2 &p_point) { |
| 3507 | Variant ret = Control::get_drag_data(p_point); |
| 3508 | if (ret != Variant()) { |
| 3509 | return ret; |
| 3510 | } |
| 3511 | |
| 3512 | if (has_selection() && selection_drag_attempt) { |
| 3513 | String t = get_selected_text(); |
| 3514 | Label *l = memnew(Label); |
| 3515 | l->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate user input. |
| 3516 | l->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 3517 | l->set_text(t); |
| 3518 | set_drag_preview(l); |
| 3519 | return t; |
| 3520 | } |
| 3521 | |
| 3522 | return Variant(); |
| 3523 | } |
| 3524 | |
| 3525 | bool TextEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const { |
| 3526 | bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data. |
| 3527 | if (drop_override) { |
| 3528 | return drop_override; |
| 3529 | } |
| 3530 | |
| 3531 | return is_editable() && p_data.is_string(); |
| 3532 | } |
| 3533 | |
| 3534 | void TextEdit::drop_data(const Point2 &p_point, const Variant &p_data) { |
| 3535 | Control::drop_data(p_point, p_data); |
| 3536 | |
| 3537 | if (p_point == Vector2(Math::INF, Math::INF)) { |
| 3538 | insert_text_at_caret(p_data); |
| 3539 | } else if (p_data.is_string() && is_editable()) { |
| 3540 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 3541 | int drop_at_line = pos.y; |
| 3542 | int drop_at_column = pos.x; |
| 3543 | int selection_index = get_selection_at_line_column(drop_at_line, drop_at_column, !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)); |
| 3544 | |
| 3545 | // Remove drag caret before the complex operation starts so it won't appear in undo. |
| 3546 | remove_caret(drag_caret_index); |
| 3547 | |
| 3548 | if (selection_drag_attempt && selection_index >= 0 && selection_index == drag_and_drop_origin_caret_index) { |
| 3549 | // Dropped onto original selection, do nothing. |
| 3550 | selection_drag_attempt = false; |
| 3551 | return; |
| 3552 | } |
| 3553 | |
| 3554 | begin_complex_operation(); |
| 3555 | begin_multicaret_edit(); |
| 3556 | if (selection_drag_attempt) { |
| 3557 | // Drop from self. |
| 3558 | selection_drag_attempt = false; |
| 3559 | if (!Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 3560 | // Delete all selections. |
| 3561 | int temp_caret = add_caret(drop_at_line, drop_at_column); |
| 3562 | |
| 3563 | delete_selection(); |
| 3564 | |
| 3565 | // Use a temporary caret to update the drop at position. |
| 3566 | drop_at_line = get_caret_line(temp_caret); |
| 3567 | drop_at_column = get_caret_column(temp_caret); |
| 3568 | } |
| 3569 | } |
| 3570 | remove_secondary_carets(); |
| 3571 | deselect(); |
| 3572 | |
| 3573 | // Insert the dragged text. |
| 3574 | set_caret_line(drop_at_line, true, false, -1); |
| 3575 | set_caret_column(drop_at_column); |
| 3576 | insert_text_at_caret(p_data); |
| 3577 | |
| 3578 | select(drop_at_line, drop_at_column, get_caret_line(), get_caret_column()); |
| 3579 | grab_focus(); |
| 3580 | adjust_viewport_to_caret(); |
| 3581 | end_multicaret_edit(); |
| 3582 | end_complex_operation(); |
| 3583 | } |
| 3584 | } |
| 3585 | |
| 3586 | Control::CursorShape TextEdit::get_cursor_shape(const Point2 &p_pos) const { |
| 3587 | if (dragging_selection) { |
| 3588 | return get_default_cursor_shape(); |
| 3589 | } |
| 3590 | |
| 3591 | if (dragging_minimap) { |
| 3592 | return CURSOR_ARROW; |
| 3593 | } |
| 3594 | |
| 3595 | Vector2i current_hovered_gutter = _get_hovered_gutter(p_pos); |
| 3596 | if (current_hovered_gutter != Vector2i(-1, -1)) { |
| 3597 | if (gutters[current_hovered_gutter.x].clickable || is_line_gutter_clickable(current_hovered_gutter.y, current_hovered_gutter.x)) { |
| 3598 | return CURSOR_POINTING_HAND; |
| 3599 | } else { |
| 3600 | return CURSOR_ARROW; |
| 3601 | } |
| 3602 | } |
| 3603 | Ref<StyleBox> style = _get_current_stylebox(); |
| 3604 | int left_margin = Math::ceil(style->get_margin(SIDE_LEFT)); |
| 3605 | if (p_pos.x < left_margin + gutters_width + gutter_padding) { |
| 3606 | return CURSOR_ARROW; |
| 3607 | } |
| 3608 | |
| 3609 | int xmargin_end = get_size().width - Math::floor(style->get_margin(SIDE_RIGHT)); |
| 3610 | if (draw_minimap && p_pos.x > xmargin_end - minimap_width && p_pos.x <= xmargin_end) { |
| 3611 | return CURSOR_ARROW; |
| 3612 | } |
| 3613 | |
| 3614 | // Hover inline objects. |
| 3615 | if (inline_object_click_handler.is_valid()) { |
| 3616 | Point2i pos = get_line_column_at_pos(p_pos); |
| 3617 | int xmargin_beg = left_margin + gutters_width + gutter_padding; |
| 3618 | int wrap_i = get_line_wrap_index_at_column(pos.y, pos.x); |
| 3619 | const float wrap_indent = _get_wrap_indent_offset(pos.y, wrap_i, is_layout_rtl()); |
| 3620 | |
| 3621 | Ref<TextParagraph> ldata = text.get_line_data(pos.y); |
| 3622 | for (Variant k : ldata->get_line_objects(wrap_i)) { |
| 3623 | if (!is_inline_info_valid(k)) { |
| 3624 | continue; |
| 3625 | } |
| 3626 | Rect2 obj_rect = ldata->get_line_object_rect(wrap_i, k); |
| 3627 | obj_rect.position.x += xmargin_beg + wrap_indent - first_visible_col; |
| 3628 | if (p_pos.x > obj_rect.position.x && p_pos.x < obj_rect.get_end().x) { |
| 3629 | return CURSOR_POINTING_HAND; |
| 3630 | } |
| 3631 | } |
| 3632 | } |
| 3633 | |
| 3634 | return get_default_cursor_shape(); |
| 3635 | } |
| 3636 | |
| 3637 | String TextEdit::get_tooltip(const Point2 &p_pos) const { |
| 3638 | if (!tooltip_callback.is_valid()) { |
| 3639 | return Control::get_tooltip(p_pos); |
| 3640 | } |
| 3641 | Point2i pos = get_line_column_at_pos(p_pos); |
| 3642 | int line = pos.y; |
| 3643 | int col = pos.x; |
| 3644 | |
| 3645 | const String &text_line = text[line]; |
| 3646 | if (text_line.is_empty()) { |
| 3647 | return Control::get_tooltip(p_pos); |
| 3648 | } |
| 3649 | const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid()); |
| 3650 | for (int i = 0; i < words.size(); i = i + 2) { |
| 3651 | if (words[i] <= col && words[i + 1] >= col) { |
| 3652 | Variant args[1] = { text_line.substr(words[i], words[i + 1] - words[i]) }; |
| 3653 | const Variant *argp[] = { &args[0] }; |
| 3654 | Callable::CallError ce; |
| 3655 | Variant ret; |
| 3656 | tooltip_callback.callp(argp, 1, ret, ce); |
| 3657 | ERR_FAIL_COND_V_MSG(ce.error != Callable::CallError::CALL_OK, "", "Failed to call custom tooltip."); |
| 3658 | return ret; |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | return Control::get_tooltip(p_pos); |
| 3663 | } |
| 3664 | |
| 3665 | void TextEdit::set_tooltip_request_func(const Callable &p_tooltip_callback) { |
| 3666 | tooltip_callback = p_tooltip_callback; |
| 3667 | queue_accessibility_update(); |
| 3668 | } |
| 3669 | |
| 3670 | /* Text */ |
| 3671 | // Text properties. |
| 3672 | bool TextEdit::has_ime_text() const { |
| 3673 | return !ime_text.is_empty(); |
| 3674 | } |
| 3675 | |
| 3676 | void TextEdit::cancel_ime() { |
| 3677 | if (!has_ime_text()) { |
| 3678 | _close_ime_window(); |
| 3679 | return; |
| 3680 | } |
| 3681 | ime_text = String(); |
| 3682 | ime_selection = Vector2i(); |
| 3683 | alt_start = false; |
| 3684 | alt_start_no_hold = false; |
| 3685 | _close_ime_window(); |
| 3686 | _update_ime_text(); |
| 3687 | } |
| 3688 | |
| 3689 | void TextEdit::apply_ime() { |
| 3690 | if (!has_ime_text()) { |
| 3691 | _close_ime_window(); |
| 3692 | return; |
| 3693 | } |
| 3694 | |
| 3695 | // Force apply the current IME text. |
| 3696 | if (alt_start || alt_start_no_hold) { |
| 3697 | cancel_ime(); |
| 3698 | if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) { |
| 3699 | handle_unicode_input(alt_code); |
| 3700 | } |
| 3701 | } else { |
| 3702 | String insert_ime_text = ime_text; |
| 3703 | cancel_ime(); |
| 3704 | insert_text_at_caret(insert_ime_text); |
| 3705 | } |
| 3706 | } |
| 3707 | |
| 3708 | void TextEdit::set_editable(bool p_editable) { |
| 3709 | if (editable == p_editable) { |
| 3710 | return; |
| 3711 | } |
| 3712 | |
| 3713 | editable = p_editable; |
| 3714 | queue_accessibility_update(); |
| 3715 | queue_redraw(); |
| 3716 | update_minimum_size(); |
| 3717 | } |
| 3718 | |
| 3719 | bool TextEdit::is_editable() const { |
| 3720 | return editable; |
| 3721 | } |
| 3722 | |
| 3723 | void TextEdit::set_text_direction(Control::TextDirection p_text_direction) { |
| 3724 | ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); |
| 3725 | if (text_direction != p_text_direction) { |
| 3726 | text_direction = p_text_direction; |
| 3727 | if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) { |
| 3728 | input_direction = text_direction; |
| 3729 | } |
| 3730 | TextServer::Direction dir; |
| 3731 | if (text_direction == Control::TEXT_DIRECTION_INHERITED) { |
| 3732 | dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR; |
| 3733 | } else { |
| 3734 | dir = (TextServer::Direction)text_direction; |
| 3735 | } |
| 3736 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 3737 | text.set_direction_and_language(dir, lang); |
| 3738 | text.invalidate_all(); |
| 3739 | _update_placeholder(); |
| 3740 | |
| 3741 | if (menu_dir) { |
| 3742 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED); |
| 3743 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO); |
| 3744 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR); |
| 3745 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL); |
| 3746 | } |
| 3747 | queue_accessibility_update(); |
| 3748 | queue_redraw(); |
| 3749 | } |
| 3750 | } |
| 3751 | |
| 3752 | Control::TextDirection TextEdit::get_text_direction() const { |
| 3753 | return text_direction; |
| 3754 | } |
| 3755 | |
| 3756 | void TextEdit::set_language(const String &p_language) { |
| 3757 | if (language != p_language) { |
| 3758 | language = p_language; |
| 3759 | TextServer::Direction dir; |
| 3760 | if (text_direction == Control::TEXT_DIRECTION_INHERITED) { |
| 3761 | dir = is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR; |
| 3762 | } else { |
| 3763 | dir = (TextServer::Direction)text_direction; |
| 3764 | } |
| 3765 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 3766 | text.set_direction_and_language(dir, lang); |
| 3767 | text.invalidate_all(); |
| 3768 | _update_placeholder(); |
| 3769 | queue_accessibility_update(); |
| 3770 | queue_redraw(); |
| 3771 | } |
| 3772 | } |
| 3773 | |
| 3774 | String TextEdit::get_language() const { |
| 3775 | return language; |
| 3776 | } |
| 3777 | |
| 3778 | void TextEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) { |
| 3779 | if (st_parser != p_parser) { |
| 3780 | st_parser = p_parser; |
| 3781 | for (int i = 0; i < text.size(); i++) { |
| 3782 | text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i])); |
| 3783 | } |
| 3784 | queue_accessibility_update(); |
| 3785 | queue_redraw(); |
| 3786 | } |
| 3787 | } |
| 3788 | |
| 3789 | TextServer::StructuredTextParser TextEdit::get_structured_text_bidi_override() const { |
| 3790 | return st_parser; |
| 3791 | } |
| 3792 | |
| 3793 | void TextEdit::set_structured_text_bidi_override_options(const Array &p_args) { |
| 3794 | if (st_args == p_args) { |
| 3795 | return; |
| 3796 | } |
| 3797 | |
| 3798 | st_args = Array(p_args); |
| 3799 | for (int i = 0; i < text.size(); i++) { |
| 3800 | text.set(i, text[i], structured_text_parser(st_parser, st_args, text[i])); |
| 3801 | } |
| 3802 | queue_accessibility_update(); |
| 3803 | queue_redraw(); |
| 3804 | } |
| 3805 | |
| 3806 | Array TextEdit::get_structured_text_bidi_override_options() const { |
| 3807 | return Array(st_args); |
| 3808 | } |
| 3809 | |
| 3810 | void TextEdit::set_tab_size(const int p_size) { |
| 3811 | ERR_FAIL_COND_MSG(p_size <= 0, "Tab size must be greater than 0."); |
| 3812 | if (p_size == text.get_tab_size()) { |
| 3813 | return; |
| 3814 | } |
| 3815 | text.set_tab_size(p_size); |
| 3816 | text.invalidate_all_lines(); |
| 3817 | _update_placeholder(); |
| 3818 | queue_accessibility_update(); |
| 3819 | queue_redraw(); |
| 3820 | } |
| 3821 | |
| 3822 | int TextEdit::get_tab_size() const { |
| 3823 | return text.get_tab_size(); |
| 3824 | } |
| 3825 | |
| 3826 | void TextEdit::set_indent_wrapped_lines(bool p_enabled) { |
| 3827 | if (text.is_indent_wrapped_lines() == p_enabled) { |
| 3828 | return; |
| 3829 | } |
| 3830 | text.set_indent_wrapped_lines(p_enabled); |
| 3831 | text.invalidate_all_lines(); |
| 3832 | _update_placeholder(); |
| 3833 | queue_redraw(); |
| 3834 | } |
| 3835 | |
| 3836 | bool TextEdit::is_indent_wrapped_lines() const { |
| 3837 | return text.is_indent_wrapped_lines(); |
| 3838 | } |
| 3839 | |
| 3840 | void TextEdit::set_tab_input_mode(bool p_enabled) { |
| 3841 | tab_input_mode = p_enabled; |
| 3842 | } |
| 3843 | |
| 3844 | bool TextEdit::get_tab_input_mode() const { |
| 3845 | return tab_input_mode; |
| 3846 | } |
| 3847 | |
| 3848 | // User controls |
| 3849 | void TextEdit::set_overtype_mode_enabled(bool p_enabled) { |
| 3850 | if (overtype_mode == p_enabled) { |
| 3851 | return; |
| 3852 | } |
| 3853 | |
| 3854 | overtype_mode = p_enabled; |
| 3855 | queue_redraw(); |
| 3856 | } |
| 3857 | |
| 3858 | bool TextEdit::is_overtype_mode_enabled() const { |
| 3859 | return overtype_mode; |
| 3860 | } |
| 3861 | |
| 3862 | void TextEdit::set_context_menu_enabled(bool p_enabled) { |
| 3863 | context_menu_enabled = p_enabled; |
| 3864 | } |
| 3865 | |
| 3866 | bool TextEdit::is_context_menu_enabled() const { |
| 3867 | return context_menu_enabled; |
| 3868 | } |
| 3869 | |
| 3870 | void TextEdit::show_emoji_and_symbol_picker() { |
| 3871 | _update_ime_window_position(); |
| 3872 | DisplayServer::get_singleton()->show_emoji_and_symbol_picker(); |
| 3873 | } |
| 3874 | |
| 3875 | void TextEdit::set_emoji_menu_enabled(bool p_enabled) { |
| 3876 | if (emoji_menu_enabled != p_enabled) { |
| 3877 | emoji_menu_enabled = p_enabled; |
| 3878 | } |
| 3879 | } |
| 3880 | |
| 3881 | bool TextEdit::is_emoji_menu_enabled() const { |
| 3882 | return emoji_menu_enabled; |
| 3883 | } |
| 3884 | |
| 3885 | void TextEdit::set_backspace_deletes_composite_character_enabled(bool p_enabled) { |
| 3886 | backspace_deletes_composite_character_enabled = p_enabled; |
| 3887 | } |
| 3888 | |
| 3889 | bool TextEdit::is_backspace_deletes_composite_character_enabled() const { |
| 3890 | return backspace_deletes_composite_character_enabled; |
| 3891 | } |
| 3892 | |
| 3893 | void TextEdit::set_shortcut_keys_enabled(bool p_enabled) { |
| 3894 | shortcut_keys_enabled = p_enabled; |
| 3895 | } |
| 3896 | |
| 3897 | bool TextEdit::is_shortcut_keys_enabled() const { |
| 3898 | return shortcut_keys_enabled; |
| 3899 | } |
| 3900 | |
| 3901 | void TextEdit::set_virtual_keyboard_enabled(bool p_enabled) { |
| 3902 | virtual_keyboard_enabled = p_enabled; |
| 3903 | } |
| 3904 | |
| 3905 | bool TextEdit::is_virtual_keyboard_enabled() const { |
| 3906 | return virtual_keyboard_enabled; |
| 3907 | } |
| 3908 | |
| 3909 | void TextEdit::set_virtual_keyboard_show_on_focus(bool p_show_on_focus) { |
| 3910 | virtual_keyboard_show_on_focus = p_show_on_focus; |
| 3911 | } |
| 3912 | |
| 3913 | bool TextEdit::get_virtual_keyboard_show_on_focus() const { |
| 3914 | return virtual_keyboard_show_on_focus; |
| 3915 | } |
| 3916 | |
| 3917 | void TextEdit::set_middle_mouse_paste_enabled(bool p_enabled) { |
| 3918 | middle_mouse_paste_enabled = p_enabled; |
| 3919 | } |
| 3920 | |
| 3921 | bool TextEdit::is_middle_mouse_paste_enabled() const { |
| 3922 | return middle_mouse_paste_enabled; |
| 3923 | } |
| 3924 | |
| 3925 | void TextEdit::set_empty_selection_clipboard_enabled(bool p_enabled) { |
| 3926 | empty_selection_clipboard_enabled = p_enabled; |
| 3927 | } |
| 3928 | |
| 3929 | bool TextEdit::is_empty_selection_clipboard_enabled() const { |
| 3930 | return empty_selection_clipboard_enabled; |
| 3931 | } |
| 3932 | |
| 3933 | // Text manipulation |
| 3934 | void TextEdit::clear() { |
| 3935 | setting_text = true; |
| 3936 | _clear(); |
| 3937 | setting_text = false; |
| 3938 | emit_signal(SNAME("text_set")); |
| 3939 | } |
| 3940 | |
| 3941 | void TextEdit::_clear() { |
| 3942 | if (editable && undo_enabled) { |
| 3943 | remove_secondary_carets(); |
| 3944 | _move_caret_document_start(false); |
| 3945 | begin_complex_operation(); |
| 3946 | |
| 3947 | _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0)); |
| 3948 | insert_text_at_caret(""); |
| 3949 | text.clear(); |
| 3950 | |
| 3951 | end_complex_operation(); |
| 3952 | return; |
| 3953 | } |
| 3954 | // Cannot merge with above, as we are not part of the tree on creation. |
| 3955 | int old_text_size = text.size(); |
| 3956 | |
| 3957 | clear_undo_history(); |
| 3958 | text.clear(); |
| 3959 | remove_secondary_carets(); |
| 3960 | set_caret_line(0, false, true, -1); |
| 3961 | set_caret_column(0); |
| 3962 | first_visible_col = 0; |
| 3963 | first_visible_line = 0; |
| 3964 | first_visible_line_wrap_ofs = 0; |
| 3965 | carets.write[0].last_fit_x = 0; |
| 3966 | deselect(); |
| 3967 | |
| 3968 | emit_signal(SNAME("lines_edited_from"), old_text_size, 0); |
| 3969 | } |
| 3970 | |
| 3971 | void TextEdit::_set_text(const String &p_text, bool p_emit_signal) { |
| 3972 | setting_text = true; |
| 3973 | |
| 3974 | if (undo_enabled) { |
| 3975 | remove_secondary_carets(); |
| 3976 | set_caret_line(0); |
| 3977 | set_caret_column(0); |
| 3978 | |
| 3979 | begin_complex_operation(); |
| 3980 | deselect(); |
| 3981 | _remove_text(0, 0, MAX(0, get_line_count() - 1), MAX(get_line(MAX(get_line_count() - 1, 0)).size() - 1, 0)); |
| 3982 | } else { |
| 3983 | _clear(); |
| 3984 | } |
| 3985 | |
| 3986 | String previous_text; |
| 3987 | if (p_emit_signal) { |
| 3988 | previous_text = get_text(); |
| 3989 | } |
| 3990 | |
| 3991 | insert_text_at_caret(p_text); |
| 3992 | |
| 3993 | if (p_emit_signal && get_text() != previous_text) { |
| 3994 | _text_changed(); |
| 3995 | } |
| 3996 | |
| 3997 | if (undo_enabled) { |
| 3998 | end_complex_operation(); |
| 3999 | } |
| 4000 | |
| 4001 | set_caret_line(0); |
| 4002 | set_caret_column(0); |
| 4003 | queue_accessibility_update(); |
| 4004 | queue_redraw(); |
| 4005 | setting_text = false; |
| 4006 | emit_signal(SNAME("text_set")); |
| 4007 | } |
| 4008 | |
| 4009 | void TextEdit::set_text(const String &p_text) { |
| 4010 | _set_text(p_text, false); |
| 4011 | } |
| 4012 | |
| 4013 | String TextEdit::get_text() const { |
| 4014 | StringBuilder ret_text; |
| 4015 | const int text_size = text.size(); |
| 4016 | for (int i = 0; i < text_size; i++) { |
| 4017 | ret_text += text[i]; |
| 4018 | if (i != text_size - 1) { |
| 4019 | ret_text += "\n"; |
| 4020 | } |
| 4021 | } |
| 4022 | return ret_text.as_string(); |
| 4023 | } |
| 4024 | |
| 4025 | int TextEdit::get_line_count() const { |
| 4026 | return text.size(); |
| 4027 | } |
| 4028 | |
| 4029 | void TextEdit::set_placeholder(const String &p_text) { |
| 4030 | if (placeholder_text == p_text) { |
| 4031 | return; |
| 4032 | } |
| 4033 | |
| 4034 | placeholder_text = p_text; |
| 4035 | _update_placeholder(); |
| 4036 | queue_accessibility_update(); |
| 4037 | queue_redraw(); |
| 4038 | } |
| 4039 | |
| 4040 | String TextEdit::get_placeholder() const { |
| 4041 | return placeholder_text; |
| 4042 | } |
| 4043 | |
| 4044 | void TextEdit::set_line(int p_line, const String &p_new_text) { |
| 4045 | if (p_line < 0 || p_line >= text.size()) { |
| 4046 | return; |
| 4047 | } |
| 4048 | begin_complex_operation(); |
| 4049 | |
| 4050 | int old_column = text[p_line].length(); |
| 4051 | |
| 4052 | // Set the affected carets column to update their last offset x. |
| 4053 | for (int i = 0; i < get_caret_count(); i++) { |
| 4054 | if (_is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_line, 0, p_line, old_column)) { |
| 4055 | set_caret_column(get_caret_column(i), false, i); |
| 4056 | } |
| 4057 | if (has_selection(i) && _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_line, 0, p_line, old_column)) { |
| 4058 | set_selection_origin_column(get_selection_origin_column(i), i); |
| 4059 | } |
| 4060 | } |
| 4061 | |
| 4062 | _remove_text(p_line, 0, p_line, old_column); |
| 4063 | int new_line, new_column; |
| 4064 | _insert_text(p_line, 0, p_new_text, &new_line, &new_column); |
| 4065 | |
| 4066 | // Don't offset carets that were on the old line. |
| 4067 | _offset_carets_after(p_line, old_column, new_line, new_column, false, false); |
| 4068 | |
| 4069 | // Set the caret lines to update the column to match visually. |
| 4070 | for (int i = 0; i < get_caret_count(); i++) { |
| 4071 | if (_is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_line, 0, p_line, old_column)) { |
| 4072 | set_caret_line(get_caret_line(i), false, true, 0, i); |
| 4073 | } |
| 4074 | if (has_selection(i) && _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_line, 0, p_line, old_column)) { |
| 4075 | set_selection_origin_line(get_selection_origin_line(i), true, 0, i); |
| 4076 | } |
| 4077 | } |
| 4078 | merge_overlapping_carets(); |
| 4079 | end_complex_operation(); |
| 4080 | } |
| 4081 | |
| 4082 | String TextEdit::get_line(int p_line) const { |
| 4083 | if (p_line < 0 || p_line >= text.size()) { |
| 4084 | return String(); |
| 4085 | } |
| 4086 | return text[p_line]; |
| 4087 | } |
| 4088 | |
| 4089 | String TextEdit::get_line_with_ime(int p_line) const { |
| 4090 | if (p_line < 0 || p_line >= text.size()) { |
| 4091 | return String(); |
| 4092 | } |
| 4093 | return text.get_text_with_ime(p_line); |
| 4094 | } |
| 4095 | |
| 4096 | int TextEdit::get_line_width(int p_line, int p_wrap_index) const { |
| 4097 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 4098 | ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0); |
| 4099 | |
| 4100 | return text.get_line_width(p_line, p_wrap_index); |
| 4101 | } |
| 4102 | |
| 4103 | int TextEdit::get_line_height() const { |
| 4104 | return MAX(text.get_line_height() + theme_cache.line_spacing, 1); |
| 4105 | } |
| 4106 | |
| 4107 | int TextEdit::_get_wrapped_indent_level(int p_line, int &r_first_wrap) const { |
| 4108 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 4109 | |
| 4110 | const Vector<Vector2i> wr = text.get_line_wrap_ranges(p_line); |
| 4111 | r_first_wrap = 0; |
| 4112 | |
| 4113 | int tab_count = 0; |
| 4114 | int whitespace_count = 0; |
| 4115 | int line_length = text[p_line].size(); |
| 4116 | for (int i = 0; i < line_length - 1; i++) { |
| 4117 | if (r_first_wrap < wr.size() && i >= wr[r_first_wrap].y) { |
| 4118 | tab_count = 0; |
| 4119 | whitespace_count = 0; |
| 4120 | r_first_wrap++; |
| 4121 | } |
| 4122 | if (text[p_line][i] == '\t') { |
| 4123 | tab_count++; |
| 4124 | } else if (text[p_line][i] == ' ') { |
| 4125 | whitespace_count++; |
| 4126 | } else { |
| 4127 | break; |
| 4128 | } |
| 4129 | } |
| 4130 | return tab_count * text.get_tab_size() + whitespace_count; |
| 4131 | } |
| 4132 | |
| 4133 | float TextEdit::_get_wrap_indent_offset(int p_line, int p_wrap_index, bool p_rtl) const { |
| 4134 | if (!text.is_indent_wrapped_lines()) { |
| 4135 | return 0; |
| 4136 | } |
| 4137 | int first_indent_line = 0; |
| 4138 | _get_wrapped_indent_level(p_line, first_indent_line); |
| 4139 | if (p_wrap_index > first_indent_line) { |
| 4140 | return MIN(text.get_indent_offset(p_line, p_rtl), wrap_at_column * 0.6); |
| 4141 | } |
| 4142 | return 0; |
| 4143 | } |
| 4144 | |
| 4145 | int TextEdit::get_indent_level(int p_line) const { |
| 4146 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 4147 | |
| 4148 | int tab_count = 0; |
| 4149 | int whitespace_count = 0; |
| 4150 | int line_length = text[p_line].size(); |
| 4151 | for (int i = 0; i < line_length - 1; i++) { |
| 4152 | if (text[p_line][i] == '\t') { |
| 4153 | tab_count++; |
| 4154 | } else if (text[p_line][i] == ' ') { |
| 4155 | whitespace_count++; |
| 4156 | } else { |
| 4157 | break; |
| 4158 | } |
| 4159 | } |
| 4160 | return tab_count * text.get_tab_size() + whitespace_count; |
| 4161 | } |
| 4162 | |
| 4163 | int TextEdit::get_first_non_whitespace_column(int p_line) const { |
| 4164 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 4165 | |
| 4166 | int col = 0; |
| 4167 | while (col < text[p_line].length() && is_whitespace(text[p_line][col])) { |
| 4168 | col++; |
| 4169 | } |
| 4170 | return col; |
| 4171 | } |
| 4172 | |
| 4173 | void TextEdit::swap_lines(int p_from_line, int p_to_line) { |
| 4174 | ERR_FAIL_INDEX(p_from_line, text.size()); |
| 4175 | ERR_FAIL_INDEX(p_to_line, text.size()); |
| 4176 | |
| 4177 | if (p_from_line == p_to_line) { |
| 4178 | return; |
| 4179 | } |
| 4180 | |
| 4181 | String from_line_text = get_line(p_from_line); |
| 4182 | String to_line_text = get_line(p_to_line); |
| 4183 | begin_complex_operation(); |
| 4184 | begin_multicaret_edit(); |
| 4185 | // Don't use set_line to avoid clamping and updating carets. |
| 4186 | _remove_text(p_to_line, 0, p_to_line, text[p_to_line].length()); |
| 4187 | _insert_text(p_to_line, 0, from_line_text); |
| 4188 | _remove_text(p_from_line, 0, p_from_line, text[p_from_line].length()); |
| 4189 | _insert_text(p_from_line, 0, to_line_text); |
| 4190 | |
| 4191 | // Swap carets. |
| 4192 | for (int i = 0; i < get_caret_count(); i++) { |
| 4193 | bool selected = has_selection(i); |
| 4194 | if (get_caret_line(i) == p_from_line || get_caret_line(i) == p_to_line) { |
| 4195 | int caret_new_line = get_caret_line(i) == p_from_line ? p_to_line : p_from_line; |
| 4196 | int caret_column = get_caret_column(i); |
| 4197 | set_caret_line(caret_new_line, false, true, -1, i); |
| 4198 | set_caret_column(caret_column, false, i); |
| 4199 | } |
| 4200 | if (selected && (get_selection_origin_line(i) == p_from_line || get_selection_origin_line(i) == p_to_line)) { |
| 4201 | int origin_new_line = get_selection_origin_line(i) == p_from_line ? p_to_line : p_from_line; |
| 4202 | int origin_column = get_selection_origin_column(i); |
| 4203 | select(origin_new_line, origin_column, get_caret_line(i), get_caret_column(i), i); |
| 4204 | } |
| 4205 | } |
| 4206 | // If only part of a selection was changed, it may now overlap. |
| 4207 | merge_overlapping_carets(); |
| 4208 | |
| 4209 | end_multicaret_edit(); |
| 4210 | end_complex_operation(); |
| 4211 | } |
| 4212 | |
| 4213 | void TextEdit::insert_line_at(int p_line, const String &p_text) { |
| 4214 | ERR_FAIL_INDEX(p_line, text.size()); |
| 4215 | |
| 4216 | // Use a complex operation so subsequent calls aren't merged together. |
| 4217 | begin_complex_operation(); |
| 4218 | |
| 4219 | int new_line, new_column; |
| 4220 | _insert_text(p_line, 0, p_text + "\n", &new_line, &new_column); |
| 4221 | _offset_carets_after(p_line, 0, new_line, new_column); |
| 4222 | |
| 4223 | end_complex_operation(); |
| 4224 | } |
| 4225 | |
| 4226 | void TextEdit::remove_line_at(int p_line, bool p_move_carets_down) { |
| 4227 | ERR_FAIL_INDEX(p_line, text.size()); |
| 4228 | |
| 4229 | if (get_line_count() == 1) { |
| 4230 | // Only one line, just remove contents. |
| 4231 | begin_complex_operation(); |
| 4232 | int line_length = get_line(p_line).length(); |
| 4233 | _remove_text(p_line, 0, p_line, line_length); |
| 4234 | collapse_carets(p_line, 0, p_line, line_length, true); |
| 4235 | end_complex_operation(); |
| 4236 | return; |
| 4237 | } |
| 4238 | |
| 4239 | begin_complex_operation(); |
| 4240 | |
| 4241 | bool is_last_line = p_line == get_line_count() - 1; |
| 4242 | int from_line = is_last_line ? p_line - 1 : p_line; |
| 4243 | int next_line = is_last_line ? p_line : p_line + 1; |
| 4244 | int from_column = is_last_line ? get_line(from_line).length() : 0; |
| 4245 | int next_column = is_last_line ? get_line(next_line).length() : 0; |
| 4246 | |
| 4247 | if ((!is_last_line && p_move_carets_down) || (p_line != 0 && !p_move_carets_down)) { |
| 4248 | // Set the carets column to update their last offset x. |
| 4249 | for (int i = 0; i < get_caret_count(); i++) { |
| 4250 | if (get_caret_line(i) == p_line) { |
| 4251 | set_caret_column(get_caret_column(i), false, i); |
| 4252 | } |
| 4253 | if (has_selection(i) && get_selection_origin_line(i) == p_line) { |
| 4254 | set_selection_origin_column(get_selection_origin_column(i), i); |
| 4255 | } |
| 4256 | } |
| 4257 | } |
| 4258 | |
| 4259 | // Remove line. |
| 4260 | _remove_text(from_line, from_column, next_line, next_column); |
| 4261 | |
| 4262 | begin_multicaret_edit(); |
| 4263 | if ((is_last_line && p_move_carets_down) || (p_line == 0 && !p_move_carets_down)) { |
| 4264 | // Collapse carets. |
| 4265 | collapse_carets(from_line, from_column, next_line, next_column, true); |
| 4266 | } else { |
| 4267 | // Move carets to visually line up. |
| 4268 | int target_line = p_move_carets_down ? p_line : p_line - 1; |
| 4269 | for (int i = 0; i < get_caret_count(); i++) { |
| 4270 | bool selected = has_selection(i); |
| 4271 | if (get_caret_line(i) == p_line) { |
| 4272 | set_caret_line(target_line, i == 0, true, 0, i); |
| 4273 | } |
| 4274 | if (selected && get_selection_origin_line(i) == p_line) { |
| 4275 | set_selection_origin_line(target_line, true, 0, i); |
| 4276 | select(get_selection_origin_line(i), get_selection_origin_column(i), get_caret_line(i), get_caret_column(i), i); |
| 4277 | } |
| 4278 | } |
| 4279 | |
| 4280 | merge_overlapping_carets(); |
| 4281 | } |
| 4282 | _offset_carets_after(next_line, next_column, from_line, from_column); |
| 4283 | end_multicaret_edit(); |
| 4284 | end_complex_operation(); |
| 4285 | |
| 4286 | queue_accessibility_update(); |
| 4287 | queue_redraw(); |
| 4288 | } |
| 4289 | |
| 4290 | void TextEdit::insert_text_at_caret(const String &p_text, int p_caret) { |
| 4291 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 4292 | |
| 4293 | begin_complex_operation(); |
| 4294 | begin_multicaret_edit(); |
| 4295 | for (int i = 0; i < get_caret_count(); i++) { |
| 4296 | if (p_caret != -1 && p_caret != i) { |
| 4297 | continue; |
| 4298 | } |
| 4299 | if (p_caret == -1 && multicaret_edit_ignore_caret(i)) { |
| 4300 | continue; |
| 4301 | } |
| 4302 | |
| 4303 | delete_selection(i); |
| 4304 | |
| 4305 | int from_line = get_caret_line(i); |
| 4306 | int from_col = get_caret_column(i); |
| 4307 | |
| 4308 | int new_line, new_column; |
| 4309 | _insert_text(from_line, from_col, p_text, &new_line, &new_column); |
| 4310 | _update_scrollbars(); |
| 4311 | _offset_carets_after(from_line, from_col, new_line, new_column); |
| 4312 | |
| 4313 | set_caret_line(new_line, false, true, -1, i); |
| 4314 | set_caret_column(new_column, i == 0, i); |
| 4315 | } |
| 4316 | |
| 4317 | if (has_ime_text()) { |
| 4318 | _update_ime_text(); |
| 4319 | } |
| 4320 | |
| 4321 | end_multicaret_edit(); |
| 4322 | end_complex_operation(); |
| 4323 | } |
| 4324 | |
| 4325 | void TextEdit::insert_text(const String &p_text, int p_line, int p_column, bool p_before_selection_begin, bool p_before_selection_end) { |
| 4326 | ERR_FAIL_INDEX(p_line, text.size()); |
| 4327 | ERR_FAIL_INDEX(p_column, text[p_line].length() + 1); |
| 4328 | |
| 4329 | begin_complex_operation(); |
| 4330 | |
| 4331 | int new_line, new_column; |
| 4332 | _insert_text(p_line, p_column, p_text, &new_line, &new_column); |
| 4333 | |
| 4334 | _offset_carets_after(p_line, p_column, new_line, new_column, p_before_selection_begin, p_before_selection_end); |
| 4335 | |
| 4336 | end_complex_operation(); |
| 4337 | } |
| 4338 | |
| 4339 | void TextEdit::remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { |
| 4340 | ERR_FAIL_INDEX(p_from_line, text.size()); |
| 4341 | ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1); |
| 4342 | ERR_FAIL_INDEX(p_to_line, text.size()); |
| 4343 | ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1); |
| 4344 | ERR_FAIL_COND(p_to_line < p_from_line); |
| 4345 | ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); |
| 4346 | |
| 4347 | begin_complex_operation(); |
| 4348 | |
| 4349 | _remove_text(p_from_line, p_from_column, p_to_line, p_to_column); |
| 4350 | collapse_carets(p_from_line, p_from_column, p_to_line, p_to_column); |
| 4351 | _offset_carets_after(p_to_line, p_to_column, p_from_line, p_from_column); |
| 4352 | |
| 4353 | end_complex_operation(); |
| 4354 | } |
| 4355 | |
| 4356 | int TextEdit::get_last_unhidden_line() const { |
| 4357 | // Returns the last line in the text that is not hidden. |
| 4358 | if (!_is_hiding_enabled()) { |
| 4359 | return text.size() - 1; |
| 4360 | } |
| 4361 | |
| 4362 | int last_line; |
| 4363 | for (last_line = text.size() - 1; last_line > 0; last_line--) { |
| 4364 | if (!_is_line_hidden(last_line)) { |
| 4365 | break; |
| 4366 | } |
| 4367 | } |
| 4368 | return last_line; |
| 4369 | } |
| 4370 | |
| 4371 | int TextEdit::get_next_visible_line_offset_from(int p_line_from, int p_visible_amount) const { |
| 4372 | // Returns the number of lines (hidden and unhidden) from p_line_from to (p_line_from + visible_amount of unhidden lines). |
| 4373 | ERR_FAIL_INDEX_V(p_line_from, text.size(), Math::abs(p_visible_amount)); |
| 4374 | |
| 4375 | if (!_is_hiding_enabled()) { |
| 4376 | return Math::abs(p_visible_amount); |
| 4377 | } |
| 4378 | |
| 4379 | int num_visible = 0; |
| 4380 | int num_total = 0; |
| 4381 | if (p_visible_amount >= 0) { |
| 4382 | for (int i = p_line_from; i < text.size(); i++) { |
| 4383 | num_total++; |
| 4384 | if (!_is_line_hidden(i)) { |
| 4385 | num_visible++; |
| 4386 | } |
| 4387 | if (num_visible >= p_visible_amount) { |
| 4388 | break; |
| 4389 | } |
| 4390 | } |
| 4391 | } else { |
| 4392 | p_visible_amount = Math::abs(p_visible_amount); |
| 4393 | for (int i = p_line_from; i >= 0; i--) { |
| 4394 | num_total++; |
| 4395 | if (!_is_line_hidden(i)) { |
| 4396 | num_visible++; |
| 4397 | } |
| 4398 | if (num_visible >= p_visible_amount) { |
| 4399 | break; |
| 4400 | } |
| 4401 | } |
| 4402 | } |
| 4403 | return num_total; |
| 4404 | } |
| 4405 | |
| 4406 | Point2i TextEdit::get_next_visible_line_index_offset_from(int p_line_from, int p_wrap_index_from, int p_visible_amount) const { |
| 4407 | // Returns the number of lines (hidden and unhidden) from (p_line_from + p_wrap_index_from) row to (p_line_from + visible_amount of unhidden and wrapped rows). |
| 4408 | // Wrap index is set to the wrap index of the last line. |
| 4409 | int wrap_index = 0; |
| 4410 | ERR_FAIL_INDEX_V(p_line_from, text.size(), Point2i(Math::abs(p_visible_amount), 0)); |
| 4411 | |
| 4412 | if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) { |
| 4413 | return Point2i(Math::abs(p_visible_amount), 0); |
| 4414 | } |
| 4415 | |
| 4416 | int num_visible = 0; |
| 4417 | int num_total = 0; |
| 4418 | if (p_visible_amount == 0) { |
| 4419 | num_total = 0; |
| 4420 | wrap_index = 0; |
| 4421 | } else if (p_visible_amount > 0) { |
| 4422 | int i; |
| 4423 | num_visible -= p_wrap_index_from; |
| 4424 | for (i = p_line_from; i < text.size(); i++) { |
| 4425 | num_total++; |
| 4426 | if (!_is_line_hidden(i)) { |
| 4427 | num_visible++; |
| 4428 | num_visible += get_line_wrap_count(i); |
| 4429 | } |
| 4430 | if (num_visible >= p_visible_amount) { |
| 4431 | break; |
| 4432 | } |
| 4433 | } |
| 4434 | wrap_index = get_line_wrap_count(MIN(i, text.size() - 1)) - MAX(0, num_visible - p_visible_amount); |
| 4435 | |
| 4436 | // If we are a hidden line, then we are the last line as we cannot reach "p_visible_amount". |
| 4437 | // This means we need to backtrack to get last visible line. |
| 4438 | // Currently, line 0 cannot be hidden so this should always be valid. |
| 4439 | int line = (p_line_from + num_total) - 1; |
| 4440 | if (_is_line_hidden(line)) { |
| 4441 | Point2i backtrack = get_next_visible_line_index_offset_from(line, 0, -1); |
| 4442 | num_total = num_total - (backtrack.x - 1); |
| 4443 | wrap_index = backtrack.y; |
| 4444 | } |
| 4445 | } else { |
| 4446 | p_visible_amount = Math::abs(p_visible_amount); |
| 4447 | int i; |
| 4448 | num_visible -= get_line_wrap_count(p_line_from) - p_wrap_index_from; |
| 4449 | for (i = p_line_from; i >= 0; i--) { |
| 4450 | num_total++; |
| 4451 | if (!_is_line_hidden(i)) { |
| 4452 | num_visible++; |
| 4453 | num_visible += get_line_wrap_count(i); |
| 4454 | } |
| 4455 | if (num_visible >= p_visible_amount) { |
| 4456 | break; |
| 4457 | } |
| 4458 | } |
| 4459 | wrap_index = MAX(0, num_visible - p_visible_amount); |
| 4460 | } |
| 4461 | wrap_index = MAX(wrap_index, 0); |
| 4462 | return Point2i(num_total, wrap_index); |
| 4463 | } |
| 4464 | |
| 4465 | void TextEdit::set_inline_object_handlers(const Callable &p_parser, const Callable &p_drawer, const Callable &p_click_handler) { |
| 4466 | inline_object_drawer = p_drawer; |
| 4467 | inline_object_click_handler = p_click_handler; |
| 4468 | text.set_inline_object_parser(p_parser); |
| 4469 | } |
| 4470 | |
| 4471 | // Overridable actions |
| 4472 | void TextEdit::handle_unicode_input(const uint32_t p_unicode, int p_caret) { |
| 4473 | if (GDVIRTUAL_CALL(_handle_unicode_input, p_unicode, p_caret)) { |
| 4474 | return; |
| 4475 | } |
| 4476 | _handle_unicode_input_internal(p_unicode, p_caret); |
| 4477 | } |
| 4478 | |
| 4479 | void TextEdit::backspace(int p_caret) { |
| 4480 | if (GDVIRTUAL_CALL(_backspace, p_caret)) { |
| 4481 | return; |
| 4482 | } |
| 4483 | _backspace_internal(p_caret); |
| 4484 | } |
| 4485 | |
| 4486 | void TextEdit::cut(int p_caret) { |
| 4487 | if (GDVIRTUAL_CALL(_cut, p_caret)) { |
| 4488 | return; |
| 4489 | } |
| 4490 | _cut_internal(p_caret); |
| 4491 | } |
| 4492 | |
| 4493 | void TextEdit::copy(int p_caret) { |
| 4494 | if (GDVIRTUAL_CALL(_copy, p_caret)) { |
| 4495 | return; |
| 4496 | } |
| 4497 | _copy_internal(p_caret); |
| 4498 | } |
| 4499 | |
| 4500 | void TextEdit::paste(int p_caret) { |
| 4501 | if (GDVIRTUAL_CALL(_paste, p_caret)) { |
| 4502 | return; |
| 4503 | } |
| 4504 | _paste_internal(p_caret); |
| 4505 | } |
| 4506 | |
| 4507 | void TextEdit::paste_primary_clipboard(int p_caret) { |
| 4508 | if (GDVIRTUAL_CALL(_paste_primary_clipboard, p_caret)) { |
| 4509 | return; |
| 4510 | } |
| 4511 | _paste_primary_clipboard_internal(p_caret); |
| 4512 | } |
| 4513 | |
| 4514 | // Context menu. |
| 4515 | PopupMenu *TextEdit::get_menu() const { |
| 4516 | if (!menu) { |
| 4517 | const_cast<TextEdit *>(this)->_generate_context_menu(); |
| 4518 | } |
| 4519 | return menu; |
| 4520 | } |
| 4521 | |
| 4522 | bool TextEdit::is_menu_visible() const { |
| 4523 | return menu && menu->is_visible(); |
| 4524 | } |
| 4525 | |
| 4526 | void TextEdit::menu_option(int p_option) { |
| 4527 | switch (p_option) { |
| 4528 | case MENU_CUT: { |
| 4529 | cut(); |
| 4530 | } break; |
| 4531 | case MENU_COPY: { |
| 4532 | copy(); |
| 4533 | } break; |
| 4534 | case MENU_PASTE: { |
| 4535 | paste(); |
| 4536 | } break; |
| 4537 | case MENU_CLEAR: { |
| 4538 | if (editable) { |
| 4539 | clear(); |
| 4540 | } |
| 4541 | } break; |
| 4542 | case MENU_SELECT_ALL: { |
| 4543 | select_all(); |
| 4544 | } break; |
| 4545 | case MENU_UNDO: { |
| 4546 | undo(); |
| 4547 | } break; |
| 4548 | case MENU_REDO: { |
| 4549 | redo(); |
| 4550 | } break; |
| 4551 | case MENU_DIR_INHERITED: { |
| 4552 | set_text_direction(TEXT_DIRECTION_INHERITED); |
| 4553 | } break; |
| 4554 | case MENU_DIR_AUTO: { |
| 4555 | set_text_direction(TEXT_DIRECTION_AUTO); |
| 4556 | } break; |
| 4557 | case MENU_DIR_LTR: { |
| 4558 | set_text_direction(TEXT_DIRECTION_LTR); |
| 4559 | } break; |
| 4560 | case MENU_DIR_RTL: { |
| 4561 | set_text_direction(TEXT_DIRECTION_RTL); |
| 4562 | } break; |
| 4563 | case MENU_DISPLAY_UCC: { |
| 4564 | set_draw_control_chars(!get_draw_control_chars()); |
| 4565 | } break; |
| 4566 | case MENU_INSERT_LRM: { |
| 4567 | if (editable) { |
| 4568 | insert_text_at_caret(String::chr(0x200E)); |
| 4569 | } |
| 4570 | } break; |
| 4571 | case MENU_INSERT_RLM: { |
| 4572 | if (editable) { |
| 4573 | insert_text_at_caret(String::chr(0x200F)); |
| 4574 | } |
| 4575 | } break; |
| 4576 | case MENU_INSERT_LRE: { |
| 4577 | if (editable) { |
| 4578 | insert_text_at_caret(String::chr(0x202A)); |
| 4579 | } |
| 4580 | } break; |
| 4581 | case MENU_INSERT_RLE: { |
| 4582 | if (editable) { |
| 4583 | insert_text_at_caret(String::chr(0x202B)); |
| 4584 | } |
| 4585 | } break; |
| 4586 | case MENU_INSERT_LRO: { |
| 4587 | if (editable) { |
| 4588 | insert_text_at_caret(String::chr(0x202D)); |
| 4589 | } |
| 4590 | } break; |
| 4591 | case MENU_INSERT_RLO: { |
| 4592 | if (editable) { |
| 4593 | insert_text_at_caret(String::chr(0x202E)); |
| 4594 | } |
| 4595 | } break; |
| 4596 | case MENU_INSERT_PDF: { |
| 4597 | if (editable) { |
| 4598 | insert_text_at_caret(String::chr(0x202C)); |
| 4599 | } |
| 4600 | } break; |
| 4601 | case MENU_INSERT_ALM: { |
| 4602 | if (editable) { |
| 4603 | insert_text_at_caret(String::chr(0x061C)); |
| 4604 | } |
| 4605 | } break; |
| 4606 | case MENU_INSERT_LRI: { |
| 4607 | if (editable) { |
| 4608 | insert_text_at_caret(String::chr(0x2066)); |
| 4609 | } |
| 4610 | } break; |
| 4611 | case MENU_INSERT_RLI: { |
| 4612 | if (editable) { |
| 4613 | insert_text_at_caret(String::chr(0x2067)); |
| 4614 | } |
| 4615 | } break; |
| 4616 | case MENU_INSERT_FSI: { |
| 4617 | if (editable) { |
| 4618 | insert_text_at_caret(String::chr(0x2068)); |
| 4619 | } |
| 4620 | } break; |
| 4621 | case MENU_INSERT_PDI: { |
| 4622 | if (editable) { |
| 4623 | insert_text_at_caret(String::chr(0x2069)); |
| 4624 | } |
| 4625 | } break; |
| 4626 | case MENU_INSERT_ZWJ: { |
| 4627 | if (editable) { |
| 4628 | insert_text_at_caret(String::chr(0x200D)); |
| 4629 | } |
| 4630 | } break; |
| 4631 | case MENU_INSERT_ZWNJ: { |
| 4632 | if (editable) { |
| 4633 | insert_text_at_caret(String::chr(0x200C)); |
| 4634 | } |
| 4635 | } break; |
| 4636 | case MENU_INSERT_WJ: { |
| 4637 | if (editable) { |
| 4638 | insert_text_at_caret(String::chr(0x2060)); |
| 4639 | } |
| 4640 | } break; |
| 4641 | case MENU_INSERT_SHY: { |
| 4642 | if (editable) { |
| 4643 | insert_text_at_caret(String::chr(0x00AD)); |
| 4644 | } |
| 4645 | } break; |
| 4646 | case MENU_EMOJI_AND_SYMBOL: { |
| 4647 | show_emoji_and_symbol_picker(); |
| 4648 | } break; |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | /* Versioning */ |
| 4653 | void TextEdit::start_action(EditAction p_action) { |
| 4654 | if (current_action != p_action) { |
| 4655 | if (current_action != EditAction::ACTION_NONE) { |
| 4656 | in_action = false; |
| 4657 | pending_action_end = false; |
| 4658 | end_complex_operation(); |
| 4659 | } |
| 4660 | |
| 4661 | if (p_action != EditAction::ACTION_NONE) { |
| 4662 | in_action = true; |
| 4663 | begin_complex_operation(); |
| 4664 | } |
| 4665 | } else if (current_action != EditAction::ACTION_NONE) { |
| 4666 | pending_action_end = false; |
| 4667 | } |
| 4668 | current_action = p_action; |
| 4669 | } |
| 4670 | |
| 4671 | void TextEdit::end_action() { |
| 4672 | if (current_action != EditAction::ACTION_NONE) { |
| 4673 | pending_action_end = true; |
| 4674 | queue_accessibility_update(); |
| 4675 | } |
| 4676 | } |
| 4677 | |
| 4678 | TextEdit::EditAction TextEdit::get_current_action() const { |
| 4679 | return current_action; |
| 4680 | } |
| 4681 | |
| 4682 | void TextEdit::begin_complex_operation() { |
| 4683 | _push_current_op(); |
| 4684 | if (complex_operation_count == 0) { |
| 4685 | next_operation_is_complex = true; |
| 4686 | current_op.start_carets = carets; |
| 4687 | } |
| 4688 | complex_operation_count++; |
| 4689 | } |
| 4690 | |
| 4691 | void TextEdit::end_complex_operation() { |
| 4692 | _push_current_op(); |
| 4693 | |
| 4694 | queue_accessibility_update(); |
| 4695 | |
| 4696 | complex_operation_count = MAX(complex_operation_count - 1, 0); |
| 4697 | if (complex_operation_count > 0) { |
| 4698 | return; |
| 4699 | } |
| 4700 | if (undo_stack.is_empty()) { |
| 4701 | return; |
| 4702 | } |
| 4703 | |
| 4704 | undo_stack.back()->get().end_carets = carets; |
| 4705 | if (undo_stack.back()->get().chain_forward) { |
| 4706 | undo_stack.back()->get().chain_forward = false; |
| 4707 | return; |
| 4708 | } |
| 4709 | |
| 4710 | undo_stack.back()->get().chain_backward = true; |
| 4711 | } |
| 4712 | |
| 4713 | bool TextEdit::has_undo() const { |
| 4714 | if (undo_stack_pos == nullptr) { |
| 4715 | int pending = current_op.type == TextOperation::TYPE_NONE ? 0 : 1; |
| 4716 | return undo_stack.size() + pending > 0; |
| 4717 | } |
| 4718 | return undo_stack_pos != undo_stack.front(); |
| 4719 | } |
| 4720 | |
| 4721 | bool TextEdit::has_redo() const { |
| 4722 | return undo_stack_pos != nullptr; |
| 4723 | } |
| 4724 | |
| 4725 | void TextEdit::undo() { |
| 4726 | if (!editable) { |
| 4727 | return; |
| 4728 | } |
| 4729 | |
| 4730 | if (in_action) { |
| 4731 | pending_action_end = true; |
| 4732 | } |
| 4733 | _push_current_op(); |
| 4734 | |
| 4735 | if (undo_stack_pos == nullptr) { |
| 4736 | if (undo_stack.is_empty()) { |
| 4737 | return; // Nothing to undo. |
| 4738 | } |
| 4739 | |
| 4740 | undo_stack_pos = undo_stack.back(); |
| 4741 | |
| 4742 | } else if (undo_stack_pos == undo_stack.front()) { |
| 4743 | return; // At the bottom of the undo stack. |
| 4744 | } else { |
| 4745 | undo_stack_pos = undo_stack_pos->prev(); |
| 4746 | } |
| 4747 | |
| 4748 | deselect(); |
| 4749 | |
| 4750 | TextOperation op = undo_stack_pos->get(); |
| 4751 | _do_text_op(op, true); |
| 4752 | |
| 4753 | current_op.version = op.prev_version; |
| 4754 | if (undo_stack_pos->get().chain_backward) { |
| 4755 | // This was part of a complex operation, undo until the chain forward at the start of the complex operation. |
| 4756 | while (true) { |
| 4757 | ERR_BREAK(!undo_stack_pos->prev()); |
| 4758 | undo_stack_pos = undo_stack_pos->prev(); |
| 4759 | op = undo_stack_pos->get(); |
| 4760 | _do_text_op(op, true); |
| 4761 | current_op.version = op.prev_version; |
| 4762 | if (undo_stack_pos->get().chain_forward) { |
| 4763 | break; |
| 4764 | } |
| 4765 | } |
| 4766 | } |
| 4767 | |
| 4768 | _update_scrollbars(); |
| 4769 | bool dirty_carets = get_caret_count() != undo_stack_pos->get().start_carets.size(); |
| 4770 | if (!dirty_carets) { |
| 4771 | for (int i = 0; i < get_caret_count(); i++) { |
| 4772 | if (carets[i].line != undo_stack_pos->get().start_carets[i].line || carets[i].column != undo_stack_pos->get().start_carets[i].column) { |
| 4773 | dirty_carets = true; |
| 4774 | break; |
| 4775 | } |
| 4776 | } |
| 4777 | } |
| 4778 | |
| 4779 | carets = undo_stack_pos->get().start_carets; |
| 4780 | |
| 4781 | _unhide_carets(); |
| 4782 | |
| 4783 | if (dirty_carets) { |
| 4784 | _caret_changed(); |
| 4785 | _selection_changed(); |
| 4786 | } |
| 4787 | adjust_viewport_to_caret(); |
| 4788 | queue_accessibility_update(); |
| 4789 | } |
| 4790 | |
| 4791 | void TextEdit::redo() { |
| 4792 | if (!editable) { |
| 4793 | return; |
| 4794 | } |
| 4795 | |
| 4796 | if (in_action) { |
| 4797 | pending_action_end = true; |
| 4798 | } |
| 4799 | _push_current_op(); |
| 4800 | |
| 4801 | if (!has_redo()) { |
| 4802 | return; // Nothing to do. |
| 4803 | } |
| 4804 | |
| 4805 | deselect(); |
| 4806 | |
| 4807 | TextOperation op = undo_stack_pos->get(); |
| 4808 | _do_text_op(op, false); |
| 4809 | current_op.version = op.version; |
| 4810 | if (undo_stack_pos->get().chain_forward) { |
| 4811 | // This was part of a complex operation, redo until the chain backward at the end of the complex operation. |
| 4812 | while (true) { |
| 4813 | ERR_BREAK(!undo_stack_pos->next()); |
| 4814 | undo_stack_pos = undo_stack_pos->next(); |
| 4815 | op = undo_stack_pos->get(); |
| 4816 | _do_text_op(op, false); |
| 4817 | current_op.version = op.version; |
| 4818 | if (undo_stack_pos->get().chain_backward) { |
| 4819 | break; |
| 4820 | } |
| 4821 | } |
| 4822 | } |
| 4823 | |
| 4824 | _update_scrollbars(); |
| 4825 | bool dirty_carets = get_caret_count() != undo_stack_pos->get().end_carets.size(); |
| 4826 | if (!dirty_carets) { |
| 4827 | for (int i = 0; i < get_caret_count(); i++) { |
| 4828 | if (carets[i].line != undo_stack_pos->get().end_carets[i].line || carets[i].column != undo_stack_pos->get().end_carets[i].column) { |
| 4829 | dirty_carets = true; |
| 4830 | break; |
| 4831 | } |
| 4832 | } |
| 4833 | } |
| 4834 | |
| 4835 | carets = undo_stack_pos->get().end_carets; |
| 4836 | undo_stack_pos = undo_stack_pos->next(); |
| 4837 | |
| 4838 | _unhide_carets(); |
| 4839 | |
| 4840 | if (dirty_carets) { |
| 4841 | _caret_changed(); |
| 4842 | _selection_changed(); |
| 4843 | } |
| 4844 | adjust_viewport_to_caret(); |
| 4845 | queue_accessibility_update(); |
| 4846 | } |
| 4847 | |
| 4848 | void TextEdit::clear_undo_history() { |
| 4849 | saved_version = 0; |
| 4850 | current_op.type = TextOperation::TYPE_NONE; |
| 4851 | undo_stack_pos = nullptr; |
| 4852 | undo_stack.clear(); |
| 4853 | } |
| 4854 | |
| 4855 | bool TextEdit::is_insert_text_operation() const { |
| 4856 | return (current_op.type == TextOperation::TYPE_INSERT || current_action == EditAction::ACTION_TYPING); |
| 4857 | } |
| 4858 | |
| 4859 | void TextEdit::tag_saved_version() { |
| 4860 | saved_version = get_version(); |
| 4861 | } |
| 4862 | |
| 4863 | uint32_t TextEdit::get_version() const { |
| 4864 | return current_op.version; |
| 4865 | } |
| 4866 | |
| 4867 | uint32_t TextEdit::get_saved_version() const { |
| 4868 | return saved_version; |
| 4869 | } |
| 4870 | |
| 4871 | /* Search */ |
| 4872 | void TextEdit::set_search_text(const String &p_search_text) { |
| 4873 | search_text = p_search_text; |
| 4874 | } |
| 4875 | |
| 4876 | void TextEdit::set_search_flags(uint32_t p_flags) { |
| 4877 | search_flags = p_flags; |
| 4878 | } |
| 4879 | |
| 4880 | Point2i TextEdit::search(const String &p_key, uint32_t p_search_flags, int p_from_line, int p_from_column) const { |
| 4881 | if (p_key.is_empty()) { |
| 4882 | return Point2(-1, -1); |
| 4883 | } |
| 4884 | ERR_FAIL_INDEX_V(p_from_line, text.size(), Point2i(-1, -1)); |
| 4885 | ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, Point2i(-1, -1)); |
| 4886 | |
| 4887 | const bool key_start_is_symbol = is_symbol(p_key[0]); |
| 4888 | const bool key_end_is_symbol = is_symbol(p_key[p_key.length() - 1]); |
| 4889 | |
| 4890 | // Search the whole document, starting from the current line. |
| 4891 | // We'll auto-wrap through the start / end to search every line. |
| 4892 | int current_line = p_from_line; |
| 4893 | int current_column = p_from_column; |
| 4894 | |
| 4895 | // + 1 because we'll search p_from_line twice - starting from p_from_column, and then again at the very end. |
| 4896 | for (int i = 0; i < text.size() + 1; i++) { |
| 4897 | const String &text_line = text[current_line]; |
| 4898 | |
| 4899 | // Search the current line as often as necessary. |
| 4900 | while (true) { |
| 4901 | if (p_search_flags & SEARCH_BACKWARDS) { |
| 4902 | current_column = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.rfind(p_key, current_column) : text_line.rfindn(p_key, current_column); |
| 4903 | } else { |
| 4904 | current_column = (p_search_flags & SEARCH_MATCH_CASE) ? text_line.find(p_key, current_column) : text_line.findn(p_key, current_column); |
| 4905 | } |
| 4906 | |
| 4907 | if (current_column == -1) { |
| 4908 | break; // Nothing else found on the current line. |
| 4909 | } |
| 4910 | |
| 4911 | bool is_match = true; |
| 4912 | |
| 4913 | if (p_search_flags & SEARCH_WHOLE_WORDS) { |
| 4914 | // Validate for whole words. |
| 4915 | if (!key_start_is_symbol && current_column > 0 && !is_symbol(text_line[current_column - 1])) { |
| 4916 | is_match = false; |
| 4917 | } else if (!key_end_is_symbol && current_column + p_key.length() < text_line.length() && !is_symbol(text_line[current_column + p_key.length()])) { |
| 4918 | is_match = false; |
| 4919 | } |
| 4920 | } |
| 4921 | |
| 4922 | if (is_match) { |
| 4923 | // Found the string! |
| 4924 | return Point2i(current_column, current_line); |
| 4925 | } |
| 4926 | |
| 4927 | // Advance past the current occurrence. |
| 4928 | current_column += p_search_flags & SEARCH_BACKWARDS ? -1 : 1; |
| 4929 | } |
| 4930 | |
| 4931 | // Prepare for next iteration. |
| 4932 | if (p_search_flags & SEARCH_BACKWARDS) { |
| 4933 | current_column = -1; |
| 4934 | current_line--; |
| 4935 | if (current_line < 0) { |
| 4936 | // Searched the whole document backwards; wrap to end. |
| 4937 | current_line = text.size() - 1; |
| 4938 | } |
| 4939 | } else { |
| 4940 | current_line++; |
| 4941 | current_column = 0; |
| 4942 | if (current_line == text.size()) { |
| 4943 | // Searched the whole document forwards; wrap to start. |
| 4944 | current_line = 0; |
| 4945 | } |
| 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | // Nothing found! |
| 4950 | return Point2i(-1, -1); |
| 4951 | } |
| 4952 | |
| 4953 | /* Mouse */ |
| 4954 | Point2 TextEdit::get_local_mouse_pos() const { |
| 4955 | Point2 mp = get_local_mouse_position(); |
| 4956 | if (is_layout_rtl()) { |
| 4957 | mp.x = get_size().width - mp.x; |
| 4958 | } |
| 4959 | return mp; |
| 4960 | } |
| 4961 | |
| 4962 | String TextEdit::get_word_at_pos(const Vector2 &p_pos) const { |
| 4963 | Point2i pos = get_line_column_at_pos(p_pos, false, false); |
| 4964 | int line = pos.y; |
| 4965 | int col = pos.x; |
| 4966 | return get_word(line, col); |
| 4967 | } |
| 4968 | |
| 4969 | String TextEdit::get_word(int p_line, int p_column) const { |
| 4970 | if (p_line < 0 || p_column < 0) { |
| 4971 | return String(); |
| 4972 | } |
| 4973 | ERR_FAIL_INDEX_V(p_line, text.size(), String()); |
| 4974 | |
| 4975 | const String &text_line = text.get_text_with_ime(p_line); |
| 4976 | if (text_line.is_empty()) { |
| 4977 | return String(); |
| 4978 | } |
| 4979 | ERR_FAIL_INDEX_V(p_column, text_line.size() + 1, String()); |
| 4980 | |
| 4981 | const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(p_line)->get_rid()); |
| 4982 | for (int i = 0; i < words.size(); i = i + 2) { |
| 4983 | if (words[i] <= p_column && words[i + 1] >= p_column) { |
| 4984 | return text_line.substr(words[i], words[i + 1] - words[i]); |
| 4985 | } |
| 4986 | } |
| 4987 | return String(); |
| 4988 | } |
| 4989 | |
| 4990 | Point2i TextEdit::get_line_column_at_pos(const Point2i &p_pos, bool p_clamp_line, bool p_clamp_column) const { |
| 4991 | Ref<StyleBox> style = _get_current_stylebox(); |
| 4992 | float rows = p_pos.y - (style->get_margin(SIDE_TOP) + (theme_cache.line_spacing / 2)); |
| 4993 | rows /= get_line_height(); |
| 4994 | rows += _get_v_scroll_offset(); |
| 4995 | int first_vis_line = get_first_visible_line(); |
| 4996 | int row = first_vis_line + Math::floor(rows); |
| 4997 | int wrap_index = 0; |
| 4998 | |
| 4999 | if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) { |
| 5000 | Point2i f_ofs = get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows))); |
| 5001 | wrap_index = f_ofs.y; |
| 5002 | |
| 5003 | if (rows < 0) { |
| 5004 | row = first_vis_line - (f_ofs.x - 1); |
| 5005 | } else { |
| 5006 | row = first_vis_line + (f_ofs.x - 1); |
| 5007 | } |
| 5008 | } |
| 5009 | |
| 5010 | row = CLAMP(row, 0, text.size() - 1); |
| 5011 | |
| 5012 | int visible_lines = get_visible_line_count_in_range(first_vis_line, row); |
| 5013 | if (rows > visible_lines) { |
| 5014 | if (p_clamp_line) { |
| 5015 | return Point2i(text[row].length(), row); |
| 5016 | } |
| 5017 | return Point2i(-1, -1); |
| 5018 | } |
| 5019 | int colx = p_pos.x - (Math::ceil(style->get_margin(SIDE_LEFT)) + gutters_width + gutter_padding); |
| 5020 | colx += first_visible_col; |
| 5021 | |
| 5022 | RID text_rid = text.get_line_data(row)->get_line_rid(wrap_index); |
| 5023 | |
| 5024 | bool rtl = is_layout_rtl(); |
| 5025 | const float wrap_indent = _get_wrap_indent_offset(row, wrap_index, rtl); |
| 5026 | |
| 5027 | if (rtl) { |
| 5028 | colx = TS->shaped_text_get_size(text_rid).x - colx + wrap_indent; |
| 5029 | } else { |
| 5030 | colx -= wrap_indent; |
| 5031 | } |
| 5032 | |
| 5033 | if (!p_clamp_column && (colx < 0 || colx > TS->shaped_text_get_size(text_rid).x)) { |
| 5034 | return Point2i(-1, -1); |
| 5035 | } |
| 5036 | |
| 5037 | int col = TS->shaped_text_hit_test_position(text_rid, colx); |
| 5038 | if (col == -1) { |
| 5039 | return Point2i(-1, -1); |
| 5040 | } |
| 5041 | if (!caret_mid_grapheme_enabled) { |
| 5042 | col = TS->shaped_text_closest_character_pos(text_rid, col); |
| 5043 | } |
| 5044 | |
| 5045 | return Point2i(col, row); |
| 5046 | } |
| 5047 | |
| 5048 | Point2i TextEdit::get_pos_at_line_column(int p_line, int p_column) const { |
| 5049 | Rect2i rect = get_rect_at_line_column(p_line, p_column); |
| 5050 | return rect.position.x == -1 ? rect.position : rect.position + Vector2i(0, get_line_height()); |
| 5051 | } |
| 5052 | |
| 5053 | Rect2i TextEdit::get_rect_at_line_column(int p_line, int p_column) const { |
| 5054 | ERR_FAIL_INDEX_V(p_line, text.size(), Rect2i(-1, -1, 0, 0)); |
| 5055 | ERR_FAIL_COND_V(p_column < 0, Rect2i(-1, -1, 0, 0)); |
| 5056 | ERR_FAIL_COND_V(p_column > text[p_line].length(), Rect2i(-1, -1, 0, 0)); |
| 5057 | |
| 5058 | if (text.size() == 1 && text[0].is_empty()) { |
| 5059 | // The TextEdit is empty. |
| 5060 | return Rect2i(); |
| 5061 | } |
| 5062 | |
| 5063 | if (line_drawing_cache.is_empty() || !line_drawing_cache.has(p_line)) { |
| 5064 | // Line is not in the cache, which means it's outside of the viewing area. |
| 5065 | return Rect2i(-1, -1, 0, 0); |
| 5066 | } |
| 5067 | LineDrawingCache cache_entry = line_drawing_cache[p_line]; |
| 5068 | |
| 5069 | int wrap_index = get_line_wrap_index_at_column(p_line, p_column); |
| 5070 | if (wrap_index >= cache_entry.first_visible_chars.size()) { |
| 5071 | // Line seems to be wrapped beyond the viewable area. |
| 5072 | return Rect2i(-1, -1, 0, 0); |
| 5073 | } |
| 5074 | |
| 5075 | int first_visible_char = cache_entry.first_visible_chars[wrap_index]; |
| 5076 | int last_visible_char = cache_entry.last_visible_chars[wrap_index]; |
| 5077 | if (p_column < first_visible_char || p_column > last_visible_char) { |
| 5078 | // Character is outside of the viewing area, no point calculating its position. |
| 5079 | return Rect2i(-1, -1, 0, 0); |
| 5080 | } |
| 5081 | |
| 5082 | const float wrap_indent = _get_wrap_indent_offset(p_line, wrap_index, is_layout_rtl()); |
| 5083 | |
| 5084 | Point2i pos, size; |
| 5085 | pos.y = cache_entry.y_offset + get_line_height() * wrap_index; |
| 5086 | pos.x = get_total_gutter_width() + Math::ceil(_get_current_stylebox()->get_margin(SIDE_LEFT)) + wrap_indent - get_h_scroll(); |
| 5087 | |
| 5088 | RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index); |
| 5089 | Vector2 col_bounds = TS->shaped_text_get_grapheme_bounds(text_rid, p_column); |
| 5090 | pos.x += col_bounds.x; |
| 5091 | size.x = col_bounds.y - col_bounds.x; |
| 5092 | |
| 5093 | size.y = get_line_height(); |
| 5094 | |
| 5095 | return Rect2i(pos, size); |
| 5096 | } |
| 5097 | |
| 5098 | int TextEdit::get_minimap_line_at_pos(const Point2i &p_pos) const { |
| 5099 | float rows = p_pos.y - _get_current_stylebox()->get_margin(SIDE_TOP); |
| 5100 | rows /= (minimap_char_size.y + minimap_line_spacing); |
| 5101 | rows += _get_v_scroll_offset(); |
| 5102 | |
| 5103 | // Calculate visible lines. |
| 5104 | int minimap_visible_lines = get_minimap_visible_lines(); |
| 5105 | int visible_rows = get_visible_line_count() + 1; |
| 5106 | int first_vis_line = get_first_visible_line() - 1; |
| 5107 | int draw_amount = visible_rows + 1; |
| 5108 | draw_amount += get_line_wrap_count(first_vis_line + 1); |
| 5109 | int minimap_line_height = (minimap_char_size.y + minimap_line_spacing); |
| 5110 | |
| 5111 | // Calculate viewport size and y offset. |
| 5112 | int viewport_height = (draw_amount - 1) * minimap_line_height; |
| 5113 | int control_height = _get_control_height() - viewport_height; |
| 5114 | int viewport_offset_y = std::round(get_scroll_pos_for_line(first_vis_line + 1) * control_height) / ((v_scroll->get_max() <= minimap_visible_lines) ? (minimap_visible_lines - draw_amount) : (v_scroll->get_max() - draw_amount)); |
| 5115 | |
| 5116 | // Calculate the first line. |
| 5117 | int num_lines_before = std::round((viewport_offset_y) / minimap_line_height); |
| 5118 | int minimap_line = (v_scroll->get_max() <= minimap_visible_lines) ? -1 : first_vis_line; |
| 5119 | if (first_vis_line > 0 && minimap_line >= 0) { |
| 5120 | minimap_line -= get_next_visible_line_index_offset_from(first_vis_line, 0, -num_lines_before).x; |
| 5121 | minimap_line -= (minimap_line > 0 && smooth_scroll_enabled ? 1 : 0); |
| 5122 | } |
| 5123 | |
| 5124 | if (minimap_line < 0) { |
| 5125 | minimap_line = 0; |
| 5126 | } |
| 5127 | |
| 5128 | int row = minimap_line + Math::floor(rows); |
| 5129 | if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE || _is_hiding_enabled()) { |
| 5130 | int f_ofs = get_next_visible_line_index_offset_from(minimap_line, first_visible_line_wrap_ofs, rows + (1 * SIGN(rows))).x - 1; |
| 5131 | if (rows < 0) { |
| 5132 | row = minimap_line - f_ofs; |
| 5133 | } else { |
| 5134 | row = minimap_line + f_ofs; |
| 5135 | } |
| 5136 | } |
| 5137 | |
| 5138 | row = CLAMP(row, 0, text.size() - 1); |
| 5139 | |
| 5140 | return row; |
| 5141 | } |
| 5142 | |
| 5143 | bool TextEdit::is_dragging_cursor() const { |
| 5144 | return dragging_selection || dragging_minimap; |
| 5145 | } |
| 5146 | |
| 5147 | bool TextEdit::is_mouse_over_selection(bool p_edges, int p_caret) const { |
| 5148 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 5149 | int line = pos.y; |
| 5150 | int column = pos.x; |
| 5151 | |
| 5152 | if ((p_caret == -1 && get_selection_at_line_column(line, column, p_edges) != -1) || (p_caret != -1 && _selection_contains(p_caret, line, column, p_edges))) { |
| 5153 | return true; |
| 5154 | } |
| 5155 | return false; |
| 5156 | } |
| 5157 | |
| 5158 | /* Caret */ |
| 5159 | void TextEdit::set_caret_type(CaretType p_type) { |
| 5160 | if (caret_type == p_type) { |
| 5161 | return; |
| 5162 | } |
| 5163 | |
| 5164 | caret_type = p_type; |
| 5165 | queue_redraw(); |
| 5166 | } |
| 5167 | |
| 5168 | TextEdit::CaretType TextEdit::get_caret_type() const { |
| 5169 | return caret_type; |
| 5170 | } |
| 5171 | |
| 5172 | void TextEdit::set_caret_blink_enabled(bool p_enabled) { |
| 5173 | if (caret_blink_enabled == p_enabled) { |
| 5174 | return; |
| 5175 | } |
| 5176 | |
| 5177 | caret_blink_enabled = p_enabled; |
| 5178 | |
| 5179 | if (has_focus()) { |
| 5180 | if (p_enabled) { |
| 5181 | caret_blink_timer->start(); |
| 5182 | } else { |
| 5183 | caret_blink_timer->stop(); |
| 5184 | } |
| 5185 | } |
| 5186 | draw_caret = true; |
| 5187 | } |
| 5188 | |
| 5189 | bool TextEdit::is_caret_blink_enabled() const { |
| 5190 | return caret_blink_enabled; |
| 5191 | } |
| 5192 | |
| 5193 | float TextEdit::get_caret_blink_interval() const { |
| 5194 | return caret_blink_timer->get_wait_time(); |
| 5195 | } |
| 5196 | |
| 5197 | void TextEdit::set_caret_blink_interval(const float p_interval) { |
| 5198 | ERR_FAIL_COND(p_interval <= 0); |
| 5199 | caret_blink_timer->set_wait_time(p_interval); |
| 5200 | } |
| 5201 | |
| 5202 | void TextEdit::set_draw_caret_when_editable_disabled(bool p_enable) { |
| 5203 | if (draw_caret_when_editable_disabled == p_enable) { |
| 5204 | return; |
| 5205 | } |
| 5206 | draw_caret_when_editable_disabled = p_enable; |
| 5207 | queue_redraw(); |
| 5208 | } |
| 5209 | |
| 5210 | bool TextEdit::is_drawing_caret_when_editable_disabled() const { |
| 5211 | return draw_caret_when_editable_disabled; |
| 5212 | } |
| 5213 | |
| 5214 | void TextEdit::set_move_caret_on_right_click_enabled(bool p_enabled) { |
| 5215 | move_caret_on_right_click = p_enabled; |
| 5216 | } |
| 5217 | |
| 5218 | bool TextEdit::is_move_caret_on_right_click_enabled() const { |
| 5219 | return move_caret_on_right_click; |
| 5220 | } |
| 5221 | |
| 5222 | void TextEdit::set_caret_mid_grapheme_enabled(bool p_enabled) { |
| 5223 | caret_mid_grapheme_enabled = p_enabled; |
| 5224 | } |
| 5225 | |
| 5226 | bool TextEdit::is_caret_mid_grapheme_enabled() const { |
| 5227 | return caret_mid_grapheme_enabled; |
| 5228 | } |
| 5229 | |
| 5230 | void TextEdit::set_multiple_carets_enabled(bool p_enabled) { |
| 5231 | multi_carets_enabled = p_enabled; |
| 5232 | if (!multi_carets_enabled) { |
| 5233 | remove_secondary_carets(); |
| 5234 | multicaret_edit_count = 0; |
| 5235 | multicaret_edit_ignore_carets.clear(); |
| 5236 | multicaret_edit_merge_queued = false; |
| 5237 | } |
| 5238 | } |
| 5239 | |
| 5240 | bool TextEdit::is_multiple_carets_enabled() const { |
| 5241 | return multi_carets_enabled; |
| 5242 | } |
| 5243 | |
| 5244 | int TextEdit::add_caret(int p_line, int p_column) { |
| 5245 | if (!multi_carets_enabled) { |
| 5246 | return -1; |
| 5247 | } |
| 5248 | _cancel_drag_and_drop_text(); |
| 5249 | |
| 5250 | p_line = CLAMP(p_line, 0, text.size() - 1); |
| 5251 | p_column = CLAMP(p_column, 0, get_line(p_line).length()); |
| 5252 | |
| 5253 | if (!is_in_mulitcaret_edit()) { |
| 5254 | // Carets cannot overlap. |
| 5255 | if (get_selection_at_line_column(p_line, p_column, true, false) != -1) { |
| 5256 | return -1; |
| 5257 | } |
| 5258 | } |
| 5259 | |
| 5260 | carets.push_back(Caret()); |
| 5261 | int new_index = carets.size() - 1; |
| 5262 | set_caret_line(p_line, false, false, -1, new_index); |
| 5263 | set_caret_column(p_column, false, new_index); |
| 5264 | _caret_changed(new_index); |
| 5265 | |
| 5266 | if (is_in_mulitcaret_edit()) { |
| 5267 | multicaret_edit_ignore_carets.insert(new_index); |
| 5268 | merge_overlapping_carets(); |
| 5269 | } |
| 5270 | return new_index; |
| 5271 | } |
| 5272 | |
| 5273 | void TextEdit::remove_caret(int p_caret) { |
| 5274 | ERR_FAIL_COND_MSG(carets.size() <= 1, "The main caret should not be removed."); |
| 5275 | ERR_FAIL_INDEX(p_caret, carets.size()); |
| 5276 | |
| 5277 | _caret_changed(p_caret); |
| 5278 | carets.remove_at(p_caret); |
| 5279 | |
| 5280 | if (drag_caret_index >= 0) { |
| 5281 | if (p_caret == drag_caret_index) { |
| 5282 | drag_caret_index = -1; |
| 5283 | } else if (p_caret < drag_caret_index) { |
| 5284 | drag_caret_index -= 1; |
| 5285 | } |
| 5286 | } |
| 5287 | } |
| 5288 | |
| 5289 | void TextEdit::remove_drag_caret() { |
| 5290 | if (drag_caret_index >= 0) { |
| 5291 | if (drag_caret_index < carets.size()) { |
| 5292 | remove_caret(drag_caret_index); |
| 5293 | } |
| 5294 | drag_caret_index = -1; |
| 5295 | } |
| 5296 | } |
| 5297 | |
| 5298 | void TextEdit::remove_secondary_carets() { |
| 5299 | if (carets.size() == 1) { |
| 5300 | return; |
| 5301 | } |
| 5302 | |
| 5303 | _caret_changed(); |
| 5304 | carets.resize(1); |
| 5305 | |
| 5306 | if (drag_caret_index >= 0) { |
| 5307 | drag_caret_index = -1; |
| 5308 | } |
| 5309 | queue_accessibility_update(); |
| 5310 | } |
| 5311 | |
| 5312 | int TextEdit::get_caret_count() const { |
| 5313 | // Don't include drag caret. |
| 5314 | if (drag_caret_index >= 0) { |
| 5315 | return carets.size() - 1; |
| 5316 | } |
| 5317 | return carets.size(); |
| 5318 | } |
| 5319 | |
| 5320 | void TextEdit::add_caret_at_carets(bool p_below) { |
| 5321 | if (!multi_carets_enabled) { |
| 5322 | return; |
| 5323 | } |
| 5324 | const int last_line_max_wrap = get_line_wrap_count(text.size() - 1); |
| 5325 | |
| 5326 | set_selection_mode(SELECTION_MODE_NONE); |
| 5327 | |
| 5328 | begin_multicaret_edit(); |
| 5329 | int view_target_caret = -1; |
| 5330 | int view_line = p_below ? -1 : INT_MAX; |
| 5331 | int num_carets = get_caret_count(); |
| 5332 | for (int i = 0; i < num_carets; i++) { |
| 5333 | const int caret_line = get_caret_line(i); |
| 5334 | const int caret_column = get_caret_column(i); |
| 5335 | bool is_selected = has_selection(i) || carets[i].last_fit_x != carets[i].selection.origin_last_fit_x; |
| 5336 | const int selection_origin_line = get_selection_origin_line(i); |
| 5337 | const int selection_origin_column = get_selection_origin_column(i); |
| 5338 | const int caret_wrap_index = get_caret_wrap_index(i); |
| 5339 | const int selection_origin_wrap_index = !is_selected ? -1 : get_line_wrap_index_at_column(selection_origin_line, selection_origin_column); |
| 5340 | |
| 5341 | if (caret_line == 0 && !p_below && (caret_wrap_index == 0 || selection_origin_wrap_index == 0)) { |
| 5342 | // Can't add above the first line. |
| 5343 | continue; |
| 5344 | } |
| 5345 | if (caret_line == text.size() - 1 && p_below && (caret_wrap_index == last_line_max_wrap || selection_origin_wrap_index == last_line_max_wrap)) { |
| 5346 | // Can't add below the last line. |
| 5347 | continue; |
| 5348 | } |
| 5349 | |
| 5350 | // Add a new caret. |
| 5351 | int new_caret_index = add_caret(caret_line, caret_column); |
| 5352 | ERR_FAIL_COND_MSG(new_caret_index < 0, "Failed to add a caret."); |
| 5353 | |
| 5354 | // Copy the selection origin and last fit. |
| 5355 | set_selection_origin_line(selection_origin_line, true, -1, new_caret_index); |
| 5356 | set_selection_origin_column(selection_origin_column, new_caret_index); |
| 5357 | carets.write[new_caret_index].last_fit_x = carets[i].last_fit_x; |
| 5358 | carets.write[new_caret_index].selection.origin_last_fit_x = carets[i].selection.origin_last_fit_x; |
| 5359 | |
| 5360 | // Move the caret up or down one visible line. |
| 5361 | if (!p_below) { |
| 5362 | // Move caret up. |
| 5363 | if (caret_wrap_index > 0) { |
| 5364 | set_caret_line(caret_line, false, false, caret_wrap_index - 1, new_caret_index); |
| 5365 | } else { |
| 5366 | int new_line = caret_line - get_next_visible_line_offset_from(caret_line - 1, -1); |
| 5367 | if (is_line_wrapped(new_line)) { |
| 5368 | set_caret_line(new_line, false, false, get_line_wrap_count(new_line), new_caret_index); |
| 5369 | } else { |
| 5370 | set_caret_line(new_line, false, false, 0, new_caret_index); |
| 5371 | } |
| 5372 | } |
| 5373 | // Move selection origin up. |
| 5374 | if (is_selected) { |
| 5375 | if (selection_origin_wrap_index > 0) { |
| 5376 | set_selection_origin_line(caret_line, false, selection_origin_wrap_index - 1, new_caret_index); |
| 5377 | } else { |
| 5378 | int new_line = selection_origin_line - get_next_visible_line_offset_from(selection_origin_line - 1, -1); |
| 5379 | if (is_line_wrapped(new_line)) { |
| 5380 | set_selection_origin_line(new_line, false, get_line_wrap_count(new_line), new_caret_index); |
| 5381 | } else { |
| 5382 | set_selection_origin_line(new_line, false, 0, new_caret_index); |
| 5383 | } |
| 5384 | } |
| 5385 | } |
| 5386 | if (get_caret_line(new_caret_index) < view_line) { |
| 5387 | view_line = get_caret_line(new_caret_index); |
| 5388 | view_target_caret = new_caret_index; |
| 5389 | } |
| 5390 | } else { |
| 5391 | // Move caret down. |
| 5392 | if (caret_wrap_index < get_line_wrap_count(caret_line)) { |
| 5393 | set_caret_line(caret_line, false, false, caret_wrap_index + 1, new_caret_index); |
| 5394 | } else { |
| 5395 | int new_line = caret_line + get_next_visible_line_offset_from(CLAMP(caret_line + 1, 0, text.size() - 1), 1); |
| 5396 | set_caret_line(new_line, false, false, 0, new_caret_index); |
| 5397 | } |
| 5398 | // Move selection origin down. |
| 5399 | if (is_selected) { |
| 5400 | if (selection_origin_wrap_index < get_line_wrap_count(selection_origin_line)) { |
| 5401 | set_selection_origin_line(selection_origin_line, false, selection_origin_wrap_index + 1, new_caret_index); |
| 5402 | } else { |
| 5403 | int new_line = selection_origin_line + get_next_visible_line_offset_from(CLAMP(selection_origin_line + 1, 0, text.size() - 1), 1); |
| 5404 | set_selection_origin_line(new_line, false, 0, new_caret_index); |
| 5405 | } |
| 5406 | } |
| 5407 | if (get_caret_line(new_caret_index) > view_line) { |
| 5408 | view_line = get_caret_line(new_caret_index); |
| 5409 | view_target_caret = new_caret_index; |
| 5410 | } |
| 5411 | } |
| 5412 | if (is_selected) { |
| 5413 | // Make sure selection is active. |
| 5414 | select(get_selection_origin_line(new_caret_index), get_selection_origin_column(new_caret_index), get_caret_line(new_caret_index), get_caret_column(new_caret_index), new_caret_index); |
| 5415 | carets.write[new_caret_index].last_fit_x = carets[i].last_fit_x; |
| 5416 | carets.write[new_caret_index].selection.origin_last_fit_x = carets[i].selection.origin_last_fit_x; |
| 5417 | } |
| 5418 | |
| 5419 | bool check_edges = !has_selection(0) || !has_selection(new_caret_index); |
| 5420 | bool will_merge_with_main_caret = _selection_contains(0, get_caret_line(new_caret_index), get_caret_column(new_caret_index), check_edges, false) || _selection_contains(new_caret_index, get_caret_line(0), get_caret_column(0), check_edges, false); |
| 5421 | if (will_merge_with_main_caret) { |
| 5422 | // Move next to the main caret so it stays the main caret after merging. |
| 5423 | Caret new_caret = carets[new_caret_index]; |
| 5424 | carets.remove_at(new_caret_index); |
| 5425 | carets.insert(0, new_caret); |
| 5426 | i++; |
| 5427 | } |
| 5428 | } |
| 5429 | |
| 5430 | // Show the topmost caret if added above or bottommost caret if added below. |
| 5431 | if (view_target_caret >= 0 && view_target_caret < get_caret_count()) { |
| 5432 | adjust_viewport_to_caret(view_target_caret); |
| 5433 | } |
| 5434 | |
| 5435 | merge_overlapping_carets(); |
| 5436 | end_multicaret_edit(); |
| 5437 | } |
| 5438 | |
| 5439 | struct _CaretSortComparator { |
| 5440 | _FORCE_INLINE_ bool operator()(const Vector3i &a, const Vector3i &b) const { |
| 5441 | // x is column, y is line, z is caret index. |
| 5442 | if (a.y == b.y) { |
| 5443 | return a.x < b.x; |
| 5444 | } |
| 5445 | return a.y < b.y; |
| 5446 | } |
| 5447 | }; |
| 5448 | |
| 5449 | Vector<int> TextEdit::get_sorted_carets(bool p_include_ignored_carets) const { |
| 5450 | // Returns caret indexes sorted by selection start or caret position from top to bottom of text. |
| 5451 | Vector<Vector3i> caret_line_col_indexes; |
| 5452 | for (int i = 0; i < get_caret_count(); i++) { |
| 5453 | if (!p_include_ignored_carets && multicaret_edit_ignore_caret(i)) { |
| 5454 | continue; |
| 5455 | } |
| 5456 | caret_line_col_indexes.push_back(Vector3i(get_selection_from_column(i), get_selection_from_line(i), i)); |
| 5457 | } |
| 5458 | caret_line_col_indexes.sort_custom<_CaretSortComparator>(); |
| 5459 | Vector<int> sorted; |
| 5460 | sorted.resize(caret_line_col_indexes.size()); |
| 5461 | for (int i = 0; i < caret_line_col_indexes.size(); i++) { |
| 5462 | sorted.set(i, caret_line_col_indexes[i].z); |
| 5463 | } |
| 5464 | return sorted; |
| 5465 | } |
| 5466 | |
| 5467 | void TextEdit::collapse_carets(int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_inclusive) { |
| 5468 | // Collapse carets in the selected range to the from position. |
| 5469 | |
| 5470 | // Clamp the collapse target position. |
| 5471 | int collapse_line = CLAMP(p_from_line, 0, text.size() - 1); |
| 5472 | int collapse_column = CLAMP(p_from_column, 0, text[collapse_line].length()); |
| 5473 | |
| 5474 | // Swap the lines if they are in the wrong order. |
| 5475 | if (p_from_line > p_to_line) { |
| 5476 | SWAP(p_from_line, p_to_line); |
| 5477 | SWAP(p_from_column, p_to_column); |
| 5478 | } |
| 5479 | if (p_from_line == p_to_line && p_from_column > p_to_column) { |
| 5480 | SWAP(p_from_column, p_to_column); |
| 5481 | } |
| 5482 | bool any_collapsed = false; |
| 5483 | |
| 5484 | // Intentionally includes carets in the multicaret_edit_ignore list so that they are moved together. |
| 5485 | for (int i = 0; i < get_caret_count(); i++) { |
| 5486 | bool is_caret_in = _is_line_col_in_range(get_caret_line(i), get_caret_column(i), p_from_line, p_from_column, p_to_line, p_to_column, p_inclusive); |
| 5487 | if (!has_selection(i)) { |
| 5488 | if (is_caret_in) { |
| 5489 | // Caret was in the collapsed area. |
| 5490 | set_caret_line(collapse_line, false, true, -1, i); |
| 5491 | set_caret_column(collapse_column, false, i); |
| 5492 | if (is_in_mulitcaret_edit() && get_caret_count() > 1) { |
| 5493 | multicaret_edit_ignore_carets.insert(i); |
| 5494 | } |
| 5495 | any_collapsed = true; |
| 5496 | } |
| 5497 | } else { |
| 5498 | bool is_origin_in = _is_line_col_in_range(get_selection_origin_line(i), get_selection_origin_column(i), p_from_line, p_from_column, p_to_line, p_to_column, p_inclusive); |
| 5499 | |
| 5500 | if (is_caret_in && is_origin_in) { |
| 5501 | // Selection was completely encapsulated. |
| 5502 | deselect(i); |
| 5503 | set_caret_line(collapse_line, false, true, -1, i); |
| 5504 | set_caret_column(collapse_column, false, i); |
| 5505 | if (is_in_mulitcaret_edit() && get_caret_count() > 1) { |
| 5506 | multicaret_edit_ignore_carets.insert(i); |
| 5507 | } |
| 5508 | any_collapsed = true; |
| 5509 | } else if (is_caret_in) { |
| 5510 | // Only caret was inside. |
| 5511 | set_caret_line(collapse_line, false, true, -1, i); |
| 5512 | set_caret_column(collapse_column, false, i); |
| 5513 | any_collapsed = true; |
| 5514 | } else if (is_origin_in) { |
| 5515 | // Only selection origin was inside. |
| 5516 | set_selection_origin_line(collapse_line, true, -1, i); |
| 5517 | set_selection_origin_column(collapse_column, i); |
| 5518 | any_collapsed = true; |
| 5519 | } |
| 5520 | } |
| 5521 | if (!p_inclusive && !any_collapsed) { |
| 5522 | if ((get_caret_line(i) == collapse_line && get_caret_column(i) == collapse_column) || (get_selection_origin_line(i) == collapse_line && get_selection_origin_column(i) == collapse_column)) { |
| 5523 | // Make sure to queue a merge, even if we didn't include it. |
| 5524 | any_collapsed = true; |
| 5525 | } |
| 5526 | } |
| 5527 | } |
| 5528 | if (any_collapsed) { |
| 5529 | merge_overlapping_carets(); |
| 5530 | } |
| 5531 | } |
| 5532 | |
| 5533 | void TextEdit::merge_overlapping_carets() { |
| 5534 | if (is_in_mulitcaret_edit()) { |
| 5535 | // Queue merge to be performed the end of the multicaret edit. |
| 5536 | multicaret_edit_merge_queued = true; |
| 5537 | return; |
| 5538 | } |
| 5539 | |
| 5540 | multicaret_edit_merge_queued = false; |
| 5541 | multicaret_edit_ignore_carets.clear(); |
| 5542 | |
| 5543 | if (get_caret_count() == 1) { |
| 5544 | return; |
| 5545 | } |
| 5546 | |
| 5547 | Vector<int> sorted_carets = get_sorted_carets(true); |
| 5548 | for (int i = 0; i < sorted_carets.size() - 1; i++) { |
| 5549 | int first_caret = sorted_carets[i]; |
| 5550 | int second_caret = sorted_carets[i + 1]; |
| 5551 | |
| 5552 | bool merge_carets; |
| 5553 | if (!has_selection(first_caret) || !has_selection(second_caret)) { |
| 5554 | // Merge if touching. |
| 5555 | merge_carets = get_selection_from_line(second_caret) < get_selection_to_line(first_caret) || (get_selection_from_line(second_caret) == get_selection_to_line(first_caret) && get_selection_from_column(second_caret) <= get_selection_to_column(first_caret)); |
| 5556 | } else { |
| 5557 | // Merge two selections if overlapping. |
| 5558 | merge_carets = get_selection_from_line(second_caret) < get_selection_to_line(first_caret) || (get_selection_from_line(second_caret) == get_selection_to_line(first_caret) && get_selection_from_column(second_caret) < get_selection_to_column(first_caret)); |
| 5559 | } |
| 5560 | |
| 5561 | if (!merge_carets) { |
| 5562 | continue; |
| 5563 | } |
| 5564 | |
| 5565 | // Save the newest one for Click + Drag. |
| 5566 | int caret_to_save = first_caret; |
| 5567 | int caret_to_remove = second_caret; |
| 5568 | if (first_caret < second_caret) { |
| 5569 | caret_to_save = second_caret; |
| 5570 | caret_to_remove = first_caret; |
| 5571 | } |
| 5572 | |
| 5573 | if (get_selection_from_line(caret_to_save) != get_selection_from_line(caret_to_remove) || get_selection_to_line(caret_to_save) != get_selection_to_line(caret_to_remove) || get_selection_from_column(caret_to_save) != get_selection_from_column(caret_to_remove) || get_selection_to_column(caret_to_save) != get_selection_to_column(caret_to_remove)) { |
| 5574 | // Selections are not the same, merge them into one bigger selection. |
| 5575 | int new_from_line = MIN(get_selection_from_line(caret_to_remove), get_selection_from_line(caret_to_save)); |
| 5576 | int new_to_line = MAX(get_selection_to_line(caret_to_remove), get_selection_to_line(caret_to_save)); |
| 5577 | int new_from_col; |
| 5578 | int new_to_col; |
| 5579 | if (get_selection_from_line(caret_to_remove) < get_selection_from_line(caret_to_save)) { |
| 5580 | new_from_col = get_selection_from_column(caret_to_remove); |
| 5581 | } else if (get_selection_from_line(caret_to_remove) > get_selection_from_line(caret_to_save)) { |
| 5582 | new_from_col = get_selection_from_column(caret_to_save); |
| 5583 | } else { |
| 5584 | new_from_col = MIN(get_selection_from_column(caret_to_remove), get_selection_from_column(caret_to_save)); |
| 5585 | } |
| 5586 | if (get_selection_to_line(caret_to_remove) < get_selection_to_line(caret_to_save)) { |
| 5587 | new_to_col = get_selection_to_column(caret_to_save); |
| 5588 | } else if (get_selection_to_line(caret_to_remove) > get_selection_to_line(caret_to_save)) { |
| 5589 | new_to_col = get_selection_to_column(caret_to_remove); |
| 5590 | } else { |
| 5591 | new_to_col = MAX(get_selection_to_column(caret_to_remove), get_selection_to_column(caret_to_save)); |
| 5592 | } |
| 5593 | |
| 5594 | // Use the direction from the last caret or the saved one. |
| 5595 | int caret_dir_to_copy; |
| 5596 | if (has_selection(caret_to_remove) && has_selection(caret_to_save)) { |
| 5597 | caret_dir_to_copy = caret_to_remove == get_caret_count() - 1 ? caret_to_remove : caret_to_save; |
| 5598 | } else { |
| 5599 | caret_dir_to_copy = !has_selection(caret_to_remove) ? caret_to_save : caret_to_remove; |
| 5600 | } |
| 5601 | |
| 5602 | if (is_caret_after_selection_origin(caret_dir_to_copy)) { |
| 5603 | select(new_from_line, new_from_col, new_to_line, new_to_col, caret_to_save); |
| 5604 | } else { |
| 5605 | select(new_to_line, new_to_col, new_from_line, new_from_col, caret_to_save); |
| 5606 | } |
| 5607 | } |
| 5608 | |
| 5609 | if (caret_to_save == 0) { |
| 5610 | adjust_viewport_to_caret(caret_to_save); |
| 5611 | } |
| 5612 | remove_caret(caret_to_remove); |
| 5613 | |
| 5614 | // Update the rest of the sorted list. |
| 5615 | for (int j = i; j < sorted_carets.size(); j++) { |
| 5616 | if (sorted_carets[j] > caret_to_remove) { |
| 5617 | // Shift the index since a caret before it was removed. |
| 5618 | sorted_carets.write[j] -= 1; |
| 5619 | } |
| 5620 | } |
| 5621 | // Remove the caret from the sorted array. |
| 5622 | sorted_carets.remove_at(caret_to_remove == first_caret ? i : i + 1); |
| 5623 | |
| 5624 | // Process the caret again, since it and the next caret might also overlap. |
| 5625 | i--; |
| 5626 | } |
| 5627 | } |
| 5628 | |
| 5629 | // Starts a multicaret edit operation. Call this before iterating over the carets and call [end_multicaret_edit] afterwards. |
| 5630 | void TextEdit::begin_multicaret_edit() { |
| 5631 | if (!multi_carets_enabled) { |
| 5632 | return; |
| 5633 | } |
| 5634 | multicaret_edit_count++; |
| 5635 | } |
| 5636 | |
| 5637 | void TextEdit::end_multicaret_edit() { |
| 5638 | if (!multi_carets_enabled) { |
| 5639 | return; |
| 5640 | } |
| 5641 | if (multicaret_edit_count > 0) { |
| 5642 | multicaret_edit_count--; |
| 5643 | } |
| 5644 | if (multicaret_edit_count != 0) { |
| 5645 | return; |
| 5646 | } |
| 5647 | |
| 5648 | // This was the last multicaret edit operation. |
| 5649 | if (multicaret_edit_merge_queued) { |
| 5650 | merge_overlapping_carets(); |
| 5651 | } |
| 5652 | multicaret_edit_ignore_carets.clear(); |
| 5653 | } |
| 5654 | |
| 5655 | bool TextEdit::is_in_mulitcaret_edit() const { |
| 5656 | return multicaret_edit_count > 0; |
| 5657 | } |
| 5658 | |
| 5659 | bool TextEdit::multicaret_edit_ignore_caret(int p_caret) const { |
| 5660 | return multicaret_edit_ignore_carets.has(p_caret); |
| 5661 | } |
| 5662 | |
| 5663 | bool TextEdit::is_caret_visible(int p_caret) const { |
| 5664 | ERR_FAIL_INDEX_V(p_caret, carets.size(), false); |
| 5665 | return carets[p_caret].visible; |
| 5666 | } |
| 5667 | |
| 5668 | Point2 TextEdit::get_caret_draw_pos(int p_caret) const { |
| 5669 | ERR_FAIL_INDEX_V(p_caret, carets.size(), Point2(0, 0)); |
| 5670 | return carets[p_caret].draw_pos; |
| 5671 | } |
| 5672 | |
| 5673 | void TextEdit::set_caret_line(int p_line, bool p_adjust_viewport, bool p_can_be_hidden, int p_wrap_index, int p_caret) { |
| 5674 | ERR_FAIL_INDEX(p_caret, carets.size()); |
| 5675 | if (setting_caret_line) { |
| 5676 | return; |
| 5677 | } |
| 5678 | |
| 5679 | setting_caret_line = true; |
| 5680 | p_line = CLAMP(p_line, 0, text.size() - 1); |
| 5681 | |
| 5682 | if (!p_can_be_hidden) { |
| 5683 | if (_is_line_hidden(p_line)) { |
| 5684 | int move_down = get_next_visible_line_offset_from(p_line, 1) - 1; |
| 5685 | if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) { |
| 5686 | p_line += move_down; |
| 5687 | } else { |
| 5688 | int move_up = get_next_visible_line_offset_from(p_line, -1) - 1; |
| 5689 | if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) { |
| 5690 | p_line -= move_up; |
| 5691 | } else { |
| 5692 | WARN_PRINT("Caret set to hidden line " + itos(p_line) + " and there are no nonhidden lines."); |
| 5693 | } |
| 5694 | } |
| 5695 | } |
| 5696 | } |
| 5697 | bool caret_moved = get_caret_line(p_caret) != p_line; |
| 5698 | carets.write[p_caret].line = p_line; |
| 5699 | |
| 5700 | int n_col; |
| 5701 | if (p_wrap_index >= 0) { |
| 5702 | // Keep caret in same visual x position it was at previously. |
| 5703 | n_col = _get_char_pos_for_line(carets[p_caret].last_fit_x, p_line, p_wrap_index); |
| 5704 | if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) { |
| 5705 | // Offset by one to not go past the end of the wrapped line. |
| 5706 | if (n_col >= text.get_line_wrap_ranges(p_line)[p_wrap_index].y) { |
| 5707 | n_col -= 1; |
| 5708 | } |
| 5709 | } |
| 5710 | } else { |
| 5711 | // Clamp the column. |
| 5712 | n_col = MIN(get_caret_column(p_caret), get_line(p_line).length()); |
| 5713 | } |
| 5714 | caret_moved = (caret_moved || get_caret_column(p_caret) != n_col); |
| 5715 | carets.write[p_caret].column = n_col; |
| 5716 | |
| 5717 | // Unselect if the caret moved to the selection origin. |
| 5718 | if (p_wrap_index >= 0 && has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) { |
| 5719 | deselect(p_caret); |
| 5720 | } |
| 5721 | |
| 5722 | if (is_inside_tree() && p_adjust_viewport) { |
| 5723 | adjust_viewport_to_caret(p_caret); |
| 5724 | } |
| 5725 | |
| 5726 | setting_caret_line = false; |
| 5727 | |
| 5728 | if (caret_moved) { |
| 5729 | _caret_changed(p_caret); |
| 5730 | } |
| 5731 | queue_accessibility_update(); |
| 5732 | } |
| 5733 | |
| 5734 | int TextEdit::get_caret_line(int p_caret) const { |
| 5735 | ERR_FAIL_INDEX_V(p_caret, carets.size(), 0); |
| 5736 | return carets[p_caret].line; |
| 5737 | } |
| 5738 | |
| 5739 | void TextEdit::set_caret_column(int p_column, bool p_adjust_viewport, int p_caret) { |
| 5740 | ERR_FAIL_INDEX(p_caret, carets.size()); |
| 5741 | |
| 5742 | p_column = CLAMP(p_column, 0, get_line(get_caret_line(p_caret)).length()); |
| 5743 | |
| 5744 | bool caret_moved = get_caret_column(p_caret) != p_column; |
| 5745 | carets.write[p_caret].column = p_column; |
| 5746 | |
| 5747 | carets.write[p_caret].last_fit_x = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret)); |
| 5748 | |
| 5749 | if (!has_selection(p_caret)) { |
| 5750 | // Set the selection origin last fit x to be the same, so we can tell if there was a selection. |
| 5751 | carets.write[p_caret].selection.origin_last_fit_x = carets[p_caret].last_fit_x; |
| 5752 | } |
| 5753 | |
| 5754 | // Unselect if the caret moved to the selection origin. |
| 5755 | if (has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) { |
| 5756 | deselect(p_caret); |
| 5757 | } |
| 5758 | |
| 5759 | if (is_inside_tree() && p_adjust_viewport) { |
| 5760 | adjust_viewport_to_caret(p_caret); |
| 5761 | } |
| 5762 | |
| 5763 | if (caret_moved) { |
| 5764 | _caret_changed(p_caret); |
| 5765 | } |
| 5766 | queue_accessibility_update(); |
| 5767 | } |
| 5768 | |
| 5769 | int TextEdit::get_caret_column(int p_caret) const { |
| 5770 | ERR_FAIL_INDEX_V(p_caret, carets.size(), 0); |
| 5771 | return carets[p_caret].column; |
| 5772 | } |
| 5773 | |
| 5774 | int TextEdit::get_caret_wrap_index(int p_caret) const { |
| 5775 | ERR_FAIL_INDEX_V(p_caret, carets.size(), 0); |
| 5776 | return get_line_wrap_index_at_column(get_caret_line(p_caret), get_caret_column(p_caret)); |
| 5777 | } |
| 5778 | |
| 5779 | String TextEdit::get_word_under_caret(int p_caret) const { |
| 5780 | ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, ""); |
| 5781 | |
| 5782 | StringBuilder selected_text; |
| 5783 | for (int c = 0; c < carets.size(); c++) { |
| 5784 | if (p_caret != -1 && p_caret != c) { |
| 5785 | continue; |
| 5786 | } |
| 5787 | |
| 5788 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid()); |
| 5789 | for (int i = 0; i < words.size(); i = i + 2) { |
| 5790 | if (words[i] <= get_caret_column(c) && words[i + 1] > get_caret_column(c)) { |
| 5791 | selected_text += text[get_caret_line(c)].substr(words[i], words[i + 1] - words[i]); |
| 5792 | if (p_caret == -1 && c != carets.size() - 1) { |
| 5793 | selected_text += "\n"; |
| 5794 | } |
| 5795 | } |
| 5796 | } |
| 5797 | } |
| 5798 | return selected_text.as_string(); |
| 5799 | } |
| 5800 | |
| 5801 | /* Selection. */ |
| 5802 | void TextEdit::set_selecting_enabled(bool p_enabled) { |
| 5803 | if (selecting_enabled == p_enabled) { |
| 5804 | return; |
| 5805 | } |
| 5806 | |
| 5807 | selecting_enabled = p_enabled; |
| 5808 | |
| 5809 | if (!selecting_enabled) { |
| 5810 | deselect(); |
| 5811 | } |
| 5812 | } |
| 5813 | |
| 5814 | bool TextEdit::is_selecting_enabled() const { |
| 5815 | return selecting_enabled; |
| 5816 | } |
| 5817 | |
| 5818 | void TextEdit::set_deselect_on_focus_loss_enabled(bool p_enabled) { |
| 5819 | if (deselect_on_focus_loss_enabled == p_enabled) { |
| 5820 | return; |
| 5821 | } |
| 5822 | |
| 5823 | deselect_on_focus_loss_enabled = p_enabled; |
| 5824 | if (p_enabled && has_selection() && !has_focus()) { |
| 5825 | deselect(); |
| 5826 | } |
| 5827 | } |
| 5828 | |
| 5829 | bool TextEdit::is_deselect_on_focus_loss_enabled() const { |
| 5830 | return deselect_on_focus_loss_enabled; |
| 5831 | } |
| 5832 | |
| 5833 | void TextEdit::set_drag_and_drop_selection_enabled(bool p_enabled) { |
| 5834 | drag_and_drop_selection_enabled = p_enabled; |
| 5835 | } |
| 5836 | |
| 5837 | bool TextEdit::is_drag_and_drop_selection_enabled() const { |
| 5838 | return drag_and_drop_selection_enabled; |
| 5839 | } |
| 5840 | |
| 5841 | void TextEdit::set_selection_mode(SelectionMode p_mode) { |
| 5842 | selecting_mode = p_mode; |
| 5843 | } |
| 5844 | |
| 5845 | TextEdit::SelectionMode TextEdit::get_selection_mode() const { |
| 5846 | return selecting_mode; |
| 5847 | } |
| 5848 | |
| 5849 | void TextEdit::select_all() { |
| 5850 | _push_current_op(); |
| 5851 | if (!selecting_enabled) { |
| 5852 | return; |
| 5853 | } |
| 5854 | |
| 5855 | if (text.size() == 1 && text[0].is_empty()) { |
| 5856 | return; |
| 5857 | } |
| 5858 | |
| 5859 | remove_secondary_carets(); |
| 5860 | set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT); |
| 5861 | select(0, 0, text.size() - 1, text[text.size() - 1].length()); |
| 5862 | } |
| 5863 | |
| 5864 | void TextEdit::select_word_under_caret(int p_caret) { |
| 5865 | ERR_FAIL_COND(p_caret >= carets.size() || p_caret < -1); |
| 5866 | |
| 5867 | _push_current_op(); |
| 5868 | if (!selecting_enabled) { |
| 5869 | return; |
| 5870 | } |
| 5871 | |
| 5872 | if (text.size() == 1 && text[0].is_empty()) { |
| 5873 | return; |
| 5874 | } |
| 5875 | |
| 5876 | set_selection_mode(SELECTION_MODE_NONE); |
| 5877 | |
| 5878 | for (int c = 0; c < carets.size(); c++) { |
| 5879 | if (p_caret != -1 && p_caret != c) { |
| 5880 | continue; |
| 5881 | } |
| 5882 | |
| 5883 | if (has_selection(c)) { |
| 5884 | // Allow toggling selection by pressing the shortcut a second time. |
| 5885 | // This is also usable as a general-purpose "deselect" shortcut after |
| 5886 | // selecting anything. |
| 5887 | deselect(c); |
| 5888 | continue; |
| 5889 | } |
| 5890 | |
| 5891 | int begin = 0; |
| 5892 | int end = 0; |
| 5893 | const PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(get_caret_line(c))->get_rid()); |
| 5894 | for (int i = 0; i < words.size(); i = i + 2) { |
| 5895 | if ((words[i] <= get_caret_column(c) && words[i + 1] >= get_caret_column(c)) || (i == words.size() - 2 && get_caret_column(c) == words[i + 1])) { |
| 5896 | begin = words[i]; |
| 5897 | end = words[i + 1]; |
| 5898 | break; |
| 5899 | } |
| 5900 | } |
| 5901 | |
| 5902 | // No word found. |
| 5903 | if (begin == 0 && end == 0) { |
| 5904 | continue; |
| 5905 | } |
| 5906 | |
| 5907 | select(get_caret_line(c), begin, get_caret_line(c), end, c); |
| 5908 | } |
| 5909 | merge_overlapping_carets(); |
| 5910 | } |
| 5911 | |
| 5912 | void TextEdit::add_selection_for_next_occurrence() { |
| 5913 | if (!selecting_enabled || !is_multiple_carets_enabled()) { |
| 5914 | return; |
| 5915 | } |
| 5916 | |
| 5917 | if (text.size() == 1 && text[0].is_empty()) { |
| 5918 | return; |
| 5919 | } |
| 5920 | |
| 5921 | _push_current_op(); |
| 5922 | // Always use the last caret, to correctly search for |
| 5923 | // the next occurrence that comes after this caret. |
| 5924 | int caret = get_caret_count() - 1; |
| 5925 | |
| 5926 | if (!has_selection(caret)) { |
| 5927 | select_word_under_caret(caret); |
| 5928 | return; |
| 5929 | } |
| 5930 | |
| 5931 | set_selection_mode(SELECTION_MODE_NONE); |
| 5932 | |
| 5933 | const String &highlighted_text = get_selected_text(caret); |
| 5934 | int column = get_selection_from_column(caret) + 1; |
| 5935 | int line = get_selection_from_line(caret); |
| 5936 | |
| 5937 | const Point2i next_occurrence = search(highlighted_text, SEARCH_MATCH_CASE, line, column); |
| 5938 | |
| 5939 | if (next_occurrence.x == -1 || next_occurrence.y == -1) { |
| 5940 | return; |
| 5941 | } |
| 5942 | |
| 5943 | int to_column = get_selection_to_column(caret) + 1; |
| 5944 | int end = next_occurrence.x + (to_column - column); |
| 5945 | int new_caret = add_caret(next_occurrence.y, end); |
| 5946 | |
| 5947 | if (new_caret != -1) { |
| 5948 | select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret); |
| 5949 | _unhide_carets(); |
| 5950 | adjust_viewport_to_caret(new_caret); |
| 5951 | merge_overlapping_carets(); |
| 5952 | } |
| 5953 | } |
| 5954 | |
| 5955 | void TextEdit::skip_selection_for_next_occurrence() { |
| 5956 | if (!selecting_enabled) { |
| 5957 | return; |
| 5958 | } |
| 5959 | |
| 5960 | if (text.size() == 1 && text[0].is_empty()) { |
| 5961 | return; |
| 5962 | } |
| 5963 | |
| 5964 | set_selection_mode(SELECTION_MODE_NONE); |
| 5965 | |
| 5966 | // Always use the last caret, to correctly search for |
| 5967 | // the next occurrence that comes after this caret. |
| 5968 | int caret = get_caret_count() - 1; |
| 5969 | |
| 5970 | // Supports getting the text under caret without selecting it. |
| 5971 | // It allows to use this shortcut to simply jump to the next (under caret) word. |
| 5972 | // Due to const and &(reference) presence, ternary operator is a way to avoid errors and warnings. |
| 5973 | const String &searched_text = has_selection(caret) ? get_selected_text(caret) : get_word_under_caret(caret); |
| 5974 | |
| 5975 | int column = get_selection_from_column(caret) + 1; |
| 5976 | int line = get_selection_from_line(caret); |
| 5977 | |
| 5978 | const Point2i next_occurrence = search(searched_text, SEARCH_MATCH_CASE, line, column); |
| 5979 | |
| 5980 | if (next_occurrence.x == -1 || next_occurrence.y == -1) { |
| 5981 | return; |
| 5982 | } |
| 5983 | |
| 5984 | int to_column = get_selection_to_column(caret) + 1; |
| 5985 | int end = next_occurrence.x + (to_column - column); |
| 5986 | int new_caret = add_caret(next_occurrence.y, end); |
| 5987 | |
| 5988 | if (new_caret != -1) { |
| 5989 | select(next_occurrence.y, next_occurrence.x, next_occurrence.y, end, new_caret); |
| 5990 | _unhide_carets(); |
| 5991 | adjust_viewport_to_caret(new_caret); |
| 5992 | merge_overlapping_carets(); |
| 5993 | } |
| 5994 | |
| 5995 | // Deselect word under previous caret. |
| 5996 | if (has_selection(caret)) { |
| 5997 | select_word_under_caret(caret); |
| 5998 | } |
| 5999 | |
| 6000 | // Remove previous caret. |
| 6001 | if (get_caret_count() > 1) { |
| 6002 | remove_caret(caret); |
| 6003 | } |
| 6004 | } |
| 6005 | |
| 6006 | void TextEdit::select(int p_origin_line, int p_origin_column, int p_caret_line, int p_caret_column, int p_caret) { |
| 6007 | ERR_FAIL_INDEX(p_caret, get_caret_count()); |
| 6008 | |
| 6009 | p_caret_line = CLAMP(p_caret_line, 0, text.size() - 1); |
| 6010 | p_caret_column = CLAMP(p_caret_column, 0, text[p_caret_line].length()); |
| 6011 | set_caret_line(p_caret_line, false, true, -1, p_caret); |
| 6012 | set_caret_column(p_caret_column, false, p_caret); |
| 6013 | |
| 6014 | if (!selecting_enabled) { |
| 6015 | return; |
| 6016 | } |
| 6017 | |
| 6018 | p_origin_line = CLAMP(p_origin_line, 0, text.size() - 1); |
| 6019 | p_origin_column = CLAMP(p_origin_column, 0, text[p_origin_line].length()); |
| 6020 | set_selection_origin_line(p_origin_line, true, -1, p_caret); |
| 6021 | set_selection_origin_column(p_origin_column, p_caret); |
| 6022 | |
| 6023 | bool had_selection = has_selection(p_caret); |
| 6024 | bool activate = p_origin_line != p_caret_line || p_origin_column != p_caret_column; |
| 6025 | carets.write[p_caret].selection.active = activate; |
| 6026 | if (had_selection != activate) { |
| 6027 | _selection_changed(p_caret); |
| 6028 | } |
| 6029 | |
| 6030 | queue_accessibility_update(); |
| 6031 | queue_redraw(); |
| 6032 | } |
| 6033 | |
| 6034 | bool TextEdit::has_selection(int p_caret) const { |
| 6035 | ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, false); |
| 6036 | if (p_caret >= 0) { |
| 6037 | return carets[p_caret].selection.active; |
| 6038 | } |
| 6039 | for (int i = 0; i < carets.size(); i++) { |
| 6040 | if (carets[i].selection.active) { |
| 6041 | return true; |
| 6042 | } |
| 6043 | } |
| 6044 | return false; |
| 6045 | } |
| 6046 | |
| 6047 | String TextEdit::get_selected_text(int p_caret) { |
| 6048 | ERR_FAIL_COND_V(p_caret >= carets.size() || p_caret < -1, ""); |
| 6049 | |
| 6050 | if (p_caret >= 0) { |
| 6051 | if (!has_selection(p_caret)) { |
| 6052 | return ""; |
| 6053 | } |
| 6054 | return _base_get_text(get_selection_from_line(p_caret), get_selection_from_column(p_caret), get_selection_to_line(p_caret), get_selection_to_column(p_caret)); |
| 6055 | } |
| 6056 | |
| 6057 | StringBuilder selected_text; |
| 6058 | Vector<int> sorted_carets = get_sorted_carets(); |
| 6059 | for (int i = 0; i < sorted_carets.size(); i++) { |
| 6060 | int caret_index = sorted_carets[i]; |
| 6061 | |
| 6062 | if (!has_selection(caret_index)) { |
| 6063 | continue; |
| 6064 | } |
| 6065 | if (selected_text.get_string_length() != 0) { |
| 6066 | selected_text += "\n"; |
| 6067 | } |
| 6068 | selected_text += _base_get_text(get_selection_from_line(caret_index), get_selection_from_column(caret_index), get_selection_to_line(caret_index), get_selection_to_column(caret_index)); |
| 6069 | } |
| 6070 | |
| 6071 | return selected_text.as_string(); |
| 6072 | } |
| 6073 | |
| 6074 | int TextEdit::get_selection_at_line_column(int p_line, int p_column, bool p_include_edges, bool p_only_selections) const { |
| 6075 | // Return the caret index of the found selection, or -1. |
| 6076 | for (int i = 0; i < get_caret_count(); i++) { |
| 6077 | if (_selection_contains(i, p_line, p_column, p_include_edges, p_only_selections)) { |
| 6078 | return i; |
| 6079 | } |
| 6080 | } |
| 6081 | return -1; |
| 6082 | } |
| 6083 | |
| 6084 | Vector<Point2i> TextEdit::get_line_ranges_from_carets(bool p_only_selections, bool p_merge_adjacent) const { |
| 6085 | // Get a series of line ranges that cover all lines that have a caret or selection. |
| 6086 | // For each Point2i range, x is the first line and y is the last line. |
| 6087 | Vector<Point2i> ret; |
| 6088 | int last_to_line = INT_MIN; |
| 6089 | |
| 6090 | Vector<int> sorted_carets = get_sorted_carets(); |
| 6091 | for (int i = 0; i < sorted_carets.size(); i++) { |
| 6092 | int caret_index = sorted_carets[i]; |
| 6093 | if (p_only_selections && !has_selection(caret_index)) { |
| 6094 | continue; |
| 6095 | } |
| 6096 | Point2i range = Point2i(get_selection_from_line(caret_index), get_selection_to_line(caret_index)); |
| 6097 | if (has_selection(caret_index) && get_selection_to_column(caret_index) == 0) { |
| 6098 | // Dont include selection end line if it ends at column 0. |
| 6099 | range.y--; |
| 6100 | } |
| 6101 | if (range.x == last_to_line || (p_merge_adjacent && range.x - 1 == last_to_line)) { |
| 6102 | // Merge if starts on the same line or adjacent line. |
| 6103 | ret.write[ret.size() - 1].y = range.y; |
| 6104 | } else { |
| 6105 | ret.append(range); |
| 6106 | } |
| 6107 | last_to_line = range.y; |
| 6108 | } |
| 6109 | return ret; |
| 6110 | } |
| 6111 | |
| 6112 | TypedArray<Vector2i> TextEdit::get_line_ranges_from_carets_typed_array(bool p_only_selections, bool p_merge_adjacent) const { |
| 6113 | // Wrapper for `get_line_ranges_from_carets` to return a datatype that can be exposed. |
| 6114 | TypedArray<Vector2i> ret; |
| 6115 | Vector<Point2i> ranges = get_line_ranges_from_carets(p_only_selections, p_merge_adjacent); |
| 6116 | for (const Point2i &range : ranges) { |
| 6117 | ret.push_back(range); |
| 6118 | } |
| 6119 | return ret; |
| 6120 | } |
| 6121 | |
| 6122 | void TextEdit::set_selection_origin_line(int p_line, bool p_can_be_hidden, int p_wrap_index, int p_caret) { |
| 6123 | if (!selecting_enabled) { |
| 6124 | return; |
| 6125 | } |
| 6126 | ERR_FAIL_INDEX(p_caret, carets.size()); |
| 6127 | p_line = CLAMP(p_line, 0, text.size() - 1); |
| 6128 | |
| 6129 | if (!p_can_be_hidden) { |
| 6130 | if (_is_line_hidden(p_line)) { |
| 6131 | int move_down = get_next_visible_line_offset_from(p_line, 1) - 1; |
| 6132 | if (p_line + move_down <= text.size() - 1 && !_is_line_hidden(p_line + move_down)) { |
| 6133 | p_line += move_down; |
| 6134 | } else { |
| 6135 | int move_up = get_next_visible_line_offset_from(p_line, -1) - 1; |
| 6136 | if (p_line - move_up > 0 && !_is_line_hidden(p_line - move_up)) { |
| 6137 | p_line -= move_up; |
| 6138 | } else { |
| 6139 | WARN_PRINT("Selection origin set to hidden line " + itos(p_line) + " and there are no nonhidden lines."); |
| 6140 | } |
| 6141 | } |
| 6142 | } |
| 6143 | } |
| 6144 | |
| 6145 | bool selection_moved = get_selection_origin_line(p_caret) != p_line; |
| 6146 | carets.write[p_caret].selection.origin_line = p_line; |
| 6147 | |
| 6148 | int n_col; |
| 6149 | if (p_wrap_index >= 0) { |
| 6150 | // Keep selection origin in same visual x position it was at previously. |
| 6151 | n_col = _get_char_pos_for_line(carets[p_caret].selection.origin_last_fit_x, p_line, p_wrap_index); |
| 6152 | if (n_col != 0 && get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE && p_wrap_index < get_line_wrap_count(p_line)) { |
| 6153 | // Offset by one to not go past the end of the wrapped line. |
| 6154 | if (n_col >= text.get_line_wrap_ranges(p_line)[p_wrap_index].y) { |
| 6155 | n_col -= 1; |
| 6156 | } |
| 6157 | } |
| 6158 | } else { |
| 6159 | // Clamp the column. |
| 6160 | n_col = MIN(get_selection_origin_column(p_caret), get_line(p_line).length()); |
| 6161 | } |
| 6162 | selection_moved = (selection_moved || get_selection_origin_column(p_caret) != n_col); |
| 6163 | carets.write[p_caret].selection.origin_column = n_col; |
| 6164 | |
| 6165 | // Unselect if the selection origin moved to the caret. |
| 6166 | if (p_wrap_index >= 0 && has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) { |
| 6167 | deselect(p_caret); |
| 6168 | } |
| 6169 | |
| 6170 | if (selection_moved && has_selection(p_caret)) { |
| 6171 | _selection_changed(p_caret); |
| 6172 | } |
| 6173 | } |
| 6174 | |
| 6175 | void TextEdit::set_selection_origin_column(int p_column, int p_caret) { |
| 6176 | if (!selecting_enabled) { |
| 6177 | return; |
| 6178 | } |
| 6179 | ERR_FAIL_INDEX(p_caret, carets.size()); |
| 6180 | |
| 6181 | p_column = CLAMP(p_column, 0, get_line(get_selection_origin_line(p_caret)).length()); |
| 6182 | |
| 6183 | bool selection_moved = get_selection_origin_column(p_caret) != p_column; |
| 6184 | |
| 6185 | carets.write[p_caret].selection.origin_column = p_column; |
| 6186 | |
| 6187 | carets.write[p_caret].selection.origin_last_fit_x = _get_column_x_offset_for_line(get_selection_origin_column(p_caret), get_selection_origin_line(p_caret), get_selection_origin_column(p_caret)); |
| 6188 | |
| 6189 | // Unselect if the selection origin moved to the caret. |
| 6190 | if (has_selection(p_caret) && get_caret_line(p_caret) == get_selection_origin_line(p_caret) && get_caret_column(p_caret) == get_selection_origin_column(p_caret)) { |
| 6191 | deselect(p_caret); |
| 6192 | } |
| 6193 | |
| 6194 | if (get_selection_mode() == SELECTION_MODE_NONE || get_selection_mode() == SELECTION_MODE_SHIFT) { |
| 6195 | carets.write[p_caret].selection.word_begin_column = p_column; |
| 6196 | carets.write[p_caret].selection.word_end_column = p_column; |
| 6197 | } |
| 6198 | |
| 6199 | if (selection_moved && has_selection(p_caret)) { |
| 6200 | _selection_changed(p_caret); |
| 6201 | } |
| 6202 | } |
| 6203 | |
| 6204 | int TextEdit::get_selection_origin_line(int p_caret) const { |
| 6205 | ERR_FAIL_INDEX_V(p_caret, carets.size(), -1); |
| 6206 | return carets[p_caret].selection.origin_line; |
| 6207 | } |
| 6208 | |
| 6209 | int TextEdit::get_selection_origin_column(int p_caret) const { |
| 6210 | ERR_FAIL_INDEX_V(p_caret, carets.size(), -1); |
| 6211 | return carets[p_caret].selection.origin_column; |
| 6212 | } |
| 6213 | |
| 6214 | int TextEdit::get_next_composite_character_column(int p_line, int p_column) const { |
| 6215 | ERR_FAIL_INDEX_V(p_line, text.size(), -1); |
| 6216 | ERR_FAIL_INDEX_V(p_column, text[p_line].length() + 1, -1); |
| 6217 | if (p_column == text[p_line].length()) { |
| 6218 | return p_column; |
| 6219 | } else { |
| 6220 | return TS->shaped_text_next_character_pos(text.get_line_data(p_line)->get_rid(), (p_column)); |
| 6221 | } |
| 6222 | } |
| 6223 | |
| 6224 | int TextEdit::get_previous_composite_character_column(int p_line, int p_column) const { |
| 6225 | ERR_FAIL_INDEX_V(p_line, text.size(), -1); |
| 6226 | ERR_FAIL_INDEX_V(p_column, text[p_line].length() + 1, -1); |
| 6227 | if (p_column == 0) { |
| 6228 | return 0; |
| 6229 | } else { |
| 6230 | return TS->shaped_text_prev_character_pos(text.get_line_data(p_line)->get_rid(), p_column); |
| 6231 | } |
| 6232 | } |
| 6233 | |
| 6234 | int TextEdit::get_selection_from_line(int p_caret) const { |
| 6235 | ERR_FAIL_INDEX_V(p_caret, carets.size(), -1); |
| 6236 | if (!has_selection(p_caret)) { |
| 6237 | return carets[p_caret].line; |
| 6238 | } |
| 6239 | return MIN(carets[p_caret].selection.origin_line, carets[p_caret].line); |
| 6240 | } |
| 6241 | |
| 6242 | int TextEdit::get_selection_from_column(int p_caret) const { |
| 6243 | ERR_FAIL_INDEX_V(p_caret, carets.size(), -1); |
| 6244 | if (!has_selection(p_caret)) { |
| 6245 | return carets[p_caret].column; |
| 6246 | } |
| 6247 | if (carets[p_caret].selection.origin_line < carets[p_caret].line) { |
| 6248 | return carets[p_caret].selection.origin_column; |
| 6249 | } else if (carets[p_caret].selection.origin_line > carets[p_caret].line) { |
| 6250 | return carets[p_caret].column; |
| 6251 | } else { |
| 6252 | return MIN(carets[p_caret].selection.origin_column, carets[p_caret].column); |
| 6253 | } |
| 6254 | } |
| 6255 | |
| 6256 | int TextEdit::get_selection_to_line(int p_caret) const { |
| 6257 | ERR_FAIL_INDEX_V(p_caret, carets.size(), -1); |
| 6258 | if (!has_selection(p_caret)) { |
| 6259 | return carets[p_caret].line; |
| 6260 | } |
| 6261 | return MAX(carets[p_caret].selection.origin_line, carets[p_caret].line); |
| 6262 | } |
| 6263 | |
| 6264 | int TextEdit::get_selection_to_column(int p_caret) const { |
| 6265 | ERR_FAIL_INDEX_V(p_caret, carets.size(), -1); |
| 6266 | if (!has_selection(p_caret)) { |
| 6267 | return carets[p_caret].column; |
| 6268 | } |
| 6269 | if (carets[p_caret].selection.origin_line < carets[p_caret].line) { |
| 6270 | return carets[p_caret].column; |
| 6271 | } else if (carets[p_caret].selection.origin_line > carets[p_caret].line) { |
| 6272 | return carets[p_caret].selection.origin_column; |
| 6273 | } else { |
| 6274 | return MAX(carets[p_caret].selection.origin_column, carets[p_caret].column); |
| 6275 | } |
| 6276 | } |
| 6277 | |
| 6278 | bool TextEdit::is_caret_after_selection_origin(int p_caret) const { |
| 6279 | ERR_FAIL_INDEX_V(p_caret, carets.size(), false); |
| 6280 | if (!has_selection(p_caret)) { |
| 6281 | return true; |
| 6282 | } |
| 6283 | return carets[p_caret].line > carets[p_caret].selection.origin_line || (carets[p_caret].line == carets[p_caret].selection.origin_line && carets[p_caret].column >= carets[p_caret].selection.origin_column); |
| 6284 | } |
| 6285 | |
| 6286 | void TextEdit::deselect(int p_caret) { |
| 6287 | ERR_FAIL_COND(p_caret >= carets.size() || p_caret < -1); |
| 6288 | bool selection_changed = false; |
| 6289 | if (p_caret >= 0) { |
| 6290 | selection_changed = carets.write[p_caret].selection.active; |
| 6291 | carets.write[p_caret].selection.active = false; |
| 6292 | } else { |
| 6293 | for (int i = 0; i < carets.size(); i++) { |
| 6294 | selection_changed |= carets.write[i].selection.active; |
| 6295 | carets.write[i].selection.active = false; |
| 6296 | } |
| 6297 | } |
| 6298 | if (selection_changed) { |
| 6299 | _selection_changed(p_caret); |
| 6300 | } |
| 6301 | |
| 6302 | queue_accessibility_update(); |
| 6303 | queue_redraw(); |
| 6304 | } |
| 6305 | |
| 6306 | void TextEdit::delete_selection(int p_caret) { |
| 6307 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 6308 | |
| 6309 | begin_complex_operation(); |
| 6310 | begin_multicaret_edit(); |
| 6311 | for (int i = 0; i < get_caret_count(); i++) { |
| 6312 | if (p_caret != -1 && p_caret != i) { |
| 6313 | continue; |
| 6314 | } |
| 6315 | if (p_caret == -1 && multicaret_edit_ignore_caret(i)) { |
| 6316 | continue; |
| 6317 | } |
| 6318 | |
| 6319 | if (!has_selection(i)) { |
| 6320 | continue; |
| 6321 | } |
| 6322 | |
| 6323 | int selection_from_line = get_selection_from_line(i); |
| 6324 | int selection_from_column = get_selection_from_column(i); |
| 6325 | int selection_to_line = get_selection_to_line(i); |
| 6326 | int selection_to_column = get_selection_to_column(i); |
| 6327 | |
| 6328 | _remove_text(selection_from_line, selection_from_column, selection_to_line, selection_to_column); |
| 6329 | _offset_carets_after(selection_to_line, selection_to_column, selection_from_line, selection_from_column); |
| 6330 | merge_overlapping_carets(); |
| 6331 | |
| 6332 | deselect(i); |
| 6333 | set_caret_line(selection_from_line, false, false, -1, i); |
| 6334 | set_caret_column(selection_from_column, i == 0, i); |
| 6335 | } |
| 6336 | end_multicaret_edit(); |
| 6337 | end_complex_operation(); |
| 6338 | } |
| 6339 | |
| 6340 | /* Line wrapping. */ |
| 6341 | void TextEdit::set_line_wrapping_mode(LineWrappingMode p_wrapping_mode) { |
| 6342 | if (line_wrapping_mode != p_wrapping_mode) { |
| 6343 | line_wrapping_mode = p_wrapping_mode; |
| 6344 | _update_wrap_at_column(true); |
| 6345 | queue_accessibility_update(); |
| 6346 | queue_redraw(); |
| 6347 | } |
| 6348 | } |
| 6349 | |
| 6350 | TextEdit::LineWrappingMode TextEdit::get_line_wrapping_mode() const { |
| 6351 | return line_wrapping_mode; |
| 6352 | } |
| 6353 | |
| 6354 | void TextEdit::set_autowrap_mode(TextServer::AutowrapMode p_mode) { |
| 6355 | if (autowrap_mode == p_mode) { |
| 6356 | return; |
| 6357 | } |
| 6358 | |
| 6359 | autowrap_mode = p_mode; |
| 6360 | if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) { |
| 6361 | _update_wrap_at_column(true); |
| 6362 | queue_accessibility_update(); |
| 6363 | queue_redraw(); |
| 6364 | } |
| 6365 | } |
| 6366 | |
| 6367 | TextServer::AutowrapMode TextEdit::get_autowrap_mode() const { |
| 6368 | return autowrap_mode; |
| 6369 | } |
| 6370 | |
| 6371 | bool TextEdit::is_line_wrapped(int p_line) const { |
| 6372 | ERR_FAIL_INDEX_V(p_line, text.size(), false); |
| 6373 | if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) { |
| 6374 | return false; |
| 6375 | } |
| 6376 | return text.get_line_wrap_amount(p_line) > 0; |
| 6377 | } |
| 6378 | |
| 6379 | int TextEdit::get_line_wrap_count(int p_line) const { |
| 6380 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 6381 | |
| 6382 | if (!is_line_wrapped(p_line)) { |
| 6383 | return 0; |
| 6384 | } |
| 6385 | |
| 6386 | return text.get_line_wrap_amount(p_line); |
| 6387 | } |
| 6388 | |
| 6389 | int TextEdit::get_line_wrap_index_at_column(int p_line, int p_column) const { |
| 6390 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 6391 | ERR_FAIL_COND_V(p_column < 0, 0); |
| 6392 | ERR_FAIL_COND_V(p_column > text.get_text_with_ime(p_line).length(), 0); |
| 6393 | |
| 6394 | if (!is_line_wrapped(p_line)) { |
| 6395 | return 0; |
| 6396 | } |
| 6397 | |
| 6398 | // Loop through wraps in the line text until we get to the column. |
| 6399 | const Vector<Vector2i> line_ranges = text.get_line_wrap_ranges(p_line); |
| 6400 | for (int i = 0; i < line_ranges.size(); i++) { |
| 6401 | if (line_ranges[i].y > p_column) { |
| 6402 | return i; |
| 6403 | } |
| 6404 | } |
| 6405 | return line_ranges.is_empty() ? 0 : line_ranges.size() - 1; |
| 6406 | } |
| 6407 | |
| 6408 | Vector<String> TextEdit::get_line_wrapped_text(int p_line) const { |
| 6409 | ERR_FAIL_INDEX_V(p_line, text.size(), Vector<String>()); |
| 6410 | |
| 6411 | Vector<String> lines; |
| 6412 | if (!is_line_wrapped(p_line)) { |
| 6413 | lines.push_back(text.get_text_with_ime(p_line)); |
| 6414 | return lines; |
| 6415 | } |
| 6416 | |
| 6417 | const String &line_text = text.get_text_with_ime(p_line); |
| 6418 | const Vector<Vector2i> line_ranges = text.get_line_wrap_ranges(p_line); |
| 6419 | lines.reserve(line_ranges.size()); |
| 6420 | for (int i = 0; i < line_ranges.size(); i++) { |
| 6421 | lines.push_back(line_text.substr(line_ranges[i].x, line_ranges[i].y - line_ranges[i].x)); |
| 6422 | } |
| 6423 | |
| 6424 | return lines; |
| 6425 | } |
| 6426 | |
| 6427 | /* Viewport */ |
| 6428 | // Scrolling. |
| 6429 | void TextEdit::set_smooth_scroll_enabled(bool p_enabled) { |
| 6430 | v_scroll->set_smooth_scroll_enabled(p_enabled); |
| 6431 | smooth_scroll_enabled = p_enabled; |
| 6432 | } |
| 6433 | |
| 6434 | bool TextEdit::is_smooth_scroll_enabled() const { |
| 6435 | return smooth_scroll_enabled; |
| 6436 | } |
| 6437 | |
| 6438 | void TextEdit::set_scroll_past_end_of_file_enabled(bool p_enabled) { |
| 6439 | if (scroll_past_end_of_file_enabled == p_enabled) { |
| 6440 | return; |
| 6441 | } |
| 6442 | |
| 6443 | scroll_past_end_of_file_enabled = p_enabled; |
| 6444 | queue_redraw(); |
| 6445 | } |
| 6446 | |
| 6447 | bool TextEdit::is_scroll_past_end_of_file_enabled() const { |
| 6448 | return scroll_past_end_of_file_enabled; |
| 6449 | } |
| 6450 | |
| 6451 | RID TextEdit::get_text_canvas_item() const { |
| 6452 | return text_ci; |
| 6453 | } |
| 6454 | |
| 6455 | VScrollBar *TextEdit::get_v_scroll_bar() const { |
| 6456 | return v_scroll; |
| 6457 | } |
| 6458 | |
| 6459 | HScrollBar *TextEdit::get_h_scroll_bar() const { |
| 6460 | return h_scroll; |
| 6461 | } |
| 6462 | |
| 6463 | void TextEdit::set_v_scroll(double p_scroll) { |
| 6464 | v_scroll->set_value(p_scroll); |
| 6465 | int max_v_scroll = v_scroll->get_max() - v_scroll->get_page(); |
| 6466 | if (p_scroll >= max_v_scroll - 1.0) { |
| 6467 | _scroll_moved(v_scroll->get_value()); |
| 6468 | } |
| 6469 | queue_accessibility_update(); |
| 6470 | } |
| 6471 | |
| 6472 | double TextEdit::get_v_scroll() const { |
| 6473 | return v_scroll->get_value(); |
| 6474 | } |
| 6475 | |
| 6476 | void TextEdit::set_h_scroll(int p_scroll) { |
| 6477 | if (p_scroll < 0) { |
| 6478 | p_scroll = 0; |
| 6479 | } |
| 6480 | h_scroll->set_value(p_scroll); |
| 6481 | queue_accessibility_update(); |
| 6482 | } |
| 6483 | |
| 6484 | int TextEdit::get_h_scroll() const { |
| 6485 | return h_scroll->get_value(); |
| 6486 | } |
| 6487 | |
| 6488 | void TextEdit::set_v_scroll_speed(float p_speed) { |
| 6489 | // Prevent setting a vertical scroll speed value under 1. |
| 6490 | ERR_FAIL_COND(p_speed < 1.0); |
| 6491 | v_scroll_speed = p_speed; |
| 6492 | } |
| 6493 | |
| 6494 | float TextEdit::get_v_scroll_speed() const { |
| 6495 | return v_scroll_speed; |
| 6496 | } |
| 6497 | |
| 6498 | void TextEdit::set_fit_content_height_enabled(bool p_enabled) { |
| 6499 | if (fit_content_height == p_enabled) { |
| 6500 | return; |
| 6501 | } |
| 6502 | fit_content_height = p_enabled; |
| 6503 | update_minimum_size(); |
| 6504 | } |
| 6505 | |
| 6506 | bool TextEdit::is_fit_content_height_enabled() const { |
| 6507 | return fit_content_height; |
| 6508 | } |
| 6509 | |
| 6510 | void TextEdit::set_fit_content_width_enabled(bool p_enabled) { |
| 6511 | if (fit_content_width == p_enabled) { |
| 6512 | return; |
| 6513 | } |
| 6514 | fit_content_width = p_enabled; |
| 6515 | update_minimum_size(); |
| 6516 | } |
| 6517 | |
| 6518 | bool TextEdit::is_fit_content_width_enabled() const { |
| 6519 | return fit_content_width; |
| 6520 | } |
| 6521 | |
| 6522 | double TextEdit::get_scroll_pos_for_line(int p_line, int p_wrap_index) const { |
| 6523 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 6524 | ERR_FAIL_COND_V(p_wrap_index < 0, 0); |
| 6525 | ERR_FAIL_COND_V(p_wrap_index > get_line_wrap_count(p_line), 0); |
| 6526 | |
| 6527 | if (get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE && !_is_hiding_enabled()) { |
| 6528 | return p_line; |
| 6529 | } |
| 6530 | |
| 6531 | double new_line_scroll_pos = 0.0; |
| 6532 | if (p_line > 0) { |
| 6533 | new_line_scroll_pos = get_visible_line_count_in_range(0, MIN(p_line - 1, text.size() - 1)); |
| 6534 | } |
| 6535 | new_line_scroll_pos += p_wrap_index; |
| 6536 | return new_line_scroll_pos; |
| 6537 | } |
| 6538 | |
| 6539 | // Visible lines. |
| 6540 | void TextEdit::set_line_as_first_visible(int p_line, int p_wrap_index) { |
| 6541 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6542 | ERR_FAIL_COND(p_wrap_index < 0); |
| 6543 | ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line)); |
| 6544 | set_v_scroll(get_scroll_pos_for_line(p_line, p_wrap_index)); |
| 6545 | |
| 6546 | scrolling = false; |
| 6547 | minimap_clicked = false; |
| 6548 | } |
| 6549 | |
| 6550 | int TextEdit::get_first_visible_line() const { |
| 6551 | return CLAMP(first_visible_line, 0, text.size() - 1); |
| 6552 | } |
| 6553 | |
| 6554 | void TextEdit::set_line_as_center_visible(int p_line, int p_wrap_index) { |
| 6555 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6556 | ERR_FAIL_COND(p_wrap_index < 0); |
| 6557 | ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line)); |
| 6558 | |
| 6559 | scrolling = false; |
| 6560 | minimap_clicked = false; |
| 6561 | |
| 6562 | int visible_rows = get_visible_line_count(); |
| 6563 | Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, (-visible_rows / 2) - 1); |
| 6564 | int first_line = p_line - next_line.x + 1; |
| 6565 | |
| 6566 | if (first_line < 0) { |
| 6567 | set_v_scroll(0); |
| 6568 | return; |
| 6569 | } |
| 6570 | set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y)); |
| 6571 | } |
| 6572 | |
| 6573 | void TextEdit::set_line_as_last_visible(int p_line, int p_wrap_index) { |
| 6574 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6575 | ERR_FAIL_COND(p_wrap_index < 0); |
| 6576 | ERR_FAIL_COND(p_wrap_index > get_line_wrap_count(p_line)); |
| 6577 | |
| 6578 | scrolling = false; |
| 6579 | minimap_clicked = false; |
| 6580 | |
| 6581 | Point2i next_line = get_next_visible_line_index_offset_from(p_line, p_wrap_index, -get_visible_line_count() - 1); |
| 6582 | int first_line = p_line - next_line.x + 1; |
| 6583 | |
| 6584 | // Adding _get_visible_lines_offset is not 100% correct as we end up showing almost p_line + 1, however, it provides a |
| 6585 | // better user experience. Therefore we need to special case < visible line count, else showing line 0 is impossible. |
| 6586 | if (get_visible_line_count_in_range(0, p_line) < get_visible_line_count() + 1) { |
| 6587 | set_v_scroll(0); |
| 6588 | return; |
| 6589 | } |
| 6590 | set_v_scroll(get_scroll_pos_for_line(first_line, next_line.y) + _get_visible_lines_offset()); |
| 6591 | } |
| 6592 | |
| 6593 | int TextEdit::get_last_full_visible_line() const { |
| 6594 | int first_vis_line = get_first_visible_line(); |
| 6595 | int last_vis_line = 0; |
| 6596 | last_vis_line = first_vis_line + get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, get_visible_line_count()).x - 1; |
| 6597 | last_vis_line = CLAMP(last_vis_line, 0, text.size() - 1); |
| 6598 | return last_vis_line; |
| 6599 | } |
| 6600 | |
| 6601 | int TextEdit::get_last_full_visible_line_wrap_index() const { |
| 6602 | int first_vis_line = get_first_visible_line(); |
| 6603 | return get_next_visible_line_index_offset_from(first_vis_line, first_visible_line_wrap_ofs, get_visible_line_count()).y; |
| 6604 | } |
| 6605 | |
| 6606 | int TextEdit::get_visible_line_count() const { |
| 6607 | return _get_control_height() / get_line_height(); |
| 6608 | } |
| 6609 | |
| 6610 | int TextEdit::get_visible_line_count_in_range(int p_from_line, int p_to_line) const { |
| 6611 | ERR_FAIL_INDEX_V(p_from_line, text.size(), 0); |
| 6612 | ERR_FAIL_INDEX_V(p_to_line, text.size(), 0); |
| 6613 | |
| 6614 | // So we can handle inputs in whatever order. |
| 6615 | if (p_from_line > p_to_line) { |
| 6616 | SWAP(p_from_line, p_to_line); |
| 6617 | } |
| 6618 | |
| 6619 | // Returns the total number of (lines + wrapped - hidden). |
| 6620 | if (!_is_hiding_enabled() && get_line_wrapping_mode() == LineWrappingMode::LINE_WRAPPING_NONE) { |
| 6621 | return (p_to_line - p_from_line) + 1; |
| 6622 | } |
| 6623 | |
| 6624 | int total_rows = 0; |
| 6625 | for (int i = p_from_line; i <= p_to_line; i++) { |
| 6626 | if (!text.is_hidden(i)) { |
| 6627 | total_rows++; |
| 6628 | total_rows += get_line_wrap_count(i); |
| 6629 | } |
| 6630 | } |
| 6631 | return total_rows; |
| 6632 | } |
| 6633 | |
| 6634 | int TextEdit::get_total_visible_line_count() const { |
| 6635 | return text.get_total_visible_line_count(); |
| 6636 | } |
| 6637 | |
| 6638 | // Auto adjust. |
| 6639 | void TextEdit::adjust_viewport_to_caret(int p_caret) { |
| 6640 | ERR_FAIL_INDEX(p_caret, carets.size()); |
| 6641 | |
| 6642 | // Move viewport so the caret is visible on the screen vertically. |
| 6643 | |
| 6644 | int cur_line = get_caret_line(p_caret); |
| 6645 | int cur_wrap = get_caret_wrap_index(p_caret); |
| 6646 | |
| 6647 | int first_vis_line = get_first_visible_line(); |
| 6648 | int first_vis_wrap = first_visible_line_wrap_ofs; |
| 6649 | int last_vis_line = get_last_full_visible_line(); |
| 6650 | int last_vis_wrap = get_last_full_visible_line_wrap_index(); |
| 6651 | |
| 6652 | if (cur_line < first_vis_line || (cur_line == first_vis_line && cur_wrap < first_vis_wrap)) { |
| 6653 | // Caret is above screen. |
| 6654 | set_line_as_first_visible(cur_line, cur_wrap); |
| 6655 | } else if (cur_line > last_vis_line || (cur_line == last_vis_line && cur_wrap > last_vis_wrap)) { |
| 6656 | // Caret is below screen. |
| 6657 | set_line_as_last_visible(cur_line, cur_wrap); |
| 6658 | } |
| 6659 | |
| 6660 | _adjust_viewport_to_caret_horizontally(p_caret, false); |
| 6661 | } |
| 6662 | |
| 6663 | void TextEdit::center_viewport_to_caret(int p_caret) { |
| 6664 | ERR_FAIL_INDEX(p_caret, carets.size()); |
| 6665 | |
| 6666 | // Move viewport so the caret is in the center of the screen vertically. |
| 6667 | scrolling = false; |
| 6668 | minimap_clicked = false; |
| 6669 | |
| 6670 | set_line_as_center_visible(get_caret_line(p_caret), get_caret_wrap_index(p_caret)); |
| 6671 | |
| 6672 | _adjust_viewport_to_caret_horizontally(p_caret); |
| 6673 | } |
| 6674 | |
| 6675 | /* Minimap */ |
| 6676 | void TextEdit::set_draw_minimap(bool p_enabled) { |
| 6677 | if (draw_minimap == p_enabled) { |
| 6678 | return; |
| 6679 | } |
| 6680 | |
| 6681 | draw_minimap = p_enabled; |
| 6682 | _update_wrap_at_column(); |
| 6683 | queue_redraw(); |
| 6684 | } |
| 6685 | |
| 6686 | bool TextEdit::is_drawing_minimap() const { |
| 6687 | return draw_minimap; |
| 6688 | } |
| 6689 | |
| 6690 | void TextEdit::set_minimap_width(int p_minimap_width) { |
| 6691 | if (minimap_width == p_minimap_width) { |
| 6692 | return; |
| 6693 | } |
| 6694 | |
| 6695 | minimap_width = p_minimap_width; |
| 6696 | _update_wrap_at_column(); |
| 6697 | queue_redraw(); |
| 6698 | } |
| 6699 | |
| 6700 | int TextEdit::get_minimap_width() const { |
| 6701 | return minimap_width; |
| 6702 | } |
| 6703 | |
| 6704 | int TextEdit::get_minimap_visible_lines() const { |
| 6705 | return _get_control_height() / (minimap_char_size.y + minimap_line_spacing); |
| 6706 | } |
| 6707 | |
| 6708 | /* Gutters. */ |
| 6709 | void TextEdit::add_gutter(int p_at) { |
| 6710 | if (p_at < 0 || p_at > gutters.size()) { |
| 6711 | gutters.push_back(GutterInfo()); |
| 6712 | } else { |
| 6713 | gutters.insert(p_at, GutterInfo()); |
| 6714 | } |
| 6715 | |
| 6716 | text.add_gutter(p_at); |
| 6717 | |
| 6718 | _update_gutter_width(); |
| 6719 | |
| 6720 | emit_signal(SNAME("gutter_added")); |
| 6721 | queue_redraw(); |
| 6722 | } |
| 6723 | |
| 6724 | void TextEdit::remove_gutter(int p_gutter) { |
| 6725 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6726 | |
| 6727 | gutters.remove_at(p_gutter); |
| 6728 | |
| 6729 | text.remove_gutter(p_gutter); |
| 6730 | |
| 6731 | _update_gutter_width(); |
| 6732 | |
| 6733 | emit_signal(SNAME("gutter_removed")); |
| 6734 | queue_redraw(); |
| 6735 | } |
| 6736 | |
| 6737 | int TextEdit::get_gutter_count() const { |
| 6738 | return gutters.size(); |
| 6739 | } |
| 6740 | |
| 6741 | void TextEdit::set_gutter_name(int p_gutter, const String &p_name) { |
| 6742 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6743 | gutters.write[p_gutter].name = p_name; |
| 6744 | } |
| 6745 | |
| 6746 | String TextEdit::get_gutter_name(int p_gutter) const { |
| 6747 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), ""); |
| 6748 | return gutters[p_gutter].name; |
| 6749 | } |
| 6750 | |
| 6751 | void TextEdit::set_gutter_type(int p_gutter, GutterType p_type) { |
| 6752 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6753 | |
| 6754 | if (gutters[p_gutter].type == p_type) { |
| 6755 | return; |
| 6756 | } |
| 6757 | |
| 6758 | gutters.write[p_gutter].type = p_type; |
| 6759 | queue_redraw(); |
| 6760 | } |
| 6761 | |
| 6762 | TextEdit::GutterType TextEdit::get_gutter_type(int p_gutter) const { |
| 6763 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), GUTTER_TYPE_STRING); |
| 6764 | return gutters[p_gutter].type; |
| 6765 | } |
| 6766 | |
| 6767 | void TextEdit::set_gutter_width(int p_gutter, int p_width) { |
| 6768 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6769 | if (gutters[p_gutter].width == p_width) { |
| 6770 | return; |
| 6771 | } |
| 6772 | gutters.write[p_gutter].width = p_width; |
| 6773 | _update_gutter_width(); |
| 6774 | } |
| 6775 | |
| 6776 | int TextEdit::get_gutter_width(int p_gutter) const { |
| 6777 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), -1); |
| 6778 | return gutters[p_gutter].width; |
| 6779 | } |
| 6780 | |
| 6781 | int TextEdit::get_total_gutter_width() const { |
| 6782 | return gutters_width + gutter_padding; |
| 6783 | } |
| 6784 | |
| 6785 | void TextEdit::set_gutter_draw(int p_gutter, bool p_draw) { |
| 6786 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6787 | if (gutters[p_gutter].draw == p_draw) { |
| 6788 | return; |
| 6789 | } |
| 6790 | gutters.write[p_gutter].draw = p_draw; |
| 6791 | _update_gutter_width(); |
| 6792 | } |
| 6793 | |
| 6794 | bool TextEdit::is_gutter_drawn(int p_gutter) const { |
| 6795 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false); |
| 6796 | return gutters[p_gutter].draw; |
| 6797 | } |
| 6798 | |
| 6799 | void TextEdit::set_gutter_clickable(int p_gutter, bool p_clickable) { |
| 6800 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6801 | |
| 6802 | if (gutters[p_gutter].clickable == p_clickable) { |
| 6803 | return; |
| 6804 | } |
| 6805 | |
| 6806 | gutters.write[p_gutter].clickable = p_clickable; |
| 6807 | queue_redraw(); |
| 6808 | } |
| 6809 | |
| 6810 | bool TextEdit::is_gutter_clickable(int p_gutter) const { |
| 6811 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false); |
| 6812 | return gutters[p_gutter].clickable; |
| 6813 | } |
| 6814 | |
| 6815 | void TextEdit::set_gutter_overwritable(int p_gutter, bool p_overwritable) { |
| 6816 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6817 | gutters.write[p_gutter].overwritable = p_overwritable; |
| 6818 | } |
| 6819 | |
| 6820 | bool TextEdit::is_gutter_overwritable(int p_gutter) const { |
| 6821 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false); |
| 6822 | return gutters[p_gutter].overwritable; |
| 6823 | } |
| 6824 | |
| 6825 | void TextEdit::merge_gutters(int p_from_line, int p_to_line) { |
| 6826 | ERR_FAIL_INDEX(p_from_line, text.size()); |
| 6827 | ERR_FAIL_INDEX(p_to_line, text.size()); |
| 6828 | if (p_from_line == p_to_line) { |
| 6829 | return; |
| 6830 | } |
| 6831 | |
| 6832 | for (int i = 0; i < gutters.size(); i++) { |
| 6833 | if (!gutters[i].overwritable) { |
| 6834 | continue; |
| 6835 | } |
| 6836 | |
| 6837 | if (text.get_line_gutter_text(p_from_line, i) != "") { |
| 6838 | text.set_line_gutter_text(p_to_line, i, text.get_line_gutter_text(p_from_line, i)); |
| 6839 | text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i)); |
| 6840 | } |
| 6841 | |
| 6842 | if (text.get_line_gutter_icon(p_from_line, i).is_valid()) { |
| 6843 | text.set_line_gutter_icon(p_to_line, i, text.get_line_gutter_icon(p_from_line, i)); |
| 6844 | text.set_line_gutter_item_color(p_to_line, i, text.get_line_gutter_item_color(p_from_line, i)); |
| 6845 | } |
| 6846 | |
| 6847 | if (text.get_line_gutter_metadata(p_from_line, i) != "") { |
| 6848 | text.set_line_gutter_metadata(p_to_line, i, text.get_line_gutter_metadata(p_from_line, i)); |
| 6849 | } |
| 6850 | |
| 6851 | if (text.is_line_gutter_clickable(p_from_line, i)) { |
| 6852 | text.set_line_gutter_clickable(p_to_line, i, true); |
| 6853 | } |
| 6854 | } |
| 6855 | queue_redraw(); |
| 6856 | } |
| 6857 | |
| 6858 | void TextEdit::set_gutter_custom_draw(int p_gutter, const Callable &p_draw_callback) { |
| 6859 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6860 | |
| 6861 | if (gutters[p_gutter].custom_draw_callback == p_draw_callback) { |
| 6862 | return; |
| 6863 | } |
| 6864 | |
| 6865 | gutters.write[p_gutter].custom_draw_callback = p_draw_callback; |
| 6866 | queue_redraw(); |
| 6867 | } |
| 6868 | |
| 6869 | // Line gutters. |
| 6870 | void TextEdit::set_line_gutter_metadata(int p_line, int p_gutter, const Variant &p_metadata) { |
| 6871 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6872 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6873 | text.set_line_gutter_metadata(p_line, p_gutter, p_metadata); |
| 6874 | } |
| 6875 | |
| 6876 | Variant TextEdit::get_line_gutter_metadata(int p_line, int p_gutter) const { |
| 6877 | ERR_FAIL_INDEX_V(p_line, text.size(), ""); |
| 6878 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), ""); |
| 6879 | return text.get_line_gutter_metadata(p_line, p_gutter); |
| 6880 | } |
| 6881 | |
| 6882 | void TextEdit::set_line_gutter_text(int p_line, int p_gutter, const String &p_text) { |
| 6883 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6884 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6885 | |
| 6886 | if (text.get_line_gutter_text(p_line, p_gutter) == p_text) { |
| 6887 | return; |
| 6888 | } |
| 6889 | |
| 6890 | text.set_line_gutter_text(p_line, p_gutter, p_text); |
| 6891 | queue_redraw(); |
| 6892 | } |
| 6893 | |
| 6894 | String TextEdit::get_line_gutter_text(int p_line, int p_gutter) const { |
| 6895 | ERR_FAIL_INDEX_V(p_line, text.size(), ""); |
| 6896 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), ""); |
| 6897 | return text.get_line_gutter_text(p_line, p_gutter); |
| 6898 | } |
| 6899 | |
| 6900 | void TextEdit::set_line_gutter_icon(int p_line, int p_gutter, const Ref<Texture2D> &p_icon) { |
| 6901 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6902 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6903 | |
| 6904 | if (text.get_line_gutter_icon(p_line, p_gutter) == p_icon) { |
| 6905 | return; |
| 6906 | } |
| 6907 | |
| 6908 | text.set_line_gutter_icon(p_line, p_gutter, p_icon); |
| 6909 | queue_redraw(); |
| 6910 | } |
| 6911 | |
| 6912 | Ref<Texture2D> TextEdit::get_line_gutter_icon(int p_line, int p_gutter) const { |
| 6913 | ERR_FAIL_INDEX_V(p_line, text.size(), Ref<Texture2D>()); |
| 6914 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Ref<Texture2D>()); |
| 6915 | return text.get_line_gutter_icon(p_line, p_gutter); |
| 6916 | } |
| 6917 | |
| 6918 | void TextEdit::set_line_gutter_item_color(int p_line, int p_gutter, const Color &p_color) { |
| 6919 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6920 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6921 | |
| 6922 | if (text.get_line_gutter_item_color(p_line, p_gutter) == p_color) { |
| 6923 | return; |
| 6924 | } |
| 6925 | |
| 6926 | text.set_line_gutter_item_color(p_line, p_gutter, p_color); |
| 6927 | queue_redraw(); |
| 6928 | } |
| 6929 | |
| 6930 | Color TextEdit::get_line_gutter_item_color(int p_line, int p_gutter) const { |
| 6931 | ERR_FAIL_INDEX_V(p_line, text.size(), Color()); |
| 6932 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), Color()); |
| 6933 | return text.get_line_gutter_item_color(p_line, p_gutter); |
| 6934 | } |
| 6935 | |
| 6936 | void TextEdit::set_line_gutter_clickable(int p_line, int p_gutter, bool p_clickable) { |
| 6937 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6938 | ERR_FAIL_INDEX(p_gutter, gutters.size()); |
| 6939 | text.set_line_gutter_clickable(p_line, p_gutter, p_clickable); |
| 6940 | } |
| 6941 | |
| 6942 | bool TextEdit::is_line_gutter_clickable(int p_line, int p_gutter) const { |
| 6943 | ERR_FAIL_INDEX_V(p_line, text.size(), false); |
| 6944 | ERR_FAIL_INDEX_V(p_gutter, gutters.size(), false); |
| 6945 | return text.is_line_gutter_clickable(p_line, p_gutter); |
| 6946 | } |
| 6947 | |
| 6948 | // Line style |
| 6949 | void TextEdit::set_line_background_color(int p_line, const Color &p_color) { |
| 6950 | ERR_FAIL_INDEX(p_line, text.size()); |
| 6951 | |
| 6952 | if (text.get_line_background_color(p_line) == p_color) { |
| 6953 | return; |
| 6954 | } |
| 6955 | |
| 6956 | text.set_line_background_color(p_line, p_color); |
| 6957 | queue_redraw(); |
| 6958 | } |
| 6959 | |
| 6960 | Color TextEdit::get_line_background_color(int p_line) const { |
| 6961 | ERR_FAIL_INDEX_V(p_line, text.size(), Color()); |
| 6962 | return text.get_line_background_color(p_line); |
| 6963 | } |
| 6964 | |
| 6965 | /* Syntax Highlighting. */ |
| 6966 | void TextEdit::set_syntax_highlighter(Ref<SyntaxHighlighter> p_syntax_highlighter) { |
| 6967 | if (syntax_highlighter == p_syntax_highlighter && syntax_highlighter.is_valid() == p_syntax_highlighter.is_valid()) { |
| 6968 | return; |
| 6969 | } |
| 6970 | |
| 6971 | syntax_highlighter = p_syntax_highlighter; |
| 6972 | if (syntax_highlighter.is_valid()) { |
| 6973 | syntax_highlighter->set_text_edit(this); |
| 6974 | } |
| 6975 | _clear_syntax_highlighting_cache(); |
| 6976 | queue_redraw(); |
| 6977 | } |
| 6978 | |
| 6979 | Ref<SyntaxHighlighter> TextEdit::get_syntax_highlighter() const { |
| 6980 | return syntax_highlighter; |
| 6981 | } |
| 6982 | |
| 6983 | /* Visual. */ |
| 6984 | void TextEdit::set_highlight_current_line(bool p_enabled) { |
| 6985 | if (highlight_current_line == p_enabled) { |
| 6986 | return; |
| 6987 | } |
| 6988 | |
| 6989 | highlight_current_line = p_enabled; |
| 6990 | queue_redraw(); |
| 6991 | } |
| 6992 | |
| 6993 | bool TextEdit::is_highlight_current_line_enabled() const { |
| 6994 | return highlight_current_line; |
| 6995 | } |
| 6996 | |
| 6997 | void TextEdit::set_highlight_all_occurrences(bool p_enabled) { |
| 6998 | if (highlight_all_occurrences == p_enabled) { |
| 6999 | return; |
| 7000 | } |
| 7001 | |
| 7002 | highlight_all_occurrences = p_enabled; |
| 7003 | queue_redraw(); |
| 7004 | } |
| 7005 | |
| 7006 | bool TextEdit::is_highlight_all_occurrences_enabled() const { |
| 7007 | return highlight_all_occurrences; |
| 7008 | } |
| 7009 | |
| 7010 | void TextEdit::set_use_default_word_separators(bool p_enabled) { |
| 7011 | text.set_use_default_word_separators(p_enabled); |
| 7012 | } |
| 7013 | |
| 7014 | bool TextEdit::is_default_word_separators_enabled() const { |
| 7015 | return text.is_default_word_separators_enabled(); |
| 7016 | } |
| 7017 | |
| 7018 | // Set word separators. Combine default separators with custom separators if those options are enabled. |
| 7019 | void TextEdit::set_custom_word_separators(const String &p_separators) { |
| 7020 | text.set_custom_word_separators(p_separators); |
| 7021 | } |
| 7022 | |
| 7023 | void TextEdit::Text::set_custom_word_separators(const String &p_separators) { |
| 7024 | if (custom_word_separators == p_separators) { |
| 7025 | return; |
| 7026 | } |
| 7027 | custom_word_separators = p_separators; |
| 7028 | invalidate_all_lines(); |
| 7029 | } |
| 7030 | |
| 7031 | bool TextEdit::is_custom_word_separators_enabled() const { |
| 7032 | return text.is_custom_word_separators_enabled(); |
| 7033 | } |
| 7034 | |
| 7035 | String TextEdit::get_custom_word_separators() const { |
| 7036 | return text.get_custom_word_separators(); |
| 7037 | } |
| 7038 | |
| 7039 | // Enable or disable custom word separators. |
| 7040 | void TextEdit::set_use_custom_word_separators(bool p_enabled) { |
| 7041 | text.set_use_custom_word_separators(p_enabled); |
| 7042 | } |
| 7043 | |
| 7044 | String TextEdit::get_default_word_separators() const { |
| 7045 | return text.get_default_word_separators(); |
| 7046 | } |
| 7047 | |
| 7048 | void TextEdit::set_draw_control_chars(bool p_enabled) { |
| 7049 | if (draw_control_chars != p_enabled) { |
| 7050 | draw_control_chars = p_enabled; |
| 7051 | if (menu) { |
| 7052 | menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars); |
| 7053 | } |
| 7054 | text.set_draw_control_chars(draw_control_chars); |
| 7055 | text.invalidate_font(); |
| 7056 | _update_placeholder(); |
| 7057 | queue_accessibility_update(); |
| 7058 | queue_redraw(); |
| 7059 | } |
| 7060 | } |
| 7061 | |
| 7062 | bool TextEdit::get_draw_control_chars() const { |
| 7063 | return draw_control_chars; |
| 7064 | } |
| 7065 | |
| 7066 | void TextEdit::set_draw_tabs(bool p_enabled) { |
| 7067 | if (draw_tabs == p_enabled) { |
| 7068 | return; |
| 7069 | } |
| 7070 | |
| 7071 | draw_tabs = p_enabled; |
| 7072 | queue_redraw(); |
| 7073 | } |
| 7074 | |
| 7075 | bool TextEdit::is_drawing_tabs() const { |
| 7076 | return draw_tabs; |
| 7077 | } |
| 7078 | |
| 7079 | void TextEdit::set_draw_spaces(bool p_enabled) { |
| 7080 | if (draw_spaces == p_enabled) { |
| 7081 | return; |
| 7082 | } |
| 7083 | |
| 7084 | draw_spaces = p_enabled; |
| 7085 | queue_redraw(); |
| 7086 | } |
| 7087 | |
| 7088 | bool TextEdit::is_drawing_spaces() const { |
| 7089 | return draw_spaces; |
| 7090 | } |
| 7091 | |
| 7092 | Color TextEdit::get_font_color() const { |
| 7093 | return theme_cache.font_color; |
| 7094 | } |
| 7095 | |
| 7096 | void TextEdit::_bind_methods() { |
| 7097 | // Private API. |
| 7098 | ClassDB::bind_method(D_METHOD("_set_text", "text", "emit_signal"), &TextEdit::_set_text, DEFVAL(false)); |
| 7099 | |
| 7100 | // Public API. |
| 7101 | |
| 7102 | /* Text */ |
| 7103 | // Text properties |
| 7104 | ClassDB::bind_method(D_METHOD("has_ime_text"), &TextEdit::has_ime_text); |
| 7105 | ClassDB::bind_method(D_METHOD("cancel_ime"), &TextEdit::cancel_ime); |
| 7106 | ClassDB::bind_method(D_METHOD("apply_ime"), &TextEdit::apply_ime); |
| 7107 | |
| 7108 | ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &TextEdit::set_editable); |
| 7109 | ClassDB::bind_method(D_METHOD("is_editable"), &TextEdit::is_editable); |
| 7110 | |
| 7111 | ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &TextEdit::set_text_direction); |
| 7112 | ClassDB::bind_method(D_METHOD("get_text_direction"), &TextEdit::get_text_direction); |
| 7113 | |
| 7114 | ClassDB::bind_method(D_METHOD("set_language", "language"), &TextEdit::set_language); |
| 7115 | ClassDB::bind_method(D_METHOD("get_language"), &TextEdit::get_language); |
| 7116 | |
| 7117 | ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &TextEdit::set_structured_text_bidi_override); |
| 7118 | ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &TextEdit::get_structured_text_bidi_override); |
| 7119 | ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &TextEdit::set_structured_text_bidi_override_options); |
| 7120 | ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &TextEdit::get_structured_text_bidi_override_options); |
| 7121 | |
| 7122 | ClassDB::bind_method(D_METHOD("set_tab_size", "size"), &TextEdit::set_tab_size); |
| 7123 | ClassDB::bind_method(D_METHOD("get_tab_size"), &TextEdit::get_tab_size); |
| 7124 | |
| 7125 | ClassDB::bind_method(D_METHOD("set_indent_wrapped_lines", "enabled"), &TextEdit::set_indent_wrapped_lines); |
| 7126 | ClassDB::bind_method(D_METHOD("is_indent_wrapped_lines"), &TextEdit::is_indent_wrapped_lines); |
| 7127 | |
| 7128 | ClassDB::bind_method(D_METHOD("set_tab_input_mode", "enabled"), &TextEdit::set_tab_input_mode); |
| 7129 | ClassDB::bind_method(D_METHOD("get_tab_input_mode"), &TextEdit::get_tab_input_mode); |
| 7130 | |
| 7131 | // User controls |
| 7132 | ClassDB::bind_method(D_METHOD("set_overtype_mode_enabled", "enabled"), &TextEdit::set_overtype_mode_enabled); |
| 7133 | ClassDB::bind_method(D_METHOD("is_overtype_mode_enabled"), &TextEdit::is_overtype_mode_enabled); |
| 7134 | |
| 7135 | ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enabled"), &TextEdit::set_context_menu_enabled); |
| 7136 | ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &TextEdit::is_context_menu_enabled); |
| 7137 | |
| 7138 | ClassDB::bind_method(D_METHOD("set_emoji_menu_enabled", "enable"), &TextEdit::set_emoji_menu_enabled); |
| 7139 | ClassDB::bind_method(D_METHOD("is_emoji_menu_enabled"), &TextEdit::is_emoji_menu_enabled); |
| 7140 | |
| 7141 | ClassDB::bind_method(D_METHOD("set_backspace_deletes_composite_character_enabled", "enable"), &TextEdit::set_backspace_deletes_composite_character_enabled); |
| 7142 | ClassDB::bind_method(D_METHOD("is_backspace_deletes_composite_character_enabled"), &TextEdit::is_backspace_deletes_composite_character_enabled); |
| 7143 | |
| 7144 | ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enabled"), &TextEdit::set_shortcut_keys_enabled); |
| 7145 | ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &TextEdit::is_shortcut_keys_enabled); |
| 7146 | |
| 7147 | ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enabled"), &TextEdit::set_virtual_keyboard_enabled); |
| 7148 | ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &TextEdit::is_virtual_keyboard_enabled); |
| 7149 | |
| 7150 | ClassDB::bind_method(D_METHOD("set_virtual_keyboard_show_on_focus", "show_on_focus"), &TextEdit::set_virtual_keyboard_show_on_focus); |
| 7151 | ClassDB::bind_method(D_METHOD("get_virtual_keyboard_show_on_focus"), &TextEdit::get_virtual_keyboard_show_on_focus); |
| 7152 | |
| 7153 | ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enabled"), &TextEdit::set_middle_mouse_paste_enabled); |
| 7154 | ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &TextEdit::is_middle_mouse_paste_enabled); |
| 7155 | |
| 7156 | ClassDB::bind_method(D_METHOD("set_empty_selection_clipboard_enabled", "enabled"), &TextEdit::set_empty_selection_clipboard_enabled); |
| 7157 | ClassDB::bind_method(D_METHOD("is_empty_selection_clipboard_enabled"), &TextEdit::is_empty_selection_clipboard_enabled); |
| 7158 | |
| 7159 | // Text manipulation |
| 7160 | ClassDB::bind_method(D_METHOD("clear"), &TextEdit::clear); |
| 7161 | |
| 7162 | ClassDB::bind_method(D_METHOD("set_text", "text"), &TextEdit::set_text); |
| 7163 | ClassDB::bind_method(D_METHOD("get_text"), &TextEdit::get_text); |
| 7164 | |
| 7165 | ClassDB::bind_method(D_METHOD("get_line_count"), &TextEdit::get_line_count); |
| 7166 | |
| 7167 | ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &TextEdit::set_placeholder); |
| 7168 | ClassDB::bind_method(D_METHOD("get_placeholder"), &TextEdit::get_placeholder); |
| 7169 | |
| 7170 | ClassDB::bind_method(D_METHOD("set_line", "line", "new_text"), &TextEdit::set_line); |
| 7171 | ClassDB::bind_method(D_METHOD("get_line", "line"), &TextEdit::get_line); |
| 7172 | ClassDB::bind_method(D_METHOD("get_line_with_ime", "line"), &TextEdit::get_line_with_ime); |
| 7173 | |
| 7174 | ClassDB::bind_method(D_METHOD("get_line_width", "line", "wrap_index"), &TextEdit::get_line_width, DEFVAL(-1)); |
| 7175 | ClassDB::bind_method(D_METHOD("get_line_height"), &TextEdit::get_line_height); |
| 7176 | |
| 7177 | ClassDB::bind_method(D_METHOD("get_indent_level", "line"), &TextEdit::get_indent_level); |
| 7178 | ClassDB::bind_method(D_METHOD("get_first_non_whitespace_column", "line"), &TextEdit::get_first_non_whitespace_column); |
| 7179 | |
| 7180 | ClassDB::bind_method(D_METHOD("swap_lines", "from_line", "to_line"), &TextEdit::swap_lines); |
| 7181 | |
| 7182 | ClassDB::bind_method(D_METHOD("insert_line_at", "line", "text"), &TextEdit::insert_line_at); |
| 7183 | ClassDB::bind_method(D_METHOD("remove_line_at", "line", "move_carets_down"), &TextEdit::remove_line_at, DEFVAL(true)); |
| 7184 | ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text", "caret_index"), &TextEdit::insert_text_at_caret, DEFVAL(-1)); |
| 7185 | |
| 7186 | ClassDB::bind_method(D_METHOD("insert_text", "text", "line", "column", "before_selection_begin", "before_selection_end"), &TextEdit::insert_text, DEFVAL(true), DEFVAL(false)); |
| 7187 | ClassDB::bind_method(D_METHOD("remove_text", "from_line", "from_column", "to_line", "to_column"), &TextEdit::remove_text); |
| 7188 | |
| 7189 | ClassDB::bind_method(D_METHOD("get_last_unhidden_line"), &TextEdit::get_last_unhidden_line); |
| 7190 | ClassDB::bind_method(D_METHOD("get_next_visible_line_offset_from", "line", "visible_amount"), &TextEdit::get_next_visible_line_offset_from); |
| 7191 | ClassDB::bind_method(D_METHOD("get_next_visible_line_index_offset_from", "line", "wrap_index", "visible_amount"), &TextEdit::get_next_visible_line_index_offset_from); |
| 7192 | |
| 7193 | // Overridable actions |
| 7194 | ClassDB::bind_method(D_METHOD("backspace", "caret_index"), &TextEdit::backspace, DEFVAL(-1)); |
| 7195 | |
| 7196 | ClassDB::bind_method(D_METHOD("cut", "caret_index"), &TextEdit::cut, DEFVAL(-1)); |
| 7197 | ClassDB::bind_method(D_METHOD("copy", "caret_index"), &TextEdit::copy, DEFVAL(-1)); |
| 7198 | ClassDB::bind_method(D_METHOD("paste", "caret_index"), &TextEdit::paste, DEFVAL(-1)); |
| 7199 | ClassDB::bind_method(D_METHOD("paste_primary_clipboard", "caret_index"), &TextEdit::paste_primary_clipboard, DEFVAL(-1)); |
| 7200 | |
| 7201 | GDVIRTUAL_BIND(_handle_unicode_input, "unicode_char", "caret_index") |
| 7202 | GDVIRTUAL_BIND(_backspace, "caret_index") |
| 7203 | GDVIRTUAL_BIND(_cut, "caret_index") |
| 7204 | GDVIRTUAL_BIND(_copy, "caret_index") |
| 7205 | GDVIRTUAL_BIND(_paste, "caret_index") |
| 7206 | GDVIRTUAL_BIND(_paste_primary_clipboard, "caret_index") |
| 7207 | |
| 7208 | // Context Menu |
| 7209 | BIND_ENUM_CONSTANT(MENU_CUT); |
| 7210 | BIND_ENUM_CONSTANT(MENU_COPY); |
| 7211 | BIND_ENUM_CONSTANT(MENU_PASTE); |
| 7212 | BIND_ENUM_CONSTANT(MENU_CLEAR); |
| 7213 | BIND_ENUM_CONSTANT(MENU_SELECT_ALL); |
| 7214 | BIND_ENUM_CONSTANT(MENU_UNDO); |
| 7215 | BIND_ENUM_CONSTANT(MENU_REDO); |
| 7216 | BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR); |
| 7217 | BIND_ENUM_CONSTANT(MENU_DIR_INHERITED); |
| 7218 | BIND_ENUM_CONSTANT(MENU_DIR_AUTO); |
| 7219 | BIND_ENUM_CONSTANT(MENU_DIR_LTR); |
| 7220 | BIND_ENUM_CONSTANT(MENU_DIR_RTL); |
| 7221 | BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC); |
| 7222 | BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC); |
| 7223 | BIND_ENUM_CONSTANT(MENU_INSERT_LRM); |
| 7224 | BIND_ENUM_CONSTANT(MENU_INSERT_RLM); |
| 7225 | BIND_ENUM_CONSTANT(MENU_INSERT_LRE); |
| 7226 | BIND_ENUM_CONSTANT(MENU_INSERT_RLE); |
| 7227 | BIND_ENUM_CONSTANT(MENU_INSERT_LRO); |
| 7228 | BIND_ENUM_CONSTANT(MENU_INSERT_RLO); |
| 7229 | BIND_ENUM_CONSTANT(MENU_INSERT_PDF); |
| 7230 | BIND_ENUM_CONSTANT(MENU_INSERT_ALM); |
| 7231 | BIND_ENUM_CONSTANT(MENU_INSERT_LRI); |
| 7232 | BIND_ENUM_CONSTANT(MENU_INSERT_RLI); |
| 7233 | BIND_ENUM_CONSTANT(MENU_INSERT_FSI); |
| 7234 | BIND_ENUM_CONSTANT(MENU_INSERT_PDI); |
| 7235 | BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ); |
| 7236 | BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ); |
| 7237 | BIND_ENUM_CONSTANT(MENU_INSERT_WJ); |
| 7238 | BIND_ENUM_CONSTANT(MENU_INSERT_SHY); |
| 7239 | BIND_ENUM_CONSTANT(MENU_EMOJI_AND_SYMBOL); |
| 7240 | BIND_ENUM_CONSTANT(MENU_MAX); |
| 7241 | |
| 7242 | /* Versioning */ |
| 7243 | BIND_ENUM_CONSTANT(ACTION_NONE); |
| 7244 | BIND_ENUM_CONSTANT(ACTION_TYPING); |
| 7245 | BIND_ENUM_CONSTANT(ACTION_BACKSPACE); |
| 7246 | BIND_ENUM_CONSTANT(ACTION_DELETE); |
| 7247 | |
| 7248 | ClassDB::bind_method(D_METHOD("start_action", "action"), &TextEdit::start_action); |
| 7249 | ClassDB::bind_method(D_METHOD("end_action"), &TextEdit::end_complex_operation); |
| 7250 | |
| 7251 | ClassDB::bind_method(D_METHOD("begin_complex_operation"), &TextEdit::begin_complex_operation); |
| 7252 | ClassDB::bind_method(D_METHOD("end_complex_operation"), &TextEdit::end_complex_operation); |
| 7253 | |
| 7254 | ClassDB::bind_method(D_METHOD("has_undo"), &TextEdit::has_undo); |
| 7255 | ClassDB::bind_method(D_METHOD("has_redo"), &TextEdit::has_redo); |
| 7256 | ClassDB::bind_method(D_METHOD("undo"), &TextEdit::undo); |
| 7257 | ClassDB::bind_method(D_METHOD("redo"), &TextEdit::redo); |
| 7258 | ClassDB::bind_method(D_METHOD("clear_undo_history"), &TextEdit::clear_undo_history); |
| 7259 | |
| 7260 | ClassDB::bind_method(D_METHOD("tag_saved_version"), &TextEdit::tag_saved_version); |
| 7261 | |
| 7262 | ClassDB::bind_method(D_METHOD("get_version"), &TextEdit::get_version); |
| 7263 | ClassDB::bind_method(D_METHOD("get_saved_version"), &TextEdit::get_saved_version); |
| 7264 | |
| 7265 | /* Search */ |
| 7266 | BIND_ENUM_CONSTANT(SEARCH_MATCH_CASE); |
| 7267 | BIND_ENUM_CONSTANT(SEARCH_WHOLE_WORDS); |
| 7268 | BIND_ENUM_CONSTANT(SEARCH_BACKWARDS); |
| 7269 | |
| 7270 | ClassDB::bind_method(D_METHOD("set_search_text", "search_text"), &TextEdit::set_search_text); |
| 7271 | ClassDB::bind_method(D_METHOD("set_search_flags", "flags"), &TextEdit::set_search_flags); |
| 7272 | |
| 7273 | ClassDB::bind_method(D_METHOD("search", "text", "flags", "from_line", "from_column"), &TextEdit::search); |
| 7274 | |
| 7275 | /* Tooltip */ |
| 7276 | ClassDB::bind_method(D_METHOD("set_tooltip_request_func", "callback"), &TextEdit::set_tooltip_request_func); |
| 7277 | |
| 7278 | /* Mouse */ |
| 7279 | ClassDB::bind_method(D_METHOD("get_local_mouse_pos"), &TextEdit::get_local_mouse_pos); |
| 7280 | |
| 7281 | ClassDB::bind_method(D_METHOD("get_word_at_pos", "position"), &TextEdit::get_word_at_pos); |
| 7282 | |
| 7283 | ClassDB::bind_method(D_METHOD("get_line_column_at_pos", "position", "clamp_line", "clamp_column"), &TextEdit::get_line_column_at_pos, DEFVAL(true), DEFVAL(true)); |
| 7284 | ClassDB::bind_method(D_METHOD("get_pos_at_line_column", "line", "column"), &TextEdit::get_pos_at_line_column); |
| 7285 | ClassDB::bind_method(D_METHOD("get_rect_at_line_column", "line", "column"), &TextEdit::get_rect_at_line_column); |
| 7286 | |
| 7287 | ClassDB::bind_method(D_METHOD("get_minimap_line_at_pos", "position"), &TextEdit::get_minimap_line_at_pos); |
| 7288 | |
| 7289 | ClassDB::bind_method(D_METHOD("is_dragging_cursor"), &TextEdit::is_dragging_cursor); |
| 7290 | ClassDB::bind_method(D_METHOD("is_mouse_over_selection", "edges", "caret_index"), &TextEdit::is_mouse_over_selection, DEFVAL(-1)); |
| 7291 | |
| 7292 | /* Caret. */ |
| 7293 | BIND_ENUM_CONSTANT(CARET_TYPE_LINE); |
| 7294 | BIND_ENUM_CONSTANT(CARET_TYPE_BLOCK); |
| 7295 | |
| 7296 | ClassDB::bind_method(D_METHOD("set_caret_type", "type"), &TextEdit::set_caret_type); |
| 7297 | ClassDB::bind_method(D_METHOD("get_caret_type"), &TextEdit::get_caret_type); |
| 7298 | |
| 7299 | ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enable"), &TextEdit::set_caret_blink_enabled); |
| 7300 | ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &TextEdit::is_caret_blink_enabled); |
| 7301 | |
| 7302 | ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &TextEdit::set_caret_blink_interval); |
| 7303 | ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &TextEdit::get_caret_blink_interval); |
| 7304 | |
| 7305 | ClassDB::bind_method(D_METHOD("set_draw_caret_when_editable_disabled", "enable"), &TextEdit::set_draw_caret_when_editable_disabled); |
| 7306 | ClassDB::bind_method(D_METHOD("is_drawing_caret_when_editable_disabled"), &TextEdit::is_drawing_caret_when_editable_disabled); |
| 7307 | |
| 7308 | ClassDB::bind_method(D_METHOD("set_move_caret_on_right_click_enabled", "enable"), &TextEdit::set_move_caret_on_right_click_enabled); |
| 7309 | ClassDB::bind_method(D_METHOD("is_move_caret_on_right_click_enabled"), &TextEdit::is_move_caret_on_right_click_enabled); |
| 7310 | |
| 7311 | ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &TextEdit::set_caret_mid_grapheme_enabled); |
| 7312 | ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &TextEdit::is_caret_mid_grapheme_enabled); |
| 7313 | |
| 7314 | ClassDB::bind_method(D_METHOD("set_multiple_carets_enabled", "enabled"), &TextEdit::set_multiple_carets_enabled); |
| 7315 | ClassDB::bind_method(D_METHOD("is_multiple_carets_enabled"), &TextEdit::is_multiple_carets_enabled); |
| 7316 | |
| 7317 | ClassDB::bind_method(D_METHOD("add_caret", "line", "column"), &TextEdit::add_caret); |
| 7318 | ClassDB::bind_method(D_METHOD("remove_caret", "caret"), &TextEdit::remove_caret); |
| 7319 | ClassDB::bind_method(D_METHOD("remove_secondary_carets"), &TextEdit::remove_secondary_carets); |
| 7320 | ClassDB::bind_method(D_METHOD("get_caret_count"), &TextEdit::get_caret_count); |
| 7321 | ClassDB::bind_method(D_METHOD("add_caret_at_carets", "below"), &TextEdit::add_caret_at_carets); |
| 7322 | |
| 7323 | ClassDB::bind_method(D_METHOD("get_sorted_carets", "include_ignored_carets"), &TextEdit::get_sorted_carets, DEFVAL(false)); |
| 7324 | ClassDB::bind_method(D_METHOD("collapse_carets", "from_line", "from_column", "to_line", "to_column", "inclusive"), &TextEdit::collapse_carets, DEFVAL(false)); |
| 7325 | |
| 7326 | ClassDB::bind_method(D_METHOD("merge_overlapping_carets"), &TextEdit::merge_overlapping_carets); |
| 7327 | ClassDB::bind_method(D_METHOD("begin_multicaret_edit"), &TextEdit::begin_multicaret_edit); |
| 7328 | ClassDB::bind_method(D_METHOD("end_multicaret_edit"), &TextEdit::end_multicaret_edit); |
| 7329 | ClassDB::bind_method(D_METHOD("is_in_mulitcaret_edit"), &TextEdit::is_in_mulitcaret_edit); |
| 7330 | ClassDB::bind_method(D_METHOD("multicaret_edit_ignore_caret", "caret_index"), &TextEdit::multicaret_edit_ignore_caret); |
| 7331 | |
| 7332 | ClassDB::bind_method(D_METHOD("is_caret_visible", "caret_index"), &TextEdit::is_caret_visible, DEFVAL(0)); |
| 7333 | ClassDB::bind_method(D_METHOD("get_caret_draw_pos", "caret_index"), &TextEdit::get_caret_draw_pos, DEFVAL(0)); |
| 7334 | |
| 7335 | ClassDB::bind_method(D_METHOD("set_caret_line", "line", "adjust_viewport", "can_be_hidden", "wrap_index", "caret_index"), &TextEdit::set_caret_line, DEFVAL(true), DEFVAL(true), DEFVAL(0), DEFVAL(0)); |
| 7336 | ClassDB::bind_method(D_METHOD("get_caret_line", "caret_index"), &TextEdit::get_caret_line, DEFVAL(0)); |
| 7337 | |
| 7338 | ClassDB::bind_method(D_METHOD("set_caret_column", "column", "adjust_viewport", "caret_index"), &TextEdit::set_caret_column, DEFVAL(true), DEFVAL(0)); |
| 7339 | ClassDB::bind_method(D_METHOD("get_caret_column", "caret_index"), &TextEdit::get_caret_column, DEFVAL(0)); |
| 7340 | ClassDB::bind_method(D_METHOD("get_next_composite_character_column", "line", "column"), &TextEdit::get_next_composite_character_column); |
| 7341 | ClassDB::bind_method(D_METHOD("get_previous_composite_character_column", "line", "column"), &TextEdit::get_previous_composite_character_column); |
| 7342 | |
| 7343 | ClassDB::bind_method(D_METHOD("get_caret_wrap_index", "caret_index"), &TextEdit::get_caret_wrap_index, DEFVAL(0)); |
| 7344 | |
| 7345 | ClassDB::bind_method(D_METHOD("get_word_under_caret", "caret_index"), &TextEdit::get_word_under_caret, DEFVAL(-1)); |
| 7346 | |
| 7347 | ClassDB::bind_method(D_METHOD("set_use_default_word_separators", "enabled"), &TextEdit::set_use_default_word_separators); |
| 7348 | ClassDB::bind_method(D_METHOD("is_default_word_separators_enabled"), &TextEdit::is_default_word_separators_enabled); |
| 7349 | |
| 7350 | ClassDB::bind_method(D_METHOD("set_use_custom_word_separators", "enabled"), &TextEdit::set_use_custom_word_separators); |
| 7351 | ClassDB::bind_method(D_METHOD("is_custom_word_separators_enabled"), &TextEdit::is_custom_word_separators_enabled); |
| 7352 | ClassDB::bind_method(D_METHOD("set_custom_word_separators", "custom_word_separators"), &TextEdit::set_custom_word_separators); |
| 7353 | ClassDB::bind_method(D_METHOD("get_custom_word_separators"), &TextEdit::get_custom_word_separators); |
| 7354 | |
| 7355 | /* Selection. */ |
| 7356 | BIND_ENUM_CONSTANT(SELECTION_MODE_NONE); |
| 7357 | BIND_ENUM_CONSTANT(SELECTION_MODE_SHIFT); |
| 7358 | BIND_ENUM_CONSTANT(SELECTION_MODE_POINTER); |
| 7359 | BIND_ENUM_CONSTANT(SELECTION_MODE_WORD); |
| 7360 | BIND_ENUM_CONSTANT(SELECTION_MODE_LINE); |
| 7361 | |
| 7362 | ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &TextEdit::set_selecting_enabled); |
| 7363 | ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &TextEdit::is_selecting_enabled); |
| 7364 | |
| 7365 | ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &TextEdit::set_deselect_on_focus_loss_enabled); |
| 7366 | ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &TextEdit::is_deselect_on_focus_loss_enabled); |
| 7367 | |
| 7368 | ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &TextEdit::set_drag_and_drop_selection_enabled); |
| 7369 | ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &TextEdit::is_drag_and_drop_selection_enabled); |
| 7370 | |
| 7371 | ClassDB::bind_method(D_METHOD("set_selection_mode", "mode"), &TextEdit::set_selection_mode); |
| 7372 | ClassDB::bind_method(D_METHOD("get_selection_mode"), &TextEdit::get_selection_mode); |
| 7373 | |
| 7374 | ClassDB::bind_method(D_METHOD("select_all"), &TextEdit::select_all); |
| 7375 | ClassDB::bind_method(D_METHOD("select_word_under_caret", "caret_index"), &TextEdit::select_word_under_caret, DEFVAL(-1)); |
| 7376 | ClassDB::bind_method(D_METHOD("add_selection_for_next_occurrence"), &TextEdit::add_selection_for_next_occurrence); |
| 7377 | ClassDB::bind_method(D_METHOD("skip_selection_for_next_occurrence"), &TextEdit::skip_selection_for_next_occurrence); |
| 7378 | ClassDB::bind_method(D_METHOD("select", "origin_line", "origin_column", "caret_line", "caret_column", "caret_index"), &TextEdit::select, DEFVAL(0)); |
| 7379 | |
| 7380 | ClassDB::bind_method(D_METHOD("has_selection", "caret_index"), &TextEdit::has_selection, DEFVAL(-1)); |
| 7381 | |
| 7382 | ClassDB::bind_method(D_METHOD("get_selected_text", "caret_index"), &TextEdit::get_selected_text, DEFVAL(-1)); |
| 7383 | ClassDB::bind_method(D_METHOD("get_selection_at_line_column", "line", "column", "include_edges", "only_selections"), &TextEdit::get_selection_at_line_column, DEFVAL(true), DEFVAL(true)); |
| 7384 | ClassDB::bind_method(D_METHOD("get_line_ranges_from_carets", "only_selections", "merge_adjacent"), &TextEdit::get_line_ranges_from_carets_typed_array, DEFVAL(false), DEFVAL(true)); |
| 7385 | |
| 7386 | ClassDB::bind_method(D_METHOD("get_selection_origin_line", "caret_index"), &TextEdit::get_selection_origin_line, DEFVAL(0)); |
| 7387 | ClassDB::bind_method(D_METHOD("get_selection_origin_column", "caret_index"), &TextEdit::get_selection_origin_column, DEFVAL(0)); |
| 7388 | ClassDB::bind_method(D_METHOD("set_selection_origin_line", "line", "can_be_hidden", "wrap_index", "caret_index"), &TextEdit::set_selection_origin_line, DEFVAL(true), DEFVAL(-1), DEFVAL(0)); |
| 7389 | ClassDB::bind_method(D_METHOD("set_selection_origin_column", "column", "caret_index"), &TextEdit::set_selection_origin_column, DEFVAL(0)); |
| 7390 | |
| 7391 | ClassDB::bind_method(D_METHOD("get_selection_from_line", "caret_index"), &TextEdit::get_selection_from_line, DEFVAL(0)); |
| 7392 | ClassDB::bind_method(D_METHOD("get_selection_from_column", "caret_index"), &TextEdit::get_selection_from_column, DEFVAL(0)); |
| 7393 | ClassDB::bind_method(D_METHOD("get_selection_to_line", "caret_index"), &TextEdit::get_selection_to_line, DEFVAL(0)); |
| 7394 | ClassDB::bind_method(D_METHOD("get_selection_to_column", "caret_index"), &TextEdit::get_selection_to_column, DEFVAL(0)); |
| 7395 | |
| 7396 | ClassDB::bind_method(D_METHOD("is_caret_after_selection_origin", "caret_index"), &TextEdit::is_caret_after_selection_origin, DEFVAL(0)); |
| 7397 | |
| 7398 | ClassDB::bind_method(D_METHOD("deselect", "caret_index"), &TextEdit::deselect, DEFVAL(-1)); |
| 7399 | ClassDB::bind_method(D_METHOD("delete_selection", "caret_index"), &TextEdit::delete_selection, DEFVAL(-1)); |
| 7400 | |
| 7401 | /* Line wrapping. */ |
| 7402 | BIND_ENUM_CONSTANT(LINE_WRAPPING_NONE); |
| 7403 | BIND_ENUM_CONSTANT(LINE_WRAPPING_BOUNDARY); |
| 7404 | |
| 7405 | ClassDB::bind_method(D_METHOD("set_line_wrapping_mode", "mode"), &TextEdit::set_line_wrapping_mode); |
| 7406 | ClassDB::bind_method(D_METHOD("get_line_wrapping_mode"), &TextEdit::get_line_wrapping_mode); |
| 7407 | |
| 7408 | ClassDB::bind_method(D_METHOD("set_autowrap_mode", "autowrap_mode"), &TextEdit::set_autowrap_mode); |
| 7409 | ClassDB::bind_method(D_METHOD("get_autowrap_mode"), &TextEdit::get_autowrap_mode); |
| 7410 | |
| 7411 | ClassDB::bind_method(D_METHOD("is_line_wrapped", "line"), &TextEdit::is_line_wrapped); |
| 7412 | ClassDB::bind_method(D_METHOD("get_line_wrap_count", "line"), &TextEdit::get_line_wrap_count); |
| 7413 | ClassDB::bind_method(D_METHOD("get_line_wrap_index_at_column", "line", "column"), &TextEdit::get_line_wrap_index_at_column); |
| 7414 | |
| 7415 | ClassDB::bind_method(D_METHOD("get_line_wrapped_text", "line"), &TextEdit::get_line_wrapped_text); |
| 7416 | |
| 7417 | /* Viewport. */ |
| 7418 | // Scrolling. |
| 7419 | ClassDB::bind_method(D_METHOD("set_smooth_scroll_enabled", "enable"), &TextEdit::set_smooth_scroll_enabled); |
| 7420 | ClassDB::bind_method(D_METHOD("is_smooth_scroll_enabled"), &TextEdit::is_smooth_scroll_enabled); |
| 7421 | |
| 7422 | ClassDB::bind_method(D_METHOD("get_v_scroll_bar"), &TextEdit::get_v_scroll_bar); |
| 7423 | ClassDB::bind_method(D_METHOD("get_h_scroll_bar"), &TextEdit::get_h_scroll_bar); |
| 7424 | |
| 7425 | ClassDB::bind_method(D_METHOD("set_v_scroll", "value"), &TextEdit::set_v_scroll); |
| 7426 | ClassDB::bind_method(D_METHOD("get_v_scroll"), &TextEdit::get_v_scroll); |
| 7427 | |
| 7428 | ClassDB::bind_method(D_METHOD("set_h_scroll", "value"), &TextEdit::set_h_scroll); |
| 7429 | ClassDB::bind_method(D_METHOD("get_h_scroll"), &TextEdit::get_h_scroll); |
| 7430 | |
| 7431 | ClassDB::bind_method(D_METHOD("set_scroll_past_end_of_file_enabled", "enable"), &TextEdit::set_scroll_past_end_of_file_enabled); |
| 7432 | ClassDB::bind_method(D_METHOD("is_scroll_past_end_of_file_enabled"), &TextEdit::is_scroll_past_end_of_file_enabled); |
| 7433 | |
| 7434 | ClassDB::bind_method(D_METHOD("set_v_scroll_speed", "speed"), &TextEdit::set_v_scroll_speed); |
| 7435 | ClassDB::bind_method(D_METHOD("get_v_scroll_speed"), &TextEdit::get_v_scroll_speed); |
| 7436 | |
| 7437 | ClassDB::bind_method(D_METHOD("set_fit_content_height_enabled", "enabled"), &TextEdit::set_fit_content_height_enabled); |
| 7438 | ClassDB::bind_method(D_METHOD("is_fit_content_height_enabled"), &TextEdit::is_fit_content_height_enabled); |
| 7439 | |
| 7440 | ClassDB::bind_method(D_METHOD("set_fit_content_width_enabled", "enabled"), &TextEdit::set_fit_content_width_enabled); |
| 7441 | ClassDB::bind_method(D_METHOD("is_fit_content_width_enabled"), &TextEdit::is_fit_content_width_enabled); |
| 7442 | |
| 7443 | ClassDB::bind_method(D_METHOD("get_scroll_pos_for_line", "line", "wrap_index"), &TextEdit::get_scroll_pos_for_line, DEFVAL(0)); |
| 7444 | |
| 7445 | // Visible lines. |
| 7446 | ClassDB::bind_method(D_METHOD("set_line_as_first_visible", "line", "wrap_index"), &TextEdit::set_line_as_first_visible, DEFVAL(0)); |
| 7447 | ClassDB::bind_method(D_METHOD("get_first_visible_line"), &TextEdit::get_first_visible_line); |
| 7448 | |
| 7449 | ClassDB::bind_method(D_METHOD("set_line_as_center_visible", "line", "wrap_index"), &TextEdit::set_line_as_center_visible, DEFVAL(0)); |
| 7450 | |
| 7451 | ClassDB::bind_method(D_METHOD("set_line_as_last_visible", "line", "wrap_index"), &TextEdit::set_line_as_last_visible, DEFVAL(0)); |
| 7452 | ClassDB::bind_method(D_METHOD("get_last_full_visible_line"), &TextEdit::get_last_full_visible_line); |
| 7453 | ClassDB::bind_method(D_METHOD("get_last_full_visible_line_wrap_index"), &TextEdit::get_last_full_visible_line_wrap_index); |
| 7454 | |
| 7455 | ClassDB::bind_method(D_METHOD("get_visible_line_count"), &TextEdit::get_visible_line_count); |
| 7456 | ClassDB::bind_method(D_METHOD("get_visible_line_count_in_range", "from_line", "to_line"), &TextEdit::get_visible_line_count_in_range); |
| 7457 | ClassDB::bind_method(D_METHOD("get_total_visible_line_count"), &TextEdit::get_total_visible_line_count); |
| 7458 | |
| 7459 | // Auto adjust |
| 7460 | ClassDB::bind_method(D_METHOD("adjust_viewport_to_caret", "caret_index"), &TextEdit::adjust_viewport_to_caret, DEFVAL(0)); |
| 7461 | ClassDB::bind_method(D_METHOD("center_viewport_to_caret", "caret_index"), &TextEdit::center_viewport_to_caret, DEFVAL(0)); |
| 7462 | |
| 7463 | // Minimap |
| 7464 | ClassDB::bind_method(D_METHOD("set_draw_minimap", "enabled"), &TextEdit::set_draw_minimap); |
| 7465 | ClassDB::bind_method(D_METHOD("is_drawing_minimap"), &TextEdit::is_drawing_minimap); |
| 7466 | |
| 7467 | ClassDB::bind_method(D_METHOD("set_minimap_width", "width"), &TextEdit::set_minimap_width); |
| 7468 | ClassDB::bind_method(D_METHOD("get_minimap_width"), &TextEdit::get_minimap_width); |
| 7469 | |
| 7470 | ClassDB::bind_method(D_METHOD("get_minimap_visible_lines"), &TextEdit::get_minimap_visible_lines); |
| 7471 | |
| 7472 | /* Gutters. */ |
| 7473 | BIND_ENUM_CONSTANT(GUTTER_TYPE_STRING); |
| 7474 | BIND_ENUM_CONSTANT(GUTTER_TYPE_ICON); |
| 7475 | BIND_ENUM_CONSTANT(GUTTER_TYPE_CUSTOM); |
| 7476 | |
| 7477 | ClassDB::bind_method(D_METHOD("add_gutter", "at"), &TextEdit::add_gutter, DEFVAL(-1)); |
| 7478 | ClassDB::bind_method(D_METHOD("remove_gutter", "gutter"), &TextEdit::remove_gutter); |
| 7479 | ClassDB::bind_method(D_METHOD("get_gutter_count"), &TextEdit::get_gutter_count); |
| 7480 | ClassDB::bind_method(D_METHOD("set_gutter_name", "gutter", "name"), &TextEdit::set_gutter_name); |
| 7481 | ClassDB::bind_method(D_METHOD("get_gutter_name", "gutter"), &TextEdit::get_gutter_name); |
| 7482 | ClassDB::bind_method(D_METHOD("set_gutter_type", "gutter", "type"), &TextEdit::set_gutter_type); |
| 7483 | ClassDB::bind_method(D_METHOD("get_gutter_type", "gutter"), &TextEdit::get_gutter_type); |
| 7484 | ClassDB::bind_method(D_METHOD("set_gutter_width", "gutter", "width"), &TextEdit::set_gutter_width); |
| 7485 | ClassDB::bind_method(D_METHOD("get_gutter_width", "gutter"), &TextEdit::get_gutter_width); |
| 7486 | ClassDB::bind_method(D_METHOD("set_gutter_draw", "gutter", "draw"), &TextEdit::set_gutter_draw); |
| 7487 | ClassDB::bind_method(D_METHOD("is_gutter_drawn", "gutter"), &TextEdit::is_gutter_drawn); |
| 7488 | ClassDB::bind_method(D_METHOD("set_gutter_clickable", "gutter", "clickable"), &TextEdit::set_gutter_clickable); |
| 7489 | ClassDB::bind_method(D_METHOD("is_gutter_clickable", "gutter"), &TextEdit::is_gutter_clickable); |
| 7490 | ClassDB::bind_method(D_METHOD("set_gutter_overwritable", "gutter", "overwritable"), &TextEdit::set_gutter_overwritable); |
| 7491 | ClassDB::bind_method(D_METHOD("is_gutter_overwritable", "gutter"), &TextEdit::is_gutter_overwritable); |
| 7492 | ClassDB::bind_method(D_METHOD("merge_gutters", "from_line", "to_line"), &TextEdit::merge_gutters); |
| 7493 | ClassDB::bind_method(D_METHOD("set_gutter_custom_draw", "column", "draw_callback"), &TextEdit::set_gutter_custom_draw); |
| 7494 | ClassDB::bind_method(D_METHOD("get_total_gutter_width"), &TextEdit::get_total_gutter_width); |
| 7495 | |
| 7496 | // Line gutters. |
| 7497 | ClassDB::bind_method(D_METHOD("set_line_gutter_metadata", "line", "gutter", "metadata"), &TextEdit::set_line_gutter_metadata); |
| 7498 | ClassDB::bind_method(D_METHOD("get_line_gutter_metadata", "line", "gutter"), &TextEdit::get_line_gutter_metadata); |
| 7499 | ClassDB::bind_method(D_METHOD("set_line_gutter_text", "line", "gutter", "text"), &TextEdit::set_line_gutter_text); |
| 7500 | ClassDB::bind_method(D_METHOD("get_line_gutter_text", "line", "gutter"), &TextEdit::get_line_gutter_text); |
| 7501 | ClassDB::bind_method(D_METHOD("set_line_gutter_icon", "line", "gutter", "icon"), &TextEdit::set_line_gutter_icon); |
| 7502 | ClassDB::bind_method(D_METHOD("get_line_gutter_icon", "line", "gutter"), &TextEdit::get_line_gutter_icon); |
| 7503 | ClassDB::bind_method(D_METHOD("set_line_gutter_item_color", "line", "gutter", "color"), &TextEdit::set_line_gutter_item_color); |
| 7504 | ClassDB::bind_method(D_METHOD("get_line_gutter_item_color", "line", "gutter"), &TextEdit::get_line_gutter_item_color); |
| 7505 | ClassDB::bind_method(D_METHOD("set_line_gutter_clickable", "line", "gutter", "clickable"), &TextEdit::set_line_gutter_clickable); |
| 7506 | ClassDB::bind_method(D_METHOD("is_line_gutter_clickable", "line", "gutter"), &TextEdit::is_line_gutter_clickable); |
| 7507 | |
| 7508 | // Line style |
| 7509 | ClassDB::bind_method(D_METHOD("set_line_background_color", "line", "color"), &TextEdit::set_line_background_color); |
| 7510 | ClassDB::bind_method(D_METHOD("get_line_background_color", "line"), &TextEdit::get_line_background_color); |
| 7511 | |
| 7512 | /* Syntax Highlighting. */ |
| 7513 | ClassDB::bind_method(D_METHOD("set_syntax_highlighter", "syntax_highlighter"), &TextEdit::set_syntax_highlighter); |
| 7514 | ClassDB::bind_method(D_METHOD("get_syntax_highlighter"), &TextEdit::get_syntax_highlighter); |
| 7515 | |
| 7516 | /* Visual. */ |
| 7517 | ClassDB::bind_method(D_METHOD("set_highlight_current_line", "enabled"), &TextEdit::set_highlight_current_line); |
| 7518 | ClassDB::bind_method(D_METHOD("is_highlight_current_line_enabled"), &TextEdit::is_highlight_current_line_enabled); |
| 7519 | |
| 7520 | ClassDB::bind_method(D_METHOD("set_highlight_all_occurrences", "enabled"), &TextEdit::set_highlight_all_occurrences); |
| 7521 | ClassDB::bind_method(D_METHOD("is_highlight_all_occurrences_enabled"), &TextEdit::is_highlight_all_occurrences_enabled); |
| 7522 | |
| 7523 | ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &TextEdit::get_draw_control_chars); |
| 7524 | ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enabled"), &TextEdit::set_draw_control_chars); |
| 7525 | |
| 7526 | ClassDB::bind_method(D_METHOD("set_draw_tabs", "enabled"), &TextEdit::set_draw_tabs); |
| 7527 | ClassDB::bind_method(D_METHOD("is_drawing_tabs"), &TextEdit::is_drawing_tabs); |
| 7528 | |
| 7529 | ClassDB::bind_method(D_METHOD("set_draw_spaces", "enabled"), &TextEdit::set_draw_spaces); |
| 7530 | ClassDB::bind_method(D_METHOD("is_drawing_spaces"), &TextEdit::is_drawing_spaces); |
| 7531 | |
| 7532 | ClassDB::bind_method(D_METHOD("get_menu"), &TextEdit::get_menu); |
| 7533 | ClassDB::bind_method(D_METHOD("is_menu_visible"), &TextEdit::is_menu_visible); |
| 7534 | ClassDB::bind_method(D_METHOD("menu_option", "option"), &TextEdit::menu_option); |
| 7535 | |
| 7536 | /* Deprecated */ |
| 7537 | #ifndef DISABLE_DEPRECATED |
| 7538 | ClassDB::bind_method(D_METHOD("adjust_carets_after_edit", "caret", "from_line", "from_col", "to_line", "to_col"), &TextEdit::adjust_carets_after_edit); |
| 7539 | ClassDB::bind_method(D_METHOD("get_caret_index_edit_order"), &TextEdit::get_caret_index_edit_order); |
| 7540 | ClassDB::bind_method(D_METHOD("get_selection_line", "caret_index"), &TextEdit::get_selection_line, DEFVAL(0)); |
| 7541 | ClassDB::bind_method(D_METHOD("get_selection_column", "caret_index"), &TextEdit::get_selection_column, DEFVAL(0)); |
| 7542 | #endif |
| 7543 | |
| 7544 | /* Inspector */ |
| 7545 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text"); |
| 7546 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text", PROPERTY_HINT_MULTILINE_TEXT), "set_placeholder", "get_placeholder"); |
| 7547 | |
| 7548 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable"); |
| 7549 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled"); |
| 7550 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emoji_menu_enabled"), "set_emoji_menu_enabled", "is_emoji_menu_enabled"); |
| 7551 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backspace_deletes_composite_character_enabled"), "set_backspace_deletes_composite_character_enabled", "is_backspace_deletes_composite_character_enabled"); |
| 7552 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled"); |
| 7553 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled"); |
| 7554 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled"); |
| 7555 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled"); |
| 7556 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled"); |
| 7557 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "empty_selection_clipboard_enabled"), "set_empty_selection_clipboard_enabled", "is_empty_selection_clipboard_enabled"); |
| 7558 | ADD_PROPERTY(PropertyInfo(Variant::INT, "wrap_mode", PROPERTY_HINT_ENUM, "None,Boundary"), "set_line_wrapping_mode", "get_line_wrapping_mode"); |
| 7559 | ADD_PROPERTY(PropertyInfo(Variant::INT, "autowrap_mode", PROPERTY_HINT_ENUM, "Arbitrary:1,Word:2,Word (Smart):3"), "set_autowrap_mode", "get_autowrap_mode"); |
| 7560 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_wrapped_lines"), "set_indent_wrapped_lines", "is_indent_wrapped_lines"); |
| 7561 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "tab_input_mode"), "set_tab_input_mode", "get_tab_input_mode"); |
| 7562 | |
| 7563 | ADD_GROUP("Virtual Keyboard", "virtual_keyboard_"); |
| 7564 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled"); |
| 7565 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_show_on_focus"), "set_virtual_keyboard_show_on_focus", "get_virtual_keyboard_show_on_focus"); |
| 7566 | |
| 7567 | ADD_GROUP("Scroll", "scroll_"); |
| 7568 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_smooth"), "set_smooth_scroll_enabled", "is_smooth_scroll_enabled"); |
| 7569 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_v_scroll_speed", PROPERTY_HINT_NONE, "suffix:lines/s"), "set_v_scroll_speed", "get_v_scroll_speed"); |
| 7570 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_past_end_of_file"), "set_scroll_past_end_of_file_enabled", "is_scroll_past_end_of_file_enabled"); |
| 7571 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "scroll_vertical", PROPERTY_HINT_NONE, "suffix:lines"), "set_v_scroll", "get_v_scroll"); |
| 7572 | ADD_PROPERTY(PropertyInfo(Variant::INT, "scroll_horizontal", PROPERTY_HINT_NONE, "suffix:px"), "set_h_scroll", "get_h_scroll"); |
| 7573 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_height"), "set_fit_content_height_enabled", "is_fit_content_height_enabled"); |
| 7574 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scroll_fit_content_width"), "set_fit_content_width_enabled", "is_fit_content_width_enabled"); |
| 7575 | |
| 7576 | ADD_GROUP("Minimap", "minimap_"); |
| 7577 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "minimap_draw", PROPERTY_HINT_GROUP_ENABLE), "set_draw_minimap", "is_drawing_minimap"); |
| 7578 | ADD_PROPERTY(PropertyInfo(Variant::INT, "minimap_width", PROPERTY_HINT_NONE, "suffix:px"), "set_minimap_width", "get_minimap_width"); |
| 7579 | |
| 7580 | ADD_GROUP("Caret", "caret_"); |
| 7581 | ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_type", PROPERTY_HINT_ENUM, "Line,Block"), "set_caret_type", "get_caret_type"); |
| 7582 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled"); |
| 7583 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_interval", PROPERTY_HINT_RANGE, "0.1,10,0.01,suffix:s"), "set_caret_blink_interval", "get_caret_blink_interval"); |
| 7584 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_draw_when_editable_disabled"), "set_draw_caret_when_editable_disabled", "is_drawing_caret_when_editable_disabled"); |
| 7585 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_move_on_right_click"), "set_move_caret_on_right_click_enabled", "is_move_caret_on_right_click_enabled"); |
| 7586 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled"); |
| 7587 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_multiple"), "set_multiple_carets_enabled", "is_multiple_carets_enabled"); |
| 7588 | |
| 7589 | ADD_GROUP("Word Separators", ""); |
| 7590 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_default_word_separators"), "set_use_default_word_separators", "is_default_word_separators_enabled"); |
| 7591 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_custom_word_separators"), "set_use_custom_word_separators", "is_custom_word_separators_enabled"); |
| 7592 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "custom_word_separators"), "set_custom_word_separators", "get_custom_word_separators"); |
| 7593 | |
| 7594 | ADD_GROUP("Highlighting", ""); |
| 7595 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "syntax_highlighter", PROPERTY_HINT_RESOURCE_TYPE, "SyntaxHighlighter", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_ALWAYS_DUPLICATE), "set_syntax_highlighter", "get_syntax_highlighter"); |
| 7596 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_all_occurrences"), "set_highlight_all_occurrences", "is_highlight_all_occurrences_enabled"); |
| 7597 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "highlight_current_line"), "set_highlight_current_line", "is_highlight_current_line_enabled"); |
| 7598 | |
| 7599 | ADD_GROUP("Visual Whitespace", "draw_"); |
| 7600 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars"); |
| 7601 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_tabs"), "set_draw_tabs", "is_drawing_tabs"); |
| 7602 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_spaces"), "set_draw_spaces", "is_drawing_spaces"); |
| 7603 | |
| 7604 | ADD_GROUP("BiDi", ""); |
| 7605 | ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction"); |
| 7606 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language"); |
| 7607 | ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override"); |
| 7608 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options"); |
| 7609 | |
| 7610 | /* Signals */ |
| 7611 | /* Core. */ |
| 7612 | ADD_SIGNAL(MethodInfo("text_set")); |
| 7613 | ADD_SIGNAL(MethodInfo("text_changed")); |
| 7614 | ADD_SIGNAL(MethodInfo("lines_edited_from", PropertyInfo(Variant::INT, "from_line"), PropertyInfo(Variant::INT, "to_line"))); |
| 7615 | |
| 7616 | /* Caret. */ |
| 7617 | ADD_SIGNAL(MethodInfo("caret_changed")); |
| 7618 | |
| 7619 | /* Gutters. */ |
| 7620 | ADD_SIGNAL(MethodInfo("gutter_clicked", PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "gutter"))); |
| 7621 | ADD_SIGNAL(MethodInfo("gutter_added")); |
| 7622 | ADD_SIGNAL(MethodInfo("gutter_removed")); |
| 7623 | |
| 7624 | // Theme items |
| 7625 | /* Search */ |
| 7626 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, search_result_color); |
| 7627 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, search_result_border_color); |
| 7628 | |
| 7629 | /* Caret */ |
| 7630 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, caret_width); |
| 7631 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, caret_color); |
| 7632 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, caret_background_color); |
| 7633 | |
| 7634 | /* Selection */ |
| 7635 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_selected_color); |
| 7636 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, selection_color); |
| 7637 | |
| 7638 | /* Other visuals */ |
| 7639 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_normal, "normal"); |
| 7640 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_focus, "focus"); |
| 7641 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, TextEdit, style_readonly, "read_only"); |
| 7642 | |
| 7643 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TextEdit, tab_icon, "tab"); |
| 7644 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, TextEdit, space_icon, "space"); |
| 7645 | |
| 7646 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, TextEdit, font); |
| 7647 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, TextEdit, font_size); |
| 7648 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_color); |
| 7649 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_readonly_color); |
| 7650 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, font_placeholder_color); |
| 7651 | |
| 7652 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, outline_size); |
| 7653 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, TextEdit, outline_color, "font_outline_color"); |
| 7654 | |
| 7655 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, line_spacing); |
| 7656 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, TextEdit, wrap_offset); |
| 7657 | |
| 7658 | #ifndef DISABLE_DEPRECATED |
| 7659 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, background_color); |
| 7660 | #endif // DISABLE_DEPRECATED |
| 7661 | |
| 7662 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, current_line_color); |
| 7663 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, TextEdit, word_highlighted_color); |
| 7664 | |
| 7665 | /* Settings. */ |
| 7666 | GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "gui/timers/text_edit_idle_detect_sec", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 3); |
| 7667 | GLOBAL_DEF(PropertyInfo(Variant::INT, "gui/common/text_edit_undo_stack_max_size", PROPERTY_HINT_RANGE, "0,10000,1,or_greater"), 1024); |
| 7668 | |
| 7669 | /* Dependencies */ |
| 7670 | ADD_CLASS_DEPENDENCY("HScrollBar"); |
| 7671 | ADD_CLASS_DEPENDENCY("PopupMenu"); |
| 7672 | ADD_CLASS_DEPENDENCY("Timer"); |
| 7673 | ADD_CLASS_DEPENDENCY("VScrollBar"); |
| 7674 | } |
| 7675 | |
| 7676 | /* Internal API for CodeEdit. */ |
| 7677 | // Line hiding. |
| 7678 | void TextEdit::_set_hiding_enabled(bool p_enabled) { |
| 7679 | if (hiding_enabled == p_enabled) { |
| 7680 | return; |
| 7681 | } |
| 7682 | |
| 7683 | if (!p_enabled) { |
| 7684 | _unhide_all_lines(); |
| 7685 | } |
| 7686 | hiding_enabled = p_enabled; |
| 7687 | queue_accessibility_update(); |
| 7688 | queue_redraw(); |
| 7689 | } |
| 7690 | |
| 7691 | bool TextEdit::_is_hiding_enabled() const { |
| 7692 | return hiding_enabled; |
| 7693 | } |
| 7694 | |
| 7695 | bool TextEdit::_is_line_hidden(int p_line) const { |
| 7696 | ERR_FAIL_INDEX_V(p_line, text.size(), false); |
| 7697 | return text.is_hidden(p_line); |
| 7698 | } |
| 7699 | |
| 7700 | void TextEdit::_unhide_all_lines() { |
| 7701 | for (int i = 0; i < text.size(); i++) { |
| 7702 | text.set_hidden(i, false); |
| 7703 | } |
| 7704 | _update_scrollbars(); |
| 7705 | queue_accessibility_update(); |
| 7706 | queue_redraw(); |
| 7707 | } |
| 7708 | |
| 7709 | void TextEdit::_unhide_carets() { |
| 7710 | // Override for functionality. |
| 7711 | } |
| 7712 | |
| 7713 | void TextEdit::_set_line_as_hidden(int p_line, bool p_hidden) { |
| 7714 | ERR_FAIL_INDEX(p_line, text.size()); |
| 7715 | |
| 7716 | if (text.is_hidden(p_line) == p_hidden) { |
| 7717 | return; |
| 7718 | } |
| 7719 | |
| 7720 | if (_is_hiding_enabled() || !p_hidden) { |
| 7721 | text.set_hidden(p_line, p_hidden); |
| 7722 | } |
| 7723 | queue_accessibility_update(); |
| 7724 | queue_redraw(); |
| 7725 | } |
| 7726 | |
| 7727 | // Symbol lookup. |
| 7728 | void TextEdit::_set_symbol_lookup_word(const String &p_symbol) { |
| 7729 | if (lookup_symbol_word == p_symbol) { |
| 7730 | return; |
| 7731 | } |
| 7732 | |
| 7733 | lookup_symbol_word = p_symbol; |
| 7734 | queue_redraw(); |
| 7735 | } |
| 7736 | |
| 7737 | /* Text manipulation */ |
| 7738 | |
| 7739 | // Overridable actions |
| 7740 | void TextEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) { |
| 7741 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 7742 | if (!editable) { |
| 7743 | return; |
| 7744 | } |
| 7745 | |
| 7746 | start_action(EditAction::ACTION_TYPING); |
| 7747 | begin_multicaret_edit(); |
| 7748 | for (int i = 0; i < get_caret_count(); i++) { |
| 7749 | if (p_caret == -1 && multicaret_edit_ignore_caret(i)) { |
| 7750 | continue; |
| 7751 | } |
| 7752 | if (p_caret != -1 && p_caret != i) { |
| 7753 | continue; |
| 7754 | } |
| 7755 | |
| 7756 | // Remove the old character if in insert mode and no selection. |
| 7757 | if (overtype_mode && !has_selection(i)) { |
| 7758 | // Make sure we don't try and remove empty space. |
| 7759 | int cl = get_caret_line(i); |
| 7760 | int cc = get_caret_column(i); |
| 7761 | if (cc < get_line(cl).length()) { |
| 7762 | _remove_text(cl, cc, cl, cc + 1); |
| 7763 | } |
| 7764 | } |
| 7765 | |
| 7766 | const char32_t chr[2] = { (char32_t)p_unicode, 0 }; |
| 7767 | insert_text_at_caret(chr, i); |
| 7768 | } |
| 7769 | end_multicaret_edit(); |
| 7770 | end_action(); |
| 7771 | } |
| 7772 | |
| 7773 | void TextEdit::_backspace_internal(int p_caret) { |
| 7774 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 7775 | if (!editable) { |
| 7776 | return; |
| 7777 | } |
| 7778 | |
| 7779 | if (has_selection(p_caret)) { |
| 7780 | delete_selection(p_caret); |
| 7781 | return; |
| 7782 | } |
| 7783 | |
| 7784 | begin_complex_operation(); |
| 7785 | begin_multicaret_edit(); |
| 7786 | for (int i = 0; i < get_caret_count(); i++) { |
| 7787 | if (p_caret == -1 && multicaret_edit_ignore_caret(i)) { |
| 7788 | continue; |
| 7789 | } |
| 7790 | if (p_caret != -1 && p_caret != i) { |
| 7791 | continue; |
| 7792 | } |
| 7793 | |
| 7794 | int to_line = get_caret_line(i); |
| 7795 | int to_column = get_caret_column(i); |
| 7796 | |
| 7797 | if (to_column == 0 && to_line == 0) { |
| 7798 | continue; |
| 7799 | } |
| 7800 | |
| 7801 | int from_line = to_column > 0 ? to_line : to_line - 1; |
| 7802 | int from_column = 0; |
| 7803 | if (to_column == 0) { |
| 7804 | from_column = text[to_line - 1].length(); |
| 7805 | } else if (caret_mid_grapheme_enabled || !backspace_deletes_composite_character_enabled) { |
| 7806 | from_column = to_column - 1; |
| 7807 | } else { |
| 7808 | from_column = get_previous_composite_character_column(to_line, to_column); |
| 7809 | } |
| 7810 | |
| 7811 | merge_gutters(from_line, to_line); |
| 7812 | |
| 7813 | _remove_text(from_line, from_column, to_line, to_column); |
| 7814 | collapse_carets(from_line, from_column, to_line, to_column); |
| 7815 | _offset_carets_after(to_line, to_column, from_line, from_column); |
| 7816 | |
| 7817 | set_caret_line(from_line, false, true, -1, i); |
| 7818 | set_caret_column(from_column, i == 0, i); |
| 7819 | } |
| 7820 | end_multicaret_edit(); |
| 7821 | end_complex_operation(); |
| 7822 | } |
| 7823 | |
| 7824 | void TextEdit::_cut_internal(int p_caret) { |
| 7825 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 7826 | |
| 7827 | _copy_internal(p_caret); |
| 7828 | |
| 7829 | if (!editable) { |
| 7830 | return; |
| 7831 | } |
| 7832 | |
| 7833 | if (has_selection(p_caret)) { |
| 7834 | delete_selection(p_caret); |
| 7835 | return; |
| 7836 | } |
| 7837 | |
| 7838 | if (!empty_selection_clipboard_enabled) { |
| 7839 | return; |
| 7840 | } |
| 7841 | |
| 7842 | // Remove full lines. |
| 7843 | begin_complex_operation(); |
| 7844 | begin_multicaret_edit(); |
| 7845 | Vector<Point2i> line_ranges; |
| 7846 | if (p_caret == -1) { |
| 7847 | line_ranges = get_line_ranges_from_carets(); |
| 7848 | } else { |
| 7849 | line_ranges.push_back(Point2i(get_caret_line(p_caret), get_caret_line(p_caret))); |
| 7850 | } |
| 7851 | int line_offset = 0; |
| 7852 | for (Point2i line_range : line_ranges) { |
| 7853 | // Preserve carets on the last line. |
| 7854 | remove_line_at(line_range.y + line_offset); |
| 7855 | if (line_range.x != line_range.y) { |
| 7856 | remove_text(line_range.x + line_offset, 0, line_range.y + line_offset, 0); |
| 7857 | } |
| 7858 | line_offset += line_range.x - line_range.y - 1; |
| 7859 | } |
| 7860 | end_multicaret_edit(); |
| 7861 | end_complex_operation(); |
| 7862 | } |
| 7863 | |
| 7864 | void TextEdit::_copy_internal(int p_caret) { |
| 7865 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 7866 | if (has_selection(p_caret)) { |
| 7867 | DisplayServer::get_singleton()->clipboard_set(get_selected_text(p_caret)); |
| 7868 | cut_copy_line = ""; |
| 7869 | return; |
| 7870 | } |
| 7871 | |
| 7872 | if (!empty_selection_clipboard_enabled) { |
| 7873 | return; |
| 7874 | } |
| 7875 | |
| 7876 | // Copy full lines. |
| 7877 | StringBuilder clipboard; |
| 7878 | Vector<Point2i> line_ranges; |
| 7879 | if (p_caret == -1) { |
| 7880 | // When there are multiple carets on a line, only copy it once. |
| 7881 | line_ranges = get_line_ranges_from_carets(false, true); |
| 7882 | } else { |
| 7883 | line_ranges.push_back(Point2i(get_caret_line(p_caret), get_caret_line(p_caret))); |
| 7884 | } |
| 7885 | for (Point2i line_range : line_ranges) { |
| 7886 | for (int i = line_range.x; i <= line_range.y; i++) { |
| 7887 | if (text[i].length() != 0) { |
| 7888 | clipboard += _base_get_text(i, 0, i, text[i].length()); |
| 7889 | } |
| 7890 | clipboard += "\n"; |
| 7891 | } |
| 7892 | } |
| 7893 | |
| 7894 | String clipboard_string = clipboard.as_string(); |
| 7895 | DisplayServer::get_singleton()->clipboard_set(clipboard_string); |
| 7896 | // Set the cut copy line so we know to paste as a line. |
| 7897 | if (get_caret_count() == 1) { |
| 7898 | cut_copy_line = clipboard_string; |
| 7899 | } else { |
| 7900 | cut_copy_line = ""; |
| 7901 | } |
| 7902 | } |
| 7903 | |
| 7904 | void TextEdit::_paste_internal(int p_caret) { |
| 7905 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 7906 | if (!editable) { |
| 7907 | return; |
| 7908 | } |
| 7909 | |
| 7910 | String clipboard = DisplayServer::get_singleton()->clipboard_get(); |
| 7911 | if (clipboard.is_empty()) { |
| 7912 | // Nothing to paste. |
| 7913 | return; |
| 7914 | } |
| 7915 | |
| 7916 | // Paste a full line. Ignore '\r' characters that may have been added to the clipboard by the OS. |
| 7917 | if (get_caret_count() == 1 && !has_selection(0) && !cut_copy_line.is_empty() && cut_copy_line == clipboard.remove_char('\r')) { |
| 7918 | insert_text(clipboard, get_caret_line(), 0); |
| 7919 | return; |
| 7920 | } |
| 7921 | |
| 7922 | // Paste text at each caret or one line per caret. |
| 7923 | Vector<String> clipboard_lines = clipboard.split("\n"); |
| 7924 | bool insert_line_per_caret = p_caret == -1 && get_caret_count() > 1 && clipboard_lines.size() == get_caret_count(); |
| 7925 | |
| 7926 | begin_complex_operation(); |
| 7927 | begin_multicaret_edit(); |
| 7928 | Vector<int> sorted_carets = get_sorted_carets(); |
| 7929 | for (int i = 0; i < sorted_carets.size(); i++) { |
| 7930 | int caret_index = sorted_carets[i]; |
| 7931 | if (p_caret != -1 && p_caret != caret_index) { |
| 7932 | continue; |
| 7933 | } |
| 7934 | |
| 7935 | if (has_selection(caret_index)) { |
| 7936 | delete_selection(caret_index); |
| 7937 | } |
| 7938 | |
| 7939 | if (insert_line_per_caret) { |
| 7940 | clipboard = clipboard_lines[i]; |
| 7941 | } |
| 7942 | |
| 7943 | insert_text_at_caret(clipboard, caret_index); |
| 7944 | } |
| 7945 | end_multicaret_edit(); |
| 7946 | end_complex_operation(); |
| 7947 | } |
| 7948 | |
| 7949 | void TextEdit::_paste_primary_clipboard_internal(int p_caret) { |
| 7950 | ERR_FAIL_COND(p_caret >= get_caret_count() || p_caret < -1); |
| 7951 | if (!is_editable() || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 7952 | return; |
| 7953 | } |
| 7954 | |
| 7955 | String paste_buffer = DisplayServer::get_singleton()->clipboard_get_primary(); |
| 7956 | |
| 7957 | if (get_caret_count() == 1) { |
| 7958 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 7959 | deselect(); |
| 7960 | set_caret_line(pos.y, true, false, -1); |
| 7961 | set_caret_column(pos.x); |
| 7962 | } |
| 7963 | |
| 7964 | if (!paste_buffer.is_empty()) { |
| 7965 | insert_text_at_caret(paste_buffer); |
| 7966 | } |
| 7967 | |
| 7968 | grab_focus(); |
| 7969 | } |
| 7970 | |
| 7971 | // Context menu. |
| 7972 | |
| 7973 | Key TextEdit::_get_menu_action_accelerator(const String &p_action) { |
| 7974 | const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action); |
| 7975 | if (!events) { |
| 7976 | return Key::NONE; |
| 7977 | } |
| 7978 | |
| 7979 | // Use first event in the list for the accelerator. |
| 7980 | const List<Ref<InputEvent>>::Element *first_event = events->front(); |
| 7981 | if (!first_event) { |
| 7982 | return Key::NONE; |
| 7983 | } |
| 7984 | |
| 7985 | const Ref<InputEventKey> event = first_event->get(); |
| 7986 | if (event.is_null()) { |
| 7987 | return Key::NONE; |
| 7988 | } |
| 7989 | |
| 7990 | // Use physical keycode if non-zero. |
| 7991 | if (event->get_physical_keycode() != Key::NONE) { |
| 7992 | return event->get_physical_keycode_with_modifiers(); |
| 7993 | } else { |
| 7994 | return event->get_keycode_with_modifiers(); |
| 7995 | } |
| 7996 | } |
| 7997 | |
| 7998 | void TextEdit::_generate_context_menu() { |
| 7999 | menu = memnew(PopupMenu); |
| 8000 | add_child(menu, false, INTERNAL_MODE_FRONT); |
| 8001 | |
| 8002 | menu_dir = memnew(PopupMenu); |
| 8003 | menu_dir->add_radio_check_item(ETR("Same as Layout Direction"), MENU_DIR_INHERITED); |
| 8004 | menu_dir->add_radio_check_item(ETR("Auto-Detect Direction"), MENU_DIR_AUTO); |
| 8005 | menu_dir->add_radio_check_item(ETR("Left-to-Right"), MENU_DIR_LTR); |
| 8006 | menu_dir->add_radio_check_item(ETR("Right-to-Left"), MENU_DIR_RTL); |
| 8007 | |
| 8008 | menu_ctl = memnew(PopupMenu); |
| 8009 | menu_ctl->add_item(ETR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM); |
| 8010 | menu_ctl->add_item(ETR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM); |
| 8011 | menu_ctl->add_item(ETR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE); |
| 8012 | menu_ctl->add_item(ETR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE); |
| 8013 | menu_ctl->add_item(ETR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO); |
| 8014 | menu_ctl->add_item(ETR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO); |
| 8015 | menu_ctl->add_item(ETR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF); |
| 8016 | menu_ctl->add_separator(); |
| 8017 | menu_ctl->add_item(ETR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM); |
| 8018 | menu_ctl->add_item(ETR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI); |
| 8019 | menu_ctl->add_item(ETR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI); |
| 8020 | menu_ctl->add_item(ETR("First Strong Isolate (FSI)"), MENU_INSERT_FSI); |
| 8021 | menu_ctl->add_item(ETR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI); |
| 8022 | menu_ctl->add_separator(); |
| 8023 | menu_ctl->add_item(ETR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ); |
| 8024 | menu_ctl->add_item(ETR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ); |
| 8025 | menu_ctl->add_item(ETR("Word Joiner (WJ)"), MENU_INSERT_WJ); |
| 8026 | menu_ctl->add_item(ETR("Soft Hyphen (SHY)"), MENU_INSERT_SHY); |
| 8027 | |
| 8028 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) { |
| 8029 | menu->add_item(ETR("Emoji & Symbols"), MENU_EMOJI_AND_SYMBOL); |
| 8030 | menu->add_separator(); |
| 8031 | } |
| 8032 | |
| 8033 | menu->add_item(ETR("Cut"), MENU_CUT); |
| 8034 | menu->add_item(ETR("Copy"), MENU_COPY); |
| 8035 | menu->add_item(ETR("Paste"), MENU_PASTE); |
| 8036 | menu->add_separator(); |
| 8037 | menu->add_item(ETR("Select All"), MENU_SELECT_ALL); |
| 8038 | menu->add_item(ETR("Clear"), MENU_CLEAR); |
| 8039 | menu->add_separator(); |
| 8040 | menu->add_item(ETR("Undo"), MENU_UNDO); |
| 8041 | menu->add_item(ETR("Redo"), MENU_REDO); |
| 8042 | menu->add_separator(); |
| 8043 | menu->add_submenu_node_item(ETR("Text Writing Direction"), menu_dir, MENU_SUBMENU_TEXT_DIR); |
| 8044 | menu->add_separator(); |
| 8045 | menu->add_check_item(ETR("Display Control Characters"), MENU_DISPLAY_UCC); |
| 8046 | menu->add_submenu_node_item(ETR("Insert Control Character"), menu_ctl, MENU_SUBMENU_INSERT_UCC); |
| 8047 | |
| 8048 | menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option)); |
| 8049 | menu_dir->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option)); |
| 8050 | menu_ctl->connect(SceneStringName(id_pressed), callable_mp(this, &TextEdit::menu_option)); |
| 8051 | } |
| 8052 | |
| 8053 | void TextEdit::_update_context_menu() { |
| 8054 | if (!menu) { |
| 8055 | _generate_context_menu(); |
| 8056 | } |
| 8057 | |
| 8058 | int idx = -1; |
| 8059 | |
| 8060 | #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \ |
| 8061 | idx = m_menu->get_item_index(m_id); \ |
| 8062 | if (idx >= 0) { \ |
| 8063 | m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \ |
| 8064 | m_menu->set_item_disabled(idx, m_disabled); \ |
| 8065 | } |
| 8066 | |
| 8067 | #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \ |
| 8068 | idx = m_menu->get_item_index(m_id); \ |
| 8069 | if (idx >= 0) { \ |
| 8070 | m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \ |
| 8071 | } |
| 8072 | |
| 8073 | #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \ |
| 8074 | idx = m_menu->get_item_index(m_id); \ |
| 8075 | if (idx >= 0) { \ |
| 8076 | m_menu->set_item_disabled(idx, m_disabled); \ |
| 8077 | } |
| 8078 | |
| 8079 | #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \ |
| 8080 | idx = m_menu->get_item_index(m_id); \ |
| 8081 | if (idx >= 0) { \ |
| 8082 | m_menu->set_item_checked(idx, m_checked); \ |
| 8083 | } |
| 8084 | |
| 8085 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) { |
| 8086 | MENU_ITEM_DISABLED(menu, MENU_EMOJI_AND_SYMBOL, !editable || !emoji_menu_enabled) |
| 8087 | } |
| 8088 | MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable) |
| 8089 | MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy") |
| 8090 | MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable) |
| 8091 | MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled) |
| 8092 | MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable) |
| 8093 | MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo()) |
| 8094 | MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo()) |
| 8095 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED) |
| 8096 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO) |
| 8097 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR) |
| 8098 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL) |
| 8099 | MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars) |
| 8100 | MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable) |
| 8101 | |
| 8102 | #undef MENU_ITEM_ACTION_DISABLED |
| 8103 | #undef MENU_ITEM_ACTION |
| 8104 | #undef MENU_ITEM_DISABLED |
| 8105 | #undef MENU_ITEM_CHECKED |
| 8106 | } |
| 8107 | |
| 8108 | /* Versioning */ |
| 8109 | void TextEdit::_push_current_op() { |
| 8110 | if (pending_action_end) { |
| 8111 | start_action(EditAction::ACTION_NONE); |
| 8112 | return; |
| 8113 | } |
| 8114 | if (current_op.type == TextOperation::TYPE_NONE) { |
| 8115 | return; // Nothing to do. |
| 8116 | } |
| 8117 | |
| 8118 | if (next_operation_is_complex) { |
| 8119 | current_op.chain_forward = true; |
| 8120 | next_operation_is_complex = false; |
| 8121 | } |
| 8122 | |
| 8123 | undo_stack.push_back(current_op); |
| 8124 | current_op.type = TextOperation::TYPE_NONE; |
| 8125 | current_op.text = ""; |
| 8126 | current_op.chain_forward = false; |
| 8127 | |
| 8128 | if (undo_stack.size() > undo_stack_max_size) { |
| 8129 | undo_stack.pop_front(); |
| 8130 | } |
| 8131 | } |
| 8132 | |
| 8133 | void TextEdit::_do_text_op(const TextOperation &p_op, bool p_reverse) { |
| 8134 | ERR_FAIL_COND(p_op.type == TextOperation::TYPE_NONE); |
| 8135 | |
| 8136 | bool insert = p_op.type == TextOperation::TYPE_INSERT; |
| 8137 | if (p_reverse) { |
| 8138 | insert = !insert; |
| 8139 | } |
| 8140 | |
| 8141 | if (insert) { |
| 8142 | int check_line; |
| 8143 | int check_column; |
| 8144 | _base_insert_text(p_op.from_line, p_op.from_column, p_op.text, check_line, check_column); |
| 8145 | ERR_FAIL_COND(check_line != p_op.to_line); // BUG. |
| 8146 | ERR_FAIL_COND(check_column != p_op.to_column); // BUG. |
| 8147 | } else { |
| 8148 | _base_remove_text(p_op.from_line, p_op.from_column, p_op.to_line, p_op.to_column); |
| 8149 | } |
| 8150 | } |
| 8151 | |
| 8152 | void TextEdit::_clear_redo() { |
| 8153 | if (undo_stack_pos == nullptr) { |
| 8154 | return; // Nothing to clear. |
| 8155 | } |
| 8156 | |
| 8157 | _push_current_op(); |
| 8158 | |
| 8159 | while (undo_stack_pos) { |
| 8160 | List<TextOperation>::Element *elem = undo_stack_pos; |
| 8161 | undo_stack_pos = undo_stack_pos->next(); |
| 8162 | undo_stack.erase(elem); |
| 8163 | } |
| 8164 | } |
| 8165 | |
| 8166 | /* Search */ |
| 8167 | int TextEdit::_get_column_pos_of_word(const String &p_key, const String &p_search, uint32_t p_search_flags, int p_from_column) const { |
| 8168 | int col = -1; |
| 8169 | |
| 8170 | if (p_key.length() > 0 && p_search.length() > 0) { |
| 8171 | if (p_from_column < 0 || p_from_column > p_search.length()) { |
| 8172 | p_from_column = 0; |
| 8173 | } |
| 8174 | |
| 8175 | bool key_start_is_symbol = is_symbol(p_key[0]); |
| 8176 | bool key_end_is_symbol = is_symbol(p_key[p_key.length() - 1]); |
| 8177 | |
| 8178 | while (col == -1 && p_from_column <= p_search.length()) { |
| 8179 | if (p_search_flags & SEARCH_MATCH_CASE) { |
| 8180 | col = p_search.find(p_key, p_from_column); |
| 8181 | } else { |
| 8182 | col = p_search.findn(p_key, p_from_column); |
| 8183 | } |
| 8184 | |
| 8185 | // If not found, just break early to improve performance. |
| 8186 | if (col == -1) { |
| 8187 | break; |
| 8188 | } |
| 8189 | |
| 8190 | // Whole words only. |
| 8191 | if (col != -1 && p_search_flags & SEARCH_WHOLE_WORDS) { |
| 8192 | p_from_column = col; |
| 8193 | |
| 8194 | if (!key_start_is_symbol && col > 0 && !is_symbol(p_search[col - 1])) { |
| 8195 | col = -1; |
| 8196 | } else if (!key_end_is_symbol && (col + p_key.length()) < p_search.length() && !is_symbol(p_search[col + p_key.length()])) { |
| 8197 | col = -1; |
| 8198 | } |
| 8199 | } |
| 8200 | |
| 8201 | p_from_column += 1; |
| 8202 | } |
| 8203 | } |
| 8204 | return col; |
| 8205 | } |
| 8206 | |
| 8207 | /* Mouse */ |
| 8208 | int TextEdit::_get_char_pos_for_line(int p_px, int p_line, int p_wrap_index) const { |
| 8209 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 8210 | p_wrap_index = MIN(p_wrap_index, text.get_line_data(p_line)->get_line_count() - 1); |
| 8211 | |
| 8212 | RID text_rid = text.get_line_data(p_line)->get_line_rid(p_wrap_index); |
| 8213 | const float wrap_indent = _get_wrap_indent_offset(p_line, p_wrap_index, is_layout_rtl()); |
| 8214 | |
| 8215 | if (is_layout_rtl()) { |
| 8216 | p_px = TS->shaped_text_get_size(text_rid).x - p_px + wrap_indent; |
| 8217 | } else { |
| 8218 | p_px -= wrap_indent; |
| 8219 | } |
| 8220 | int ofs = TS->shaped_text_hit_test_position(text_rid, p_px); |
| 8221 | if (ofs == -1) { |
| 8222 | return 0; |
| 8223 | } |
| 8224 | if (!caret_mid_grapheme_enabled) { |
| 8225 | ofs = TS->shaped_text_closest_character_pos(text_rid, ofs); |
| 8226 | } |
| 8227 | return ofs; |
| 8228 | } |
| 8229 | |
| 8230 | /* Caret */ |
| 8231 | void TextEdit::_caret_changed(int p_caret) { |
| 8232 | queue_redraw(); |
| 8233 | |
| 8234 | if (has_selection(p_caret)) { |
| 8235 | _selection_changed(p_caret); |
| 8236 | } |
| 8237 | |
| 8238 | if (caret_pos_dirty) { |
| 8239 | return; |
| 8240 | } |
| 8241 | |
| 8242 | if (is_inside_tree()) { |
| 8243 | callable_mp(this, &TextEdit::_emit_caret_changed).call_deferred(); |
| 8244 | } |
| 8245 | caret_pos_dirty = true; |
| 8246 | } |
| 8247 | |
| 8248 | void TextEdit::_emit_caret_changed() { |
| 8249 | emit_signal(SNAME("caret_changed")); |
| 8250 | caret_pos_dirty = false; |
| 8251 | } |
| 8252 | |
| 8253 | void TextEdit::_reset_caret_blink_timer() { |
| 8254 | if (!caret_blink_enabled) { |
| 8255 | return; |
| 8256 | } |
| 8257 | |
| 8258 | draw_caret = true; |
| 8259 | if (has_focus()) { |
| 8260 | caret_blink_timer->stop(); |
| 8261 | caret_blink_timer->start(); |
| 8262 | queue_redraw(); |
| 8263 | } |
| 8264 | } |
| 8265 | |
| 8266 | void TextEdit::_toggle_draw_caret() { |
| 8267 | draw_caret = !draw_caret; |
| 8268 | if (is_visible_in_tree() && has_focus() && window_has_focus) { |
| 8269 | queue_redraw(); |
| 8270 | } |
| 8271 | } |
| 8272 | |
| 8273 | int TextEdit::_get_column_x_offset_for_line(int p_char, int p_line, int p_column) const { |
| 8274 | ERR_FAIL_INDEX_V(p_line, text.size(), 0); |
| 8275 | |
| 8276 | int wrap_index = 0; |
| 8277 | Vector<Vector2i> wrap_ranges = text.get_line_wrap_ranges(p_line); |
| 8278 | for (int i = 0; i < wrap_ranges.size(); i++) { |
| 8279 | if ((p_char >= wrap_ranges[i].x) && (p_char < wrap_ranges[i].y || (i == wrap_ranges.size() - 1 && p_char == wrap_ranges[i].y))) { |
| 8280 | wrap_index = i; |
| 8281 | break; |
| 8282 | } |
| 8283 | } |
| 8284 | |
| 8285 | RID text_rid = text.get_line_data(p_line)->get_line_rid(wrap_index); |
| 8286 | bool rtl = is_layout_rtl(); |
| 8287 | const float wrap_indent = _get_wrap_indent_offset(p_line, wrap_index, rtl); |
| 8288 | |
| 8289 | CaretInfo ts_caret = TS->shaped_text_get_carets(text_rid, p_column); |
| 8290 | if ((ts_caret.l_caret != Rect2() && (ts_caret.l_dir == TextServer::DIRECTION_AUTO || ts_caret.l_dir == (TextServer::Direction)input_direction)) || (ts_caret.t_caret == Rect2())) { |
| 8291 | return ts_caret.l_caret.position.x + (rtl ? -wrap_indent : wrap_indent); |
| 8292 | } else { |
| 8293 | return ts_caret.t_caret.position.x + (rtl ? -wrap_indent : wrap_indent); |
| 8294 | } |
| 8295 | } |
| 8296 | |
| 8297 | bool TextEdit::_is_line_col_in_range(int p_line, int p_column, int p_from_line, int p_from_column, int p_to_line, int p_to_column, bool p_include_edges) const { |
| 8298 | if (p_line >= p_from_line && p_line <= p_to_line && (p_line > p_from_line || p_column > p_from_column) && (p_line < p_to_line || p_column < p_to_column)) { |
| 8299 | return true; |
| 8300 | } |
| 8301 | if (p_include_edges) { |
| 8302 | if ((p_line == p_from_line && p_column == p_from_column) || (p_line == p_to_line && p_column == p_to_column)) { |
| 8303 | return true; |
| 8304 | } |
| 8305 | } |
| 8306 | return false; |
| 8307 | } |
| 8308 | |
| 8309 | void TextEdit::_offset_carets_after(int p_old_line, int p_old_column, int p_new_line, int p_new_column, bool p_include_selection_begin, bool p_include_selection_end) { |
| 8310 | // Moves all carets at or after old_line and old_column. |
| 8311 | // Called after deleting or inserting text so that the carets stay with the text they are at. |
| 8312 | |
| 8313 | int edit_height = p_new_line - p_old_line; |
| 8314 | int edit_size = p_new_column - p_old_column; |
| 8315 | if (edit_height == 0 && edit_size == 0) { |
| 8316 | return; |
| 8317 | } |
| 8318 | |
| 8319 | // Intentionally includes carets in the multicaret_edit_ignore list so that they are moved together. |
| 8320 | for (int i = 0; i < get_caret_count(); i++) { |
| 8321 | bool selected = has_selection(i); |
| 8322 | bool caret_at_end = selected && is_caret_after_selection_origin(i); |
| 8323 | bool include_caret_at = caret_at_end ? p_include_selection_end : p_include_selection_begin; |
| 8324 | |
| 8325 | // Move caret. |
| 8326 | int caret_line = get_caret_line(i); |
| 8327 | int caret_column = get_caret_column(i); |
| 8328 | bool caret_after = caret_line > p_old_line || (caret_line == p_old_line && caret_column > p_old_column); |
| 8329 | bool caret_at = caret_line == p_old_line && caret_column == p_old_column; |
| 8330 | if (caret_after || (caret_at && include_caret_at)) { |
| 8331 | caret_line += edit_height; |
| 8332 | if (caret_line == p_new_line) { |
| 8333 | caret_column += edit_size; |
| 8334 | } |
| 8335 | |
| 8336 | if (edit_height != 0) { |
| 8337 | set_caret_line(caret_line, false, true, -1, i); |
| 8338 | } |
| 8339 | set_caret_column(caret_column, false, i); |
| 8340 | } |
| 8341 | |
| 8342 | // Move selection origin. |
| 8343 | if (!selected) { |
| 8344 | continue; |
| 8345 | } |
| 8346 | bool include_selection_origin_at = !caret_at_end ? p_include_selection_end : p_include_selection_begin; |
| 8347 | |
| 8348 | int selection_origin_line = get_selection_origin_line(i); |
| 8349 | int selection_origin_column = get_selection_origin_column(i); |
| 8350 | bool selection_origin_after = selection_origin_line > p_old_line || (selection_origin_line == p_old_line && selection_origin_column > p_old_column); |
| 8351 | bool selection_origin_at = selection_origin_line == p_old_line && selection_origin_column == p_old_column; |
| 8352 | if (selection_origin_after || (selection_origin_at && include_selection_origin_at)) { |
| 8353 | selection_origin_line += edit_height; |
| 8354 | if (selection_origin_line == p_new_line) { |
| 8355 | selection_origin_column += edit_size; |
| 8356 | } |
| 8357 | select(selection_origin_line, selection_origin_column, caret_line, caret_column, i); |
| 8358 | } |
| 8359 | } |
| 8360 | if (!p_include_selection_begin && p_include_selection_end && has_selection()) { |
| 8361 | // It is possible that two adjacent selections now overlap. |
| 8362 | merge_overlapping_carets(); |
| 8363 | } |
| 8364 | } |
| 8365 | |
| 8366 | void TextEdit::_cancel_drag_and_drop_text() { |
| 8367 | // Cancel the drag operation if drag originated from here. |
| 8368 | if (selection_drag_attempt && get_viewport()) { |
| 8369 | get_viewport()->gui_cancel_drag(); |
| 8370 | } |
| 8371 | } |
| 8372 | |
| 8373 | /* Selection */ |
| 8374 | void TextEdit::_selection_changed(int p_caret) { |
| 8375 | if (!selecting_enabled) { |
| 8376 | return; |
| 8377 | } |
| 8378 | |
| 8379 | _cancel_drag_and_drop_text(); |
| 8380 | queue_accessibility_update(); |
| 8381 | queue_redraw(); |
| 8382 | } |
| 8383 | |
| 8384 | void TextEdit::_click_selection_held() { |
| 8385 | // Update the selection mode on a timer so it is updated when the view scrolls even if the mouse isn't moving. |
| 8386 | if (!Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) { |
| 8387 | click_select_held->stop(); |
| 8388 | return; |
| 8389 | } |
| 8390 | switch (get_selection_mode()) { |
| 8391 | case SelectionMode::SELECTION_MODE_POINTER: { |
| 8392 | _update_selection_mode_pointer(); |
| 8393 | } break; |
| 8394 | case SelectionMode::SELECTION_MODE_WORD: { |
| 8395 | _update_selection_mode_word(); |
| 8396 | } break; |
| 8397 | case SelectionMode::SELECTION_MODE_LINE: { |
| 8398 | _update_selection_mode_line(); |
| 8399 | } break; |
| 8400 | default: { |
| 8401 | click_select_held->stop(); |
| 8402 | break; |
| 8403 | } |
| 8404 | } |
| 8405 | } |
| 8406 | |
| 8407 | void TextEdit::_update_selection_mode_pointer(bool p_initial) { |
| 8408 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 8409 | int line = pos.y; |
| 8410 | int column = pos.x; |
| 8411 | int caret_index = get_caret_count() - 1; |
| 8412 | |
| 8413 | if (p_initial && !has_selection(caret_index)) { |
| 8414 | set_selection_origin_line(line, true, -1, caret_index); |
| 8415 | set_selection_origin_column(column, caret_index); |
| 8416 | // Set the word begin and end to the column in case the mode changes later. |
| 8417 | carets.write[caret_index].selection.word_begin_column = column; |
| 8418 | carets.write[caret_index].selection.word_end_column = column; |
| 8419 | } else { |
| 8420 | int origin_line = get_selection_origin_line(caret_index); |
| 8421 | bool is_new_selection_dir_right = line > origin_line || (line == origin_line && column >= carets[caret_index].selection.word_begin_column); |
| 8422 | int origin_col = is_new_selection_dir_right ? carets[caret_index].selection.word_begin_column : carets[caret_index].selection.word_end_column; |
| 8423 | select(origin_line, origin_col, line, column, caret_index); |
| 8424 | } |
| 8425 | adjust_viewport_to_caret(caret_index); |
| 8426 | |
| 8427 | if (has_selection(caret_index)) { |
| 8428 | // Only set to true if any selection has been made. |
| 8429 | dragging_selection = true; |
| 8430 | } |
| 8431 | |
| 8432 | click_select_held->start(); |
| 8433 | merge_overlapping_carets(); |
| 8434 | } |
| 8435 | |
| 8436 | void TextEdit::_update_selection_mode_word(bool p_initial) { |
| 8437 | dragging_selection = true; |
| 8438 | |
| 8439 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 8440 | int line = pos.y; |
| 8441 | int column = pos.x; |
| 8442 | int caret_index = get_caret_count() - 1; |
| 8443 | |
| 8444 | int caret_pos = CLAMP(column, 0, text[line].length()); |
| 8445 | int beg = caret_pos; |
| 8446 | int end = beg; |
| 8447 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text.get_line_data(line)->get_rid()); |
| 8448 | for (int i = 0; i < words.size(); i = i + 2) { |
| 8449 | if ((p_initial && words[i] <= caret_pos && words[i + 1] >= caret_pos) || (!p_initial && words[i] < caret_pos && words[i + 1] > caret_pos)) { |
| 8450 | beg = words[i]; |
| 8451 | end = words[i + 1]; |
| 8452 | break; |
| 8453 | } |
| 8454 | } |
| 8455 | |
| 8456 | if (p_initial && !has_selection(caret_index)) { |
| 8457 | // Set the selection origin if there is no existing selection. |
| 8458 | select(line, beg, line, end, caret_index); |
| 8459 | carets.write[caret_index].selection.word_begin_column = beg; |
| 8460 | carets.write[caret_index].selection.word_end_column = end; |
| 8461 | } else { |
| 8462 | // Expand the word selection to the mouse. |
| 8463 | int origin_line = get_selection_origin_line(caret_index); |
| 8464 | bool is_new_selection_dir_right = line > origin_line || (line == origin_line && column >= carets[caret_index].selection.word_begin_column); |
| 8465 | int origin_col = is_new_selection_dir_right ? carets[caret_index].selection.word_begin_column : carets[caret_index].selection.word_end_column; |
| 8466 | int caret_col = is_new_selection_dir_right ? end : beg; |
| 8467 | |
| 8468 | // Expand the word selection only if the caret is not at the start of the selection. |
| 8469 | if (column != carets[caret_index].selection.word_begin_column || line != origin_line || carets[caret_index].selection.word_begin_column == carets[caret_index].selection.word_end_column) { |
| 8470 | select(origin_line, origin_col, line, caret_col, caret_index); |
| 8471 | } |
| 8472 | } |
| 8473 | adjust_viewport_to_caret(caret_index); |
| 8474 | |
| 8475 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 8476 | DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text()); |
| 8477 | } |
| 8478 | |
| 8479 | click_select_held->start(); |
| 8480 | merge_overlapping_carets(); |
| 8481 | } |
| 8482 | |
| 8483 | void TextEdit::_update_selection_mode_line(bool p_initial) { |
| 8484 | dragging_selection = true; |
| 8485 | |
| 8486 | Point2i pos = get_line_column_at_pos(get_local_mouse_pos()); |
| 8487 | int line = pos.y; |
| 8488 | int caret_index = get_caret_count() - 1; |
| 8489 | |
| 8490 | int origin_line = p_initial && !has_selection(caret_index) ? line : get_selection_origin_line(caret_index); |
| 8491 | bool line_below = line >= origin_line; |
| 8492 | int origin_col = line_below ? 0 : get_line(origin_line).length(); |
| 8493 | int caret_line = line_below ? line + 1 : line; |
| 8494 | int caret_col = caret_line < text.size() ? 0 : get_line(text.size() - 1).length(); |
| 8495 | |
| 8496 | select(origin_line, origin_col, caret_line, caret_col, caret_index); |
| 8497 | adjust_viewport_to_caret(caret_index); |
| 8498 | |
| 8499 | if (p_initial) { |
| 8500 | // Set the word begin and end to the start and end of the origin line in case the mode changes later. |
| 8501 | carets.write[caret_index].selection.word_begin_column = 0; |
| 8502 | carets.write[caret_index].selection.word_end_column = get_line(origin_line).length(); |
| 8503 | } |
| 8504 | |
| 8505 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 8506 | DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text()); |
| 8507 | } |
| 8508 | |
| 8509 | click_select_held->start(); |
| 8510 | merge_overlapping_carets(); |
| 8511 | } |
| 8512 | |
| 8513 | void TextEdit::_pre_shift_selection(int p_caret) { |
| 8514 | if (!selecting_enabled) { |
| 8515 | return; |
| 8516 | } |
| 8517 | |
| 8518 | set_selection_mode(SelectionMode::SELECTION_MODE_SHIFT); |
| 8519 | if (has_selection(p_caret)) { |
| 8520 | return; |
| 8521 | } |
| 8522 | // Prepare selection to start at current caret position. |
| 8523 | set_selection_origin_line(get_caret_line(p_caret), true, -1, p_caret); |
| 8524 | set_selection_origin_column(get_caret_column(p_caret), p_caret); |
| 8525 | carets.write[p_caret].selection.active = true; |
| 8526 | } |
| 8527 | |
| 8528 | bool TextEdit::_selection_contains(int p_caret, int p_line, int p_column, bool p_include_edges, bool p_only_selections) const { |
| 8529 | if (!has_selection(p_caret)) { |
| 8530 | return !p_only_selections && p_line == get_caret_line(p_caret) && p_column == get_caret_column(p_caret); |
| 8531 | } |
| 8532 | return _is_line_col_in_range(p_line, p_column, get_selection_from_line(p_caret), get_selection_from_column(p_caret), get_selection_to_line(p_caret), get_selection_to_column(p_caret), p_include_edges); |
| 8533 | } |
| 8534 | |
| 8535 | /* Line Wrapping */ |
| 8536 | void TextEdit::_update_wrap_at_column(bool p_force) { |
| 8537 | int new_wrap_at = get_size().width - _get_current_stylebox()->get_minimum_size().width - gutters_width - gutter_padding; |
| 8538 | if (draw_minimap) { |
| 8539 | new_wrap_at -= minimap_width; |
| 8540 | } |
| 8541 | if (v_scroll->is_visible_in_tree()) { |
| 8542 | new_wrap_at -= v_scroll->get_combined_minimum_size().width; |
| 8543 | } |
| 8544 | /* Give it a little more space. */ |
| 8545 | new_wrap_at -= theme_cache.wrap_offset; |
| 8546 | |
| 8547 | if ((wrap_at_column != new_wrap_at) || p_force) { |
| 8548 | wrap_at_column = new_wrap_at; |
| 8549 | if (line_wrapping_mode) { |
| 8550 | BitField<TextServer::LineBreakFlag> autowrap_flags = TextServer::BREAK_MANDATORY; |
| 8551 | switch (autowrap_mode) { |
| 8552 | case TextServer::AUTOWRAP_WORD_SMART: |
| 8553 | autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_ADAPTIVE | TextServer::BREAK_MANDATORY; |
| 8554 | break; |
| 8555 | case TextServer::AUTOWRAP_WORD: |
| 8556 | autowrap_flags = TextServer::BREAK_WORD_BOUND | TextServer::BREAK_MANDATORY; |
| 8557 | break; |
| 8558 | case TextServer::AUTOWRAP_ARBITRARY: |
| 8559 | autowrap_flags = TextServer::BREAK_GRAPHEME_BOUND | TextServer::BREAK_MANDATORY; |
| 8560 | break; |
| 8561 | case TextServer::AUTOWRAP_OFF: |
| 8562 | break; |
| 8563 | } |
| 8564 | text.set_brk_flags(autowrap_flags); |
| 8565 | text.set_width(wrap_at_column); |
| 8566 | text.invalidate_all_lines(); |
| 8567 | _update_placeholder(); |
| 8568 | } else if (text.get_width() != -1) { |
| 8569 | text.set_width(-1); |
| 8570 | text.invalidate_all_lines(); |
| 8571 | _update_placeholder(); |
| 8572 | } |
| 8573 | } |
| 8574 | |
| 8575 | // Update viewport. |
| 8576 | int first_vis_line = get_first_visible_line(); |
| 8577 | if (is_line_wrapped(first_vis_line)) { |
| 8578 | first_visible_line_wrap_ofs = MIN(first_visible_line_wrap_ofs, get_line_wrap_count(first_vis_line)); |
| 8579 | } else { |
| 8580 | first_visible_line_wrap_ofs = 0; |
| 8581 | } |
| 8582 | set_line_as_first_visible(first_visible_line, first_visible_line_wrap_ofs); |
| 8583 | queue_accessibility_update(); |
| 8584 | } |
| 8585 | |
| 8586 | /* Viewport. */ |
| 8587 | void TextEdit::_update_scrollbars() { |
| 8588 | Size2 size = get_size(); |
| 8589 | Size2 hmin = h_scroll->get_combined_minimum_size(); |
| 8590 | Size2 vmin = v_scroll->get_combined_minimum_size(); |
| 8591 | |
| 8592 | Ref<StyleBox> style = _get_current_stylebox(); |
| 8593 | v_scroll->set_begin(Point2(size.width - vmin.width, style->get_margin(SIDE_TOP))); |
| 8594 | v_scroll->set_end(Point2(size.width, size.height - style->get_margin(SIDE_TOP) - style->get_margin(SIDE_BOTTOM))); |
| 8595 | |
| 8596 | h_scroll->set_begin(Point2(0, size.height - hmin.height)); |
| 8597 | h_scroll->set_end(Point2(size.width - vmin.width, size.height)); |
| 8598 | |
| 8599 | bool draw_placeholder = _using_placeholder(); |
| 8600 | |
| 8601 | int visible_rows = get_visible_line_count(); |
| 8602 | int total_rows = draw_placeholder ? placeholder_wrapped_rows.size() : get_total_visible_line_count(); |
| 8603 | if ((scroll_past_end_of_file_enabled && !fit_content_height) || visible_rows == 0) { |
| 8604 | total_rows += visible_rows - 1; |
| 8605 | } |
| 8606 | |
| 8607 | int visible_width = size.width - style->get_minimum_size().width; |
| 8608 | int total_width = (draw_placeholder ? placeholder_max_width : text.get_max_width()) + gutters_width + gutter_padding; |
| 8609 | |
| 8610 | if (draw_minimap) { |
| 8611 | total_width += minimap_width; |
| 8612 | } |
| 8613 | |
| 8614 | content_size_cache = Vector2i(total_width + 10, MAX(total_rows, 1) * get_line_height()); |
| 8615 | if (fit_content_height || fit_content_width) { |
| 8616 | update_minimum_size(); |
| 8617 | } |
| 8618 | |
| 8619 | updating_scrolls = true; |
| 8620 | |
| 8621 | if (!fit_content_height && total_rows > visible_rows) { |
| 8622 | double visible_rows_exact = (double)_get_control_height() / (double)get_line_height(); |
| 8623 | double fractional_visible_rows = visible_rows_exact - (double)visible_rows; |
| 8624 | fractional_visible_rows = CLAMP(fractional_visible_rows, 0.0, 1.0); |
| 8625 | v_scroll->show(); |
| 8626 | v_scroll->set_max(total_rows); |
| 8627 | v_scroll->set_page(visible_rows + fractional_visible_rows); |
| 8628 | set_v_scroll(get_v_scroll()); |
| 8629 | } else { |
| 8630 | first_visible_line = 0; |
| 8631 | first_visible_line_wrap_ofs = 0; |
| 8632 | v_scroll->set_value(0); |
| 8633 | v_scroll->set_max(0); |
| 8634 | v_scroll->hide(); |
| 8635 | } |
| 8636 | |
| 8637 | if (total_width > visible_width) { |
| 8638 | h_scroll->show(); |
| 8639 | h_scroll->set_max(total_width); |
| 8640 | h_scroll->set_page(visible_width); |
| 8641 | if (first_visible_col > (total_width - visible_width)) { |
| 8642 | first_visible_col = (total_width - visible_width); |
| 8643 | } |
| 8644 | if (std::fabs(h_scroll->get_value() - (double)first_visible_col) >= 1) { |
| 8645 | h_scroll->set_value(first_visible_col); |
| 8646 | } |
| 8647 | |
| 8648 | } else { |
| 8649 | first_visible_col = 0; |
| 8650 | h_scroll->set_value(0); |
| 8651 | h_scroll->set_max(0); |
| 8652 | h_scroll->hide(); |
| 8653 | } |
| 8654 | |
| 8655 | updating_scrolls = false; |
| 8656 | } |
| 8657 | |
| 8658 | int TextEdit::_get_control_height() const { |
| 8659 | int control_height = get_size().height - _get_current_stylebox()->get_minimum_size().height; |
| 8660 | if (h_scroll->is_visible_in_tree()) { |
| 8661 | control_height -= h_scroll->get_size().height; |
| 8662 | } |
| 8663 | return control_height; |
| 8664 | } |
| 8665 | |
| 8666 | void TextEdit::_v_scroll_input() { |
| 8667 | scrolling = false; |
| 8668 | minimap_clicked = false; |
| 8669 | } |
| 8670 | |
| 8671 | void TextEdit::_scroll_moved(double p_to_val) { |
| 8672 | if (updating_scrolls) { |
| 8673 | return; |
| 8674 | } |
| 8675 | |
| 8676 | if (h_scroll->is_visible_in_tree()) { |
| 8677 | first_visible_col = h_scroll->get_value(); |
| 8678 | } |
| 8679 | if (v_scroll->is_visible_in_tree()) { |
| 8680 | // Set line ofs and wrap ofs. |
| 8681 | bool draw_placeholder = _using_placeholder(); |
| 8682 | |
| 8683 | int v_scroll_i = std::floor(get_v_scroll()); |
| 8684 | int sc = 0; |
| 8685 | int n_line; |
| 8686 | for (n_line = 0; n_line < text.size(); n_line++) { |
| 8687 | if (!_is_line_hidden(n_line)) { |
| 8688 | sc++; |
| 8689 | sc += draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(n_line); |
| 8690 | if (sc > v_scroll_i) { |
| 8691 | break; |
| 8692 | } |
| 8693 | } |
| 8694 | } |
| 8695 | n_line = MIN(n_line, text.size() - 1); |
| 8696 | int line_wrap_amount = draw_placeholder ? placeholder_wrapped_rows.size() - 1 : get_line_wrap_count(n_line); |
| 8697 | int wi = line_wrap_amount - (sc - v_scroll_i - 1); |
| 8698 | wi = CLAMP(wi, 0, line_wrap_amount); |
| 8699 | |
| 8700 | first_visible_line = n_line; |
| 8701 | first_visible_line_wrap_ofs = wi; |
| 8702 | } |
| 8703 | queue_accessibility_update(); |
| 8704 | queue_redraw(); |
| 8705 | } |
| 8706 | |
| 8707 | double TextEdit::_get_visible_lines_offset() const { |
| 8708 | double total = _get_control_height(); |
| 8709 | total /= (double)get_line_height(); |
| 8710 | total = total - std::floor(total); |
| 8711 | total = -CLAMP(total, 0.001, 1) + 1; |
| 8712 | return total; |
| 8713 | } |
| 8714 | |
| 8715 | double TextEdit::_get_v_scroll_offset() const { |
| 8716 | double val = get_v_scroll() - std::floor(get_v_scroll()); |
| 8717 | return CLAMP(val, 0, 1); |
| 8718 | } |
| 8719 | |
| 8720 | void TextEdit::_scroll_up(real_t p_delta, bool p_animate) { |
| 8721 | if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(-p_delta)) { |
| 8722 | scrolling = false; |
| 8723 | minimap_clicked = false; |
| 8724 | } |
| 8725 | |
| 8726 | if (scrolling) { |
| 8727 | target_v_scroll = (target_v_scroll - p_delta); |
| 8728 | } else { |
| 8729 | target_v_scroll = (get_v_scroll() - p_delta); |
| 8730 | } |
| 8731 | |
| 8732 | if (smooth_scroll_enabled) { |
| 8733 | if (target_v_scroll <= 0) { |
| 8734 | target_v_scroll = 0; |
| 8735 | } |
| 8736 | if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) { |
| 8737 | v_scroll->set_value(target_v_scroll); |
| 8738 | queue_accessibility_update(); |
| 8739 | } else { |
| 8740 | scrolling = true; |
| 8741 | set_process_internal(true); |
| 8742 | } |
| 8743 | } else { |
| 8744 | set_v_scroll(target_v_scroll); |
| 8745 | } |
| 8746 | } |
| 8747 | |
| 8748 | void TextEdit::_scroll_down(real_t p_delta, bool p_animate) { |
| 8749 | if (scrolling && smooth_scroll_enabled && SIGN(target_v_scroll - v_scroll->get_value()) != SIGN(p_delta)) { |
| 8750 | scrolling = false; |
| 8751 | minimap_clicked = false; |
| 8752 | } |
| 8753 | |
| 8754 | if (scrolling) { |
| 8755 | target_v_scroll = (target_v_scroll + p_delta); |
| 8756 | } else { |
| 8757 | target_v_scroll = (get_v_scroll() + p_delta); |
| 8758 | } |
| 8759 | |
| 8760 | if (smooth_scroll_enabled) { |
| 8761 | int max_v_scroll = std::round(v_scroll->get_max() - v_scroll->get_page()); |
| 8762 | if (target_v_scroll > max_v_scroll) { |
| 8763 | target_v_scroll = max_v_scroll; |
| 8764 | } |
| 8765 | if (!p_animate || Math::abs(target_v_scroll - v_scroll->get_value()) < 1.0) { |
| 8766 | v_scroll->set_value(target_v_scroll); |
| 8767 | queue_accessibility_update(); |
| 8768 | } else { |
| 8769 | scrolling = true; |
| 8770 | set_process_internal(true); |
| 8771 | } |
| 8772 | } else { |
| 8773 | set_v_scroll(target_v_scroll); |
| 8774 | } |
| 8775 | } |
| 8776 | |
| 8777 | void TextEdit::_scroll_lines_up() { |
| 8778 | scrolling = false; |
| 8779 | minimap_clicked = false; |
| 8780 | |
| 8781 | // Adjust the vertical scroll. |
| 8782 | set_v_scroll(get_v_scroll() - 1); |
| 8783 | |
| 8784 | // Adjust the caret to viewport. |
| 8785 | for (int i = 0; i < carets.size(); i++) { |
| 8786 | if (has_selection(i)) { |
| 8787 | continue; |
| 8788 | } |
| 8789 | |
| 8790 | int last_vis_line = get_last_full_visible_line(); |
| 8791 | int last_vis_wrap = get_last_full_visible_line_wrap_index(); |
| 8792 | if (get_caret_line(i) > last_vis_line || (get_caret_line(i) == last_vis_line && get_caret_wrap_index(i) > last_vis_wrap)) { |
| 8793 | set_caret_line(last_vis_line, false, false, last_vis_wrap, i); |
| 8794 | } |
| 8795 | } |
| 8796 | merge_overlapping_carets(); |
| 8797 | } |
| 8798 | |
| 8799 | void TextEdit::_scroll_lines_down() { |
| 8800 | scrolling = false; |
| 8801 | minimap_clicked = false; |
| 8802 | |
| 8803 | // Adjust the vertical scroll. |
| 8804 | set_v_scroll(get_v_scroll() + 1); |
| 8805 | |
| 8806 | // Adjust the caret to viewport. |
| 8807 | for (int i = 0; i < carets.size(); i++) { |
| 8808 | if (has_selection(i)) { |
| 8809 | continue; |
| 8810 | } |
| 8811 | |
| 8812 | int first_vis_line = get_first_visible_line(); |
| 8813 | if (get_caret_line(i) < first_vis_line || (get_caret_line(i) == first_vis_line && get_caret_wrap_index(i) < first_visible_line_wrap_ofs)) { |
| 8814 | set_caret_line(first_vis_line, false, false, first_visible_line_wrap_ofs, i); |
| 8815 | } |
| 8816 | } |
| 8817 | merge_overlapping_carets(); |
| 8818 | } |
| 8819 | |
| 8820 | void TextEdit::_adjust_viewport_to_caret_horizontally(int p_caret, bool p_maximize_selection) { |
| 8821 | if (get_line_wrapping_mode() != LineWrappingMode::LINE_WRAPPING_NONE) { |
| 8822 | first_visible_col = 0; |
| 8823 | h_scroll->set_value(first_visible_col); |
| 8824 | queue_redraw(); |
| 8825 | return; |
| 8826 | } |
| 8827 | |
| 8828 | int visible_width = get_size().width - _get_current_stylebox()->get_minimum_size().width - gutters_width - gutter_padding; |
| 8829 | if (draw_minimap) { |
| 8830 | visible_width -= minimap_width; |
| 8831 | } |
| 8832 | if (v_scroll->is_visible_in_tree()) { |
| 8833 | visible_width -= v_scroll->get_combined_minimum_size().width; |
| 8834 | } |
| 8835 | visible_width -= 20; // Give it a little more space. |
| 8836 | |
| 8837 | if (visible_width <= 0) { |
| 8838 | // Not resized yet. |
| 8839 | return; |
| 8840 | } |
| 8841 | |
| 8842 | int caret_start_pos; |
| 8843 | int caret_end_pos; |
| 8844 | bool prioritize_end = true; |
| 8845 | |
| 8846 | // Get start and end position of the caret. |
| 8847 | if (has_ime_text()) { |
| 8848 | // Use the size of the IME. |
| 8849 | int ime_start_column = get_caret_column(p_caret) + ime_selection.x; |
| 8850 | caret_start_pos = _get_column_x_offset_for_line(ime_start_column, get_caret_line(p_caret), ime_start_column); |
| 8851 | int ime_end_column = get_caret_column(p_caret) + (ime_selection.y > 0 ? ime_selection.x + ime_selection.y : ime_text.length()); |
| 8852 | caret_end_pos = _get_column_x_offset_for_line(ime_end_column, get_caret_line(p_caret), ime_end_column); |
| 8853 | prioritize_end = false; |
| 8854 | } else if (p_maximize_selection && has_selection(p_caret) && get_selection_from_line(p_caret) == get_selection_to_line(p_caret)) { |
| 8855 | // Use selection if it is on one line. |
| 8856 | caret_start_pos = _get_column_x_offset_for_line(get_selection_from_column(p_caret), get_caret_line(p_caret), get_selection_from_column(p_caret)); |
| 8857 | caret_end_pos = _get_column_x_offset_for_line(get_selection_to_column(p_caret), get_caret_line(p_caret), get_selection_to_column(p_caret)); |
| 8858 | prioritize_end = is_caret_after_selection_origin(); |
| 8859 | } else { |
| 8860 | caret_start_pos = _get_column_x_offset_for_line(get_caret_column(p_caret), get_caret_line(p_caret), get_caret_column(p_caret)); |
| 8861 | caret_end_pos = caret_start_pos; |
| 8862 | } |
| 8863 | |
| 8864 | if (caret_start_pos > caret_end_pos) { |
| 8865 | // For RTL text. |
| 8866 | SWAP(caret_start_pos, caret_end_pos); |
| 8867 | prioritize_end = !prioritize_end; |
| 8868 | } |
| 8869 | |
| 8870 | if (!prioritize_end && caret_end_pos > first_visible_col + visible_width) { |
| 8871 | first_visible_col = caret_end_pos - visible_width + 1; |
| 8872 | } |
| 8873 | if (caret_start_pos < first_visible_col) { |
| 8874 | first_visible_col = caret_start_pos; |
| 8875 | } |
| 8876 | if (prioritize_end && caret_end_pos > first_visible_col + visible_width) { |
| 8877 | first_visible_col = caret_end_pos - visible_width + 1; |
| 8878 | } |
| 8879 | |
| 8880 | h_scroll->set_value(first_visible_col); |
| 8881 | |
| 8882 | queue_accessibility_update(); |
| 8883 | queue_redraw(); |
| 8884 | } |
| 8885 | |
| 8886 | // Minimap |
| 8887 | |
| 8888 | void TextEdit::_update_minimap_hover() { |
| 8889 | const Point2 mp = get_local_mouse_pos(); |
| 8890 | const int xmargin_end = get_size().width - Math::floor(_get_current_stylebox()->get_margin(SIDE_RIGHT)); |
| 8891 | |
| 8892 | bool hovering_sidebar = mp.x > xmargin_end - minimap_width && mp.x < xmargin_end; |
| 8893 | if (!hovering_sidebar) { |
| 8894 | if (hovering_minimap) { |
| 8895 | // Only redraw if the hovering status changed. |
| 8896 | hovering_minimap = false; |
| 8897 | queue_redraw(); |
| 8898 | } |
| 8899 | |
| 8900 | // Return early to avoid running the operations below when not needed. |
| 8901 | return; |
| 8902 | } |
| 8903 | |
| 8904 | const int row = get_minimap_line_at_pos(mp); |
| 8905 | |
| 8906 | bool new_hovering_minimap = row >= get_first_visible_line() && row <= get_last_full_visible_line(); |
| 8907 | if (new_hovering_minimap != hovering_minimap) { |
| 8908 | // Only redraw if the hovering status changed. |
| 8909 | hovering_minimap = new_hovering_minimap; |
| 8910 | queue_redraw(); |
| 8911 | } |
| 8912 | } |
| 8913 | |
| 8914 | void TextEdit::_update_minimap_click() { |
| 8915 | Point2 mp = get_local_mouse_pos(); |
| 8916 | |
| 8917 | int xmargin_end = get_size().width - Math::floor(_get_current_stylebox()->get_margin(SIDE_RIGHT)); |
| 8918 | if (!dragging_minimap && (mp.x < xmargin_end - minimap_width || mp.x > xmargin_end)) { |
| 8919 | minimap_clicked = false; |
| 8920 | return; |
| 8921 | } |
| 8922 | minimap_clicked = true; |
| 8923 | dragging_minimap = true; |
| 8924 | |
| 8925 | int row = get_minimap_line_at_pos(mp); |
| 8926 | |
| 8927 | if (row >= get_first_visible_line() && (row < get_last_full_visible_line() || row >= (text.size() - 1))) { |
| 8928 | minimap_scroll_ratio = v_scroll->get_as_ratio(); |
| 8929 | minimap_scroll_click_pos = mp.y; |
| 8930 | can_drag_minimap = true; |
| 8931 | return; |
| 8932 | } |
| 8933 | |
| 8934 | Point2i next_line = get_next_visible_line_index_offset_from(row, 0, -get_visible_line_count() / 2); |
| 8935 | int first_line = MAX(0, row - next_line.x + 1); |
| 8936 | double delta = get_scroll_pos_for_line(first_line, next_line.y) - get_v_scroll(); |
| 8937 | if (delta < 0) { |
| 8938 | _scroll_up(-delta, true); |
| 8939 | } else { |
| 8940 | _scroll_down(delta, true); |
| 8941 | } |
| 8942 | } |
| 8943 | |
| 8944 | void TextEdit::_update_minimap_drag() { |
| 8945 | if (!can_drag_minimap) { |
| 8946 | return; |
| 8947 | } |
| 8948 | |
| 8949 | int control_height = _get_control_height(); |
| 8950 | int scroll_height = v_scroll->get_max() * (minimap_char_size.y + minimap_line_spacing); |
| 8951 | if (control_height > scroll_height) { |
| 8952 | control_height = scroll_height; |
| 8953 | } |
| 8954 | |
| 8955 | Point2 mp = get_local_mouse_pos(); |
| 8956 | |
| 8957 | double diff = (mp.y - minimap_scroll_click_pos) / control_height; |
| 8958 | v_scroll->set_as_ratio(minimap_scroll_ratio + diff); |
| 8959 | } |
| 8960 | |
| 8961 | /* Gutters. */ |
| 8962 | void TextEdit::_update_gutter_width() { |
| 8963 | gutters_width = 0; |
| 8964 | for (int i = 0; i < gutters.size(); i++) { |
| 8965 | if (gutters[i].draw) { |
| 8966 | gutters_width += gutters[i].width; |
| 8967 | } |
| 8968 | } |
| 8969 | if (gutters_width > 0) { |
| 8970 | gutter_padding = 2; |
| 8971 | } |
| 8972 | if (get_viewport()) { |
| 8973 | hovered_gutter = _get_hovered_gutter(get_local_mouse_position()); |
| 8974 | } |
| 8975 | queue_accessibility_update(); |
| 8976 | queue_redraw(); |
| 8977 | } |
| 8978 | |
| 8979 | Vector2i TextEdit::_get_hovered_gutter(const Point2 &p_mouse_pos) const { |
| 8980 | int left_margin = Math::ceil(_get_current_stylebox()->get_margin(SIDE_LEFT)); |
| 8981 | if (p_mouse_pos.x > left_margin + gutters_width + gutter_padding) { |
| 8982 | return Vector2i(-1, -1); |
| 8983 | } |
| 8984 | int hovered_row = get_line_column_at_pos(p_mouse_pos, false).y; |
| 8985 | if (hovered_row == -1) { |
| 8986 | return Vector2i(-1, -1); |
| 8987 | } |
| 8988 | for (int i = 0; i < gutters.size(); i++) { |
| 8989 | if (!gutters[i].draw || gutters[i].width <= 0) { |
| 8990 | continue; |
| 8991 | } |
| 8992 | |
| 8993 | if (p_mouse_pos.x >= left_margin && p_mouse_pos.x < left_margin + gutters[i].width) { |
| 8994 | return Vector2i(i, hovered_row); |
| 8995 | } |
| 8996 | |
| 8997 | left_margin += gutters[i].width; |
| 8998 | } |
| 8999 | return Vector2i(-1, -1); |
| 9000 | } |
| 9001 | |
| 9002 | /* Syntax highlighting. */ |
| 9003 | Vector<Pair<int64_t, Color>> TextEdit::_get_line_syntax_highlighting(int p_line) { |
| 9004 | if (syntax_highlighter.is_null() || setting_text) { |
| 9005 | return Vector<Pair<int64_t, Color>>(); |
| 9006 | } |
| 9007 | |
| 9008 | HashMap<int, Vector<Pair<int64_t, Color>>>::Iterator E = syntax_highlighting_cache.find(p_line); |
| 9009 | if (E) { |
| 9010 | return E->value; |
| 9011 | } |
| 9012 | |
| 9013 | Dictionary color_map = syntax_highlighter->get_line_syntax_highlighting(p_line); |
| 9014 | Vector<Pair<int64_t, Color>> result; |
| 9015 | result.resize(color_map.size()); |
| 9016 | int i = 0; |
| 9017 | for (const Variant *key = color_map.next(nullptr); key; key = color_map.next(key), i++) { |
| 9018 | int64_t key_data = *key; |
| 9019 | const Variant *color_data = color_map.getptr(*key); |
| 9020 | Color color_value = editable ? theme_cache.font_color : theme_cache.font_readonly_color; |
| 9021 | if (color_data != nullptr) { |
| 9022 | color_value = (color_data->operator Dictionary()).get("color", color_value); |
| 9023 | } |
| 9024 | result.write[i] = Pair<int64_t, Color>(key_data, color_value); |
| 9025 | } |
| 9026 | syntax_highlighting_cache.insert(p_line, result); |
| 9027 | |
| 9028 | return result; |
| 9029 | } |
| 9030 | |
| 9031 | void TextEdit::_clear_syntax_highlighting_cache() { |
| 9032 | syntax_highlighting_cache.clear(); |
| 9033 | } |
| 9034 | |
| 9035 | /* Deprecated. */ |
| 9036 | #ifndef DISABLE_DEPRECATED |
| 9037 | Vector<int> TextEdit::get_caret_index_edit_order() { |
| 9038 | Vector<int> carets_order = get_sorted_carets(); |
| 9039 | carets_order.reverse(); |
| 9040 | return carets_order; |
| 9041 | } |
| 9042 | |
| 9043 | void TextEdit::adjust_carets_after_edit(int p_caret, int p_from_line, int p_from_col, int p_to_line, int p_to_col) { |
| 9044 | } |
| 9045 | |
| 9046 | int TextEdit::get_selection_line(int p_caret) const { |
| 9047 | return get_selection_origin_line(p_caret); |
| 9048 | } |
| 9049 | |
| 9050 | int TextEdit::get_selection_column(int p_caret) const { |
| 9051 | return get_selection_origin_column(p_caret); |
| 9052 | } |
| 9053 | #endif |
| 9054 | |
| 9055 | /*** Super internal Core API. Everything builds on it. ***/ |
| 9056 | |
| 9057 | void TextEdit::_text_changed() { |
| 9058 | _clear_syntax_highlighting_cache(); |
| 9059 | _cancel_drag_and_drop_text(); |
| 9060 | queue_redraw(); |
| 9061 | |
| 9062 | if (text_changed_dirty || setting_text) { |
| 9063 | return; |
| 9064 | } |
| 9065 | |
| 9066 | if (is_inside_tree()) { |
| 9067 | callable_mp(this, &TextEdit::_emit_text_changed).call_deferred(); |
| 9068 | } |
| 9069 | text_changed_dirty = true; |
| 9070 | } |
| 9071 | |
| 9072 | void TextEdit::_emit_text_changed() { |
| 9073 | emit_signal(SceneStringName(text_changed)); |
| 9074 | text_changed_dirty = false; |
| 9075 | } |
| 9076 | |
| 9077 | void TextEdit::_insert_text(int p_line, int p_char, const String &p_text, int *r_end_line, int *r_end_char) { |
| 9078 | if (!setting_text && idle_detect->is_inside_tree()) { |
| 9079 | idle_detect->start(); |
| 9080 | } |
| 9081 | |
| 9082 | if (undo_enabled) { |
| 9083 | _clear_redo(); |
| 9084 | } |
| 9085 | |
| 9086 | int retline, retchar; |
| 9087 | _base_insert_text(p_line, p_char, p_text, retline, retchar); |
| 9088 | if (r_end_line) { |
| 9089 | *r_end_line = retline; |
| 9090 | } |
| 9091 | if (r_end_char) { |
| 9092 | *r_end_char = retchar; |
| 9093 | } |
| 9094 | |
| 9095 | if (!undo_enabled) { |
| 9096 | return; |
| 9097 | } |
| 9098 | |
| 9099 | /* UNDO!! */ |
| 9100 | TextOperation op; |
| 9101 | op.type = TextOperation::TYPE_INSERT; |
| 9102 | op.from_line = p_line; |
| 9103 | op.from_column = p_char; |
| 9104 | op.to_line = retline; |
| 9105 | op.to_column = retchar; |
| 9106 | op.text = p_text; |
| 9107 | op.version = ++version; |
| 9108 | op.chain_forward = false; |
| 9109 | op.chain_backward = false; |
| 9110 | if (next_operation_is_complex) { |
| 9111 | op.start_carets = current_op.start_carets; |
| 9112 | } else { |
| 9113 | op.start_carets = carets; |
| 9114 | } |
| 9115 | op.end_carets = carets; |
| 9116 | |
| 9117 | op.prev_version = get_version(); |
| 9118 | _push_current_op(); |
| 9119 | current_op = op; |
| 9120 | } |
| 9121 | |
| 9122 | void TextEdit::_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { |
| 9123 | if (!setting_text && idle_detect->is_inside_tree()) { |
| 9124 | idle_detect->start(); |
| 9125 | } |
| 9126 | |
| 9127 | String txt; |
| 9128 | if (undo_enabled) { |
| 9129 | _clear_redo(); |
| 9130 | txt = _base_get_text(p_from_line, p_from_column, p_to_line, p_to_column); |
| 9131 | } |
| 9132 | |
| 9133 | _base_remove_text(p_from_line, p_from_column, p_to_line, p_to_column); |
| 9134 | |
| 9135 | if (!undo_enabled) { |
| 9136 | return; |
| 9137 | } |
| 9138 | |
| 9139 | /* UNDO! */ |
| 9140 | TextOperation op; |
| 9141 | op.type = TextOperation::TYPE_REMOVE; |
| 9142 | op.from_line = p_from_line; |
| 9143 | op.from_column = p_from_column; |
| 9144 | op.to_line = p_to_line; |
| 9145 | op.to_column = p_to_column; |
| 9146 | op.text = txt; |
| 9147 | op.version = ++version; |
| 9148 | op.chain_forward = false; |
| 9149 | op.chain_backward = false; |
| 9150 | if (next_operation_is_complex) { |
| 9151 | op.start_carets = current_op.start_carets; |
| 9152 | } else { |
| 9153 | op.start_carets = carets; |
| 9154 | } |
| 9155 | op.end_carets = carets; |
| 9156 | |
| 9157 | op.prev_version = get_version(); |
| 9158 | _push_current_op(); |
| 9159 | current_op = op; |
| 9160 | } |
| 9161 | |
| 9162 | void TextEdit::_base_insert_text(int p_line, int p_char, const String &p_text, int &r_end_line, int &r_end_column) { |
| 9163 | // Save for undo. |
| 9164 | ERR_FAIL_INDEX(p_line, text.size()); |
| 9165 | ERR_FAIL_COND(p_char < 0); |
| 9166 | |
| 9167 | /* STEP 1: Remove \r from source text and separate in substrings. */ |
| 9168 | const String text_to_insert = p_text.remove_char('\r'); |
| 9169 | Vector<String> substrings = text_to_insert.split("\n"); |
| 9170 | |
| 9171 | // Is this just a new empty line? |
| 9172 | bool shift_first_line = p_char == 0 && substrings.size() == 2 && text_to_insert == "\n"; |
| 9173 | |
| 9174 | /* STEP 2: Add spaces if the char is greater than the end of the line. */ |
| 9175 | while (p_char > text[p_line].length()) { |
| 9176 | text.set(p_line, text[p_line] + String::chr(' '), structured_text_parser(st_parser, st_args, text[p_line] + String::chr(' '))); |
| 9177 | } |
| 9178 | |
| 9179 | /* STEP 3: Separate dest string in pre and post text. */ |
| 9180 | String postinsert_text = text[p_line].substr(p_char); |
| 9181 | |
| 9182 | substrings.write[0] = text[p_line].substr(0, p_char) + substrings[0]; |
| 9183 | substrings.write[substrings.size() - 1] += postinsert_text; |
| 9184 | |
| 9185 | Vector<Array> bidi_override; |
| 9186 | bidi_override.resize(substrings.size()); |
| 9187 | for (int i = 0; i < substrings.size(); i++) { |
| 9188 | bidi_override.write[i] = structured_text_parser(st_parser, st_args, substrings[i]); |
| 9189 | } |
| 9190 | |
| 9191 | text.insert(p_line, substrings, bidi_override); |
| 9192 | |
| 9193 | if (shift_first_line) { |
| 9194 | text.move_gutters(p_line, p_line + 1); |
| 9195 | text.set_hidden(p_line + 1, text.is_hidden(p_line)); |
| 9196 | |
| 9197 | text.set_hidden(p_line, false); |
| 9198 | } |
| 9199 | |
| 9200 | r_end_line = p_line + substrings.size() - 1; |
| 9201 | r_end_column = text[r_end_line].length() - postinsert_text.length(); |
| 9202 | |
| 9203 | TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text.get_line_data(r_end_line)->get_rid(), (r_end_line == p_line) ? carets[0].column : 0, r_end_column); |
| 9204 | if (dir != TextServer::DIRECTION_AUTO) { |
| 9205 | input_direction = (TextDirection)dir; |
| 9206 | } |
| 9207 | |
| 9208 | _text_changed(); |
| 9209 | emit_signal(SNAME("lines_edited_from"), p_line, r_end_line); |
| 9210 | } |
| 9211 | |
| 9212 | String TextEdit::_base_get_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) const { |
| 9213 | ERR_FAIL_INDEX_V(p_from_line, text.size(), String()); |
| 9214 | ERR_FAIL_INDEX_V(p_from_column, text[p_from_line].length() + 1, String()); |
| 9215 | ERR_FAIL_INDEX_V(p_to_line, text.size(), String()); |
| 9216 | ERR_FAIL_INDEX_V(p_to_column, text[p_to_line].length() + 1, String()); |
| 9217 | ERR_FAIL_COND_V(p_to_line < p_from_line, String()); // 'from > to'. |
| 9218 | ERR_FAIL_COND_V(p_to_line == p_from_line && p_to_column < p_from_column, String()); // 'from > to'. |
| 9219 | |
| 9220 | StringBuilder ret; |
| 9221 | |
| 9222 | for (int i = p_from_line; i <= p_to_line; i++) { |
| 9223 | int begin = (i == p_from_line) ? p_from_column : 0; |
| 9224 | int end = (i == p_to_line) ? p_to_column : text[i].length(); |
| 9225 | |
| 9226 | if (i > p_from_line) { |
| 9227 | ret += "\n"; |
| 9228 | } |
| 9229 | ret += text[i].substr(begin, end - begin); |
| 9230 | } |
| 9231 | |
| 9232 | return ret.as_string(); |
| 9233 | } |
| 9234 | |
| 9235 | void TextEdit::_base_remove_text(int p_from_line, int p_from_column, int p_to_line, int p_to_column) { |
| 9236 | ERR_FAIL_INDEX(p_from_line, text.size()); |
| 9237 | ERR_FAIL_INDEX(p_from_column, text[p_from_line].length() + 1); |
| 9238 | ERR_FAIL_INDEX(p_to_line, text.size()); |
| 9239 | ERR_FAIL_INDEX(p_to_column, text[p_to_line].length() + 1); |
| 9240 | ERR_FAIL_COND(p_to_line < p_from_line); // 'from > to'. |
| 9241 | ERR_FAIL_COND(p_to_line == p_from_line && p_to_column < p_from_column); // 'from > to'. |
| 9242 | |
| 9243 | String pre_text = text[p_from_line].substr(0, p_from_column); |
| 9244 | String post_text = text[p_to_line].substr(p_to_column); |
| 9245 | |
| 9246 | text.remove_range(p_from_line, p_to_line); |
| 9247 | text.set(p_from_line, pre_text + post_text, structured_text_parser(st_parser, st_args, pre_text + post_text)); |
| 9248 | |
| 9249 | _text_changed(); |
| 9250 | emit_signal(SNAME("lines_edited_from"), p_to_line, p_from_line); |
| 9251 | } |
| 9252 | |
| 9253 | void TextEdit::_draw_rect_unfilled(RID p_canvas_item, const Rect2 &p_rect, const Color &p_color, real_t p_width, bool p_antialiased) const { |
| 9254 | Rect2 rect = p_rect.abs(); |
| 9255 | |
| 9256 | if (p_width >= rect.size.width || p_width >= rect.size.height) { |
| 9257 | RS::get_singleton()->canvas_item_add_rect(p_canvas_item, rect.grow(0.5f * p_width), p_color, p_antialiased); |
| 9258 | } else { |
| 9259 | Vector<Vector2> points; |
| 9260 | points.resize(5); |
| 9261 | points.write[0] = rect.position; |
| 9262 | points.write[1] = rect.position + Vector2(rect.size.x, 0); |
| 9263 | points.write[2] = rect.position + rect.size; |
| 9264 | points.write[3] = rect.position + Vector2(0, rect.size.y); |
| 9265 | points.write[4] = rect.position; |
| 9266 | |
| 9267 | Vector<Color> colors = { p_color }; |
| 9268 | |
| 9269 | RS::get_singleton()->canvas_item_add_polyline(p_canvas_item, points, colors, p_width, p_antialiased); |
| 9270 | } |
| 9271 | } |
| 9272 | |
| 9273 | TextEdit::TextEdit(const String &p_placeholder) { |
| 9274 | placeholder_data_buf.instantiate(); |
| 9275 | carets.push_back(Caret()); |
| 9276 | |
| 9277 | clear(); |
| 9278 | set_focus_mode(FOCUS_ALL); |
| 9279 | set_default_cursor_shape(CURSOR_IBEAM); |
| 9280 | set_process_unhandled_key_input(true); |
| 9281 | |
| 9282 | text.set_tab_size(text.get_tab_size()); |
| 9283 | |
| 9284 | text_ci = RS::get_singleton()->canvas_item_create(); |
| 9285 | RS::get_singleton()->canvas_item_set_parent(text_ci, get_canvas_item()); |
| 9286 | RS::get_singleton()->canvas_item_set_use_parent_material(text_ci, true); |
| 9287 | |
| 9288 | h_scroll = memnew(HScrollBar); |
| 9289 | v_scroll = memnew(VScrollBar); |
| 9290 | |
| 9291 | add_child(h_scroll, false, INTERNAL_MODE_FRONT); |
| 9292 | add_child(v_scroll, false, INTERNAL_MODE_FRONT); |
| 9293 | |
| 9294 | h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &TextEdit::_scroll_moved)); |
| 9295 | v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &TextEdit::_scroll_moved)); |
| 9296 | |
| 9297 | v_scroll->connect("scrolling", callable_mp(this, &TextEdit::_v_scroll_input)); |
| 9298 | |
| 9299 | /* Caret. */ |
| 9300 | caret_blink_timer = memnew(Timer); |
| 9301 | add_child(caret_blink_timer, false, INTERNAL_MODE_FRONT); |
| 9302 | caret_blink_timer->set_wait_time(0.65); |
| 9303 | caret_blink_timer->connect("timeout", callable_mp(this, &TextEdit::_toggle_draw_caret)); |
| 9304 | set_caret_blink_enabled(false); |
| 9305 | |
| 9306 | /* Selection. */ |
| 9307 | click_select_held = memnew(Timer); |
| 9308 | add_child(click_select_held, false, INTERNAL_MODE_FRONT); |
| 9309 | click_select_held->set_wait_time(0.05); |
| 9310 | click_select_held->connect("timeout", callable_mp(this, &TextEdit::_click_selection_held)); |
| 9311 | |
| 9312 | idle_detect = memnew(Timer); |
| 9313 | add_child(idle_detect, false, INTERNAL_MODE_FRONT); |
| 9314 | idle_detect->set_one_shot(true); |
| 9315 | idle_detect->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/text_edit_idle_detect_sec")); |
| 9316 | idle_detect->connect("timeout", callable_mp(this, &TextEdit::_push_current_op)); |
| 9317 | |
| 9318 | undo_stack_max_size = GLOBAL_GET_CACHED(int, "gui/common/text_edit_undo_stack_max_size"); |
| 9319 | |
| 9320 | set_placeholder(p_placeholder); |
| 9321 | |
| 9322 | set_editable(true); |
| 9323 | } |
| 9324 | |
| 9325 | TextEdit::~TextEdit() { |
| 9326 | RS::get_singleton()->free_rid(text_ci); |
| 9327 | } |