line_edit.cpp 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573
  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 "line_edit.compat.inc"
  32. #include "core/config/project_settings.h"
  33. #include "core/input/input_map.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "core/string/alt_codes.h"
  37. #include "core/string/translation_server.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/main/window.h"
  40. #include "scene/theme/theme_db.h"
  41. #include "servers/display/display_server.h"
  42. #include "servers/text/text_server.h"
  43. #ifdef TOOLS_ENABLED
  44. #include "editor/settings/editor_settings.h"
  45. #endif
  46. void LineEdit::edit(bool p_hide_focus) {
  47. _edit(true, p_hide_focus);
  48. }
  49. void LineEdit::_edit(bool p_show_virtual_keyboard, bool p_hide_focus) {
  50. if (!is_inside_tree()) {
  51. return;
  52. }
  53. if (!has_focus()) {
  54. grab_focus(p_hide_focus);
  55. return;
  56. }
  57. if (!editable || editing) {
  58. return;
  59. }
  60. if (select_all_on_focus) {
  61. if (Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT)) {
  62. // Select all when the mouse button is up.
  63. pending_select_all_on_focus = true;
  64. } else {
  65. select_all();
  66. }
  67. }
  68. editing = true;
  69. _validate_caret_can_draw();
  70. if (p_show_virtual_keyboard && !pending_select_all_on_focus) {
  71. show_virtual_keyboard();
  72. }
  73. queue_redraw();
  74. }
  75. void LineEdit::unedit() {
  76. if (!editing) {
  77. return;
  78. }
  79. editing = false;
  80. _validate_caret_can_draw();
  81. apply_ime();
  82. set_caret_column(caret_column); // Update scroll_offset.
  83. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  84. DisplayServer::get_singleton()->virtual_keyboard_hide();
  85. }
  86. if (deselect_on_focus_loss_enabled && !selection.drag_attempt) {
  87. deselect();
  88. }
  89. }
  90. bool LineEdit::is_editing() const {
  91. return editing;
  92. }
  93. void LineEdit::set_keep_editing_on_text_submit(bool p_enabled) {
  94. keep_editing_on_text_submit = p_enabled;
  95. }
  96. bool LineEdit::is_editing_kept_on_text_submit() const {
  97. return keep_editing_on_text_submit;
  98. }
  99. void LineEdit::_close_ime_window() {
  100. DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID;
  101. if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  102. return;
  103. }
  104. DisplayServer::get_singleton()->window_set_ime_position(Point2(), wid);
  105. DisplayServer::get_singleton()->window_set_ime_active(false, wid);
  106. }
  107. void LineEdit::_update_ime_window_position() {
  108. DisplayServer::WindowID wid = get_window() ? get_window()->get_window_id() : DisplayServer::INVALID_WINDOW_ID;
  109. if (wid == DisplayServer::INVALID_WINDOW_ID || !DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_IME)) {
  110. return;
  111. }
  112. DisplayServer::get_singleton()->window_set_ime_active(true, wid);
  113. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2) + get_global_position();
  114. if (get_window()->get_embedder()) {
  115. pos += get_viewport()->get_popup_base_transform().get_origin();
  116. }
  117. // Take into account the window's transform.
  118. pos = get_window()->get_screen_transform().xform(pos);
  119. // The window will move to the updated position the next time the IME is updated, not immediately.
  120. DisplayServer::get_singleton()->window_set_ime_position(pos, wid);
  121. }
  122. bool LineEdit::has_ime_text() const {
  123. return !ime_text.is_empty();
  124. }
  125. void LineEdit::cancel_ime() {
  126. if (!has_ime_text()) {
  127. _close_ime_window();
  128. return;
  129. }
  130. ime_text = String();
  131. ime_selection = Vector2i();
  132. alt_start = false;
  133. alt_start_no_hold = false;
  134. _close_ime_window();
  135. _shape();
  136. }
  137. void LineEdit::apply_ime() {
  138. if (!has_ime_text()) {
  139. _close_ime_window();
  140. return;
  141. }
  142. // Force apply the current IME text.
  143. if (alt_start || alt_start_no_hold) {
  144. cancel_ime();
  145. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff && alt_code <= 0x10ffff)) {
  146. char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
  147. insert_text_at_caret(ucodestr);
  148. }
  149. } else {
  150. String insert_ime_text = ime_text;
  151. cancel_ime();
  152. insert_text_at_caret(insert_ime_text);
  153. }
  154. }
  155. void LineEdit::_swap_current_input_direction() {
  156. if (input_direction == TEXT_DIRECTION_LTR) {
  157. input_direction = TEXT_DIRECTION_RTL;
  158. } else {
  159. input_direction = TEXT_DIRECTION_LTR;
  160. }
  161. set_caret_column(get_caret_column());
  162. }
  163. void LineEdit::_move_caret_left(bool p_select, bool p_move_by_word) {
  164. if (selection.enabled && !p_select) {
  165. set_caret_column(selection.begin);
  166. deselect();
  167. return;
  168. }
  169. shift_selection_check_pre(p_select);
  170. if (p_move_by_word) {
  171. int cc = caret_column;
  172. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  173. if (words.is_empty() || cc <= words[0]) {
  174. // Move to the start when there are no more words.
  175. cc = 0;
  176. } else {
  177. for (int i = words.size() - 2; i >= 0; i = i - 2) {
  178. if (words[i] < cc) {
  179. cc = words[i];
  180. break;
  181. }
  182. }
  183. }
  184. set_caret_column(cc);
  185. } else {
  186. if (caret_mid_grapheme_enabled) {
  187. set_caret_column(get_caret_column() - 1);
  188. } else {
  189. set_caret_column(TS->shaped_text_prev_character_pos(text_rid, get_caret_column()));
  190. }
  191. }
  192. shift_selection_check_post(p_select);
  193. _reset_caret_blink_timer();
  194. }
  195. void LineEdit::_move_caret_right(bool p_select, bool p_move_by_word) {
  196. if (selection.enabled && !p_select) {
  197. set_caret_column(selection.end);
  198. deselect();
  199. return;
  200. }
  201. shift_selection_check_pre(p_select);
  202. if (p_move_by_word) {
  203. int cc = caret_column;
  204. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  205. if (words.is_empty() || cc >= words[words.size() - 1]) {
  206. // Move to the end when there are no more words.
  207. cc = text.length();
  208. } else {
  209. for (int i = 1; i < words.size(); i = i + 2) {
  210. if (words[i] > cc) {
  211. cc = words[i];
  212. break;
  213. }
  214. }
  215. }
  216. set_caret_column(cc);
  217. } else {
  218. if (caret_mid_grapheme_enabled) {
  219. set_caret_column(get_caret_column() + 1);
  220. } else {
  221. set_caret_column(TS->shaped_text_next_character_pos(text_rid, get_caret_column()));
  222. }
  223. }
  224. shift_selection_check_post(p_select);
  225. _reset_caret_blink_timer();
  226. }
  227. void LineEdit::_move_caret_start(bool p_select) {
  228. shift_selection_check_pre(p_select);
  229. set_caret_column(0);
  230. shift_selection_check_post(p_select);
  231. }
  232. void LineEdit::_move_caret_end(bool p_select) {
  233. shift_selection_check_pre(p_select);
  234. set_caret_column(text.length());
  235. shift_selection_check_post(p_select);
  236. }
  237. void LineEdit::_backspace(bool p_word, bool p_all_to_left) {
  238. if (!editable) {
  239. return;
  240. }
  241. if (selection.enabled) {
  242. selection_delete();
  243. return;
  244. }
  245. if (caret_column == 0) {
  246. return; // Nothing to do.
  247. }
  248. if (p_all_to_left) {
  249. text = text.substr(caret_column);
  250. _shape();
  251. set_caret_column(0);
  252. _text_changed();
  253. return;
  254. }
  255. if (p_word) {
  256. int cc = caret_column;
  257. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  258. if (words.is_empty() || cc <= words[0]) {
  259. // Delete to the start when there are no more words.
  260. cc = 0;
  261. } else {
  262. for (int i = words.size() - 2; i >= 0; i = i - 2) {
  263. if (words[i] < cc) {
  264. cc = words[i];
  265. break;
  266. }
  267. }
  268. }
  269. delete_text(cc, caret_column);
  270. set_caret_column(cc);
  271. } else {
  272. delete_char();
  273. }
  274. }
  275. void LineEdit::_delete(bool p_word, bool p_all_to_right) {
  276. if (!editable) {
  277. return;
  278. }
  279. if (selection.enabled) {
  280. selection_delete();
  281. return;
  282. }
  283. if (caret_column == text.length()) {
  284. return; // Nothing to do.
  285. }
  286. if (p_all_to_right) {
  287. text = text.substr(0, caret_column);
  288. _shape();
  289. _text_changed();
  290. return;
  291. }
  292. if (p_word) {
  293. int cc = caret_column;
  294. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  295. if (words.is_empty() || cc >= words[words.size() - 1]) {
  296. // Delete to the end when there are no more words.
  297. cc = text.length();
  298. } else {
  299. for (int i = 1; i < words.size(); i = i + 2) {
  300. if (words[i] > cc) {
  301. cc = words[i];
  302. break;
  303. }
  304. }
  305. }
  306. delete_text(caret_column, cc);
  307. set_caret_column(caret_column);
  308. } else {
  309. if (caret_mid_grapheme_enabled) {
  310. set_caret_column(caret_column + 1);
  311. delete_char();
  312. } else {
  313. int cc = caret_column;
  314. set_caret_column(TS->shaped_text_next_character_pos(text_rid, caret_column));
  315. delete_text(cc, caret_column);
  316. }
  317. }
  318. }
  319. Point2 LineEdit::_get_right_icon_size(Ref<Texture2D> p_right_icon) const {
  320. Size2 icon_size;
  321. if (p_right_icon.is_null()) {
  322. return icon_size;
  323. }
  324. switch (icon_expand_mode) {
  325. default:
  326. case LineEdit::EXPAND_MODE_ORIGINAL_SIZE:
  327. icon_size = p_right_icon->get_size();
  328. break;
  329. case LineEdit::EXPAND_MODE_FIT_TO_TEXT: {
  330. real_t height = theme_cache.font->get_height(theme_cache.font_size);
  331. icon_size = Size2(height, height);
  332. } break;
  333. case LineEdit::EXPAND_MODE_FIT_TO_LINE_EDIT: {
  334. icon_size = p_right_icon->get_size();
  335. Point2 size = get_size();
  336. float icon_width = icon_size.width * size.height / icon_size.height;
  337. float icon_height = size.height;
  338. if (icon_width > size.width) {
  339. icon_width = size.width;
  340. icon_height = icon_size.height * icon_width / icon_size.width;
  341. }
  342. icon_size = Size2(icon_width, icon_height) * right_icon_scale;
  343. } break;
  344. }
  345. return icon_size;
  346. }
  347. void LineEdit::unhandled_key_input(const Ref<InputEvent> &p_event) {
  348. // Return to prevent editing if just focused.
  349. if (!editing) {
  350. return;
  351. }
  352. Ref<InputEventKey> k = p_event;
  353. if (k.is_valid()) {
  354. if (!k->is_pressed()) {
  355. return;
  356. }
  357. // Handle Unicode (with modifiers active, process after shortcuts).
  358. if (has_focus() && editable && (k->get_unicode() >= 32)) {
  359. selection_delete();
  360. char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 };
  361. int prev_len = text.length();
  362. insert_text_at_caret(ucodestr);
  363. if (text.length() != prev_len) {
  364. _text_changed();
  365. }
  366. accept_event();
  367. }
  368. }
  369. }
  370. void LineEdit::gui_input(const Ref<InputEvent> &p_event) {
  371. ERR_FAIL_COND(p_event.is_null());
  372. Ref<InputEventMouseButton> b = p_event;
  373. if (b.is_valid()) {
  374. if (b->is_pressed() && b->get_button_index() == MouseButton::RIGHT) {
  375. apply_ime();
  376. if (editable && !selection.enabled) {
  377. set_caret_at_pixel_pos(b->get_position().x);
  378. }
  379. if (context_menu_enabled) {
  380. _update_context_menu();
  381. menu->set_position(get_screen_transform().xform(get_local_mouse_position()));
  382. menu->reset_size();
  383. menu->popup();
  384. }
  385. if (editable && !editing) {
  386. edit(true);
  387. emit_signal(SNAME("editing_toggled"), true);
  388. }
  389. accept_event();
  390. return;
  391. }
  392. if (editable && is_middle_mouse_paste_enabled() && b->is_pressed() && b->get_button_index() == MouseButton::MIDDLE && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  393. apply_ime();
  394. String paste_buffer = DisplayServer::get_singleton()->clipboard_get_primary().strip_escapes();
  395. deselect();
  396. set_caret_at_pixel_pos(b->get_position().x);
  397. if (!editing) {
  398. edit(true);
  399. emit_signal(SNAME("editing_toggled"), true);
  400. }
  401. if (!paste_buffer.is_empty()) {
  402. insert_text_at_caret(paste_buffer);
  403. if (!text_changed_dirty) {
  404. if (is_inside_tree()) {
  405. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  406. }
  407. text_changed_dirty = true;
  408. }
  409. }
  410. accept_event();
  411. return;
  412. }
  413. if (b->get_button_index() != MouseButton::LEFT) {
  414. return;
  415. }
  416. if (editing) {
  417. _reset_caret_blink_timer();
  418. }
  419. if (b->is_pressed()) {
  420. apply_ime();
  421. accept_event(); // Don't pass event further when clicked on text field.
  422. if (editable && !text.is_empty() && _is_over_clear_button(b->get_position())) {
  423. clear_button_status.press_attempt = true;
  424. clear_button_status.pressing_inside = true;
  425. queue_redraw();
  426. return;
  427. }
  428. if (b->is_shift_pressed()) {
  429. shift_selection_check_pre(true);
  430. }
  431. set_caret_at_pixel_pos(b->get_position().x);
  432. if (b->is_shift_pressed()) {
  433. selection_fill_at_caret();
  434. selection.creating = true;
  435. } else {
  436. if (selecting_enabled && !text.is_empty()) {
  437. const int triple_click_timeout = 600;
  438. const int triple_click_tolerance = 5;
  439. 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;
  440. if (is_triple_click) {
  441. // Triple-click select all.
  442. selection.enabled = true;
  443. selection.begin = 0;
  444. selection.end = text.length();
  445. selection.double_click = true;
  446. last_dblclk = 0;
  447. set_caret_column(selection.begin);
  448. if (!pass && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  449. DisplayServer::get_singleton()->clipboard_set_primary(text);
  450. }
  451. queue_accessibility_update();
  452. } else if (b->is_double_click()) {
  453. // Double-click select word.
  454. last_dblclk = OS::get_singleton()->get_ticks_msec();
  455. last_dblclk_pos = b->get_position();
  456. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  457. for (int i = 0; i < words.size(); i = i + 2) {
  458. if (words[i] <= caret_column && words[i + 1] >= caret_column) {
  459. selection.enabled = true;
  460. selection.begin = words[i];
  461. selection.end = words[i + 1];
  462. selection.double_click = true;
  463. selection.creating = true;
  464. selection.start_column = caret_column;
  465. set_caret_column(selection.end);
  466. queue_accessibility_update();
  467. break;
  468. }
  469. }
  470. if (!pass && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  471. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  472. }
  473. }
  474. }
  475. selection.drag_attempt = false;
  476. if (!selection.double_click) {
  477. bool is_inside_sel = selection.enabled && caret_column >= selection.begin && caret_column <= selection.end;
  478. if (!pass && drag_and_drop_selection_enabled && is_inside_sel) {
  479. selection.drag_attempt = true;
  480. } else {
  481. deselect();
  482. selection.start_column = caret_column;
  483. selection.creating = true;
  484. }
  485. }
  486. }
  487. if (editable && !editing) {
  488. edit(true);
  489. emit_signal(SNAME("editing_toggled"), true);
  490. return;
  491. }
  492. } else {
  493. if (selection.enabled && !pass && b->get_button_index() == MouseButton::LEFT && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_CLIPBOARD_PRIMARY)) {
  494. DisplayServer::get_singleton()->clipboard_set_primary(get_selected_text());
  495. }
  496. if (editable && !text.is_empty() && clear_button_enabled) {
  497. bool press_attempt = clear_button_status.press_attempt;
  498. clear_button_status.press_attempt = false;
  499. if (press_attempt && clear_button_status.pressing_inside && _is_over_clear_button(b->get_position())) {
  500. clear();
  501. return;
  502. }
  503. }
  504. if ((!selection.creating) && (!selection.double_click)) {
  505. deselect();
  506. }
  507. selection.creating = false;
  508. selection.double_click = false;
  509. if (!drag_action) {
  510. selection.drag_attempt = false;
  511. }
  512. if (pending_select_all_on_focus) {
  513. select_all();
  514. pending_select_all_on_focus = false;
  515. }
  516. if (editable) {
  517. show_virtual_keyboard();
  518. }
  519. }
  520. queue_redraw();
  521. return;
  522. }
  523. Ref<InputEventMouseMotion> m = p_event;
  524. if (m.is_valid()) {
  525. if (editable && !text.is_empty() && clear_button_enabled) {
  526. bool last_press_inside = clear_button_status.pressing_inside;
  527. clear_button_status.pressing_inside = clear_button_status.press_attempt && _is_over_clear_button(m->get_position());
  528. if (last_press_inside != clear_button_status.pressing_inside) {
  529. queue_redraw();
  530. }
  531. }
  532. if (m->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
  533. if (selection.creating) {
  534. set_caret_at_pixel_pos(m->get_position().x);
  535. selection_fill_at_caret();
  536. if (selection.double_click) {
  537. // Expand selection to whole words.
  538. PackedInt32Array words = TS->shaped_text_get_word_breaks(text_rid);
  539. for (int i = 0; i < words.size(); i = i + 2) {
  540. if (words[i] <= selection.begin && words[i + 1] >= selection.begin) {
  541. selection.begin = words[i];
  542. }
  543. if (words[i] <= selection.end && words[i + 1] >= selection.end) {
  544. selection.end = words[i + 1];
  545. selection.enabled = true;
  546. break;
  547. }
  548. }
  549. if (caret_column < selection.start_column) {
  550. set_caret_column(selection.begin);
  551. } else {
  552. set_caret_column(selection.end);
  553. }
  554. }
  555. }
  556. }
  557. if (drag_action && can_drop_data(m->get_position(), get_viewport()->gui_get_drag_data())) {
  558. drag_caret_force_displayed = true;
  559. set_caret_at_pixel_pos(m->get_position().x);
  560. }
  561. return;
  562. }
  563. Ref<InputEventKey> k = p_event;
  564. if (k.is_null()) {
  565. return;
  566. }
  567. if (editable && !editing && k->is_action_pressed("ui_text_submit", false)) {
  568. edit();
  569. emit_signal(SNAME("editing_toggled"), true);
  570. accept_event();
  571. return;
  572. }
  573. // Open context menu.
  574. if (context_menu_enabled) {
  575. if (k->is_action("ui_menu", true)) {
  576. _update_context_menu();
  577. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2);
  578. menu->set_position(get_screen_transform().xform(pos));
  579. menu->reset_size();
  580. menu->popup();
  581. menu->grab_focus();
  582. accept_event();
  583. return;
  584. }
  585. }
  586. if (is_shortcut_keys_enabled()) {
  587. if (k->is_action("ui_copy", true)) {
  588. copy_text();
  589. accept_event();
  590. return;
  591. }
  592. if (k->is_action("ui_text_select_all", true)) {
  593. select();
  594. accept_event();
  595. return;
  596. }
  597. if (k->is_action("ui_cut", true)) {
  598. if (editable) {
  599. cut_text();
  600. } else {
  601. copy_text();
  602. }
  603. accept_event();
  604. return;
  605. }
  606. }
  607. if (!editing) {
  608. return;
  609. }
  610. // Start Unicode Alt input (hold).
  611. if (k->is_alt_pressed() && k->get_keycode() == Key::KP_ADD && !alt_start && !alt_start_no_hold) {
  612. if (selection.enabled) {
  613. selection_delete();
  614. }
  615. alt_start = true;
  616. alt_code = 0;
  617. alt_mode = ALT_INPUT_UNICODE;
  618. ime_text = "u";
  619. ime_selection = Vector2i(0, -1);
  620. _shape();
  621. queue_redraw();
  622. accept_event();
  623. return;
  624. }
  625. // Start Unicode input (press).
  626. if (k->is_action("ui_unicode_start", true) && !alt_start && !alt_start_no_hold) {
  627. if (selection.enabled) {
  628. selection_delete();
  629. }
  630. alt_start_no_hold = true;
  631. alt_code = 0;
  632. alt_mode = ALT_INPUT_UNICODE;
  633. ime_text = "u";
  634. ime_selection = Vector2i(0, -1);
  635. _shape();
  636. queue_redraw();
  637. accept_event();
  638. return;
  639. }
  640. // Start OEM Alt input (hold).
  641. if (k->is_alt_pressed() && k->get_keycode() >= Key::KP_1 && k->get_keycode() <= Key::KP_9 && !alt_start && !alt_start_no_hold) {
  642. if (selection.enabled) {
  643. selection_delete();
  644. }
  645. alt_start = true;
  646. alt_code = (uint32_t)(k->get_keycode() - Key::KP_0);
  647. alt_mode = ALT_INPUT_OEM;
  648. ime_text = vformat("o%s", String::num_int64(alt_code, 10));
  649. ime_selection = Vector2i(0, -1);
  650. _shape();
  651. queue_redraw();
  652. accept_event();
  653. return;
  654. }
  655. // Start Windows Alt input (hold).
  656. if (k->is_alt_pressed() && k->get_keycode() == Key::KP_0 && !alt_start && !alt_start_no_hold) {
  657. if (selection.enabled) {
  658. selection_delete();
  659. }
  660. alt_start = true;
  661. alt_mode = ALT_INPUT_WIN;
  662. alt_code = 0;
  663. ime_text = "w";
  664. ime_selection = Vector2i(0, -1);
  665. _shape();
  666. queue_redraw();
  667. accept_event();
  668. return;
  669. }
  670. // Update Unicode input.
  671. if (k->is_pressed() && ((k->is_alt_pressed() && alt_start) || alt_start_no_hold)) {
  672. if (k->get_keycode() >= Key::KEY_0 && k->get_keycode() <= Key::KEY_9) {
  673. if (alt_mode == ALT_INPUT_UNICODE) {
  674. alt_code = alt_code << 4;
  675. } else {
  676. alt_code = alt_code * 10;
  677. }
  678. alt_code += (uint32_t)(k->get_keycode() - Key::KEY_0);
  679. } else if (k->get_keycode() >= Key::KP_0 && k->get_keycode() <= Key::KP_9) {
  680. if (alt_mode == ALT_INPUT_UNICODE) {
  681. alt_code = alt_code << 4;
  682. } else {
  683. alt_code = alt_code * 10;
  684. }
  685. alt_code += (uint32_t)(k->get_keycode() - Key::KP_0);
  686. } else if (alt_mode == ALT_INPUT_UNICODE && k->get_keycode() >= Key::A && k->get_keycode() <= Key::F) {
  687. alt_code = alt_code << 4;
  688. alt_code += (uint32_t)(k->get_keycode() - Key::A) + 10;
  689. } else if ((Key)k->get_unicode() >= Key::KEY_0 && (Key)k->get_unicode() <= Key::KEY_9) {
  690. if (alt_mode == ALT_INPUT_UNICODE) {
  691. alt_code = alt_code << 4;
  692. } else {
  693. alt_code = alt_code * 10;
  694. }
  695. alt_code += (uint32_t)((Key)k->get_unicode() - Key::KEY_0);
  696. } else if (alt_mode == ALT_INPUT_UNICODE && (Key)k->get_unicode() >= Key::A && (Key)k->get_unicode() <= Key::F) {
  697. alt_code = alt_code << 4;
  698. alt_code += (uint32_t)((Key)k->get_unicode() - Key::A) + 10;
  699. } else if (k->get_physical_keycode() >= Key::KEY_0 && k->get_physical_keycode() <= Key::KEY_9) {
  700. if (alt_mode == ALT_INPUT_UNICODE) {
  701. alt_code = alt_code << 4;
  702. } else {
  703. alt_code = alt_code * 10;
  704. }
  705. alt_code += (uint32_t)(k->get_physical_keycode() - Key::KEY_0);
  706. }
  707. if (k->get_keycode() == Key::BACKSPACE) {
  708. if (alt_mode == ALT_INPUT_UNICODE) {
  709. alt_code = alt_code >> 4;
  710. } else {
  711. alt_code = alt_code / 10;
  712. }
  713. }
  714. if (alt_code > 0x10ffff) {
  715. alt_code = 0x10ffff;
  716. }
  717. if (alt_code > 0) {
  718. if (alt_mode == ALT_INPUT_UNICODE) {
  719. ime_text = vformat("u%s", String::num_int64(alt_code, 16, true));
  720. } else if (alt_mode == ALT_INPUT_OEM) {
  721. ime_text = vformat("o%s", String::num_int64(alt_code, 10));
  722. } else if (alt_mode == ALT_INPUT_WIN) {
  723. ime_text = vformat("w%s", String::num_int64(alt_code, 10));
  724. }
  725. } else {
  726. if (alt_mode == ALT_INPUT_UNICODE) {
  727. ime_text = "u";
  728. } else if (alt_mode == ALT_INPUT_OEM) {
  729. ime_text = "o";
  730. } else if (alt_mode == ALT_INPUT_WIN) {
  731. ime_text = "w";
  732. }
  733. }
  734. ime_selection = Vector2i(0, -1);
  735. _shape();
  736. queue_redraw();
  737. accept_event();
  738. return;
  739. }
  740. // Submit Unicode input.
  741. if ((!k->is_pressed() && alt_start && k->get_keycode() == Key::ALT) || (alt_start_no_hold && (k->is_action("ui_text_submit", true) || k->is_action("ui_accept", true)))) {
  742. alt_start = false;
  743. alt_start_no_hold = false;
  744. int prev_len = text.length();
  745. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  746. ime_text = String();
  747. ime_selection = Vector2i();
  748. if (alt_mode == ALT_INPUT_UNICODE) {
  749. if ((alt_code > 0x31 && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  750. char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
  751. insert_text_at_caret(ucodestr);
  752. }
  753. } else if (alt_mode == ALT_INPUT_OEM) {
  754. if (alt_code > 0x00 && alt_code <= 0xff) {
  755. char32_t ucodestr[2] = { alt_code_oem437[alt_code], 0 };
  756. insert_text_at_caret(ucodestr);
  757. } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  758. char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
  759. insert_text_at_caret(ucodestr);
  760. }
  761. } else if (alt_mode == ALT_INPUT_WIN) {
  762. if (alt_code > 0x00 && alt_code <= 0xff) {
  763. char32_t ucodestr[2] = { alt_code_cp1252[alt_code], 0 };
  764. insert_text_at_caret(ucodestr);
  765. } else if ((alt_code > 0xff && alt_code < 0xd800) || (alt_code > 0xdfff)) {
  766. char32_t ucodestr[2] = { (char32_t)alt_code, 0 };
  767. insert_text_at_caret(ucodestr);
  768. }
  769. }
  770. alt_mode = ALT_INPUT_NONE;
  771. // Mirror paste behavior: defer _text_changed once per frame while emitting text_changed signal.
  772. if (!text_changed_dirty) {
  773. if (is_inside_tree() && text.length() != prev_len) {
  774. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  775. }
  776. text_changed_dirty = true;
  777. }
  778. } else {
  779. ime_text = String();
  780. ime_selection = Vector2i();
  781. _shape();
  782. }
  783. queue_redraw();
  784. accept_event();
  785. return;
  786. }
  787. // Cancel Unicode input.
  788. if (alt_start_no_hold && k->is_action("ui_cancel", true)) {
  789. alt_start = false;
  790. alt_start_no_hold = false;
  791. alt_mode = ALT_INPUT_NONE;
  792. ime_text = String();
  793. ime_selection = Vector2i();
  794. _shape();
  795. queue_redraw();
  796. accept_event();
  797. return;
  798. }
  799. if (!k->is_pressed()) {
  800. return;
  801. }
  802. // Default is ENTER and KP_ENTER. Cannot use ui_accept as default includes SPACE.
  803. if (k->is_action_pressed("ui_text_submit")) {
  804. emit_signal(SceneStringName(text_submitted), text);
  805. if (editing && !keep_editing_on_text_submit) {
  806. unedit();
  807. emit_signal(SNAME("editing_toggled"), false);
  808. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  809. DisplayServer::get_singleton()->virtual_keyboard_hide();
  810. }
  811. }
  812. accept_event();
  813. return;
  814. }
  815. if (k->is_action("ui_cancel")) {
  816. if (editing) {
  817. unedit();
  818. emit_signal(SNAME("editing_toggled"), false);
  819. }
  820. accept_event();
  821. return;
  822. }
  823. if (is_shortcut_keys_enabled()) {
  824. if (k->is_action("ui_paste", true)) {
  825. paste_text();
  826. accept_event();
  827. return;
  828. }
  829. // Undo / Redo
  830. if (k->is_action("ui_undo", true)) {
  831. undo();
  832. accept_event();
  833. return;
  834. }
  835. if (k->is_action("ui_redo", true)) {
  836. redo();
  837. accept_event();
  838. return;
  839. }
  840. }
  841. // BACKSPACE
  842. if (k->is_action("ui_text_backspace_all_to_left", true)) {
  843. _backspace(false, true);
  844. accept_event();
  845. return;
  846. }
  847. if (k->is_action("ui_text_backspace_word", true)) {
  848. _backspace(true);
  849. accept_event();
  850. return;
  851. }
  852. if (k->is_action("ui_text_backspace", true)) {
  853. _backspace();
  854. accept_event();
  855. return;
  856. }
  857. // DELETE
  858. if (k->is_action("ui_text_delete_all_to_right", true)) {
  859. _delete(false, true);
  860. accept_event();
  861. return;
  862. }
  863. if (k->is_action("ui_text_delete_word", true)) {
  864. _delete(true);
  865. accept_event();
  866. return;
  867. }
  868. if (k->is_action("ui_text_delete", true)) {
  869. _delete();
  870. accept_event();
  871. return;
  872. }
  873. // Cursor Movement
  874. k = k->duplicate();
  875. bool shift_pressed = k->is_shift_pressed();
  876. // Remove shift or else actions will not match. Use above variable for selection.
  877. k->set_shift_pressed(false);
  878. if (k->is_action("ui_text_caret_word_left", true)) {
  879. _move_caret_left(shift_pressed, true);
  880. accept_event();
  881. return;
  882. }
  883. if (k->is_action("ui_text_caret_left", true)) {
  884. _move_caret_left(shift_pressed);
  885. accept_event();
  886. return;
  887. }
  888. if (k->is_action("ui_text_caret_word_right", true)) {
  889. _move_caret_right(shift_pressed, true);
  890. accept_event();
  891. return;
  892. }
  893. if (k->is_action("ui_text_caret_right", true)) {
  894. _move_caret_right(shift_pressed, false);
  895. accept_event();
  896. return;
  897. }
  898. // Up = Home, Down = End
  899. 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)) {
  900. _move_caret_start(shift_pressed);
  901. accept_event();
  902. return;
  903. }
  904. 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)) {
  905. _move_caret_end(shift_pressed);
  906. accept_event();
  907. return;
  908. }
  909. // Misc
  910. if (k->is_action("ui_swap_input_direction", true)) {
  911. _swap_current_input_direction();
  912. accept_event();
  913. return;
  914. }
  915. _reset_caret_blink_timer();
  916. // Allow unicode handling if:
  917. // * No Modifiers are pressed (except shift)
  918. bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
  919. if (allow_unicode_handling && editable && k->get_unicode() >= 32) {
  920. // Handle Unicode if no modifiers are active.
  921. selection_delete();
  922. char32_t ucodestr[2] = { (char32_t)k->get_unicode(), 0 };
  923. int prev_len = text.length();
  924. insert_text_at_caret(ucodestr);
  925. if (text.length() != prev_len) {
  926. if (!text_changed_dirty) {
  927. if (is_inside_tree()) {
  928. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  929. }
  930. text_changed_dirty = true;
  931. }
  932. }
  933. accept_event();
  934. return;
  935. }
  936. }
  937. void LineEdit::set_horizontal_alignment(HorizontalAlignment p_alignment) {
  938. ERR_FAIL_INDEX((int)p_alignment, 4);
  939. if (alignment == p_alignment) {
  940. return;
  941. }
  942. alignment = p_alignment;
  943. _shape();
  944. queue_redraw();
  945. }
  946. HorizontalAlignment LineEdit::get_horizontal_alignment() const {
  947. return alignment;
  948. }
  949. Variant LineEdit::get_drag_data(const Point2 &p_point) {
  950. Variant ret = Control::get_drag_data(p_point);
  951. if (ret != Variant()) {
  952. return ret;
  953. }
  954. if (selection.drag_attempt && selection.enabled) {
  955. String t = get_selected_text();
  956. Label *l = memnew(Label);
  957. l->set_text(t);
  958. l->set_focus_mode(FOCUS_ACCESSIBILITY);
  959. l->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Don't translate user input.
  960. set_drag_preview(l);
  961. return t;
  962. }
  963. return Variant();
  964. }
  965. bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  966. bool drop_override = Control::can_drop_data(p_point, p_data); // In case user wants to drop custom data.
  967. if (drop_override) {
  968. return drop_override;
  969. }
  970. return is_editable() && p_data.is_string();
  971. }
  972. void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  973. Control::drop_data(p_point, p_data);
  974. if (p_data.is_string() && is_editable()) {
  975. apply_ime();
  976. if (p_point != Vector2(Math::INF, Math::INF)) {
  977. set_caret_at_pixel_pos(p_point.x);
  978. }
  979. int caret_column_tmp = caret_column;
  980. bool is_inside_sel = selection.enabled && caret_column >= selection.begin && caret_column <= selection.end;
  981. if (Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  982. is_inside_sel = selection.enabled && caret_column > selection.begin && caret_column < selection.end;
  983. }
  984. if (selection.drag_attempt) {
  985. selection.drag_attempt = false;
  986. if (!is_inside_sel) {
  987. if (!Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  988. if (caret_column_tmp > selection.end) {
  989. caret_column_tmp = caret_column_tmp - (selection.end - selection.begin);
  990. }
  991. selection_delete();
  992. }
  993. set_caret_column(caret_column_tmp);
  994. insert_text_at_caret(p_data);
  995. }
  996. } else if (selection.enabled && caret_column >= selection.begin && caret_column <= selection.end) {
  997. caret_column_tmp = selection.begin;
  998. selection_delete();
  999. set_caret_column(caret_column_tmp);
  1000. insert_text_at_caret(p_data);
  1001. grab_focus(true);
  1002. } else {
  1003. insert_text_at_caret(p_data);
  1004. grab_focus(true);
  1005. }
  1006. select(caret_column_tmp, caret_column);
  1007. if (!text_changed_dirty) {
  1008. if (is_inside_tree()) {
  1009. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  1010. }
  1011. text_changed_dirty = true;
  1012. }
  1013. queue_accessibility_update();
  1014. queue_redraw();
  1015. }
  1016. }
  1017. Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const {
  1018. if ((!text.is_empty() && is_editable() && _is_over_clear_button(p_pos)) || (!is_editable() && (!is_selecting_enabled() || text.is_empty()))) {
  1019. return CURSOR_ARROW;
  1020. }
  1021. return Control::get_cursor_shape(p_pos);
  1022. }
  1023. bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const {
  1024. if (!clear_button_enabled || !has_point(p_pos)) {
  1025. return false;
  1026. }
  1027. Ref<Texture2D> icon = theme_cache.clear_icon;
  1028. return is_layout_rtl() ? p_pos.x < theme_cache.normal->get_margin(SIDE_LEFT) + icon->get_width() : p_pos.x > get_size().width - icon->get_width() - theme_cache.normal->get_margin(SIDE_RIGHT);
  1029. }
  1030. void LineEdit::_update_theme_item_cache() {
  1031. Control::_update_theme_item_cache();
  1032. theme_cache.base_scale = get_theme_default_base_scale();
  1033. }
  1034. void LineEdit::_accessibility_action_set_selection(const Variant &p_data) {
  1035. Dictionary new_selection = p_data;
  1036. int sel_start_pos = new_selection["start_char"];
  1037. int sel_end_pos = new_selection["end_char"];
  1038. select(sel_start_pos, sel_end_pos);
  1039. }
  1040. void LineEdit::_accessibility_action_replace_selected(const Variant &p_data) {
  1041. String new_text = p_data;
  1042. insert_text_at_caret(new_text);
  1043. }
  1044. void LineEdit::_accessibility_action_set_value(const Variant &p_data) {
  1045. String new_text = p_data;
  1046. set_text(new_text);
  1047. }
  1048. void LineEdit::_accessibility_action_menu(const Variant &p_data) {
  1049. _update_context_menu();
  1050. Point2 pos = Point2(get_caret_pixel_pos().x, (get_size().y + theme_cache.font->get_height(theme_cache.font_size)) / 2);
  1051. menu->set_position(get_screen_position() + pos);
  1052. menu->reset_size();
  1053. menu->popup();
  1054. menu->grab_focus();
  1055. }
  1056. void LineEdit::_notification(int p_what) {
  1057. switch (p_what) {
  1058. #ifdef TOOLS_ENABLED
  1059. case NOTIFICATION_ENTER_TREE: {
  1060. if (Engine::get_singleton()->is_editor_hint() && !is_part_of_edited_scene()) {
  1061. set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  1062. set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  1063. if (!EditorSettings::get_singleton()->is_connected("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed))) {
  1064. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &LineEdit::_editor_settings_changed));
  1065. }
  1066. }
  1067. } break;
  1068. #endif
  1069. case NOTIFICATION_EXIT_TREE:
  1070. case NOTIFICATION_ACCESSIBILITY_INVALIDATE: {
  1071. accessibility_text_root_element = RID();
  1072. } break;
  1073. case NOTIFICATION_ACCESSIBILITY_UPDATE: {
  1074. RID ae = get_accessibility_element();
  1075. ERR_FAIL_COND(ae.is_null());
  1076. DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_TEXT_FIELD);
  1077. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1078. if (using_placeholder && !placeholder.is_empty()) {
  1079. DisplayServer::get_singleton()->accessibility_update_set_placeholder(ae, atr(placeholder));
  1080. }
  1081. if (!placeholder.is_empty() && get_accessibility_name().is_empty()) {
  1082. DisplayServer::get_singleton()->accessibility_update_set_name(ae, atr(placeholder));
  1083. }
  1084. DisplayServer::get_singleton()->accessibility_update_set_flag(ae, DisplayServer::AccessibilityFlags::FLAG_READONLY, !editable);
  1085. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_TEXT_SELECTION, callable_mp(this, &LineEdit::_accessibility_action_set_selection));
  1086. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_REPLACE_SELECTED_TEXT, callable_mp(this, &LineEdit::_accessibility_action_replace_selected));
  1087. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SET_VALUE, callable_mp(this, &LineEdit::_accessibility_action_set_value));
  1088. DisplayServer::get_singleton()->accessibility_update_add_action(ae, DisplayServer::AccessibilityAction::ACTION_SHOW_CONTEXT_MENU, callable_mp(this, &LineEdit::_accessibility_action_menu));
  1089. const String &lang = language.is_empty() ? _get_locale() : language;
  1090. DisplayServer::get_singleton()->accessibility_update_set_language(ae, lang);
  1091. bool rtl = is_layout_rtl();
  1092. Ref<StyleBox> style = theme_cache.normal;
  1093. Ref<Font> font = theme_cache.font;
  1094. Size2 size = get_size();
  1095. int x_ofs = 0;
  1096. float text_width = TS->shaped_text_get_size(text_rid).x;
  1097. float text_height = TS->shaped_text_get_size(text_rid).y;
  1098. int y_area = size.height - style->get_minimum_size().height;
  1099. int y_ofs = style->get_offset().y + (y_area - text_height) / 2;
  1100. switch (alignment) {
  1101. case HORIZONTAL_ALIGNMENT_FILL:
  1102. case HORIZONTAL_ALIGNMENT_LEFT: {
  1103. if (rtl) {
  1104. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  1105. } else {
  1106. x_ofs = style->get_margin(SIDE_LEFT);
  1107. }
  1108. } break;
  1109. case HORIZONTAL_ALIGNMENT_CENTER: {
  1110. if (!Math::is_zero_approx(scroll_offset)) {
  1111. x_ofs = style->get_margin(SIDE_LEFT);
  1112. } else {
  1113. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1114. int centered = int((size.width - total_margin - text_width)) / 2;
  1115. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1116. }
  1117. } break;
  1118. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1119. if (rtl) {
  1120. x_ofs = style->get_margin(SIDE_LEFT);
  1121. } else {
  1122. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  1123. }
  1124. } break;
  1125. }
  1126. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1127. if (right_icon.is_valid() || display_clear_icon) {
  1128. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1129. Point2 right_icon_size = _get_right_icon_size(r_icon);
  1130. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1131. if (Math::is_zero_approx(scroll_offset)) {
  1132. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1133. int center = int(size.width - total_margin - text_width - right_icon_size.width) / 2;
  1134. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1135. }
  1136. if (rtl) {
  1137. x_ofs += right_icon_size.width;
  1138. }
  1139. } else {
  1140. if (rtl) {
  1141. x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs);
  1142. } else {
  1143. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT));
  1144. }
  1145. }
  1146. }
  1147. float text_off_x = x_ofs + scroll_offset;
  1148. if (accessibility_text_root_element.is_null()) {
  1149. accessibility_text_root_element = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(ae, using_placeholder ? RID() : text_rid, text_height, -1, true);
  1150. }
  1151. Transform2D text_xform;
  1152. text_xform.set_origin(Vector2i(text_off_x, y_ofs));
  1153. DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_text_root_element, text_xform);
  1154. if (selection.enabled) {
  1155. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, accessibility_text_root_element, selection.begin, accessibility_text_root_element, selection.end);
  1156. } else {
  1157. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, accessibility_text_root_element, caret_column, accessibility_text_root_element, caret_column);
  1158. }
  1159. } break;
  1160. case NOTIFICATION_RESIZED: {
  1161. _fit_to_width();
  1162. scroll_offset = 0.0;
  1163. set_caret_column(get_caret_column());
  1164. } break;
  1165. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  1166. case NOTIFICATION_THEME_CHANGED: {
  1167. _shape();
  1168. queue_redraw();
  1169. } break;
  1170. case NOTIFICATION_TRANSLATION_CHANGED: {
  1171. placeholder_translated = atr(placeholder);
  1172. _shape();
  1173. queue_redraw();
  1174. } break;
  1175. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  1176. window_has_focus = true;
  1177. _validate_caret_can_draw();
  1178. queue_redraw();
  1179. } break;
  1180. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  1181. window_has_focus = false;
  1182. _validate_caret_can_draw();
  1183. queue_redraw();
  1184. } break;
  1185. case NOTIFICATION_INTERNAL_PROCESS: {
  1186. if (caret_blink_enabled && caret_can_draw) {
  1187. caret_blink_timer += get_process_delta_time();
  1188. if (caret_blink_timer >= caret_blink_interval) {
  1189. caret_blink_timer = 0.0;
  1190. _toggle_draw_caret();
  1191. }
  1192. }
  1193. } break;
  1194. case NOTIFICATION_DRAW: {
  1195. int width, height;
  1196. bool rtl = is_layout_rtl();
  1197. Size2 size = get_size();
  1198. width = size.width;
  1199. height = size.height;
  1200. RID ci = get_canvas_item();
  1201. Ref<StyleBox> style = theme_cache.normal;
  1202. if (!is_editable()) {
  1203. style = theme_cache.read_only;
  1204. }
  1205. Ref<Font> font = theme_cache.font;
  1206. if (!flat) {
  1207. style->draw(ci, Rect2(Point2(), size));
  1208. }
  1209. if (has_focus(Engine::get_singleton()->is_editor_hint() || GLOBAL_GET_CACHED(int, "gui/common/show_focus_state_on_pointer_event") != 1)) {
  1210. theme_cache.focus->draw(ci, Rect2(Point2(), size));
  1211. }
  1212. int x_ofs = 0;
  1213. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1214. float text_width = TS->shaped_text_get_size(text_rid).x;
  1215. float text_height = TS->shaped_text_get_size(text_rid).y;
  1216. switch (alignment) {
  1217. case HORIZONTAL_ALIGNMENT_FILL:
  1218. case HORIZONTAL_ALIGNMENT_LEFT: {
  1219. if (rtl) {
  1220. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  1221. } else {
  1222. x_ofs = style->get_margin(SIDE_LEFT);
  1223. }
  1224. } break;
  1225. case HORIZONTAL_ALIGNMENT_CENTER: {
  1226. if (!Math::is_zero_approx(scroll_offset)) {
  1227. x_ofs = style->get_margin(SIDE_LEFT);
  1228. } else {
  1229. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1230. int centered = int((size.width - total_margin - text_width)) / 2;
  1231. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1232. }
  1233. } break;
  1234. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1235. if (rtl) {
  1236. x_ofs = style->get_margin(SIDE_LEFT);
  1237. } else {
  1238. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  1239. }
  1240. } break;
  1241. }
  1242. int ofs_max = width - style->get_margin(SIDE_RIGHT);
  1243. int y_area = height - style->get_minimum_size().height;
  1244. int y_ofs = style->get_offset().y + (y_area - text_height) / 2;
  1245. Color selection_color = theme_cache.selection_color;
  1246. Color font_color;
  1247. if (is_editable()) {
  1248. font_color = theme_cache.font_color;
  1249. } else {
  1250. font_color = theme_cache.font_uneditable_color;
  1251. }
  1252. Color font_selected_color = theme_cache.font_selected_color;
  1253. Color caret_color = theme_cache.caret_color;
  1254. // Draw placeholder color.
  1255. if (using_placeholder) {
  1256. font_color = theme_cache.font_placeholder_color;
  1257. }
  1258. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1259. if (right_icon.is_valid() || display_clear_icon) {
  1260. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1261. Point2 right_icon_size = _get_right_icon_size(r_icon);
  1262. Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9);
  1263. if (display_clear_icon) {
  1264. if (clear_button_status.press_attempt && clear_button_status.pressing_inside) {
  1265. color_icon = theme_cache.clear_button_color_pressed;
  1266. } else {
  1267. color_icon = theme_cache.clear_button_color;
  1268. }
  1269. }
  1270. Point2 icon_pos = Point2(width - right_icon_size.width - style->get_margin(SIDE_RIGHT), height / 2 - right_icon_size.height / 2);
  1271. if (rtl) {
  1272. icon_pos.x = style->get_margin(SIDE_LEFT);
  1273. }
  1274. Rect2 icon_region = Rect2(icon_pos, right_icon_size);
  1275. draw_texture_rect(r_icon, icon_region, false, color_icon);
  1276. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1277. if (Math::is_zero_approx(scroll_offset)) {
  1278. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1279. int center = int(size.width - total_margin - text_width - right_icon_size.width) / 2;
  1280. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1281. }
  1282. if (rtl) {
  1283. x_ofs += right_icon_size.width;
  1284. }
  1285. } else {
  1286. if (rtl) {
  1287. x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs);
  1288. } else {
  1289. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT));
  1290. }
  1291. }
  1292. if (!rtl) {
  1293. ofs_max -= right_icon_size.width;
  1294. }
  1295. }
  1296. // Draw selections rects.
  1297. Vector2 ofs = Point2(x_ofs + scroll_offset, y_ofs);
  1298. if (selection.enabled) {
  1299. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, selection.begin, selection.end);
  1300. for (int i = 0; i < sel.size(); i++) {
  1301. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  1302. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  1303. continue;
  1304. }
  1305. if (rect.position.x < x_ofs) {
  1306. rect.size.x -= (x_ofs - rect.position.x);
  1307. rect.position.x = x_ofs;
  1308. }
  1309. if (rect.position.x + rect.size.x > ofs_max) {
  1310. rect.size.x = ofs_max - rect.position.x;
  1311. }
  1312. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_color);
  1313. }
  1314. }
  1315. const Glyph *glyphs = TS->shaped_text_get_glyphs(text_rid);
  1316. int gl_size = TS->shaped_text_get_glyph_count(text_rid);
  1317. // Draw text.
  1318. ofs.y += TS->shaped_text_get_ascent(text_rid);
  1319. Color font_outline_color = theme_cache.font_outline_color;
  1320. int outline_size = theme_cache.font_outline_size;
  1321. if (outline_size > 0 && font_outline_color.a > 0) {
  1322. Vector2 oofs = ofs;
  1323. for (int i = 0; i < gl_size; i++) {
  1324. for (int j = 0; j < glyphs[i].repeat; j++) {
  1325. if (std::ceil(oofs.x) >= x_ofs && (oofs.x + glyphs[i].advance) <= ofs_max) {
  1326. if (glyphs[i].font_rid != RID()) {
  1327. 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);
  1328. }
  1329. }
  1330. oofs.x += glyphs[i].advance;
  1331. }
  1332. if (oofs.x >= ofs_max) {
  1333. break;
  1334. }
  1335. }
  1336. }
  1337. for (int i = 0; i < gl_size; i++) {
  1338. bool selected = selection.enabled && glyphs[i].start >= selection.begin && glyphs[i].end <= selection.end;
  1339. for (int j = 0; j < glyphs[i].repeat; j++) {
  1340. if (std::ceil(ofs.x) >= x_ofs && (ofs.x + glyphs[i].advance) <= ofs_max) {
  1341. if (glyphs[i].font_rid != RID()) {
  1342. 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);
  1343. } 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)) {
  1344. 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);
  1345. }
  1346. }
  1347. ofs.x += glyphs[i].advance;
  1348. }
  1349. if (ofs.x >= ofs_max) {
  1350. break;
  1351. }
  1352. }
  1353. // Draw carets.
  1354. ofs.x = x_ofs + scroll_offset;
  1355. if ((caret_can_draw && draw_caret) || drag_caret_force_displayed) {
  1356. // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
  1357. const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
  1358. if (ime_text.is_empty() || ime_selection.y == 0) {
  1359. // Normal caret.
  1360. CaretInfo caret = TS->shaped_text_get_carets(text_rid, ime_text.is_empty() ? caret_column : caret_column + ime_selection.x);
  1361. if (using_placeholder || (caret.l_caret == Rect2() && caret.t_caret == Rect2())) {
  1362. // No carets, add one at the start.
  1363. int h = theme_cache.font->get_height(theme_cache.font_size);
  1364. int y = style->get_offset().y + (y_area - h) / 2;
  1365. caret.l_dir = (rtl) ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  1366. switch (alignment) {
  1367. case HORIZONTAL_ALIGNMENT_FILL:
  1368. case HORIZONTAL_ALIGNMENT_LEFT: {
  1369. if (rtl) {
  1370. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  1371. } else {
  1372. caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h));
  1373. }
  1374. } break;
  1375. case HORIZONTAL_ALIGNMENT_CENTER: {
  1376. int icon_width = 0;
  1377. if (right_icon.is_valid()) {
  1378. icon_width = right_icon->get_width();
  1379. }
  1380. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1381. int center = int(size.width - total_margin - icon_width) / 2;
  1382. caret.l_caret = Rect2(Vector2(style->get_margin(SIDE_LEFT) + MAX(0, center), y), Size2(caret_width, h));
  1383. } break;
  1384. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1385. if (rtl) {
  1386. caret.l_caret = Rect2(Vector2(x_ofs, y), Size2(caret_width, h));
  1387. } else {
  1388. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  1389. }
  1390. } break;
  1391. }
  1392. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  1393. } else {
  1394. if (caret.l_caret != Rect2() && caret.l_dir == TextServer::DIRECTION_AUTO) {
  1395. // Draw extra marker on top of mid caret.
  1396. Rect2 trect = Rect2(caret.l_caret.position.x - 2.5 * caret_width, caret.l_caret.position.y, 6 * caret_width, caret_width);
  1397. trect.position += ofs;
  1398. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1399. } else if (caret.l_caret != Rect2() && caret.t_caret != Rect2() && caret.l_dir != caret.t_dir) {
  1400. // Draw extra direction marker on top of split caret.
  1401. float d = (caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1402. 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);
  1403. trect.position += ofs;
  1404. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1405. d = (caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1406. trect = Rect2(caret.t_caret.position.x + d * caret_width, caret.t_caret.position.y, 3 * caret_width, caret_width);
  1407. trect.position += ofs;
  1408. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1409. }
  1410. caret.l_caret.position += ofs;
  1411. caret.l_caret.size.x = caret_width;
  1412. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  1413. caret.t_caret.position += ofs;
  1414. caret.t_caret.size.x = caret_width;
  1415. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.t_caret, caret_color);
  1416. }
  1417. }
  1418. if (!ime_text.is_empty()) {
  1419. {
  1420. // IME intermediate text range.
  1421. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column, caret_column + ime_text.length());
  1422. for (int i = 0; i < sel.size(); i++) {
  1423. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  1424. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  1425. continue;
  1426. }
  1427. if (rect.position.x < x_ofs) {
  1428. rect.size.x -= (x_ofs - rect.position.x);
  1429. rect.position.x = x_ofs;
  1430. } else if (rect.position.x + rect.size.x > ofs_max) {
  1431. rect.size.x = ofs_max - rect.position.x;
  1432. }
  1433. rect.size.y = caret_width;
  1434. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  1435. }
  1436. }
  1437. {
  1438. // IME caret.
  1439. if (ime_selection.y > 0) {
  1440. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column + ime_selection.x, caret_column + ime_selection.x + ime_selection.y);
  1441. for (int i = 0; i < sel.size(); i++) {
  1442. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  1443. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  1444. continue;
  1445. }
  1446. if (rect.position.x < x_ofs) {
  1447. rect.size.x -= (x_ofs - rect.position.x);
  1448. rect.position.x = x_ofs;
  1449. } else if (rect.position.x + rect.size.x > ofs_max) {
  1450. rect.size.x = ofs_max - rect.position.x;
  1451. }
  1452. rect.size.y = caret_width * 3;
  1453. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  1454. }
  1455. }
  1456. }
  1457. }
  1458. }
  1459. if (editing) {
  1460. _update_ime_window_position();
  1461. }
  1462. } break;
  1463. case NOTIFICATION_FOCUS_ENTER: {
  1464. // Only allow editing if the LineEdit is not focused with arrow keys.
  1465. if (!(Input::get_singleton()->is_action_pressed("ui_up") || Input::get_singleton()->is_action_pressed("ui_down") || Input::get_singleton()->is_action_pressed("ui_left") || Input::get_singleton()->is_action_pressed("ui_right"))) {
  1466. _edit(virtual_keyboard_show_on_focus);
  1467. emit_signal(SNAME("editing_toggled"), true);
  1468. }
  1469. } break;
  1470. case NOTIFICATION_FOCUS_EXIT: {
  1471. if (editing) {
  1472. unedit();
  1473. emit_signal(SNAME("editing_toggled"), false);
  1474. }
  1475. } break;
  1476. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  1477. if (editing) {
  1478. const String &new_ime_text = DisplayServer::get_singleton()->ime_get_text();
  1479. const Vector2i &new_ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  1480. if (ime_text == new_ime_text && ime_selection == new_ime_selection) {
  1481. break;
  1482. }
  1483. if (!window_has_focus && !new_ime_text.is_empty()) {
  1484. break;
  1485. }
  1486. ime_text = new_ime_text;
  1487. ime_selection = new_ime_selection;
  1488. if (!ime_text.is_empty()) {
  1489. selection_delete();
  1490. }
  1491. _shape();
  1492. set_caret_column(caret_column); // Update scroll_offset.
  1493. }
  1494. } break;
  1495. case NOTIFICATION_DRAG_BEGIN: {
  1496. drag_action = true;
  1497. } break;
  1498. case NOTIFICATION_DRAG_END: {
  1499. if (is_drag_successful()) {
  1500. if (selection.drag_attempt) {
  1501. selection.drag_attempt = false;
  1502. if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1503. selection_delete();
  1504. } else if (deselect_on_focus_loss_enabled) {
  1505. deselect();
  1506. }
  1507. }
  1508. } else {
  1509. selection.drag_attempt = false;
  1510. }
  1511. drag_action = false;
  1512. drag_caret_force_displayed = false;
  1513. queue_redraw();
  1514. } break;
  1515. case NOTIFICATION_MOUSE_EXIT: {
  1516. if (drag_caret_force_displayed) {
  1517. drag_caret_force_displayed = false;
  1518. queue_redraw();
  1519. }
  1520. } break;
  1521. }
  1522. }
  1523. void LineEdit::copy_text() {
  1524. if (selection.enabled && !pass) {
  1525. DisplayServer::get_singleton()->clipboard_set(get_selected_text());
  1526. }
  1527. }
  1528. void LineEdit::cut_text() {
  1529. if (editable && selection.enabled && !pass) {
  1530. DisplayServer::get_singleton()->clipboard_set(get_selected_text());
  1531. selection_delete();
  1532. }
  1533. }
  1534. void LineEdit::paste_text() {
  1535. if (!editable) {
  1536. return;
  1537. }
  1538. // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
  1539. String paste_buffer = DisplayServer::get_singleton()->clipboard_get().strip_escapes();
  1540. if (!paste_buffer.is_empty()) {
  1541. int prev_len = text.length();
  1542. if (selection.enabled) {
  1543. selection_delete();
  1544. }
  1545. insert_text_at_caret(paste_buffer);
  1546. if (!text_changed_dirty) {
  1547. if (is_inside_tree() && text.length() != prev_len) {
  1548. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  1549. }
  1550. text_changed_dirty = true;
  1551. }
  1552. }
  1553. }
  1554. bool LineEdit::has_undo() const {
  1555. if (undo_stack_pos == nullptr) {
  1556. return undo_stack.size() > 1;
  1557. }
  1558. return undo_stack_pos != undo_stack.front();
  1559. }
  1560. bool LineEdit::has_redo() const {
  1561. return undo_stack_pos != nullptr && undo_stack_pos != undo_stack.back();
  1562. }
  1563. void LineEdit::undo() {
  1564. if (!editable) {
  1565. return;
  1566. }
  1567. if (!has_undo()) {
  1568. return;
  1569. }
  1570. if (undo_stack_pos == nullptr) {
  1571. undo_stack_pos = undo_stack.back();
  1572. }
  1573. deselect();
  1574. undo_stack_pos = undo_stack_pos->prev();
  1575. TextOperation op = undo_stack_pos->get();
  1576. text = op.text;
  1577. scroll_offset = op.scroll_offset;
  1578. _shape();
  1579. set_caret_column(op.caret_column);
  1580. _emit_text_change();
  1581. }
  1582. void LineEdit::redo() {
  1583. if (!editable) {
  1584. return;
  1585. }
  1586. if (!has_redo()) {
  1587. return;
  1588. }
  1589. deselect();
  1590. undo_stack_pos = undo_stack_pos->next();
  1591. TextOperation op = undo_stack_pos->get();
  1592. text = op.text;
  1593. scroll_offset = op.scroll_offset;
  1594. _shape();
  1595. set_caret_column(op.caret_column);
  1596. _emit_text_change();
  1597. }
  1598. void LineEdit::shift_selection_check_pre(bool p_shift) {
  1599. if (!selection.enabled && p_shift) {
  1600. selection.start_column = caret_column;
  1601. }
  1602. if (!p_shift) {
  1603. deselect();
  1604. }
  1605. }
  1606. void LineEdit::shift_selection_check_post(bool p_shift) {
  1607. if (p_shift) {
  1608. selection_fill_at_caret();
  1609. }
  1610. }
  1611. void LineEdit::set_caret_at_pixel_pos(int p_x) {
  1612. Ref<StyleBox> style = theme_cache.normal;
  1613. bool rtl = is_layout_rtl();
  1614. int x_ofs = 0;
  1615. float text_width = TS->shaped_text_get_size(text_rid).x;
  1616. switch (alignment) {
  1617. case HORIZONTAL_ALIGNMENT_FILL:
  1618. case HORIZONTAL_ALIGNMENT_LEFT: {
  1619. if (rtl) {
  1620. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1621. } else {
  1622. x_ofs = style->get_margin(SIDE_LEFT);
  1623. }
  1624. } break;
  1625. case HORIZONTAL_ALIGNMENT_CENTER: {
  1626. if (!Math::is_zero_approx(scroll_offset)) {
  1627. x_ofs = style->get_margin(SIDE_LEFT);
  1628. } else {
  1629. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1630. int centered = int((get_size().width - total_margin - text_width)) / 2;
  1631. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1632. }
  1633. } break;
  1634. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1635. if (rtl) {
  1636. x_ofs = style->get_margin(SIDE_LEFT);
  1637. } else {
  1638. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1639. }
  1640. } break;
  1641. }
  1642. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1643. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1644. if (right_icon.is_valid() || display_clear_icon) {
  1645. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1646. Point2 right_icon_size = _get_right_icon_size(r_icon);
  1647. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1648. if (Math::is_zero_approx(scroll_offset)) {
  1649. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1650. int center = int(get_size().width - total_margin - text_width - right_icon_size.width) / 2;
  1651. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1652. }
  1653. if (rtl) {
  1654. x_ofs += right_icon_size.width;
  1655. }
  1656. } else {
  1657. if (rtl) {
  1658. x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs);
  1659. } else {
  1660. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT));
  1661. }
  1662. }
  1663. }
  1664. int ofs = std::ceil(TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset));
  1665. if (ofs == -1) {
  1666. return;
  1667. }
  1668. if (!caret_mid_grapheme_enabled) {
  1669. ofs = TS->shaped_text_closest_character_pos(text_rid, ofs);
  1670. }
  1671. set_caret_column(ofs);
  1672. }
  1673. Vector2 LineEdit::get_caret_pixel_pos() {
  1674. Ref<StyleBox> style = theme_cache.normal;
  1675. bool rtl = is_layout_rtl();
  1676. int x_ofs = 0;
  1677. float text_width = TS->shaped_text_get_size(text_rid).x;
  1678. switch (alignment) {
  1679. case HORIZONTAL_ALIGNMENT_FILL:
  1680. case HORIZONTAL_ALIGNMENT_LEFT: {
  1681. if (rtl) {
  1682. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1683. } else {
  1684. x_ofs = style->get_margin(SIDE_LEFT);
  1685. }
  1686. } break;
  1687. case HORIZONTAL_ALIGNMENT_CENTER: {
  1688. if (!Math::is_zero_approx(scroll_offset)) {
  1689. x_ofs = style->get_margin(SIDE_LEFT);
  1690. } else {
  1691. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1692. int centered = int((get_size().width - total_margin - text_width)) / 2;
  1693. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1694. }
  1695. } break;
  1696. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1697. if (rtl) {
  1698. x_ofs = style->get_margin(SIDE_LEFT);
  1699. } else {
  1700. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1701. }
  1702. } break;
  1703. }
  1704. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1705. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1706. if (right_icon.is_valid() || display_clear_icon) {
  1707. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1708. Point2 right_icon_size = _get_right_icon_size(r_icon);
  1709. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1710. if (Math::is_zero_approx(scroll_offset)) {
  1711. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1712. int center = int(get_size().width - total_margin - text_width - right_icon_size.width) / 2;
  1713. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1714. }
  1715. if (rtl) {
  1716. x_ofs += right_icon_size.width;
  1717. }
  1718. } else {
  1719. if (rtl) {
  1720. x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs);
  1721. } else {
  1722. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT));
  1723. }
  1724. }
  1725. }
  1726. Vector2 ret;
  1727. CaretInfo caret;
  1728. // Get position of the start of caret.
  1729. if (!ime_text.is_empty() && ime_selection.x != 0) {
  1730. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x);
  1731. } else {
  1732. caret = TS->shaped_text_get_carets(text_rid, caret_column);
  1733. }
  1734. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1735. ret.x = x_ofs + caret.l_caret.position.x + scroll_offset;
  1736. } else {
  1737. ret.x = x_ofs + caret.t_caret.position.x + scroll_offset;
  1738. }
  1739. // Get position of the end of caret.
  1740. if (!ime_text.is_empty()) {
  1741. if (ime_selection.y != 0) {
  1742. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x + ime_selection.y);
  1743. } else {
  1744. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_text.size());
  1745. }
  1746. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1747. ret.y = x_ofs + caret.l_caret.position.x + scroll_offset;
  1748. } else {
  1749. ret.y = x_ofs + caret.t_caret.position.x + scroll_offset;
  1750. }
  1751. } else {
  1752. ret.y = ret.x;
  1753. }
  1754. return ret;
  1755. }
  1756. void LineEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) {
  1757. caret_mid_grapheme_enabled = p_enabled;
  1758. }
  1759. bool LineEdit::is_caret_mid_grapheme_enabled() const {
  1760. return caret_mid_grapheme_enabled;
  1761. }
  1762. bool LineEdit::is_caret_blink_enabled() const {
  1763. return caret_blink_enabled;
  1764. }
  1765. void LineEdit::set_caret_blink_enabled(const bool p_enabled) {
  1766. if (caret_blink_enabled == p_enabled) {
  1767. return;
  1768. }
  1769. caret_blink_enabled = p_enabled;
  1770. set_process_internal(p_enabled);
  1771. draw_caret = !caret_blink_enabled;
  1772. if (caret_blink_enabled) {
  1773. caret_blink_timer = 0.0;
  1774. }
  1775. queue_redraw();
  1776. notify_property_list_changed();
  1777. }
  1778. bool LineEdit::is_caret_force_displayed() const {
  1779. return caret_force_displayed;
  1780. }
  1781. void LineEdit::set_caret_force_displayed(const bool p_enabled) {
  1782. if (caret_force_displayed == p_enabled) {
  1783. return;
  1784. }
  1785. caret_force_displayed = p_enabled;
  1786. _validate_caret_can_draw();
  1787. queue_redraw();
  1788. }
  1789. float LineEdit::get_caret_blink_interval() const {
  1790. return caret_blink_interval;
  1791. }
  1792. void LineEdit::set_caret_blink_interval(const float p_interval) {
  1793. ERR_FAIL_COND(p_interval <= 0);
  1794. caret_blink_interval = p_interval;
  1795. }
  1796. void LineEdit::_reset_caret_blink_timer() {
  1797. if (caret_blink_enabled) {
  1798. draw_caret = true;
  1799. if (caret_can_draw) {
  1800. caret_blink_timer = 0.0;
  1801. queue_redraw();
  1802. }
  1803. }
  1804. }
  1805. void LineEdit::_toggle_draw_caret() {
  1806. draw_caret = !draw_caret;
  1807. if (is_visible_in_tree() && caret_can_draw) {
  1808. queue_redraw();
  1809. }
  1810. }
  1811. void LineEdit::_validate_caret_can_draw() {
  1812. if (caret_blink_enabled) {
  1813. draw_caret = true;
  1814. caret_blink_timer = 0.0;
  1815. }
  1816. caret_can_draw = (caret_force_displayed && !is_part_of_edited_scene()) || (editing && (window_has_focus || (menu && menu->has_focus())) && has_focus());
  1817. }
  1818. void LineEdit::delete_char() {
  1819. if (text.is_empty() || caret_column == 0) {
  1820. return;
  1821. }
  1822. int delete_char_offset = 1;
  1823. if (!caret_mid_grapheme_enabled && backspace_deletes_composite_character_enabled) {
  1824. delete_char_offset = caret_column - get_previous_composite_character_column(caret_column);
  1825. }
  1826. text = text.left(caret_column - delete_char_offset) + text.substr(caret_column);
  1827. _shape();
  1828. set_caret_column(get_caret_column() - delete_char_offset);
  1829. _text_changed();
  1830. }
  1831. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  1832. ERR_FAIL_COND_MSG(p_from_column < 0 || p_from_column > p_to_column || p_to_column > text.length(),
  1833. vformat("Positional parameters (from: %d, to: %d) are inverted or outside the text length (%d).", p_from_column, p_to_column, text.length()));
  1834. text = text.left(p_from_column) + text.substr(p_to_column);
  1835. _shape();
  1836. set_caret_column(caret_column - CLAMP(caret_column - p_from_column, 0, p_to_column - p_from_column));
  1837. if (!text_changed_dirty) {
  1838. if (is_inside_tree()) {
  1839. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  1840. }
  1841. text_changed_dirty = true;
  1842. }
  1843. }
  1844. void LineEdit::_set_text(String p_text, bool p_emit_signal) {
  1845. clear_internal();
  1846. String previous_text = get_text();
  1847. insert_text_at_caret(p_text);
  1848. if (get_text() != previous_text) {
  1849. _create_undo_state();
  1850. if (p_emit_signal) {
  1851. _text_changed();
  1852. }
  1853. }
  1854. queue_redraw();
  1855. caret_column = 0;
  1856. scroll_offset = 0.0;
  1857. }
  1858. void LineEdit::set_text(String p_text) {
  1859. _set_text(p_text);
  1860. }
  1861. void LineEdit::set_text_with_selection(const String &p_text) {
  1862. Selection selection_copy = selection;
  1863. clear_internal();
  1864. String previous_text = get_text();
  1865. insert_text_at_caret(p_text);
  1866. if (get_text() != previous_text) {
  1867. _create_undo_state();
  1868. }
  1869. int tlen = text.length();
  1870. selection = selection_copy;
  1871. selection.begin = MIN(selection.begin, tlen);
  1872. selection.end = MIN(selection.end, tlen);
  1873. selection.start_column = MIN(selection.start_column, tlen);
  1874. queue_redraw();
  1875. }
  1876. void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
  1877. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1878. if (text_direction != p_text_direction) {
  1879. text_direction = p_text_direction;
  1880. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  1881. input_direction = text_direction;
  1882. }
  1883. _shape();
  1884. if (menu_dir) {
  1885. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  1886. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  1887. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  1888. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  1889. }
  1890. queue_redraw();
  1891. }
  1892. }
  1893. Control::TextDirection LineEdit::get_text_direction() const {
  1894. return text_direction;
  1895. }
  1896. void LineEdit::set_language(const String &p_language) {
  1897. if (language != p_language) {
  1898. language = p_language;
  1899. _shape();
  1900. queue_redraw();
  1901. }
  1902. }
  1903. String LineEdit::get_language() const {
  1904. return language;
  1905. }
  1906. void LineEdit::set_draw_control_chars(bool p_draw_control_chars) {
  1907. if (draw_control_chars != p_draw_control_chars) {
  1908. draw_control_chars = p_draw_control_chars;
  1909. if (menu && menu->get_item_index(MENU_DISPLAY_UCC) >= 0) {
  1910. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  1911. }
  1912. _shape();
  1913. queue_redraw();
  1914. }
  1915. }
  1916. bool LineEdit::get_draw_control_chars() const {
  1917. return draw_control_chars;
  1918. }
  1919. void LineEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  1920. if (st_parser != p_parser) {
  1921. st_parser = p_parser;
  1922. _shape();
  1923. queue_redraw();
  1924. }
  1925. }
  1926. TextServer::StructuredTextParser LineEdit::get_structured_text_bidi_override() const {
  1927. return st_parser;
  1928. }
  1929. void LineEdit::set_structured_text_bidi_override_options(const Array &p_args) {
  1930. st_args = Array(p_args);
  1931. _shape();
  1932. queue_redraw();
  1933. }
  1934. Array LineEdit::get_structured_text_bidi_override_options() const {
  1935. return Array(st_args);
  1936. }
  1937. void LineEdit::clear() {
  1938. bool was_empty = text.is_empty();
  1939. clear_internal();
  1940. _clear_redo();
  1941. if (!was_empty) {
  1942. _emit_text_change();
  1943. }
  1944. // This should reset virtual keyboard state if needed.
  1945. if (editing) {
  1946. show_virtual_keyboard();
  1947. }
  1948. }
  1949. void LineEdit::show_virtual_keyboard() {
  1950. _update_ime_window_position();
  1951. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1952. if (selection.enabled) {
  1953. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, selection.begin, selection.end);
  1954. } else {
  1955. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, caret_column);
  1956. }
  1957. }
  1958. }
  1959. String LineEdit::get_text() const {
  1960. return text;
  1961. }
  1962. void LineEdit::set_placeholder(String p_text) {
  1963. if (placeholder == p_text) {
  1964. return;
  1965. }
  1966. placeholder = p_text;
  1967. placeholder_translated = atr(placeholder);
  1968. _shape();
  1969. queue_redraw();
  1970. }
  1971. String LineEdit::get_placeholder() const {
  1972. return placeholder;
  1973. }
  1974. void LineEdit::set_caret_column(int p_column) {
  1975. if (p_column > (int)text.length()) {
  1976. p_column = text.length();
  1977. }
  1978. if (p_column < 0) {
  1979. p_column = 0;
  1980. }
  1981. caret_column = p_column;
  1982. queue_accessibility_update();
  1983. // Fit to window.
  1984. if (!is_inside_tree()) {
  1985. scroll_offset = 0.0;
  1986. return;
  1987. }
  1988. Ref<StyleBox> style = theme_cache.normal;
  1989. bool rtl = is_layout_rtl();
  1990. int x_ofs = 0;
  1991. float text_width = TS->shaped_text_get_size(text_rid).x;
  1992. switch (alignment) {
  1993. case HORIZONTAL_ALIGNMENT_FILL:
  1994. case HORIZONTAL_ALIGNMENT_LEFT: {
  1995. if (rtl) {
  1996. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1997. } else {
  1998. x_ofs = style->get_margin(SIDE_LEFT);
  1999. }
  2000. } break;
  2001. case HORIZONTAL_ALIGNMENT_CENTER: {
  2002. if (!Math::is_zero_approx(scroll_offset)) {
  2003. x_ofs = style->get_margin(SIDE_LEFT);
  2004. } else {
  2005. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  2006. int centered = int((get_size().width - total_margin - text_width)) / 2;
  2007. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  2008. }
  2009. } break;
  2010. case HORIZONTAL_ALIGNMENT_RIGHT: {
  2011. if (rtl) {
  2012. x_ofs = style->get_margin(SIDE_LEFT);
  2013. } else {
  2014. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  2015. }
  2016. } break;
  2017. }
  2018. int ofs_max = get_size().width - style->get_margin(SIDE_RIGHT);
  2019. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  2020. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  2021. if (right_icon.is_valid() || display_clear_icon) {
  2022. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  2023. Point2 right_icon_size = _get_right_icon_size(r_icon);
  2024. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  2025. if (Math::is_zero_approx(scroll_offset)) {
  2026. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  2027. int center = int(get_size().width - total_margin - text_width - right_icon_size.width) / 2;
  2028. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  2029. }
  2030. if (rtl) {
  2031. x_ofs += right_icon_size.width;
  2032. }
  2033. } else {
  2034. if (rtl) {
  2035. x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs);
  2036. } else {
  2037. if (rtl) {
  2038. x_ofs = MAX(style->get_margin(SIDE_LEFT) + right_icon_size.width, x_ofs);
  2039. } else {
  2040. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - right_icon_size.width - style->get_margin(SIDE_RIGHT));
  2041. }
  2042. }
  2043. }
  2044. if (!rtl) {
  2045. ofs_max -= right_icon_size.width;
  2046. }
  2047. }
  2048. // Note: Use two coordinates to fit IME input range.
  2049. Vector2 primary_caret_offset = get_caret_pixel_pos();
  2050. if (MIN(primary_caret_offset.x, primary_caret_offset.y) <= x_ofs) {
  2051. scroll_offset += x_ofs - MIN(primary_caret_offset.x, primary_caret_offset.y);
  2052. } else if (MAX(primary_caret_offset.x, primary_caret_offset.y) >= ofs_max) {
  2053. scroll_offset += ofs_max - MAX(primary_caret_offset.x, primary_caret_offset.y);
  2054. }
  2055. // Scroll to show as much text as possible
  2056. if (text_width + scroll_offset + x_ofs < ofs_max) {
  2057. scroll_offset = ofs_max - x_ofs - text_width;
  2058. }
  2059. scroll_offset = MIN(0, scroll_offset);
  2060. queue_accessibility_update();
  2061. queue_redraw();
  2062. }
  2063. int LineEdit::get_caret_column() const {
  2064. return caret_column;
  2065. }
  2066. int LineEdit::get_next_composite_character_column(int p_column) const {
  2067. ERR_FAIL_INDEX_V(p_column, text.length() + 1, -1);
  2068. if (p_column == text.length()) {
  2069. return p_column;
  2070. } else {
  2071. return TS->shaped_text_next_character_pos(text_rid, p_column);
  2072. }
  2073. }
  2074. int LineEdit::get_previous_composite_character_column(int p_column) const {
  2075. ERR_FAIL_INDEX_V(p_column, text.length() + 1, -1);
  2076. if (p_column == 0) {
  2077. return 0;
  2078. } else {
  2079. return TS->shaped_text_prev_character_pos(text_rid, p_column);
  2080. }
  2081. }
  2082. void LineEdit::set_scroll_offset(float p_pos) {
  2083. scroll_offset = p_pos;
  2084. if (scroll_offset < 0.0) {
  2085. scroll_offset = 0.0;
  2086. }
  2087. }
  2088. float LineEdit::get_scroll_offset() const {
  2089. return scroll_offset;
  2090. }
  2091. void LineEdit::insert_text_at_caret(String p_text) {
  2092. if (max_length > 0) {
  2093. // Truncate text to append to fit in max_length, if needed.
  2094. int available_chars = max_length - text.length();
  2095. if (p_text.length() > available_chars) {
  2096. emit_signal(SNAME("text_change_rejected"), p_text.substr(available_chars));
  2097. p_text = p_text.substr(0, available_chars);
  2098. }
  2099. }
  2100. String pre = text.substr(0, caret_column);
  2101. String post = text.substr(caret_column);
  2102. text = pre + p_text + post;
  2103. _shape();
  2104. TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text_rid, caret_column, caret_column + p_text.length());
  2105. if (dir != TextServer::DIRECTION_AUTO) {
  2106. input_direction = (TextDirection)dir;
  2107. }
  2108. set_caret_column(caret_column + p_text.length());
  2109. if (!ime_text.is_empty()) {
  2110. _shape();
  2111. }
  2112. }
  2113. void LineEdit::clear_internal() {
  2114. deselect();
  2115. _clear_undo_stack();
  2116. caret_column = 0;
  2117. scroll_offset = 0.0;
  2118. undo_text = "";
  2119. text = "";
  2120. _shape();
  2121. queue_redraw();
  2122. }
  2123. Size2 LineEdit::get_minimum_size() const {
  2124. Ref<Font> font = theme_cache.font;
  2125. int font_size = theme_cache.font_size;
  2126. Size2 min_size;
  2127. // Minimum size of text.
  2128. // W is wider than M in most fonts, Using M may result in hiding the last digit when using float values in SpinBox, ie. ColorPicker RAW values.
  2129. float em_space_size = font->get_char_size('W', font_size).x;
  2130. min_size.width = theme_cache.minimum_character_width * em_space_size;
  2131. if (expand_to_text_length) {
  2132. // Ensure some space for the caret when placed at the end.
  2133. min_size.width = MAX(min_size.width, full_width + theme_cache.caret_width);
  2134. }
  2135. min_size.height = MAX(TS->shaped_text_get_size(text_rid).y, font->get_height(font_size));
  2136. // Take icons into account.
  2137. int icon_max_width = 0;
  2138. if (right_icon.is_valid()) {
  2139. Point2 right_icon_size = _get_right_icon_size(right_icon);
  2140. min_size.height = MAX(min_size.height, right_icon_size.height);
  2141. icon_max_width = right_icon_size.width;
  2142. }
  2143. if (clear_button_enabled) {
  2144. Point2 right_icon_size = _get_right_icon_size(theme_cache.clear_icon);
  2145. min_size.height = MAX(min_size.height, right_icon_size.height);
  2146. icon_max_width = MAX(icon_max_width, right_icon_size.width);
  2147. }
  2148. min_size.width += icon_max_width;
  2149. Size2 style_min_size = theme_cache.normal->get_minimum_size().max(theme_cache.read_only->get_minimum_size());
  2150. return style_min_size + min_size;
  2151. }
  2152. void LineEdit::deselect() {
  2153. selection.begin = 0;
  2154. selection.end = 0;
  2155. selection.start_column = 0;
  2156. selection.enabled = false;
  2157. selection.creating = false;
  2158. selection.double_click = false;
  2159. queue_accessibility_update();
  2160. queue_redraw();
  2161. }
  2162. bool LineEdit::has_selection() const {
  2163. return selection.enabled;
  2164. }
  2165. String LineEdit::get_selected_text() {
  2166. if (selection.enabled) {
  2167. return text.substr(selection.begin, selection.end - selection.begin);
  2168. } else {
  2169. return String();
  2170. }
  2171. }
  2172. int LineEdit::get_selection_from_column() const {
  2173. ERR_FAIL_COND_V(!selection.enabled, -1);
  2174. return selection.begin;
  2175. }
  2176. int LineEdit::get_selection_to_column() const {
  2177. ERR_FAIL_COND_V(!selection.enabled, -1);
  2178. return selection.end;
  2179. }
  2180. void LineEdit::selection_delete() {
  2181. if (selection.enabled) {
  2182. delete_text(selection.begin, selection.end);
  2183. }
  2184. deselect();
  2185. }
  2186. void LineEdit::set_max_length(int p_max_length) {
  2187. ERR_FAIL_COND(p_max_length < 0);
  2188. max_length = p_max_length;
  2189. set_text(text);
  2190. }
  2191. int LineEdit::get_max_length() const {
  2192. return max_length;
  2193. }
  2194. void LineEdit::selection_fill_at_caret() {
  2195. if (!selecting_enabled) {
  2196. return;
  2197. }
  2198. selection.begin = caret_column;
  2199. selection.end = selection.start_column;
  2200. if (selection.end < selection.begin) {
  2201. int aux = selection.end;
  2202. selection.end = selection.begin;
  2203. selection.begin = aux;
  2204. }
  2205. selection.enabled = (selection.begin != selection.end);
  2206. queue_accessibility_update();
  2207. }
  2208. void LineEdit::select_all() {
  2209. if (!selecting_enabled) {
  2210. return;
  2211. }
  2212. if (text.is_empty()) {
  2213. set_caret_column(0);
  2214. return;
  2215. }
  2216. selection.begin = 0;
  2217. selection.end = text.length();
  2218. selection.enabled = true;
  2219. queue_accessibility_update();
  2220. queue_redraw();
  2221. }
  2222. void LineEdit::set_editable(bool p_editable) {
  2223. if (editable == p_editable) {
  2224. return;
  2225. }
  2226. editable = p_editable;
  2227. if (!editable && editing) {
  2228. unedit();
  2229. emit_signal(SNAME("editing_toggled"), false);
  2230. }
  2231. if (editable && has_focus() && !editing) {
  2232. edit();
  2233. emit_signal(SNAME("editing_toggled"), true);
  2234. }
  2235. _validate_caret_can_draw();
  2236. update_minimum_size();
  2237. queue_accessibility_update();
  2238. queue_redraw();
  2239. }
  2240. bool LineEdit::is_editable() const {
  2241. return editable;
  2242. }
  2243. void LineEdit::set_secret(bool p_secret) {
  2244. if (pass == p_secret) {
  2245. return;
  2246. }
  2247. pass = p_secret;
  2248. _shape();
  2249. set_caret_column(caret_column); // Update scroll_offset.
  2250. queue_redraw();
  2251. }
  2252. bool LineEdit::is_secret() const {
  2253. return pass;
  2254. }
  2255. void LineEdit::set_secret_character(const String &p_string) {
  2256. String c = p_string;
  2257. if (c.length() > 1) {
  2258. WARN_PRINT("Secret character must be exactly one character long (" + itos(c.length()) + " characters given).");
  2259. c = c.left(1);
  2260. }
  2261. if (secret_character == c) {
  2262. return;
  2263. }
  2264. secret_character = c;
  2265. _shape();
  2266. set_caret_column(caret_column); // Update scroll_offset.
  2267. queue_redraw();
  2268. }
  2269. String LineEdit::get_secret_character() const {
  2270. return secret_character;
  2271. }
  2272. void LineEdit::select(int p_from, int p_to) {
  2273. if (!selecting_enabled) {
  2274. return;
  2275. }
  2276. if (p_from == 0 && p_to == 0) {
  2277. deselect();
  2278. return;
  2279. }
  2280. int len = text.length();
  2281. if (p_from < 0) {
  2282. p_from = 0;
  2283. }
  2284. if (p_from > len) {
  2285. p_from = len;
  2286. }
  2287. if (p_to < 0 || p_to > len) {
  2288. p_to = len;
  2289. }
  2290. if (p_from >= p_to) {
  2291. return;
  2292. }
  2293. selection.enabled = true;
  2294. selection.begin = p_from;
  2295. selection.end = p_to;
  2296. selection.creating = false;
  2297. selection.double_click = false;
  2298. queue_accessibility_update();
  2299. queue_redraw();
  2300. }
  2301. bool LineEdit::is_text_field() const {
  2302. return true;
  2303. }
  2304. void LineEdit::menu_option(int p_option) {
  2305. int prev_len = text.length();
  2306. bool inserted_control_char = false;
  2307. switch (p_option) {
  2308. case MENU_CUT: {
  2309. if (editable) {
  2310. cut_text();
  2311. }
  2312. } break;
  2313. case MENU_COPY: {
  2314. copy_text();
  2315. } break;
  2316. case MENU_PASTE: {
  2317. if (editable) {
  2318. paste_text();
  2319. }
  2320. } break;
  2321. case MENU_CLEAR: {
  2322. if (editable) {
  2323. clear();
  2324. }
  2325. } break;
  2326. case MENU_SELECT_ALL: {
  2327. select_all();
  2328. } break;
  2329. case MENU_UNDO: {
  2330. if (editable) {
  2331. undo();
  2332. }
  2333. } break;
  2334. case MENU_REDO: {
  2335. if (editable) {
  2336. redo();
  2337. }
  2338. } break;
  2339. case MENU_DIR_INHERITED: {
  2340. set_text_direction(TEXT_DIRECTION_INHERITED);
  2341. } break;
  2342. case MENU_DIR_AUTO: {
  2343. set_text_direction(TEXT_DIRECTION_AUTO);
  2344. } break;
  2345. case MENU_DIR_LTR: {
  2346. set_text_direction(TEXT_DIRECTION_LTR);
  2347. } break;
  2348. case MENU_DIR_RTL: {
  2349. set_text_direction(TEXT_DIRECTION_RTL);
  2350. } break;
  2351. case MENU_DISPLAY_UCC: {
  2352. set_draw_control_chars(!get_draw_control_chars());
  2353. } break;
  2354. case MENU_INSERT_LRM: {
  2355. if (editable) {
  2356. insert_text_at_caret(String::chr(0x200E));
  2357. inserted_control_char = true;
  2358. }
  2359. } break;
  2360. case MENU_INSERT_RLM: {
  2361. if (editable) {
  2362. insert_text_at_caret(String::chr(0x200F));
  2363. inserted_control_char = true;
  2364. }
  2365. } break;
  2366. case MENU_INSERT_LRE: {
  2367. if (editable) {
  2368. insert_text_at_caret(String::chr(0x202A));
  2369. inserted_control_char = true;
  2370. }
  2371. } break;
  2372. case MENU_INSERT_RLE: {
  2373. if (editable) {
  2374. insert_text_at_caret(String::chr(0x202B));
  2375. inserted_control_char = true;
  2376. }
  2377. } break;
  2378. case MENU_INSERT_LRO: {
  2379. if (editable) {
  2380. insert_text_at_caret(String::chr(0x202D));
  2381. inserted_control_char = true;
  2382. }
  2383. } break;
  2384. case MENU_INSERT_RLO: {
  2385. if (editable) {
  2386. insert_text_at_caret(String::chr(0x202E));
  2387. inserted_control_char = true;
  2388. }
  2389. } break;
  2390. case MENU_INSERT_PDF: {
  2391. if (editable) {
  2392. insert_text_at_caret(String::chr(0x202C));
  2393. inserted_control_char = true;
  2394. }
  2395. } break;
  2396. case MENU_INSERT_ALM: {
  2397. if (editable) {
  2398. insert_text_at_caret(String::chr(0x061C));
  2399. inserted_control_char = true;
  2400. }
  2401. } break;
  2402. case MENU_INSERT_LRI: {
  2403. if (editable) {
  2404. insert_text_at_caret(String::chr(0x2066));
  2405. inserted_control_char = true;
  2406. }
  2407. } break;
  2408. case MENU_INSERT_RLI: {
  2409. if (editable) {
  2410. insert_text_at_caret(String::chr(0x2067));
  2411. inserted_control_char = true;
  2412. }
  2413. } break;
  2414. case MENU_INSERT_FSI: {
  2415. if (editable) {
  2416. insert_text_at_caret(String::chr(0x2068));
  2417. inserted_control_char = true;
  2418. }
  2419. } break;
  2420. case MENU_INSERT_PDI: {
  2421. if (editable) {
  2422. insert_text_at_caret(String::chr(0x2069));
  2423. inserted_control_char = true;
  2424. }
  2425. } break;
  2426. case MENU_INSERT_ZWJ: {
  2427. if (editable) {
  2428. insert_text_at_caret(String::chr(0x200D));
  2429. inserted_control_char = true;
  2430. }
  2431. } break;
  2432. case MENU_INSERT_ZWNJ: {
  2433. if (editable) {
  2434. insert_text_at_caret(String::chr(0x200C));
  2435. inserted_control_char = true;
  2436. }
  2437. } break;
  2438. case MENU_INSERT_WJ: {
  2439. if (editable) {
  2440. insert_text_at_caret(String::chr(0x2060));
  2441. inserted_control_char = true;
  2442. }
  2443. } break;
  2444. case MENU_INSERT_SHY: {
  2445. if (editable) {
  2446. insert_text_at_caret(String::chr(0x00AD));
  2447. inserted_control_char = true;
  2448. }
  2449. } break;
  2450. case MENU_EMOJI_AND_SYMBOL: {
  2451. show_emoji_and_symbol_picker();
  2452. } break;
  2453. }
  2454. // Mirror paste/drag behavior, emit text_changed signal if a control character was inserted.
  2455. if (inserted_control_char && !text_changed_dirty) {
  2456. if (is_inside_tree() && text.length() != prev_len) {
  2457. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  2458. }
  2459. text_changed_dirty = true;
  2460. }
  2461. }
  2462. void LineEdit::set_context_menu_enabled(bool p_enable) {
  2463. context_menu_enabled = p_enable;
  2464. }
  2465. bool LineEdit::is_context_menu_enabled() {
  2466. return context_menu_enabled;
  2467. }
  2468. void LineEdit::show_emoji_and_symbol_picker() {
  2469. _update_ime_window_position();
  2470. DisplayServer::get_singleton()->show_emoji_and_symbol_picker();
  2471. }
  2472. void LineEdit::set_emoji_menu_enabled(bool p_enabled) {
  2473. if (emoji_menu_enabled != p_enabled) {
  2474. emoji_menu_enabled = p_enabled;
  2475. }
  2476. }
  2477. bool LineEdit::is_emoji_menu_enabled() const {
  2478. return emoji_menu_enabled;
  2479. }
  2480. void LineEdit::set_backspace_deletes_composite_character_enabled(bool p_enabled) {
  2481. backspace_deletes_composite_character_enabled = p_enabled;
  2482. }
  2483. bool LineEdit::is_backspace_deletes_composite_character_enabled() const {
  2484. return backspace_deletes_composite_character_enabled;
  2485. }
  2486. bool LineEdit::is_menu_visible() const {
  2487. return menu && menu->is_visible();
  2488. }
  2489. PopupMenu *LineEdit::get_menu() const {
  2490. if (!menu) {
  2491. const_cast<LineEdit *>(this)->_generate_context_menu();
  2492. }
  2493. return menu;
  2494. }
  2495. void LineEdit::_editor_settings_changed() {
  2496. #ifdef TOOLS_ENABLED
  2497. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/appearance/caret")) {
  2498. return;
  2499. }
  2500. set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  2501. set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  2502. #endif
  2503. }
  2504. void LineEdit::set_expand_to_text_length_enabled(bool p_enabled) {
  2505. expand_to_text_length = p_enabled;
  2506. update_minimum_size();
  2507. set_caret_column(caret_column);
  2508. }
  2509. bool LineEdit::is_expand_to_text_length_enabled() const {
  2510. return expand_to_text_length;
  2511. }
  2512. void LineEdit::set_clear_button_enabled(bool p_enabled) {
  2513. if (clear_button_enabled == p_enabled) {
  2514. return;
  2515. }
  2516. clear_button_enabled = p_enabled;
  2517. _fit_to_width();
  2518. update_minimum_size();
  2519. queue_redraw();
  2520. }
  2521. bool LineEdit::is_clear_button_enabled() const {
  2522. return clear_button_enabled;
  2523. }
  2524. void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
  2525. shortcut_keys_enabled = p_enabled;
  2526. }
  2527. bool LineEdit::is_shortcut_keys_enabled() const {
  2528. return shortcut_keys_enabled;
  2529. }
  2530. void LineEdit::set_virtual_keyboard_enabled(bool p_enable) {
  2531. virtual_keyboard_enabled = p_enable;
  2532. }
  2533. bool LineEdit::is_virtual_keyboard_enabled() const {
  2534. return virtual_keyboard_enabled;
  2535. }
  2536. void LineEdit::set_virtual_keyboard_show_on_focus(bool p_show_on_focus) {
  2537. virtual_keyboard_show_on_focus = p_show_on_focus;
  2538. }
  2539. bool LineEdit::get_virtual_keyboard_show_on_focus() const {
  2540. return virtual_keyboard_show_on_focus;
  2541. }
  2542. void LineEdit::set_virtual_keyboard_type(VirtualKeyboardType p_type) {
  2543. virtual_keyboard_type = p_type;
  2544. }
  2545. LineEdit::VirtualKeyboardType LineEdit::get_virtual_keyboard_type() const {
  2546. return virtual_keyboard_type;
  2547. }
  2548. void LineEdit::set_middle_mouse_paste_enabled(bool p_enabled) {
  2549. middle_mouse_paste_enabled = p_enabled;
  2550. }
  2551. bool LineEdit::is_middle_mouse_paste_enabled() const {
  2552. return middle_mouse_paste_enabled;
  2553. }
  2554. void LineEdit::set_selecting_enabled(bool p_enabled) {
  2555. if (selecting_enabled == p_enabled) {
  2556. return;
  2557. }
  2558. selecting_enabled = p_enabled;
  2559. if (!selecting_enabled) {
  2560. deselect();
  2561. }
  2562. }
  2563. bool LineEdit::is_selecting_enabled() const {
  2564. return selecting_enabled;
  2565. }
  2566. void LineEdit::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
  2567. if (deselect_on_focus_loss_enabled == p_enabled) {
  2568. return;
  2569. }
  2570. deselect_on_focus_loss_enabled = p_enabled;
  2571. if (p_enabled && selection.enabled && !has_focus()) {
  2572. deselect();
  2573. }
  2574. }
  2575. bool LineEdit::is_deselect_on_focus_loss_enabled() const {
  2576. return deselect_on_focus_loss_enabled;
  2577. }
  2578. void LineEdit::set_drag_and_drop_selection_enabled(const bool p_enabled) {
  2579. drag_and_drop_selection_enabled = p_enabled;
  2580. }
  2581. bool LineEdit::is_drag_and_drop_selection_enabled() const {
  2582. return drag_and_drop_selection_enabled;
  2583. }
  2584. void LineEdit::_texture_changed() {
  2585. _fit_to_width();
  2586. update_minimum_size();
  2587. queue_redraw();
  2588. }
  2589. void LineEdit::set_right_icon(const Ref<Texture2D> &p_icon) {
  2590. if (right_icon == p_icon) {
  2591. return;
  2592. }
  2593. if (right_icon.is_valid()) {
  2594. right_icon->disconnect_changed(callable_mp(this, &LineEdit::_texture_changed));
  2595. }
  2596. right_icon = p_icon;
  2597. if (right_icon.is_valid()) {
  2598. right_icon->connect_changed(callable_mp(this, &LineEdit::_texture_changed));
  2599. }
  2600. _fit_to_width();
  2601. update_minimum_size();
  2602. queue_redraw();
  2603. }
  2604. Ref<Texture2D> LineEdit::get_right_icon() {
  2605. return right_icon;
  2606. }
  2607. void LineEdit::set_icon_expand_mode(ExpandMode p_mode) {
  2608. if (icon_expand_mode == p_mode) {
  2609. return;
  2610. }
  2611. icon_expand_mode = p_mode;
  2612. queue_redraw();
  2613. update_minimum_size();
  2614. notify_property_list_changed();
  2615. }
  2616. LineEdit::ExpandMode LineEdit::get_icon_expand_mode() const {
  2617. return icon_expand_mode;
  2618. }
  2619. void LineEdit::set_right_icon_scale(float p_scale) {
  2620. if (right_icon_scale == p_scale) {
  2621. return;
  2622. }
  2623. right_icon_scale = p_scale;
  2624. queue_redraw();
  2625. update_minimum_size();
  2626. }
  2627. float LineEdit::get_right_icon_scale() const {
  2628. return right_icon_scale;
  2629. }
  2630. void LineEdit::set_flat(bool p_enabled) {
  2631. if (flat != p_enabled) {
  2632. flat = p_enabled;
  2633. queue_redraw();
  2634. }
  2635. }
  2636. bool LineEdit::is_flat() const {
  2637. return flat;
  2638. }
  2639. void LineEdit::set_select_all_on_focus(bool p_enabled) {
  2640. select_all_on_focus = p_enabled;
  2641. }
  2642. bool LineEdit::is_select_all_on_focus() const {
  2643. return select_all_on_focus;
  2644. }
  2645. void LineEdit::clear_pending_select_all_on_focus() {
  2646. pending_select_all_on_focus = false;
  2647. }
  2648. void LineEdit::_text_changed() {
  2649. _emit_text_change();
  2650. _clear_redo();
  2651. }
  2652. void LineEdit::_emit_text_change() {
  2653. emit_signal(SceneStringName(text_changed), text);
  2654. text_changed_dirty = false;
  2655. }
  2656. PackedStringArray LineEdit::get_configuration_warnings() const {
  2657. PackedStringArray warnings = Control::get_configuration_warnings();
  2658. if (secret_character.length() > 1) {
  2659. warnings.push_back("Secret Character property supports only one character. Extra characters will be ignored.");
  2660. }
  2661. return warnings;
  2662. }
  2663. void LineEdit::_shape() {
  2664. const Ref<Font> &font = theme_cache.font;
  2665. int font_size = theme_cache.font_size;
  2666. if (font.is_null()) {
  2667. return;
  2668. }
  2669. Size2 old_size = TS->shaped_text_get_size(text_rid);
  2670. TS->shaped_text_clear(text_rid);
  2671. String t;
  2672. if (text.is_empty() && ime_text.is_empty()) {
  2673. t = placeholder_translated;
  2674. } else if (pass) {
  2675. String s = secret_character.is_empty() ? U"•" : secret_character.left(1);
  2676. t = s.repeat(text.length() + ime_text.length());
  2677. } else {
  2678. if (!ime_text.is_empty()) {
  2679. t = text.substr(0, caret_column) + ime_text + text.substr(caret_column);
  2680. } else {
  2681. t = text;
  2682. }
  2683. }
  2684. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2685. TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  2686. } else {
  2687. TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
  2688. }
  2689. TS->shaped_text_set_preserve_control(text_rid, draw_control_chars);
  2690. const String &lang = language.is_empty() ? _get_locale() : language;
  2691. TS->shaped_text_add_string(text_rid, t, font->get_rids(), font_size, font->get_opentype_features(), lang);
  2692. TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, t));
  2693. full_width = TS->shaped_text_get_size(text_rid).x;
  2694. _fit_to_width();
  2695. Size2 size = TS->shaped_text_get_size(text_rid);
  2696. if ((expand_to_text_length && old_size.x != size.x) || (old_size.y != size.y)) {
  2697. update_minimum_size();
  2698. }
  2699. if (accessibility_text_root_element.is_valid()) {
  2700. DisplayServer::get_singleton()->accessibility_free_element(accessibility_text_root_element);
  2701. accessibility_text_root_element = RID();
  2702. }
  2703. queue_accessibility_update();
  2704. }
  2705. void LineEdit::_fit_to_width() {
  2706. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  2707. Ref<StyleBox> style = theme_cache.normal;
  2708. int t_width = get_size().width - style->get_margin(SIDE_RIGHT) - style->get_margin(SIDE_LEFT);
  2709. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  2710. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  2711. if (right_icon.is_valid() || display_clear_icon) {
  2712. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  2713. Point2 right_icon_size = _get_right_icon_size(r_icon);
  2714. t_width -= right_icon_size.width;
  2715. }
  2716. TS->shaped_text_fit_to_width(text_rid, MAX(t_width, full_width));
  2717. }
  2718. }
  2719. void LineEdit::_clear_redo() {
  2720. _create_undo_state();
  2721. if (undo_stack_pos == nullptr) {
  2722. return;
  2723. }
  2724. undo_stack_pos = undo_stack_pos->next();
  2725. while (undo_stack_pos) {
  2726. List<TextOperation>::Element *elem = undo_stack_pos;
  2727. undo_stack_pos = undo_stack_pos->next();
  2728. undo_stack.erase(elem);
  2729. }
  2730. _create_undo_state();
  2731. }
  2732. void LineEdit::_clear_undo_stack() {
  2733. undo_stack.clear();
  2734. undo_stack_pos = nullptr;
  2735. _create_undo_state();
  2736. }
  2737. void LineEdit::_create_undo_state() {
  2738. TextOperation op;
  2739. op.text = text;
  2740. op.caret_column = caret_column;
  2741. op.scroll_offset = scroll_offset;
  2742. undo_stack.push_back(op);
  2743. }
  2744. Key LineEdit::_get_menu_action_accelerator(const String &p_action) {
  2745. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  2746. if (!events) {
  2747. return Key::NONE;
  2748. }
  2749. // Use first event in the list for the accelerator.
  2750. const List<Ref<InputEvent>>::Element *first_event = events->front();
  2751. if (!first_event) {
  2752. return Key::NONE;
  2753. }
  2754. const Ref<InputEventKey> event = first_event->get();
  2755. if (event.is_null()) {
  2756. return Key::NONE;
  2757. }
  2758. // Use physical keycode if non-zero.
  2759. if (event->get_physical_keycode() != Key::NONE) {
  2760. return event->get_physical_keycode_with_modifiers();
  2761. } else {
  2762. return event->get_keycode_with_modifiers();
  2763. }
  2764. }
  2765. void LineEdit::_generate_context_menu() {
  2766. menu = memnew(PopupMenu);
  2767. add_child(menu, false, INTERNAL_MODE_FRONT);
  2768. menu_dir = memnew(PopupMenu);
  2769. menu_dir->add_radio_check_item(ETR("Same as Layout Direction"), MENU_DIR_INHERITED);
  2770. menu_dir->add_radio_check_item(ETR("Auto-Detect Direction"), MENU_DIR_AUTO);
  2771. menu_dir->add_radio_check_item(ETR("Left-to-Right"), MENU_DIR_LTR);
  2772. menu_dir->add_radio_check_item(ETR("Right-to-Left"), MENU_DIR_RTL);
  2773. menu_ctl = memnew(PopupMenu);
  2774. menu_ctl->add_item(ETR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM);
  2775. menu_ctl->add_item(ETR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM);
  2776. menu_ctl->add_item(ETR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE);
  2777. menu_ctl->add_item(ETR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE);
  2778. menu_ctl->add_item(ETR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO);
  2779. menu_ctl->add_item(ETR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO);
  2780. menu_ctl->add_item(ETR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF);
  2781. menu_ctl->add_separator();
  2782. menu_ctl->add_item(ETR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM);
  2783. menu_ctl->add_item(ETR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI);
  2784. menu_ctl->add_item(ETR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI);
  2785. menu_ctl->add_item(ETR("First Strong Isolate (FSI)"), MENU_INSERT_FSI);
  2786. menu_ctl->add_item(ETR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI);
  2787. menu_ctl->add_separator();
  2788. menu_ctl->add_item(ETR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ);
  2789. menu_ctl->add_item(ETR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  2790. menu_ctl->add_item(ETR("Word Joiner (WJ)"), MENU_INSERT_WJ);
  2791. menu_ctl->add_item(ETR("Soft Hyphen (SHY)"), MENU_INSERT_SHY);
  2792. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  2793. menu->add_item(ETR("Emoji & Symbols"), MENU_EMOJI_AND_SYMBOL);
  2794. menu->add_separator();
  2795. }
  2796. menu->add_item(ETR("Cut"), MENU_CUT);
  2797. menu->add_item(ETR("Copy"), MENU_COPY);
  2798. menu->add_item(ETR("Paste"), MENU_PASTE);
  2799. menu->add_separator();
  2800. menu->add_item(ETR("Select All"), MENU_SELECT_ALL);
  2801. menu->add_item(ETR("Clear"), MENU_CLEAR);
  2802. menu->add_separator();
  2803. menu->add_item(ETR("Undo"), MENU_UNDO);
  2804. menu->add_item(ETR("Redo"), MENU_REDO);
  2805. menu->add_separator();
  2806. menu->add_submenu_node_item(ETR("Text Writing Direction"), menu_dir, MENU_SUBMENU_TEXT_DIR);
  2807. menu->add_separator();
  2808. menu->add_check_item(ETR("Display Control Characters"), MENU_DISPLAY_UCC);
  2809. menu->add_submenu_node_item(ETR("Insert Control Character"), menu_ctl, MENU_SUBMENU_INSERT_UCC);
  2810. menu->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option));
  2811. menu_dir->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option));
  2812. menu_ctl->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option));
  2813. menu->connect(SceneStringName(focus_entered), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2814. menu->connect(SceneStringName(focus_exited), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2815. }
  2816. void LineEdit::_update_context_menu() {
  2817. if (!menu) {
  2818. _generate_context_menu();
  2819. }
  2820. int idx = -1;
  2821. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  2822. idx = m_menu->get_item_index(m_id); \
  2823. if (idx >= 0) { \
  2824. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2825. m_menu->set_item_disabled(idx, m_disabled); \
  2826. }
  2827. #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \
  2828. idx = m_menu->get_item_index(m_id); \
  2829. if (idx >= 0) { \
  2830. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2831. }
  2832. #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \
  2833. idx = m_menu->get_item_index(m_id); \
  2834. if (idx >= 0) { \
  2835. m_menu->set_item_disabled(idx, m_disabled); \
  2836. }
  2837. #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \
  2838. idx = m_menu->get_item_index(m_id); \
  2839. if (idx >= 0) { \
  2840. m_menu->set_item_checked(idx, m_checked); \
  2841. }
  2842. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  2843. MENU_ITEM_DISABLED(menu, MENU_EMOJI_AND_SYMBOL, !editable || !emoji_menu_enabled)
  2844. }
  2845. MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable)
  2846. MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy")
  2847. MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable)
  2848. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled)
  2849. MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable)
  2850. MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo())
  2851. MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo())
  2852. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED)
  2853. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO)
  2854. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR)
  2855. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL)
  2856. MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars)
  2857. MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable)
  2858. #undef MENU_ITEM_ACTION_DISABLED
  2859. #undef MENU_ITEM_ACTION
  2860. #undef MENU_ITEM_DISABLED
  2861. #undef MENU_ITEM_CHECKED
  2862. }
  2863. void LineEdit::_validate_property(PropertyInfo &p_property) const {
  2864. if (!Engine::get_singleton()->is_editor_hint()) {
  2865. return;
  2866. }
  2867. if (!caret_blink_enabled && p_property.name == "caret_blink_interval") {
  2868. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2869. } else if (icon_expand_mode != EXPAND_MODE_FIT_TO_LINE_EDIT && p_property.name == "right_icon_scale") {
  2870. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2871. }
  2872. }
  2873. void LineEdit::_bind_methods() {
  2874. // Private exposed API.
  2875. ClassDB::bind_method(D_METHOD("_set_text", "text", "emit_signal"), &LineEdit::_set_text, DEFVAL(false));
  2876. // Public API.
  2877. ClassDB::bind_method(D_METHOD("has_ime_text"), &LineEdit::has_ime_text);
  2878. ClassDB::bind_method(D_METHOD("cancel_ime"), &LineEdit::cancel_ime);
  2879. ClassDB::bind_method(D_METHOD("apply_ime"), &LineEdit::apply_ime);
  2880. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment);
  2881. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment);
  2882. ClassDB::bind_method(D_METHOD("edit", "hide_focus"), &LineEdit::edit, DEFVAL(false));
  2883. ClassDB::bind_method(D_METHOD("unedit"), &LineEdit::unedit);
  2884. ClassDB::bind_method(D_METHOD("is_editing"), &LineEdit::is_editing);
  2885. ClassDB::bind_method(D_METHOD("set_keep_editing_on_text_submit", "enable"), &LineEdit::set_keep_editing_on_text_submit);
  2886. ClassDB::bind_method(D_METHOD("is_editing_kept_on_text_submit"), &LineEdit::is_editing_kept_on_text_submit);
  2887. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  2888. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  2889. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  2890. ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect);
  2891. ClassDB::bind_method(D_METHOD("has_undo"), &LineEdit::has_undo);
  2892. ClassDB::bind_method(D_METHOD("has_redo"), &LineEdit::has_redo);
  2893. ClassDB::bind_method(D_METHOD("has_selection"), &LineEdit::has_selection);
  2894. ClassDB::bind_method(D_METHOD("get_selected_text"), &LineEdit::get_selected_text);
  2895. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &LineEdit::get_selection_from_column);
  2896. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &LineEdit::get_selection_to_column);
  2897. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  2898. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  2899. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &LineEdit::get_draw_control_chars);
  2900. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enable"), &LineEdit::set_draw_control_chars);
  2901. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LineEdit::set_text_direction);
  2902. ClassDB::bind_method(D_METHOD("get_text_direction"), &LineEdit::get_text_direction);
  2903. ClassDB::bind_method(D_METHOD("set_language", "language"), &LineEdit::set_language);
  2904. ClassDB::bind_method(D_METHOD("get_language"), &LineEdit::get_language);
  2905. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LineEdit::set_structured_text_bidi_override);
  2906. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LineEdit::get_structured_text_bidi_override);
  2907. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LineEdit::set_structured_text_bidi_override_options);
  2908. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LineEdit::get_structured_text_bidi_override_options);
  2909. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  2910. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  2911. ClassDB::bind_method(D_METHOD("set_caret_column", "position"), &LineEdit::set_caret_column);
  2912. ClassDB::bind_method(D_METHOD("get_caret_column"), &LineEdit::get_caret_column);
  2913. ClassDB::bind_method(D_METHOD("get_next_composite_character_column", "column"), &LineEdit::get_next_composite_character_column);
  2914. ClassDB::bind_method(D_METHOD("get_previous_composite_character_column", "column"), &LineEdit::get_previous_composite_character_column);
  2915. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset);
  2916. ClassDB::bind_method(D_METHOD("set_expand_to_text_length_enabled", "enabled"), &LineEdit::set_expand_to_text_length_enabled);
  2917. ClassDB::bind_method(D_METHOD("is_expand_to_text_length_enabled"), &LineEdit::is_expand_to_text_length_enabled);
  2918. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enabled"), &LineEdit::set_caret_blink_enabled);
  2919. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &LineEdit::is_caret_blink_enabled);
  2920. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &LineEdit::set_caret_mid_grapheme_enabled);
  2921. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &LineEdit::is_caret_mid_grapheme_enabled);
  2922. ClassDB::bind_method(D_METHOD("set_caret_force_displayed", "enabled"), &LineEdit::set_caret_force_displayed);
  2923. ClassDB::bind_method(D_METHOD("is_caret_force_displayed"), &LineEdit::is_caret_force_displayed);
  2924. ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &LineEdit::set_caret_blink_interval);
  2925. ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &LineEdit::get_caret_blink_interval);
  2926. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  2927. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  2928. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text"), &LineEdit::insert_text_at_caret);
  2929. ClassDB::bind_method(D_METHOD("delete_char_at_caret"), &LineEdit::delete_char);
  2930. ClassDB::bind_method(D_METHOD("delete_text", "from_column", "to_column"), &LineEdit::delete_text);
  2931. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  2932. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  2933. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  2934. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  2935. ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
  2936. ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
  2937. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  2938. // TODO: Properly handle popups when advanced GUI is disabled.
  2939. #ifndef ADVANCED_GUI_DISABLED
  2940. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  2941. #endif // ADVANCED_GUI_DISABLED
  2942. ClassDB::bind_method(D_METHOD("is_menu_visible"), &LineEdit::is_menu_visible);
  2943. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
  2944. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled);
  2945. ClassDB::bind_method(D_METHOD("set_emoji_menu_enabled", "enable"), &LineEdit::set_emoji_menu_enabled);
  2946. ClassDB::bind_method(D_METHOD("is_emoji_menu_enabled"), &LineEdit::is_emoji_menu_enabled);
  2947. ClassDB::bind_method(D_METHOD("set_backspace_deletes_composite_character_enabled", "enable"), &LineEdit::set_backspace_deletes_composite_character_enabled);
  2948. ClassDB::bind_method(D_METHOD("is_backspace_deletes_composite_character_enabled"), &LineEdit::is_backspace_deletes_composite_character_enabled);
  2949. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &LineEdit::set_virtual_keyboard_enabled);
  2950. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &LineEdit::is_virtual_keyboard_enabled);
  2951. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_show_on_focus", "show_on_focus"), &LineEdit::set_virtual_keyboard_show_on_focus);
  2952. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_show_on_focus"), &LineEdit::get_virtual_keyboard_show_on_focus);
  2953. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_type", "type"), &LineEdit::set_virtual_keyboard_type);
  2954. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_type"), &LineEdit::get_virtual_keyboard_type);
  2955. ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled);
  2956. ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled);
  2957. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled);
  2958. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled);
  2959. ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enable"), &LineEdit::set_middle_mouse_paste_enabled);
  2960. ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &LineEdit::is_middle_mouse_paste_enabled);
  2961. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled);
  2962. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled);
  2963. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &LineEdit::set_deselect_on_focus_loss_enabled);
  2964. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &LineEdit::is_deselect_on_focus_loss_enabled);
  2965. ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &LineEdit::set_drag_and_drop_selection_enabled);
  2966. ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &LineEdit::is_drag_and_drop_selection_enabled);
  2967. ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
  2968. ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
  2969. ClassDB::bind_method(D_METHOD("set_icon_expand_mode", "mode"), &LineEdit::set_icon_expand_mode);
  2970. ClassDB::bind_method(D_METHOD("get_icon_expand_mode"), &LineEdit::get_icon_expand_mode);
  2971. ClassDB::bind_method(D_METHOD("set_right_icon_scale", "scale"), &LineEdit::set_right_icon_scale);
  2972. ClassDB::bind_method(D_METHOD("get_right_icon_scale"), &LineEdit::get_right_icon_scale);
  2973. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &LineEdit::set_flat);
  2974. ClassDB::bind_method(D_METHOD("is_flat"), &LineEdit::is_flat);
  2975. ClassDB::bind_method(D_METHOD("set_select_all_on_focus", "enabled"), &LineEdit::set_select_all_on_focus);
  2976. ClassDB::bind_method(D_METHOD("is_select_all_on_focus"), &LineEdit::is_select_all_on_focus);
  2977. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text")));
  2978. ADD_SIGNAL(MethodInfo("text_change_rejected", PropertyInfo(Variant::STRING, "rejected_substring")));
  2979. ADD_SIGNAL(MethodInfo("text_submitted", PropertyInfo(Variant::STRING, "new_text")));
  2980. ADD_SIGNAL(MethodInfo("editing_toggled", PropertyInfo(Variant::BOOL, "toggled_on")));
  2981. BIND_ENUM_CONSTANT(MENU_CUT);
  2982. BIND_ENUM_CONSTANT(MENU_COPY);
  2983. BIND_ENUM_CONSTANT(MENU_PASTE);
  2984. BIND_ENUM_CONSTANT(MENU_CLEAR);
  2985. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  2986. BIND_ENUM_CONSTANT(MENU_UNDO);
  2987. BIND_ENUM_CONSTANT(MENU_REDO);
  2988. BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR);
  2989. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  2990. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  2991. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  2992. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  2993. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  2994. BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC);
  2995. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  2996. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  2997. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  2998. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  2999. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  3000. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  3001. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  3002. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  3003. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  3004. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  3005. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  3006. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  3007. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  3008. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  3009. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  3010. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  3011. BIND_ENUM_CONSTANT(MENU_EMOJI_AND_SYMBOL);
  3012. BIND_ENUM_CONSTANT(MENU_MAX);
  3013. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_DEFAULT);
  3014. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_MULTILINE);
  3015. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER);
  3016. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER_DECIMAL);
  3017. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PHONE);
  3018. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_EMAIL_ADDRESS);
  3019. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PASSWORD);
  3020. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_URL);
  3021. BIND_ENUM_CONSTANT(EXPAND_MODE_ORIGINAL_SIZE);
  3022. BIND_ENUM_CONSTANT(EXPAND_MODE_FIT_TO_TEXT);
  3023. BIND_ENUM_CONSTANT(EXPAND_MODE_FIT_TO_LINE_EDIT);
  3024. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  3025. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  3026. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  3027. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_max_length", "get_max_length");
  3028. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  3029. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_editing_on_text_submit"), "set_keep_editing_on_text_submit", "is_editing_kept_on_text_submit");
  3030. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length_enabled", "is_expand_to_text_length_enabled");
  3031. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  3032. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emoji_menu_enabled"), "set_emoji_menu_enabled", "is_emoji_menu_enabled");
  3033. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backspace_deletes_composite_character_enabled"), "set_backspace_deletes_composite_character_enabled", "is_backspace_deletes_composite_character_enabled");
  3034. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled");
  3035. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  3036. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled");
  3037. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  3038. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  3039. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled");
  3040. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  3041. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  3042. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");
  3043. ADD_GROUP("Virtual Keyboard", "virtual_keyboard_");
  3044. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  3045. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_show_on_focus"), "set_virtual_keyboard_show_on_focus", "get_virtual_keyboard_show_on_focus");
  3046. 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");
  3047. ADD_GROUP("Caret", "caret_");
  3048. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  3049. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_interval", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "set_caret_blink_interval", "get_caret_blink_interval");
  3050. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_column", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_caret_column", "get_caret_column");
  3051. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_force_displayed"), "set_caret_force_displayed", "is_caret_force_displayed");
  3052. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  3053. ADD_GROUP("Secret", "secret");
  3054. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret", PROPERTY_HINT_GROUP_ENABLE), "set_secret", "is_secret");
  3055. ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
  3056. ADD_GROUP("BiDi", "");
  3057. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  3058. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  3059. 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");
  3060. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  3061. ADD_GROUP("Icon", "");
  3062. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_right_icon", "get_right_icon");
  3063. ADD_PROPERTY(PropertyInfo(Variant::INT, "icon_expand_mode", PROPERTY_HINT_ENUM, "Original,Fit to Text,Fit to LineEdit"), "set_icon_expand_mode", "get_icon_expand_mode");
  3064. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "right_icon_scale", PROPERTY_HINT_RANGE, "0.1,1.0,0.01"), "set_right_icon_scale", "get_right_icon_scale");
  3065. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, normal);
  3066. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, read_only);
  3067. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, focus);
  3068. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, LineEdit, font);
  3069. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, LineEdit, font_size);
  3070. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_color);
  3071. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_uneditable_color);
  3072. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_selected_color);
  3073. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, LineEdit, font_outline_size, "outline_size");
  3074. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_outline_color);
  3075. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_placeholder_color);
  3076. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LineEdit, caret_width);
  3077. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, caret_color);
  3078. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LineEdit, minimum_character_width);
  3079. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, selection_color);
  3080. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, LineEdit, clear_icon, "clear");
  3081. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, clear_button_color);
  3082. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, clear_button_color_pressed);
  3083. ADD_CLASS_DEPENDENCY("PopupMenu");
  3084. }
  3085. LineEdit::LineEdit(const String &p_placeholder) {
  3086. text_rid = TS->create_shaped_text();
  3087. _create_undo_state();
  3088. deselect();
  3089. set_focus_mode(FOCUS_ALL);
  3090. set_default_cursor_shape(CURSOR_IBEAM);
  3091. set_mouse_filter(MOUSE_FILTER_STOP);
  3092. set_process_unhandled_key_input(true);
  3093. set_caret_blink_enabled(false);
  3094. set_placeholder(p_placeholder);
  3095. set_editable(true); // Initialize to opposite first, so we get past the early-out in set_editable.
  3096. }
  3097. LineEdit::~LineEdit() {
  3098. TS->free_rid(text_rid);
  3099. }