line_edit.cpp 54 KB

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