file_dialog.cpp 33 KB

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