file_dialog.cpp 29 KB

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