line_edit.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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-2016 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. void LineEdit::_input_event(InputEvent p_event) {
  35. switch(p_event.type) {
  36. case InputEvent::MOUSE_BUTTON: {
  37. const InputEventMouseButton &b = p_event.mouse_button;
  38. if (b.button_index!=1)
  39. break;
  40. if (b.pressed) {
  41. set_cursor_at_pixel_pos(b.x);
  42. if (b.doubleclick) {
  43. selection.enabled=true;
  44. selection.begin=0;
  45. selection.end=text.length();
  46. selection.doubleclick=true;
  47. }
  48. selection.drag_attempt=false;
  49. if ((cursor_pos<selection.begin) || (cursor_pos>selection.end) || !selection.enabled) {
  50. selection_clear();
  51. selection.cursor_start=cursor_pos;
  52. selection.creating=true;
  53. } else if (selection.enabled) {
  54. selection.drag_attempt=true;
  55. }
  56. // if (!editable)
  57. // non_editable_clicked_signal.call();
  58. update();
  59. } else {
  60. if ( (!selection.creating) && (!selection.doubleclick)) {
  61. selection_clear();
  62. }
  63. selection.creating=false;
  64. selection.doubleclick=false;
  65. if (OS::get_singleton()->has_virtual_keyboard())
  66. OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect());
  67. }
  68. update();
  69. } break;
  70. case InputEvent::MOUSE_MOTION: {
  71. const InputEventMouseMotion& m=p_event.mouse_motion;
  72. if (m.button_mask&1) {
  73. if (selection.creating) {
  74. set_cursor_at_pixel_pos(m.x);
  75. selection_fill_at_cursor();
  76. }
  77. }
  78. } break;
  79. case InputEvent::KEY: {
  80. const InputEventKey &k =p_event.key;
  81. if (!k.pressed)
  82. return;
  83. unsigned int code = k.scancode;
  84. if (k.mod.command) {
  85. bool handled=true;
  86. switch (code) {
  87. case (KEY_X): { // CUT
  88. if(k.mod.command && editable) {
  89. cut_text();
  90. }
  91. } break;
  92. case (KEY_C): { // COPY
  93. if(k.mod.command) {
  94. copy_text();
  95. }
  96. } break;
  97. case (KEY_V): { // PASTE
  98. if(k.mod.command && editable) {
  99. paste_text();
  100. }
  101. } break;
  102. case (KEY_Z): { // Simple One level undo
  103. if( k.mod.command && editable) {
  104. int old_cursor_pos = cursor_pos;
  105. text = undo_text;
  106. Ref<Font> font = get_font("font");
  107. cached_width = 0;
  108. for (int i = 0; i<text.length(); i++)
  109. cached_width += font->get_char_size(text[i]).width;
  110. if(old_cursor_pos > text.length()) {
  111. set_cursor_pos(text.length());
  112. } else {
  113. set_cursor_pos(old_cursor_pos);
  114. }
  115. }
  116. emit_signal("text_changed",text);
  117. _change_notify("text");
  118. } break;
  119. case (KEY_U): { // Delete from start to cursor
  120. if( k.mod.command && editable) {
  121. selection_clear();
  122. undo_text = text;
  123. text = text.substr(cursor_pos,text.length()-cursor_pos);
  124. Ref<Font> font = get_font("font");
  125. cached_width = 0;
  126. if (font != NULL) {
  127. for (int i = 0; i < text.length(); i++)
  128. cached_width += font->get_char_size(text[i]).width;
  129. }
  130. set_cursor_pos(0);
  131. emit_signal("text_changed",text);
  132. _change_notify("text");
  133. }
  134. } break;
  135. case (KEY_Y): { // PASTE (Yank for unix users)
  136. if(k.mod.command && editable) {
  137. paste_text();
  138. }
  139. } break;
  140. case (KEY_K): { // Delete from cursor_pos to end
  141. if(k.mod.command && editable) {
  142. selection_clear();
  143. undo_text = text;
  144. text = text.substr(0,cursor_pos);
  145. emit_signal("text_changed",text);
  146. _change_notify("text");
  147. }
  148. } break;
  149. case (KEY_A): { //Select All
  150. select();
  151. } break;
  152. default: { handled=false;}
  153. }
  154. if (handled) {
  155. accept_event();
  156. return;
  157. }
  158. }
  159. if (!k.mod.alt && !k.mod.meta && !k.mod.command) {
  160. bool handled=true;
  161. switch (code) {
  162. case KEY_ENTER:
  163. case KEY_RETURN: {
  164. emit_signal( "text_entered",text );
  165. if (OS::get_singleton()->has_virtual_keyboard())
  166. OS::get_singleton()->hide_virtual_keyboard();
  167. return;
  168. } break;
  169. case KEY_BACKSPACE: {
  170. if (editable) {
  171. undo_text = text;
  172. if (selection.enabled)
  173. selection_delete();
  174. else
  175. delete_char();
  176. }
  177. } break;
  178. case KEY_KP_4: {
  179. if (k.unicode != 0) {
  180. handled = false;
  181. break;
  182. }
  183. // numlock disabled. fallthrough to key_left
  184. }
  185. case KEY_LEFT: {
  186. shift_selection_check_pre(k.mod.shift);
  187. set_cursor_pos(get_cursor_pos()-1);
  188. shift_selection_check_post(k.mod.shift);
  189. } break;
  190. case KEY_KP_6: {
  191. if (k.unicode != 0) {
  192. handled = false;
  193. break;
  194. }
  195. // numlock disabled. fallthrough to key_right
  196. }
  197. case KEY_RIGHT: {
  198. shift_selection_check_pre(k.mod.shift);
  199. set_cursor_pos(get_cursor_pos()+1);
  200. shift_selection_check_post(k.mod.shift);
  201. } break;
  202. case KEY_DELETE: {
  203. if (editable) {
  204. undo_text = text;
  205. if (selection.enabled)
  206. selection_delete();
  207. else if (cursor_pos<text.length()) {
  208. set_cursor_pos(get_cursor_pos()+1);
  209. delete_char();
  210. }
  211. }
  212. } break;
  213. case KEY_KP_7: {
  214. if (k.unicode != 0) {
  215. handled = false;
  216. break;
  217. }
  218. // numlock disabled. fallthrough to key_home
  219. }
  220. case KEY_HOME: {
  221. shift_selection_check_pre(k.mod.shift);
  222. set_cursor_pos(0);
  223. shift_selection_check_post(k.mod.shift);
  224. } break;
  225. case KEY_KP_1: {
  226. if (k.unicode != 0) {
  227. handled = false;
  228. break;
  229. }
  230. // numlock disabled. fallthrough to key_end
  231. }
  232. case KEY_END: {
  233. shift_selection_check_pre(k.mod.shift);
  234. set_cursor_pos(text.length());
  235. shift_selection_check_post(k.mod.shift);
  236. } break;
  237. default: {
  238. handled=false;
  239. } break;
  240. }
  241. if (handled) {
  242. accept_event();
  243. } else {
  244. if (k.unicode>=32 && k.scancode!=KEY_DELETE) {
  245. if (editable) {
  246. selection_delete();
  247. CharType ucodestr[2]={(CharType)k.unicode,0};
  248. append_at_cursor(ucodestr);
  249. emit_signal("text_changed",text);
  250. _change_notify("text");
  251. accept_event();
  252. }
  253. } else {
  254. return;
  255. }
  256. }
  257. selection.old_shift=k.mod.shift;
  258. update();
  259. }
  260. return;
  261. } break;
  262. }
  263. }
  264. void LineEdit::set_align(Align p_align) {
  265. ERR_FAIL_INDEX(p_align, 4);
  266. align = p_align;
  267. update();
  268. }
  269. LineEdit::Align LineEdit::get_align() const{
  270. return align;
  271. }
  272. Variant LineEdit::get_drag_data(const Point2& p_point) {
  273. if (selection.drag_attempt && selection.enabled) {
  274. String t = text.substr(selection.begin, selection.end - selection.begin);
  275. Label *l = memnew( Label );
  276. l->set_text(t);
  277. set_drag_preview(l);
  278. return t;
  279. }
  280. return Variant();
  281. }
  282. bool LineEdit::can_drop_data(const Point2& p_point,const Variant& p_data) const{
  283. return p_data.get_type()==Variant::STRING;
  284. }
  285. void LineEdit::drop_data(const Point2& p_point,const Variant& p_data){
  286. if (p_data.get_type()==Variant::STRING) {
  287. set_cursor_at_pixel_pos(p_point.x);
  288. int selected = selection.end - selection.begin;
  289. Ref<Font> font = get_font("font");
  290. if (font != NULL) {
  291. for (int i = selection.begin; i < selection.end; i++)
  292. cached_width -= font->get_char_size(text[i]).width;
  293. }
  294. text.erase(selection.begin, selected);
  295. append_at_cursor(p_data);
  296. selection.begin = cursor_pos-selected;
  297. selection.end = cursor_pos;
  298. }
  299. }
  300. void LineEdit::_notification(int p_what) {
  301. switch(p_what) {
  302. case NOTIFICATION_RESIZED: {
  303. set_cursor_pos( get_cursor_pos() );
  304. } break;
  305. case NOTIFICATION_DRAW: {
  306. int width,height;
  307. Size2 size=get_size();
  308. width=size.width;
  309. height=size.height;
  310. RID ci = get_canvas_item();
  311. Ref<StyleBox> style = get_stylebox("normal");
  312. if (!is_editable())
  313. style=get_stylebox("read_only");
  314. Ref<Font> font=get_font("font");
  315. style->draw( ci, Rect2( Point2(), size ) );
  316. if (has_focus()) {
  317. get_stylebox("focus")->draw( ci, Rect2( Point2(), size ) );
  318. }
  319. int x_ofs=0;
  320. switch (align) {
  321. case ALIGN_FILL:
  322. case ALIGN_LEFT: {
  323. x_ofs=style->get_offset().x;
  324. } break;
  325. case ALIGN_CENTER: {
  326. x_ofs=x_ofs=int(size.width-(cached_width))/2;
  327. } break;
  328. case ALIGN_RIGHT: {
  329. x_ofs=x_ofs=int(size.width-style->get_offset().x-(cached_width));
  330. } break;
  331. }
  332. int ofs_max=width-style->get_minimum_size().width;
  333. int char_ofs=window_pos;
  334. int y_area=height-style->get_minimum_size().height;
  335. int y_ofs=style->get_offset().y;
  336. int font_ascent=font->get_ascent();
  337. Color selection_color=get_color("selection_color");
  338. Color font_color=get_color("font_color");
  339. Color font_color_selected=get_color("font_color_selected");
  340. Color cursor_color=get_color("cursor_color");
  341. while(true) {
  342. //end of string, break!
  343. if (char_ofs>=text.length())
  344. break;
  345. CharType cchar=pass?'*':text[char_ofs];
  346. CharType next=pass?'*':text[char_ofs+1];
  347. int char_width=font->get_char_size( cchar,next ).width;
  348. // end of widget, break!
  349. if ((x_ofs + char_width) > ofs_max)
  350. break;
  351. bool selected=selection.enabled && char_ofs>=selection.begin && char_ofs<selection.end;
  352. if (selected)
  353. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(x_ofs, y_ofs), Size2(char_width, y_area)), selection_color);
  354. font->draw_char(ci, Point2(x_ofs, y_ofs + font_ascent), cchar, next, selected ? font_color_selected : font_color);
  355. if (char_ofs==cursor_pos && has_focus())
  356. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(
  357. Point2( x_ofs , y_ofs ), Size2( 1, y_area ) ), cursor_color );
  358. x_ofs+=char_width;
  359. char_ofs++;
  360. }
  361. if (char_ofs==cursor_pos && has_focus()) //may be at the end
  362. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(
  363. Point2( x_ofs , y_ofs ), Size2( 1, y_area ) ), cursor_color );
  364. } break;
  365. case NOTIFICATION_FOCUS_ENTER: {
  366. if (OS::get_singleton()->has_virtual_keyboard())
  367. OS::get_singleton()->show_virtual_keyboard(get_text(),get_global_rect());
  368. } break;
  369. case NOTIFICATION_FOCUS_EXIT: {
  370. if (OS::get_singleton()->has_virtual_keyboard())
  371. OS::get_singleton()->hide_virtual_keyboard();
  372. } break;
  373. }
  374. }
  375. void LineEdit::copy_text() {
  376. if(selection.enabled) {
  377. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  378. }
  379. }
  380. void LineEdit::cut_text() {
  381. if(selection.enabled) {
  382. undo_text = text;
  383. OS::get_singleton()->set_clipboard(text.substr(selection.begin, selection.end - selection.begin));
  384. selection_delete();
  385. }
  386. }
  387. void LineEdit::paste_text() {
  388. String paste_buffer = OS::get_singleton()->get_clipboard();
  389. if(paste_buffer != "") {
  390. if(selection.enabled) selection_delete();
  391. append_at_cursor(paste_buffer);
  392. emit_signal("text_changed",text);
  393. _change_notify("text");
  394. }
  395. }
  396. void LineEdit::shift_selection_check_pre(bool p_shift) {
  397. if (!selection.old_shift && p_shift) {
  398. selection.cursor_start=cursor_pos;
  399. }
  400. if (!p_shift)
  401. selection_clear();
  402. }
  403. void LineEdit::shift_selection_check_post(bool p_shift) {
  404. if (p_shift)
  405. selection_fill_at_cursor();
  406. }
  407. void LineEdit::set_cursor_at_pixel_pos(int p_x) {
  408. Ref<Font> font = get_font("font");
  409. int ofs = window_pos;
  410. Ref<StyleBox> style = get_stylebox("normal");
  411. int pixel_ofs = 0;
  412. Size2 size = get_size();
  413. switch (align) {
  414. case ALIGN_FILL:
  415. case ALIGN_LEFT: {
  416. pixel_ofs = int(style->get_offset().x);
  417. } break;
  418. case ALIGN_CENTER: {
  419. pixel_ofs=int(size.width-(cached_width))/2;
  420. } break;
  421. case ALIGN_RIGHT: {
  422. pixel_ofs=int(size.width-style->get_offset().x-(cached_width));
  423. } break;
  424. }
  425. while (ofs<text.length()) {
  426. int char_w = 0;
  427. if (font != NULL) {
  428. char_w = font->get_char_size(text[ofs]).width;
  429. }
  430. pixel_ofs+=char_w;
  431. if (pixel_ofs > p_x) { //found what we look for
  432. if ( (pixel_ofs-p_x) < (char_w >> 1 ) ) {
  433. ofs+=1;
  434. }
  435. break;
  436. }
  437. ofs++;
  438. }
  439. set_cursor_pos( ofs );
  440. /*
  441. int new_cursor_pos=p_x;
  442. int charwidth=draw_area->get_font_char_width(' ',0);
  443. new_cursor_pos=( ( (new_cursor_pos-2)+ (charwidth/2) ) /charwidth );
  444. if (new_cursor_pos>(int)text.length()) new_cursor_pos=text.length();
  445. set_cursor_pos(window_pos+new_cursor_pos); */
  446. }
  447. void LineEdit::delete_char() {
  448. if ((text.length()<=0) || (cursor_pos==0)) return;
  449. Ref<Font> font = get_font("font");
  450. if (font != NULL) {
  451. cached_width -= font->get_char_size(text[cursor_pos - 1]).width;
  452. }
  453. text.erase( cursor_pos-1, 1 );
  454. set_cursor_pos(get_cursor_pos()-1);
  455. if (cursor_pos==window_pos) {
  456. // set_window_pos(cursor_pos-get_window_length());
  457. }
  458. emit_signal("text_changed",text);
  459. _change_notify("text");
  460. }
  461. void LineEdit::set_text(String p_text) {
  462. clear_internal();
  463. append_at_cursor(p_text);
  464. update();
  465. cursor_pos=0;
  466. window_pos=0;
  467. }
  468. void LineEdit::clear() {
  469. clear_internal();
  470. }
  471. String LineEdit::get_text() const {
  472. return text;
  473. }
  474. void LineEdit::set_cursor_pos(int p_pos) {
  475. if (p_pos>(int)text.length())
  476. p_pos=text.length();
  477. if(p_pos<0)
  478. p_pos=0;
  479. cursor_pos=p_pos;
  480. // if (cursor_pos>(window_pos+get_window_length())) {
  481. // set_window_pos(cursor_pos-get_window_lengt//h());
  482. // }
  483. if (!is_inside_tree()) {
  484. window_pos=cursor_pos;
  485. return;
  486. }
  487. Ref<StyleBox> style = get_stylebox("normal");
  488. Ref<Font> font=get_font("font");
  489. if (cursor_pos<window_pos) {
  490. /* Adjust window if cursor goes too much to the left */
  491. set_window_pos(cursor_pos);
  492. } else if (cursor_pos>window_pos) {
  493. /* Adjust window if cursor goes too much to the right */
  494. int window_width=get_size().width-style->get_minimum_size().width;
  495. if (window_width<0)
  496. return;
  497. int width_to_cursor=0;
  498. int wp=window_pos;
  499. if (font != NULL) {
  500. for (int i=window_pos;i<cursor_pos;i++)
  501. width_to_cursor+=font->get_char_size( text[i] ).width;
  502. while (width_to_cursor >= window_width && wp < text.length()) {
  503. width_to_cursor -= font->get_char_size(text[wp]).width;
  504. wp++;
  505. }
  506. }
  507. if (wp!=window_pos)
  508. set_window_pos( wp );
  509. }
  510. update();
  511. }
  512. int LineEdit::get_cursor_pos() const {
  513. return cursor_pos;
  514. }
  515. void LineEdit::set_window_pos(int p_pos) {
  516. window_pos=p_pos;
  517. if (window_pos<0) window_pos=0;
  518. }
  519. void LineEdit::append_at_cursor(String p_text) {
  520. if ( ( max_length <= 0 ) || (text.length()+p_text.length() <= max_length)) {
  521. undo_text = text;
  522. Ref<Font> font = get_font("font");
  523. if (font != NULL) {
  524. for (int i = 0; i < p_text.length(); i++)
  525. cached_width += font->get_char_size(p_text[i]).width;
  526. }
  527. else {
  528. cached_width = 0;
  529. }
  530. String pre = text.substr( 0, cursor_pos );
  531. String post = text.substr( cursor_pos, text.length()-cursor_pos );
  532. text=pre+p_text+post;
  533. set_cursor_pos(cursor_pos+p_text.length());
  534. }
  535. }
  536. void LineEdit::clear_internal() {
  537. cached_width = 0;
  538. cursor_pos=0;
  539. window_pos=0;
  540. undo_text="";
  541. text="";
  542. update();
  543. }
  544. Size2 LineEdit::get_minimum_size() const {
  545. Ref<StyleBox> style = get_stylebox("normal");
  546. Ref<Font> font=get_font("font");
  547. Size2 min=style->get_minimum_size();
  548. min.height+=font->get_height();
  549. min.width+=get_constant("minimum_spaces")*font->get_char_size(' ').x;
  550. return min;
  551. }
  552. /* selection */
  553. void LineEdit::selection_clear() {
  554. selection.begin=0;
  555. selection.end=0;
  556. selection.cursor_start=0;
  557. selection.enabled=false;
  558. selection.creating=false;
  559. selection.old_shift=false;
  560. selection.doubleclick=false;
  561. update();
  562. }
  563. void LineEdit::selection_delete() {
  564. if (selection.enabled) {
  565. undo_text = text;
  566. if (text.size() > 0)
  567. {
  568. Ref<Font> font = get_font("font");
  569. if (font != NULL) {
  570. for (int i = selection.begin; i < selection.end; i++)
  571. cached_width -= font->get_char_size(text[i]).width;
  572. }
  573. }
  574. else
  575. {
  576. cached_width = 0;
  577. }
  578. text.erase(selection.begin,selection.end-selection.begin);
  579. cursor_pos-=CLAMP( cursor_pos-selection.begin, 0, selection.end-selection.begin);
  580. if (cursor_pos>=text.length()) {
  581. cursor_pos=text.length();
  582. }
  583. if (window_pos>cursor_pos) {
  584. window_pos=cursor_pos;
  585. }
  586. emit_signal("text_changed",text);
  587. _change_notify("text");
  588. };
  589. selection_clear();
  590. }
  591. void LineEdit::set_max_length(int p_max_length) {
  592. ERR_FAIL_COND(p_max_length<0);
  593. max_length = p_max_length;
  594. set_text(text);
  595. }
  596. int LineEdit::get_max_length() const {
  597. return max_length;
  598. }
  599. void LineEdit::selection_fill_at_cursor() {
  600. int aux;
  601. selection.begin=cursor_pos;
  602. selection.end=selection.cursor_start;
  603. if (selection.end<selection.begin) {
  604. aux=selection.end;
  605. selection.end=selection.begin;
  606. selection.begin=aux;
  607. }
  608. selection.enabled=(selection.begin!=selection.end);
  609. }
  610. void LineEdit::select_all() {
  611. if (!text.length())
  612. return;
  613. selection.begin=0;
  614. selection.end=text.length();
  615. selection.enabled=true;
  616. update();
  617. }
  618. void LineEdit::set_editable(bool p_editable) {
  619. editable=p_editable;
  620. update();
  621. }
  622. bool LineEdit::is_editable() const {
  623. return editable;
  624. }
  625. void LineEdit::set_secret(bool p_secret) {
  626. pass=p_secret;
  627. update();
  628. }
  629. bool LineEdit::is_secret() const {
  630. return pass;
  631. }
  632. void LineEdit::select(int p_from, int p_to) {
  633. if (p_from==0 && p_to==0) {
  634. selection_clear();
  635. return;
  636. }
  637. int len = text.length();
  638. if (p_from<0)
  639. p_from=0;
  640. if (p_from>len)
  641. p_from=len;
  642. if (p_to<0 || p_to>len)
  643. p_to=len;
  644. if (p_from>=p_to)
  645. return;
  646. selection.enabled=true;
  647. selection.begin=p_from;
  648. selection.end=p_to;
  649. selection.creating=false;
  650. selection.old_shift=false;
  651. selection.doubleclick=false;
  652. update();
  653. }
  654. bool LineEdit::is_text_field() const {
  655. return true;
  656. }
  657. void LineEdit::_bind_methods() {
  658. ObjectTypeDB::bind_method(_MD("set_align", "align"), &LineEdit::set_align);
  659. ObjectTypeDB::bind_method(_MD("get_align"), &LineEdit::get_align);
  660. ObjectTypeDB::bind_method(_MD("_input_event"),&LineEdit::_input_event);
  661. ObjectTypeDB::bind_method(_MD("clear"),&LineEdit::clear);
  662. ObjectTypeDB::bind_method(_MD("select_all"),&LineEdit::select_all);
  663. ObjectTypeDB::bind_method(_MD("set_text","text"),&LineEdit::set_text);
  664. ObjectTypeDB::bind_method(_MD("get_text"),&LineEdit::get_text);
  665. ObjectTypeDB::bind_method(_MD("set_cursor_pos","pos"),&LineEdit::set_cursor_pos);
  666. ObjectTypeDB::bind_method(_MD("get_cursor_pos"),&LineEdit::get_cursor_pos);
  667. ObjectTypeDB::bind_method(_MD("set_max_length","chars"),&LineEdit::set_max_length);
  668. ObjectTypeDB::bind_method(_MD("get_max_length"),&LineEdit::get_max_length);
  669. ObjectTypeDB::bind_method(_MD("append_at_cursor","text"),&LineEdit::append_at_cursor);
  670. ObjectTypeDB::bind_method(_MD("set_editable","enabled"),&LineEdit::set_editable);
  671. ObjectTypeDB::bind_method(_MD("is_editable"),&LineEdit::is_editable);
  672. ObjectTypeDB::bind_method(_MD("set_secret","enabled"),&LineEdit::set_secret);
  673. ObjectTypeDB::bind_method(_MD("is_secret"),&LineEdit::is_secret);
  674. ObjectTypeDB::bind_method(_MD("select","from","to"),&LineEdit::select,DEFVAL(0),DEFVAL(-1));
  675. ADD_SIGNAL( MethodInfo("text_changed", PropertyInfo( Variant::STRING, "text" )) );
  676. ADD_SIGNAL( MethodInfo("text_entered", PropertyInfo( Variant::STRING, "text" )) );
  677. BIND_CONSTANT(ALIGN_LEFT);
  678. BIND_CONSTANT(ALIGN_CENTER);
  679. BIND_CONSTANT(ALIGN_RIGHT);
  680. BIND_CONSTANT(ALIGN_FILL);
  681. ADD_PROPERTY( PropertyInfo( Variant::STRING, "text" ), _SCS("set_text"),_SCS("get_text") );
  682. ADD_PROPERTYNZ(PropertyInfo(Variant::INT, "align", PROPERTY_HINT_ENUM, "Left,Center,Right,Fill"), _SCS("set_align"), _SCS("get_align"));
  683. ADD_PROPERTY( PropertyInfo( Variant::INT, "max_length" ), _SCS("set_max_length"),_SCS("get_max_length") );
  684. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "editable" ), _SCS("set_editable"),_SCS("is_editable") );
  685. ADD_PROPERTY( PropertyInfo( Variant::BOOL, "secret" ), _SCS("set_secret"),_SCS("is_secret") );
  686. }
  687. LineEdit::LineEdit() {
  688. align = ALIGN_LEFT;
  689. cached_width = 0;
  690. cursor_pos=0;
  691. window_pos=0;
  692. max_length = 0;
  693. pass=false;
  694. selection_clear();
  695. set_focus_mode( FOCUS_ALL );
  696. editable=true;
  697. set_default_cursor_shape(CURSOR_IBEAM);
  698. set_stop_mouse(true);
  699. }
  700. LineEdit::~LineEdit() {
  701. }