file_dialog.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /**************************************************************************/
  2. /* file_dialog.h */
  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. #pragma once
  31. #include "scene/gui/dialogs.h"
  32. #include "scene/property_list_helper.h"
  33. class DirAccess;
  34. class FlowContainer;
  35. class GridContainer;
  36. class HBoxContainer;
  37. class ItemList;
  38. class LineEdit;
  39. class MenuButton;
  40. class OptionButton;
  41. class PopupMenu;
  42. class VBoxContainer;
  43. class VSeparator;
  44. class FileDialog : public ConfirmationDialog {
  45. GDCLASS(FileDialog, ConfirmationDialog);
  46. inline static constexpr int MAX_RECENTS = 20;
  47. struct Option {
  48. String name;
  49. Vector<String> values;
  50. int default_idx = 0;
  51. };
  52. struct DirInfo {
  53. String name;
  54. uint64_t modified_time = 0;
  55. bool bundle = false;
  56. struct NameComparator {
  57. bool operator()(const DirInfo &p_a, const DirInfo &p_b) const {
  58. return FileNoCaseComparator()(p_a.name, p_b.name);
  59. }
  60. };
  61. struct TimeComparator {
  62. bool operator()(const DirInfo &p_a, const DirInfo &p_b) const {
  63. return p_a.modified_time > p_b.modified_time;
  64. }
  65. };
  66. };
  67. struct FileInfo {
  68. String name;
  69. String match_string;
  70. String type_sort;
  71. uint64_t modified_time = 0;
  72. struct NameComparator {
  73. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  74. return FileNoCaseComparator()(p_a.name, p_b.name);
  75. }
  76. };
  77. struct TypeComparator {
  78. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  79. return FileNoCaseComparator()(p_a.type_sort, p_b.type_sort);
  80. }
  81. };
  82. struct TimeComparator {
  83. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  84. return p_a.modified_time > p_b.modified_time;
  85. }
  86. };
  87. };
  88. enum class FileSortOption {
  89. NAME,
  90. NAME_REVERSE,
  91. TYPE,
  92. TYPE_REVERSE,
  93. MODIFIED_TIME,
  94. MODIFIED_TIME_REVERSE,
  95. MAX
  96. };
  97. public:
  98. enum Access {
  99. ACCESS_RESOURCES,
  100. ACCESS_USERDATA,
  101. ACCESS_FILESYSTEM,
  102. };
  103. enum FileMode {
  104. FILE_MODE_OPEN_FILE,
  105. FILE_MODE_OPEN_FILES,
  106. FILE_MODE_OPEN_DIR,
  107. FILE_MODE_OPEN_ANY,
  108. FILE_MODE_SAVE_FILE,
  109. };
  110. enum DisplayMode {
  111. DISPLAY_THUMBNAILS,
  112. DISPLAY_LIST,
  113. };
  114. enum ItemMenu {
  115. ITEM_MENU_COPY_PATH,
  116. ITEM_MENU_SHOW_IN_EXPLORER,
  117. ITEM_MENU_SHOW_BUNDLE_CONTENT,
  118. };
  119. enum Customization {
  120. CUSTOMIZATION_HIDDEN_FILES,
  121. CUSTOMIZATION_CREATE_FOLDER,
  122. CUSTOMIZATION_FILE_FILTER,
  123. CUSTOMIZATION_FILE_SORT,
  124. CUSTOMIZATION_FAVORITES,
  125. CUSTOMIZATION_RECENT,
  126. CUSTOMIZATION_LAYOUT,
  127. CUSTOMIZATION_MAX
  128. };
  129. typedef Ref<Texture2D> (*GetIconFunc)(const String &);
  130. typedef void (*RegisterFunc)(FileDialog *);
  131. inline static GetIconFunc get_icon_func = nullptr;
  132. inline static RegisterFunc register_func = nullptr;
  133. inline static RegisterFunc unregister_func = nullptr;
  134. private:
  135. static inline PropertyListHelper base_property_helper;
  136. PropertyListHelper property_helper;
  137. inline static bool default_show_hidden_files = false;
  138. bool show_hidden_files = false;
  139. bool use_native_dialog = false;
  140. bool customization_flags[CUSTOMIZATION_MAX]; // Initialized to true in the constructor.
  141. inline static LocalVector<String> global_favorites;
  142. inline static LocalVector<String> global_recents;
  143. Access access = ACCESS_RESOURCES;
  144. FileMode mode = FILE_MODE_SAVE_FILE;
  145. DisplayMode display_mode = DISPLAY_THUMBNAILS;
  146. FileSortOption file_sort = FileSortOption::NAME;
  147. Ref<DirAccess> dir_access;
  148. Vector<String> filters;
  149. Vector<String> processed_filters;
  150. Vector<Option> options;
  151. Dictionary selected_options;
  152. bool options_dirty = false;
  153. String file_name_filter;
  154. bool show_filename_filter = false;
  155. Vector<String> local_history;
  156. int local_history_pos = 0;
  157. bool mode_overrides_title = true;
  158. String root_subfolder;
  159. String root_prefix;
  160. String full_dir;
  161. bool is_invalidating = false;
  162. VBoxContainer *main_vbox = nullptr;
  163. Button *dir_prev = nullptr;
  164. Button *dir_next = nullptr;
  165. Button *dir_up = nullptr;
  166. HBoxContainer *drives_container = nullptr;
  167. OptionButton *drives = nullptr;
  168. LineEdit *directory_edit = nullptr;
  169. HBoxContainer *shortcuts_container = nullptr;
  170. Button *refresh_button = nullptr;
  171. Button *favorite_button = nullptr;
  172. HBoxContainer *make_dir_container = nullptr;
  173. Button *make_dir_button = nullptr;
  174. Button *show_hidden = nullptr;
  175. VSeparator *show_hidden_separator = nullptr;
  176. HBoxContainer *layout_container = nullptr;
  177. VSeparator *layout_separator = nullptr;
  178. Button *thumbnail_mode_button = nullptr;
  179. Button *list_mode_button = nullptr;
  180. Button *show_filename_filter_button = nullptr;
  181. MenuButton *file_sort_button = nullptr;
  182. VBoxContainer *favorite_vbox = nullptr;
  183. Button *fav_up_button = nullptr;
  184. Button *fav_down_button = nullptr;
  185. ItemList *favorite_list = nullptr;
  186. VBoxContainer *recent_vbox = nullptr;
  187. ItemList *recent_list = nullptr;
  188. ItemList *file_list = nullptr;
  189. Label *message = nullptr;
  190. PopupMenu *item_menu = nullptr;
  191. HBoxContainer *filename_filter_box = nullptr;
  192. LineEdit *filename_filter = nullptr;
  193. HBoxContainer *file_box = nullptr;
  194. LineEdit *filename_edit = nullptr;
  195. OptionButton *filter = nullptr;
  196. FlowContainer *flow_checkbox_options = nullptr;
  197. GridContainer *grid_select_options = nullptr;
  198. ConfirmationDialog *make_dir_dialog = nullptr;
  199. LineEdit *new_dir_name = nullptr;
  200. AcceptDialog *mkdirerr = nullptr;
  201. AcceptDialog *exterr = nullptr;
  202. ConfirmationDialog *confirm_save = nullptr;
  203. struct ThemeCache {
  204. int thumbnail_size = 64;
  205. Ref<Texture2D> parent_folder;
  206. Ref<Texture2D> forward_folder;
  207. Ref<Texture2D> back_folder;
  208. Ref<Texture2D> reload;
  209. Ref<Texture2D> toggle_hidden;
  210. Ref<Texture2D> toggle_filename_filter;
  211. Ref<Texture2D> thumbnail_mode;
  212. Ref<Texture2D> list_mode;
  213. Ref<Texture2D> folder;
  214. Ref<Texture2D> file;
  215. Ref<Texture2D> create_folder;
  216. Ref<Texture2D> sort;
  217. Ref<Texture2D> favorite;
  218. Ref<Texture2D> favorite_up;
  219. Ref<Texture2D> favorite_down;
  220. Ref<Texture2D> file_thumbnail;
  221. Ref<Texture2D> folder_thumbnail;
  222. Color folder_icon_color;
  223. Color file_icon_color;
  224. Color file_disabled_color;
  225. Color icon_normal_color;
  226. Color icon_hover_color;
  227. Color icon_focus_color;
  228. Color icon_pressed_color;
  229. } theme_cache;
  230. void update_dir();
  231. void update_file_name();
  232. void update_file_list();
  233. void update_filename_filter();
  234. void update_filename_filter_gui();
  235. void update_filters();
  236. void update_customization();
  237. void _item_menu_id_pressed(int p_option);
  238. void _empty_clicked(const Vector2 &p_pos, MouseButton p_button);
  239. void _item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_button);
  240. void _focus_file_text();
  241. int _get_selected_file_idx();
  242. void _file_list_multi_selected(int p_item, bool p_selected);
  243. void _file_list_selected(int p_item);
  244. void _file_list_item_activated(int p_item);
  245. void _select_drive(int p_idx);
  246. void _dir_submitted(String p_dir);
  247. void _file_submitted(const String &p_file);
  248. void _action_pressed();
  249. void _save_confirm_pressed();
  250. void _cancel_pressed();
  251. void _filter_selected(int);
  252. void _filename_filter_changed();
  253. void _filename_filter_selected();
  254. void _file_list_select_first();
  255. void _make_dir();
  256. void _make_dir_confirm();
  257. void _go_up();
  258. void _go_back();
  259. void _go_forward();
  260. void _push_history();
  261. void _change_dir(const String &p_new_dir);
  262. void _update_drives(bool p_select = true);
  263. void _sort_option_selected(int p_option);
  264. void _favorite_selected(int p_item);
  265. void _favorite_pressed();
  266. void _favorite_move_up();
  267. void _favorite_move_down();
  268. void _update_favorite_list();
  269. void _update_fav_buttons();
  270. void _recent_selected(int p_item);
  271. void _save_to_recent();
  272. void _update_recent_list();
  273. bool _path_matches_access(const String &p_path) const;
  274. void _invalidate();
  275. void _setup_button(Button *p_button, const Ref<Texture2D> &p_icon);
  276. void _update_make_dir_visible();
  277. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  278. bool _can_use_native_popup();
  279. void _native_popup();
  280. void _native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter);
  281. void _native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options);
  282. bool _is_open_should_be_disabled();
  283. TypedArray<Dictionary> _get_options() const;
  284. void _update_option_controls();
  285. void _option_changed_checkbox_toggled(bool p_pressed, const String &p_name);
  286. void _option_changed_item_selected(int p_idx, const String &p_name);
  287. virtual void _post_popup() override;
  288. protected:
  289. void _validate_property(PropertyInfo &p_property) const;
  290. void _notification(int p_what);
  291. bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
  292. bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
  293. void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
  294. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  295. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  296. static void _bind_methods();
  297. public:
  298. virtual void set_visible(bool p_visible) override;
  299. virtual void popup(const Rect2i &p_rect = Rect2i()) override;
  300. void popup_file_dialog();
  301. void clear_filters();
  302. void add_filter(const String &p_filter, const String &p_description = "");
  303. void set_filters(const Vector<String> &p_filters);
  304. Vector<String> get_filters() const;
  305. void clear_filename_filter();
  306. void set_filename_filter(const String &p_filename_filter);
  307. String get_filename_filter() const;
  308. void set_enable_multiple_selection(bool p_enable);
  309. Vector<String> get_selected_files() const;
  310. String get_current_dir() const;
  311. String get_current_file() const;
  312. String get_current_path() const;
  313. void set_current_dir(const String &p_dir);
  314. void set_current_file(const String &p_file);
  315. void set_current_path(const String &p_path);
  316. String get_option_name(int p_option) const;
  317. Vector<String> get_option_values(int p_option) const;
  318. int get_option_default(int p_option) const;
  319. void set_option_name(int p_option, const String &p_name);
  320. void set_option_values(int p_option, const Vector<String> &p_values);
  321. void set_option_default(int p_option, int p_index);
  322. void add_option(const String &p_name, const Vector<String> &p_values, int p_index);
  323. void set_option_count(int p_count);
  324. int get_option_count() const;
  325. Dictionary get_selected_options() const;
  326. void set_root_subfolder(const String &p_root);
  327. String get_root_subfolder() const;
  328. void set_mode_overrides_title(bool p_override);
  329. bool is_mode_overriding_title() const;
  330. void set_use_native_dialog(bool p_native);
  331. bool get_use_native_dialog() const;
  332. void set_file_mode(FileMode p_mode);
  333. FileMode get_file_mode() const;
  334. void set_display_mode(DisplayMode p_mode);
  335. DisplayMode get_display_mode() const;
  336. void set_customization_flag_enabled(Customization p_flag, bool p_enabled);
  337. bool is_customization_flag_enabled(Customization p_flag) const;
  338. VBoxContainer *get_vbox() { return main_vbox; }
  339. LineEdit *get_line_edit() { return filename_edit; }
  340. void set_access(Access p_access);
  341. Access get_access() const;
  342. void set_show_hidden_files(bool p_show);
  343. bool is_showing_hidden_files() const;
  344. void set_show_filename_filter(bool p_show);
  345. bool get_show_filename_filter() const;
  346. static void set_default_show_hidden_files(bool p_show);
  347. void invalidate();
  348. void deselect_all();
  349. FileDialog();
  350. ~FileDialog();
  351. };
  352. VARIANT_ENUM_CAST(FileDialog::FileMode);
  353. VARIANT_ENUM_CAST(FileDialog::Access);
  354. VARIANT_ENUM_CAST(FileDialog::DisplayMode);
  355. VARIANT_ENUM_CAST(FileDialog::Customization);