line_edit.cpp 49 KB

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