2
0

line_edit.cpp 76 KB

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