file_dialog.cpp 34 KB

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