file_dialog.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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_unhandled_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::unhandled_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(drives->get_item_count() - 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_unhandled_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. memdelete(dir_access);
  680. switch (p_access) {
  681. case ACCESS_FILESYSTEM: {
  682. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  683. } break;
  684. case ACCESS_RESOURCES: {
  685. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  686. } break;
  687. case ACCESS_USERDATA: {
  688. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  689. } break;
  690. }
  691. access = p_access;
  692. _update_drives();
  693. invalidate();
  694. update_filters();
  695. update_dir();
  696. }
  697. void FileDialog::invalidate() {
  698. if (is_visible()) {
  699. update_file_list();
  700. invalidated = false;
  701. } else {
  702. invalidated = true;
  703. }
  704. }
  705. FileDialog::Access FileDialog::get_access() const {
  706. return access;
  707. }
  708. void FileDialog::_make_dir_confirm() {
  709. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  710. if (err == OK) {
  711. dir_access->change_dir(makedirname->get_text().strip_edges());
  712. invalidate();
  713. update_filters();
  714. update_dir();
  715. _push_history();
  716. } else {
  717. mkdirerr->popup_centered(Size2(250, 50));
  718. }
  719. makedirname->set_text(""); // reset label
  720. }
  721. void FileDialog::_make_dir() {
  722. makedialog->popup_centered(Size2(250, 80));
  723. makedirname->grab_focus();
  724. }
  725. void FileDialog::_select_drive(int p_idx) {
  726. String d = drives->get_item_text(p_idx);
  727. dir_access->change_dir(d);
  728. file->set_text("");
  729. invalidate();
  730. update_dir();
  731. _push_history();
  732. }
  733. void FileDialog::_update_drives(bool p_select) {
  734. int dc = dir_access->get_drive_count();
  735. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  736. drives->hide();
  737. } else {
  738. drives->clear();
  739. Node *dp = drives->get_parent();
  740. if (dp) {
  741. dp->remove_child(drives);
  742. }
  743. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  744. dp->add_child(drives);
  745. drives->show();
  746. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  747. drives->add_item(dir_access->get_drive(i));
  748. }
  749. if (p_select) {
  750. drives->select(dir_access->get_current_drive());
  751. }
  752. }
  753. }
  754. bool FileDialog::default_show_hidden_files = false;
  755. void FileDialog::_bind_methods() {
  756. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  757. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  758. ClassDB::bind_method(D_METHOD("add_filter", "filter"), &FileDialog::add_filter);
  759. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  760. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  761. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  762. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  763. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  764. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  765. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  766. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  767. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  768. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  769. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  770. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  771. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  772. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  773. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  774. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  775. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  776. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  777. ClassDB::bind_method(D_METHOD("_update_file_name"), &FileDialog::update_file_name);
  778. ClassDB::bind_method(D_METHOD("_update_dir"), &FileDialog::update_dir);
  779. ClassDB::bind_method(D_METHOD("_update_file_list"), &FileDialog::update_file_list);
  780. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  781. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  782. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  783. 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");
  784. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access");
  785. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  786. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  787. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir"), "set_current_dir", "get_current_dir");
  788. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file"), "set_current_file", "get_current_file");
  789. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path"), "set_current_path", "get_current_path");
  790. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  791. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  792. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  793. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  794. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  795. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  796. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  797. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  798. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  799. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  800. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  801. }
  802. void FileDialog::set_show_hidden_files(bool p_show) {
  803. show_hidden_files = p_show;
  804. invalidate();
  805. }
  806. bool FileDialog::is_showing_hidden_files() const {
  807. return show_hidden_files;
  808. }
  809. void FileDialog::set_default_show_hidden_files(bool p_show) {
  810. default_show_hidden_files = p_show;
  811. }
  812. FileDialog::FileDialog() {
  813. show_hidden_files = default_show_hidden_files;
  814. vbox = memnew(VBoxContainer);
  815. add_child(vbox, false, INTERNAL_MODE_FRONT);
  816. vbox->connect("theme_changed", callable_mp(this, &FileDialog::_theme_changed));
  817. mode = FILE_MODE_SAVE_FILE;
  818. set_title(TTRC("Save a File"));
  819. HBoxContainer *hbc = memnew(HBoxContainer);
  820. dir_prev = memnew(Button);
  821. dir_prev->set_flat(true);
  822. dir_prev->set_tooltip(TTRC("Go to previous folder."));
  823. dir_next = memnew(Button);
  824. dir_next->set_flat(true);
  825. dir_next->set_tooltip(TTRC("Go to next folder."));
  826. dir_up = memnew(Button);
  827. dir_up->set_flat(true);
  828. dir_up->set_tooltip(TTRC("Go to parent folder."));
  829. hbc->add_child(dir_prev);
  830. hbc->add_child(dir_next);
  831. hbc->add_child(dir_up);
  832. dir_prev->connect("pressed", callable_mp(this, &FileDialog::_go_back));
  833. dir_next->connect("pressed", callable_mp(this, &FileDialog::_go_forward));
  834. dir_up->connect("pressed", callable_mp(this, &FileDialog::_go_up));
  835. hbc->add_child(memnew(Label(TTRC("Path:"))));
  836. drives_container = memnew(HBoxContainer);
  837. hbc->add_child(drives_container);
  838. drives = memnew(OptionButton);
  839. drives->connect("item_selected", callable_mp(this, &FileDialog::_select_drive));
  840. hbc->add_child(drives);
  841. dir = memnew(LineEdit);
  842. dir->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  843. hbc->add_child(dir);
  844. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  845. refresh = memnew(Button);
  846. refresh->set_flat(true);
  847. refresh->set_tooltip(TTRC("Refresh files."));
  848. refresh->connect("pressed", callable_mp(this, &FileDialog::update_file_list));
  849. hbc->add_child(refresh);
  850. show_hidden = memnew(Button);
  851. show_hidden->set_flat(true);
  852. show_hidden->set_toggle_mode(true);
  853. show_hidden->set_pressed(is_showing_hidden_files());
  854. show_hidden->set_tooltip(TTRC("Toggle the visibility of hidden files."));
  855. show_hidden->connect("toggled", callable_mp(this, &FileDialog::set_show_hidden_files));
  856. hbc->add_child(show_hidden);
  857. shortcuts_container = memnew(HBoxContainer);
  858. hbc->add_child(shortcuts_container);
  859. makedir = memnew(Button);
  860. makedir->set_text(TTRC("Create Folder"));
  861. makedir->connect("pressed", callable_mp(this, &FileDialog::_make_dir));
  862. hbc->add_child(makedir);
  863. vbox->add_child(hbc);
  864. tree = memnew(Tree);
  865. tree->set_hide_root(true);
  866. vbox->add_margin_child(TTRC("Directories & Files:"), tree, true);
  867. message = memnew(Label);
  868. message->hide();
  869. message->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  870. message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  871. message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  872. tree->add_child(message);
  873. file_box = memnew(HBoxContainer);
  874. file_box->add_child(memnew(Label(TTRC("File:"))));
  875. file = memnew(LineEdit);
  876. file->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  877. file->set_stretch_ratio(4);
  878. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  879. file_box->add_child(file);
  880. filter = memnew(OptionButton);
  881. filter->set_stretch_ratio(3);
  882. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  883. filter->set_clip_text(true); // too many extensions overflows it
  884. file_box->add_child(filter);
  885. vbox->add_child(file_box);
  886. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  887. _update_drives();
  888. connect("confirmed", callable_mp(this, &FileDialog::_action_pressed));
  889. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), varray(), CONNECT_DEFERRED);
  890. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), varray(), CONNECT_DEFERRED);
  891. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated), varray());
  892. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  893. dir->connect("text_submitted", callable_mp(this, &FileDialog::_dir_submitted));
  894. file->connect("text_submitted", callable_mp(this, &FileDialog::_file_submitted));
  895. filter->connect("item_selected", callable_mp(this, &FileDialog::_filter_selected));
  896. confirm_save = memnew(ConfirmationDialog);
  897. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  898. confirm_save->connect("confirmed", callable_mp(this, &FileDialog::_save_confirm_pressed));
  899. makedialog = memnew(ConfirmationDialog);
  900. makedialog->set_title(TTRC("Create Folder"));
  901. VBoxContainer *makevb = memnew(VBoxContainer);
  902. makedialog->add_child(makevb);
  903. makedirname = memnew(LineEdit);
  904. makedirname->set_structured_text_bidi_override(Control::STRUCTURED_TEXT_FILE);
  905. makevb->add_margin_child(TTRC("Name:"), makedirname);
  906. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  907. makedialog->register_text_enter(makedirname);
  908. makedialog->connect("confirmed", callable_mp(this, &FileDialog::_make_dir_confirm));
  909. mkdirerr = memnew(AcceptDialog);
  910. mkdirerr->set_text(TTRC("Could not create folder."));
  911. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  912. exterr = memnew(AcceptDialog);
  913. exterr->set_text(TTRC("Must use a valid extension."));
  914. add_child(exterr, false, INTERNAL_MODE_FRONT);
  915. update_filters();
  916. update_dir();
  917. set_hide_on_ok(false);
  918. if (register_func) {
  919. register_func(this);
  920. }
  921. }
  922. FileDialog::~FileDialog() {
  923. if (unregister_func) {
  924. unregister_func(this);
  925. }
  926. memdelete(dir_access);
  927. }