file_dialog.cpp 24 KB

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