line_edit.cpp 86 KB

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