file_dialog.cpp 54 KB

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