line_edit.cpp 80 KB

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