line_edit.cpp 49 KB

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