line_edit.cpp 36 KB

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