line_edit.cpp 51 KB

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