file_dialog.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  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/config/project_settings.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/string/print_string.h"
  34. #include "scene/gui/check_box.h"
  35. #include "scene/gui/grid_container.h"
  36. #include "scene/gui/label.h"
  37. #include "scene/gui/option_button.h"
  38. #include "scene/theme/theme_db.h"
  39. FileDialog::GetIconFunc FileDialog::get_icon_func = nullptr;
  40. FileDialog::RegisterFunc FileDialog::register_func = nullptr;
  41. FileDialog::RegisterFunc FileDialog::unregister_func = nullptr;
  42. void FileDialog::popup_file_dialog() {
  43. popup_centered_clamped(Size2i(700, 500), 0.8f);
  44. _focus_file_text();
  45. }
  46. void FileDialog::_focus_file_text() {
  47. int lp = file->get_text().rfind_char('.');
  48. if (lp != -1) {
  49. file->select(0, lp);
  50. if (file->is_inside_tree() && !is_part_of_edited_scene()) {
  51. file->grab_focus();
  52. }
  53. }
  54. }
  55. void FileDialog::_native_popup() {
  56. // Show native dialog directly.
  57. String root;
  58. if (!root_prefix.is_empty()) {
  59. root = ProjectSettings::get_singleton()->globalize_path(root_prefix);
  60. } else if (access == ACCESS_RESOURCES) {
  61. root = ProjectSettings::get_singleton()->get_resource_path();
  62. } else if (access == ACCESS_USERDATA) {
  63. root = OS::get_singleton()->get_user_data_dir();
  64. }
  65. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_EXTRA)) {
  66. DisplayServer::get_singleton()->file_dialog_with_options_show(get_translated_title(), ProjectSettings::get_singleton()->globalize_path(full_dir), root, file->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), processed_filters, _get_options(), callable_mp(this, &FileDialog::_native_dialog_cb_with_options));
  67. } else {
  68. DisplayServer::get_singleton()->file_dialog_show(get_translated_title(), ProjectSettings::get_singleton()->globalize_path(full_dir), file->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), processed_filters, callable_mp(this, &FileDialog::_native_dialog_cb));
  69. }
  70. }
  71. bool FileDialog::_can_use_native_popup() {
  72. if (access == ACCESS_RESOURCES || access == ACCESS_USERDATA || options.size() > 0) {
  73. return DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_EXTRA);
  74. }
  75. return DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE);
  76. }
  77. void FileDialog::popup(const Rect2i &p_rect) {
  78. _update_option_controls();
  79. #ifdef TOOLS_ENABLED
  80. if (is_part_of_edited_scene()) {
  81. ConfirmationDialog::popup(p_rect);
  82. return;
  83. }
  84. #endif
  85. if (_can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  86. _native_popup();
  87. } else {
  88. ConfirmationDialog::popup(p_rect);
  89. }
  90. }
  91. void FileDialog::set_visible(bool p_visible) {
  92. if (p_visible) {
  93. _update_option_controls();
  94. }
  95. #ifdef TOOLS_ENABLED
  96. if (is_part_of_edited_scene()) {
  97. ConfirmationDialog::set_visible(p_visible);
  98. return;
  99. }
  100. #endif
  101. if (_can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  102. if (p_visible) {
  103. _native_popup();
  104. }
  105. } else {
  106. ConfirmationDialog::set_visible(p_visible);
  107. }
  108. }
  109. void FileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter) {
  110. _native_dialog_cb_with_options(p_ok, p_files, p_filter, Dictionary());
  111. }
  112. void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options) {
  113. if (!p_ok) {
  114. file->set_text("");
  115. emit_signal(SNAME("canceled"));
  116. return;
  117. }
  118. if (p_files.is_empty()) {
  119. return;
  120. }
  121. Vector<String> files = p_files;
  122. if (access != ACCESS_FILESYSTEM) {
  123. for (String &file_name : files) {
  124. file_name = ProjectSettings::get_singleton()->localize_path(file_name);
  125. }
  126. }
  127. selected_options = p_selected_options;
  128. String f = files[0];
  129. filter->select(p_filter);
  130. dir->set_text(f.get_base_dir());
  131. file->set_text(f.get_file());
  132. _change_dir(f.get_base_dir());
  133. if (mode == FILE_MODE_OPEN_FILES) {
  134. emit_signal(SNAME("files_selected"), files);
  135. } else {
  136. if (mode == FILE_MODE_SAVE_FILE) {
  137. if (p_filter != 0 && p_filter != filter->get_item_count() - 1) {
  138. bool valid = false;
  139. int idx = p_filter;
  140. if (filters.size() > 1) {
  141. idx--;
  142. }
  143. if (idx >= 0 && idx < filters.size()) {
  144. String flt = filters[idx].get_slice(";", 0);
  145. int filter_slice_count = flt.get_slice_count(",");
  146. for (int j = 0; j < filter_slice_count; j++) {
  147. String str = (flt.get_slice(",", j).strip_edges());
  148. if (f.match(str)) {
  149. valid = true;
  150. break;
  151. }
  152. }
  153. if (!valid && filter_slice_count > 0) {
  154. String str = (flt.get_slice(",", 0).strip_edges());
  155. f += str.substr(1, str.length() - 1);
  156. }
  157. }
  158. }
  159. emit_signal(SNAME("file_selected"), f);
  160. } else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) {
  161. emit_signal(SNAME("file_selected"), f);
  162. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  163. emit_signal(SNAME("dir_selected"), f);
  164. }
  165. }
  166. }
  167. VBoxContainer *FileDialog::get_vbox() {
  168. return vbox;
  169. }
  170. void FileDialog::_validate_property(PropertyInfo &p_property) const {
  171. if (p_property.name == "dialog_text") {
  172. // File dialogs have a custom layout, and dialog nodes can't have both a text and a layout.
  173. p_property.usage = PROPERTY_USAGE_NONE;
  174. }
  175. }
  176. void FileDialog::_notification(int p_what) {
  177. switch (p_what) {
  178. case NOTIFICATION_READY: {
  179. #ifdef TOOLS_ENABLED
  180. if (is_part_of_edited_scene()) {
  181. return;
  182. }
  183. #endif
  184. // Replace the built-in dialog with the native one if it started visible.
  185. if (is_visible() && _can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  186. ConfirmationDialog::set_visible(false);
  187. _native_popup();
  188. }
  189. } break;
  190. case NOTIFICATION_VISIBILITY_CHANGED: {
  191. if (!is_visible()) {
  192. set_process_shortcut_input(false);
  193. }
  194. invalidate(); // Put it here to preview in the editor.
  195. } break;
  196. case NOTIFICATION_THEME_CHANGED: {
  197. dir_up->set_button_icon(theme_cache.parent_folder);
  198. if (vbox->is_layout_rtl()) {
  199. dir_prev->set_button_icon(theme_cache.forward_folder);
  200. dir_next->set_button_icon(theme_cache.back_folder);
  201. } else {
  202. dir_prev->set_button_icon(theme_cache.back_folder);
  203. dir_next->set_button_icon(theme_cache.forward_folder);
  204. }
  205. refresh->set_button_icon(theme_cache.reload);
  206. show_hidden->set_button_icon(theme_cache.toggle_hidden);
  207. makedir->set_button_icon(theme_cache.create_folder);
  208. show_filename_filter_button->set_button_icon(theme_cache.toggle_filename_filter);
  209. dir_up->begin_bulk_theme_override();
  210. dir_up->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  211. dir_up->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  212. dir_up->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  213. dir_up->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  214. dir_up->end_bulk_theme_override();
  215. dir_prev->begin_bulk_theme_override();
  216. dir_prev->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  217. dir_prev->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  218. dir_prev->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  219. dir_prev->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  220. dir_prev->end_bulk_theme_override();
  221. dir_next->begin_bulk_theme_override();
  222. dir_next->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  223. dir_next->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  224. dir_next->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  225. dir_next->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  226. dir_next->end_bulk_theme_override();
  227. refresh->begin_bulk_theme_override();
  228. refresh->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  229. refresh->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  230. refresh->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  231. refresh->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  232. refresh->end_bulk_theme_override();
  233. show_hidden->begin_bulk_theme_override();
  234. show_hidden->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  235. show_hidden->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  236. show_hidden->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  237. show_hidden->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  238. show_hidden->end_bulk_theme_override();
  239. makedir->begin_bulk_theme_override();
  240. makedir->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  241. makedir->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  242. makedir->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  243. makedir->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  244. makedir->end_bulk_theme_override();
  245. show_filename_filter_button->begin_bulk_theme_override();
  246. show_filename_filter_button->add_theme_color_override("icon_normal_color", theme_cache.icon_normal_color);
  247. show_filename_filter_button->add_theme_color_override("icon_hover_color", theme_cache.icon_hover_color);
  248. show_filename_filter_button->add_theme_color_override("icon_focus_color", theme_cache.icon_focus_color);
  249. show_filename_filter_button->add_theme_color_override("icon_pressed_color", theme_cache.icon_pressed_color);
  250. show_filename_filter_button->end_bulk_theme_override();
  251. invalidate();
  252. } break;
  253. case NOTIFICATION_TRANSLATION_CHANGED: {
  254. update_filters();
  255. } break;
  256. }
  257. }
  258. void FileDialog::shortcut_input(const Ref<InputEvent> &p_event) {
  259. ERR_FAIL_COND(p_event.is_null());
  260. Ref<InputEventKey> k = p_event;
  261. if (k.is_valid() && has_focus()) {
  262. if (k->is_pressed()) {
  263. bool handled = true;
  264. switch (k->get_keycode()) {
  265. case Key::H: {
  266. if (k->is_command_or_control_pressed()) {
  267. set_show_hidden_files(!show_hidden_files);
  268. } else {
  269. handled = false;
  270. }
  271. } break;
  272. case Key::F: {
  273. if (k->is_command_or_control_pressed()) {
  274. show_filename_filter_button->set_pressed(!show_filename_filter_button->is_pressed());
  275. } else {
  276. handled = false;
  277. }
  278. } break;
  279. case Key::F5: {
  280. invalidate();
  281. } break;
  282. case Key::BACKSPACE: {
  283. _dir_submitted("..");
  284. } break;
  285. #ifdef MACOS_ENABLED
  286. // Cmd + Shift + G (matches Finder's "Go To" shortcut).
  287. case Key::G: {
  288. if (k->is_command_or_control_pressed() && k->is_shift_pressed()) {
  289. dir->grab_focus();
  290. dir->select_all();
  291. } else {
  292. handled = false;
  293. }
  294. } break;
  295. #endif
  296. // Ctrl + L (matches most Windows/Linux file managers' "focus on path bar" shortcut,
  297. // plus macOS Safari's "focus on address bar" shortcut).
  298. case Key::L: {
  299. if (k->is_command_or_control_pressed()) {
  300. dir->grab_focus();
  301. dir->select_all();
  302. } else {
  303. handled = false;
  304. }
  305. } break;
  306. default: {
  307. handled = false;
  308. }
  309. }
  310. if (handled) {
  311. set_input_as_handled();
  312. }
  313. }
  314. }
  315. }
  316. void FileDialog::set_enable_multiple_selection(bool p_enable) {
  317. tree->set_select_mode(p_enable ? Tree::SELECT_MULTI : Tree::SELECT_SINGLE);
  318. }
  319. Vector<String> FileDialog::get_selected_files() const {
  320. Vector<String> list;
  321. TreeItem *item = tree->get_root();
  322. item = tree->get_next_selected(item);
  323. while (item) {
  324. list.push_back(dir_access->get_current_dir().path_join(item->get_text(0)));
  325. item = tree->get_next_selected(item);
  326. }
  327. return list;
  328. }
  329. void FileDialog::update_dir() {
  330. full_dir = dir_access->get_current_dir();
  331. if (root_prefix.is_empty()) {
  332. dir->set_text(dir_access->get_current_dir(false));
  333. } else {
  334. dir->set_text(dir_access->get_current_dir(false).trim_prefix(root_prefix).trim_prefix("/"));
  335. }
  336. if (drives->is_visible()) {
  337. if (dir_access->get_current_dir().is_network_share_path()) {
  338. _update_drives(false);
  339. drives->add_item(ETR("Network"));
  340. drives->set_item_disabled(-1, true);
  341. drives->select(drives->get_item_count() - 1);
  342. } else {
  343. drives->select(dir_access->get_current_drive());
  344. }
  345. }
  346. // Deselect any item, to make "Select Current Folder" button text by default.
  347. deselect_all();
  348. }
  349. void FileDialog::_dir_submitted(String p_dir) {
  350. String new_dir = p_dir;
  351. #ifdef WINDOWS_ENABLED
  352. if (root_prefix.is_empty() && drives->is_visible() && !new_dir.is_network_share_path() && new_dir.is_absolute_path() && new_dir.find(":/") == -1 && new_dir.find(":\\") == -1) {
  353. // Non network path without X:/ prefix on Windows, add drive letter.
  354. new_dir = drives->get_item_text(drives->get_selected()).path_join(new_dir);
  355. }
  356. #endif
  357. if (!root_prefix.is_empty()) {
  358. new_dir = root_prefix.path_join(new_dir);
  359. }
  360. _change_dir(new_dir);
  361. file->set_text("");
  362. _push_history();
  363. }
  364. void FileDialog::_file_submitted(const String &p_file) {
  365. _action_pressed();
  366. }
  367. void FileDialog::_save_confirm_pressed() {
  368. String f = dir_access->get_current_dir().path_join(file->get_text());
  369. emit_signal(SNAME("file_selected"), f);
  370. hide();
  371. }
  372. void FileDialog::_post_popup() {
  373. ConfirmationDialog::_post_popup();
  374. if (mode == FILE_MODE_SAVE_FILE) {
  375. file->grab_focus();
  376. } else {
  377. tree->grab_focus();
  378. }
  379. set_process_shortcut_input(true);
  380. // For open dir mode, deselect all items on file dialog open.
  381. if (mode == FILE_MODE_OPEN_DIR) {
  382. deselect_all();
  383. file_box->set_visible(false);
  384. } else {
  385. file_box->set_visible(true);
  386. }
  387. local_history.clear();
  388. local_history_pos = -1;
  389. _push_history();
  390. }
  391. void FileDialog::_push_history() {
  392. local_history.resize(local_history_pos + 1);
  393. String new_path = dir_access->get_current_dir();
  394. if (local_history.size() == 0 || new_path != local_history[local_history_pos]) {
  395. local_history.push_back(new_path);
  396. local_history_pos++;
  397. dir_prev->set_disabled(local_history_pos == 0);
  398. dir_next->set_disabled(true);
  399. }
  400. }
  401. void FileDialog::_action_pressed() {
  402. if (mode == FILE_MODE_OPEN_FILES) {
  403. TreeItem *ti = tree->get_next_selected(nullptr);
  404. String fbase = dir_access->get_current_dir();
  405. Vector<String> files;
  406. while (ti) {
  407. files.push_back(fbase.path_join(ti->get_text(0)));
  408. ti = tree->get_next_selected(ti);
  409. }
  410. if (files.size()) {
  411. emit_signal(SNAME("files_selected"), files);
  412. hide();
  413. }
  414. return;
  415. }
  416. String file_text = file->get_text();
  417. String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text);
  418. if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && (dir_access->file_exists(f) || dir_access->is_bundle(f))) {
  419. emit_signal(SNAME("file_selected"), f);
  420. hide();
  421. } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) {
  422. String path = dir_access->get_current_dir();
  423. path = path.replace("\\", "/");
  424. TreeItem *item = tree->get_selected();
  425. if (item) {
  426. Dictionary d = item->get_metadata(0);
  427. if (d["dir"] && d["name"] != "..") {
  428. path = path.path_join(d["name"]);
  429. }
  430. }
  431. emit_signal(SNAME("dir_selected"), path);
  432. hide();
  433. }
  434. if (mode == FILE_MODE_SAVE_FILE) {
  435. bool valid = false;
  436. if (filter->get_selected() == filter->get_item_count() - 1) {
  437. valid = true; // match none
  438. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  439. // match all filters
  440. for (int i = 0; i < filters.size(); i++) {
  441. String flt = filters[i].get_slice(";", 0);
  442. for (int j = 0; j < flt.get_slice_count(","); j++) {
  443. String str = flt.get_slice(",", j).strip_edges();
  444. if (f.matchn(str)) {
  445. valid = true;
  446. break;
  447. }
  448. }
  449. if (valid) {
  450. break;
  451. }
  452. }
  453. } else {
  454. int idx = filter->get_selected();
  455. if (filters.size() > 1) {
  456. idx--;
  457. }
  458. if (idx >= 0 && idx < filters.size()) {
  459. String flt = filters[idx].get_slice(";", 0);
  460. int filter_slice_count = flt.get_slice_count(",");
  461. for (int j = 0; j < filter_slice_count; j++) {
  462. String str = (flt.get_slice(",", j).strip_edges());
  463. if (f.matchn(str)) {
  464. valid = true;
  465. break;
  466. }
  467. }
  468. if (!valid && filter_slice_count > 0) {
  469. String str = (flt.get_slice(",", 0).strip_edges());
  470. f += str.substr(1, str.length() - 1);
  471. file->set_text(f.get_file());
  472. valid = true;
  473. }
  474. } else {
  475. valid = true;
  476. }
  477. }
  478. String file_name = file_text.strip_edges().get_file();
  479. if (!valid || file_name.is_empty()) {
  480. exterr->popup_centered(Size2(250, 80));
  481. return;
  482. }
  483. if (dir_access->file_exists(f) || dir_access->is_bundle(f)) {
  484. confirm_save->set_text(vformat(atr(ETR("File \"%s\" already exists.\nDo you want to overwrite it?")), f));
  485. confirm_save->popup_centered(Size2(250, 80));
  486. } else {
  487. emit_signal(SNAME("file_selected"), f);
  488. hide();
  489. }
  490. }
  491. }
  492. void FileDialog::_cancel_pressed() {
  493. file->set_text("");
  494. invalidate();
  495. hide();
  496. }
  497. bool FileDialog::_is_open_should_be_disabled() {
  498. if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_SAVE_FILE) {
  499. return false;
  500. }
  501. TreeItem *ti = tree->get_next_selected(tree->get_root());
  502. while (ti) {
  503. TreeItem *prev_ti = ti;
  504. ti = tree->get_next_selected(tree->get_root());
  505. if (ti == prev_ti) {
  506. break;
  507. }
  508. }
  509. // We have something that we can't select?
  510. if (!ti) {
  511. return mode != FILE_MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder.
  512. }
  513. Dictionary d = ti->get_metadata(0);
  514. // Opening a file, but selected a folder? Forbidden.
  515. return ((mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
  516. (mode == FILE_MODE_OPEN_DIR && !d["dir"]);
  517. }
  518. void FileDialog::_go_up() {
  519. _change_dir("..");
  520. _push_history();
  521. }
  522. void FileDialog::_go_back() {
  523. if (local_history_pos <= 0) {
  524. return;
  525. }
  526. local_history_pos--;
  527. _change_dir(local_history[local_history_pos]);
  528. dir_prev->set_disabled(local_history_pos == 0);
  529. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  530. }
  531. void FileDialog::_go_forward() {
  532. if (local_history_pos >= local_history.size() - 1) {
  533. return;
  534. }
  535. local_history_pos++;
  536. _change_dir(local_history[local_history_pos]);
  537. dir_prev->set_disabled(local_history_pos == 0);
  538. dir_next->set_disabled(local_history_pos == local_history.size() - 1);
  539. }
  540. void FileDialog::deselect_all() {
  541. // Clear currently selected items in file manager.
  542. tree->deselect_all();
  543. // And change get_ok title.
  544. if (!tree->is_anything_selected()) {
  545. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  546. switch (mode) {
  547. case FILE_MODE_OPEN_FILE:
  548. case FILE_MODE_OPEN_FILES:
  549. set_ok_button_text(ETR("Open"));
  550. break;
  551. case FILE_MODE_OPEN_DIR:
  552. set_ok_button_text(ETR("Select Current Folder"));
  553. break;
  554. case FILE_MODE_OPEN_ANY:
  555. case FILE_MODE_SAVE_FILE:
  556. // FIXME: Implement, or refactor to avoid duplication with set_mode
  557. break;
  558. }
  559. }
  560. }
  561. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  562. _tree_selected();
  563. }
  564. void FileDialog::_tree_selected() {
  565. TreeItem *ti = tree->get_selected();
  566. if (!ti) {
  567. return;
  568. }
  569. Dictionary d = ti->get_metadata(0);
  570. if (!d["dir"]) {
  571. file->set_text(d["name"]);
  572. } else if (mode == FILE_MODE_OPEN_DIR) {
  573. set_ok_button_text(ETR("Select This Folder"));
  574. }
  575. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  576. }
  577. void FileDialog::_tree_item_activated() {
  578. TreeItem *ti = tree->get_selected();
  579. if (!ti) {
  580. return;
  581. }
  582. Dictionary d = ti->get_metadata(0);
  583. if (d["dir"]) {
  584. _change_dir(d["name"]);
  585. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  586. file->set_text("");
  587. }
  588. _push_history();
  589. } else {
  590. _action_pressed();
  591. }
  592. }
  593. void FileDialog::update_file_name() {
  594. int idx = filter->get_selected() - 1;
  595. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  596. if (idx == -1) {
  597. idx += 1;
  598. }
  599. String filter_str = filters[idx];
  600. String file_str = file->get_text();
  601. String base_name = file_str.get_basename();
  602. Vector<String> filter_substr = filter_str.split(";");
  603. if (filter_substr.size() >= 2) {
  604. file_str = base_name + "." + filter_substr[0].strip_edges().get_extension().to_lower();
  605. } else {
  606. file_str = base_name + "." + filter_str.strip_edges().get_extension().to_lower();
  607. }
  608. file->set_text(file_str);
  609. }
  610. }
  611. void FileDialog::_item_menu_id_pressed(int p_option) {
  612. switch (p_option) {
  613. case ITEM_MENU_SHOW_IN_EXPLORER: {
  614. TreeItem *ti = tree->get_selected();
  615. String path;
  616. if (ti) {
  617. Dictionary d = ti->get_metadata(0);
  618. path = ProjectSettings::get_singleton()->globalize_path(dir_access->get_current_dir().path_join(d["name"]));
  619. } else {
  620. path = ProjectSettings::get_singleton()->globalize_path(dir_access->get_current_dir());
  621. }
  622. OS::get_singleton()->shell_show_in_file_manager(path, true);
  623. } break;
  624. case ITEM_MENU_SHOW_BUNDLE_CONTENT: {
  625. TreeItem *ti = tree->get_selected();
  626. if (!ti) {
  627. return;
  628. }
  629. Dictionary d = ti->get_metadata(0);
  630. _change_dir(d["name"]);
  631. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  632. file->set_text("");
  633. }
  634. _push_history();
  635. } break;
  636. }
  637. }
  638. void FileDialog::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) {
  639. if (p_button == MouseButton::RIGHT) {
  640. item_menu->clear();
  641. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  642. // Opening the system file manager is not supported on the Android and web editors.
  643. item_menu->add_item(ETR("Open in File Manager"), ITEM_MENU_SHOW_IN_EXPLORER);
  644. item_menu->set_position(tree->get_screen_position() + p_pos);
  645. item_menu->reset_size();
  646. item_menu->popup();
  647. #endif
  648. }
  649. }
  650. void FileDialog::_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
  651. if (p_button == MouseButton::RIGHT) {
  652. item_menu->clear();
  653. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  654. // Opening the system file manager is not supported on the Android and web editors.
  655. TreeItem *ti = tree->get_selected();
  656. if (!ti) {
  657. return;
  658. }
  659. Dictionary d = ti->get_metadata(0);
  660. if (d["bundle"]) {
  661. item_menu->add_item(ETR("Show Package Contents"), ITEM_MENU_SHOW_BUNDLE_CONTENT);
  662. }
  663. item_menu->add_item(ETR("Open in File Manager"), ITEM_MENU_SHOW_IN_EXPLORER);
  664. item_menu->set_position(tree->get_screen_position() + p_pos);
  665. item_menu->reset_size();
  666. item_menu->popup();
  667. #endif
  668. } else {
  669. _tree_selected();
  670. }
  671. }
  672. void FileDialog::update_file_list() {
  673. tree->clear();
  674. // Scroll back to the top after opening a directory
  675. tree->get_vscroll_bar()->set_value(0);
  676. dir_access->list_dir_begin();
  677. if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) {
  678. message->hide();
  679. } else {
  680. message->set_text(ETR("You don't have permission to access contents of this folder."));
  681. message->show();
  682. }
  683. TreeItem *root = tree->create_item();
  684. List<String> files;
  685. List<String> dirs;
  686. bool is_hidden;
  687. String item = dir_access->get_next();
  688. while (!item.is_empty()) {
  689. if (item == "." || item == "..") {
  690. item = dir_access->get_next();
  691. continue;
  692. }
  693. is_hidden = dir_access->current_is_hidden();
  694. if (show_hidden_files || !is_hidden) {
  695. if (!dir_access->current_is_dir()) {
  696. files.push_back(item);
  697. } else {
  698. dirs.push_back(item);
  699. }
  700. }
  701. item = dir_access->get_next();
  702. }
  703. dirs.sort_custom<FileNoCaseComparator>();
  704. files.sort_custom<FileNoCaseComparator>();
  705. String filename_filter_lower = file_name_filter.to_lower();
  706. List<String> patterns;
  707. // build filter
  708. if (filter->get_selected() == filter->get_item_count() - 1) {
  709. // match all
  710. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  711. // match all filters
  712. for (int i = 0; i < filters.size(); i++) {
  713. String f = filters[i].get_slice(";", 0);
  714. for (int j = 0; j < f.get_slice_count(","); j++) {
  715. patterns.push_back(f.get_slice(",", j).strip_edges());
  716. }
  717. }
  718. } else {
  719. int idx = filter->get_selected();
  720. if (filters.size() > 1) {
  721. idx--;
  722. }
  723. if (idx >= 0 && idx < filters.size()) {
  724. String f = filters[idx].get_slice(";", 0);
  725. for (int j = 0; j < f.get_slice_count(","); j++) {
  726. patterns.push_back(f.get_slice(",", j).strip_edges());
  727. }
  728. }
  729. }
  730. while (!dirs.is_empty()) {
  731. const String &dir_name = dirs.front()->get();
  732. bool bundle = dir_access->is_bundle(dir_name);
  733. bool found = true;
  734. if (bundle) {
  735. bool match = patterns.is_empty();
  736. for (const String &E : patterns) {
  737. if (dir_name.matchn(E)) {
  738. match = true;
  739. break;
  740. }
  741. }
  742. found = match;
  743. }
  744. if (found && (filename_filter_lower.is_empty() || dir_name.to_lower().contains(filename_filter_lower))) {
  745. TreeItem *ti = tree->create_item(root);
  746. ti->set_text(0, dir_name);
  747. ti->set_icon(0, theme_cache.folder);
  748. ti->set_icon_modulate(0, theme_cache.folder_icon_color);
  749. Dictionary d;
  750. d["name"] = dir_name;
  751. d["dir"] = !bundle;
  752. d["bundle"] = bundle;
  753. ti->set_metadata(0, d);
  754. }
  755. dirs.pop_front();
  756. }
  757. String base_dir = dir_access->get_current_dir();
  758. while (!files.is_empty()) {
  759. bool match = patterns.is_empty();
  760. String match_str;
  761. for (const String &E : patterns) {
  762. if (files.front()->get().matchn(E)) {
  763. match_str = E;
  764. match = true;
  765. break;
  766. }
  767. }
  768. if (match && (filename_filter_lower.is_empty() || files.front()->get().to_lower().contains(filename_filter_lower))) {
  769. TreeItem *ti = tree->create_item(root);
  770. ti->set_text(0, files.front()->get());
  771. if (get_icon_func) {
  772. Ref<Texture2D> icon = get_icon_func(base_dir.path_join(files.front()->get()));
  773. ti->set_icon(0, icon);
  774. } else {
  775. ti->set_icon(0, theme_cache.file);
  776. }
  777. ti->set_icon_modulate(0, theme_cache.file_icon_color);
  778. if (mode == FILE_MODE_OPEN_DIR) {
  779. ti->set_custom_color(0, theme_cache.file_disabled_color);
  780. ti->set_selectable(0, false);
  781. }
  782. Dictionary d;
  783. d["name"] = files.front()->get();
  784. d["dir"] = false;
  785. d["bundle"] = false;
  786. ti->set_metadata(0, d);
  787. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  788. ti->select(0);
  789. }
  790. }
  791. files.pop_front();
  792. }
  793. if (mode != FILE_MODE_SAVE_FILE && mode != FILE_MODE_OPEN_DIR) {
  794. // Select the first file from list if nothing is selected.
  795. if (tree->get_root() && tree->get_root()->get_first_child() && tree->get_selected() == nullptr) {
  796. tree->get_root()->get_first_child()->select(0);
  797. }
  798. }
  799. }
  800. void FileDialog::_filter_selected(int) {
  801. update_file_name();
  802. update_file_list();
  803. }
  804. void FileDialog::_filename_filter_changed() {
  805. update_filename_filter();
  806. update_file_list();
  807. callable_mp(this, &FileDialog::_tree_select_first).call_deferred();
  808. }
  809. void FileDialog::_tree_select_first() {
  810. if (tree->get_root() && tree->get_root()->get_first_child()) {
  811. tree->get_root()->get_first_child()->select(0);
  812. }
  813. }
  814. void FileDialog::_filename_filter_selected() {
  815. TreeItem *item = tree->get_selected();
  816. if (item) {
  817. file->set_text(item->get_text(0));
  818. file->emit_signal(SceneStringName(text_submitted), file->get_text());
  819. }
  820. }
  821. void FileDialog::update_filters() {
  822. filter->clear();
  823. processed_filters.clear();
  824. if (filters.size() > 1) {
  825. String all_filters;
  826. String all_filters_full;
  827. const int max_filters = 5;
  828. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  829. String flt = filters[i].get_slicec(';', 0).strip_edges();
  830. if (i > 0) {
  831. all_filters += ", ";
  832. }
  833. all_filters += flt;
  834. }
  835. for (int i = 0; i < filters.size(); i++) {
  836. String flt = filters[i].get_slicec(';', 0).strip_edges();
  837. if (i > 0) {
  838. all_filters_full += ",";
  839. }
  840. all_filters_full += flt;
  841. }
  842. if (max_filters < filters.size()) {
  843. all_filters += ", ...";
  844. }
  845. String f = atr(ETR("All Recognized")) + " (" + all_filters + ")";
  846. filter->add_item(f);
  847. processed_filters.push_back(all_filters_full + ";" + f);
  848. }
  849. for (int i = 0; i < filters.size(); i++) {
  850. String flt = filters[i].get_slicec(';', 0).strip_edges();
  851. String desc = filters[i].get_slice(";", 1).strip_edges();
  852. if (desc.length()) {
  853. String f = atr(desc) + " (" + flt + ")";
  854. filter->add_item(f);
  855. processed_filters.push_back(flt + ";" + f);
  856. } else {
  857. String f = "(" + flt + ")";
  858. filter->add_item(f);
  859. processed_filters.push_back(flt + ";" + f);
  860. }
  861. }
  862. String f = atr(ETR("All Files")) + " (*.*)";
  863. filter->add_item(f);
  864. processed_filters.push_back("*.*;" + f);
  865. }
  866. void FileDialog::clear_filename_filter() {
  867. set_filename_filter("");
  868. update_filename_filter_gui();
  869. invalidate();
  870. }
  871. void FileDialog::update_filename_filter_gui() {
  872. filename_filter_box->set_visible(show_filename_filter);
  873. if (!show_filename_filter) {
  874. file_name_filter.clear();
  875. }
  876. if (filename_filter->get_text() == file_name_filter) {
  877. return;
  878. }
  879. filename_filter->set_text(file_name_filter);
  880. }
  881. void FileDialog::update_filename_filter() {
  882. if (filename_filter->get_text() == file_name_filter) {
  883. return;
  884. }
  885. set_filename_filter(filename_filter->get_text());
  886. }
  887. void FileDialog::clear_filters() {
  888. filters.clear();
  889. update_filters();
  890. invalidate();
  891. }
  892. void FileDialog::add_filter(const String &p_filter, const String &p_description) {
  893. ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot.");
  894. if (p_description.is_empty()) {
  895. filters.push_back(p_filter);
  896. } else {
  897. filters.push_back(vformat("%s ; %s", p_filter, p_description));
  898. }
  899. update_filters();
  900. invalidate();
  901. }
  902. void FileDialog::set_filters(const Vector<String> &p_filters) {
  903. if (filters == p_filters) {
  904. return;
  905. }
  906. filters = p_filters;
  907. update_filters();
  908. invalidate();
  909. }
  910. void FileDialog::set_filename_filter(const String &p_filename_filter) {
  911. if (file_name_filter == p_filename_filter) {
  912. return;
  913. }
  914. file_name_filter = p_filename_filter;
  915. update_filename_filter_gui();
  916. emit_signal(SNAME("filename_filter_changed"), filter);
  917. invalidate();
  918. }
  919. Vector<String> FileDialog::get_filters() const {
  920. return filters;
  921. }
  922. String FileDialog::get_filename_filter() const {
  923. return file_name_filter;
  924. }
  925. String FileDialog::get_current_dir() const {
  926. return full_dir;
  927. }
  928. String FileDialog::get_current_file() const {
  929. return file->get_text();
  930. }
  931. String FileDialog::get_current_path() const {
  932. return full_dir.path_join(file->get_text());
  933. }
  934. void FileDialog::set_current_dir(const String &p_dir) {
  935. _change_dir(p_dir);
  936. _push_history();
  937. }
  938. void FileDialog::set_current_file(const String &p_file) {
  939. if (file->get_text() == p_file) {
  940. return;
  941. }
  942. file->set_text(p_file);
  943. update_dir();
  944. invalidate();
  945. _focus_file_text();
  946. }
  947. void FileDialog::set_current_path(const String &p_path) {
  948. if (!p_path.size()) {
  949. return;
  950. }
  951. int pos = MAX(p_path.rfind_char('/'), p_path.rfind_char('\\'));
  952. if (pos == -1) {
  953. set_current_file(p_path);
  954. } else {
  955. String path_dir = p_path.substr(0, pos);
  956. String path_file = p_path.substr(pos + 1, p_path.length());
  957. set_current_dir(path_dir);
  958. set_current_file(path_file);
  959. }
  960. }
  961. void FileDialog::set_root_subfolder(const String &p_root) {
  962. root_subfolder = p_root;
  963. ERR_FAIL_COND_MSG(!dir_access->dir_exists(p_root), "root_subfolder must be an existing sub-directory.");
  964. local_history.clear();
  965. local_history_pos = -1;
  966. dir_access->change_dir(root_subfolder);
  967. if (root_subfolder.is_empty()) {
  968. root_prefix = "";
  969. } else {
  970. root_prefix = dir_access->get_current_dir();
  971. }
  972. invalidate();
  973. update_dir();
  974. }
  975. String FileDialog::get_root_subfolder() const {
  976. return root_subfolder;
  977. }
  978. void FileDialog::set_mode_overrides_title(bool p_override) {
  979. mode_overrides_title = p_override;
  980. }
  981. bool FileDialog::is_mode_overriding_title() const {
  982. return mode_overrides_title;
  983. }
  984. void FileDialog::set_file_mode(FileMode p_mode) {
  985. ERR_FAIL_INDEX((int)p_mode, 5);
  986. if (mode == p_mode) {
  987. return;
  988. }
  989. mode = p_mode;
  990. switch (mode) {
  991. case FILE_MODE_OPEN_FILE:
  992. set_ok_button_text(ETR("Open"));
  993. if (mode_overrides_title) {
  994. set_title(ETR("Open a File"));
  995. }
  996. makedir->hide();
  997. break;
  998. case FILE_MODE_OPEN_FILES:
  999. set_ok_button_text(ETR("Open"));
  1000. if (mode_overrides_title) {
  1001. set_title(ETR("Open File(s)"));
  1002. }
  1003. makedir->hide();
  1004. break;
  1005. case FILE_MODE_OPEN_DIR:
  1006. set_ok_button_text(ETR("Select Current Folder"));
  1007. if (mode_overrides_title) {
  1008. set_title(ETR("Open a Directory"));
  1009. }
  1010. makedir->show();
  1011. break;
  1012. case FILE_MODE_OPEN_ANY:
  1013. set_ok_button_text(ETR("Open"));
  1014. if (mode_overrides_title) {
  1015. set_title(ETR("Open a File or Directory"));
  1016. }
  1017. makedir->show();
  1018. break;
  1019. case FILE_MODE_SAVE_FILE:
  1020. set_ok_button_text(ETR("Save"));
  1021. if (mode_overrides_title) {
  1022. set_title(ETR("Save a File"));
  1023. }
  1024. makedir->show();
  1025. break;
  1026. }
  1027. if (mode == FILE_MODE_OPEN_FILES) {
  1028. tree->set_select_mode(Tree::SELECT_MULTI);
  1029. } else {
  1030. tree->set_select_mode(Tree::SELECT_SINGLE);
  1031. }
  1032. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  1033. }
  1034. FileDialog::FileMode FileDialog::get_file_mode() const {
  1035. return mode;
  1036. }
  1037. void FileDialog::set_access(Access p_access) {
  1038. ERR_FAIL_INDEX(p_access, 3);
  1039. if (access == p_access) {
  1040. return;
  1041. }
  1042. access = p_access;
  1043. root_prefix = "";
  1044. root_subfolder = "";
  1045. switch (p_access) {
  1046. case ACCESS_FILESYSTEM: {
  1047. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1048. #ifdef ANDROID_ENABLED
  1049. set_root_subfolder(OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DESKTOP));
  1050. #endif
  1051. } break;
  1052. case ACCESS_RESOURCES: {
  1053. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1054. } break;
  1055. case ACCESS_USERDATA: {
  1056. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  1057. } break;
  1058. }
  1059. _update_drives();
  1060. invalidate();
  1061. update_filters();
  1062. update_dir();
  1063. }
  1064. void FileDialog::invalidate() {
  1065. if (!is_visible() || is_invalidating) {
  1066. return;
  1067. }
  1068. is_invalidating = true;
  1069. callable_mp(this, &FileDialog::_invalidate).call_deferred();
  1070. }
  1071. void FileDialog::_invalidate() {
  1072. if (!is_invalidating) {
  1073. return;
  1074. }
  1075. update_file_list();
  1076. is_invalidating = false;
  1077. }
  1078. FileDialog::Access FileDialog::get_access() const {
  1079. return access;
  1080. }
  1081. void FileDialog::_make_dir_confirm() {
  1082. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  1083. if (err == OK) {
  1084. _change_dir(makedirname->get_text().strip_edges());
  1085. update_filters();
  1086. _push_history();
  1087. } else {
  1088. mkdirerr->popup_centered(Size2(250, 50));
  1089. }
  1090. makedirname->set_text(""); // reset label
  1091. }
  1092. void FileDialog::_make_dir() {
  1093. makedialog->popup_centered(Size2(250, 80));
  1094. makedirname->grab_focus();
  1095. }
  1096. void FileDialog::_select_drive(int p_idx) {
  1097. String d = drives->get_item_text(p_idx);
  1098. _change_dir(d);
  1099. file->set_text("");
  1100. _push_history();
  1101. }
  1102. void FileDialog::_change_dir(const String &p_new_dir) {
  1103. if (root_prefix.is_empty()) {
  1104. dir_access->change_dir(p_new_dir);
  1105. } else {
  1106. String old_dir = dir_access->get_current_dir();
  1107. dir_access->change_dir(p_new_dir);
  1108. if (!dir_access->get_current_dir(false).begins_with(root_prefix)) {
  1109. dir_access->change_dir(old_dir);
  1110. return;
  1111. }
  1112. }
  1113. invalidate();
  1114. update_dir();
  1115. }
  1116. void FileDialog::_update_drives(bool p_select) {
  1117. int dc = dir_access->get_drive_count();
  1118. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  1119. drives->hide();
  1120. } else {
  1121. drives->clear();
  1122. Node *dp = drives->get_parent();
  1123. if (dp) {
  1124. dp->remove_child(drives);
  1125. }
  1126. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  1127. dp->add_child(drives);
  1128. drives->show();
  1129. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  1130. drives->add_item(dir_access->get_drive(i));
  1131. }
  1132. if (p_select) {
  1133. drives->select(dir_access->get_current_drive());
  1134. }
  1135. }
  1136. }
  1137. bool FileDialog::default_show_hidden_files = false;
  1138. TypedArray<Dictionary> FileDialog::_get_options() const {
  1139. TypedArray<Dictionary> out;
  1140. for (const FileDialog::Option &opt : options) {
  1141. Dictionary dict;
  1142. dict["name"] = opt.name;
  1143. dict["values"] = opt.values;
  1144. dict["default"] = (int)selected_options.get(opt.name, opt.default_idx);
  1145. out.push_back(dict);
  1146. }
  1147. return out;
  1148. }
  1149. void FileDialog::_option_changed_checkbox_toggled(bool p_pressed, const String &p_name) {
  1150. if (selected_options.has(p_name)) {
  1151. selected_options[p_name] = p_pressed;
  1152. }
  1153. }
  1154. void FileDialog::_option_changed_item_selected(int p_idx, const String &p_name) {
  1155. if (selected_options.has(p_name)) {
  1156. selected_options[p_name] = p_idx;
  1157. }
  1158. }
  1159. void FileDialog::_update_option_controls() {
  1160. if (!options_dirty) {
  1161. return;
  1162. }
  1163. options_dirty = false;
  1164. while (grid_options->get_child_count() > 0) {
  1165. Node *child = grid_options->get_child(0);
  1166. grid_options->remove_child(child);
  1167. child->queue_free();
  1168. }
  1169. selected_options.clear();
  1170. for (const FileDialog::Option &opt : options) {
  1171. Label *lbl = memnew(Label);
  1172. lbl->set_text(opt.name);
  1173. grid_options->add_child(lbl);
  1174. if (opt.values.is_empty()) {
  1175. CheckBox *cb = memnew(CheckBox);
  1176. cb->set_pressed(opt.default_idx);
  1177. grid_options->add_child(cb);
  1178. cb->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::_option_changed_checkbox_toggled).bind(opt.name));
  1179. selected_options[opt.name] = (bool)opt.default_idx;
  1180. } else {
  1181. OptionButton *ob = memnew(OptionButton);
  1182. for (const String &val : opt.values) {
  1183. ob->add_item(val);
  1184. }
  1185. ob->select(opt.default_idx);
  1186. grid_options->add_child(ob);
  1187. ob->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_option_changed_item_selected).bind(opt.name));
  1188. selected_options[opt.name] = opt.default_idx;
  1189. }
  1190. }
  1191. }
  1192. Dictionary FileDialog::get_selected_options() const {
  1193. return selected_options;
  1194. }
  1195. String FileDialog::get_option_name(int p_option) const {
  1196. ERR_FAIL_INDEX_V(p_option, options.size(), String());
  1197. return options[p_option].name;
  1198. }
  1199. Vector<String> FileDialog::get_option_values(int p_option) const {
  1200. ERR_FAIL_INDEX_V(p_option, options.size(), Vector<String>());
  1201. return options[p_option].values;
  1202. }
  1203. int FileDialog::get_option_default(int p_option) const {
  1204. ERR_FAIL_INDEX_V(p_option, options.size(), -1);
  1205. return options[p_option].default_idx;
  1206. }
  1207. void FileDialog::set_option_name(int p_option, const String &p_name) {
  1208. if (p_option < 0) {
  1209. p_option += get_option_count();
  1210. }
  1211. ERR_FAIL_INDEX(p_option, options.size());
  1212. options.write[p_option].name = p_name;
  1213. options_dirty = true;
  1214. if (is_visible()) {
  1215. _update_option_controls();
  1216. }
  1217. }
  1218. void FileDialog::set_option_values(int p_option, const Vector<String> &p_values) {
  1219. if (p_option < 0) {
  1220. p_option += get_option_count();
  1221. }
  1222. ERR_FAIL_INDEX(p_option, options.size());
  1223. options.write[p_option].values = p_values;
  1224. if (p_values.is_empty()) {
  1225. options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, 1);
  1226. } else {
  1227. options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, options[p_option].values.size() - 1);
  1228. }
  1229. options_dirty = true;
  1230. if (is_visible()) {
  1231. _update_option_controls();
  1232. }
  1233. }
  1234. void FileDialog::set_option_default(int p_option, int p_index) {
  1235. if (p_option < 0) {
  1236. p_option += get_option_count();
  1237. }
  1238. ERR_FAIL_INDEX(p_option, options.size());
  1239. if (options[p_option].values.is_empty()) {
  1240. options.write[p_option].default_idx = CLAMP(p_index, 0, 1);
  1241. } else {
  1242. options.write[p_option].default_idx = CLAMP(p_index, 0, options[p_option].values.size() - 1);
  1243. }
  1244. options_dirty = true;
  1245. if (is_visible()) {
  1246. _update_option_controls();
  1247. }
  1248. }
  1249. void FileDialog::add_option(const String &p_name, const Vector<String> &p_values, int p_index) {
  1250. Option opt;
  1251. opt.name = p_name;
  1252. opt.values = p_values;
  1253. if (opt.values.is_empty()) {
  1254. opt.default_idx = CLAMP(p_index, 0, 1);
  1255. } else {
  1256. opt.default_idx = CLAMP(p_index, 0, opt.values.size() - 1);
  1257. }
  1258. options.push_back(opt);
  1259. options_dirty = true;
  1260. if (is_visible()) {
  1261. _update_option_controls();
  1262. }
  1263. }
  1264. void FileDialog::set_option_count(int p_count) {
  1265. ERR_FAIL_COND(p_count < 0);
  1266. if (options.size() == p_count) {
  1267. return;
  1268. }
  1269. options.resize(p_count);
  1270. options_dirty = true;
  1271. notify_property_list_changed();
  1272. if (is_visible()) {
  1273. _update_option_controls();
  1274. }
  1275. }
  1276. int FileDialog::get_option_count() const {
  1277. return options.size();
  1278. }
  1279. void FileDialog::_bind_methods() {
  1280. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  1281. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  1282. ClassDB::bind_method(D_METHOD("add_filter", "filter", "description"), &FileDialog::add_filter, DEFVAL(""));
  1283. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  1284. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  1285. ClassDB::bind_method(D_METHOD("clear_filename_filter"), &FileDialog::clear_filename_filter);
  1286. ClassDB::bind_method(D_METHOD("set_filename_filter", "filter"), &FileDialog::set_filename_filter);
  1287. ClassDB::bind_method(D_METHOD("get_filename_filter"), &FileDialog::get_filename_filter);
  1288. ClassDB::bind_method(D_METHOD("get_option_name", "option"), &FileDialog::get_option_name);
  1289. ClassDB::bind_method(D_METHOD("get_option_values", "option"), &FileDialog::get_option_values);
  1290. ClassDB::bind_method(D_METHOD("get_option_default", "option"), &FileDialog::get_option_default);
  1291. ClassDB::bind_method(D_METHOD("set_option_name", "option", "name"), &FileDialog::set_option_name);
  1292. ClassDB::bind_method(D_METHOD("set_option_values", "option", "values"), &FileDialog::set_option_values);
  1293. ClassDB::bind_method(D_METHOD("set_option_default", "option", "default_value_index"), &FileDialog::set_option_default);
  1294. ClassDB::bind_method(D_METHOD("set_option_count", "count"), &FileDialog::set_option_count);
  1295. ClassDB::bind_method(D_METHOD("get_option_count"), &FileDialog::get_option_count);
  1296. ClassDB::bind_method(D_METHOD("add_option", "name", "values", "default_value_index"), &FileDialog::add_option);
  1297. ClassDB::bind_method(D_METHOD("get_selected_options"), &FileDialog::get_selected_options);
  1298. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  1299. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  1300. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  1301. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  1302. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  1303. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  1304. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  1305. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  1306. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  1307. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  1308. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  1309. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  1310. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  1311. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  1312. ClassDB::bind_method(D_METHOD("set_root_subfolder", "dir"), &FileDialog::set_root_subfolder);
  1313. ClassDB::bind_method(D_METHOD("get_root_subfolder"), &FileDialog::get_root_subfolder);
  1314. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  1315. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  1316. ClassDB::bind_method(D_METHOD("set_use_native_dialog", "native"), &FileDialog::set_use_native_dialog);
  1317. ClassDB::bind_method(D_METHOD("get_use_native_dialog"), &FileDialog::get_use_native_dialog);
  1318. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  1319. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  1320. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  1321. 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");
  1322. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User Data,File System"), "set_access", "get_access");
  1323. ADD_PROPERTY(PropertyInfo(Variant::STRING, "root_subfolder"), "set_root_subfolder", "get_root_subfolder");
  1324. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  1325. ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename_filter"), "set_filename_filter", "get_filename_filter");
  1326. ADD_ARRAY_COUNT("Options", "option_count", "set_option_count", "get_option_count", "option_");
  1327. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  1328. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_native_dialog"), "set_use_native_dialog", "get_use_native_dialog");
  1329. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir", PROPERTY_HINT_DIR, "", PROPERTY_USAGE_NONE), "set_current_dir", "get_current_dir");
  1330. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file", PROPERTY_HINT_FILE, "*", PROPERTY_USAGE_NONE), "set_current_file", "get_current_file");
  1331. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_path", "get_current_path");
  1332. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  1333. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  1334. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  1335. ADD_SIGNAL(MethodInfo("filename_filter_changed", PropertyInfo(Variant::STRING, "filter")));
  1336. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  1337. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  1338. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  1339. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  1340. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  1341. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  1342. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  1343. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  1344. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, parent_folder);
  1345. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, forward_folder);
  1346. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, back_folder);
  1347. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, reload);
  1348. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_hidden);
  1349. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, folder);
  1350. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_filename_filter);
  1351. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, file);
  1352. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, create_folder);
  1353. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, folder_icon_color);
  1354. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_icon_color);
  1355. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_disabled_color);
  1356. // TODO: Define own colors?
  1357. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_normal_color, "font_color", "Button");
  1358. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_hover_color, "font_hover_color", "Button");
  1359. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_focus_color, "font_focus_color", "Button");
  1360. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_pressed_color, "font_pressed_color", "Button");
  1361. Option defaults;
  1362. base_property_helper.set_prefix("option_");
  1363. base_property_helper.set_array_length_getter(&FileDialog::get_option_count);
  1364. base_property_helper.register_property(PropertyInfo(Variant::STRING, "name"), defaults.name, &FileDialog::set_option_name, &FileDialog::get_option_name);
  1365. base_property_helper.register_property(PropertyInfo(Variant::PACKED_STRING_ARRAY, "values"), defaults.values, &FileDialog::set_option_values, &FileDialog::get_option_values);
  1366. base_property_helper.register_property(PropertyInfo(Variant::INT, "default"), defaults.default_idx, &FileDialog::set_option_default, &FileDialog::get_option_default);
  1367. PropertyListHelper::register_base_helper(&base_property_helper);
  1368. }
  1369. void FileDialog::set_show_hidden_files(bool p_show) {
  1370. if (show_hidden_files == p_show) {
  1371. return;
  1372. }
  1373. show_hidden_files = p_show;
  1374. invalidate();
  1375. }
  1376. void FileDialog::set_show_filename_filter(bool p_show) {
  1377. if (p_show == show_filename_filter) {
  1378. return;
  1379. }
  1380. if (p_show) {
  1381. filename_filter->grab_focus();
  1382. } else {
  1383. if (filename_filter->has_focus()) {
  1384. tree->call_deferred("grab_focus");
  1385. }
  1386. }
  1387. show_filename_filter = p_show;
  1388. update_filename_filter_gui();
  1389. invalidate();
  1390. }
  1391. bool FileDialog::get_show_filename_filter() const {
  1392. return show_filename_filter;
  1393. }
  1394. bool FileDialog::is_showing_hidden_files() const {
  1395. return show_hidden_files;
  1396. }
  1397. void FileDialog::set_default_show_hidden_files(bool p_show) {
  1398. default_show_hidden_files = p_show;
  1399. }
  1400. void FileDialog::set_use_native_dialog(bool p_native) {
  1401. use_native_dialog = p_native;
  1402. #ifdef TOOLS_ENABLED
  1403. if (is_part_of_edited_scene()) {
  1404. return;
  1405. }
  1406. #endif
  1407. // Replace the built-in dialog with the native one if it's currently visible.
  1408. if (is_inside_tree() && is_visible() && _can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  1409. ConfirmationDialog::set_visible(false);
  1410. _native_popup();
  1411. }
  1412. }
  1413. bool FileDialog::get_use_native_dialog() const {
  1414. return use_native_dialog;
  1415. }
  1416. FileDialog::FileDialog() {
  1417. show_hidden_files = default_show_hidden_files;
  1418. vbox = memnew(VBoxContainer);
  1419. add_child(vbox, false, INTERNAL_MODE_FRONT);
  1420. mode = FILE_MODE_SAVE_FILE;
  1421. set_title(TTRC("Save a File"));
  1422. HBoxContainer *hbc = memnew(HBoxContainer);
  1423. dir_prev = memnew(Button);
  1424. dir_prev->set_theme_type_variation(SceneStringName(FlatButton));
  1425. dir_prev->set_tooltip_text(ETR("Go to previous folder."));
  1426. dir_next = memnew(Button);
  1427. dir_next->set_theme_type_variation(SceneStringName(FlatButton));
  1428. dir_next->set_tooltip_text(ETR("Go to next folder."));
  1429. dir_up = memnew(Button);
  1430. dir_up->set_theme_type_variation(SceneStringName(FlatButton));
  1431. dir_up->set_tooltip_text(ETR("Go to parent folder."));
  1432. hbc->add_child(dir_prev);
  1433. hbc->add_child(dir_next);
  1434. hbc->add_child(dir_up);
  1435. dir_prev->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_back));
  1436. dir_next->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_forward));
  1437. dir_up->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_up));
  1438. hbc->add_child(memnew(Label(ETR("Path:"))));
  1439. drives_container = memnew(HBoxContainer);
  1440. hbc->add_child(drives_container);
  1441. drives = memnew(OptionButton);
  1442. drives->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_select_drive));
  1443. hbc->add_child(drives);
  1444. dir = memnew(LineEdit);
  1445. dir->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1446. hbc->add_child(dir);
  1447. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1448. refresh = memnew(Button);
  1449. refresh->set_theme_type_variation(SceneStringName(FlatButton));
  1450. refresh->set_tooltip_text(ETR("Refresh files."));
  1451. refresh->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::update_file_list));
  1452. hbc->add_child(refresh);
  1453. show_hidden = memnew(Button);
  1454. show_hidden->set_theme_type_variation(SceneStringName(FlatButton));
  1455. show_hidden->set_toggle_mode(true);
  1456. show_hidden->set_pressed(is_showing_hidden_files());
  1457. show_hidden->set_tooltip_text(ETR("Toggle the visibility of hidden files."));
  1458. show_hidden->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_hidden_files));
  1459. hbc->add_child(show_hidden);
  1460. show_filename_filter_button = memnew(Button);
  1461. show_filename_filter_button->set_theme_type_variation(SceneStringName(FlatButton));
  1462. show_filename_filter_button->set_toggle_mode(true);
  1463. show_filename_filter_button->set_pressed(false);
  1464. show_filename_filter_button->set_tooltip_text(RTR("Toggle the visibility of the filter for file names."));
  1465. show_filename_filter_button->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_filename_filter));
  1466. hbc->add_child(show_filename_filter_button);
  1467. shortcuts_container = memnew(HBoxContainer);
  1468. hbc->add_child(shortcuts_container);
  1469. makedir = memnew(Button);
  1470. makedir->set_theme_type_variation(SceneStringName(FlatButton));
  1471. makedir->set_tooltip_text(ETR("Create a new folder."));
  1472. makedir->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_make_dir));
  1473. hbc->add_child(makedir);
  1474. vbox->add_child(hbc);
  1475. tree = memnew(Tree);
  1476. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1477. tree->set_hide_root(true);
  1478. vbox->add_margin_child(ETR("Directories & Files:"), tree, true);
  1479. message = memnew(Label);
  1480. message->hide();
  1481. message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1482. message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1483. message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1484. tree->add_child(message);
  1485. filename_filter_box = memnew(HBoxContainer);
  1486. filename_filter_box->add_child(memnew(Label(RTR("Filter:"))));
  1487. filename_filter = memnew(LineEdit);
  1488. filename_filter->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1489. filename_filter->set_stretch_ratio(4);
  1490. filename_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1491. filename_filter->set_clear_button_enabled(true);
  1492. filename_filter_box->add_child(filename_filter);
  1493. filename_filter_box->set_visible(false);
  1494. vbox->add_child(filename_filter_box);
  1495. file_box = memnew(HBoxContainer);
  1496. file_box->add_child(memnew(Label(ETR("File:"))));
  1497. file = memnew(LineEdit);
  1498. file->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1499. file->set_stretch_ratio(4);
  1500. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1501. file_box->add_child(file);
  1502. filter = memnew(OptionButton);
  1503. filter->set_stretch_ratio(3);
  1504. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1505. filter->set_clip_text(true); // too many extensions overflows it
  1506. file_box->add_child(filter);
  1507. vbox->add_child(file_box);
  1508. grid_options = memnew(GridContainer);
  1509. grid_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  1510. grid_options->set_columns(2);
  1511. vbox->add_child(grid_options);
  1512. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1513. _update_drives();
  1514. connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_action_pressed));
  1515. tree->set_allow_rmb_select(true);
  1516. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), CONNECT_DEFERRED);
  1517. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), CONNECT_DEFERRED);
  1518. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated));
  1519. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  1520. tree->connect("item_mouse_selected", callable_mp(this, &FileDialog::_rmb_select));
  1521. tree->connect("empty_clicked", callable_mp(this, &FileDialog::_empty_clicked));
  1522. dir->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_dir_submitted));
  1523. filename_filter->connect(SceneStringName(text_changed), callable_mp(this, &FileDialog::_filename_filter_changed).unbind(1));
  1524. filename_filter->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_filename_filter_selected).unbind(1));
  1525. file->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_file_submitted));
  1526. filter->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_filter_selected));
  1527. confirm_save = memnew(ConfirmationDialog);
  1528. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  1529. confirm_save->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_save_confirm_pressed));
  1530. makedialog = memnew(ConfirmationDialog);
  1531. makedialog->set_title(ETR("Create Folder"));
  1532. VBoxContainer *makevb = memnew(VBoxContainer);
  1533. makedialog->add_child(makevb);
  1534. makedirname = memnew(LineEdit);
  1535. makedirname->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1536. makevb->add_margin_child(ETR("Name:"), makedirname);
  1537. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  1538. makedialog->register_text_enter(makedirname);
  1539. makedialog->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_make_dir_confirm));
  1540. mkdirerr = memnew(AcceptDialog);
  1541. mkdirerr->set_text(ETR("Could not create folder."));
  1542. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  1543. exterr = memnew(AcceptDialog);
  1544. exterr->set_text(ETR("Invalid extension, or empty filename."));
  1545. add_child(exterr, false, INTERNAL_MODE_FRONT);
  1546. item_menu = memnew(PopupMenu);
  1547. item_menu->connect(SceneStringName(id_pressed), callable_mp(this, &FileDialog::_item_menu_id_pressed));
  1548. add_child(item_menu);
  1549. update_filters();
  1550. update_filename_filter_gui();
  1551. update_dir();
  1552. set_hide_on_ok(false);
  1553. set_size(Size2(640, 360));
  1554. if (register_func) {
  1555. register_func(this);
  1556. }
  1557. property_helper.setup_for_instance(base_property_helper, this);
  1558. }
  1559. FileDialog::~FileDialog() {
  1560. if (unregister_func) {
  1561. unregister_func(this);
  1562. }
  1563. }