line_edit.cpp 95 KB

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