code_editor.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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::_on_settings_change() {
  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","Fonts"));
  721. // AUTO BRACE COMPLETION
  722. text_editor->set_auto_brace_completion(
  723. EDITOR_DEF("text_editor/auto_brace_complete", true)
  724. );
  725. code_complete_timer->set_wait_time(
  726. EDITOR_DEF("text_editor/code_complete_delay",.3f)
  727. );
  728. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  729. // call hint settings
  730. text_editor->set_callhint_settings(
  731. EDITOR_DEF("text_editor/put_callhint_tooltip_below_current_line", true),
  732. EDITOR_DEF("text_editor/callhint_tooltip_offset", Vector2())
  733. );
  734. }
  735. void CodeTextEditor::_text_changed_idle_timeout() {
  736. _validate_script();
  737. }
  738. void CodeTextEditor::_notification(int p_what) {
  739. if (p_what==EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED)
  740. _load_theme_settings();
  741. }
  742. void CodeTextEditor::_bind_methods() {
  743. ObjectTypeDB::bind_method("_line_col_changed",&CodeTextEditor::_line_col_changed);
  744. ObjectTypeDB::bind_method("_text_changed",&CodeTextEditor::_text_changed);
  745. ObjectTypeDB::bind_method("_on_settings_change",&CodeTextEditor::_on_settings_change);
  746. ObjectTypeDB::bind_method("_text_changed_idle_timeout",&CodeTextEditor::_text_changed_idle_timeout);
  747. ObjectTypeDB::bind_method("_code_complete_timer_timeout",&CodeTextEditor::_code_complete_timer_timeout);
  748. ObjectTypeDB::bind_method("_complete_request",&CodeTextEditor::_complete_request);
  749. }
  750. CodeTextEditor::CodeTextEditor() {
  751. find_replace_bar = memnew( FindReplaceBar );
  752. add_child(find_replace_bar);
  753. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  754. find_replace_bar->hide();
  755. text_editor = memnew( TextEdit );
  756. add_child(text_editor);
  757. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  758. find_replace_bar->set_text_edit(text_editor);
  759. String editor_font = EDITOR_DEF("text_editor/font", "");
  760. bool font_overrode = false;
  761. if (editor_font!="") {
  762. Ref<Font> fnt = ResourceLoader::load(editor_font);
  763. if (fnt.is_valid()) {
  764. text_editor->add_font_override("font",fnt);
  765. font_overrode = true;
  766. }
  767. }
  768. if (!font_overrode)
  769. text_editor->add_font_override("font",get_font("source","Fonts"));
  770. text_editor->set_show_line_numbers(true);
  771. text_editor->set_brace_matching(true);
  772. text_editor->set_auto_indent(true);
  773. MarginContainer *status_mc = memnew( MarginContainer );
  774. add_child(status_mc);
  775. status_mc->set("custom_constants/margin_left", 2);
  776. status_mc->set("custom_constants/margin_top", 5);
  777. status_mc->set("custom_constants/margin_right", 2);
  778. status_mc->set("custom_constants/margin_bottom", 1);
  779. HBoxContainer *status_bar = memnew( HBoxContainer );
  780. status_mc->add_child(status_bar);
  781. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  782. idle = memnew( Timer );
  783. add_child(idle);
  784. idle->set_one_shot(true);
  785. idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay",2));
  786. code_complete_timer = memnew(Timer);
  787. add_child(code_complete_timer);
  788. code_complete_timer->set_one_shot(true);
  789. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  790. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/code_complete_delay",.3f));
  791. error = memnew( Label );
  792. status_bar->add_child(error);
  793. error->hide();
  794. error->set_valign(Label::VALIGN_CENTER);
  795. error->add_color_override("font_color",Color(1,0.7,0.6,0.9));
  796. status_bar->add_spacer();
  797. line_col = memnew( Label );
  798. status_bar->add_child(line_col);
  799. line_col->set_valign(Label::VALIGN_CENTER);
  800. text_editor->connect("cursor_changed", this,"_line_col_changed");
  801. text_editor->connect("text_changed", this,"_text_changed");
  802. text_editor->connect("request_completion", this,"_complete_request");
  803. Vector<String> cs;
  804. cs.push_back(".");
  805. cs.push_back(",");
  806. cs.push_back("(");
  807. text_editor->set_completion(true,cs);
  808. idle->connect("timeout", this,"_text_changed_idle_timeout");
  809. code_complete_timer->connect("timeout", this,"_code_complete_timer_timeout");
  810. EditorSettings::get_singleton()->connect("settings_changed",this,"_on_settings_change");
  811. }