file_dialog.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 FileDialog : public ConfirmationDialog {
  44. GDCLASS(FileDialog, ConfirmationDialog);
  45. struct Option {
  46. String name;
  47. Vector<String> values;
  48. int default_idx = 0;
  49. };
  50. struct DirInfo {
  51. String name;
  52. uint64_t modified_time = 0;
  53. bool bundle = false;
  54. struct NameComparator {
  55. bool operator()(const DirInfo &p_a, const DirInfo &p_b) const {
  56. return FileNoCaseComparator()(p_a.name, p_b.name);
  57. }
  58. };
  59. struct TimeComparator {
  60. bool operator()(const DirInfo &p_a, const DirInfo &p_b) const {
  61. return p_a.modified_time > p_b.modified_time;
  62. }
  63. };
  64. };
  65. struct FileInfo {
  66. String name;
  67. String match_string;
  68. String type_sort;
  69. uint64_t modified_time = 0;
  70. struct NameComparator {
  71. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  72. return FileNoCaseComparator()(p_a.name, p_b.name);
  73. }
  74. };
  75. struct TypeComparator {
  76. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  77. return FileNoCaseComparator()(p_a.type_sort, p_b.type_sort);
  78. }
  79. };
  80. struct TimeComparator {
  81. bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
  82. return p_a.modified_time > p_b.modified_time;
  83. }
  84. };
  85. };
  86. enum class FileSortOption {
  87. NAME,
  88. NAME_REVERSE,
  89. TYPE,
  90. TYPE_REVERSE,
  91. MODIFIED_TIME,
  92. MODIFIED_TIME_REVERSE,
  93. MAX
  94. };
  95. public:
  96. enum Access {
  97. ACCESS_RESOURCES,
  98. ACCESS_USERDATA,
  99. ACCESS_FILESYSTEM,
  100. };
  101. enum FileMode {
  102. FILE_MODE_OPEN_FILE,
  103. FILE_MODE_OPEN_FILES,
  104. FILE_MODE_OPEN_DIR,
  105. FILE_MODE_OPEN_ANY,
  106. FILE_MODE_SAVE_FILE,
  107. };
  108. enum ItemMenu {
  109. ITEM_MENU_COPY_PATH,
  110. ITEM_MENU_SHOW_IN_EXPLORER,
  111. ITEM_MENU_SHOW_BUNDLE_CONTENT,
  112. };
  113. typedef Ref<Texture2D> (*GetIconFunc)(const String &);
  114. typedef void (*RegisterFunc)(FileDialog *);
  115. inline static GetIconFunc get_icon_func = nullptr;
  116. inline static RegisterFunc register_func = nullptr;
  117. inline static RegisterFunc unregister_func = nullptr;
  118. private:
  119. static inline PropertyListHelper base_property_helper;
  120. PropertyListHelper property_helper;
  121. inline static bool default_show_hidden_files = false;
  122. bool show_hidden_files = false;
  123. bool use_native_dialog = false;
  124. Access access = ACCESS_RESOURCES;
  125. FileMode mode = FILE_MODE_SAVE_FILE;
  126. FileSortOption file_sort = FileSortOption::NAME;
  127. Ref<DirAccess> dir_access;
  128. Vector<String> filters;
  129. Vector<String> processed_filters;
  130. Vector<Option> options;
  131. Dictionary selected_options;
  132. bool options_dirty = false;
  133. String file_name_filter;
  134. bool show_filename_filter = false;
  135. Vector<String> local_history;
  136. int local_history_pos = 0;
  137. bool mode_overrides_title = true;
  138. String root_subfolder;
  139. String root_prefix;
  140. String full_dir;
  141. bool is_invalidating = false;
  142. VBoxContainer *main_vbox = nullptr;
  143. Button *dir_prev = nullptr;
  144. Button *dir_next = nullptr;
  145. Button *dir_up = nullptr;
  146. HBoxContainer *drives_container = nullptr;
  147. OptionButton *drives = nullptr;
  148. LineEdit *directory_edit = nullptr;
  149. HBoxContainer *shortcuts_container = nullptr;
  150. Button *refresh_button = nullptr;
  151. Button *make_dir_button = nullptr;
  152. Button *show_hidden = nullptr;
  153. Button *show_filename_filter_button = nullptr;
  154. MenuButton *file_sort_button = nullptr;
  155. ItemList *file_list = nullptr;
  156. Label *message = nullptr;
  157. PopupMenu *item_menu = nullptr;
  158. HBoxContainer *filename_filter_box = nullptr;
  159. LineEdit *filename_filter = nullptr;
  160. HBoxContainer *file_box = nullptr;
  161. LineEdit *filename_edit = nullptr;
  162. OptionButton *filter = nullptr;
  163. FlowContainer *flow_checkbox_options = nullptr;
  164. GridContainer *grid_select_options = nullptr;
  165. ConfirmationDialog *make_dir_dialog = nullptr;
  166. LineEdit *new_dir_name = nullptr;
  167. AcceptDialog *mkdirerr = nullptr;
  168. AcceptDialog *exterr = nullptr;
  169. ConfirmationDialog *confirm_save = nullptr;
  170. struct ThemeCache {
  171. Ref<Texture2D> parent_folder;
  172. Ref<Texture2D> forward_folder;
  173. Ref<Texture2D> back_folder;
  174. Ref<Texture2D> reload;
  175. Ref<Texture2D> toggle_hidden;
  176. Ref<Texture2D> toggle_filename_filter;
  177. Ref<Texture2D> folder;
  178. Ref<Texture2D> file;
  179. Ref<Texture2D> create_folder;
  180. Ref<Texture2D> sort;
  181. Color folder_icon_color;
  182. Color file_icon_color;
  183. Color file_disabled_color;
  184. Color icon_normal_color;
  185. Color icon_hover_color;
  186. Color icon_focus_color;
  187. Color icon_pressed_color;
  188. } theme_cache;
  189. void update_dir();
  190. void update_file_name();
  191. void update_file_list();
  192. void update_filename_filter();
  193. void update_filename_filter_gui();
  194. void update_filters();
  195. void _item_menu_id_pressed(int p_option);
  196. void _empty_clicked(const Vector2 &p_pos, MouseButton p_button);
  197. void _item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_button);
  198. void _focus_file_text();
  199. int _get_selected_file_idx();
  200. void _file_list_multi_selected(int p_item, bool p_selected);
  201. void _file_list_selected(int p_item);
  202. void _file_list_item_activated(int p_item);
  203. void _select_drive(int p_idx);
  204. void _dir_submitted(String p_dir);
  205. void _file_submitted(const String &p_file);
  206. void _action_pressed();
  207. void _save_confirm_pressed();
  208. void _cancel_pressed();
  209. void _filter_selected(int);
  210. void _filename_filter_changed();
  211. void _filename_filter_selected();
  212. void _file_list_select_first();
  213. void _make_dir();
  214. void _make_dir_confirm();
  215. void _go_up();
  216. void _go_back();
  217. void _go_forward();
  218. void _push_history();
  219. void _change_dir(const String &p_new_dir);
  220. void _update_drives(bool p_select = true);
  221. void _sort_option_selected(int p_option);
  222. void _invalidate();
  223. void _setup_button(Button *p_button, const Ref<Texture2D> &p_icon);
  224. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  225. bool _can_use_native_popup();
  226. void _native_popup();
  227. void _native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter);
  228. void _native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options);
  229. bool _is_open_should_be_disabled();
  230. TypedArray<Dictionary> _get_options() const;
  231. void _update_option_controls();
  232. void _option_changed_checkbox_toggled(bool p_pressed, const String &p_name);
  233. void _option_changed_item_selected(int p_idx, const String &p_name);
  234. virtual void _post_popup() override;
  235. protected:
  236. void _validate_property(PropertyInfo &p_property) const;
  237. void _notification(int p_what);
  238. bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
  239. bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
  240. void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
  241. bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
  242. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
  243. static void _bind_methods();
  244. public:
  245. virtual void set_visible(bool p_visible) override;
  246. virtual void popup(const Rect2i &p_rect = Rect2i()) override;
  247. void popup_file_dialog();
  248. void clear_filters();
  249. void add_filter(const String &p_filter, const String &p_description = "");
  250. void set_filters(const Vector<String> &p_filters);
  251. Vector<String> get_filters() const;
  252. void clear_filename_filter();
  253. void set_filename_filter(const String &p_filename_filter);
  254. String get_filename_filter() const;
  255. void set_enable_multiple_selection(bool p_enable);
  256. Vector<String> get_selected_files() const;
  257. String get_current_dir() const;
  258. String get_current_file() const;
  259. String get_current_path() const;
  260. void set_current_dir(const String &p_dir);
  261. void set_current_file(const String &p_file);
  262. void set_current_path(const String &p_path);
  263. String get_option_name(int p_option) const;
  264. Vector<String> get_option_values(int p_option) const;
  265. int get_option_default(int p_option) const;
  266. void set_option_name(int p_option, const String &p_name);
  267. void set_option_values(int p_option, const Vector<String> &p_values);
  268. void set_option_default(int p_option, int p_index);
  269. void add_option(const String &p_name, const Vector<String> &p_values, int p_index);
  270. void set_option_count(int p_count);
  271. int get_option_count() const;
  272. Dictionary get_selected_options() const;
  273. void set_root_subfolder(const String &p_root);
  274. String get_root_subfolder() const;
  275. void set_mode_overrides_title(bool p_override);
  276. bool is_mode_overriding_title() const;
  277. void set_use_native_dialog(bool p_native);
  278. bool get_use_native_dialog() const;
  279. void set_file_mode(FileMode p_mode);
  280. FileMode get_file_mode() const;
  281. VBoxContainer *get_vbox() { return main_vbox; }
  282. LineEdit *get_line_edit() { return filename_edit; }
  283. void set_access(Access p_access);
  284. Access get_access() const;
  285. void set_show_hidden_files(bool p_show);
  286. bool is_showing_hidden_files() const;
  287. void set_show_filename_filter(bool p_show);
  288. bool get_show_filename_filter() const;
  289. static void set_default_show_hidden_files(bool p_show);
  290. void invalidate();
  291. void deselect_all();
  292. FileDialog();
  293. ~FileDialog();
  294. };
  295. VARIANT_ENUM_CAST(FileDialog::FileMode);
  296. VARIANT_ENUM_CAST(FileDialog::Access);