line_edit.cpp 88 KB

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