line_edit.cpp 106 KB

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