line_edit.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883
  1. /*************************************************************************/
  2. /* line_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/message_queue.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #include "core/print_string.h"
  35. #include "core/translation.h"
  36. #include "label.h"
  37. #ifdef TOOLS_ENABLED
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #endif
  41. static bool _is_text_char(CharType c) {
  42. return !is_symbol(c);
  43. }
  44. void LineEdit::_gui_input(Ref<InputEvent> p_event) {
  45. Ref<InputEventMouseButton> b = p_event;
  46. if (b.is_valid()) {
  47. if (b->is_pressed() && b->get_button_index() == BUTTON_RIGHT && context_menu_enabled) {
  48. menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  49. menu->set_size(Vector2(1, 1));
  50. menu->set_scale(get_global_transform().get_scale());
  51. menu->popup();
  52. grab_focus();
  53. accept_event();
  54. return;
  55. }
  56. if (b->get_button_index() != BUTTON_LEFT)
  57. return;
  58. _reset_caret_blink_timer();
  59. if (b->is_pressed()) {
  60. accept_event(); //don't pass event further when clicked on text field
  61. if (!text.empty() && is_editable() && _is_over_clear_button(b->get_position())) {
  62. clear_button_status.press_attempt = true;
  63. clear_button_status.pressing_inside = true;
  64. return;
  65. }
  66. shift_selection_check_pre(b->get_shift());
  67. set_cursor_at_pixel_pos(b->get_position().x);
  68. if (b->get_shift()) {
  69. selection_fill_at_cursor();
  70. selection.creating = true;
  71. } else {
  72. if (b->is_doubleclick() && selecting_enabled) {
  73. selection.enabled = true;
  74. selection.begin = 0;
  75. selection.end = text.length();
  76. selection.doubleclick = true;
  77. }
  78. selection.drag_attempt = false;
  79. if ((cursor_pos < selection.begin) || (cursor_pos > selection.end) || !selection.enabled) {
  80. deselect();
  81. selection.cursor_start = cursor_pos;
  82. selection.creating = true;
  83. } else if (selection.enabled) {
  84. selection.drag_attempt = true;
  85. }
  86. }
  87. update();
  88. } else {
  89. if (!text.empty() && is_editable() && clear_button_enabled) {
  90. bool press_attempt = clear_button_status.press_attempt;
  91. clear_button_status.press_attempt = false;
  92. if (press_attempt && clear_button_status.pressing_inside && _is_over_clear_button(b->get_position())) {
  93. clear();
  94. return;
  95. }
  96. }
  97. if ((!selection.creating) && (!selection.doubleclick)) {
  98. deselect();
  99. }
  100. selection.creating = false;
  101. selection.doubleclick = false;
  102. if (OS::get_singleton()->has_virtual_keyboard())
  103. OS::get_singleton()->show_virtual_keyboard(text, get_global_rect(), max_length);
  104. }
  105. update();
  106. }
  107. Ref<InputEventMouseMotion> m = p_event;
  108. if (m.is_valid()) {
  109. if (!text.empty() && is_editable() && clear_button_enabled) {
  110. bool last_press_inside = clear_button_status.pressing_inside;
  111. clear_button_status.pressing_inside = clear_button_status.press_attempt && _is_over_clear_button(m->get_position());
  112. if (last_press_inside != clear_button_status.pressing_inside) {
  113. update();
  114. }
  115. }
  116. if (m->get_button_mask() & BUTTON_LEFT) {
  117. if (selection.creating) {
  118. set_cursor_at_pixel_pos(m->get_position().x);
  119. selection_fill_at_cursor();
  120. }
  121. }
  122. }
  123. Ref<InputEventKey> k = p_event;
  124. if (k.is_valid()) {
  125. if (!k->is_pressed())
  126. return;
  127. #ifdef APPLE_STYLE_KEYS
  128. if (k->get_control() && !k->get_shift() && !k->get_alt() && !k->get_command()) {
  129. uint32_t remap_key = KEY_UNKNOWN;
  130. switch (k->get_scancode()) {
  131. case KEY_F: {
  132. remap_key = KEY_RIGHT;
  133. } break;
  134. case KEY_B: {
  135. remap_key = KEY_LEFT;
  136. } break;
  137. case KEY_P: {
  138. remap_key = KEY_UP;
  139. } break;
  140. case KEY_N: {
  141. remap_key = KEY_DOWN;
  142. } break;
  143. case KEY_D: {
  144. remap_key = KEY_DELETE;
  145. } break;
  146. case KEY_H: {
  147. remap_key = KEY_BACKSPACE;
  148. } break;
  149. }
  150. if (remap_key != KEY_UNKNOWN) {
  151. k->set_scancode(remap_key);
  152. k->set_control(false);
  153. }
  154. }
  155. #endif
  156. unsigned int code = k->get_scancode();
  157. if (k->get_command() && is_shortcut_keys_enabled()) {
  158. bool handled = true;
  159. switch (code) {
  160. case (KEY_X): { // CUT.
  161. if (editable) {
  162. cut_text();
  163. }
  164. } break;
  165. case (KEY_C): { // COPY.
  166. copy_text();
  167. } break;
  168. case (KEY_V): { // PASTE.
  169. if (editable) {
  170. paste_text();
  171. }
  172. } break;
  173. case (KEY_Z): { // Undo/redo.
  174. if (editable) {
  175. if (k->get_shift()) {
  176. redo();
  177. } else {
  178. undo();
  179. }
  180. }
  181. } break;
  182. case (KEY_U): { // Delete from start to cursor.
  183. if (editable) {
  184. deselect();
  185. text = text.substr(cursor_pos, text.length() - cursor_pos);
  186. Ref<Font> font = get_font("font");
  187. cached_width = 0;
  188. if (font != NULL) {
  189. for (int i = 0; i < text.length(); i++)
  190. cached_width += font->get_char_size(text[i]).width;
  191. }
  192. set_cursor_position(0);
  193. _text_changed();
  194. }
  195. } break;
  196. case (KEY_Y): { // PASTE (Yank for unix users).
  197. if (editable) {
  198. paste_text();
  199. }
  200. } break;
  201. case (KEY_K): { // Delete from cursor_pos to end.
  202. if (editable) {
  203. deselect();
  204. text = text.substr(0, cursor_pos);
  205. _text_changed();
  206. }
  207. } break;
  208. case (KEY_A): { // Select all.
  209. select();
  210. } break;
  211. #ifdef APPLE_STYLE_KEYS
  212. case (KEY_LEFT): { // Go to start of text - like HOME key.
  213. set_cursor_position(0);
  214. } break;
  215. case (KEY_RIGHT): { // Go to end of text - like END key.
  216. set_cursor_position(text.length());
  217. } break;
  218. #endif
  219. default: {
  220. handled = false;
  221. }
  222. }
  223. if (handled) {
  224. accept_event();
  225. return;
  226. }
  227. }
  228. _reset_caret_blink_timer();
  229. if (!k->get_metakey()) {
  230. bool handled = true;
  231. switch (code) {
  232. case KEY_KP_ENTER:
  233. case KEY_ENTER: {
  234. emit_signal("text_entered", text);
  235. if (OS::get_singleton()->has_virtual_keyboard())
  236. OS::get_singleton()->hide_virtual_keyboard();
  237. return;
  238. } break;
  239. case KEY_BACKSPACE: {
  240. if (!editable)
  241. break;
  242. if (selection.enabled) {
  243. selection_delete();
  244. break;
  245. }
  246. #ifdef APPLE_STYLE_KEYS
  247. if (k->get_alt()) {
  248. #else
  249. if (k->get_alt()) {
  250. handled = false;
  251. break;
  252. } else if (k->get_command()) {
  253. #endif
  254. int cc = cursor_pos;
  255. bool prev_char = false;
  256. while (cc > 0) {
  257. bool ischar = _is_text_char(text[cc - 1]);
  258. if (prev_char && !ischar)
  259. break;
  260. prev_char = ischar;
  261. cc--;
  262. }
  263. delete_text(cc, cursor_pos);
  264. set_cursor_position(cc);
  265. } else {
  266. delete_char();
  267. }
  268. } break;
  269. case KEY_KP_4: {
  270. if (k->get_unicode() != 0) {
  271. handled = false;
  272. break;
  273. }
  274. FALLTHROUGH;
  275. }
  276. case KEY_LEFT: {
  277. #ifndef APPLE_STYLE_KEYS
  278. if (!k->get_alt())
  279. #endif
  280. shift_selection_check_pre(k->get_shift());
  281. #ifdef APPLE_STYLE_KEYS
  282. if (k->get_command()) {
  283. set_cursor_position(0);
  284. } else if (k->get_alt()) {
  285. #else
  286. if (k->get_alt()) {
  287. handled = false;
  288. break;
  289. } else if (k->get_command()) {
  290. #endif
  291. bool prev_char = false;
  292. int cc = cursor_pos;
  293. while (cc > 0) {
  294. bool ischar = _is_text_char(text[cc - 1]);
  295. if (prev_char && !ischar)
  296. break;
  297. prev_char = ischar;
  298. cc--;
  299. }
  300. set_cursor_position(cc);
  301. } else {
  302. set_cursor_position(get_cursor_position() - 1);
  303. }
  304. shift_selection_check_post(k->get_shift());
  305. } break;
  306. case KEY_KP_6: {
  307. if (k->get_unicode() != 0) {
  308. handled = false;
  309. break;
  310. }
  311. FALLTHROUGH;
  312. }
  313. case KEY_RIGHT: {
  314. shift_selection_check_pre(k->get_shift());
  315. #ifdef APPLE_STYLE_KEYS
  316. if (k->get_command()) {
  317. set_cursor_position(text.length());
  318. } else if (k->get_alt()) {
  319. #else
  320. if (k->get_alt()) {
  321. handled = false;
  322. break;
  323. } else if (k->get_command()) {
  324. #endif
  325. bool prev_char = false;
  326. int cc = cursor_pos;
  327. while (cc < text.length()) {
  328. bool ischar = _is_text_char(text[cc]);
  329. if (prev_char && !ischar)
  330. break;
  331. prev_char = ischar;
  332. cc++;
  333. }
  334. set_cursor_position(cc);
  335. } else {
  336. set_cursor_position(get_cursor_position() + 1);
  337. }
  338. shift_selection_check_post(k->get_shift());
  339. } break;
  340. case KEY_UP: {
  341. shift_selection_check_pre(k->get_shift());
  342. if (get_cursor_position() == 0) handled = false;
  343. set_cursor_position(0);
  344. shift_selection_check_post(k->get_shift());
  345. } break;
  346. case KEY_DOWN: {
  347. shift_selection_check_pre(k->get_shift());
  348. if (get_cursor_position() == text.length()) handled = false;
  349. set_cursor_position(text.length());
  350. shift_selection_check_post(k->get_shift());
  351. } break;
  352. case KEY_DELETE: {
  353. if (!editable)
  354. break;
  355. if (k->get_shift() && !k->get_command() && !k->get_alt()) {
  356. cut_text();
  357. break;
  358. }
  359. if (selection.enabled) {
  360. selection_delete();
  361. break;
  362. }
  363. int text_len = text.length();
  364. if (cursor_pos == text_len)
  365. break; // Nothing to do.
  366. #ifdef APPLE_STYLE_KEYS
  367. if (k->get_alt()) {
  368. #else
  369. if (k->get_alt()) {
  370. handled = false;
  371. break;
  372. } else if (k->get_command()) {
  373. #endif
  374. int cc = cursor_pos;
  375. bool prev_char = false;
  376. while (cc < text.length()) {
  377. bool ischar = _is_text_char(text[cc]);
  378. if (prev_char && !ischar)
  379. break;
  380. prev_char = ischar;
  381. cc++;
  382. }
  383. delete_text(cursor_pos, cc);
  384. } else {
  385. set_cursor_position(cursor_pos + 1);
  386. delete_char();
  387. }
  388. } break;
  389. case KEY_KP_7: {
  390. if (k->get_unicode() != 0) {
  391. handled = false;
  392. break;
  393. }
  394. FALLTHROUGH;
  395. }
  396. case KEY_HOME: {
  397. shift_selection_check_pre(k->get_shift());
  398. set_cursor_position(0);
  399. shift_selection_check_post(k->get_shift());
  400. } break;
  401. case KEY_KP_1: {
  402. if (k->get_unicode() != 0) {
  403. handled = false;
  404. break;
  405. }
  406. FALLTHROUGH;
  407. }
  408. case KEY_END: {
  409. shift_selection_check_pre(k->get_shift());
  410. set_cursor_position(text.length());
  411. shift_selection_check_post(k->get_shift());
  412. } break;
  413. case KEY_MENU: {
  414. if (context_menu_enabled) {
  415. Point2 pos = Point2(get_cursor_pixel_pos(), (get_size().y + get_font("font")->get_height()) / 2);
  416. menu->set_position(get_global_transform().xform(pos));
  417. menu->set_size(Vector2(1, 1));
  418. menu->set_scale(get_global_transform().get_scale());
  419. menu->popup();
  420. menu->grab_focus();
  421. }
  422. } break;
  423. default: {
  424. handled = false;
  425. } break;
  426. }
  427. if (handled) {
  428. accept_event();
  429. } else if (!k->get_command()) {
  430. if (k->get_unicode() >= 32 && k->get_scancode() != KEY_DELETE) {
  431. if (editable) {
  432. selection_delete();
  433. CharType ucodestr[2] = { (CharType)k->get_unicode(), 0 };
  434. int prev_len = text.length();
  435. append_at_cursor(ucodestr);
  436. if (text.length() != prev_len) {
  437. _text_changed();
  438. }
  439. accept_event();
  440. }
  441. } else {
  442. return;
  443. }
  444. }
  445. update();
  446. }
  447. return;
  448. }
  449. }
  450. void LineEdit::set_align(Align p_align) {
  451. ERR_FAIL_INDEX((int)p_align, 4);
  452. align = p_align;
  453. update();
  454. }
  455. LineEdit::Align LineEdit::get_align() const {
  456. return align;
  457. }
  458. Variant LineEdit::get_drag_data(const Point2 &p_point) {
  459. if (selection.drag_attempt && selection.enabled) {
  460. String t = text.substr(selection.begin, selection.end - selection.begin);
  461. Label *l = memnew(Label);
  462. l->set_text(t);
  463. set_drag_preview(l);
  464. return t;
  465. }
  466. return Variant();
  467. }
  468. bool LineEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
  469. return p_data.get_type() == Variant::STRING;
  470. }
  471. void LineEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
  472. if (p_data.get_type() == Variant::STRING) {
  473. set_cursor_at_pixel_pos(p_point.x);
  474. int selected = selection.end - selection.begin;
  475. Ref<Font> font = get_font("font");
  476. if (font != NULL) {
  477. for (int i = selection.begin; i < selection.end; i++)
  478. cached_width -= font->get_char_size(text[i]).width;
  479. }
  480. text.erase(selection.begin, selected);
  481. append_at_cursor(p_data);
  482. selection.begin = cursor_pos - selected;
  483. selection.end = cursor_pos;
  484. }
  485. }
  486. Control::CursorShape LineEdit::get_cursor_shape(const Point2 &p_pos) const {
  487. if (!text.empty() && is_editable() && _is_over_clear_button(p_pos)) {
  488. return CURSOR_ARROW;
  489. }
  490. return Control::get_cursor_shape(p_pos);
  491. }
  492. bool LineEdit::_is_over_clear_button(const Point2 &p_pos) const {
  493. if (!clear_button_enabled || !has_point(p_pos)) {
  494. return false;
  495. }
  496. Ref<Texture> icon = Control::get_icon("clear");
  497. int x_ofs = get_stylebox("normal")->get_offset().x;
  498. return p_pos.x > get_size().width - icon->get_width() - x_ofs;
  499. }
  500. void LineEdit::_notification(int p_what) {
  501. switch (p_what) {
  502. #ifdef TOOLS_ENABLED
  503. case NOTIFICATION_ENTER_TREE: {
  504. if (Engine::get_singleton()->is_editor_hint() && !get_tree()->is_node_being_edited(this)) {
  505. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  506. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  507. if (!EditorSettings::get_singleton()->is_connected("settings_changed", this, "_editor_settings_changed")) {
  508. EditorSettings::get_singleton()->connect("settings_changed", this, "_editor_settings_changed");
  509. }
  510. }
  511. } break;
  512. #endif
  513. case NOTIFICATION_RESIZED: {
  514. window_pos = 0;
  515. set_cursor_position(get_cursor_position());
  516. } break;
  517. case NOTIFICATION_TRANSLATION_CHANGED: {
  518. placeholder_translated = tr(placeholder);
  519. update_placeholder_width();
  520. update();
  521. } break;
  522. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  523. window_has_focus = true;
  524. draw_caret = true;
  525. update();
  526. } break;
  527. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  528. window_has_focus = false;
  529. draw_caret = false;
  530. update();
  531. } break;
  532. case NOTIFICATION_DRAW: {
  533. if ((!has_focus() && !menu->has_focus()) || !window_has_focus) {
  534. draw_caret = false;
  535. }
  536. int width, height;
  537. Size2 size = get_size();
  538. width = size.width;
  539. height = size.height;
  540. RID ci = get_canvas_item();
  541. Ref<StyleBox> style = get_stylebox("normal");
  542. if (!is_editable()) {
  543. style = get_stylebox("read_only");
  544. draw_caret = false;
  545. }
  546. Ref<Font> font = get_font("font");
  547. style->draw(ci, Rect2(Point2(), size));
  548. if (has_focus()) {
  549. get_stylebox("focus")->draw(ci, Rect2(Point2(), size));
  550. }
  551. int x_ofs = 0;
  552. bool using_placeholder = text.empty() && ime_text.empty();
  553. int cached_text_width = using_placeholder ? cached_placeholder_width : cached_width;
  554. switch (align) {
  555. case ALIGN_FILL:
  556. case ALIGN_LEFT: {
  557. x_ofs = style->get_offset().x;
  558. } break;
  559. case ALIGN_CENTER: {
  560. if (window_pos != 0)
  561. x_ofs = style->get_offset().x;
  562. else
  563. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - (cached_text_width)) / 2);
  564. } break;
  565. case ALIGN_RIGHT: {
  566. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - style->get_margin(MARGIN_RIGHT) - (cached_text_width)));
  567. } break;
  568. }
  569. int ofs_max = width - style->get_margin(MARGIN_RIGHT);
  570. int char_ofs = window_pos;
  571. int y_area = height - style->get_minimum_size().height;
  572. int y_ofs = style->get_offset().y + (y_area - font->get_height()) / 2;
  573. int font_ascent = font->get_ascent();
  574. Color selection_color = get_color("selection_color");
  575. Color font_color = is_editable() ? get_color("font_color") : get_color("font_color_uneditable");
  576. Color font_color_selected = get_color("font_color_selected");
  577. Color cursor_color = get_color("cursor_color");
  578. const String &t = using_placeholder ? placeholder_translated : text;
  579. // Draw placeholder color.
  580. if (using_placeholder)
  581. font_color.a *= placeholder_alpha;
  582. bool display_clear_icon = !using_placeholder && is_editable() && clear_button_enabled;
  583. if (right_icon.is_valid() || display_clear_icon) {
  584. Ref<Texture> r_icon = display_clear_icon ? Control::get_icon("clear") : right_icon;
  585. Color color_icon(1, 1, 1, !is_editable() ? .5 * .9 : .9);
  586. if (display_clear_icon) {
  587. if (clear_button_status.press_attempt && clear_button_status.pressing_inside) {
  588. color_icon = get_color("clear_button_color_pressed");
  589. } else {
  590. color_icon = get_color("clear_button_color");
  591. }
  592. }
  593. r_icon->draw(ci, Point2(width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT), height / 2 - r_icon->get_height() / 2), color_icon);
  594. if (align == ALIGN_CENTER) {
  595. if (window_pos == 0) {
  596. x_ofs = MAX(style->get_margin(MARGIN_LEFT), int(size.width - cached_text_width - r_icon->get_width() - style->get_margin(MARGIN_RIGHT) * 2) / 2);
  597. }
  598. } else {
  599. x_ofs = MAX(style->get_margin(MARGIN_LEFT), x_ofs - r_icon->get_width() - style->get_margin(MARGIN_RIGHT));
  600. }
  601. ofs_max -= r_icon->get_width();
  602. }
  603. int caret_height = font->get_height() > y_area ? y_area : font->get_height();
  604. FontDrawer drawer(font, Color(1, 1, 1));
  605. while (true) {
  606. // End of string, break.
  607. if (char_ofs >= t.length())
  608. break;
  609. if (char_ofs == cursor_pos) {
  610. if (ime_text.length() > 0) {
  611. int ofs = 0;
  612. while (true) {
  613. if (ofs >= ime_text.length())
  614. break;
  615. CharType cchar = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs];
  616. CharType next = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs + 1];
  617. int im_char_width = font->get_char_size(cchar, next).width;
  618. if ((x_ofs + im_char_width) > ofs_max)
  619. break;
  620. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  621. if (selected) {
  622. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 3)), font_color);
  623. } else {
  624. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 1)), font_color);
  625. }
  626. drawer.draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, font_color);
  627. x_ofs += im_char_width;
  628. ofs++;
  629. }
  630. }
  631. }
  632. CharType cchar = (pass && !text.empty()) ? secret_character[0] : t[char_ofs];
  633. CharType next = (pass && !text.empty()) ? secret_character[0] : t[char_ofs + 1];
  634. int char_width = font->get_char_size(cchar, next).width;
  635. // End of widget, break.
  636. if ((x_ofs + char_width) > ofs_max)
  637. break;
  638. bool selected = selection.enabled && char_ofs >= selection.begin && char_ofs < selection.end;
  639. if (selected)
  640. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(char_width, caret_height)), selection_color);
  641. int yofs = y_ofs + (caret_height - font->get_height()) / 2;
  642. drawer.draw_char(ci, Point2(x_ofs, yofs + font_ascent), cchar, next, selected ? font_color_selected : font_color);
  643. if (char_ofs == cursor_pos && draw_caret && !using_placeholder) {
  644. if (ime_text.length() == 0) {
  645. #ifdef TOOLS_ENABLED
  646. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(Math::round(EDSCALE), caret_height)), cursor_color);
  647. #else
  648. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(1, caret_height)), cursor_color);
  649. #endif
  650. }
  651. }
  652. x_ofs += char_width;
  653. char_ofs++;
  654. }
  655. if (char_ofs == cursor_pos) {
  656. if (ime_text.length() > 0) {
  657. int ofs = 0;
  658. while (true) {
  659. if (ofs >= ime_text.length())
  660. break;
  661. CharType cchar = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs];
  662. CharType next = (pass && !text.empty()) ? secret_character[0] : ime_text[ofs + 1];
  663. int im_char_width = font->get_char_size(cchar, next).width;
  664. if ((x_ofs + im_char_width) > ofs_max)
  665. break;
  666. bool selected = ofs >= ime_selection.x && ofs < ime_selection.x + ime_selection.y;
  667. if (selected) {
  668. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 3)), font_color);
  669. } else {
  670. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs + caret_height), Size2(im_char_width, 1)), font_color);
  671. }
  672. drawer.draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, font_color);
  673. x_ofs += im_char_width;
  674. ofs++;
  675. }
  676. }
  677. }
  678. if ((char_ofs == cursor_pos || using_placeholder) && draw_caret) { // May be at the end, or placeholder.
  679. if (ime_text.length() == 0) {
  680. int caret_x_ofs = x_ofs;
  681. if (using_placeholder) {
  682. switch (align) {
  683. case ALIGN_LEFT:
  684. case ALIGN_FILL: {
  685. caret_x_ofs = style->get_offset().x;
  686. } break;
  687. case ALIGN_CENTER: {
  688. caret_x_ofs = ofs_max / 2;
  689. } break;
  690. case ALIGN_RIGHT: {
  691. caret_x_ofs = ofs_max;
  692. } break;
  693. }
  694. }
  695. #ifdef TOOLS_ENABLED
  696. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(caret_x_ofs, y_ofs), Size2(Math::round(EDSCALE), caret_height)), cursor_color);
  697. #else
  698. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(caret_x_ofs, y_ofs), Size2(1, caret_height)), cursor_color);
  699. #endif
  700. }
  701. }
  702. if (has_focus()) {
  703. OS::get_singleton()->set_ime_active(true);
  704. OS::get_singleton()->set_ime_position(get_global_position() + Point2(using_placeholder ? 0 : x_ofs, y_ofs + caret_height));
  705. }
  706. } break;
  707. case NOTIFICATION_FOCUS_ENTER: {
  708. if (caret_blink_enabled) {
  709. caret_blink_timer->start();
  710. } else {
  711. draw_caret = true;
  712. }
  713. OS::get_singleton()->set_ime_active(true);
  714. Point2 cursor_pos = Point2(get_cursor_position(), 1) * get_minimum_size().height;
  715. OS::get_singleton()->set_ime_position(get_global_position() + cursor_pos);
  716. if (OS::get_singleton()->has_virtual_keyboard())
  717. OS::get_singleton()->show_virtual_keyboard(text, get_global_rect(), max_length);
  718. } break;
  719. case NOTIFICATION_FOCUS_EXIT: {
  720. if (caret_blink_enabled) {
  721. caret_blink_timer->stop();
  722. }
  723. OS::get_singleton()->set_ime_position(Point2());
  724. OS::get_singleton()->set_ime_active(false);
  725. ime_text = "";
  726. ime_selection = Point2();
  727. if (OS::get_singleton()->has_virtual_keyboard())
  728. OS::get_singleton()->hide_virtual_keyboard();
  729. } break;
  730. case MainLoop::NOTIFICATION_OS_IME_UPDATE: {
  731. if (has_focus()) {
  732. ime_text = OS::get_singleton()->get_ime_text();
  733. ime_selection = OS::get_singleton()->get_ime_selection();
  734. update();
  735. }
  736. } break;
  737. }
  738. }
  739. void LineEdit::copy_text() {
  740. if (selection.enabled && !pass) {
  741. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  742. }
  743. }
  744. void LineEdit::cut_text() {
  745. if (selection.enabled && !pass) {
  746. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  747. selection_delete();
  748. }
  749. }
  750. void LineEdit::paste_text() {
  751. // Strip escape characters like \n and \t as they can't be displayed on LineEdit.
  752. String paste_buffer = OS::get_singleton()->get_clipboard().strip_escapes();
  753. if (paste_buffer != "") {
  754. int prev_len = text.length();
  755. if (selection.enabled) selection_delete();
  756. append_at_cursor(paste_buffer);
  757. if (!text_changed_dirty) {
  758. if (is_inside_tree() && text.length() != prev_len) {
  759. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  760. }
  761. text_changed_dirty = true;
  762. }
  763. }
  764. }
  765. void LineEdit::undo() {
  766. if (undo_stack_pos == NULL) {
  767. if (undo_stack.size() <= 1) {
  768. return;
  769. }
  770. undo_stack_pos = undo_stack.back();
  771. } else if (undo_stack_pos == undo_stack.front()) {
  772. return;
  773. }
  774. undo_stack_pos = undo_stack_pos->prev();
  775. TextOperation op = undo_stack_pos->get();
  776. text = op.text;
  777. cached_width = op.cached_width;
  778. window_pos = op.window_pos;
  779. set_cursor_position(op.cursor_pos);
  780. if (expand_to_text_length)
  781. minimum_size_changed();
  782. _emit_text_change();
  783. }
  784. void LineEdit::redo() {
  785. if (undo_stack_pos == NULL) {
  786. return;
  787. }
  788. if (undo_stack_pos == undo_stack.back()) {
  789. return;
  790. }
  791. undo_stack_pos = undo_stack_pos->next();
  792. TextOperation op = undo_stack_pos->get();
  793. text = op.text;
  794. cached_width = op.cached_width;
  795. window_pos = op.window_pos;
  796. set_cursor_position(op.cursor_pos);
  797. if (expand_to_text_length)
  798. minimum_size_changed();
  799. _emit_text_change();
  800. }
  801. void LineEdit::shift_selection_check_pre(bool p_shift) {
  802. if (!selection.enabled && p_shift) {
  803. selection.cursor_start = cursor_pos;
  804. }
  805. if (!p_shift)
  806. deselect();
  807. }
  808. void LineEdit::shift_selection_check_post(bool p_shift) {
  809. if (p_shift)
  810. selection_fill_at_cursor();
  811. }
  812. void LineEdit::set_cursor_at_pixel_pos(int p_x) {
  813. Ref<Font> font = get_font("font");
  814. int ofs = window_pos;
  815. Ref<StyleBox> style = get_stylebox("normal");
  816. int pixel_ofs = 0;
  817. Size2 size = get_size();
  818. bool display_clear_icon = !text.empty() && is_editable() && clear_button_enabled;
  819. int r_icon_width = Control::get_icon("clear")->get_width();
  820. switch (align) {
  821. case ALIGN_FILL:
  822. case ALIGN_LEFT: {
  823. pixel_ofs = int(style->get_offset().x);
  824. } break;
  825. case ALIGN_CENTER: {
  826. if (window_pos != 0)
  827. pixel_ofs = int(style->get_offset().x);
  828. else
  829. pixel_ofs = int(size.width - (cached_width)) / 2;
  830. if (display_clear_icon)
  831. pixel_ofs -= int(r_icon_width / 2 + style->get_margin(MARGIN_RIGHT));
  832. } break;
  833. case ALIGN_RIGHT: {
  834. pixel_ofs = int(size.width - style->get_margin(MARGIN_RIGHT) - (cached_width));
  835. if (display_clear_icon)
  836. pixel_ofs -= int(r_icon_width + style->get_margin(MARGIN_RIGHT));
  837. } break;
  838. }
  839. while (ofs < text.length()) {
  840. int char_w = 0;
  841. if (font != NULL) {
  842. char_w = font->get_char_size(text[ofs]).width;
  843. }
  844. pixel_ofs += char_w;
  845. if (pixel_ofs > p_x) { // Found what we look for.
  846. break;
  847. }
  848. ofs++;
  849. }
  850. set_cursor_position(ofs);
  851. }
  852. int LineEdit::get_cursor_pixel_pos() {
  853. Ref<Font> font = get_font("font");
  854. int ofs = window_pos;
  855. Ref<StyleBox> style = get_stylebox("normal");
  856. int pixel_ofs = 0;
  857. Size2 size = get_size();
  858. bool display_clear_icon = !text.empty() && is_editable() && clear_button_enabled;
  859. int r_icon_width = Control::get_icon("clear")->get_width();
  860. switch (align) {
  861. case ALIGN_FILL:
  862. case ALIGN_LEFT: {
  863. pixel_ofs = int(style->get_offset().x);
  864. } break;
  865. case ALIGN_CENTER: {
  866. if (window_pos != 0)
  867. pixel_ofs = int(style->get_offset().x);
  868. else
  869. pixel_ofs = int(size.width - (cached_width)) / 2;
  870. if (display_clear_icon)
  871. pixel_ofs -= int(r_icon_width / 2 + style->get_margin(MARGIN_RIGHT));
  872. } break;
  873. case ALIGN_RIGHT: {
  874. pixel_ofs = int(size.width - style->get_margin(MARGIN_RIGHT) - (cached_width));
  875. if (display_clear_icon)
  876. pixel_ofs -= int(r_icon_width + style->get_margin(MARGIN_RIGHT));
  877. } break;
  878. }
  879. while (ofs < cursor_pos) {
  880. if (font != NULL) {
  881. pixel_ofs += font->get_char_size(text[ofs]).width;
  882. }
  883. ofs++;
  884. }
  885. return pixel_ofs;
  886. }
  887. bool LineEdit::cursor_get_blink_enabled() const {
  888. return caret_blink_enabled;
  889. }
  890. void LineEdit::cursor_set_blink_enabled(const bool p_enabled) {
  891. caret_blink_enabled = p_enabled;
  892. if (has_focus()) {
  893. if (p_enabled) {
  894. caret_blink_timer->start();
  895. } else {
  896. caret_blink_timer->stop();
  897. }
  898. }
  899. draw_caret = true;
  900. }
  901. float LineEdit::cursor_get_blink_speed() const {
  902. return caret_blink_timer->get_wait_time();
  903. }
  904. void LineEdit::cursor_set_blink_speed(const float p_speed) {
  905. ERR_FAIL_COND(p_speed <= 0);
  906. caret_blink_timer->set_wait_time(p_speed);
  907. }
  908. void LineEdit::_reset_caret_blink_timer() {
  909. if (caret_blink_enabled) {
  910. draw_caret = true;
  911. if (has_focus()) {
  912. caret_blink_timer->stop();
  913. caret_blink_timer->start();
  914. update();
  915. }
  916. }
  917. }
  918. void LineEdit::_toggle_draw_caret() {
  919. draw_caret = !draw_caret;
  920. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  921. update();
  922. }
  923. }
  924. void LineEdit::delete_char() {
  925. if ((text.length() <= 0) || (cursor_pos == 0)) return;
  926. Ref<Font> font = get_font("font");
  927. if (font != NULL) {
  928. cached_width -= font->get_char_size(text[cursor_pos - 1]).width;
  929. }
  930. text.erase(cursor_pos - 1, 1);
  931. set_cursor_position(get_cursor_position() - 1);
  932. if (align == ALIGN_CENTER || align == ALIGN_RIGHT) {
  933. window_pos = CLAMP(window_pos - 1, 0, MAX(text.length() - 1, 0));
  934. }
  935. _text_changed();
  936. }
  937. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  938. if (text.size() > 0) {
  939. Ref<Font> font = get_font("font");
  940. if (font != NULL) {
  941. for (int i = p_from_column; i < p_to_column; i++)
  942. cached_width -= font->get_char_size(text[i]).width;
  943. }
  944. } else {
  945. cached_width = 0;
  946. }
  947. text.erase(p_from_column, p_to_column - p_from_column);
  948. cursor_pos -= CLAMP(cursor_pos - p_from_column, 0, p_to_column - p_from_column);
  949. if (cursor_pos >= text.length()) {
  950. cursor_pos = text.length();
  951. }
  952. if (window_pos > cursor_pos) {
  953. window_pos = cursor_pos;
  954. }
  955. if (align == ALIGN_CENTER || align == ALIGN_RIGHT) {
  956. window_pos = CLAMP(window_pos - (p_to_column - p_from_column), 0, MAX(text.length() - 1, 0));
  957. }
  958. if (!text_changed_dirty) {
  959. if (is_inside_tree()) {
  960. MessageQueue::get_singleton()->push_call(this, "_text_changed");
  961. }
  962. text_changed_dirty = true;
  963. }
  964. }
  965. void LineEdit::set_text(String p_text) {
  966. clear_internal();
  967. append_at_cursor(p_text);
  968. if (expand_to_text_length) {
  969. minimum_size_changed();
  970. }
  971. update();
  972. cursor_pos = 0;
  973. window_pos = 0;
  974. }
  975. void LineEdit::clear() {
  976. clear_internal();
  977. _text_changed();
  978. }
  979. String LineEdit::get_text() const {
  980. return text;
  981. }
  982. void LineEdit::set_placeholder(String p_text) {
  983. placeholder = p_text;
  984. placeholder_translated = tr(placeholder);
  985. update_placeholder_width();
  986. update();
  987. }
  988. String LineEdit::get_placeholder() const {
  989. return placeholder;
  990. }
  991. void LineEdit::set_placeholder_alpha(float p_alpha) {
  992. placeholder_alpha = p_alpha;
  993. update();
  994. }
  995. float LineEdit::get_placeholder_alpha() const {
  996. return placeholder_alpha;
  997. }
  998. void LineEdit::set_cursor_position(int p_pos) {
  999. if (p_pos > (int)text.length())
  1000. p_pos = text.length();
  1001. if (p_pos < 0)
  1002. p_pos = 0;
  1003. cursor_pos = p_pos;
  1004. if (!is_inside_tree()) {
  1005. window_pos = cursor_pos;
  1006. return;
  1007. }
  1008. Ref<StyleBox> style = get_stylebox("normal");
  1009. Ref<Font> font = get_font("font");
  1010. if (cursor_pos <= window_pos) {
  1011. // Adjust window if cursor goes too much to the left.
  1012. set_window_pos(MAX(0, cursor_pos - 1));
  1013. } else {
  1014. // Adjust window if cursor goes too much to the right.
  1015. int window_width = get_size().width - style->get_minimum_size().width;
  1016. bool display_clear_icon = !text.empty() && is_editable() && clear_button_enabled;
  1017. if (right_icon.is_valid() || display_clear_icon) {
  1018. Ref<Texture> r_icon = display_clear_icon ? Control::get_icon("clear") : right_icon;
  1019. window_width -= r_icon->get_width();
  1020. }
  1021. if (window_width < 0)
  1022. return;
  1023. int wp = window_pos;
  1024. if (font.is_valid()) {
  1025. int accum_width = 0;
  1026. for (int i = cursor_pos; i >= window_pos; i--) {
  1027. if (i >= text.length()) {
  1028. // Do not do this, because if the cursor is at the end, its just fine that it takes no space.
  1029. // accum_width = font->get_char_size(' ').width;
  1030. } else {
  1031. accum_width += font->get_char_size(text[i], i + 1 < text.length() ? text[i + 1] : 0).width; // Anything should do.
  1032. }
  1033. if (accum_width > window_width)
  1034. break;
  1035. wp = i;
  1036. }
  1037. }
  1038. if (wp != window_pos)
  1039. set_window_pos(wp);
  1040. }
  1041. update();
  1042. }
  1043. int LineEdit::get_cursor_position() const {
  1044. return cursor_pos;
  1045. }
  1046. void LineEdit::set_window_pos(int p_pos) {
  1047. window_pos = p_pos;
  1048. if (window_pos < 0) window_pos = 0;
  1049. }
  1050. void LineEdit::append_at_cursor(String p_text) {
  1051. if ((max_length <= 0) || (text.length() + p_text.length() <= max_length)) {
  1052. Ref<Font> font = get_font("font");
  1053. if (font != NULL) {
  1054. for (int i = 0; i < p_text.length(); i++)
  1055. cached_width += font->get_char_size(p_text[i]).width;
  1056. } else {
  1057. cached_width = 0;
  1058. }
  1059. String pre = text.substr(0, cursor_pos);
  1060. String post = text.substr(cursor_pos, text.length() - cursor_pos);
  1061. text = pre + p_text + post;
  1062. set_cursor_position(cursor_pos + p_text.length());
  1063. } else {
  1064. emit_signal("text_change_rejected");
  1065. }
  1066. }
  1067. void LineEdit::clear_internal() {
  1068. deselect();
  1069. _clear_undo_stack();
  1070. cached_width = 0;
  1071. cursor_pos = 0;
  1072. window_pos = 0;
  1073. undo_text = "";
  1074. text = "";
  1075. update();
  1076. }
  1077. Size2 LineEdit::get_minimum_size() const {
  1078. Ref<StyleBox> style = get_stylebox("normal");
  1079. Ref<Font> font = get_font("font");
  1080. Size2 min_size;
  1081. // Minimum size of text.
  1082. int space_size = font->get_char_size(' ').x;
  1083. min_size.width = get_constant("minimum_spaces") * space_size;
  1084. if (expand_to_text_length) {
  1085. // Add a space because some fonts are too exact, and because cursor needs a bit more when at the end.
  1086. min_size.width = MAX(min_size.width, font->get_string_size(text).x + space_size);
  1087. }
  1088. min_size.height = font->get_height();
  1089. // Take icons into account.
  1090. if (!text.empty() && is_editable() && clear_button_enabled) {
  1091. min_size.width = MAX(min_size.width, Control::get_icon("clear")->get_width());
  1092. min_size.height = MAX(min_size.height, Control::get_icon("clear")->get_height());
  1093. }
  1094. if (right_icon.is_valid()) {
  1095. min_size.width = MAX(min_size.width, right_icon->get_width());
  1096. min_size.height = MAX(min_size.height, right_icon->get_height());
  1097. }
  1098. return style->get_minimum_size() + min_size;
  1099. }
  1100. void LineEdit::deselect() {
  1101. selection.begin = 0;
  1102. selection.end = 0;
  1103. selection.cursor_start = 0;
  1104. selection.enabled = false;
  1105. selection.creating = false;
  1106. selection.doubleclick = false;
  1107. update();
  1108. }
  1109. void LineEdit::selection_delete() {
  1110. if (selection.enabled)
  1111. delete_text(selection.begin, selection.end);
  1112. deselect();
  1113. }
  1114. void LineEdit::set_max_length(int p_max_length) {
  1115. ERR_FAIL_COND(p_max_length < 0);
  1116. max_length = p_max_length;
  1117. set_text(text);
  1118. }
  1119. int LineEdit::get_max_length() const {
  1120. return max_length;
  1121. }
  1122. void LineEdit::selection_fill_at_cursor() {
  1123. if (!selecting_enabled)
  1124. return;
  1125. selection.begin = cursor_pos;
  1126. selection.end = selection.cursor_start;
  1127. if (selection.end < selection.begin) {
  1128. int aux = selection.end;
  1129. selection.end = selection.begin;
  1130. selection.begin = aux;
  1131. }
  1132. selection.enabled = (selection.begin != selection.end);
  1133. }
  1134. void LineEdit::select_all() {
  1135. if (!selecting_enabled)
  1136. return;
  1137. if (!text.length())
  1138. return;
  1139. selection.begin = 0;
  1140. selection.end = text.length();
  1141. selection.enabled = true;
  1142. update();
  1143. }
  1144. void LineEdit::set_editable(bool p_editable) {
  1145. if (editable == p_editable)
  1146. return;
  1147. editable = p_editable;
  1148. _generate_context_menu();
  1149. minimum_size_changed();
  1150. update();
  1151. }
  1152. bool LineEdit::is_editable() const {
  1153. return editable;
  1154. }
  1155. void LineEdit::set_secret(bool p_secret) {
  1156. pass = p_secret;
  1157. update();
  1158. }
  1159. bool LineEdit::is_secret() const {
  1160. return pass;
  1161. }
  1162. void LineEdit::set_secret_character(const String &p_string) {
  1163. // An empty string as the secret character would crash the engine.
  1164. // It also wouldn't make sense to use multiple characters as the secret character.
  1165. ERR_FAIL_COND_MSG(p_string.length() != 1, "Secret character must be exactly one character long (" + itos(p_string.length()) + " characters given).");
  1166. secret_character = p_string;
  1167. update();
  1168. }
  1169. String LineEdit::get_secret_character() const {
  1170. return secret_character;
  1171. }
  1172. void LineEdit::select(int p_from, int p_to) {
  1173. if (!selecting_enabled)
  1174. return;
  1175. if (p_from == 0 && p_to == 0) {
  1176. deselect();
  1177. return;
  1178. }
  1179. int len = text.length();
  1180. if (p_from < 0)
  1181. p_from = 0;
  1182. if (p_from > len)
  1183. p_from = len;
  1184. if (p_to < 0 || p_to > len)
  1185. p_to = len;
  1186. if (p_from >= p_to)
  1187. return;
  1188. selection.enabled = true;
  1189. selection.begin = p_from;
  1190. selection.end = p_to;
  1191. selection.creating = false;
  1192. selection.doubleclick = false;
  1193. update();
  1194. }
  1195. bool LineEdit::is_text_field() const {
  1196. return true;
  1197. }
  1198. void LineEdit::menu_option(int p_option) {
  1199. switch (p_option) {
  1200. case MENU_CUT: {
  1201. if (editable) {
  1202. cut_text();
  1203. }
  1204. } break;
  1205. case MENU_COPY: {
  1206. copy_text();
  1207. } break;
  1208. case MENU_PASTE: {
  1209. if (editable) {
  1210. paste_text();
  1211. }
  1212. } break;
  1213. case MENU_CLEAR: {
  1214. if (editable) {
  1215. clear();
  1216. }
  1217. } break;
  1218. case MENU_SELECT_ALL: {
  1219. select_all();
  1220. } break;
  1221. case MENU_UNDO: {
  1222. if (editable) {
  1223. undo();
  1224. }
  1225. } break;
  1226. case MENU_REDO: {
  1227. if (editable) {
  1228. redo();
  1229. }
  1230. }
  1231. }
  1232. }
  1233. void LineEdit::set_context_menu_enabled(bool p_enable) {
  1234. context_menu_enabled = p_enable;
  1235. }
  1236. bool LineEdit::is_context_menu_enabled() {
  1237. return context_menu_enabled;
  1238. }
  1239. PopupMenu *LineEdit::get_menu() const {
  1240. return menu;
  1241. }
  1242. void LineEdit::_editor_settings_changed() {
  1243. #ifdef TOOLS_ENABLED
  1244. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  1245. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  1246. #endif
  1247. }
  1248. void LineEdit::set_expand_to_text_length(bool p_enabled) {
  1249. expand_to_text_length = p_enabled;
  1250. minimum_size_changed();
  1251. set_window_pos(0);
  1252. }
  1253. bool LineEdit::get_expand_to_text_length() const {
  1254. return expand_to_text_length;
  1255. }
  1256. void LineEdit::set_clear_button_enabled(bool p_enabled) {
  1257. if (clear_button_enabled == p_enabled) {
  1258. return;
  1259. }
  1260. clear_button_enabled = p_enabled;
  1261. minimum_size_changed();
  1262. update();
  1263. }
  1264. bool LineEdit::is_clear_button_enabled() const {
  1265. return clear_button_enabled;
  1266. }
  1267. void LineEdit::set_shortcut_keys_enabled(bool p_enabled) {
  1268. shortcut_keys_enabled = p_enabled;
  1269. _generate_context_menu();
  1270. }
  1271. bool LineEdit::is_shortcut_keys_enabled() const {
  1272. return shortcut_keys_enabled;
  1273. }
  1274. void LineEdit::set_selecting_enabled(bool p_enabled) {
  1275. selecting_enabled = p_enabled;
  1276. if (!selecting_enabled)
  1277. deselect();
  1278. _generate_context_menu();
  1279. }
  1280. bool LineEdit::is_selecting_enabled() const {
  1281. return selecting_enabled;
  1282. }
  1283. void LineEdit::set_right_icon(const Ref<Texture> &p_icon) {
  1284. if (right_icon == p_icon) {
  1285. return;
  1286. }
  1287. right_icon = p_icon;
  1288. minimum_size_changed();
  1289. update();
  1290. }
  1291. Ref<Texture> LineEdit::get_right_icon() {
  1292. return right_icon;
  1293. }
  1294. void LineEdit::_text_changed() {
  1295. if (expand_to_text_length)
  1296. minimum_size_changed();
  1297. _emit_text_change();
  1298. _clear_redo();
  1299. }
  1300. void LineEdit::_emit_text_change() {
  1301. emit_signal("text_changed", text);
  1302. _change_notify("text");
  1303. text_changed_dirty = false;
  1304. }
  1305. void LineEdit::update_placeholder_width() {
  1306. if ((max_length <= 0) || (placeholder_translated.length() <= max_length)) {
  1307. Ref<Font> font = get_font("font");
  1308. cached_placeholder_width = 0;
  1309. if (font != NULL) {
  1310. for (int i = 0; i < placeholder_translated.length(); i++) {
  1311. cached_placeholder_width += font->get_char_size(placeholder_translated[i]).width;
  1312. }
  1313. }
  1314. }
  1315. }
  1316. void LineEdit::_clear_redo() {
  1317. _create_undo_state();
  1318. if (undo_stack_pos == NULL) {
  1319. return;
  1320. }
  1321. undo_stack_pos = undo_stack_pos->next();
  1322. while (undo_stack_pos) {
  1323. List<TextOperation>::Element *elem = undo_stack_pos;
  1324. undo_stack_pos = undo_stack_pos->next();
  1325. undo_stack.erase(elem);
  1326. }
  1327. _create_undo_state();
  1328. }
  1329. void LineEdit::_clear_undo_stack() {
  1330. undo_stack.clear();
  1331. undo_stack_pos = NULL;
  1332. _create_undo_state();
  1333. }
  1334. void LineEdit::_create_undo_state() {
  1335. TextOperation op;
  1336. op.text = text;
  1337. op.cached_width = cached_width;
  1338. op.cursor_pos = cursor_pos;
  1339. op.window_pos = window_pos;
  1340. undo_stack.push_back(op);
  1341. }
  1342. void LineEdit::_generate_context_menu() {
  1343. // Reorganize context menu.
  1344. menu->clear();
  1345. if (editable)
  1346. menu->add_item(RTR("Cut"), MENU_CUT, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_X : 0);
  1347. menu->add_item(RTR("Copy"), MENU_COPY, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_C : 0);
  1348. if (editable)
  1349. menu->add_item(RTR("Paste"), MENU_PASTE, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_V : 0);
  1350. menu->add_separator();
  1351. if (is_selecting_enabled())
  1352. menu->add_item(RTR("Select All"), MENU_SELECT_ALL, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_A : 0);
  1353. if (editable) {
  1354. menu->add_item(RTR("Clear"), MENU_CLEAR);
  1355. menu->add_separator();
  1356. menu->add_item(RTR("Undo"), MENU_UNDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_Z : 0);
  1357. menu->add_item(RTR("Redo"), MENU_REDO, is_shortcut_keys_enabled() ? KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z : 0);
  1358. }
  1359. }
  1360. void LineEdit::_bind_methods() {
  1361. ClassDB::bind_method(D_METHOD("_text_changed"), &LineEdit::_text_changed);
  1362. ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &LineEdit::_toggle_draw_caret);
  1363. ClassDB::bind_method("_editor_settings_changed", &LineEdit::_editor_settings_changed);
  1364. ClassDB::bind_method(D_METHOD("set_align", "align"), &LineEdit::set_align);
  1365. ClassDB::bind_method(D_METHOD("get_align"), &LineEdit::get_align);
  1366. ClassDB::bind_method(D_METHOD("_gui_input"), &LineEdit::_gui_input);
  1367. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  1368. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  1369. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  1370. ClassDB::bind_method(D_METHOD("deselect"), &LineEdit::deselect);
  1371. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  1372. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  1373. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  1374. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  1375. ClassDB::bind_method(D_METHOD("set_placeholder_alpha", "alpha"), &LineEdit::set_placeholder_alpha);
  1376. ClassDB::bind_method(D_METHOD("get_placeholder_alpha"), &LineEdit::get_placeholder_alpha);
  1377. ClassDB::bind_method(D_METHOD("set_cursor_position", "position"), &LineEdit::set_cursor_position);
  1378. ClassDB::bind_method(D_METHOD("get_cursor_position"), &LineEdit::get_cursor_position);
  1379. ClassDB::bind_method(D_METHOD("set_expand_to_text_length", "enabled"), &LineEdit::set_expand_to_text_length);
  1380. ClassDB::bind_method(D_METHOD("get_expand_to_text_length"), &LineEdit::get_expand_to_text_length);
  1381. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enabled"), &LineEdit::cursor_set_blink_enabled);
  1382. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &LineEdit::cursor_get_blink_enabled);
  1383. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &LineEdit::cursor_set_blink_speed);
  1384. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &LineEdit::cursor_get_blink_speed);
  1385. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  1386. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  1387. ClassDB::bind_method(D_METHOD("append_at_cursor", "text"), &LineEdit::append_at_cursor);
  1388. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  1389. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  1390. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  1391. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  1392. ClassDB::bind_method(D_METHOD("set_secret_character", "character"), &LineEdit::set_secret_character);
  1393. ClassDB::bind_method(D_METHOD("get_secret_character"), &LineEdit::get_secret_character);
  1394. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  1395. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  1396. ClassDB::bind_method(D_METHOD("set_context_menu_enabled", "enable"), &LineEdit::set_context_menu_enabled);
  1397. ClassDB::bind_method(D_METHOD("is_context_menu_enabled"), &LineEdit::is_context_menu_enabled);
  1398. ClassDB::bind_method(D_METHOD("set_clear_button_enabled", "enable"), &LineEdit::set_clear_button_enabled);
  1399. ClassDB::bind_method(D_METHOD("is_clear_button_enabled"), &LineEdit::is_clear_button_enabled);
  1400. ClassDB::bind_method(D_METHOD("set_shortcut_keys_enabled", "enable"), &LineEdit::set_shortcut_keys_enabled);
  1401. ClassDB::bind_method(D_METHOD("is_shortcut_keys_enabled"), &LineEdit::is_shortcut_keys_enabled);
  1402. ClassDB::bind_method(D_METHOD("set_selecting_enabled", "enable"), &LineEdit::set_selecting_enabled);
  1403. ClassDB::bind_method(D_METHOD("is_selecting_enabled"), &LineEdit::is_selecting_enabled);
  1404. ClassDB::bind_method(D_METHOD("set_right_icon", "icon"), &LineEdit::set_right_icon);
  1405. ClassDB::bind_method(D_METHOD("get_right_icon"), &LineEdit::get_right_icon);
  1406. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "new_text")));
  1407. ADD_SIGNAL(MethodInfo("text_change_rejected"));
  1408. ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "new_text")));
  1409. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  1410. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  1411. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  1412. BIND_ENUM_CONSTANT(ALIGN_FILL);
  1413. BIND_ENUM_CONSTANT(MENU_CUT);
  1414. BIND_ENUM_CONSTANT(MENU_COPY);
  1415. BIND_ENUM_CONSTANT(MENU_PASTE);
  1416. BIND_ENUM_CONSTANT(MENU_CLEAR);
  1417. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  1418. BIND_ENUM_CONSTANT(MENU_UNDO);
  1419. BIND_ENUM_CONSTANT(MENU_REDO);
  1420. BIND_ENUM_CONSTANT(MENU_MAX);
  1421. ADD_PROPERTY(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  1422. ADD_PROPERTY(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  1423. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_length"), "set_max_length", "get_max_length");
  1424. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  1425. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret");
  1426. ADD_PROPERTY(PropertyInfo(Variant::STRING, "secret_character"), "set_secret_character", "get_secret_character");
  1427. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "expand_to_text_length"), "set_expand_to_text_length", "get_expand_to_text_length");
  1428. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "context_menu_enabled"), "set_context_menu_enabled", "is_context_menu_enabled");
  1429. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clear_button_enabled"), "set_clear_button_enabled", "is_clear_button_enabled");
  1430. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "shortcut_keys_enabled"), "set_shortcut_keys_enabled", "is_shortcut_keys_enabled");
  1431. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "selecting_enabled"), "set_selecting_enabled", "is_selecting_enabled");
  1432. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "right_icon", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_right_icon", "get_right_icon");
  1433. ADD_GROUP("Placeholder", "placeholder_");
  1434. ADD_PROPERTY(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  1435. ADD_PROPERTY(PropertyInfo(Variant::REAL, "placeholder_alpha", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_placeholder_alpha", "get_placeholder_alpha");
  1436. ADD_GROUP("Caret", "caret_");
  1437. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  1438. ADD_PROPERTY(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.01"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  1439. ADD_PROPERTY(PropertyInfo(Variant::INT, "caret_position"), "set_cursor_position", "get_cursor_position");
  1440. }
  1441. LineEdit::LineEdit() {
  1442. undo_stack_pos = NULL;
  1443. _create_undo_state();
  1444. align = ALIGN_LEFT;
  1445. cached_width = 0;
  1446. cached_placeholder_width = 0;
  1447. cursor_pos = 0;
  1448. window_pos = 0;
  1449. window_has_focus = true;
  1450. max_length = 0;
  1451. pass = false;
  1452. secret_character = "*";
  1453. text_changed_dirty = false;
  1454. placeholder_alpha = 0.6;
  1455. clear_button_enabled = false;
  1456. clear_button_status.press_attempt = false;
  1457. clear_button_status.pressing_inside = false;
  1458. shortcut_keys_enabled = true;
  1459. selecting_enabled = true;
  1460. deselect();
  1461. set_focus_mode(FOCUS_ALL);
  1462. set_default_cursor_shape(CURSOR_IBEAM);
  1463. set_mouse_filter(MOUSE_FILTER_STOP);
  1464. draw_caret = true;
  1465. caret_blink_enabled = false;
  1466. caret_blink_timer = memnew(Timer);
  1467. add_child(caret_blink_timer);
  1468. caret_blink_timer->set_wait_time(0.65);
  1469. caret_blink_timer->connect("timeout", this, "_toggle_draw_caret");
  1470. cursor_set_blink_enabled(false);
  1471. context_menu_enabled = true;
  1472. menu = memnew(PopupMenu);
  1473. add_child(menu);
  1474. editable = false; // Initialise to opposite first, so we get past the early-out in set_editable.
  1475. set_editable(true);
  1476. menu->connect("id_pressed", this, "menu_option");
  1477. expand_to_text_length = false;
  1478. }
  1479. LineEdit::~LineEdit() {
  1480. }