line_edit.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475
  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) {
  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. selection_clear();
  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. selection_clear();
  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. selection_clear();
  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. selection_clear();
  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. selection_clear();
  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. int new_cursor_pos=p_x;
  671. int charwidth=draw_area->get_font_char_width(' ',0);
  672. new_cursor_pos=( ( (new_cursor_pos-2)+ (charwidth/2) ) /charwidth );
  673. if (new_cursor_pos>(int)text.length()) new_cursor_pos=text.length();
  674. set_cursor_position(window_pos+new_cursor_pos); */
  675. }
  676. bool LineEdit::cursor_get_blink_enabled() const {
  677. return caret_blink_enabled;
  678. }
  679. void LineEdit::cursor_set_blink_enabled(const bool p_enabled) {
  680. caret_blink_enabled = p_enabled;
  681. if (p_enabled) {
  682. caret_blink_timer->start();
  683. } else {
  684. caret_blink_timer->stop();
  685. }
  686. draw_caret = true;
  687. }
  688. float LineEdit::cursor_get_blink_speed() const {
  689. return caret_blink_timer->get_wait_time();
  690. }
  691. void LineEdit::cursor_set_blink_speed(const float p_speed) {
  692. ERR_FAIL_COND(p_speed <= 0);
  693. caret_blink_timer->set_wait_time(p_speed);
  694. }
  695. void LineEdit::_reset_caret_blink_timer() {
  696. if (caret_blink_enabled) {
  697. caret_blink_timer->stop();
  698. caret_blink_timer->start();
  699. draw_caret = true;
  700. update();
  701. }
  702. }
  703. void LineEdit::_toggle_draw_caret() {
  704. draw_caret = !draw_caret;
  705. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  706. update();
  707. }
  708. }
  709. void LineEdit::delete_char() {
  710. if ((text.length() <= 0) || (cursor_pos == 0)) return;
  711. Ref<Font> font = get_font("font");
  712. if (font != NULL) {
  713. cached_width -= font->get_char_size(text[cursor_pos - 1]).width;
  714. }
  715. text.erase(cursor_pos - 1, 1);
  716. set_cursor_position(get_cursor_position() - 1);
  717. if (cursor_pos == window_pos) {
  718. //set_window_pos(cursor_pos-get_window_length());
  719. }
  720. _text_changed();
  721. }
  722. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  723. if (text.size() > 0) {
  724. Ref<Font> font = get_font("font");
  725. if (font != NULL) {
  726. for (int i = p_from_column; i < p_to_column; i++)
  727. cached_width -= font->get_char_size(text[i]).width;
  728. }
  729. } else {
  730. cached_width = 0;
  731. }
  732. text.erase(p_from_column, p_to_column - p_from_column);
  733. cursor_pos -= CLAMP(cursor_pos - p_from_column, 0, p_to_column - p_from_column);
  734. if (cursor_pos >= text.length()) {
  735. cursor_pos = text.length();
  736. }
  737. if (window_pos > cursor_pos) {
  738. window_pos = cursor_pos;
  739. }
  740. _text_changed();
  741. }
  742. void LineEdit::set_text(String p_text) {
  743. clear_internal();
  744. append_at_cursor(p_text);
  745. update();
  746. cursor_pos = 0;
  747. window_pos = 0;
  748. _text_changed();
  749. }
  750. void LineEdit::clear() {
  751. clear_internal();
  752. _text_changed();
  753. }
  754. String LineEdit::get_text() const {
  755. return text;
  756. }
  757. void LineEdit::set_placeholder(String p_text) {
  758. placeholder = tr(p_text);
  759. update();
  760. }
  761. String LineEdit::get_placeholder() const {
  762. return placeholder;
  763. }
  764. void LineEdit::set_placeholder_alpha(float p_alpha) {
  765. placeholder_alpha = p_alpha;
  766. update();
  767. }
  768. float LineEdit::get_placeholder_alpha() const {
  769. return placeholder_alpha;
  770. }
  771. void LineEdit::set_cursor_position(int p_pos) {
  772. if (p_pos > (int)text.length())
  773. p_pos = text.length();
  774. if (p_pos < 0)
  775. p_pos = 0;
  776. cursor_pos = p_pos;
  777. if (!is_inside_tree()) {
  778. window_pos = cursor_pos;
  779. return;
  780. }
  781. Ref<StyleBox> style = get_stylebox("normal");
  782. Ref<Font> font = get_font("font");
  783. if (cursor_pos <= window_pos) {
  784. /* Adjust window if cursor goes too much to the left */
  785. if (window_pos > 0)
  786. set_window_pos(window_pos - 1);
  787. } else if (cursor_pos > window_pos) {
  788. /* Adjust window if cursor goes too much to the right */
  789. int window_width = get_size().width - style->get_minimum_size().width;
  790. if (window_width < 0)
  791. return;
  792. int wp = window_pos;
  793. if (font.is_valid()) {
  794. int accum_width = 0;
  795. for (int i = cursor_pos; i >= window_pos; i--) {
  796. if (i >= text.length()) {
  797. accum_width = font->get_char_size(' ').width; //anything should do
  798. } else {
  799. accum_width += font->get_char_size(text[i], i + 1 < text.length() ? text[i + 1] : 0).width; //anything should do
  800. }
  801. if (accum_width >= window_width)
  802. break;
  803. wp = i;
  804. }
  805. }
  806. if (wp != window_pos)
  807. set_window_pos(wp);
  808. }
  809. update();
  810. }
  811. int LineEdit::get_cursor_position() const {
  812. return cursor_pos;
  813. }
  814. void LineEdit::set_window_pos(int p_pos) {
  815. window_pos = p_pos;
  816. if (window_pos < 0) window_pos = 0;
  817. }
  818. void LineEdit::append_at_cursor(String p_text) {
  819. if ((max_length <= 0) || (text.length() + p_text.length() <= max_length)) {
  820. Ref<Font> font = get_font("font");
  821. if (font != NULL) {
  822. for (int i = 0; i < p_text.length(); i++)
  823. cached_width += font->get_char_size(p_text[i]).width;
  824. } else {
  825. cached_width = 0;
  826. }
  827. String pre = text.substr(0, cursor_pos);
  828. String post = text.substr(cursor_pos, text.length() - cursor_pos);
  829. text = pre + p_text + post;
  830. set_cursor_position(cursor_pos + p_text.length());
  831. }
  832. }
  833. void LineEdit::clear_internal() {
  834. _clear_undo_stack();
  835. cached_width = 0;
  836. cursor_pos = 0;
  837. window_pos = 0;
  838. undo_text = "";
  839. text = "";
  840. update();
  841. }
  842. Size2 LineEdit::get_minimum_size() const {
  843. Ref<StyleBox> style = get_stylebox("normal");
  844. Ref<Font> font = get_font("font");
  845. Size2 min = style->get_minimum_size();
  846. min.height += font->get_height();
  847. //minimum size of text
  848. int space_size = font->get_char_size(' ').x;
  849. int mstext = get_constant("minimum_spaces") * space_size;
  850. if (expand_to_text_length) {
  851. mstext = MAX(mstext, font->get_string_size(text).x + space_size); //add a spce because some fonts are too exact
  852. }
  853. min.width += mstext;
  854. return min;
  855. }
  856. /* selection */
  857. void LineEdit::selection_clear() {
  858. selection.begin = 0;
  859. selection.end = 0;
  860. selection.cursor_start = 0;
  861. selection.enabled = false;
  862. selection.creating = false;
  863. selection.doubleclick = false;
  864. update();
  865. }
  866. void LineEdit::selection_delete() {
  867. if (selection.enabled)
  868. delete_text(selection.begin, selection.end);
  869. selection_clear();
  870. }
  871. void LineEdit::set_max_length(int p_max_length) {
  872. ERR_FAIL_COND(p_max_length < 0);
  873. max_length = p_max_length;
  874. set_text(text);
  875. }
  876. int LineEdit::get_max_length() const {
  877. return max_length;
  878. }
  879. void LineEdit::selection_fill_at_cursor() {
  880. int aux;
  881. selection.begin = cursor_pos;
  882. selection.end = selection.cursor_start;
  883. if (selection.end < selection.begin) {
  884. aux = selection.end;
  885. selection.end = selection.begin;
  886. selection.begin = aux;
  887. }
  888. selection.enabled = (selection.begin != selection.end);
  889. }
  890. void LineEdit::select_all() {
  891. if (!text.length())
  892. return;
  893. selection.begin = 0;
  894. selection.end = text.length();
  895. selection.enabled = true;
  896. update();
  897. }
  898. void LineEdit::set_editable(bool p_editable) {
  899. editable = p_editable;
  900. update();
  901. }
  902. bool LineEdit::is_editable() const {
  903. return editable;
  904. }
  905. void LineEdit::set_secret(bool p_secret) {
  906. pass = p_secret;
  907. update();
  908. }
  909. bool LineEdit::is_secret() const {
  910. return pass;
  911. }
  912. void LineEdit::select(int p_from, int p_to) {
  913. if (p_from == 0 && p_to == 0) {
  914. selection_clear();
  915. return;
  916. }
  917. int len = text.length();
  918. if (p_from < 0)
  919. p_from = 0;
  920. if (p_from > len)
  921. p_from = len;
  922. if (p_to < 0 || p_to > len)
  923. p_to = len;
  924. if (p_from >= p_to)
  925. return;
  926. selection.enabled = true;
  927. selection.begin = p_from;
  928. selection.end = p_to;
  929. selection.creating = false;
  930. selection.doubleclick = false;
  931. update();
  932. }
  933. bool LineEdit::is_text_field() const {
  934. return true;
  935. }
  936. void LineEdit::menu_option(int p_option) {
  937. switch (p_option) {
  938. case MENU_CUT: {
  939. if (editable) {
  940. cut_text();
  941. }
  942. } break;
  943. case MENU_COPY: {
  944. copy_text();
  945. } break;
  946. case MENU_PASTE: {
  947. if (editable) {
  948. paste_text();
  949. }
  950. } break;
  951. case MENU_CLEAR: {
  952. if (editable) {
  953. clear();
  954. }
  955. } break;
  956. case MENU_SELECT_ALL: {
  957. select_all();
  958. } break;
  959. case MENU_UNDO: {
  960. if (editable) {
  961. undo();
  962. }
  963. } break;
  964. case MENU_REDO: {
  965. if (editable) {
  966. redo();
  967. }
  968. }
  969. }
  970. }
  971. PopupMenu *LineEdit::get_menu() const {
  972. return menu;
  973. }
  974. #ifdef TOOLS_ENABLED
  975. void LineEdit::_editor_settings_changed() {
  976. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  977. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  978. }
  979. #endif
  980. void LineEdit::set_expand_to_text_length(bool p_enabled) {
  981. expand_to_text_length = p_enabled;
  982. minimum_size_changed();
  983. }
  984. bool LineEdit::get_expand_to_text_length() const {
  985. return expand_to_text_length;
  986. }
  987. void LineEdit::_ime_text_callback(void *p_self, String p_text, Point2 p_selection) {
  988. LineEdit *self = (LineEdit *)p_self;
  989. self->ime_text = p_text;
  990. self->ime_selection = p_selection;
  991. self->update();
  992. }
  993. void LineEdit::_text_changed() {
  994. if (expand_to_text_length)
  995. minimum_size_changed();
  996. _emit_text_change();
  997. _clear_redo();
  998. }
  999. void LineEdit::_emit_text_change() {
  1000. emit_signal("text_changed", text);
  1001. _change_notify("text");
  1002. }
  1003. void LineEdit::_clear_redo() {
  1004. _create_undo_state();
  1005. if (undo_stack_pos == NULL) {
  1006. return;
  1007. }
  1008. undo_stack_pos = undo_stack_pos->next();
  1009. while (undo_stack_pos) {
  1010. List<TextOperation>::Element *elem = undo_stack_pos;
  1011. undo_stack_pos = undo_stack_pos->next();
  1012. undo_stack.erase(elem);
  1013. }
  1014. _create_undo_state();
  1015. }
  1016. void LineEdit::_clear_undo_stack() {
  1017. undo_stack.clear();
  1018. undo_stack_pos = NULL;
  1019. _create_undo_state();
  1020. }
  1021. void LineEdit::_create_undo_state() {
  1022. TextOperation op;
  1023. op.text = text;
  1024. op.cursor_pos = cursor_pos;
  1025. undo_stack.push_back(op);
  1026. }
  1027. void LineEdit::_bind_methods() {
  1028. ClassDB::bind_method(D_METHOD("_toggle_draw_caret"), &LineEdit::_toggle_draw_caret);
  1029. #ifdef TOOLS_ENABLED
  1030. ClassDB::bind_method("_editor_settings_changed", &LineEdit::_editor_settings_changed);
  1031. #endif
  1032. ClassDB::bind_method(D_METHOD("set_align", "align"), &LineEdit::set_align);
  1033. ClassDB::bind_method(D_METHOD("get_align"), &LineEdit::get_align);
  1034. ClassDB::bind_method(D_METHOD("_gui_input"), &LineEdit::_gui_input);
  1035. ClassDB::bind_method(D_METHOD("clear"), &LineEdit::clear);
  1036. ClassDB::bind_method(D_METHOD("select_all"), &LineEdit::select_all);
  1037. ClassDB::bind_method(D_METHOD("set_text", "text"), &LineEdit::set_text);
  1038. ClassDB::bind_method(D_METHOD("get_text"), &LineEdit::get_text);
  1039. ClassDB::bind_method(D_METHOD("set_placeholder", "text"), &LineEdit::set_placeholder);
  1040. ClassDB::bind_method(D_METHOD("get_placeholder"), &LineEdit::get_placeholder);
  1041. ClassDB::bind_method(D_METHOD("set_placeholder_alpha", "alpha"), &LineEdit::set_placeholder_alpha);
  1042. ClassDB::bind_method(D_METHOD("get_placeholder_alpha"), &LineEdit::get_placeholder_alpha);
  1043. ClassDB::bind_method(D_METHOD("set_cursor_position", "position"), &LineEdit::set_cursor_position);
  1044. ClassDB::bind_method(D_METHOD("get_cursor_position"), &LineEdit::get_cursor_position);
  1045. ClassDB::bind_method(D_METHOD("set_expand_to_text_length", "enabled"), &LineEdit::set_expand_to_text_length);
  1046. ClassDB::bind_method(D_METHOD("get_expand_to_text_length"), &LineEdit::get_expand_to_text_length);
  1047. ClassDB::bind_method(D_METHOD("cursor_set_blink_enabled", "enabled"), &LineEdit::cursor_set_blink_enabled);
  1048. ClassDB::bind_method(D_METHOD("cursor_get_blink_enabled"), &LineEdit::cursor_get_blink_enabled);
  1049. ClassDB::bind_method(D_METHOD("cursor_set_blink_speed", "blink_speed"), &LineEdit::cursor_set_blink_speed);
  1050. ClassDB::bind_method(D_METHOD("cursor_get_blink_speed"), &LineEdit::cursor_get_blink_speed);
  1051. ClassDB::bind_method(D_METHOD("set_max_length", "chars"), &LineEdit::set_max_length);
  1052. ClassDB::bind_method(D_METHOD("get_max_length"), &LineEdit::get_max_length);
  1053. ClassDB::bind_method(D_METHOD("append_at_cursor", "text"), &LineEdit::append_at_cursor);
  1054. ClassDB::bind_method(D_METHOD("set_editable", "enabled"), &LineEdit::set_editable);
  1055. ClassDB::bind_method(D_METHOD("is_editable"), &LineEdit::is_editable);
  1056. ClassDB::bind_method(D_METHOD("set_secret", "enabled"), &LineEdit::set_secret);
  1057. ClassDB::bind_method(D_METHOD("is_secret"), &LineEdit::is_secret);
  1058. ClassDB::bind_method(D_METHOD("select", "from", "to"), &LineEdit::select, DEFVAL(0), DEFVAL(-1));
  1059. ClassDB::bind_method(D_METHOD("menu_option", "option"), &LineEdit::menu_option);
  1060. ClassDB::bind_method(D_METHOD("get_menu"), &LineEdit::get_menu);
  1061. ADD_SIGNAL(MethodInfo("text_changed", PropertyInfo(Variant::STRING, "text")));
  1062. ADD_SIGNAL(MethodInfo("text_entered", PropertyInfo(Variant::STRING, "text")));
  1063. BIND_ENUM_CONSTANT(ALIGN_LEFT);
  1064. BIND_ENUM_CONSTANT(ALIGN_CENTER);
  1065. BIND_ENUM_CONSTANT(ALIGN_RIGHT);
  1066. BIND_ENUM_CONSTANT(ALIGN_FILL);
  1067. BIND_ENUM_CONSTANT(MENU_CUT);
  1068. BIND_ENUM_CONSTANT(MENU_COPY);
  1069. BIND_ENUM_CONSTANT(MENU_PASTE);
  1070. BIND_ENUM_CONSTANT(MENU_CLEAR);
  1071. BIND_ENUM_CONSTANT(MENU_SELECT_ALL);
  1072. BIND_ENUM_CONSTANT(MENU_UNDO);
  1073. BIND_ENUM_CONSTANT(MENU_REDO);
  1074. BIND_ENUM_CONSTANT(MENU_MAX);
  1075. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "text"), "set_text", "get_text");
  1076. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  1077. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "max_length"), "set_max_length", "get_max_length");
  1078. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "editable"), "set_editable", "is_editable");
  1079. ADD_PROPERTYNZ(PropertyInfo(Variant::BOOL, "secret"), "set_secret", "is_secret");
  1080. ADD_PROPERTYNO(PropertyInfo(Variant::BOOL, "expand_to_len"), "set_expand_to_text_length", "get_expand_to_text_length");
  1081. ADD_PROPERTY(PropertyInfo(Variant::INT, "focus_mode", PROPERTY_HINT_ENUM, "None,Click,All"), "set_focus_mode", "get_focus_mode");
  1082. ADD_GROUP("Placeholder", "placeholder_");
  1083. ADD_PROPERTYNZ(PropertyInfo(Variant::STRING, "placeholder_text"), "set_placeholder", "get_placeholder");
  1084. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "placeholder_alpha", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_placeholder_alpha", "get_placeholder_alpha");
  1085. ADD_GROUP("Caret", "caret_");
  1086. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  1087. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed", PROPERTY_HINT_RANGE, "0.1,10,0.1"), "cursor_set_blink_speed", "cursor_get_blink_speed");
  1088. }
  1089. LineEdit::LineEdit() {
  1090. undo_stack_pos = NULL;
  1091. _create_undo_state();
  1092. align = ALIGN_LEFT;
  1093. cached_width = 0;
  1094. cursor_pos = 0;
  1095. window_pos = 0;
  1096. window_has_focus = true;
  1097. max_length = 0;
  1098. pass = false;
  1099. placeholder_alpha = 0.6;
  1100. selection_clear();
  1101. set_focus_mode(FOCUS_ALL);
  1102. editable = true;
  1103. set_default_cursor_shape(CURSOR_IBEAM);
  1104. set_mouse_filter(MOUSE_FILTER_STOP);
  1105. draw_caret = true;
  1106. caret_blink_enabled = false;
  1107. caret_blink_timer = memnew(Timer);
  1108. add_child(caret_blink_timer);
  1109. caret_blink_timer->set_wait_time(0.65);
  1110. caret_blink_timer->connect("timeout", this, "_toggle_draw_caret");
  1111. cursor_set_blink_enabled(false);
  1112. menu = memnew(PopupMenu);
  1113. add_child(menu);
  1114. menu->add_item(TTR("Cut"), MENU_CUT, KEY_MASK_CMD | KEY_X);
  1115. menu->add_item(TTR("Copy"), MENU_COPY, KEY_MASK_CMD | KEY_C);
  1116. menu->add_item(TTR("Paste"), MENU_PASTE, KEY_MASK_CMD | KEY_V);
  1117. menu->add_separator();
  1118. menu->add_item(TTR("Select All"), MENU_SELECT_ALL, KEY_MASK_CMD | KEY_A);
  1119. menu->add_item(TTR("Clear"), MENU_CLEAR);
  1120. menu->add_separator();
  1121. menu->add_item(TTR("Undo"), MENU_UNDO, KEY_MASK_CMD | KEY_Z);
  1122. menu->add_item(TTR("Redo"), MENU_REDO, KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Z);
  1123. menu->connect("id_pressed", this, "menu_option");
  1124. expand_to_text_length = false;
  1125. }
  1126. LineEdit::~LineEdit() {
  1127. }