line_edit.cpp 43 KB

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