file_dialog.cpp 63 KB

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