line_edit.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321
  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/object/message_queue.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "core/string/print_string.h"
  35. #include "core/string/translation.h"
  36. #include "label.h"
  37. #include "servers/display_server.h"
  38. #include "servers/text_server.h"
  39. #ifdef TOOLS_ENABLED
  40. #include "editor/editor_scale.h"
  41. #include "editor/editor_settings.h"
  42. #endif
  43. #include "scene/main/window.h"
  44. void LineEdit::_gui_input(Ref<InputEvent> p_event) {
  45. Ref<InputEventMouseButton> b = p_event;
  46. if (b.is_valid()) {
  47. if (ime_text.length() != 0) {
  48. // Ignore mouse clicks in IME input mode.
  49. return;
  50. }
  51. if (b->is_pressed() && b->get_button_index() == BUTTON_RIGHT && context_menu_enabled) {
  52. menu->set_position(get_screen_transform().xform(get_local_mouse_position()));
  53. menu->set_size(Vector2(1, 1));
  54. //menu->set_scale(get_global_transform().get_scale());
  55. menu->popup();
  56. grab_focus();
  57. accept_event();
  58. return;
  59. }
  60. if (b->get_button_index() != BUTTON_LEFT) {
  61. return;
  62. }
  63. _reset_caret_blink_timer();
  64. if (b->is_pressed()) {
  65. accept_event(); //don't pass event further when clicked on text field
  66. if (!text.empty() && is_editable() && _is_over_clear_button(b->get_position())) {
  67. clear_button_status.press_attempt = true;
  68. clear_button_status.pressing_inside = true;
  69. update();
  70. return;
  71. }
  72. shift_selection_check_pre(b->get_shift());
  73. set_cursor_at_pixel_pos(b->get_position().x);
  74. if (b->get_shift()) {
  75. selection_fill_at_cursor();
  76. selection.creating = true;
  77. } else {
  78. if (b->is_doubleclick() && selecting_enabled) {
  79. selection.enabled = true;
  80. selection.begin = 0;
  81. selection.end = text.length();
  82. selection.doubleclick = true;
  83. }
  84. selection.drag_attempt = false;
  85. if ((cursor_pos < selection.begin) || (cursor_pos > selection.end) || !selection.enabled) {
  86. deselect();
  87. selection.cursor_start = cursor_pos;
  88. selection.creating = true;
  89. } else if (selection.enabled) {
  90. selection.drag_attempt = true;
  91. }
  92. }
  93. update();
  94. } else {
  95. if (!text.empty() && is_editable() && clear_button_enabled) {
  96. bool press_attempt = clear_button_status.press_attempt;
  97. clear_button_status.press_attempt = false;
  98. if (press_attempt && clear_button_status.pressing_inside && _is_over_clear_button(b->get_position())) {
  99. clear();
  100. return;
  101. }
  102. }
  103. if ((!selection.creating) && (!selection.doubleclick)) {
  104. deselect();
  105. }
  106. selection.creating = false;
  107. selection.doubleclick = false;
  108. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  109. if (selection.enabled) {
  110. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), false, max_length, selection.begin, selection.end);
  111. } else {
  112. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), false, max_length, cursor_pos);
  113. }
  114. }
  115. }
  116. update();
  117. }
  118. Ref<InputEventMouseMotion> m = p_event;
  119. if (m.is_valid()) {
  120. if (!text.empty() && is_editable() && clear_button_enabled) {
  121. bool last_press_inside = clear_button_status.pressing_inside;
  122. clear_button_status.pressing_inside = clear_button_status.press_attempt && _is_over_clear_button(m->get_position());
  123. if (last_press_inside != clear_button_status.pressing_inside) {
  124. update();
  125. }
  126. }
  127. if (m->get_button_mask() & BUTTON_LEFT) {
  128. if (selection.creating) {
  129. set_cursor_at_pixel_pos(m->get_position().x);
  130. selection_fill_at_cursor();
  131. }
  132. }
  133. }
  134. Ref<InputEventKey> k = p_event;
  135. if (k.is_valid()) {
  136. if (!k->is_pressed()) {
  137. return;
  138. }
  139. #ifdef APPLE_STYLE_KEYS
  140. if (k->get_control() && !k->get_shift() && !k->get_alt() && !k->get_command()) {
  141. uint32_t remap_key = KEY_UNKNOWN;
  142. switch (k->get_keycode()) {
  143. case KEY_F: {
  144. remap_key = KEY_RIGHT;
  145. } break;
  146. case KEY_B: {
  147. remap_key = KEY_LEFT;
  148. } break;
  149. case KEY_P: {
  150. remap_key = KEY_UP;
  151. } break;
  152. case KEY_N: {
  153. remap_key = KEY_DOWN;
  154. } break;
  155. case KEY_D: {
  156. remap_key = KEY_DELETE;
  157. } break;
  158. case KEY_H: {
  159. remap_key = KEY_BACKSPACE;
  160. } break;
  161. case KEY_A: {
  162. remap_key = KEY_HOME;
  163. } break;
  164. case KEY_E: {
  165. remap_key = KEY_END;
  166. } break;
  167. }
  168. if (remap_key != KEY_UNKNOWN) {
  169. k->set_keycode(remap_key);
  170. k->set_control(false);
  171. }
  172. }
  173. #endif
  174. unsigned int code = k->get_keycode();
  175. if (k->get_command() && is_shortcut_keys_enabled()) {
  176. bool handled = true;
  177. switch (code) {
  178. case (KEY_QUOTELEFT): { // Swap current input direction (primary cursor)
  179. if (input_direction == TEXT_DIRECTION_LTR) {
  180. input_direction = TEXT_DIRECTION_RTL;
  181. } else {
  182. input_direction = TEXT_DIRECTION_LTR;
  183. }
  184. set_cursor_position(get_cursor_position());
  185. update();
  186. } break;
  187. case (KEY_X): { // CUT.
  188. if (editable) {
  189. cut_text();
  190. }
  191. } break;
  192. case (KEY_C): { // COPY.
  193. copy_text();
  194. } break;
  195. case (KEY_Y): // PASTE (Yank for unix users).
  196. case (KEY_V): { // PASTE.
  197. if (editable) {
  198. paste_text();
  199. }
  200. } break;
  201. case (KEY_Z): { // Undo/redo.
  202. if (editable) {
  203. if (k->get_shift()) {
  204. redo();
  205. } else {
  206. undo();
  207. }
  208. }
  209. } break;
  210. case (KEY_U): { // Delete from start to cursor.
  211. if (editable) {
  212. deselect();
  213. text = text.substr(cursor_pos, text.length() - cursor_pos);
  214. _shape();
  215. set_cursor_position(0);
  216. _text_changed();
  217. }
  218. } break;
  219. case (KEY_K): { // Delete from cursor_pos to end.
  220. if (editable) {
  221. deselect();
  222. text = text.substr(0, cursor_pos);
  223. _text_changed();
  224. }
  225. } break;
  226. case (KEY_A): { // Select all.
  227. select();
  228. } break;
  229. #ifdef APPLE_STYLE_KEYS
  230. case (KEY_LEFT): { // Go to start of text - like HOME key.
  231. shift_selection_check_pre(k->get_shift());
  232. set_cursor_position(0);
  233. shift_selection_check_post(k->get_shift());
  234. } break;
  235. case (KEY_RIGHT): { // Go to end of text - like END key.
  236. shift_selection_check_pre(k->get_shift());
  237. set_cursor_position(text.length());
  238. shift_selection_check_post(k->get_shift());
  239. } break;
  240. case (KEY_BACKSPACE): {
  241. if (!editable)
  242. break;
  243. // If selected, delete the selection
  244. if (selection.enabled) {
  245. selection_delete();
  246. break;
  247. }
  248. // Otherwise delete from cursor to beginning of text edit
  249. int current_pos = get_cursor_position();
  250. if (current_pos != 0) {
  251. delete_text(0, current_pos);
  252. }
  253. } break;
  254. #endif
  255. default: {
  256. handled = false;
  257. }
  258. }
  259. if (handled) {
  260. accept_event();
  261. return;
  262. }
  263. }
  264. _reset_caret_blink_timer();
  265. if (!k->get_metakey()) {
  266. bool handled = true;
  267. switch (code) {
  268. case KEY_KP_ENTER:
  269. case KEY_ENTER: {
  270. emit_signal("text_entered", text);
  271. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  272. DisplayServer::get_singleton()->virtual_keyboard_hide();
  273. }
  274. } break;
  275. case KEY_BACKSPACE: {
  276. if (!editable) {
  277. break;
  278. }
  279. if (selection.enabled) {
  280. selection_delete();
  281. break;
  282. }
  283. #ifdef APPLE_STYLE_KEYS
  284. if (k->get_alt()) {
  285. #else
  286. if (k->get_alt()) {
  287. handled = false;
  288. break;
  289. } else if (k->get_command()) {
  290. #endif
  291. int cc = cursor_pos;
  292. Vector<Vector2i> words = TS->shaped_text_get_word_breaks(text_rid);
  293. for (int i = words.size() - 1; i >= 0; i--) {
  294. if (words[i].x < cc) {
  295. cc = words[i].x;
  296. break;
  297. }
  298. }
  299. delete_text(cc, cursor_pos);
  300. set_cursor_position(cc);
  301. } else {
  302. delete_char();
  303. }
  304. } break;
  305. case KEY_KP_4: {
  306. if (k->get_unicode() != 0) {
  307. handled = false;
  308. break;
  309. }
  310. [[fallthrough]];
  311. }
  312. case KEY_LEFT: {
  313. #ifndef APPLE_STYLE_KEYS
  314. if (!k->get_alt()) {
  315. #endif
  316. if (selection.enabled && !k->get_shift()) {
  317. set_cursor_position(selection.begin);
  318. deselect();
  319. handled = true;
  320. break;
  321. }
  322. shift_selection_check_pre(k->get_shift());
  323. #ifndef APPLE_STYLE_KEYS
  324. }
  325. #endif
  326. #ifdef APPLE_STYLE_KEYS
  327. if (k->get_command()) {
  328. set_cursor_position(0);
  329. } else if (k->get_alt()) {
  330. #else
  331. if (k->get_alt()) {
  332. handled = false;
  333. break;
  334. } else if (k->get_command()) {
  335. #endif
  336. int cc = cursor_pos;
  337. Vector<Vector2i> words = TS->shaped_text_get_word_breaks(text_rid);
  338. for (int i = words.size() - 1; i >= 0; i--) {
  339. if (words[i].x < cc) {
  340. cc = words[i].x;
  341. break;
  342. }
  343. }
  344. set_cursor_position(cc);
  345. } else {
  346. if (mid_grapheme_caret_enabled) {
  347. set_cursor_position(get_cursor_position() - 1);
  348. } else {
  349. set_cursor_position(TS->shaped_text_prev_grapheme_pos(text_rid, get_cursor_position()));
  350. }
  351. }
  352. shift_selection_check_post(k->get_shift());
  353. } break;
  354. case KEY_KP_6: {
  355. if (k->get_unicode() != 0) {
  356. handled = false;
  357. break;
  358. }
  359. [[fallthrough]];
  360. }
  361. case KEY_RIGHT: {
  362. #ifndef APPLE_STYLE_KEYS
  363. if (!k->get_alt()) {
  364. #endif
  365. if (selection.enabled && !k->get_shift()) {
  366. set_cursor_position(selection.end);
  367. deselect();
  368. handled = true;
  369. break;
  370. }
  371. shift_selection_check_pre(k->get_shift());
  372. #ifndef APPLE_STYLE_KEYS
  373. }
  374. #endif
  375. #ifdef APPLE_STYLE_KEYS
  376. if (k->get_command()) {
  377. set_cursor_position(text.length());
  378. } else if (k->get_alt()) {
  379. #else
  380. if (k->get_alt()) {
  381. handled = false;
  382. break;
  383. } else if (k->get_command()) {
  384. #endif
  385. int cc = cursor_pos;
  386. Vector<Vector2i> words = TS->shaped_text_get_word_breaks(text_rid);
  387. for (int i = 0; i < words.size(); i++) {
  388. if (words[i].y > cc) {
  389. cc = words[i].y;
  390. break;
  391. }
  392. }
  393. set_cursor_position(cc);
  394. } else {
  395. if (mid_grapheme_caret_enabled) {
  396. set_cursor_position(get_cursor_position() + 1);
  397. } else {
  398. set_cursor_position(TS->shaped_text_next_grapheme_pos(text_rid, get_cursor_position()));
  399. }
  400. }
  401. shift_selection_check_post(k->get_shift());
  402. } break;
  403. case KEY_UP: {
  404. shift_selection_check_pre(k->get_shift());
  405. if (get_cursor_position() == 0) {
  406. handled = false;
  407. }
  408. set_cursor_position(0);
  409. shift_selection_check_post(k->get_shift());
  410. } break;
  411. case KEY_DOWN: {
  412. shift_selection_check_pre(k->get_shift());
  413. if (get_cursor_position() == text.length()) {
  414. handled = false;
  415. }
  416. set_cursor_position(text.length());
  417. shift_selection_check_post(k->get_shift());
  418. } break;
  419. case KEY_DELETE: {
  420. if (!editable) {
  421. break;
  422. }
  423. if (k->get_shift() && !k->get_command() && !k->get_alt()) {
  424. cut_text();
  425. break;
  426. }
  427. if (selection.enabled) {
  428. selection_delete();
  429. break;
  430. }
  431. int text_len = text.length();
  432. if (cursor_pos == text_len) {
  433. break; // Nothing to do.
  434. }
  435. #ifdef APPLE_STYLE_KEYS
  436. if (k->get_alt()) {
  437. #else
  438. if (k->get_alt()) {
  439. handled = false;
  440. break;
  441. } else if (k->get_command()) {
  442. #endif
  443. int cc = cursor_pos;
  444. Vector<Vector2i> words = TS->shaped_text_get_word_breaks(text_rid);
  445. for (int i = 0; i < words.size(); i++) {
  446. if (words[i].y > cc) {
  447. cc = words[i].y;
  448. break;
  449. }
  450. }
  451. delete_text(cursor_pos, cc);
  452. } else {
  453. if (mid_grapheme_caret_enabled) {
  454. set_cursor_position(cursor_pos + 1);
  455. delete_char();
  456. } else {
  457. int cc = cursor_pos;
  458. set_cursor_position(TS->shaped_text_next_grapheme_pos(text_rid, cursor_pos));
  459. delete_text(cc, cursor_pos);
  460. }
  461. }
  462. } break;
  463. case KEY_KP_7: {
  464. if (k->get_unicode() != 0) {
  465. handled = false;
  466. break;
  467. }
  468. [[fallthrough]];
  469. }
  470. case KEY_HOME: {
  471. shift_selection_check_pre(k->get_shift());
  472. set_cursor_position(0);
  473. shift_selection_check_post(k->get_shift());
  474. } break;
  475. case KEY_KP_1: {
  476. if (k->get_unicode() != 0) {
  477. handled = false;
  478. break;
  479. }
  480. [[fallthrough]];
  481. }
  482. case KEY_END: {
  483. shift_selection_check_pre(k->get_shift());
  484. set_cursor_position(text.length());
  485. shift_selection_check_post(k->get_shift());
  486. } break;
  487. case KEY_MENU: {
  488. if (context_menu_enabled) {
  489. Point2 pos = Point2(get_cursor_pixel_pos().x, (get_size().y + get_theme_font("font")->get_height(get_theme_font_size("font_size"))) / 2);
  490. menu->set_position(get_global_transform().xform(pos));
  491. menu->set_size(Vector2(1, 1));
  492. //menu->set_scale(get_global_transform().get_scale());
  493. menu->popup();
  494. menu->grab_focus();
  495. }
  496. } break;
  497. default: {
  498. handled = false;
  499. } break;
  500. }
  501. if (handled) {
  502. accept_event();
  503. } else if (!k->get_command() || (k->get_command() && k->get_alt())) {
  504. if (k->get_unicode() >= 32 && k->get_keycode() != KEY_DELETE) {
  505. if (editable) {
  506. selection_delete();
  507. char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 };
  508. int prev_len = text.length();
  509. append_at_cursor(ucodestr);
  510. if (text.length() != prev_len) {
  511. _text_changed();
  512. }
  513. accept_event();
  514. }
  515. } else {
  516. return;
  517. }
  518. }
  519. update();
  520. }
  521. return;
  522. }
  523. }
  524. void LineEdit::set_align(Align p_align) {
  525. ERR_FAIL_INDEX((int)p_align, 4);
  526. if (align != p_align) {
  527. align = p_align;
  528. _shape();
  529. }
  530. update();
  531. }
  532. LineEdit::Align LineEdit::get_align() const {
  533. return align;
  534. }
  535. Variant LineEdit::get_drag_data(const Point2 &p_point) {
  536. if (selection.drag_attempt && selection.enabled) {
  537. String t = text.substr(selection.begin, selection.end - selection.begin);
  538. Label *l = memnew(Label);
  539. l->set_text(t);
  540. set_drag_preview(l);
  541. return t;
  542. }
  543. return Variant();
  544. }
  545. bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  546. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  547. if (drop_override) {
  548. return drop_override;
  549. }
  550. return p_data.get_type() == Variant::STRING;
  551. }
  552. void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  553. Control::drop_data(p_point, p_data);
  554. if (p_data.get_type() == Variant::STRING) {
  555. set_cursor_at_pixel_pos(p_point.x);
  556. int selected = selection.end - selection.begin;
  557. text.erase(selection.begin, selected);
  558. _shape();
  559. append_at_cursor(p_data);
  560. selection.begin = cursor_pos - selected;
  561. selection.end = cursor_pos;
  562. }
  563. }
  564. Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const {
  565. if (!text.empty() && is_editable() && _is_over_clear_button(p_pos)) {
  566. return CURSOR_ARROW;
  567. }
  568. return Control::get_cursor_shape(p_pos);
  569. }
  570. bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const {
  571. if (!clear_button_enabled || !has_point(p_pos)) {
  572. return false;
  573. }
  574. Ref<Texture2D> icon = Control::get_theme_icon("clear");
  575. int x_ofs = get_theme_stylebox("normal")->get_offset().x;
  576. return p_pos.x > get_size().width - icon->get_width() - x_ofs;
  577. }
  578. void LineEdit::_notification(int p_what) {
  579. switch (p_what) {
  580. #ifdef TOOLS_ENABLED
  581. case NOTIFICATION_ENTER_TREE: {
  582. if (Engine::get_singleton()->is_editor_hint() && !get_tree()->is_node_being_edited(this)) {
  583. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  584. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  585. if (!EditorSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed))) {
  586. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed));
  587. }
  588. }
  589. } break;
  590. #endif
  591. case NOTIFICATION_RESIZED: {
  592. _fit_to_width();
  593. scroll_offset = 0;
  594. set_cursor_position(get_cursor_position());
  595. } break;
  596. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  597. case NOTIFICATION_THEME_CHANGED: {
  598. _shape();
  599. update();
  600. } break;
  601. case NOTIFICATION_TRANSLATION_CHANGED: {
  602. placeholder_translated = tr(placeholder);
  603. _shape();
  604. update();
  605. } break;
  606. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  607. window_has_focus = true;
  608. draw_caret = true;
  609. update();
  610. } break;
  611. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  612. window_has_focus = false;
  613. draw_caret = false;
  614. update();
  615. } break;
  616. case NOTIFICATION_DRAW: {
  617. if ((!has_focus() && !menu->has_focus() && !caret_force_displayed) || !window_has_focus) {
  618. draw_caret = false;
  619. }
  620. int width, height;
  621. bool rtl = is_layout_rtl();
  622. Size2 size = get_size();
  623. width = size.width;
  624. height = size.height;
  625. RID ci = get_canvas_item();
  626. Ref<StyleBox> style = get_theme_stylebox("normal");
  627. if (!is_editable()) {
  628. style = get_theme_stylebox("read_only");
  629. draw_caret = false;
  630. }
  631. Ref<Font> font = get_theme_font("font");
  632. style->draw(ci, Rect2(Point2(), size));
  633. if (has_focus()) {
  634. get_theme_stylebox("focus")->draw(ci, Rect2(Point2(), size));
  635. }
  636. int x_ofs = 0;
  637. bool using_placeholder = text.empty() && ime_text.empty();
  638. float text_width = TS->shaped_text_get_size(text_rid).x;
  639. float text_height = TS->shaped_text_get_size(text_rid).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM);
  640. switch (align) {
  641. case ALIGN_FILL:
  642. case ALIGN_LEFT: {
  643. if (rtl) {
  644. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  645. } else {
  646. x_ofs = style->get_offset().x;
  647. }
  648. } break;
  649. case ALIGN_CENTER: {
  650. if (scroll_offset != 0) {
  651. x_ofs = style->get_offset().x;
  652. } else {
  653. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - (text_width)) / 2);
  654. }
  655. } break;
  656. case ALIGN_RIGHT: {
  657. if (rtl) {
  658. x_ofs = style->get_offset().x;
  659. } else {
  660. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  661. }
  662. } break;
  663. }
  664. int ofs_max = width - style->get_margin(MARGIN_RIGHT);
  665. int y_area = height - style->get_minimum_size().height;
  666. int y_ofs = style->get_offset().y + (y_area - text_height) / 2;
  667. Color selection_color = get_theme_color("selection_color");
  668. Color font_color = is_editable() ? get_theme_color("font_color") : get_theme_color("font_color_uneditable");
  669. Color font_color_selected = get_theme_color("font_color_selected");
  670. Color cursor_color = get_theme_color("cursor_color");
  671. // Draw placeholder color.
  672. if (using_placeholder) {
  673. font_color.a *= placeholder_alpha;
  674. }
  675. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  676. if (right_icon.is_valid() || display_clear_icon) {
  677. Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon("clear") : right_icon;
  678. Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9);
  679. if (display_clear_icon) {
  680. if (clear_button_status.press_attempt && clear_button_status.pressing_inside) {
  681. color_icon = get_theme_color("clear_button_color_pressed");
  682. } else {
  683. color_icon = get_theme_color("clear_button_color");
  684. }
  685. }
  686. r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
  687. if (align == ALIGN_CENTER) {
  688. if (scroll_offset == 0) {
  689. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2);
  690. }
  691. } else {
  692. x_ofs = MAX(style->get_margin(MARGIN_LEFT), x_ofs - r_icon->get_width() - style->get_margin(MARGIN_RIGHT));
  693. }
  694. ofs_max -= r_icon->get_width();
  695. }
  696. #ifdef TOOLS_ENABLED
  697. int caret_width = Math::round(EDSCALE);
  698. #else
  699. int caret_width = 1;
  700. #endif
  701. // Draw selections rects.
  702. Vector2 ofs = Point2(x_ofs + scroll_offset, y_ofs);
  703. if (selection.enabled) {
  704. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, selection.begin, selection.end);
  705. for (int i = 0; i < sel.size(); i++) {
  706. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  707. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  708. continue;
  709. }
  710. if (rect.position.x < x_ofs) {
  711. rect.size.x -= (x_ofs - rect.position.x);
  712. rect.position.x = x_ofs;
  713. } else if (rect.position.x + rect.size.x > ofs_max) {
  714. rect.size.x = ofs_max - rect.position.x;
  715. }
  716. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_color);
  717. }
  718. }
  719. const Vector<TextServer::Glyph> visual = TS->shaped_text_get_glyphs(text_rid);
  720. const TextServer::Glyph *glyphs = visual.ptr();
  721. int gl_size = visual.size();
  722. // Draw text.
  723. ofs.y += TS->shaped_text_get_ascent(text_rid);
  724. for (int i = 0; i < gl_size; i++) {
  725. bool selected = selection.enabled && glyphs[i].start >= selection.begin && glyphs[i].end <= selection.end;
  726. for (int j = 0; j < glyphs[i].repeat; j++) {
  727. if (ceil(ofs.x) >= x_ofs && (ofs.x + glyphs[i].advance) <= ofs_max) {
  728. if (glyphs[i].font_rid != RID()) {
  729. TS->font_draw_glyph(glyphs[i].font_rid, ci, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, selected ? font_color_selected : font_color);
  730. } else if ((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  731. TS->draw_hex_code_box(ci, glyphs[i].font_size, ofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, selected ? font_color_selected : font_color);
  732. }
  733. }
  734. ofs.x += glyphs[i].advance;
  735. }
  736. if (ofs.x >= ofs_max) {
  737. break;
  738. }
  739. }
  740. // Draw carets.
  741. ofs.x = x_ofs + scroll_offset;
  742. if (draw_caret) {
  743. if (ime_text.length() == 0) {
  744. // Normal caret.
  745. Rect2 l_caret, t_caret;
  746. TextServer::Direction l_dir, t_dir;
  747. TS->shaped_text_get_carets(text_rid, cursor_pos, l_caret, l_dir, t_caret, t_dir);
  748. if (l_caret == Rect2() && t_caret == Rect2()) {
  749. // No carets, add one at the start.
  750. int h = get_theme_font("font")->get_height(get_theme_font_size("font_size"));
  751. int y = style->get_offset().y + (y_area - h) / 2;
  752. if (rtl) {
  753. l_dir = TextServer::DIRECTION_RTL;
  754. l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  755. } else {
  756. l_dir = TextServer::DIRECTION_LTR;
  757. l_caret = Rect2(Vector2(x_ofs, y), Size2(caret_width, h));
  758. }
  759. RenderingServer::get_singleton()->canvas_item_add_rect(ci, l_caret, cursor_color);
  760. } else {
  761. if (l_caret != Rect2() && l_dir == TextServer::DIRECTION_AUTO) {
  762. // Draw extra marker on top of mid caret.
  763. Rect2 trect = Rect2(l_caret.position.x - 3 * caret_width, l_caret.position.y, 6 * caret_width, caret_width);
  764. trect.position += ofs;
  765. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, cursor_color);
  766. }
  767. l_caret.position += ofs;
  768. l_caret.size.x = caret_width;
  769. RenderingServer::get_singleton()->canvas_item_add_rect(ci, l_caret, cursor_color);
  770. t_caret.position += ofs;
  771. t_caret.size.x = caret_width;
  772. RenderingServer::get_singleton()->canvas_item_add_rect(ci, t_caret, cursor_color);
  773. }
  774. } else {
  775. {
  776. // IME intermidiet text range.
  777. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, cursor_pos, cursor_pos + ime_text.length());
  778. for (int i = 0; i < sel.size(); i++) {
  779. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  780. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  781. continue;
  782. }
  783. if (rect.position.x < x_ofs) {
  784. rect.size.x -= (x_ofs - rect.position.x);
  785. rect.position.x = x_ofs;
  786. } else if (rect.position.x + rect.size.x > ofs_max) {
  787. rect.size.x = ofs_max - rect.position.x;
  788. }
  789. rect.size.y = caret_width;
  790. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, cursor_color);
  791. }
  792. }
  793. {
  794. // IME caret.
  795. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, cursor_pos + ime_selection.x, cursor_pos + ime_selection.x + ime_selection.y);
  796. for (int i = 0; i < sel.size(); i++) {
  797. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  798. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  799. continue;
  800. }
  801. if (rect.position.x < x_ofs) {
  802. rect.size.x -= (x_ofs - rect.position.x);
  803. rect.position.x = x_ofs;
  804. } else if (rect.position.x + rect.size.x > ofs_max) {
  805. rect.size.x = ofs_max - rect.position.x;
  806. }
  807. rect.size.y = caret_width * 3;
  808. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, cursor_color);
  809. }
  810. }
  811. }
  812. }
  813. if (has_focus()) {
  814. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID) {
  815. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  816. DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + Point2(using_placeholder ? 0 : x_ofs, y_ofs + TS->shaped_text_get_size(text_rid).y), get_viewport()->get_window_id());
  817. }
  818. }
  819. } break;
  820. case NOTIFICATION_FOCUS_ENTER: {
  821. if (!caret_force_displayed) {
  822. if (caret_blink_enabled) {
  823. if (caret_blink_timer->is_stopped()) {
  824. caret_blink_timer->start();
  825. }
  826. } else {
  827. draw_caret = true;
  828. }
  829. }
  830. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID) {
  831. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  832. Point2 cursor_pos = Point2(get_cursor_position(), 1) * get_minimum_size().height;
  833. DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + cursor_pos, get_viewport()->get_window_id());
  834. }
  835. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  836. if (selection.enabled) {
  837. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), false, max_length, selection.begin, selection.end);
  838. } else {
  839. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), false, max_length, cursor_pos);
  840. }
  841. }
  842. } break;
  843. case NOTIFICATION_FOCUS_EXIT: {
  844. if (caret_blink_enabled && !caret_force_displayed) {
  845. caret_blink_timer->stop();
  846. }
  847. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID) {
  848. DisplayServer::get_singleton()->window_set_ime_position(Point2(), get_viewport()->get_window_id());
  849. DisplayServer::get_singleton()->window_set_ime_active(false, get_viewport()->get_window_id());
  850. }
  851. ime_text = "";
  852. ime_selection = Point2();
  853. _shape();
  854. set_cursor_position(cursor_pos); // Update scroll_offset
  855. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  856. DisplayServer::get_singleton()->virtual_keyboard_hide();
  857. }
  858. } break;
  859. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  860. if (has_focus()) {
  861. ime_text = DisplayServer::get_singleton()->ime_get_text();
  862. ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  863. _shape();
  864. set_cursor_position(cursor_pos); // Update scroll_offset
  865. update();
  866. }
  867. } break;
  868. }
  869. }
  870. void LineEdit::copy_text() {
  871. if (selection.enabled && !pass) {
  872. DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
  873. }
  874. }
  875. void LineEdit::cut_text() {
  876. if (selection.enabled && !pass) {
  877. DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
  878. selection_delete();
  879. }
  880. }
  881. void LineEdit::paste_text() {
  882. // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
  883. String paste_buffer = DisplayServer::get_singleton()->clipboard_get().strip_escapes();
  884. if (paste_buffer != "") {
  885. int prev_len = text.length();
  886. if (selection.enabled) {
  887. selection_delete();
  888. }
  889. append_at_cursor(paste_buffer);
  890. if (!text_changed_dirty) {
  891. if (is_inside_tree() && text.length() != prev_len) {
  892. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  893. }
  894. text_changed_dirty = true;
  895. }
  896. }
  897. }
  898. void LineEdit::undo() {
  899. if (undo_stack_pos == nullptr) {
  900. if (undo_stack.size() <= 1) {
  901. return;
  902. }
  903. undo_stack_pos = undo_stack.back();
  904. } else if (undo_stack_pos == undo_stack.front()) {
  905. return;
  906. }
  907. undo_stack_pos = undo_stack_pos->prev();
  908. TextOperation op = undo_stack_pos->get();
  909. text = op.text;
  910. scroll_offset = op.scroll_offset;
  911. set_cursor_position(op.cursor_pos);
  912. _shape();
  913. _emit_text_change();
  914. }
  915. void LineEdit::redo() {
  916. if (undo_stack_pos == nullptr) {
  917. return;
  918. }
  919. if (undo_stack_pos == undo_stack.back()) {
  920. return;
  921. }
  922. undo_stack_pos = undo_stack_pos->next();
  923. TextOperation op = undo_stack_pos->get();
  924. text = op.text;
  925. scroll_offset = op.scroll_offset;
  926. set_cursor_position(op.cursor_pos);
  927. _shape();
  928. _emit_text_change();
  929. }
  930. void LineEdit::shift_selection_check_pre(bool p_shift) {
  931. if (!selection.enabled && p_shift) {
  932. selection.cursor_start = cursor_pos;
  933. }
  934. if (!p_shift) {
  935. deselect();
  936. }
  937. }
  938. void LineEdit::shift_selection_check_post(bool p_shift) {
  939. if (p_shift) {
  940. selection_fill_at_cursor();
  941. }
  942. }
  943. void LineEdit::set_cursor_at_pixel_pos(int p_x) {
  944. Ref<StyleBox> style = get_theme_stylebox("normal");
  945. bool rtl = is_layout_rtl();
  946. int x_ofs = 0;
  947. float text_width = TS->shaped_text_get_size(text_rid).x;
  948. switch (align) {
  949. case ALIGN_FILL:
  950. case ALIGN_LEFT: {
  951. if (rtl) {
  952. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  953. } else {
  954. x_ofs = style->get_offset().x;
  955. }
  956. } break;
  957. case ALIGN_CENTER: {
  958. if (scroll_offset != 0) {
  959. x_ofs = style->get_offset().x;
  960. } else {
  961. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - (text_width)) / 2);
  962. }
  963. } break;
  964. case ALIGN_RIGHT: {
  965. if (rtl) {
  966. x_ofs = style->get_offset().x;
  967. } else {
  968. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  969. }
  970. } break;
  971. }
  972. bool using_placeholder = text.empty() && ime_text.empty();
  973. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  974. if (right_icon.is_valid() || display_clear_icon) {
  975. Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon("clear") : right_icon;
  976. if (align == ALIGN_CENTER) {
  977. if (scroll_offset == 0) {
  978. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2);
  979. }
  980. } else {
  981. x_ofs = MAX(style->get_margin(MARGIN_LEFT), x_ofs - r_icon->get_width() - style->get_margin(MARGIN_RIGHT));
  982. }
  983. }
  984. int ofs = TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset);
  985. set_cursor_position(ofs);
  986. }
  987. Vector2i LineEdit::get_cursor_pixel_pos() {
  988. Ref<StyleBox> style = get_theme_stylebox("normal");
  989. bool rtl = is_layout_rtl();
  990. int x_ofs = 0;
  991. float text_width = TS->shaped_text_get_size(text_rid).x;
  992. switch (align) {
  993. case ALIGN_FILL:
  994. case ALIGN_LEFT: {
  995. if (rtl) {
  996. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  997. } else {
  998. x_ofs = style->get_offset().x;
  999. }
  1000. } break;
  1001. case ALIGN_CENTER: {
  1002. if (scroll_offset != 0) {
  1003. x_ofs = style->get_offset().x;
  1004. } else {
  1005. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - (text_width)) / 2);
  1006. }
  1007. } break;
  1008. case ALIGN_RIGHT: {
  1009. if (rtl) {
  1010. x_ofs = style->get_offset().x;
  1011. } else {
  1012. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  1013. }
  1014. } break;
  1015. }
  1016. bool using_placeholder = text.empty() && ime_text.empty();
  1017. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1018. if (right_icon.is_valid() || display_clear_icon) {
  1019. Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon("clear") : right_icon;
  1020. if (align == ALIGN_CENTER) {
  1021. if (scroll_offset == 0) {
  1022. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2);
  1023. }
  1024. } else {
  1025. x_ofs = MAX(style->get_margin(MARGIN_LEFT), x_ofs - r_icon->get_width() - style->get_margin(MARGIN_RIGHT));
  1026. }
  1027. }
  1028. Vector2i ret;
  1029. Rect2 l_caret, t_caret;
  1030. TextServer::Direction l_dir, t_dir;
  1031. // Get position of the start of caret.
  1032. if (ime_text.length() != 0 && ime_selection.x != 0) {
  1033. TS->shaped_text_get_carets(text_rid, cursor_pos + ime_selection.x, l_caret, l_dir, t_caret, t_dir);
  1034. } else {
  1035. TS->shaped_text_get_carets(text_rid, cursor_pos, l_caret, l_dir, t_caret, t_dir);
  1036. }
  1037. if ((l_caret != Rect2() && (l_dir == TextServer::DIRECTION_AUTO || l_dir == (TextServer::Direction)input_direction)) || (t_caret == Rect2())) {
  1038. ret.x = x_ofs + l_caret.position.x + scroll_offset;
  1039. } else {
  1040. ret.x = x_ofs + t_caret.position.x + scroll_offset;
  1041. }
  1042. // Get position of the end of caret.
  1043. if (ime_text.length() != 0) {
  1044. if (ime_selection.y != 0) {
  1045. TS->shaped_text_get_carets(text_rid, cursor_pos + ime_selection.x + ime_selection.y, l_caret, l_dir, t_caret, t_dir);
  1046. } else {
  1047. TS->shaped_text_get_carets(text_rid, cursor_pos + ime_text.size(), l_caret, l_dir, t_caret, t_dir);
  1048. }
  1049. if ((l_caret != Rect2() && (l_dir == TextServer::DIRECTION_AUTO || l_dir == (TextServer::Direction)input_direction)) || (t_caret == Rect2())) {
  1050. ret.y = x_ofs + l_caret.position.x + scroll_offset;
  1051. } else {
  1052. ret.y = x_ofs + t_caret.position.x + scroll_offset;
  1053. }
  1054. } else {
  1055. ret.y = ret.x;
  1056. }
  1057. return ret;
  1058. }
  1059. void LineEdit::set_mid_grapheme_caret_enabled(const bool p_enabled) {
  1060. mid_grapheme_caret_enabled = p_enabled;
  1061. }
  1062. bool LineEdit::get_mid_grapheme_caret_enabled() const {
  1063. return mid_grapheme_caret_enabled;
  1064. }
  1065. bool LineEdit::cursor_get_blink_enabled() const {
  1066. return caret_blink_enabled;
  1067. }
  1068. void LineEdit::cursor_set_blink_enabled(const bool p_enabled) {
  1069. caret_blink_enabled = p_enabled;
  1070. if (has_focus() || caret_force_displayed) {
  1071. if (p_enabled) {
  1072. if (caret_blink_timer->is_stopped()) {
  1073. caret_blink_timer->start();
  1074. }
  1075. } else {
  1076. caret_blink_timer->stop();
  1077. }
  1078. }
  1079. draw_caret = true;
  1080. }
  1081. bool LineEdit::cursor_get_force_displayed() const {
  1082. return caret_force_displayed;
  1083. }
  1084. void LineEdit::cursor_set_force_displayed(const bool p_enabled) {
  1085. caret_force_displayed = p_enabled;
  1086. cursor_set_blink_enabled(caret_blink_enabled);
  1087. update();
  1088. }
  1089. float LineEdit::cursor_get_blink_speed() const {
  1090. return caret_blink_timer->get_wait_time();
  1091. }
  1092. void LineEdit::cursor_set_blink_speed(const float p_speed) {
  1093. ERR_FAIL_COND(p_speed <= 0);
  1094. caret_blink_timer->set_wait_time(p_speed);
  1095. }
  1096. void LineEdit::_reset_caret_blink_timer() {
  1097. if (caret_blink_enabled) {
  1098. draw_caret = true;
  1099. if (has_focus()) {
  1100. caret_blink_timer->stop();
  1101. caret_blink_timer->start();
  1102. update();
  1103. }
  1104. }
  1105. }
  1106. void LineEdit::_toggle_draw_caret() {
  1107. draw_caret = !draw_caret;
  1108. if (is_visible_in_tree() && ((has_focus() && window_has_focus) || caret_force_displayed)) {
  1109. update();
  1110. }
  1111. }
  1112. void LineEdit::delete_char() {
  1113. if ((text.length() <= 0) || (cursor_pos == 0)) {
  1114. return;
  1115. }
  1116. text.erase(cursor_pos - 1, 1);
  1117. _shape();
  1118. set_cursor_position(get_cursor_position() - 1);
  1119. _text_changed();
  1120. }
  1121. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  1122. ERR_FAIL_COND_MSG(p_from_column < 0 || p_from_column > p_to_column || p_to_column > text.length(),
  1123. vformat("Positional parameters (from: %d, to: %d) are inverted or outside the text length (%d).", p_from_column, p_to_column, text.length()));
  1124. text.erase(p_from_column, p_to_column - p_from_column);
  1125. _shape();
  1126. cursor_pos -= CLAMP(cursor_pos - p_from_column, 0, p_to_column - p_from_column);
  1127. if (cursor_pos >= text.length()) {
  1128. cursor_pos = text.length();
  1129. }
  1130. if (!text_changed_dirty) {
  1131. if (is_inside_tree()) {
  1132. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  1133. }
  1134. text_changed_dirty = true;
  1135. }
  1136. }
  1137. void LineEdit::set_text(String p_text) {
  1138. clear_internal();
  1139. append_at_cursor(p_text);
  1140. update();
  1141. cursor_pos = 0;
  1142. scroll_offset = 0;
  1143. }
  1144. void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
  1145. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1146. if (text_direction != p_text_direction) {
  1147. text_direction = p_text_direction;
  1148. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  1149. input_direction = text_direction;
  1150. }
  1151. _shape();
  1152. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  1153. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  1154. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  1155. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  1156. update();
  1157. }
  1158. }
  1159. Control::TextDirection LineEdit::get_text_direction() const {
  1160. return text_direction;
  1161. }
  1162. void LineEdit::clear_opentype_features() {
  1163. opentype_features.clear();
  1164. _shape();
  1165. update();
  1166. }
  1167. void LineEdit::set_opentype_feature(const String &p_name, int p_value) {
  1168. int32_t tag = TS->name_to_tag(p_name);
  1169. if (!opentype_features.has(tag) || (int)opentype_features[tag] != p_value) {
  1170. opentype_features[tag] = p_value;
  1171. _shape();
  1172. update();
  1173. }
  1174. }
  1175. int LineEdit::get_opentype_feature(const String &p_name) const {
  1176. int32_t tag = TS->name_to_tag(p_name);
  1177. if (!opentype_features.has(tag)) {
  1178. return -1;
  1179. }
  1180. return opentype_features[tag];
  1181. }
  1182. void LineEdit::set_language(const String &p_language) {
  1183. if (language != p_language) {
  1184. language = p_language;
  1185. _shape();
  1186. update();
  1187. }
  1188. }
  1189. String LineEdit::get_language() const {
  1190. return language;
  1191. }
  1192. void LineEdit::set_draw_control_chars(bool p_draw_control_chars) {
  1193. if (draw_control_chars != p_draw_control_chars) {
  1194. draw_control_chars = p_draw_control_chars;
  1195. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  1196. _shape();
  1197. update();
  1198. }
  1199. }
  1200. bool LineEdit::get_draw_control_chars() const {
  1201. return draw_control_chars;
  1202. }
  1203. void LineEdit::set_structured_text_bidi_override(Control::StructuredTextParser p_parser) {
  1204. if (st_parser != p_parser) {
  1205. st_parser = p_parser;
  1206. _shape();
  1207. update();
  1208. }
  1209. }
  1210. Control::StructuredTextParser LineEdit::get_structured_text_bidi_override() const {
  1211. return st_parser;
  1212. }
  1213. void LineEdit::set_structured_text_bidi_override_options(Array p_args) {
  1214. st_args = p_args;
  1215. _shape();
  1216. update();
  1217. }
  1218. Array LineEdit::get_structured_text_bidi_override_options() const {
  1219. return st_args;
  1220. }
  1221. void LineEdit::clear() {
  1222. clear_internal();
  1223. _text_changed();
  1224. }
  1225. String LineEdit::get_text() const {
  1226. return text;
  1227. }
  1228. void LineEdit::set_placeholder(String p_text) {
  1229. placeholder = p_text;
  1230. placeholder_translated = tr(placeholder);
  1231. _shape();
  1232. update();
  1233. }
  1234. String LineEdit::get_placeholder() const {
  1235. return placeholder;
  1236. }
  1237. void LineEdit::set_placeholder_alpha(float p_alpha) {
  1238. placeholder_alpha = p_alpha;
  1239. update();
  1240. }
  1241. float LineEdit::get_placeholder_alpha() const {
  1242. return placeholder_alpha;
  1243. }
  1244. void LineEdit::set_cursor_position(int p_pos) {
  1245. if (p_pos > (int)text.length()) {
  1246. p_pos = text.length();
  1247. }
  1248. if (p_pos < 0) {
  1249. p_pos = 0;
  1250. }
  1251. cursor_pos = p_pos;
  1252. // Fit to window.
  1253. if (!is_inside_tree()) {
  1254. scroll_offset = 0;
  1255. return;
  1256. }
  1257. Ref<StyleBox> style = get_theme_stylebox("normal");
  1258. bool rtl = is_layout_rtl();
  1259. int x_ofs = 0;
  1260. float text_width = TS->shaped_text_get_size(text_rid).x;
  1261. switch (align) {
  1262. case ALIGN_FILL:
  1263. case ALIGN_LEFT: {
  1264. if (rtl) {
  1265. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  1266. } else {
  1267. x_ofs = style->get_offset().x;
  1268. }
  1269. } break;
  1270. case ALIGN_CENTER: {
  1271. if (scroll_offset != 0) {
  1272. x_ofs = style->get_offset().x;
  1273. } else {
  1274. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - (text_width)) / 2);
  1275. }
  1276. } break;
  1277. case ALIGN_RIGHT: {
  1278. if (rtl) {
  1279. x_ofs = style->get_offset().x;
  1280. } else {
  1281. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - style->get_margin(MARGIN_RIGHT) - (text_width)));
  1282. }
  1283. } break;
  1284. }
  1285. int ofs_max = get_size().width - style->get_margin(MARGIN_RIGHT);
  1286. bool using_placeholder = text.empty() && ime_text.empty();
  1287. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1288. if (right_icon.is_valid() || display_clear_icon) {
  1289. Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon("clear") : right_icon;
  1290. if (align == ALIGN_CENTER) {
  1291. if (scroll_offset == 0) {
  1292. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2);
  1293. }
  1294. } else {
  1295. x_ofs = MAX(style->get_margin(MARGIN_LEFT), x_ofs - r_icon->get_width() - style->get_margin(MARGIN_RIGHT));
  1296. }
  1297. ofs_max -= r_icon->get_width();
  1298. }
  1299. // Note: Use too coordinates to fit IME input range.
  1300. Vector2i primary_catret_offset = get_cursor_pixel_pos();
  1301. if (MIN(primary_catret_offset.x, primary_catret_offset.y) <= x_ofs) {
  1302. scroll_offset += (x_ofs - MIN(primary_catret_offset.x, primary_catret_offset.y));
  1303. } else if (MAX(primary_catret_offset.x, primary_catret_offset.y) >= ofs_max) {
  1304. scroll_offset += (ofs_max - MAX(primary_catret_offset.x, primary_catret_offset.y));
  1305. }
  1306. scroll_offset = MIN(0, scroll_offset);
  1307. update();
  1308. }
  1309. int LineEdit::get_cursor_position() const {
  1310. return cursor_pos;
  1311. }
  1312. void LineEdit::set_scroll_offset(int p_pos) {
  1313. scroll_offset = p_pos;
  1314. if (scroll_offset < 0) {
  1315. scroll_offset = 0;
  1316. }
  1317. }
  1318. int LineEdit::get_scroll_offset() const {
  1319. return scroll_offset;
  1320. }
  1321. void LineEdit::append_at_cursor(String p_text) {
  1322. if ((max_length <= 0) || (text.length() + p_text.length() <= max_length)) {
  1323. String pre = text.substr(0, cursor_pos);
  1324. String post = text.substr(cursor_pos, text.length() - cursor_pos);
  1325. text = pre + p_text + post;
  1326. _shape();
  1327. TextServer::Direction dir = TS->shaped_text_get_dominant_direciton_in_range(text_rid, cursor_pos, cursor_pos + p_text.length());
  1328. if (dir != TextServer::DIRECTION_AUTO) {
  1329. input_direction = (TextDirection)dir;
  1330. }
  1331. set_cursor_position(cursor_pos + p_text.length());
  1332. } else {
  1333. emit_signal("text_change_rejected");
  1334. }
  1335. }
  1336. void LineEdit::clear_internal() {
  1337. deselect();
  1338. _clear_undo_stack();
  1339. cursor_pos = 0;
  1340. scroll_offset = 0;
  1341. undo_text = "";
  1342. text = "";
  1343. _shape();
  1344. update();
  1345. }
  1346. Size2 LineEdit::get_minimum_size() const {
  1347. Ref<StyleBox> style = get_theme_stylebox("normal");
  1348. Ref<Font> font = get_theme_font("font");
  1349. int font_size = get_theme_font_size("font_size");
  1350. Size2 min_size;
  1351. // Minimum size of text.
  1352. int space_size = font->get_char_size('m', 0, font_size).x;
  1353. min_size.width = get_theme_constant("minimum_spaces") * space_size;
  1354. if (expand_to_text_length) {
  1355. // Add a space because some fonts are too exact, and because cursor needs a bit more when at the end.
  1356. min_size.width = MAX(min_size.width, full_width + space_size);
  1357. }
  1358. min_size.height = MAX(TS->shaped_text_get_size(text_rid).y + font->get_spacing(Font::SPACING_TOP) + font->get_spacing(Font::SPACING_BOTTOM), font->get_height(font_size));
  1359. // Take icons into account.
  1360. bool using_placeholder = text.empty() && ime_text.empty();
  1361. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1362. if (right_icon.is_valid() || display_clear_icon) {
  1363. Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon("clear") : right_icon;
  1364. min_size.width += r_icon->get_width();
  1365. min_size.height = MAX(min_size.height, r_icon->get_height());
  1366. }
  1367. return style->get_minimum_size() + min_size;
  1368. }
  1369. void LineEdit::deselect() {
  1370. selection.begin = 0;
  1371. selection.end = 0;
  1372. selection.cursor_start = 0;
  1373. selection.enabled = false;
  1374. selection.creating = false;
  1375. selection.doubleclick = false;
  1376. update();
  1377. }
  1378. void LineEdit::selection_delete() {
  1379. if (selection.enabled) {
  1380. delete_text(selection.begin, selection.end);
  1381. }
  1382. deselect();
  1383. }
  1384. void LineEdit::set_max_length(int p_max_length) {
  1385. ERR_FAIL_COND(p_max_length < 0);
  1386. max_length = p_max_length;
  1387. set_text(text);
  1388. }
  1389. int LineEdit::get_max_length() const {
  1390. return max_length;
  1391. }
  1392. void LineEdit::selection_fill_at_cursor() {
  1393. if (!selecting_enabled) {
  1394. return;
  1395. }
  1396. selection.begin = cursor_pos;
  1397. selection.end = selection.cursor_start;
  1398. if (selection.end < selection.begin) {
  1399. int aux = selection.end;
  1400. selection.end = selection.begin;
  1401. selection.begin = aux;
  1402. }
  1403. selection.enabled = (selection.begin != selection.end);
  1404. }
  1405. void LineEdit::select_all() {
  1406. if (!selecting_enabled) {
  1407. return;
  1408. }
  1409. if (!text.length()) {
  1410. return;
  1411. }
  1412. selection.begin = 0;
  1413. selection.end = text.length();
  1414. selection.enabled = true;
  1415. update();
  1416. }
  1417. void LineEdit::set_editable(bool p_editable) {
  1418. if (editable == p_editable) {
  1419. return;
  1420. }
  1421. editable = p_editable;
  1422. _generate_context_menu();
  1423. minimum_size_changed();
  1424. update();
  1425. }
  1426. bool LineEdit::is_editable() const {
  1427. return editable;
  1428. }
  1429. void LineEdit::set_secret(bool p_secret) {
  1430. if (pass != p_secret) {
  1431. pass = p_secret;
  1432. _shape();
  1433. }
  1434. update();
  1435. }
  1436. bool LineEdit::is_secret() const {
  1437. return pass;
  1438. }
  1439. void LineEdit::set_secret_character(const String &p_string) {
  1440. // An empty string as the secret character would crash the engine.
  1441. // It also wouldn't make sense to use multiple characters as the secret character.
  1442. ERR_FAIL_COND_MSG(p_string.length() != 1, "Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given).");
  1443. if (secret_character != p_string) {
  1444. secret_character = p_string;
  1445. _shape();
  1446. }
  1447. update();
  1448. }
  1449. String LineEdit::get_secret_character() const {
  1450. return secret_character;
  1451. }
  1452. void LineEdit::select(int p_from, int p_to) {
  1453. if (!selecting_enabled) {
  1454. return;
  1455. }
  1456. if (p_from == 0 && p_to == 0) {
  1457. deselect();
  1458. return;
  1459. }
  1460. int len = text.length();
  1461. if (p_from < 0) {
  1462. p_from = 0;
  1463. }
  1464. if (p_from > len) {
  1465. p_from = len;
  1466. }
  1467. if (p_to < 0 || p_to > len) {
  1468. p_to = len;
  1469. }
  1470. if (p_from >= p_to) {
  1471. return;
  1472. }
  1473. selection.enabled = true;
  1474. selection.begin = p_from;
  1475. selection.end = p_to;
  1476. selection.creating = false;
  1477. selection.doubleclick = false;
  1478. update();
  1479. }
  1480. bool LineEdit::is_text_field() const {
  1481. return true;
  1482. }
  1483. void LineEdit::menu_option(int p_option) {
  1484. switch (p_option) {
  1485. case MENU_CUT: {
  1486. if (editable) {
  1487. cut_text();
  1488. }
  1489. } break;
  1490. case MENU_COPY: {
  1491. copy_text();
  1492. } break;
  1493. case MENU_PASTE: {
  1494. if (editable) {
  1495. paste_text();
  1496. }
  1497. } break;
  1498. case MENU_CLEAR: {
  1499. if (editable) {
  1500. clear();
  1501. }
  1502. } break;
  1503. case MENU_SELECT_ALL: {
  1504. select_all();
  1505. } break;
  1506. case MENU_UNDO: {
  1507. if (editable) {
  1508. undo();
  1509. }
  1510. } break;
  1511. case MENU_REDO: {
  1512. if (editable) {
  1513. redo();
  1514. }
  1515. } break;
  1516. case MENU_DIR_INHERITED: {
  1517. set_text_direction(TEXT_DIRECTION_INHERITED);
  1518. } break;
  1519. case MENU_DIR_AUTO: {
  1520. set_text_direction(TEXT_DIRECTION_AUTO);
  1521. } break;
  1522. case MENU_DIR_LTR: {
  1523. set_text_direction(TEXT_DIRECTION_LTR);
  1524. } break;
  1525. case MENU_DIR_RTL: {
  1526. set_text_direction(TEXT_DIRECTION_RTL);
  1527. } break;
  1528. case MENU_DISPLAY_UCC: {
  1529. set_draw_control_chars(!get_draw_control_chars());
  1530. } break;
  1531. case MENU_INSERT_LRM: {
  1532. if (editable) {
  1533. append_at_cursor(String::chr(0x200E));
  1534. }
  1535. } break;
  1536. case MENU_INSERT_RLM: {
  1537. if (editable) {
  1538. append_at_cursor(String::chr(0x200F));
  1539. }
  1540. } break;
  1541. case MENU_INSERT_LRE: {
  1542. if (editable) {
  1543. append_at_cursor(String::chr(0x202A));
  1544. }
  1545. } break;
  1546. case MENU_INSERT_RLE: {
  1547. if (editable) {
  1548. append_at_cursor(String::chr(0x202B));
  1549. }
  1550. } break;
  1551. case MENU_INSERT_LRO: {
  1552. if (editable) {
  1553. append_at_cursor(String::chr(0x202D));
  1554. }
  1555. } break;
  1556. case MENU_INSERT_RLO: {
  1557. if (editable) {
  1558. append_at_cursor(String::chr(0x202E));
  1559. }
  1560. } break;
  1561. case MENU_INSERT_PDF: {
  1562. if (editable) {
  1563. append_at_cursor(String::chr(0x202C));
  1564. }
  1565. } break;
  1566. case MENU_INSERT_ALM: {
  1567. if (editable) {
  1568. append_at_cursor(String::chr(0x061C));
  1569. }
  1570. } break;
  1571. case MENU_INSERT_LRI: {
  1572. if (editable) {
  1573. append_at_cursor(String::chr(0x2066));
  1574. }
  1575. } break;
  1576. case MENU_INSERT_RLI: {
  1577. if (editable) {
  1578. append_at_cursor(String::chr(0x2067));
  1579. }
  1580. } break;
  1581. case MENU_INSERT_FSI: {
  1582. if (editable) {
  1583. append_at_cursor(String::chr(0x2068));
  1584. }
  1585. } break;
  1586. case MENU_INSERT_PDI: {
  1587. if (editable) {
  1588. append_at_cursor(String::chr(0x2069));
  1589. }
  1590. } break;
  1591. case MENU_INSERT_ZWJ: {
  1592. if (editable) {
  1593. append_at_cursor(String::chr(0x200D));
  1594. }
  1595. } break;
  1596. case MENU_INSERT_ZWNJ: {
  1597. if (editable) {
  1598. append_at_cursor(String::chr(0x200C));
  1599. }
  1600. } break;
  1601. case MENU_INSERT_WJ: {
  1602. if (editable) {
  1603. append_at_cursor(String::chr(0x2060));
  1604. }
  1605. } break;
  1606. case MENU_INSERT_SHY: {
  1607. if (editable) {
  1608. append_at_cursor(String::chr(0x00AD));
  1609. }
  1610. }
  1611. }
  1612. }
  1613. void LineEdit::set_context_menu_enabled(bool p_enable) {
  1614. context_menu_enabled = p_enable;
  1615. }
  1616. bool LineEdit::is_context_menu_enabled() {
  1617. return context_menu_enabled;
  1618. }
  1619. PopupMenu *LineEdit::get_menu() const {
  1620. return menu;
  1621. }
  1622. void LineEdit::_editor_settings_changed() {
  1623. #ifdef TOOLS_ENABLED
  1624. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  1625. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  1626. #endif
  1627. }
  1628. void LineEdit::set_expand_to_text_length(bool p_enabled) {
  1629. expand_to_text_length = p_enabled;
  1630. minimum_size_changed();
  1631. set_cursor_position(cursor_pos);
  1632. }
  1633. bool LineEdit::get_expand_to_text_length() const {
  1634. return expand_to_text_length;
  1635. }
  1636. void LineEdit::set_clear_button_enabled(bool p_enabled) {
  1637. if (clear_button_enabled == p_enabled) {
  1638. return;
  1639. }
  1640. clear_button_enabled = p_enabled;
  1641. _fit_to_width();
  1642. minimum_size_changed();
  1643. update();
  1644. }
  1645. bool LineEdit::is_clear_button_enabled() const {
  1646. return clear_button_enabled;
  1647. }
  1648. void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
  1649. shortcut_keys_enabled = p_enabled;
  1650. _generate_context_menu();
  1651. }
  1652. bool LineEdit::is_shortcut_keys_enabled() const {
  1653. return shortcut_keys_enabled;
  1654. }
  1655. void LineEdit::set_virtual_keyboard_enabled(bool p_enable) {
  1656. virtual_keyboard_enabled = p_enable;
  1657. }
  1658. bool LineEdit::is_virtual_keyboard_enabled() const {
  1659. return virtual_keyboard_enabled;
  1660. }
  1661. void LineEdit::set_selecting_enabled(bool p_enabled) {
  1662. selecting_enabled = p_enabled;
  1663. if (!selecting_enabled) {
  1664. deselect();
  1665. }
  1666. _generate_context_menu();
  1667. }
  1668. bool LineEdit::is_selecting_enabled() const {
  1669. return selecting_enabled;
  1670. }
  1671. void LineEdit::set_right_icon(const Ref<Texture2D> &p_icon) {
  1672. if (right_icon == p_icon) {
  1673. return;
  1674. }
  1675. right_icon = p_icon;
  1676. _fit_to_width();
  1677. minimum_size_changed();
  1678. update();
  1679. }
  1680. Ref<Texture2D> LineEdit::get_right_icon() {
  1681. return right_icon;
  1682. }
  1683. void LineEdit::_text_changed() {
  1684. _emit_text_change();
  1685. _clear_redo();
  1686. }
  1687. void LineEdit::_emit_text_change() {
  1688. emit_signal("text_changed", text);
  1689. _change_notify("text");
  1690. text_changed_dirty = false;
  1691. }
  1692. void LineEdit::_shape() {
  1693. Size2 old_size = TS->shaped_text_get_size(text_rid);
  1694. TS->shaped_text_clear(text_rid);
  1695. String t;
  1696. if (text.length() == 0) {
  1697. t = placeholder_translated;
  1698. } else if (pass) {
  1699. t = secret_character.repeat(text.length() + ime_text.length());
  1700. } else {
  1701. if (ime_text.length() > 0) {
  1702. t = text.substr(0, cursor_pos) + ime_text + text.substr(cursor_pos, text.length());
  1703. } else {
  1704. t = text;
  1705. }
  1706. }
  1707. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1708. TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1709. } else {
  1710. TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
  1711. }
  1712. TS->shaped_text_set_preserve_control(text_rid, draw_control_chars);
  1713. const Ref<Font> &font = get_theme_font("font");
  1714. int font_size = get_theme_font_size("font_size");
  1715. TS->shaped_text_add_string(text_rid, t, font->get_rids(), font_size, opentype_features, (language != "") ? language : TranslationServer::get_singleton()->get_tool_locale());
  1716. TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, t));
  1717. full_width = TS->shaped_text_get_size(text_rid).x;
  1718. _fit_to_width();
  1719. Size2 size = TS->shaped_text_get_size(text_rid);
  1720. if ((expand_to_text_length && old_size.x != size.x) || (old_size.y != size.y)) {
  1721. minimum_size_changed();
  1722. }
  1723. }
  1724. void LineEdit::_fit_to_width() {
  1725. if (align == ALIGN_FILL) {
  1726. Ref<StyleBox> style = get_theme_stylebox("normal");
  1727. int t_width = get_size().width - style->get_margin(MARGIN_RIGHT) - style->get_margin(MARGIN_LEFT);
  1728. bool using_placeholder = text.empty() && ime_text.empty();
  1729. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1730. if (right_icon.is_valid() || display_clear_icon) {
  1731. Ref<Texture2D> r_icon = display_clear_icon ? Control::get_theme_icon("clear") : right_icon;
  1732. t_width -= r_icon->get_width();
  1733. }
  1734. TS->shaped_text_fit_to_width(text_rid, MAX(t_width, full_width));
  1735. }
  1736. }
  1737. void LineEdit::_clear_redo() {
  1738. _create_undo_state();
  1739. if (undo_stack_pos == nullptr) {
  1740. return;
  1741. }
  1742. undo_stack_pos = undo_stack_pos->next();
  1743. while (undo_stack_pos) {
  1744. List<TextOperation>::Element *elem = undo_stack_pos;
  1745. undo_stack_pos = undo_stack_pos->next();
  1746. undo_stack.erase(elem);
  1747. }
  1748. _create_undo_state();
  1749. }
  1750. void LineEdit::_clear_undo_stack() {
  1751. undo_stack.clear();
  1752. undo_stack_pos = nullptr;
  1753. _create_undo_state();
  1754. }
  1755. void LineEdit::_create_undo_state() {
  1756. TextOperation op;
  1757. op.text = text;
  1758. op.cursor_pos = cursor_pos;
  1759. op.scroll_offset = scroll_offset;
  1760. undo_stack.push_back(op);
  1761. }
  1762. void LineEdit::_generate_context_menu() {
  1763. // Reorganize context menu.
  1764. menu->clear();
  1765. if (editable) {
  1766. menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0);
  1767. }
  1768. menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0);
  1769. if (editable) {
  1770. menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0);
  1771. }
  1772. menu->add_separator();
  1773. if (is_selecting_enabled()) {
  1774. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0);
  1775. }
  1776. if (editable) {
  1777. menu->add_item(RTR("Clear"), MENU_CLEAR);
  1778. menu->add_separator();
  1779. menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0);
  1780. menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0);
  1781. }
  1782. menu->add_separator();
  1783. menu->add_submenu_item(RTR("Text writing direction"), "DirMenu");
  1784. menu->add_separator();
  1785. menu->add_check_item(RTR("Display control characters"), MENU_DISPLAY_UCC);
  1786. if (editable) {
  1787. menu->add_submenu_item(RTR("Insert control character"), "CTLMenu");
  1788. }
  1789. }
  1790. bool LineEdit::_set(const StringName &p_name, const Variant &p_value) {
  1791. String str = p_name;
  1792. if (str.begins_with("opentype_features/")) {
  1793. String name = str.get_slicec('/', 1);
  1794. int32_t tag = TS->name_to_tag(name);
  1795. double value = p_value;
  1796. if (value == -1) {
  1797. if (opentype_features.has(tag)) {
  1798. opentype_features.erase(tag);
  1799. _shape();
  1800. update();
  1801. }
  1802. } else {
  1803. if ((double)opentype_features[tag] != value) {
  1804. opentype_features[tag] = value;
  1805. _shape();
  1806. update();
  1807. }
  1808. }
  1809. _change_notify();
  1810. return true;
  1811. }
  1812. return false;
  1813. }
  1814. bool LineEdit::_get(const StringName &p_name, Variant &r_ret) const {
  1815. String str = p_name;
  1816. if (str.begins_with("opentype_features/")) {
  1817. String name = str.get_slicec('/', 1);
  1818. int32_t tag = TS->name_to_tag(name);
  1819. if (opentype_features.has(tag)) {
  1820. r_ret = opentype_features[tag];
  1821. return true;
  1822. } else {
  1823. r_ret = -1;
  1824. return true;
  1825. }
  1826. }
  1827. return false;
  1828. }
  1829. void LineEdit::_get_property_list(List<PropertyInfo> *p_list) const {
  1830. for (const Variant *ftr = opentype_features.next(nullptr); ftr != nullptr; ftr = opentype_features.next(ftr)) {
  1831. String name = TS->tag_to_name(*ftr);
  1832. p_list->push_back(PropertyInfo(Variant::FLOAT, "opentype_features/" + name));
  1833. }
  1834. p_list->push_back(PropertyInfo(Variant::NIL, "opentype_features/_new", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR));
  1835. }
  1836. void LineEdit::_bind_methods() {
  1837. ClassDB::bind_method(D_METHOD("_text_changed"), &LineEdit::_text_changed);
  1838. ClassDB::bind_method(D_METHOD("set_align", "align"), &LineEdit::set_align);
  1839. ClassDB::bind_method(D_METHOD("get_align"), &LineEdit::get_align);
  1840. ClassDB::bind_method(D_METHOD("_gui_input"), &LineEdit::_gui_input);
  1841. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  1842. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  1843. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  1844. ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect);
  1845. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  1846. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  1847. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &LineEdit::get_draw_control_chars);
  1848. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enable"), &LineEdit::set_draw_control_chars);
  1849. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LineEdit::set_text_direction);
  1850. ClassDB::bind_method(D_METHOD("get_text_direction"), &LineEdit::get_text_direction);
  1851. ClassDB::bind_method(D_METHOD("set_opentype_feature", "tag", "value"), &LineEdit::set_opentype_feature);
  1852. ClassDB::bind_method(D_METHOD("get_opentype_feature", "tag"), &LineEdit::get_opentype_feature);
  1853. ClassDB::bind_method(D_METHOD("clear_opentype_features"), &LineEdit::clear_opentype_features);
  1854. ClassDB::bind_method(D_METHOD("set_language", "language"), &LineEdit::set_language);
  1855. ClassDB::bind_method(D_METHOD("get_language"), &LineEdit::get_language);
  1856. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LineEdit::set_structured_text_bidi_override);
  1857. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LineEdit::get_structured_text_bidi_override);
  1858. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LineEdit::set_structured_text_bidi_override_options);
  1859. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LineEdit::get_structured_text_bidi_override_options);
  1860. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  1861. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  1862. ClassDB::bind_method(D_METHOD("set_placeholder_alpha", "alpha"), &LineEdit::set_placeholder_alpha);
  1863. ClassDB::bind_method(D_METHOD("get_placeholder_alpha"), &LineEdit::get_placeholder_alpha);
  1864. ClassDB::bind_method(D_METHOD("set_cursor_position", "position"), &LineEdit::set_cursor_position);
  1865. ClassDB::bind_method(D_METHOD("get_cursor_position"), &LineEdit::get_cursor_position);
  1866. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset);
  1867. ClassDB::bind_method(D_METHOD("set_expand_to_text_length", "enabled"), &LineEdit::set_expand_to_text_length);
  1868. ClassDB::bind_method(D_METHOD("get_expand_to_text_length"), &LineEdit::get_expand_to_text_length);
  1869. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enabled"), &LineEdit::cursor_set_blink_enabled);
  1870. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &LineEdit::cursor_get_blink_enabled);
  1871. ClassDB::bind_method(D_METHOD("set_mid_grapheme_caret_enabled", "enabled"), &LineEdit::set_mid_grapheme_caret_enabled);
  1872. ClassDB::bind_method(D_METHOD("get_mid_grapheme_caret_enabled"), &LineEdit::get_mid_grapheme_caret_enabled);
  1873. ClassDB::bind_method(D_METHOD("cursor_set_force_displayed", "enabled"), &LineEdit::cursor_set_force_displayed);
  1874. ClassDB::bind_method(D_METHOD("cursor_get_force_displayed"), &LineEdit::cursor_get_force_displayed);
  1875. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &LineEdit::cursor_set_blink_speed);
  1876. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &LineEdit::cursor_get_blink_speed);
  1877. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  1878. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  1879. ClassDB::bind_method(D_METHOD("append_at_cursor", "text"), &LineEdit::append_at_cursor);
  1880. ClassDB::bind_method(D_METHOD("delete_char_at_cursor"), &LineEdit::delete_char);
  1881. ClassDB::bind_method(D_METHOD("delete_text", "from_column", "to_column"), &LineEdit::delete_text);
  1882. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  1883. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  1884. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  1885. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  1886. ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
  1887. ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
  1888. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  1889. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  1890. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
  1891. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled);
  1892. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &LineEdit::set_virtual_keyboard_enabled);
  1893. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &LineEdit::is_virtual_keyboard_enabled);
  1894. ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled);
  1895. ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled);
  1896. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled);
  1897. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled);
  1898. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled);
  1899. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled);
  1900. ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
  1901. ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
  1902. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text")));
  1903. ADD_SIGNAL(MethodInfo("text_change_rejected"));
  1904. ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "new_text")));
  1905. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  1906. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  1907. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  1908. BIND_ENUM_CONSTANT(ALIGN_FILL);
  1909. BIND_ENUM_CONSTANT(MENU_CUT);
  1910. BIND_ENUM_CONSTANT(MENU_COPY);
  1911. BIND_ENUM_CONSTANT(MENU_PASTE);
  1912. BIND_ENUM_CONSTANT(MENU_CLEAR);
  1913. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  1914. BIND_ENUM_CONSTANT(MENU_UNDO);
  1915. BIND_ENUM_CONSTANT(MENU_REDO);
  1916. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  1917. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  1918. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  1919. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  1920. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  1921. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  1922. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  1923. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  1924. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  1925. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  1926. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  1927. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  1928. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  1929. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  1930. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  1931. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  1932. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  1933. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  1934. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  1935. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  1936. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  1937. BIND_ENUM_CONSTANT(MENU_MAX);
  1938. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  1939. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  1940. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length"), "set_max_length", "get_max_length");
  1941. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  1942. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret");
  1943. ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
  1944. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length", "get_expand_to_text_length");
  1945. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  1946. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  1947. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled");
  1948. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  1949. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  1950. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_right_icon", "get_right_icon");
  1951. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,LTR,RTL,Inherited"), "set_text_direction", "get_text_direction");
  1952. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language"), "set_language", "get_language");
  1953. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  1954. ADD_GROUP("Structured Text", "structured_text_");
  1955. ADD_PROPERTY(PropertyInfo(Variant::INT, "structured_text_bidi_override", PROPERTY_HINT_ENUM, "Default,URI,File,Email,List,None,Custom"), "set_structured_text_bidi_override", "get_structured_text_bidi_override");
  1956. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  1957. ADD_GROUP("Placeholder", "placeholder_");
  1958. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  1959. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "placeholder_alpha", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_placeholder_alpha", "get_placeholder_alpha");
  1960. ADD_GROUP("Caret", "caret_");
  1961. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  1962. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  1963. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_position"), "set_cursor_position", "get_cursor_position");
  1964. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_force_displayed"), "cursor_set_force_displayed", "cursor_get_force_displayed");
  1965. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_mid_grapheme_caret_enabled", "get_mid_grapheme_caret_enabled");
  1966. }
  1967. LineEdit::LineEdit() {
  1968. text_rid = TS->create_shaped_text();
  1969. _create_undo_state();
  1970. clear_button_status.press_attempt = false;
  1971. clear_button_status.pressing_inside = false;
  1972. deselect();
  1973. set_focus_mode(FOCUS_ALL);
  1974. set_default_cursor_shape(CURSOR_IBEAM);
  1975. set_mouse_filter(MOUSE_FILTER_STOP);
  1976. caret_blink_timer = memnew(Timer);
  1977. add_child(caret_blink_timer);
  1978. caret_blink_timer->set_wait_time(0.65);
  1979. caret_blink_timer->connect("timeout", callable_mp(this, &LineEdit::_toggle_draw_caret));
  1980. cursor_set_blink_enabled(false);
  1981. menu = memnew(PopupMenu);
  1982. add_child(menu);
  1983. menu_dir = memnew(PopupMenu);
  1984. menu_dir->set_name("DirMenu");
  1985. menu_dir->add_radio_check_item(RTR("Same as layout direction"), MENU_DIR_INHERITED);
  1986. menu_dir->add_radio_check_item(RTR("Auto-detect direction"), MENU_DIR_AUTO);
  1987. menu_dir->add_radio_check_item(RTR("Left-to-right"), MENU_DIR_LTR);
  1988. menu_dir->add_radio_check_item(RTR("Right-to-left"), MENU_DIR_RTL);
  1989. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), true);
  1990. menu->add_child(menu_dir);
  1991. menu_ctl = memnew(PopupMenu);
  1992. menu_ctl->set_name("CTLMenu");
  1993. menu_ctl->add_item(RTR("Left-to-right mark (LRM)"), MENU_INSERT_LRM);
  1994. menu_ctl->add_item(RTR("Right-to-left mark (RLM)"), MENU_INSERT_RLM);
  1995. menu_ctl->add_item(RTR("Start of left-to-right embedding (LRE)"), MENU_INSERT_LRE);
  1996. menu_ctl->add_item(RTR("Start of right-to-left embedding (RLE)"), MENU_INSERT_RLE);
  1997. menu_ctl->add_item(RTR("Start of left-to-right override (LRO)"), MENU_INSERT_LRO);
  1998. menu_ctl->add_item(RTR("Start of right-to-left override (RLO)"), MENU_INSERT_RLO);
  1999. menu_ctl->add_item(RTR("Pop direction formatting (PDF)"), MENU_INSERT_PDF);
  2000. menu_ctl->add_separator();
  2001. menu_ctl->add_item(RTR("Arabic letter mark (ALM)"), MENU_INSERT_ALM);
  2002. menu_ctl->add_item(RTR("Left-to-right isolate (LRI)"), MENU_INSERT_LRI);
  2003. menu_ctl->add_item(RTR("Right-to-left isolate (RLI)"), MENU_INSERT_RLI);
  2004. menu_ctl->add_item(RTR("First strong isolate (FSI)"), MENU_INSERT_FSI);
  2005. menu_ctl->add_item(RTR("Pop direction isolate (PDI)"), MENU_INSERT_PDI);
  2006. menu_ctl->add_separator();
  2007. menu_ctl->add_item(RTR("Zero width joiner (ZWJ)"), MENU_INSERT_ZWJ);
  2008. menu_ctl->add_item(RTR("Zero width non-joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  2009. menu_ctl->add_item(RTR("Word joiner (WJ)"), MENU_INSERT_WJ);
  2010. menu_ctl->add_item(RTR("Soft hyphen (SHY)"), MENU_INSERT_SHY);
  2011. menu->add_child(menu_ctl);
  2012. set_editable(true); // Initialise to opposite first, so we get past the early-out in set_editable.
  2013. menu->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2014. menu_dir->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2015. menu_ctl->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2016. }
  2017. LineEdit::~LineEdit() {
  2018. TS->free(text_rid);
  2019. }