2
0

code_editor.cpp 29 KB

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