file_dialog.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. #ifndef FILE_DIALOG_H
  31. #define FILE_DIALOG_H
  32. #include "box_container.h"
  33. #include "core/io/dir_access.h"
  34. #include "scene/gui/dialogs.h"
  35. #include "scene/gui/line_edit.h"
  36. #include "scene/gui/option_button.h"
  37. #include "scene/gui/tree.h"
  38. #include "scene/property_list_helper.h"
  39. class GridContainer;
  40. class PopupMenu;
  41. class FileDialog : public ConfirmationDialog {
  42. GDCLASS(FileDialog, ConfirmationDialog);
  43. public:
  44. enum Access {
  45. ACCESS_RESOURCES,
  46. ACCESS_USERDATA,
  47. ACCESS_FILESYSTEM
  48. };
  49. enum FileMode {
  50. FILE_MODE_OPEN_FILE,
  51. FILE_MODE_OPEN_FILES,
  52. FILE_MODE_OPEN_DIR,
  53. FILE_MODE_OPEN_ANY,
  54. FILE_MODE_SAVE_FILE
  55. };
  56. enum ItemMenu {
  57. ITEM_MENU_COPY_PATH,
  58. ITEM_MENU_SHOW_IN_EXPLORER,
  59. ITEM_MENU_SHOW_BUNDLE_CONTENT,
  60. };
  61. typedef Ref<Texture2D> (*GetIconFunc)(const String &);
  62. typedef void (*RegisterFunc)(FileDialog *);
  63. static GetIconFunc get_icon_func;
  64. static RegisterFunc register_func;
  65. static RegisterFunc unregister_func;
  66. private:
  67. ConfirmationDialog *makedialog = nullptr;
  68. LineEdit *makedirname = nullptr;
  69. Button *makedir = nullptr;
  70. Access access = ACCESS_RESOURCES;
  71. VBoxContainer *vbox = nullptr;
  72. GridContainer *grid_options = nullptr;
  73. FileMode mode;
  74. LineEdit *dir = nullptr;
  75. HBoxContainer *drives_container = nullptr;
  76. HBoxContainer *shortcuts_container = nullptr;
  77. OptionButton *drives = nullptr;
  78. Tree *tree = nullptr;
  79. HBoxContainer *filename_filter_box = nullptr;
  80. LineEdit *filename_filter = nullptr;
  81. HBoxContainer *file_box = nullptr;
  82. LineEdit *file = nullptr;
  83. OptionButton *filter = nullptr;
  84. AcceptDialog *mkdirerr = nullptr;
  85. AcceptDialog *exterr = nullptr;
  86. Ref<DirAccess> dir_access;
  87. ConfirmationDialog *confirm_save = nullptr;
  88. PopupMenu *item_menu = nullptr;
  89. Label *message = nullptr;
  90. Button *dir_prev = nullptr;
  91. Button *dir_next = nullptr;
  92. Button *dir_up = nullptr;
  93. Button *refresh = nullptr;
  94. Button *show_hidden = nullptr;
  95. Button *show_filename_filter_button = nullptr;
  96. Vector<String> filters;
  97. Vector<String> processed_filters;
  98. String file_name_filter;
  99. bool show_filename_filter = false;
  100. Vector<String> local_history;
  101. int local_history_pos = 0;
  102. void _push_history();
  103. bool mode_overrides_title = true;
  104. String root_subfolder;
  105. String root_prefix;
  106. static bool default_show_hidden_files;
  107. bool show_hidden_files = false;
  108. bool use_native_dialog = false;
  109. bool is_invalidating = false;
  110. struct ThemeCache {
  111. Ref<Texture2D> parent_folder;
  112. Ref<Texture2D> forward_folder;
  113. Ref<Texture2D> back_folder;
  114. Ref<Texture2D> reload;
  115. Ref<Texture2D> toggle_hidden;
  116. Ref<Texture2D> toggle_filename_filter;
  117. Ref<Texture2D> folder;
  118. Ref<Texture2D> file;
  119. Ref<Texture2D> create_folder;
  120. Color folder_icon_color;
  121. Color file_icon_color;
  122. Color file_disabled_color;
  123. Color icon_normal_color;
  124. Color icon_hover_color;
  125. Color icon_focus_color;
  126. Color icon_pressed_color;
  127. } theme_cache;
  128. struct Option {
  129. String name;
  130. Vector<String> values;
  131. int default_idx = 0;
  132. };
  133. static inline PropertyListHelper base_property_helper;
  134. PropertyListHelper property_helper;
  135. Vector<Option> options;
  136. Dictionary selected_options;
  137. bool options_dirty = false;
  138. String full_dir;
  139. void update_dir();
  140. void update_file_name();
  141. void update_file_list();
  142. void update_filename_filter();
  143. void update_filename_filter_gui();
  144. void update_filters();
  145. void _item_menu_id_pressed(int p_option);
  146. void _empty_clicked(const Vector2 &p_pos, MouseButton p_button);
  147. void _rmb_select(const Vector2 &p_pos, MouseButton p_button = MouseButton::RIGHT);
  148. void _focus_file_text();
  149. void _tree_multi_selected(Object *p_object, int p_cell, bool p_selected);
  150. void _tree_selected();
  151. void _select_drive(int p_idx);
  152. void _tree_item_activated();
  153. void _dir_submitted(String p_dir);
  154. void _file_submitted(const String &p_file);
  155. void _action_pressed();
  156. void _save_confirm_pressed();
  157. void _cancel_pressed();
  158. void _filter_selected(int);
  159. void _filename_filter_changed();
  160. void _filename_filter_selected();
  161. void _tree_select_first();
  162. void _make_dir();
  163. void _make_dir_confirm();
  164. void _go_up();
  165. void _go_back();
  166. void _go_forward();
  167. void _change_dir(const String &p_new_dir);
  168. void _update_drives(bool p_select = true);
  169. void _invalidate();
  170. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  171. bool _can_use_native_popup();
  172. void _native_popup();
  173. void _native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter);
  174. void _native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options);
  175. bool _is_open_should_be_disabled();
  176. TypedArray<Dictionary> _get_options() const;
  177. void _update_option_controls();
  178. void _option_changed_checkbox_toggled(bool p_pressed, const String &p_name);
  179. void _option_changed_item_selected(int p_idx, const String &p_name);
  180. virtual void _post_popup() override;
  181. protected:
  182. void _validate_property(PropertyInfo &p_property) const;
  183. void _notification(int p_what);
  184. bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
  185. bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
  186. void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
  187. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  188. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  189. static void _bind_methods();
  190. public:
  191. virtual void set_visible(bool p_visible) override;
  192. virtual void popup(const Rect2i &p_rect = Rect2i()) override;
  193. void popup_file_dialog();
  194. void clear_filters();
  195. void add_filter(const String &p_filter, const String &p_description = "");
  196. void set_filters(const Vector<String> &p_filters);
  197. Vector<String> get_filters() const;
  198. void clear_filename_filter();
  199. void set_filename_filter(const String &p_filename_filter);
  200. String get_filename_filter() const;
  201. void set_enable_multiple_selection(bool p_enable);
  202. Vector<String> get_selected_files() const;
  203. String get_current_dir() const;
  204. String get_current_file() const;
  205. String get_current_path() const;
  206. void set_current_dir(const String &p_dir);
  207. void set_current_file(const String &p_file);
  208. void set_current_path(const String &p_path);
  209. String get_option_name(int p_option) const;
  210. Vector<String> get_option_values(int p_option) const;
  211. int get_option_default(int p_option) const;
  212. void set_option_name(int p_option, const String &p_name);
  213. void set_option_values(int p_option, const Vector<String> &p_values);
  214. void set_option_default(int p_option, int p_index);
  215. void add_option(const String &p_name, const Vector<String> &p_values, int p_index);
  216. void set_option_count(int p_count);
  217. int get_option_count() const;
  218. Dictionary get_selected_options() const;
  219. void set_root_subfolder(const String &p_root);
  220. String get_root_subfolder() const;
  221. void set_mode_overrides_title(bool p_override);
  222. bool is_mode_overriding_title() const;
  223. void set_use_native_dialog(bool p_native);
  224. bool get_use_native_dialog() const;
  225. void set_file_mode(FileMode p_mode);
  226. FileMode get_file_mode() const;
  227. VBoxContainer *get_vbox();
  228. LineEdit *get_line_edit() { return file; }
  229. void set_access(Access p_access);
  230. Access get_access() const;
  231. void set_show_hidden_files(bool p_show);
  232. bool is_showing_hidden_files() const;
  233. void set_show_filename_filter(bool p_show);
  234. bool get_show_filename_filter() const;
  235. static void set_default_show_hidden_files(bool p_show);
  236. void invalidate();
  237. void deselect_all();
  238. FileDialog();
  239. ~FileDialog();
  240. };
  241. VARIANT_ENUM_CAST(FileDialog::FileMode);
  242. VARIANT_ENUM_CAST(FileDialog::Access);
  243. #endif // FILE_DIALOG_H