file_dialog.cpp 47 KB

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