file_dialog.h 9.4 KB

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