code_editor.cpp 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. /*************************************************************************/
  2. /* code_editor.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 "code_editor.h"
  30. #include "editor_settings.h"
  31. #include "scene/gui/margin_container.h"
  32. #include "scene/gui/separator.h"
  33. #include "os/keyboard.h"
  34. void GotoLineDialog::popup_find_line(TextEdit *p_edit) {
  35. text_editor=p_edit;
  36. line->set_text(itos(text_editor->cursor_get_line()));
  37. line->select_all();
  38. popup_centered(Size2(180,80));
  39. line->grab_focus();
  40. }
  41. int GotoLineDialog::get_line() const {
  42. return line->get_text().to_int();
  43. }
  44. void GotoLineDialog::ok_pressed() {
  45. if (get_line()<1 || get_line()>text_editor->get_line_count())
  46. return;
  47. text_editor->cursor_set_line(get_line()-1);
  48. hide();
  49. }
  50. GotoLineDialog::GotoLineDialog() {
  51. set_title(TTR("Go to Line"));
  52. Label *l = memnew(Label);
  53. l->set_text(TTR("Line Number:"));
  54. l->set_pos(Point2(5,5));
  55. add_child(l);
  56. line = memnew( LineEdit );
  57. line->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  58. line->set_begin( Point2(15,22) );
  59. line->set_end( Point2(15,35) );
  60. add_child(line);
  61. register_text_enter(line);
  62. text_editor=NULL;
  63. set_hide_on_ok(false);
  64. }
  65. void FindReplaceBar::_notification(int p_what) {
  66. if (p_what == NOTIFICATION_READY) {
  67. find_prev->set_icon(get_icon("MoveUp", "EditorIcons"));
  68. find_next->set_icon(get_icon("MoveDown", "EditorIcons"));
  69. hide_button->set_normal_texture(get_icon("Close","EditorIcons"));
  70. hide_button->set_hover_texture(get_icon("CloseHover","EditorIcons"));
  71. hide_button->set_pressed_texture(get_icon("Close","EditorIcons"));
  72. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  73. set_process_unhandled_input(is_visible());
  74. }
  75. }
  76. void FindReplaceBar::_unhandled_input(const InputEvent &p_event) {
  77. if (p_event.type == InputEvent::KEY) {
  78. const InputEventKey& k = p_event.key;
  79. if (k.pressed && (text_edit->has_focus() || text_vbc->is_a_parent_of(get_focus_owner()))) {
  80. bool accepted = true;
  81. switch (k.scancode) {
  82. case KEY_ESCAPE: {
  83. _hide_bar();
  84. } break;
  85. default: {
  86. accepted = false;
  87. } break;
  88. }
  89. if (accepted) {
  90. accept_event();
  91. }
  92. }
  93. }
  94. }
  95. bool FindReplaceBar::_search(bool p_include_current, bool p_backwards) {
  96. String text=get_search_text();
  97. uint32_t flags=0;
  98. if (is_whole_words())
  99. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  100. if (is_case_sensitive())
  101. flags|=TextEdit::SEARCH_MATCH_CASE;
  102. if (p_backwards)
  103. flags|=TextEdit::SEARCH_BACKWARDS;
  104. int line=text_edit->cursor_get_line();
  105. int col=text_edit->cursor_get_column();
  106. if (text_edit->is_selection_active() && !replace_all_mode) {
  107. line = text_edit->get_selection_from_line();
  108. col = text_edit->get_selection_from_column();
  109. }
  110. bool cursor_at_result=false;
  111. if (line==current_result_line && col>=current_result_col && col<=current_result_col+text.length()) {
  112. col=current_result_col;
  113. cursor_at_result=true;
  114. }
  115. if (!p_include_current) {
  116. if (p_backwards) {
  117. col-=text.length();
  118. if (col<0) {
  119. line-=1;
  120. if (line<0)
  121. line=text_edit->get_line_count()-1;
  122. col=text_edit->get_line(line).length();
  123. }
  124. } else if (cursor_at_result) {
  125. col+=text.length();
  126. if (col>text_edit->get_line(line).length()) {
  127. line+=1;
  128. if (line>=text_edit->get_line_count())
  129. line=0;
  130. col=0;
  131. }
  132. }
  133. }
  134. bool found = text_edit->search(text,flags,line,col,line,col);
  135. if (!found) {
  136. if (p_backwards) {
  137. line = text_edit->get_line_count()-1;
  138. col = text_edit->get_line(line).length()-1;
  139. } else {
  140. line = 0;
  141. col = 0;
  142. }
  143. found = text_edit->search(text,flags,line,col,line,col);
  144. }
  145. if (found) {
  146. text_edit->cursor_set_line(line);
  147. text_edit->cursor_set_column(p_backwards?col:col+text.length());
  148. text_edit->select(line,col,line,col+text.length());
  149. text_edit->set_search_text(text);
  150. text_edit->set_search_flags(flags);
  151. text_edit->set_current_search_result(line,col);
  152. current_result_line = line;
  153. current_result_col = col;
  154. set_error("");
  155. } else {
  156. current_result_line = -1;
  157. current_result_col = -1;
  158. text_edit->set_search_text("");
  159. set_error(text.empty()?"":TTR("No Matches"));
  160. }
  161. return found;
  162. }
  163. void FindReplaceBar::_replace() {
  164. if (text_edit->get_selection_text()==get_search_text()) {
  165. text_edit->insert_text_at_cursor(get_replace_text());
  166. }
  167. search_current();
  168. }
  169. void FindReplaceBar::_replace_all() {
  170. // line as x so it gets priority in comparison, column as y
  171. Point2i orig_cursor(text_edit->cursor_get_line(),text_edit->cursor_get_column());
  172. Point2i prev_match=Point2(-1,-1);
  173. bool selection_enabled = text_edit->is_selection_active();
  174. Point2i selection_begin,selection_end;
  175. if (selection_enabled) {
  176. selection_begin=Point2i(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  177. selection_end=Point2i(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  178. }
  179. int vsval = text_edit->get_v_scroll();
  180. text_edit->cursor_set_line(0);
  181. text_edit->cursor_set_column(0);
  182. int rc=0;
  183. replace_all_mode = true;
  184. text_edit->begin_complex_operation();
  185. while(_search(false)) {
  186. if (!text_edit->is_selection_active()) {
  187. // search selects
  188. break;
  189. }
  190. // replace area
  191. Point2i match_from(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  192. Point2i match_to(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  193. if (match_from < prev_match)
  194. break; // done
  195. prev_match=match_to;
  196. if (selection_enabled && is_selection_only()) {
  197. if (match_from<selection_begin || match_to>selection_end)
  198. continue;
  199. // replace but adjust selection bounds
  200. text_edit->insert_text_at_cursor(get_replace_text());
  201. if (match_to.x==selection_end.x)
  202. selection_end.y+=get_replace_text().length() - get_search_text().length();
  203. } else {
  204. //just replace
  205. text_edit->insert_text_at_cursor(get_replace_text());
  206. }
  207. rc++;
  208. }
  209. text_edit->end_complex_operation();
  210. replace_all_mode = false;
  211. // restore editor state (selection, cursor, scroll)
  212. text_edit->cursor_set_line(orig_cursor.x);
  213. text_edit->cursor_set_column(orig_cursor.y);
  214. if (selection_enabled && is_selection_only()) {
  215. // reselect
  216. text_edit->select(selection_begin.x,selection_begin.y,selection_end.x,selection_end.y);
  217. } else {
  218. text_edit->deselect();
  219. }
  220. text_edit->set_v_scroll(vsval);
  221. set_error(vformat(TTR("Replaced %d Ocurrence(s)."), rc));
  222. }
  223. void FindReplaceBar::search_current() {
  224. _search(true);
  225. }
  226. void FindReplaceBar::search_prev() {
  227. _search(false, true);
  228. }
  229. void FindReplaceBar::search_next() {
  230. _search();
  231. }
  232. void FindReplaceBar::_hide_bar() {
  233. text_edit->set_search_text("");
  234. current_result_line = -1;
  235. current_result_col = -1;
  236. replace_hbc->hide();
  237. replace_options_hbc->hide();
  238. hide();
  239. }
  240. void FindReplaceBar::_show_search() {
  241. show();
  242. search_text->grab_focus();
  243. if (text_edit->is_selection_active()) {
  244. search_text->set_text(text_edit->get_selection_text());
  245. }
  246. if (!get_search_text().empty()) {
  247. search_text->select_all();
  248. search_text->set_cursor_pos(search_text->get_text().length());
  249. search_current();
  250. }
  251. }
  252. void FindReplaceBar::popup_search() {
  253. replace_hbc->hide();
  254. replace_options_hbc->hide();
  255. _show_search();
  256. }
  257. void FindReplaceBar::popup_replace() {
  258. if (!replace_hbc->is_visible() || !replace_options_hbc->is_visible()) {
  259. replace_text->clear();
  260. replace_hbc->show();
  261. replace_options_hbc->show();
  262. }
  263. _show_search();
  264. }
  265. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  266. search_current();
  267. }
  268. void FindReplaceBar::_search_text_changed(const String& p_text) {
  269. search_current();
  270. }
  271. void FindReplaceBar::_search_text_entered(const String& p_text) {
  272. search_next();
  273. }
  274. String FindReplaceBar::get_search_text() const {
  275. return search_text->get_text();
  276. }
  277. String FindReplaceBar::get_replace_text() const {
  278. return replace_text->get_text();
  279. }
  280. bool FindReplaceBar::is_case_sensitive() const {
  281. return case_sensitive->is_pressed();
  282. }
  283. bool FindReplaceBar::is_whole_words() const {
  284. return whole_words->is_pressed();
  285. }
  286. bool FindReplaceBar::is_selection_only() const {
  287. return selection_only->is_pressed();
  288. }
  289. void FindReplaceBar::set_error(const String &p_label) {
  290. error_label->set_text(p_label);
  291. }
  292. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  293. text_edit = p_text_edit;
  294. text_edit->connect("_text_changed",this,"_search_text_changed",varray(String()));
  295. }
  296. void FindReplaceBar::_bind_methods() {
  297. ObjectTypeDB::bind_method("_unhandled_input",&FindReplaceBar::_unhandled_input);
  298. ObjectTypeDB::bind_method("_search_text_changed",&FindReplaceBar::_search_text_changed);
  299. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceBar::_search_text_entered);
  300. ObjectTypeDB::bind_method("_search_current",&FindReplaceBar::search_current);
  301. ObjectTypeDB::bind_method("_search_next",&FindReplaceBar::search_next);
  302. ObjectTypeDB::bind_method("_search_prev",&FindReplaceBar::search_prev);
  303. ObjectTypeDB::bind_method("_replace_pressed",&FindReplaceBar::_replace);
  304. ObjectTypeDB::bind_method("_replace_all_pressed",&FindReplaceBar::_replace_all);
  305. ObjectTypeDB::bind_method("_search_options_changed",&FindReplaceBar::_search_options_changed);
  306. ObjectTypeDB::bind_method("_hide_pressed",&FindReplaceBar::_hide_bar);
  307. ADD_SIGNAL(MethodInfo("search"));
  308. }
  309. FindReplaceBar::FindReplaceBar() {
  310. text_vbc = memnew(VBoxContainer);
  311. add_child(text_vbc);
  312. HBoxContainer *search_hbc = memnew(HBoxContainer);
  313. text_vbc->add_child(search_hbc);
  314. search_text = memnew(LineEdit);
  315. search_hbc->add_child(search_text);
  316. search_text->set_custom_minimum_size(Size2(200, 0));
  317. search_text->connect("text_changed",this,"_search_text_changed");
  318. search_text->connect("text_entered",this,"_search_text_entered");
  319. find_prev = memnew(ToolButton);
  320. search_hbc->add_child(find_prev);
  321. find_prev->set_focus_mode(FOCUS_NONE);
  322. find_prev->connect("pressed",this,"_search_prev");
  323. find_next = memnew(ToolButton);
  324. search_hbc->add_child(find_next);
  325. find_next->set_focus_mode(FOCUS_NONE);
  326. find_next->connect("pressed",this,"_search_next");
  327. replace_hbc = memnew(HBoxContainer);
  328. text_vbc->add_child(replace_hbc);
  329. replace_hbc->hide();
  330. replace_text = memnew(LineEdit);
  331. replace_hbc->add_child(replace_text);
  332. replace_text->set_custom_minimum_size(Size2(200, 0));
  333. replace_text->connect("text_entered",this,"_search_text_entered");
  334. replace = memnew(ToolButton);
  335. replace_hbc->add_child(replace);
  336. replace->set_text(TTR("Replace"));
  337. replace->set_focus_mode(FOCUS_NONE);
  338. replace->connect("pressed",this,"_replace_pressed");
  339. replace_all = memnew(ToolButton);
  340. replace_hbc->add_child(replace_all);
  341. replace_all->set_text(TTR("Replace All"));
  342. replace_all->set_focus_mode(FOCUS_NONE);
  343. replace_all->connect("pressed",this,"_replace_all_pressed");
  344. Control *spacer_split = memnew( Control );
  345. spacer_split->set_custom_minimum_size(Size2(0, 1));
  346. text_vbc->add_child(spacer_split);
  347. VBoxContainer *options_vbc = memnew(VBoxContainer);
  348. add_child(options_vbc);
  349. options_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  350. HBoxContainer *search_options = memnew(HBoxContainer);
  351. options_vbc->add_child(search_options);
  352. case_sensitive = memnew(CheckBox);
  353. search_options->add_child(case_sensitive);
  354. case_sensitive->set_text(TTR("Match Case"));
  355. case_sensitive->set_focus_mode(FOCUS_NONE);
  356. case_sensitive->connect("toggled",this,"_search_options_changed");
  357. whole_words = memnew(CheckBox);
  358. search_options->add_child(whole_words);
  359. whole_words->set_text(TTR("Whole Words"));
  360. whole_words->set_focus_mode(FOCUS_NONE);
  361. whole_words->connect("toggled",this,"_search_options_changed");
  362. error_label = memnew(Label);
  363. search_options->add_child(error_label);
  364. search_options->add_spacer();
  365. hide_button = memnew(TextureButton);
  366. search_options->add_child(hide_button);
  367. hide_button->set_focus_mode(FOCUS_NONE);
  368. hide_button->connect("pressed",this,"_hide_pressed");
  369. replace_options_hbc = memnew(HBoxContainer);
  370. options_vbc->add_child(replace_options_hbc);
  371. replace_options_hbc->hide();
  372. selection_only = memnew(CheckBox);
  373. replace_options_hbc->add_child(selection_only);
  374. selection_only->set_text(TTR("Selection Only"));
  375. selection_only->set_focus_mode(FOCUS_NONE);
  376. selection_only->connect("toggled",this,"_search_options_changed");
  377. }
  378. void FindReplaceDialog::popup_search() {
  379. set_title(TTR("Search"));
  380. replace_mc->hide();
  381. replace_label->hide();
  382. replace_vb->hide();
  383. skip->hide();
  384. popup_centered(Point2(300,190));
  385. get_ok()->set_text(TTR("Find"));
  386. search_text->grab_focus();
  387. if (text_edit->is_selection_active() && ( text_edit->get_selection_from_line() == text_edit->get_selection_to_line())) {
  388. search_text->set_text( text_edit->get_selection_text() );
  389. }
  390. search_text->select_all();
  391. error_label->set_text("");
  392. }
  393. void FindReplaceDialog::popup_replace() {
  394. set_title(TTR("Replace"));
  395. bool do_selection=(text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line());
  396. set_replace_selection_only(do_selection);
  397. if (!do_selection && text_edit->is_selection_active()) {
  398. search_text->set_text(text_edit->get_selection_text());
  399. }
  400. replace_mc->show();
  401. replace_label->show();
  402. replace_vb->show();
  403. popup_centered(Point2(300,300));
  404. if (search_text->get_text()!="" && replace_text->get_text()=="") {
  405. search_text->select(0,0);
  406. replace_text->grab_focus();
  407. } else {
  408. search_text->grab_focus();
  409. search_text->select_all();
  410. }
  411. error_label->set_text("");
  412. if (prompt->is_pressed()) {
  413. skip->show();
  414. get_ok()->set_text(TTR("Next"));
  415. selection_only->set_disabled(true);
  416. } else {
  417. skip->hide();
  418. get_ok()->set_text(TTR("Replace"));
  419. selection_only->set_disabled(false);
  420. }
  421. }
  422. void FindReplaceDialog::_search_callback() {
  423. if (is_replace_mode())
  424. _replace();
  425. else
  426. _search();
  427. }
  428. void FindReplaceDialog::_replace_skip_callback() {
  429. _search();
  430. }
  431. void FindReplaceDialog::_replace() {
  432. text_edit->begin_complex_operation();
  433. if (is_replace_all_mode()) {
  434. //line as x so it gets priority in comparison, column as y
  435. Point2i orig_cursor(text_edit->cursor_get_line(),text_edit->cursor_get_column());
  436. Point2i prev_match=Point2(-1,-1);
  437. bool selection_enabled = text_edit->is_selection_active();
  438. Point2i selection_begin,selection_end;
  439. if (selection_enabled) {
  440. selection_begin=Point2i(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  441. selection_end=Point2i(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  442. }
  443. int vsval = text_edit->get_v_scroll();
  444. //int hsval = text_edit->get_h_scroll();
  445. text_edit->cursor_set_line(0);
  446. text_edit->cursor_set_column(0);
  447. int rc=0;
  448. while(_search()) {
  449. if (!text_edit->is_selection_active()) {
  450. //search selects
  451. break;
  452. }
  453. //replace area
  454. Point2i match_from(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  455. Point2i match_to(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  456. if (match_from < prev_match)
  457. break; //done
  458. prev_match=match_to;
  459. if (selection_enabled && is_replace_selection_only()) {
  460. if (match_from<selection_begin || match_to>selection_end)
  461. continue;
  462. //replace but adjust selection bounds
  463. text_edit->insert_text_at_cursor(get_replace_text());
  464. if (match_to.x==selection_end.x)
  465. selection_end.y+=get_replace_text().length() - get_search_text().length();
  466. } else {
  467. //just replace
  468. text_edit->insert_text_at_cursor(get_replace_text());
  469. }
  470. rc++;
  471. }
  472. //restore editor state (selection, cursor, scroll)
  473. text_edit->cursor_set_line(orig_cursor.x);
  474. text_edit->cursor_set_column(orig_cursor.y);
  475. if (selection_enabled && is_replace_selection_only()) {
  476. //reselect
  477. text_edit->select(selection_begin.x,selection_begin.y,selection_end.x,selection_end.y);
  478. } else {
  479. text_edit->deselect();
  480. }
  481. text_edit->set_v_scroll(vsval);
  482. // text_edit->set_h_scroll(hsval);
  483. error_label->set_text(vformat(TTR("Replaced %d ocurrence(s)."),rc));
  484. //hide();
  485. } else {
  486. if (text_edit->get_selection_text()==get_search_text()) {
  487. text_edit->insert_text_at_cursor(get_replace_text());
  488. }
  489. _search();
  490. }
  491. text_edit->end_complex_operation();
  492. }
  493. bool FindReplaceDialog::_search() {
  494. String text=get_search_text();
  495. uint32_t flags=0;
  496. if (is_whole_words())
  497. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  498. if (is_case_sensitive())
  499. flags|=TextEdit::SEARCH_MATCH_CASE;
  500. if (is_backwards())
  501. flags|=TextEdit::SEARCH_BACKWARDS;
  502. int line=text_edit->cursor_get_line(),col=text_edit->cursor_get_column();
  503. if (is_backwards()) {
  504. col-=1;
  505. if (col<0) {
  506. line-=1;
  507. if (line<0) {
  508. line=text_edit->get_line_count()-1;
  509. }
  510. col=text_edit->get_line(line).length();
  511. }
  512. }
  513. bool found = text_edit->search(text,flags,line,col,line,col);
  514. if (found) {
  515. // print_line("found");
  516. text_edit->cursor_set_line(line);
  517. if (is_backwards())
  518. text_edit->cursor_set_column(col);
  519. else
  520. text_edit->cursor_set_column(col+text.length());
  521. text_edit->select(line,col,line,col+text.length());
  522. set_error("");
  523. return true;
  524. } else {
  525. set_error(TTR("Not found!"));
  526. return false;
  527. }
  528. }
  529. void FindReplaceDialog::_prompt_changed() {
  530. if (prompt->is_pressed()) {
  531. skip->show();
  532. get_ok()->set_text(TTR("Next"));
  533. selection_only->set_disabled(true);
  534. } else {
  535. skip->hide();
  536. get_ok()->set_text(TTR("Replace"));
  537. selection_only->set_disabled(false);
  538. }
  539. }
  540. void FindReplaceDialog::_skip_pressed() {
  541. _replace_skip_callback();
  542. }
  543. bool FindReplaceDialog::is_replace_mode() const {
  544. return replace_text->is_visible();
  545. }
  546. bool FindReplaceDialog::is_replace_all_mode() const {
  547. return !prompt->is_pressed();
  548. }
  549. bool FindReplaceDialog::is_replace_selection_only() const {
  550. return selection_only->is_pressed();
  551. }
  552. void FindReplaceDialog::set_replace_selection_only(bool p_enable){
  553. selection_only->set_pressed(p_enable);
  554. }
  555. void FindReplaceDialog::ok_pressed() {
  556. _search_callback();
  557. }
  558. void FindReplaceDialog::_search_text_entered(const String& p_text) {
  559. if (replace_text->is_visible())
  560. return;
  561. emit_signal("search");
  562. _search();
  563. }
  564. void FindReplaceDialog::_replace_text_entered(const String& p_text) {
  565. if (!replace_text->is_visible())
  566. return;
  567. emit_signal("search");
  568. _replace();
  569. }
  570. String FindReplaceDialog::get_search_text() const {
  571. return search_text->get_text();
  572. }
  573. String FindReplaceDialog::get_replace_text() const {
  574. return replace_text->get_text();
  575. }
  576. bool FindReplaceDialog::is_whole_words() const {
  577. return whole_words->is_pressed();
  578. }
  579. bool FindReplaceDialog::is_case_sensitive() const {
  580. return case_sensitive->is_pressed();
  581. }
  582. bool FindReplaceDialog::is_backwards() const {
  583. return backwards->is_pressed();
  584. }
  585. void FindReplaceDialog::set_error(const String& p_error) {
  586. error_label->set_text(p_error);
  587. }
  588. void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
  589. text_edit=p_text_edit;
  590. }
  591. void FindReplaceDialog::search_next() {
  592. _search();
  593. }
  594. void FindReplaceDialog::_bind_methods() {
  595. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceDialog::_search_text_entered);
  596. ObjectTypeDB::bind_method("_replace_text_entered",&FindReplaceDialog::_replace_text_entered);
  597. ObjectTypeDB::bind_method("_prompt_changed",&FindReplaceDialog::_prompt_changed);
  598. ObjectTypeDB::bind_method("_skip_pressed",&FindReplaceDialog::_skip_pressed);
  599. ADD_SIGNAL(MethodInfo("search"));
  600. ADD_SIGNAL(MethodInfo("skip"));
  601. }
  602. FindReplaceDialog::FindReplaceDialog() {
  603. set_self_opacity(0.8);
  604. VBoxContainer *vb = memnew( VBoxContainer );
  605. add_child(vb);
  606. set_child_rect(vb);
  607. search_text = memnew( LineEdit );
  608. vb->add_margin_child(TTR("Search"),search_text);
  609. search_text->connect("text_entered", this,"_search_text_entered");
  610. //search_text->set_self_opacity(0.7);
  611. replace_label = memnew( Label);
  612. replace_label->set_text(TTR("Replace By"));
  613. vb->add_child(replace_label);
  614. replace_mc= memnew( MarginContainer);
  615. vb->add_child(replace_mc);
  616. replace_text = memnew( LineEdit );
  617. replace_text->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  618. replace_text->set_begin( Point2(15,132) );
  619. replace_text->set_end( Point2(15,135) );
  620. //replace_text->set_self_opacity(0.7);
  621. replace_mc->add_child(replace_text);
  622. replace_text->connect("text_entered", this,"_replace_text_entered");
  623. MarginContainer *opt_mg = memnew( MarginContainer );
  624. vb->add_child(opt_mg);
  625. VBoxContainer *svb = memnew( VBoxContainer);
  626. opt_mg->add_child(svb);
  627. svb ->add_child(memnew(Label));
  628. whole_words = memnew( CheckButton );
  629. whole_words->set_text(TTR("Whole Words"));
  630. svb->add_child(whole_words);
  631. case_sensitive = memnew( CheckButton );
  632. case_sensitive->set_text(TTR("Case Sensitive"));
  633. svb->add_child(case_sensitive);
  634. backwards = memnew( CheckButton );
  635. backwards->set_text(TTR("Backwards"));
  636. svb->add_child(backwards);
  637. opt_mg = memnew( MarginContainer );
  638. vb->add_child(opt_mg);
  639. VBoxContainer *rvb = memnew( VBoxContainer);
  640. opt_mg->add_child(rvb);
  641. replace_vb=rvb;
  642. // rvb ->add_child(memnew(HSeparator));
  643. rvb ->add_child(memnew(Label));
  644. prompt = memnew( CheckButton );
  645. prompt->set_text(TTR("Prompt On Replace"));
  646. rvb->add_child(prompt);
  647. prompt->connect("pressed", this,"_prompt_changed");
  648. selection_only = memnew( CheckButton );
  649. selection_only->set_text(TTR("Selection Only"));
  650. rvb->add_child(selection_only);
  651. int margin = get_constant("margin","Dialogs");
  652. int button_margin = get_constant("button_margin","Dialogs");
  653. skip = memnew( Button );
  654. skip->set_anchor( MARGIN_LEFT, ANCHOR_END );
  655. skip->set_anchor( MARGIN_TOP, ANCHOR_END );
  656. skip->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  657. skip->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  658. skip->set_begin( Point2( 70, button_margin ) );
  659. skip->set_end( Point2( 10, margin ) );
  660. skip->set_text(TTR("Skip"));
  661. add_child(skip);
  662. skip->connect("pressed", this,"_skip_pressed");
  663. error_label = memnew( Label );
  664. error_label->set_align(Label::ALIGN_CENTER);
  665. error_label->add_color_override("font_color",Color(1,0.4,0.3));
  666. error_label->add_color_override("font_color_shadow",Color(0,0,0,0.2));
  667. error_label->add_constant_override("shadow_as_outline",1);
  668. vb->add_child(error_label);
  669. set_hide_on_ok(false);
  670. }
  671. /*** CODE EDITOR ****/
  672. void CodeTextEditor::_line_col_changed() {
  673. String text = String()+TTR("Line:")+" "+itos(text_editor->cursor_get_line()+1)+", "+TTR("Col:")+" "+itos(text_editor->cursor_get_column());
  674. line_col->set_text(text);
  675. }
  676. void CodeTextEditor::_text_changed() {
  677. code_complete_timer->start();
  678. idle->start();
  679. }
  680. void CodeTextEditor::_code_complete_timer_timeout() {
  681. if (!is_visible())
  682. return;
  683. if (enable_complete_timer)
  684. text_editor->query_code_comple();
  685. }
  686. void CodeTextEditor::_complete_request() {
  687. List<String> entries;
  688. _code_complete_script(text_editor->get_text_for_completion(),&entries);
  689. // print_line("COMPLETE: "+p_request);
  690. if (entries.size()==0)
  691. return;
  692. Vector<String> strs;
  693. strs.resize(entries.size());
  694. int i=0;
  695. for(List<String>::Element *E=entries.front();E;E=E->next()) {
  696. strs[i++]=E->get();
  697. }
  698. text_editor->code_complete(strs);
  699. }
  700. void CodeTextEditor::set_error(const String& p_error) {
  701. if (p_error!="") {
  702. error->set_text(p_error);
  703. error->show();
  704. } else {
  705. error->hide();
  706. }
  707. }
  708. void CodeTextEditor::_update_font() {
  709. // FONTS
  710. String editor_font = EDITOR_DEF("text_editor/font", "");
  711. bool font_overrode = false;
  712. if (editor_font!="") {
  713. Ref<Font> fnt = ResourceLoader::load(editor_font);
  714. if (fnt.is_valid()) {
  715. text_editor->add_font_override("font",fnt);
  716. font_overrode = true;
  717. }
  718. }
  719. if(!font_overrode)
  720. text_editor->add_font_override("font",get_font("source","EditorFonts"));
  721. }
  722. void CodeTextEditor::_on_settings_change() {
  723. _update_font();
  724. // AUTO BRACE COMPLETION
  725. text_editor->set_auto_brace_completion(
  726. EDITOR_DEF("text_editor/auto_brace_complete", true)
  727. );
  728. code_complete_timer->set_wait_time(
  729. EDITOR_DEF("text_editor/code_complete_delay",.3f)
  730. );
  731. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  732. // call hint settings
  733. text_editor->set_callhint_settings(
  734. EDITOR_DEF("text_editor/put_callhint_tooltip_below_current_line", true),
  735. EDITOR_DEF("text_editor/callhint_tooltip_offset", Vector2())
  736. );
  737. }
  738. void CodeTextEditor::_text_changed_idle_timeout() {
  739. _validate_script();
  740. }
  741. void CodeTextEditor::_notification(int p_what) {
  742. if (p_what==EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED)
  743. _load_theme_settings();
  744. if (p_what==NOTIFICATION_ENTER_TREE) {
  745. _update_font();
  746. }
  747. }
  748. void CodeTextEditor::_bind_methods() {
  749. ObjectTypeDB::bind_method("_line_col_changed",&CodeTextEditor::_line_col_changed);
  750. ObjectTypeDB::bind_method("_text_changed",&CodeTextEditor::_text_changed);
  751. ObjectTypeDB::bind_method("_on_settings_change",&CodeTextEditor::_on_settings_change);
  752. ObjectTypeDB::bind_method("_text_changed_idle_timeout",&CodeTextEditor::_text_changed_idle_timeout);
  753. ObjectTypeDB::bind_method("_code_complete_timer_timeout",&CodeTextEditor::_code_complete_timer_timeout);
  754. ObjectTypeDB::bind_method("_complete_request",&CodeTextEditor::_complete_request);
  755. }
  756. CodeTextEditor::CodeTextEditor() {
  757. find_replace_bar = memnew( FindReplaceBar );
  758. add_child(find_replace_bar);
  759. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  760. find_replace_bar->hide();
  761. text_editor = memnew( TextEdit );
  762. add_child(text_editor);
  763. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  764. find_replace_bar->set_text_edit(text_editor);
  765. text_editor->set_show_line_numbers(true);
  766. text_editor->set_brace_matching(true);
  767. text_editor->set_auto_indent(true);
  768. MarginContainer *status_mc = memnew( MarginContainer );
  769. add_child(status_mc);
  770. status_mc->set("custom_constants/margin_left", 2);
  771. status_mc->set("custom_constants/margin_top", 5);
  772. status_mc->set("custom_constants/margin_right", 2);
  773. status_mc->set("custom_constants/margin_bottom", 1);
  774. HBoxContainer *status_bar = memnew( HBoxContainer );
  775. status_mc->add_child(status_bar);
  776. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  777. idle = memnew( Timer );
  778. add_child(idle);
  779. idle->set_one_shot(true);
  780. idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay",2));
  781. code_complete_timer = memnew(Timer);
  782. add_child(code_complete_timer);
  783. code_complete_timer->set_one_shot(true);
  784. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  785. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/code_complete_delay",.3f));
  786. error = memnew( Label );
  787. status_bar->add_child(error);
  788. error->hide();
  789. error->set_valign(Label::VALIGN_CENTER);
  790. error->add_color_override("font_color",Color(1,0.7,0.6,0.9));
  791. status_bar->add_spacer();
  792. line_col = memnew( Label );
  793. status_bar->add_child(line_col);
  794. line_col->set_valign(Label::VALIGN_CENTER);
  795. text_editor->connect("cursor_changed", this,"_line_col_changed");
  796. text_editor->connect("text_changed", this,"_text_changed");
  797. text_editor->connect("request_completion", this,"_complete_request");
  798. Vector<String> cs;
  799. cs.push_back(".");
  800. cs.push_back(",");
  801. cs.push_back("(");
  802. text_editor->set_completion(true,cs);
  803. idle->connect("timeout", this,"_text_changed_idle_timeout");
  804. code_complete_timer->connect("timeout", this,"_code_complete_timer_timeout");
  805. EditorSettings::get_singleton()->connect("settings_changed",this,"_on_settings_change");
  806. }