line_edit.cpp 42 KB

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