file_dialog.cpp 23 KB

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