file_dialog.cpp 58 KB

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