file_dialog.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. /**************************************************************************/
  2. /* file_dialog.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/string/print_string.h"
  33. #include "scene/gui/label.h"
  34. #include "scene/theme/theme_db.h"
  35. FileDialog::GetIconFunc FileDialog::get_icon_func = nullptr;
  36. FileDialog::RegisterFunc FileDialog::register_func = nullptr;
  37. FileDialog::RegisterFunc FileDialog::unregister_func = nullptr;
  38. void FileDialog::popup_file_dialog() {
  39. popup_centered_clamped(Size2i(700, 500), 0.8f);
  40. _focus_file_text();
  41. }
  42. void FileDialog::_focus_file_text() {
  43. int lp = file->get_text().rfind(".");
  44. if (lp != -1) {
  45. file->select(0, lp);
  46. if (file->is_inside_tree() && !get_tree()->is_node_being_edited(file)) {
  47. file->grab_focus();
  48. }
  49. }
  50. }
  51. void FileDialog::popup(const Rect2i &p_rect) {
  52. if (access == ACCESS_FILESYSTEM && DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG) && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  53. DisplayServer::get_singleton()->file_dialog_show(get_title(), dir->get_text(), file->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), filters, callable_mp(this, &FileDialog::_native_dialog_cb));
  54. } else {
  55. ConfirmationDialog::popup(p_rect);
  56. }
  57. }
  58. void FileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_files) {
  59. if (p_ok) {
  60. if (p_files.size() > 0) {
  61. String f = p_files[0];
  62. if (mode == FILE_MODE_OPEN_FILES) {
  63. emit_signal(SNAME("files_selected"), p_files);
  64. } else {
  65. if (mode == FILE_MODE_SAVE_FILE) {
  66. emit_signal(SNAME("file_selected"), f);
  67. } else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  68. emit_signal(SNAME("file_selected"), f);
  69. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  70. emit_signal(SNAME("dir_selected"), f);
  71. }
  72. }
  73. file->set_text(f);
  74. dir->set_text(f.get_base_dir());
  75. }
  76. } else {
  77. file->set_text("");
  78. emit_signal(SNAME("canceled"));
  79. }
  80. }
  81. VBoxContainer *FileDialog::get_vbox() {
  82. return vbox;
  83. }
  84. void FileDialog::_notification(int p_what) {
  85. switch (p_what) {
  86. case NOTIFICATION_VISIBILITY_CHANGED: {
  87. if (!is_visible()) {
  88. set_process_shortcut_input(false);
  89. }
  90. invalidate(); // Put it here to preview in the editor.
  91. } break;
  92. case NOTIFICATION_THEME_CHANGED: {
  93. dir_up->set_icon(theme_cache.parent_folder);
  94. if (vbox->is_layout_rtl()) {
  95. dir_prev->set_icon(theme_cache.forward_folder);
  96. dir_next->set_icon(theme_cache.back_folder);
  97. } else {
  98. dir_prev->set_icon(theme_cache.back_folder);
  99. dir_next->set_icon(theme_cache.forward_folder);
  100. }
  101. refresh->set_icon(theme_cache.reload);
  102. show_hidden->set_icon(theme_cache.toggle_hidden);
  103. dir_up->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  104. dir_up->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  105. dir_up->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  106. dir_up->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  107. dir_prev->add_theme_color_override("icon_color_normal", theme_cache.icon_normal_color);
  108. dir_prev->add_theme_color_override("icon_color_hover", theme_cache.icon_hover_color);
  109. dir_prev->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  110. dir_prev->add_theme_color_override("icon_color_pressed", theme_cache.icon_pressed_color);
  111. dir_next->add_theme_color_override("icon_color_normal", theme_cache.icon_normal_color);
  112. dir_next->add_theme_color_override("icon_color_hover", theme_cache.icon_hover_color);
  113. dir_next->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  114. dir_next->add_theme_color_override("icon_color_pressed", theme_cache.icon_pressed_color);
  115. refresh->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  116. refresh->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  117. refresh->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  118. refresh->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  119. show_hidden->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  120. show_hidden->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  121. show_hidden->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  122. show_hidden->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  123. invalidate();
  124. } break;
  125. case NOTIFICATION_TRANSLATION_CHANGED: {
  126. update_filters();
  127. } break;
  128. }
  129. }
  130. void FileDialog::shortcut_input(const Ref<InputEvent> &p_event) {
  131. ERR_FAIL_COND(p_event.is_null());
  132. Ref<InputEventKey> k = p_event;
  133. if (k.is_valid() && has_focus()) {
  134. if (k->is_pressed()) {
  135. bool handled = true;
  136. switch (k->get_keycode()) {
  137. case Key::H: {
  138. if (k->is_command_or_control_pressed()) {
  139. set_show_hidden_files(!show_hidden_files);
  140. } else {
  141. handled = false;
  142. }
  143. } break;
  144. case Key::F5: {
  145. invalidate();
  146. } break;
  147. case Key::BACKSPACE: {
  148. _dir_submitted("..");
  149. } break;
  150. default: {
  151. handled = false;
  152. }
  153. }
  154. if (handled) {
  155. set_input_as_handled();
  156. }
  157. }
  158. }
  159. }
  160. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  161. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  162. }
  163. Vector<String> FileDialog::get_selected_files() const {
  164. Vector<String> list;
  165. TreeItem *item = tree->get_root();
  166. item = tree->get_next_selected(item);
  167. while (item) {
  168. list.push_back(dir_access->get_current_dir().path_join(item->get_text(0)));
  169. item = tree->get_next_selected(item);
  170. }
  171. return list;
  172. }
  173. void FileDialog::update_dir() {
  174. if (root_prefix.is_empty()) {
  175. dir->set_text(dir_access->get_current_dir(false));
  176. } else {
  177. dir->set_text(dir_access->get_current_dir(false).trim_prefix(root_prefix).trim_prefix("/"));
  178. }
  179. if (drives->is_visible()) {
  180. if (dir_access->get_current_dir().is_network_share_path()) {
  181. _update_drives(false);
  182. drives->add_item(RTR("Network"));
  183. drives->set_item_disabled(-1, true);
  184. drives->select(drives->get_item_count() - 1);
  185. } else {
  186. drives->select(dir_access->get_current_drive());
  187. }
  188. }
  189. // Deselect any item, to make "Select Current Folder" button text by default.
  190. deselect_all();
  191. }
  192. void FileDialog::_dir_submitted(String p_dir) {
  193. _change_dir(root_prefix.path_join(p_dir));
  194. file->set_text("");
  195. _push_history();
  196. }
  197. void FileDialog::_file_submitted(const String &p_file) {
  198. _action_pressed();
  199. }
  200. void FileDialog::_save_confirm_pressed() {
  201. String f = dir_access->get_current_dir().path_join(file->get_text());
  202. emit_signal(SNAME("file_selected"), f);
  203. hide();
  204. }
  205. void FileDialog::_post_popup() {
  206. ConfirmationDialog::_post_popup();
  207. if (mode == FILE_MODE_SAVE_FILE) {
  208. file->grab_focus();
  209. } else {
  210. tree->grab_focus();
  211. }
  212. set_process_shortcut_input(true);
  213. // For open dir mode, deselect all items on file dialog open.
  214. if (mode == FILE_MODE_OPEN_DIR) {
  215. deselect_all();
  216. file_box->set_visible(false);
  217. } else {
  218. file_box->set_visible(true);
  219. }
  220. local_history.clear();
  221. local_history_pos = -1;
  222. _push_history();
  223. }
  224. void FileDialog::_push_history() {
  225. local_history.resize(local_history_pos + 1);
  226. String new_path = dir_access->get_current_dir();
  227. if (local_history.size() == 0 || new_path != local_history[local_history_pos]) {
  228. local_history.push_back(new_path);
  229. local_history_pos++;
  230. dir_prev->set_disabled(local_history_pos == 0);
  231. dir_next->set_disabled(true);
  232. }
  233. }
  234. void FileDialog::_action_pressed() {
  235. if (mode == FILE_MODE_OPEN_FILES) {
  236. TreeItem *ti = tree->get_next_selected(nullptr);
  237. String fbase = dir_access->get_current_dir();
  238. Vector<String> files;
  239. while (ti) {
  240. files.push_back(fbase.path_join(ti->get_text(0)));
  241. ti = tree->get_next_selected(ti);
  242. }
  243. if (files.size()) {
  244. emit_signal(SNAME("files_selected"), files);
  245. hide();
  246. }
  247. return;
  248. }
  249. String file_text = file->get_text();
  250. String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text);
  251. if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  252. emit_signal(SNAME("file_selected"), f);
  253. hide();
  254. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  255. String path = dir_access->get_current_dir();
  256. path = path.replace("\\", "/");
  257. TreeItem *item = tree->get_selected();
  258. if (item) {
  259. Dictionary d = item->get_metadata(0);
  260. if (d["dir"] && d["name"] != "..") {
  261. path = path.path_join(d["name"]);
  262. }
  263. }
  264. emit_signal(SNAME("dir_selected"), path);
  265. hide();
  266. }
  267. if (mode == FILE_MODE_SAVE_FILE) {
  268. bool valid = false;
  269. if (filter->get_selected() == filter->get_item_count() - 1) {
  270. valid = true; // match none
  271. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  272. // match all filters
  273. for (int i = 0; i < filters.size(); i++) {
  274. String flt = filters[i].get_slice(";", 0);
  275. for (int j = 0; j < flt.get_slice_count(","); j++) {
  276. String str = flt.get_slice(",", j).strip_edges();
  277. if (f.match(str)) {
  278. valid = true;
  279. break;
  280. }
  281. }
  282. if (valid) {
  283. break;
  284. }
  285. }
  286. } else {
  287. int idx = filter->get_selected();
  288. if (filters.size() > 1) {
  289. idx--;
  290. }
  291. if (idx >= 0 && idx < filters.size()) {
  292. String flt = filters[idx].get_slice(";", 0);
  293. int filterSliceCount = flt.get_slice_count(",");
  294. for (int j = 0; j < filterSliceCount; j++) {
  295. String str = (flt.get_slice(",", j).strip_edges());
  296. if (f.match(str)) {
  297. valid = true;
  298. break;
  299. }
  300. }
  301. if (!valid && filterSliceCount > 0) {
  302. String str = (flt.get_slice(",", 0).strip_edges());
  303. f += str.substr(1, str.length() - 1);
  304. file->set_text(f.get_file());
  305. valid = true;
  306. }
  307. } else {
  308. valid = true;
  309. }
  310. }
  311. String file_name = file_text.strip_edges().get_file();
  312. if (!valid || file_name.is_empty()) {
  313. exterr->popup_centered(Size2(250, 80));
  314. return;
  315. }
  316. if (dir_access->file_exists(f)) {
  317. confirm_save->set_text(vformat(RTR("File \"%s\" already exists.\nDo you want to overwrite it?"), f));
  318. confirm_save->popup_centered(Size2(250, 80));
  319. } else {
  320. emit_signal(SNAME("file_selected"), f);
  321. hide();
  322. }
  323. }
  324. }
  325. void FileDialog::_cancel_pressed() {
  326. file->set_text("");
  327. invalidate();
  328. hide();
  329. }
  330. bool FileDialog::_is_open_should_be_disabled() {
  331. if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_SAVE_FILE) {
  332. return false;
  333. }
  334. TreeItem *ti = tree->get_next_selected(tree->get_root());
  335. while (ti) {
  336. TreeItem *prev_ti = ti;
  337. ti = tree->get_next_selected(tree->get_root());
  338. if (ti == prev_ti) {
  339. break;
  340. }
  341. }
  342. // We have something that we can't select?
  343. if (!ti) {
  344. return mode != FILE_MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  345. }
  346. Dictionary d = ti->get_metadata(0);
  347. // Opening a file, but selected a folder? Forbidden.
  348. return ((mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  349. (mode == FILE_MODE_OPEN_DIR && !d["dir"]);
  350. }
  351. void FileDialog::_go_up() {
  352. _change_dir("..");
  353. _push_history();
  354. }
  355. void FileDialog::_go_back() {
  356. if (local_history_pos <= 0) {
  357. return;
  358. }
  359. local_history_pos--;
  360. _change_dir(local_history[local_history_pos]);
  361. dir_prev->set_disabled(local_history_pos == 0);
  362. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  363. }
  364. void FileDialog::_go_forward() {
  365. if (local_history_pos >= local_history.size() - 1) {
  366. return;
  367. }
  368. local_history_pos++;
  369. _change_dir(local_history[local_history_pos]);
  370. dir_prev->set_disabled(local_history_pos == 0);
  371. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  372. }
  373. void FileDialog::deselect_all() {
  374. // Clear currently selected items in file manager.
  375. tree->deselect_all();
  376. // And change get_ok title.
  377. if (!tree->is_anything_selected()) {
  378. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  379. switch (mode) {
  380. case FILE_MODE_OPEN_FILE:
  381. case FILE_MODE_OPEN_FILES:
  382. set_ok_button_text(RTR("Open"));
  383. break;
  384. case FILE_MODE_OPEN_DIR:
  385. set_ok_button_text(RTR("Select Current Folder"));
  386. break;
  387. case FILE_MODE_OPEN_ANY:
  388. case FILE_MODE_SAVE_FILE:
  389. // FIXME: Implement, or refactor to avoid duplication with set_mode
  390. break;
  391. }
  392. }
  393. }
  394. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  395. _tree_selected();
  396. }
  397. void FileDialog::_tree_selected() {
  398. TreeItem *ti = tree->get_selected();
  399. if (!ti) {
  400. return;
  401. }
  402. Dictionary d = ti->get_metadata(0);
  403. if (!d["dir"]) {
  404. file->set_text(d["name"]);
  405. } else if (mode == FILE_MODE_OPEN_DIR) {
  406. set_ok_button_text(RTR("Select This Folder"));
  407. }
  408. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  409. }
  410. void FileDialog::_tree_item_activated() {
  411. TreeItem *ti = tree->get_selected();
  412. if (!ti) {
  413. return;
  414. }
  415. Dictionary d = ti->get_metadata(0);
  416. if (d["dir"]) {
  417. _change_dir(d["name"]);
  418. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  419. file->set_text("");
  420. }
  421. _push_history();
  422. } else {
  423. _action_pressed();
  424. }
  425. }
  426. void FileDialog::update_file_name() {
  427. int idx = filter->get_selected() - 1;
  428. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  429. if (idx == -1) {
  430. idx += 1;
  431. }
  432. String filter_str = filters[idx];
  433. String file_str = file->get_text();
  434. String base_name = file_str.get_basename();
  435. Vector<String> filter_substr = filter_str.split(";");
  436. if (filter_substr.size() >= 2) {
  437. file_str = base_name + "." + filter_substr[0].strip_edges().get_extension().to_lower();
  438. } else {
  439. file_str = base_name + "." + filter_str.strip_edges().get_extension().to_lower();
  440. }
  441. file->set_text(file_str);
  442. }
  443. }
  444. void FileDialog::update_file_list() {
  445. tree->clear();
  446. // Scroll back to the top after opening a directory
  447. tree->get_vscroll_bar()->set_value(0);
  448. dir_access->list_dir_begin();
  449. if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) {
  450. message->hide();
  451. } else {
  452. message->set_text(RTR("You don't have permission to access contents of this folder."));
  453. message->show();
  454. }
  455. TreeItem *root = tree->create_item();
  456. List<String> files;
  457. List<String> dirs;
  458. bool is_hidden;
  459. String item = dir_access->get_next();
  460. while (!item.is_empty()) {
  461. if (item == "." || item == "..") {
  462. item = dir_access->get_next();
  463. continue;
  464. }
  465. is_hidden = dir_access->current_is_hidden();
  466. if (show_hidden_files || !is_hidden) {
  467. if (!dir_access->current_is_dir()) {
  468. files.push_back(item);
  469. } else {
  470. dirs.push_back(item);
  471. }
  472. }
  473. item = dir_access->get_next();
  474. }
  475. dirs.sort_custom<NaturalNoCaseComparator>();
  476. files.sort_custom<NaturalNoCaseComparator>();
  477. while (!dirs.is_empty()) {
  478. String &dir_name = dirs.front()->get();
  479. TreeItem *ti = tree->create_item(root);
  480. ti->set_text(0, dir_name);
  481. ti->set_icon(0, theme_cache.folder);
  482. ti->set_icon_modulate(0, theme_cache.folder_icon_color);
  483. Dictionary d;
  484. d["name"] = dir_name;
  485. d["dir"] = true;
  486. ti->set_metadata(0, d);
  487. dirs.pop_front();
  488. }
  489. List<String> patterns;
  490. // build filter
  491. if (filter->get_selected() == filter->get_item_count() - 1) {
  492. // match all
  493. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  494. // match all filters
  495. for (int i = 0; i < filters.size(); i++) {
  496. String f = filters[i].get_slice(";", 0);
  497. for (int j = 0; j < f.get_slice_count(","); j++) {
  498. patterns.push_back(f.get_slice(",", j).strip_edges());
  499. }
  500. }
  501. } else {
  502. int idx = filter->get_selected();
  503. if (filters.size() > 1) {
  504. idx--;
  505. }
  506. if (idx >= 0 && idx < filters.size()) {
  507. String f = filters[idx].get_slice(";", 0);
  508. for (int j = 0; j < f.get_slice_count(","); j++) {
  509. patterns.push_back(f.get_slice(",", j).strip_edges());
  510. }
  511. }
  512. }
  513. String base_dir = dir_access->get_current_dir();
  514. while (!files.is_empty()) {
  515. bool match = patterns.is_empty();
  516. String match_str;
  517. for (const String &E : patterns) {
  518. if (files.front()->get().matchn(E)) {
  519. match_str = E;
  520. match = true;
  521. break;
  522. }
  523. }
  524. if (match) {
  525. TreeItem *ti = tree->create_item(root);
  526. ti->set_text(0, files.front()->get());
  527. if (get_icon_func) {
  528. Ref<Texture2D> icon = get_icon_func(base_dir.path_join(files.front()->get()));
  529. ti->set_icon(0, icon);
  530. } else {
  531. ti->set_icon(0, theme_cache.file);
  532. }
  533. ti->set_icon_modulate(0, theme_cache.file_icon_color);
  534. if (mode == FILE_MODE_OPEN_DIR) {
  535. ti->set_custom_color(0, theme_cache.file_disabled_color);
  536. ti->set_selectable(0, false);
  537. }
  538. Dictionary d;
  539. d["name"] = files.front()->get();
  540. d["dir"] = false;
  541. ti->set_metadata(0, d);
  542. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  543. ti->select(0);
  544. }
  545. }
  546. files.pop_front();
  547. }
  548. if (mode != FILE_MODE_SAVE_FILE) {
  549. // Select the first file from list if nothing is selected.
  550. if (tree->get_root() && tree->get_root()->get_first_child() && tree->get_selected() == nullptr) {
  551. tree->get_root()->get_first_child()->select(0);
  552. }
  553. }
  554. }
  555. void FileDialog::_filter_selected(int) {
  556. update_file_name();
  557. update_file_list();
  558. }
  559. void FileDialog::update_filters() {
  560. filter->clear();
  561. if (filters.size() > 1) {
  562. String all_filters;
  563. const int max_filters = 5;
  564. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  565. String flt = filters[i].get_slice(";", 0).strip_edges();
  566. if (i > 0) {
  567. all_filters += ", ";
  568. }
  569. all_filters += flt;
  570. }
  571. if (max_filters < filters.size()) {
  572. all_filters += ", ...";
  573. }
  574. filter->add_item(RTR("All Recognized") + " (" + all_filters + ")");
  575. }
  576. for (int i = 0; i < filters.size(); i++) {
  577. String flt = filters[i].get_slice(";", 0).strip_edges();
  578. String desc = filters[i].get_slice(";", 1).strip_edges();
  579. if (desc.length()) {
  580. filter->add_item(String(tr(desc)) + " (" + flt + ")");
  581. } else {
  582. filter->add_item("(" + flt + ")");
  583. }
  584. }
  585. filter->add_item(RTR("All Files") + " (*)");
  586. }
  587. void FileDialog::clear_filters() {
  588. filters.clear();
  589. update_filters();
  590. invalidate();
  591. }
  592. void FileDialog::add_filter(const String &p_filter, const String &p_description) {
  593. ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot.");
  594. if (p_description.is_empty()) {
  595. filters.push_back(p_filter);
  596. } else {
  597. filters.push_back(vformat("%s ; %s", p_filter, p_description));
  598. }
  599. update_filters();
  600. invalidate();
  601. }
  602. void FileDialog::set_filters(const Vector<String> &p_filters) {
  603. if (filters == p_filters) {
  604. return;
  605. }
  606. filters = p_filters;
  607. update_filters();
  608. invalidate();
  609. }
  610. Vector<String> FileDialog::get_filters() const {
  611. return filters;
  612. }
  613. String FileDialog::get_current_dir() const {
  614. return dir->get_text();
  615. }
  616. String FileDialog::get_current_file() const {
  617. return file->get_text();
  618. }
  619. String FileDialog::get_current_path() const {
  620. return dir->get_text().path_join(file->get_text());
  621. }
  622. void FileDialog::set_current_dir(const String &p_dir) {
  623. _change_dir(p_dir);
  624. _push_history();
  625. }
  626. void FileDialog::set_current_file(const String &p_file) {
  627. if (file->get_text() == p_file) {
  628. return;
  629. }
  630. file->set_text(p_file);
  631. update_dir();
  632. invalidate();
  633. _focus_file_text();
  634. }
  635. void FileDialog::set_current_path(const String &p_path) {
  636. if (!p_path.size()) {
  637. return;
  638. }
  639. int pos = MAX(p_path.rfind("/"), p_path.rfind("\\"));
  640. if (pos == -1) {
  641. set_current_file(p_path);
  642. } else {
  643. String path_dir = p_path.substr(0, pos);
  644. String path_file = p_path.substr(pos + 1, p_path.length());
  645. set_current_dir(path_dir);
  646. set_current_file(path_file);
  647. }
  648. }
  649. void FileDialog::set_root_subfolder(const String &p_root) {
  650. root_subfolder = p_root;
  651. ERR_FAIL_COND_MSG(!dir_access->dir_exists(p_root), "root_subfolder must be an existing sub-directory.");
  652. local_history.clear();
  653. local_history_pos = -1;
  654. dir_access->change_dir(root_subfolder);
  655. if (root_subfolder.is_empty()) {
  656. root_prefix = "";
  657. } else {
  658. root_prefix = dir_access->get_current_dir();
  659. }
  660. invalidate();
  661. update_dir();
  662. }
  663. String FileDialog::get_root_subfolder() const {
  664. return root_subfolder;
  665. }
  666. void FileDialog::set_mode_overrides_title(bool p_override) {
  667. mode_overrides_title = p_override;
  668. }
  669. bool FileDialog::is_mode_overriding_title() const {
  670. return mode_overrides_title;
  671. }
  672. void FileDialog::set_file_mode(FileMode p_mode) {
  673. ERR_FAIL_INDEX((int)p_mode, 5);
  674. if (mode == p_mode) {
  675. return;
  676. }
  677. mode = p_mode;
  678. switch (mode) {
  679. case FILE_MODE_OPEN_FILE:
  680. set_ok_button_text(RTR("Open"));
  681. if (mode_overrides_title) {
  682. set_title(TTRC("Open a File"));
  683. }
  684. makedir->hide();
  685. break;
  686. case FILE_MODE_OPEN_FILES:
  687. set_ok_button_text(RTR("Open"));
  688. if (mode_overrides_title) {
  689. set_title(TTRC("Open File(s)"));
  690. }
  691. makedir->hide();
  692. break;
  693. case FILE_MODE_OPEN_DIR:
  694. set_ok_button_text(RTR("Select Current Folder"));
  695. if (mode_overrides_title) {
  696. set_title(TTRC("Open a Directory"));
  697. }
  698. makedir->show();
  699. break;
  700. case FILE_MODE_OPEN_ANY:
  701. set_ok_button_text(RTR("Open"));
  702. if (mode_overrides_title) {
  703. set_title(TTRC("Open a File or Directory"));
  704. }
  705. makedir->show();
  706. break;
  707. case FILE_MODE_SAVE_FILE:
  708. set_ok_button_text(RTR("Save"));
  709. if (mode_overrides_title) {
  710. set_title(TTRC("Save a File"));
  711. }
  712. makedir->show();
  713. break;
  714. }
  715. if (mode == FILE_MODE_OPEN_FILES) {
  716. tree->set_select_mode(Tree::SELECT_MULTI);
  717. } else {
  718. tree->set_select_mode(Tree::SELECT_SINGLE);
  719. }
  720. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  721. }
  722. FileDialog::FileMode FileDialog::get_file_mode() const {
  723. return mode;
  724. }
  725. void FileDialog::set_access(Access p_access) {
  726. ERR_FAIL_INDEX(p_access, 3);
  727. if (access == p_access) {
  728. return;
  729. }
  730. switch (p_access) {
  731. case ACCESS_FILESYSTEM: {
  732. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  733. } break;
  734. case ACCESS_RESOURCES: {
  735. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  736. } break;
  737. case ACCESS_USERDATA: {
  738. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  739. } break;
  740. }
  741. access = p_access;
  742. root_prefix = "";
  743. root_subfolder = "";
  744. _update_drives();
  745. invalidate();
  746. update_filters();
  747. update_dir();
  748. }
  749. void FileDialog::invalidate() {
  750. if (!is_visible() || is_invalidating) {
  751. return;
  752. }
  753. is_invalidating = true;
  754. callable_mp(this, &FileDialog::_invalidate).call_deferred();
  755. }
  756. void FileDialog::_invalidate() {
  757. if (!is_invalidating) {
  758. return;
  759. }
  760. update_file_list();
  761. is_invalidating = false;
  762. }
  763. FileDialog::Access FileDialog::get_access() const {
  764. return access;
  765. }
  766. void FileDialog::_make_dir_confirm() {
  767. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  768. if (err == OK) {
  769. _change_dir(makedirname->get_text().strip_edges());
  770. update_filters();
  771. _push_history();
  772. } else {
  773. mkdirerr->popup_centered(Size2(250, 50));
  774. }
  775. makedirname->set_text(""); // reset label
  776. }
  777. void FileDialog::_make_dir() {
  778. makedialog->popup_centered(Size2(250, 80));
  779. makedirname->grab_focus();
  780. }
  781. void FileDialog::_select_drive(int p_idx) {
  782. String d = drives->get_item_text(p_idx);
  783. _change_dir(d);
  784. file->set_text("");
  785. _push_history();
  786. }
  787. void FileDialog::_change_dir(const String &p_new_dir) {
  788. if (root_prefix.is_empty()) {
  789. dir_access->change_dir(p_new_dir);
  790. } else {
  791. String old_dir = dir_access->get_current_dir();
  792. dir_access->change_dir(p_new_dir);
  793. if (!dir_access->get_current_dir(false).begins_with(root_prefix)) {
  794. dir_access->change_dir(old_dir);
  795. return;
  796. }
  797. }
  798. invalidate();
  799. update_dir();
  800. }
  801. void FileDialog::_update_drives(bool p_select) {
  802. int dc = dir_access->get_drive_count();
  803. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  804. drives->hide();
  805. } else {
  806. drives->clear();
  807. Node *dp = drives->get_parent();
  808. if (dp) {
  809. dp->remove_child(drives);
  810. }
  811. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  812. dp->add_child(drives);
  813. drives->show();
  814. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  815. drives->add_item(dir_access->get_drive(i));
  816. }
  817. if (p_select) {
  818. drives->select(dir_access->get_current_drive());
  819. }
  820. }
  821. }
  822. bool FileDialog::default_show_hidden_files = false;
  823. void FileDialog::_bind_methods() {
  824. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  825. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  826. ClassDB::bind_method(D_METHOD("add_filter", "filter", "description"), &FileDialog::add_filter, DEFVAL(""));
  827. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  828. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  829. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  830. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  831. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  832. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  833. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  834. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  835. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  836. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  837. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  838. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  839. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  840. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  841. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  842. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  843. ClassDB::bind_method(D_METHOD("set_root_subfolder", "dir"), &FileDialog::set_root_subfolder);
  844. ClassDB::bind_method(D_METHOD("get_root_subfolder"), &FileDialog::get_root_subfolder);
  845. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  846. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  847. ClassDB::bind_method(D_METHOD("set_use_native_dialog", "native"), &FileDialog::set_use_native_dialog);
  848. ClassDB::bind_method(D_METHOD("get_use_native_dialog"), &FileDialog::get_use_native_dialog);
  849. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  850. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  851. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  852. ADD_PROPERTY(PropertyInfo(Variant::INT, "file_mode", PROPERTY_HINT_ENUM, "Open File,Open Files,Open Folder,Open Any,Save"), "set_file_mode", "get_file_mode");
  853. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User Data,File System"), "set_access", "get_access");
  854. ADD_PROPERTY(PropertyInfo(Variant::STRING, "root_subfolder"), "set_root_subfolder", "get_root_subfolder");
  855. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  856. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  857. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_native_dialog"), "set_use_native_dialog", "get_use_native_dialog");
  858. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir", PROPERTY_HINT_DIR, "", PROPERTY_USAGE_NONE), "set_current_dir", "get_current_dir");
  859. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file", PROPERTY_HINT_FILE, "*", PROPERTY_USAGE_NONE), "set_current_file", "get_current_file");
  860. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_path", "get_current_path");
  861. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  862. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  863. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  864. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  865. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  866. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  867. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  868. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  869. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  870. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  871. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  872. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, parent_folder);
  873. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, forward_folder);
  874. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, back_folder);
  875. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, reload);
  876. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_hidden);
  877. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, folder);
  878. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, file);
  879. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, folder_icon_color);
  880. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_icon_color);
  881. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_disabled_color);
  882. // TODO: Define own colors?
  883. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_normal_color, "font_color", "Button");
  884. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_hover_color, "font_hover_color", "Button");
  885. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_focus_color, "font_focus_color", "Button");
  886. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_pressed_color, "font_pressed_color", "Button");
  887. }
  888. void FileDialog::set_show_hidden_files(bool p_show) {
  889. if (show_hidden_files == p_show) {
  890. return;
  891. }
  892. show_hidden_files = p_show;
  893. invalidate();
  894. }
  895. bool FileDialog::is_showing_hidden_files() const {
  896. return show_hidden_files;
  897. }
  898. void FileDialog::set_default_show_hidden_files(bool p_show) {
  899. default_show_hidden_files = p_show;
  900. }
  901. void FileDialog::set_use_native_dialog(bool p_native) {
  902. use_native_dialog = p_native;
  903. }
  904. bool FileDialog::get_use_native_dialog() const {
  905. return use_native_dialog;
  906. }
  907. FileDialog::FileDialog() {
  908. show_hidden_files = default_show_hidden_files;
  909. vbox = memnew(VBoxContainer);
  910. add_child(vbox, false, INTERNAL_MODE_FRONT);
  911. mode = FILE_MODE_SAVE_FILE;
  912. set_title(TTRC("Save a File"));
  913. HBoxContainer *hbc = memnew(HBoxContainer);
  914. dir_prev = memnew(Button);
  915. dir_prev->set_flat(true);
  916. dir_prev->set_tooltip_text(RTR("Go to previous folder."));
  917. dir_next = memnew(Button);
  918. dir_next->set_flat(true);
  919. dir_next->set_tooltip_text(RTR("Go to next folder."));
  920. dir_up = memnew(Button);
  921. dir_up->set_flat(true);
  922. dir_up->set_tooltip_text(RTR("Go to parent folder."));
  923. hbc->add_child(dir_prev);
  924. hbc->add_child(dir_next);
  925. hbc->add_child(dir_up);
  926. dir_prev->connect("pressed", callable_mp(this, &FileDialog::_go_back));
  927. dir_next->connect("pressed", callable_mp(this, &FileDialog::_go_forward));
  928. dir_up->connect("pressed", callable_mp(this, &FileDialog::_go_up));
  929. hbc->add_child(memnew(Label(RTR("Path:"))));
  930. drives_container = memnew(HBoxContainer);
  931. hbc->add_child(drives_container);
  932. drives = memnew(OptionButton);
  933. drives->connect("item_selected", callable_mp(this, &FileDialog::_select_drive));
  934. hbc->add_child(drives);
  935. dir = memnew(LineEdit);
  936. dir->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  937. hbc->add_child(dir);
  938. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  939. refresh = memnew(Button);
  940. refresh->set_flat(true);
  941. refresh->set_tooltip_text(RTR("Refresh files."));
  942. refresh->connect("pressed", callable_mp(this, &FileDialog::update_file_list));
  943. hbc->add_child(refresh);
  944. show_hidden = memnew(Button);
  945. show_hidden->set_flat(true);
  946. show_hidden->set_toggle_mode(true);
  947. show_hidden->set_pressed(is_showing_hidden_files());
  948. show_hidden->set_tooltip_text(RTR("Toggle the visibility of hidden files."));
  949. show_hidden->connect("toggled", callable_mp(this, &FileDialog::set_show_hidden_files));
  950. hbc->add_child(show_hidden);
  951. shortcuts_container = memnew(HBoxContainer);
  952. hbc->add_child(shortcuts_container);
  953. makedir = memnew(Button);
  954. makedir->set_text(RTR("Create Folder"));
  955. makedir->connect("pressed", callable_mp(this, &FileDialog::_make_dir));
  956. hbc->add_child(makedir);
  957. vbox->add_child(hbc);
  958. tree = memnew(Tree);
  959. tree->set_hide_root(true);
  960. vbox->add_margin_child(RTR("Directories & Files:"), tree, true);
  961. message = memnew(Label);
  962. message->hide();
  963. message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  964. message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  965. message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  966. tree->add_child(message);
  967. file_box = memnew(HBoxContainer);
  968. file_box->add_child(memnew(Label(RTR("File:"))));
  969. file = memnew(LineEdit);
  970. file->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  971. file->set_stretch_ratio(4);
  972. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  973. file_box->add_child(file);
  974. filter = memnew(OptionButton);
  975. filter->set_stretch_ratio(3);
  976. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  977. filter->set_clip_text(true); // too many extensions overflows it
  978. file_box->add_child(filter);
  979. vbox->add_child(file_box);
  980. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  981. _update_drives();
  982. connect("confirmed", callable_mp(this, &FileDialog::_action_pressed));
  983. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), CONNECT_DEFERRED);
  984. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), CONNECT_DEFERRED);
  985. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated));
  986. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  987. dir->connect("text_submitted", callable_mp(this, &FileDialog::_dir_submitted));
  988. file->connect("text_submitted", callable_mp(this, &FileDialog::_file_submitted));
  989. filter->connect("item_selected", callable_mp(this, &FileDialog::_filter_selected));
  990. confirm_save = memnew(ConfirmationDialog);
  991. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  992. confirm_save->connect("confirmed", callable_mp(this, &FileDialog::_save_confirm_pressed));
  993. makedialog = memnew(ConfirmationDialog);
  994. makedialog->set_title(RTR("Create Folder"));
  995. VBoxContainer *makevb = memnew(VBoxContainer);
  996. makedialog->add_child(makevb);
  997. makedirname = memnew(LineEdit);
  998. makedirname->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  999. makevb->add_margin_child(RTR("Name:"), makedirname);
  1000. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  1001. makedialog->register_text_enter(makedirname);
  1002. makedialog->connect("confirmed", callable_mp(this, &FileDialog::_make_dir_confirm));
  1003. mkdirerr = memnew(AcceptDialog);
  1004. mkdirerr->set_text(RTR("Could not create folder."));
  1005. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  1006. exterr = memnew(AcceptDialog);
  1007. exterr->set_text(RTR("Invalid extension, or empty filename."));
  1008. add_child(exterr, false, INTERNAL_MODE_FRONT);
  1009. update_filters();
  1010. update_dir();
  1011. set_hide_on_ok(false);
  1012. if (register_func) {
  1013. register_func(this);
  1014. }
  1015. }
  1016. FileDialog::~FileDialog() {
  1017. if (unregister_func) {
  1018. unregister_func(this);
  1019. }
  1020. }