file_dialog.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*************************************************************************/
  2. /* file_dialog.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 GetIconFunc get_large_icon_func;
  57. static RegisterFunc register_func;
  58. static RegisterFunc unregister_func;
  59. private:
  60. ConfirmationDialog *makedialog;
  61. LineEdit *makedirname;
  62. Button *makedir;
  63. Access access = ACCESS_RESOURCES;
  64. //Button *action;
  65. VBoxContainer *vbox;
  66. FileMode mode;
  67. LineEdit *dir;
  68. HBoxContainer *drives_container;
  69. HBoxContainer *shortcuts_container;
  70. OptionButton *drives;
  71. Tree *tree;
  72. HBoxContainer *file_box;
  73. LineEdit *file;
  74. OptionButton *filter;
  75. AcceptDialog *mkdirerr;
  76. AcceptDialog *exterr;
  77. DirAccess *dir_access;
  78. ConfirmationDialog *confirm_save;
  79. Label *message;
  80. Button *dir_prev;
  81. Button *dir_next;
  82. Button *dir_up;
  83. Button *refresh;
  84. Button *show_hidden;
  85. Vector<String> filters;
  86. Vector<String> local_history;
  87. int local_history_pos = 0;
  88. void _push_history();
  89. bool mode_overrides_title = true;
  90. static bool default_show_hidden_files;
  91. bool show_hidden_files = false;
  92. bool invalidated = true;
  93. void update_dir();
  94. void update_file_name();
  95. void update_file_list();
  96. void update_filters();
  97. void _tree_multi_selected(Object *p_object, int p_cell, bool p_selected);
  98. void _tree_selected();
  99. void _select_drive(int p_idx);
  100. void _tree_item_activated();
  101. void _dir_submitted(String p_dir);
  102. void _file_submitted(const String &p_file);
  103. void _action_pressed();
  104. void _save_confirm_pressed();
  105. void _cancel_pressed();
  106. void _filter_selected(int);
  107. void _make_dir();
  108. void _make_dir_confirm();
  109. void _go_up();
  110. void _go_back();
  111. void _go_forward();
  112. void _update_drives();
  113. virtual void unhandled_input(const Ref<InputEvent> &p_event) override;
  114. bool _is_open_should_be_disabled();
  115. virtual void _post_popup() override;
  116. protected:
  117. void _theme_changed();
  118. void _notification(int p_what);
  119. static void _bind_methods();
  120. //bind helpers
  121. public:
  122. void popup_file_dialog();
  123. void clear_filters();
  124. void add_filter(const String &p_filter);
  125. void set_filters(const Vector<String> &p_filters);
  126. Vector<String> get_filters() const;
  127. void set_enable_multiple_selection(bool p_enable);
  128. Vector<String> get_selected_files() const;
  129. String get_current_dir() const;
  130. String get_current_file() const;
  131. String get_current_path() const;
  132. void set_current_dir(const String &p_dir);
  133. void set_current_file(const String &p_file);
  134. void set_current_path(const String &p_path);
  135. void set_mode_overrides_title(bool p_override);
  136. bool is_mode_overriding_title() const;
  137. void set_file_mode(FileMode p_mode);
  138. FileMode get_file_mode() const;
  139. VBoxContainer *get_vbox();
  140. LineEdit *get_line_edit() { return file; }
  141. void set_access(Access p_access);
  142. Access get_access() const;
  143. void set_show_hidden_files(bool p_show);
  144. bool is_showing_hidden_files() const;
  145. static void set_default_show_hidden_files(bool p_show);
  146. void invalidate();
  147. void deselect_all();
  148. FileDialog();
  149. ~FileDialog();
  150. };
  151. VARIANT_ENUM_CAST(FileDialog::FileMode);
  152. VARIANT_ENUM_CAST(FileDialog::Access);
  153. #endif