line_edit.cpp 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  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. if (selection.drag_attempt && selection.enabled) {
  557. String t = text.substr(selection.begin, selection.end - selection.begin);
  558. Label *l = memnew(Label);
  559. l->set_text(t);
  560. set_drag_preview(l);
  561. return t;
  562. }
  563. return Variant();
  564. }
  565. bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  566. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  567. if (drop_override) {
  568. return drop_override;
  569. }
  570. return is_editable() && p_data.get_type() == Variant::STRING;
  571. }
  572. void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  573. Control::drop_data(p_point, p_data);
  574. if (p_data.get_type() == Variant::STRING && is_editable()) {
  575. set_caret_at_pixel_pos(p_point.x);
  576. int caret_column_tmp = caret_column;
  577. bool is_inside_sel = selection.enabled && caret_column >= selection.begin && caret_column <= selection.end;
  578. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  579. is_inside_sel = selection.enabled && caret_column > selection.begin && caret_column < selection.end;
  580. }
  581. if (selection.drag_attempt) {
  582. selection.drag_attempt = false;
  583. if (!is_inside_sel) {
  584. if (!Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  585. if (caret_column_tmp > selection.end) {
  586. caret_column_tmp = caret_column_tmp - (selection.end - selection.begin);
  587. }
  588. selection_delete();
  589. }
  590. set_caret_column(caret_column_tmp);
  591. insert_text_at_caret(p_data);
  592. }
  593. } else if (selection.enabled && caret_column >= selection.begin && caret_column <= selection.end) {
  594. caret_column_tmp = selection.begin;
  595. selection_delete();
  596. set_caret_column(caret_column_tmp);
  597. insert_text_at_caret(p_data);
  598. grab_focus();
  599. } else {
  600. insert_text_at_caret(p_data);
  601. grab_focus();
  602. }
  603. select(caret_column_tmp, caret_column);
  604. if (!text_changed_dirty) {
  605. if (is_inside_tree()) {
  606. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  607. }
  608. text_changed_dirty = true;
  609. }
  610. queue_redraw();
  611. }
  612. }
  613. Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const {
  614. if ((!text.is_empty() && is_editable() && _is_over_clear_button(p_pos)) || (!is_editable() && (!is_selecting_enabled() || text.is_empty()))) {
  615. return CURSOR_ARROW;
  616. }
  617. return Control::get_cursor_shape(p_pos);
  618. }
  619. bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const {
  620. if (!clear_button_enabled || !has_point(p_pos)) {
  621. return false;
  622. }
  623. Ref<Texture2D> icon = theme_cache.clear_icon;
  624. int x_ofs = theme_cache.normal->get_margin(SIDE_RIGHT);
  625. return p_pos.x > get_size().width - icon->get_width() - x_ofs;
  626. }
  627. void LineEdit::_update_theme_item_cache() {
  628. Control::_update_theme_item_cache();
  629. theme_cache.normal = get_theme_stylebox(SNAME("normal"));
  630. theme_cache.read_only = get_theme_stylebox(SNAME("read_only"));
  631. theme_cache.focus = get_theme_stylebox(SNAME("focus"));
  632. theme_cache.font = get_theme_font(SNAME("font"));
  633. theme_cache.font_size = get_theme_font_size(SNAME("font_size"));
  634. theme_cache.font_color = get_theme_color(SNAME("font_color"));
  635. theme_cache.font_uneditable_color = get_theme_color(SNAME("font_uneditable_color"));
  636. theme_cache.font_selected_color = get_theme_color(SNAME("font_selected_color"));
  637. theme_cache.font_outline_size = get_theme_constant(SNAME("outline_size"));
  638. theme_cache.font_outline_color = get_theme_color(SNAME("font_outline_color"));
  639. theme_cache.font_placeholder_color = get_theme_color(SNAME("font_placeholder_color"));
  640. theme_cache.caret_width = get_theme_constant(SNAME("caret_width"));
  641. theme_cache.caret_color = get_theme_color(SNAME("caret_color"));
  642. theme_cache.minimum_character_width = get_theme_constant(SNAME("minimum_character_width"));
  643. theme_cache.selection_color = get_theme_color(SNAME("selection_color"));
  644. theme_cache.clear_icon = get_theme_icon(SNAME("clear"));
  645. theme_cache.clear_button_color = get_theme_color(SNAME("clear_button_color"));
  646. theme_cache.clear_button_color_pressed = get_theme_color(SNAME("clear_button_color_pressed"));
  647. theme_cache.base_scale = get_theme_default_base_scale();
  648. }
  649. void LineEdit::_notification(int p_what) {
  650. switch (p_what) {
  651. #ifdef TOOLS_ENABLED
  652. case NOTIFICATION_ENTER_TREE: {
  653. if (Engine::get_singleton()->is_editor_hint() && !get_tree()->is_node_being_edited(this)) {
  654. set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  655. set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  656. if (!EditorSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed))) {
  657. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed));
  658. }
  659. }
  660. } break;
  661. #endif
  662. case NOTIFICATION_RESIZED: {
  663. _fit_to_width();
  664. scroll_offset = 0.0;
  665. set_caret_column(get_caret_column());
  666. } break;
  667. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  668. case NOTIFICATION_THEME_CHANGED: {
  669. _shape();
  670. queue_redraw();
  671. } break;
  672. case NOTIFICATION_TRANSLATION_CHANGED: {
  673. placeholder_translated = atr(placeholder);
  674. _shape();
  675. queue_redraw();
  676. } break;
  677. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  678. window_has_focus = true;
  679. _validate_caret_can_draw();
  680. queue_redraw();
  681. } break;
  682. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  683. window_has_focus = false;
  684. _validate_caret_can_draw();
  685. queue_redraw();
  686. } break;
  687. case NOTIFICATION_INTERNAL_PROCESS: {
  688. if (caret_blink_enabled && caret_can_draw) {
  689. caret_blink_timer += get_process_delta_time();
  690. if (caret_blink_timer >= caret_blink_interval) {
  691. caret_blink_timer = 0.0;
  692. _toggle_draw_caret();
  693. }
  694. }
  695. } break;
  696. case NOTIFICATION_DRAW: {
  697. int width, height;
  698. bool rtl = is_layout_rtl();
  699. Size2 size = get_size();
  700. width = size.width;
  701. height = size.height;
  702. RID ci = get_canvas_item();
  703. Ref<StyleBox> style = theme_cache.normal;
  704. if (!is_editable()) {
  705. style = theme_cache.read_only;
  706. }
  707. Ref<Font> font = theme_cache.font;
  708. if (!flat) {
  709. style->draw(ci, Rect2(Point2(), size));
  710. }
  711. if (has_focus()) {
  712. theme_cache.focus->draw(ci, Rect2(Point2(), size));
  713. }
  714. int x_ofs = 0;
  715. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  716. float text_width = TS->shaped_text_get_size(text_rid).x;
  717. float text_height = TS->shaped_text_get_size(text_rid).y;
  718. switch (alignment) {
  719. case HORIZONTAL_ALIGNMENT_FILL:
  720. case HORIZONTAL_ALIGNMENT_LEFT: {
  721. if (rtl) {
  722. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  723. } else {
  724. x_ofs = style->get_offset().x;
  725. }
  726. } break;
  727. case HORIZONTAL_ALIGNMENT_CENTER: {
  728. if (!Math::is_zero_approx(scroll_offset)) {
  729. x_ofs = style->get_offset().x;
  730. } else {
  731. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - (text_width)) / 2);
  732. }
  733. } break;
  734. case HORIZONTAL_ALIGNMENT_RIGHT: {
  735. if (rtl) {
  736. x_ofs = style->get_offset().x;
  737. } else {
  738. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  739. }
  740. } break;
  741. }
  742. int ofs_max = width - style->get_margin(SIDE_RIGHT);
  743. int y_area = height - style->get_minimum_size().height;
  744. int y_ofs = style->get_offset().y + (y_area - text_height) / 2;
  745. Color selection_color = theme_cache.selection_color;
  746. Color font_color;
  747. if (is_editable()) {
  748. font_color = theme_cache.font_color;
  749. } else {
  750. font_color = theme_cache.font_uneditable_color;
  751. }
  752. Color font_selected_color = theme_cache.font_selected_color;
  753. Color caret_color = theme_cache.caret_color;
  754. // Draw placeholder color.
  755. if (using_placeholder) {
  756. font_color = theme_cache.font_placeholder_color;
  757. }
  758. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  759. if (right_icon.is_valid() || display_clear_icon) {
  760. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  761. Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9);
  762. if (display_clear_icon) {
  763. if (clear_button_status.press_attempt && clear_button_status.pressing_inside) {
  764. color_icon = theme_cache.clear_button_color_pressed;
  765. } else {
  766. color_icon = theme_cache.clear_button_color;
  767. }
  768. }
  769. r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(SIDE_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
  770. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  771. if (Math::is_zero_approx(scroll_offset)) {
  772. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - text_width - r_icon->get_width() - style->get_margin(SIDE_RIGHT) * 2) / 2);
  773. }
  774. } else {
  775. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  776. }
  777. ofs_max -= r_icon->get_width();
  778. }
  779. // Draw selections rects.
  780. Vector2 ofs = Point2(x_ofs + scroll_offset, y_ofs);
  781. if (selection.enabled) {
  782. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, selection.begin, selection.end);
  783. for (int i = 0; i < sel.size(); i++) {
  784. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  785. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  786. continue;
  787. }
  788. if (rect.position.x < x_ofs) {
  789. rect.size.x -= (x_ofs - rect.position.x);
  790. rect.position.x = x_ofs;
  791. } else if (rect.position.x + rect.size.x > ofs_max) {
  792. rect.size.x = ofs_max - rect.position.x;
  793. }
  794. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_color);
  795. }
  796. }
  797. const Glyph *glyphs = TS->shaped_text_get_glyphs(text_rid);
  798. int gl_size = TS->shaped_text_get_glyph_count(text_rid);
  799. // Draw text.
  800. ofs.y += TS->shaped_text_get_ascent(text_rid);
  801. Color font_outline_color = theme_cache.font_outline_color;
  802. int outline_size = theme_cache.font_outline_size;
  803. if (outline_size > 0 && font_outline_color.a > 0) {
  804. Vector2 oofs = ofs;
  805. for (int i = 0; i < gl_size; i++) {
  806. for (int j = 0; j < glyphs[i].repeat; j++) {
  807. if (ceil(oofs.x) >= x_ofs && (oofs.x + glyphs[i].advance) <= ofs_max) {
  808. if (glyphs[i].font_rid != RID()) {
  809. 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);
  810. }
  811. }
  812. oofs.x += glyphs[i].advance;
  813. }
  814. if (oofs.x >= ofs_max) {
  815. break;
  816. }
  817. }
  818. }
  819. for (int i = 0; i < gl_size; i++) {
  820. bool selected = selection.enabled && glyphs[i].start >= selection.begin && glyphs[i].end <= selection.end;
  821. for (int j = 0; j < glyphs[i].repeat; j++) {
  822. if (ceil(ofs.x) >= x_ofs && (ofs.x + glyphs[i].advance) <= ofs_max) {
  823. if (glyphs[i].font_rid != RID()) {
  824. 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);
  825. } else if ((glyphs[i].flags & TextServer::GRAPHEME_IS_VIRTUAL) != TextServer::GRAPHEME_IS_VIRTUAL) {
  826. 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);
  827. }
  828. }
  829. ofs.x += glyphs[i].advance;
  830. }
  831. if (ofs.x >= ofs_max) {
  832. break;
  833. }
  834. }
  835. // Draw carets.
  836. ofs.x = x_ofs + scroll_offset;
  837. if ((caret_can_draw && draw_caret) || drag_caret_force_displayed) {
  838. // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
  839. const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
  840. if (ime_text.is_empty() || ime_selection.y == 0) {
  841. // Normal caret.
  842. CaretInfo caret = TS->shaped_text_get_carets(text_rid, ime_text.is_empty() ? caret_column : caret_column + ime_selection.x);
  843. if (using_placeholder || (caret.l_caret == Rect2() && caret.t_caret == Rect2())) {
  844. // No carets, add one at the start.
  845. int h = theme_cache.font->get_height(theme_cache.font_size);
  846. int y = style->get_offset().y + (y_area - h) / 2;
  847. caret.l_dir = (rtl) ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  848. switch (alignment) {
  849. case HORIZONTAL_ALIGNMENT_FILL:
  850. case HORIZONTAL_ALIGNMENT_LEFT: {
  851. if (rtl) {
  852. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  853. } else {
  854. caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h));
  855. }
  856. } break;
  857. case HORIZONTAL_ALIGNMENT_CENTER: {
  858. caret.l_caret = Rect2(Vector2(size.x / 2, y), Size2(caret_width, h));
  859. } break;
  860. case HORIZONTAL_ALIGNMENT_RIGHT: {
  861. if (rtl) {
  862. caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h));
  863. } else {
  864. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  865. }
  866. } break;
  867. }
  868. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  869. } else {
  870. if (caret.l_caret != Rect2() && caret.l_dir == TextServer::DIRECTION_AUTO) {
  871. // Draw extra marker on top of mid caret.
  872. Rect2 trect = Rect2(caret.l_caret.position.x - 2.5 * caret_width, caret.l_caret.position.y, 6 * caret_width, caret_width);
  873. trect.position += ofs;
  874. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  875. } else if (caret.l_caret != Rect2() && caret.t_caret != Rect2() && caret.l_dir != caret.t_dir) {
  876. // Draw extra direction marker on top of split caret.
  877. float d = (caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  878. 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);
  879. trect.position += ofs;
  880. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  881. d = (caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  882. trect = Rect2(caret.t_caret.position.x + d * caret_width, caret.t_caret.position.y, 3 * caret_width, caret_width);
  883. trect.position += ofs;
  884. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  885. }
  886. caret.l_caret.position += ofs;
  887. caret.l_caret.size.x = caret_width;
  888. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  889. caret.t_caret.position += ofs;
  890. caret.t_caret.size.x = caret_width;
  891. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.t_caret, caret_color);
  892. }
  893. }
  894. if (!ime_text.is_empty()) {
  895. {
  896. // IME intermediate text range.
  897. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column, caret_column + ime_text.length());
  898. for (int i = 0; i < sel.size(); i++) {
  899. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  900. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  901. continue;
  902. }
  903. if (rect.position.x < x_ofs) {
  904. rect.size.x -= (x_ofs - rect.position.x);
  905. rect.position.x = x_ofs;
  906. } else if (rect.position.x + rect.size.x > ofs_max) {
  907. rect.size.x = ofs_max - rect.position.x;
  908. }
  909. rect.size.y = caret_width;
  910. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  911. }
  912. }
  913. {
  914. // IME caret.
  915. if (ime_selection.y > 0) {
  916. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column + ime_selection.x, caret_column + ime_selection.x + ime_selection.y);
  917. for (int i = 0; i < sel.size(); i++) {
  918. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  919. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  920. continue;
  921. }
  922. if (rect.position.x < x_ofs) {
  923. rect.size.x -= (x_ofs - rect.position.x);
  924. rect.position.x = x_ofs;
  925. } else if (rect.position.x + rect.size.x > ofs_max) {
  926. rect.size.x = ofs_max - rect.position.x;
  927. }
  928. rect.size.y = caret_width * 3;
  929. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  930. }
  931. }
  932. }
  933. }
  934. }
  935. if (has_focus()) {
  936. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  937. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  938. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2);
  939. DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + pos, get_viewport()->get_window_id());
  940. }
  941. }
  942. } break;
  943. case NOTIFICATION_FOCUS_ENTER: {
  944. _validate_caret_can_draw();
  945. if (select_all_on_focus) {
  946. if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  947. // Select all when the mouse button is up.
  948. pending_select_all_on_focus = true;
  949. } else {
  950. select_all();
  951. }
  952. }
  953. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  954. DisplayServer::get_singleton()->window_set_ime_active(true, get_viewport()->get_window_id());
  955. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2);
  956. DisplayServer::get_singleton()->window_set_ime_position(get_global_position() + pos, get_viewport()->get_window_id());
  957. }
  958. show_virtual_keyboard();
  959. } break;
  960. case NOTIFICATION_FOCUS_EXIT: {
  961. _validate_caret_can_draw();
  962. if (get_viewport()->get_window_id() != DisplayServer::INVALID_WINDOW_ID && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  963. DisplayServer::get_singleton()->window_set_ime_position(Point2(), get_viewport()->get_window_id());
  964. DisplayServer::get_singleton()->window_set_ime_active(false, get_viewport()->get_window_id());
  965. }
  966. ime_text = "";
  967. ime_selection = Point2();
  968. _shape();
  969. set_caret_column(caret_column); // Update scroll_offset
  970. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  971. DisplayServer::get_singleton()->virtual_keyboard_hide();
  972. }
  973. if (deselect_on_focus_loss_enabled && !selection.drag_attempt) {
  974. deselect();
  975. }
  976. } break;
  977. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  978. if (has_focus()) {
  979. ime_text = DisplayServer::get_singleton()->ime_get_text();
  980. ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  981. if (!ime_text.is_empty()) {
  982. selection_delete();
  983. }
  984. _shape();
  985. set_caret_column(caret_column); // Update scroll_offset
  986. queue_redraw();
  987. }
  988. } break;
  989. case NOTIFICATION_DRAG_BEGIN: {
  990. drag_action = true;
  991. } break;
  992. case NOTIFICATION_DRAG_END: {
  993. if (is_drag_successful()) {
  994. if (selection.drag_attempt) {
  995. selection.drag_attempt = false;
  996. if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  997. selection_delete();
  998. } else if (deselect_on_focus_loss_enabled) {
  999. deselect();
  1000. }
  1001. }
  1002. } else {
  1003. selection.drag_attempt = false;
  1004. }
  1005. drag_action = false;
  1006. drag_caret_force_displayed = false;
  1007. } break;
  1008. }
  1009. }
  1010. void LineEdit::copy_text() {
  1011. if (selection.enabled && !pass) {
  1012. DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
  1013. }
  1014. }
  1015. void LineEdit::cut_text() {
  1016. if (editable && selection.enabled && !pass) {
  1017. DisplayServer::get_singleton()->clipboard_set(text.substr(selection.begin, selection.end - selection.begin));
  1018. selection_delete();
  1019. }
  1020. }
  1021. void LineEdit::paste_text() {
  1022. if (!editable) {
  1023. return;
  1024. }
  1025. // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
  1026. String paste_buffer = DisplayServer::get_singleton()->clipboard_get().strip_escapes();
  1027. if (!paste_buffer.is_empty()) {
  1028. int prev_len = text.length();
  1029. if (selection.enabled) {
  1030. selection_delete();
  1031. }
  1032. insert_text_at_caret(paste_buffer);
  1033. if (!text_changed_dirty) {
  1034. if (is_inside_tree() && text.length() != prev_len) {
  1035. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  1036. }
  1037. text_changed_dirty = true;
  1038. }
  1039. }
  1040. }
  1041. bool LineEdit::has_undo() const {
  1042. if (undo_stack_pos == nullptr) {
  1043. return undo_stack.size() > 1;
  1044. }
  1045. return undo_stack_pos != undo_stack.front();
  1046. }
  1047. bool LineEdit::has_redo() const {
  1048. return undo_stack_pos != nullptr && undo_stack_pos != undo_stack.back();
  1049. }
  1050. void LineEdit::undo() {
  1051. if (!editable) {
  1052. return;
  1053. }
  1054. if (undo_stack_pos == nullptr) {
  1055. if (undo_stack.size() <= 1) {
  1056. return;
  1057. }
  1058. undo_stack_pos = undo_stack.back();
  1059. } else if (undo_stack_pos == undo_stack.front()) {
  1060. return;
  1061. }
  1062. deselect();
  1063. undo_stack_pos = undo_stack_pos->prev();
  1064. TextOperation op = undo_stack_pos->get();
  1065. text = op.text;
  1066. scroll_offset = op.scroll_offset;
  1067. set_caret_column(op.caret_column);
  1068. _shape();
  1069. _emit_text_change();
  1070. }
  1071. void LineEdit::redo() {
  1072. if (!editable) {
  1073. return;
  1074. }
  1075. if (undo_stack_pos == nullptr) {
  1076. return;
  1077. }
  1078. if (undo_stack_pos == undo_stack.back()) {
  1079. return;
  1080. }
  1081. deselect();
  1082. undo_stack_pos = undo_stack_pos->next();
  1083. TextOperation op = undo_stack_pos->get();
  1084. text = op.text;
  1085. scroll_offset = op.scroll_offset;
  1086. set_caret_column(op.caret_column);
  1087. _shape();
  1088. _emit_text_change();
  1089. }
  1090. void LineEdit::shift_selection_check_pre(bool p_shift) {
  1091. if (!selection.enabled && p_shift) {
  1092. selection.start_column = caret_column;
  1093. }
  1094. if (!p_shift) {
  1095. deselect();
  1096. }
  1097. }
  1098. void LineEdit::shift_selection_check_post(bool p_shift) {
  1099. if (p_shift) {
  1100. selection_fill_at_caret();
  1101. }
  1102. }
  1103. void LineEdit::set_caret_at_pixel_pos(int p_x) {
  1104. Ref<StyleBox> style = theme_cache.normal;
  1105. bool rtl = is_layout_rtl();
  1106. int x_ofs = 0;
  1107. float text_width = TS->shaped_text_get_size(text_rid).x;
  1108. switch (alignment) {
  1109. case HORIZONTAL_ALIGNMENT_FILL:
  1110. case HORIZONTAL_ALIGNMENT_LEFT: {
  1111. if (rtl) {
  1112. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1113. } else {
  1114. x_ofs = style->get_offset().x;
  1115. }
  1116. } break;
  1117. case HORIZONTAL_ALIGNMENT_CENTER: {
  1118. if (!Math::is_zero_approx(scroll_offset)) {
  1119. x_ofs = style->get_offset().x;
  1120. } else {
  1121. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
  1122. }
  1123. } break;
  1124. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1125. if (rtl) {
  1126. x_ofs = style->get_offset().x;
  1127. } else {
  1128. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1129. }
  1130. } break;
  1131. }
  1132. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1133. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1134. if (right_icon.is_valid() || display_clear_icon) {
  1135. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1136. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1137. if (Math::is_zero_approx(scroll_offset)) {
  1138. 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);
  1139. }
  1140. } else {
  1141. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1142. }
  1143. }
  1144. int ofs = ceil(TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset));
  1145. set_caret_column(ofs);
  1146. }
  1147. Vector2 LineEdit::get_caret_pixel_pos() {
  1148. Ref<StyleBox> style = theme_cache.normal;
  1149. bool rtl = is_layout_rtl();
  1150. int x_ofs = 0;
  1151. float text_width = TS->shaped_text_get_size(text_rid).x;
  1152. switch (alignment) {
  1153. case HORIZONTAL_ALIGNMENT_FILL:
  1154. case HORIZONTAL_ALIGNMENT_LEFT: {
  1155. if (rtl) {
  1156. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1157. } else {
  1158. x_ofs = style->get_offset().x;
  1159. }
  1160. } break;
  1161. case HORIZONTAL_ALIGNMENT_CENTER: {
  1162. if (!Math::is_zero_approx(scroll_offset)) {
  1163. x_ofs = style->get_offset().x;
  1164. } else {
  1165. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
  1166. }
  1167. } break;
  1168. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1169. if (rtl) {
  1170. x_ofs = style->get_offset().x;
  1171. } else {
  1172. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1173. }
  1174. } break;
  1175. }
  1176. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1177. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1178. if (right_icon.is_valid() || display_clear_icon) {
  1179. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1180. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1181. if (Math::is_zero_approx(scroll_offset)) {
  1182. 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);
  1183. }
  1184. } else {
  1185. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1186. }
  1187. }
  1188. Vector2 ret;
  1189. CaretInfo caret;
  1190. // Get position of the start of caret.
  1191. if (ime_text.length() != 0 && ime_selection.x != 0) {
  1192. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x);
  1193. } else {
  1194. caret = TS->shaped_text_get_carets(text_rid, caret_column);
  1195. }
  1196. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1197. ret.x = x_ofs + caret.l_caret.position.x + scroll_offset;
  1198. } else {
  1199. ret.x = x_ofs + caret.t_caret.position.x + scroll_offset;
  1200. }
  1201. // Get position of the end of caret.
  1202. if (ime_text.length() != 0) {
  1203. if (ime_selection.y != 0) {
  1204. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x + ime_selection.y);
  1205. } else {
  1206. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_text.size());
  1207. }
  1208. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1209. ret.y = x_ofs + caret.l_caret.position.x + scroll_offset;
  1210. } else {
  1211. ret.y = x_ofs + caret.t_caret.position.x + scroll_offset;
  1212. }
  1213. } else {
  1214. ret.y = ret.x;
  1215. }
  1216. return ret;
  1217. }
  1218. void LineEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) {
  1219. caret_mid_grapheme_enabled = p_enabled;
  1220. }
  1221. bool LineEdit::is_caret_mid_grapheme_enabled() const {
  1222. return caret_mid_grapheme_enabled;
  1223. }
  1224. bool LineEdit::is_caret_blink_enabled() const {
  1225. return caret_blink_enabled;
  1226. }
  1227. void LineEdit::set_caret_blink_enabled(const bool p_enabled) {
  1228. if (caret_blink_enabled == p_enabled) {
  1229. return;
  1230. }
  1231. caret_blink_enabled = p_enabled;
  1232. set_process_internal(p_enabled);
  1233. draw_caret = !caret_blink_enabled;
  1234. if (caret_blink_enabled) {
  1235. caret_blink_timer = 0.0;
  1236. }
  1237. queue_redraw();
  1238. notify_property_list_changed();
  1239. }
  1240. bool LineEdit::is_caret_force_displayed() const {
  1241. return caret_force_displayed;
  1242. }
  1243. void LineEdit::set_caret_force_displayed(const bool p_enabled) {
  1244. if (caret_force_displayed == p_enabled) {
  1245. return;
  1246. }
  1247. caret_force_displayed = p_enabled;
  1248. _validate_caret_can_draw();
  1249. queue_redraw();
  1250. }
  1251. float LineEdit::get_caret_blink_interval() const {
  1252. return caret_blink_interval;
  1253. }
  1254. void LineEdit::set_caret_blink_interval(const float p_interval) {
  1255. ERR_FAIL_COND(p_interval <= 0);
  1256. caret_blink_interval = p_interval;
  1257. }
  1258. void LineEdit::_reset_caret_blink_timer() {
  1259. if (caret_blink_enabled) {
  1260. draw_caret = true;
  1261. if (caret_can_draw) {
  1262. caret_blink_timer = 0.0;
  1263. queue_redraw();
  1264. }
  1265. }
  1266. }
  1267. void LineEdit::_toggle_draw_caret() {
  1268. draw_caret = !draw_caret;
  1269. if (is_visible_in_tree() && caret_can_draw) {
  1270. queue_redraw();
  1271. }
  1272. }
  1273. void LineEdit::_validate_caret_can_draw() {
  1274. if (caret_blink_enabled) {
  1275. draw_caret = true;
  1276. caret_blink_timer = 0.0;
  1277. }
  1278. caret_can_draw = editable && (window_has_focus || (menu && menu->has_focus())) && (has_focus() || caret_force_displayed);
  1279. }
  1280. void LineEdit::delete_char() {
  1281. if ((text.length() <= 0) || (caret_column == 0)) {
  1282. return;
  1283. }
  1284. text = text.left(caret_column - 1) + text.substr(caret_column);
  1285. _shape();
  1286. set_caret_column(get_caret_column() - 1);
  1287. _text_changed();
  1288. }
  1289. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  1290. ERR_FAIL_COND_MSG(p_from_column < 0 || p_from_column > p_to_column || p_to_column > text.length(),
  1291. vformat("Positional parameters (from: %d, to: %d) are inverted or outside the text length (%d).", p_from_column, p_to_column, text.length()));
  1292. text = text.left(p_from_column) + text.substr(p_to_column);
  1293. _shape();
  1294. caret_column -= CLAMP(caret_column - p_from_column, 0, p_to_column - p_from_column);
  1295. if (caret_column >= text.length()) {
  1296. caret_column = text.length();
  1297. }
  1298. if (!text_changed_dirty) {
  1299. if (is_inside_tree()) {
  1300. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  1301. }
  1302. text_changed_dirty = true;
  1303. }
  1304. }
  1305. void LineEdit::set_text(String p_text) {
  1306. clear_internal();
  1307. insert_text_at_caret(p_text);
  1308. _create_undo_state();
  1309. queue_redraw();
  1310. caret_column = 0;
  1311. scroll_offset = 0.0;
  1312. }
  1313. void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
  1314. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1315. if (text_direction != p_text_direction) {
  1316. text_direction = p_text_direction;
  1317. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  1318. input_direction = text_direction;
  1319. }
  1320. _shape();
  1321. if (menu_dir) {
  1322. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  1323. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  1324. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  1325. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  1326. }
  1327. queue_redraw();
  1328. }
  1329. }
  1330. Control::TextDirection LineEdit::get_text_direction() const {
  1331. return text_direction;
  1332. }
  1333. void LineEdit::set_language(const String &p_language) {
  1334. if (language != p_language) {
  1335. language = p_language;
  1336. _shape();
  1337. queue_redraw();
  1338. }
  1339. }
  1340. String LineEdit::get_language() const {
  1341. return language;
  1342. }
  1343. void LineEdit::set_draw_control_chars(bool p_draw_control_chars) {
  1344. if (draw_control_chars != p_draw_control_chars) {
  1345. draw_control_chars = p_draw_control_chars;
  1346. if (menu && menu->get_item_index(MENU_DISPLAY_UCC) >= 0) {
  1347. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  1348. }
  1349. _shape();
  1350. queue_redraw();
  1351. }
  1352. }
  1353. bool LineEdit::get_draw_control_chars() const {
  1354. return draw_control_chars;
  1355. }
  1356. void LineEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  1357. if (st_parser != p_parser) {
  1358. st_parser = p_parser;
  1359. _shape();
  1360. queue_redraw();
  1361. }
  1362. }
  1363. TextServer::StructuredTextParser LineEdit::get_structured_text_bidi_override() const {
  1364. return st_parser;
  1365. }
  1366. void LineEdit::set_structured_text_bidi_override_options(Array p_args) {
  1367. st_args = p_args;
  1368. _shape();
  1369. queue_redraw();
  1370. }
  1371. Array LineEdit::get_structured_text_bidi_override_options() const {
  1372. return st_args;
  1373. }
  1374. void LineEdit::clear() {
  1375. clear_internal();
  1376. _text_changed();
  1377. // This should reset virtual keyboard state if needed.
  1378. if (has_focus()) {
  1379. show_virtual_keyboard();
  1380. }
  1381. }
  1382. void LineEdit::show_virtual_keyboard() {
  1383. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1384. if (selection.enabled) {
  1385. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, selection.begin, selection.end);
  1386. } else {
  1387. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, caret_column);
  1388. }
  1389. }
  1390. }
  1391. String LineEdit::get_text() const {
  1392. return text;
  1393. }
  1394. void LineEdit::set_placeholder(String p_text) {
  1395. if (placeholder == p_text) {
  1396. return;
  1397. }
  1398. placeholder = p_text;
  1399. placeholder_translated = atr(placeholder);
  1400. _shape();
  1401. queue_redraw();
  1402. }
  1403. String LineEdit::get_placeholder() const {
  1404. return placeholder;
  1405. }
  1406. void LineEdit::set_caret_column(int p_column) {
  1407. if (p_column > (int)text.length()) {
  1408. p_column = text.length();
  1409. }
  1410. if (p_column < 0) {
  1411. p_column = 0;
  1412. }
  1413. caret_column = p_column;
  1414. // Fit to window.
  1415. if (!is_inside_tree()) {
  1416. scroll_offset = 0.0;
  1417. return;
  1418. }
  1419. Ref<StyleBox> style = theme_cache.normal;
  1420. bool rtl = is_layout_rtl();
  1421. int x_ofs = 0;
  1422. float text_width = TS->shaped_text_get_size(text_rid).x;
  1423. switch (alignment) {
  1424. case HORIZONTAL_ALIGNMENT_FILL:
  1425. case HORIZONTAL_ALIGNMENT_LEFT: {
  1426. if (rtl) {
  1427. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1428. } else {
  1429. x_ofs = style->get_offset().x;
  1430. }
  1431. } break;
  1432. case HORIZONTAL_ALIGNMENT_CENTER: {
  1433. if (!Math::is_zero_approx(scroll_offset)) {
  1434. x_ofs = style->get_offset().x;
  1435. } else {
  1436. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - (text_width)) / 2);
  1437. }
  1438. } break;
  1439. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1440. if (rtl) {
  1441. x_ofs = style->get_offset().x;
  1442. } else {
  1443. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1444. }
  1445. } break;
  1446. }
  1447. int ofs_max = get_size().width - style->get_margin(SIDE_RIGHT);
  1448. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1449. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1450. if (right_icon.is_valid() || display_clear_icon) {
  1451. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1452. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1453. if (Math::is_zero_approx(scroll_offset)) {
  1454. 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);
  1455. }
  1456. } else {
  1457. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1458. }
  1459. ofs_max -= r_icon->get_width();
  1460. }
  1461. // Note: Use two coordinates to fit IME input range.
  1462. Vector2 primary_caret_offset = get_caret_pixel_pos();
  1463. if (MIN(primary_caret_offset.x, primary_caret_offset.y) <= x_ofs) {
  1464. scroll_offset += x_ofs - MIN(primary_caret_offset.x, primary_caret_offset.y);
  1465. } else if (MAX(primary_caret_offset.x, primary_caret_offset.y) >= ofs_max) {
  1466. scroll_offset += ofs_max - MAX(primary_caret_offset.x, primary_caret_offset.y);
  1467. }
  1468. scroll_offset = MIN(0, scroll_offset);
  1469. queue_redraw();
  1470. }
  1471. int LineEdit::get_caret_column() const {
  1472. return caret_column;
  1473. }
  1474. void LineEdit::set_scroll_offset(float p_pos) {
  1475. scroll_offset = p_pos;
  1476. if (scroll_offset < 0.0) {
  1477. scroll_offset = 0.0;
  1478. }
  1479. }
  1480. float LineEdit::get_scroll_offset() const {
  1481. return scroll_offset;
  1482. }
  1483. void LineEdit::insert_text_at_caret(String p_text) {
  1484. if (max_length > 0) {
  1485. // Truncate text to append to fit in max_length, if needed.
  1486. int available_chars = max_length - text.length();
  1487. if (p_text.length() > available_chars) {
  1488. emit_signal(SNAME("text_change_rejected"), p_text.substr(available_chars));
  1489. p_text = p_text.substr(0, available_chars);
  1490. }
  1491. }
  1492. String pre = text.substr(0, caret_column);
  1493. String post = text.substr(caret_column, text.length() - caret_column);
  1494. text = pre + p_text + post;
  1495. _shape();
  1496. TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text_rid, caret_column, caret_column + p_text.length());
  1497. if (dir != TextServer::DIRECTION_AUTO) {
  1498. input_direction = (TextDirection)dir;
  1499. }
  1500. set_caret_column(caret_column + p_text.length());
  1501. if (!ime_text.is_empty()) {
  1502. _shape();
  1503. }
  1504. }
  1505. void LineEdit::clear_internal() {
  1506. deselect();
  1507. _clear_undo_stack();
  1508. caret_column = 0;
  1509. scroll_offset = 0.0;
  1510. undo_text = "";
  1511. text = "";
  1512. _shape();
  1513. queue_redraw();
  1514. }
  1515. Size2 LineEdit::get_minimum_size() const {
  1516. Ref<StyleBox> style = theme_cache.normal;
  1517. Ref<Font> font = theme_cache.font;
  1518. int font_size = theme_cache.font_size;
  1519. Size2 min_size;
  1520. // Minimum size of text.
  1521. float em_space_size = font->get_char_size('M', font_size).x;
  1522. min_size.width = theme_cache.minimum_character_width * em_space_size;
  1523. if (expand_to_text_length) {
  1524. // Add a space because some fonts are too exact, and because caret needs a bit more when at the end.
  1525. min_size.width = MAX(min_size.width, full_width + em_space_size);
  1526. }
  1527. min_size.height = MAX(TS->shaped_text_get_size(text_rid).y, font->get_height(font_size));
  1528. // Take icons into account.
  1529. int icon_max_width = 0;
  1530. if (right_icon.is_valid()) {
  1531. min_size.height = MAX(min_size.height, right_icon->get_height());
  1532. icon_max_width = right_icon->get_width();
  1533. }
  1534. if (clear_button_enabled) {
  1535. min_size.height = MAX(min_size.height, theme_cache.clear_icon->get_height());
  1536. icon_max_width = MAX(icon_max_width, theme_cache.clear_icon->get_width());
  1537. }
  1538. min_size.width += icon_max_width;
  1539. return style->get_minimum_size() + min_size;
  1540. }
  1541. void LineEdit::deselect() {
  1542. selection.begin = 0;
  1543. selection.end = 0;
  1544. selection.start_column = 0;
  1545. selection.enabled = false;
  1546. selection.creating = false;
  1547. selection.double_click = false;
  1548. queue_redraw();
  1549. }
  1550. bool LineEdit::has_selection() const {
  1551. return selection.enabled;
  1552. }
  1553. int LineEdit::get_selection_from_column() const {
  1554. ERR_FAIL_COND_V(!selection.enabled, -1);
  1555. return selection.begin;
  1556. }
  1557. int LineEdit::get_selection_to_column() const {
  1558. ERR_FAIL_COND_V(!selection.enabled, -1);
  1559. return selection.end;
  1560. }
  1561. void LineEdit::selection_delete() {
  1562. if (selection.enabled) {
  1563. delete_text(selection.begin, selection.end);
  1564. }
  1565. deselect();
  1566. }
  1567. void LineEdit::set_max_length(int p_max_length) {
  1568. ERR_FAIL_COND(p_max_length < 0);
  1569. max_length = p_max_length;
  1570. set_text(text);
  1571. }
  1572. int LineEdit::get_max_length() const {
  1573. return max_length;
  1574. }
  1575. void LineEdit::selection_fill_at_caret() {
  1576. if (!selecting_enabled) {
  1577. return;
  1578. }
  1579. selection.begin = caret_column;
  1580. selection.end = selection.start_column;
  1581. if (selection.end < selection.begin) {
  1582. int aux = selection.end;
  1583. selection.end = selection.begin;
  1584. selection.begin = aux;
  1585. }
  1586. selection.enabled = (selection.begin != selection.end);
  1587. }
  1588. void LineEdit::select_all() {
  1589. if (!selecting_enabled) {
  1590. return;
  1591. }
  1592. if (!text.length()) {
  1593. return;
  1594. }
  1595. selection.begin = 0;
  1596. selection.end = text.length();
  1597. selection.enabled = true;
  1598. queue_redraw();
  1599. }
  1600. void LineEdit::set_editable(bool p_editable) {
  1601. if (editable == p_editable) {
  1602. return;
  1603. }
  1604. editable = p_editable;
  1605. _validate_caret_can_draw();
  1606. update_minimum_size();
  1607. queue_redraw();
  1608. }
  1609. bool LineEdit::is_editable() const {
  1610. return editable;
  1611. }
  1612. void LineEdit::set_secret(bool p_secret) {
  1613. if (pass == p_secret) {
  1614. return;
  1615. }
  1616. pass = p_secret;
  1617. _shape();
  1618. queue_redraw();
  1619. }
  1620. bool LineEdit::is_secret() const {
  1621. return pass;
  1622. }
  1623. void LineEdit::set_secret_character(const String &p_string) {
  1624. // An empty string as the secret character would crash the engine.
  1625. // It also wouldn't make sense to use multiple characters as the secret character.
  1626. ERR_FAIL_COND_MSG(p_string.length() != 1, "Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given).");
  1627. if (secret_character == p_string) {
  1628. return;
  1629. }
  1630. secret_character = p_string;
  1631. _shape();
  1632. queue_redraw();
  1633. }
  1634. String LineEdit::get_secret_character() const {
  1635. return secret_character;
  1636. }
  1637. void LineEdit::select(int p_from, int p_to) {
  1638. if (!selecting_enabled) {
  1639. return;
  1640. }
  1641. if (p_from == 0 && p_to == 0) {
  1642. deselect();
  1643. return;
  1644. }
  1645. int len = text.length();
  1646. if (p_from < 0) {
  1647. p_from = 0;
  1648. }
  1649. if (p_from > len) {
  1650. p_from = len;
  1651. }
  1652. if (p_to < 0 || p_to > len) {
  1653. p_to = len;
  1654. }
  1655. if (p_from >= p_to) {
  1656. return;
  1657. }
  1658. selection.enabled = true;
  1659. selection.begin = p_from;
  1660. selection.end = p_to;
  1661. selection.creating = false;
  1662. selection.double_click = false;
  1663. queue_redraw();
  1664. }
  1665. bool LineEdit::is_text_field() const {
  1666. return true;
  1667. }
  1668. void LineEdit::menu_option(int p_option) {
  1669. switch (p_option) {
  1670. case MENU_CUT: {
  1671. if (editable) {
  1672. cut_text();
  1673. }
  1674. } break;
  1675. case MENU_COPY: {
  1676. copy_text();
  1677. } break;
  1678. case MENU_PASTE: {
  1679. if (editable) {
  1680. paste_text();
  1681. }
  1682. } break;
  1683. case MENU_CLEAR: {
  1684. if (editable) {
  1685. clear();
  1686. }
  1687. } break;
  1688. case MENU_SELECT_ALL: {
  1689. select_all();
  1690. } break;
  1691. case MENU_UNDO: {
  1692. if (editable) {
  1693. undo();
  1694. }
  1695. } break;
  1696. case MENU_REDO: {
  1697. if (editable) {
  1698. redo();
  1699. }
  1700. } break;
  1701. case MENU_DIR_INHERITED: {
  1702. set_text_direction(TEXT_DIRECTION_INHERITED);
  1703. } break;
  1704. case MENU_DIR_AUTO: {
  1705. set_text_direction(TEXT_DIRECTION_AUTO);
  1706. } break;
  1707. case MENU_DIR_LTR: {
  1708. set_text_direction(TEXT_DIRECTION_LTR);
  1709. } break;
  1710. case MENU_DIR_RTL: {
  1711. set_text_direction(TEXT_DIRECTION_RTL);
  1712. } break;
  1713. case MENU_DISPLAY_UCC: {
  1714. set_draw_control_chars(!get_draw_control_chars());
  1715. } break;
  1716. case MENU_INSERT_LRM: {
  1717. if (editable) {
  1718. insert_text_at_caret(String::chr(0x200E));
  1719. }
  1720. } break;
  1721. case MENU_INSERT_RLM: {
  1722. if (editable) {
  1723. insert_text_at_caret(String::chr(0x200F));
  1724. }
  1725. } break;
  1726. case MENU_INSERT_LRE: {
  1727. if (editable) {
  1728. insert_text_at_caret(String::chr(0x202A));
  1729. }
  1730. } break;
  1731. case MENU_INSERT_RLE: {
  1732. if (editable) {
  1733. insert_text_at_caret(String::chr(0x202B));
  1734. }
  1735. } break;
  1736. case MENU_INSERT_LRO: {
  1737. if (editable) {
  1738. insert_text_at_caret(String::chr(0x202D));
  1739. }
  1740. } break;
  1741. case MENU_INSERT_RLO: {
  1742. if (editable) {
  1743. insert_text_at_caret(String::chr(0x202E));
  1744. }
  1745. } break;
  1746. case MENU_INSERT_PDF: {
  1747. if (editable) {
  1748. insert_text_at_caret(String::chr(0x202C));
  1749. }
  1750. } break;
  1751. case MENU_INSERT_ALM: {
  1752. if (editable) {
  1753. insert_text_at_caret(String::chr(0x061C));
  1754. }
  1755. } break;
  1756. case MENU_INSERT_LRI: {
  1757. if (editable) {
  1758. insert_text_at_caret(String::chr(0x2066));
  1759. }
  1760. } break;
  1761. case MENU_INSERT_RLI: {
  1762. if (editable) {
  1763. insert_text_at_caret(String::chr(0x2067));
  1764. }
  1765. } break;
  1766. case MENU_INSERT_FSI: {
  1767. if (editable) {
  1768. insert_text_at_caret(String::chr(0x2068));
  1769. }
  1770. } break;
  1771. case MENU_INSERT_PDI: {
  1772. if (editable) {
  1773. insert_text_at_caret(String::chr(0x2069));
  1774. }
  1775. } break;
  1776. case MENU_INSERT_ZWJ: {
  1777. if (editable) {
  1778. insert_text_at_caret(String::chr(0x200D));
  1779. }
  1780. } break;
  1781. case MENU_INSERT_ZWNJ: {
  1782. if (editable) {
  1783. insert_text_at_caret(String::chr(0x200C));
  1784. }
  1785. } break;
  1786. case MENU_INSERT_WJ: {
  1787. if (editable) {
  1788. insert_text_at_caret(String::chr(0x2060));
  1789. }
  1790. } break;
  1791. case MENU_INSERT_SHY: {
  1792. if (editable) {
  1793. insert_text_at_caret(String::chr(0x00AD));
  1794. }
  1795. }
  1796. }
  1797. }
  1798. void LineEdit::set_context_menu_enabled(bool p_enable) {
  1799. context_menu_enabled = p_enable;
  1800. }
  1801. bool LineEdit::is_context_menu_enabled() {
  1802. return context_menu_enabled;
  1803. }
  1804. bool LineEdit::is_menu_visible() const {
  1805. return menu && menu->is_visible();
  1806. }
  1807. PopupMenu *LineEdit::get_menu() const {
  1808. if (!menu) {
  1809. const_cast<LineEdit *>(this)->_generate_context_menu();
  1810. }
  1811. return menu;
  1812. }
  1813. void LineEdit::_editor_settings_changed() {
  1814. #ifdef TOOLS_ENABLED
  1815. set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  1816. set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  1817. #endif
  1818. }
  1819. void LineEdit::set_expand_to_text_length_enabled(bool p_enabled) {
  1820. expand_to_text_length = p_enabled;
  1821. update_minimum_size();
  1822. set_caret_column(caret_column);
  1823. }
  1824. bool LineEdit::is_expand_to_text_length_enabled() const {
  1825. return expand_to_text_length;
  1826. }
  1827. void LineEdit::set_clear_button_enabled(bool p_enabled) {
  1828. if (clear_button_enabled == p_enabled) {
  1829. return;
  1830. }
  1831. clear_button_enabled = p_enabled;
  1832. _fit_to_width();
  1833. update_minimum_size();
  1834. queue_redraw();
  1835. }
  1836. bool LineEdit::is_clear_button_enabled() const {
  1837. return clear_button_enabled;
  1838. }
  1839. void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
  1840. shortcut_keys_enabled = p_enabled;
  1841. }
  1842. bool LineEdit::is_shortcut_keys_enabled() const {
  1843. return shortcut_keys_enabled;
  1844. }
  1845. void LineEdit::set_virtual_keyboard_enabled(bool p_enable) {
  1846. virtual_keyboard_enabled = p_enable;
  1847. }
  1848. bool LineEdit::is_virtual_keyboard_enabled() const {
  1849. return virtual_keyboard_enabled;
  1850. }
  1851. void LineEdit::set_virtual_keyboard_type(VirtualKeyboardType p_type) {
  1852. virtual_keyboard_type = p_type;
  1853. }
  1854. LineEdit::VirtualKeyboardType LineEdit::get_virtual_keyboard_type() const {
  1855. return virtual_keyboard_type;
  1856. }
  1857. void LineEdit::set_middle_mouse_paste_enabled(bool p_enabled) {
  1858. middle_mouse_paste_enabled = p_enabled;
  1859. }
  1860. bool LineEdit::is_middle_mouse_paste_enabled() const {
  1861. return middle_mouse_paste_enabled;
  1862. }
  1863. void LineEdit::set_selecting_enabled(bool p_enabled) {
  1864. if (selecting_enabled == p_enabled) {
  1865. return;
  1866. }
  1867. selecting_enabled = p_enabled;
  1868. if (!selecting_enabled) {
  1869. deselect();
  1870. }
  1871. }
  1872. bool LineEdit::is_selecting_enabled() const {
  1873. return selecting_enabled;
  1874. }
  1875. void LineEdit::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
  1876. if (deselect_on_focus_loss_enabled == p_enabled) {
  1877. return;
  1878. }
  1879. deselect_on_focus_loss_enabled = p_enabled;
  1880. if (p_enabled && selection.enabled && !has_focus()) {
  1881. deselect();
  1882. }
  1883. }
  1884. bool LineEdit::is_deselect_on_focus_loss_enabled() const {
  1885. return deselect_on_focus_loss_enabled;
  1886. }
  1887. void LineEdit::set_right_icon(const Ref<Texture2D> &p_icon) {
  1888. if (right_icon == p_icon) {
  1889. return;
  1890. }
  1891. right_icon = p_icon;
  1892. _fit_to_width();
  1893. update_minimum_size();
  1894. queue_redraw();
  1895. }
  1896. Ref<Texture2D> LineEdit::get_right_icon() {
  1897. return right_icon;
  1898. }
  1899. void LineEdit::set_flat(bool p_enabled) {
  1900. if (flat != p_enabled) {
  1901. flat = p_enabled;
  1902. queue_redraw();
  1903. }
  1904. }
  1905. bool LineEdit::is_flat() const {
  1906. return flat;
  1907. }
  1908. void LineEdit::set_select_all_on_focus(bool p_enabled) {
  1909. select_all_on_focus = p_enabled;
  1910. }
  1911. bool LineEdit::is_select_all_on_focus() const {
  1912. return select_all_on_focus;
  1913. }
  1914. void LineEdit::clear_pending_select_all_on_focus() {
  1915. pending_select_all_on_focus = false;
  1916. }
  1917. void LineEdit::_text_changed() {
  1918. _emit_text_change();
  1919. _clear_redo();
  1920. }
  1921. void LineEdit::_emit_text_change() {
  1922. emit_signal(SNAME("text_changed"), text);
  1923. text_changed_dirty = false;
  1924. }
  1925. void LineEdit::_shape() {
  1926. const Ref<Font> &font = theme_cache.font;
  1927. int font_size = theme_cache.font_size;
  1928. if (font.is_null()) {
  1929. return;
  1930. }
  1931. Size2 old_size = TS->shaped_text_get_size(text_rid);
  1932. TS->shaped_text_clear(text_rid);
  1933. String t;
  1934. if (text.length() == 0 && ime_text.length() == 0) {
  1935. t = placeholder_translated;
  1936. } else if (pass) {
  1937. t = secret_character.repeat(text.length() + ime_text.length());
  1938. } else {
  1939. if (ime_text.length() > 0) {
  1940. t = text.substr(0, caret_column) + ime_text + text.substr(caret_column, text.length());
  1941. } else {
  1942. t = text;
  1943. }
  1944. }
  1945. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  1946. TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  1947. } else {
  1948. TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
  1949. }
  1950. TS->shaped_text_set_preserve_control(text_rid, draw_control_chars);
  1951. TS->shaped_text_add_string(text_rid, t, font->get_rids(), font_size, font->get_opentype_features(), language);
  1952. for (int i = 0; i < TextServer::SPACING_MAX; i++) {
  1953. TS->shaped_text_set_spacing(text_rid, TextServer::SpacingType(i), font->get_spacing(TextServer::SpacingType(i)));
  1954. }
  1955. TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, t));
  1956. full_width = TS->shaped_text_get_size(text_rid).x;
  1957. _fit_to_width();
  1958. Size2 size = TS->shaped_text_get_size(text_rid);
  1959. if ((expand_to_text_length && old_size.x != size.x) || (old_size.y != size.y)) {
  1960. update_minimum_size();
  1961. }
  1962. }
  1963. void LineEdit::_fit_to_width() {
  1964. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  1965. Ref<StyleBox> style = theme_cache.normal;
  1966. int t_width = get_size().width - style->get_margin(SIDE_RIGHT) - style->get_margin(SIDE_LEFT);
  1967. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1968. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1969. if (right_icon.is_valid() || display_clear_icon) {
  1970. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1971. t_width -= r_icon->get_width();
  1972. }
  1973. TS->shaped_text_fit_to_width(text_rid, MAX(t_width, full_width));
  1974. }
  1975. }
  1976. void LineEdit::_clear_redo() {
  1977. _create_undo_state();
  1978. if (undo_stack_pos == nullptr) {
  1979. return;
  1980. }
  1981. undo_stack_pos = undo_stack_pos->next();
  1982. while (undo_stack_pos) {
  1983. List<TextOperation>::Element *elem = undo_stack_pos;
  1984. undo_stack_pos = undo_stack_pos->next();
  1985. undo_stack.erase(elem);
  1986. }
  1987. _create_undo_state();
  1988. }
  1989. void LineEdit::_clear_undo_stack() {
  1990. undo_stack.clear();
  1991. undo_stack_pos = nullptr;
  1992. _create_undo_state();
  1993. }
  1994. void LineEdit::_create_undo_state() {
  1995. TextOperation op;
  1996. op.text = text;
  1997. op.caret_column = caret_column;
  1998. op.scroll_offset = scroll_offset;
  1999. undo_stack.push_back(op);
  2000. }
  2001. Key LineEdit::_get_menu_action_accelerator(const String &p_action) {
  2002. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  2003. if (!events) {
  2004. return Key::NONE;
  2005. }
  2006. // Use first event in the list for the accelerator.
  2007. const List<Ref<InputEvent>>::Element *first_event = events->front();
  2008. if (!first_event) {
  2009. return Key::NONE;
  2010. }
  2011. const Ref<InputEventKey> event = first_event->get();
  2012. if (event.is_null()) {
  2013. return Key::NONE;
  2014. }
  2015. // Use physical keycode if non-zero
  2016. if (event->get_physical_keycode() != Key::NONE) {
  2017. return event->get_physical_keycode_with_modifiers();
  2018. } else {
  2019. return event->get_keycode_with_modifiers();
  2020. }
  2021. }
  2022. void LineEdit::_generate_context_menu() {
  2023. menu = memnew(PopupMenu);
  2024. add_child(menu, false, INTERNAL_MODE_FRONT);
  2025. menu_dir = memnew(PopupMenu);
  2026. menu_dir->set_name("DirMenu");
  2027. menu_dir->add_radio_check_item(RTR("Same as Layout Direction"), MENU_DIR_INHERITED);
  2028. menu_dir->add_radio_check_item(RTR("Auto-Detect Direction"), MENU_DIR_AUTO);
  2029. menu_dir->add_radio_check_item(RTR("Left-to-Right"), MENU_DIR_LTR);
  2030. menu_dir->add_radio_check_item(RTR("Right-to-Left"), MENU_DIR_RTL);
  2031. menu->add_child(menu_dir, false, INTERNAL_MODE_FRONT);
  2032. menu_ctl = memnew(PopupMenu);
  2033. menu_ctl->set_name("CTLMenu");
  2034. menu_ctl->add_item(RTR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM);
  2035. menu_ctl->add_item(RTR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM);
  2036. menu_ctl->add_item(RTR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE);
  2037. menu_ctl->add_item(RTR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE);
  2038. menu_ctl->add_item(RTR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO);
  2039. menu_ctl->add_item(RTR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO);
  2040. menu_ctl->add_item(RTR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF);
  2041. menu_ctl->add_separator();
  2042. menu_ctl->add_item(RTR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM);
  2043. menu_ctl->add_item(RTR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI);
  2044. menu_ctl->add_item(RTR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI);
  2045. menu_ctl->add_item(RTR("First Strong Isolate (FSI)"), MENU_INSERT_FSI);
  2046. menu_ctl->add_item(RTR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI);
  2047. menu_ctl->add_separator();
  2048. menu_ctl->add_item(RTR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ);
  2049. menu_ctl->add_item(RTR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  2050. menu_ctl->add_item(RTR("Word Joiner (WJ)"), MENU_INSERT_WJ);
  2051. menu_ctl->add_item(RTR("Soft Hyphen (SHY)"), MENU_INSERT_SHY);
  2052. menu->add_child(menu_ctl, false, INTERNAL_MODE_FRONT);
  2053. menu->add_item(RTR("Cut"), MENU_CUT);
  2054. menu->add_item(RTR("Copy"), MENU_COPY);
  2055. menu->add_item(RTR("Paste"), MENU_PASTE);
  2056. menu->add_separator();
  2057. menu->add_item(RTR("Select All"), MENU_SELECT_ALL);
  2058. menu->add_item(RTR("Clear"), MENU_CLEAR);
  2059. menu->add_separator();
  2060. menu->add_item(RTR("Undo"), MENU_UNDO);
  2061. menu->add_item(RTR("Redo"), MENU_REDO);
  2062. menu->add_separator();
  2063. menu->add_submenu_item(RTR("Text Writing Direction"), "DirMenu", MENU_SUBMENU_TEXT_DIR);
  2064. menu->add_separator();
  2065. menu->add_check_item(RTR("Display Control Characters"), MENU_DISPLAY_UCC);
  2066. menu->add_submenu_item(RTR("Insert Control Character"), "CTLMenu", MENU_SUBMENU_INSERT_UCC);
  2067. menu->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2068. menu_dir->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2069. menu_ctl->connect("id_pressed", callable_mp(this, &LineEdit::menu_option));
  2070. menu->connect(SNAME("focus_entered"), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2071. menu->connect(SNAME("focus_exited"), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2072. }
  2073. void LineEdit::_update_context_menu() {
  2074. if (!menu) {
  2075. _generate_context_menu();
  2076. }
  2077. int idx = -1;
  2078. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  2079. idx = m_menu->get_item_index(m_id); \
  2080. if (idx >= 0) { \
  2081. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2082. m_menu->set_item_disabled(idx, m_disabled); \
  2083. }
  2084. #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \
  2085. idx = m_menu->get_item_index(m_id); \
  2086. if (idx >= 0) { \
  2087. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2088. }
  2089. #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \
  2090. idx = m_menu->get_item_index(m_id); \
  2091. if (idx >= 0) { \
  2092. m_menu->set_item_disabled(idx, m_disabled); \
  2093. }
  2094. #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \
  2095. idx = m_menu->get_item_index(m_id); \
  2096. if (idx >= 0) { \
  2097. m_menu->set_item_checked(idx, m_checked); \
  2098. }
  2099. MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable)
  2100. MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy")
  2101. MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable)
  2102. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled)
  2103. MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable)
  2104. MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo())
  2105. MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo())
  2106. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED)
  2107. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO)
  2108. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR)
  2109. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL)
  2110. MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars)
  2111. MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable)
  2112. #undef MENU_ITEM_ACTION_DISABLED
  2113. #undef MENU_ITEM_ACTION
  2114. #undef MENU_ITEM_DISABLED
  2115. #undef MENU_ITEM_CHECKED
  2116. }
  2117. void LineEdit::_validate_property(PropertyInfo &p_property) const {
  2118. if (!caret_blink_enabled && p_property.name == "caret_blink_interval") {
  2119. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2120. }
  2121. }
  2122. void LineEdit::_bind_methods() {
  2123. ClassDB::bind_method(D_METHOD("_text_changed"), &LineEdit::_text_changed);
  2124. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment);
  2125. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment);
  2126. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  2127. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  2128. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  2129. ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect);
  2130. ClassDB::bind_method(D_METHOD("has_selection"), &LineEdit::has_selection);
  2131. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &LineEdit::get_selection_from_column);
  2132. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &LineEdit::get_selection_to_column);
  2133. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  2134. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  2135. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &LineEdit::get_draw_control_chars);
  2136. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enable"), &LineEdit::set_draw_control_chars);
  2137. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LineEdit::set_text_direction);
  2138. ClassDB::bind_method(D_METHOD("get_text_direction"), &LineEdit::get_text_direction);
  2139. ClassDB::bind_method(D_METHOD("set_language", "language"), &LineEdit::set_language);
  2140. ClassDB::bind_method(D_METHOD("get_language"), &LineEdit::get_language);
  2141. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LineEdit::set_structured_text_bidi_override);
  2142. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LineEdit::get_structured_text_bidi_override);
  2143. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LineEdit::set_structured_text_bidi_override_options);
  2144. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LineEdit::get_structured_text_bidi_override_options);
  2145. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  2146. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  2147. ClassDB::bind_method(D_METHOD("set_caret_column", "position"), &LineEdit::set_caret_column);
  2148. ClassDB::bind_method(D_METHOD("get_caret_column"), &LineEdit::get_caret_column);
  2149. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset);
  2150. ClassDB::bind_method(D_METHOD("set_expand_to_text_length_enabled", "enabled"), &LineEdit::set_expand_to_text_length_enabled);
  2151. ClassDB::bind_method(D_METHOD("is_expand_to_text_length_enabled"), &LineEdit::is_expand_to_text_length_enabled);
  2152. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enabled"), &LineEdit::set_caret_blink_enabled);
  2153. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &LineEdit::is_caret_blink_enabled);
  2154. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &LineEdit::set_caret_mid_grapheme_enabled);
  2155. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &LineEdit::is_caret_mid_grapheme_enabled);
  2156. ClassDB::bind_method(D_METHOD("set_caret_force_displayed", "enabled"), &LineEdit::set_caret_force_displayed);
  2157. ClassDB::bind_method(D_METHOD("is_caret_force_displayed"), &LineEdit::is_caret_force_displayed);
  2158. ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &LineEdit::set_caret_blink_interval);
  2159. ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &LineEdit::get_caret_blink_interval);
  2160. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  2161. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  2162. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text"), &LineEdit::insert_text_at_caret);
  2163. ClassDB::bind_method(D_METHOD("delete_char_at_caret"), &LineEdit::delete_char);
  2164. ClassDB::bind_method(D_METHOD("delete_text", "from_column", "to_column"), &LineEdit::delete_text);
  2165. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  2166. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  2167. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  2168. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  2169. ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
  2170. ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
  2171. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  2172. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  2173. ClassDB::bind_method(D_METHOD("is_menu_visible"), &LineEdit::is_menu_visible);
  2174. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
  2175. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled);
  2176. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &LineEdit::set_virtual_keyboard_enabled);
  2177. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &LineEdit::is_virtual_keyboard_enabled);
  2178. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_type", "type"), &LineEdit::set_virtual_keyboard_type);
  2179. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_type"), &LineEdit::get_virtual_keyboard_type);
  2180. ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled);
  2181. ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled);
  2182. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled);
  2183. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled);
  2184. ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enable"), &LineEdit::set_middle_mouse_paste_enabled);
  2185. ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &LineEdit::is_middle_mouse_paste_enabled);
  2186. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled);
  2187. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled);
  2188. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &LineEdit::set_deselect_on_focus_loss_enabled);
  2189. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &LineEdit::is_deselect_on_focus_loss_enabled);
  2190. ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
  2191. ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
  2192. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &LineEdit::set_flat);
  2193. ClassDB::bind_method(D_METHOD("is_flat"), &LineEdit::is_flat);
  2194. ClassDB::bind_method(D_METHOD("set_select_all_on_focus", "enabled"), &LineEdit::set_select_all_on_focus);
  2195. ClassDB::bind_method(D_METHOD("is_select_all_on_focus"), &LineEdit::is_select_all_on_focus);
  2196. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text")));
  2197. ADD_SIGNAL(MethodInfo("text_change_rejected", PropertyInfo(Variant::STRING, "rejected_substring")));
  2198. ADD_SIGNAL(MethodInfo("text_submitted", PropertyInfo(Variant::STRING, "new_text")));
  2199. BIND_ENUM_CONSTANT(MENU_CUT);
  2200. BIND_ENUM_CONSTANT(MENU_COPY);
  2201. BIND_ENUM_CONSTANT(MENU_PASTE);
  2202. BIND_ENUM_CONSTANT(MENU_CLEAR);
  2203. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  2204. BIND_ENUM_CONSTANT(MENU_UNDO);
  2205. BIND_ENUM_CONSTANT(MENU_REDO);
  2206. BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR);
  2207. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  2208. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  2209. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  2210. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  2211. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  2212. BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC);
  2213. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  2214. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  2215. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  2216. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  2217. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  2218. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  2219. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  2220. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  2221. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  2222. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  2223. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  2224. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  2225. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  2226. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  2227. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  2228. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  2229. BIND_ENUM_CONSTANT(MENU_MAX);
  2230. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_DEFAULT);
  2231. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_MULTILINE);
  2232. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER);
  2233. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER_DECIMAL);
  2234. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PHONE);
  2235. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_EMAIL_ADDRESS);
  2236. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PASSWORD);
  2237. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_URL);
  2238. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  2239. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  2240. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  2241. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_max_length", "get_max_length");
  2242. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  2243. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret");
  2244. ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
  2245. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length_enabled", "is_expand_to_text_length_enabled");
  2246. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  2247. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  2248. 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");
  2249. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled");
  2250. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  2251. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled");
  2252. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  2253. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  2254. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_right_icon", "get_right_icon");
  2255. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  2256. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  2257. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");
  2258. ADD_GROUP("Caret", "caret_");
  2259. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  2260. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_interval", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "set_caret_blink_interval", "get_caret_blink_interval");
  2261. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_column", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_caret_column", "get_caret_column");
  2262. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_force_displayed"), "set_caret_force_displayed", "is_caret_force_displayed");
  2263. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  2264. ADD_GROUP("BiDi", "");
  2265. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  2266. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  2267. 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");
  2268. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  2269. }
  2270. LineEdit::LineEdit(const String &p_placeholder) {
  2271. text_rid = TS->create_shaped_text();
  2272. _create_undo_state();
  2273. deselect();
  2274. set_focus_mode(FOCUS_ALL);
  2275. set_default_cursor_shape(CURSOR_IBEAM);
  2276. set_mouse_filter(MOUSE_FILTER_STOP);
  2277. set_process_unhandled_key_input(true);
  2278. set_caret_blink_enabled(false);
  2279. set_placeholder(p_placeholder);
  2280. set_editable(true); // Initialize to opposite first, so we get past the early-out in set_editable.
  2281. }
  2282. LineEdit::~LineEdit() {
  2283. TS->free_rid(text_rid);
  2284. }