line_edit.cpp 104 KB

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