line_edit.cpp 93 KB

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