code_editor.cpp 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "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 "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. String replace_text=get_replace_text();
  144. int search_text_len=get_search_text().length();
  145. int rc=0;
  146. replace_all_mode = true;
  147. text_edit->begin_complex_operation();
  148. while (search_next()) {
  149. // replace area
  150. Point2i match_from(result_line,result_col);
  151. Point2i match_to(result_line,result_col+search_text_len);
  152. if (match_from < prev_match)
  153. break; // done
  154. prev_match=Point2i(result_line,result_col+replace_text.length());
  155. text_edit->select(result_line,result_col,result_line,match_to.y);
  156. if (selection_enabled && is_selection_only()) {
  157. if (match_from<selection_begin || match_to>selection_end)
  158. continue;
  159. // replace but adjust selection bounds
  160. text_edit->insert_text_at_cursor(replace_text);
  161. if (match_to.x==selection_end.x)
  162. selection_end.y+=replace_text.length()-search_text_len;
  163. } else {
  164. // just replace
  165. text_edit->insert_text_at_cursor(replace_text);
  166. }
  167. rc++;
  168. }
  169. text_edit->end_complex_operation();
  170. replace_all_mode = false;
  171. // restore editor state (selection, cursor, scroll)
  172. text_edit->cursor_set_line(orig_cursor.x);
  173. text_edit->cursor_set_column(orig_cursor.y);
  174. if (selection_enabled && is_selection_only()) {
  175. // reselect
  176. text_edit->select(selection_begin.x,selection_begin.y,selection_end.x,selection_end.y);
  177. } else {
  178. text_edit->deselect();
  179. }
  180. text_edit->set_v_scroll(vsval);
  181. set_error(vformat(TTR("Replaced %d Ocurrence(s)."), rc));
  182. }
  183. void FindReplaceBar::_get_search_from(int& r_line, int& r_col) {
  184. r_line=text_edit->cursor_get_line();
  185. r_col=text_edit->cursor_get_column();
  186. if (text_edit->is_selection_active() && !replace_all_mode) {
  187. int selection_line=text_edit->get_selection_from_line();
  188. if (text_edit->get_selection_text()==get_search_text() && r_line==selection_line) {
  189. int selection_from_col=text_edit->get_selection_from_column();
  190. if (r_col>=selection_from_col && r_col<=text_edit->get_selection_to_column()) {
  191. r_col=selection_line;
  192. r_col=selection_from_col;
  193. }
  194. }
  195. }
  196. if (r_line==result_line && r_col>=result_col && r_col<=result_col+get_search_text().length()) {
  197. r_col=result_col;
  198. }
  199. }
  200. bool FindReplaceBar::search_current() {
  201. uint32_t flags=0;
  202. if (is_whole_words())
  203. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  204. if (is_case_sensitive())
  205. flags|=TextEdit::SEARCH_MATCH_CASE;
  206. int line, col;
  207. _get_search_from(line, col);
  208. return _search(flags,line,col);
  209. }
  210. bool FindReplaceBar::search_prev() {
  211. uint32_t flags=0;
  212. String text = get_search_text();
  213. if (is_whole_words())
  214. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  215. if (is_case_sensitive())
  216. flags|=TextEdit::SEARCH_MATCH_CASE;
  217. flags|=TextEdit::SEARCH_BACKWARDS;
  218. int line, col;
  219. _get_search_from(line, col);
  220. col-=text.length();
  221. if (col<0) {
  222. line-=1;
  223. if (line<0)
  224. line=text_edit->get_line_count()-1;
  225. col=text_edit->get_line(line).length();
  226. }
  227. return _search(flags,line,col);
  228. }
  229. bool FindReplaceBar::search_next() {
  230. uint32_t flags=0;
  231. String text = get_search_text();
  232. if (is_whole_words())
  233. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  234. if (is_case_sensitive())
  235. flags|=TextEdit::SEARCH_MATCH_CASE;
  236. int line, col;
  237. _get_search_from(line, col);
  238. if (line==result_line && col==result_col) {
  239. col+=text.length();
  240. if (col>text_edit->get_line(line).length()) {
  241. line+=1;
  242. if (line>=text_edit->get_line_count())
  243. line=0;
  244. col=0;
  245. }
  246. }
  247. return _search(flags,line,col);
  248. }
  249. void FindReplaceBar::_hide_bar() {
  250. if (replace_text->has_focus() || search_text->has_focus())
  251. text_edit->grab_focus();
  252. text_edit->set_search_text("");
  253. result_line = -1;
  254. result_col = -1;
  255. replace_hbc->hide();
  256. replace_options_hbc->hide();
  257. hide();
  258. }
  259. void FindReplaceBar::_show_search() {
  260. show();
  261. search_text->grab_focus();
  262. if (text_edit->is_selection_active()) {
  263. search_text->set_text(text_edit->get_selection_text());
  264. }
  265. if (!get_search_text().empty()) {
  266. search_text->select_all();
  267. search_text->set_cursor_pos(search_text->get_text().length());
  268. search_current();
  269. }
  270. }
  271. void FindReplaceBar::popup_search() {
  272. replace_hbc->hide();
  273. replace_options_hbc->hide();
  274. _show_search();
  275. }
  276. void FindReplaceBar::popup_replace() {
  277. if (!replace_hbc->is_visible() || !replace_options_hbc->is_visible()) {
  278. replace_text->clear();
  279. replace_hbc->show();
  280. replace_options_hbc->show();
  281. }
  282. _show_search();
  283. }
  284. void FindReplaceBar::_search_options_changed(bool p_pressed) {
  285. search_current();
  286. }
  287. void FindReplaceBar::_editor_text_changed() {
  288. if (is_visible()) {
  289. preserve_cursor=true;
  290. search_current();
  291. preserve_cursor=false;
  292. }
  293. }
  294. void FindReplaceBar::_search_text_changed(const String& p_text) {
  295. search_current();
  296. }
  297. void FindReplaceBar::_search_text_entered(const String& p_text) {
  298. search_next();
  299. }
  300. String FindReplaceBar::get_search_text() const {
  301. return search_text->get_text();
  302. }
  303. String FindReplaceBar::get_replace_text() const {
  304. return replace_text->get_text();
  305. }
  306. bool FindReplaceBar::is_case_sensitive() const {
  307. return case_sensitive->is_pressed();
  308. }
  309. bool FindReplaceBar::is_whole_words() const {
  310. return whole_words->is_pressed();
  311. }
  312. bool FindReplaceBar::is_selection_only() const {
  313. return selection_only->is_pressed();
  314. }
  315. void FindReplaceBar::set_error(const String &p_label) {
  316. error_label->set_text(p_label);
  317. }
  318. void FindReplaceBar::set_text_edit(TextEdit *p_text_edit) {
  319. text_edit = p_text_edit;
  320. text_edit->connect("text_changed",this,"_editor_text_changed");
  321. }
  322. void FindReplaceBar::_bind_methods() {
  323. ObjectTypeDB::bind_method("_unhandled_input",&FindReplaceBar::_unhandled_input);
  324. ObjectTypeDB::bind_method("_editor_text_changed",&FindReplaceBar::_editor_text_changed);
  325. ObjectTypeDB::bind_method("_search_text_changed",&FindReplaceBar::_search_text_changed);
  326. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceBar::_search_text_entered);
  327. ObjectTypeDB::bind_method("_search_current",&FindReplaceBar::search_current);
  328. ObjectTypeDB::bind_method("_search_next",&FindReplaceBar::search_next);
  329. ObjectTypeDB::bind_method("_search_prev",&FindReplaceBar::search_prev);
  330. ObjectTypeDB::bind_method("_replace_pressed",&FindReplaceBar::_replace);
  331. ObjectTypeDB::bind_method("_replace_all_pressed",&FindReplaceBar::_replace_all);
  332. ObjectTypeDB::bind_method("_search_options_changed",&FindReplaceBar::_search_options_changed);
  333. ObjectTypeDB::bind_method("_hide_pressed",&FindReplaceBar::_hide_bar);
  334. ADD_SIGNAL(MethodInfo("search"));
  335. }
  336. FindReplaceBar::FindReplaceBar() {
  337. replace_all_mode=false;
  338. preserve_cursor=false;
  339. text_vbc = memnew(VBoxContainer);
  340. add_child(text_vbc);
  341. HBoxContainer *search_hbc = memnew(HBoxContainer);
  342. text_vbc->add_child(search_hbc);
  343. search_text = memnew(LineEdit);
  344. search_hbc->add_child(search_text);
  345. search_text->set_custom_minimum_size(Size2(200, 0));
  346. search_text->connect("text_changed",this,"_search_text_changed");
  347. search_text->connect("text_entered",this,"_search_text_entered");
  348. find_prev = memnew(ToolButton);
  349. search_hbc->add_child(find_prev);
  350. find_prev->set_focus_mode(FOCUS_NONE);
  351. find_prev->connect("pressed",this,"_search_prev");
  352. find_next = memnew(ToolButton);
  353. search_hbc->add_child(find_next);
  354. find_next->set_focus_mode(FOCUS_NONE);
  355. find_next->connect("pressed",this,"_search_next");
  356. replace_hbc = memnew(HBoxContainer);
  357. text_vbc->add_child(replace_hbc);
  358. replace_hbc->hide();
  359. replace_text = memnew(LineEdit);
  360. replace_hbc->add_child(replace_text);
  361. replace_text->set_custom_minimum_size(Size2(200, 0));
  362. replace_text->connect("text_entered",this,"_search_text_entered");
  363. replace = memnew(ToolButton);
  364. replace_hbc->add_child(replace);
  365. replace->set_text(TTR("Replace"));
  366. replace->set_focus_mode(FOCUS_NONE);
  367. replace->connect("pressed",this,"_replace_pressed");
  368. replace_all = memnew(ToolButton);
  369. replace_hbc->add_child(replace_all);
  370. replace_all->set_text(TTR("Replace All"));
  371. replace_all->set_focus_mode(FOCUS_NONE);
  372. replace_all->connect("pressed",this,"_replace_all_pressed");
  373. Control *spacer_split = memnew( Control );
  374. spacer_split->set_custom_minimum_size(Size2(0, 1));
  375. text_vbc->add_child(spacer_split);
  376. VBoxContainer *options_vbc = memnew(VBoxContainer);
  377. add_child(options_vbc);
  378. options_vbc->set_h_size_flags(SIZE_EXPAND_FILL);
  379. HBoxContainer *search_options = memnew(HBoxContainer);
  380. options_vbc->add_child(search_options);
  381. case_sensitive = memnew(CheckBox);
  382. search_options->add_child(case_sensitive);
  383. case_sensitive->set_text(TTR("Match Case"));
  384. case_sensitive->set_focus_mode(FOCUS_NONE);
  385. case_sensitive->connect("toggled",this,"_search_options_changed");
  386. whole_words = memnew(CheckBox);
  387. search_options->add_child(whole_words);
  388. whole_words->set_text(TTR("Whole Words"));
  389. whole_words->set_focus_mode(FOCUS_NONE);
  390. whole_words->connect("toggled",this,"_search_options_changed");
  391. error_label = memnew(Label);
  392. search_options->add_child(error_label);
  393. error_label->add_color_override("font_color", Color(1,1,0,1));
  394. error_label->add_color_override("font_color_shadow", Color(0,0,0,1));
  395. error_label->add_constant_override("shadow_as_outline", 1);
  396. search_options->add_spacer();
  397. hide_button = memnew(TextureButton);
  398. search_options->add_child(hide_button);
  399. hide_button->set_focus_mode(FOCUS_NONE);
  400. hide_button->connect("pressed",this,"_hide_pressed");
  401. replace_options_hbc = memnew(HBoxContainer);
  402. options_vbc->add_child(replace_options_hbc);
  403. replace_options_hbc->hide();
  404. selection_only = memnew(CheckBox);
  405. replace_options_hbc->add_child(selection_only);
  406. selection_only->set_text(TTR("Selection Only"));
  407. selection_only->set_focus_mode(FOCUS_NONE);
  408. selection_only->connect("toggled",this,"_search_options_changed");
  409. }
  410. void FindReplaceDialog::popup_search() {
  411. set_title(TTR("Search"));
  412. replace_mc->hide();
  413. replace_label->hide();
  414. replace_vb->hide();
  415. skip->hide();
  416. popup_centered(Point2(300,190));
  417. get_ok()->set_text(TTR("Find"));
  418. search_text->grab_focus();
  419. if (text_edit->is_selection_active() && ( text_edit->get_selection_from_line() == text_edit->get_selection_to_line())) {
  420. search_text->set_text( text_edit->get_selection_text() );
  421. }
  422. search_text->select_all();
  423. error_label->set_text("");
  424. }
  425. void FindReplaceDialog::popup_replace() {
  426. set_title(TTR("Replace"));
  427. bool do_selection=(text_edit->is_selection_active() && text_edit->get_selection_from_line() < text_edit->get_selection_to_line());
  428. set_replace_selection_only(do_selection);
  429. if (!do_selection && text_edit->is_selection_active()) {
  430. search_text->set_text(text_edit->get_selection_text());
  431. }
  432. replace_mc->show();
  433. replace_label->show();
  434. replace_vb->show();
  435. popup_centered(Point2(300,300));
  436. if (search_text->get_text()!="" && replace_text->get_text()=="") {
  437. search_text->select(0,0);
  438. replace_text->grab_focus();
  439. } else {
  440. search_text->grab_focus();
  441. search_text->select_all();
  442. }
  443. error_label->set_text("");
  444. if (prompt->is_pressed()) {
  445. skip->show();
  446. get_ok()->set_text(TTR("Next"));
  447. selection_only->set_disabled(true);
  448. } else {
  449. skip->hide();
  450. get_ok()->set_text(TTR("Replace"));
  451. selection_only->set_disabled(false);
  452. }
  453. }
  454. void FindReplaceDialog::_search_callback() {
  455. if (is_replace_mode())
  456. _replace();
  457. else
  458. _search();
  459. }
  460. void FindReplaceDialog::_replace_skip_callback() {
  461. _search();
  462. }
  463. void FindReplaceDialog::_replace() {
  464. text_edit->begin_complex_operation();
  465. if (is_replace_all_mode()) {
  466. //line as x so it gets priority in comparison, column as y
  467. Point2i orig_cursor(text_edit->cursor_get_line(),text_edit->cursor_get_column());
  468. Point2i prev_match=Point2(-1,-1);
  469. bool selection_enabled = text_edit->is_selection_active();
  470. Point2i selection_begin,selection_end;
  471. if (selection_enabled) {
  472. selection_begin=Point2i(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  473. selection_end=Point2i(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  474. }
  475. int vsval = text_edit->get_v_scroll();
  476. //int hsval = text_edit->get_h_scroll();
  477. text_edit->cursor_set_line(0);
  478. text_edit->cursor_set_column(0);
  479. int rc=0;
  480. while(_search()) {
  481. if (!text_edit->is_selection_active()) {
  482. //search selects
  483. break;
  484. }
  485. //replace area
  486. Point2i match_from(text_edit->get_selection_from_line(),text_edit->get_selection_from_column());
  487. Point2i match_to(text_edit->get_selection_to_line(),text_edit->get_selection_to_column());
  488. if (match_from < prev_match)
  489. break; //done
  490. prev_match=match_to;
  491. if (selection_enabled && is_replace_selection_only()) {
  492. if (match_from<selection_begin || match_to>selection_end)
  493. continue;
  494. //replace but adjust selection bounds
  495. text_edit->insert_text_at_cursor(get_replace_text());
  496. if (match_to.x==selection_end.x)
  497. selection_end.y+=get_replace_text().length() - get_search_text().length();
  498. } else {
  499. //just replace
  500. text_edit->insert_text_at_cursor(get_replace_text());
  501. }
  502. rc++;
  503. }
  504. //restore editor state (selection, cursor, scroll)
  505. text_edit->cursor_set_line(orig_cursor.x);
  506. text_edit->cursor_set_column(orig_cursor.y);
  507. if (selection_enabled && is_replace_selection_only()) {
  508. //reselect
  509. text_edit->select(selection_begin.x,selection_begin.y,selection_end.x,selection_end.y);
  510. } else {
  511. text_edit->deselect();
  512. }
  513. text_edit->set_v_scroll(vsval);
  514. // text_edit->set_h_scroll(hsval);
  515. error_label->set_text(vformat(TTR("Replaced %d ocurrence(s)."),rc));
  516. //hide();
  517. } else {
  518. if (text_edit->get_selection_text()==get_search_text()) {
  519. text_edit->insert_text_at_cursor(get_replace_text());
  520. }
  521. _search();
  522. }
  523. text_edit->end_complex_operation();
  524. }
  525. bool FindReplaceDialog::_search() {
  526. String text=get_search_text();
  527. uint32_t flags=0;
  528. if (is_whole_words())
  529. flags|=TextEdit::SEARCH_WHOLE_WORDS;
  530. if (is_case_sensitive())
  531. flags|=TextEdit::SEARCH_MATCH_CASE;
  532. if (is_backwards())
  533. flags|=TextEdit::SEARCH_BACKWARDS;
  534. int line=text_edit->cursor_get_line(),col=text_edit->cursor_get_column();
  535. if (is_backwards()) {
  536. col-=1;
  537. if (col<0) {
  538. line-=1;
  539. if (line<0) {
  540. line=text_edit->get_line_count()-1;
  541. }
  542. col=text_edit->get_line(line).length();
  543. }
  544. }
  545. bool found = text_edit->search(text,flags,line,col,line,col);
  546. if (found) {
  547. // print_line("found");
  548. text_edit->cursor_set_line(line);
  549. if (is_backwards())
  550. text_edit->cursor_set_column(col);
  551. else
  552. text_edit->cursor_set_column(col+text.length());
  553. text_edit->select(line,col,line,col+text.length());
  554. set_error("");
  555. return true;
  556. } else {
  557. set_error(TTR("Not found!"));
  558. return false;
  559. }
  560. }
  561. void FindReplaceDialog::_prompt_changed() {
  562. if (prompt->is_pressed()) {
  563. skip->show();
  564. get_ok()->set_text(TTR("Next"));
  565. selection_only->set_disabled(true);
  566. } else {
  567. skip->hide();
  568. get_ok()->set_text(TTR("Replace"));
  569. selection_only->set_disabled(false);
  570. }
  571. }
  572. void FindReplaceDialog::_skip_pressed() {
  573. _replace_skip_callback();
  574. }
  575. bool FindReplaceDialog::is_replace_mode() const {
  576. return replace_text->is_visible();
  577. }
  578. bool FindReplaceDialog::is_replace_all_mode() const {
  579. return !prompt->is_pressed();
  580. }
  581. bool FindReplaceDialog::is_replace_selection_only() const {
  582. return selection_only->is_pressed();
  583. }
  584. void FindReplaceDialog::set_replace_selection_only(bool p_enable){
  585. selection_only->set_pressed(p_enable);
  586. }
  587. void FindReplaceDialog::ok_pressed() {
  588. _search_callback();
  589. }
  590. void FindReplaceDialog::_search_text_entered(const String& p_text) {
  591. if (replace_text->is_visible())
  592. return;
  593. emit_signal("search");
  594. _search();
  595. }
  596. void FindReplaceDialog::_replace_text_entered(const String& p_text) {
  597. if (!replace_text->is_visible())
  598. return;
  599. emit_signal("search");
  600. _replace();
  601. }
  602. String FindReplaceDialog::get_search_text() const {
  603. return search_text->get_text();
  604. }
  605. String FindReplaceDialog::get_replace_text() const {
  606. return replace_text->get_text();
  607. }
  608. bool FindReplaceDialog::is_whole_words() const {
  609. return whole_words->is_pressed();
  610. }
  611. bool FindReplaceDialog::is_case_sensitive() const {
  612. return case_sensitive->is_pressed();
  613. }
  614. bool FindReplaceDialog::is_backwards() const {
  615. return backwards->is_pressed();
  616. }
  617. void FindReplaceDialog::set_error(const String& p_error) {
  618. error_label->set_text(p_error);
  619. }
  620. void FindReplaceDialog::set_text_edit(TextEdit *p_text_edit) {
  621. text_edit=p_text_edit;
  622. }
  623. void FindReplaceDialog::search_next() {
  624. _search();
  625. }
  626. void FindReplaceDialog::_bind_methods() {
  627. ObjectTypeDB::bind_method("_search_text_entered",&FindReplaceDialog::_search_text_entered);
  628. ObjectTypeDB::bind_method("_replace_text_entered",&FindReplaceDialog::_replace_text_entered);
  629. ObjectTypeDB::bind_method("_prompt_changed",&FindReplaceDialog::_prompt_changed);
  630. ObjectTypeDB::bind_method("_skip_pressed",&FindReplaceDialog::_skip_pressed);
  631. ADD_SIGNAL(MethodInfo("search"));
  632. ADD_SIGNAL(MethodInfo("skip"));
  633. }
  634. FindReplaceDialog::FindReplaceDialog() {
  635. set_self_opacity(0.8);
  636. VBoxContainer *vb = memnew( VBoxContainer );
  637. add_child(vb);
  638. set_child_rect(vb);
  639. search_text = memnew( LineEdit );
  640. vb->add_margin_child(TTR("Search"),search_text);
  641. search_text->connect("text_entered", this,"_search_text_entered");
  642. //search_text->set_self_opacity(0.7);
  643. replace_label = memnew( Label);
  644. replace_label->set_text(TTR("Replace By"));
  645. vb->add_child(replace_label);
  646. replace_mc= memnew( MarginContainer);
  647. vb->add_child(replace_mc);
  648. replace_text = memnew( LineEdit );
  649. replace_text->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  650. replace_text->set_begin( Point2(15,132) );
  651. replace_text->set_end( Point2(15,135) );
  652. //replace_text->set_self_opacity(0.7);
  653. replace_mc->add_child(replace_text);
  654. replace_text->connect("text_entered", this,"_replace_text_entered");
  655. MarginContainer *opt_mg = memnew( MarginContainer );
  656. vb->add_child(opt_mg);
  657. VBoxContainer *svb = memnew( VBoxContainer);
  658. opt_mg->add_child(svb);
  659. svb ->add_child(memnew(Label));
  660. whole_words = memnew( CheckButton );
  661. whole_words->set_text(TTR("Whole Words"));
  662. svb->add_child(whole_words);
  663. case_sensitive = memnew( CheckButton );
  664. case_sensitive->set_text(TTR("Case Sensitive"));
  665. svb->add_child(case_sensitive);
  666. backwards = memnew( CheckButton );
  667. backwards->set_text(TTR("Backwards"));
  668. svb->add_child(backwards);
  669. opt_mg = memnew( MarginContainer );
  670. vb->add_child(opt_mg);
  671. VBoxContainer *rvb = memnew( VBoxContainer);
  672. opt_mg->add_child(rvb);
  673. replace_vb=rvb;
  674. // rvb ->add_child(memnew(HSeparator));
  675. rvb ->add_child(memnew(Label));
  676. prompt = memnew( CheckButton );
  677. prompt->set_text(TTR("Prompt On Replace"));
  678. rvb->add_child(prompt);
  679. prompt->connect("pressed", this,"_prompt_changed");
  680. selection_only = memnew( CheckButton );
  681. selection_only->set_text(TTR("Selection Only"));
  682. rvb->add_child(selection_only);
  683. int margin = get_constant("margin","Dialogs");
  684. int button_margin = get_constant("button_margin","Dialogs");
  685. skip = memnew( Button );
  686. skip->set_anchor( MARGIN_LEFT, ANCHOR_END );
  687. skip->set_anchor( MARGIN_TOP, ANCHOR_END );
  688. skip->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  689. skip->set_anchor( MARGIN_BOTTOM, ANCHOR_END );
  690. skip->set_begin( Point2( 70, button_margin ) );
  691. skip->set_end( Point2( 10, margin ) );
  692. skip->set_text(TTR("Skip"));
  693. add_child(skip);
  694. skip->connect("pressed", this,"_skip_pressed");
  695. error_label = memnew( Label );
  696. error_label->set_align(Label::ALIGN_CENTER);
  697. error_label->add_color_override("font_color",Color(1,0.4,0.3));
  698. error_label->add_color_override("font_color_shadow",Color(0,0,0,0.2));
  699. error_label->add_constant_override("shadow_as_outline",1);
  700. vb->add_child(error_label);
  701. set_hide_on_ok(false);
  702. }
  703. /*** CODE EDITOR ****/
  704. void CodeTextEditor::_text_editor_input_event(const InputEvent& p_event) {
  705. if (p_event.type==InputEvent::MOUSE_BUTTON) {
  706. const InputEventMouseButton& mb=p_event.mouse_button;
  707. if (mb.pressed && mb.mod.command) {
  708. if (mb.button_index==BUTTON_WHEEL_UP) {
  709. _zoom_in();
  710. } else if (mb.button_index==BUTTON_WHEEL_DOWN) {
  711. _zoom_out();
  712. }
  713. }
  714. } else if (p_event.type==InputEvent::KEY) {
  715. if (p_event.key.pressed) {
  716. if (ED_IS_SHORTCUT("script_editor/zoom_in", p_event)) {
  717. _zoom_in();
  718. }
  719. if (ED_IS_SHORTCUT("script_editor/zoom_out", p_event)) {
  720. _zoom_out();
  721. }
  722. if (ED_IS_SHORTCUT("script_editor/reset_zoom", p_event)) {
  723. _reset_zoom();
  724. }
  725. }
  726. }
  727. }
  728. void CodeTextEditor::_zoom_in() {
  729. font_resize_val+=1;
  730. if (font_resize_timer->get_time_left()==0)
  731. font_resize_timer->start();
  732. }
  733. void CodeTextEditor::_zoom_out() {
  734. font_resize_val-=1;
  735. if (font_resize_timer->get_time_left()==0)
  736. font_resize_timer->start();
  737. }
  738. void CodeTextEditor::_reset_zoom() {
  739. Ref<DynamicFont> font = text_editor->get_font("font"); // reset source font size to default
  740. if (font.is_valid()) {
  741. EditorSettings::get_singleton()->set("global/source_font_size",14);
  742. font->set_size(14);
  743. }
  744. }
  745. void CodeTextEditor::_line_col_changed() {
  746. line_nb->set_text(itos(text_editor->cursor_get_line() + 1));
  747. col_nb->set_text(itos(text_editor->cursor_get_column() + 1));
  748. }
  749. void CodeTextEditor::_text_changed() {
  750. code_complete_timer->start();
  751. idle->start();
  752. }
  753. void CodeTextEditor::_code_complete_timer_timeout() {
  754. if (!is_visible())
  755. return;
  756. if (enable_complete_timer)
  757. text_editor->query_code_comple();
  758. }
  759. void CodeTextEditor::_complete_request() {
  760. List<String> entries;
  761. _code_complete_script(text_editor->get_text_for_completion(),&entries);
  762. // print_line("COMPLETE: "+p_request);
  763. if (entries.size()==0)
  764. return;
  765. Vector<String> strs;
  766. strs.resize(entries.size());
  767. int i=0;
  768. for(List<String>::Element *E=entries.front();E;E=E->next()) {
  769. strs[i++]=E->get();
  770. }
  771. text_editor->code_complete(strs);
  772. }
  773. void CodeTextEditor::_font_resize_timeout() {
  774. Ref<DynamicFont> font = text_editor->get_font("font");
  775. if (font.is_valid()) {
  776. int size=font->get_size()+font_resize_val;
  777. if (size>=8 && size<=96) {
  778. EditorSettings::get_singleton()->set("global/source_font_size",size);
  779. font->set_size(size);
  780. }
  781. font_resize_val=0;
  782. }
  783. }
  784. void CodeTextEditor::update_editor_settings() {
  785. text_editor->set_auto_brace_completion(EditorSettings::get_singleton()->get("text_editor/auto_brace_complete"));
  786. text_editor->set_scroll_pass_end_of_file(EditorSettings::get_singleton()->get("text_editor/scroll_past_end_of_file"));
  787. text_editor->set_tab_size(EditorSettings::get_singleton()->get("text_editor/tab_size"));
  788. text_editor->set_draw_tabs(EditorSettings::get_singleton()->get("text_editor/draw_tabs"));
  789. text_editor->set_show_line_numbers(EditorSettings::get_singleton()->get("text_editor/show_line_numbers"));
  790. text_editor->set_line_numbers_zero_padded(EditorSettings::get_singleton()->get("text_editor/line_numbers_zero_padded"));
  791. text_editor->set_show_line_length_guideline(EditorSettings::get_singleton()->get("text_editor/show_line_length_guideline"));
  792. text_editor->set_line_length_guideline_column(EditorSettings::get_singleton()->get("text_editor/line_length_guideline_column"));
  793. text_editor->set_syntax_coloring(EditorSettings::get_singleton()->get("text_editor/syntax_highlighting"));
  794. text_editor->set_highlight_all_occurrences(EditorSettings::get_singleton()->get("text_editor/highlight_all_occurrences"));
  795. text_editor->cursor_set_blink_enabled(EditorSettings::get_singleton()->get("text_editor/caret_blink"));
  796. text_editor->cursor_set_blink_speed(EditorSettings::get_singleton()->get("text_editor/caret_blink_speed"));
  797. text_editor->set_draw_breakpoint_gutter(EditorSettings::get_singleton()->get("text_editor/show_breakpoint_gutter"));
  798. text_editor->cursor_set_block_mode(EditorSettings::get_singleton()->get("text_editor/block_caret"));
  799. }
  800. void CodeTextEditor::set_error(const String& p_error) {
  801. if (p_error!="") {
  802. error->set_text(p_error);
  803. error->show();
  804. } else {
  805. error->hide();
  806. }
  807. }
  808. void CodeTextEditor::_update_font() {
  809. // FONTS
  810. String editor_font = EDITOR_DEF("text_editor/font", "");
  811. bool font_overridden = false;
  812. if (editor_font!="") {
  813. Ref<Font> fnt = ResourceLoader::load(editor_font);
  814. if (fnt.is_valid()) {
  815. text_editor->add_font_override("font",fnt);
  816. font_overridden = true;
  817. }
  818. }
  819. if(!font_overridden)
  820. text_editor->add_font_override("font",get_font("source","EditorFonts"));
  821. }
  822. void CodeTextEditor::_on_settings_change() {
  823. _update_font();
  824. // AUTO BRACE COMPLETION
  825. text_editor->set_auto_brace_completion(
  826. EDITOR_DEF("text_editor/auto_brace_complete", true)
  827. );
  828. code_complete_timer->set_wait_time(
  829. EDITOR_DEF("text_editor/code_complete_delay",.3f)
  830. );
  831. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  832. // call hint settings
  833. text_editor->set_callhint_settings(
  834. EDITOR_DEF("text_editor/put_callhint_tooltip_below_current_line", true),
  835. EDITOR_DEF("text_editor/callhint_tooltip_offset", Vector2())
  836. );
  837. }
  838. void CodeTextEditor::_text_changed_idle_timeout() {
  839. _validate_script();
  840. }
  841. void CodeTextEditor::_notification(int p_what) {
  842. if (p_what==EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED)
  843. _load_theme_settings();
  844. if (p_what==NOTIFICATION_ENTER_TREE) {
  845. _update_font();
  846. }
  847. }
  848. void CodeTextEditor::_bind_methods() {
  849. ObjectTypeDB::bind_method("_text_editor_input_event",&CodeTextEditor::_text_editor_input_event);
  850. ObjectTypeDB::bind_method("_line_col_changed",&CodeTextEditor::_line_col_changed);
  851. ObjectTypeDB::bind_method("_text_changed",&CodeTextEditor::_text_changed);
  852. ObjectTypeDB::bind_method("_on_settings_change",&CodeTextEditor::_on_settings_change);
  853. ObjectTypeDB::bind_method("_text_changed_idle_timeout",&CodeTextEditor::_text_changed_idle_timeout);
  854. ObjectTypeDB::bind_method("_code_complete_timer_timeout",&CodeTextEditor::_code_complete_timer_timeout);
  855. ObjectTypeDB::bind_method("_complete_request",&CodeTextEditor::_complete_request);
  856. ObjectTypeDB::bind_method("_font_resize_timeout",&CodeTextEditor::_font_resize_timeout);
  857. }
  858. CodeTextEditor::CodeTextEditor() {
  859. ED_SHORTCUT("script_editor/zoom_in", TTR("Zoom In"), KEY_MASK_CMD|KEY_EQUAL);
  860. ED_SHORTCUT("script_editor/zoom_out", TTR("Zoom Out"), KEY_MASK_CMD|KEY_MINUS);
  861. ED_SHORTCUT("script_editor/reset_zoom", TTR("Reset Zoom"), KEY_MASK_CMD|KEY_0);
  862. find_replace_bar = memnew( FindReplaceBar );
  863. add_child(find_replace_bar);
  864. find_replace_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  865. find_replace_bar->hide();
  866. text_editor = memnew( TextEdit );
  867. add_child(text_editor);
  868. text_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  869. find_replace_bar->set_text_edit(text_editor);
  870. text_editor->set_show_line_numbers(true);
  871. text_editor->set_brace_matching(true);
  872. text_editor->set_auto_indent(true);
  873. MarginContainer *status_mc = memnew( MarginContainer );
  874. add_child(status_mc);
  875. status_mc->set("custom_constants/margin_left", 2);
  876. status_mc->set("custom_constants/margin_top", 5);
  877. status_mc->set("custom_constants/margin_right", 2);
  878. status_mc->set("custom_constants/margin_bottom", 1);
  879. HBoxContainer *status_bar = memnew( HBoxContainer );
  880. status_mc->add_child(status_bar);
  881. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  882. status_bar->add_child( memnew( Label ) ); //to keep the height if the other labels are not visible
  883. idle = memnew( Timer );
  884. add_child(idle);
  885. idle->set_one_shot(true);
  886. idle->set_wait_time(EDITOR_DEF("text_editor/idle_parse_delay",2));
  887. code_complete_timer = memnew(Timer);
  888. add_child(code_complete_timer);
  889. code_complete_timer->set_one_shot(true);
  890. enable_complete_timer = EDITOR_DEF("text_editor/enable_code_completion_delay",true);
  891. code_complete_timer->set_wait_time(EDITOR_DEF("text_editor/code_complete_delay",.3f));
  892. error = memnew( Label );
  893. status_bar->add_child(error);
  894. error->hide();
  895. error->set_valign(Label::VALIGN_CENTER);
  896. error->add_color_override("font_color",Color(1,0.7,0.6,0.9));
  897. status_bar->add_spacer();
  898. Label *line_txt = memnew( Label );
  899. status_bar->add_child(line_txt);
  900. line_txt->set_align(Label::ALIGN_RIGHT);
  901. line_txt->set_valign(Label::VALIGN_CENTER);
  902. line_txt->set_v_size_flags(SIZE_FILL);
  903. line_txt->set_text(TTR("Line:"));
  904. line_nb = memnew( Label );
  905. status_bar->add_child(line_nb);
  906. line_nb->set_valign(Label::VALIGN_CENTER);
  907. line_nb->set_v_size_flags(SIZE_FILL);
  908. line_nb->set_autowrap(true); // workaround to prevent resizing the label on each change
  909. line_nb->set_custom_minimum_size(Size2(40,1)*EDSCALE);
  910. Label *col_txt = memnew( Label );
  911. status_bar->add_child(col_txt);
  912. col_txt->set_align(Label::ALIGN_RIGHT);
  913. col_txt->set_valign(Label::VALIGN_CENTER);
  914. col_txt->set_v_size_flags(SIZE_FILL);
  915. col_txt->set_text(TTR("Col:"));
  916. col_nb = memnew( Label );
  917. status_bar->add_child(col_nb);
  918. col_nb->set_valign(Label::VALIGN_CENTER);
  919. col_nb->set_v_size_flags(SIZE_FILL);
  920. col_nb->set_autowrap(true); // workaround to prevent resizing the label on each change
  921. col_nb->set_custom_minimum_size(Size2(40,1)*EDSCALE);
  922. text_editor->connect("input_event", this,"_text_editor_input_event");
  923. text_editor->connect("cursor_changed", this,"_line_col_changed");
  924. text_editor->connect("text_changed", this,"_text_changed");
  925. text_editor->connect("request_completion", this,"_complete_request");
  926. Vector<String> cs;
  927. cs.push_back(".");
  928. cs.push_back(",");
  929. cs.push_back("(");
  930. text_editor->set_completion(true,cs);
  931. idle->connect("timeout", this,"_text_changed_idle_timeout");
  932. code_complete_timer->connect("timeout", this,"_code_complete_timer_timeout");
  933. font_resize_val=0;
  934. font_resize_timer = memnew(Timer);
  935. add_child(font_resize_timer);
  936. font_resize_timer->set_one_shot(true);
  937. font_resize_timer->set_wait_time(0.07);
  938. font_resize_timer->connect("timeout", this, "_font_resize_timeout");
  939. EditorSettings::get_singleton()->connect("settings_changed",this,"_on_settings_change");
  940. }