line_edit.cpp 106 KB

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