line_edit.cpp 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966
  1. /*************************************************************************/
  2. /* line_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "line_edit.h"
  31. #include "core/message_queue.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include "core/translation.h"
  36. #include "label.h"
  37. #ifdef TOOLS_ENABLED
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #endif
  41. static bool _is_text_char(CharType c) {
  42. return !is_symbol(c);
  43. }
  44. void LineEdit::_gui_input(Ref<InputEvent> p_event) {
  45. Ref<InputEventMouseButton> b = p_event;
  46. if (b.is_valid()) {
  47. if (b->is_pressed() && b->get_button_index() == BUTTON_RIGHT && context_menu_enabled) {
  48. menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  49. menu->set_size(Vector2(1, 1));
  50. menu->set_scale(get_global_transform().get_scale());
  51. menu->popup();
  52. grab_focus();
  53. accept_event();
  54. return;
  55. }
  56. if (b->get_button_index() != BUTTON_LEFT)
  57. return;
  58. _reset_caret_blink_timer();
  59. if (b->is_pressed()) {
  60. accept_event(); //don't pass event further when clicked on text field
  61. if (!text.empty() && is_editable() && _is_over_clear_button(b->get_position())) {
  62. clear_button_status.press_attempt = true;
  63. clear_button_status.pressing_inside = true;
  64. update();
  65. return;
  66. }
  67. shift_selection_check_pre(b->get_shift());
  68. set_cursor_at_pixel_pos(b->get_position().x);
  69. if (b->get_shift()) {
  70. selection_fill_at_cursor();
  71. selection.creating = true;
  72. } else {
  73. if (selecting_enabled) {
  74. if (!b->is_doubleclick() && (OS::get_singleton()->get_ticks_msec() - selection.last_dblclk) < 600) {
  75. // Triple-click select all.
  76. selection.enabled = true;
  77. selection.begin = 0;
  78. selection.end = text.length();
  79. selection.doubleclick = true;
  80. selection.last_dblclk = 0;
  81. } else if (b->is_doubleclick()) {
  82. // Double-click select word.
  83. selection.enabled = true;
  84. int beg = cursor_pos;
  85. int end = beg;
  86. bool symbol = beg < text.length() && is_symbol(text[beg]);
  87. while (beg > 0 && text[beg - 1] > 32 && (symbol == is_symbol(text[beg - 1]))) {
  88. beg--;
  89. }
  90. while (end < text.length() && text[end + 1] > 32 && (symbol == is_symbol(text[end + 1]))) {
  91. end++;
  92. }
  93. if (end < text.length()) {
  94. end += 1;
  95. }
  96. selection.begin = beg;
  97. selection.end = end;
  98. selection.doubleclick = true;
  99. selection.last_dblclk = OS::get_singleton()->get_ticks_msec();
  100. }
  101. }
  102. selection.drag_attempt = false;
  103. if ((cursor_pos < selection.begin) || (cursor_pos > selection.end) || !selection.enabled) {
  104. deselect();
  105. selection.cursor_start = cursor_pos;
  106. selection.creating = true;
  107. } else if (selection.enabled) {
  108. selection.drag_attempt = true;
  109. }
  110. }
  111. update();
  112. } else {
  113. if (!text.empty() && is_editable() && clear_button_enabled) {
  114. bool press_attempt = clear_button_status.press_attempt;
  115. clear_button_status.press_attempt = false;
  116. if (press_attempt && clear_button_status.pressing_inside && _is_over_clear_button(b->get_position())) {
  117. clear();
  118. return;
  119. }
  120. }
  121. if ((!selection.creating) && (!selection.doubleclick)) {
  122. deselect();
  123. }
  124. selection.creating = false;
  125. selection.doubleclick = false;
  126. show_virtual_keyboard();
  127. }
  128. update();
  129. }
  130. Ref<InputEventMouseMotion> m = p_event;
  131. if (m.is_valid()) {
  132. if (!text.empty() && is_editable() && clear_button_enabled) {
  133. bool last_press_inside = clear_button_status.pressing_inside;
  134. clear_button_status.pressing_inside = clear_button_status.press_attempt && _is_over_clear_button(m->get_position());
  135. if (last_press_inside != clear_button_status.pressing_inside) {
  136. update();
  137. }
  138. }
  139. if (m->get_button_mask() & BUTTON_LEFT) {
  140. if (selection.creating) {
  141. set_cursor_at_pixel_pos(m->get_position().x);
  142. selection_fill_at_cursor();
  143. }
  144. }
  145. }
  146. Ref<InputEventKey> k = p_event;
  147. if (k.is_valid()) {
  148. if (!k->is_pressed())
  149. return;
  150. #ifdef APPLE_STYLE_KEYS
  151. if (k->get_control() && !k->get_shift() && !k->get_alt() && !k->get_command()) {
  152. uint32_t remap_key = KEY_UNKNOWN;
  153. switch (k->get_scancode()) {
  154. case KEY_F: {
  155. remap_key = KEY_RIGHT;
  156. } break;
  157. case KEY_B: {
  158. remap_key = KEY_LEFT;
  159. } break;
  160. case KEY_P: {
  161. remap_key = KEY_UP;
  162. } break;
  163. case KEY_N: {
  164. remap_key = KEY_DOWN;
  165. } break;
  166. case KEY_D: {
  167. remap_key = KEY_DELETE;
  168. } break;
  169. case KEY_H: {
  170. remap_key = KEY_BACKSPACE;
  171. } break;
  172. }
  173. if (remap_key != KEY_UNKNOWN) {
  174. k->set_scancode(remap_key);
  175. k->set_control(false);
  176. }
  177. }
  178. #endif
  179. unsigned int code = k->get_scancode();
  180. if (k->get_command() && is_shortcut_keys_enabled()) {
  181. bool handled = true;
  182. switch (code) {
  183. case (KEY_X): { // CUT.
  184. if (editable) {
  185. cut_text();
  186. }
  187. } break;
  188. case (KEY_C): { // COPY.
  189. copy_text();
  190. } break;
  191. case (KEY_V): { // PASTE.
  192. if (editable) {
  193. paste_text();
  194. }
  195. } break;
  196. case (KEY_Z): { // Undo/redo.
  197. if (editable) {
  198. if (k->get_shift()) {
  199. redo();
  200. } else {
  201. undo();
  202. }
  203. }
  204. } break;
  205. case (KEY_U): { // Delete from start to cursor.
  206. if (editable) {
  207. deselect();
  208. text = text.substr(cursor_pos, text.length() - cursor_pos);
  209. update_cached_width();
  210. set_cursor_position(0);
  211. _text_changed();
  212. }
  213. } break;
  214. case (KEY_Y): { // PASTE (Yank for unix users).
  215. if (editable) {
  216. paste_text();
  217. }
  218. } break;
  219. case (KEY_K): { // Delete from cursor_pos to end.
  220. if (editable) {
  221. deselect();
  222. text = text.substr(0, cursor_pos);
  223. _text_changed();
  224. }
  225. } break;
  226. case (KEY_A): { // Select all.
  227. select();
  228. } break;
  229. #ifdef APPLE_STYLE_KEYS
  230. case (KEY_LEFT): { // Go to start of text - like HOME key.
  231. shift_selection_check_pre(k->get_shift());
  232. set_cursor_position(0);
  233. shift_selection_check_post(k->get_shift());
  234. } break;
  235. case (KEY_RIGHT): { // Go to end of text - like END key.
  236. shift_selection_check_pre(k->get_shift());
  237. set_cursor_position(text.length());
  238. shift_selection_check_post(k->get_shift());
  239. } break;
  240. case (KEY_BACKSPACE): {
  241. if (!editable)
  242. break;
  243. // If selected, delete the selection
  244. if (selection.enabled) {
  245. selection_delete();
  246. break;
  247. }
  248. // Otherwise delete from cursor to beginning of text edit
  249. int current_pos = get_cursor_position();
  250. if (current_pos != 0) {
  251. delete_text(0, current_pos);
  252. }
  253. } break;
  254. #endif
  255. default: {
  256. handled = false;
  257. }
  258. }
  259. if (handled) {
  260. accept_event();
  261. return;
  262. }
  263. }
  264. _reset_caret_blink_timer();
  265. if (!k->get_metakey()) {
  266. bool handled = true;
  267. switch (code) {
  268. case KEY_KP_ENTER:
  269. case KEY_ENTER: {
  270. emit_signal("text_entered", text);
  271. if (OS::get_singleton()->has_virtual_keyboard() && virtual_keyboard_enabled)
  272. OS::get_singleton()->hide_virtual_keyboard();
  273. } break;
  274. case KEY_BACKSPACE: {
  275. if (!editable)
  276. break;
  277. if (selection.enabled) {
  278. selection_delete();
  279. break;
  280. }
  281. #ifdef APPLE_STYLE_KEYS
  282. if (k->get_alt()) {
  283. #else
  284. if (k->get_alt()) {
  285. handled = false;
  286. break;
  287. } else if (k->get_command()) {
  288. #endif
  289. int cc = cursor_pos;
  290. bool prev_char = false;
  291. while (cc > 0) {
  292. bool ischar = _is_text_char(text[cc - 1]);
  293. if (prev_char && !ischar)
  294. break;
  295. prev_char = ischar;
  296. cc--;
  297. }
  298. delete_text(cc, cursor_pos);
  299. set_cursor_position(cc);
  300. } else {
  301. delete_char();
  302. }
  303. } break;
  304. case KEY_KP_4: {
  305. if (k->get_unicode() != 0) {
  306. handled = false;
  307. break;
  308. }
  309. FALLTHROUGH;
  310. }
  311. case KEY_LEFT: {
  312. #ifndef APPLE_STYLE_KEYS
  313. if (!k->get_alt())
  314. #endif
  315. shift_selection_check_pre(k->get_shift());
  316. #ifdef APPLE_STYLE_KEYS
  317. if (k->get_command()) {
  318. set_cursor_position(0);
  319. } else if (k->get_alt()) {
  320. #else
  321. if (k->get_alt()) {
  322. handled = false;
  323. break;
  324. } else if (k->get_command()) {
  325. #endif
  326. bool prev_char = false;
  327. int cc = cursor_pos;
  328. while (cc > 0) {
  329. bool ischar = _is_text_char(text[cc - 1]);
  330. if (prev_char && !ischar)
  331. break;
  332. prev_char = ischar;
  333. cc--;
  334. }
  335. set_cursor_position(cc);
  336. } else {
  337. set_cursor_position(get_cursor_position() - 1);
  338. }
  339. shift_selection_check_post(k->get_shift());
  340. } break;
  341. case KEY_KP_6: {
  342. if (k->get_unicode() != 0) {
  343. handled = false;
  344. break;
  345. }
  346. FALLTHROUGH;
  347. }
  348. case KEY_RIGHT: {
  349. shift_selection_check_pre(k->get_shift());
  350. #ifdef APPLE_STYLE_KEYS
  351. if (k->get_command()) {
  352. set_cursor_position(text.length());
  353. } else if (k->get_alt()) {
  354. #else
  355. if (k->get_alt()) {
  356. handled = false;
  357. break;
  358. } else if (k->get_command()) {
  359. #endif
  360. bool prev_char = false;
  361. int cc = cursor_pos;
  362. while (cc < text.length()) {
  363. bool ischar = _is_text_char(text[cc]);
  364. if (prev_char && !ischar)
  365. break;
  366. prev_char = ischar;
  367. cc++;
  368. }
  369. set_cursor_position(cc);
  370. } else {
  371. set_cursor_position(get_cursor_position() + 1);
  372. }
  373. shift_selection_check_post(k->get_shift());
  374. } break;
  375. case KEY_UP: {
  376. shift_selection_check_pre(k->get_shift());
  377. if (get_cursor_position() == 0) handled = false;
  378. set_cursor_position(0);
  379. shift_selection_check_post(k->get_shift());
  380. } break;
  381. case KEY_DOWN: {
  382. shift_selection_check_pre(k->get_shift());
  383. if (get_cursor_position() == text.length()) handled = false;
  384. set_cursor_position(text.length());
  385. shift_selection_check_post(k->get_shift());
  386. } break;
  387. case KEY_DELETE: {
  388. if (!editable)
  389. break;
  390. if (k->get_shift() && !k->get_command() && !k->get_alt()) {
  391. cut_text();
  392. break;
  393. }
  394. if (selection.enabled) {
  395. selection_delete();
  396. break;
  397. }
  398. int text_len = text.length();
  399. if (cursor_pos == text_len)
  400. break; // Nothing to do.
  401. #ifdef APPLE_STYLE_KEYS
  402. if (k->get_alt()) {
  403. #else
  404. if (k->get_alt()) {
  405. handled = false;
  406. break;
  407. } else if (k->get_command()) {
  408. #endif
  409. int cc = cursor_pos;
  410. bool prev_char = false;
  411. while (cc < text.length()) {
  412. bool ischar = _is_text_char(text[cc]);
  413. if (prev_char && !ischar)
  414. break;
  415. prev_char = ischar;
  416. cc++;
  417. }
  418. delete_text(cursor_pos, cc);
  419. } else {
  420. set_cursor_position(cursor_pos + 1);
  421. delete_char();
  422. }
  423. } break;
  424. case KEY_KP_7: {
  425. if (k->get_unicode() != 0) {
  426. handled = false;
  427. break;
  428. }
  429. FALLTHROUGH;
  430. }
  431. case KEY_HOME: {
  432. shift_selection_check_pre(k->get_shift());
  433. set_cursor_position(0);
  434. shift_selection_check_post(k->get_shift());
  435. } break;
  436. case KEY_KP_1: {
  437. if (k->get_unicode() != 0) {
  438. handled = false;
  439. break;
  440. }
  441. FALLTHROUGH;
  442. }
  443. case KEY_END: {
  444. shift_selection_check_pre(k->get_shift());
  445. set_cursor_position(text.length());
  446. shift_selection_check_post(k->get_shift());
  447. } break;
  448. case KEY_MENU: {
  449. if (context_menu_enabled) {
  450. Point2 pos = Point2(get_cursor_pixel_pos(), (get_size().y + get_font("font")->get_height()) / 2);
  451. menu->set_position(get_global_transform().xform(pos));
  452. menu->set_size(Vector2(1, 1));
  453. menu->set_scale(get_global_transform().get_scale());
  454. menu->popup();
  455. menu->grab_focus();
  456. }
  457. } break;
  458. default: {
  459. handled = false;
  460. } break;
  461. }
  462. if (handled) {
  463. accept_event();
  464. } else if (!k->get_command()) {
  465. if (k->get_unicode() >= 32 && k->get_scancode() != KEY_DELETE) {
  466. if (editable) {
  467. selection_delete();
  468. CharType ucodestr[2] = { (CharType)k->get_unicode(), 0 };
  469. int prev_len = text.length();
  470. append_at_cursor(ucodestr);
  471. if (text.length() != prev_len) {
  472. _text_changed();
  473. }
  474. accept_event();
  475. }
  476. } else {
  477. return;
  478. }
  479. }
  480. update();
  481. }
  482. return;
  483. }
  484. }
  485. void LineEdit::set_align(Align p_align) {
  486. ERR_FAIL_INDEX((int)p_align, 4);
  487. align = p_align;
  488. update();
  489. }
  490. LineEdit::Align LineEdit::get_align() const {
  491. return align;
  492. }
  493. Variant LineEdit::get_drag_data(const Point2 &p_point) {
  494. if (selection.drag_attempt && selection.enabled) {
  495. String t = text.substr(selection.begin, selection.end - selection.begin);
  496. Label *l = memnew(Label);
  497. l->set_text(t);
  498. set_drag_preview(l);
  499. return t;
  500. }
  501. return Variant();
  502. }
  503. bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  504. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  505. if (drop_override) {
  506. return drop_override;
  507. }
  508. return p_data.get_type() == Variant::STRING;
  509. }
  510. void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  511. Control::drop_data(p_point, p_data);
  512. if (p_data.get_type() == Variant::STRING) {
  513. set_cursor_at_pixel_pos(p_point.x);
  514. int selected = selection.end - selection.begin;
  515. Ref<Font> font = get_font("font");
  516. if (font != NULL) {
  517. for (int i = selection.begin; i < selection.end; i++)
  518. cached_width -= font->get_char_size(pass ? secret_character[0] : text[i]).width;
  519. }
  520. text.erase(selection.begin, selected);
  521. append_at_cursor(p_data);
  522. selection.begin = cursor_pos - selected;
  523. selection.end = cursor_pos;
  524. }
  525. }
  526. Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const {
  527. if (!text.empty() && is_editable() && _is_over_clear_button(p_pos)) {
  528. return CURSOR_ARROW;
  529. }
  530. return Control::get_cursor_shape(p_pos);
  531. }
  532. bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const {
  533. if (!clear_button_enabled || !has_point(p_pos)) {
  534. return false;
  535. }
  536. Ref<Texture> icon = Control::get_icon("clear");
  537. int x_ofs = get_stylebox("normal")->get_offset().x;
  538. return p_pos.x > get_size().width - icon->get_width() - x_ofs;
  539. }
  540. void LineEdit::_notification(int p_what) {
  541. switch (p_what) {
  542. #ifdef TOOLS_ENABLED
  543. case NOTIFICATION_ENTER_TREE: {
  544. if (Engine::get_singleton()->is_editor_hint() && !get_tree()->is_node_being_edited(this)) {
  545. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  546. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  547. if (!EditorSettings::get_singleton()->is_connected("settings_changed", this, "_editor_settings_changed")) {
  548. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  549. }
  550. }
  551. } break;
  552. #endif
  553. case NOTIFICATION_RESIZED: {
  554. scroll_offset = 0;
  555. set_cursor_position(get_cursor_position());
  556. } break;
  557. case NOTIFICATION_TRANSLATION_CHANGED: {
  558. placeholder_translated = tr(placeholder);
  559. update_placeholder_width();
  560. update();
  561. } break;
  562. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  563. window_has_focus = true;
  564. draw_caret = true;
  565. update();
  566. } break;
  567. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  568. window_has_focus = false;
  569. draw_caret = false;
  570. update();
  571. } break;
  572. case NOTIFICATION_DRAW: {
  573. if ((!has_focus() && !menu->has_focus()) || !window_has_focus) {
  574. draw_caret = false;
  575. }
  576. int width, height;
  577. Size2 size = get_size();
  578. width = size.width;
  579. height = size.height;
  580. RID ci = get_canvas_item();
  581. Ref<StyleBox> style = get_stylebox("normal");
  582. if (!is_editable()) {
  583. style = get_stylebox("read_only");
  584. draw_caret = false;
  585. }
  586. Ref<Font> font = get_font("font");
  587. style->draw(ci, Rect2(Point2(), size));
  588. if (has_focus()) {
  589. get_stylebox("focus")->draw(ci, Rect2(Point2(), size));
  590. }
  591. int x_ofs = 0;
  592. bool using_placeholder = text.empty() && ime_text.empty();
  593. int cached_text_width = using_placeholder ? cached_placeholder_width : cached_width;
  594. switch (align) {
  595. case ALIGN_FILL:
  596. case ALIGN_LEFT: {
  597. x_ofs = style->get_offset().x;
  598. } break;
  599. case ALIGN_CENTER: {
  600. if (scroll_offset != 0)
  601. x_ofs = style->get_offset().x;
  602. else
  603. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - (cached_text_width)) / 2);
  604. } break;
  605. case ALIGN_RIGHT: {
  606. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - style->get_margin(MARGIN_RIGHT) - (cached_text_width)));
  607. } break;
  608. }
  609. int ofs_max = width - style->get_margin(MARGIN_RIGHT);
  610. int char_ofs = scroll_offset;
  611. int y_area = height - style->get_minimum_size().height;
  612. int y_ofs = style->get_offset().y + (y_area - font->get_height()) / 2;
  613. int font_ascent = font->get_ascent();
  614. Color selection_color = get_color("selection_color");
  615. Color font_color = is_editable() ? get_color("font_color") : get_color("font_color_uneditable");
  616. Color font_color_selected = get_color("font_color_selected");
  617. Color cursor_color = get_color("cursor_color");
  618. const String &t = using_placeholder ? placeholder_translated : text;
  619. // Draw placeholder color.
  620. if (using_placeholder)
  621. font_color.a *= placeholder_alpha;
  622. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  623. if (right_icon.is_valid() || display_clear_icon) {
  624. Ref<Texture> r_icon = display_clear_icon ? Control::get_icon("clear") : right_icon;
  625. Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9);
  626. if (display_clear_icon) {
  627. if (clear_button_status.press_attempt && clear_button_status.pressing_inside) {
  628. color_icon = get_color("clear_button_color_pressed");
  629. } else {
  630. color_icon = get_color("clear_button_color");
  631. }
  632. }
  633. r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
  634. if (align == ALIGN_CENTER) {
  635. if (scroll_offset == 0) {
  636. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - cached_text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2);
  637. }
  638. } else {
  639. x_ofs = MAX(style->get_margin(MARGIN_LEFT), x_ofs - r_icon->get_width() - style->get_margin(MARGIN_RIGHT));
  640. }
  641. ofs_max -= r_icon->get_width();
  642. }
  643. int caret_height = font->get_height() > y_area ? y_area : font->get_height();
  644. FontDrawer drawer(font, Color(1, 1, 1));
  645. while (true) {
  646. // End of string, break.
  647. if (char_ofs >= t.length())
  648. break;
  649. if (char_ofs == cursor_pos) {
  650. if (ime_text.length() > 0) {
  651. int ofs = 0;
  652. while (true) {
  653. if (ofs >= ime_text.length())
  654. break;
  655. CharType cchar = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs];
  656. CharType next = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs + 1];
  657. int im_char_width = font->get_char_size(cchar, next).width;
  658. if ((x_ofs + im_char_width) > ofs_max)
  659. break;
  660. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  661. if (selected) {
  662. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 3)), font_color);
  663. } else {
  664. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 1)), font_color);
  665. }
  666. drawer.draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, font_color);
  667. x_ofs += im_char_width;
  668. ofs++;
  669. }
  670. }
  671. }
  672. CharType cchar = (pass && !text.empty()) ? secret_character[0] : t[char_ofs];
  673. CharType next = (pass && !text.empty()) ? secret_character[0] : t[char_ofs + 1];
  674. int char_width = font->get_char_size(cchar, next).width;
  675. // End of widget, break.
  676. if ((x_ofs + char_width) > ofs_max)
  677. break;
  678. bool selected = selection.enabled && char_ofs >= selection.begin && char_ofs < selection.end;
  679. if (selected)
  680. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(char_width, caret_height)), selection_color);
  681. int yofs = y_ofs + (caret_height - font->get_height()) / 2;
  682. drawer.draw_char(ci, Point2(x_ofs, yofs + font_ascent), cchar, next, selected ? font_color_selected : font_color);
  683. if (char_ofs == cursor_pos && draw_caret && !using_placeholder) {
  684. if (ime_text.length() == 0) {
  685. #ifdef TOOLS_ENABLED
  686. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(Math::round(EDSCALE), caret_height)), cursor_color);
  687. #else
  688. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(1, caret_height)), cursor_color);
  689. #endif
  690. }
  691. }
  692. x_ofs += char_width;
  693. char_ofs++;
  694. }
  695. if (char_ofs == cursor_pos) {
  696. if (ime_text.length() > 0) {
  697. int ofs = 0;
  698. while (true) {
  699. if (ofs >= ime_text.length())
  700. break;
  701. CharType cchar = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs];
  702. CharType next = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs + 1];
  703. int im_char_width = font->get_char_size(cchar, next).width;
  704. if ((x_ofs + im_char_width) > ofs_max)
  705. break;
  706. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  707. if (selected) {
  708. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 3)), font_color);
  709. } else {
  710. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 1)), font_color);
  711. }
  712. drawer.draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, font_color);
  713. x_ofs += im_char_width;
  714. ofs++;
  715. }
  716. }
  717. }
  718. if ((char_ofs == cursor_pos || using_placeholder) && draw_caret) { // May be at the end, or placeholder.
  719. if (ime_text.length() == 0) {
  720. int caret_x_ofs = x_ofs;
  721. if (using_placeholder) {
  722. switch (align) {
  723. case ALIGN_LEFT:
  724. case ALIGN_FILL: {
  725. caret_x_ofs = style->get_offset().x;
  726. } break;
  727. case ALIGN_CENTER: {
  728. caret_x_ofs = ofs_max / 2;
  729. } break;
  730. case ALIGN_RIGHT: {
  731. caret_x_ofs = ofs_max;
  732. } break;
  733. }
  734. }
  735. #ifdef TOOLS_ENABLED
  736. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(caret_x_ofs, y_ofs), Size2(Math::round(EDSCALE), caret_height)), cursor_color);
  737. #else
  738. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(caret_x_ofs, y_ofs), Size2(1, caret_height)), cursor_color);
  739. #endif
  740. }
  741. }
  742. if (has_focus()) {
  743. OS::get_singleton()->set_ime_active(true);
  744. OS::get_singleton()->set_ime_position(get_global_position() + Point2(using_placeholder ? 0 : x_ofs, y_ofs + caret_height));
  745. }
  746. } break;
  747. case NOTIFICATION_FOCUS_ENTER: {
  748. if (caret_blink_enabled) {
  749. caret_blink_timer->start();
  750. } else {
  751. draw_caret = true;
  752. }
  753. {
  754. OS::get_singleton()->set_ime_active(true);
  755. Point2 cursor_pos2 = Point2(get_cursor_position(), 1) * get_minimum_size().height;
  756. OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos2);
  757. }
  758. show_virtual_keyboard();
  759. } break;
  760. case NOTIFICATION_FOCUS_EXIT: {
  761. if (caret_blink_enabled) {
  762. caret_blink_timer->stop();
  763. }
  764. OS::get_singleton()->set_ime_position(Point2());
  765. OS::get_singleton()->set_ime_active(false);
  766. ime_text = "";
  767. ime_selection = Point2();
  768. if (OS::get_singleton()->has_virtual_keyboard() && virtual_keyboard_enabled)
  769. OS::get_singleton()->hide_virtual_keyboard();
  770. } break;
  771. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  772. if (has_focus()) {
  773. ime_text = OS::get_singleton()->get_ime_text();
  774. ime_selection = OS::get_singleton()->get_ime_selection();
  775. update();
  776. }
  777. } break;
  778. }
  779. }
  780. void LineEdit::copy_text() {
  781. if (selection.enabled && !pass) {
  782. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  783. }
  784. }
  785. void LineEdit::cut_text() {
  786. if (selection.enabled && !pass) {
  787. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  788. selection_delete();
  789. }
  790. }
  791. void LineEdit::paste_text() {
  792. // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
  793. String paste_buffer = OS::get_singleton()->get_clipboard().strip_escapes();
  794. if (paste_buffer != "") {
  795. int prev_len = text.length();
  796. if (selection.enabled) selection_delete();
  797. append_at_cursor(paste_buffer);
  798. if (!text_changed_dirty) {
  799. if (is_inside_tree() && text.length() != prev_len) {
  800. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  801. }
  802. text_changed_dirty = true;
  803. }
  804. }
  805. }
  806. void LineEdit::undo() {
  807. if (undo_stack_pos == NULL) {
  808. if (undo_stack.size() <= 1) {
  809. return;
  810. }
  811. undo_stack_pos = undo_stack.back();
  812. } else if (undo_stack_pos == undo_stack.front()) {
  813. return;
  814. }
  815. undo_stack_pos = undo_stack_pos->prev();
  816. TextOperation op = undo_stack_pos->get();
  817. text = op.text;
  818. cached_width = op.cached_width;
  819. scroll_offset = op.scroll_offset;
  820. set_cursor_position(op.cursor_pos);
  821. if (expand_to_text_length)
  822. minimum_size_changed();
  823. _emit_text_change();
  824. }
  825. void LineEdit::redo() {
  826. if (undo_stack_pos == NULL) {
  827. return;
  828. }
  829. if (undo_stack_pos == undo_stack.back()) {
  830. return;
  831. }
  832. undo_stack_pos = undo_stack_pos->next();
  833. TextOperation op = undo_stack_pos->get();
  834. text = op.text;
  835. cached_width = op.cached_width;
  836. scroll_offset = op.scroll_offset;
  837. set_cursor_position(op.cursor_pos);
  838. if (expand_to_text_length)
  839. minimum_size_changed();
  840. _emit_text_change();
  841. }
  842. void LineEdit::shift_selection_check_pre(bool p_shift) {
  843. if (!selection.enabled && p_shift) {
  844. selection.cursor_start = cursor_pos;
  845. }
  846. if (!p_shift)
  847. deselect();
  848. }
  849. void LineEdit::shift_selection_check_post(bool p_shift) {
  850. if (p_shift)
  851. selection_fill_at_cursor();
  852. }
  853. void LineEdit::set_cursor_at_pixel_pos(int p_x) {
  854. Ref<Font> font = get_font("font");
  855. int ofs = scroll_offset;
  856. Ref<StyleBox> style = get_stylebox("normal");
  857. int pixel_ofs = 0;
  858. Size2 size = get_size();
  859. bool display_clear_icon = !text.empty() && is_editable() && clear_button_enabled;
  860. int r_icon_width = Control::get_icon("clear")->get_width();
  861. switch (align) {
  862. case ALIGN_FILL:
  863. case ALIGN_LEFT: {
  864. pixel_ofs = int(style->get_offset().x);
  865. } break;
  866. case ALIGN_CENTER: {
  867. if (scroll_offset != 0)
  868. pixel_ofs = int(style->get_offset().x);
  869. else
  870. pixel_ofs = int(size.width - (cached_width)) / 2;
  871. if (display_clear_icon)
  872. pixel_ofs -= int(r_icon_width / 2 + style->get_margin(MARGIN_RIGHT));
  873. } break;
  874. case ALIGN_RIGHT: {
  875. pixel_ofs = int(size.width - style->get_margin(MARGIN_RIGHT) - (cached_width));
  876. if (display_clear_icon)
  877. pixel_ofs -= int(r_icon_width + style->get_margin(MARGIN_RIGHT));
  878. } break;
  879. }
  880. while (ofs < text.length()) {
  881. int char_w = 0;
  882. if (font != NULL) {
  883. char_w = font->get_char_size(pass ? secret_character[0] : text[ofs]).width;
  884. }
  885. pixel_ofs += char_w;
  886. if (pixel_ofs > p_x) { // Found what we look for.
  887. break;
  888. }
  889. ofs++;
  890. }
  891. set_cursor_position(ofs);
  892. }
  893. int LineEdit::get_cursor_pixel_pos() {
  894. Ref<Font> font = get_font("font");
  895. int ofs = scroll_offset;
  896. Ref<StyleBox> style = get_stylebox("normal");
  897. int pixel_ofs = 0;
  898. Size2 size = get_size();
  899. bool display_clear_icon = !text.empty() && is_editable() && clear_button_enabled;
  900. int r_icon_width = Control::get_icon("clear")->get_width();
  901. switch (align) {
  902. case ALIGN_FILL:
  903. case ALIGN_LEFT: {
  904. pixel_ofs = int(style->get_offset().x);
  905. } break;
  906. case ALIGN_CENTER: {
  907. if (scroll_offset != 0)
  908. pixel_ofs = int(style->get_offset().x);
  909. else
  910. pixel_ofs = int(size.width - (cached_width)) / 2;
  911. if (display_clear_icon)
  912. pixel_ofs -= int(r_icon_width / 2 + style->get_margin(MARGIN_RIGHT));
  913. } break;
  914. case ALIGN_RIGHT: {
  915. pixel_ofs = int(size.width - style->get_margin(MARGIN_RIGHT) - (cached_width));
  916. if (display_clear_icon)
  917. pixel_ofs -= int(r_icon_width + style->get_margin(MARGIN_RIGHT));
  918. } break;
  919. }
  920. while (ofs < cursor_pos) {
  921. if (font != NULL) {
  922. pixel_ofs += font->get_char_size(pass ? secret_character[0] : text[ofs]).width;
  923. }
  924. ofs++;
  925. }
  926. return pixel_ofs;
  927. }
  928. bool LineEdit::cursor_get_blink_enabled() const {
  929. return caret_blink_enabled;
  930. }
  931. void LineEdit::cursor_set_blink_enabled(const bool p_enabled) {
  932. caret_blink_enabled = p_enabled;
  933. if (has_focus()) {
  934. if (p_enabled) {
  935. caret_blink_timer->start();
  936. } else {
  937. caret_blink_timer->stop();
  938. }
  939. }
  940. draw_caret = true;
  941. }
  942. float LineEdit::cursor_get_blink_speed() const {
  943. return caret_blink_timer->get_wait_time();
  944. }
  945. void LineEdit::cursor_set_blink_speed(const float p_speed) {
  946. ERR_FAIL_COND(p_speed <= 0);
  947. caret_blink_timer->set_wait_time(p_speed);
  948. }
  949. void LineEdit::_reset_caret_blink_timer() {
  950. if (caret_blink_enabled) {
  951. draw_caret = true;
  952. if (has_focus()) {
  953. caret_blink_timer->stop();
  954. caret_blink_timer->start();
  955. update();
  956. }
  957. }
  958. }
  959. void LineEdit::_toggle_draw_caret() {
  960. draw_caret = !draw_caret;
  961. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  962. update();
  963. }
  964. }
  965. void LineEdit::delete_char() {
  966. if ((text.length() <= 0) || (cursor_pos == 0)) return;
  967. Ref<Font> font = get_font("font");
  968. if (font != NULL) {
  969. cached_width -= font->get_char_size(pass ? secret_character[0] : text[cursor_pos - 1]).width;
  970. }
  971. text.erase(cursor_pos - 1, 1);
  972. set_cursor_position(get_cursor_position() - 1);
  973. if (align == ALIGN_CENTER || align == ALIGN_RIGHT) {
  974. scroll_offset = CLAMP(scroll_offset - 1, 0, MAX(text.length() - 1, 0));
  975. }
  976. _text_changed();
  977. }
  978. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  979. ERR_FAIL_COND_MSG(p_from_column < 0 || p_from_column > p_to_column || p_to_column > text.length(),
  980. vformat("Positional parameters (from: %d, to: %d) are inverted or outside the text length (%d).", p_from_column, p_to_column, text.length()));
  981. if (text.size() > 0) {
  982. Ref<Font> font = get_font("font");
  983. if (font != NULL) {
  984. for (int i = p_from_column; i < p_to_column; i++)
  985. cached_width -= font->get_char_size(pass ? secret_character[0] : text[i]).width;
  986. }
  987. } else {
  988. cached_width = 0;
  989. }
  990. text.erase(p_from_column, p_to_column - p_from_column);
  991. cursor_pos -= CLAMP(cursor_pos - p_from_column, 0, p_to_column - p_from_column);
  992. if (cursor_pos >= text.length()) {
  993. cursor_pos = text.length();
  994. }
  995. if (scroll_offset > cursor_pos) {
  996. scroll_offset = cursor_pos;
  997. }
  998. if (align == ALIGN_CENTER || align == ALIGN_RIGHT) {
  999. scroll_offset = CLAMP(scroll_offset - (p_to_column - p_from_column), 0, MAX(text.length() - 1, 0));
  1000. }
  1001. if (!text_changed_dirty) {
  1002. if (is_inside_tree()) {
  1003. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  1004. }
  1005. text_changed_dirty = true;
  1006. }
  1007. }
  1008. void LineEdit::set_text(String p_text) {
  1009. clear_internal();
  1010. append_at_cursor(p_text);
  1011. _create_undo_state();
  1012. if (expand_to_text_length) {
  1013. minimum_size_changed();
  1014. }
  1015. update();
  1016. cursor_pos = 0;
  1017. scroll_offset = 0;
  1018. }
  1019. void LineEdit::clear() {
  1020. clear_internal();
  1021. _text_changed();
  1022. // This should reset virtual keyboard state if needed.
  1023. if (has_focus()) {
  1024. show_virtual_keyboard();
  1025. }
  1026. }
  1027. void LineEdit::show_virtual_keyboard() {
  1028. if (OS::get_singleton()->has_virtual_keyboard() && virtual_keyboard_enabled) {
  1029. if (selection.enabled) {
  1030. OS::get_singleton()->show_virtual_keyboard(text, get_global_rect(), false, max_length, selection.begin, selection.end);
  1031. } else {
  1032. OS::get_singleton()->show_virtual_keyboard(text, get_global_rect(), false, max_length, cursor_pos);
  1033. }
  1034. }
  1035. }
  1036. String LineEdit::get_text() const {
  1037. return text;
  1038. }
  1039. void LineEdit::set_placeholder(String p_text) {
  1040. placeholder = p_text;
  1041. placeholder_translated = tr(placeholder);
  1042. update_placeholder_width();
  1043. update();
  1044. }
  1045. String LineEdit::get_placeholder() const {
  1046. return placeholder;
  1047. }
  1048. void LineEdit::set_placeholder_alpha(float p_alpha) {
  1049. placeholder_alpha = p_alpha;
  1050. update();
  1051. }
  1052. float LineEdit::get_placeholder_alpha() const {
  1053. return placeholder_alpha;
  1054. }
  1055. void LineEdit::set_cursor_position(int p_pos) {
  1056. if (p_pos > (int)text.length())
  1057. p_pos = text.length();
  1058. if (p_pos < 0)
  1059. p_pos = 0;
  1060. cursor_pos = p_pos;
  1061. if (!is_inside_tree()) {
  1062. scroll_offset = cursor_pos;
  1063. return;
  1064. }
  1065. Ref<StyleBox> style = get_stylebox("normal");
  1066. Ref<Font> font = get_font("font");
  1067. if (cursor_pos <= scroll_offset) {
  1068. // Adjust window if cursor goes too much to the left.
  1069. set_scroll_offset(MAX(0, cursor_pos - 1));
  1070. } else {
  1071. // Adjust window if cursor goes too much to the right.
  1072. int window_width = get_size().width - style->get_minimum_size().width;
  1073. bool display_clear_icon = !text.empty() && is_editable() && clear_button_enabled;
  1074. if (right_icon.is_valid() || display_clear_icon) {
  1075. Ref<Texture> r_icon = display_clear_icon ? Control::get_icon("clear") : right_icon;
  1076. window_width -= r_icon->get_width();
  1077. }
  1078. if (window_width < 0)
  1079. return;
  1080. int wp = scroll_offset;
  1081. if (font.is_valid()) {
  1082. int accum_width = 0;
  1083. for (int i = cursor_pos; i >= scroll_offset; i--) {
  1084. if (i >= text.length()) {
  1085. // Do not do this, because if the cursor is at the end, its just fine that it takes no space.
  1086. // accum_width = font->get_char_size(' ').width;
  1087. } else {
  1088. if (pass) {
  1089. accum_width += font->get_char_size(secret_character[0], i + 1 < text.length() ? secret_character[0] : 0).width;
  1090. } else {
  1091. accum_width += font->get_char_size(text[i], i + 1 < text.length() ? text[i + 1] : 0).width; // Anything should do.
  1092. }
  1093. }
  1094. if (accum_width > window_width)
  1095. break;
  1096. wp = i;
  1097. }
  1098. }
  1099. if (wp != scroll_offset) {
  1100. set_scroll_offset(wp);
  1101. }
  1102. }
  1103. update();
  1104. }
  1105. int LineEdit::get_cursor_position() const {
  1106. return cursor_pos;
  1107. }
  1108. void LineEdit::set_scroll_offset(int p_pos) {
  1109. scroll_offset = p_pos;
  1110. if (scroll_offset < 0) {
  1111. scroll_offset = 0;
  1112. }
  1113. }
  1114. int LineEdit::get_scroll_offset() const {
  1115. return scroll_offset;
  1116. }
  1117. void LineEdit::append_at_cursor(String p_text) {
  1118. if ((max_length <= 0) || (text.length() + p_text.length() <= max_length)) {
  1119. String pre = text.substr(0, cursor_pos);
  1120. String post = text.substr(cursor_pos, text.length() - cursor_pos);
  1121. text = pre + p_text + post;
  1122. update_cached_width();
  1123. set_cursor_position(cursor_pos + p_text.length());
  1124. } else {
  1125. emit_signal("text_change_rejected");
  1126. }
  1127. }
  1128. void LineEdit::clear_internal() {
  1129. deselect();
  1130. _clear_undo_stack();
  1131. cached_width = 0;
  1132. cursor_pos = 0;
  1133. scroll_offset = 0;
  1134. undo_text = "";
  1135. text = "";
  1136. update();
  1137. }
  1138. Size2 LineEdit::get_minimum_size() const {
  1139. Ref<StyleBox> style = get_stylebox("normal");
  1140. Ref<Font> font = get_font("font");
  1141. Size2 min_size;
  1142. // Minimum size of text.
  1143. int space_size = font->get_char_size(' ').x;
  1144. min_size.width = get_constant("minimum_spaces") * space_size;
  1145. if (expand_to_text_length) {
  1146. // Add a space because some fonts are too exact, and because cursor needs a bit more when at the end.
  1147. min_size.width = MAX(min_size.width, font->get_string_size(text).x + space_size);
  1148. }
  1149. min_size.height = font->get_height();
  1150. // Take icons into account.
  1151. if (!text.empty() && is_editable() && clear_button_enabled) {
  1152. min_size.width = MAX(min_size.width, Control::get_icon("clear")->get_width());
  1153. min_size.height = MAX(min_size.height, Control::get_icon("clear")->get_height());
  1154. }
  1155. if (right_icon.is_valid()) {
  1156. min_size.width = MAX(min_size.width, right_icon->get_width());
  1157. min_size.height = MAX(min_size.height, right_icon->get_height());
  1158. }
  1159. return style->get_minimum_size() + min_size;
  1160. }
  1161. void LineEdit::deselect() {
  1162. selection.begin = 0;
  1163. selection.end = 0;
  1164. selection.cursor_start = 0;
  1165. selection.enabled = false;
  1166. selection.creating = false;
  1167. selection.doubleclick = false;
  1168. update();
  1169. }
  1170. void LineEdit::selection_delete() {
  1171. if (selection.enabled)
  1172. delete_text(selection.begin, selection.end);
  1173. deselect();
  1174. }
  1175. void LineEdit::set_max_length(int p_max_length) {
  1176. ERR_FAIL_COND(p_max_length < 0);
  1177. max_length = p_max_length;
  1178. set_text(text);
  1179. }
  1180. int LineEdit::get_max_length() const {
  1181. return max_length;
  1182. }
  1183. void LineEdit::selection_fill_at_cursor() {
  1184. if (!selecting_enabled)
  1185. return;
  1186. selection.begin = cursor_pos;
  1187. selection.end = selection.cursor_start;
  1188. if (selection.end < selection.begin) {
  1189. int aux = selection.end;
  1190. selection.end = selection.begin;
  1191. selection.begin = aux;
  1192. }
  1193. selection.enabled = (selection.begin != selection.end);
  1194. }
  1195. void LineEdit::select_all() {
  1196. if (!selecting_enabled)
  1197. return;
  1198. if (!text.length())
  1199. return;
  1200. selection.begin = 0;
  1201. selection.end = text.length();
  1202. selection.enabled = true;
  1203. update();
  1204. }
  1205. void LineEdit::set_editable(bool p_editable) {
  1206. if (editable == p_editable)
  1207. return;
  1208. editable = p_editable;
  1209. _generate_context_menu();
  1210. minimum_size_changed();
  1211. update();
  1212. }
  1213. bool LineEdit::is_editable() const {
  1214. return editable;
  1215. }
  1216. void LineEdit::set_secret(bool p_secret) {
  1217. pass = p_secret;
  1218. update_cached_width();
  1219. update();
  1220. }
  1221. bool LineEdit::is_secret() const {
  1222. return pass;
  1223. }
  1224. void LineEdit::set_secret_character(const String &p_string) {
  1225. // An empty string as the secret character would crash the engine.
  1226. // It also wouldn't make sense to use multiple characters as the secret character.
  1227. ERR_FAIL_COND_MSG(p_string.length() != 1, "Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given).");
  1228. secret_character = p_string;
  1229. update_cached_width();
  1230. update();
  1231. }
  1232. String LineEdit::get_secret_character() const {
  1233. return secret_character;
  1234. }
  1235. void LineEdit::select(int p_from, int p_to) {
  1236. if (!selecting_enabled)
  1237. return;
  1238. if (p_from == 0 && p_to == 0) {
  1239. deselect();
  1240. return;
  1241. }
  1242. int len = text.length();
  1243. if (p_from < 0)
  1244. p_from = 0;
  1245. if (p_from > len)
  1246. p_from = len;
  1247. if (p_to < 0 || p_to > len)
  1248. p_to = len;
  1249. if (p_from >= p_to)
  1250. return;
  1251. selection.enabled = true;
  1252. selection.begin = p_from;
  1253. selection.end = p_to;
  1254. selection.creating = false;
  1255. selection.doubleclick = false;
  1256. update();
  1257. }
  1258. bool LineEdit::is_text_field() const {
  1259. return true;
  1260. }
  1261. void LineEdit::menu_option(int p_option) {
  1262. switch (p_option) {
  1263. case MENU_CUT: {
  1264. if (editable) {
  1265. cut_text();
  1266. }
  1267. } break;
  1268. case MENU_COPY: {
  1269. copy_text();
  1270. } break;
  1271. case MENU_PASTE: {
  1272. if (editable) {
  1273. paste_text();
  1274. }
  1275. } break;
  1276. case MENU_CLEAR: {
  1277. if (editable) {
  1278. clear();
  1279. }
  1280. } break;
  1281. case MENU_SELECT_ALL: {
  1282. select_all();
  1283. } break;
  1284. case MENU_UNDO: {
  1285. if (editable) {
  1286. undo();
  1287. }
  1288. } break;
  1289. case MENU_REDO: {
  1290. if (editable) {
  1291. redo();
  1292. }
  1293. }
  1294. }
  1295. }
  1296. void LineEdit::set_context_menu_enabled(bool p_enable) {
  1297. context_menu_enabled = p_enable;
  1298. }
  1299. bool LineEdit::is_context_menu_enabled() {
  1300. return context_menu_enabled;
  1301. }
  1302. PopupMenu *LineEdit::get_menu() const {
  1303. return menu;
  1304. }
  1305. void LineEdit::_editor_settings_changed() {
  1306. #ifdef TOOLS_ENABLED
  1307. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  1308. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  1309. #endif
  1310. }
  1311. void LineEdit::set_expand_to_text_length(bool p_enabled) {
  1312. expand_to_text_length = p_enabled;
  1313. minimum_size_changed();
  1314. set_scroll_offset(0);
  1315. }
  1316. bool LineEdit::get_expand_to_text_length() const {
  1317. return expand_to_text_length;
  1318. }
  1319. void LineEdit::set_clear_button_enabled(bool p_enabled) {
  1320. if (clear_button_enabled == p_enabled) {
  1321. return;
  1322. }
  1323. clear_button_enabled = p_enabled;
  1324. minimum_size_changed();
  1325. update();
  1326. }
  1327. bool LineEdit::is_clear_button_enabled() const {
  1328. return clear_button_enabled;
  1329. }
  1330. void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
  1331. shortcut_keys_enabled = p_enabled;
  1332. _generate_context_menu();
  1333. }
  1334. bool LineEdit::is_shortcut_keys_enabled() const {
  1335. return shortcut_keys_enabled;
  1336. }
  1337. void LineEdit::set_virtual_keyboard_enabled(bool p_enable) {
  1338. virtual_keyboard_enabled = p_enable;
  1339. }
  1340. bool LineEdit::is_virtual_keyboard_enabled() const {
  1341. return virtual_keyboard_enabled;
  1342. }
  1343. void LineEdit::set_selecting_enabled(bool p_enabled) {
  1344. selecting_enabled = p_enabled;
  1345. if (!selecting_enabled)
  1346. deselect();
  1347. _generate_context_menu();
  1348. }
  1349. bool LineEdit::is_selecting_enabled() const {
  1350. return selecting_enabled;
  1351. }
  1352. void LineEdit::set_right_icon(const Ref<Texture> &p_icon) {
  1353. if (right_icon == p_icon) {
  1354. return;
  1355. }
  1356. right_icon = p_icon;
  1357. minimum_size_changed();
  1358. update();
  1359. }
  1360. Ref<Texture> LineEdit::get_right_icon() {
  1361. return right_icon;
  1362. }
  1363. void LineEdit::_text_changed() {
  1364. if (expand_to_text_length)
  1365. minimum_size_changed();
  1366. _emit_text_change();
  1367. _clear_redo();
  1368. }
  1369. void LineEdit::_emit_text_change() {
  1370. emit_signal("text_changed", text);
  1371. _change_notify("text");
  1372. text_changed_dirty = false;
  1373. }
  1374. void LineEdit::update_cached_width() {
  1375. Ref<Font> font = get_font("font");
  1376. cached_width = 0;
  1377. if (font != NULL) {
  1378. String text = get_text();
  1379. for (int i = 0; i < text.length(); i++) {
  1380. cached_width += font->get_char_size(pass ? secret_character[0] : text[i]).width;
  1381. }
  1382. }
  1383. }
  1384. void LineEdit::update_placeholder_width() {
  1385. Ref<Font> font = get_font("font");
  1386. cached_placeholder_width = 0;
  1387. if (font != NULL) {
  1388. for (int i = 0; i < placeholder_translated.length(); i++) {
  1389. cached_placeholder_width += font->get_char_size(placeholder_translated[i]).width;
  1390. }
  1391. }
  1392. }
  1393. void LineEdit::_clear_redo() {
  1394. _create_undo_state();
  1395. if (undo_stack_pos == NULL) {
  1396. return;
  1397. }
  1398. undo_stack_pos = undo_stack_pos->next();
  1399. while (undo_stack_pos) {
  1400. List<TextOperation>::Element *elem = undo_stack_pos;
  1401. undo_stack_pos = undo_stack_pos->next();
  1402. undo_stack.erase(elem);
  1403. }
  1404. _create_undo_state();
  1405. }
  1406. void LineEdit::_clear_undo_stack() {
  1407. undo_stack.clear();
  1408. undo_stack_pos = NULL;
  1409. _create_undo_state();
  1410. }
  1411. void LineEdit::_create_undo_state() {
  1412. TextOperation op;
  1413. op.text = text;
  1414. op.cached_width = cached_width;
  1415. op.cursor_pos = cursor_pos;
  1416. op.scroll_offset = scroll_offset;
  1417. undo_stack.push_back(op);
  1418. }
  1419. void LineEdit::_generate_context_menu() {
  1420. // Reorganize context menu.
  1421. menu->clear();
  1422. if (editable)
  1423. menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0);
  1424. menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0);
  1425. if (editable)
  1426. menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0);
  1427. menu->add_separator();
  1428. if (is_selecting_enabled())
  1429. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0);
  1430. if (editable) {
  1431. menu->add_item(RTR("Clear"), MENU_CLEAR);
  1432. menu->add_separator();
  1433. menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0);
  1434. menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0);
  1435. }
  1436. }
  1437. void LineEdit::_bind_methods() {
  1438. ClassDB::bind_method(D_METHOD("_text_changed"), &LineEdit::_text_changed);
  1439. ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &LineEdit::_toggle_draw_caret);
  1440. ClassDB::bind_method("_editor_settings_changed", &LineEdit::_editor_settings_changed);
  1441. ClassDB::bind_method(D_METHOD("set_align", "align"), &LineEdit::set_align);
  1442. ClassDB::bind_method(D_METHOD("get_align"), &LineEdit::get_align);
  1443. ClassDB::bind_method(D_METHOD("_gui_input"), &LineEdit::_gui_input);
  1444. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  1445. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  1446. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  1447. ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect);
  1448. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  1449. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  1450. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  1451. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  1452. ClassDB::bind_method(D_METHOD("set_placeholder_alpha", "alpha"), &LineEdit::set_placeholder_alpha);
  1453. ClassDB::bind_method(D_METHOD("get_placeholder_alpha"), &LineEdit::get_placeholder_alpha);
  1454. ClassDB::bind_method(D_METHOD("set_cursor_position", "position"), &LineEdit::set_cursor_position);
  1455. ClassDB::bind_method(D_METHOD("get_cursor_position"), &LineEdit::get_cursor_position);
  1456. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset);
  1457. ClassDB::bind_method(D_METHOD("set_expand_to_text_length", "enabled"), &LineEdit::set_expand_to_text_length);
  1458. ClassDB::bind_method(D_METHOD("get_expand_to_text_length"), &LineEdit::get_expand_to_text_length);
  1459. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enabled"), &LineEdit::cursor_set_blink_enabled);
  1460. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &LineEdit::cursor_get_blink_enabled);
  1461. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &LineEdit::cursor_set_blink_speed);
  1462. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &LineEdit::cursor_get_blink_speed);
  1463. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  1464. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  1465. ClassDB::bind_method(D_METHOD("append_at_cursor", "text"), &LineEdit::append_at_cursor);
  1466. ClassDB::bind_method(D_METHOD("delete_char_at_cursor"), &LineEdit::delete_char);
  1467. ClassDB::bind_method(D_METHOD("delete_text", "from_column", "to_column"), &LineEdit::delete_text);
  1468. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  1469. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  1470. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  1471. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  1472. ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
  1473. ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
  1474. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  1475. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  1476. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
  1477. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled);
  1478. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &LineEdit::set_virtual_keyboard_enabled);
  1479. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &LineEdit::is_virtual_keyboard_enabled);
  1480. ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled);
  1481. ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled);
  1482. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled);
  1483. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled);
  1484. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled);
  1485. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled);
  1486. ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
  1487. ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
  1488. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text")));
  1489. ADD_SIGNAL(MethodInfo("text_change_rejected"));
  1490. ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "new_text")));
  1491. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  1492. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  1493. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  1494. BIND_ENUM_CONSTANT(ALIGN_FILL);
  1495. BIND_ENUM_CONSTANT(MENU_CUT);
  1496. BIND_ENUM_CONSTANT(MENU_COPY);
  1497. BIND_ENUM_CONSTANT(MENU_PASTE);
  1498. BIND_ENUM_CONSTANT(MENU_CLEAR);
  1499. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  1500. BIND_ENUM_CONSTANT(MENU_UNDO);
  1501. BIND_ENUM_CONSTANT(MENU_REDO);
  1502. BIND_ENUM_CONSTANT(MENU_MAX);
  1503. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  1504. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  1505. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length"), "set_max_length", "get_max_length");
  1506. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  1507. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret");
  1508. ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
  1509. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length", "get_expand_to_text_length");
  1510. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  1511. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  1512. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled");
  1513. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  1514. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  1515. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_right_icon", "get_right_icon");
  1516. ADD_GROUP("Placeholder", "placeholder_");
  1517. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  1518. ADD_PROPERTY(PropertyInfo(Variant::REAL, "placeholder_alpha", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_placeholder_alpha", "get_placeholder_alpha");
  1519. ADD_GROUP("Caret", "caret_");
  1520. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  1521. ADD_PROPERTY(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  1522. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_position"), "set_cursor_position", "get_cursor_position");
  1523. }
  1524. LineEdit::LineEdit() {
  1525. align = ALIGN_LEFT;
  1526. cached_width = 0;
  1527. cached_placeholder_width = 0;
  1528. cursor_pos = 0;
  1529. scroll_offset = 0;
  1530. window_has_focus = true;
  1531. max_length = 0;
  1532. pass = false;
  1533. secret_character = "*";
  1534. text_changed_dirty = false;
  1535. placeholder_alpha = 0.6;
  1536. clear_button_enabled = false;
  1537. clear_button_status.press_attempt = false;
  1538. clear_button_status.pressing_inside = false;
  1539. shortcut_keys_enabled = true;
  1540. selecting_enabled = true;
  1541. undo_stack_pos = nullptr;
  1542. _create_undo_state();
  1543. deselect();
  1544. set_focus_mode(FOCUS_ALL);
  1545. set_default_cursor_shape(CURSOR_IBEAM);
  1546. set_mouse_filter(MOUSE_FILTER_STOP);
  1547. draw_caret = true;
  1548. caret_blink_enabled = false;
  1549. caret_blink_timer = memnew(Timer);
  1550. add_child(caret_blink_timer);
  1551. caret_blink_timer->set_wait_time(0.65);
  1552. caret_blink_timer->connect("timeout", this, "_toggle_draw_caret");
  1553. cursor_set_blink_enabled(false);
  1554. context_menu_enabled = true;
  1555. menu = memnew(PopupMenu);
  1556. add_child(menu);
  1557. editable = false; // Initialise to opposite first, so we get past the early-out in set_editable.
  1558. set_editable(true);
  1559. menu->connect("id_pressed", this, "menu_option");
  1560. expand_to_text_length = false;
  1561. }
  1562. LineEdit::~LineEdit() {
  1563. }