file_dialog.cpp 26 KB

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