file_dialog.cpp 34 KB

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