file_dialog.cpp 35 KB

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