line_edit.cpp 93 KB

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