file_dialog.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. dir_up->set_icon(get_icon("parent_folder"));
  44. refresh->set_icon(get_icon("reload"));
  45. show_hidden->set_icon(get_icon("toggle_hidden"));
  46. }
  47. if (p_what == NOTIFICATION_POPUP_HIDE) {
  48. set_process_unhandled_input(false);
  49. }
  50. }
  51. void FileDialog::_unhandled_input(const Ref<InputEvent> &p_event) {
  52. Ref<InputEventKey> k = p_event;
  53. if (k.is_valid() && is_window_modal_on_top()) {
  54. if (k->is_pressed()) {
  55. bool handled = true;
  56. switch (k->get_scancode()) {
  57. case KEY_H: {
  58. if (k->get_command()) {
  59. set_show_hidden_files(!show_hidden_files);
  60. } else {
  61. handled = false;
  62. }
  63. } break;
  64. case KEY_F5: {
  65. invalidate();
  66. } break;
  67. case KEY_BACKSPACE: {
  68. _dir_entered("..");
  69. } break;
  70. default: {
  71. handled = false;
  72. }
  73. }
  74. if (handled)
  75. accept_event();
  76. }
  77. }
  78. }
  79. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  80. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  81. };
  82. Vector<String> FileDialog::get_selected_files() const {
  83. Vector<String> list;
  84. TreeItem *item = tree->get_root();
  85. while ((item = tree->get_next_selected(item))) {
  86. list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0)));
  87. };
  88. return list;
  89. };
  90. void FileDialog::update_dir() {
  91. dir->set_text(dir_access->get_current_dir());
  92. if (drives->is_visible()) {
  93. drives->select(dir_access->get_current_drive());
  94. }
  95. // Deselect any item, to make "Select Current Folder" button text by default.
  96. deselect_items();
  97. }
  98. void FileDialog::_dir_entered(String p_dir) {
  99. dir_access->change_dir(p_dir);
  100. file->set_text("");
  101. invalidate();
  102. update_dir();
  103. }
  104. void FileDialog::_file_entered(const String &p_file) {
  105. _action_pressed();
  106. }
  107. void FileDialog::_save_confirm_pressed() {
  108. String f = dir_access->get_current_dir().plus_file(file->get_text());
  109. emit_signal("file_selected", f);
  110. hide();
  111. }
  112. void FileDialog::_post_popup() {
  113. ConfirmationDialog::_post_popup();
  114. if (invalidated) {
  115. update_file_list();
  116. invalidated = false;
  117. }
  118. if (mode == MODE_SAVE_FILE)
  119. file->grab_focus();
  120. else
  121. tree->grab_focus();
  122. set_process_unhandled_input(true);
  123. // For open dir mode, deselect all items on file dialog open.
  124. if (mode == MODE_OPEN_DIR)
  125. deselect_items();
  126. }
  127. void FileDialog::_action_pressed() {
  128. if (mode == MODE_OPEN_FILES) {
  129. TreeItem *ti = tree->get_next_selected(NULL);
  130. String fbase = dir_access->get_current_dir();
  131. PoolVector<String> files;
  132. while (ti) {
  133. files.push_back(fbase.plus_file(ti->get_text(0)));
  134. ti = tree->get_next_selected(ti);
  135. }
  136. if (files.size()) {
  137. emit_signal("files_selected", files);
  138. hide();
  139. }
  140. return;
  141. }
  142. String f = dir_access->get_current_dir().plus_file(file->get_text());
  143. if ((mode == MODE_OPEN_ANY || mode == MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  144. emit_signal("file_selected", f);
  145. hide();
  146. } else if (mode == MODE_OPEN_ANY || mode == MODE_OPEN_DIR) {
  147. String path = dir_access->get_current_dir();
  148. path = path.replace("\\", "/");
  149. TreeItem *item = tree->get_selected();
  150. if (item) {
  151. Dictionary d = item->get_metadata(0);
  152. if (d["dir"] && d["name"] != "..") {
  153. path = path.plus_file(d["name"]);
  154. }
  155. }
  156. emit_signal("dir_selected", path);
  157. hide();
  158. }
  159. if (mode == MODE_SAVE_FILE) {
  160. bool valid = false;
  161. if (filter->get_selected() == filter->get_item_count() - 1) {
  162. valid = true; // match none
  163. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  164. // match all filters
  165. for (int i = 0; i < filters.size(); i++) {
  166. String flt = filters[i].get_slice(";", 0);
  167. for (int j = 0; j < flt.get_slice_count(","); j++) {
  168. String str = flt.get_slice(",", j).strip_edges();
  169. if (f.match(str)) {
  170. valid = true;
  171. break;
  172. }
  173. }
  174. if (valid)
  175. break;
  176. }
  177. } else {
  178. int idx = filter->get_selected();
  179. if (filters.size() > 1)
  180. idx--;
  181. if (idx >= 0 && idx < filters.size()) {
  182. String flt = filters[idx].get_slice(";", 0);
  183. int filterSliceCount = flt.get_slice_count(",");
  184. for (int j = 0; j < filterSliceCount; 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 && filterSliceCount > 0) {
  192. String str = (flt.get_slice(",", 0).strip_edges());
  193. f += str.substr(1, str.length() - 1);
  194. file->set_text(f.get_file());
  195. valid = true;
  196. }
  197. } else {
  198. valid = true;
  199. }
  200. }
  201. if (!valid) {
  202. exterr->popup_centered_minsize(Size2(250, 80));
  203. return;
  204. }
  205. if (dir_access->file_exists(f)) {
  206. confirm_save->set_text(RTR("File Exists, Overwrite?"));
  207. confirm_save->popup_centered(Size2(200, 80));
  208. } else {
  209. emit_signal("file_selected", f);
  210. hide();
  211. }
  212. }
  213. }
  214. void FileDialog::_cancel_pressed() {
  215. file->set_text("");
  216. invalidate();
  217. hide();
  218. }
  219. bool FileDialog::_is_open_should_be_disabled() {
  220. if (mode == MODE_OPEN_ANY || mode == MODE_SAVE_FILE)
  221. return false;
  222. TreeItem *ti = tree->get_next_selected(tree->get_root());
  223. while (ti) {
  224. TreeItem *prev_ti = ti;
  225. ti = tree->get_next_selected(tree->get_root());
  226. if (ti == prev_ti)
  227. break;
  228. }
  229. // We have something that we can't select?
  230. if (!ti)
  231. return mode != MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  232. Dictionary d = ti->get_metadata(0);
  233. // Opening a file, but selected a folder? Forbidden.
  234. if (((mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  235. (mode == MODE_OPEN_DIR && !d["dir"]))
  236. return true;
  237. return false;
  238. }
  239. void FileDialog::_go_up() {
  240. dir_access->change_dir("..");
  241. update_file_list();
  242. update_dir();
  243. }
  244. void FileDialog::deselect_items() {
  245. // Clear currently selected items in file manager.
  246. tree->deselect_all();
  247. // And change get_ok title.
  248. if (!tree->is_anything_selected()) {
  249. get_ok()->set_disabled(_is_open_should_be_disabled());
  250. switch (mode) {
  251. case MODE_OPEN_FILE:
  252. case MODE_OPEN_FILES:
  253. get_ok()->set_text(RTR("Open"));
  254. break;
  255. case MODE_OPEN_DIR:
  256. get_ok()->set_text(RTR("Select Current Folder"));
  257. break;
  258. case MODE_OPEN_ANY:
  259. case MODE_SAVE_FILE:
  260. // FIXME: Implement, or refactor to avoid duplication with set_mode
  261. break;
  262. }
  263. }
  264. }
  265. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  266. _tree_selected();
  267. }
  268. void FileDialog::_tree_selected() {
  269. TreeItem *ti = tree->get_selected();
  270. if (!ti)
  271. return;
  272. Dictionary d = ti->get_metadata(0);
  273. if (!d["dir"]) {
  274. file->set_text(d["name"]);
  275. } else if (mode == MODE_OPEN_DIR) {
  276. get_ok()->set_text(RTR("Select This Folder"));
  277. }
  278. get_ok()->set_disabled(_is_open_should_be_disabled());
  279. }
  280. void FileDialog::_tree_item_activated() {
  281. TreeItem *ti = tree->get_selected();
  282. if (!ti)
  283. return;
  284. Dictionary d = ti->get_metadata(0);
  285. if (d["dir"]) {
  286. dir_access->change_dir(d["name"]);
  287. if (mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES || mode == MODE_OPEN_DIR || mode == MODE_OPEN_ANY)
  288. file->set_text("");
  289. call_deferred("_update_file_list");
  290. call_deferred("_update_dir");
  291. } else {
  292. _action_pressed();
  293. }
  294. }
  295. void FileDialog::update_file_list() {
  296. tree->clear();
  297. dir_access->list_dir_begin();
  298. TreeItem *root = tree->create_item();
  299. Ref<Texture> folder = get_icon("folder");
  300. List<String> files;
  301. List<String> dirs;
  302. bool is_dir;
  303. bool is_hidden;
  304. String item;
  305. while ((item = dir_access->get_next(&is_dir)) != "") {
  306. if (item == "." || item == "..")
  307. continue;
  308. is_hidden = dir_access->current_is_hidden();
  309. if (show_hidden_files || !is_hidden) {
  310. if (!is_dir)
  311. files.push_back(item);
  312. else
  313. dirs.push_back(item);
  314. }
  315. }
  316. dirs.sort_custom<NaturalNoCaseComparator>();
  317. files.sort_custom<NaturalNoCaseComparator>();
  318. while (!dirs.empty()) {
  319. String &dir_name = dirs.front()->get();
  320. TreeItem *ti = tree->create_item(root);
  321. ti->set_text(0, dir_name);
  322. ti->set_icon(0, folder);
  323. Dictionary d;
  324. d["name"] = dir_name;
  325. d["dir"] = true;
  326. ti->set_metadata(0, d);
  327. dirs.pop_front();
  328. }
  329. List<String> patterns;
  330. // build filter
  331. if (filter->get_selected() == filter->get_item_count() - 1) {
  332. // match all
  333. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  334. // match all filters
  335. for (int i = 0; i < filters.size(); i++) {
  336. String f = filters[i].get_slice(";", 0);
  337. for (int j = 0; j < f.get_slice_count(","); j++) {
  338. patterns.push_back(f.get_slice(",", j).strip_edges());
  339. }
  340. }
  341. } else {
  342. int idx = filter->get_selected();
  343. if (filters.size() > 1)
  344. idx--;
  345. if (idx >= 0 && idx < filters.size()) {
  346. String f = filters[idx].get_slice(";", 0);
  347. for (int j = 0; j < f.get_slice_count(","); j++) {
  348. patterns.push_back(f.get_slice(",", j).strip_edges());
  349. }
  350. }
  351. }
  352. String base_dir = dir_access->get_current_dir();
  353. while (!files.empty()) {
  354. bool match = patterns.empty();
  355. String match_str;
  356. for (List<String>::Element *E = patterns.front(); E; E = E->next()) {
  357. if (files.front()->get().matchn(E->get())) {
  358. match_str = E->get();
  359. match = true;
  360. break;
  361. }
  362. }
  363. if (match) {
  364. TreeItem *ti = tree->create_item(root);
  365. ti->set_text(0, files.front()->get());
  366. if (get_icon_func) {
  367. Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  368. ti->set_icon(0, icon);
  369. }
  370. if (mode == MODE_OPEN_DIR) {
  371. ti->set_custom_color(0, get_color("files_disabled"));
  372. ti->set_selectable(0, false);
  373. }
  374. Dictionary d;
  375. d["name"] = files.front()->get();
  376. d["dir"] = false;
  377. ti->set_metadata(0, d);
  378. if (file->get_text() == files.front()->get() || match_str == files.front()->get())
  379. ti->select(0);
  380. }
  381. files.pop_front();
  382. }
  383. if (tree->get_root() && tree->get_root()->get_children() && tree->get_selected() == NULL)
  384. tree->get_root()->get_children()->select(0);
  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->set_tooltip(RTR("Refresh"));
  676. refresh->connect("pressed", this, "_update_file_list");
  677. hbc->add_child(refresh);
  678. show_hidden = memnew(ToolButton);
  679. show_hidden->set_toggle_mode(true);
  680. show_hidden->set_pressed(is_showing_hidden_files());
  681. show_hidden->set_tooltip(RTR("Toggle Hidden Files"));
  682. show_hidden->connect("toggled", this, "set_show_hidden_files");
  683. hbc->add_child(show_hidden);
  684. drives = memnew(OptionButton);
  685. hbc->add_child(drives);
  686. drives->connect("item_selected", this, "_select_drive");
  687. makedir = memnew(Button);
  688. makedir->set_text(RTR("Create Folder"));
  689. makedir->connect("pressed", this, "_make_dir");
  690. hbc->add_child(makedir);
  691. vbc->add_child(hbc);
  692. tree = memnew(Tree);
  693. tree->set_hide_root(true);
  694. vbc->add_margin_child(RTR("Directories & Files:"), tree, true);
  695. hbc = memnew(HBoxContainer);
  696. hbc->add_child(memnew(Label(RTR("File:"))));
  697. file = memnew(LineEdit);
  698. file->set_stretch_ratio(4);
  699. file->set_h_size_flags(SIZE_EXPAND_FILL);
  700. hbc->add_child(file);
  701. filter = memnew(OptionButton);
  702. filter->set_stretch_ratio(3);
  703. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  704. filter->set_clip_text(true); // too many extensions overflows it
  705. hbc->add_child(filter);
  706. vbc->add_child(hbc);
  707. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  708. access = ACCESS_RESOURCES;
  709. _update_drives();
  710. connect("confirmed", this, "_action_pressed");
  711. tree->connect("multi_selected", this, "_tree_multi_selected", varray(), CONNECT_DEFERRED);
  712. tree->connect("cell_selected", this, "_tree_selected", varray(), CONNECT_DEFERRED);
  713. tree->connect("item_activated", this, "_tree_item_activated", varray());
  714. tree->connect("nothing_selected", this, "deselect_items");
  715. dir->connect("text_entered", this, "_dir_entered");
  716. file->connect("text_entered", this, "_file_entered");
  717. filter->connect("item_selected", this, "_filter_selected");
  718. confirm_save = memnew(ConfirmationDialog);
  719. confirm_save->set_as_toplevel(true);
  720. add_child(confirm_save);
  721. confirm_save->connect("confirmed", this, "_save_confirm_pressed");
  722. makedialog = memnew(ConfirmationDialog);
  723. makedialog->set_title(RTR("Create Folder"));
  724. VBoxContainer *makevb = memnew(VBoxContainer);
  725. makedialog->add_child(makevb);
  726. makedirname = memnew(LineEdit);
  727. makevb->add_margin_child(RTR("Name:"), makedirname);
  728. add_child(makedialog);
  729. makedialog->register_text_enter(makedirname);
  730. makedialog->connect("confirmed", this, "_make_dir_confirm");
  731. mkdirerr = memnew(AcceptDialog);
  732. mkdirerr->set_text(RTR("Could not create folder."));
  733. add_child(mkdirerr);
  734. exterr = memnew(AcceptDialog);
  735. exterr->set_text(RTR("Must use a valid extension."));
  736. add_child(exterr);
  737. update_filters();
  738. update_dir();
  739. set_hide_on_ok(false);
  740. vbox = vbc;
  741. invalidated = true;
  742. if (register_func)
  743. register_func(this);
  744. }
  745. FileDialog::~FileDialog() {
  746. if (unregister_func)
  747. unregister_func(this);
  748. memdelete(dir_access);
  749. }
  750. void LineEditFileChooser::_bind_methods() {
  751. ClassDB::bind_method(D_METHOD("_browse"), &LineEditFileChooser::_browse);
  752. ClassDB::bind_method(D_METHOD("_chosen"), &LineEditFileChooser::_chosen);
  753. ClassDB::bind_method(D_METHOD("get_button"), &LineEditFileChooser::get_button);
  754. ClassDB::bind_method(D_METHOD("get_line_edit"), &LineEditFileChooser::get_line_edit);
  755. ClassDB::bind_method(D_METHOD("get_file_dialog"), &LineEditFileChooser::get_file_dialog);
  756. }
  757. void LineEditFileChooser::_chosen(const String &p_text) {
  758. line_edit->set_text(p_text);
  759. line_edit->emit_signal("text_entered", p_text);
  760. }
  761. void LineEditFileChooser::_browse() {
  762. dialog->popup_centered_ratio();
  763. }
  764. LineEditFileChooser::LineEditFileChooser() {
  765. line_edit = memnew(LineEdit);
  766. add_child(line_edit);
  767. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  768. button = memnew(Button);
  769. button->set_text(" .. ");
  770. add_child(button);
  771. button->connect("pressed", this, "_browse");
  772. dialog = memnew(FileDialog);
  773. add_child(dialog);
  774. dialog->connect("file_selected", this, "_chosen");
  775. dialog->connect("dir_selected", this, "_chosen");
  776. dialog->connect("files_selected", this, "_chosen");
  777. }