file_dialog.cpp 23 KB

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