file_dialog.cpp 28 KB

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