line_edit.cpp 83 KB

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