file_dialog.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. class FileDialog : public ConfirmationDialog {
  39. GDCLASS(FileDialog, ConfirmationDialog);
  40. public:
  41. enum Access {
  42. ACCESS_RESOURCES,
  43. ACCESS_USERDATA,
  44. ACCESS_FILESYSTEM
  45. };
  46. enum FileMode {
  47. FILE_MODE_OPEN_FILE,
  48. FILE_MODE_OPEN_FILES,
  49. FILE_MODE_OPEN_DIR,
  50. FILE_MODE_OPEN_ANY,
  51. FILE_MODE_SAVE_FILE
  52. };
  53. typedef Ref<Texture2D> (*GetIconFunc)(const String &);
  54. typedef void (*RegisterFunc)(FileDialog *);
  55. static GetIconFunc get_icon_func;
  56. static RegisterFunc register_func;
  57. static RegisterFunc unregister_func;
  58. private:
  59. ConfirmationDialog *makedialog = nullptr;
  60. LineEdit *makedirname = nullptr;
  61. Button *makedir = nullptr;
  62. Access access = ACCESS_RESOURCES;
  63. VBoxContainer *vbox = nullptr;
  64. FileMode mode;
  65. LineEdit *dir = nullptr;
  66. HBoxContainer *drives_container = nullptr;
  67. HBoxContainer *shortcuts_container = nullptr;
  68. OptionButton *drives = nullptr;
  69. Tree *tree = nullptr;
  70. HBoxContainer *file_box = nullptr;
  71. LineEdit *file = nullptr;
  72. OptionButton *filter = nullptr;
  73. AcceptDialog *mkdirerr = nullptr;
  74. AcceptDialog *exterr = nullptr;
  75. Ref<DirAccess> dir_access;
  76. ConfirmationDialog *confirm_save = nullptr;
  77. Label *message = nullptr;
  78. Button *dir_prev = nullptr;
  79. Button *dir_next = nullptr;
  80. Button *dir_up = nullptr;
  81. Button *refresh = nullptr;
  82. Button *show_hidden = nullptr;
  83. Vector<String> filters;
  84. Vector<String> local_history;
  85. int local_history_pos = 0;
  86. void _push_history();
  87. bool mode_overrides_title = true;
  88. String root_subfolder;
  89. String root_prefix;
  90. static bool default_show_hidden_files;
  91. bool show_hidden_files = false;
  92. bool use_native_dialog = false;
  93. bool is_invalidating = false;
  94. struct ThemeCache {
  95. Ref<Texture2D> parent_folder;
  96. Ref<Texture2D> forward_folder;
  97. Ref<Texture2D> back_folder;
  98. Ref<Texture2D> reload;
  99. Ref<Texture2D> toggle_hidden;
  100. Ref<Texture2D> folder;
  101. Ref<Texture2D> file;
  102. Color folder_icon_color;
  103. Color file_icon_color;
  104. Color file_disabled_color;
  105. Color icon_normal_color;
  106. Color icon_hover_color;
  107. Color icon_focus_color;
  108. Color icon_pressed_color;
  109. } theme_cache;
  110. void update_dir();
  111. void update_file_name();
  112. void update_file_list();
  113. void update_filters();
  114. void _focus_file_text();
  115. void _tree_multi_selected(Object *p_object, int p_cell, bool p_selected);
  116. void _tree_selected();
  117. void _select_drive(int p_idx);
  118. void _tree_item_activated();
  119. void _dir_submitted(String p_dir);
  120. void _file_submitted(const String &p_file);
  121. void _action_pressed();
  122. void _save_confirm_pressed();
  123. void _cancel_pressed();
  124. void _filter_selected(int);
  125. void _make_dir();
  126. void _make_dir_confirm();
  127. void _go_up();
  128. void _go_back();
  129. void _go_forward();
  130. void _change_dir(const String &p_new_dir);
  131. void _update_drives(bool p_select = true);
  132. void _invalidate();
  133. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  134. void _native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter);
  135. bool _is_open_should_be_disabled();
  136. virtual void _post_popup() override;
  137. protected:
  138. void _validate_property(PropertyInfo &p_property) const;
  139. void _notification(int p_what);
  140. static void _bind_methods();
  141. public:
  142. virtual void set_visible(bool p_visible) override;
  143. virtual void popup(const Rect2i &p_rect = Rect2i()) override;
  144. void popup_file_dialog();
  145. void clear_filters();
  146. void add_filter(const String &p_filter, const String &p_description = "");
  147. void set_filters(const Vector<String> &p_filters);
  148. Vector<String> get_filters() const;
  149. void set_enable_multiple_selection(bool p_enable);
  150. Vector<String> get_selected_files() const;
  151. String get_current_dir() const;
  152. String get_current_file() const;
  153. String get_current_path() const;
  154. void set_current_dir(const String &p_dir);
  155. void set_current_file(const String &p_file);
  156. void set_current_path(const String &p_path);
  157. void set_root_subfolder(const String &p_root);
  158. String get_root_subfolder() const;
  159. void set_mode_overrides_title(bool p_override);
  160. bool is_mode_overriding_title() const;
  161. void set_use_native_dialog(bool p_native);
  162. bool get_use_native_dialog() const;
  163. void set_file_mode(FileMode p_mode);
  164. FileMode get_file_mode() const;
  165. VBoxContainer *get_vbox();
  166. LineEdit *get_line_edit() { return file; }
  167. void set_access(Access p_access);
  168. Access get_access() const;
  169. void set_show_hidden_files(bool p_show);
  170. bool is_showing_hidden_files() const;
  171. static void set_default_show_hidden_files(bool p_show);
  172. void invalidate();
  173. void deselect_all();
  174. FileDialog();
  175. ~FileDialog();
  176. };
  177. VARIANT_ENUM_CAST(FileDialog::FileMode);
  178. VARIANT_ENUM_CAST(FileDialog::Access);
  179. #endif // FILE_DIALOG_H