line_edit.cpp 107 KB

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