line_edit.cpp 87 KB

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