code_editor.cpp 32 KB

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