file_dialog.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 invalidated = true;
  93. struct ThemeCache {
  94. Ref<Texture2D> parent_folder;
  95. Ref<Texture2D> forward_folder;
  96. Ref<Texture2D> back_folder;
  97. Ref<Texture2D> reload;
  98. Ref<Texture2D> toggle_hidden;
  99. Ref<Texture2D> folder;
  100. Ref<Texture2D> file;
  101. Color folder_icon_color;
  102. Color file_icon_color;
  103. Color file_disabled_color;
  104. Color icon_normal_color;
  105. Color icon_hover_color;
  106. Color icon_focus_color;
  107. Color icon_pressed_color;
  108. } theme_cache;
  109. void update_dir();
  110. void update_file_name();
  111. void update_file_list();
  112. void update_filters();
  113. void _focus_file_text();
  114. void _tree_multi_selected(Object *p_object, int p_cell, bool p_selected);
  115. void _tree_selected();
  116. void _select_drive(int p_idx);
  117. void _tree_item_activated();
  118. void _dir_submitted(String p_dir);
  119. void _file_submitted(const String &p_file);
  120. void _action_pressed();
  121. void _save_confirm_pressed();
  122. void _cancel_pressed();
  123. void _filter_selected(int);
  124. void _make_dir();
  125. void _make_dir_confirm();
  126. void _go_up();
  127. void _go_back();
  128. void _go_forward();
  129. void _change_dir(const String &p_new_dir);
  130. void _update_drives(bool p_select = true);
  131. virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
  132. bool _is_open_should_be_disabled();
  133. virtual void _post_popup() override;
  134. protected:
  135. virtual void _update_theme_item_cache() override;
  136. void _notification(int p_what);
  137. static void _bind_methods();
  138. //bind helpers
  139. public:
  140. void popup_file_dialog();
  141. void clear_filters();
  142. void add_filter(const String &p_filter, const String &p_description = "");
  143. void set_filters(const Vector<String> &p_filters);
  144. Vector<String> get_filters() const;
  145. void set_enable_multiple_selection(bool p_enable);
  146. Vector<String> get_selected_files() const;
  147. String get_current_dir() const;
  148. String get_current_file() const;
  149. String get_current_path() const;
  150. void set_current_dir(const String &p_dir);
  151. void set_current_file(const String &p_file);
  152. void set_current_path(const String &p_path);
  153. void set_root_subfolder(const String &p_root);
  154. String get_root_subfolder() const;
  155. void set_mode_overrides_title(bool p_override);
  156. bool is_mode_overriding_title() const;
  157. void set_file_mode(FileMode p_mode);
  158. FileMode get_file_mode() const;
  159. VBoxContainer *get_vbox();
  160. LineEdit *get_line_edit() { return file; }
  161. void set_access(Access p_access);
  162. Access get_access() const;
  163. void set_show_hidden_files(bool p_show);
  164. bool is_showing_hidden_files() const;
  165. static void set_default_show_hidden_files(bool p_show);
  166. void invalidate();
  167. void deselect_all();
  168. FileDialog();
  169. ~FileDialog();
  170. };
  171. VARIANT_ENUM_CAST(FileDialog::FileMode);
  172. VARIANT_ENUM_CAST(FileDialog::Access);
  173. #endif // FILE_DIALOG_H