file_dialog.cpp 48 KB

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