file_dialog.cpp 30 KB

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