2
0

line_edit.cpp 50 KB

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