file_dialog.cpp 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863
  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. set_ok_button_text(ETR("Open"));
  555. break;
  556. case FILE_MODE_SAVE_FILE:
  557. set_ok_button_text(ETR("Save"));
  558. break;
  559. }
  560. }
  561. }
  562. void FileDialog::_tree_multi_selected(Object *p_object, int p_cell, bool p_selected) {
  563. _tree_selected();
  564. }
  565. void FileDialog::_tree_selected() {
  566. TreeItem *ti = tree->get_selected();
  567. if (!ti) {
  568. return;
  569. }
  570. Dictionary d = ti->get_metadata(0);
  571. if (!d["dir"]) {
  572. file->set_text(d["name"]);
  573. if (mode == FILE_MODE_SAVE_FILE) {
  574. set_ok_button_text(ETR("Save"));
  575. } else {
  576. set_ok_button_text(ETR("Open"));
  577. }
  578. } else if (mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY || !dir_access->file_exists(file->get_text())) {
  579. file->set_text("");
  580. if (mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  581. set_ok_button_text(ETR("Select This Folder"));
  582. }
  583. }
  584. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  585. }
  586. void FileDialog::_tree_item_activated() {
  587. TreeItem *ti = tree->get_selected();
  588. if (!ti) {
  589. return;
  590. }
  591. Dictionary d = ti->get_metadata(0);
  592. if (d["dir"]) {
  593. _change_dir(d["name"]);
  594. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  595. file->set_text("");
  596. }
  597. _push_history();
  598. } else {
  599. _action_pressed();
  600. }
  601. }
  602. void FileDialog::update_file_name() {
  603. int idx = filter->get_selected() - 1;
  604. if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) {
  605. if (idx == -1) {
  606. idx += 1;
  607. }
  608. String filter_str = filters[idx];
  609. String file_str = file->get_text();
  610. String base_name = file_str.get_basename();
  611. Vector<String> filter_substr = filter_str.split(";");
  612. if (filter_substr.size() >= 2) {
  613. file_str = base_name + "." + filter_substr[0].strip_edges().get_extension().to_lower();
  614. } else {
  615. file_str = base_name + "." + filter_str.strip_edges().get_extension().to_lower();
  616. }
  617. file->set_text(file_str);
  618. }
  619. }
  620. void FileDialog::_item_menu_id_pressed(int p_option) {
  621. switch (p_option) {
  622. case ITEM_MENU_SHOW_IN_EXPLORER: {
  623. TreeItem *ti = tree->get_selected();
  624. String path;
  625. if (ti) {
  626. Dictionary d = ti->get_metadata(0);
  627. path = ProjectSettings::get_singleton()->globalize_path(dir_access->get_current_dir().path_join(d["name"]));
  628. } else {
  629. path = ProjectSettings::get_singleton()->globalize_path(dir_access->get_current_dir());
  630. }
  631. OS::get_singleton()->shell_show_in_file_manager(path, true);
  632. } break;
  633. case ITEM_MENU_SHOW_BUNDLE_CONTENT: {
  634. TreeItem *ti = tree->get_selected();
  635. if (!ti) {
  636. return;
  637. }
  638. Dictionary d = ti->get_metadata(0);
  639. _change_dir(d["name"]);
  640. if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) {
  641. file->set_text("");
  642. }
  643. _push_history();
  644. } break;
  645. }
  646. }
  647. void FileDialog::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) {
  648. if (p_button == MouseButton::RIGHT) {
  649. item_menu->clear();
  650. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  651. // Opening the system file manager is not supported on the Android and web editors.
  652. item_menu->add_item(ETR("Open in File Manager"), ITEM_MENU_SHOW_IN_EXPLORER);
  653. item_menu->set_position(tree->get_screen_position() + p_pos);
  654. item_menu->reset_size();
  655. item_menu->popup();
  656. #endif
  657. }
  658. }
  659. void FileDialog::_rmb_select(const Vector2 &p_pos, MouseButton p_button) {
  660. if (p_button == MouseButton::RIGHT) {
  661. item_menu->clear();
  662. #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED)
  663. // Opening the system file manager is not supported on the Android and web editors.
  664. TreeItem *ti = tree->get_selected();
  665. if (!ti) {
  666. return;
  667. }
  668. Dictionary d = ti->get_metadata(0);
  669. if (d["bundle"]) {
  670. item_menu->add_item(ETR("Show Package Contents"), ITEM_MENU_SHOW_BUNDLE_CONTENT);
  671. }
  672. item_menu->add_item(ETR("Open in File Manager"), ITEM_MENU_SHOW_IN_EXPLORER);
  673. item_menu->set_position(tree->get_screen_position() + p_pos);
  674. item_menu->reset_size();
  675. item_menu->popup();
  676. #endif
  677. } else {
  678. _tree_selected();
  679. }
  680. }
  681. void FileDialog::update_file_list() {
  682. tree->clear();
  683. // Scroll back to the top after opening a directory
  684. tree->get_vscroll_bar()->set_value(0);
  685. dir_access->list_dir_begin();
  686. if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) {
  687. message->hide();
  688. } else {
  689. message->set_text(ETR("You don't have permission to access contents of this folder."));
  690. message->show();
  691. }
  692. TreeItem *root = tree->create_item();
  693. List<String> files;
  694. List<String> dirs;
  695. bool is_hidden;
  696. String item = dir_access->get_next();
  697. while (!item.is_empty()) {
  698. if (item == "." || item == "..") {
  699. item = dir_access->get_next();
  700. continue;
  701. }
  702. is_hidden = dir_access->current_is_hidden();
  703. if (show_hidden_files || !is_hidden) {
  704. if (!dir_access->current_is_dir()) {
  705. files.push_back(item);
  706. } else {
  707. dirs.push_back(item);
  708. }
  709. }
  710. item = dir_access->get_next();
  711. }
  712. dirs.sort_custom<FileNoCaseComparator>();
  713. files.sort_custom<FileNoCaseComparator>();
  714. String filename_filter_lower = file_name_filter.to_lower();
  715. List<String> patterns;
  716. // build filter
  717. if (filter->get_selected() == filter->get_item_count() - 1) {
  718. // match all
  719. } else if (filters.size() > 1 && filter->get_selected() == 0) {
  720. // match all filters
  721. for (int i = 0; i < filters.size(); i++) {
  722. String f = filters[i].get_slice(";", 0);
  723. for (int j = 0; j < f.get_slice_count(","); j++) {
  724. patterns.push_back(f.get_slice(",", j).strip_edges());
  725. }
  726. }
  727. } else {
  728. int idx = filter->get_selected();
  729. if (filters.size() > 1) {
  730. idx--;
  731. }
  732. if (idx >= 0 && idx < filters.size()) {
  733. String f = filters[idx].get_slice(";", 0);
  734. for (int j = 0; j < f.get_slice_count(","); j++) {
  735. patterns.push_back(f.get_slice(",", j).strip_edges());
  736. }
  737. }
  738. }
  739. while (!dirs.is_empty()) {
  740. const String &dir_name = dirs.front()->get();
  741. bool bundle = dir_access->is_bundle(dir_name);
  742. bool found = true;
  743. if (bundle) {
  744. bool match = patterns.is_empty();
  745. for (const String &E : patterns) {
  746. if (dir_name.matchn(E)) {
  747. match = true;
  748. break;
  749. }
  750. }
  751. found = match;
  752. }
  753. if (found && (filename_filter_lower.is_empty() || dir_name.to_lower().contains(filename_filter_lower))) {
  754. TreeItem *ti = tree->create_item(root);
  755. ti->set_text(0, dir_name);
  756. ti->set_icon(0, theme_cache.folder);
  757. ti->set_icon_modulate(0, theme_cache.folder_icon_color);
  758. Dictionary d;
  759. d["name"] = dir_name;
  760. d["dir"] = !bundle;
  761. d["bundle"] = bundle;
  762. ti->set_metadata(0, d);
  763. }
  764. dirs.pop_front();
  765. }
  766. String base_dir = dir_access->get_current_dir();
  767. while (!files.is_empty()) {
  768. bool match = patterns.is_empty();
  769. String match_str;
  770. for (const String &E : patterns) {
  771. if (files.front()->get().matchn(E)) {
  772. match_str = E;
  773. match = true;
  774. break;
  775. }
  776. }
  777. if (match && (filename_filter_lower.is_empty() || files.front()->get().to_lower().contains(filename_filter_lower))) {
  778. TreeItem *ti = tree->create_item(root);
  779. ti->set_text(0, files.front()->get());
  780. if (get_icon_func) {
  781. Ref<Texture2D> icon = get_icon_func(base_dir.path_join(files.front()->get()));
  782. ti->set_icon(0, icon);
  783. } else {
  784. ti->set_icon(0, theme_cache.file);
  785. }
  786. ti->set_icon_modulate(0, theme_cache.file_icon_color);
  787. if (mode == FILE_MODE_OPEN_DIR) {
  788. ti->set_custom_color(0, theme_cache.file_disabled_color);
  789. ti->set_selectable(0, false);
  790. }
  791. Dictionary d;
  792. d["name"] = files.front()->get();
  793. d["dir"] = false;
  794. d["bundle"] = false;
  795. ti->set_metadata(0, d);
  796. if (file->get_text() == files.front()->get() || match_str == files.front()->get()) {
  797. ti->select(0);
  798. }
  799. }
  800. files.pop_front();
  801. }
  802. if (mode != FILE_MODE_SAVE_FILE && mode != FILE_MODE_OPEN_DIR) {
  803. // Select the first file from list if nothing is selected.
  804. if (tree->get_root() && tree->get_root()->get_first_child() && tree->get_selected() == nullptr) {
  805. tree->get_root()->get_first_child()->select(0);
  806. _tree_selected();
  807. }
  808. }
  809. }
  810. void FileDialog::_filter_selected(int) {
  811. update_file_name();
  812. update_file_list();
  813. }
  814. void FileDialog::_filename_filter_changed() {
  815. update_filename_filter();
  816. update_file_list();
  817. callable_mp(this, &FileDialog::_tree_select_first).call_deferred();
  818. }
  819. void FileDialog::_tree_select_first() {
  820. if (tree->get_root() && tree->get_root()->get_first_child()) {
  821. tree->get_root()->get_first_child()->select(0);
  822. _tree_selected();
  823. }
  824. }
  825. void FileDialog::_filename_filter_selected() {
  826. TreeItem *item = tree->get_selected();
  827. if (item) {
  828. file->set_text(item->get_text(0));
  829. file->emit_signal(SceneStringName(text_submitted), file->get_text());
  830. }
  831. }
  832. void FileDialog::update_filters() {
  833. filter->clear();
  834. processed_filters.clear();
  835. if (filters.size() > 1) {
  836. String all_filters;
  837. String all_mime;
  838. String all_filters_full;
  839. String all_mime_full;
  840. const int max_filters = 5;
  841. // "All Recognized" display name.
  842. for (int i = 0; i < MIN(max_filters, filters.size()); i++) {
  843. String flt = filters[i].get_slicec(';', 0).strip_edges();
  844. if (!all_filters.is_empty() && !flt.is_empty()) {
  845. all_filters += ", ";
  846. }
  847. all_filters += flt;
  848. String mime = filters[i].get_slicec(';', 2).strip_edges();
  849. if (!all_mime.is_empty() && !mime.is_empty()) {
  850. all_mime += ", ";
  851. }
  852. all_mime += mime;
  853. }
  854. // "All Recognized" filter.
  855. for (int i = 0; i < filters.size(); i++) {
  856. String flt = filters[i].get_slicec(';', 0).strip_edges();
  857. if (!all_filters_full.is_empty() && !flt.is_empty()) {
  858. all_filters_full += ",";
  859. }
  860. all_filters_full += flt;
  861. String mime = filters[i].get_slicec(';', 2).strip_edges();
  862. if (!all_mime_full.is_empty() && !mime.is_empty()) {
  863. all_mime_full += ",";
  864. }
  865. all_mime_full += mime;
  866. }
  867. String native_all_name;
  868. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_MIME)) {
  869. native_all_name += all_filters;
  870. }
  871. if (!native_all_name.is_empty()) {
  872. native_all_name += ", ";
  873. }
  874. native_all_name += all_mime;
  875. if (max_filters < filters.size()) {
  876. all_filters += ", ...";
  877. native_all_name += ", ...";
  878. }
  879. filter->add_item(atr(ETR("All Recognized")) + " (" + all_filters + ")");
  880. processed_filters.push_back(all_filters_full + ";" + atr(ETR("All Recognized")) + " (" + native_all_name + ")" + ";" + all_mime_full);
  881. }
  882. for (int i = 0; i < filters.size(); i++) {
  883. String flt = filters[i].get_slicec(';', 0).strip_edges();
  884. String desc = filters[i].get_slicec(';', 1).strip_edges();
  885. String mime = filters[i].get_slicec(';', 2).strip_edges();
  886. String native_name;
  887. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_MIME)) {
  888. native_name += flt;
  889. }
  890. if (!native_name.is_empty() && !mime.is_empty()) {
  891. native_name += ", ";
  892. }
  893. native_name += mime;
  894. if (!desc.is_empty()) {
  895. filter->add_item(atr(desc) + " (" + flt + ")");
  896. processed_filters.push_back(flt + ";" + atr(desc) + " (" + native_name + ");" + mime);
  897. } else {
  898. filter->add_item("(" + flt + ")");
  899. processed_filters.push_back(flt + ";(" + native_name + ");" + mime);
  900. }
  901. }
  902. String f = atr(ETR("All Files")) + " (*.*)";
  903. filter->add_item(f);
  904. processed_filters.push_back("*.*;" + f + ";application/octet-stream");
  905. }
  906. void FileDialog::clear_filename_filter() {
  907. set_filename_filter("");
  908. update_filename_filter_gui();
  909. invalidate();
  910. }
  911. void FileDialog::update_filename_filter_gui() {
  912. filename_filter_box->set_visible(show_filename_filter);
  913. if (!show_filename_filter) {
  914. file_name_filter.clear();
  915. }
  916. if (filename_filter->get_text() == file_name_filter) {
  917. return;
  918. }
  919. filename_filter->set_text(file_name_filter);
  920. }
  921. void FileDialog::update_filename_filter() {
  922. if (filename_filter->get_text() == file_name_filter) {
  923. return;
  924. }
  925. set_filename_filter(filename_filter->get_text());
  926. }
  927. void FileDialog::clear_filters() {
  928. filters.clear();
  929. update_filters();
  930. invalidate();
  931. }
  932. void FileDialog::add_filter(const String &p_filter, const String &p_description) {
  933. ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot.");
  934. if (p_description.is_empty()) {
  935. filters.push_back(p_filter);
  936. } else {
  937. filters.push_back(vformat("%s ; %s", p_filter, p_description));
  938. }
  939. update_filters();
  940. invalidate();
  941. }
  942. void FileDialog::set_filters(const Vector<String> &p_filters) {
  943. if (filters == p_filters) {
  944. return;
  945. }
  946. filters = p_filters;
  947. update_filters();
  948. invalidate();
  949. }
  950. void FileDialog::set_filename_filter(const String &p_filename_filter) {
  951. if (file_name_filter == p_filename_filter) {
  952. return;
  953. }
  954. file_name_filter = p_filename_filter;
  955. update_filename_filter_gui();
  956. emit_signal(SNAME("filename_filter_changed"), filter);
  957. invalidate();
  958. }
  959. Vector<String> FileDialog::get_filters() const {
  960. return filters;
  961. }
  962. String FileDialog::get_filename_filter() const {
  963. return file_name_filter;
  964. }
  965. String FileDialog::get_current_dir() const {
  966. return full_dir;
  967. }
  968. String FileDialog::get_current_file() const {
  969. return file->get_text();
  970. }
  971. String FileDialog::get_current_path() const {
  972. return full_dir.path_join(file->get_text());
  973. }
  974. void FileDialog::set_current_dir(const String &p_dir) {
  975. _change_dir(p_dir);
  976. _push_history();
  977. }
  978. void FileDialog::set_current_file(const String &p_file) {
  979. if (file->get_text() == p_file) {
  980. return;
  981. }
  982. file->set_text(p_file);
  983. update_dir();
  984. invalidate();
  985. _focus_file_text();
  986. }
  987. void FileDialog::set_current_path(const String &p_path) {
  988. if (!p_path.size()) {
  989. return;
  990. }
  991. int pos = MAX(p_path.rfind_char('/'), p_path.rfind_char('\\'));
  992. if (pos == -1) {
  993. set_current_file(p_path);
  994. } else {
  995. String path_dir = p_path.substr(0, pos);
  996. String path_file = p_path.substr(pos + 1, p_path.length());
  997. set_current_dir(path_dir);
  998. set_current_file(path_file);
  999. }
  1000. }
  1001. void FileDialog::set_root_subfolder(const String &p_root) {
  1002. root_subfolder = p_root;
  1003. ERR_FAIL_COND_MSG(!dir_access->dir_exists(p_root), "root_subfolder must be an existing sub-directory.");
  1004. local_history.clear();
  1005. local_history_pos = -1;
  1006. dir_access->change_dir(root_subfolder);
  1007. if (root_subfolder.is_empty()) {
  1008. root_prefix = "";
  1009. } else {
  1010. root_prefix = dir_access->get_current_dir();
  1011. }
  1012. invalidate();
  1013. update_dir();
  1014. }
  1015. String FileDialog::get_root_subfolder() const {
  1016. return root_subfolder;
  1017. }
  1018. void FileDialog::set_mode_overrides_title(bool p_override) {
  1019. mode_overrides_title = p_override;
  1020. }
  1021. bool FileDialog::is_mode_overriding_title() const {
  1022. return mode_overrides_title;
  1023. }
  1024. void FileDialog::set_file_mode(FileMode p_mode) {
  1025. ERR_FAIL_INDEX((int)p_mode, 5);
  1026. if (mode == p_mode) {
  1027. return;
  1028. }
  1029. mode = p_mode;
  1030. switch (mode) {
  1031. case FILE_MODE_OPEN_FILE:
  1032. set_ok_button_text(ETR("Open"));
  1033. if (mode_overrides_title) {
  1034. set_title(ETR("Open a File"));
  1035. }
  1036. makedir->hide();
  1037. break;
  1038. case FILE_MODE_OPEN_FILES:
  1039. set_ok_button_text(ETR("Open"));
  1040. if (mode_overrides_title) {
  1041. set_title(ETR("Open File(s)"));
  1042. }
  1043. makedir->hide();
  1044. break;
  1045. case FILE_MODE_OPEN_DIR:
  1046. set_ok_button_text(ETR("Select Current Folder"));
  1047. if (mode_overrides_title) {
  1048. set_title(ETR("Open a Directory"));
  1049. }
  1050. makedir->show();
  1051. break;
  1052. case FILE_MODE_OPEN_ANY:
  1053. set_ok_button_text(ETR("Open"));
  1054. if (mode_overrides_title) {
  1055. set_title(ETR("Open a File or Directory"));
  1056. }
  1057. makedir->show();
  1058. break;
  1059. case FILE_MODE_SAVE_FILE:
  1060. set_ok_button_text(ETR("Save"));
  1061. if (mode_overrides_title) {
  1062. set_title(ETR("Save a File"));
  1063. }
  1064. makedir->show();
  1065. break;
  1066. }
  1067. if (mode == FILE_MODE_OPEN_FILES) {
  1068. tree->set_select_mode(Tree::SELECT_MULTI);
  1069. } else {
  1070. tree->set_select_mode(Tree::SELECT_SINGLE);
  1071. }
  1072. get_ok_button()->set_disabled(_is_open_should_be_disabled());
  1073. }
  1074. FileDialog::FileMode FileDialog::get_file_mode() const {
  1075. return mode;
  1076. }
  1077. void FileDialog::set_access(Access p_access) {
  1078. ERR_FAIL_INDEX(p_access, 3);
  1079. if (access == p_access) {
  1080. return;
  1081. }
  1082. access = p_access;
  1083. root_prefix = "";
  1084. root_subfolder = "";
  1085. switch (p_access) {
  1086. case ACCESS_FILESYSTEM: {
  1087. dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
  1088. #ifdef ANDROID_ENABLED
  1089. set_root_subfolder(OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DESKTOP));
  1090. #endif
  1091. } break;
  1092. case ACCESS_RESOURCES: {
  1093. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1094. } break;
  1095. case ACCESS_USERDATA: {
  1096. dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA);
  1097. } break;
  1098. }
  1099. _update_drives();
  1100. invalidate();
  1101. update_filters();
  1102. update_dir();
  1103. }
  1104. void FileDialog::invalidate() {
  1105. if (!is_visible() || is_invalidating) {
  1106. return;
  1107. }
  1108. is_invalidating = true;
  1109. callable_mp(this, &FileDialog::_invalidate).call_deferred();
  1110. }
  1111. void FileDialog::_invalidate() {
  1112. if (!is_invalidating) {
  1113. return;
  1114. }
  1115. update_file_list();
  1116. is_invalidating = false;
  1117. }
  1118. FileDialog::Access FileDialog::get_access() const {
  1119. return access;
  1120. }
  1121. void FileDialog::_make_dir_confirm() {
  1122. Error err = dir_access->make_dir(makedirname->get_text().strip_edges());
  1123. if (err == OK) {
  1124. _change_dir(makedirname->get_text().strip_edges());
  1125. update_filters();
  1126. _push_history();
  1127. } else {
  1128. mkdirerr->popup_centered(Size2(250, 50));
  1129. }
  1130. makedirname->set_text(""); // reset label
  1131. }
  1132. void FileDialog::_make_dir() {
  1133. makedialog->popup_centered(Size2(250, 80));
  1134. makedirname->grab_focus();
  1135. }
  1136. void FileDialog::_select_drive(int p_idx) {
  1137. String d = drives->get_item_text(p_idx);
  1138. _change_dir(d);
  1139. file->set_text("");
  1140. _push_history();
  1141. }
  1142. void FileDialog::_change_dir(const String &p_new_dir) {
  1143. if (root_prefix.is_empty()) {
  1144. dir_access->change_dir(p_new_dir);
  1145. } else {
  1146. String old_dir = dir_access->get_current_dir();
  1147. dir_access->change_dir(p_new_dir);
  1148. if (!dir_access->get_current_dir(false).begins_with(root_prefix)) {
  1149. dir_access->change_dir(old_dir);
  1150. return;
  1151. }
  1152. }
  1153. invalidate();
  1154. update_dir();
  1155. }
  1156. void FileDialog::_update_drives(bool p_select) {
  1157. int dc = dir_access->get_drive_count();
  1158. if (dc == 0 || access != ACCESS_FILESYSTEM) {
  1159. drives->hide();
  1160. } else {
  1161. drives->clear();
  1162. Node *dp = drives->get_parent();
  1163. if (dp) {
  1164. dp->remove_child(drives);
  1165. }
  1166. dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container;
  1167. dp->add_child(drives);
  1168. drives->show();
  1169. for (int i = 0; i < dir_access->get_drive_count(); i++) {
  1170. drives->add_item(dir_access->get_drive(i));
  1171. }
  1172. if (p_select) {
  1173. drives->select(dir_access->get_current_drive());
  1174. }
  1175. }
  1176. }
  1177. bool FileDialog::default_show_hidden_files = false;
  1178. TypedArray<Dictionary> FileDialog::_get_options() const {
  1179. TypedArray<Dictionary> out;
  1180. for (const FileDialog::Option &opt : options) {
  1181. Dictionary dict;
  1182. dict["name"] = opt.name;
  1183. dict["values"] = opt.values;
  1184. dict["default"] = (int)selected_options.get(opt.name, opt.default_idx);
  1185. out.push_back(dict);
  1186. }
  1187. return out;
  1188. }
  1189. void FileDialog::_option_changed_checkbox_toggled(bool p_pressed, const String &p_name) {
  1190. if (selected_options.has(p_name)) {
  1191. selected_options[p_name] = p_pressed;
  1192. }
  1193. }
  1194. void FileDialog::_option_changed_item_selected(int p_idx, const String &p_name) {
  1195. if (selected_options.has(p_name)) {
  1196. selected_options[p_name] = p_idx;
  1197. }
  1198. }
  1199. void FileDialog::_update_option_controls() {
  1200. if (!options_dirty) {
  1201. return;
  1202. }
  1203. options_dirty = false;
  1204. while (grid_options->get_child_count() > 0) {
  1205. Node *child = grid_options->get_child(0);
  1206. grid_options->remove_child(child);
  1207. child->queue_free();
  1208. }
  1209. selected_options.clear();
  1210. for (const FileDialog::Option &opt : options) {
  1211. Label *lbl = memnew(Label);
  1212. lbl->set_text(opt.name);
  1213. grid_options->add_child(lbl);
  1214. if (opt.values.is_empty()) {
  1215. CheckBox *cb = memnew(CheckBox);
  1216. cb->set_pressed(opt.default_idx);
  1217. grid_options->add_child(cb);
  1218. cb->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::_option_changed_checkbox_toggled).bind(opt.name));
  1219. selected_options[opt.name] = (bool)opt.default_idx;
  1220. } else {
  1221. OptionButton *ob = memnew(OptionButton);
  1222. for (const String &val : opt.values) {
  1223. ob->add_item(val);
  1224. }
  1225. ob->select(opt.default_idx);
  1226. grid_options->add_child(ob);
  1227. ob->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_option_changed_item_selected).bind(opt.name));
  1228. selected_options[opt.name] = opt.default_idx;
  1229. }
  1230. }
  1231. }
  1232. Dictionary FileDialog::get_selected_options() const {
  1233. return selected_options;
  1234. }
  1235. String FileDialog::get_option_name(int p_option) const {
  1236. ERR_FAIL_INDEX_V(p_option, options.size(), String());
  1237. return options[p_option].name;
  1238. }
  1239. Vector<String> FileDialog::get_option_values(int p_option) const {
  1240. ERR_FAIL_INDEX_V(p_option, options.size(), Vector<String>());
  1241. return options[p_option].values;
  1242. }
  1243. int FileDialog::get_option_default(int p_option) const {
  1244. ERR_FAIL_INDEX_V(p_option, options.size(), -1);
  1245. return options[p_option].default_idx;
  1246. }
  1247. void FileDialog::set_option_name(int p_option, const String &p_name) {
  1248. if (p_option < 0) {
  1249. p_option += get_option_count();
  1250. }
  1251. ERR_FAIL_INDEX(p_option, options.size());
  1252. options.write[p_option].name = p_name;
  1253. options_dirty = true;
  1254. if (is_visible()) {
  1255. _update_option_controls();
  1256. }
  1257. }
  1258. void FileDialog::set_option_values(int p_option, const Vector<String> &p_values) {
  1259. if (p_option < 0) {
  1260. p_option += get_option_count();
  1261. }
  1262. ERR_FAIL_INDEX(p_option, options.size());
  1263. options.write[p_option].values = p_values;
  1264. if (p_values.is_empty()) {
  1265. options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, 1);
  1266. } else {
  1267. options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, options[p_option].values.size() - 1);
  1268. }
  1269. options_dirty = true;
  1270. if (is_visible()) {
  1271. _update_option_controls();
  1272. }
  1273. }
  1274. void FileDialog::set_option_default(int p_option, int p_index) {
  1275. if (p_option < 0) {
  1276. p_option += get_option_count();
  1277. }
  1278. ERR_FAIL_INDEX(p_option, options.size());
  1279. if (options[p_option].values.is_empty()) {
  1280. options.write[p_option].default_idx = CLAMP(p_index, 0, 1);
  1281. } else {
  1282. options.write[p_option].default_idx = CLAMP(p_index, 0, options[p_option].values.size() - 1);
  1283. }
  1284. options_dirty = true;
  1285. if (is_visible()) {
  1286. _update_option_controls();
  1287. }
  1288. }
  1289. void FileDialog::add_option(const String &p_name, const Vector<String> &p_values, int p_index) {
  1290. Option opt;
  1291. opt.name = p_name;
  1292. opt.values = p_values;
  1293. if (opt.values.is_empty()) {
  1294. opt.default_idx = CLAMP(p_index, 0, 1);
  1295. } else {
  1296. opt.default_idx = CLAMP(p_index, 0, opt.values.size() - 1);
  1297. }
  1298. options.push_back(opt);
  1299. options_dirty = true;
  1300. if (is_visible()) {
  1301. _update_option_controls();
  1302. }
  1303. }
  1304. void FileDialog::set_option_count(int p_count) {
  1305. ERR_FAIL_COND(p_count < 0);
  1306. if (options.size() == p_count) {
  1307. return;
  1308. }
  1309. options.resize(p_count);
  1310. options_dirty = true;
  1311. notify_property_list_changed();
  1312. if (is_visible()) {
  1313. _update_option_controls();
  1314. }
  1315. }
  1316. int FileDialog::get_option_count() const {
  1317. return options.size();
  1318. }
  1319. void FileDialog::_bind_methods() {
  1320. ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed);
  1321. ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters);
  1322. ClassDB::bind_method(D_METHOD("add_filter", "filter", "description"), &FileDialog::add_filter, DEFVAL(""));
  1323. ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters);
  1324. ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters);
  1325. ClassDB::bind_method(D_METHOD("clear_filename_filter"), &FileDialog::clear_filename_filter);
  1326. ClassDB::bind_method(D_METHOD("set_filename_filter", "filter"), &FileDialog::set_filename_filter);
  1327. ClassDB::bind_method(D_METHOD("get_filename_filter"), &FileDialog::get_filename_filter);
  1328. ClassDB::bind_method(D_METHOD("get_option_name", "option"), &FileDialog::get_option_name);
  1329. ClassDB::bind_method(D_METHOD("get_option_values", "option"), &FileDialog::get_option_values);
  1330. ClassDB::bind_method(D_METHOD("get_option_default", "option"), &FileDialog::get_option_default);
  1331. ClassDB::bind_method(D_METHOD("set_option_name", "option", "name"), &FileDialog::set_option_name);
  1332. ClassDB::bind_method(D_METHOD("set_option_values", "option", "values"), &FileDialog::set_option_values);
  1333. ClassDB::bind_method(D_METHOD("set_option_default", "option", "default_value_index"), &FileDialog::set_option_default);
  1334. ClassDB::bind_method(D_METHOD("set_option_count", "count"), &FileDialog::set_option_count);
  1335. ClassDB::bind_method(D_METHOD("get_option_count"), &FileDialog::get_option_count);
  1336. ClassDB::bind_method(D_METHOD("add_option", "name", "values", "default_value_index"), &FileDialog::add_option);
  1337. ClassDB::bind_method(D_METHOD("get_selected_options"), &FileDialog::get_selected_options);
  1338. ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir);
  1339. ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file);
  1340. ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path);
  1341. ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
  1342. ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
  1343. ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
  1344. ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
  1345. ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
  1346. ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode);
  1347. ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode);
  1348. ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
  1349. ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit);
  1350. ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access);
  1351. ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access);
  1352. ClassDB::bind_method(D_METHOD("set_root_subfolder", "dir"), &FileDialog::set_root_subfolder);
  1353. ClassDB::bind_method(D_METHOD("get_root_subfolder"), &FileDialog::get_root_subfolder);
  1354. ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files);
  1355. ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files);
  1356. ClassDB::bind_method(D_METHOD("set_use_native_dialog", "native"), &FileDialog::set_use_native_dialog);
  1357. ClassDB::bind_method(D_METHOD("get_use_native_dialog"), &FileDialog::get_use_native_dialog);
  1358. ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all);
  1359. ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
  1360. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
  1361. 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");
  1362. ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User Data,File System"), "set_access", "get_access");
  1363. ADD_PROPERTY(PropertyInfo(Variant::STRING, "root_subfolder"), "set_root_subfolder", "get_root_subfolder");
  1364. ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters");
  1365. ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename_filter"), "set_filename_filter", "get_filename_filter");
  1366. ADD_ARRAY_COUNT("Options", "option_count", "set_option_count", "get_option_count", "option_");
  1367. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files");
  1368. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_native_dialog"), "set_use_native_dialog", "get_use_native_dialog");
  1369. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir", PROPERTY_HINT_DIR, "", PROPERTY_USAGE_NONE), "set_current_dir", "get_current_dir");
  1370. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file", PROPERTY_HINT_FILE, "*", PROPERTY_USAGE_NONE), "set_current_file", "get_current_file");
  1371. ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_path", "get_current_path");
  1372. ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path")));
  1373. ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths")));
  1374. ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir")));
  1375. ADD_SIGNAL(MethodInfo("filename_filter_changed", PropertyInfo(Variant::STRING, "filter")));
  1376. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE);
  1377. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES);
  1378. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR);
  1379. BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY);
  1380. BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE);
  1381. BIND_ENUM_CONSTANT(ACCESS_RESOURCES);
  1382. BIND_ENUM_CONSTANT(ACCESS_USERDATA);
  1383. BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
  1384. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, parent_folder);
  1385. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, forward_folder);
  1386. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, back_folder);
  1387. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, reload);
  1388. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_hidden);
  1389. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, folder);
  1390. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_filename_filter);
  1391. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, file);
  1392. BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, create_folder);
  1393. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, folder_icon_color);
  1394. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_icon_color);
  1395. BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_disabled_color);
  1396. // TODO: Define own colors?
  1397. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_normal_color, "font_color", "Button");
  1398. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_hover_color, "font_hover_color", "Button");
  1399. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_focus_color, "font_focus_color", "Button");
  1400. BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_pressed_color, "font_pressed_color", "Button");
  1401. Option defaults;
  1402. base_property_helper.set_prefix("option_");
  1403. base_property_helper.set_array_length_getter(&FileDialog::get_option_count);
  1404. base_property_helper.register_property(PropertyInfo(Variant::STRING, "name"), defaults.name, &FileDialog::set_option_name, &FileDialog::get_option_name);
  1405. base_property_helper.register_property(PropertyInfo(Variant::PACKED_STRING_ARRAY, "values"), defaults.values, &FileDialog::set_option_values, &FileDialog::get_option_values);
  1406. base_property_helper.register_property(PropertyInfo(Variant::INT, "default"), defaults.default_idx, &FileDialog::set_option_default, &FileDialog::get_option_default);
  1407. PropertyListHelper::register_base_helper(&base_property_helper);
  1408. }
  1409. void FileDialog::set_show_hidden_files(bool p_show) {
  1410. if (show_hidden_files == p_show) {
  1411. return;
  1412. }
  1413. show_hidden_files = p_show;
  1414. invalidate();
  1415. }
  1416. void FileDialog::set_show_filename_filter(bool p_show) {
  1417. if (p_show == show_filename_filter) {
  1418. return;
  1419. }
  1420. if (p_show) {
  1421. filename_filter->grab_focus();
  1422. } else {
  1423. if (filename_filter->has_focus()) {
  1424. tree->call_deferred("grab_focus");
  1425. }
  1426. }
  1427. show_filename_filter = p_show;
  1428. update_filename_filter_gui();
  1429. invalidate();
  1430. }
  1431. bool FileDialog::get_show_filename_filter() const {
  1432. return show_filename_filter;
  1433. }
  1434. bool FileDialog::is_showing_hidden_files() const {
  1435. return show_hidden_files;
  1436. }
  1437. void FileDialog::set_default_show_hidden_files(bool p_show) {
  1438. default_show_hidden_files = p_show;
  1439. }
  1440. void FileDialog::set_use_native_dialog(bool p_native) {
  1441. use_native_dialog = p_native;
  1442. #ifdef TOOLS_ENABLED
  1443. if (is_part_of_edited_scene()) {
  1444. return;
  1445. }
  1446. #endif
  1447. // Replace the built-in dialog with the native one if it's currently visible.
  1448. if (is_inside_tree() && is_visible() && _can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed())) {
  1449. ConfirmationDialog::set_visible(false);
  1450. _native_popup();
  1451. }
  1452. }
  1453. bool FileDialog::get_use_native_dialog() const {
  1454. return use_native_dialog;
  1455. }
  1456. FileDialog::FileDialog() {
  1457. show_hidden_files = default_show_hidden_files;
  1458. vbox = memnew(VBoxContainer);
  1459. add_child(vbox, false, INTERNAL_MODE_FRONT);
  1460. mode = FILE_MODE_SAVE_FILE;
  1461. set_title(TTRC("Save a File"));
  1462. HBoxContainer *hbc = memnew(HBoxContainer);
  1463. dir_prev = memnew(Button);
  1464. dir_prev->set_theme_type_variation(SceneStringName(FlatButton));
  1465. dir_prev->set_tooltip_text(ETR("Go to previous folder."));
  1466. dir_next = memnew(Button);
  1467. dir_next->set_theme_type_variation(SceneStringName(FlatButton));
  1468. dir_next->set_tooltip_text(ETR("Go to next folder."));
  1469. dir_up = memnew(Button);
  1470. dir_up->set_theme_type_variation(SceneStringName(FlatButton));
  1471. dir_up->set_tooltip_text(ETR("Go to parent folder."));
  1472. hbc->add_child(dir_prev);
  1473. hbc->add_child(dir_next);
  1474. hbc->add_child(dir_up);
  1475. dir_prev->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_back));
  1476. dir_next->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_forward));
  1477. dir_up->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_up));
  1478. hbc->add_child(memnew(Label(ETR("Path:"))));
  1479. drives_container = memnew(HBoxContainer);
  1480. hbc->add_child(drives_container);
  1481. drives = memnew(OptionButton);
  1482. drives->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_select_drive));
  1483. hbc->add_child(drives);
  1484. dir = memnew(LineEdit);
  1485. dir->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1486. hbc->add_child(dir);
  1487. dir->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1488. refresh = memnew(Button);
  1489. refresh->set_theme_type_variation(SceneStringName(FlatButton));
  1490. refresh->set_tooltip_text(ETR("Refresh files."));
  1491. refresh->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::update_file_list));
  1492. hbc->add_child(refresh);
  1493. show_hidden = memnew(Button);
  1494. show_hidden->set_theme_type_variation(SceneStringName(FlatButton));
  1495. show_hidden->set_toggle_mode(true);
  1496. show_hidden->set_pressed(is_showing_hidden_files());
  1497. show_hidden->set_tooltip_text(ETR("Toggle the visibility of hidden files."));
  1498. show_hidden->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_hidden_files));
  1499. hbc->add_child(show_hidden);
  1500. show_filename_filter_button = memnew(Button);
  1501. show_filename_filter_button->set_theme_type_variation(SceneStringName(FlatButton));
  1502. show_filename_filter_button->set_toggle_mode(true);
  1503. show_filename_filter_button->set_pressed(false);
  1504. show_filename_filter_button->set_tooltip_text(RTR("Toggle the visibility of the filter for file names."));
  1505. show_filename_filter_button->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_filename_filter));
  1506. hbc->add_child(show_filename_filter_button);
  1507. shortcuts_container = memnew(HBoxContainer);
  1508. hbc->add_child(shortcuts_container);
  1509. makedir = memnew(Button);
  1510. makedir->set_theme_type_variation(SceneStringName(FlatButton));
  1511. makedir->set_tooltip_text(ETR("Create a new folder."));
  1512. makedir->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_make_dir));
  1513. hbc->add_child(makedir);
  1514. vbox->add_child(hbc);
  1515. tree = memnew(Tree);
  1516. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  1517. tree->set_hide_root(true);
  1518. vbox->add_margin_child(ETR("Directories & Files:"), tree, true);
  1519. message = memnew(Label);
  1520. message->hide();
  1521. message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1522. message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1523. message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  1524. tree->add_child(message);
  1525. filename_filter_box = memnew(HBoxContainer);
  1526. filename_filter_box->add_child(memnew(Label(RTR("Filter:"))));
  1527. filename_filter = memnew(LineEdit);
  1528. filename_filter->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1529. filename_filter->set_stretch_ratio(4);
  1530. filename_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1531. filename_filter->set_clear_button_enabled(true);
  1532. filename_filter_box->add_child(filename_filter);
  1533. filename_filter_box->set_visible(false);
  1534. vbox->add_child(filename_filter_box);
  1535. file_box = memnew(HBoxContainer);
  1536. file_box->add_child(memnew(Label(ETR("File:"))));
  1537. file = memnew(LineEdit);
  1538. file->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1539. file->set_stretch_ratio(4);
  1540. file->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1541. file_box->add_child(file);
  1542. filter = memnew(OptionButton);
  1543. filter->set_stretch_ratio(3);
  1544. filter->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  1545. filter->set_clip_text(true); // too many extensions overflows it
  1546. file_box->add_child(filter);
  1547. vbox->add_child(file_box);
  1548. grid_options = memnew(GridContainer);
  1549. grid_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
  1550. grid_options->set_columns(2);
  1551. vbox->add_child(grid_options);
  1552. dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  1553. _update_drives();
  1554. connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_action_pressed));
  1555. tree->set_allow_rmb_select(true);
  1556. tree->connect("multi_selected", callable_mp(this, &FileDialog::_tree_multi_selected), CONNECT_DEFERRED);
  1557. tree->connect("cell_selected", callable_mp(this, &FileDialog::_tree_selected), CONNECT_DEFERRED);
  1558. tree->connect("item_activated", callable_mp(this, &FileDialog::_tree_item_activated));
  1559. tree->connect("nothing_selected", callable_mp(this, &FileDialog::deselect_all));
  1560. tree->connect("item_mouse_selected", callable_mp(this, &FileDialog::_rmb_select));
  1561. tree->connect("empty_clicked", callable_mp(this, &FileDialog::_empty_clicked));
  1562. dir->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_dir_submitted));
  1563. filename_filter->connect(SceneStringName(text_changed), callable_mp(this, &FileDialog::_filename_filter_changed).unbind(1));
  1564. filename_filter->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_filename_filter_selected).unbind(1));
  1565. file->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_file_submitted));
  1566. filter->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_filter_selected));
  1567. confirm_save = memnew(ConfirmationDialog);
  1568. add_child(confirm_save, false, INTERNAL_MODE_FRONT);
  1569. confirm_save->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_save_confirm_pressed));
  1570. makedialog = memnew(ConfirmationDialog);
  1571. makedialog->set_title(ETR("Create Folder"));
  1572. VBoxContainer *makevb = memnew(VBoxContainer);
  1573. makedialog->add_child(makevb);
  1574. makedirname = memnew(LineEdit);
  1575. makedirname->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE);
  1576. makevb->add_margin_child(ETR("Name:"), makedirname);
  1577. add_child(makedialog, false, INTERNAL_MODE_FRONT);
  1578. makedialog->register_text_enter(makedirname);
  1579. makedialog->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_make_dir_confirm));
  1580. mkdirerr = memnew(AcceptDialog);
  1581. mkdirerr->set_text(ETR("Could not create folder."));
  1582. add_child(mkdirerr, false, INTERNAL_MODE_FRONT);
  1583. exterr = memnew(AcceptDialog);
  1584. exterr->set_text(ETR("Invalid extension, or empty filename."));
  1585. add_child(exterr, false, INTERNAL_MODE_FRONT);
  1586. item_menu = memnew(PopupMenu);
  1587. item_menu->connect(SceneStringName(id_pressed), callable_mp(this, &FileDialog::_item_menu_id_pressed));
  1588. add_child(item_menu);
  1589. update_filters();
  1590. update_filename_filter_gui();
  1591. update_dir();
  1592. set_hide_on_ok(false);
  1593. set_size(Size2(640, 360));
  1594. if (register_func) {
  1595. register_func(this);
  1596. }
  1597. property_helper.setup_for_instance(base_property_helper, this);
  1598. }
  1599. FileDialog::~FileDialog() {
  1600. if (unregister_func) {
  1601. unregister_func(this);
  1602. }
  1603. }