line_edit.cpp 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654
  1. /**************************************************************************/
  2. /* line_edit.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "line_edit.h"
  31. #include "core/input/input_map.h"
  32. #include "core/object/message_queue.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/os/os.h"
  35. #include "core/string/print_string.h"
  36. #include "core/string/translation.h"
  37. #include "label.h"
  38. #include "servers/display_server.h"
  39. #include "servers/text_server.h"
  40. #ifdef TOOLS_ENABLED
  41. #include "editor/editor_settings.h"
  42. #endif
  43. #include "scene/main/window.h"
  44. void LineEdit::_swap_current_input_direction() {
  45. if (input_direction == TEXT_DIRECTION_LTR) {
  46. input_direction = TEXT_DIRECTION_RTL;
  47. } else {
  48. input_direction = TEXT_DIRECTION_LTR;
  49. }
  50. set_caret_column(get_caret_column());
  51. queue_redraw();
  52. }
  53. void LineEdit::_move_caret_left(bool p_select, bool p_move_by_word) {
  54. if (selection.enabled && !p_select) {
  55. set_caret_column(selection.begin);
  56. deselect();
  57. return;
  58. }
  59. shift_selection_check_pre(p_select);
  60. if (p_move_by_word) {
  61. int cc = caret_column;
  62. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  63. for (int i = words.size() - 2; i >= 0; i = i - 2) {
  64. if (words[i] < cc) {
  65. cc = words[i];
  66. break;
  67. }
  68. }
  69. set_caret_column(cc);
  70. } else {
  71. if (caret_mid_grapheme_enabled) {
  72. set_caret_column(get_caret_column() - 1);
  73. } else {
  74. set_caret_column(TS->shaped_text_prev_grapheme_pos(text_rid, get_caret_column()));
  75. }
  76. }
  77. shift_selection_check_post(p_select);
  78. _reset_caret_blink_timer();
  79. }
  80. void LineEdit::_move_caret_right(bool p_select, bool p_move_by_word) {
  81. if (selection.enabled && !p_select) {
  82. set_caret_column(selection.end);
  83. deselect();
  84. return;
  85. }
  86. shift_selection_check_pre(p_select);
  87. if (p_move_by_word) {
  88. int cc = caret_column;
  89. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  90. for (int i = 1; i < words.size(); i = i + 2) {
  91. if (words[i] > cc) {
  92. cc = words[i];
  93. break;
  94. }
  95. }
  96. set_caret_column(cc);
  97. } else {
  98. if (caret_mid_grapheme_enabled) {
  99. set_caret_column(get_caret_column() + 1);
  100. } else {
  101. set_caret_column(TS->shaped_text_next_grapheme_pos(text_rid, get_caret_column()));
  102. }
  103. }
  104. shift_selection_check_post(p_select);
  105. _reset_caret_blink_timer();
  106. }
  107. void LineEdit::_move_caret_start(bool p_select) {
  108. shift_selection_check_pre(p_select);
  109. set_caret_column(0);
  110. shift_selection_check_post(p_select);
  111. }
  112. void LineEdit::_move_caret_end(bool p_select) {
  113. shift_selection_check_pre(p_select);
  114. set_caret_column(text.length());
  115. shift_selection_check_post(p_select);
  116. }
  117. void LineEdit::_backspace(bool p_word, bool p_all_to_left) {
  118. if (!editable) {
  119. return;
  120. }
  121. if (p_all_to_left) {
  122. deselect();
  123. text = text.substr(0, caret_column);
  124. _text_changed();
  125. return;
  126. }
  127. if (selection.enabled) {
  128. selection_delete();
  129. return;
  130. }
  131. if (p_word) {
  132. int cc = caret_column;
  133. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  134. for (int i = words.size() - 2; i >= 0; i = i - 2) {
  135. if (words[i] < cc) {
  136. cc = words[i];
  137. break;
  138. }
  139. }
  140. delete_text(cc, caret_column);
  141. set_caret_column(cc);
  142. } else {
  143. delete_char();
  144. }
  145. }
  146. void LineEdit::_delete(bool p_word, bool p_all_to_right) {
  147. if (!editable) {
  148. return;
  149. }
  150. if (p_all_to_right) {
  151. deselect();
  152. text = text.substr(caret_column, text.length() - caret_column);
  153. _shape();
  154. set_caret_column(0);
  155. _text_changed();
  156. return;
  157. }
  158. if (selection.enabled) {
  159. selection_delete();
  160. return;
  161. }
  162. int text_len = text.length();
  163. if (caret_column == text_len) {
  164. return; // Nothing to do.
  165. }
  166. if (p_word) {
  167. int cc = caret_column;
  168. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  169. for (int i = 1; i < words.size(); i = i + 2) {
  170. if (words[i] > cc) {
  171. cc = words[i];
  172. break;
  173. }
  174. }
  175. delete_text(caret_column, cc);
  176. set_caret_column(caret_column);
  177. } else {
  178. if (caret_mid_grapheme_enabled) {
  179. set_caret_column(caret_column + 1);
  180. delete_char();
  181. } else {
  182. int cc = caret_column;
  183. set_caret_column(TS->shaped_text_next_grapheme_pos(text_rid, caret_column));
  184. delete_text(cc, caret_column);
  185. }
  186. }
  187. }
  188. void LineEdit::unhandled_key_input(const Ref<InputEvent> &p_event) {
  189. Ref<InputEventKey> k = p_event;
  190. if (k.is_valid()) {
  191. if (!k->is_pressed()) {
  192. return;
  193. }
  194. // Handle Unicode (with modifiers active, process after shortcuts).
  195. if (has_focus() && editable && (k->get_unicode() >= 32)) {
  196. selection_delete();
  197. char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 };
  198. int prev_len = text.length();
  199. insert_text_at_caret(ucodestr);
  200. if (text.length() != prev_len) {
  201. _text_changed();
  202. }
  203. accept_event();
  204. }
  205. }
  206. }
  207. void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
  208. ERR_FAIL_COND(p_event.is_null());
  209. Ref<InputEventMouseButton> b = p_event;
  210. if (b.is_valid()) {
  211. if (ime_text.length() != 0) {
  212. // Ignore mouse clicks in IME input mode.
  213. return;
  214. }
  215. if (b->is_pressed() && b->get_button_index() == MouseButton::RIGHT && context_menu_enabled) {
  216. _update_context_menu();
  217. menu->set_position(get_screen_position() + get_local_mouse_position());
  218. menu->reset_size();
  219. menu->popup();
  220. grab_focus();
  221. accept_event();
  222. return;
  223. }
  224. if (is_middle_mouse_paste_enabled() && b->is_pressed() && b->get_button_index() == MouseButton::MIDDLE && is_editable() && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  225. String paste_buffer = DisplayServer::get_singleton()->clipboard_get_primary().strip_escapes();
  226. deselect();
  227. set_caret_at_pixel_pos(b->get_position().x);
  228. if (!paste_buffer.is_empty()) {
  229. insert_text_at_caret(paste_buffer);
  230. if (!text_changed_dirty) {
  231. if (is_inside_tree()) {
  232. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  233. }
  234. text_changed_dirty = true;
  235. }
  236. }
  237. grab_focus();
  238. accept_event();
  239. return;
  240. }
  241. if (b->get_button_index() != MouseButton::LEFT) {
  242. return;
  243. }
  244. _reset_caret_blink_timer();
  245. if (b->is_pressed()) {
  246. accept_event(); // don't pass event further when clicked on text field
  247. if (!text.is_empty() && is_editable() && _is_over_clear_button(b->get_position())) {
  248. clear_button_status.press_attempt = true;
  249. clear_button_status.pressing_inside = true;
  250. queue_redraw();
  251. return;
  252. }
  253. if (b->is_shift_pressed()) {
  254. shift_selection_check_pre(true);
  255. }
  256. set_caret_at_pixel_pos(b->get_position().x);
  257. if (b->is_shift_pressed()) {
  258. selection_fill_at_caret();
  259. selection.creating = true;
  260. } else {
  261. if (selecting_enabled) {
  262. const int triple_click_timeout = 600;
  263. const int triple_click_tolerance = 5;
  264. const bool is_triple_click = !b->is_double_click() && (OS::get_singleton()->get_ticks_msec() - last_dblclk) < triple_click_timeout && b->get_position().distance_to(last_dblclk_pos) < triple_click_tolerance;
  265. if (is_triple_click && text.length()) {
  266. // Triple-click select all.
  267. selection.enabled = true;
  268. selection.begin = 0;
  269. selection.end = text.length();
  270. selection.double_click = true;
  271. last_dblclk = 0;
  272. set_caret_column(selection.begin);
  273. if (!pass && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  274. DisplayServer::get_singleton()->clipboard_set_primary(text);
  275. }
  276. } else if (b->is_double_click()) {
  277. // Double-click select word.
  278. last_dblclk = OS::get_singleton()->get_ticks_msec();
  279. last_dblclk_pos = b->get_position();
  280. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  281. for (int i = 0; i < words.size(); i = i + 2) {
  282. if ((words[i] < caret_column && words[i + 1] > caret_column) || (i == words.size() - 2 && caret_column == words[i + 1])) {
  283. selection.enabled = true;
  284. selection.begin = words[i];
  285. selection.end = words[i + 1];
  286. selection.double_click = true;
  287. set_caret_column(selection.end);
  288. break;
  289. }
  290. }
  291. if (!pass && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  292. DisplayServer::get_singleton()->clipboard_set_primary(text.substr(selection.begin, selection.end - selection.begin));
  293. }
  294. }
  295. }
  296. selection.drag_attempt = false;
  297. if ((caret_column < selection.begin) || (caret_column > selection.end) || !selection.enabled) {
  298. deselect();
  299. selection.start_column = caret_column;
  300. selection.creating = true;
  301. } else if (selection.enabled && !selection.double_click) {
  302. selection.drag_attempt = true;
  303. }
  304. }
  305. queue_redraw();
  306. } else {
  307. if (selection.enabled && !pass && b->get_button_index() == MouseButton::LEFT && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  308. DisplayServer::get_singleton()->clipboard_set_primary(text.substr(selection.begin, selection.end - selection.begin));
  309. }
  310. if (!text.is_empty() && is_editable() && clear_button_enabled) {
  311. bool press_attempt = clear_button_status.press_attempt;
  312. clear_button_status.press_attempt = false;
  313. if (press_attempt && clear_button_status.pressing_inside && _is_over_clear_button(b->get_position())) {
  314. clear();
  315. return;
  316. }
  317. }
  318. if ((!selection.creating) && (!selection.double_click)) {
  319. deselect();
  320. }
  321. selection.creating = false;
  322. selection.double_click = false;
  323. if (!drag_action) {
  324. selection.drag_attempt = false;
  325. }
  326. if (pending_select_all_on_focus) {
  327. select_all();
  328. pending_select_all_on_focus = false;
  329. }
  330. show_virtual_keyboard();
  331. }
  332. queue_redraw();
  333. return;
  334. }
  335. Ref<InputEventMouseMotion> m = p_event;
  336. if (m.is_valid()) {
  337. if (!text.is_empty() && is_editable() && clear_button_enabled) {
  338. bool last_press_inside = clear_button_status.pressing_inside;
  339. clear_button_status.pressing_inside = clear_button_status.press_attempt && _is_over_clear_button(m->get_position());
  340. if (last_press_inside != clear_button_status.pressing_inside) {
  341. queue_redraw();
  342. }
  343. }
  344. if (m->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
  345. if (selection.creating) {
  346. set_caret_at_pixel_pos(m->get_position().x);
  347. selection_fill_at_caret();
  348. }
  349. }
  350. if (drag_action && can_drop_data(m->get_position(), get_viewport()->gui_get_drag_data())) {
  351. drag_caret_force_displayed = true;
  352. set_caret_at_pixel_pos(m->get_position().x);
  353. }
  354. return;
  355. }
  356. Ref<InputEventKey> k = p_event;
  357. if (k.is_valid()) {
  358. if (!k->is_pressed()) {
  359. if (alt_start && k->get_keycode() == Key::ALT) {
  360. alt_start = false;
  361. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
  362. char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
  363. insert_text_at_caret(ucodestr);
  364. }
  365. accept_event();
  366. return;
  367. }
  368. return;
  369. }
  370. // Alt+ Unicode input:
  371. if (k->is_alt_pressed()) {
  372. if (!alt_start) {
  373. if (k->get_keycode() == Key::KP_ADD) {
  374. alt_start = true;
  375. alt_code = 0;
  376. accept_event();
  377. return;
  378. }
  379. } else {
  380. if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
  381. alt_code = alt_code << 4;
  382. alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
  383. }
  384. if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
  385. alt_code = alt_code << 4;
  386. alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
  387. }
  388. if (k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
  389. alt_code = alt_code << 4;
  390. alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
  391. }
  392. accept_event();
  393. return;
  394. }
  395. }
  396. if (context_menu_enabled) {
  397. if (k->is_action("ui_menu", true)) {
  398. _update_context_menu();
  399. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2);
  400. menu->set_position(get_screen_position() + pos);
  401. menu->reset_size();
  402. menu->popup();
  403. menu->grab_focus();
  404. accept_event();
  405. return;
  406. }
  407. }
  408. // Default is ENTER and KP_ENTER. Cannot use ui_accept as default includes SPACE
  409. if (k->is_action("ui_text_submit", false)) {
  410. emit_signal(SNAME("text_submitted"), text);
  411. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  412. DisplayServer::get_singleton()->virtual_keyboard_hide();
  413. }
  414. accept_event();
  415. return;
  416. }
  417. if (is_shortcut_keys_enabled()) {
  418. if (k->is_action("ui_copy", true)) {
  419. copy_text();
  420. accept_event();
  421. return;
  422. }
  423. if (k->is_action("ui_text_select_all", true)) {
  424. select();
  425. accept_event();
  426. return;
  427. }
  428. // Cut / Paste
  429. if (k->is_action("ui_cut", true)) {
  430. cut_text();
  431. accept_event();
  432. return;
  433. }
  434. if (k->is_action("ui_paste", true)) {
  435. paste_text();
  436. accept_event();
  437. return;
  438. }
  439. // Undo / Redo
  440. if (k->is_action("ui_undo", true)) {
  441. undo();
  442. accept_event();
  443. return;
  444. }
  445. if (k->is_action("ui_redo", true)) {
  446. redo();
  447. accept_event();
  448. return;
  449. }
  450. }
  451. // BACKSPACE
  452. if (k->is_action("ui_text_backspace_all_to_left", true)) {
  453. _backspace(false, true);
  454. accept_event();
  455. return;
  456. }
  457. if (k->is_action("ui_text_backspace_word", true)) {
  458. _backspace(true);
  459. accept_event();
  460. return;
  461. }
  462. if (k->is_action("ui_text_backspace", true)) {
  463. _backspace();
  464. accept_event();
  465. return;
  466. }
  467. // DELETE
  468. if (k->is_action("ui_text_delete_all_to_right", true)) {
  469. _delete(false, true);
  470. accept_event();
  471. return;
  472. }
  473. if (k->is_action("ui_text_delete_word", true)) {
  474. _delete(true);
  475. accept_event();
  476. return;
  477. }
  478. if (k->is_action("ui_text_delete", true)) {
  479. _delete();
  480. accept_event();
  481. return;
  482. }
  483. // Cursor Movement
  484. k = k->duplicate();
  485. bool shift_pressed = k->is_shift_pressed();
  486. // Remove shift or else actions will not match. Use above variable for selection.
  487. k->set_shift_pressed(false);
  488. if (k->is_action("ui_text_caret_word_left", true)) {
  489. _move_caret_left(shift_pressed, true);
  490. accept_event();
  491. return;
  492. }
  493. if (k->is_action("ui_text_caret_left", true)) {
  494. _move_caret_left(shift_pressed);
  495. accept_event();
  496. return;
  497. }
  498. if (k->is_action("ui_text_caret_word_right", true)) {
  499. _move_caret_right(shift_pressed, true);
  500. accept_event();
  501. return;
  502. }
  503. if (k->is_action("ui_text_caret_right", true)) {
  504. _move_caret_right(shift_pressed, false);
  505. accept_event();
  506. return;
  507. }
  508. // Up = Home, Down = End
  509. if (k->is_action("ui_text_caret_up", true) || k->is_action("ui_text_caret_line_start", true) || k->is_action("ui_text_caret_page_up", true)) {
  510. _move_caret_start(shift_pressed);
  511. accept_event();
  512. return;
  513. }
  514. if (k->is_action("ui_text_caret_down", true) || k->is_action("ui_text_caret_line_end", true) || k->is_action("ui_text_caret_page_down", true)) {
  515. _move_caret_end(shift_pressed);
  516. accept_event();
  517. return;
  518. }
  519. // Misc
  520. if (k->is_action("ui_swap_input_direction", true)) {
  521. _swap_current_input_direction();
  522. accept_event();
  523. return;
  524. }
  525. _reset_caret_blink_timer();
  526. // Allow unicode handling if:
  527. // * No Modifiers are pressed (except shift)
  528. bool allow_unicode_handling = !(k->is_command_or_control_pressed() || k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  529. if (allow_unicode_handling && editable && k->get_unicode() >= 32) {
  530. // Handle Unicode (if no modifiers active)
  531. selection_delete();
  532. char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 };
  533. int prev_len = text.length();
  534. insert_text_at_caret(ucodestr);
  535. if (text.length() != prev_len) {
  536. _text_changed();
  537. }
  538. accept_event();
  539. return;
  540. }
  541. }
  542. }
  543. void LineEdit::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  544. ERR_FAIL_INDEX((int)p_alignment, 4);
  545. if (alignment == p_alignment) {
  546. return;
  547. }
  548. alignment = p_alignment;
  549. _shape();
  550. queue_redraw();
  551. }
  552. HorizontalAlignment LineEdit::get_horizontal_alignment() const {
  553. return alignment;
  554. }
  555. Variant LineEdit::get_drag_data(const Point2 &p_point) {
  556. Variant ret = Control::get_drag_data(p_point);
  557. if (ret != Variant()) {
  558. return ret;
  559. }
  560. if (selection.drag_attempt && selection.enabled) {
  561. String t = text.substr(selection.begin, selection.end - selection.begin);
  562. Label *l = memnew(Label);
  563. l->set_text(t);
  564. set_drag_preview(l);
  565. return t;
  566. }
  567. return Variant();
  568. }
  569. bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  570. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  571. if (drop_override) {
  572. return drop_override;
  573. }
  574. return is_editable() && p_data.get_type() == Variant::STRING;
  575. }
  576. void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  577. Control::drop_data(p_point, p_data);
  578. if (p_data.get_type() == Variant::STRING && is_editable()) {
  579. set_caret_at_pixel_pos(p_point.x);
  580. int caret_column_tmp = caret_column;
  581. bool is_inside_sel = selection.enabled && caret_column >= selection.begin && caret_column <= selection.end;
  582. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  583. is_inside_sel = selection.enabled && caret_column > selection.begin && caret_column < selection.end;
  584. }
  585. if (selection.drag_attempt) {
  586. selection.drag_attempt = false;
  587. if (!is_inside_sel) {
  588. if (!Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  589. if (caret_column_tmp > selection.end) {
  590. caret_column_tmp = caret_column_tmp - (selection.end - selection.begin);
  591. }
  592. selection_delete();
  593. }
  594. set_caret_column(caret_column_tmp);
  595. insert_text_at_caret(p_data);
  596. }
  597. } else if (selection.enabled && caret_column >= selection.begin && caret_column <= selection.end) {
  598. caret_column_tmp = selection.begin;
  599. selection_delete();
  600. set_caret_column(caret_column_tmp);
  601. insert_text_at_caret(p_data);
  602. grab_focus();
  603. } else {
  604. insert_text_at_caret(p_data);
  605. grab_focus();
  606. }
  607. select(caret_column_tmp, caret_column);
  608. if (!text_changed_dirty) {
  609. if (is_inside_tree()) {
  610. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  611. }
  612. text_changed_dirty = true;
  613. }
  614. queue_redraw();
  615. }
  616. }
  617. Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const {
  618. if ((!text.is_empty() && is_editable() && _is_over_clear_button(p_pos)) || (!is_editable() && (!is_selecting_enabled() || text.is_empty()))) {
  619. return CURSOR_ARROW;
  620. }
  621. return Control::get_cursor_shape(p_pos);
  622. }
  623. bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const {
  624. if (!clear_button_enabled || !has_point(p_pos)) {
  625. return false;
  626. }
  627. Ref<Texture2D> icon = theme_cache.clear_icon;
  628. int x_ofs = theme_cache.normal->get_margin(SIDE_RIGHT);
  629. return p_pos.x > get_size().width - icon->get_width() - x_ofs;
  630. }
  631. void LineEdit::_update_theme_item_cache() {
  632. Control::_update_theme_item_cache();
  633. theme_cache.normal = get_theme_stylebox(SNAME("normal"));
  634. theme_cache.read_only = get_theme_stylebox(SNAME("read_only"));
  635. theme_cache.focus = get_theme_stylebox(SNAME("focus"));
  636. theme_cache.font = get_theme_font(SNAME("font"));
  637. theme_cache.font_size = get_theme_font_size(SNAME("font_size"));
  638. theme_cache.font_color = get_theme_color(SNAME("font_color"));
  639. theme_cache.font_uneditable_color = get_theme_color(SNAME("font_uneditable_color"));
  640. theme_cache.font_selected_color = get_theme_color(SNAME("font_selected_color"));
  641. theme_cache.font_outline_size = get_theme_constant(SNAME("outline_size"));
  642. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  643. theme_cache.font_placeholder_color = get_theme_color(SNAME("font_placeholder_color"));
  644. theme_cache.caret_width = get_theme_constant(SNAME("caret_width"));
  645. theme_cache.caret_color = get_theme_color(SNAME("caret_color"));
  646. theme_cache.minimum_character_width = get_theme_constant(SNAME("minimum_character_width"));
  647. theme_cache.selection_color = get_theme_color(SNAME("selection_color"));
  648. theme_cache.clear_icon = get_theme_icon(SNAME("clear"));
  649. theme_cache.clear_button_color = get_theme_color(SNAME("clear_button_color"));
  650. theme_cache.clear_button_color_pressed = get_theme_color(SNAME("clear_button_color_pressed"));
  651. theme_cache.base_scale = get_theme_default_base_scale();
  652. }
  653. void LineEdit::_notification(int p_what) {
  654. switch (p_what) {
  655. #ifdef TOOLS_ENABLED
  656. case NOTIFICATION_ENTER_TREE: {
  657. if (Engine::get_singleton()->is_editor_hint() && !get_tree()->is_node_being_edited(this)) {
  658. set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  659. set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  660. if (!EditorSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed))) {
  661. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed));
  662. }
  663. }
  664. } break;
  665. #endif
  666. case NOTIFICATION_RESIZED: {
  667. _fit_to_width();
  668. scroll_offset = 0.0;
  669. set_caret_column(get_caret_column());
  670. } break;
  671. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  672. case NOTIFICATION_THEME_CHANGED: {
  673. _shape();
  674. queue_redraw();
  675. } break;
  676. case NOTIFICATION_TRANSLATION_CHANGED: {
  677. placeholder_translated = atr(placeholder);
  678. _shape();
  679. queue_redraw();
  680. } break;
  681. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  682. window_has_focus = true;
  683. _validate_caret_can_draw();
  684. queue_redraw();
  685. } break;
  686. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  687. window_has_focus = false;
  688. _validate_caret_can_draw();
  689. queue_redraw();
  690. } break;
  691. case NOTIFICATION_INTERNAL_PROCESS: {
  692. if (caret_blink_enabled && caret_can_draw) {
  693. caret_blink_timer += get_process_delta_time();
  694. if (caret_blink_timer >= caret_blink_interval) {
  695. caret_blink_timer = 0.0;
  696. _toggle_draw_caret();
  697. }
  698. }
  699. } break;
  700. case NOTIFICATION_DRAW: {
  701. int width, height;
  702. bool rtl = is_layout_rtl();
  703. Size2 size = get_size();
  704. width = size.width;
  705. height = size.height;
  706. RID ci = get_canvas_item();
  707. Ref<StyleBox> style = theme_cache.normal;
  708. if (!is_editable()) {
  709. style = theme_cache.read_only;
  710. }
  711. Ref<Font> font = theme_cache.font;
  712. if (!flat) {
  713. style->draw(ci, Rect2(Point2(), size));
  714. }
  715. if (has_focus()) {
  716. theme_cache.focus->draw(ci, Rect2(Point2(), size));
  717. }
  718. int x_ofs = 0;
  719. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  720. float text_width = TS->shaped_text_get_size(text_rid).x;
  721. float text_height = TS->shaped_text_get_size(text_rid).y;
  722. switch (alignment) {
  723. case HORIZONTAL_ALIGNMENT_FILL:
  724. case HORIZONTAL_ALIGNMENT_LEFT: {
  725. if (rtl) {
  726. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  727. } else {
  728. x_ofs = style->get_offset().x;
  729. }
  730. } break;
  731. case HORIZONTAL_ALIGNMENT_CENTER: {
  732. if (!Math::is_zero_approx(scroll_offset)) {
  733. x_ofs = style->get_offset().x;
  734. } else {
  735. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2);
  736. }
  737. } break;
  738. case HORIZONTAL_ALIGNMENT_RIGHT: {
  739. if (rtl) {
  740. x_ofs = style->get_offset().x;
  741. } else {
  742. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  743. }
  744. } break;
  745. }
  746. int ofs_max = width - style->get_margin(SIDE_RIGHT);
  747. int y_area = height - style->get_minimum_size().height;
  748. int y_ofs = style->get_offset().y + (y_area - text_height) / 2;
  749. Color selection_color = theme_cache.selection_color;
  750. Color font_color;
  751. if (is_editable()) {
  752. font_color = theme_cache.font_color;
  753. } else {
  754. font_color = theme_cache.font_uneditable_color;
  755. }
  756. Color font_selected_color = theme_cache.font_selected_color;
  757. Color caret_color = theme_cache.caret_color;
  758. // Draw placeholder color.
  759. if (using_placeholder) {
  760. font_color = theme_cache.font_placeholder_color;
  761. }
  762. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  763. if (right_icon.is_valid() || display_clear_icon) {
  764. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  765. Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9);
  766. if (display_clear_icon) {
  767. if (clear_button_status.press_attempt && clear_button_status.pressing_inside) {
  768. color_icon = theme_cache.clear_button_color_pressed;
  769. } else {
  770. color_icon = theme_cache.clear_button_color;
  771. }
  772. }
  773. r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(SIDE_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
  774. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  775. if (Math::is_zero_approx(scroll_offset)) {
  776. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
  777. }
  778. } else {
  779. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  780. }
  781. ofs_max -= r_icon->get_width();
  782. }
  783. // Draw selections rects.
  784. Vector2 ofs = Point2(x_ofs + scroll_offset, y_ofs);
  785. if (selection.enabled) {
  786. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, selection.begin, selection.end);
  787. for (int i = 0; i < sel.size(); i++) {
  788. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  789. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  790. continue;
  791. }
  792. if (rect.position.x < x_ofs) {
  793. rect.size.x -= (x_ofs - rect.position.x);
  794. rect.position.x = x_ofs;
  795. } else if (rect.position.x + rect.size.x > ofs_max) {
  796. rect.size.x = ofs_max - rect.position.x;
  797. }
  798. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_color);
  799. }
  800. }
  801. const Glyph *glyphs = TS->shaped_text_get_glyphs(text_rid);
  802. int gl_size = TS->shaped_text_get_glyph_count(text_rid);
  803. // Draw text.
  804. ofs.y += TS->shaped_text_get_ascent(text_rid);
  805. Color font_outline_color = theme_cache.font_outline_color;
  806. int outline_size = theme_cache.font_outline_size;
  807. if (outline_size > 0 && font_outline_color.a > 0) {
  808. Vector2 oofs = ofs;
  809. for (int i = 0; i < gl_size; i++) {
  810. for (int j = 0; j < glyphs[i].repeat; j++) {
  811. if (ceil(oofs.x) >= x_ofs && (oofs.x + glyphs[i].advance) <= ofs_max) {
  812. if (glyphs[i].font_rid != RID()) {
  813. TS->font_draw_glyph_outline(glyphs[i].font_rid, ci, glyphs[i].font_size, outline_size, oofs + Vector2(glyphs[i].x_off, glyphs[i].y_off), glyphs[i].index, font_outline_color);
  814. }
  815. }
  816. oofs.x += glyphs[i].advance;
  817. }
  818. if (oofs.x >= ofs_max) {
  819. break;
  820. }
  821. }
  822. }
  823. for (int i = 0; i < gl_size; i++) {
  824. bool selected = selection.enabled && glyphs[i].start >= selection.begin && glyphs[i].end <= selection.end;
  825. for (int j = 0; j < glyphs[i].repeat; j++) {
  826. if (ceil(ofs.x) >= x_ofs && (ofs.x + glyphs[i].advance) <= ofs_max) {
  827. if (glyphs[i].font_rid != RID()) {
  828. 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_selected_color : font_color);
  829. } else if ((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  830. 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_selected_color : font_color);
  831. }
  832. }
  833. ofs.x += glyphs[i].advance;
  834. }
  835. if (ofs.x >= ofs_max) {
  836. break;
  837. }
  838. }
  839. // Draw carets.
  840. ofs.x = x_ofs + scroll_offset;
  841. if ((caret_can_draw && draw_caret) || drag_caret_force_displayed) {
  842. // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
  843. const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
  844. if (ime_text.is_empty() || ime_selection.y == 0) {
  845. // Normal caret.
  846. CaretInfo caret = TS->shaped_text_get_carets(text_rid, ime_text.is_empty() ? caret_column : caret_column + ime_selection.x);
  847. if (using_placeholder || (caret.l_caret == Rect2() && caret.t_caret == Rect2())) {
  848. // No carets, add one at the start.
  849. int h = theme_cache.font->get_height(theme_cache.font_size);
  850. int y = style->get_offset().y + (y_area - h) / 2;
  851. caret.l_dir = (rtl) ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  852. switch (alignment) {
  853. case HORIZONTAL_ALIGNMENT_FILL:
  854. case HORIZONTAL_ALIGNMENT_LEFT: {
  855. if (rtl) {
  856. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  857. } else {
  858. caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h));
  859. }
  860. } break;
  861. case HORIZONTAL_ALIGNMENT_CENTER: {
  862. caret.l_caret = Rect2(Vector2(size.x / 2, y), Size2(caret_width, h));
  863. } break;
  864. case HORIZONTAL_ALIGNMENT_RIGHT: {
  865. if (rtl) {
  866. caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h));
  867. } else {
  868. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  869. }
  870. } break;
  871. }
  872. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  873. } else {
  874. if (caret.l_caret != Rect2() && caret.l_dir == TextServer::DIRECTION_AUTO) {
  875. // Draw extra marker on top of mid caret.
  876. Rect2 trect = Rect2(caret.l_caret.position.x - 2.5 * caret_width, caret.l_caret.position.y, 6 * caret_width, caret_width);
  877. trect.position += ofs;
  878. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  879. } else if (caret.l_caret != Rect2() && caret.t_caret != Rect2() && caret.l_dir != caret.t_dir) {
  880. // Draw extra direction marker on top of split caret.
  881. float d = (caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  882. Rect2 trect = Rect2(caret.l_caret.position.x + d * caret_width, caret.l_caret.position.y + caret.l_caret.size.y - caret_width, 3 * caret_width, caret_width);
  883. trect.position += ofs;
  884. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  885. d = (caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  886. trect = Rect2(caret.t_caret.position.x + d * caret_width, caret.t_caret.position.y, 3 * caret_width, caret_width);
  887. trect.position += ofs;
  888. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  889. }
  890. caret.l_caret.position += ofs;
  891. caret.l_caret.size.x = caret_width;
  892. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  893. caret.t_caret.position += ofs;
  894. caret.t_caret.size.x = caret_width;
  895. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.t_caret, caret_color);
  896. }
  897. }
  898. if (!ime_text.is_empty()) {
  899. {
  900. // IME intermediate text range.
  901. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column, caret_column + ime_text.length());
  902. for (int i = 0; i < sel.size(); i++) {
  903. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  904. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  905. continue;
  906. }
  907. if (rect.position.x < x_ofs) {
  908. rect.size.x -= (x_ofs - rect.position.x);
  909. rect.position.x = x_ofs;
  910. } else if (rect.position.x + rect.size.x > ofs_max) {
  911. rect.size.x = ofs_max - rect.position.x;
  912. }
  913. rect.size.y = caret_width;
  914. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  915. }
  916. }
  917. {
  918. // IME caret.
  919. if (ime_selection.y > 0) {
  920. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column + ime_selection.x, caret_column + ime_selection.x + ime_selection.y);
  921. for (int i = 0; i < sel.size(); i++) {
  922. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  923. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  924. continue;
  925. }
  926. if (rect.position.x < x_ofs) {
  927. rect.size.x -= (x_ofs - rect.position.x);
  928. rect.position.x = x_ofs;
  929. } else if (rect.position.x + rect.size.x > ofs_max) {
  930. rect.size.x = ofs_max - rect.position.x;
  931. }
  932. rect.size.y = caret_width * 3;
  933. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  934. }
  935. }
  936. }
  937. }
  938. }
  939. if (has_focus()) {
  940. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  941. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  942. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2) + get_global_position();
  943. if (get_window()->get_embedder()) {
  944. pos += get_viewport()->get_popup_base_transform().get_origin();
  945. }
  946. DisplayServer::get_singleton()->window_set_ime_position(pos, get_viewport()->get_window_id());
  947. }
  948. }
  949. } break;
  950. case NOTIFICATION_FOCUS_ENTER: {
  951. _validate_caret_can_draw();
  952. if (select_all_on_focus) {
  953. if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  954. // Select all when the mouse button is up.
  955. pending_select_all_on_focus = true;
  956. } else {
  957. select_all();
  958. }
  959. }
  960. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  961. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  962. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2) + get_global_position();
  963. if (get_window()->get_embedder()) {
  964. pos += get_viewport()->get_popup_base_transform().get_origin();
  965. }
  966. DisplayServer::get_singleton()->window_set_ime_position(pos, get_viewport()->get_window_id());
  967. }
  968. show_virtual_keyboard();
  969. } break;
  970. case NOTIFICATION_FOCUS_EXIT: {
  971. _validate_caret_can_draw();
  972. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  973. DisplayServer::get_singleton()->window_set_ime_position(Point2(), get_viewport()->get_window_id());
  974. DisplayServer::get_singleton()->window_set_ime_active(false, get_viewport()->get_window_id());
  975. }
  976. ime_text = "";
  977. ime_selection = Point2();
  978. _shape();
  979. set_caret_column(caret_column); // Update scroll_offset
  980. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  981. DisplayServer::get_singleton()->virtual_keyboard_hide();
  982. }
  983. if (deselect_on_focus_loss_enabled && !selection.drag_attempt) {
  984. deselect();
  985. }
  986. } break;
  987. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  988. if (has_focus()) {
  989. ime_text = DisplayServer::get_singleton()->ime_get_text();
  990. ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  991. if (!ime_text.is_empty()) {
  992. selection_delete();
  993. }
  994. _shape();
  995. set_caret_column(caret_column); // Update scroll_offset
  996. queue_redraw();
  997. }
  998. } break;
  999. case NOTIFICATION_DRAG_BEGIN: {
  1000. drag_action = true;
  1001. } break;
  1002. case NOTIFICATION_DRAG_END: {
  1003. if (is_drag_successful()) {
  1004. if (selection.drag_attempt) {
  1005. selection.drag_attempt = false;
  1006. if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1007. selection_delete();
  1008. } else if (deselect_on_focus_loss_enabled) {
  1009. deselect();
  1010. }
  1011. }
  1012. } else {
  1013. selection.drag_attempt = false;
  1014. }
  1015. drag_action = false;
  1016. drag_caret_force_displayed = false;
  1017. } break;
  1018. }
  1019. }
  1020. void LineEdit::copy_text() {
  1021. if (selection.enabled && !pass) {
  1022. DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
  1023. }
  1024. }
  1025. void LineEdit::cut_text() {
  1026. if (editable && selection.enabled && !pass) {
  1027. DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
  1028. selection_delete();
  1029. }
  1030. }
  1031. void LineEdit::paste_text() {
  1032. if (!editable) {
  1033. return;
  1034. }
  1035. // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
  1036. String paste_buffer = DisplayServer::get_singleton()->clipboard_get().strip_escapes();
  1037. if (!paste_buffer.is_empty()) {
  1038. int prev_len = text.length();
  1039. if (selection.enabled) {
  1040. selection_delete();
  1041. }
  1042. insert_text_at_caret(paste_buffer);
  1043. if (!text_changed_dirty) {
  1044. if (is_inside_tree() && text.length() != prev_len) {
  1045. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  1046. }
  1047. text_changed_dirty = true;
  1048. }
  1049. }
  1050. }
  1051. bool LineEdit::has_undo() const {
  1052. if (undo_stack_pos == nullptr) {
  1053. return undo_stack.size() > 1;
  1054. }
  1055. return undo_stack_pos != undo_stack.front();
  1056. }
  1057. bool LineEdit::has_redo() const {
  1058. return undo_stack_pos != nullptr && undo_stack_pos != undo_stack.back();
  1059. }
  1060. void LineEdit::undo() {
  1061. if (!editable) {
  1062. return;
  1063. }
  1064. if (undo_stack_pos == nullptr) {
  1065. if (undo_stack.size() <= 1) {
  1066. return;
  1067. }
  1068. undo_stack_pos = undo_stack.back();
  1069. } else if (undo_stack_pos == undo_stack.front()) {
  1070. return;
  1071. }
  1072. deselect();
  1073. undo_stack_pos = undo_stack_pos->prev();
  1074. TextOperation op = undo_stack_pos->get();
  1075. text = op.text;
  1076. scroll_offset = op.scroll_offset;
  1077. set_caret_column(op.caret_column);
  1078. _shape();
  1079. _emit_text_change();
  1080. }
  1081. void LineEdit::redo() {
  1082. if (!editable) {
  1083. return;
  1084. }
  1085. if (undo_stack_pos == nullptr) {
  1086. return;
  1087. }
  1088. if (undo_stack_pos == undo_stack.back()) {
  1089. return;
  1090. }
  1091. deselect();
  1092. undo_stack_pos = undo_stack_pos->next();
  1093. TextOperation op = undo_stack_pos->get();
  1094. text = op.text;
  1095. scroll_offset = op.scroll_offset;
  1096. set_caret_column(op.caret_column);
  1097. _shape();
  1098. _emit_text_change();
  1099. }
  1100. void LineEdit::shift_selection_check_pre(bool p_shift) {
  1101. if (!selection.enabled && p_shift) {
  1102. selection.start_column = caret_column;
  1103. }
  1104. if (!p_shift) {
  1105. deselect();
  1106. }
  1107. }
  1108. void LineEdit::shift_selection_check_post(bool p_shift) {
  1109. if (p_shift) {
  1110. selection_fill_at_caret();
  1111. }
  1112. }
  1113. void LineEdit::set_caret_at_pixel_pos(int p_x) {
  1114. Ref<StyleBox> style = theme_cache.normal;
  1115. bool rtl = is_layout_rtl();
  1116. int x_ofs = 0;
  1117. float text_width = TS->shaped_text_get_size(text_rid).x;
  1118. switch (alignment) {
  1119. case HORIZONTAL_ALIGNMENT_FILL:
  1120. case HORIZONTAL_ALIGNMENT_LEFT: {
  1121. if (rtl) {
  1122. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1123. } else {
  1124. x_ofs = style->get_offset().x;
  1125. }
  1126. } break;
  1127. case HORIZONTAL_ALIGNMENT_CENTER: {
  1128. if (!Math::is_zero_approx(scroll_offset)) {
  1129. x_ofs = style->get_offset().x;
  1130. } else {
  1131. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
  1132. }
  1133. } break;
  1134. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1135. if (rtl) {
  1136. x_ofs = style->get_offset().x;
  1137. } else {
  1138. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1139. }
  1140. } break;
  1141. }
  1142. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1143. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1144. if (right_icon.is_valid() || display_clear_icon) {
  1145. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1146. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1147. if (Math::is_zero_approx(scroll_offset)) {
  1148. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
  1149. }
  1150. } else {
  1151. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1152. }
  1153. }
  1154. int ofs = ceil(TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset));
  1155. set_caret_column(ofs);
  1156. }
  1157. Vector2 LineEdit::get_caret_pixel_pos() {
  1158. Ref<StyleBox> style = theme_cache.normal;
  1159. bool rtl = is_layout_rtl();
  1160. int x_ofs = 0;
  1161. float text_width = TS->shaped_text_get_size(text_rid).x;
  1162. switch (alignment) {
  1163. case HORIZONTAL_ALIGNMENT_FILL:
  1164. case HORIZONTAL_ALIGNMENT_LEFT: {
  1165. if (rtl) {
  1166. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1167. } else {
  1168. x_ofs = style->get_offset().x;
  1169. }
  1170. } break;
  1171. case HORIZONTAL_ALIGNMENT_CENTER: {
  1172. if (!Math::is_zero_approx(scroll_offset)) {
  1173. x_ofs = style->get_offset().x;
  1174. } else {
  1175. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
  1176. }
  1177. } break;
  1178. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1179. if (rtl) {
  1180. x_ofs = style->get_offset().x;
  1181. } else {
  1182. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1183. }
  1184. } break;
  1185. }
  1186. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1187. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1188. if (right_icon.is_valid() || display_clear_icon) {
  1189. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1190. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1191. if (Math::is_zero_approx(scroll_offset)) {
  1192. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
  1193. }
  1194. } else {
  1195. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1196. }
  1197. }
  1198. Vector2 ret;
  1199. CaretInfo caret;
  1200. // Get position of the start of caret.
  1201. if (ime_text.length() != 0 && ime_selection.x != 0) {
  1202. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x);
  1203. } else {
  1204. caret = TS->shaped_text_get_carets(text_rid, caret_column);
  1205. }
  1206. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1207. ret.x = x_ofs + caret.l_caret.position.x + scroll_offset;
  1208. } else {
  1209. ret.x = x_ofs + caret.t_caret.position.x + scroll_offset;
  1210. }
  1211. // Get position of the end of caret.
  1212. if (ime_text.length() != 0) {
  1213. if (ime_selection.y != 0) {
  1214. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x + ime_selection.y);
  1215. } else {
  1216. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_text.size());
  1217. }
  1218. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1219. ret.y = x_ofs + caret.l_caret.position.x + scroll_offset;
  1220. } else {
  1221. ret.y = x_ofs + caret.t_caret.position.x + scroll_offset;
  1222. }
  1223. } else {
  1224. ret.y = ret.x;
  1225. }
  1226. return ret;
  1227. }
  1228. void LineEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) {
  1229. caret_mid_grapheme_enabled = p_enabled;
  1230. }
  1231. bool LineEdit::is_caret_mid_grapheme_enabled() const {
  1232. return caret_mid_grapheme_enabled;
  1233. }
  1234. bool LineEdit::is_caret_blink_enabled() const {
  1235. return caret_blink_enabled;
  1236. }
  1237. void LineEdit::set_caret_blink_enabled(const bool p_enabled) {
  1238. if (caret_blink_enabled == p_enabled) {
  1239. return;
  1240. }
  1241. caret_blink_enabled = p_enabled;
  1242. set_process_internal(p_enabled);
  1243. draw_caret = !caret_blink_enabled;
  1244. if (caret_blink_enabled) {
  1245. caret_blink_timer = 0.0;
  1246. }
  1247. queue_redraw();
  1248. notify_property_list_changed();
  1249. }
  1250. bool LineEdit::is_caret_force_displayed() const {
  1251. return caret_force_displayed;
  1252. }
  1253. void LineEdit::set_caret_force_displayed(const bool p_enabled) {
  1254. if (caret_force_displayed == p_enabled) {
  1255. return;
  1256. }
  1257. caret_force_displayed = p_enabled;
  1258. _validate_caret_can_draw();
  1259. queue_redraw();
  1260. }
  1261. float LineEdit::get_caret_blink_interval() const {
  1262. return caret_blink_interval;
  1263. }
  1264. void LineEdit::set_caret_blink_interval(const float p_interval) {
  1265. ERR_FAIL_COND(p_interval <= 0);
  1266. caret_blink_interval = p_interval;
  1267. }
  1268. void LineEdit::_reset_caret_blink_timer() {
  1269. if (caret_blink_enabled) {
  1270. draw_caret = true;
  1271. if (caret_can_draw) {
  1272. caret_blink_timer = 0.0;
  1273. queue_redraw();
  1274. }
  1275. }
  1276. }
  1277. void LineEdit::_toggle_draw_caret() {
  1278. draw_caret = !draw_caret;
  1279. if (is_visible_in_tree() && caret_can_draw) {
  1280. queue_redraw();
  1281. }
  1282. }
  1283. void LineEdit::_validate_caret_can_draw() {
  1284. if (caret_blink_enabled) {
  1285. draw_caret = true;
  1286. caret_blink_timer = 0.0;
  1287. }
  1288. caret_can_draw = editable && (window_has_focus || (menu && menu->has_focus())) && (has_focus() || caret_force_displayed);
  1289. }
  1290. void LineEdit::delete_char() {
  1291. if ((text.length() <= 0) || (caret_column == 0)) {
  1292. return;
  1293. }
  1294. text = text.left(caret_column - 1) + text.substr(caret_column);
  1295. _shape();
  1296. set_caret_column(get_caret_column() - 1);
  1297. _text_changed();
  1298. }
  1299. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  1300. ERR_FAIL_COND_MSG(p_from_column < 0 || p_from_column > p_to_column || p_to_column > text.length(),
  1301. vformat("Positional parameters (from: %d, to: %d) are inverted or outside the text length (%d).", p_from_column, p_to_column, text.length()));
  1302. text = text.left(p_from_column) + text.substr(p_to_column);
  1303. _shape();
  1304. caret_column -= CLAMP(caret_column - p_from_column, 0, p_to_column - p_from_column);
  1305. if (caret_column >= text.length()) {
  1306. caret_column = text.length();
  1307. }
  1308. if (!text_changed_dirty) {
  1309. if (is_inside_tree()) {
  1310. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  1311. }
  1312. text_changed_dirty = true;
  1313. }
  1314. }
  1315. void LineEdit::set_text(String p_text) {
  1316. clear_internal();
  1317. insert_text_at_caret(p_text);
  1318. _create_undo_state();
  1319. queue_redraw();
  1320. caret_column = 0;
  1321. scroll_offset = 0.0;
  1322. }
  1323. void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
  1324. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1325. if (text_direction != p_text_direction) {
  1326. text_direction = p_text_direction;
  1327. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  1328. input_direction = text_direction;
  1329. }
  1330. _shape();
  1331. if (menu_dir) {
  1332. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  1333. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  1334. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  1335. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  1336. }
  1337. queue_redraw();
  1338. }
  1339. }
  1340. Control::TextDirection LineEdit::get_text_direction() const {
  1341. return text_direction;
  1342. }
  1343. void LineEdit::set_language(const String &p_language) {
  1344. if (language != p_language) {
  1345. language = p_language;
  1346. _shape();
  1347. queue_redraw();
  1348. }
  1349. }
  1350. String LineEdit::get_language() const {
  1351. return language;
  1352. }
  1353. void LineEdit::set_draw_control_chars(bool p_draw_control_chars) {
  1354. if (draw_control_chars != p_draw_control_chars) {
  1355. draw_control_chars = p_draw_control_chars;
  1356. if (menu && menu->get_item_index(MENU_DISPLAY_UCC) >= 0) {
  1357. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  1358. }
  1359. _shape();
  1360. queue_redraw();
  1361. }
  1362. }
  1363. bool LineEdit::get_draw_control_chars() const {
  1364. return draw_control_chars;
  1365. }
  1366. void LineEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  1367. if (st_parser != p_parser) {
  1368. st_parser = p_parser;
  1369. _shape();
  1370. queue_redraw();
  1371. }
  1372. }
  1373. TextServer::StructuredTextParser LineEdit::get_structured_text_bidi_override() const {
  1374. return st_parser;
  1375. }
  1376. void LineEdit::set_structured_text_bidi_override_options(Array p_args) {
  1377. st_args = p_args;
  1378. _shape();
  1379. queue_redraw();
  1380. }
  1381. Array LineEdit::get_structured_text_bidi_override_options() const {
  1382. return st_args;
  1383. }
  1384. void LineEdit::clear() {
  1385. clear_internal();
  1386. _text_changed();
  1387. // This should reset virtual keyboard state if needed.
  1388. if (has_focus()) {
  1389. show_virtual_keyboard();
  1390. }
  1391. }
  1392. void LineEdit::show_virtual_keyboard() {
  1393. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1394. if (selection.enabled) {
  1395. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, selection.begin, selection.end);
  1396. } else {
  1397. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, caret_column);
  1398. }
  1399. }
  1400. }
  1401. String LineEdit::get_text() const {
  1402. return text;
  1403. }
  1404. void LineEdit::set_placeholder(String p_text) {
  1405. if (placeholder == p_text) {
  1406. return;
  1407. }
  1408. placeholder = p_text;
  1409. placeholder_translated = atr(placeholder);
  1410. _shape();
  1411. queue_redraw();
  1412. }
  1413. String LineEdit::get_placeholder() const {
  1414. return placeholder;
  1415. }
  1416. void LineEdit::set_caret_column(int p_column) {
  1417. if (p_column > (int)text.length()) {
  1418. p_column = text.length();
  1419. }
  1420. if (p_column < 0) {
  1421. p_column = 0;
  1422. }
  1423. caret_column = p_column;
  1424. // Fit to window.
  1425. if (!is_inside_tree()) {
  1426. scroll_offset = 0.0;
  1427. return;
  1428. }
  1429. Ref<StyleBox> style = theme_cache.normal;
  1430. bool rtl = is_layout_rtl();
  1431. int x_ofs = 0;
  1432. float text_width = TS->shaped_text_get_size(text_rid).x;
  1433. switch (alignment) {
  1434. case HORIZONTAL_ALIGNMENT_FILL:
  1435. case HORIZONTAL_ALIGNMENT_LEFT: {
  1436. if (rtl) {
  1437. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1438. } else {
  1439. x_ofs = style->get_offset().x;
  1440. }
  1441. } break;
  1442. case HORIZONTAL_ALIGNMENT_CENTER: {
  1443. if (!Math::is_zero_approx(scroll_offset)) {
  1444. x_ofs = style->get_offset().x;
  1445. } else {
  1446. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
  1447. }
  1448. } break;
  1449. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1450. if (rtl) {
  1451. x_ofs = style->get_offset().x;
  1452. } else {
  1453. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1454. }
  1455. } break;
  1456. }
  1457. int ofs_max = get_size().width - style->get_margin(SIDE_RIGHT);
  1458. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1459. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1460. if (right_icon.is_valid() || display_clear_icon) {
  1461. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1462. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1463. if (Math::is_zero_approx(scroll_offset)) {
  1464. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
  1465. }
  1466. } else {
  1467. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1468. }
  1469. ofs_max -= r_icon->get_width();
  1470. }
  1471. // Note: Use two coordinates to fit IME input range.
  1472. Vector2 primary_caret_offset = get_caret_pixel_pos();
  1473. if (MIN(primary_caret_offset.x, primary_caret_offset.y) <= x_ofs) {
  1474. scroll_offset += x_ofs - MIN(primary_caret_offset.x, primary_caret_offset.y);
  1475. } else if (MAX(primary_caret_offset.x, primary_caret_offset.y) >= ofs_max) {
  1476. scroll_offset += ofs_max - MAX(primary_caret_offset.x, primary_caret_offset.y);
  1477. }
  1478. scroll_offset = MIN(0, scroll_offset);
  1479. queue_redraw();
  1480. }
  1481. int LineEdit::get_caret_column() const {
  1482. return caret_column;
  1483. }
  1484. void LineEdit::set_scroll_offset(float p_pos) {
  1485. scroll_offset = p_pos;
  1486. if (scroll_offset < 0.0) {
  1487. scroll_offset = 0.0;
  1488. }
  1489. }
  1490. float LineEdit::get_scroll_offset() const {
  1491. return scroll_offset;
  1492. }
  1493. void LineEdit::insert_text_at_caret(String p_text) {
  1494. if (max_length > 0) {
  1495. // Truncate text to append to fit in max_length, if needed.
  1496. int available_chars = max_length - text.length();
  1497. if (p_text.length() > available_chars) {
  1498. emit_signal(SNAME("text_change_rejected"), p_text.substr(available_chars));
  1499. p_text = p_text.substr(0, available_chars);
  1500. }
  1501. }
  1502. String pre = text.substr(0, caret_column);
  1503. String post = text.substr(caret_column, text.length() - caret_column);
  1504. text = pre + p_text + post;
  1505. _shape();
  1506. TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text_rid, caret_column, caret_column + p_text.length());
  1507. if (dir != TextServer::DIRECTION_AUTO) {
  1508. input_direction = (TextDirection)dir;
  1509. }
  1510. set_caret_column(caret_column + p_text.length());
  1511. if (!ime_text.is_empty()) {
  1512. _shape();
  1513. }
  1514. }
  1515. void LineEdit::clear_internal() {
  1516. deselect();
  1517. _clear_undo_stack();
  1518. caret_column = 0;
  1519. scroll_offset = 0.0;
  1520. undo_text = "";
  1521. text = "";
  1522. _shape();
  1523. queue_redraw();
  1524. }
  1525. Size2 LineEdit::get_minimum_size() const {
  1526. Ref<StyleBox> style = theme_cache.normal;
  1527. Ref<Font> font = theme_cache.font;
  1528. int font_size = theme_cache.font_size;
  1529. Size2 min_size;
  1530. // Minimum size of text.
  1531. float em_space_size = font->get_char_size('M', font_size).x;
  1532. min_size.width = theme_cache.minimum_character_width * em_space_size;
  1533. if (expand_to_text_length) {
  1534. // Add a space because some fonts are too exact, and because caret needs a bit more when at the end.
  1535. min_size.width = MAX(min_size.width, full_width + em_space_size);
  1536. }
  1537. min_size.height = MAX(TS->shaped_text_get_size(text_rid).y, font->get_height(font_size));
  1538. // Take icons into account.
  1539. int icon_max_width = 0;
  1540. if (right_icon.is_valid()) {
  1541. min_size.height = MAX(min_size.height, right_icon->get_height());
  1542. icon_max_width = right_icon->get_width();
  1543. }
  1544. if (clear_button_enabled) {
  1545. min_size.height = MAX(min_size.height, theme_cache.clear_icon->get_height());
  1546. icon_max_width = MAX(icon_max_width, theme_cache.clear_icon->get_width());
  1547. }
  1548. min_size.width += icon_max_width;
  1549. return style->get_minimum_size() + min_size;
  1550. }
  1551. void LineEdit::deselect() {
  1552. selection.begin = 0;
  1553. selection.end = 0;
  1554. selection.start_column = 0;
  1555. selection.enabled = false;
  1556. selection.creating = false;
  1557. selection.double_click = false;
  1558. queue_redraw();
  1559. }
  1560. bool LineEdit::has_selection() const {
  1561. return selection.enabled;
  1562. }
  1563. int LineEdit::get_selection_from_column() const {
  1564. ERR_FAIL_COND_V(!selection.enabled, -1);
  1565. return selection.begin;
  1566. }
  1567. int LineEdit::get_selection_to_column() const {
  1568. ERR_FAIL_COND_V(!selection.enabled, -1);
  1569. return selection.end;
  1570. }
  1571. void LineEdit::selection_delete() {
  1572. if (selection.enabled) {
  1573. delete_text(selection.begin, selection.end);
  1574. }
  1575. deselect();
  1576. }
  1577. void LineEdit::set_max_length(int p_max_length) {
  1578. ERR_FAIL_COND(p_max_length < 0);
  1579. max_length = p_max_length;
  1580. set_text(text);
  1581. }
  1582. int LineEdit::get_max_length() const {
  1583. return max_length;
  1584. }
  1585. void LineEdit::selection_fill_at_caret() {
  1586. if (!selecting_enabled) {
  1587. return;
  1588. }
  1589. selection.begin = caret_column;
  1590. selection.end = selection.start_column;
  1591. if (selection.end < selection.begin) {
  1592. int aux = selection.end;
  1593. selection.end = selection.begin;
  1594. selection.begin = aux;
  1595. }
  1596. selection.enabled = (selection.begin != selection.end);
  1597. }
  1598. void LineEdit::select_all() {
  1599. if (!selecting_enabled) {
  1600. return;
  1601. }
  1602. if (!text.length()) {
  1603. return;
  1604. }
  1605. selection.begin = 0;
  1606. selection.end = text.length();
  1607. selection.enabled = true;
  1608. queue_redraw();
  1609. }
  1610. void LineEdit::set_editable(bool p_editable) {
  1611. if (editable == p_editable) {
  1612. return;
  1613. }
  1614. editable = p_editable;
  1615. _validate_caret_can_draw();
  1616. update_minimum_size();
  1617. queue_redraw();
  1618. }
  1619. bool LineEdit::is_editable() const {
  1620. return editable;
  1621. }
  1622. void LineEdit::set_secret(bool p_secret) {
  1623. if (pass == p_secret) {
  1624. return;
  1625. }
  1626. pass = p_secret;
  1627. _shape();
  1628. queue_redraw();
  1629. }
  1630. bool LineEdit::is_secret() const {
  1631. return pass;
  1632. }
  1633. void LineEdit::set_secret_character(const String &p_string) {
  1634. // An empty string as the secret character would crash the engine.
  1635. // It also wouldn't make sense to use multiple characters as the secret character.
  1636. ERR_FAIL_COND_MSG(p_string.length() != 1, "Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given).");
  1637. if (secret_character == p_string) {
  1638. return;
  1639. }
  1640. secret_character = p_string;
  1641. _shape();
  1642. queue_redraw();
  1643. }
  1644. String LineEdit::get_secret_character() const {
  1645. return secret_character;
  1646. }
  1647. void LineEdit::select(int p_from, int p_to) {
  1648. if (!selecting_enabled) {
  1649. return;
  1650. }
  1651. if (p_from == 0 && p_to == 0) {
  1652. deselect();
  1653. return;
  1654. }
  1655. int len = text.length();
  1656. if (p_from < 0) {
  1657. p_from = 0;
  1658. }
  1659. if (p_from > len) {
  1660. p_from = len;
  1661. }
  1662. if (p_to < 0 || p_to > len) {
  1663. p_to = len;
  1664. }
  1665. if (p_from >= p_to) {
  1666. return;
  1667. }
  1668. selection.enabled = true;
  1669. selection.begin = p_from;
  1670. selection.end = p_to;
  1671. selection.creating = false;
  1672. selection.double_click = false;
  1673. queue_redraw();
  1674. }
  1675. bool LineEdit::is_text_field() const {
  1676. return true;
  1677. }
  1678. void LineEdit::menu_option(int p_option) {
  1679. switch (p_option) {
  1680. case MENU_CUT: {
  1681. if (editable) {
  1682. cut_text();
  1683. }
  1684. } break;
  1685. case MENU_COPY: {
  1686. copy_text();
  1687. } break;
  1688. case MENU_PASTE: {
  1689. if (editable) {
  1690. paste_text();
  1691. }
  1692. } break;
  1693. case MENU_CLEAR: {
  1694. if (editable) {
  1695. clear();
  1696. }
  1697. } break;
  1698. case MENU_SELECT_ALL: {
  1699. select_all();
  1700. } break;
  1701. case MENU_UNDO: {
  1702. if (editable) {
  1703. undo();
  1704. }
  1705. } break;
  1706. case MENU_REDO: {
  1707. if (editable) {
  1708. redo();
  1709. }
  1710. } break;
  1711. case MENU_DIR_INHERITED: {
  1712. set_text_direction(TEXT_DIRECTION_INHERITED);
  1713. } break;
  1714. case MENU_DIR_AUTO: {
  1715. set_text_direction(TEXT_DIRECTION_AUTO);
  1716. } break;
  1717. case MENU_DIR_LTR: {
  1718. set_text_direction(TEXT_DIRECTION_LTR);
  1719. } break;
  1720. case MENU_DIR_RTL: {
  1721. set_text_direction(TEXT_DIRECTION_RTL);
  1722. } break;
  1723. case MENU_DISPLAY_UCC: {
  1724. set_draw_control_chars(!get_draw_control_chars());
  1725. } break;
  1726. case MENU_INSERT_LRM: {
  1727. if (editable) {
  1728. insert_text_at_caret(String::chr(0x200E));
  1729. }
  1730. } break;
  1731. case MENU_INSERT_RLM: {
  1732. if (editable) {
  1733. insert_text_at_caret(String::chr(0x200F));
  1734. }
  1735. } break;
  1736. case MENU_INSERT_LRE: {
  1737. if (editable) {
  1738. insert_text_at_caret(String::chr(0x202A));
  1739. }
  1740. } break;
  1741. case MENU_INSERT_RLE: {
  1742. if (editable) {
  1743. insert_text_at_caret(String::chr(0x202B));
  1744. }
  1745. } break;
  1746. case MENU_INSERT_LRO: {
  1747. if (editable) {
  1748. insert_text_at_caret(String::chr(0x202D));
  1749. }
  1750. } break;
  1751. case MENU_INSERT_RLO: {
  1752. if (editable) {
  1753. insert_text_at_caret(String::chr(0x202E));
  1754. }
  1755. } break;
  1756. case MENU_INSERT_PDF: {
  1757. if (editable) {
  1758. insert_text_at_caret(String::chr(0x202C));
  1759. }
  1760. } break;
  1761. case MENU_INSERT_ALM: {
  1762. if (editable) {
  1763. insert_text_at_caret(String::chr(0x061C));
  1764. }
  1765. } break;
  1766. case MENU_INSERT_LRI: {
  1767. if (editable) {
  1768. insert_text_at_caret(String::chr(0x2066));
  1769. }
  1770. } break;
  1771. case MENU_INSERT_RLI: {
  1772. if (editable) {
  1773. insert_text_at_caret(String::chr(0x2067));
  1774. }
  1775. } break;
  1776. case MENU_INSERT_FSI: {
  1777. if (editable) {
  1778. insert_text_at_caret(String::chr(0x2068));
  1779. }
  1780. } break;
  1781. case MENU_INSERT_PDI: {
  1782. if (editable) {
  1783. insert_text_at_caret(String::chr(0x2069));
  1784. }
  1785. } break;
  1786. case MENU_INSERT_ZWJ: {
  1787. if (editable) {
  1788. insert_text_at_caret(String::chr(0x200D));
  1789. }
  1790. } break;
  1791. case MENU_INSERT_ZWNJ: {
  1792. if (editable) {
  1793. insert_text_at_caret(String::chr(0x200C));
  1794. }
  1795. } break;
  1796. case MENU_INSERT_WJ: {
  1797. if (editable) {
  1798. insert_text_at_caret(String::chr(0x2060));
  1799. }
  1800. } break;
  1801. case MENU_INSERT_SHY: {
  1802. if (editable) {
  1803. insert_text_at_caret(String::chr(0x00AD));
  1804. }
  1805. }
  1806. }
  1807. }
  1808. void LineEdit::set_context_menu_enabled(bool p_enable) {
  1809. context_menu_enabled = p_enable;
  1810. }
  1811. bool LineEdit::is_context_menu_enabled() {
  1812. return context_menu_enabled;
  1813. }
  1814. bool LineEdit::is_menu_visible() const {
  1815. return menu && menu->is_visible();
  1816. }
  1817. PopupMenu *LineEdit::get_menu() const {
  1818. if (!menu) {
  1819. const_cast<LineEdit *>(this)->_generate_context_menu();
  1820. }
  1821. return menu;
  1822. }
  1823. void LineEdit::_editor_settings_changed() {
  1824. #ifdef TOOLS_ENABLED
  1825. set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  1826. set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  1827. #endif
  1828. }
  1829. void LineEdit::set_expand_to_text_length_enabled(bool p_enabled) {
  1830. expand_to_text_length = p_enabled;
  1831. update_minimum_size();
  1832. set_caret_column(caret_column);
  1833. }
  1834. bool LineEdit::is_expand_to_text_length_enabled() const {
  1835. return expand_to_text_length;
  1836. }
  1837. void LineEdit::set_clear_button_enabled(bool p_enabled) {
  1838. if (clear_button_enabled == p_enabled) {
  1839. return;
  1840. }
  1841. clear_button_enabled = p_enabled;
  1842. _fit_to_width();
  1843. update_minimum_size();
  1844. queue_redraw();
  1845. }
  1846. bool LineEdit::is_clear_button_enabled() const {
  1847. return clear_button_enabled;
  1848. }
  1849. void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
  1850. shortcut_keys_enabled = p_enabled;
  1851. }
  1852. bool LineEdit::is_shortcut_keys_enabled() const {
  1853. return shortcut_keys_enabled;
  1854. }
  1855. void LineEdit::set_virtual_keyboard_enabled(bool p_enable) {
  1856. virtual_keyboard_enabled = p_enable;
  1857. }
  1858. bool LineEdit::is_virtual_keyboard_enabled() const {
  1859. return virtual_keyboard_enabled;
  1860. }
  1861. void LineEdit::set_virtual_keyboard_type(VirtualKeyboardType p_type) {
  1862. virtual_keyboard_type = p_type;
  1863. }
  1864. LineEdit::VirtualKeyboardType LineEdit::get_virtual_keyboard_type() const {
  1865. return virtual_keyboard_type;
  1866. }
  1867. void LineEdit::set_middle_mouse_paste_enabled(bool p_enabled) {
  1868. middle_mouse_paste_enabled = p_enabled;
  1869. }
  1870. bool LineEdit::is_middle_mouse_paste_enabled() const {
  1871. return middle_mouse_paste_enabled;
  1872. }
  1873. void LineEdit::set_selecting_enabled(bool p_enabled) {
  1874. if (selecting_enabled == p_enabled) {
  1875. return;
  1876. }
  1877. selecting_enabled = p_enabled;
  1878. if (!selecting_enabled) {
  1879. deselect();
  1880. }
  1881. }
  1882. bool LineEdit::is_selecting_enabled() const {
  1883. return selecting_enabled;
  1884. }
  1885. void LineEdit::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
  1886. if (deselect_on_focus_loss_enabled == p_enabled) {
  1887. return;
  1888. }
  1889. deselect_on_focus_loss_enabled = p_enabled;
  1890. if (p_enabled && selection.enabled && !has_focus()) {
  1891. deselect();
  1892. }
  1893. }
  1894. bool LineEdit::is_deselect_on_focus_loss_enabled() const {
  1895. return deselect_on_focus_loss_enabled;
  1896. }
  1897. void LineEdit::set_right_icon(const Ref<Texture2D> &p_icon) {
  1898. if (right_icon == p_icon) {
  1899. return;
  1900. }
  1901. right_icon = p_icon;
  1902. _fit_to_width();
  1903. update_minimum_size();
  1904. queue_redraw();
  1905. }
  1906. Ref<Texture2D> LineEdit::get_right_icon() {
  1907. return right_icon;
  1908. }
  1909. void LineEdit::set_flat(bool p_enabled) {
  1910. if (flat != p_enabled) {
  1911. flat = p_enabled;
  1912. queue_redraw();
  1913. }
  1914. }
  1915. bool LineEdit::is_flat() const {
  1916. return flat;
  1917. }
  1918. void LineEdit::set_select_all_on_focus(bool p_enabled) {
  1919. select_all_on_focus = p_enabled;
  1920. }
  1921. bool LineEdit::is_select_all_on_focus() const {
  1922. return select_all_on_focus;
  1923. }
  1924. void LineEdit::clear_pending_select_all_on_focus() {
  1925. pending_select_all_on_focus = false;
  1926. }
  1927. void LineEdit::_text_changed() {
  1928. _emit_text_change();
  1929. _clear_redo();
  1930. }
  1931. void LineEdit::_emit_text_change() {
  1932. emit_signal(SNAME("text_changed"), text);
  1933. text_changed_dirty = false;
  1934. }
  1935. void LineEdit::_shape() {
  1936. const Ref<Font> &font = theme_cache.font;
  1937. int font_size = theme_cache.font_size;
  1938. if (font.is_null()) {
  1939. return;
  1940. }
  1941. Size2 old_size = TS->shaped_text_get_size(text_rid);
  1942. TS->shaped_text_clear(text_rid);
  1943. String t;
  1944. if (text.length() == 0 && ime_text.length() == 0) {
  1945. t = placeholder_translated;
  1946. } else if (pass) {
  1947. t = secret_character.repeat(text.length() + ime_text.length());
  1948. } else {
  1949. if (ime_text.length() > 0) {
  1950. t = text.substr(0, caret_column) + ime_text + text.substr(caret_column, text.length());
  1951. } else {
  1952. t = text;
  1953. }
  1954. }
  1955. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1956. TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1957. } else {
  1958. TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
  1959. }
  1960. TS->shaped_text_set_preserve_control(text_rid, draw_control_chars);
  1961. TS->shaped_text_add_string(text_rid, t, font->get_rids(), font_size, font->get_opentype_features(), language);
  1962. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  1963. TS->shaped_text_set_spacing(text_rid, TextServer::SpacingType(i), font->get_spacing(TextServer::SpacingType(i)));
  1964. }
  1965. TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, t));
  1966. full_width = TS->shaped_text_get_size(text_rid).x;
  1967. _fit_to_width();
  1968. Size2 size = TS->shaped_text_get_size(text_rid);
  1969. if ((expand_to_text_length && old_size.x != size.x) || (old_size.y != size.y)) {
  1970. update_minimum_size();
  1971. }
  1972. }
  1973. void LineEdit::_fit_to_width() {
  1974. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  1975. Ref<StyleBox> style = theme_cache.normal;
  1976. int t_width = get_size().width - style->get_margin(SIDE_RIGHT) - style->get_margin(SIDE_LEFT);
  1977. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1978. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1979. if (right_icon.is_valid() || display_clear_icon) {
  1980. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1981. t_width -= r_icon->get_width();
  1982. }
  1983. TS->shaped_text_fit_to_width(text_rid, MAX(t_width, full_width));
  1984. }
  1985. }
  1986. void LineEdit::_clear_redo() {
  1987. _create_undo_state();
  1988. if (undo_stack_pos == nullptr) {
  1989. return;
  1990. }
  1991. undo_stack_pos = undo_stack_pos->next();
  1992. while (undo_stack_pos) {
  1993. List<TextOperation>::Element *elem = undo_stack_pos;
  1994. undo_stack_pos = undo_stack_pos->next();
  1995. undo_stack.erase(elem);
  1996. }
  1997. _create_undo_state();
  1998. }
  1999. void LineEdit::_clear_undo_stack() {
  2000. undo_stack.clear();
  2001. undo_stack_pos = nullptr;
  2002. _create_undo_state();
  2003. }
  2004. void LineEdit::_create_undo_state() {
  2005. TextOperation op;
  2006. op.text = text;
  2007. op.caret_column = caret_column;
  2008. op.scroll_offset = scroll_offset;
  2009. undo_stack.push_back(op);
  2010. }
  2011. Key LineEdit::_get_menu_action_accelerator(const String &p_action) {
  2012. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  2013. if (!events) {
  2014. return Key::NONE;
  2015. }
  2016. // Use first event in the list for the accelerator.
  2017. const List<Ref<InputEvent>>::Element *first_event = events->front();
  2018. if (!first_event) {
  2019. return Key::NONE;
  2020. }
  2021. const Ref<InputEventKey> event = first_event->get();
  2022. if (event.is_null()) {
  2023. return Key::NONE;
  2024. }
  2025. // Use physical keycode if non-zero
  2026. if (event->get_physical_keycode() != Key::NONE) {
  2027. return event->get_physical_keycode_with_modifiers();
  2028. } else {
  2029. return event->get_keycode_with_modifiers();
  2030. }
  2031. }
  2032. void LineEdit::_generate_context_menu() {
  2033. menu = memnew(PopupMenu);
  2034. add_child(menu, false, INTERNAL_MODE_FRONT);
  2035. menu_dir = memnew(PopupMenu);
  2036. menu_dir->set_name("DirMenu");
  2037. menu_dir->add_radio_check_item(RTR("Same as Layout Direction"), MENU_DIR_INHERITED);
  2038. menu_dir->add_radio_check_item(RTR("Auto-Detect Direction"), MENU_DIR_AUTO);
  2039. menu_dir->add_radio_check_item(RTR("Left-to-Right"), MENU_DIR_LTR);
  2040. menu_dir->add_radio_check_item(RTR("Right-to-Left"), MENU_DIR_RTL);
  2041. menu->add_child(menu_dir, false, INTERNAL_MODE_FRONT);
  2042. menu_ctl = memnew(PopupMenu);
  2043. menu_ctl->set_name("CTLMenu");
  2044. menu_ctl->add_item(RTR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM);
  2045. menu_ctl->add_item(RTR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM);
  2046. menu_ctl->add_item(RTR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE);
  2047. menu_ctl->add_item(RTR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE);
  2048. menu_ctl->add_item(RTR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO);
  2049. menu_ctl->add_item(RTR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO);
  2050. menu_ctl->add_item(RTR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF);
  2051. menu_ctl->add_separator();
  2052. menu_ctl->add_item(RTR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM);
  2053. menu_ctl->add_item(RTR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI);
  2054. menu_ctl->add_item(RTR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI);
  2055. menu_ctl->add_item(RTR("First Strong Isolate (FSI)"), MENU_INSERT_FSI);
  2056. menu_ctl->add_item(RTR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI);
  2057. menu_ctl->add_separator();
  2058. menu_ctl->add_item(RTR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ);
  2059. menu_ctl->add_item(RTR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  2060. menu_ctl->add_item(RTR("Word Joiner (WJ)"), MENU_INSERT_WJ);
  2061. menu_ctl->add_item(RTR("Soft Hyphen (SHY)"), MENU_INSERT_SHY);
  2062. menu->add_child(menu_ctl, false, INTERNAL_MODE_FRONT);
  2063. menu->add_item(RTR("Cut"), MENU_CUT);
  2064. menu->add_item(RTR("Copy"), MENU_COPY);
  2065. menu->add_item(RTR("Paste"), MENU_PASTE);
  2066. menu->add_separator();
  2067. menu->add_item(RTR("Select All"), MENU_SELECT_ALL);
  2068. menu->add_item(RTR("Clear"), MENU_CLEAR);
  2069. menu->add_separator();
  2070. menu->add_item(RTR("Undo"), MENU_UNDO);
  2071. menu->add_item(RTR("Redo"), MENU_REDO);
  2072. menu->add_separator();
  2073. menu->add_submenu_item(RTR("Text Writing Direction"), "DirMenu", MENU_SUBMENU_TEXT_DIR);
  2074. menu->add_separator();
  2075. menu->add_check_item(RTR("Display Control Characters"), MENU_DISPLAY_UCC);
  2076. menu->add_submenu_item(RTR("Insert Control Character"), "CTLMenu", MENU_SUBMENU_INSERT_UCC);
  2077. menu->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2078. menu_dir->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2079. menu_ctl->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2080. menu->connect(SNAME("focus_entered"), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2081. menu->connect(SNAME("focus_exited"), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2082. }
  2083. void LineEdit::_update_context_menu() {
  2084. if (!menu) {
  2085. _generate_context_menu();
  2086. }
  2087. int idx = -1;
  2088. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  2089. idx = m_menu->get_item_index(m_id); \
  2090. if (idx >= 0) { \
  2091. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2092. m_menu->set_item_disabled(idx, m_disabled); \
  2093. }
  2094. #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \
  2095. idx = m_menu->get_item_index(m_id); \
  2096. if (idx >= 0) { \
  2097. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2098. }
  2099. #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \
  2100. idx = m_menu->get_item_index(m_id); \
  2101. if (idx >= 0) { \
  2102. m_menu->set_item_disabled(idx, m_disabled); \
  2103. }
  2104. #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \
  2105. idx = m_menu->get_item_index(m_id); \
  2106. if (idx >= 0) { \
  2107. m_menu->set_item_checked(idx, m_checked); \
  2108. }
  2109. MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable)
  2110. MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy")
  2111. MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable)
  2112. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled)
  2113. MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable)
  2114. MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo())
  2115. MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo())
  2116. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED)
  2117. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO)
  2118. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR)
  2119. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL)
  2120. MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars)
  2121. MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable)
  2122. #undef MENU_ITEM_ACTION_DISABLED
  2123. #undef MENU_ITEM_ACTION
  2124. #undef MENU_ITEM_DISABLED
  2125. #undef MENU_ITEM_CHECKED
  2126. }
  2127. void LineEdit::_validate_property(PropertyInfo &p_property) const {
  2128. if (!caret_blink_enabled && p_property.name == "caret_blink_interval") {
  2129. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2130. }
  2131. }
  2132. void LineEdit::_bind_methods() {
  2133. ClassDB::bind_method(D_METHOD("_text_changed"), &LineEdit::_text_changed);
  2134. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment);
  2135. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment);
  2136. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  2137. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  2138. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  2139. ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect);
  2140. ClassDB::bind_method(D_METHOD("has_selection"), &LineEdit::has_selection);
  2141. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &LineEdit::get_selection_from_column);
  2142. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &LineEdit::get_selection_to_column);
  2143. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  2144. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  2145. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &LineEdit::get_draw_control_chars);
  2146. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enable"), &LineEdit::set_draw_control_chars);
  2147. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LineEdit::set_text_direction);
  2148. ClassDB::bind_method(D_METHOD("get_text_direction"), &LineEdit::get_text_direction);
  2149. ClassDB::bind_method(D_METHOD("set_language", "language"), &LineEdit::set_language);
  2150. ClassDB::bind_method(D_METHOD("get_language"), &LineEdit::get_language);
  2151. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LineEdit::set_structured_text_bidi_override);
  2152. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LineEdit::get_structured_text_bidi_override);
  2153. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LineEdit::set_structured_text_bidi_override_options);
  2154. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LineEdit::get_structured_text_bidi_override_options);
  2155. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  2156. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  2157. ClassDB::bind_method(D_METHOD("set_caret_column", "position"), &LineEdit::set_caret_column);
  2158. ClassDB::bind_method(D_METHOD("get_caret_column"), &LineEdit::get_caret_column);
  2159. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset);
  2160. ClassDB::bind_method(D_METHOD("set_expand_to_text_length_enabled", "enabled"), &LineEdit::set_expand_to_text_length_enabled);
  2161. ClassDB::bind_method(D_METHOD("is_expand_to_text_length_enabled"), &LineEdit::is_expand_to_text_length_enabled);
  2162. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enabled"), &LineEdit::set_caret_blink_enabled);
  2163. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &LineEdit::is_caret_blink_enabled);
  2164. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &LineEdit::set_caret_mid_grapheme_enabled);
  2165. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &LineEdit::is_caret_mid_grapheme_enabled);
  2166. ClassDB::bind_method(D_METHOD("set_caret_force_displayed", "enabled"), &LineEdit::set_caret_force_displayed);
  2167. ClassDB::bind_method(D_METHOD("is_caret_force_displayed"), &LineEdit::is_caret_force_displayed);
  2168. ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &LineEdit::set_caret_blink_interval);
  2169. ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &LineEdit::get_caret_blink_interval);
  2170. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  2171. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  2172. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text"), &LineEdit::insert_text_at_caret);
  2173. ClassDB::bind_method(D_METHOD("delete_char_at_caret"), &LineEdit::delete_char);
  2174. ClassDB::bind_method(D_METHOD("delete_text", "from_column", "to_column"), &LineEdit::delete_text);
  2175. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  2176. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  2177. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  2178. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  2179. ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
  2180. ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
  2181. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  2182. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  2183. ClassDB::bind_method(D_METHOD("is_menu_visible"), &LineEdit::is_menu_visible);
  2184. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
  2185. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled);
  2186. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &LineEdit::set_virtual_keyboard_enabled);
  2187. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &LineEdit::is_virtual_keyboard_enabled);
  2188. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_type", "type"), &LineEdit::set_virtual_keyboard_type);
  2189. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_type"), &LineEdit::get_virtual_keyboard_type);
  2190. ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled);
  2191. ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled);
  2192. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled);
  2193. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled);
  2194. ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enable"), &LineEdit::set_middle_mouse_paste_enabled);
  2195. ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &LineEdit::is_middle_mouse_paste_enabled);
  2196. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled);
  2197. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled);
  2198. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &LineEdit::set_deselect_on_focus_loss_enabled);
  2199. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &LineEdit::is_deselect_on_focus_loss_enabled);
  2200. ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
  2201. ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
  2202. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &LineEdit::set_flat);
  2203. ClassDB::bind_method(D_METHOD("is_flat"), &LineEdit::is_flat);
  2204. ClassDB::bind_method(D_METHOD("set_select_all_on_focus", "enabled"), &LineEdit::set_select_all_on_focus);
  2205. ClassDB::bind_method(D_METHOD("is_select_all_on_focus"), &LineEdit::is_select_all_on_focus);
  2206. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text")));
  2207. ADD_SIGNAL(MethodInfo("text_change_rejected", PropertyInfo(Variant::STRING, "rejected_substring")));
  2208. ADD_SIGNAL(MethodInfo("text_submitted", PropertyInfo(Variant::STRING, "new_text")));
  2209. BIND_ENUM_CONSTANT(MENU_CUT);
  2210. BIND_ENUM_CONSTANT(MENU_COPY);
  2211. BIND_ENUM_CONSTANT(MENU_PASTE);
  2212. BIND_ENUM_CONSTANT(MENU_CLEAR);
  2213. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  2214. BIND_ENUM_CONSTANT(MENU_UNDO);
  2215. BIND_ENUM_CONSTANT(MENU_REDO);
  2216. BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR);
  2217. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  2218. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  2219. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  2220. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  2221. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  2222. BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC);
  2223. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  2224. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  2225. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  2226. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  2227. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  2228. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  2229. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  2230. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  2231. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  2232. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  2233. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  2234. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  2235. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  2236. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  2237. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  2238. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  2239. BIND_ENUM_CONSTANT(MENU_MAX);
  2240. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_DEFAULT);
  2241. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_MULTILINE);
  2242. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER);
  2243. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER_DECIMAL);
  2244. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PHONE);
  2245. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_EMAIL_ADDRESS);
  2246. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PASSWORD);
  2247. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_URL);
  2248. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  2249. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  2250. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  2251. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_max_length", "get_max_length");
  2252. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  2253. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret");
  2254. ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
  2255. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length_enabled", "is_expand_to_text_length_enabled");
  2256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  2257. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  2258. ADD_PROPERTY(PropertyInfo(Variant::INT, "virtual_keyboard_type", PROPERTY_HINT_ENUM, "Default,Multiline,Number,Decimal,Phone,Email,Password,URL"), "set_virtual_keyboard_type", "get_virtual_keyboard_type");
  2259. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled");
  2260. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  2261. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled");
  2262. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  2263. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  2264. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_right_icon", "get_right_icon");
  2265. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  2266. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  2267. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");
  2268. ADD_GROUP("Caret", "caret_");
  2269. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  2270. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_interval", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "set_caret_blink_interval", "get_caret_blink_interval");
  2271. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_column", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_caret_column", "get_caret_column");
  2272. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_force_displayed"), "set_caret_force_displayed", "is_caret_force_displayed");
  2273. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  2274. ADD_GROUP("BiDi", "");
  2275. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  2276. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  2277. 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");
  2278. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  2279. }
  2280. LineEdit::LineEdit(const String &p_placeholder) {
  2281. text_rid = TS->create_shaped_text();
  2282. _create_undo_state();
  2283. deselect();
  2284. set_focus_mode(FOCUS_ALL);
  2285. set_default_cursor_shape(CURSOR_IBEAM);
  2286. set_mouse_filter(MOUSE_FILTER_STOP);
  2287. set_process_unhandled_key_input(true);
  2288. set_caret_blink_enabled(false);
  2289. set_placeholder(p_placeholder);
  2290. set_editable(true); // Initialize to opposite first, so we get past the early-out in set_editable.
  2291. }
  2292. LineEdit::~LineEdit() {
  2293. TS->free_rid(text_rid);
  2294. }