file_dialog.cpp 57 KB

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