line_edit.cpp 94 KB

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