file_dialog.cpp 48 KB

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