line_edit.cpp 61 KB

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