file_dialog.cpp 60 KB

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