code_editor.cpp 32 KB

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