file_dialog.cpp 23 KB

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