line_edit.cpp 50 KB

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