file_dialog.cpp 27 KB

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