file_dialog.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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. if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) {
  404. message->hide();
  405. } else {
  406. message->set_text(TTRC("You don't have permission to access contents of this folder."));
  407. message->show();
  408. }
  409. TreeItem *root = tree->create_item();
  410. Ref<Texture2D> folder = vbox->get_theme_icon(SNAME("folder"), SNAME("FileDialog"));
  411. Ref<Texture2D> file_icon = vbox->get_theme_icon(SNAME("file"), SNAME("FileDialog"));
  412. const Color folder_color = vbox->get_theme_color(SNAME("folder_icon_modulate"), SNAME("FileDialog"));
  413. const Color file_color = vbox->get_theme_color(SNAME("file_icon_modulate"), SNAME("FileDialog"));
  414. List<String> files;
  415. List<String> dirs;
  416. bool is_hidden;
  417. String item = dir_access->get_next();
  418. while (!item.is_empty()) {
  419. if (item == "." || item == "..") {
  420. item = dir_access->get_next();
  421. continue;
  422. }
  423. is_hidden = dir_access->current_is_hidden();
  424. if (show_hidden_files || !is_hidden) {
  425. if (!dir_access->current_is_dir()) {
  426. files.push_back(item);
  427. } else {
  428. dirs.push_back(item);
  429. }
  430. }
  431. item = dir_access->get_next();
  432. }
  433. dirs.sort_custom<NaturalNoCaseComparator>();
  434. files.sort_custom<NaturalNoCaseComparator>();
  435. while (!dirs.is_empty()) {
  436. String &dir_name = dirs.front()->get();
  437. TreeItem *ti = tree->create_item(root);
  438. ti->set_text(0, dir_name);
  439. ti->set_icon(0, folder);
  440. ti->set_icon_modulate(0, folder_color);
  441. Dictionary d;
  442. d["name"] = dir_name;
  443. d["dir"] = true;
  444. ti->set_metadata(0, d);
  445. dirs.pop_front();
  446. }
  447. List<String> patterns;
  448. // build filter
  449. if (filter->get_selected() == filter->get_item_count() - 1) {
  450. // match all
  451. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  452. // match all filters
  453. for (int i = 0; i < filters.size(); i++) {
  454. String f = filters[i].get_slice(";", 0);
  455. for (int j = 0; j < f.get_slice_count(","); j++) {
  456. patterns.push_back(f.get_slice(",", j).strip_edges());
  457. }
  458. }
  459. } else {
  460. int idx = filter->get_selected();
  461. if (filters.size() > 1) {
  462. idx--;
  463. }
  464. if (idx >= 0 && idx < filters.size()) {
  465. String f = filters[idx].get_slice(";", 0);
  466. for (int j = 0; j < f.get_slice_count(","); j++) {
  467. patterns.push_back(f.get_slice(",", j).strip_edges());
  468. }
  469. }
  470. }
  471. String base_dir = dir_access->get_current_dir();
  472. while (!files.is_empty()) {
  473. bool match = patterns.is_empty();
  474. String match_str;
  475. for (const String &E : patterns) {
  476. if (files.front()->get().matchn(E)) {
  477. match_str = E;
  478. match = true;
  479. break;
  480. }
  481. }
  482. if (match) {
  483. TreeItem *ti = tree->create_item(root);
  484. ti->set_text(0, files.front()->get());
  485. if (get_icon_func) {
  486. Ref<Texture2D> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  487. ti->set_icon(0, icon);
  488. } else {
  489. ti->set_icon(0, file_icon);
  490. }
  491. ti->set_icon_modulate(0, file_color);
  492. if (mode == FILE_MODE_OPEN_DIR) {
  493. ti->set_custom_color(0, vbox->get_theme_color(SNAME("files_disabled"), SNAME("FileDialog")));
  494. ti->set_selectable(0, false);
  495. }
  496. Dictionary d;
  497. d["name"] = files.front()->get();
  498. d["dir"] = false;
  499. ti->set_metadata(0, d);
  500. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  501. ti->select(0);
  502. }
  503. }
  504. files.pop_front();
  505. }
  506. if (tree->get_root() && tree->get_root()->get_first_child() && tree->get_selected() == nullptr) {
  507. tree->get_root()->get_first_child()->select(0);
  508. }
  509. }
  510. void FileDialog::_filter_selected(int) {
  511. update_file_name();
  512. update_file_list();
  513. }
  514. void FileDialog::update_filters() {
  515. filter->clear();
  516. if (filters.size() > 1) {
  517. String all_filters;
  518. const int max_filters = 5;
  519. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  520. String flt = filters[i].get_slice(";", 0).strip_edges();
  521. if (i > 0) {
  522. all_filters += ", ";
  523. }
  524. all_filters += flt;
  525. }
  526. if (max_filters < filters.size()) {
  527. all_filters += ", ...";
  528. }
  529. filter->add_item(String(TTRC("All Recognized")) + " (" + all_filters + ")");
  530. }
  531. for (int i = 0; i < filters.size(); i++) {
  532. String flt = filters[i].get_slice(";", 0).strip_edges();
  533. String desc = filters[i].get_slice(";", 1).strip_edges();
  534. if (desc.length()) {
  535. filter->add_item(String(tr(desc)) + " (" + flt + ")");
  536. } else {
  537. filter->add_item("(" + flt + ")");
  538. }
  539. }
  540. filter->add_item(TTRC("All Files (*)"));
  541. }
  542. void FileDialog::clear_filters() {
  543. filters.clear();
  544. update_filters();
  545. invalidate();
  546. }
  547. void FileDialog::add_filter(const String &p_filter) {
  548. filters.push_back(p_filter);
  549. update_filters();
  550. invalidate();
  551. }
  552. void FileDialog::set_filters(const Vector<String> &p_filters) {
  553. filters = p_filters;
  554. update_filters();
  555. invalidate();
  556. }
  557. Vector<String> FileDialog::get_filters() const {
  558. return filters;
  559. }
  560. String FileDialog::get_current_dir() const {
  561. return dir->get_text();
  562. }
  563. String FileDialog::get_current_file() const {
  564. return file->get_text();
  565. }
  566. String FileDialog::get_current_path() const {
  567. return dir->get_text().plus_file(file->get_text());
  568. }
  569. void FileDialog::set_current_dir(const String &p_dir) {
  570. dir_access->change_dir(p_dir);
  571. update_dir();
  572. invalidate();
  573. _push_history();
  574. }
  575. void FileDialog::set_current_file(const String &p_file) {
  576. file->set_text(p_file);
  577. update_dir();
  578. invalidate();
  579. int lp = p_file.rfind(".");
  580. if (lp != -1) {
  581. file->select(0, lp);
  582. if (file->is_inside_tree() && !get_tree()->is_node_being_edited(file)) {
  583. file->grab_focus();
  584. }
  585. }
  586. }
  587. void FileDialog::set_current_path(const String &p_path) {
  588. if (!p_path.size()) {
  589. return;
  590. }
  591. int pos = MAX(p_path.rfind("/"), p_path.rfind("\\"));
  592. if (pos == -1) {
  593. set_current_file(p_path);
  594. } else {
  595. String dir = p_path.substr(0, pos);
  596. String file = p_path.substr(pos + 1, p_path.length());
  597. set_current_dir(dir);
  598. set_current_file(file);
  599. }
  600. }
  601. void FileDialog::set_mode_overrides_title(bool p_override) {
  602. mode_overrides_title = p_override;
  603. }
  604. bool FileDialog::is_mode_overriding_title() const {
  605. return mode_overrides_title;
  606. }
  607. void FileDialog::set_file_mode(FileMode p_mode) {
  608. ERR_FAIL_INDEX((int)p_mode, 5);
  609. mode = p_mode;
  610. switch (mode) {
  611. case FILE_MODE_OPEN_FILE:
  612. get_ok_button()->set_text(TTRC("Open"));
  613. if (mode_overrides_title) {
  614. set_title(TTRC("Open a File"));
  615. }
  616. makedir->hide();
  617. break;
  618. case FILE_MODE_OPEN_FILES:
  619. get_ok_button()->set_text(TTRC("Open"));
  620. if (mode_overrides_title) {
  621. set_title(TTRC("Open File(s)"));
  622. }
  623. makedir->hide();
  624. break;
  625. case FILE_MODE_OPEN_DIR:
  626. get_ok_button()->set_text(TTRC("Select Current Folder"));
  627. if (mode_overrides_title) {
  628. set_title(TTRC("Open a Directory"));
  629. }
  630. makedir->show();
  631. break;
  632. case FILE_MODE_OPEN_ANY:
  633. get_ok_button()->set_text(TTRC("Open"));
  634. if (mode_overrides_title) {
  635. set_title(TTRC("Open a File or Directory"));
  636. }
  637. makedir->show();
  638. break;
  639. case FILE_MODE_SAVE_FILE:
  640. get_ok_button()->set_text(TTRC("Save"));
  641. if (mode_overrides_title) {
  642. set_title(TTRC("Save a File"));
  643. }
  644. makedir->show();
  645. break;
  646. }
  647. if (mode == FILE_MODE_OPEN_FILES) {
  648. tree->set_select_mode(Tree::SELECT_MULTI);
  649. } else {
  650. tree->set_select_mode(Tree::SELECT_SINGLE);
  651. }
  652. }
  653. FileDialog::FileMode FileDialog::get_file_mode() const {
  654. return mode;
  655. }
  656. void FileDialog::set_access(Access p_access) {
  657. ERR_FAIL_INDEX(p_access, 3);
  658. if (access == p_access) {
  659. return;
  660. }
  661. memdelete(dir_access);
  662. switch (p_access) {
  663. case ACCESS_FILESYSTEM: {
  664. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  665. } break;
  666. case ACCESS_RESOURCES: {
  667. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  668. } break;
  669. case ACCESS_USERDATA: {
  670. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  671. } break;
  672. }
  673. access = p_access;
  674. _update_drives();
  675. invalidate();
  676. update_filters();
  677. update_dir();
  678. }
  679. void FileDialog::invalidate() {
  680. if (is_visible()) {
  681. update_file_list();
  682. invalidated = false;
  683. } else {
  684. invalidated = true;
  685. }
  686. }
  687. FileDialog::Access FileDialog::get_access() const {
  688. return access;
  689. }
  690. void FileDialog::_make_dir_confirm() {
  691. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  692. if (err == OK) {
  693. dir_access->change_dir(makedirname->get_text().strip_edges());
  694. invalidate();
  695. update_filters();
  696. update_dir();
  697. _push_history();
  698. } else {
  699. mkdirerr->popup_centered(Size2(250, 50));
  700. }
  701. makedirname->set_text(""); // reset label
  702. }
  703. void FileDialog::_make_dir() {
  704. makedialog->popup_centered(Size2(250, 80));
  705. makedirname->grab_focus();
  706. }
  707. void FileDialog::_select_drive(int p_idx) {
  708. String d = drives->get_item_text(p_idx);
  709. dir_access->change_dir(d);
  710. file->set_text("");
  711. invalidate();
  712. update_dir();
  713. _push_history();
  714. }
  715. void FileDialog::_update_drives() {
  716. int dc = dir_access->get_drive_count();
  717. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  718. drives->hide();
  719. } else {
  720. drives->clear();
  721. Node *dp = drives->get_parent();
  722. if (dp) {
  723. dp->remove_child(drives);
  724. }
  725. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  726. dp->add_child(drives);
  727. drives->show();
  728. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  729. drives->add_item(dir_access->get_drive(i));
  730. }
  731. drives->select(dir_access->get_current_drive());
  732. }
  733. }
  734. bool FileDialog::default_show_hidden_files = false;
  735. void FileDialog::_bind_methods() {
  736. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  737. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  738. ClassDB::bind_method(D_METHOD("add_filter", "filter"), &FileDialog::add_filter);
  739. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  740. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  741. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  742. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  743. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  744. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  745. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  746. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  747. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  748. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  749. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  750. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  751. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  752. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  753. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  754. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  755. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  756. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  757. ClassDB::bind_method(D_METHOD("_update_file_name"), &FileDialog::update_file_name);
  758. ClassDB::bind_method(D_METHOD("_update_dir"), &FileDialog::update_dir);
  759. ClassDB::bind_method(D_METHOD("_update_file_list"), &FileDialog::update_file_list);
  760. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  761. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  762. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  763. 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");
  764. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access");
  765. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  766. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  767. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir"), "set_current_dir", "get_current_dir");
  768. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file"), "set_current_file", "get_current_file");
  769. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path"), "set_current_path", "get_current_path");
  770. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  771. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  772. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  773. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  774. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  775. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  776. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  777. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  778. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  779. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  780. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  781. }
  782. void FileDialog::set_show_hidden_files(bool p_show) {
  783. show_hidden_files = p_show;
  784. invalidate();
  785. }
  786. bool FileDialog::is_showing_hidden_files() const {
  787. return show_hidden_files;
  788. }
  789. void FileDialog::set_default_show_hidden_files(bool p_show) {
  790. default_show_hidden_files = p_show;
  791. }
  792. FileDialog::FileDialog() {
  793. show_hidden_files = default_show_hidden_files;
  794. vbox = memnew(VBoxContainer);
  795. add_child(vbox, false, INTERNAL_MODE_FRONT);
  796. vbox->connect("theme_changed", callable_mp(this, &FileDialog::_theme_changed));
  797. mode = FILE_MODE_SAVE_FILE;
  798. set_title(TTRC("Save a File"));
  799. HBoxContainer *hbc = memnew(HBoxContainer);
  800. dir_prev = memnew(Button);
  801. dir_prev->set_flat(true);
  802. dir_prev->set_tooltip(TTRC("Go to previous folder."));
  803. dir_next = memnew(Button);
  804. dir_next->set_flat(true);
  805. dir_next->set_tooltip(TTRC("Go to next folder."));
  806. dir_up = memnew(Button);
  807. dir_up->set_flat(true);
  808. dir_up->set_tooltip(TTRC("Go to parent folder."));
  809. hbc->add_child(dir_prev);
  810. hbc->add_child(dir_next);
  811. hbc->add_child(dir_up);
  812. dir_prev->connect("pressed", callable_mp(this, &FileDialog::_go_back));
  813. dir_next->connect("pressed", callable_mp(this, &FileDialog::_go_forward));
  814. dir_up->connect("pressed", callable_mp(this, &FileDialog::_go_up));
  815. hbc->add_child(memnew(Label(TTRC("Path:"))));
  816. drives_container = memnew(HBoxContainer);
  817. hbc->add_child(drives_container);
  818. drives = memnew(OptionButton);
  819. drives->connect("item_selected", callable_mp(this, &FileDialog::_select_drive));
  820. hbc->add_child(drives);
  821. dir = memnew(LineEdit);
  822. dir->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  823. hbc->add_child(dir);
  824. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  825. refresh = memnew(Button);
  826. refresh->set_flat(true);
  827. refresh->set_tooltip(TTRC("Refresh files."));
  828. refresh->connect("pressed", callable_mp(this, &FileDialog::update_file_list));
  829. hbc->add_child(refresh);
  830. show_hidden = memnew(Button);
  831. show_hidden->set_flat(true);
  832. show_hidden->set_toggle_mode(true);
  833. show_hidden->set_pressed(is_showing_hidden_files());
  834. show_hidden->set_tooltip(TTRC("Toggle the visibility of hidden files."));
  835. show_hidden->connect("toggled", callable_mp(this, &FileDialog::set_show_hidden_files));
  836. hbc->add_child(show_hidden);
  837. shortcuts_container = memnew(HBoxContainer);
  838. hbc->add_child(shortcuts_container);
  839. makedir = memnew(Button);
  840. makedir->set_text(TTRC("Create Folder"));
  841. makedir->connect("pressed", callable_mp(this, &FileDialog::_make_dir));
  842. hbc->add_child(makedir);
  843. vbox->add_child(hbc);
  844. tree = memnew(Tree);
  845. tree->set_hide_root(true);
  846. vbox->add_margin_child(TTRC("Directories & Files:"), tree, true);
  847. message = memnew(Label);
  848. message->hide();
  849. message->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  850. message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  851. message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  852. tree->add_child(message);
  853. file_box = memnew(HBoxContainer);
  854. file_box->add_child(memnew(Label(TTRC("File:"))));
  855. file = memnew(LineEdit);
  856. file->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  857. file->set_stretch_ratio(4);
  858. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  859. file_box->add_child(file);
  860. filter = memnew(OptionButton);
  861. filter->set_stretch_ratio(3);
  862. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  863. filter->set_clip_text(true); // too many extensions overflows it
  864. file_box->add_child(filter);
  865. vbox->add_child(file_box);
  866. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  867. _update_drives();
  868. connect("confirmed", callable_mp(this, &FileDialog::_action_pressed));
  869. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), varray(), CONNECT_DEFERRED);
  870. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), varray(), CONNECT_DEFERRED);
  871. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated), varray());
  872. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  873. dir->connect("text_submitted", callable_mp(this, &FileDialog::_dir_submitted));
  874. file->connect("text_submitted", callable_mp(this, &FileDialog::_file_submitted));
  875. filter->connect("item_selected", callable_mp(this, &FileDialog::_filter_selected));
  876. confirm_save = memnew(ConfirmationDialog);
  877. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  878. confirm_save->connect("confirmed", callable_mp(this, &FileDialog::_save_confirm_pressed));
  879. makedialog = memnew(ConfirmationDialog);
  880. makedialog->set_title(TTRC("Create Folder"));
  881. VBoxContainer *makevb = memnew(VBoxContainer);
  882. makedialog->add_child(makevb);
  883. makedirname = memnew(LineEdit);
  884. makedirname->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  885. makevb->add_margin_child(TTRC("Name:"), makedirname);
  886. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  887. makedialog->register_text_enter(makedirname);
  888. makedialog->connect("confirmed", callable_mp(this, &FileDialog::_make_dir_confirm));
  889. mkdirerr = memnew(AcceptDialog);
  890. mkdirerr->set_text(TTRC("Could not create folder."));
  891. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  892. exterr = memnew(AcceptDialog);
  893. exterr->set_text(TTRC("Must use a valid extension."));
  894. add_child(exterr, false, INTERNAL_MODE_FRONT);
  895. update_filters();
  896. update_dir();
  897. set_hide_on_ok(false);
  898. if (register_func) {
  899. register_func(this);
  900. }
  901. }
  902. FileDialog::~FileDialog() {
  903. if (unregister_func) {
  904. unregister_func(this);
  905. }
  906. memdelete(dir_access);
  907. }