line_edit.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  1. /*************************************************************************/
  2. /* line_edit.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "line_edit.h"
  30. #include "os/keyboard.h"
  31. #include "os/os.h"
  32. #include "print_string.h"
  33. #include "label.h"
  34. #include "translation.h"
  35. #ifdef TOOLS_ENABLED
  36. #include "tools/editor/editor_settings.h"
  37. #endif
  38. static bool _is_text_char(CharType c) {
  39. return (c>='a' && c<='z') || (c>='A' && c<='Z') || (c>='0' && c<='9') || c=='_';
  40. }
  41. void LineEdit::_gui_input(InputEvent p_event) {
  42. switch(p_event.type) {
  43. case InputEvent::MOUSE_BUTTON: {
  44. const InputEventMouseButton &b = p_event.mouse_button;
  45. if (b.pressed && b.button_index==BUTTON_RIGHT) {
  46. menu->set_pos(get_global_transform().xform(get_local_mouse_pos()));
  47. menu->set_size(Vector2(1,1));
  48. menu->popup();
  49. grab_focus();
  50. return;
  51. }
  52. if (b.button_index!=BUTTON_LEFT)
  53. break;
  54. _reset_caret_blink_timer();
  55. if (b.pressed) {
  56. shift_selection_check_pre(b.mod.shift);
  57. set_cursor_at_pixel_pos(b.x);
  58. if (b.mod.shift) {
  59. selection_fill_at_cursor();
  60. selection.creating=true;
  61. } else {
  62. if (b.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. } break;
  89. case InputEvent::MOUSE_MOTION: {
  90. const InputEventMouseMotion& m=p_event.mouse_motion;
  91. if (m.button_mask&BUTTON_LEFT) {
  92. if (selection.creating) {
  93. set_cursor_at_pixel_pos(m.x);
  94. selection_fill_at_cursor();
  95. }
  96. }
  97. } break;
  98. case InputEvent::KEY: {
  99. const InputEventKey &k =p_event.key;
  100. if (!k.pressed)
  101. return;
  102. unsigned int code = k.scancode;
  103. if (k.mod.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): { // Simple One level undo
  120. if(editable) {
  121. undo();
  122. }
  123. } break;
  124. case (KEY_U): { // Delete from start to cursor
  125. if(editable) {
  126. selection_clear();
  127. undo_text = text;
  128. text = text.substr(cursor_pos,text.length()-cursor_pos);
  129. Ref<Font> font = get_font("font");
  130. cached_width = 0;
  131. if (font != NULL) {
  132. for (int i = 0; i < text.length(); i++)
  133. cached_width += font->get_char_size(text[i]).width;
  134. }
  135. set_cursor_pos(0);
  136. _text_changed();
  137. }
  138. } break;
  139. case (KEY_Y): { // PASTE (Yank for unix users)
  140. if(editable) {
  141. paste_text();
  142. }
  143. } break;
  144. case (KEY_K): { // Delete from cursor_pos to end
  145. if(editable) {
  146. selection_clear();
  147. undo_text = text;
  148. text = text.substr(0,cursor_pos);
  149. _text_changed();
  150. }
  151. } break;
  152. case (KEY_A): { //Select All
  153. select();
  154. } break;
  155. default: { handled=false;}
  156. }
  157. if (handled) {
  158. accept_event();
  159. return;
  160. }
  161. }
  162. _reset_caret_blink_timer();
  163. if (!k.mod.meta) {
  164. bool handled=true;
  165. switch (code) {
  166. case KEY_ENTER:
  167. case KEY_RETURN: {
  168. emit_signal( "text_entered",text );
  169. if (OS::get_singleton()->has_virtual_keyboard())
  170. OS::get_singleton()->hide_virtual_keyboard();
  171. return;
  172. } break;
  173. case KEY_BACKSPACE: {
  174. if (!editable)
  175. break;
  176. if (selection.enabled) {
  177. undo_text=text;
  178. selection_delete();
  179. break;
  180. }
  181. #ifdef APPLE_STYLE_KEYS
  182. if (k.mod.alt) {
  183. #else
  184. if (k.mod.alt) {
  185. handled=false;
  186. break;
  187. } else if (k.mod.command) {
  188. #endif
  189. int cc=cursor_pos;
  190. bool prev_char=false;
  191. while (cc>0) {
  192. bool ischar=_is_text_char(text[cc-1]);
  193. if (prev_char && !ischar)
  194. break;
  195. prev_char=ischar;
  196. cc--;
  197. }
  198. delete_text(cc, cursor_pos);
  199. set_cursor_pos(cc);
  200. } else {
  201. undo_text=text;
  202. delete_char();
  203. }
  204. } break;
  205. case KEY_KP_4: {
  206. if (k.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.mod.alt)
  215. #endif
  216. shift_selection_check_pre(k.mod.shift);
  217. #ifdef APPLE_STYLE_KEYS
  218. if (k.mod.command) {
  219. set_cursor_pos(0);
  220. } else if (k.mod.alt) {
  221. #else
  222. if (k.mod.alt) {
  223. handled=false;
  224. break;
  225. } else if (k.mod.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_pos(cc);
  237. } else {
  238. set_cursor_pos(get_cursor_pos()-1);
  239. }
  240. shift_selection_check_post(k.mod.shift);
  241. } break;
  242. case KEY_KP_6: {
  243. if (k.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.mod.shift);
  251. #ifdef APPLE_STYLE_KEYS
  252. if (k.mod.command) {
  253. set_cursor_pos(text.length());
  254. } else if (k.mod.alt) {
  255. #else
  256. if (k.mod.alt) {
  257. handled=false;
  258. break;
  259. } else if (k.mod.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_pos(cc);
  271. } else {
  272. set_cursor_pos(get_cursor_pos()+1);
  273. }
  274. shift_selection_check_post(k.mod.shift);
  275. } break;
  276. case KEY_DELETE: {
  277. if (!editable)
  278. break;
  279. if (k.mod.shift && !k.mod.command && !k.mod.alt) {
  280. cut_text();
  281. break;
  282. }
  283. if (selection.enabled) {
  284. undo_text=text;
  285. selection_delete();
  286. break;
  287. }
  288. int text_len = text.length();
  289. if (cursor_pos==text_len)
  290. break; // nothing to do
  291. #ifdef APPLE_STYLE_KEYS
  292. if (k.mod.alt) {
  293. #else
  294. if (k.mod.alt) {
  295. handled=false;
  296. break;
  297. } else if (k.mod.command) {
  298. #endif
  299. int cc=cursor_pos;
  300. bool prev_char=false;
  301. while (cc<text.length()) {
  302. bool ischar=_is_text_char(text[cc]);
  303. if (prev_char && !ischar)
  304. break;
  305. prev_char=ischar;
  306. cc++;
  307. }
  308. delete_text(cursor_pos,cc);
  309. } else {
  310. undo_text=text;
  311. set_cursor_pos(cursor_pos+1);
  312. delete_char();
  313. }
  314. } break;
  315. case KEY_KP_7: {
  316. if (k.unicode != 0) {
  317. handled = false;
  318. break;
  319. }
  320. // numlock disabled. fallthrough to key_home
  321. }
  322. case KEY_HOME: {
  323. shift_selection_check_pre(k.mod.shift);
  324. set_cursor_pos(0);
  325. shift_selection_check_post(k.mod.shift);
  326. } break;
  327. case KEY_KP_1: {
  328. if (k.unicode != 0) {
  329. handled = false;
  330. break;
  331. }
  332. // numlock disabled. fallthrough to key_end
  333. }
  334. case KEY_END: {
  335. shift_selection_check_pre(k.mod.shift);
  336. set_cursor_pos(text.length());
  337. shift_selection_check_post(k.mod.shift);
  338. } break;
  339. default: {
  340. handled=false;
  341. } break;
  342. }
  343. if (handled) {
  344. accept_event();
  345. } else if (!k.mod.alt && !k.mod.command) {
  346. if (k.unicode>=32 && k.scancode!=KEY_DELETE) {
  347. if (editable) {
  348. selection_delete();
  349. CharType ucodestr[2]={(CharType)k.unicode,0};
  350. append_at_cursor(ucodestr);
  351. _text_changed();
  352. accept_event();
  353. }
  354. } else {
  355. return;
  356. }
  357. }
  358. update();
  359. }
  360. return;
  361. } break;
  362. }
  363. }
  364. void LineEdit::set_align(Align p_align) {
  365. ERR_FAIL_INDEX(p_align, 4);
  366. align = p_align;
  367. update();
  368. }
  369. LineEdit::Align LineEdit::get_align() const{
  370. return align;
  371. }
  372. Variant LineEdit::get_drag_data(const Point2& p_point) {
  373. if (selection.drag_attempt && selection.enabled) {
  374. String t = text.substr(selection.begin, selection.end - selection.begin);
  375. Label *l = memnew( Label );
  376. l->set_text(t);
  377. set_drag_preview(l);
  378. return t;
  379. }
  380. return Variant();
  381. }
  382. bool LineEdit::can_drop_data(const Point2& p_point,const Variant& p_data) const{
  383. return p_data.get_type()==Variant::STRING;
  384. }
  385. void LineEdit::drop_data(const Point2& p_point,const Variant& p_data){
  386. if (p_data.get_type()==Variant::STRING) {
  387. set_cursor_at_pixel_pos(p_point.x);
  388. int selected = selection.end - selection.begin;
  389. Ref<Font> font = get_font("font");
  390. if (font != NULL) {
  391. for (int i = selection.begin; i < selection.end; i++)
  392. cached_width -= font->get_char_size(text[i]).width;
  393. }
  394. text.erase(selection.begin, selected);
  395. append_at_cursor(p_data);
  396. selection.begin = cursor_pos-selected;
  397. selection.end = cursor_pos;
  398. }
  399. }
  400. void LineEdit::_notification(int p_what) {
  401. switch(p_what) {
  402. #ifdef TOOLS_ENABLED
  403. case NOTIFICATION_ENTER_TREE: {
  404. if (get_tree()->is_editor_hint()) {
  405. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  406. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  407. if (!EditorSettings::get_singleton()->is_connected("settings_changed",this,"_editor_settings_changed")) {
  408. EditorSettings::get_singleton()->connect("settings_changed",this,"_editor_settings_changed");
  409. }
  410. }
  411. } break;
  412. #endif
  413. case NOTIFICATION_RESIZED: {
  414. set_cursor_pos( get_cursor_pos() );
  415. } break;
  416. case MainLoop::NOTIFICATION_WM_FOCUS_IN: {
  417. window_has_focus = true;
  418. draw_caret = true;
  419. update();
  420. } break;
  421. case MainLoop::NOTIFICATION_WM_FOCUS_OUT: {
  422. window_has_focus = false;
  423. draw_caret = false;
  424. update();
  425. } break;
  426. case NOTIFICATION_DRAW: {
  427. if ((!has_focus() && !menu->has_focus()) || !window_has_focus) {
  428. draw_caret = false;
  429. }
  430. int width,height;
  431. Size2 size=get_size();
  432. width=size.width;
  433. height=size.height;
  434. RID ci = get_canvas_item();
  435. Ref<StyleBox> style = get_stylebox("normal");
  436. if (!is_editable())
  437. style=get_stylebox("read_only");
  438. Ref<Font> font=get_font("font");
  439. style->draw( ci, Rect2( Point2(), size ) );
  440. if (has_focus()) {
  441. get_stylebox("focus")->draw( ci, Rect2( Point2(), size ) );
  442. }
  443. int x_ofs=0;
  444. switch (align) {
  445. case ALIGN_FILL:
  446. case ALIGN_LEFT: {
  447. x_ofs=style->get_offset().x;
  448. } break;
  449. case ALIGN_CENTER: {
  450. x_ofs=int(size.width-(cached_width))/2;
  451. } break;
  452. case ALIGN_RIGHT: {
  453. x_ofs=int(size.width-style->get_offset().x-(cached_width));
  454. } break;
  455. }
  456. int ofs_max=width-style->get_minimum_size().width;
  457. int char_ofs=window_pos;
  458. int y_area=height-style->get_minimum_size().height;
  459. int y_ofs=style->get_offset().y;
  460. int font_ascent=font->get_ascent();
  461. Color selection_color=get_color("selection_color");
  462. Color font_color=get_color("font_color");
  463. Color font_color_selected=get_color("font_color_selected");
  464. Color cursor_color=get_color("cursor_color");
  465. const String& t = text.empty() ? placeholder : text;
  466. // draw placeholder color
  467. if(text.empty())
  468. font_color.a *= placeholder_alpha;
  469. int caret_height = font->get_height() > y_area ? y_area : font->get_height();
  470. while(true) {
  471. //end of string, break!
  472. if (char_ofs>=t.length())
  473. break;
  474. CharType cchar=pass?'*':t[char_ofs];
  475. CharType next=pass?'*':t[char_ofs+1];
  476. int char_width=font->get_char_size( cchar,next ).width;
  477. // end of widget, break!
  478. if ((x_ofs + char_width) > ofs_max)
  479. break;
  480. bool selected=selection.enabled && char_ofs>=selection.begin && char_ofs<selection.end;
  481. if (selected)
  482. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(char_width, caret_height)), selection_color);
  483. font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, selected ? font_color_selected : font_color);
  484. if (char_ofs==cursor_pos && draw_caret) {
  485. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(
  486. Point2( x_ofs , y_ofs ), Size2( 1, caret_height ) ), cursor_color );
  487. }
  488. x_ofs+=char_width;
  489. char_ofs++;
  490. }
  491. if (char_ofs==cursor_pos && draw_caret) {//may be at the end
  492. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(
  493. Point2( x_ofs , y_ofs ), Size2( 1, caret_height ) ), cursor_color );
  494. }
  495. } break;
  496. case NOTIFICATION_FOCUS_ENTER: {
  497. if (!caret_blink_enabled) {
  498. draw_caret = true;
  499. }
  500. if (OS::get_singleton()->has_virtual_keyboard())
  501. OS::get_singleton()->show_virtual_keyboard(text,get_global_rect());
  502. } break;
  503. case NOTIFICATION_FOCUS_EXIT: {
  504. if (OS::get_singleton()->has_virtual_keyboard())
  505. OS::get_singleton()->hide_virtual_keyboard();
  506. } break;
  507. }
  508. }
  509. void LineEdit::copy_text() {
  510. if(selection.enabled) {
  511. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  512. }
  513. }
  514. void LineEdit::cut_text() {
  515. if(selection.enabled) {
  516. undo_text = text;
  517. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  518. selection_delete();
  519. }
  520. }
  521. void LineEdit::paste_text() {
  522. String paste_buffer = OS::get_singleton()->get_clipboard();
  523. if(paste_buffer != "") {
  524. if(selection.enabled) selection_delete();
  525. append_at_cursor(paste_buffer);
  526. _text_changed();
  527. }
  528. }
  529. void LineEdit::undo() {
  530. int old_cursor_pos = cursor_pos;
  531. text = undo_text;
  532. Ref<Font> font = get_font("font");
  533. cached_width = 0;
  534. for (int i = 0; i<text.length(); i++)
  535. cached_width += font->get_char_size(text[i]).width;
  536. if(old_cursor_pos > text.length()) {
  537. set_cursor_pos(text.length());
  538. } else {
  539. set_cursor_pos(old_cursor_pos);
  540. }
  541. _text_changed();
  542. }
  543. void LineEdit::shift_selection_check_pre(bool p_shift) {
  544. if (!selection.enabled && p_shift) {
  545. selection.cursor_start=cursor_pos;
  546. }
  547. if (!p_shift)
  548. selection_clear();
  549. }
  550. void LineEdit::shift_selection_check_post(bool p_shift) {
  551. if (p_shift)
  552. selection_fill_at_cursor();
  553. }
  554. void LineEdit::set_cursor_at_pixel_pos(int p_x) {
  555. Ref<Font> font = get_font("font");
  556. int ofs = window_pos;
  557. Ref<StyleBox> style = get_stylebox("normal");
  558. int pixel_ofs = 0;
  559. Size2 size = get_size();
  560. switch (align) {
  561. case ALIGN_FILL:
  562. case ALIGN_LEFT: {
  563. pixel_ofs = int(style->get_offset().x);
  564. } break;
  565. case ALIGN_CENTER: {
  566. pixel_ofs=int(size.width-(cached_width))/2;
  567. } break;
  568. case ALIGN_RIGHT: {
  569. pixel_ofs=int(size.width-style->get_offset().x-(cached_width));
  570. } break;
  571. }
  572. while (ofs<text.length()) {
  573. int char_w = 0;
  574. if (font != NULL) {
  575. char_w = font->get_char_size(text[ofs]).width;
  576. }
  577. pixel_ofs+=char_w;
  578. if (pixel_ofs > p_x) { //found what we look for
  579. break;
  580. }
  581. ofs++;
  582. }
  583. set_cursor_pos( ofs );
  584. /*
  585. int new_cursor_pos=p_x;
  586. int charwidth=draw_area->get_font_char_width(' ',0);
  587. new_cursor_pos=( ( (new_cursor_pos-2)+ (charwidth/2) ) /charwidth );
  588. if (new_cursor_pos>(int)text.length()) new_cursor_pos=text.length();
  589. set_cursor_pos(window_pos+new_cursor_pos); */
  590. }
  591. bool LineEdit::cursor_get_blink_enabled() const {
  592. return caret_blink_enabled;
  593. }
  594. void LineEdit::cursor_set_blink_enabled(const bool p_enabled) {
  595. caret_blink_enabled = p_enabled;
  596. if (p_enabled) {
  597. caret_blink_timer->start();
  598. } else {
  599. caret_blink_timer->stop();
  600. }
  601. draw_caret = true;
  602. }
  603. float LineEdit::cursor_get_blink_speed() const {
  604. return caret_blink_timer->get_wait_time();
  605. }
  606. void LineEdit::cursor_set_blink_speed(const float p_speed) {
  607. ERR_FAIL_COND(p_speed <= 0);
  608. caret_blink_timer->set_wait_time(p_speed);
  609. }
  610. void LineEdit::_reset_caret_blink_timer() {
  611. if (caret_blink_enabled) {
  612. caret_blink_timer->stop();
  613. caret_blink_timer->start();
  614. draw_caret = true;
  615. update();
  616. }
  617. }
  618. void LineEdit::_toggle_draw_caret() {
  619. draw_caret = !draw_caret;
  620. if (is_visible_in_tree() && has_focus() && window_has_focus) {
  621. update();
  622. }
  623. }
  624. void LineEdit::delete_char() {
  625. if ((text.length()<=0) || (cursor_pos==0)) return;
  626. Ref<Font> font = get_font("font");
  627. if (font != NULL) {
  628. cached_width -= font->get_char_size(text[cursor_pos - 1]).width;
  629. }
  630. text.erase( cursor_pos-1, 1 );
  631. set_cursor_pos(get_cursor_pos()-1);
  632. if (cursor_pos==window_pos) {
  633. //set_window_pos(cursor_pos-get_window_length());
  634. }
  635. _text_changed();
  636. }
  637. void LineEdit::delete_text(int p_from_column, int p_to_column) {
  638. undo_text = text;
  639. if (text.size() > 0)
  640. {
  641. Ref<Font> font = get_font("font");
  642. if (font != NULL) {
  643. for (int i = p_from_column; i < p_to_column; i++)
  644. cached_width -= font->get_char_size(text[i]).width;
  645. }
  646. }
  647. else
  648. {
  649. cached_width = 0;
  650. }
  651. text.erase(p_from_column,p_to_column-p_from_column);
  652. cursor_pos-=CLAMP( cursor_pos-p_from_column, 0, p_to_column-p_from_column);
  653. if (cursor_pos>=text.length()) {
  654. cursor_pos=text.length();
  655. }
  656. if (window_pos>cursor_pos) {
  657. window_pos=cursor_pos;
  658. }
  659. _text_changed();
  660. }
  661. void LineEdit::set_text(String p_text) {
  662. clear_internal();
  663. append_at_cursor(p_text);
  664. update();
  665. cursor_pos=0;
  666. window_pos=0;
  667. _text_changed();
  668. }
  669. void LineEdit::clear() {
  670. clear_internal();
  671. _text_changed();
  672. }
  673. String LineEdit::get_text() const {
  674. return text;
  675. }
  676. void LineEdit::set_placeholder(String p_text) {
  677. placeholder = XL_MESSAGE(p_text);
  678. update();
  679. }
  680. String LineEdit::get_placeholder() const {
  681. return placeholder;
  682. }
  683. void LineEdit::set_placeholder_alpha(float p_alpha) {
  684. placeholder_alpha = p_alpha;
  685. update();
  686. }
  687. float LineEdit::get_placeholder_alpha() const {
  688. return placeholder_alpha;
  689. }
  690. void LineEdit::set_cursor_pos(int p_pos) {
  691. if (p_pos>(int)text.length())
  692. p_pos=text.length();
  693. if(p_pos<0)
  694. p_pos=0;
  695. cursor_pos=p_pos;
  696. if (!is_inside_tree()) {
  697. window_pos=cursor_pos;
  698. return;
  699. }
  700. Ref<StyleBox> style = get_stylebox("normal");
  701. Ref<Font> font=get_font("font");
  702. if (cursor_pos<window_pos) {
  703. /* Adjust window if cursor goes too much to the left */
  704. set_window_pos(cursor_pos);
  705. } else if (cursor_pos>window_pos) {
  706. /* Adjust window if cursor goes too much to the right */
  707. int window_width=get_size().width-style->get_minimum_size().width;
  708. if (window_width<0)
  709. return;
  710. int wp=window_pos;
  711. if (font.is_valid()) {
  712. int accum_width=0;
  713. for(int i=cursor_pos;i>=window_pos;i--) {
  714. if (i>=text.length()) {
  715. accum_width=font->get_char_size(' ').width; //anything should do
  716. } else {
  717. accum_width+=font->get_char_size(text[i],i+1<text.length()?text[i+1]:0).width; //anything should do
  718. }
  719. if (accum_width>=window_width)
  720. break;
  721. wp=i;
  722. }
  723. }
  724. if (wp!=window_pos)
  725. set_window_pos( wp );
  726. }
  727. update();
  728. }
  729. int LineEdit::get_cursor_pos() const {
  730. return cursor_pos;
  731. }
  732. void LineEdit::set_window_pos(int p_pos) {
  733. window_pos=p_pos;
  734. if (window_pos<0) window_pos=0;
  735. }
  736. void LineEdit::append_at_cursor(String p_text) {
  737. if ( ( max_length <= 0 ) || (text.length()+p_text.length() <= max_length)) {
  738. undo_text = text;
  739. Ref<Font> font = get_font("font");
  740. if (font != NULL) {
  741. for (int i = 0; i < p_text.length(); i++)
  742. cached_width += font->get_char_size(p_text[i]).width;
  743. }
  744. else {
  745. cached_width = 0;
  746. }
  747. String pre = text.substr( 0, cursor_pos );
  748. String post = text.substr( cursor_pos, text.length()-cursor_pos );
  749. text=pre+p_text+post;
  750. set_cursor_pos(cursor_pos+p_text.length());
  751. }
  752. }
  753. void LineEdit::clear_internal() {
  754. cached_width = 0;
  755. cursor_pos=0;
  756. window_pos=0;
  757. undo_text="";
  758. text="";
  759. update();
  760. }
  761. Size2 LineEdit::get_minimum_size() const {
  762. Ref<StyleBox> style = get_stylebox("normal");
  763. Ref<Font> font=get_font("font");
  764. Size2 min=style->get_minimum_size();
  765. min.height+=font->get_height();
  766. //minimum size of text
  767. int space_size = font->get_char_size(' ').x;
  768. int mstext = get_constant("minimum_spaces")*space_size;
  769. if (expand_to_text_length) {
  770. mstext=MAX(mstext,font->get_string_size(text).x+space_size); //add a spce because some fonts are too exact
  771. }
  772. min.width+=mstext;
  773. return min;
  774. }
  775. /* selection */
  776. void LineEdit::selection_clear() {
  777. selection.begin=0;
  778. selection.end=0;
  779. selection.cursor_start=0;
  780. selection.enabled=false;
  781. selection.creating=false;
  782. selection.doubleclick=false;
  783. update();
  784. }
  785. void LineEdit::selection_delete() {
  786. if (selection.enabled)
  787. delete_text(selection.begin,selection.end);
  788. selection_clear();
  789. }
  790. void LineEdit::set_max_length(int p_max_length) {
  791. ERR_FAIL_COND(p_max_length<0);
  792. max_length = p_max_length;
  793. set_text(text);
  794. }
  795. int LineEdit::get_max_length() const {
  796. return max_length;
  797. }
  798. void LineEdit::selection_fill_at_cursor() {
  799. int aux;
  800. selection.begin=cursor_pos;
  801. selection.end=selection.cursor_start;
  802. if (selection.end<selection.begin) {
  803. aux=selection.end;
  804. selection.end=selection.begin;
  805. selection.begin=aux;
  806. }
  807. selection.enabled=(selection.begin!=selection.end);
  808. }
  809. void LineEdit::select_all() {
  810. if (!text.length())
  811. return;
  812. selection.begin=0;
  813. selection.end=text.length();
  814. selection.enabled=true;
  815. update();
  816. }
  817. void LineEdit::set_editable(bool p_editable) {
  818. editable=p_editable;
  819. update();
  820. }
  821. bool LineEdit::is_editable() const {
  822. return editable;
  823. }
  824. void LineEdit::set_secret(bool p_secret) {
  825. pass=p_secret;
  826. update();
  827. }
  828. bool LineEdit::is_secret() const {
  829. return pass;
  830. }
  831. void LineEdit::select(int p_from, int p_to) {
  832. if (p_from==0 && p_to==0) {
  833. selection_clear();
  834. return;
  835. }
  836. int len = text.length();
  837. if (p_from<0)
  838. p_from=0;
  839. if (p_from>len)
  840. p_from=len;
  841. if (p_to<0 || p_to>len)
  842. p_to=len;
  843. if (p_from>=p_to)
  844. return;
  845. selection.enabled=true;
  846. selection.begin=p_from;
  847. selection.end=p_to;
  848. selection.creating=false;
  849. selection.doubleclick=false;
  850. update();
  851. }
  852. bool LineEdit::is_text_field() const {
  853. return true;
  854. }
  855. void LineEdit::menu_option(int p_option) {
  856. switch(p_option) {
  857. case MENU_CUT: {
  858. if (editable) {
  859. cut_text();
  860. }
  861. } break;
  862. case MENU_COPY: {
  863. copy_text();
  864. } break;
  865. case MENU_PASTE: {
  866. if (editable) {
  867. paste_text();
  868. }
  869. } break;
  870. case MENU_CLEAR: {
  871. if (editable) {
  872. clear();
  873. }
  874. } break;
  875. case MENU_SELECT_ALL: {
  876. select_all();
  877. } break;
  878. case MENU_UNDO: {
  879. undo();
  880. } break;
  881. }
  882. }
  883. PopupMenu *LineEdit::get_menu() const {
  884. return menu;
  885. }
  886. #ifdef TOOLS_ENABLED
  887. void LineEdit::_editor_settings_changed() {
  888. cursor_set_blink_enabled(EDITOR_DEF("text_editor/cursor/caret_blink", false));
  889. cursor_set_blink_speed(EDITOR_DEF("text_editor/cursor/caret_blink_speed", 0.65));
  890. }
  891. #endif
  892. void LineEdit::set_expand_to_text_length(bool p_enabled) {
  893. expand_to_text_length = p_enabled;
  894. minimum_size_changed();
  895. }
  896. bool LineEdit::get_expand_to_text_length() const{
  897. return expand_to_text_length;
  898. }
  899. void LineEdit::_text_changed() {
  900. if (expand_to_text_length)
  901. minimum_size_changed();
  902. emit_signal("text_changed",text);
  903. _change_notify("text");
  904. }
  905. void LineEdit::_bind_methods() {
  906. ClassDB::bind_method(_MD("_toggle_draw_caret"),&LineEdit::_toggle_draw_caret);
  907. #ifdef TOOLS_ENABLED
  908. ClassDB::bind_method("_editor_settings_changed",&LineEdit::_editor_settings_changed);
  909. #endif
  910. ClassDB::bind_method(_MD("set_align", "align"), &LineEdit::set_align);
  911. ClassDB::bind_method(_MD("get_align"), &LineEdit::get_align);
  912. ClassDB::bind_method(_MD("_gui_input"),&LineEdit::_gui_input);
  913. ClassDB::bind_method(_MD("clear"),&LineEdit::clear);
  914. ClassDB::bind_method(_MD("select_all"),&LineEdit::select_all);
  915. ClassDB::bind_method(_MD("set_text","text"),&LineEdit::set_text);
  916. ClassDB::bind_method(_MD("get_text"),&LineEdit::get_text);
  917. ClassDB::bind_method(_MD("set_placeholder","text"),&LineEdit::set_placeholder);
  918. ClassDB::bind_method(_MD("get_placeholder"),&LineEdit::get_placeholder);
  919. ClassDB::bind_method(_MD("set_placeholder_alpha","alpha"),&LineEdit::set_placeholder_alpha);
  920. ClassDB::bind_method(_MD("get_placeholder_alpha"),&LineEdit::get_placeholder_alpha);
  921. ClassDB::bind_method(_MD("set_cursor_pos","pos"),&LineEdit::set_cursor_pos);
  922. ClassDB::bind_method(_MD("get_cursor_pos"),&LineEdit::get_cursor_pos);
  923. ClassDB::bind_method(_MD("set_expand_to_text_length","enabled"),&LineEdit::set_expand_to_text_length);
  924. ClassDB::bind_method(_MD("get_expand_to_text_length"),&LineEdit::get_expand_to_text_length);
  925. ClassDB::bind_method(_MD("cursor_set_blink_enabled", "enabled"),&LineEdit::cursor_set_blink_enabled);
  926. ClassDB::bind_method(_MD("cursor_get_blink_enabled"),&LineEdit::cursor_get_blink_enabled);
  927. ClassDB::bind_method(_MD("cursor_set_blink_speed", "blink_speed"),&LineEdit::cursor_set_blink_speed);
  928. ClassDB::bind_method(_MD("cursor_get_blink_speed"),&LineEdit::cursor_get_blink_speed);
  929. ClassDB::bind_method(_MD("set_max_length","chars"),&LineEdit::set_max_length);
  930. ClassDB::bind_method(_MD("get_max_length"),&LineEdit::get_max_length);
  931. ClassDB::bind_method(_MD("append_at_cursor","text"),&LineEdit::append_at_cursor);
  932. ClassDB::bind_method(_MD("set_editable","enabled"),&LineEdit::set_editable);
  933. ClassDB::bind_method(_MD("is_editable"),&LineEdit::is_editable);
  934. ClassDB::bind_method(_MD("set_secret","enabled"),&LineEdit::set_secret);
  935. ClassDB::bind_method(_MD("is_secret"),&LineEdit::is_secret);
  936. ClassDB::bind_method(_MD("select","from","to"),&LineEdit::select,DEFVAL(0),DEFVAL(-1));
  937. ClassDB::bind_method(_MD("menu_option","option"),&LineEdit::menu_option);
  938. ClassDB::bind_method(_MD("get_menu:PopupMenu"),&LineEdit::get_menu);
  939. ADD_SIGNAL( MethodInfo("text_changed", PropertyInfo( Variant::STRING, "text" )) );
  940. ADD_SIGNAL( MethodInfo("text_entered", PropertyInfo( Variant::STRING, "text" )) );
  941. BIND_CONSTANT(ALIGN_LEFT);
  942. BIND_CONSTANT(ALIGN_CENTER);
  943. BIND_CONSTANT(ALIGN_RIGHT);
  944. BIND_CONSTANT(ALIGN_FILL);
  945. BIND_CONSTANT( MENU_CUT );
  946. BIND_CONSTANT( MENU_COPY );
  947. BIND_CONSTANT( MENU_PASTE );
  948. BIND_CONSTANT( MENU_CLEAR );
  949. BIND_CONSTANT( MENU_SELECT_ALL );
  950. BIND_CONSTANT( MENU_UNDO );
  951. BIND_CONSTANT( MENU_MAX );
  952. ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "text" ), "set_text","get_text") ;
  953. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), "set_align", "get_align");
  954. ADD_PROPERTYNZ( PropertyInfo( Variant::INT, "max_length" ), "set_max_length","get_max_length") ;
  955. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "editable" ), "set_editable","is_editable") ;
  956. ADD_PROPERTYNZ( PropertyInfo( Variant::BOOL, "secret" ), "set_secret","is_secret") ;
  957. ADD_PROPERTYNO( PropertyInfo( Variant::BOOL, "expand_to_len" ), "set_expand_to_text_length","get_expand_to_text_length") ;
  958. ADD_PROPERTY( PropertyInfo( Variant::INT,"focus_mode", PROPERTY_HINT_ENUM, "None,Click,All" ), "set_focus_mode", "get_focus_mode") ;
  959. ADD_GROUP("Placeholder","placeholder_");
  960. ADD_PROPERTYNZ( PropertyInfo( Variant::STRING, "placeholder_text" ), "set_placeholder","get_placeholder") ;
  961. ADD_PROPERTYNZ( PropertyInfo( Variant::REAL, "placeholder_alpha",PROPERTY_HINT_RANGE,"0,1,0.001" ), "set_placeholder_alpha","get_placeholder_alpha") ;
  962. ADD_GROUP("Caret","caret_");
  963. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "caret_blink"), "cursor_set_blink_enabled", "cursor_get_blink_enabled");
  964. ADD_PROPERTYNZ(PropertyInfo(Variant::REAL, "caret_blink_speed",PROPERTY_HINT_RANGE,"0.1,10,0.1"), "cursor_set_blink_speed","cursor_get_blink_speed") ;
  965. }
  966. LineEdit::LineEdit() {
  967. align = ALIGN_LEFT;
  968. cached_width = 0;
  969. cursor_pos=0;
  970. window_pos=0;
  971. window_has_focus=true;
  972. max_length = 0;
  973. pass=false;
  974. placeholder_alpha=0.6;
  975. selection_clear();
  976. set_focus_mode( FOCUS_ALL );
  977. editable=true;
  978. set_default_cursor_shape(CURSOR_IBEAM);
  979. set_mouse_filter(MOUSE_FILTER_STOP);
  980. draw_caret=true;
  981. caret_blink_enabled=false;
  982. caret_blink_timer = memnew(Timer);
  983. add_child(caret_blink_timer);
  984. caret_blink_timer->set_wait_time(0.65);
  985. caret_blink_timer->connect("timeout", this,"_toggle_draw_caret");
  986. cursor_set_blink_enabled(false);
  987. menu = memnew( PopupMenu );
  988. add_child(menu);
  989. menu->add_item(TTR("Cut"),MENU_CUT,KEY_MASK_CMD|KEY_X);
  990. menu->add_item(TTR("Copy"),MENU_COPY,KEY_MASK_CMD|KEY_C);
  991. menu->add_item(TTR("Paste"),MENU_PASTE,KEY_MASK_CMD|KEY_V);
  992. menu->add_separator();
  993. menu->add_item(TTR("Select All"),MENU_SELECT_ALL,KEY_MASK_CMD|KEY_A);
  994. menu->add_item(TTR("Clear"),MENU_CLEAR);
  995. menu->add_separator();
  996. menu->add_item(TTR("Undo"),MENU_UNDO,KEY_MASK_CMD|KEY_Z);
  997. menu->connect("id_pressed",this,"menu_option");
  998. expand_to_text_length=false;
  999. }
  1000. LineEdit::~LineEdit() {
  1001. }