2
0

file_dialog.cpp 48 KB

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