file_dialog.cpp 33 KB

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