file_dialog.cpp 54 KB

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