2
0

file_dialog.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*************************************************************************/
  2. /* file_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "file_dialog.h"
  31. #include "core/os/keyboard.h"
  32. #include "core/string/print_string.h"
  33. #include "scene/gui/label.h"
  34. FileDialog::GetIconFunc FileDialog::get_icon_func = nullptr;
  35. FileDialog::GetIconFunc FileDialog::get_large_icon_func = nullptr;
  36. FileDialog::RegisterFunc FileDialog::register_func = nullptr;
  37. FileDialog::RegisterFunc FileDialog::unregister_func = nullptr;
  38. void FileDialog::popup_file_dialog() {
  39. popup_centered_clamped(Size2i(700, 500), 0.8f);
  40. }
  41. VBoxContainer *FileDialog::get_vbox() {
  42. return vbox;
  43. }
  44. void FileDialog::_theme_changed() {
  45. Color font_color = vbox->get_theme_color(SNAME("font_color"), SNAME("Button"));
  46. Color font_hover_color = vbox->get_theme_color(SNAME("font_hover_color"), SNAME("Button"));
  47. Color font_focus_color = vbox->get_theme_color(SNAME("font_focus_color"), SNAME("Button"));
  48. Color font_pressed_color = vbox->get_theme_color(SNAME("font_pressed_color"), SNAME("Button"));
  49. dir_up->add_theme_color_override("icon_normal_color", font_color);
  50. dir_up->add_theme_color_override("icon_hover_color", font_hover_color);
  51. dir_up->add_theme_color_override("icon_focus_color", font_focus_color);
  52. dir_up->add_theme_color_override("icon_pressed_color", font_pressed_color);
  53. dir_prev->add_theme_color_override("icon_color_normal", font_color);
  54. dir_prev->add_theme_color_override("icon_color_hover", font_hover_color);
  55. dir_prev->add_theme_color_override("icon_focus_color", font_focus_color);
  56. dir_prev->add_theme_color_override("icon_color_pressed", font_pressed_color);
  57. dir_next->add_theme_color_override("icon_color_normal", font_color);
  58. dir_next->add_theme_color_override("icon_color_hover", font_hover_color);
  59. dir_next->add_theme_color_override("icon_focus_color", font_focus_color);
  60. dir_next->add_theme_color_override("icon_color_pressed", font_pressed_color);
  61. refresh->add_theme_color_override("icon_normal_color", font_color);
  62. refresh->add_theme_color_override("icon_hover_color", font_hover_color);
  63. refresh->add_theme_color_override("icon_focus_color", font_focus_color);
  64. refresh->add_theme_color_override("icon_pressed_color", font_pressed_color);
  65. show_hidden->add_theme_color_override("icon_normal_color", font_color);
  66. show_hidden->add_theme_color_override("icon_hover_color", font_hover_color);
  67. show_hidden->add_theme_color_override("icon_focus_color", font_focus_color);
  68. show_hidden->add_theme_color_override("icon_pressed_color", font_pressed_color);
  69. }
  70. void FileDialog::_notification(int p_what) {
  71. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  72. if (!is_visible()) {
  73. set_process_unhandled_input(false);
  74. }
  75. }
  76. if (p_what == NOTIFICATION_ENTER_TREE) {
  77. dir_up->set_icon(vbox->get_theme_icon(SNAME("parent_folder"), SNAME("FileDialog")));
  78. if (vbox->is_layout_rtl()) {
  79. dir_prev->set_icon(vbox->get_theme_icon(SNAME("forward_folder"), SNAME("FileDialog")));
  80. dir_next->set_icon(vbox->get_theme_icon(SNAME("back_folder"), SNAME("FileDialog")));
  81. } else {
  82. dir_prev->set_icon(vbox->get_theme_icon(SNAME("back_folder"), SNAME("FileDialog")));
  83. dir_next->set_icon(vbox->get_theme_icon(SNAME("forward_folder"), SNAME("FileDialog")));
  84. }
  85. refresh->set_icon(vbox->get_theme_icon(SNAME("reload"), SNAME("FileDialog")));
  86. show_hidden->set_icon(vbox->get_theme_icon(SNAME("toggle_hidden"), SNAME("FileDialog")));
  87. _theme_changed();
  88. }
  89. }
  90. void FileDialog::unhandled_input(const Ref<InputEvent> &p_event) {
  91. ERR_FAIL_COND(p_event.is_null());
  92. Ref<InputEventKey> k = p_event;
  93. if (k.is_valid() && has_focus()) {
  94. if (k->is_pressed()) {
  95. bool handled = true;
  96. switch (k->get_keycode()) {
  97. case KEY_H: {
  98. if (k->is_command_pressed()) {
  99. set_show_hidden_files(!show_hidden_files);
  100. } else {
  101. handled = false;
  102. }
  103. } break;
  104. case KEY_F5: {
  105. invalidate();
  106. } break;
  107. case KEY_BACKSPACE: {
  108. _dir_submitted("..");
  109. } break;
  110. default: {
  111. handled = false;
  112. }
  113. }
  114. if (handled) {
  115. set_input_as_handled();
  116. }
  117. }
  118. }
  119. }
  120. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  121. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  122. };
  123. Vector<String> FileDialog::get_selected_files() const {
  124. Vector<String> list;
  125. TreeItem *item = tree->get_root();
  126. while ((item = tree->get_next_selected(item))) {
  127. list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0)));
  128. };
  129. return list;
  130. };
  131. void FileDialog::update_dir() {
  132. dir->set_text(dir_access->get_current_dir(false));
  133. if (drives->is_visible()) {
  134. drives->select(dir_access->get_current_drive());
  135. }
  136. // Deselect any item, to make "Select Current Folder" button text by default.
  137. deselect_all();
  138. }
  139. void FileDialog::_dir_submitted(String p_dir) {
  140. dir_access->change_dir(p_dir);
  141. file->set_text("");
  142. invalidate();
  143. update_dir();
  144. _push_history();
  145. }
  146. void FileDialog::_file_submitted(const String &p_file) {
  147. _action_pressed();
  148. }
  149. void FileDialog::_save_confirm_pressed() {
  150. String f = dir_access->get_current_dir().plus_file(file->get_text());
  151. emit_signal(SNAME("file_selected"), f);
  152. hide();
  153. }
  154. void FileDialog::_post_popup() {
  155. ConfirmationDialog::_post_popup();
  156. if (invalidated) {
  157. update_file_list();
  158. invalidated = false;
  159. }
  160. if (mode == FILE_MODE_SAVE_FILE) {
  161. file->grab_focus();
  162. } else {
  163. tree->grab_focus();
  164. }
  165. set_process_unhandled_input(true);
  166. // For open dir mode, deselect all items on file dialog open.
  167. if (mode == FILE_MODE_OPEN_DIR) {
  168. deselect_all();
  169. file_box->set_visible(false);
  170. } else {
  171. file_box->set_visible(true);
  172. }
  173. local_history.clear();
  174. local_history_pos = -1;
  175. _push_history();
  176. }
  177. void FileDialog::_push_history() {
  178. local_history.resize(local_history_pos + 1);
  179. String new_path = dir_access->get_current_dir();
  180. if (local_history.size() == 0 || new_path != local_history[local_history_pos]) {
  181. local_history.push_back(new_path);
  182. local_history_pos++;
  183. dir_prev->set_disabled(local_history_pos == 0);
  184. dir_next->set_disabled(true);
  185. }
  186. }
  187. void FileDialog::_action_pressed() {
  188. if (mode == FILE_MODE_OPEN_FILES) {
  189. TreeItem *ti = tree->get_next_selected(nullptr);
  190. String fbase = dir_access->get_current_dir();
  191. Vector<String> files;
  192. while (ti) {
  193. files.push_back(fbase.plus_file(ti->get_text(0)));
  194. ti = tree->get_next_selected(ti);
  195. }
  196. if (files.size()) {
  197. emit_signal(SNAME("files_selected"), files);
  198. hide();
  199. }
  200. return;
  201. }
  202. String f = dir_access->get_current_dir().plus_file(file->get_text());
  203. if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  204. emit_signal(SNAME("file_selected"), f);
  205. hide();
  206. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  207. String path = dir_access->get_current_dir();
  208. path = path.replace("\\", "/");
  209. TreeItem *item = tree->get_selected();
  210. if (item) {
  211. Dictionary d = item->get_metadata(0);
  212. if (d["dir"] && d["name"] != "..") {
  213. path = path.plus_file(d["name"]);
  214. }
  215. }
  216. emit_signal(SNAME("dir_selected"), path);
  217. hide();
  218. }
  219. if (mode == FILE_MODE_SAVE_FILE) {
  220. bool valid = false;
  221. if (filter->get_selected() == filter->get_item_count() - 1) {
  222. valid = true; // match none
  223. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  224. // match all filters
  225. for (int i = 0; i < filters.size(); i++) {
  226. String flt = filters[i].get_slice(";", 0);
  227. for (int j = 0; j < flt.get_slice_count(","); j++) {
  228. String str = flt.get_slice(",", j).strip_edges();
  229. if (f.match(str)) {
  230. valid = true;
  231. break;
  232. }
  233. }
  234. if (valid) {
  235. break;
  236. }
  237. }
  238. } else {
  239. int idx = filter->get_selected();
  240. if (filters.size() > 1) {
  241. idx--;
  242. }
  243. if (idx >= 0 && idx < filters.size()) {
  244. String flt = filters[idx].get_slice(";", 0);
  245. int filterSliceCount = flt.get_slice_count(",");
  246. for (int j = 0; j < filterSliceCount; j++) {
  247. String str = (flt.get_slice(",", j).strip_edges());
  248. if (f.match(str)) {
  249. valid = true;
  250. break;
  251. }
  252. }
  253. if (!valid && filterSliceCount > 0) {
  254. String str = (flt.get_slice(",", 0).strip_edges());
  255. f += str.substr(1, str.length() - 1);
  256. file->set_text(f.get_file());
  257. valid = true;
  258. }
  259. } else {
  260. valid = true;
  261. }
  262. }
  263. if (!valid) {
  264. exterr->popup_centered(Size2(250, 80));
  265. return;
  266. }
  267. if (dir_access->file_exists(f)) {
  268. confirm_save->set_text(TTRC("File exists, overwrite?"));
  269. confirm_save->popup_centered(Size2(200, 80));
  270. } else {
  271. emit_signal(SNAME("file_selected"), f);
  272. hide();
  273. }
  274. }
  275. }
  276. void FileDialog::_cancel_pressed() {
  277. file->set_text("");
  278. invalidate();
  279. hide();
  280. }
  281. bool FileDialog::_is_open_should_be_disabled() {
  282. if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_SAVE_FILE) {
  283. return false;
  284. }
  285. TreeItem *ti = tree->get_next_selected(tree->get_root());
  286. while (ti) {
  287. TreeItem *prev_ti = ti;
  288. ti = tree->get_next_selected(tree->get_root());
  289. if (ti == prev_ti) {
  290. break;
  291. }
  292. }
  293. // We have something that we can't select?
  294. if (!ti) {
  295. return mode != FILE_MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  296. }
  297. Dictionary d = ti->get_metadata(0);
  298. // Opening a file, but selected a folder? Forbidden.
  299. return ((mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  300. (mode == FILE_MODE_OPEN_DIR && !d["dir"]);
  301. }
  302. void FileDialog::_go_up() {
  303. dir_access->change_dir("..");
  304. update_file_list();
  305. update_dir();
  306. _push_history();
  307. }
  308. void FileDialog::_go_back() {
  309. if (local_history_pos <= 0) {
  310. return;
  311. }
  312. local_history_pos--;
  313. dir_access->change_dir(local_history[local_history_pos]);
  314. update_file_list();
  315. update_dir();
  316. dir_prev->set_disabled(local_history_pos == 0);
  317. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  318. }
  319. void FileDialog::_go_forward() {
  320. if (local_history_pos == local_history.size() - 1) {
  321. return;
  322. }
  323. local_history_pos++;
  324. dir_access->change_dir(local_history[local_history_pos]);
  325. update_file_list();
  326. update_dir();
  327. dir_prev->set_disabled(local_history_pos == 0);
  328. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  329. }
  330. void FileDialog::deselect_all() {
  331. // Clear currently selected items in file manager.
  332. tree->deselect_all();
  333. // And change get_ok title.
  334. if (!tree->is_anything_selected()) {
  335. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  336. switch (mode) {
  337. case FILE_MODE_OPEN_FILE:
  338. case FILE_MODE_OPEN_FILES:
  339. get_ok_button()->set_text(TTRC("Open"));
  340. break;
  341. case FILE_MODE_OPEN_DIR:
  342. get_ok_button()->set_text(TTRC("Select Current Folder"));
  343. break;
  344. case FILE_MODE_OPEN_ANY:
  345. case FILE_MODE_SAVE_FILE:
  346. // FIXME: Implement, or refactor to avoid duplication with set_mode
  347. break;
  348. }
  349. }
  350. }
  351. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  352. _tree_selected();
  353. }
  354. void FileDialog::_tree_selected() {
  355. TreeItem *ti = tree->get_selected();
  356. if (!ti) {
  357. return;
  358. }
  359. Dictionary d = ti->get_metadata(0);
  360. if (!d["dir"]) {
  361. file->set_text(d["name"]);
  362. } else if (mode == FILE_MODE_OPEN_DIR) {
  363. get_ok_button()->set_text(TTRC("Select This Folder"));
  364. }
  365. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  366. }
  367. void FileDialog::_tree_item_activated() {
  368. TreeItem *ti = tree->get_selected();
  369. if (!ti) {
  370. return;
  371. }
  372. Dictionary d = ti->get_metadata(0);
  373. if (d["dir"]) {
  374. dir_access->change_dir(d["name"]);
  375. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  376. file->set_text("");
  377. }
  378. call_deferred(SNAME("_update_file_list"));
  379. call_deferred(SNAME("_update_dir"));
  380. _push_history();
  381. } else {
  382. _action_pressed();
  383. }
  384. }
  385. void FileDialog::update_file_name() {
  386. int idx = filter->get_selected() - 1;
  387. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  388. if (idx == -1) {
  389. idx += 1;
  390. }
  391. String filter_str = filters[idx];
  392. String file_str = file->get_text();
  393. String base_name = file_str.get_basename();
  394. file_str = base_name + "." + filter_str.strip_edges().to_lower();
  395. file->set_text(file_str);
  396. }
  397. }
  398. void FileDialog::update_file_list() {
  399. tree->clear();
  400. // Scroll back to the top after opening a directory
  401. tree->get_vscroll_bar()->set_value(0);
  402. dir_access->list_dir_begin();
  403. TreeItem *root = tree->create_item();
  404. Ref<Texture2D> folder = vbox->get_theme_icon(SNAME("folder"), SNAME("FileDialog"));
  405. Ref<Texture2D> file_icon = vbox->get_theme_icon(SNAME("file"), SNAME("FileDialog"));
  406. const Color folder_color = vbox->get_theme_color(SNAME("folder_icon_modulate"), SNAME("FileDialog"));
  407. const Color file_color = vbox->get_theme_color(SNAME("file_icon_modulate"), SNAME("FileDialog"));
  408. List<String> files;
  409. List<String> dirs;
  410. bool is_hidden;
  411. String item;
  412. if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) {
  413. message->hide();
  414. } else {
  415. message->set_text(TTRC("You don't have permission to access contents of this folder."));
  416. message->show();
  417. }
  418. while ((item = dir_access->get_next()) != "") {
  419. if (item == "." || item == "..") {
  420. continue;
  421. }
  422. is_hidden = dir_access->current_is_hidden();
  423. if (show_hidden_files || !is_hidden) {
  424. if (!dir_access->current_is_dir()) {
  425. files.push_back(item);
  426. } else {
  427. dirs.push_back(item);
  428. }
  429. }
  430. }
  431. dirs.sort_custom<NaturalNoCaseComparator>();
  432. files.sort_custom<NaturalNoCaseComparator>();
  433. while (!dirs.is_empty()) {
  434. String &dir_name = dirs.front()->get();
  435. TreeItem *ti = tree->create_item(root);
  436. ti->set_text(0, dir_name);
  437. ti->set_icon(0, folder);
  438. ti->set_icon_modulate(0, folder_color);
  439. Dictionary d;
  440. d["name"] = dir_name;
  441. d["dir"] = true;
  442. ti->set_metadata(0, d);
  443. dirs.pop_front();
  444. }
  445. List<String> patterns;
  446. // build filter
  447. if (filter->get_selected() == filter->get_item_count() - 1) {
  448. // match all
  449. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  450. // match all filters
  451. for (int i = 0; i < filters.size(); i++) {
  452. String f = filters[i].get_slice(";", 0);
  453. for (int j = 0; j < f.get_slice_count(","); j++) {
  454. patterns.push_back(f.get_slice(",", j).strip_edges());
  455. }
  456. }
  457. } else {
  458. int idx = filter->get_selected();
  459. if (filters.size() > 1) {
  460. idx--;
  461. }
  462. if (idx >= 0 && idx < filters.size()) {
  463. String f = filters[idx].get_slice(";", 0);
  464. for (int j = 0; j < f.get_slice_count(","); j++) {
  465. patterns.push_back(f.get_slice(",", j).strip_edges());
  466. }
  467. }
  468. }
  469. String base_dir = dir_access->get_current_dir();
  470. while (!files.is_empty()) {
  471. bool match = patterns.is_empty();
  472. String match_str;
  473. for (const String &E : patterns) {
  474. if (files.front()->get().matchn(E)) {
  475. match_str = E;
  476. match = true;
  477. break;
  478. }
  479. }
  480. if (match) {
  481. TreeItem *ti = tree->create_item(root);
  482. ti->set_text(0, files.front()->get());
  483. if (get_icon_func) {
  484. Ref<Texture2D> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  485. ti->set_icon(0, icon);
  486. } else {
  487. ti->set_icon(0, file_icon);
  488. }
  489. ti->set_icon_modulate(0, file_color);
  490. if (mode == FILE_MODE_OPEN_DIR) {
  491. ti->set_custom_color(0, vbox->get_theme_color(SNAME("files_disabled"), SNAME("FileDialog")));
  492. ti->set_selectable(0, false);
  493. }
  494. Dictionary d;
  495. d["name"] = files.front()->get();
  496. d["dir"] = false;
  497. ti->set_metadata(0, d);
  498. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  499. ti->select(0);
  500. }
  501. }
  502. files.pop_front();
  503. }
  504. if (tree->get_root() && tree->get_root()->get_first_child() && tree->get_selected() == nullptr) {
  505. tree->get_root()->get_first_child()->select(0);
  506. }
  507. }
  508. void FileDialog::_filter_selected(int) {
  509. update_file_name();
  510. update_file_list();
  511. }
  512. void FileDialog::update_filters() {
  513. filter->clear();
  514. if (filters.size() > 1) {
  515. String all_filters;
  516. const int max_filters = 5;
  517. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  518. String flt = filters[i].get_slice(";", 0).strip_edges();
  519. if (i > 0) {
  520. all_filters += ", ";
  521. }
  522. all_filters += flt;
  523. }
  524. if (max_filters < filters.size()) {
  525. all_filters += ", ...";
  526. }
  527. filter->add_item(String(TTRC("All Recognized")) + " (" + all_filters + ")");
  528. }
  529. for (int i = 0; i < filters.size(); i++) {
  530. String flt = filters[i].get_slice(";", 0).strip_edges();
  531. String desc = filters[i].get_slice(";", 1).strip_edges();
  532. if (desc.length()) {
  533. filter->add_item(String(tr(desc)) + " (" + flt + ")");
  534. } else {
  535. filter->add_item("(" + flt + ")");
  536. }
  537. }
  538. filter->add_item(TTRC("All Files (*)"));
  539. }
  540. void FileDialog::clear_filters() {
  541. filters.clear();
  542. update_filters();
  543. invalidate();
  544. }
  545. void FileDialog::add_filter(const String &p_filter) {
  546. filters.push_back(p_filter);
  547. update_filters();
  548. invalidate();
  549. }
  550. void FileDialog::set_filters(const Vector<String> &p_filters) {
  551. filters = p_filters;
  552. update_filters();
  553. invalidate();
  554. }
  555. Vector<String> FileDialog::get_filters() const {
  556. return filters;
  557. }
  558. String FileDialog::get_current_dir() const {
  559. return dir->get_text();
  560. }
  561. String FileDialog::get_current_file() const {
  562. return file->get_text();
  563. }
  564. String FileDialog::get_current_path() const {
  565. return dir->get_text().plus_file(file->get_text());
  566. }
  567. void FileDialog::set_current_dir(const String &p_dir) {
  568. dir_access->change_dir(p_dir);
  569. update_dir();
  570. invalidate();
  571. _push_history();
  572. }
  573. void FileDialog::set_current_file(const String &p_file) {
  574. file->set_text(p_file);
  575. update_dir();
  576. invalidate();
  577. int lp = p_file.rfind(".");
  578. if (lp != -1) {
  579. file->select(0, lp);
  580. if (file->is_inside_tree() && !get_tree()->is_node_being_edited(file)) {
  581. file->grab_focus();
  582. }
  583. }
  584. }
  585. void FileDialog::set_current_path(const String &p_path) {
  586. if (!p_path.size()) {
  587. return;
  588. }
  589. int pos = MAX(p_path.rfind("/"), p_path.rfind("\\"));
  590. if (pos == -1) {
  591. set_current_file(p_path);
  592. } else {
  593. String dir = p_path.substr(0, pos);
  594. String file = p_path.substr(pos + 1, p_path.length());
  595. set_current_dir(dir);
  596. set_current_file(file);
  597. }
  598. }
  599. void FileDialog::set_mode_overrides_title(bool p_override) {
  600. mode_overrides_title = p_override;
  601. }
  602. bool FileDialog::is_mode_overriding_title() const {
  603. return mode_overrides_title;
  604. }
  605. void FileDialog::set_file_mode(FileMode p_mode) {
  606. ERR_FAIL_INDEX((int)p_mode, 5);
  607. mode = p_mode;
  608. switch (mode) {
  609. case FILE_MODE_OPEN_FILE:
  610. get_ok_button()->set_text(TTRC("Open"));
  611. if (mode_overrides_title) {
  612. set_title(TTRC("Open a File"));
  613. }
  614. makedir->hide();
  615. break;
  616. case FILE_MODE_OPEN_FILES:
  617. get_ok_button()->set_text(TTRC("Open"));
  618. if (mode_overrides_title) {
  619. set_title(TTRC("Open File(s)"));
  620. }
  621. makedir->hide();
  622. break;
  623. case FILE_MODE_OPEN_DIR:
  624. get_ok_button()->set_text(TTRC("Select Current Folder"));
  625. if (mode_overrides_title) {
  626. set_title(TTRC("Open a Directory"));
  627. }
  628. makedir->show();
  629. break;
  630. case FILE_MODE_OPEN_ANY:
  631. get_ok_button()->set_text(TTRC("Open"));
  632. if (mode_overrides_title) {
  633. set_title(TTRC("Open a File or Directory"));
  634. }
  635. makedir->show();
  636. break;
  637. case FILE_MODE_SAVE_FILE:
  638. get_ok_button()->set_text(TTRC("Save"));
  639. if (mode_overrides_title) {
  640. set_title(TTRC("Save a File"));
  641. }
  642. makedir->show();
  643. break;
  644. }
  645. if (mode == FILE_MODE_OPEN_FILES) {
  646. tree->set_select_mode(Tree::SELECT_MULTI);
  647. } else {
  648. tree->set_select_mode(Tree::SELECT_SINGLE);
  649. }
  650. }
  651. FileDialog::FileMode FileDialog::get_file_mode() const {
  652. return mode;
  653. }
  654. void FileDialog::set_access(Access p_access) {
  655. ERR_FAIL_INDEX(p_access, 3);
  656. if (access == p_access) {
  657. return;
  658. }
  659. memdelete(dir_access);
  660. switch (p_access) {
  661. case ACCESS_FILESYSTEM: {
  662. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  663. } break;
  664. case ACCESS_RESOURCES: {
  665. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  666. } break;
  667. case ACCESS_USERDATA: {
  668. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  669. } break;
  670. }
  671. access = p_access;
  672. _update_drives();
  673. invalidate();
  674. update_filters();
  675. update_dir();
  676. }
  677. void FileDialog::invalidate() {
  678. if (is_visible()) {
  679. update_file_list();
  680. invalidated = false;
  681. } else {
  682. invalidated = true;
  683. }
  684. }
  685. FileDialog::Access FileDialog::get_access() const {
  686. return access;
  687. }
  688. void FileDialog::_make_dir_confirm() {
  689. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  690. if (err == OK) {
  691. dir_access->change_dir(makedirname->get_text().strip_edges());
  692. invalidate();
  693. update_filters();
  694. update_dir();
  695. _push_history();
  696. } else {
  697. mkdirerr->popup_centered(Size2(250, 50));
  698. }
  699. makedirname->set_text(""); // reset label
  700. }
  701. void FileDialog::_make_dir() {
  702. makedialog->popup_centered(Size2(250, 80));
  703. makedirname->grab_focus();
  704. }
  705. void FileDialog::_select_drive(int p_idx) {
  706. String d = drives->get_item_text(p_idx);
  707. dir_access->change_dir(d);
  708. file->set_text("");
  709. invalidate();
  710. update_dir();
  711. _push_history();
  712. }
  713. void FileDialog::_update_drives() {
  714. int dc = dir_access->get_drive_count();
  715. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  716. drives->hide();
  717. } else {
  718. drives->clear();
  719. Node *dp = drives->get_parent();
  720. if (dp) {
  721. dp->remove_child(drives);
  722. }
  723. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  724. dp->add_child(drives);
  725. drives->show();
  726. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  727. drives->add_item(dir_access->get_drive(i));
  728. }
  729. drives->select(dir_access->get_current_drive());
  730. }
  731. }
  732. bool FileDialog::default_show_hidden_files = false;
  733. void FileDialog::_bind_methods() {
  734. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  735. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  736. ClassDB::bind_method(D_METHOD("add_filter", "filter"), &FileDialog::add_filter);
  737. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  738. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  739. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  740. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  741. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  742. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  743. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  744. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  745. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  746. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  747. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  748. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  749. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  750. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  751. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  752. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  753. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  754. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  755. ClassDB::bind_method(D_METHOD("_update_file_name"), &FileDialog::update_file_name);
  756. ClassDB::bind_method(D_METHOD("_update_dir"), &FileDialog::update_dir);
  757. ClassDB::bind_method(D_METHOD("_update_file_list"), &FileDialog::update_file_list);
  758. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  759. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  760. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  761. ADD_PROPERTY(PropertyInfo(Variant::INT, "file_mode", PROPERTY_HINT_ENUM, "Open File,Open Files,Open Folder,Open Any,Save"), "set_file_mode", "get_file_mode");
  762. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access");
  763. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  764. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  765. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir"), "set_current_dir", "get_current_dir");
  766. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file"), "set_current_file", "get_current_file");
  767. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path"), "set_current_path", "get_current_path");
  768. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  769. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  770. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  771. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  772. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  773. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  774. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  775. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  776. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  777. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  778. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  779. }
  780. void FileDialog::set_show_hidden_files(bool p_show) {
  781. show_hidden_files = p_show;
  782. invalidate();
  783. }
  784. bool FileDialog::is_showing_hidden_files() const {
  785. return show_hidden_files;
  786. }
  787. void FileDialog::set_default_show_hidden_files(bool p_show) {
  788. default_show_hidden_files = p_show;
  789. }
  790. FileDialog::FileDialog() {
  791. show_hidden_files = default_show_hidden_files;
  792. vbox = memnew(VBoxContainer);
  793. add_child(vbox, false, INTERNAL_MODE_FRONT);
  794. vbox->connect("theme_changed", callable_mp(this, &FileDialog::_theme_changed));
  795. mode = FILE_MODE_SAVE_FILE;
  796. set_title(TTRC("Save a File"));
  797. HBoxContainer *hbc = memnew(HBoxContainer);
  798. dir_prev = memnew(Button);
  799. dir_prev->set_flat(true);
  800. dir_prev->set_tooltip(TTRC("Go to previous folder."));
  801. dir_next = memnew(Button);
  802. dir_next->set_flat(true);
  803. dir_next->set_tooltip(TTRC("Go to next folder."));
  804. dir_up = memnew(Button);
  805. dir_up->set_flat(true);
  806. dir_up->set_tooltip(TTRC("Go to parent folder."));
  807. hbc->add_child(dir_prev);
  808. hbc->add_child(dir_next);
  809. hbc->add_child(dir_up);
  810. dir_prev->connect("pressed", callable_mp(this, &FileDialog::_go_back));
  811. dir_next->connect("pressed", callable_mp(this, &FileDialog::_go_forward));
  812. dir_up->connect("pressed", callable_mp(this, &FileDialog::_go_up));
  813. hbc->add_child(memnew(Label(TTRC("Path:"))));
  814. drives_container = memnew(HBoxContainer);
  815. hbc->add_child(drives_container);
  816. drives = memnew(OptionButton);
  817. drives->connect("item_selected", callable_mp(this, &FileDialog::_select_drive));
  818. hbc->add_child(drives);
  819. dir = memnew(LineEdit);
  820. dir->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  821. hbc->add_child(dir);
  822. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  823. refresh = memnew(Button);
  824. refresh->set_flat(true);
  825. refresh->set_tooltip(TTRC("Refresh files."));
  826. refresh->connect("pressed", callable_mp(this, &FileDialog::update_file_list));
  827. hbc->add_child(refresh);
  828. show_hidden = memnew(Button);
  829. show_hidden->set_flat(true);
  830. show_hidden->set_toggle_mode(true);
  831. show_hidden->set_pressed(is_showing_hidden_files());
  832. show_hidden->set_tooltip(TTRC("Toggle the visibility of hidden files."));
  833. show_hidden->connect("toggled", callable_mp(this, &FileDialog::set_show_hidden_files));
  834. hbc->add_child(show_hidden);
  835. shortcuts_container = memnew(HBoxContainer);
  836. hbc->add_child(shortcuts_container);
  837. makedir = memnew(Button);
  838. makedir->set_text(TTRC("Create Folder"));
  839. makedir->connect("pressed", callable_mp(this, &FileDialog::_make_dir));
  840. hbc->add_child(makedir);
  841. vbox->add_child(hbc);
  842. tree = memnew(Tree);
  843. tree->set_hide_root(true);
  844. vbox->add_margin_child(TTRC("Directories & Files:"), tree, true);
  845. message = memnew(Label);
  846. message->hide();
  847. message->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  848. message->set_align(Label::ALIGN_CENTER);
  849. message->set_valign(Label::VALIGN_CENTER);
  850. tree->add_child(message);
  851. file_box = memnew(HBoxContainer);
  852. file_box->add_child(memnew(Label(TTRC("File:"))));
  853. file = memnew(LineEdit);
  854. file->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  855. file->set_stretch_ratio(4);
  856. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  857. file_box->add_child(file);
  858. filter = memnew(OptionButton);
  859. filter->set_stretch_ratio(3);
  860. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  861. filter->set_clip_text(true); // too many extensions overflows it
  862. file_box->add_child(filter);
  863. vbox->add_child(file_box);
  864. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  865. _update_drives();
  866. connect("confirmed", callable_mp(this, &FileDialog::_action_pressed));
  867. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), varray(), CONNECT_DEFERRED);
  868. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), varray(), CONNECT_DEFERRED);
  869. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated), varray());
  870. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  871. dir->connect("text_submitted", callable_mp(this, &FileDialog::_dir_submitted));
  872. file->connect("text_submitted", callable_mp(this, &FileDialog::_file_submitted));
  873. filter->connect("item_selected", callable_mp(this, &FileDialog::_filter_selected));
  874. confirm_save = memnew(ConfirmationDialog);
  875. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  876. confirm_save->connect("confirmed", callable_mp(this, &FileDialog::_save_confirm_pressed));
  877. makedialog = memnew(ConfirmationDialog);
  878. makedialog->set_title(TTRC("Create Folder"));
  879. VBoxContainer *makevb = memnew(VBoxContainer);
  880. makedialog->add_child(makevb);
  881. makedirname = memnew(LineEdit);
  882. makedirname->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  883. makevb->add_margin_child(TTRC("Name:"), makedirname);
  884. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  885. makedialog->register_text_enter(makedirname);
  886. makedialog->connect("confirmed", callable_mp(this, &FileDialog::_make_dir_confirm));
  887. mkdirerr = memnew(AcceptDialog);
  888. mkdirerr->set_text(TTRC("Could not create folder."));
  889. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  890. exterr = memnew(AcceptDialog);
  891. exterr->set_text(TTRC("Must use a valid extension."));
  892. add_child(exterr, false, INTERNAL_MODE_FRONT);
  893. update_filters();
  894. update_dir();
  895. set_hide_on_ok(false);
  896. if (register_func) {
  897. register_func(this);
  898. }
  899. }
  900. FileDialog::~FileDialog() {
  901. if (unregister_func) {
  902. unregister_func(this);
  903. }
  904. memdelete(dir_access);
  905. }