| 1 | /**************************************************************************/ |
| 2 | /* line_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 "line_edit.h" |
| 32 | #include "line_edit.compat.inc" |
| 33 | |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/input/input_map.h" |
| 36 | #include "core/os/keyboard.h" |
| 37 | #include "core/os/os.h" |
| 38 | #include "core/string/alt_codes.h" |
| 39 | #include "core/string/translation_server.h" |
| 40 | #include "scene/gui/label.h" |
| 41 | #include "scene/main/window.h" |
| 42 | #include "scene/theme/theme_db.h" |
| 43 | #include "servers/display/display_server.h" |
| 44 | #include "servers/text/text_server.h" |
| 45 | |
| 46 | #ifdef TOOLS_ENABLED |
| 47 | #include "editor/settings/editor_settings.h" |
| 48 | #endif |
| 49 | |
| 50 | void LineEdit::edit(bool p_hide_focus) { |
| 51 | _edit(true, p_hide_focus); |
| 52 | } |
| 53 | |
| 54 | void LineEdit::_edit(bool p_show_virtual_keyboard, bool p_hide_focus) { |
| 55 | if (!is_inside_tree()) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | if (!has_focus()) { |
| 60 | grab_focus(p_hide_focus); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (!editable || editing) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if (select_all_on_focus) { |
| 69 | if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) { |
| 70 | // Select all when the mouse button is up. |
| 71 | pending_select_all_on_focus = true; |
| 72 | } else { |
| 73 | select_all(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | editing = true; |
| 78 | _validate_caret_can_draw(); |
| 79 | |
| 80 | if (p_show_virtual_keyboard && !pending_select_all_on_focus) { |
| 81 | show_virtual_keyboard(); |
| 82 | } |
| 83 | queue_redraw(); |
| 84 | } |
| 85 | |
| 86 | void LineEdit::unedit() { |
| 87 | if (!editing) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | editing = false; |
| 92 | _validate_caret_can_draw(); |
| 93 | |
| 94 | apply_ime(); |
| 95 | set_caret_column(caret_column); // Update scroll_offset. |
| 96 | |
| 97 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) { |
| 98 | DisplayServer::get_singleton()->virtual_keyboard_hide(); |
| 99 | } |
| 100 | |
| 101 | if (deselect_on_focus_loss_enabled && !selection.drag_attempt) { |
| 102 | deselect(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | bool LineEdit::is_editing() const { |
| 107 | return editing; |
| 108 | } |
| 109 | |
| 110 | void LineEdit::set_keep_editing_on_text_submit(bool p_enabled) { |
| 111 | keep_editing_on_text_submit = p_enabled; |
| 112 | } |
| 113 | |
| 114 | bool LineEdit::is_editing_kept_on_text_submit() const { |
| 115 | return keep_editing_on_text_submit; |
| 116 | } |
| 117 | |
| 118 | void LineEdit::_close_ime_window() { |
| 119 | DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID; |
| 120 | if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) { |
| 121 | return; |
| 122 | } |
| 123 | DisplayServer::get_singleton()->window_set_ime_position(Point2(), wid); |
| 124 | DisplayServer::get_singleton()->window_set_ime_active(false, wid); |
| 125 | } |
| 126 | |
| 127 | void LineEdit::_update_ime_window_position() { |
| 128 | DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID; |
| 129 | if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) { |
| 130 | return; |
| 131 | } |
| 132 | DisplayServer::get_singleton()->window_set_ime_active(true, wid); |
| 133 | Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2) + get_global_position(); |
| 134 | if (get_window()->get_embedder()) { |
| 135 | pos += get_viewport()->get_popup_base_transform().get_origin(); |
| 136 | } |
| 137 | // Take into account the window's transform. |
| 138 | pos = get_window()->get_screen_transform().xform(pos); |
| 139 | // The window will move to the updated position the next time the IME is updated, not immediately. |
| 140 | DisplayServer::get_singleton()->window_set_ime_position(pos, wid); |
| 141 | } |
| 142 | |
| 143 | bool LineEdit::has_ime_text() const { |
| 144 | return !ime_text.is_empty(); |
| 145 | } |
| 146 | |
| 147 | void LineEdit::cancel_ime() { |
| 148 | if (!has_ime_text()) { |
| 149 | _close_ime_window(); |
| 150 | return; |
| 151 | } |
| 152 | ime_text = String(); |
| 153 | ime_selection = Vector2i(); |
| 154 | alt_start = false; |
| 155 | alt_start_no_hold = false; |
| 156 | _close_ime_window(); |
| 157 | _shape(); |
| 158 | } |
| 159 | |
| 160 | void LineEdit::apply_ime() { |
| 161 | if (!has_ime_text()) { |
| 162 | _close_ime_window(); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Force apply the current IME text. |
| 167 | if (alt_start || alt_start_no_hold) { |
| 168 | cancel_ime(); |
| 169 | if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) { |
| 170 | char32_t ucodestr[2] = { (char32_t)alt_code, 0 }; |
| 171 | insert_text_at_caret(ucodestr); |
| 172 | } |
| 173 | } else { |
| 174 | String insert_ime_text = ime_text; |
| 175 | cancel_ime(); |
| 176 | insert_text_at_caret(insert_ime_text); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | void LineEdit::_swap_current_input_direction() { |
| 181 | if (input_direction == TEXT_DIRECTION_LTR) { |
| 182 | input_direction = TEXT_DIRECTION_RTL; |
| 183 | } else { |
| 184 | input_direction = TEXT_DIRECTION_LTR; |
| 185 | } |
| 186 | set_caret_column(get_caret_column()); |
| 187 | } |
| 188 | |
| 189 | void LineEdit::_move_caret_left(bool p_select, bool p_move_by_word) { |
| 190 | if (selection.enabled && !p_select) { |
| 191 | set_caret_column(selection.begin); |
| 192 | deselect(); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | shift_selection_check_pre(p_select); |
| 197 | |
| 198 | if (p_move_by_word) { |
| 199 | int cc = caret_column; |
| 200 | |
| 201 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid); |
| 202 | if (words.is_empty() || cc <= words[0]) { |
| 203 | // Move to the start when there are no more words. |
| 204 | cc = 0; |
| 205 | } else { |
| 206 | for (int i = words.size() - 2; i >= 0; i = i - 2) { |
| 207 | if (words[i] < cc) { |
| 208 | cc = words[i]; |
| 209 | break; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | set_caret_column(cc); |
| 215 | } else { |
| 216 | if (caret_mid_grapheme_enabled) { |
| 217 | set_caret_column(get_caret_column() - 1); |
| 218 | } else { |
| 219 | set_caret_column(TS->shaped_text_prev_character_pos(text_rid, get_caret_column())); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | shift_selection_check_post(p_select); |
| 224 | _reset_caret_blink_timer(); |
| 225 | } |
| 226 | |
| 227 | void LineEdit::_move_caret_right(bool p_select, bool p_move_by_word) { |
| 228 | if (selection.enabled && !p_select) { |
| 229 | set_caret_column(selection.end); |
| 230 | deselect(); |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | shift_selection_check_pre(p_select); |
| 235 | |
| 236 | if (p_move_by_word) { |
| 237 | int cc = caret_column; |
| 238 | |
| 239 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid); |
| 240 | if (words.is_empty() || cc >= words[words.size() - 1]) { |
| 241 | // Move to the end when there are no more words. |
| 242 | cc = text.length(); |
| 243 | } else { |
| 244 | for (int i = 1; i < words.size(); i = i + 2) { |
| 245 | if (words[i] > cc) { |
| 246 | cc = words[i]; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | set_caret_column(cc); |
| 253 | } else { |
| 254 | if (caret_mid_grapheme_enabled) { |
| 255 | set_caret_column(get_caret_column() + 1); |
| 256 | } else { |
| 257 | set_caret_column(TS->shaped_text_next_character_pos(text_rid, get_caret_column())); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | shift_selection_check_post(p_select); |
| 262 | _reset_caret_blink_timer(); |
| 263 | } |
| 264 | |
| 265 | void LineEdit::_move_caret_start(bool p_select) { |
| 266 | shift_selection_check_pre(p_select); |
| 267 | set_caret_column(0); |
| 268 | shift_selection_check_post(p_select); |
| 269 | } |
| 270 | |
| 271 | void LineEdit::_move_caret_end(bool p_select) { |
| 272 | shift_selection_check_pre(p_select); |
| 273 | set_caret_column(text.length()); |
| 274 | shift_selection_check_post(p_select); |
| 275 | } |
| 276 | |
| 277 | void LineEdit::_backspace(bool p_word, bool p_all_to_left) { |
| 278 | if (!editable) { |
| 279 | return; |
| 280 | } |
| 281 | |
| 282 | if (selection.enabled) { |
| 283 | selection_delete(); |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | if (caret_column == 0) { |
| 288 | return; // Nothing to do. |
| 289 | } |
| 290 | |
| 291 | if (p_all_to_left) { |
| 292 | text = text.substr(caret_column); |
| 293 | _shape(); |
| 294 | set_caret_column(0); |
| 295 | _text_changed(); |
| 296 | return; |
| 297 | } |
| 298 | |
| 299 | if (p_word) { |
| 300 | int cc = caret_column; |
| 301 | |
| 302 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid); |
| 303 | if (words.is_empty() || cc <= words[0]) { |
| 304 | // Delete to the start when there are no more words. |
| 305 | cc = 0; |
| 306 | } else { |
| 307 | for (int i = words.size() - 2; i >= 0; i = i - 2) { |
| 308 | if (words[i] < cc) { |
| 309 | cc = words[i]; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | delete_text(cc, caret_column); |
| 316 | |
| 317 | set_caret_column(cc); |
| 318 | } else { |
| 319 | delete_char(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | void LineEdit::_delete(bool p_word, bool p_all_to_right) { |
| 324 | if (!editable) { |
| 325 | return; |
| 326 | } |
| 327 | |
| 328 | if (selection.enabled) { |
| 329 | selection_delete(); |
| 330 | return; |
| 331 | } |
| 332 | |
| 333 | if (caret_column == text.length()) { |
| 334 | return; // Nothing to do. |
| 335 | } |
| 336 | |
| 337 | if (p_all_to_right) { |
| 338 | text = text.substr(0, caret_column); |
| 339 | _shape(); |
| 340 | _text_changed(); |
| 341 | return; |
| 342 | } |
| 343 | |
| 344 | if (p_word) { |
| 345 | int cc = caret_column; |
| 346 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid); |
| 347 | if (words.is_empty() || cc >= words[words.size() - 1]) { |
| 348 | // Delete to the end when there are no more words. |
| 349 | cc = text.length(); |
| 350 | } else { |
| 351 | for (int i = 1; i < words.size(); i = i + 2) { |
| 352 | if (words[i] > cc) { |
| 353 | cc = words[i]; |
| 354 | break; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | delete_text(caret_column, cc); |
| 360 | set_caret_column(caret_column); |
| 361 | } else { |
| 362 | if (caret_mid_grapheme_enabled) { |
| 363 | set_caret_column(caret_column + 1); |
| 364 | delete_char(); |
| 365 | } else { |
| 366 | int cc = caret_column; |
| 367 | set_caret_column(TS->shaped_text_next_character_pos(text_rid, caret_column)); |
| 368 | delete_text(cc, caret_column); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | Point2 LineEdit::_get_right_icon_size(Ref<Texture2D> p_right_icon) const { |
| 374 | Size2 icon_size; |
| 375 | |
| 376 | if (p_right_icon.is_null()) { |
| 377 | return icon_size; |
| 378 | } |
| 379 | |
| 380 | switch (icon_expand_mode) { |
| 381 | default: |
| 382 | case LineEdit::EXPAND_MODE_ORIGINAL_SIZE: |
| 383 | icon_size = p_right_icon->get_size(); |
| 384 | break; |
| 385 | case LineEdit::EXPAND_MODE_FIT_TO_TEXT: { |
| 386 | real_t height = theme_cache.font->get_height(theme_cache.font_size); |
| 387 | icon_size = Size2(height, height); |
| 388 | } break; |
| 389 | case LineEdit::EXPAND_MODE_FIT_TO_LINE_EDIT: { |
| 390 | icon_size = p_right_icon->get_size(); |
| 391 | Point2 size = get_size(); |
| 392 | float icon_width = icon_size.width * size.height / icon_size.height; |
| 393 | float icon_height = size.height; |
| 394 | |
| 395 | if (icon_width > size.width) { |
| 396 | icon_width = size.width; |
| 397 | icon_height = icon_size.height * icon_width / icon_size.width; |
| 398 | } |
| 399 | |
| 400 | icon_size = Size2(icon_width, icon_height) * right_icon_scale; |
| 401 | } break; |
| 402 | } |
| 403 | |
| 404 | return icon_size; |
| 405 | } |
| 406 | |
| 407 | void LineEdit::unhandled_key_input(const Ref<InputEvent> &p_event) { |
| 408 | // Return to prevent editing if just focused. |
| 409 | if (!editing) { |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | Ref<InputEventKey> k = p_event; |
| 414 | |
| 415 | if (k.is_valid()) { |
| 416 | if (!k->is_pressed()) { |
| 417 | return; |
| 418 | } |
| 419 | // Handle Unicode (with modifiers active, process after shortcuts). |
| 420 | if (has_focus() && editable && (k->get_unicode() >= 32)) { |
| 421 | selection_delete(); |
| 422 | char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 }; |
| 423 | int prev_len = text.length(); |
| 424 | insert_text_at_caret(ucodestr); |
| 425 | if (text.length() != prev_len) { |
| 426 | _text_changed(); |
| 427 | } |
| 428 | accept_event(); |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | void LineEdit::gui_input(const Ref<InputEvent> &p_event) { |
| 434 | ERR_FAIL_COND(p_event.is_null()); |
| 435 | |
| 436 | Ref<InputEventMouseButton> b = p_event; |
| 437 | |
| 438 | if (b.is_valid()) { |
| 439 | if (b->is_pressed() && b->get_button_index() == MouseButton::RIGHT) { |
| 440 | apply_ime(); |
| 441 | |
| 442 | if (editable && !selection.enabled) { |
| 443 | set_caret_at_pixel_pos(b->get_position().x); |
| 444 | } |
| 445 | |
| 446 | if (context_menu_enabled) { |
| 447 | _update_context_menu(); |
| 448 | menu->set_position(get_screen_transform().xform(get_local_mouse_position())); |
| 449 | menu->reset_size(); |
| 450 | menu->popup(); |
| 451 | } |
| 452 | |
| 453 | if (editable && !editing) { |
| 454 | edit(true); |
| 455 | emit_signal(SNAME("editing_toggled"), true); |
| 456 | } |
| 457 | |
| 458 | accept_event(); |
| 459 | return; |
| 460 | } |
| 461 | |
| 462 | if (editable && is_middle_mouse_paste_enabled() && b->is_pressed() && b->get_button_index() == MouseButton::MIDDLE && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 463 | apply_ime(); |
| 464 | |
| 465 | String paste_buffer = DisplayServer::get_singleton()->clipboard_get_primary().strip_escapes(); |
| 466 | |
| 467 | deselect(); |
| 468 | set_caret_at_pixel_pos(b->get_position().x); |
| 469 | |
| 470 | if (!editing) { |
| 471 | edit(true); |
| 472 | emit_signal(SNAME("editing_toggled"), true); |
| 473 | } |
| 474 | |
| 475 | if (!paste_buffer.is_empty()) { |
| 476 | insert_text_at_caret(paste_buffer); |
| 477 | |
| 478 | if (!text_changed_dirty) { |
| 479 | if (is_inside_tree()) { |
| 480 | callable_mp(this, &LineEdit::_text_changed).call_deferred(); |
| 481 | } |
| 482 | text_changed_dirty = true; |
| 483 | } |
| 484 | } |
| 485 | accept_event(); |
| 486 | return; |
| 487 | } |
| 488 | |
| 489 | if (b->get_button_index() != MouseButton::LEFT) { |
| 490 | return; |
| 491 | } |
| 492 | |
| 493 | if (editing) { |
| 494 | _reset_caret_blink_timer(); |
| 495 | } |
| 496 | |
| 497 | if (b->is_pressed()) { |
| 498 | apply_ime(); |
| 499 | |
| 500 | accept_event(); // Don't pass event further when clicked on text field. |
| 501 | if (editable && !text.is_empty() && _is_over_clear_button(b->get_position())) { |
| 502 | clear_button_status.press_attempt = true; |
| 503 | clear_button_status.pressing_inside = true; |
| 504 | queue_redraw(); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | if (b->is_shift_pressed()) { |
| 509 | shift_selection_check_pre(true); |
| 510 | } |
| 511 | |
| 512 | set_caret_at_pixel_pos(b->get_position().x); |
| 513 | |
| 514 | if (b->is_shift_pressed()) { |
| 515 | selection_fill_at_caret(); |
| 516 | selection.creating = true; |
| 517 | |
| 518 | } else { |
| 519 | if (selecting_enabled && !text.is_empty()) { |
| 520 | const int triple_click_timeout = 600; |
| 521 | const int triple_click_tolerance = 5; |
| 522 | const bool is_triple_click = !b->is_double_click() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < triple_click_timeout && b->get_position().distance_to(last_dblclk_pos) < triple_click_tolerance; |
| 523 | |
| 524 | if (is_triple_click) { |
| 525 | // Triple-click select all. |
| 526 | selection.enabled = true; |
| 527 | selection.begin = 0; |
| 528 | selection.end = text.length(); |
| 529 | selection.double_click = true; |
| 530 | last_dblclk = 0; |
| 531 | set_caret_column(selection.begin); |
| 532 | if (!pass && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 533 | DisplayServer::get_singleton()->clipboard_set_primary(text); |
| 534 | } |
| 535 | queue_accessibility_update(); |
| 536 | } else if (b->is_double_click()) { |
| 537 | // Double-click select word. |
| 538 | last_dblclk = OS::get_singleton()->get_ticks_msec(); |
| 539 | last_dblclk_pos = b->get_position(); |
| 540 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid); |
| 541 | for (int i = 0; i < words.size(); i = i + 2) { |
| 542 | if (words[i] <= caret_column && words[i + 1] >= caret_column) { |
| 543 | selection.enabled = true; |
| 544 | selection.begin = words[i]; |
| 545 | selection.end = words[i + 1]; |
| 546 | selection.double_click = true; |
| 547 | selection.creating = true; |
| 548 | selection.start_column = caret_column; |
| 549 | set_caret_column(selection.end); |
| 550 | queue_accessibility_update(); |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | if (!pass && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 555 | DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text()); |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | selection.drag_attempt = false; |
| 561 | if (!selection.double_click) { |
| 562 | bool is_inside_sel = selection.enabled && caret_column >= selection.begin && caret_column <= selection.end; |
| 563 | if (!pass && drag_and_drop_selection_enabled && is_inside_sel) { |
| 564 | selection.drag_attempt = true; |
| 565 | } else { |
| 566 | deselect(); |
| 567 | selection.start_column = caret_column; |
| 568 | selection.creating = true; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | if (editable && !editing) { |
| 574 | edit(true); |
| 575 | emit_signal(SNAME("editing_toggled"), true); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | } else { |
| 580 | if (selection.enabled && !pass && b->get_button_index() == MouseButton::LEFT && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) { |
| 581 | DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text()); |
| 582 | } |
| 583 | if (editable && !text.is_empty() && clear_button_enabled) { |
| 584 | bool press_attempt = clear_button_status.press_attempt; |
| 585 | clear_button_status.press_attempt = false; |
| 586 | if (press_attempt && clear_button_status.pressing_inside && _is_over_clear_button(b->get_position())) { |
| 587 | clear(); |
| 588 | return; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if ((!selection.creating) && (!selection.double_click)) { |
| 593 | deselect(); |
| 594 | } |
| 595 | selection.creating = false; |
| 596 | selection.double_click = false; |
| 597 | if (!drag_action) { |
| 598 | selection.drag_attempt = false; |
| 599 | } |
| 600 | |
| 601 | if (pending_select_all_on_focus) { |
| 602 | select_all(); |
| 603 | pending_select_all_on_focus = false; |
| 604 | } |
| 605 | |
| 606 | if (editable) { |
| 607 | show_virtual_keyboard(); |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | queue_redraw(); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | Ref<InputEventMouseMotion> m = p_event; |
| 616 | |
| 617 | if (m.is_valid()) { |
| 618 | if (editable && !text.is_empty() && clear_button_enabled) { |
| 619 | bool last_press_inside = clear_button_status.pressing_inside; |
| 620 | clear_button_status.pressing_inside = clear_button_status.press_attempt && _is_over_clear_button(m->get_position()); |
| 621 | if (last_press_inside != clear_button_status.pressing_inside) { |
| 622 | queue_redraw(); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | if (m->get_button_mask().has_flag(MouseButtonMask::LEFT)) { |
| 627 | if (selection.creating) { |
| 628 | set_caret_at_pixel_pos(m->get_position().x); |
| 629 | selection_fill_at_caret(); |
| 630 | |
| 631 | if (selection.double_click) { |
| 632 | // Expand selection to whole words. |
| 633 | |
| 634 | PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid); |
| 635 | for (int i = 0; i < words.size(); i = i + 2) { |
| 636 | if (words[i] <= selection.begin && words[i + 1] >= selection.begin) { |
| 637 | selection.begin = words[i]; |
| 638 | } |
| 639 | if (words[i] <= selection.end && words[i + 1] >= selection.end) { |
| 640 | selection.end = words[i + 1]; |
| 641 | selection.enabled = true; |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | if (caret_column < selection.start_column) { |
| 646 | set_caret_column(selection.begin); |
| 647 | } else { |
| 648 | set_caret_column(selection.end); |
| 649 | } |
| 650 | } |
| 651 | } |
| 652 | } |
| 653 | |
| 654 | if (drag_action && can_drop_data(m->get_position(), get_viewport()->gui_get_drag_data())) { |
| 655 | drag_caret_force_displayed = true; |
| 656 | set_caret_at_pixel_pos(m->get_position().x); |
| 657 | } |
| 658 | |
| 659 | return; |
| 660 | } |
| 661 | |
| 662 | Ref<InputEventKey> k = p_event; |
| 663 | |
| 664 | if (k.is_null()) { |
| 665 | return; |
| 666 | } |
| 667 | |
| 668 | if (editable && !editing && k->is_action_pressed("ui_text_submit", false)) { |
| 669 | edit(); |
| 670 | emit_signal(SNAME("editing_toggled"), true); |
| 671 | accept_event(); |
| 672 | return; |
| 673 | } |
| 674 | |
| 675 | // Open context menu. |
| 676 | if (context_menu_enabled) { |
| 677 | if (k->is_action("ui_menu", true)) { |
| 678 | _update_context_menu(); |
| 679 | Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2); |
| 680 | menu->set_position(get_screen_transform().xform(pos)); |
| 681 | menu->reset_size(); |
| 682 | menu->popup(); |
| 683 | menu->grab_focus(); |
| 684 | |
| 685 | accept_event(); |
| 686 | return; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | if (is_shortcut_keys_enabled()) { |
| 691 | if (k->is_action("ui_copy", true)) { |
| 692 | copy_text(); |
| 693 | accept_event(); |
| 694 | return; |
| 695 | } |
| 696 | |
| 697 | if (k->is_action("ui_text_select_all", true)) { |
| 698 | select(); |
| 699 | accept_event(); |
| 700 | return; |
| 701 | } |
| 702 | |
| 703 | if (k->is_action("ui_cut", true)) { |
| 704 | if (editable) { |
| 705 | cut_text(); |
| 706 | } else { |
| 707 | copy_text(); |
| 708 | } |
| 709 | accept_event(); |
| 710 | return; |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | if (!editing) { |
| 715 | return; |
| 716 | } |
| 717 | |
| 718 | // Start Unicode Alt input (hold). |
| 719 | if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) { |
| 720 | if (selection.enabled) { |
| 721 | selection_delete(); |
| 722 | } |
| 723 | alt_start = true; |
| 724 | alt_code = 0; |
| 725 | alt_mode = ALT_INPUT_UNICODE; |
| 726 | ime_text = "u"; |
| 727 | ime_selection = Vector2i(0, -1); |
| 728 | _shape(); |
| 729 | queue_redraw(); |
| 730 | accept_event(); |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | // Start Unicode input (press). |
| 735 | if (k->is_action("ui_unicode_start", true) && !alt_start && !alt_start_no_hold) { |
| 736 | if (selection.enabled) { |
| 737 | selection_delete(); |
| 738 | } |
| 739 | alt_start_no_hold = true; |
| 740 | alt_code = 0; |
| 741 | alt_mode = ALT_INPUT_UNICODE; |
| 742 | ime_text = "u"; |
| 743 | ime_selection = Vector2i(0, -1); |
| 744 | _shape(); |
| 745 | queue_redraw(); |
| 746 | accept_event(); |
| 747 | return; |
| 748 | } |
| 749 | |
| 750 | // Start OEM Alt input (hold). |
| 751 | if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) { |
| 752 | if (selection.enabled) { |
| 753 | selection_delete(); |
| 754 | } |
| 755 | alt_start = true; |
| 756 | alt_code = (uint32_t)(k->get_keycode() - Key::KP_0); |
| 757 | alt_mode = ALT_INPUT_OEM; |
| 758 | ime_text = vformat("o%s", String::num_int64(alt_code, 10)); |
| 759 | ime_selection = Vector2i(0, -1); |
| 760 | _shape(); |
| 761 | queue_redraw(); |
| 762 | accept_event(); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | // Start Windows Alt input (hold). |
| 767 | if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) { |
| 768 | if (selection.enabled) { |
| 769 | selection_delete(); |
| 770 | } |
| 771 | alt_start = true; |
| 772 | alt_mode = ALT_INPUT_WIN; |
| 773 | alt_code = 0; |
| 774 | ime_text = "w"; |
| 775 | ime_selection = Vector2i(0, -1); |
| 776 | _shape(); |
| 777 | queue_redraw(); |
| 778 | accept_event(); |
| 779 | return; |
| 780 | } |
| 781 | |
| 782 | // Update Unicode input. |
| 783 | if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) { |
| 784 | if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) { |
| 785 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 786 | alt_code = alt_code << 4; |
| 787 | } else { |
| 788 | alt_code = alt_code * 10; |
| 789 | } |
| 790 | alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0); |
| 791 | } else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) { |
| 792 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 793 | alt_code = alt_code << 4; |
| 794 | } else { |
| 795 | alt_code = alt_code * 10; |
| 796 | } |
| 797 | alt_code += (uint32_t)(k->get_keycode() - Key::KP_0); |
| 798 | } else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) { |
| 799 | alt_code = alt_code << 4; |
| 800 | alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10; |
| 801 | } else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) { |
| 802 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 803 | alt_code = alt_code << 4; |
| 804 | } else { |
| 805 | alt_code = alt_code * 10; |
| 806 | } |
| 807 | alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0); |
| 808 | } else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) { |
| 809 | alt_code = alt_code << 4; |
| 810 | alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10; |
| 811 | } else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) { |
| 812 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 813 | alt_code = alt_code << 4; |
| 814 | } else { |
| 815 | alt_code = alt_code * 10; |
| 816 | } |
| 817 | alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0); |
| 818 | } |
| 819 | if (k->get_keycode() == Key::BACKSPACE) { |
| 820 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 821 | alt_code = alt_code >> 4; |
| 822 | } else { |
| 823 | alt_code = alt_code / 10; |
| 824 | } |
| 825 | } |
| 826 | if (alt_code > 0x10ffff) { |
| 827 | alt_code = 0x10ffff; |
| 828 | } |
| 829 | if (alt_code > 0) { |
| 830 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 831 | ime_text = vformat("u%s", String::num_int64(alt_code, 16, true)); |
| 832 | } else if (alt_mode == ALT_INPUT_OEM) { |
| 833 | ime_text = vformat("o%s", String::num_int64(alt_code, 10)); |
| 834 | } else if (alt_mode == ALT_INPUT_WIN) { |
| 835 | ime_text = vformat("w%s", String::num_int64(alt_code, 10)); |
| 836 | } |
| 837 | } else { |
| 838 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 839 | ime_text = "u"; |
| 840 | } else if (alt_mode == ALT_INPUT_OEM) { |
| 841 | ime_text = "o"; |
| 842 | } else if (alt_mode == ALT_INPUT_WIN) { |
| 843 | ime_text = "w"; |
| 844 | } |
| 845 | } |
| 846 | ime_selection = Vector2i(0, -1); |
| 847 | _shape(); |
| 848 | queue_redraw(); |
| 849 | accept_event(); |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | // Submit Unicode input. |
| 854 | 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)))) { |
| 855 | alt_start = false; |
| 856 | alt_start_no_hold = false; |
| 857 | int prev_len = text.length(); |
| 858 | if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 859 | ime_text = String(); |
| 860 | ime_selection = Vector2i(); |
| 861 | if (alt_mode == ALT_INPUT_UNICODE) { |
| 862 | if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 863 | char32_t ucodestr[2] = { (char32_t)alt_code, 0 }; |
| 864 | insert_text_at_caret(ucodestr); |
| 865 | } |
| 866 | } else if (alt_mode == ALT_INPUT_OEM) { |
| 867 | if (alt_code > 0x00 && alt_code <= 0xff) { |
| 868 | char32_t ucodestr[2] = { alt_code_oem437[alt_code], 0 }; |
| 869 | insert_text_at_caret(ucodestr); |
| 870 | } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 871 | char32_t ucodestr[2] = { (char32_t)alt_code, 0 }; |
| 872 | insert_text_at_caret(ucodestr); |
| 873 | } |
| 874 | } else if (alt_mode == ALT_INPUT_WIN) { |
| 875 | if (alt_code > 0x00 && alt_code <= 0xff) { |
| 876 | char32_t ucodestr[2] = { alt_code_cp1252[alt_code], 0 }; |
| 877 | insert_text_at_caret(ucodestr); |
| 878 | } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) { |
| 879 | char32_t ucodestr[2] = { (char32_t)alt_code, 0 }; |
| 880 | insert_text_at_caret(ucodestr); |
| 881 | } |
| 882 | } |
| 883 | alt_mode = ALT_INPUT_NONE; |
| 884 | // Mirror paste behavior: defer _text_changed once per frame while emitting text_changed signal. |
| 885 | if (!text_changed_dirty) { |
| 886 | if (is_inside_tree() && text.length() != prev_len) { |
| 887 | callable_mp(this, &LineEdit::_text_changed).call_deferred(); |
| 888 | } |
| 889 | text_changed_dirty = true; |
| 890 | } |
| 891 | } else { |
| 892 | ime_text = String(); |
| 893 | ime_selection = Vector2i(); |
| 894 | _shape(); |
| 895 | } |
| 896 | queue_redraw(); |
| 897 | accept_event(); |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | // Cancel Unicode input. |
| 902 | if (alt_start_no_hold && k->is_action("ui_cancel", true)) { |
| 903 | alt_start = false; |
| 904 | alt_start_no_hold = false; |
| 905 | alt_mode = ALT_INPUT_NONE; |
| 906 | ime_text = String(); |
| 907 | ime_selection = Vector2i(); |
| 908 | _shape(); |
| 909 | queue_redraw(); |
| 910 | accept_event(); |
| 911 | return; |
| 912 | } |
| 913 | |
| 914 | if (!k->is_pressed()) { |
| 915 | return; |
| 916 | } |
| 917 | |
| 918 | // Default is ENTER and KP_ENTER. Cannot use ui_accept as default includes SPACE. |
| 919 | if (k->is_action_pressed("ui_text_submit")) { |
| 920 | emit_signal(SceneStringName(text_submitted), text); |
| 921 | |
| 922 | if (editing && !keep_editing_on_text_submit) { |
| 923 | unedit(); |
| 924 | emit_signal(SNAME("editing_toggled"), false); |
| 925 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) { |
| 926 | DisplayServer::get_singleton()->virtual_keyboard_hide(); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | accept_event(); |
| 931 | return; |
| 932 | } |
| 933 | |
| 934 | if (k->is_action("ui_cancel")) { |
| 935 | if (editing) { |
| 936 | unedit(); |
| 937 | emit_signal(SNAME("editing_toggled"), false); |
| 938 | } |
| 939 | |
| 940 | accept_event(); |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | if (is_shortcut_keys_enabled()) { |
| 945 | if (k->is_action("ui_paste", true)) { |
| 946 | paste_text(); |
| 947 | accept_event(); |
| 948 | return; |
| 949 | } |
| 950 | |
| 951 | // Undo / Redo |
| 952 | if (k->is_action("ui_undo", true)) { |
| 953 | undo(); |
| 954 | accept_event(); |
| 955 | return; |
| 956 | } |
| 957 | |
| 958 | if (k->is_action("ui_redo", true)) { |
| 959 | redo(); |
| 960 | accept_event(); |
| 961 | return; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | // BACKSPACE |
| 966 | if (k->is_action("ui_text_backspace_all_to_left", true)) { |
| 967 | _backspace(false, true); |
| 968 | accept_event(); |
| 969 | return; |
| 970 | } |
| 971 | if (k->is_action("ui_text_backspace_word", true)) { |
| 972 | _backspace(true); |
| 973 | accept_event(); |
| 974 | return; |
| 975 | } |
| 976 | if (k->is_action("ui_text_backspace", true)) { |
| 977 | _backspace(); |
| 978 | accept_event(); |
| 979 | return; |
| 980 | } |
| 981 | |
| 982 | // DELETE |
| 983 | if (k->is_action("ui_text_delete_all_to_right", true)) { |
| 984 | _delete(false, true); |
| 985 | accept_event(); |
| 986 | return; |
| 987 | } |
| 988 | if (k->is_action("ui_text_delete_word", true)) { |
| 989 | _delete(true); |
| 990 | accept_event(); |
| 991 | return; |
| 992 | } |
| 993 | if (k->is_action("ui_text_delete", true)) { |
| 994 | _delete(); |
| 995 | accept_event(); |
| 996 | return; |
| 997 | } |
| 998 | |
| 999 | // Cursor Movement |
| 1000 | |
| 1001 | k = k->duplicate(); |
| 1002 | bool shift_pressed = k->is_shift_pressed(); |
| 1003 | // Remove shift or else actions will not match. Use above variable for selection. |
| 1004 | k->set_shift_pressed(false); |
| 1005 | |
| 1006 | if (k->is_action("ui_text_caret_word_left", true)) { |
| 1007 | _move_caret_left(shift_pressed, true); |
| 1008 | accept_event(); |
| 1009 | return; |
| 1010 | } |
| 1011 | if (k->is_action("ui_text_caret_left", true)) { |
| 1012 | _move_caret_left(shift_pressed); |
| 1013 | accept_event(); |
| 1014 | return; |
| 1015 | } |
| 1016 | if (k->is_action("ui_text_caret_word_right", true)) { |
| 1017 | _move_caret_right(shift_pressed, true); |
| 1018 | accept_event(); |
| 1019 | return; |
| 1020 | } |
| 1021 | if (k->is_action("ui_text_caret_right", true)) { |
| 1022 | _move_caret_right(shift_pressed, false); |
| 1023 | accept_event(); |
| 1024 | return; |
| 1025 | } |
| 1026 | |
| 1027 | // Up = Home, Down = End |
| 1028 | if (k->is_action("ui_text_caret_up", true) || k->is_action("ui_text_caret_line_start", true) || k->is_action("ui_text_caret_page_up", true)) { |
| 1029 | _move_caret_start(shift_pressed); |
| 1030 | accept_event(); |
| 1031 | return; |
| 1032 | } |
| 1033 | if (k->is_action("ui_text_caret_down", true) || k->is_action("ui_text_caret_line_end", true) || k->is_action("ui_text_caret_page_down", true)) { |
| 1034 | _move_caret_end(shift_pressed); |
| 1035 | accept_event(); |
| 1036 | return; |
| 1037 | } |
| 1038 | |
| 1039 | // Misc |
| 1040 | if (k->is_action("ui_swap_input_direction", true)) { |
| 1041 | _swap_current_input_direction(); |
| 1042 | accept_event(); |
| 1043 | return; |
| 1044 | } |
| 1045 | |
| 1046 | _reset_caret_blink_timer(); |
| 1047 | |
| 1048 | // Allow unicode handling if: |
| 1049 | // * No Modifiers are pressed (except shift) |
| 1050 | bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed()); |
| 1051 | |
| 1052 | if (allow_unicode_handling && editable && k->get_unicode() >= 32) { |
| 1053 | // Handle Unicode if no modifiers are active. |
| 1054 | selection_delete(); |
| 1055 | char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 }; |
| 1056 | int prev_len = text.length(); |
| 1057 | insert_text_at_caret(ucodestr); |
| 1058 | if (text.length() != prev_len) { |
| 1059 | if (!text_changed_dirty) { |
| 1060 | if (is_inside_tree()) { |
| 1061 | callable_mp(this, &LineEdit::_text_changed).call_deferred(); |
| 1062 | } |
| 1063 | text_changed_dirty = true; |
| 1064 | } |
| 1065 | } |
| 1066 | accept_event(); |
| 1067 | return; |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | void LineEdit::set_horizontal_alignment(HorizontalAlignment p_alignment) { |
| 1072 | ERR_FAIL_INDEX((int)p_alignment, 4); |
| 1073 | if (alignment == p_alignment) { |
| 1074 | return; |
| 1075 | } |
| 1076 | |
| 1077 | alignment = p_alignment; |
| 1078 | _shape(); |
| 1079 | queue_redraw(); |
| 1080 | } |
| 1081 | |
| 1082 | HorizontalAlignment LineEdit::get_horizontal_alignment() const { |
| 1083 | return alignment; |
| 1084 | } |
| 1085 | |
| 1086 | Variant LineEdit::get_drag_data(const Point2 &p_point) { |
| 1087 | Variant ret = Control::get_drag_data(p_point); |
| 1088 | if (ret != Variant()) { |
| 1089 | return ret; |
| 1090 | } |
| 1091 | |
| 1092 | if (selection.drag_attempt && selection.enabled) { |
| 1093 | String t = get_selected_text(); |
| 1094 | Label *l = memnew(Label); |
| 1095 | l->set_text(t); |
| 1096 | l->set_focus_mode(FOCUS_ACCESSIBILITY); |
| 1097 | l->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate user input. |
| 1098 | set_drag_preview(l); |
| 1099 | return t; |
| 1100 | } |
| 1101 | |
| 1102 | return Variant(); |
| 1103 | } |
| 1104 | |
| 1105 | bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const { |
| 1106 | bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data. |
| 1107 | if (drop_override) { |
| 1108 | return drop_override; |
| 1109 | } |
| 1110 | |
| 1111 | return is_editable() && p_data.is_string(); |
| 1112 | } |
| 1113 | |
| 1114 | void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) { |
| 1115 | Control::drop_data(p_point, p_data); |
| 1116 | |
| 1117 | if (p_data.is_string() && is_editable()) { |
| 1118 | apply_ime(); |
| 1119 | |
| 1120 | if (p_point != Vector2(Math::INF, Math::INF)) { |
| 1121 | set_caret_at_pixel_pos(p_point.x); |
| 1122 | } |
| 1123 | int caret_column_tmp = caret_column; |
| 1124 | bool is_inside_sel = selection.enabled && caret_column >= selection.begin && caret_column <= selection.end; |
| 1125 | if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 1126 | is_inside_sel = selection.enabled && caret_column > selection.begin && caret_column < selection.end; |
| 1127 | } |
| 1128 | if (selection.drag_attempt) { |
| 1129 | selection.drag_attempt = false; |
| 1130 | if (!is_inside_sel) { |
| 1131 | if (!Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 1132 | if (caret_column_tmp > selection.end) { |
| 1133 | caret_column_tmp = caret_column_tmp - (selection.end - selection.begin); |
| 1134 | } |
| 1135 | selection_delete(); |
| 1136 | } |
| 1137 | |
| 1138 | set_caret_column(caret_column_tmp); |
| 1139 | insert_text_at_caret(p_data); |
| 1140 | } |
| 1141 | } else if (selection.enabled && caret_column >= selection.begin && caret_column <= selection.end) { |
| 1142 | caret_column_tmp = selection.begin; |
| 1143 | selection_delete(); |
| 1144 | set_caret_column(caret_column_tmp); |
| 1145 | insert_text_at_caret(p_data); |
| 1146 | grab_focus(true); |
| 1147 | } else { |
| 1148 | insert_text_at_caret(p_data); |
| 1149 | grab_focus(true); |
| 1150 | } |
| 1151 | select(caret_column_tmp, caret_column); |
| 1152 | if (!text_changed_dirty) { |
| 1153 | if (is_inside_tree()) { |
| 1154 | callable_mp(this, &LineEdit::_text_changed).call_deferred(); |
| 1155 | } |
| 1156 | text_changed_dirty = true; |
| 1157 | } |
| 1158 | queue_accessibility_update(); |
| 1159 | queue_redraw(); |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const { |
| 1164 | if ((!text.is_empty() && is_editable() && _is_over_clear_button(p_pos)) || (!is_editable() && (!is_selecting_enabled() || text.is_empty()))) { |
| 1165 | return CURSOR_ARROW; |
| 1166 | } |
| 1167 | return Control::get_cursor_shape(p_pos); |
| 1168 | } |
| 1169 | |
| 1170 | bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const { |
| 1171 | if (!clear_button_enabled || !has_point(p_pos)) { |
| 1172 | return false; |
| 1173 | } |
| 1174 | Ref<Texture2D> icon = theme_cache.clear_icon; |
| 1175 | return is_layout_rtl() ? p_pos.x < theme_cache.normal->get_margin(SIDE_LEFT) + icon->get_width() : p_pos.x > get_size().width - icon->get_width() - theme_cache.normal->get_margin(SIDE_RIGHT); |
| 1176 | } |
| 1177 | |
| 1178 | void LineEdit::_update_theme_item_cache() { |
| 1179 | Control::_update_theme_item_cache(); |
| 1180 | |
| 1181 | theme_cache.base_scale = get_theme_default_base_scale(); |
| 1182 | } |
| 1183 | |
| 1184 | void LineEdit::_accessibility_action_set_selection(const Variant &p_data) { |
| 1185 | Dictionary new_selection = p_data; |
| 1186 | int sel_start_pos = new_selection["start_char"]; |
| 1187 | int sel_end_pos = new_selection["end_char"]; |
| 1188 | select(sel_start_pos, sel_end_pos); |
| 1189 | } |
| 1190 | |
| 1191 | void LineEdit::_accessibility_action_replace_selected(const Variant &p_data) { |
| 1192 | String new_text = p_data; |
| 1193 | insert_text_at_caret(new_text); |
| 1194 | } |
| 1195 | |
| 1196 | void LineEdit::_accessibility_action_set_value(const Variant &p_data) { |
| 1197 | String new_text = p_data; |
| 1198 | set_text(new_text); |
| 1199 | } |
| 1200 | |
| 1201 | void LineEdit::_accessibility_action_menu(const Variant &p_data) { |
| 1202 | _update_context_menu(); |
| 1203 | |
| 1204 | Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2); |
| 1205 | menu->set_position(get_screen_position() + pos); |
| 1206 | menu->reset_size(); |
| 1207 | menu->popup(); |
| 1208 | menu->grab_focus(); |
| 1209 | } |
| 1210 | |
| 1211 | void LineEdit::_notification(int p_what) { |
| 1212 | switch (p_what) { |
| 1213 | #ifdef TOOLS_ENABLED |
| 1214 | case NOTIFICATION_ENTER_TREE: { |
| 1215 | if (Engine::get_singleton()->is_editor_hint() && !is_part_of_edited_scene()) { |
| 1216 | set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink")); |
| 1217 | set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval")); |
| 1218 | |
| 1219 | if (!EditorSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed))) { |
| 1220 | EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed)); |
| 1221 | } |
| 1222 | } |
| 1223 | } break; |
| 1224 | #endif |
| 1225 | case NOTIFICATION_EXIT_TREE: |
| 1226 | case NOTIFICATION_ACCESSIBILITY_INVALIDATE: { |
| 1227 | accessibility_text_root_element = RID(); |
| 1228 | } break; |
| 1229 | |
| 1230 | case NOTIFICATION_ACCESSIBILITY_UPDATE: { |
| 1231 | RID ae = get_accessibility_element(); |
| 1232 | ERR_FAIL_COND(ae.is_null()); |
| 1233 | |
| 1234 | DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_TEXT_FIELD); |
| 1235 | bool using_placeholder = text.is_empty() && ime_text.is_empty(); |
| 1236 | if (using_placeholder && !placeholder.is_empty()) { |
| 1237 | DisplayServer::get_singleton()->accessibility_update_set_placeholder(ae, atr(placeholder)); |
| 1238 | } |
| 1239 | if (!placeholder.is_empty() && get_accessibility_name().is_empty()) { |
| 1240 | DisplayServer::get_singleton()->accessibility_update_set_name(ae, atr(placeholder)); |
| 1241 | } |
| 1242 | DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_READONLY, !editable); |
| 1243 | |
| 1244 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_TEXT_SELECTION, callable_mp(this, &LineEdit::_accessibility_action_set_selection)); |
| 1245 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_REPLACE_SELECTED_TEXT, callable_mp(this, &LineEdit::_accessibility_action_replace_selected)); |
| 1246 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &LineEdit::_accessibility_action_set_value)); |
| 1247 | DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &LineEdit::_accessibility_action_menu)); |
| 1248 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 1249 | DisplayServer::get_singleton()->accessibility_update_set_language(ae, lang); |
| 1250 | |
| 1251 | bool rtl = is_layout_rtl(); |
| 1252 | Ref<StyleBox> style = theme_cache.normal; |
| 1253 | Ref<Font> font = theme_cache.font; |
| 1254 | |
| 1255 | Size2 size = get_size(); |
| 1256 | |
| 1257 | int x_ofs = 0; |
| 1258 | float text_width = TS->shaped_text_get_size(text_rid).x; |
| 1259 | float text_height = TS->shaped_text_get_size(text_rid).y; |
| 1260 | int y_area = size.height - style->get_minimum_size().height; |
| 1261 | int y_ofs = style->get_offset().y + (y_area - text_height) / 2; |
| 1262 | |
| 1263 | switch (alignment) { |
| 1264 | case HORIZONTAL_ALIGNMENT_FILL: |
| 1265 | case HORIZONTAL_ALIGNMENT_LEFT: { |
| 1266 | if (rtl) { |
| 1267 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width)))); |
| 1268 | } else { |
| 1269 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1270 | } |
| 1271 | } break; |
| 1272 | case HORIZONTAL_ALIGNMENT_CENTER: { |
| 1273 | if (!Math::is_zero_approx(scroll_offset)) { |
| 1274 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1275 | } else { |
| 1276 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1277 | int centered = int((size.width - total_margin - text_width)) / 2; |
| 1278 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); |
| 1279 | } |
| 1280 | } break; |
| 1281 | case HORIZONTAL_ALIGNMENT_RIGHT: { |
| 1282 | if (rtl) { |
| 1283 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1284 | } else { |
| 1285 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width)))); |
| 1286 | } |
| 1287 | } break; |
| 1288 | } |
| 1289 | bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled; |
| 1290 | if (right_icon.is_valid() || display_clear_icon) { |
| 1291 | Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; |
| 1292 | Point2 right_icon_size = _get_right_icon_size(r_icon); |
| 1293 | if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { |
| 1294 | if (Math::is_zero_approx(scroll_offset)) { |
| 1295 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1296 | int center = int(size.width - total_margin - text_width - right_icon_size.width) / 2; |
| 1297 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); |
| 1298 | } |
| 1299 | if (rtl) { |
| 1300 | x_ofs += right_icon_size.width; |
| 1301 | } |
| 1302 | } else { |
| 1303 | if (rtl) { |
| 1304 | x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs); |
| 1305 | } else { |
| 1306 | x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT)); |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | float text_off_x = x_ofs + scroll_offset; |
| 1312 | |
| 1313 | if (accessibility_text_root_element.is_null()) { |
| 1314 | accessibility_text_root_element = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(ae, using_placeholder ? RID() : text_rid, text_height, -1, true); |
| 1315 | } |
| 1316 | |
| 1317 | Transform2D text_xform; |
| 1318 | text_xform.set_origin(Vector2i(text_off_x, y_ofs)); |
| 1319 | DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_text_root_element, text_xform); |
| 1320 | if (selection.enabled) { |
| 1321 | DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, accessibility_text_root_element, selection.begin, accessibility_text_root_element, selection.end); |
| 1322 | } else { |
| 1323 | DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, accessibility_text_root_element, caret_column, accessibility_text_root_element, caret_column); |
| 1324 | } |
| 1325 | } break; |
| 1326 | |
| 1327 | case NOTIFICATION_RESIZED: { |
| 1328 | _fit_to_width(); |
| 1329 | scroll_offset = 0.0; |
| 1330 | set_caret_column(get_caret_column()); |
| 1331 | } break; |
| 1332 | |
| 1333 | case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: |
| 1334 | case NOTIFICATION_THEME_CHANGED: { |
| 1335 | _shape(); |
| 1336 | queue_redraw(); |
| 1337 | } break; |
| 1338 | |
| 1339 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 1340 | placeholder_translated = atr(placeholder); |
| 1341 | _shape(); |
| 1342 | queue_redraw(); |
| 1343 | } break; |
| 1344 | |
| 1345 | case NOTIFICATION_WM_WINDOW_FOCUS_IN: { |
| 1346 | window_has_focus = true; |
| 1347 | _validate_caret_can_draw(); |
| 1348 | queue_redraw(); |
| 1349 | } break; |
| 1350 | |
| 1351 | case NOTIFICATION_WM_WINDOW_FOCUS_OUT: { |
| 1352 | window_has_focus = false; |
| 1353 | _validate_caret_can_draw(); |
| 1354 | queue_redraw(); |
| 1355 | } break; |
| 1356 | |
| 1357 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 1358 | if (caret_blink_enabled && caret_can_draw) { |
| 1359 | caret_blink_timer += get_process_delta_time(); |
| 1360 | |
| 1361 | if (caret_blink_timer >= caret_blink_interval) { |
| 1362 | caret_blink_timer = 0.0; |
| 1363 | _toggle_draw_caret(); |
| 1364 | } |
| 1365 | } |
| 1366 | } break; |
| 1367 | |
| 1368 | case NOTIFICATION_DRAW: { |
| 1369 | int width, height; |
| 1370 | bool rtl = is_layout_rtl(); |
| 1371 | |
| 1372 | Size2 size = get_size(); |
| 1373 | width = size.width; |
| 1374 | height = size.height; |
| 1375 | |
| 1376 | RID ci = get_canvas_item(); |
| 1377 | |
| 1378 | Ref<StyleBox> style = theme_cache.normal; |
| 1379 | if (!is_editable()) { |
| 1380 | style = theme_cache.read_only; |
| 1381 | } |
| 1382 | Ref<Font> font = theme_cache.font; |
| 1383 | |
| 1384 | if (!flat) { |
| 1385 | style->draw(ci, Rect2(Point2(), size)); |
| 1386 | } |
| 1387 | |
| 1388 | if (has_focus(Engine::get_singleton()->is_editor_hint() || GLOBAL_GET_CACHED(int, "gui/common/show_focus_state_on_pointer_event") != 1)) { |
| 1389 | theme_cache.focus->draw(ci, Rect2(Point2(), size)); |
| 1390 | } |
| 1391 | |
| 1392 | int x_ofs = 0; |
| 1393 | bool using_placeholder = text.is_empty() && ime_text.is_empty(); |
| 1394 | float text_width = TS->shaped_text_get_size(text_rid).x; |
| 1395 | float text_height = TS->shaped_text_get_size(text_rid).y; |
| 1396 | |
| 1397 | switch (alignment) { |
| 1398 | case HORIZONTAL_ALIGNMENT_FILL: |
| 1399 | case HORIZONTAL_ALIGNMENT_LEFT: { |
| 1400 | if (rtl) { |
| 1401 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width)))); |
| 1402 | } else { |
| 1403 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1404 | } |
| 1405 | } break; |
| 1406 | case HORIZONTAL_ALIGNMENT_CENTER: { |
| 1407 | if (!Math::is_zero_approx(scroll_offset)) { |
| 1408 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1409 | } else { |
| 1410 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1411 | int centered = int((size.width - total_margin - text_width)) / 2; |
| 1412 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); |
| 1413 | } |
| 1414 | } break; |
| 1415 | case HORIZONTAL_ALIGNMENT_RIGHT: { |
| 1416 | if (rtl) { |
| 1417 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1418 | } else { |
| 1419 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width)))); |
| 1420 | } |
| 1421 | } break; |
| 1422 | } |
| 1423 | |
| 1424 | int ofs_max = width - style->get_margin(SIDE_RIGHT); |
| 1425 | |
| 1426 | int y_area = height - style->get_minimum_size().height; |
| 1427 | int y_ofs = style->get_offset().y + (y_area - text_height) / 2; |
| 1428 | |
| 1429 | Color selection_color = theme_cache.selection_color; |
| 1430 | Color font_color; |
| 1431 | if (is_editable()) { |
| 1432 | font_color = theme_cache.font_color; |
| 1433 | } else { |
| 1434 | font_color = theme_cache.font_uneditable_color; |
| 1435 | } |
| 1436 | Color font_selected_color = theme_cache.font_selected_color; |
| 1437 | Color caret_color = theme_cache.caret_color; |
| 1438 | |
| 1439 | // Draw placeholder color. |
| 1440 | if (using_placeholder) { |
| 1441 | font_color = theme_cache.font_placeholder_color; |
| 1442 | } |
| 1443 | |
| 1444 | bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled; |
| 1445 | if (right_icon.is_valid() || display_clear_icon) { |
| 1446 | Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; |
| 1447 | Point2 right_icon_size = _get_right_icon_size(r_icon); |
| 1448 | Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9); |
| 1449 | if (display_clear_icon) { |
| 1450 | if (clear_button_status.press_attempt && clear_button_status.pressing_inside) { |
| 1451 | color_icon = theme_cache.clear_button_color_pressed; |
| 1452 | } else { |
| 1453 | color_icon = theme_cache.clear_button_color; |
| 1454 | } |
| 1455 | } |
| 1456 | |
| 1457 | Point2 icon_pos = Point2(width - right_icon_size.width - style->get_margin(SIDE_RIGHT), height / 2 - right_icon_size.height / 2); |
| 1458 | if (rtl) { |
| 1459 | icon_pos.x = style->get_margin(SIDE_LEFT); |
| 1460 | } |
| 1461 | Rect2 icon_region = Rect2(icon_pos, right_icon_size); |
| 1462 | draw_texture_rect(r_icon, icon_region, false, color_icon); |
| 1463 | |
| 1464 | if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { |
| 1465 | if (Math::is_zero_approx(scroll_offset)) { |
| 1466 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1467 | int center = int(size.width - total_margin - text_width - right_icon_size.width) / 2; |
| 1468 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); |
| 1469 | } |
| 1470 | if (rtl) { |
| 1471 | x_ofs += right_icon_size.width; |
| 1472 | } |
| 1473 | } else { |
| 1474 | if (rtl) { |
| 1475 | x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs); |
| 1476 | } else { |
| 1477 | x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT)); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | if (!rtl) { |
| 1482 | ofs_max -= right_icon_size.width; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | // Draw selections rects. |
| 1487 | Vector2 ofs = Point2(x_ofs + scroll_offset, y_ofs); |
| 1488 | if (selection.enabled) { |
| 1489 | Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, selection.begin, selection.end); |
| 1490 | for (int i = 0; i < sel.size(); i++) { |
| 1491 | Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height); |
| 1492 | if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) { |
| 1493 | continue; |
| 1494 | } |
| 1495 | if (rect.position.x < x_ofs) { |
| 1496 | rect.size.x -= (x_ofs - rect.position.x); |
| 1497 | rect.position.x = x_ofs; |
| 1498 | } |
| 1499 | if (rect.position.x + rect.size.x > ofs_max) { |
| 1500 | rect.size.x = ofs_max - rect.position.x; |
| 1501 | } |
| 1502 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_color); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | const Glyph *glyphs = TS->shaped_text_get_glyphs(text_rid); |
| 1507 | int gl_size = TS->shaped_text_get_glyph_count(text_rid); |
| 1508 | |
| 1509 | // Draw text. |
| 1510 | ofs.y += TS->shaped_text_get_ascent(text_rid); |
| 1511 | Color font_outline_color = theme_cache.font_outline_color; |
| 1512 | int outline_size = theme_cache.font_outline_size; |
| 1513 | if (outline_size > 0 && font_outline_color.a > 0) { |
| 1514 | Vector2 oofs = ofs; |
| 1515 | for (int i = 0; i < gl_size; i++) { |
| 1516 | for (int j = 0; j < glyphs[i].repeat; j++) { |
| 1517 | if (std::ceil(oofs.x) >= x_ofs && (oofs.x + glyphs[i].advance) <= ofs_max) { |
| 1518 | if (glyphs[i].font_rid != RID()) { |
| 1519 | TS->font_draw_glyph_outline(glyphs[i].font_rid, ci, glyphs[i].font_size, outline_size, oofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, font_outline_color); |
| 1520 | } |
| 1521 | } |
| 1522 | oofs.x += glyphs[i].advance; |
| 1523 | } |
| 1524 | if (oofs.x >= ofs_max) { |
| 1525 | break; |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 | for (int i = 0; i < gl_size; i++) { |
| 1530 | bool selected = selection.enabled && glyphs[i].start >= selection.begin && glyphs[i].end <= selection.end; |
| 1531 | for (int j = 0; j < glyphs[i].repeat; j++) { |
| 1532 | if (std::ceil(ofs.x) >= x_ofs && (ofs.x + glyphs[i].advance) <= ofs_max) { |
| 1533 | if (glyphs[i].font_rid != RID()) { |
| 1534 | TS->font_draw_glyph(glyphs[i].font_rid, ci, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, selected ? font_selected_color : font_color); |
| 1535 | } else if (((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) && ((glyphs[i].flags & TextServer::GRAPHEME_IS_EMBEDDED_OBJECT) != TextServer::GRAPHEME_IS_EMBEDDED_OBJECT)) { |
| 1536 | TS->draw_hex_code_box(ci, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, selected ? font_selected_color : font_color); |
| 1537 | } |
| 1538 | } |
| 1539 | ofs.x += glyphs[i].advance; |
| 1540 | } |
| 1541 | if (ofs.x >= ofs_max) { |
| 1542 | break; |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | // Draw carets. |
| 1547 | ofs.x = x_ofs + scroll_offset; |
| 1548 | if ((caret_can_draw && draw_caret) || drag_caret_force_displayed) { |
| 1549 | // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1). |
| 1550 | const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale); |
| 1551 | |
| 1552 | if (ime_text.is_empty() || ime_selection.y == 0) { |
| 1553 | // Normal caret. |
| 1554 | CaretInfo caret = TS->shaped_text_get_carets(text_rid, ime_text.is_empty() ? caret_column : caret_column + ime_selection.x); |
| 1555 | if (using_placeholder || (caret.l_caret == Rect2() && caret.t_caret == Rect2())) { |
| 1556 | // No carets, add one at the start. |
| 1557 | int h = theme_cache.font->get_height(theme_cache.font_size); |
| 1558 | int y = style->get_offset().y + (y_area - h) / 2; |
| 1559 | caret.l_dir = (rtl) ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR; |
| 1560 | switch (alignment) { |
| 1561 | case HORIZONTAL_ALIGNMENT_FILL: |
| 1562 | case HORIZONTAL_ALIGNMENT_LEFT: { |
| 1563 | if (rtl) { |
| 1564 | caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h)); |
| 1565 | } else { |
| 1566 | caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h)); |
| 1567 | } |
| 1568 | } break; |
| 1569 | case HORIZONTAL_ALIGNMENT_CENTER: { |
| 1570 | int icon_width = 0; |
| 1571 | if (right_icon.is_valid()) { |
| 1572 | icon_width = right_icon->get_width(); |
| 1573 | } |
| 1574 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1575 | int center = int(size.width - total_margin - icon_width) / 2; |
| 1576 | |
| 1577 | caret.l_caret = Rect2(Vector2(style->get_margin(SIDE_LEFT) + MAX(0, center), y), Size2(caret_width, h)); |
| 1578 | } break; |
| 1579 | case HORIZONTAL_ALIGNMENT_RIGHT: { |
| 1580 | if (rtl) { |
| 1581 | caret.l_caret = Rect2(Vector2(x_ofs, y), Size2(caret_width, h)); |
| 1582 | } else { |
| 1583 | caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h)); |
| 1584 | } |
| 1585 | } break; |
| 1586 | } |
| 1587 | |
| 1588 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color); |
| 1589 | } else { |
| 1590 | if (caret.l_caret != Rect2() && caret.l_dir == TextServer::DIRECTION_AUTO) { |
| 1591 | // Draw extra marker on top of mid caret. |
| 1592 | Rect2 trect = Rect2(caret.l_caret.position.x - 2.5 * caret_width, caret.l_caret.position.y, 6 * caret_width, caret_width); |
| 1593 | trect.position += ofs; |
| 1594 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color); |
| 1595 | } else if (caret.l_caret != Rect2() && caret.t_caret != Rect2() && caret.l_dir != caret.t_dir) { |
| 1596 | // Draw extra direction marker on top of split caret. |
| 1597 | float d = (caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3; |
| 1598 | Rect2 trect = Rect2(caret.l_caret.position.x + d * caret_width, caret.l_caret.position.y + caret.l_caret.size.y - caret_width, 3 * caret_width, caret_width); |
| 1599 | trect.position += ofs; |
| 1600 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color); |
| 1601 | |
| 1602 | d = (caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3; |
| 1603 | trect = Rect2(caret.t_caret.position.x + d * caret_width, caret.t_caret.position.y, 3 * caret_width, caret_width); |
| 1604 | trect.position += ofs; |
| 1605 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color); |
| 1606 | } |
| 1607 | |
| 1608 | caret.l_caret.position += ofs; |
| 1609 | caret.l_caret.size.x = caret_width; |
| 1610 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color); |
| 1611 | |
| 1612 | caret.t_caret.position += ofs; |
| 1613 | caret.t_caret.size.x = caret_width; |
| 1614 | |
| 1615 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.t_caret, caret_color); |
| 1616 | } |
| 1617 | } |
| 1618 | if (!ime_text.is_empty()) { |
| 1619 | { |
| 1620 | // IME intermediate text range. |
| 1621 | Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column, caret_column + ime_text.length()); |
| 1622 | for (int i = 0; i < sel.size(); i++) { |
| 1623 | Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height); |
| 1624 | if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) { |
| 1625 | continue; |
| 1626 | } |
| 1627 | if (rect.position.x < x_ofs) { |
| 1628 | rect.size.x -= (x_ofs - rect.position.x); |
| 1629 | rect.position.x = x_ofs; |
| 1630 | } else if (rect.position.x + rect.size.x > ofs_max) { |
| 1631 | rect.size.x = ofs_max - rect.position.x; |
| 1632 | } |
| 1633 | rect.size.y = caret_width; |
| 1634 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color); |
| 1635 | } |
| 1636 | } |
| 1637 | { |
| 1638 | // IME caret. |
| 1639 | if (ime_selection.y > 0) { |
| 1640 | Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column + ime_selection.x, caret_column + ime_selection.x + ime_selection.y); |
| 1641 | for (int i = 0; i < sel.size(); i++) { |
| 1642 | Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height); |
| 1643 | if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) { |
| 1644 | continue; |
| 1645 | } |
| 1646 | if (rect.position.x < x_ofs) { |
| 1647 | rect.size.x -= (x_ofs - rect.position.x); |
| 1648 | rect.position.x = x_ofs; |
| 1649 | } else if (rect.position.x + rect.size.x > ofs_max) { |
| 1650 | rect.size.x = ofs_max - rect.position.x; |
| 1651 | } |
| 1652 | rect.size.y = caret_width * 3; |
| 1653 | RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color); |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | if (editing) { |
| 1661 | _update_ime_window_position(); |
| 1662 | } |
| 1663 | } break; |
| 1664 | |
| 1665 | case NOTIFICATION_FOCUS_ENTER: { |
| 1666 | // Only allow editing if the LineEdit is not focused with arrow keys. |
| 1667 | if (!(Input::get_singleton()->is_action_pressed("ui_up") || Input::get_singleton()->is_action_pressed("ui_down") || Input::get_singleton()->is_action_pressed("ui_left") || Input::get_singleton()->is_action_pressed("ui_right"))) { |
| 1668 | _edit(virtual_keyboard_show_on_focus); |
| 1669 | emit_signal(SNAME("editing_toggled"), true); |
| 1670 | } |
| 1671 | } break; |
| 1672 | |
| 1673 | case NOTIFICATION_FOCUS_EXIT: { |
| 1674 | if (editing) { |
| 1675 | unedit(); |
| 1676 | emit_signal(SNAME("editing_toggled"), false); |
| 1677 | } |
| 1678 | } break; |
| 1679 | |
| 1680 | case MainLoop::NOTIFICATION_OS_IME_UPDATE: { |
| 1681 | if (editing) { |
| 1682 | const String &new_ime_text = DisplayServer::get_singleton()->ime_get_text(); |
| 1683 | const Vector2i &new_ime_selection = DisplayServer::get_singleton()->ime_get_selection(); |
| 1684 | if (ime_text == new_ime_text && ime_selection == new_ime_selection) { |
| 1685 | break; |
| 1686 | } |
| 1687 | if (!window_has_focus && !new_ime_text.is_empty()) { |
| 1688 | break; |
| 1689 | } |
| 1690 | |
| 1691 | ime_text = new_ime_text; |
| 1692 | ime_selection = new_ime_selection; |
| 1693 | |
| 1694 | if (!ime_text.is_empty()) { |
| 1695 | selection_delete(); |
| 1696 | } |
| 1697 | |
| 1698 | _shape(); |
| 1699 | set_caret_column(caret_column); // Update scroll_offset. |
| 1700 | } |
| 1701 | } break; |
| 1702 | |
| 1703 | case NOTIFICATION_DRAG_BEGIN: { |
| 1704 | drag_action = true; |
| 1705 | } break; |
| 1706 | |
| 1707 | case NOTIFICATION_DRAG_END: { |
| 1708 | if (is_drag_successful()) { |
| 1709 | if (selection.drag_attempt) { |
| 1710 | selection.drag_attempt = false; |
| 1711 | if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) { |
| 1712 | selection_delete(); |
| 1713 | } else if (deselect_on_focus_loss_enabled) { |
| 1714 | deselect(); |
| 1715 | } |
| 1716 | } |
| 1717 | } else { |
| 1718 | selection.drag_attempt = false; |
| 1719 | } |
| 1720 | drag_action = false; |
| 1721 | drag_caret_force_displayed = false; |
| 1722 | queue_redraw(); |
| 1723 | } break; |
| 1724 | |
| 1725 | case NOTIFICATION_MOUSE_EXIT: { |
| 1726 | if (drag_caret_force_displayed) { |
| 1727 | drag_caret_force_displayed = false; |
| 1728 | queue_redraw(); |
| 1729 | } |
| 1730 | } break; |
| 1731 | } |
| 1732 | } |
| 1733 | |
| 1734 | void LineEdit::copy_text() { |
| 1735 | if (selection.enabled && !pass) { |
| 1736 | DisplayServer::get_singleton()->clipboard_set(get_selected_text()); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | void LineEdit::cut_text() { |
| 1741 | if (editable && selection.enabled && !pass) { |
| 1742 | DisplayServer::get_singleton()->clipboard_set(get_selected_text()); |
| 1743 | selection_delete(); |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | void LineEdit::paste_text() { |
| 1748 | if (!editable) { |
| 1749 | return; |
| 1750 | } |
| 1751 | |
| 1752 | // Strip escape characters like \n and \t as they can't be displayed on LineEdit. |
| 1753 | String paste_buffer = DisplayServer::get_singleton()->clipboard_get().strip_escapes(); |
| 1754 | |
| 1755 | if (!paste_buffer.is_empty()) { |
| 1756 | int prev_len = text.length(); |
| 1757 | if (selection.enabled) { |
| 1758 | selection_delete(); |
| 1759 | } |
| 1760 | insert_text_at_caret(paste_buffer); |
| 1761 | |
| 1762 | if (!text_changed_dirty) { |
| 1763 | if (is_inside_tree() && text.length() != prev_len) { |
| 1764 | callable_mp(this, &LineEdit::_text_changed).call_deferred(); |
| 1765 | } |
| 1766 | text_changed_dirty = true; |
| 1767 | } |
| 1768 | } |
| 1769 | } |
| 1770 | |
| 1771 | bool LineEdit::has_undo() const { |
| 1772 | if (undo_stack_pos == nullptr) { |
| 1773 | return undo_stack.size() > 1; |
| 1774 | } |
| 1775 | return undo_stack_pos != undo_stack.front(); |
| 1776 | } |
| 1777 | |
| 1778 | bool LineEdit::has_redo() const { |
| 1779 | return undo_stack_pos != nullptr && undo_stack_pos != undo_stack.back(); |
| 1780 | } |
| 1781 | |
| 1782 | void LineEdit::undo() { |
| 1783 | if (!editable) { |
| 1784 | return; |
| 1785 | } |
| 1786 | |
| 1787 | if (!has_undo()) { |
| 1788 | return; |
| 1789 | } |
| 1790 | |
| 1791 | if (undo_stack_pos == nullptr) { |
| 1792 | undo_stack_pos = undo_stack.back(); |
| 1793 | } |
| 1794 | |
| 1795 | deselect(); |
| 1796 | |
| 1797 | undo_stack_pos = undo_stack_pos->prev(); |
| 1798 | TextOperation op = undo_stack_pos->get(); |
| 1799 | text = op.text; |
| 1800 | scroll_offset = op.scroll_offset; |
| 1801 | |
| 1802 | _shape(); |
| 1803 | set_caret_column(op.caret_column); |
| 1804 | |
| 1805 | _emit_text_change(); |
| 1806 | } |
| 1807 | |
| 1808 | void LineEdit::redo() { |
| 1809 | if (!editable) { |
| 1810 | return; |
| 1811 | } |
| 1812 | |
| 1813 | if (!has_redo()) { |
| 1814 | return; |
| 1815 | } |
| 1816 | |
| 1817 | deselect(); |
| 1818 | |
| 1819 | undo_stack_pos = undo_stack_pos->next(); |
| 1820 | TextOperation op = undo_stack_pos->get(); |
| 1821 | text = op.text; |
| 1822 | scroll_offset = op.scroll_offset; |
| 1823 | |
| 1824 | _shape(); |
| 1825 | set_caret_column(op.caret_column); |
| 1826 | |
| 1827 | _emit_text_change(); |
| 1828 | } |
| 1829 | |
| 1830 | void LineEdit::shift_selection_check_pre(bool p_shift) { |
| 1831 | if (!selection.enabled && p_shift) { |
| 1832 | selection.start_column = caret_column; |
| 1833 | } |
| 1834 | if (!p_shift) { |
| 1835 | deselect(); |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | void LineEdit::shift_selection_check_post(bool p_shift) { |
| 1840 | if (p_shift) { |
| 1841 | selection_fill_at_caret(); |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | void LineEdit::set_caret_at_pixel_pos(int p_x) { |
| 1846 | Ref<StyleBox> style = theme_cache.normal; |
| 1847 | bool rtl = is_layout_rtl(); |
| 1848 | |
| 1849 | int x_ofs = 0; |
| 1850 | float text_width = TS->shaped_text_get_size(text_rid).x; |
| 1851 | switch (alignment) { |
| 1852 | case HORIZONTAL_ALIGNMENT_FILL: |
| 1853 | case HORIZONTAL_ALIGNMENT_LEFT: { |
| 1854 | if (rtl) { |
| 1855 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width))); |
| 1856 | } else { |
| 1857 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1858 | } |
| 1859 | } break; |
| 1860 | case HORIZONTAL_ALIGNMENT_CENTER: { |
| 1861 | if (!Math::is_zero_approx(scroll_offset)) { |
| 1862 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1863 | } else { |
| 1864 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1865 | int centered = int((get_size().width - total_margin - text_width)) / 2; |
| 1866 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); |
| 1867 | } |
| 1868 | } break; |
| 1869 | case HORIZONTAL_ALIGNMENT_RIGHT: { |
| 1870 | if (rtl) { |
| 1871 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1872 | } else { |
| 1873 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width))); |
| 1874 | } |
| 1875 | } break; |
| 1876 | } |
| 1877 | |
| 1878 | bool using_placeholder = text.is_empty() && ime_text.is_empty(); |
| 1879 | bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled; |
| 1880 | if (right_icon.is_valid() || display_clear_icon) { |
| 1881 | Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; |
| 1882 | Point2 right_icon_size = _get_right_icon_size(r_icon); |
| 1883 | if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { |
| 1884 | if (Math::is_zero_approx(scroll_offset)) { |
| 1885 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1886 | int center = int(get_size().width - total_margin - text_width - right_icon_size.width) / 2; |
| 1887 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); |
| 1888 | } |
| 1889 | if (rtl) { |
| 1890 | x_ofs += right_icon_size.width; |
| 1891 | } |
| 1892 | } else { |
| 1893 | if (rtl) { |
| 1894 | x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs); |
| 1895 | } else { |
| 1896 | x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT)); |
| 1897 | } |
| 1898 | } |
| 1899 | } |
| 1900 | |
| 1901 | int ofs = std::ceil(TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset)); |
| 1902 | if (ofs == -1) { |
| 1903 | return; |
| 1904 | } |
| 1905 | if (!caret_mid_grapheme_enabled) { |
| 1906 | ofs = TS->shaped_text_closest_character_pos(text_rid, ofs); |
| 1907 | } |
| 1908 | set_caret_column(ofs); |
| 1909 | } |
| 1910 | |
| 1911 | Vector2 LineEdit::get_caret_pixel_pos() { |
| 1912 | Ref<StyleBox> style = theme_cache.normal; |
| 1913 | bool rtl = is_layout_rtl(); |
| 1914 | |
| 1915 | int x_ofs = 0; |
| 1916 | float text_width = TS->shaped_text_get_size(text_rid).x; |
| 1917 | switch (alignment) { |
| 1918 | case HORIZONTAL_ALIGNMENT_FILL: |
| 1919 | case HORIZONTAL_ALIGNMENT_LEFT: { |
| 1920 | if (rtl) { |
| 1921 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width))); |
| 1922 | } else { |
| 1923 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1924 | } |
| 1925 | } break; |
| 1926 | case HORIZONTAL_ALIGNMENT_CENTER: { |
| 1927 | if (!Math::is_zero_approx(scroll_offset)) { |
| 1928 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1929 | } else { |
| 1930 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1931 | int centered = int((get_size().width - total_margin - text_width)) / 2; |
| 1932 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); |
| 1933 | } |
| 1934 | } break; |
| 1935 | case HORIZONTAL_ALIGNMENT_RIGHT: { |
| 1936 | if (rtl) { |
| 1937 | x_ofs = style->get_margin(SIDE_LEFT); |
| 1938 | } else { |
| 1939 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width))); |
| 1940 | } |
| 1941 | } break; |
| 1942 | } |
| 1943 | |
| 1944 | bool using_placeholder = text.is_empty() && ime_text.is_empty(); |
| 1945 | bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled; |
| 1946 | if (right_icon.is_valid() || display_clear_icon) { |
| 1947 | Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; |
| 1948 | Point2 right_icon_size = _get_right_icon_size(r_icon); |
| 1949 | if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { |
| 1950 | if (Math::is_zero_approx(scroll_offset)) { |
| 1951 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 1952 | int center = int(get_size().width - total_margin - text_width - right_icon_size.width) / 2; |
| 1953 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); |
| 1954 | } |
| 1955 | if (rtl) { |
| 1956 | x_ofs += right_icon_size.width; |
| 1957 | } |
| 1958 | } else { |
| 1959 | if (rtl) { |
| 1960 | x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs); |
| 1961 | } else { |
| 1962 | x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT)); |
| 1963 | } |
| 1964 | } |
| 1965 | } |
| 1966 | |
| 1967 | Vector2 ret; |
| 1968 | CaretInfo caret; |
| 1969 | // Get position of the start of caret. |
| 1970 | if (!ime_text.is_empty() && ime_selection.x != 0) { |
| 1971 | caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x); |
| 1972 | } else { |
| 1973 | caret = TS->shaped_text_get_carets(text_rid, caret_column); |
| 1974 | } |
| 1975 | |
| 1976 | if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) { |
| 1977 | ret.x = x_ofs + caret.l_caret.position.x + scroll_offset; |
| 1978 | } else { |
| 1979 | ret.x = x_ofs + caret.t_caret.position.x + scroll_offset; |
| 1980 | } |
| 1981 | |
| 1982 | // Get position of the end of caret. |
| 1983 | if (!ime_text.is_empty()) { |
| 1984 | if (ime_selection.y != 0) { |
| 1985 | caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x + ime_selection.y); |
| 1986 | } else { |
| 1987 | caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_text.size()); |
| 1988 | } |
| 1989 | if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) { |
| 1990 | ret.y = x_ofs + caret.l_caret.position.x + scroll_offset; |
| 1991 | } else { |
| 1992 | ret.y = x_ofs + caret.t_caret.position.x + scroll_offset; |
| 1993 | } |
| 1994 | } else { |
| 1995 | ret.y = ret.x; |
| 1996 | } |
| 1997 | |
| 1998 | return ret; |
| 1999 | } |
| 2000 | |
| 2001 | void LineEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) { |
| 2002 | caret_mid_grapheme_enabled = p_enabled; |
| 2003 | } |
| 2004 | |
| 2005 | bool LineEdit::is_caret_mid_grapheme_enabled() const { |
| 2006 | return caret_mid_grapheme_enabled; |
| 2007 | } |
| 2008 | |
| 2009 | bool LineEdit::is_caret_blink_enabled() const { |
| 2010 | return caret_blink_enabled; |
| 2011 | } |
| 2012 | |
| 2013 | void LineEdit::set_caret_blink_enabled(const bool p_enabled) { |
| 2014 | if (caret_blink_enabled == p_enabled) { |
| 2015 | return; |
| 2016 | } |
| 2017 | |
| 2018 | caret_blink_enabled = p_enabled; |
| 2019 | set_process_internal(p_enabled); |
| 2020 | |
| 2021 | draw_caret = !caret_blink_enabled; |
| 2022 | if (caret_blink_enabled) { |
| 2023 | caret_blink_timer = 0.0; |
| 2024 | } |
| 2025 | queue_redraw(); |
| 2026 | |
| 2027 | notify_property_list_changed(); |
| 2028 | } |
| 2029 | |
| 2030 | bool LineEdit::is_caret_force_displayed() const { |
| 2031 | return caret_force_displayed; |
| 2032 | } |
| 2033 | |
| 2034 | void LineEdit::set_caret_force_displayed(const bool p_enabled) { |
| 2035 | if (caret_force_displayed == p_enabled) { |
| 2036 | return; |
| 2037 | } |
| 2038 | |
| 2039 | caret_force_displayed = p_enabled; |
| 2040 | _validate_caret_can_draw(); |
| 2041 | |
| 2042 | queue_redraw(); |
| 2043 | } |
| 2044 | |
| 2045 | float LineEdit::get_caret_blink_interval() const { |
| 2046 | return caret_blink_interval; |
| 2047 | } |
| 2048 | |
| 2049 | void LineEdit::set_caret_blink_interval(const float p_interval) { |
| 2050 | ERR_FAIL_COND(p_interval <= 0); |
| 2051 | caret_blink_interval = p_interval; |
| 2052 | } |
| 2053 | |
| 2054 | void LineEdit::_reset_caret_blink_timer() { |
| 2055 | if (caret_blink_enabled) { |
| 2056 | draw_caret = true; |
| 2057 | if (caret_can_draw) { |
| 2058 | caret_blink_timer = 0.0; |
| 2059 | queue_redraw(); |
| 2060 | } |
| 2061 | } |
| 2062 | } |
| 2063 | |
| 2064 | void LineEdit::_toggle_draw_caret() { |
| 2065 | draw_caret = !draw_caret; |
| 2066 | if (is_visible_in_tree() && caret_can_draw) { |
| 2067 | queue_redraw(); |
| 2068 | } |
| 2069 | } |
| 2070 | |
| 2071 | void LineEdit::_validate_caret_can_draw() { |
| 2072 | if (caret_blink_enabled) { |
| 2073 | draw_caret = true; |
| 2074 | caret_blink_timer = 0.0; |
| 2075 | } |
| 2076 | caret_can_draw = (caret_force_displayed && !is_part_of_edited_scene()) || (editing && (window_has_focus || (menu && menu->has_focus())) && has_focus()); |
| 2077 | } |
| 2078 | |
| 2079 | void LineEdit::delete_char() { |
| 2080 | if (text.is_empty() || caret_column == 0) { |
| 2081 | return; |
| 2082 | } |
| 2083 | int delete_char_offset = 1; |
| 2084 | if (!caret_mid_grapheme_enabled && backspace_deletes_composite_character_enabled) { |
| 2085 | delete_char_offset = caret_column - get_previous_composite_character_column(caret_column); |
| 2086 | } |
| 2087 | text = text.left(caret_column - delete_char_offset) + text.substr(caret_column); |
| 2088 | _shape(); |
| 2089 | |
| 2090 | set_caret_column(get_caret_column() - delete_char_offset); |
| 2091 | |
| 2092 | _text_changed(); |
| 2093 | } |
| 2094 | |
| 2095 | void LineEdit::delete_text(int p_from_column, int p_to_column) { |
| 2096 | ERR_FAIL_COND_MSG(p_from_column < 0 || p_from_column > p_to_column || p_to_column > text.length(), |
| 2097 | vformat("Positional parameters (from: %d, to: %d) are inverted or outside the text length (%d).", p_from_column, p_to_column, text.length())); |
| 2098 | |
| 2099 | text = text.left(p_from_column) + text.substr(p_to_column); |
| 2100 | _shape(); |
| 2101 | |
| 2102 | set_caret_column(caret_column - CLAMP(caret_column - p_from_column, 0, p_to_column - p_from_column)); |
| 2103 | |
| 2104 | if (!text_changed_dirty) { |
| 2105 | if (is_inside_tree()) { |
| 2106 | callable_mp(this, &LineEdit::_text_changed).call_deferred(); |
| 2107 | } |
| 2108 | text_changed_dirty = true; |
| 2109 | } |
| 2110 | } |
| 2111 | |
| 2112 | void LineEdit::_set_text(String p_text, bool p_emit_signal) { |
| 2113 | clear_internal(); |
| 2114 | |
| 2115 | String previous_text = get_text(); |
| 2116 | insert_text_at_caret(p_text); |
| 2117 | |
| 2118 | if (get_text() != previous_text) { |
| 2119 | _create_undo_state(); |
| 2120 | if (p_emit_signal) { |
| 2121 | _text_changed(); |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | queue_redraw(); |
| 2126 | caret_column = 0; |
| 2127 | scroll_offset = 0.0; |
| 2128 | } |
| 2129 | |
| 2130 | void LineEdit::set_text(String p_text) { |
| 2131 | _set_text(p_text); |
| 2132 | } |
| 2133 | |
| 2134 | void LineEdit::set_text_with_selection(const String &p_text) { |
| 2135 | Selection selection_copy = selection; |
| 2136 | |
| 2137 | clear_internal(); |
| 2138 | |
| 2139 | String previous_text = get_text(); |
| 2140 | insert_text_at_caret(p_text); |
| 2141 | |
| 2142 | if (get_text() != previous_text) { |
| 2143 | _create_undo_state(); |
| 2144 | } |
| 2145 | |
| 2146 | int tlen = text.length(); |
| 2147 | selection = selection_copy; |
| 2148 | selection.begin = MIN(selection.begin, tlen); |
| 2149 | selection.end = MIN(selection.end, tlen); |
| 2150 | selection.start_column = MIN(selection.start_column, tlen); |
| 2151 | |
| 2152 | queue_redraw(); |
| 2153 | } |
| 2154 | |
| 2155 | void LineEdit::set_text_direction(Control::TextDirection p_text_direction) { |
| 2156 | ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3); |
| 2157 | if (text_direction != p_text_direction) { |
| 2158 | text_direction = p_text_direction; |
| 2159 | if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) { |
| 2160 | input_direction = text_direction; |
| 2161 | } |
| 2162 | _shape(); |
| 2163 | |
| 2164 | if (menu_dir) { |
| 2165 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED); |
| 2166 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO); |
| 2167 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR); |
| 2168 | menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL); |
| 2169 | } |
| 2170 | queue_redraw(); |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | Control::TextDirection LineEdit::get_text_direction() const { |
| 2175 | return text_direction; |
| 2176 | } |
| 2177 | |
| 2178 | void LineEdit::set_language(const String &p_language) { |
| 2179 | if (language != p_language) { |
| 2180 | language = p_language; |
| 2181 | _shape(); |
| 2182 | queue_redraw(); |
| 2183 | } |
| 2184 | } |
| 2185 | |
| 2186 | String LineEdit::get_language() const { |
| 2187 | return language; |
| 2188 | } |
| 2189 | |
| 2190 | void LineEdit::set_draw_control_chars(bool p_draw_control_chars) { |
| 2191 | if (draw_control_chars != p_draw_control_chars) { |
| 2192 | draw_control_chars = p_draw_control_chars; |
| 2193 | if (menu && menu->get_item_index(MENU_DISPLAY_UCC) >= 0) { |
| 2194 | menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars); |
| 2195 | } |
| 2196 | _shape(); |
| 2197 | queue_redraw(); |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | bool LineEdit::get_draw_control_chars() const { |
| 2202 | return draw_control_chars; |
| 2203 | } |
| 2204 | |
| 2205 | void LineEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) { |
| 2206 | if (st_parser != p_parser) { |
| 2207 | st_parser = p_parser; |
| 2208 | _shape(); |
| 2209 | queue_redraw(); |
| 2210 | } |
| 2211 | } |
| 2212 | |
| 2213 | TextServer::StructuredTextParser LineEdit::get_structured_text_bidi_override() const { |
| 2214 | return st_parser; |
| 2215 | } |
| 2216 | |
| 2217 | void LineEdit::set_structured_text_bidi_override_options(const Array &p_args) { |
| 2218 | st_args = Array(p_args); |
| 2219 | _shape(); |
| 2220 | queue_redraw(); |
| 2221 | } |
| 2222 | |
| 2223 | Array LineEdit::get_structured_text_bidi_override_options() const { |
| 2224 | return Array(st_args); |
| 2225 | } |
| 2226 | |
| 2227 | void LineEdit::clear() { |
| 2228 | bool was_empty = text.is_empty(); |
| 2229 | clear_internal(); |
| 2230 | _clear_redo(); |
| 2231 | if (!was_empty) { |
| 2232 | _emit_text_change(); |
| 2233 | } |
| 2234 | |
| 2235 | // This should reset virtual keyboard state if needed. |
| 2236 | if (editing) { |
| 2237 | show_virtual_keyboard(); |
| 2238 | } |
| 2239 | } |
| 2240 | |
| 2241 | void LineEdit::show_virtual_keyboard() { |
| 2242 | _update_ime_window_position(); |
| 2243 | |
| 2244 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) { |
| 2245 | if (selection.enabled) { |
| 2246 | DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, selection.begin, selection.end); |
| 2247 | } else { |
| 2248 | DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, caret_column); |
| 2249 | } |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | String LineEdit::get_text() const { |
| 2254 | return text; |
| 2255 | } |
| 2256 | |
| 2257 | void LineEdit::set_placeholder(String p_text) { |
| 2258 | if (placeholder == p_text) { |
| 2259 | return; |
| 2260 | } |
| 2261 | |
| 2262 | placeholder = p_text; |
| 2263 | placeholder_translated = atr(placeholder); |
| 2264 | _shape(); |
| 2265 | queue_redraw(); |
| 2266 | } |
| 2267 | |
| 2268 | String LineEdit::get_placeholder() const { |
| 2269 | return placeholder; |
| 2270 | } |
| 2271 | |
| 2272 | void LineEdit::set_caret_column(int p_column) { |
| 2273 | if (p_column > (int)text.length()) { |
| 2274 | p_column = text.length(); |
| 2275 | } |
| 2276 | |
| 2277 | if (p_column < 0) { |
| 2278 | p_column = 0; |
| 2279 | } |
| 2280 | |
| 2281 | caret_column = p_column; |
| 2282 | |
| 2283 | queue_accessibility_update(); |
| 2284 | |
| 2285 | // Fit to window. |
| 2286 | |
| 2287 | if (!is_inside_tree()) { |
| 2288 | scroll_offset = 0.0; |
| 2289 | return; |
| 2290 | } |
| 2291 | |
| 2292 | Ref<StyleBox> style = theme_cache.normal; |
| 2293 | bool rtl = is_layout_rtl(); |
| 2294 | |
| 2295 | int x_ofs = 0; |
| 2296 | float text_width = TS->shaped_text_get_size(text_rid).x; |
| 2297 | switch (alignment) { |
| 2298 | case HORIZONTAL_ALIGNMENT_FILL: |
| 2299 | case HORIZONTAL_ALIGNMENT_LEFT: { |
| 2300 | if (rtl) { |
| 2301 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width))); |
| 2302 | } else { |
| 2303 | x_ofs = style->get_margin(SIDE_LEFT); |
| 2304 | } |
| 2305 | } break; |
| 2306 | case HORIZONTAL_ALIGNMENT_CENTER: { |
| 2307 | if (!Math::is_zero_approx(scroll_offset)) { |
| 2308 | x_ofs = style->get_margin(SIDE_LEFT); |
| 2309 | } else { |
| 2310 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 2311 | int centered = int((get_size().width - total_margin - text_width)) / 2; |
| 2312 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered); |
| 2313 | } |
| 2314 | } break; |
| 2315 | case HORIZONTAL_ALIGNMENT_RIGHT: { |
| 2316 | if (rtl) { |
| 2317 | x_ofs = style->get_margin(SIDE_LEFT); |
| 2318 | } else { |
| 2319 | x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width))); |
| 2320 | } |
| 2321 | } break; |
| 2322 | } |
| 2323 | |
| 2324 | int ofs_max = get_size().width - style->get_margin(SIDE_RIGHT); |
| 2325 | bool using_placeholder = text.is_empty() && ime_text.is_empty(); |
| 2326 | bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled; |
| 2327 | if (right_icon.is_valid() || display_clear_icon) { |
| 2328 | Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; |
| 2329 | Point2 right_icon_size = _get_right_icon_size(r_icon); |
| 2330 | if (alignment == HORIZONTAL_ALIGNMENT_CENTER) { |
| 2331 | if (Math::is_zero_approx(scroll_offset)) { |
| 2332 | int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT); |
| 2333 | int center = int(get_size().width - total_margin - text_width - right_icon_size.width) / 2; |
| 2334 | x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center); |
| 2335 | } |
| 2336 | if (rtl) { |
| 2337 | x_ofs += right_icon_size.width; |
| 2338 | } |
| 2339 | } else { |
| 2340 | if (rtl) { |
| 2341 | x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs); |
| 2342 | } else { |
| 2343 | if (rtl) { |
| 2344 | x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs); |
| 2345 | } else { |
| 2346 | x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT)); |
| 2347 | } |
| 2348 | } |
| 2349 | } |
| 2350 | if (!rtl) { |
| 2351 | ofs_max -= right_icon_size.width; |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | // Note: Use two coordinates to fit IME input range. |
| 2356 | Vector2 primary_caret_offset = get_caret_pixel_pos(); |
| 2357 | |
| 2358 | if (MIN(primary_caret_offset.x, primary_caret_offset.y) <= x_ofs) { |
| 2359 | scroll_offset += x_ofs - MIN(primary_caret_offset.x, primary_caret_offset.y); |
| 2360 | } else if (MAX(primary_caret_offset.x, primary_caret_offset.y) >= ofs_max) { |
| 2361 | scroll_offset += ofs_max - MAX(primary_caret_offset.x, primary_caret_offset.y); |
| 2362 | } |
| 2363 | |
| 2364 | // Scroll to show as much text as possible |
| 2365 | if (text_width + scroll_offset + x_ofs < ofs_max) { |
| 2366 | scroll_offset = ofs_max - x_ofs - text_width; |
| 2367 | } |
| 2368 | |
| 2369 | scroll_offset = MIN(0, scroll_offset); |
| 2370 | |
| 2371 | queue_accessibility_update(); |
| 2372 | queue_redraw(); |
| 2373 | } |
| 2374 | |
| 2375 | int LineEdit::get_caret_column() const { |
| 2376 | return caret_column; |
| 2377 | } |
| 2378 | |
| 2379 | int LineEdit::get_next_composite_character_column(int p_column) const { |
| 2380 | ERR_FAIL_INDEX_V(p_column, text.length() + 1, -1); |
| 2381 | if (p_column == text.length()) { |
| 2382 | return p_column; |
| 2383 | } else { |
| 2384 | return TS->shaped_text_next_character_pos(text_rid, p_column); |
| 2385 | } |
| 2386 | } |
| 2387 | |
| 2388 | int LineEdit::get_previous_composite_character_column(int p_column) const { |
| 2389 | ERR_FAIL_INDEX_V(p_column, text.length() + 1, -1); |
| 2390 | if (p_column == 0) { |
| 2391 | return 0; |
| 2392 | } else { |
| 2393 | return TS->shaped_text_prev_character_pos(text_rid, p_column); |
| 2394 | } |
| 2395 | } |
| 2396 | |
| 2397 | void LineEdit::set_scroll_offset(float p_pos) { |
| 2398 | scroll_offset = p_pos; |
| 2399 | if (scroll_offset < 0.0) { |
| 2400 | scroll_offset = 0.0; |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | float LineEdit::get_scroll_offset() const { |
| 2405 | return scroll_offset; |
| 2406 | } |
| 2407 | |
| 2408 | void LineEdit::insert_text_at_caret(String p_text) { |
| 2409 | if (max_length > 0) { |
| 2410 | // Truncate text to append to fit in max_length, if needed. |
| 2411 | int available_chars = max_length - text.length(); |
| 2412 | if (p_text.length() > available_chars) { |
| 2413 | emit_signal(SNAME("text_change_rejected"), p_text.substr(available_chars)); |
| 2414 | p_text = p_text.substr(0, available_chars); |
| 2415 | } |
| 2416 | } |
| 2417 | String pre = text.substr(0, caret_column); |
| 2418 | String post = text.substr(caret_column); |
| 2419 | text = pre + p_text + post; |
| 2420 | _shape(); |
| 2421 | TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text_rid, caret_column, caret_column + p_text.length()); |
| 2422 | if (dir != TextServer::DIRECTION_AUTO) { |
| 2423 | input_direction = (TextDirection)dir; |
| 2424 | } |
| 2425 | set_caret_column(caret_column + p_text.length()); |
| 2426 | |
| 2427 | if (!ime_text.is_empty()) { |
| 2428 | _shape(); |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | void LineEdit::clear_internal() { |
| 2433 | deselect(); |
| 2434 | _clear_undo_stack(); |
| 2435 | caret_column = 0; |
| 2436 | scroll_offset = 0.0; |
| 2437 | undo_text = ""; |
| 2438 | text = ""; |
| 2439 | _shape(); |
| 2440 | queue_redraw(); |
| 2441 | } |
| 2442 | |
| 2443 | Size2 LineEdit::get_minimum_size() const { |
| 2444 | Ref<Font> font = theme_cache.font; |
| 2445 | int font_size = theme_cache.font_size; |
| 2446 | |
| 2447 | Size2 min_size; |
| 2448 | |
| 2449 | // Minimum size of text. |
| 2450 | // W is wider than M in most fonts, Using M may result in hiding the last digit when using float values in SpinBox, ie. ColorPicker RAW values. |
| 2451 | float em_space_size = font->get_char_size('W', font_size).x; |
| 2452 | min_size.width = theme_cache.minimum_character_width * em_space_size; |
| 2453 | |
| 2454 | if (expand_to_text_length) { |
| 2455 | // Ensure some space for the caret when placed at the end. |
| 2456 | min_size.width = MAX(min_size.width, full_width + theme_cache.caret_width); |
| 2457 | } |
| 2458 | |
| 2459 | min_size.height = MAX(TS->shaped_text_get_size(text_rid).y, font->get_height(font_size)); |
| 2460 | |
| 2461 | // Take icons into account. |
| 2462 | int icon_max_width = 0; |
| 2463 | if (right_icon.is_valid()) { |
| 2464 | Point2 right_icon_size = _get_right_icon_size(right_icon); |
| 2465 | min_size.height = MAX(min_size.height, right_icon_size.height); |
| 2466 | icon_max_width = right_icon_size.width; |
| 2467 | } |
| 2468 | if (clear_button_enabled) { |
| 2469 | Point2 right_icon_size = _get_right_icon_size(theme_cache.clear_icon); |
| 2470 | min_size.height = MAX(min_size.height, right_icon_size.height); |
| 2471 | icon_max_width = MAX(icon_max_width, right_icon_size.width); |
| 2472 | } |
| 2473 | min_size.width += icon_max_width; |
| 2474 | |
| 2475 | Size2 style_min_size = theme_cache.normal->get_minimum_size().max(theme_cache.read_only->get_minimum_size()); |
| 2476 | return style_min_size + min_size; |
| 2477 | } |
| 2478 | |
| 2479 | void LineEdit::deselect() { |
| 2480 | selection.begin = 0; |
| 2481 | selection.end = 0; |
| 2482 | selection.start_column = 0; |
| 2483 | selection.enabled = false; |
| 2484 | selection.creating = false; |
| 2485 | selection.double_click = false; |
| 2486 | queue_accessibility_update(); |
| 2487 | queue_redraw(); |
| 2488 | } |
| 2489 | |
| 2490 | bool LineEdit::has_selection() const { |
| 2491 | return selection.enabled; |
| 2492 | } |
| 2493 | |
| 2494 | String LineEdit::get_selected_text() { |
| 2495 | if (selection.enabled) { |
| 2496 | return text.substr(selection.begin, selection.end - selection.begin); |
| 2497 | } else { |
| 2498 | return String(); |
| 2499 | } |
| 2500 | } |
| 2501 | |
| 2502 | int LineEdit::get_selection_from_column() const { |
| 2503 | ERR_FAIL_COND_V(!selection.enabled, -1); |
| 2504 | return selection.begin; |
| 2505 | } |
| 2506 | |
| 2507 | int LineEdit::get_selection_to_column() const { |
| 2508 | ERR_FAIL_COND_V(!selection.enabled, -1); |
| 2509 | return selection.end; |
| 2510 | } |
| 2511 | |
| 2512 | void LineEdit::selection_delete() { |
| 2513 | if (selection.enabled) { |
| 2514 | delete_text(selection.begin, selection.end); |
| 2515 | } |
| 2516 | |
| 2517 | deselect(); |
| 2518 | } |
| 2519 | |
| 2520 | void LineEdit::set_max_length(int p_max_length) { |
| 2521 | ERR_FAIL_COND(p_max_length < 0); |
| 2522 | max_length = p_max_length; |
| 2523 | set_text(text); |
| 2524 | } |
| 2525 | |
| 2526 | int LineEdit::get_max_length() const { |
| 2527 | return max_length; |
| 2528 | } |
| 2529 | |
| 2530 | void LineEdit::selection_fill_at_caret() { |
| 2531 | if (!selecting_enabled) { |
| 2532 | return; |
| 2533 | } |
| 2534 | |
| 2535 | selection.begin = caret_column; |
| 2536 | selection.end = selection.start_column; |
| 2537 | |
| 2538 | if (selection.end < selection.begin) { |
| 2539 | int aux = selection.end; |
| 2540 | selection.end = selection.begin; |
| 2541 | selection.begin = aux; |
| 2542 | } |
| 2543 | |
| 2544 | selection.enabled = (selection.begin != selection.end); |
| 2545 | queue_accessibility_update(); |
| 2546 | } |
| 2547 | |
| 2548 | void LineEdit::select_all() { |
| 2549 | if (!selecting_enabled) { |
| 2550 | return; |
| 2551 | } |
| 2552 | |
| 2553 | if (text.is_empty()) { |
| 2554 | set_caret_column(0); |
| 2555 | return; |
| 2556 | } |
| 2557 | |
| 2558 | selection.begin = 0; |
| 2559 | selection.end = text.length(); |
| 2560 | selection.enabled = true; |
| 2561 | queue_accessibility_update(); |
| 2562 | queue_redraw(); |
| 2563 | } |
| 2564 | |
| 2565 | void LineEdit::set_editable(bool p_editable) { |
| 2566 | if (editable == p_editable) { |
| 2567 | return; |
| 2568 | } |
| 2569 | |
| 2570 | editable = p_editable; |
| 2571 | |
| 2572 | if (!editable && editing) { |
| 2573 | unedit(); |
| 2574 | emit_signal(SNAME("editing_toggled"), false); |
| 2575 | } |
| 2576 | |
| 2577 | if (editable && has_focus() && !editing) { |
| 2578 | edit(); |
| 2579 | emit_signal(SNAME("editing_toggled"), true); |
| 2580 | } |
| 2581 | |
| 2582 | _validate_caret_can_draw(); |
| 2583 | |
| 2584 | update_minimum_size(); |
| 2585 | queue_accessibility_update(); |
| 2586 | queue_redraw(); |
| 2587 | } |
| 2588 | |
| 2589 | bool LineEdit::is_editable() const { |
| 2590 | return editable; |
| 2591 | } |
| 2592 | |
| 2593 | void LineEdit::set_secret(bool p_secret) { |
| 2594 | if (pass == p_secret) { |
| 2595 | return; |
| 2596 | } |
| 2597 | |
| 2598 | pass = p_secret; |
| 2599 | _shape(); |
| 2600 | set_caret_column(caret_column); // Update scroll_offset. |
| 2601 | queue_redraw(); |
| 2602 | } |
| 2603 | |
| 2604 | bool LineEdit::is_secret() const { |
| 2605 | return pass; |
| 2606 | } |
| 2607 | |
| 2608 | void LineEdit::set_secret_character(const String &p_string) { |
| 2609 | String c = p_string; |
| 2610 | if (c.length() > 1) { |
| 2611 | WARN_PRINT("Secret character must be exactly one character long (" + itos(c.length()) + " characters given)."); |
| 2612 | c = c.left(1); |
| 2613 | } |
| 2614 | if (secret_character == c) { |
| 2615 | return; |
| 2616 | } |
| 2617 | secret_character = c; |
| 2618 | _shape(); |
| 2619 | set_caret_column(caret_column); // Update scroll_offset. |
| 2620 | queue_redraw(); |
| 2621 | } |
| 2622 | |
| 2623 | String LineEdit::get_secret_character() const { |
| 2624 | return secret_character; |
| 2625 | } |
| 2626 | |
| 2627 | void LineEdit::select(int p_from, int p_to) { |
| 2628 | if (!selecting_enabled) { |
| 2629 | return; |
| 2630 | } |
| 2631 | |
| 2632 | if (p_from == 0 && p_to == 0) { |
| 2633 | deselect(); |
| 2634 | return; |
| 2635 | } |
| 2636 | |
| 2637 | int len = text.length(); |
| 2638 | if (p_from < 0) { |
| 2639 | p_from = 0; |
| 2640 | } |
| 2641 | if (p_from > len) { |
| 2642 | p_from = len; |
| 2643 | } |
| 2644 | if (p_to < 0 || p_to > len) { |
| 2645 | p_to = len; |
| 2646 | } |
| 2647 | |
| 2648 | if (p_from >= p_to) { |
| 2649 | return; |
| 2650 | } |
| 2651 | |
| 2652 | selection.enabled = true; |
| 2653 | selection.begin = p_from; |
| 2654 | selection.end = p_to; |
| 2655 | selection.creating = false; |
| 2656 | selection.double_click = false; |
| 2657 | queue_accessibility_update(); |
| 2658 | queue_redraw(); |
| 2659 | } |
| 2660 | |
| 2661 | bool LineEdit::is_text_field() const { |
| 2662 | return true; |
| 2663 | } |
| 2664 | |
| 2665 | void LineEdit::menu_option(int p_option) { |
| 2666 | int prev_len = text.length(); |
| 2667 | bool inserted_control_char = false; |
| 2668 | switch (p_option) { |
| 2669 | case MENU_CUT: { |
| 2670 | if (editable) { |
| 2671 | cut_text(); |
| 2672 | } |
| 2673 | } break; |
| 2674 | case MENU_COPY: { |
| 2675 | copy_text(); |
| 2676 | } break; |
| 2677 | case MENU_PASTE: { |
| 2678 | if (editable) { |
| 2679 | paste_text(); |
| 2680 | } |
| 2681 | } break; |
| 2682 | case MENU_CLEAR: { |
| 2683 | if (editable) { |
| 2684 | clear(); |
| 2685 | } |
| 2686 | } break; |
| 2687 | case MENU_SELECT_ALL: { |
| 2688 | select_all(); |
| 2689 | } break; |
| 2690 | case MENU_UNDO: { |
| 2691 | if (editable) { |
| 2692 | undo(); |
| 2693 | } |
| 2694 | } break; |
| 2695 | case MENU_REDO: { |
| 2696 | if (editable) { |
| 2697 | redo(); |
| 2698 | } |
| 2699 | } break; |
| 2700 | case MENU_DIR_INHERITED: { |
| 2701 | set_text_direction(TEXT_DIRECTION_INHERITED); |
| 2702 | } break; |
| 2703 | case MENU_DIR_AUTO: { |
| 2704 | set_text_direction(TEXT_DIRECTION_AUTO); |
| 2705 | } break; |
| 2706 | case MENU_DIR_LTR: { |
| 2707 | set_text_direction(TEXT_DIRECTION_LTR); |
| 2708 | } break; |
| 2709 | case MENU_DIR_RTL: { |
| 2710 | set_text_direction(TEXT_DIRECTION_RTL); |
| 2711 | } break; |
| 2712 | case MENU_DISPLAY_UCC: { |
| 2713 | set_draw_control_chars(!get_draw_control_chars()); |
| 2714 | } break; |
| 2715 | case MENU_INSERT_LRM: { |
| 2716 | if (editable) { |
| 2717 | insert_text_at_caret(String::chr(0x200E)); |
| 2718 | inserted_control_char = true; |
| 2719 | } |
| 2720 | } break; |
| 2721 | case MENU_INSERT_RLM: { |
| 2722 | if (editable) { |
| 2723 | insert_text_at_caret(String::chr(0x200F)); |
| 2724 | inserted_control_char = true; |
| 2725 | } |
| 2726 | } break; |
| 2727 | case MENU_INSERT_LRE: { |
| 2728 | if (editable) { |
| 2729 | insert_text_at_caret(String::chr(0x202A)); |
| 2730 | inserted_control_char = true; |
| 2731 | } |
| 2732 | } break; |
| 2733 | case MENU_INSERT_RLE: { |
| 2734 | if (editable) { |
| 2735 | insert_text_at_caret(String::chr(0x202B)); |
| 2736 | inserted_control_char = true; |
| 2737 | } |
| 2738 | } break; |
| 2739 | case MENU_INSERT_LRO: { |
| 2740 | if (editable) { |
| 2741 | insert_text_at_caret(String::chr(0x202D)); |
| 2742 | inserted_control_char = true; |
| 2743 | } |
| 2744 | } break; |
| 2745 | case MENU_INSERT_RLO: { |
| 2746 | if (editable) { |
| 2747 | insert_text_at_caret(String::chr(0x202E)); |
| 2748 | inserted_control_char = true; |
| 2749 | } |
| 2750 | } break; |
| 2751 | case MENU_INSERT_PDF: { |
| 2752 | if (editable) { |
| 2753 | insert_text_at_caret(String::chr(0x202C)); |
| 2754 | inserted_control_char = true; |
| 2755 | } |
| 2756 | } break; |
| 2757 | case MENU_INSERT_ALM: { |
| 2758 | if (editable) { |
| 2759 | insert_text_at_caret(String::chr(0x061C)); |
| 2760 | inserted_control_char = true; |
| 2761 | } |
| 2762 | } break; |
| 2763 | case MENU_INSERT_LRI: { |
| 2764 | if (editable) { |
| 2765 | insert_text_at_caret(String::chr(0x2066)); |
| 2766 | inserted_control_char = true; |
| 2767 | } |
| 2768 | } break; |
| 2769 | case MENU_INSERT_RLI: { |
| 2770 | if (editable) { |
| 2771 | insert_text_at_caret(String::chr(0x2067)); |
| 2772 | inserted_control_char = true; |
| 2773 | } |
| 2774 | } break; |
| 2775 | case MENU_INSERT_FSI: { |
| 2776 | if (editable) { |
| 2777 | insert_text_at_caret(String::chr(0x2068)); |
| 2778 | inserted_control_char = true; |
| 2779 | } |
| 2780 | } break; |
| 2781 | case MENU_INSERT_PDI: { |
| 2782 | if (editable) { |
| 2783 | insert_text_at_caret(String::chr(0x2069)); |
| 2784 | inserted_control_char = true; |
| 2785 | } |
| 2786 | } break; |
| 2787 | case MENU_INSERT_ZWJ: { |
| 2788 | if (editable) { |
| 2789 | insert_text_at_caret(String::chr(0x200D)); |
| 2790 | inserted_control_char = true; |
| 2791 | } |
| 2792 | } break; |
| 2793 | case MENU_INSERT_ZWNJ: { |
| 2794 | if (editable) { |
| 2795 | insert_text_at_caret(String::chr(0x200C)); |
| 2796 | inserted_control_char = true; |
| 2797 | } |
| 2798 | } break; |
| 2799 | case MENU_INSERT_WJ: { |
| 2800 | if (editable) { |
| 2801 | insert_text_at_caret(String::chr(0x2060)); |
| 2802 | inserted_control_char = true; |
| 2803 | } |
| 2804 | } break; |
| 2805 | case MENU_INSERT_SHY: { |
| 2806 | if (editable) { |
| 2807 | insert_text_at_caret(String::chr(0x00AD)); |
| 2808 | inserted_control_char = true; |
| 2809 | } |
| 2810 | } break; |
| 2811 | case MENU_EMOJI_AND_SYMBOL: { |
| 2812 | show_emoji_and_symbol_picker(); |
| 2813 | } break; |
| 2814 | } |
| 2815 | |
| 2816 | // Mirror paste/drag behavior, emit text_changed signal if a control character was inserted. |
| 2817 | if (inserted_control_char && !text_changed_dirty) { |
| 2818 | if (is_inside_tree() && text.length() != prev_len) { |
| 2819 | callable_mp(this, &LineEdit::_text_changed).call_deferred(); |
| 2820 | } |
| 2821 | text_changed_dirty = true; |
| 2822 | } |
| 2823 | } |
| 2824 | |
| 2825 | void LineEdit::set_context_menu_enabled(bool p_enable) { |
| 2826 | context_menu_enabled = p_enable; |
| 2827 | } |
| 2828 | |
| 2829 | bool LineEdit::is_context_menu_enabled() { |
| 2830 | return context_menu_enabled; |
| 2831 | } |
| 2832 | |
| 2833 | void LineEdit::show_emoji_and_symbol_picker() { |
| 2834 | _update_ime_window_position(); |
| 2835 | DisplayServer::get_singleton()->show_emoji_and_symbol_picker(); |
| 2836 | } |
| 2837 | |
| 2838 | void LineEdit::set_emoji_menu_enabled(bool p_enabled) { |
| 2839 | if (emoji_menu_enabled != p_enabled) { |
| 2840 | emoji_menu_enabled = p_enabled; |
| 2841 | } |
| 2842 | } |
| 2843 | |
| 2844 | bool LineEdit::is_emoji_menu_enabled() const { |
| 2845 | return emoji_menu_enabled; |
| 2846 | } |
| 2847 | |
| 2848 | void LineEdit::set_backspace_deletes_composite_character_enabled(bool p_enabled) { |
| 2849 | backspace_deletes_composite_character_enabled = p_enabled; |
| 2850 | } |
| 2851 | |
| 2852 | bool LineEdit::is_backspace_deletes_composite_character_enabled() const { |
| 2853 | return backspace_deletes_composite_character_enabled; |
| 2854 | } |
| 2855 | |
| 2856 | bool LineEdit::is_menu_visible() const { |
| 2857 | return menu && menu->is_visible(); |
| 2858 | } |
| 2859 | |
| 2860 | PopupMenu *LineEdit::get_menu() const { |
| 2861 | if (!menu) { |
| 2862 | const_cast<LineEdit *>(this)->_generate_context_menu(); |
| 2863 | } |
| 2864 | return menu; |
| 2865 | } |
| 2866 | |
| 2867 | void LineEdit::_editor_settings_changed() { |
| 2868 | #ifdef TOOLS_ENABLED |
| 2869 | if (!EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/appearance/caret")) { |
| 2870 | return; |
| 2871 | } |
| 2872 | set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink")); |
| 2873 | set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval")); |
| 2874 | #endif |
| 2875 | } |
| 2876 | |
| 2877 | void LineEdit::set_expand_to_text_length_enabled(bool p_enabled) { |
| 2878 | expand_to_text_length = p_enabled; |
| 2879 | update_minimum_size(); |
| 2880 | set_caret_column(caret_column); |
| 2881 | } |
| 2882 | |
| 2883 | bool LineEdit::is_expand_to_text_length_enabled() const { |
| 2884 | return expand_to_text_length; |
| 2885 | } |
| 2886 | |
| 2887 | void LineEdit::set_clear_button_enabled(bool p_enabled) { |
| 2888 | if (clear_button_enabled == p_enabled) { |
| 2889 | return; |
| 2890 | } |
| 2891 | clear_button_enabled = p_enabled; |
| 2892 | _fit_to_width(); |
| 2893 | update_minimum_size(); |
| 2894 | queue_redraw(); |
| 2895 | } |
| 2896 | |
| 2897 | bool LineEdit::is_clear_button_enabled() const { |
| 2898 | return clear_button_enabled; |
| 2899 | } |
| 2900 | |
| 2901 | void LineEdit::set_shortcut_keys_enabled(bool p_enabled) { |
| 2902 | shortcut_keys_enabled = p_enabled; |
| 2903 | } |
| 2904 | |
| 2905 | bool LineEdit::is_shortcut_keys_enabled() const { |
| 2906 | return shortcut_keys_enabled; |
| 2907 | } |
| 2908 | |
| 2909 | void LineEdit::set_virtual_keyboard_enabled(bool p_enable) { |
| 2910 | virtual_keyboard_enabled = p_enable; |
| 2911 | } |
| 2912 | |
| 2913 | bool LineEdit::is_virtual_keyboard_enabled() const { |
| 2914 | return virtual_keyboard_enabled; |
| 2915 | } |
| 2916 | |
| 2917 | void LineEdit::set_virtual_keyboard_show_on_focus(bool p_show_on_focus) { |
| 2918 | virtual_keyboard_show_on_focus = p_show_on_focus; |
| 2919 | } |
| 2920 | |
| 2921 | bool LineEdit::get_virtual_keyboard_show_on_focus() const { |
| 2922 | return virtual_keyboard_show_on_focus; |
| 2923 | } |
| 2924 | |
| 2925 | void LineEdit::set_virtual_keyboard_type(VirtualKeyboardType p_type) { |
| 2926 | virtual_keyboard_type = p_type; |
| 2927 | } |
| 2928 | |
| 2929 | LineEdit::VirtualKeyboardType LineEdit::get_virtual_keyboard_type() const { |
| 2930 | return virtual_keyboard_type; |
| 2931 | } |
| 2932 | |
| 2933 | void LineEdit::set_middle_mouse_paste_enabled(bool p_enabled) { |
| 2934 | middle_mouse_paste_enabled = p_enabled; |
| 2935 | } |
| 2936 | |
| 2937 | bool LineEdit::is_middle_mouse_paste_enabled() const { |
| 2938 | return middle_mouse_paste_enabled; |
| 2939 | } |
| 2940 | |
| 2941 | void LineEdit::set_selecting_enabled(bool p_enabled) { |
| 2942 | if (selecting_enabled == p_enabled) { |
| 2943 | return; |
| 2944 | } |
| 2945 | |
| 2946 | selecting_enabled = p_enabled; |
| 2947 | |
| 2948 | if (!selecting_enabled) { |
| 2949 | deselect(); |
| 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | bool LineEdit::is_selecting_enabled() const { |
| 2954 | return selecting_enabled; |
| 2955 | } |
| 2956 | |
| 2957 | void LineEdit::set_deselect_on_focus_loss_enabled(const bool p_enabled) { |
| 2958 | if (deselect_on_focus_loss_enabled == p_enabled) { |
| 2959 | return; |
| 2960 | } |
| 2961 | |
| 2962 | deselect_on_focus_loss_enabled = p_enabled; |
| 2963 | if (p_enabled && selection.enabled && !has_focus()) { |
| 2964 | deselect(); |
| 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | bool LineEdit::is_deselect_on_focus_loss_enabled() const { |
| 2969 | return deselect_on_focus_loss_enabled; |
| 2970 | } |
| 2971 | |
| 2972 | void LineEdit::set_drag_and_drop_selection_enabled(const bool p_enabled) { |
| 2973 | drag_and_drop_selection_enabled = p_enabled; |
| 2974 | } |
| 2975 | |
| 2976 | bool LineEdit::is_drag_and_drop_selection_enabled() const { |
| 2977 | return drag_and_drop_selection_enabled; |
| 2978 | } |
| 2979 | |
| 2980 | void LineEdit::_texture_changed() { |
| 2981 | _fit_to_width(); |
| 2982 | update_minimum_size(); |
| 2983 | queue_redraw(); |
| 2984 | } |
| 2985 | |
| 2986 | void LineEdit::set_right_icon(const Ref<Texture2D> &p_icon) { |
| 2987 | if (right_icon == p_icon) { |
| 2988 | return; |
| 2989 | } |
| 2990 | |
| 2991 | if (right_icon.is_valid()) { |
| 2992 | right_icon->disconnect_changed(callable_mp(this, &LineEdit::_texture_changed)); |
| 2993 | } |
| 2994 | |
| 2995 | right_icon = p_icon; |
| 2996 | |
| 2997 | if (right_icon.is_valid()) { |
| 2998 | right_icon->connect_changed(callable_mp(this, &LineEdit::_texture_changed)); |
| 2999 | } |
| 3000 | |
| 3001 | _fit_to_width(); |
| 3002 | update_minimum_size(); |
| 3003 | queue_redraw(); |
| 3004 | } |
| 3005 | |
| 3006 | Ref<Texture2D> LineEdit::get_right_icon() { |
| 3007 | return right_icon; |
| 3008 | } |
| 3009 | |
| 3010 | void LineEdit::set_icon_expand_mode(ExpandMode p_mode) { |
| 3011 | if (icon_expand_mode == p_mode) { |
| 3012 | return; |
| 3013 | } |
| 3014 | |
| 3015 | icon_expand_mode = p_mode; |
| 3016 | queue_redraw(); |
| 3017 | update_minimum_size(); |
| 3018 | notify_property_list_changed(); |
| 3019 | } |
| 3020 | |
| 3021 | LineEdit::ExpandMode LineEdit::get_icon_expand_mode() const { |
| 3022 | return icon_expand_mode; |
| 3023 | } |
| 3024 | |
| 3025 | void LineEdit::set_right_icon_scale(float p_scale) { |
| 3026 | if (right_icon_scale == p_scale) { |
| 3027 | return; |
| 3028 | } |
| 3029 | |
| 3030 | right_icon_scale = p_scale; |
| 3031 | queue_redraw(); |
| 3032 | update_minimum_size(); |
| 3033 | } |
| 3034 | |
| 3035 | float LineEdit::get_right_icon_scale() const { |
| 3036 | return right_icon_scale; |
| 3037 | } |
| 3038 | |
| 3039 | void LineEdit::set_flat(bool p_enabled) { |
| 3040 | if (flat != p_enabled) { |
| 3041 | flat = p_enabled; |
| 3042 | queue_redraw(); |
| 3043 | } |
| 3044 | } |
| 3045 | |
| 3046 | bool LineEdit::is_flat() const { |
| 3047 | return flat; |
| 3048 | } |
| 3049 | |
| 3050 | void LineEdit::set_select_all_on_focus(bool p_enabled) { |
| 3051 | select_all_on_focus = p_enabled; |
| 3052 | } |
| 3053 | |
| 3054 | bool LineEdit::is_select_all_on_focus() const { |
| 3055 | return select_all_on_focus; |
| 3056 | } |
| 3057 | |
| 3058 | void LineEdit::clear_pending_select_all_on_focus() { |
| 3059 | pending_select_all_on_focus = false; |
| 3060 | } |
| 3061 | |
| 3062 | void LineEdit::_text_changed() { |
| 3063 | _emit_text_change(); |
| 3064 | _clear_redo(); |
| 3065 | } |
| 3066 | |
| 3067 | void LineEdit::_emit_text_change() { |
| 3068 | emit_signal(SceneStringName(text_changed), text); |
| 3069 | text_changed_dirty = false; |
| 3070 | } |
| 3071 | |
| 3072 | PackedStringArray LineEdit::get_configuration_warnings() const { |
| 3073 | PackedStringArray warnings = Control::get_configuration_warnings(); |
| 3074 | if (secret_character.length() > 1) { |
| 3075 | warnings.push_back("Secret Character property supports only one character. Extra characters will be ignored."); |
| 3076 | } |
| 3077 | return warnings; |
| 3078 | } |
| 3079 | |
| 3080 | void LineEdit::_shape() { |
| 3081 | const Ref<Font> &font = theme_cache.font; |
| 3082 | int font_size = theme_cache.font_size; |
| 3083 | if (font.is_null()) { |
| 3084 | return; |
| 3085 | } |
| 3086 | |
| 3087 | Size2 old_size = TS->shaped_text_get_size(text_rid); |
| 3088 | TS->shaped_text_clear(text_rid); |
| 3089 | |
| 3090 | String t; |
| 3091 | if (text.is_empty() && ime_text.is_empty()) { |
| 3092 | t = placeholder_translated; |
| 3093 | } else if (pass) { |
| 3094 | String s = secret_character.is_empty() ? U"•" : secret_character.left(1); |
| 3095 | t = s.repeat(text.length() + ime_text.length()); |
| 3096 | } else { |
| 3097 | if (!ime_text.is_empty()) { |
| 3098 | t = text.substr(0, caret_column) + ime_text + text.substr(caret_column); |
| 3099 | } else { |
| 3100 | t = text; |
| 3101 | } |
| 3102 | } |
| 3103 | if (text_direction == Control::TEXT_DIRECTION_INHERITED) { |
| 3104 | TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR); |
| 3105 | } else { |
| 3106 | TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction); |
| 3107 | } |
| 3108 | TS->shaped_text_set_preserve_control(text_rid, draw_control_chars); |
| 3109 | |
| 3110 | const String &lang = language.is_empty() ? _get_locale() : language; |
| 3111 | TS->shaped_text_add_string(text_rid, t, font->get_rids(), font_size, font->get_opentype_features(), lang); |
| 3112 | TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, t)); |
| 3113 | |
| 3114 | full_width = TS->shaped_text_get_size(text_rid).x; |
| 3115 | _fit_to_width(); |
| 3116 | |
| 3117 | Size2 size = TS->shaped_text_get_size(text_rid); |
| 3118 | |
| 3119 | if ((expand_to_text_length && old_size.x != size.x) || (old_size.y != size.y)) { |
| 3120 | update_minimum_size(); |
| 3121 | } |
| 3122 | |
| 3123 | if (accessibility_text_root_element.is_valid()) { |
| 3124 | DisplayServer::get_singleton()->accessibility_free_element(accessibility_text_root_element); |
| 3125 | accessibility_text_root_element = RID(); |
| 3126 | } |
| 3127 | |
| 3128 | queue_accessibility_update(); |
| 3129 | } |
| 3130 | |
| 3131 | void LineEdit::_fit_to_width() { |
| 3132 | if (alignment == HORIZONTAL_ALIGNMENT_FILL) { |
| 3133 | Ref<StyleBox> style = theme_cache.normal; |
| 3134 | int t_width = get_size().width - style->get_margin(SIDE_RIGHT) - style->get_margin(SIDE_LEFT); |
| 3135 | bool using_placeholder = text.is_empty() && ime_text.is_empty(); |
| 3136 | bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled; |
| 3137 | if (right_icon.is_valid() || display_clear_icon) { |
| 3138 | Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon; |
| 3139 | Point2 right_icon_size = _get_right_icon_size(r_icon); |
| 3140 | t_width -= right_icon_size.width; |
| 3141 | } |
| 3142 | TS->shaped_text_fit_to_width(text_rid, MAX(t_width, full_width)); |
| 3143 | } |
| 3144 | } |
| 3145 | |
| 3146 | void LineEdit::_clear_redo() { |
| 3147 | _create_undo_state(); |
| 3148 | if (undo_stack_pos == nullptr) { |
| 3149 | return; |
| 3150 | } |
| 3151 | |
| 3152 | undo_stack_pos = undo_stack_pos->next(); |
| 3153 | while (undo_stack_pos) { |
| 3154 | List<TextOperation>::Element *elem = undo_stack_pos; |
| 3155 | undo_stack_pos = undo_stack_pos->next(); |
| 3156 | undo_stack.erase(elem); |
| 3157 | } |
| 3158 | _create_undo_state(); |
| 3159 | } |
| 3160 | |
| 3161 | void LineEdit::_clear_undo_stack() { |
| 3162 | undo_stack.clear(); |
| 3163 | undo_stack_pos = nullptr; |
| 3164 | _create_undo_state(); |
| 3165 | } |
| 3166 | |
| 3167 | void LineEdit::_create_undo_state() { |
| 3168 | TextOperation op; |
| 3169 | op.text = text; |
| 3170 | op.caret_column = caret_column; |
| 3171 | op.scroll_offset = scroll_offset; |
| 3172 | undo_stack.push_back(op); |
| 3173 | } |
| 3174 | |
| 3175 | Key LineEdit::_get_menu_action_accelerator(const String &p_action) { |
| 3176 | const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action); |
| 3177 | if (!events) { |
| 3178 | return Key::NONE; |
| 3179 | } |
| 3180 | |
| 3181 | // Use first event in the list for the accelerator. |
| 3182 | const List<Ref<InputEvent>>::Element *first_event = events->front(); |
| 3183 | if (!first_event) { |
| 3184 | return Key::NONE; |
| 3185 | } |
| 3186 | |
| 3187 | const Ref<InputEventKey> event = first_event->get(); |
| 3188 | if (event.is_null()) { |
| 3189 | return Key::NONE; |
| 3190 | } |
| 3191 | |
| 3192 | // Use physical keycode if non-zero. |
| 3193 | if (event->get_physical_keycode() != Key::NONE) { |
| 3194 | return event->get_physical_keycode_with_modifiers(); |
| 3195 | } else { |
| 3196 | return event->get_keycode_with_modifiers(); |
| 3197 | } |
| 3198 | } |
| 3199 | |
| 3200 | void LineEdit::_generate_context_menu() { |
| 3201 | menu = memnew(PopupMenu); |
| 3202 | add_child(menu, false, INTERNAL_MODE_FRONT); |
| 3203 | |
| 3204 | menu_dir = memnew(PopupMenu); |
| 3205 | menu_dir->add_radio_check_item(ETR("Same as Layout Direction"), MENU_DIR_INHERITED); |
| 3206 | menu_dir->add_radio_check_item(ETR("Auto-Detect Direction"), MENU_DIR_AUTO); |
| 3207 | menu_dir->add_radio_check_item(ETR("Left-to-Right"), MENU_DIR_LTR); |
| 3208 | menu_dir->add_radio_check_item(ETR("Right-to-Left"), MENU_DIR_RTL); |
| 3209 | |
| 3210 | menu_ctl = memnew(PopupMenu); |
| 3211 | menu_ctl->add_item(ETR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM); |
| 3212 | menu_ctl->add_item(ETR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM); |
| 3213 | menu_ctl->add_item(ETR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE); |
| 3214 | menu_ctl->add_item(ETR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE); |
| 3215 | menu_ctl->add_item(ETR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO); |
| 3216 | menu_ctl->add_item(ETR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO); |
| 3217 | menu_ctl->add_item(ETR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF); |
| 3218 | menu_ctl->add_separator(); |
| 3219 | menu_ctl->add_item(ETR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM); |
| 3220 | menu_ctl->add_item(ETR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI); |
| 3221 | menu_ctl->add_item(ETR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI); |
| 3222 | menu_ctl->add_item(ETR("First Strong Isolate (FSI)"), MENU_INSERT_FSI); |
| 3223 | menu_ctl->add_item(ETR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI); |
| 3224 | menu_ctl->add_separator(); |
| 3225 | menu_ctl->add_item(ETR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ); |
| 3226 | menu_ctl->add_item(ETR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ); |
| 3227 | menu_ctl->add_item(ETR("Word Joiner (WJ)"), MENU_INSERT_WJ); |
| 3228 | menu_ctl->add_item(ETR("Soft Hyphen (SHY)"), MENU_INSERT_SHY); |
| 3229 | |
| 3230 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) { |
| 3231 | menu->add_item(ETR("Emoji & Symbols"), MENU_EMOJI_AND_SYMBOL); |
| 3232 | menu->add_separator(); |
| 3233 | } |
| 3234 | |
| 3235 | menu->add_item(ETR("Cut"), MENU_CUT); |
| 3236 | menu->add_item(ETR("Copy"), MENU_COPY); |
| 3237 | menu->add_item(ETR("Paste"), MENU_PASTE); |
| 3238 | menu->add_separator(); |
| 3239 | menu->add_item(ETR("Select All"), MENU_SELECT_ALL); |
| 3240 | menu->add_item(ETR("Clear"), MENU_CLEAR); |
| 3241 | menu->add_separator(); |
| 3242 | menu->add_item(ETR("Undo"), MENU_UNDO); |
| 3243 | menu->add_item(ETR("Redo"), MENU_REDO); |
| 3244 | menu->add_separator(); |
| 3245 | menu->add_submenu_node_item(ETR("Text Writing Direction"), menu_dir, MENU_SUBMENU_TEXT_DIR); |
| 3246 | menu->add_separator(); |
| 3247 | menu->add_check_item(ETR("Display Control Characters"), MENU_DISPLAY_UCC); |
| 3248 | menu->add_submenu_node_item(ETR("Insert Control Character"), menu_ctl, MENU_SUBMENU_INSERT_UCC); |
| 3249 | |
| 3250 | menu->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option)); |
| 3251 | menu_dir->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option)); |
| 3252 | menu_ctl->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option)); |
| 3253 | |
| 3254 | menu->connect(SceneStringName(focus_entered), callable_mp(this, &LineEdit::_validate_caret_can_draw)); |
| 3255 | menu->connect(SceneStringName(focus_exited), callable_mp(this, &LineEdit::_validate_caret_can_draw)); |
| 3256 | } |
| 3257 | |
| 3258 | void LineEdit::_update_context_menu() { |
| 3259 | if (!menu) { |
| 3260 | _generate_context_menu(); |
| 3261 | } |
| 3262 | |
| 3263 | int idx = -1; |
| 3264 | |
| 3265 | #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \ |
| 3266 | idx = m_menu->get_item_index(m_id); \ |
| 3267 | if (idx >= 0) { \ |
| 3268 | m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \ |
| 3269 | m_menu->set_item_disabled(idx, m_disabled); \ |
| 3270 | } |
| 3271 | |
| 3272 | #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \ |
| 3273 | idx = m_menu->get_item_index(m_id); \ |
| 3274 | if (idx >= 0) { \ |
| 3275 | m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \ |
| 3276 | } |
| 3277 | |
| 3278 | #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \ |
| 3279 | idx = m_menu->get_item_index(m_id); \ |
| 3280 | if (idx >= 0) { \ |
| 3281 | m_menu->set_item_disabled(idx, m_disabled); \ |
| 3282 | } |
| 3283 | |
| 3284 | #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \ |
| 3285 | idx = m_menu->get_item_index(m_id); \ |
| 3286 | if (idx >= 0) { \ |
| 3287 | m_menu->set_item_checked(idx, m_checked); \ |
| 3288 | } |
| 3289 | |
| 3290 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) { |
| 3291 | MENU_ITEM_DISABLED(menu, MENU_EMOJI_AND_SYMBOL, !editable || !emoji_menu_enabled) |
| 3292 | } |
| 3293 | MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable) |
| 3294 | MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy") |
| 3295 | MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable) |
| 3296 | MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled) |
| 3297 | MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable) |
| 3298 | MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo()) |
| 3299 | MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo()) |
| 3300 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED) |
| 3301 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO) |
| 3302 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR) |
| 3303 | MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL) |
| 3304 | MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars) |
| 3305 | MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable) |
| 3306 | |
| 3307 | #undef MENU_ITEM_ACTION_DISABLED |
| 3308 | #undef MENU_ITEM_ACTION |
| 3309 | #undef MENU_ITEM_DISABLED |
| 3310 | #undef MENU_ITEM_CHECKED |
| 3311 | } |
| 3312 | |
| 3313 | void LineEdit::_validate_property(PropertyInfo &p_property) const { |
| 3314 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 3315 | return; |
| 3316 | } |
| 3317 | if (!caret_blink_enabled && p_property.name == "caret_blink_interval") { |
| 3318 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 3319 | } else if (icon_expand_mode != EXPAND_MODE_FIT_TO_LINE_EDIT && p_property.name == "right_icon_scale") { |
| 3320 | p_property.usage = PROPERTY_USAGE_NO_EDITOR; |
| 3321 | } |
| 3322 | } |
| 3323 | |
| 3324 | void LineEdit::_bind_methods() { |
| 3325 | // Private exposed API. |
| 3326 | ClassDB::bind_method(D_METHOD("_set_text", "text", "emit_signal"), &LineEdit::_set_text, DEFVAL(false)); |
| 3327 | |
| 3328 | // Public API. |
| 3329 | ClassDB::bind_method(D_METHOD("has_ime_text"), &LineEdit::has_ime_text); |
| 3330 | ClassDB::bind_method(D_METHOD("cancel_ime"), &LineEdit::cancel_ime); |
| 3331 | ClassDB::bind_method(D_METHOD("apply_ime"), &LineEdit::apply_ime); |
| 3332 | |
| 3333 | ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment); |
| 3334 | ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment); |
| 3335 | |
| 3336 | ClassDB::bind_method(D_METHOD("edit", "hide_focus"), &LineEdit::edit, DEFVAL(false)); |
| 3337 | ClassDB::bind_method(D_METHOD("unedit"), &LineEdit::unedit); |
| 3338 | ClassDB::bind_method(D_METHOD("is_editing"), &LineEdit::is_editing); |
| 3339 | ClassDB::bind_method(D_METHOD("set_keep_editing_on_text_submit", "enable"), &LineEdit::set_keep_editing_on_text_submit); |
| 3340 | ClassDB::bind_method(D_METHOD("is_editing_kept_on_text_submit"), &LineEdit::is_editing_kept_on_text_submit); |
| 3341 | ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear); |
| 3342 | ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1)); |
| 3343 | ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all); |
| 3344 | ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect); |
| 3345 | ClassDB::bind_method(D_METHOD("has_undo"), &LineEdit::has_undo); |
| 3346 | ClassDB::bind_method(D_METHOD("has_redo"), &LineEdit::has_redo); |
| 3347 | ClassDB::bind_method(D_METHOD("has_selection"), &LineEdit::has_selection); |
| 3348 | ClassDB::bind_method(D_METHOD("get_selected_text"), &LineEdit::get_selected_text); |
| 3349 | ClassDB::bind_method(D_METHOD("get_selection_from_column"), &LineEdit::get_selection_from_column); |
| 3350 | ClassDB::bind_method(D_METHOD("get_selection_to_column"), &LineEdit::get_selection_to_column); |
| 3351 | ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text); |
| 3352 | ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text); |
| 3353 | ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &LineEdit::get_draw_control_chars); |
| 3354 | ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enable"), &LineEdit::set_draw_control_chars); |
| 3355 | ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LineEdit::set_text_direction); |
| 3356 | ClassDB::bind_method(D_METHOD("get_text_direction"), &LineEdit::get_text_direction); |
| 3357 | ClassDB::bind_method(D_METHOD("set_language", "language"), &LineEdit::set_language); |
| 3358 | ClassDB::bind_method(D_METHOD("get_language"), &LineEdit::get_language); |
| 3359 | ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LineEdit::set_structured_text_bidi_override); |
| 3360 | ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LineEdit::get_structured_text_bidi_override); |
| 3361 | ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LineEdit::set_structured_text_bidi_override_options); |
| 3362 | ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LineEdit::get_structured_text_bidi_override_options); |
| 3363 | ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder); |
| 3364 | ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder); |
| 3365 | ClassDB::bind_method(D_METHOD("set_caret_column", "position"), &LineEdit::set_caret_column); |
| 3366 | ClassDB::bind_method(D_METHOD("get_caret_column"), &LineEdit::get_caret_column); |
| 3367 | ClassDB::bind_method(D_METHOD("get_next_composite_character_column", "column"), &LineEdit::get_next_composite_character_column); |
| 3368 | ClassDB::bind_method(D_METHOD("get_previous_composite_character_column", "column"), &LineEdit::get_previous_composite_character_column); |
| 3369 | ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset); |
| 3370 | ClassDB::bind_method(D_METHOD("set_expand_to_text_length_enabled", "enabled"), &LineEdit::set_expand_to_text_length_enabled); |
| 3371 | ClassDB::bind_method(D_METHOD("is_expand_to_text_length_enabled"), &LineEdit::is_expand_to_text_length_enabled); |
| 3372 | ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enabled"), &LineEdit::set_caret_blink_enabled); |
| 3373 | ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &LineEdit::is_caret_blink_enabled); |
| 3374 | ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &LineEdit::set_caret_mid_grapheme_enabled); |
| 3375 | ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &LineEdit::is_caret_mid_grapheme_enabled); |
| 3376 | ClassDB::bind_method(D_METHOD("set_caret_force_displayed", "enabled"), &LineEdit::set_caret_force_displayed); |
| 3377 | ClassDB::bind_method(D_METHOD("is_caret_force_displayed"), &LineEdit::is_caret_force_displayed); |
| 3378 | ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &LineEdit::set_caret_blink_interval); |
| 3379 | ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &LineEdit::get_caret_blink_interval); |
| 3380 | ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length); |
| 3381 | ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length); |
| 3382 | ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text"), &LineEdit::insert_text_at_caret); |
| 3383 | ClassDB::bind_method(D_METHOD("delete_char_at_caret"), &LineEdit::delete_char); |
| 3384 | ClassDB::bind_method(D_METHOD("delete_text", "from_column", "to_column"), &LineEdit::delete_text); |
| 3385 | ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable); |
| 3386 | ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable); |
| 3387 | ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret); |
| 3388 | ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret); |
| 3389 | ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character); |
| 3390 | ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character); |
| 3391 | ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option); |
| 3392 | // TODO: Properly handle popups when advanced GUI is disabled. |
| 3393 | #ifndef ADVANCED_GUI_DISABLED |
| 3394 | ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu); |
| 3395 | #endif // ADVANCED_GUI_DISABLED |
| 3396 | ClassDB::bind_method(D_METHOD("is_menu_visible"), &LineEdit::is_menu_visible); |
| 3397 | ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled); |
| 3398 | ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled); |
| 3399 | ClassDB::bind_method(D_METHOD("set_emoji_menu_enabled", "enable"), &LineEdit::set_emoji_menu_enabled); |
| 3400 | ClassDB::bind_method(D_METHOD("is_emoji_menu_enabled"), &LineEdit::is_emoji_menu_enabled); |
| 3401 | ClassDB::bind_method(D_METHOD("set_backspace_deletes_composite_character_enabled", "enable"), &LineEdit::set_backspace_deletes_composite_character_enabled); |
| 3402 | ClassDB::bind_method(D_METHOD("is_backspace_deletes_composite_character_enabled"), &LineEdit::is_backspace_deletes_composite_character_enabled); |
| 3403 | ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &LineEdit::set_virtual_keyboard_enabled); |
| 3404 | ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &LineEdit::is_virtual_keyboard_enabled); |
| 3405 | ClassDB::bind_method(D_METHOD("set_virtual_keyboard_show_on_focus", "show_on_focus"), &LineEdit::set_virtual_keyboard_show_on_focus); |
| 3406 | ClassDB::bind_method(D_METHOD("get_virtual_keyboard_show_on_focus"), &LineEdit::get_virtual_keyboard_show_on_focus); |
| 3407 | ClassDB::bind_method(D_METHOD("set_virtual_keyboard_type", "type"), &LineEdit::set_virtual_keyboard_type); |
| 3408 | ClassDB::bind_method(D_METHOD("get_virtual_keyboard_type"), &LineEdit::get_virtual_keyboard_type); |
| 3409 | ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled); |
| 3410 | ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled); |
| 3411 | ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled); |
| 3412 | ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled); |
| 3413 | ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enable"), &LineEdit::set_middle_mouse_paste_enabled); |
| 3414 | ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &LineEdit::is_middle_mouse_paste_enabled); |
| 3415 | ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled); |
| 3416 | ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled); |
| 3417 | ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &LineEdit::set_deselect_on_focus_loss_enabled); |
| 3418 | ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &LineEdit::is_deselect_on_focus_loss_enabled); |
| 3419 | ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &LineEdit::set_drag_and_drop_selection_enabled); |
| 3420 | ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &LineEdit::is_drag_and_drop_selection_enabled); |
| 3421 | ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon); |
| 3422 | ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon); |
| 3423 | ClassDB::bind_method(D_METHOD("set_icon_expand_mode", "mode"), &LineEdit::set_icon_expand_mode); |
| 3424 | ClassDB::bind_method(D_METHOD("get_icon_expand_mode"), &LineEdit::get_icon_expand_mode); |
| 3425 | ClassDB::bind_method(D_METHOD("set_right_icon_scale", "scale"), &LineEdit::set_right_icon_scale); |
| 3426 | ClassDB::bind_method(D_METHOD("get_right_icon_scale"), &LineEdit::get_right_icon_scale); |
| 3427 | ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &LineEdit::set_flat); |
| 3428 | ClassDB::bind_method(D_METHOD("is_flat"), &LineEdit::is_flat); |
| 3429 | ClassDB::bind_method(D_METHOD("set_select_all_on_focus", "enabled"), &LineEdit::set_select_all_on_focus); |
| 3430 | ClassDB::bind_method(D_METHOD("is_select_all_on_focus"), &LineEdit::is_select_all_on_focus); |
| 3431 | |
| 3432 | ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text"))); |
| 3433 | ADD_SIGNAL(MethodInfo("text_change_rejected", PropertyInfo(Variant::STRING, "rejected_substring"))); |
| 3434 | ADD_SIGNAL(MethodInfo("text_submitted", PropertyInfo(Variant::STRING, "new_text"))); |
| 3435 | ADD_SIGNAL(MethodInfo("editing_toggled", PropertyInfo(Variant::BOOL, "toggled_on"))); |
| 3436 | |
| 3437 | BIND_ENUM_CONSTANT(MENU_CUT); |
| 3438 | BIND_ENUM_CONSTANT(MENU_COPY); |
| 3439 | BIND_ENUM_CONSTANT(MENU_PASTE); |
| 3440 | BIND_ENUM_CONSTANT(MENU_CLEAR); |
| 3441 | BIND_ENUM_CONSTANT(MENU_SELECT_ALL); |
| 3442 | BIND_ENUM_CONSTANT(MENU_UNDO); |
| 3443 | BIND_ENUM_CONSTANT(MENU_REDO); |
| 3444 | BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR); |
| 3445 | BIND_ENUM_CONSTANT(MENU_DIR_INHERITED); |
| 3446 | BIND_ENUM_CONSTANT(MENU_DIR_AUTO); |
| 3447 | BIND_ENUM_CONSTANT(MENU_DIR_LTR); |
| 3448 | BIND_ENUM_CONSTANT(MENU_DIR_RTL); |
| 3449 | BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC); |
| 3450 | BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC); |
| 3451 | BIND_ENUM_CONSTANT(MENU_INSERT_LRM); |
| 3452 | BIND_ENUM_CONSTANT(MENU_INSERT_RLM); |
| 3453 | BIND_ENUM_CONSTANT(MENU_INSERT_LRE); |
| 3454 | BIND_ENUM_CONSTANT(MENU_INSERT_RLE); |
| 3455 | BIND_ENUM_CONSTANT(MENU_INSERT_LRO); |
| 3456 | BIND_ENUM_CONSTANT(MENU_INSERT_RLO); |
| 3457 | BIND_ENUM_CONSTANT(MENU_INSERT_PDF); |
| 3458 | BIND_ENUM_CONSTANT(MENU_INSERT_ALM); |
| 3459 | BIND_ENUM_CONSTANT(MENU_INSERT_LRI); |
| 3460 | BIND_ENUM_CONSTANT(MENU_INSERT_RLI); |
| 3461 | BIND_ENUM_CONSTANT(MENU_INSERT_FSI); |
| 3462 | BIND_ENUM_CONSTANT(MENU_INSERT_PDI); |
| 3463 | BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ); |
| 3464 | BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ); |
| 3465 | BIND_ENUM_CONSTANT(MENU_INSERT_WJ); |
| 3466 | BIND_ENUM_CONSTANT(MENU_INSERT_SHY); |
| 3467 | BIND_ENUM_CONSTANT(MENU_EMOJI_AND_SYMBOL); |
| 3468 | BIND_ENUM_CONSTANT(MENU_MAX); |
| 3469 | |
| 3470 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_DEFAULT); |
| 3471 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_MULTILINE); |
| 3472 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER); |
| 3473 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER_DECIMAL); |
| 3474 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PHONE); |
| 3475 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_EMAIL_ADDRESS); |
| 3476 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PASSWORD); |
| 3477 | BIND_ENUM_CONSTANT(KEYBOARD_TYPE_URL); |
| 3478 | |
| 3479 | BIND_ENUM_CONSTANT(EXPAND_MODE_ORIGINAL_SIZE); |
| 3480 | BIND_ENUM_CONSTANT(EXPAND_MODE_FIT_TO_TEXT); |
| 3481 | BIND_ENUM_CONSTANT(EXPAND_MODE_FIT_TO_LINE_EDIT); |
| 3482 | |
| 3483 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text"); |
| 3484 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder"); |
| 3485 | ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment"); |
| 3486 | ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_max_length", "get_max_length"); |
| 3487 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable"); |
| 3488 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_editing_on_text_submit"), "set_keep_editing_on_text_submit", "is_editing_kept_on_text_submit"); |
| 3489 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length_enabled", "is_expand_to_text_length_enabled"); |
| 3490 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled"); |
| 3491 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emoji_menu_enabled"), "set_emoji_menu_enabled", "is_emoji_menu_enabled"); |
| 3492 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backspace_deletes_composite_character_enabled"), "set_backspace_deletes_composite_character_enabled", "is_backspace_deletes_composite_character_enabled"); |
| 3493 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled"); |
| 3494 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled"); |
| 3495 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled"); |
| 3496 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled"); |
| 3497 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled"); |
| 3498 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled"); |
| 3499 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat"); |
| 3500 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars"); |
| 3501 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus"); |
| 3502 | |
| 3503 | ADD_GROUP("Virtual Keyboard", "virtual_keyboard_"); |
| 3504 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled"); |
| 3505 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_show_on_focus"), "set_virtual_keyboard_show_on_focus", "get_virtual_keyboard_show_on_focus"); |
| 3506 | ADD_PROPERTY(PropertyInfo(Variant::INT, "virtual_keyboard_type", PROPERTY_HINT_ENUM, "Default,Multiline,Number,Decimal,Phone,Email,Password,URL"), "set_virtual_keyboard_type", "get_virtual_keyboard_type"); |
| 3507 | |
| 3508 | ADD_GROUP("Caret", "caret_"); |
| 3509 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled"); |
| 3510 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_interval", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "set_caret_blink_interval", "get_caret_blink_interval"); |
| 3511 | ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_column", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_caret_column", "get_caret_column"); |
| 3512 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_force_displayed"), "set_caret_force_displayed", "is_caret_force_displayed"); |
| 3513 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled"); |
| 3514 | |
| 3515 | ADD_GROUP("Secret", "secret"); |
| 3516 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret", PROPERTY_HINT_GROUP_ENABLE), "set_secret", "is_secret"); |
| 3517 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character"); |
| 3518 | |
| 3519 | ADD_GROUP("BiDi", ""); |
| 3520 | ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction"); |
| 3521 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language"); |
| 3522 | 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"); |
| 3523 | ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options"); |
| 3524 | |
| 3525 | ADD_GROUP("Icon", ""); |
| 3526 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_right_icon", "get_right_icon"); |
| 3527 | ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_expand_mode", PROPERTY_HINT_ENUM, "Original,Fit to Text,Fit to LineEdit"), "set_icon_expand_mode", "get_icon_expand_mode"); |
| 3528 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "right_icon_scale", PROPERTY_HINT_RANGE, "0.1,1.0,0.01"), "set_right_icon_scale", "get_right_icon_scale"); |
| 3529 | |
| 3530 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, normal); |
| 3531 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, read_only); |
| 3532 | BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, focus); |
| 3533 | |
| 3534 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, LineEdit, font); |
| 3535 | BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, LineEdit, font_size); |
| 3536 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_color); |
| 3537 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_uneditable_color); |
| 3538 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_selected_color); |
| 3539 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, LineEdit, font_outline_size, "outline_size"); |
| 3540 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_outline_color); |
| 3541 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_placeholder_color); |
| 3542 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LineEdit, caret_width); |
| 3543 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, caret_color); |
| 3544 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LineEdit, minimum_character_width); |
| 3545 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, selection_color); |
| 3546 | |
| 3547 | BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, LineEdit, clear_icon, "clear"); |
| 3548 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, clear_button_color); |
| 3549 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, clear_button_color_pressed); |
| 3550 | |
| 3551 | ADD_CLASS_DEPENDENCY("PopupMenu"); |
| 3552 | } |
| 3553 | |
| 3554 | LineEdit::LineEdit(const String &p_placeholder) { |
| 3555 | text_rid = TS->create_shaped_text(); |
| 3556 | _create_undo_state(); |
| 3557 | |
| 3558 | deselect(); |
| 3559 | set_focus_mode(FOCUS_ALL); |
| 3560 | set_default_cursor_shape(CURSOR_IBEAM); |
| 3561 | set_mouse_filter(MOUSE_FILTER_STOP); |
| 3562 | set_process_unhandled_key_input(true); |
| 3563 | |
| 3564 | set_caret_blink_enabled(false); |
| 3565 | |
| 3566 | set_placeholder(p_placeholder); |
| 3567 | |
| 3568 | set_editable(true); // Initialize to opposite first, so we get past the early-out in set_editable. |
| 3569 | } |
| 3570 | |
| 3571 | LineEdit::~LineEdit() { |
| 3572 | TS->free_rid(text_rid); |
| 3573 | } |