line_edit.cpp 93 KB

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