line_edit.cpp 49 KB

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