file_dialog.cpp 25 KB

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