line_edit.cpp 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376
  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. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1080. int centered = int((size.width - total_margin - text_width)) / 2;
  1081. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1082. }
  1083. } break;
  1084. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1085. if (rtl) {
  1086. x_ofs = style->get_offset().x;
  1087. } else {
  1088. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  1089. }
  1090. } break;
  1091. }
  1092. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1093. if (right_icon.is_valid() || display_clear_icon) {
  1094. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1095. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1096. if (Math::is_zero_approx(scroll_offset)) {
  1097. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1098. int center = int(size.width - total_margin - text_width - r_icon->get_width()) / 2;
  1099. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1100. }
  1101. } else {
  1102. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1103. }
  1104. }
  1105. float text_off_x = x_ofs + scroll_offset;
  1106. if (accessibility_text_root_element.is_null()) {
  1107. accessibility_text_root_element = DisplayServer::get_singleton()->accessibility_create_sub_text_edit_elements(ae, using_placeholder ? RID() : text_rid, text_height);
  1108. }
  1109. Transform2D text_xform;
  1110. text_xform.set_origin(Vector2i(text_off_x, y_ofs));
  1111. DisplayServer::get_singleton()->accessibility_update_set_transform(accessibility_text_root_element, text_xform);
  1112. if (selection.enabled) {
  1113. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, accessibility_text_root_element, selection.begin, accessibility_text_root_element, selection.end);
  1114. } else {
  1115. DisplayServer::get_singleton()->accessibility_update_set_text_selection(ae, accessibility_text_root_element, caret_column, accessibility_text_root_element, caret_column);
  1116. }
  1117. } break;
  1118. case NOTIFICATION_RESIZED: {
  1119. _fit_to_width();
  1120. scroll_offset = 0.0;
  1121. set_caret_column(get_caret_column());
  1122. } break;
  1123. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  1124. case NOTIFICATION_THEME_CHANGED: {
  1125. _shape();
  1126. queue_redraw();
  1127. } break;
  1128. case NOTIFICATION_TRANSLATION_CHANGED: {
  1129. placeholder_translated = atr(placeholder);
  1130. _shape();
  1131. queue_redraw();
  1132. } break;
  1133. case NOTIFICATION_WM_WINDOW_FOCUS_IN: {
  1134. window_has_focus = true;
  1135. _validate_caret_can_draw();
  1136. queue_redraw();
  1137. } break;
  1138. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  1139. window_has_focus = false;
  1140. _validate_caret_can_draw();
  1141. queue_redraw();
  1142. } break;
  1143. case NOTIFICATION_INTERNAL_PROCESS: {
  1144. if (caret_blink_enabled && caret_can_draw) {
  1145. caret_blink_timer += get_process_delta_time();
  1146. if (caret_blink_timer >= caret_blink_interval) {
  1147. caret_blink_timer = 0.0;
  1148. _toggle_draw_caret();
  1149. }
  1150. }
  1151. } break;
  1152. case NOTIFICATION_DRAW: {
  1153. int width, height;
  1154. bool rtl = is_layout_rtl();
  1155. Size2 size = get_size();
  1156. width = size.width;
  1157. height = size.height;
  1158. RID ci = get_canvas_item();
  1159. Ref<StyleBox> style = theme_cache.normal;
  1160. if (!is_editable()) {
  1161. style = theme_cache.read_only;
  1162. }
  1163. Ref<Font> font = theme_cache.font;
  1164. if (!flat) {
  1165. style->draw(ci, Rect2(Point2(), size));
  1166. }
  1167. if (has_focus()) {
  1168. theme_cache.focus->draw(ci, Rect2(Point2(), size));
  1169. }
  1170. int x_ofs = 0;
  1171. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1172. float text_width = TS->shaped_text_get_size(text_rid).x;
  1173. float text_height = TS->shaped_text_get_size(text_rid).y;
  1174. switch (alignment) {
  1175. case HORIZONTAL_ALIGNMENT_FILL:
  1176. case HORIZONTAL_ALIGNMENT_LEFT: {
  1177. if (rtl) {
  1178. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  1179. } else {
  1180. x_ofs = style->get_offset().x;
  1181. }
  1182. } break;
  1183. case HORIZONTAL_ALIGNMENT_CENTER: {
  1184. if (!Math::is_zero_approx(scroll_offset)) {
  1185. x_ofs = style->get_offset().x;
  1186. } else {
  1187. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1188. int centered = int((size.width - total_margin - text_width)) / 2;
  1189. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1190. }
  1191. } break;
  1192. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1193. if (rtl) {
  1194. x_ofs = style->get_offset().x;
  1195. } else {
  1196. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(size.width - Math::ceil(style->get_margin(SIDE_RIGHT) + (text_width))));
  1197. }
  1198. } break;
  1199. }
  1200. int ofs_max = width - style->get_margin(SIDE_RIGHT);
  1201. int y_area = height - style->get_minimum_size().height;
  1202. int y_ofs = style->get_offset().y + (y_area - text_height) / 2;
  1203. Color selection_color = theme_cache.selection_color;
  1204. Color font_color;
  1205. if (is_editable()) {
  1206. font_color = theme_cache.font_color;
  1207. } else {
  1208. font_color = theme_cache.font_uneditable_color;
  1209. }
  1210. Color font_selected_color = theme_cache.font_selected_color;
  1211. Color caret_color = theme_cache.caret_color;
  1212. // Draw placeholder color.
  1213. if (using_placeholder) {
  1214. font_color = theme_cache.font_placeholder_color;
  1215. }
  1216. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1217. if (right_icon.is_valid() || display_clear_icon) {
  1218. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1219. Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9);
  1220. if (display_clear_icon) {
  1221. if (clear_button_status.press_attempt && clear_button_status.pressing_inside) {
  1222. color_icon = theme_cache.clear_button_color_pressed;
  1223. } else {
  1224. color_icon = theme_cache.clear_button_color;
  1225. }
  1226. }
  1227. r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(SIDE_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
  1228. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1229. if (Math::is_zero_approx(scroll_offset)) {
  1230. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1231. int center = int(size.width - total_margin - text_width - r_icon->get_width()) / 2;
  1232. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1233. }
  1234. } else {
  1235. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1236. }
  1237. ofs_max -= r_icon->get_width();
  1238. }
  1239. // Draw selections rects.
  1240. Vector2 ofs = Point2(x_ofs + scroll_offset, y_ofs);
  1241. if (selection.enabled) {
  1242. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, selection.begin, selection.end);
  1243. for (int i = 0; i < sel.size(); i++) {
  1244. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  1245. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  1246. continue;
  1247. }
  1248. if (rect.position.x < x_ofs) {
  1249. rect.size.x -= (x_ofs - rect.position.x);
  1250. rect.position.x = x_ofs;
  1251. }
  1252. if (rect.position.x + rect.size.x > ofs_max) {
  1253. rect.size.x = ofs_max - rect.position.x;
  1254. }
  1255. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, selection_color);
  1256. }
  1257. }
  1258. const Glyph *glyphs = TS->shaped_text_get_glyphs(text_rid);
  1259. int gl_size = TS->shaped_text_get_glyph_count(text_rid);
  1260. // Draw text.
  1261. ofs.y += TS->shaped_text_get_ascent(text_rid);
  1262. Color font_outline_color = theme_cache.font_outline_color;
  1263. int outline_size = theme_cache.font_outline_size;
  1264. if (outline_size > 0 && font_outline_color.a > 0) {
  1265. Vector2 oofs = ofs;
  1266. for (int i = 0; i < gl_size; i++) {
  1267. for (int j = 0; j < glyphs[i].repeat; j++) {
  1268. if (std::ceil(oofs.x) >= x_ofs && (oofs.x + glyphs[i].advance) <= ofs_max) {
  1269. if (glyphs[i].font_rid != RID()) {
  1270. 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);
  1271. }
  1272. }
  1273. oofs.x += glyphs[i].advance;
  1274. }
  1275. if (oofs.x >= ofs_max) {
  1276. break;
  1277. }
  1278. }
  1279. }
  1280. for (int i = 0; i < gl_size; i++) {
  1281. bool selected = selection.enabled && glyphs[i].start >= selection.begin && glyphs[i].end <= selection.end;
  1282. for (int j = 0; j < glyphs[i].repeat; j++) {
  1283. if (std::ceil(ofs.x) >= x_ofs && (ofs.x + glyphs[i].advance) <= ofs_max) {
  1284. if (glyphs[i].font_rid != RID()) {
  1285. 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);
  1286. } 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)) {
  1287. 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);
  1288. }
  1289. }
  1290. ofs.x += glyphs[i].advance;
  1291. }
  1292. if (ofs.x >= ofs_max) {
  1293. break;
  1294. }
  1295. }
  1296. // Draw carets.
  1297. ofs.x = x_ofs + scroll_offset;
  1298. if ((caret_can_draw && draw_caret) || drag_caret_force_displayed) {
  1299. // Prevent carets from disappearing at theme scales below 1.0 (if the caret width is 1).
  1300. const int caret_width = theme_cache.caret_width * MAX(1, theme_cache.base_scale);
  1301. if (ime_text.is_empty() || ime_selection.y == 0) {
  1302. // Normal caret.
  1303. CaretInfo caret = TS->shaped_text_get_carets(text_rid, ime_text.is_empty() ? caret_column : caret_column + ime_selection.x);
  1304. if (using_placeholder || (caret.l_caret == Rect2() && caret.t_caret == Rect2())) {
  1305. // No carets, add one at the start.
  1306. int h = theme_cache.font->get_height(theme_cache.font_size);
  1307. int y = style->get_offset().y + (y_area - h) / 2;
  1308. caret.l_dir = (rtl) ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR;
  1309. switch (alignment) {
  1310. case HORIZONTAL_ALIGNMENT_FILL:
  1311. case HORIZONTAL_ALIGNMENT_LEFT: {
  1312. if (rtl) {
  1313. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  1314. } else {
  1315. caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h));
  1316. }
  1317. } break;
  1318. case HORIZONTAL_ALIGNMENT_CENTER: {
  1319. int icon_width = 0;
  1320. if (right_icon.is_valid()) {
  1321. icon_width = right_icon->get_width();
  1322. }
  1323. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1324. int center = int(size.width - total_margin - icon_width) / 2;
  1325. caret.l_caret = Rect2(Vector2(style->get_margin(SIDE_LEFT) + MAX(0, center), y), Size2(caret_width, h));
  1326. } break;
  1327. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1328. if (rtl) {
  1329. caret.l_caret = Rect2(Vector2(style->get_offset().x, y), Size2(caret_width, h));
  1330. } else {
  1331. caret.l_caret = Rect2(Vector2(ofs_max, y), Size2(caret_width, h));
  1332. }
  1333. } break;
  1334. }
  1335. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  1336. } else {
  1337. if (caret.l_caret != Rect2() && caret.l_dir == TextServer::DIRECTION_AUTO) {
  1338. // Draw extra marker on top of mid caret.
  1339. Rect2 trect = Rect2(caret.l_caret.position.x - 2.5 * caret_width, caret.l_caret.position.y, 6 * caret_width, caret_width);
  1340. trect.position += ofs;
  1341. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1342. } else if (caret.l_caret != Rect2() && caret.t_caret != Rect2() && caret.l_dir != caret.t_dir) {
  1343. // Draw extra direction marker on top of split caret.
  1344. float d = (caret.l_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1345. 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);
  1346. trect.position += ofs;
  1347. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1348. d = (caret.t_dir == TextServer::DIRECTION_LTR) ? 0.5 : -3;
  1349. trect = Rect2(caret.t_caret.position.x + d * caret_width, caret.t_caret.position.y, 3 * caret_width, caret_width);
  1350. trect.position += ofs;
  1351. RenderingServer::get_singleton()->canvas_item_add_rect(ci, trect, caret_color);
  1352. }
  1353. caret.l_caret.position += ofs;
  1354. caret.l_caret.size.x = caret_width;
  1355. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.l_caret, caret_color);
  1356. caret.t_caret.position += ofs;
  1357. caret.t_caret.size.x = caret_width;
  1358. RenderingServer::get_singleton()->canvas_item_add_rect(ci, caret.t_caret, caret_color);
  1359. }
  1360. }
  1361. if (!ime_text.is_empty()) {
  1362. {
  1363. // IME intermediate text range.
  1364. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column, caret_column + ime_text.length());
  1365. for (int i = 0; i < sel.size(); i++) {
  1366. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  1367. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  1368. continue;
  1369. }
  1370. if (rect.position.x < x_ofs) {
  1371. rect.size.x -= (x_ofs - rect.position.x);
  1372. rect.position.x = x_ofs;
  1373. } else if (rect.position.x + rect.size.x > ofs_max) {
  1374. rect.size.x = ofs_max - rect.position.x;
  1375. }
  1376. rect.size.y = caret_width;
  1377. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  1378. }
  1379. }
  1380. {
  1381. // IME caret.
  1382. if (ime_selection.y > 0) {
  1383. Vector<Vector2> sel = TS->shaped_text_get_selection(text_rid, caret_column + ime_selection.x, caret_column + ime_selection.x + ime_selection.y);
  1384. for (int i = 0; i < sel.size(); i++) {
  1385. Rect2 rect = Rect2(sel[i].x + ofs.x, ofs.y, sel[i].y - sel[i].x, text_height);
  1386. if (rect.position.x + rect.size.x <= x_ofs || rect.position.x > ofs_max) {
  1387. continue;
  1388. }
  1389. if (rect.position.x < x_ofs) {
  1390. rect.size.x -= (x_ofs - rect.position.x);
  1391. rect.position.x = x_ofs;
  1392. } else if (rect.position.x + rect.size.x > ofs_max) {
  1393. rect.size.x = ofs_max - rect.position.x;
  1394. }
  1395. rect.size.y = caret_width * 3;
  1396. RenderingServer::get_singleton()->canvas_item_add_rect(ci, rect, caret_color);
  1397. }
  1398. }
  1399. }
  1400. }
  1401. }
  1402. if (editing) {
  1403. _update_ime_window_position();
  1404. }
  1405. } break;
  1406. case NOTIFICATION_FOCUS_ENTER: {
  1407. // Only allow editing if the LineEdit is not focused with arrow keys.
  1408. 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"))) {
  1409. _edit(virtual_keyboard_show_on_focus);
  1410. emit_signal(SNAME("editing_toggled"), true);
  1411. }
  1412. } break;
  1413. case NOTIFICATION_FOCUS_EXIT: {
  1414. if (editing) {
  1415. unedit();
  1416. emit_signal(SNAME("editing_toggled"), false);
  1417. }
  1418. } break;
  1419. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  1420. if (editing) {
  1421. const String &new_ime_text = DisplayServer::get_singleton()->ime_get_text();
  1422. const Vector2i &new_ime_selection = DisplayServer::get_singleton()->ime_get_selection();
  1423. if (ime_text == new_ime_text && ime_selection == new_ime_selection) {
  1424. break;
  1425. }
  1426. ime_text = new_ime_text;
  1427. ime_selection = new_ime_selection;
  1428. if (!ime_text.is_empty()) {
  1429. selection_delete();
  1430. }
  1431. _shape();
  1432. set_caret_column(caret_column); // Update scroll_offset.
  1433. }
  1434. } break;
  1435. case NOTIFICATION_DRAG_BEGIN: {
  1436. drag_action = true;
  1437. } break;
  1438. case NOTIFICATION_DRAG_END: {
  1439. if (is_drag_successful()) {
  1440. if (selection.drag_attempt) {
  1441. selection.drag_attempt = false;
  1442. if (is_editable() && !Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL)) {
  1443. selection_delete();
  1444. } else if (deselect_on_focus_loss_enabled) {
  1445. deselect();
  1446. }
  1447. }
  1448. } else {
  1449. selection.drag_attempt = false;
  1450. }
  1451. drag_action = false;
  1452. drag_caret_force_displayed = false;
  1453. queue_redraw();
  1454. } break;
  1455. case NOTIFICATION_MOUSE_EXIT: {
  1456. if (drag_caret_force_displayed) {
  1457. drag_caret_force_displayed = false;
  1458. queue_redraw();
  1459. }
  1460. } break;
  1461. }
  1462. }
  1463. void LineEdit::copy_text() {
  1464. if (selection.enabled && !pass) {
  1465. DisplayServer::get_singleton()->clipboard_set(get_selected_text());
  1466. }
  1467. }
  1468. void LineEdit::cut_text() {
  1469. if (editable && selection.enabled && !pass) {
  1470. DisplayServer::get_singleton()->clipboard_set(get_selected_text());
  1471. selection_delete();
  1472. }
  1473. }
  1474. void LineEdit::paste_text() {
  1475. if (!editable) {
  1476. return;
  1477. }
  1478. // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
  1479. String paste_buffer = DisplayServer::get_singleton()->clipboard_get().strip_escapes();
  1480. if (!paste_buffer.is_empty()) {
  1481. int prev_len = text.length();
  1482. if (selection.enabled) {
  1483. selection_delete();
  1484. }
  1485. insert_text_at_caret(paste_buffer);
  1486. if (!text_changed_dirty) {
  1487. if (is_inside_tree() && text.length() != prev_len) {
  1488. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  1489. }
  1490. text_changed_dirty = true;
  1491. }
  1492. }
  1493. }
  1494. bool LineEdit::has_undo() const {
  1495. if (undo_stack_pos == nullptr) {
  1496. return undo_stack.size() > 1;
  1497. }
  1498. return undo_stack_pos != undo_stack.front();
  1499. }
  1500. bool LineEdit::has_redo() const {
  1501. return undo_stack_pos != nullptr && undo_stack_pos != undo_stack.back();
  1502. }
  1503. void LineEdit::undo() {
  1504. if (!editable) {
  1505. return;
  1506. }
  1507. if (!has_undo()) {
  1508. return;
  1509. }
  1510. if (undo_stack_pos == nullptr) {
  1511. undo_stack_pos = undo_stack.back();
  1512. }
  1513. deselect();
  1514. undo_stack_pos = undo_stack_pos->prev();
  1515. TextOperation op = undo_stack_pos->get();
  1516. text = op.text;
  1517. scroll_offset = op.scroll_offset;
  1518. _shape();
  1519. set_caret_column(op.caret_column);
  1520. _emit_text_change();
  1521. }
  1522. void LineEdit::redo() {
  1523. if (!editable) {
  1524. return;
  1525. }
  1526. if (!has_redo()) {
  1527. return;
  1528. }
  1529. deselect();
  1530. undo_stack_pos = undo_stack_pos->next();
  1531. TextOperation op = undo_stack_pos->get();
  1532. text = op.text;
  1533. scroll_offset = op.scroll_offset;
  1534. _shape();
  1535. set_caret_column(op.caret_column);
  1536. _emit_text_change();
  1537. }
  1538. void LineEdit::shift_selection_check_pre(bool p_shift) {
  1539. if (!selection.enabled && p_shift) {
  1540. selection.start_column = caret_column;
  1541. }
  1542. if (!p_shift) {
  1543. deselect();
  1544. }
  1545. }
  1546. void LineEdit::shift_selection_check_post(bool p_shift) {
  1547. if (p_shift) {
  1548. selection_fill_at_caret();
  1549. }
  1550. }
  1551. void LineEdit::set_caret_at_pixel_pos(int p_x) {
  1552. Ref<StyleBox> style = theme_cache.normal;
  1553. bool rtl = is_layout_rtl();
  1554. int x_ofs = 0;
  1555. float text_width = TS->shaped_text_get_size(text_rid).x;
  1556. switch (alignment) {
  1557. case HORIZONTAL_ALIGNMENT_FILL:
  1558. case HORIZONTAL_ALIGNMENT_LEFT: {
  1559. if (rtl) {
  1560. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1561. } else {
  1562. x_ofs = style->get_offset().x;
  1563. }
  1564. } break;
  1565. case HORIZONTAL_ALIGNMENT_CENTER: {
  1566. if (!Math::is_zero_approx(scroll_offset)) {
  1567. x_ofs = style->get_offset().x;
  1568. } else {
  1569. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1570. int centered = int((get_size().width - total_margin - text_width)) / 2;
  1571. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1572. }
  1573. } break;
  1574. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1575. if (rtl) {
  1576. x_ofs = style->get_offset().x;
  1577. } else {
  1578. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1579. }
  1580. } break;
  1581. }
  1582. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1583. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1584. if (right_icon.is_valid() || display_clear_icon) {
  1585. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1586. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1587. if (Math::is_zero_approx(scroll_offset)) {
  1588. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1589. int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2;
  1590. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1591. }
  1592. } else {
  1593. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1594. }
  1595. }
  1596. int ofs = std::ceil(TS->shaped_text_hit_test_position(text_rid, p_x - x_ofs - scroll_offset));
  1597. if (ofs == -1) {
  1598. return;
  1599. }
  1600. if (!caret_mid_grapheme_enabled) {
  1601. ofs = TS->shaped_text_closest_character_pos(text_rid, ofs);
  1602. }
  1603. set_caret_column(ofs);
  1604. }
  1605. Vector2 LineEdit::get_caret_pixel_pos() {
  1606. Ref<StyleBox> style = theme_cache.normal;
  1607. bool rtl = is_layout_rtl();
  1608. int x_ofs = 0;
  1609. float text_width = TS->shaped_text_get_size(text_rid).x;
  1610. switch (alignment) {
  1611. case HORIZONTAL_ALIGNMENT_FILL:
  1612. case HORIZONTAL_ALIGNMENT_LEFT: {
  1613. if (rtl) {
  1614. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1615. } else {
  1616. x_ofs = style->get_offset().x;
  1617. }
  1618. } break;
  1619. case HORIZONTAL_ALIGNMENT_CENTER: {
  1620. if (!Math::is_zero_approx(scroll_offset)) {
  1621. x_ofs = style->get_offset().x;
  1622. } else {
  1623. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1624. int centered = int((get_size().width - total_margin - text_width)) / 2;
  1625. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1626. }
  1627. } break;
  1628. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1629. if (rtl) {
  1630. x_ofs = style->get_offset().x;
  1631. } else {
  1632. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1633. }
  1634. } break;
  1635. }
  1636. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1637. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1638. if (right_icon.is_valid() || display_clear_icon) {
  1639. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1640. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1641. if (Math::is_zero_approx(scroll_offset)) {
  1642. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1643. int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2;
  1644. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1645. }
  1646. } else {
  1647. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1648. }
  1649. }
  1650. Vector2 ret;
  1651. CaretInfo caret;
  1652. // Get position of the start of caret.
  1653. if (!ime_text.is_empty() && ime_selection.x != 0) {
  1654. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x);
  1655. } else {
  1656. caret = TS->shaped_text_get_carets(text_rid, caret_column);
  1657. }
  1658. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1659. ret.x = x_ofs + caret.l_caret.position.x + scroll_offset;
  1660. } else {
  1661. ret.x = x_ofs + caret.t_caret.position.x + scroll_offset;
  1662. }
  1663. // Get position of the end of caret.
  1664. if (!ime_text.is_empty()) {
  1665. if (ime_selection.y != 0) {
  1666. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_selection.x + ime_selection.y);
  1667. } else {
  1668. caret = TS->shaped_text_get_carets(text_rid, caret_column + ime_text.size());
  1669. }
  1670. if ((caret.l_caret != Rect2() && (caret.l_dir == TextServer::DIRECTION_AUTO || caret.l_dir == (TextServer::Direction)input_direction)) || (caret.t_caret == Rect2())) {
  1671. ret.y = x_ofs + caret.l_caret.position.x + scroll_offset;
  1672. } else {
  1673. ret.y = x_ofs + caret.t_caret.position.x + scroll_offset;
  1674. }
  1675. } else {
  1676. ret.y = ret.x;
  1677. }
  1678. return ret;
  1679. }
  1680. void LineEdit::set_caret_mid_grapheme_enabled(const bool p_enabled) {
  1681. caret_mid_grapheme_enabled = p_enabled;
  1682. }
  1683. bool LineEdit::is_caret_mid_grapheme_enabled() const {
  1684. return caret_mid_grapheme_enabled;
  1685. }
  1686. bool LineEdit::is_caret_blink_enabled() const {
  1687. return caret_blink_enabled;
  1688. }
  1689. void LineEdit::set_caret_blink_enabled(const bool p_enabled) {
  1690. if (caret_blink_enabled == p_enabled) {
  1691. return;
  1692. }
  1693. caret_blink_enabled = p_enabled;
  1694. set_process_internal(p_enabled);
  1695. draw_caret = !caret_blink_enabled;
  1696. if (caret_blink_enabled) {
  1697. caret_blink_timer = 0.0;
  1698. }
  1699. queue_redraw();
  1700. notify_property_list_changed();
  1701. }
  1702. bool LineEdit::is_caret_force_displayed() const {
  1703. return caret_force_displayed;
  1704. }
  1705. void LineEdit::set_caret_force_displayed(const bool p_enabled) {
  1706. if (caret_force_displayed == p_enabled) {
  1707. return;
  1708. }
  1709. caret_force_displayed = p_enabled;
  1710. _validate_caret_can_draw();
  1711. queue_redraw();
  1712. }
  1713. float LineEdit::get_caret_blink_interval() const {
  1714. return caret_blink_interval;
  1715. }
  1716. void LineEdit::set_caret_blink_interval(const float p_interval) {
  1717. ERR_FAIL_COND(p_interval <= 0);
  1718. caret_blink_interval = p_interval;
  1719. }
  1720. void LineEdit::_reset_caret_blink_timer() {
  1721. if (caret_blink_enabled) {
  1722. draw_caret = true;
  1723. if (caret_can_draw) {
  1724. caret_blink_timer = 0.0;
  1725. queue_redraw();
  1726. }
  1727. }
  1728. }
  1729. void LineEdit::_toggle_draw_caret() {
  1730. draw_caret = !draw_caret;
  1731. if (is_visible_in_tree() && caret_can_draw) {
  1732. queue_redraw();
  1733. }
  1734. }
  1735. void LineEdit::_validate_caret_can_draw() {
  1736. if (caret_blink_enabled) {
  1737. draw_caret = true;
  1738. caret_blink_timer = 0.0;
  1739. }
  1740. caret_can_draw = (caret_force_displayed && !is_part_of_edited_scene()) || (editing && (window_has_focus || (menu && menu->has_focus())) && has_focus());
  1741. }
  1742. void LineEdit::delete_char() {
  1743. if (text.is_empty() || caret_column == 0) {
  1744. return;
  1745. }
  1746. int delete_char_offset = 1;
  1747. if (!caret_mid_grapheme_enabled && backspace_deletes_composite_character_enabled) {
  1748. delete_char_offset = caret_column - get_previous_composite_character_column(caret_column);
  1749. }
  1750. text = text.left(caret_column - delete_char_offset) + text.substr(caret_column);
  1751. _shape();
  1752. set_caret_column(get_caret_column() - delete_char_offset);
  1753. _text_changed();
  1754. }
  1755. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  1756. ERR_FAIL_COND_MSG(p_from_column < 0 || p_from_column > p_to_column || p_to_column > text.length(),
  1757. vformat("Positional parameters (from: %d, to: %d) are inverted or outside the text length (%d).", p_from_column, p_to_column, text.length()));
  1758. text = text.left(p_from_column) + text.substr(p_to_column);
  1759. _shape();
  1760. set_caret_column(caret_column - CLAMP(caret_column - p_from_column, 0, p_to_column - p_from_column));
  1761. if (!text_changed_dirty) {
  1762. if (is_inside_tree()) {
  1763. callable_mp(this, &LineEdit::_text_changed).call_deferred();
  1764. }
  1765. text_changed_dirty = true;
  1766. }
  1767. }
  1768. void LineEdit::set_text(String p_text) {
  1769. clear_internal();
  1770. insert_text_at_caret(p_text);
  1771. _create_undo_state();
  1772. queue_redraw();
  1773. caret_column = 0;
  1774. scroll_offset = 0.0;
  1775. }
  1776. void LineEdit::set_text_with_selection(const String &p_text) {
  1777. Selection selection_copy = selection;
  1778. clear_internal();
  1779. insert_text_at_caret(p_text);
  1780. _create_undo_state();
  1781. int tlen = text.length();
  1782. selection = selection_copy;
  1783. selection.begin = MIN(selection.begin, tlen);
  1784. selection.end = MIN(selection.end, tlen);
  1785. selection.start_column = MIN(selection.start_column, tlen);
  1786. queue_redraw();
  1787. }
  1788. void LineEdit::set_text_direction(Control::TextDirection p_text_direction) {
  1789. ERR_FAIL_COND((int)p_text_direction < -1 || (int)p_text_direction > 3);
  1790. if (text_direction != p_text_direction) {
  1791. text_direction = p_text_direction;
  1792. if (text_direction != TEXT_DIRECTION_AUTO && text_direction != TEXT_DIRECTION_INHERITED) {
  1793. input_direction = text_direction;
  1794. }
  1795. _shape();
  1796. if (menu_dir) {
  1797. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_INHERITED), text_direction == TEXT_DIRECTION_INHERITED);
  1798. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_AUTO), text_direction == TEXT_DIRECTION_AUTO);
  1799. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_LTR), text_direction == TEXT_DIRECTION_LTR);
  1800. menu_dir->set_item_checked(menu_dir->get_item_index(MENU_DIR_RTL), text_direction == TEXT_DIRECTION_RTL);
  1801. }
  1802. queue_redraw();
  1803. }
  1804. }
  1805. Control::TextDirection LineEdit::get_text_direction() const {
  1806. return text_direction;
  1807. }
  1808. void LineEdit::set_language(const String &p_language) {
  1809. if (language != p_language) {
  1810. language = p_language;
  1811. _shape();
  1812. queue_redraw();
  1813. }
  1814. }
  1815. String LineEdit::get_language() const {
  1816. return language;
  1817. }
  1818. void LineEdit::set_draw_control_chars(bool p_draw_control_chars) {
  1819. if (draw_control_chars != p_draw_control_chars) {
  1820. draw_control_chars = p_draw_control_chars;
  1821. if (menu && menu->get_item_index(MENU_DISPLAY_UCC) >= 0) {
  1822. menu->set_item_checked(menu->get_item_index(MENU_DISPLAY_UCC), draw_control_chars);
  1823. }
  1824. _shape();
  1825. queue_redraw();
  1826. }
  1827. }
  1828. bool LineEdit::get_draw_control_chars() const {
  1829. return draw_control_chars;
  1830. }
  1831. void LineEdit::set_structured_text_bidi_override(TextServer::StructuredTextParser p_parser) {
  1832. if (st_parser != p_parser) {
  1833. st_parser = p_parser;
  1834. _shape();
  1835. queue_redraw();
  1836. }
  1837. }
  1838. TextServer::StructuredTextParser LineEdit::get_structured_text_bidi_override() const {
  1839. return st_parser;
  1840. }
  1841. void LineEdit::set_structured_text_bidi_override_options(Array p_args) {
  1842. st_args = p_args;
  1843. _shape();
  1844. queue_redraw();
  1845. }
  1846. Array LineEdit::get_structured_text_bidi_override_options() const {
  1847. return st_args;
  1848. }
  1849. void LineEdit::clear() {
  1850. bool was_empty = text.is_empty();
  1851. clear_internal();
  1852. _clear_redo();
  1853. if (!was_empty) {
  1854. _emit_text_change();
  1855. }
  1856. // This should reset virtual keyboard state if needed.
  1857. if (editing) {
  1858. show_virtual_keyboard();
  1859. }
  1860. }
  1861. void LineEdit::show_virtual_keyboard() {
  1862. _update_ime_window_position();
  1863. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_VIRTUAL_KEYBOARD) && virtual_keyboard_enabled) {
  1864. if (selection.enabled) {
  1865. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, selection.begin, selection.end);
  1866. } else {
  1867. DisplayServer::get_singleton()->virtual_keyboard_show(text, get_global_rect(), DisplayServer::VirtualKeyboardType(virtual_keyboard_type), max_length, caret_column);
  1868. }
  1869. }
  1870. }
  1871. String LineEdit::get_text() const {
  1872. return text;
  1873. }
  1874. void LineEdit::set_placeholder(String p_text) {
  1875. if (placeholder == p_text) {
  1876. return;
  1877. }
  1878. placeholder = p_text;
  1879. placeholder_translated = atr(placeholder);
  1880. _shape();
  1881. queue_redraw();
  1882. }
  1883. String LineEdit::get_placeholder() const {
  1884. return placeholder;
  1885. }
  1886. void LineEdit::set_caret_column(int p_column) {
  1887. if (p_column > (int)text.length()) {
  1888. p_column = text.length();
  1889. }
  1890. if (p_column < 0) {
  1891. p_column = 0;
  1892. }
  1893. caret_column = p_column;
  1894. queue_accessibility_update();
  1895. // Fit to window.
  1896. if (!is_inside_tree()) {
  1897. scroll_offset = 0.0;
  1898. return;
  1899. }
  1900. Ref<StyleBox> style = theme_cache.normal;
  1901. bool rtl = is_layout_rtl();
  1902. int x_ofs = 0;
  1903. float text_width = TS->shaped_text_get_size(text_rid).x;
  1904. switch (alignment) {
  1905. case HORIZONTAL_ALIGNMENT_FILL:
  1906. case HORIZONTAL_ALIGNMENT_LEFT: {
  1907. if (rtl) {
  1908. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1909. } else {
  1910. x_ofs = style->get_offset().x;
  1911. }
  1912. } break;
  1913. case HORIZONTAL_ALIGNMENT_CENTER: {
  1914. if (!Math::is_zero_approx(scroll_offset)) {
  1915. x_ofs = style->get_offset().x;
  1916. } else {
  1917. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1918. int centered = int((get_size().width - total_margin - text_width)) / 2;
  1919. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, centered);
  1920. }
  1921. } break;
  1922. case HORIZONTAL_ALIGNMENT_RIGHT: {
  1923. if (rtl) {
  1924. x_ofs = style->get_offset().x;
  1925. } else {
  1926. x_ofs = MAX(style->get_margin(SIDE_LEFT), int(get_size().width - style->get_margin(SIDE_RIGHT) - (text_width)));
  1927. }
  1928. } break;
  1929. }
  1930. int ofs_max = get_size().width - style->get_margin(SIDE_RIGHT);
  1931. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  1932. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  1933. if (right_icon.is_valid() || display_clear_icon) {
  1934. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  1935. if (alignment == HORIZONTAL_ALIGNMENT_CENTER) {
  1936. if (Math::is_zero_approx(scroll_offset)) {
  1937. int total_margin = style->get_margin(SIDE_LEFT) + style->get_margin(SIDE_RIGHT);
  1938. int center = int(get_size().width - total_margin - text_width - r_icon->get_width()) / 2;
  1939. x_ofs = style->get_margin(SIDE_LEFT) + MAX(0, center);
  1940. }
  1941. } else {
  1942. x_ofs = MAX(style->get_margin(SIDE_LEFT), x_ofs - r_icon->get_width() - style->get_margin(SIDE_RIGHT));
  1943. }
  1944. ofs_max -= r_icon->get_width();
  1945. }
  1946. // Note: Use two coordinates to fit IME input range.
  1947. Vector2 primary_caret_offset = get_caret_pixel_pos();
  1948. if (MIN(primary_caret_offset.x, primary_caret_offset.y) <= x_ofs) {
  1949. scroll_offset += x_ofs - MIN(primary_caret_offset.x, primary_caret_offset.y);
  1950. } else if (MAX(primary_caret_offset.x, primary_caret_offset.y) >= ofs_max) {
  1951. scroll_offset += ofs_max - MAX(primary_caret_offset.x, primary_caret_offset.y);
  1952. }
  1953. // Scroll to show as much text as possible
  1954. if (text_width + scroll_offset + x_ofs < ofs_max) {
  1955. scroll_offset = ofs_max - x_ofs - text_width;
  1956. }
  1957. scroll_offset = MIN(0, scroll_offset);
  1958. queue_accessibility_update();
  1959. queue_redraw();
  1960. }
  1961. int LineEdit::get_caret_column() const {
  1962. return caret_column;
  1963. }
  1964. int LineEdit::get_next_composite_character_column(int p_column) const {
  1965. ERR_FAIL_INDEX_V(p_column, text.length() + 1, -1);
  1966. if (p_column == text.length()) {
  1967. return p_column;
  1968. } else {
  1969. return TS->shaped_text_next_character_pos(text_rid, p_column);
  1970. }
  1971. }
  1972. int LineEdit::get_previous_composite_character_column(int p_column) const {
  1973. ERR_FAIL_INDEX_V(p_column, text.length() + 1, -1);
  1974. if (p_column == 0) {
  1975. return 0;
  1976. } else {
  1977. return TS->shaped_text_prev_character_pos(text_rid, p_column);
  1978. }
  1979. }
  1980. void LineEdit::set_scroll_offset(float p_pos) {
  1981. scroll_offset = p_pos;
  1982. if (scroll_offset < 0.0) {
  1983. scroll_offset = 0.0;
  1984. }
  1985. }
  1986. float LineEdit::get_scroll_offset() const {
  1987. return scroll_offset;
  1988. }
  1989. void LineEdit::insert_text_at_caret(String p_text) {
  1990. if (max_length > 0) {
  1991. // Truncate text to append to fit in max_length, if needed.
  1992. int available_chars = max_length - text.length();
  1993. if (p_text.length() > available_chars) {
  1994. emit_signal(SNAME("text_change_rejected"), p_text.substr(available_chars));
  1995. p_text = p_text.substr(0, available_chars);
  1996. }
  1997. }
  1998. String pre = text.substr(0, caret_column);
  1999. String post = text.substr(caret_column);
  2000. text = pre + p_text + post;
  2001. _shape();
  2002. TextServer::Direction dir = TS->shaped_text_get_dominant_direction_in_range(text_rid, caret_column, caret_column + p_text.length());
  2003. if (dir != TextServer::DIRECTION_AUTO) {
  2004. input_direction = (TextDirection)dir;
  2005. }
  2006. set_caret_column(caret_column + p_text.length());
  2007. if (!ime_text.is_empty()) {
  2008. _shape();
  2009. }
  2010. }
  2011. void LineEdit::clear_internal() {
  2012. deselect();
  2013. _clear_undo_stack();
  2014. caret_column = 0;
  2015. scroll_offset = 0.0;
  2016. undo_text = "";
  2017. text = "";
  2018. _shape();
  2019. queue_redraw();
  2020. }
  2021. Size2 LineEdit::get_minimum_size() const {
  2022. Ref<Font> font = theme_cache.font;
  2023. int font_size = theme_cache.font_size;
  2024. Size2 min_size;
  2025. // Minimum size of text.
  2026. // 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.
  2027. float em_space_size = font->get_char_size('W', font_size).x;
  2028. min_size.width = theme_cache.minimum_character_width * em_space_size;
  2029. if (expand_to_text_length) {
  2030. // Ensure some space for the caret when placed at the end.
  2031. min_size.width = MAX(min_size.width, full_width + theme_cache.caret_width);
  2032. }
  2033. min_size.height = MAX(TS->shaped_text_get_size(text_rid).y, font->get_height(font_size));
  2034. // Take icons into account.
  2035. int icon_max_width = 0;
  2036. if (right_icon.is_valid()) {
  2037. min_size.height = MAX(min_size.height, right_icon->get_height());
  2038. icon_max_width = right_icon->get_width();
  2039. }
  2040. if (clear_button_enabled) {
  2041. min_size.height = MAX(min_size.height, theme_cache.clear_icon->get_height());
  2042. icon_max_width = MAX(icon_max_width, theme_cache.clear_icon->get_width());
  2043. }
  2044. min_size.width += icon_max_width;
  2045. Size2 style_min_size = theme_cache.normal->get_minimum_size().max(theme_cache.read_only->get_minimum_size());
  2046. return style_min_size + min_size;
  2047. }
  2048. void LineEdit::deselect() {
  2049. selection.begin = 0;
  2050. selection.end = 0;
  2051. selection.start_column = 0;
  2052. selection.enabled = false;
  2053. selection.creating = false;
  2054. selection.double_click = false;
  2055. queue_accessibility_update();
  2056. queue_redraw();
  2057. }
  2058. bool LineEdit::has_selection() const {
  2059. return selection.enabled;
  2060. }
  2061. String LineEdit::get_selected_text() {
  2062. if (selection.enabled) {
  2063. return text.substr(selection.begin, selection.end - selection.begin);
  2064. } else {
  2065. return String();
  2066. }
  2067. }
  2068. int LineEdit::get_selection_from_column() const {
  2069. ERR_FAIL_COND_V(!selection.enabled, -1);
  2070. return selection.begin;
  2071. }
  2072. int LineEdit::get_selection_to_column() const {
  2073. ERR_FAIL_COND_V(!selection.enabled, -1);
  2074. return selection.end;
  2075. }
  2076. void LineEdit::selection_delete() {
  2077. if (selection.enabled) {
  2078. delete_text(selection.begin, selection.end);
  2079. }
  2080. deselect();
  2081. }
  2082. void LineEdit::set_max_length(int p_max_length) {
  2083. ERR_FAIL_COND(p_max_length < 0);
  2084. max_length = p_max_length;
  2085. set_text(text);
  2086. }
  2087. int LineEdit::get_max_length() const {
  2088. return max_length;
  2089. }
  2090. void LineEdit::selection_fill_at_caret() {
  2091. if (!selecting_enabled) {
  2092. return;
  2093. }
  2094. selection.begin = caret_column;
  2095. selection.end = selection.start_column;
  2096. if (selection.end < selection.begin) {
  2097. int aux = selection.end;
  2098. selection.end = selection.begin;
  2099. selection.begin = aux;
  2100. }
  2101. selection.enabled = (selection.begin != selection.end);
  2102. queue_accessibility_update();
  2103. }
  2104. void LineEdit::select_all() {
  2105. if (!selecting_enabled) {
  2106. return;
  2107. }
  2108. if (text.is_empty()) {
  2109. set_caret_column(0);
  2110. return;
  2111. }
  2112. selection.begin = 0;
  2113. selection.end = text.length();
  2114. selection.enabled = true;
  2115. queue_accessibility_update();
  2116. queue_redraw();
  2117. }
  2118. void LineEdit::set_editable(bool p_editable) {
  2119. if (editable == p_editable) {
  2120. return;
  2121. }
  2122. editable = p_editable;
  2123. if (!editable && editing) {
  2124. unedit();
  2125. emit_signal(SNAME("editing_toggled"), false);
  2126. }
  2127. _validate_caret_can_draw();
  2128. update_minimum_size();
  2129. queue_accessibility_update();
  2130. queue_redraw();
  2131. }
  2132. bool LineEdit::is_editable() const {
  2133. return editable;
  2134. }
  2135. void LineEdit::set_secret(bool p_secret) {
  2136. if (pass == p_secret) {
  2137. return;
  2138. }
  2139. pass = p_secret;
  2140. _shape();
  2141. set_caret_column(caret_column); // Update scroll_offset.
  2142. queue_redraw();
  2143. }
  2144. bool LineEdit::is_secret() const {
  2145. return pass;
  2146. }
  2147. void LineEdit::set_secret_character(const String &p_string) {
  2148. String c = p_string;
  2149. if (c.length() > 1) {
  2150. WARN_PRINT("Secret character must be exactly one character long (" + itos(c.length()) + " characters given).");
  2151. c = c.left(1);
  2152. }
  2153. if (secret_character == c) {
  2154. return;
  2155. }
  2156. secret_character = c;
  2157. _shape();
  2158. set_caret_column(caret_column); // Update scroll_offset.
  2159. queue_redraw();
  2160. }
  2161. String LineEdit::get_secret_character() const {
  2162. return secret_character;
  2163. }
  2164. void LineEdit::select(int p_from, int p_to) {
  2165. if (!selecting_enabled) {
  2166. return;
  2167. }
  2168. if (p_from == 0 && p_to == 0) {
  2169. deselect();
  2170. return;
  2171. }
  2172. int len = text.length();
  2173. if (p_from < 0) {
  2174. p_from = 0;
  2175. }
  2176. if (p_from > len) {
  2177. p_from = len;
  2178. }
  2179. if (p_to < 0 || p_to > len) {
  2180. p_to = len;
  2181. }
  2182. if (p_from >= p_to) {
  2183. return;
  2184. }
  2185. selection.enabled = true;
  2186. selection.begin = p_from;
  2187. selection.end = p_to;
  2188. selection.creating = false;
  2189. selection.double_click = false;
  2190. queue_accessibility_update();
  2191. queue_redraw();
  2192. }
  2193. bool LineEdit::is_text_field() const {
  2194. return true;
  2195. }
  2196. void LineEdit::menu_option(int p_option) {
  2197. switch (p_option) {
  2198. case MENU_CUT: {
  2199. if (editable) {
  2200. cut_text();
  2201. }
  2202. } break;
  2203. case MENU_COPY: {
  2204. copy_text();
  2205. } break;
  2206. case MENU_PASTE: {
  2207. if (editable) {
  2208. paste_text();
  2209. }
  2210. } break;
  2211. case MENU_CLEAR: {
  2212. if (editable) {
  2213. clear();
  2214. }
  2215. } break;
  2216. case MENU_SELECT_ALL: {
  2217. select_all();
  2218. } break;
  2219. case MENU_UNDO: {
  2220. if (editable) {
  2221. undo();
  2222. }
  2223. } break;
  2224. case MENU_REDO: {
  2225. if (editable) {
  2226. redo();
  2227. }
  2228. } break;
  2229. case MENU_DIR_INHERITED: {
  2230. set_text_direction(TEXT_DIRECTION_INHERITED);
  2231. } break;
  2232. case MENU_DIR_AUTO: {
  2233. set_text_direction(TEXT_DIRECTION_AUTO);
  2234. } break;
  2235. case MENU_DIR_LTR: {
  2236. set_text_direction(TEXT_DIRECTION_LTR);
  2237. } break;
  2238. case MENU_DIR_RTL: {
  2239. set_text_direction(TEXT_DIRECTION_RTL);
  2240. } break;
  2241. case MENU_DISPLAY_UCC: {
  2242. set_draw_control_chars(!get_draw_control_chars());
  2243. } break;
  2244. case MENU_INSERT_LRM: {
  2245. if (editable) {
  2246. insert_text_at_caret(String::chr(0x200E));
  2247. }
  2248. } break;
  2249. case MENU_INSERT_RLM: {
  2250. if (editable) {
  2251. insert_text_at_caret(String::chr(0x200F));
  2252. }
  2253. } break;
  2254. case MENU_INSERT_LRE: {
  2255. if (editable) {
  2256. insert_text_at_caret(String::chr(0x202A));
  2257. }
  2258. } break;
  2259. case MENU_INSERT_RLE: {
  2260. if (editable) {
  2261. insert_text_at_caret(String::chr(0x202B));
  2262. }
  2263. } break;
  2264. case MENU_INSERT_LRO: {
  2265. if (editable) {
  2266. insert_text_at_caret(String::chr(0x202D));
  2267. }
  2268. } break;
  2269. case MENU_INSERT_RLO: {
  2270. if (editable) {
  2271. insert_text_at_caret(String::chr(0x202E));
  2272. }
  2273. } break;
  2274. case MENU_INSERT_PDF: {
  2275. if (editable) {
  2276. insert_text_at_caret(String::chr(0x202C));
  2277. }
  2278. } break;
  2279. case MENU_INSERT_ALM: {
  2280. if (editable) {
  2281. insert_text_at_caret(String::chr(0x061C));
  2282. }
  2283. } break;
  2284. case MENU_INSERT_LRI: {
  2285. if (editable) {
  2286. insert_text_at_caret(String::chr(0x2066));
  2287. }
  2288. } break;
  2289. case MENU_INSERT_RLI: {
  2290. if (editable) {
  2291. insert_text_at_caret(String::chr(0x2067));
  2292. }
  2293. } break;
  2294. case MENU_INSERT_FSI: {
  2295. if (editable) {
  2296. insert_text_at_caret(String::chr(0x2068));
  2297. }
  2298. } break;
  2299. case MENU_INSERT_PDI: {
  2300. if (editable) {
  2301. insert_text_at_caret(String::chr(0x2069));
  2302. }
  2303. } break;
  2304. case MENU_INSERT_ZWJ: {
  2305. if (editable) {
  2306. insert_text_at_caret(String::chr(0x200D));
  2307. }
  2308. } break;
  2309. case MENU_INSERT_ZWNJ: {
  2310. if (editable) {
  2311. insert_text_at_caret(String::chr(0x200C));
  2312. }
  2313. } break;
  2314. case MENU_INSERT_WJ: {
  2315. if (editable) {
  2316. insert_text_at_caret(String::chr(0x2060));
  2317. }
  2318. } break;
  2319. case MENU_INSERT_SHY: {
  2320. if (editable) {
  2321. insert_text_at_caret(String::chr(0x00AD));
  2322. }
  2323. } break;
  2324. case MENU_EMOJI_AND_SYMBOL: {
  2325. show_emoji_and_symbol_picker();
  2326. } break;
  2327. }
  2328. }
  2329. void LineEdit::set_context_menu_enabled(bool p_enable) {
  2330. context_menu_enabled = p_enable;
  2331. }
  2332. bool LineEdit::is_context_menu_enabled() {
  2333. return context_menu_enabled;
  2334. }
  2335. void LineEdit::show_emoji_and_symbol_picker() {
  2336. _update_ime_window_position();
  2337. DisplayServer::get_singleton()->show_emoji_and_symbol_picker();
  2338. }
  2339. void LineEdit::set_emoji_menu_enabled(bool p_enabled) {
  2340. if (emoji_menu_enabled != p_enabled) {
  2341. emoji_menu_enabled = p_enabled;
  2342. _update_context_menu();
  2343. }
  2344. }
  2345. bool LineEdit::is_emoji_menu_enabled() const {
  2346. return emoji_menu_enabled;
  2347. }
  2348. void LineEdit::set_backspace_deletes_composite_character_enabled(bool p_enabled) {
  2349. backspace_deletes_composite_character_enabled = p_enabled;
  2350. }
  2351. bool LineEdit::is_backspace_deletes_composite_character_enabled() const {
  2352. return backspace_deletes_composite_character_enabled;
  2353. }
  2354. bool LineEdit::is_menu_visible() const {
  2355. return menu && menu->is_visible();
  2356. }
  2357. PopupMenu *LineEdit::get_menu() const {
  2358. if (!menu) {
  2359. const_cast<LineEdit *>(this)->_generate_context_menu();
  2360. }
  2361. return menu;
  2362. }
  2363. void LineEdit::_editor_settings_changed() {
  2364. #ifdef TOOLS_ENABLED
  2365. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/appearance/caret")) {
  2366. return;
  2367. }
  2368. set_caret_blink_enabled(EDITOR_GET("text_editor/appearance/caret/caret_blink"));
  2369. set_caret_blink_interval(EDITOR_GET("text_editor/appearance/caret/caret_blink_interval"));
  2370. #endif
  2371. }
  2372. void LineEdit::set_expand_to_text_length_enabled(bool p_enabled) {
  2373. expand_to_text_length = p_enabled;
  2374. update_minimum_size();
  2375. set_caret_column(caret_column);
  2376. }
  2377. bool LineEdit::is_expand_to_text_length_enabled() const {
  2378. return expand_to_text_length;
  2379. }
  2380. void LineEdit::set_clear_button_enabled(bool p_enabled) {
  2381. if (clear_button_enabled == p_enabled) {
  2382. return;
  2383. }
  2384. clear_button_enabled = p_enabled;
  2385. _fit_to_width();
  2386. update_minimum_size();
  2387. queue_redraw();
  2388. }
  2389. bool LineEdit::is_clear_button_enabled() const {
  2390. return clear_button_enabled;
  2391. }
  2392. void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
  2393. shortcut_keys_enabled = p_enabled;
  2394. }
  2395. bool LineEdit::is_shortcut_keys_enabled() const {
  2396. return shortcut_keys_enabled;
  2397. }
  2398. void LineEdit::set_virtual_keyboard_enabled(bool p_enable) {
  2399. virtual_keyboard_enabled = p_enable;
  2400. }
  2401. bool LineEdit::is_virtual_keyboard_enabled() const {
  2402. return virtual_keyboard_enabled;
  2403. }
  2404. void LineEdit::set_virtual_keyboard_show_on_focus(bool p_show_on_focus) {
  2405. virtual_keyboard_show_on_focus = p_show_on_focus;
  2406. }
  2407. bool LineEdit::get_virtual_keyboard_show_on_focus() const {
  2408. return virtual_keyboard_show_on_focus;
  2409. }
  2410. void LineEdit::set_virtual_keyboard_type(VirtualKeyboardType p_type) {
  2411. virtual_keyboard_type = p_type;
  2412. }
  2413. LineEdit::VirtualKeyboardType LineEdit::get_virtual_keyboard_type() const {
  2414. return virtual_keyboard_type;
  2415. }
  2416. void LineEdit::set_middle_mouse_paste_enabled(bool p_enabled) {
  2417. middle_mouse_paste_enabled = p_enabled;
  2418. }
  2419. bool LineEdit::is_middle_mouse_paste_enabled() const {
  2420. return middle_mouse_paste_enabled;
  2421. }
  2422. void LineEdit::set_selecting_enabled(bool p_enabled) {
  2423. if (selecting_enabled == p_enabled) {
  2424. return;
  2425. }
  2426. selecting_enabled = p_enabled;
  2427. if (!selecting_enabled) {
  2428. deselect();
  2429. }
  2430. }
  2431. bool LineEdit::is_selecting_enabled() const {
  2432. return selecting_enabled;
  2433. }
  2434. void LineEdit::set_deselect_on_focus_loss_enabled(const bool p_enabled) {
  2435. if (deselect_on_focus_loss_enabled == p_enabled) {
  2436. return;
  2437. }
  2438. deselect_on_focus_loss_enabled = p_enabled;
  2439. if (p_enabled && selection.enabled && !has_focus()) {
  2440. deselect();
  2441. }
  2442. }
  2443. bool LineEdit::is_deselect_on_focus_loss_enabled() const {
  2444. return deselect_on_focus_loss_enabled;
  2445. }
  2446. void LineEdit::set_drag_and_drop_selection_enabled(const bool p_enabled) {
  2447. drag_and_drop_selection_enabled = p_enabled;
  2448. }
  2449. bool LineEdit::is_drag_and_drop_selection_enabled() const {
  2450. return drag_and_drop_selection_enabled;
  2451. }
  2452. void LineEdit::_texture_changed() {
  2453. _fit_to_width();
  2454. update_minimum_size();
  2455. queue_redraw();
  2456. }
  2457. void LineEdit::set_right_icon(const Ref<Texture2D> &p_icon) {
  2458. if (right_icon == p_icon) {
  2459. return;
  2460. }
  2461. if (right_icon.is_valid()) {
  2462. right_icon->disconnect_changed(callable_mp(this, &LineEdit::_texture_changed));
  2463. }
  2464. right_icon = p_icon;
  2465. if (right_icon.is_valid()) {
  2466. right_icon->connect_changed(callable_mp(this, &LineEdit::_texture_changed));
  2467. }
  2468. _fit_to_width();
  2469. update_minimum_size();
  2470. queue_redraw();
  2471. }
  2472. Ref<Texture2D> LineEdit::get_right_icon() {
  2473. return right_icon;
  2474. }
  2475. void LineEdit::set_flat(bool p_enabled) {
  2476. if (flat != p_enabled) {
  2477. flat = p_enabled;
  2478. queue_redraw();
  2479. }
  2480. }
  2481. bool LineEdit::is_flat() const {
  2482. return flat;
  2483. }
  2484. void LineEdit::set_select_all_on_focus(bool p_enabled) {
  2485. select_all_on_focus = p_enabled;
  2486. }
  2487. bool LineEdit::is_select_all_on_focus() const {
  2488. return select_all_on_focus;
  2489. }
  2490. void LineEdit::clear_pending_select_all_on_focus() {
  2491. pending_select_all_on_focus = false;
  2492. }
  2493. void LineEdit::_text_changed() {
  2494. _emit_text_change();
  2495. _clear_redo();
  2496. }
  2497. void LineEdit::_emit_text_change() {
  2498. emit_signal(SceneStringName(text_changed), text);
  2499. text_changed_dirty = false;
  2500. }
  2501. PackedStringArray LineEdit::get_configuration_warnings() const {
  2502. PackedStringArray warnings = Control::get_configuration_warnings();
  2503. if (secret_character.length() > 1) {
  2504. warnings.push_back("Secret Character property supports only one character. Extra characters will be ignored.");
  2505. }
  2506. return warnings;
  2507. }
  2508. void LineEdit::_shape() {
  2509. const Ref<Font> &font = theme_cache.font;
  2510. int font_size = theme_cache.font_size;
  2511. if (font.is_null()) {
  2512. return;
  2513. }
  2514. Size2 old_size = TS->shaped_text_get_size(text_rid);
  2515. TS->shaped_text_clear(text_rid);
  2516. String t;
  2517. if (text.is_empty() && ime_text.is_empty()) {
  2518. t = placeholder_translated;
  2519. } else if (pass) {
  2520. String s = secret_character.is_empty() ? U"•" : secret_character.left(1);
  2521. t = s.repeat(text.length() + ime_text.length());
  2522. } else {
  2523. if (!ime_text.is_empty()) {
  2524. t = text.substr(0, caret_column) + ime_text + text.substr(caret_column);
  2525. } else {
  2526. t = text;
  2527. }
  2528. }
  2529. if (text_direction == Control::TEXT_DIRECTION_INHERITED) {
  2530. TS->shaped_text_set_direction(text_rid, is_layout_rtl() ? TextServer::DIRECTION_RTL : TextServer::DIRECTION_LTR);
  2531. } else {
  2532. TS->shaped_text_set_direction(text_rid, (TextServer::Direction)text_direction);
  2533. }
  2534. TS->shaped_text_set_preserve_control(text_rid, draw_control_chars);
  2535. TS->shaped_text_add_string(text_rid, t, font->get_rids(), font_size, font->get_opentype_features(), language);
  2536. TS->shaped_text_set_bidi_override(text_rid, structured_text_parser(st_parser, st_args, t));
  2537. full_width = TS->shaped_text_get_size(text_rid).x;
  2538. _fit_to_width();
  2539. Size2 size = TS->shaped_text_get_size(text_rid);
  2540. if ((expand_to_text_length && old_size.x != size.x) || (old_size.y != size.y)) {
  2541. update_minimum_size();
  2542. }
  2543. if (accessibility_text_root_element.is_valid()) {
  2544. DisplayServer::get_singleton()->accessibility_free_element(accessibility_text_root_element);
  2545. accessibility_text_root_element = RID();
  2546. }
  2547. queue_accessibility_update();
  2548. }
  2549. void LineEdit::_fit_to_width() {
  2550. if (alignment == HORIZONTAL_ALIGNMENT_FILL) {
  2551. Ref<StyleBox> style = theme_cache.normal;
  2552. int t_width = get_size().width - style->get_margin(SIDE_RIGHT) - style->get_margin(SIDE_LEFT);
  2553. bool using_placeholder = text.is_empty() && ime_text.is_empty();
  2554. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  2555. if (right_icon.is_valid() || display_clear_icon) {
  2556. Ref<Texture2D> r_icon = display_clear_icon ? theme_cache.clear_icon : right_icon;
  2557. t_width -= r_icon->get_width();
  2558. }
  2559. TS->shaped_text_fit_to_width(text_rid, MAX(t_width, full_width));
  2560. }
  2561. }
  2562. void LineEdit::_clear_redo() {
  2563. _create_undo_state();
  2564. if (undo_stack_pos == nullptr) {
  2565. return;
  2566. }
  2567. undo_stack_pos = undo_stack_pos->next();
  2568. while (undo_stack_pos) {
  2569. List<TextOperation>::Element *elem = undo_stack_pos;
  2570. undo_stack_pos = undo_stack_pos->next();
  2571. undo_stack.erase(elem);
  2572. }
  2573. _create_undo_state();
  2574. }
  2575. void LineEdit::_clear_undo_stack() {
  2576. undo_stack.clear();
  2577. undo_stack_pos = nullptr;
  2578. _create_undo_state();
  2579. }
  2580. void LineEdit::_create_undo_state() {
  2581. TextOperation op;
  2582. op.text = text;
  2583. op.caret_column = caret_column;
  2584. op.scroll_offset = scroll_offset;
  2585. undo_stack.push_back(op);
  2586. }
  2587. Key LineEdit::_get_menu_action_accelerator(const String &p_action) {
  2588. const List<Ref<InputEvent>> *events = InputMap::get_singleton()->action_get_events(p_action);
  2589. if (!events) {
  2590. return Key::NONE;
  2591. }
  2592. // Use first event in the list for the accelerator.
  2593. const List<Ref<InputEvent>>::Element *first_event = events->front();
  2594. if (!first_event) {
  2595. return Key::NONE;
  2596. }
  2597. const Ref<InputEventKey> event = first_event->get();
  2598. if (event.is_null()) {
  2599. return Key::NONE;
  2600. }
  2601. // Use physical keycode if non-zero.
  2602. if (event->get_physical_keycode() != Key::NONE) {
  2603. return event->get_physical_keycode_with_modifiers();
  2604. } else {
  2605. return event->get_keycode_with_modifiers();
  2606. }
  2607. }
  2608. void LineEdit::_generate_context_menu() {
  2609. menu = memnew(PopupMenu);
  2610. add_child(menu, false, INTERNAL_MODE_FRONT);
  2611. menu_dir = memnew(PopupMenu);
  2612. menu_dir->add_radio_check_item(ETR("Same as Layout Direction"), MENU_DIR_INHERITED);
  2613. menu_dir->add_radio_check_item(ETR("Auto-Detect Direction"), MENU_DIR_AUTO);
  2614. menu_dir->add_radio_check_item(ETR("Left-to-Right"), MENU_DIR_LTR);
  2615. menu_dir->add_radio_check_item(ETR("Right-to-Left"), MENU_DIR_RTL);
  2616. menu_ctl = memnew(PopupMenu);
  2617. menu_ctl->add_item(ETR("Left-to-Right Mark (LRM)"), MENU_INSERT_LRM);
  2618. menu_ctl->add_item(ETR("Right-to-Left Mark (RLM)"), MENU_INSERT_RLM);
  2619. menu_ctl->add_item(ETR("Start of Left-to-Right Embedding (LRE)"), MENU_INSERT_LRE);
  2620. menu_ctl->add_item(ETR("Start of Right-to-Left Embedding (RLE)"), MENU_INSERT_RLE);
  2621. menu_ctl->add_item(ETR("Start of Left-to-Right Override (LRO)"), MENU_INSERT_LRO);
  2622. menu_ctl->add_item(ETR("Start of Right-to-Left Override (RLO)"), MENU_INSERT_RLO);
  2623. menu_ctl->add_item(ETR("Pop Direction Formatting (PDF)"), MENU_INSERT_PDF);
  2624. menu_ctl->add_separator();
  2625. menu_ctl->add_item(ETR("Arabic Letter Mark (ALM)"), MENU_INSERT_ALM);
  2626. menu_ctl->add_item(ETR("Left-to-Right Isolate (LRI)"), MENU_INSERT_LRI);
  2627. menu_ctl->add_item(ETR("Right-to-Left Isolate (RLI)"), MENU_INSERT_RLI);
  2628. menu_ctl->add_item(ETR("First Strong Isolate (FSI)"), MENU_INSERT_FSI);
  2629. menu_ctl->add_item(ETR("Pop Direction Isolate (PDI)"), MENU_INSERT_PDI);
  2630. menu_ctl->add_separator();
  2631. menu_ctl->add_item(ETR("Zero-Width Joiner (ZWJ)"), MENU_INSERT_ZWJ);
  2632. menu_ctl->add_item(ETR("Zero-Width Non-Joiner (ZWNJ)"), MENU_INSERT_ZWNJ);
  2633. menu_ctl->add_item(ETR("Word Joiner (WJ)"), MENU_INSERT_WJ);
  2634. menu_ctl->add_item(ETR("Soft Hyphen (SHY)"), MENU_INSERT_SHY);
  2635. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  2636. menu->add_item(ETR("Emoji & Symbols"), MENU_EMOJI_AND_SYMBOL);
  2637. menu->add_separator();
  2638. }
  2639. menu->add_item(ETR("Cut"), MENU_CUT);
  2640. menu->add_item(ETR("Copy"), MENU_COPY);
  2641. menu->add_item(ETR("Paste"), MENU_PASTE);
  2642. menu->add_separator();
  2643. menu->add_item(ETR("Select All"), MENU_SELECT_ALL);
  2644. menu->add_item(ETR("Clear"), MENU_CLEAR);
  2645. menu->add_separator();
  2646. menu->add_item(ETR("Undo"), MENU_UNDO);
  2647. menu->add_item(ETR("Redo"), MENU_REDO);
  2648. menu->add_separator();
  2649. menu->add_submenu_node_item(ETR("Text Writing Direction"), menu_dir, MENU_SUBMENU_TEXT_DIR);
  2650. menu->add_separator();
  2651. menu->add_check_item(ETR("Display Control Characters"), MENU_DISPLAY_UCC);
  2652. menu->add_submenu_node_item(ETR("Insert Control Character"), menu_ctl, MENU_SUBMENU_INSERT_UCC);
  2653. menu->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option));
  2654. menu_dir->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option));
  2655. menu_ctl->connect(SceneStringName(id_pressed), callable_mp(this, &LineEdit::menu_option));
  2656. menu->connect(SceneStringName(focus_entered), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2657. menu->connect(SceneStringName(focus_exited), callable_mp(this, &LineEdit::_validate_caret_can_draw));
  2658. }
  2659. void LineEdit::_update_context_menu() {
  2660. if (!menu) {
  2661. _generate_context_menu();
  2662. }
  2663. int idx = -1;
  2664. #define MENU_ITEM_ACTION_DISABLED(m_menu, m_id, m_action, m_disabled) \
  2665. idx = m_menu->get_item_index(m_id); \
  2666. if (idx >= 0) { \
  2667. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2668. m_menu->set_item_disabled(idx, m_disabled); \
  2669. }
  2670. #define MENU_ITEM_ACTION(m_menu, m_id, m_action) \
  2671. idx = m_menu->get_item_index(m_id); \
  2672. if (idx >= 0) { \
  2673. m_menu->set_item_accelerator(idx, shortcut_keys_enabled ? _get_menu_action_accelerator(m_action) : Key::NONE); \
  2674. }
  2675. #define MENU_ITEM_DISABLED(m_menu, m_id, m_disabled) \
  2676. idx = m_menu->get_item_index(m_id); \
  2677. if (idx >= 0) { \
  2678. m_menu->set_item_disabled(idx, m_disabled); \
  2679. }
  2680. #define MENU_ITEM_CHECKED(m_menu, m_id, m_checked) \
  2681. idx = m_menu->get_item_index(m_id); \
  2682. if (idx >= 0) { \
  2683. m_menu->set_item_checked(idx, m_checked); \
  2684. }
  2685. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  2686. MENU_ITEM_DISABLED(menu, MENU_EMOJI_AND_SYMBOL, !editable || !emoji_menu_enabled)
  2687. }
  2688. MENU_ITEM_ACTION_DISABLED(menu, MENU_CUT, "ui_cut", !editable)
  2689. MENU_ITEM_ACTION(menu, MENU_COPY, "ui_copy")
  2690. MENU_ITEM_ACTION_DISABLED(menu, MENU_PASTE, "ui_paste", !editable)
  2691. MENU_ITEM_ACTION_DISABLED(menu, MENU_SELECT_ALL, "ui_text_select_all", !selecting_enabled)
  2692. MENU_ITEM_DISABLED(menu, MENU_CLEAR, !editable)
  2693. MENU_ITEM_ACTION_DISABLED(menu, MENU_UNDO, "ui_undo", !editable || !has_undo())
  2694. MENU_ITEM_ACTION_DISABLED(menu, MENU_REDO, "ui_redo", !editable || !has_redo())
  2695. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_INHERITED, text_direction == TEXT_DIRECTION_INHERITED)
  2696. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_AUTO, text_direction == TEXT_DIRECTION_AUTO)
  2697. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_LTR, text_direction == TEXT_DIRECTION_LTR)
  2698. MENU_ITEM_CHECKED(menu_dir, MENU_DIR_RTL, text_direction == TEXT_DIRECTION_RTL)
  2699. MENU_ITEM_CHECKED(menu, MENU_DISPLAY_UCC, draw_control_chars)
  2700. MENU_ITEM_DISABLED(menu, MENU_SUBMENU_INSERT_UCC, !editable)
  2701. #undef MENU_ITEM_ACTION_DISABLED
  2702. #undef MENU_ITEM_ACTION
  2703. #undef MENU_ITEM_DISABLED
  2704. #undef MENU_ITEM_CHECKED
  2705. }
  2706. void LineEdit::_validate_property(PropertyInfo &p_property) const {
  2707. if (!Engine::get_singleton()->is_editor_hint()) {
  2708. return;
  2709. }
  2710. if (!caret_blink_enabled && p_property.name == "caret_blink_interval") {
  2711. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  2712. }
  2713. }
  2714. void LineEdit::_bind_methods() {
  2715. ClassDB::bind_method(D_METHOD("has_ime_text"), &LineEdit::has_ime_text);
  2716. ClassDB::bind_method(D_METHOD("cancel_ime"), &LineEdit::cancel_ime);
  2717. ClassDB::bind_method(D_METHOD("apply_ime"), &LineEdit::apply_ime);
  2718. ClassDB::bind_method(D_METHOD("set_horizontal_alignment", "alignment"), &LineEdit::set_horizontal_alignment);
  2719. ClassDB::bind_method(D_METHOD("get_horizontal_alignment"), &LineEdit::get_horizontal_alignment);
  2720. ClassDB::bind_method(D_METHOD("edit"), &LineEdit::edit);
  2721. ClassDB::bind_method(D_METHOD("unedit"), &LineEdit::unedit);
  2722. ClassDB::bind_method(D_METHOD("is_editing"), &LineEdit::is_editing);
  2723. ClassDB::bind_method(D_METHOD("set_keep_editing_on_text_submit", "enable"), &LineEdit::set_keep_editing_on_text_submit);
  2724. ClassDB::bind_method(D_METHOD("is_editing_kept_on_text_submit"), &LineEdit::is_editing_kept_on_text_submit);
  2725. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  2726. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  2727. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  2728. ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect);
  2729. ClassDB::bind_method(D_METHOD("has_undo"), &LineEdit::has_undo);
  2730. ClassDB::bind_method(D_METHOD("has_redo"), &LineEdit::has_redo);
  2731. ClassDB::bind_method(D_METHOD("has_selection"), &LineEdit::has_selection);
  2732. ClassDB::bind_method(D_METHOD("get_selected_text"), &LineEdit::get_selected_text);
  2733. ClassDB::bind_method(D_METHOD("get_selection_from_column"), &LineEdit::get_selection_from_column);
  2734. ClassDB::bind_method(D_METHOD("get_selection_to_column"), &LineEdit::get_selection_to_column);
  2735. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  2736. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  2737. ClassDB::bind_method(D_METHOD("get_draw_control_chars"), &LineEdit::get_draw_control_chars);
  2738. ClassDB::bind_method(D_METHOD("set_draw_control_chars", "enable"), &LineEdit::set_draw_control_chars);
  2739. ClassDB::bind_method(D_METHOD("set_text_direction", "direction"), &LineEdit::set_text_direction);
  2740. ClassDB::bind_method(D_METHOD("get_text_direction"), &LineEdit::get_text_direction);
  2741. ClassDB::bind_method(D_METHOD("set_language", "language"), &LineEdit::set_language);
  2742. ClassDB::bind_method(D_METHOD("get_language"), &LineEdit::get_language);
  2743. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override", "parser"), &LineEdit::set_structured_text_bidi_override);
  2744. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override"), &LineEdit::get_structured_text_bidi_override);
  2745. ClassDB::bind_method(D_METHOD("set_structured_text_bidi_override_options", "args"), &LineEdit::set_structured_text_bidi_override_options);
  2746. ClassDB::bind_method(D_METHOD("get_structured_text_bidi_override_options"), &LineEdit::get_structured_text_bidi_override_options);
  2747. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  2748. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  2749. ClassDB::bind_method(D_METHOD("set_caret_column", "position"), &LineEdit::set_caret_column);
  2750. ClassDB::bind_method(D_METHOD("get_caret_column"), &LineEdit::get_caret_column);
  2751. ClassDB::bind_method(D_METHOD("get_next_composite_character_column", "column"), &LineEdit::get_next_composite_character_column);
  2752. ClassDB::bind_method(D_METHOD("get_previous_composite_character_column", "column"), &LineEdit::get_previous_composite_character_column);
  2753. ClassDB::bind_method(D_METHOD("get_scroll_offset"), &LineEdit::get_scroll_offset);
  2754. ClassDB::bind_method(D_METHOD("set_expand_to_text_length_enabled", "enabled"), &LineEdit::set_expand_to_text_length_enabled);
  2755. ClassDB::bind_method(D_METHOD("is_expand_to_text_length_enabled"), &LineEdit::is_expand_to_text_length_enabled);
  2756. ClassDB::bind_method(D_METHOD("set_caret_blink_enabled", "enabled"), &LineEdit::set_caret_blink_enabled);
  2757. ClassDB::bind_method(D_METHOD("is_caret_blink_enabled"), &LineEdit::is_caret_blink_enabled);
  2758. ClassDB::bind_method(D_METHOD("set_caret_mid_grapheme_enabled", "enabled"), &LineEdit::set_caret_mid_grapheme_enabled);
  2759. ClassDB::bind_method(D_METHOD("is_caret_mid_grapheme_enabled"), &LineEdit::is_caret_mid_grapheme_enabled);
  2760. ClassDB::bind_method(D_METHOD("set_caret_force_displayed", "enabled"), &LineEdit::set_caret_force_displayed);
  2761. ClassDB::bind_method(D_METHOD("is_caret_force_displayed"), &LineEdit::is_caret_force_displayed);
  2762. ClassDB::bind_method(D_METHOD("set_caret_blink_interval", "interval"), &LineEdit::set_caret_blink_interval);
  2763. ClassDB::bind_method(D_METHOD("get_caret_blink_interval"), &LineEdit::get_caret_blink_interval);
  2764. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  2765. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  2766. ClassDB::bind_method(D_METHOD("insert_text_at_caret", "text"), &LineEdit::insert_text_at_caret);
  2767. ClassDB::bind_method(D_METHOD("delete_char_at_caret"), &LineEdit::delete_char);
  2768. ClassDB::bind_method(D_METHOD("delete_text", "from_column", "to_column"), &LineEdit::delete_text);
  2769. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  2770. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  2771. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  2772. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  2773. ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
  2774. ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
  2775. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  2776. // TODO: Properly handle popups when advanced GUI is disabled.
  2777. #ifndef ADVANCED_GUI_DISABLED
  2778. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  2779. #endif // ADVANCED_GUI_DISABLED
  2780. ClassDB::bind_method(D_METHOD("is_menu_visible"), &LineEdit::is_menu_visible);
  2781. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
  2782. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled);
  2783. ClassDB::bind_method(D_METHOD("set_emoji_menu_enabled", "enable"), &LineEdit::set_emoji_menu_enabled);
  2784. ClassDB::bind_method(D_METHOD("is_emoji_menu_enabled"), &LineEdit::is_emoji_menu_enabled);
  2785. ClassDB::bind_method(D_METHOD("set_backspace_deletes_composite_character_enabled", "enable"), &LineEdit::set_backspace_deletes_composite_character_enabled);
  2786. ClassDB::bind_method(D_METHOD("is_backspace_deletes_composite_character_enabled"), &LineEdit::is_backspace_deletes_composite_character_enabled);
  2787. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_enabled", "enable"), &LineEdit::set_virtual_keyboard_enabled);
  2788. ClassDB::bind_method(D_METHOD("is_virtual_keyboard_enabled"), &LineEdit::is_virtual_keyboard_enabled);
  2789. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_show_on_focus", "show_on_focus"), &LineEdit::set_virtual_keyboard_show_on_focus);
  2790. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_show_on_focus"), &LineEdit::get_virtual_keyboard_show_on_focus);
  2791. ClassDB::bind_method(D_METHOD("set_virtual_keyboard_type", "type"), &LineEdit::set_virtual_keyboard_type);
  2792. ClassDB::bind_method(D_METHOD("get_virtual_keyboard_type"), &LineEdit::get_virtual_keyboard_type);
  2793. ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled);
  2794. ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled);
  2795. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled);
  2796. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled);
  2797. ClassDB::bind_method(D_METHOD("set_middle_mouse_paste_enabled", "enable"), &LineEdit::set_middle_mouse_paste_enabled);
  2798. ClassDB::bind_method(D_METHOD("is_middle_mouse_paste_enabled"), &LineEdit::is_middle_mouse_paste_enabled);
  2799. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled);
  2800. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled);
  2801. ClassDB::bind_method(D_METHOD("set_deselect_on_focus_loss_enabled", "enable"), &LineEdit::set_deselect_on_focus_loss_enabled);
  2802. ClassDB::bind_method(D_METHOD("is_deselect_on_focus_loss_enabled"), &LineEdit::is_deselect_on_focus_loss_enabled);
  2803. ClassDB::bind_method(D_METHOD("set_drag_and_drop_selection_enabled", "enable"), &LineEdit::set_drag_and_drop_selection_enabled);
  2804. ClassDB::bind_method(D_METHOD("is_drag_and_drop_selection_enabled"), &LineEdit::is_drag_and_drop_selection_enabled);
  2805. ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
  2806. ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
  2807. ClassDB::bind_method(D_METHOD("set_flat", "enabled"), &LineEdit::set_flat);
  2808. ClassDB::bind_method(D_METHOD("is_flat"), &LineEdit::is_flat);
  2809. ClassDB::bind_method(D_METHOD("set_select_all_on_focus", "enabled"), &LineEdit::set_select_all_on_focus);
  2810. ClassDB::bind_method(D_METHOD("is_select_all_on_focus"), &LineEdit::is_select_all_on_focus);
  2811. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text")));
  2812. ADD_SIGNAL(MethodInfo("text_change_rejected", PropertyInfo(Variant::STRING, "rejected_substring")));
  2813. ADD_SIGNAL(MethodInfo("text_submitted", PropertyInfo(Variant::STRING, "new_text")));
  2814. ADD_SIGNAL(MethodInfo("editing_toggled", PropertyInfo(Variant::BOOL, "toggled_on")));
  2815. BIND_ENUM_CONSTANT(MENU_CUT);
  2816. BIND_ENUM_CONSTANT(MENU_COPY);
  2817. BIND_ENUM_CONSTANT(MENU_PASTE);
  2818. BIND_ENUM_CONSTANT(MENU_CLEAR);
  2819. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  2820. BIND_ENUM_CONSTANT(MENU_UNDO);
  2821. BIND_ENUM_CONSTANT(MENU_REDO);
  2822. BIND_ENUM_CONSTANT(MENU_SUBMENU_TEXT_DIR);
  2823. BIND_ENUM_CONSTANT(MENU_DIR_INHERITED);
  2824. BIND_ENUM_CONSTANT(MENU_DIR_AUTO);
  2825. BIND_ENUM_CONSTANT(MENU_DIR_LTR);
  2826. BIND_ENUM_CONSTANT(MENU_DIR_RTL);
  2827. BIND_ENUM_CONSTANT(MENU_DISPLAY_UCC);
  2828. BIND_ENUM_CONSTANT(MENU_SUBMENU_INSERT_UCC);
  2829. BIND_ENUM_CONSTANT(MENU_INSERT_LRM);
  2830. BIND_ENUM_CONSTANT(MENU_INSERT_RLM);
  2831. BIND_ENUM_CONSTANT(MENU_INSERT_LRE);
  2832. BIND_ENUM_CONSTANT(MENU_INSERT_RLE);
  2833. BIND_ENUM_CONSTANT(MENU_INSERT_LRO);
  2834. BIND_ENUM_CONSTANT(MENU_INSERT_RLO);
  2835. BIND_ENUM_CONSTANT(MENU_INSERT_PDF);
  2836. BIND_ENUM_CONSTANT(MENU_INSERT_ALM);
  2837. BIND_ENUM_CONSTANT(MENU_INSERT_LRI);
  2838. BIND_ENUM_CONSTANT(MENU_INSERT_RLI);
  2839. BIND_ENUM_CONSTANT(MENU_INSERT_FSI);
  2840. BIND_ENUM_CONSTANT(MENU_INSERT_PDI);
  2841. BIND_ENUM_CONSTANT(MENU_INSERT_ZWJ);
  2842. BIND_ENUM_CONSTANT(MENU_INSERT_ZWNJ);
  2843. BIND_ENUM_CONSTANT(MENU_INSERT_WJ);
  2844. BIND_ENUM_CONSTANT(MENU_INSERT_SHY);
  2845. BIND_ENUM_CONSTANT(MENU_EMOJI_AND_SYMBOL);
  2846. BIND_ENUM_CONSTANT(MENU_MAX);
  2847. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_DEFAULT);
  2848. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_MULTILINE);
  2849. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER);
  2850. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_NUMBER_DECIMAL);
  2851. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PHONE);
  2852. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_EMAIL_ADDRESS);
  2853. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_PASSWORD);
  2854. BIND_ENUM_CONSTANT(KEYBOARD_TYPE_URL);
  2855. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  2856. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  2857. ADD_PROPERTY(PropertyInfo(Variant::INT, "alignment", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_horizontal_alignment", "get_horizontal_alignment");
  2858. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_max_length", "get_max_length");
  2859. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  2860. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_editing_on_text_submit"), "set_keep_editing_on_text_submit", "is_editing_kept_on_text_submit");
  2861. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length_enabled", "is_expand_to_text_length_enabled");
  2862. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  2863. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "emoji_menu_enabled"), "set_emoji_menu_enabled", "is_emoji_menu_enabled");
  2864. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "backspace_deletes_composite_character_enabled"), "set_backspace_deletes_composite_character_enabled", "is_backspace_deletes_composite_character_enabled");
  2865. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_enabled"), "set_virtual_keyboard_enabled", "is_virtual_keyboard_enabled");
  2866. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "virtual_keyboard_show_on_focus"), "set_virtual_keyboard_show_on_focus", "get_virtual_keyboard_show_on_focus");
  2867. 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");
  2868. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled");
  2869. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  2870. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "middle_mouse_paste_enabled"), "set_middle_mouse_paste_enabled", "is_middle_mouse_paste_enabled");
  2871. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  2872. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deselect_on_focus_loss_enabled"), "set_deselect_on_focus_loss_enabled", "is_deselect_on_focus_loss_enabled");
  2873. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "drag_and_drop_selection_enabled"), "set_drag_and_drop_selection_enabled", "is_drag_and_drop_selection_enabled");
  2874. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_right_icon", "get_right_icon");
  2875. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flat"), "set_flat", "is_flat");
  2876. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "draw_control_chars"), "set_draw_control_chars", "get_draw_control_chars");
  2877. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "select_all_on_focus"), "set_select_all_on_focus", "is_select_all_on_focus");
  2878. ADD_GROUP("Caret", "caret_");
  2879. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "set_caret_blink_enabled", "is_caret_blink_enabled");
  2880. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "caret_blink_interval", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "set_caret_blink_interval", "get_caret_blink_interval");
  2881. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_column", PROPERTY_HINT_RANGE, "0,1000,1,or_greater"), "set_caret_column", "get_caret_column");
  2882. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_force_displayed"), "set_caret_force_displayed", "is_caret_force_displayed");
  2883. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_mid_grapheme"), "set_caret_mid_grapheme_enabled", "is_caret_mid_grapheme_enabled");
  2884. ADD_GROUP("Secret", "secret");
  2885. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret", PROPERTY_HINT_GROUP_ENABLE), "set_secret", "is_secret");
  2886. ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
  2887. ADD_GROUP("BiDi", "");
  2888. ADD_PROPERTY(PropertyInfo(Variant::INT, "text_direction", PROPERTY_HINT_ENUM, "Auto,Left-to-Right,Right-to-Left,Inherited"), "set_text_direction", "get_text_direction");
  2889. ADD_PROPERTY(PropertyInfo(Variant::STRING, "language", PROPERTY_HINT_LOCALE_ID, ""), "set_language", "get_language");
  2890. 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");
  2891. ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "structured_text_bidi_override_options"), "set_structured_text_bidi_override_options", "get_structured_text_bidi_override_options");
  2892. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, normal);
  2893. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, read_only);
  2894. BIND_THEME_ITEM(Theme::DATA_TYPE_STYLEBOX, LineEdit, focus);
  2895. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, LineEdit, font);
  2896. BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, LineEdit, font_size);
  2897. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_color);
  2898. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_uneditable_color);
  2899. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_selected_color);
  2900. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, LineEdit, font_outline_size, "outline_size");
  2901. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_outline_color);
  2902. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, font_placeholder_color);
  2903. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LineEdit, caret_width);
  2904. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, caret_color);
  2905. BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, LineEdit, minimum_character_width);
  2906. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, selection_color);
  2907. BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, LineEdit, clear_icon, "clear");
  2908. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, clear_button_color);
  2909. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, LineEdit, clear_button_color_pressed);
  2910. ADD_CLASS_DEPENDENCY("PopupMenu");
  2911. }
  2912. LineEdit::LineEdit(const String &p_placeholder) {
  2913. text_rid = TS->create_shaped_text();
  2914. _create_undo_state();
  2915. deselect();
  2916. set_focus_mode(FOCUS_ALL);
  2917. set_default_cursor_shape(CURSOR_IBEAM);
  2918. set_mouse_filter(MOUSE_FILTER_STOP);
  2919. set_process_unhandled_key_input(true);
  2920. set_caret_blink_enabled(false);
  2921. set_placeholder(p_placeholder);
  2922. set_editable(true); // Initialize to opposite first, so we get past the early-out in set_editable.
  2923. }
  2924. LineEdit::~LineEdit() {
  2925. TS->free_rid(text_rid);
  2926. }