file_dialog.cpp 28 KB

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