file_dialog.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /*************************************************************************/
  2. /* file_dialog.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "core/os/keyboard.h"
  32. #include "core/print_string.h"
  33. #include "scene/gui/label.h"
  34. FileDialog::GetIconFunc FileDialog::get_icon_func = nullptr;
  35. FileDialog::GetIconFunc FileDialog::get_large_icon_func = nullptr;
  36. FileDialog::RegisterFunc FileDialog::register_func = nullptr;
  37. FileDialog::RegisterFunc FileDialog::unregister_func = nullptr;
  38. VBoxContainer *FileDialog::get_vbox() {
  39. return vbox;
  40. }
  41. void FileDialog::_notification(int p_what) {
  42. if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
  43. if (p_what == NOTIFICATION_ENTER_TREE) {
  44. dir_up->set_icon(get_icon("parent_folder"));
  45. refresh->set_icon(get_icon("reload"));
  46. show_hidden->set_icon(get_icon("toggle_hidden"));
  47. }
  48. Color font_color = get_color("font_color", "ToolButton");
  49. Color font_color_hover = get_color("font_color_hover", "ToolButton");
  50. Color font_color_focus = get_color("font_color_focus", "ToolButton");
  51. Color font_color_pressed = get_color("font_color_pressed", "ToolButton");
  52. dir_up->add_color_override("icon_color_normal", font_color);
  53. dir_up->add_color_override("icon_color_hover", font_color_hover);
  54. dir_up->add_color_override("font_color_focus", font_color_focus);
  55. dir_up->add_color_override("icon_color_pressed", font_color_pressed);
  56. refresh->add_color_override("icon_color_normal", font_color);
  57. refresh->add_color_override("icon_color_hover", font_color_hover);
  58. refresh->add_color_override("font_color_focus", font_color_focus);
  59. refresh->add_color_override("icon_color_pressed", font_color_pressed);
  60. show_hidden->add_color_override("icon_color_normal", font_color);
  61. show_hidden->add_color_override("icon_color_hover", font_color_hover);
  62. show_hidden->add_color_override("font_color_focus", font_color_focus);
  63. show_hidden->add_color_override("icon_color_pressed", font_color_pressed);
  64. } else if (p_what == NOTIFICATION_POPUP_HIDE) {
  65. set_process_unhandled_input(false);
  66. }
  67. }
  68. void FileDialog::_unhandled_input(const Ref<InputEvent> &p_event) {
  69. Ref<InputEventKey> k = p_event;
  70. if (k.is_valid() && is_window_modal_on_top()) {
  71. if (k->is_pressed()) {
  72. bool handled = true;
  73. switch (k->get_scancode()) {
  74. case KEY_H: {
  75. if (k->get_command()) {
  76. set_show_hidden_files(!show_hidden_files);
  77. } else {
  78. handled = false;
  79. }
  80. } break;
  81. case KEY_F5: {
  82. invalidate();
  83. } break;
  84. case KEY_BACKSPACE: {
  85. _dir_entered("..");
  86. } break;
  87. default: {
  88. handled = false;
  89. }
  90. }
  91. if (handled) {
  92. accept_event();
  93. }
  94. }
  95. }
  96. }
  97. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  98. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  99. };
  100. Vector<String> FileDialog::get_selected_files() const {
  101. Vector<String> list;
  102. TreeItem *item = tree->get_root();
  103. while ((item = tree->get_next_selected(item))) {
  104. list.push_back(dir_access->get_current_dir().plus_file(item->get_text(0)));
  105. };
  106. return list;
  107. };
  108. void FileDialog::update_dir() {
  109. dir->set_text(dir_access->get_current_dir_without_drive());
  110. if (drives->is_visible()) {
  111. drives->select(dir_access->get_current_drive());
  112. }
  113. // Deselect any item, to make "Select Current Folder" button text by default.
  114. deselect_items();
  115. }
  116. void FileDialog::_dir_entered(String p_dir) {
  117. dir_access->change_dir(p_dir);
  118. file->set_text("");
  119. invalidate();
  120. update_dir();
  121. }
  122. void FileDialog::_file_entered(const String &p_file) {
  123. _action_pressed();
  124. }
  125. void FileDialog::_save_confirm_pressed() {
  126. String f = dir_access->get_current_dir().plus_file(file->get_text());
  127. emit_signal("file_selected", f);
  128. hide();
  129. }
  130. void FileDialog::_post_popup() {
  131. ConfirmationDialog::_post_popup();
  132. if (invalidated) {
  133. update_file_list();
  134. invalidated = false;
  135. }
  136. if (mode == MODE_SAVE_FILE) {
  137. file->grab_focus();
  138. } else {
  139. tree->grab_focus();
  140. }
  141. set_process_unhandled_input(true);
  142. // For open dir mode, deselect all items on file dialog open.
  143. if (mode == MODE_OPEN_DIR) {
  144. deselect_items();
  145. file_box->set_visible(false);
  146. } else {
  147. file_box->set_visible(true);
  148. }
  149. }
  150. void FileDialog::_action_pressed() {
  151. if (mode == MODE_OPEN_FILES) {
  152. TreeItem *ti = tree->get_next_selected(nullptr);
  153. String fbase = dir_access->get_current_dir();
  154. PoolVector<String> files;
  155. while (ti) {
  156. files.push_back(fbase.plus_file(ti->get_text(0)));
  157. ti = tree->get_next_selected(ti);
  158. }
  159. if (files.size()) {
  160. emit_signal("files_selected", files);
  161. hide();
  162. }
  163. return;
  164. }
  165. String f = dir_access->get_current_dir().plus_file(file->get_text());
  166. if ((mode == MODE_OPEN_ANY || mode == MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  167. emit_signal("file_selected", f);
  168. hide();
  169. } else if (mode == MODE_OPEN_ANY || mode == MODE_OPEN_DIR) {
  170. String path = dir_access->get_current_dir();
  171. path = path.replace("\\", "/");
  172. TreeItem *item = tree->get_selected();
  173. if (item) {
  174. Dictionary d = item->get_metadata(0);
  175. if (d["dir"] && d["name"] != "..") {
  176. path = path.plus_file(d["name"]);
  177. }
  178. }
  179. emit_signal("dir_selected", path);
  180. hide();
  181. }
  182. if (mode == MODE_SAVE_FILE) {
  183. bool valid = false;
  184. if (filter->get_selected() == filter->get_item_count() - 1) {
  185. valid = true; // match none
  186. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  187. // match all filters
  188. for (int i = 0; i < filters.size(); i++) {
  189. String flt = filters[i].get_slice(";", 0);
  190. for (int j = 0; j < flt.get_slice_count(","); j++) {
  191. String str = flt.get_slice(",", j).strip_edges();
  192. if (f.match(str)) {
  193. valid = true;
  194. break;
  195. }
  196. }
  197. if (valid) {
  198. break;
  199. }
  200. }
  201. } else {
  202. int idx = filter->get_selected();
  203. if (filters.size() > 1) {
  204. idx--;
  205. }
  206. if (idx >= 0 && idx < filters.size()) {
  207. String flt = filters[idx].get_slice(";", 0);
  208. int filterSliceCount = flt.get_slice_count(",");
  209. for (int j = 0; j < filterSliceCount; j++) {
  210. String str = (flt.get_slice(",", j).strip_edges());
  211. if (f.match(str)) {
  212. valid = true;
  213. break;
  214. }
  215. }
  216. if (!valid && filterSliceCount > 0) {
  217. String str = (flt.get_slice(",", 0).strip_edges());
  218. f += str.substr(1, str.length() - 1);
  219. file->set_text(f.get_file());
  220. valid = true;
  221. }
  222. } else {
  223. valid = true;
  224. }
  225. }
  226. if (!valid) {
  227. exterr->popup_centered_minsize(Size2(250, 80));
  228. return;
  229. }
  230. if (dir_access->file_exists(f)) {
  231. confirm_save->set_text(RTR("File exists, overwrite?"));
  232. confirm_save->popup_centered(Size2(200, 80));
  233. } else {
  234. emit_signal("file_selected", f);
  235. hide();
  236. }
  237. }
  238. }
  239. void FileDialog::_cancel_pressed() {
  240. file->set_text("");
  241. invalidate();
  242. hide();
  243. }
  244. bool FileDialog::_is_open_should_be_disabled() {
  245. if (mode == MODE_OPEN_ANY || mode == MODE_SAVE_FILE) {
  246. return false;
  247. }
  248. TreeItem *ti = tree->get_next_selected(tree->get_root());
  249. while (ti) {
  250. TreeItem *prev_ti = ti;
  251. ti = tree->get_next_selected(tree->get_root());
  252. if (ti == prev_ti) {
  253. break;
  254. }
  255. }
  256. // We have something that we can't select?
  257. if (!ti) {
  258. return mode != MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  259. }
  260. Dictionary d = ti->get_metadata(0);
  261. // Opening a file, but selected a folder? Forbidden.
  262. return ((mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  263. (mode == MODE_OPEN_DIR && !d["dir"]);
  264. }
  265. void FileDialog::_go_up() {
  266. dir_access->change_dir("..");
  267. update_file_list();
  268. update_dir();
  269. }
  270. void FileDialog::deselect_items() {
  271. // Clear currently selected items in file manager.
  272. tree->deselect_all();
  273. // And change get_ok title.
  274. if (!tree->is_anything_selected()) {
  275. get_ok()->set_disabled(_is_open_should_be_disabled());
  276. switch (mode) {
  277. case MODE_OPEN_FILE:
  278. case MODE_OPEN_FILES:
  279. get_ok()->set_text(RTR("Open"));
  280. break;
  281. case MODE_OPEN_DIR:
  282. get_ok()->set_text(RTR("Select Current Folder"));
  283. break;
  284. case MODE_OPEN_ANY:
  285. case MODE_SAVE_FILE:
  286. // FIXME: Implement, or refactor to avoid duplication with set_mode
  287. break;
  288. }
  289. }
  290. }
  291. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  292. _tree_selected();
  293. }
  294. void FileDialog::_tree_selected() {
  295. TreeItem *ti = tree->get_selected();
  296. if (!ti) {
  297. return;
  298. }
  299. Dictionary d = ti->get_metadata(0);
  300. if (!d["dir"]) {
  301. file->set_text(d["name"]);
  302. } else if (mode == MODE_OPEN_DIR) {
  303. get_ok()->set_text(RTR("Select This Folder"));
  304. }
  305. get_ok()->set_disabled(_is_open_should_be_disabled());
  306. }
  307. void FileDialog::_tree_item_activated() {
  308. TreeItem *ti = tree->get_selected();
  309. if (!ti) {
  310. return;
  311. }
  312. Dictionary d = ti->get_metadata(0);
  313. if (d["dir"]) {
  314. dir_access->change_dir(d["name"]);
  315. if (mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES || mode == MODE_OPEN_DIR || mode == MODE_OPEN_ANY) {
  316. file->set_text("");
  317. }
  318. call_deferred("_update_file_list");
  319. call_deferred("_update_dir");
  320. } else {
  321. _action_pressed();
  322. }
  323. }
  324. void FileDialog::update_file_name() {
  325. int idx = filter->get_selected() - 1;
  326. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  327. if (idx == -1) {
  328. idx += 1;
  329. }
  330. String filter_str = filters[idx];
  331. String file_str = file->get_text();
  332. String base_name = file_str.get_basename();
  333. file_str = base_name + "." + filter_str.strip_edges().to_lower();
  334. file->set_text(file_str);
  335. }
  336. }
  337. void FileDialog::update_file_list() {
  338. tree->clear();
  339. // Scroll back to the top after opening a directory
  340. tree->get_vscroll_bar()->set_value(0);
  341. dir_access->list_dir_begin();
  342. TreeItem *root = tree->create_item();
  343. Ref<Texture> folder = get_icon("folder");
  344. Ref<Texture> file_icon = get_icon("file");
  345. const Color folder_color = get_color("folder_icon_modulate");
  346. const Color file_color = get_color("file_icon_modulate");
  347. List<String> files;
  348. List<String> dirs;
  349. bool is_hidden;
  350. String item;
  351. while ((item = dir_access->get_next()) != "") {
  352. if (item == "." || item == "..") {
  353. continue;
  354. }
  355. is_hidden = dir_access->current_is_hidden();
  356. if (show_hidden_files || !is_hidden) {
  357. if (!dir_access->current_is_dir()) {
  358. files.push_back(item);
  359. } else {
  360. dirs.push_back(item);
  361. }
  362. }
  363. }
  364. dirs.sort_custom<NaturalNoCaseComparator>();
  365. files.sort_custom<NaturalNoCaseComparator>();
  366. while (!dirs.empty()) {
  367. String &dir_name = dirs.front()->get();
  368. TreeItem *ti = tree->create_item(root);
  369. ti->set_text(0, dir_name);
  370. ti->set_icon(0, folder);
  371. ti->set_icon_modulate(0, folder_color);
  372. Dictionary d;
  373. d["name"] = dir_name;
  374. d["dir"] = true;
  375. ti->set_metadata(0, d);
  376. dirs.pop_front();
  377. }
  378. List<String> patterns;
  379. // build filter
  380. if (filter->get_selected() == filter->get_item_count() - 1) {
  381. // match all
  382. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  383. // match all filters
  384. for (int i = 0; i < filters.size(); i++) {
  385. String f = filters[i].get_slice(";", 0);
  386. for (int j = 0; j < f.get_slice_count(","); j++) {
  387. patterns.push_back(f.get_slice(",", j).strip_edges());
  388. }
  389. }
  390. } else {
  391. int idx = filter->get_selected();
  392. if (filters.size() > 1) {
  393. idx--;
  394. }
  395. if (idx >= 0 && idx < filters.size()) {
  396. String f = filters[idx].get_slice(";", 0);
  397. for (int j = 0; j < f.get_slice_count(","); j++) {
  398. patterns.push_back(f.get_slice(",", j).strip_edges());
  399. }
  400. }
  401. }
  402. String base_dir = dir_access->get_current_dir();
  403. while (!files.empty()) {
  404. bool match = patterns.empty();
  405. String match_str;
  406. for (List<String>::Element *E = patterns.front(); E; E = E->next()) {
  407. if (files.front()->get().matchn(E->get())) {
  408. match_str = E->get();
  409. match = true;
  410. break;
  411. }
  412. }
  413. if (match) {
  414. TreeItem *ti = tree->create_item(root);
  415. ti->set_text(0, files.front()->get());
  416. if (get_icon_func) {
  417. Ref<Texture> icon = get_icon_func(base_dir.plus_file(files.front()->get()));
  418. ti->set_icon(0, icon);
  419. } else {
  420. ti->set_icon(0, file_icon);
  421. }
  422. ti->set_icon_modulate(0, file_color);
  423. if (mode == MODE_OPEN_DIR) {
  424. ti->set_custom_color(0, get_color("files_disabled"));
  425. ti->set_selectable(0, false);
  426. }
  427. Dictionary d;
  428. d["name"] = files.front()->get();
  429. d["dir"] = false;
  430. ti->set_metadata(0, d);
  431. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  432. ti->select(0);
  433. }
  434. }
  435. files.pop_front();
  436. }
  437. if (tree->get_root() && tree->get_root()->get_children() && tree->get_selected() == nullptr) {
  438. tree->get_root()->get_children()->select(0);
  439. }
  440. }
  441. void FileDialog::_filter_selected(int) {
  442. update_file_name();
  443. update_file_list();
  444. }
  445. void FileDialog::update_filters() {
  446. filter->clear();
  447. if (filters.size() > 1) {
  448. String all_filters;
  449. const int max_filters = 5;
  450. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  451. String flt = filters[i].get_slice(";", 0).strip_edges();
  452. if (i > 0) {
  453. all_filters += ", ";
  454. }
  455. all_filters += flt;
  456. }
  457. if (max_filters < filters.size()) {
  458. all_filters += ", ...";
  459. }
  460. filter->add_item(RTR("All Recognized") + " (" + all_filters + ")");
  461. }
  462. for (int i = 0; i < filters.size(); i++) {
  463. String flt = filters[i].get_slice(";", 0).strip_edges();
  464. String desc = filters[i].get_slice(";", 1).strip_edges();
  465. if (desc.length()) {
  466. filter->add_item(String(tr(desc)) + " (" + flt + ")");
  467. } else {
  468. filter->add_item("(" + flt + ")");
  469. }
  470. }
  471. filter->add_item(RTR("All Files (*)"));
  472. }
  473. void FileDialog::clear_filters() {
  474. filters.clear();
  475. update_filters();
  476. invalidate();
  477. }
  478. void FileDialog::add_filter(const String &p_filter) {
  479. ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot.");
  480. filters.push_back(p_filter);
  481. update_filters();
  482. invalidate();
  483. }
  484. void FileDialog::set_filters(const Vector<String> &p_filters) {
  485. filters = p_filters;
  486. update_filters();
  487. invalidate();
  488. }
  489. Vector<String> FileDialog::get_filters() const {
  490. return filters;
  491. }
  492. String FileDialog::get_current_dir() const {
  493. return dir->get_text();
  494. }
  495. String FileDialog::get_current_file() const {
  496. return file->get_text();
  497. }
  498. String FileDialog::get_current_path() const {
  499. return dir->get_text().plus_file(file->get_text());
  500. }
  501. void FileDialog::set_current_dir(const String &p_dir) {
  502. dir_access->change_dir(p_dir);
  503. update_dir();
  504. invalidate();
  505. }
  506. void FileDialog::set_current_file(const String &p_file) {
  507. file->set_text(p_file);
  508. update_dir();
  509. invalidate();
  510. int lp = p_file.find_last(".");
  511. if (lp != -1) {
  512. file->select(0, lp);
  513. if (file->is_inside_tree() && !get_tree()->is_node_being_edited(file)) {
  514. file->grab_focus();
  515. }
  516. }
  517. }
  518. void FileDialog::set_current_path(const String &p_path) {
  519. if (!p_path.size()) {
  520. return;
  521. }
  522. int pos = MAX(p_path.find_last("/"), p_path.find_last("\\"));
  523. if (pos == -1) {
  524. set_current_file(p_path);
  525. } else {
  526. String dir = p_path.substr(0, pos);
  527. String file = p_path.substr(pos + 1, p_path.length());
  528. set_current_dir(dir);
  529. set_current_file(file);
  530. }
  531. }
  532. void FileDialog::set_mode_overrides_title(bool p_override) {
  533. mode_overrides_title = p_override;
  534. }
  535. bool FileDialog::is_mode_overriding_title() const {
  536. return mode_overrides_title;
  537. }
  538. void FileDialog::set_mode(Mode p_mode) {
  539. ERR_FAIL_INDEX((int)p_mode, 5);
  540. mode = p_mode;
  541. switch (mode) {
  542. case MODE_OPEN_FILE:
  543. get_ok()->set_text(RTR("Open"));
  544. if (mode_overrides_title) {
  545. set_title(RTR("Open a File"));
  546. }
  547. makedir->hide();
  548. break;
  549. case MODE_OPEN_FILES:
  550. get_ok()->set_text(RTR("Open"));
  551. if (mode_overrides_title) {
  552. set_title(RTR("Open File(s)"));
  553. }
  554. makedir->hide();
  555. break;
  556. case MODE_OPEN_DIR:
  557. get_ok()->set_text(RTR("Select Current Folder"));
  558. if (mode_overrides_title) {
  559. set_title(RTR("Open a Directory"));
  560. }
  561. makedir->show();
  562. break;
  563. case MODE_OPEN_ANY:
  564. get_ok()->set_text(RTR("Open"));
  565. if (mode_overrides_title) {
  566. set_title(RTR("Open a File or Directory"));
  567. }
  568. makedir->show();
  569. break;
  570. case MODE_SAVE_FILE:
  571. get_ok()->set_text(RTR("Save"));
  572. if (mode_overrides_title) {
  573. set_title(RTR("Save a File"));
  574. }
  575. makedir->show();
  576. break;
  577. }
  578. if (mode == MODE_OPEN_FILES) {
  579. tree->set_select_mode(Tree::SELECT_MULTI);
  580. } else {
  581. tree->set_select_mode(Tree::SELECT_SINGLE);
  582. }
  583. }
  584. FileDialog::Mode FileDialog::get_mode() const {
  585. return mode;
  586. }
  587. void FileDialog::set_access(Access p_access) {
  588. ERR_FAIL_INDEX(p_access, 3);
  589. if (access == p_access) {
  590. return;
  591. }
  592. memdelete(dir_access);
  593. switch (p_access) {
  594. case ACCESS_FILESYSTEM: {
  595. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  596. } break;
  597. case ACCESS_RESOURCES: {
  598. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  599. } break;
  600. case ACCESS_USERDATA: {
  601. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  602. } break;
  603. }
  604. access = p_access;
  605. _update_drives();
  606. invalidate();
  607. update_filters();
  608. update_dir();
  609. }
  610. void FileDialog::invalidate() {
  611. if (is_visible_in_tree()) {
  612. update_file_list();
  613. invalidated = false;
  614. } else {
  615. invalidated = true;
  616. }
  617. }
  618. FileDialog::Access FileDialog::get_access() const {
  619. return access;
  620. }
  621. void FileDialog::_make_dir_confirm() {
  622. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  623. if (err == OK) {
  624. dir_access->change_dir(makedirname->get_text().strip_edges());
  625. invalidate();
  626. update_filters();
  627. update_dir();
  628. } else {
  629. mkdirerr->popup_centered_minsize(Size2(250, 50));
  630. }
  631. makedirname->set_text(""); // reset label
  632. }
  633. void FileDialog::_make_dir() {
  634. makedialog->popup_centered_minsize(Size2(250, 80));
  635. makedirname->grab_focus();
  636. }
  637. void FileDialog::_select_drive(int p_idx) {
  638. String d = drives->get_item_text(p_idx);
  639. dir_access->change_dir(d);
  640. file->set_text("");
  641. invalidate();
  642. update_dir();
  643. }
  644. void FileDialog::_update_drives() {
  645. int dc = dir_access->get_drive_count();
  646. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  647. drives->hide();
  648. } else {
  649. drives->clear();
  650. Node *dp = drives->get_parent();
  651. if (dp) {
  652. dp->remove_child(drives);
  653. }
  654. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  655. dp->add_child(drives);
  656. drives->show();
  657. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  658. drives->add_item(dir_access->get_drive(i));
  659. }
  660. drives->select(dir_access->get_current_drive());
  661. }
  662. }
  663. bool FileDialog::default_show_hidden_files = false;
  664. void FileDialog::_bind_methods() {
  665. ClassDB::bind_method(D_METHOD("_unhandled_input"), &FileDialog::_unhandled_input);
  666. ClassDB::bind_method(D_METHOD("_tree_multi_selected"), &FileDialog::_tree_multi_selected);
  667. ClassDB::bind_method(D_METHOD("_tree_selected"), &FileDialog::_tree_selected);
  668. ClassDB::bind_method(D_METHOD("_tree_item_activated"), &FileDialog::_tree_item_activated);
  669. ClassDB::bind_method(D_METHOD("_dir_entered"), &FileDialog::_dir_entered);
  670. ClassDB::bind_method(D_METHOD("_file_entered"), &FileDialog::_file_entered);
  671. ClassDB::bind_method(D_METHOD("_action_pressed"), &FileDialog::_action_pressed);
  672. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  673. ClassDB::bind_method(D_METHOD("_filter_selected"), &FileDialog::_filter_selected);
  674. ClassDB::bind_method(D_METHOD("_save_confirm_pressed"), &FileDialog::_save_confirm_pressed);
  675. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  676. ClassDB::bind_method(D_METHOD("add_filter", "filter"), &FileDialog::add_filter);
  677. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  678. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  679. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  680. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  681. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  682. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  683. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  684. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  685. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  686. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  687. ClassDB::bind_method(D_METHOD("set_mode", "mode"), &FileDialog::set_mode);
  688. ClassDB::bind_method(D_METHOD("get_mode"), &FileDialog::get_mode);
  689. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  690. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  691. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  692. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  693. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  694. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  695. ClassDB::bind_method(D_METHOD("_select_drive"), &FileDialog::_select_drive);
  696. ClassDB::bind_method(D_METHOD("_make_dir"), &FileDialog::_make_dir);
  697. ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileDialog::_make_dir_confirm);
  698. ClassDB::bind_method(D_METHOD("_update_file_name"), &FileDialog::update_file_name);
  699. ClassDB::bind_method(D_METHOD("_update_file_list"), &FileDialog::update_file_list);
  700. ClassDB::bind_method(D_METHOD("_update_dir"), &FileDialog::update_dir);
  701. ClassDB::bind_method(D_METHOD("_go_up"), &FileDialog::_go_up);
  702. ClassDB::bind_method(D_METHOD("deselect_items"), &FileDialog::deselect_items);
  703. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  704. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  705. ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Open File,Open Files,Open Folder,Open Any,Save"), "set_mode", "get_mode");
  706. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access");
  707. ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  708. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  709. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir"), "set_current_dir", "get_current_dir");
  710. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file"), "set_current_file", "get_current_file");
  711. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path"), "set_current_path", "get_current_path");
  712. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  713. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::POOL_STRING_ARRAY, "paths")));
  714. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  715. BIND_ENUM_CONSTANT(MODE_OPEN_FILE);
  716. BIND_ENUM_CONSTANT(MODE_OPEN_FILES);
  717. BIND_ENUM_CONSTANT(MODE_OPEN_DIR);
  718. BIND_ENUM_CONSTANT(MODE_OPEN_ANY);
  719. BIND_ENUM_CONSTANT(MODE_SAVE_FILE);
  720. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  721. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  722. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  723. }
  724. void FileDialog::set_show_hidden_files(bool p_show) {
  725. show_hidden_files = p_show;
  726. invalidate();
  727. }
  728. bool FileDialog::is_showing_hidden_files() const {
  729. return show_hidden_files;
  730. }
  731. void FileDialog::set_default_show_hidden_files(bool p_show) {
  732. default_show_hidden_files = p_show;
  733. }
  734. FileDialog::FileDialog() {
  735. show_hidden_files = default_show_hidden_files;
  736. mode_overrides_title = true;
  737. VBoxContainer *vbc = memnew(VBoxContainer);
  738. add_child(vbc);
  739. mode = MODE_SAVE_FILE;
  740. set_title(RTR("Save a File"));
  741. HBoxContainer *hbc = memnew(HBoxContainer);
  742. dir_up = memnew(ToolButton);
  743. dir_up->set_tooltip(RTR("Go to parent folder."));
  744. hbc->add_child(dir_up);
  745. dir_up->connect("pressed", this, "_go_up");
  746. hbc->add_child(memnew(Label(RTR("Path:"))));
  747. drives_container = memnew(HBoxContainer);
  748. hbc->add_child(drives_container);
  749. drives = memnew(OptionButton);
  750. drives->connect("item_selected", this, "_select_drive");
  751. hbc->add_child(drives);
  752. dir = memnew(LineEdit);
  753. hbc->add_child(dir);
  754. dir->set_h_size_flags(SIZE_EXPAND_FILL);
  755. refresh = memnew(ToolButton);
  756. refresh->set_tooltip(RTR("Refresh files."));
  757. refresh->connect("pressed", this, "_update_file_list");
  758. hbc->add_child(refresh);
  759. show_hidden = memnew(ToolButton);
  760. show_hidden->set_toggle_mode(true);
  761. show_hidden->set_pressed(is_showing_hidden_files());
  762. show_hidden->set_tooltip(RTR("Toggle the visibility of hidden files."));
  763. show_hidden->connect("toggled", this, "set_show_hidden_files");
  764. hbc->add_child(show_hidden);
  765. shortcuts_container = memnew(HBoxContainer);
  766. hbc->add_child(shortcuts_container);
  767. makedir = memnew(Button);
  768. makedir->set_text(RTR("Create Folder"));
  769. makedir->connect("pressed", this, "_make_dir");
  770. hbc->add_child(makedir);
  771. vbc->add_child(hbc);
  772. tree = memnew(Tree);
  773. tree->set_hide_root(true);
  774. vbc->add_margin_child(RTR("Directories & Files:"), tree, true);
  775. file_box = memnew(HBoxContainer);
  776. file_box->add_child(memnew(Label(RTR("File:"))));
  777. file = memnew(LineEdit);
  778. file->set_stretch_ratio(4);
  779. file->set_h_size_flags(SIZE_EXPAND_FILL);
  780. file_box->add_child(file);
  781. filter = memnew(OptionButton);
  782. filter->set_stretch_ratio(3);
  783. filter->set_h_size_flags(SIZE_EXPAND_FILL);
  784. filter->set_clip_text(true); // too many extensions overflows it
  785. file_box->add_child(filter);
  786. vbc->add_child(file_box);
  787. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  788. access = ACCESS_RESOURCES;
  789. _update_drives();
  790. connect("confirmed", this, "_action_pressed");
  791. tree->connect("multi_selected", this, "_tree_multi_selected", varray(), CONNECT_DEFERRED);
  792. tree->connect("cell_selected", this, "_tree_selected", varray(), CONNECT_DEFERRED);
  793. tree->connect("item_activated", this, "_tree_item_activated", varray());
  794. tree->connect("nothing_selected", this, "deselect_items");
  795. dir->connect("text_entered", this, "_dir_entered");
  796. file->connect("text_entered", this, "_file_entered");
  797. filter->connect("item_selected", this, "_filter_selected");
  798. confirm_save = memnew(ConfirmationDialog);
  799. confirm_save->set_as_toplevel(true);
  800. add_child(confirm_save);
  801. confirm_save->connect("confirmed", this, "_save_confirm_pressed");
  802. makedialog = memnew(ConfirmationDialog);
  803. makedialog->set_title(RTR("Create Folder"));
  804. VBoxContainer *makevb = memnew(VBoxContainer);
  805. makedialog->add_child(makevb);
  806. makedirname = memnew(LineEdit);
  807. makevb->add_margin_child(RTR("Name:"), makedirname);
  808. add_child(makedialog);
  809. makedialog->register_text_enter(makedirname);
  810. makedialog->connect("confirmed", this, "_make_dir_confirm");
  811. mkdirerr = memnew(AcceptDialog);
  812. mkdirerr->set_text(RTR("Could not create folder."));
  813. add_child(mkdirerr);
  814. exterr = memnew(AcceptDialog);
  815. exterr->set_text(RTR("Must use a valid extension."));
  816. add_child(exterr);
  817. update_filters();
  818. update_dir();
  819. set_hide_on_ok(false);
  820. vbox = vbc;
  821. invalidated = true;
  822. if (register_func) {
  823. register_func(this);
  824. }
  825. }
  826. FileDialog::~FileDialog() {
  827. if (unregister_func) {
  828. unregister_func(this);
  829. }
  830. memdelete(dir_access);
  831. }
  832. void LineEditFileChooser::_bind_methods() {
  833. ClassDB::bind_method(D_METHOD("_browse"), &LineEditFileChooser::_browse);
  834. ClassDB::bind_method(D_METHOD("_chosen"), &LineEditFileChooser::_chosen);
  835. ClassDB::bind_method(D_METHOD("get_button"), &LineEditFileChooser::get_button);
  836. ClassDB::bind_method(D_METHOD("get_line_edit"), &LineEditFileChooser::get_line_edit);
  837. ClassDB::bind_method(D_METHOD("get_file_dialog"), &LineEditFileChooser::get_file_dialog);
  838. }
  839. void LineEditFileChooser::_chosen(const String &p_text) {
  840. line_edit->set_text(p_text);
  841. line_edit->emit_signal("text_entered", p_text);
  842. }
  843. void LineEditFileChooser::_browse() {
  844. dialog->popup_centered_ratio();
  845. }
  846. LineEditFileChooser::LineEditFileChooser() {
  847. line_edit = memnew(LineEdit);
  848. add_child(line_edit);
  849. line_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  850. button = memnew(Button);
  851. button->set_text(" .. ");
  852. add_child(button);
  853. button->connect("pressed", this, "_browse");
  854. dialog = memnew(FileDialog);
  855. add_child(dialog);
  856. dialog->connect("file_selected", this, "_chosen");
  857. dialog->connect("dir_selected", this, "_chosen");
  858. dialog->connect("files_selected", this, "_chosen");
  859. }