file_dialog.cpp 23 KB

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