Debian Codegraph @

scene/gui/file_dialog.h

raw symbols SCIP
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
31 #pragma once
32
33 #include "scene/gui/dialogs.h"
34 #include "scene/property_list_helper.h"
35
36 class DirAccess;
37 class FlowContainer;
38 class GridContainer;
39 class HBoxContainer;
40 class ItemList;
41 class LineEdit;
42 class MenuButton;
43 class OptionButton;
44 class PopupMenu;
45 class VBoxContainer;
46 class VSeparator;
47
48 class FileDialog : public ConfirmationDialog {
49 GDCLASS(FileDialog, ConfirmationDialog);
50
51 inline static constexpr int MAX_RECENTS = 20;
52
53 struct Option {
54 String name;
55 Vector<String> values;
56 int default_idx = 0;
57 };
58
59 struct DirInfo {
60 String name;
61 uint64_t modified_time = 0;
62 bool bundle = false;
63
64 struct NameComparator {
65 bool operator()(const DirInfo &p_a, const DirInfo &p_b) const {
66 return FileNoCaseComparator()(p_a.name, p_b.name);
67 }
68 };
69
70 struct TimeComparator {
71 bool operator()(const DirInfo &p_a, const DirInfo &p_b) const {
72 return p_a.modified_time > p_b.modified_time;
73 }
74 };
75 };
76
77 struct FileInfo {
78 String name;
79 String match_string;
80 String type_sort;
81 uint64_t modified_time = 0;
82
83 struct NameComparator {
84 bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
85 return FileNoCaseComparator()(p_a.name, p_b.name);
86 }
87 };
88
89 struct TypeComparator {
90 bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
91 return FileNoCaseComparator()(p_a.type_sort, p_b.type_sort);
92 }
93 };
94
95 struct TimeComparator {
96 bool operator()(const FileInfo &p_a, const FileInfo &p_b) const {
97 return p_a.modified_time > p_b.modified_time;
98 }
99 };
100 };
101
102 enum class FileSortOption {
103 NAME,
104 NAME_REVERSE,
105 TYPE,
106 TYPE_REVERSE,
107 MODIFIED_TIME,
108 MODIFIED_TIME_REVERSE,
109 MAX
110 };
111
112 public:
113 enum Access {
114 ACCESS_RESOURCES,
115 ACCESS_USERDATA,
116 ACCESS_FILESYSTEM,
117 };
118
119 enum FileMode {
120 FILE_MODE_OPEN_FILE,
121 FILE_MODE_OPEN_FILES,
122 FILE_MODE_OPEN_DIR,
123 FILE_MODE_OPEN_ANY,
124 FILE_MODE_SAVE_FILE,
125 };
126
127 enum DisplayMode {
128 DISPLAY_THUMBNAILS,
129 DISPLAY_LIST,
130 DISPLAY_MAX
131 };
132
133 enum ItemMenu {
134 ITEM_MENU_COPY_PATH,
135 ITEM_MENU_DELETE,
136 ITEM_MENU_REFRESH,
137 ITEM_MENU_NEW_FOLDER,
138 ITEM_MENU_SHOW_IN_EXPLORER,
139 ITEM_MENU_SHOW_BUNDLE_CONTENT,
140 // Not in the menu, only for shortcuts.
141 ITEM_MENU_GO_UP,
142 ITEM_MENU_TOGGLE_HIDDEN,
143 ITEM_MENU_FIND,
144 ITEM_MENU_FOCUS_PATH,
145 };
146
147 enum Customization {
148 CUSTOMIZATION_HIDDEN_FILES,
149 CUSTOMIZATION_CREATE_FOLDER,
150 CUSTOMIZATION_FILE_FILTER,
151 CUSTOMIZATION_FILE_SORT,
152 CUSTOMIZATION_FAVORITES,
153 CUSTOMIZATION_RECENT,
154 CUSTOMIZATION_LAYOUT,
155 CUSTOMIZATION_OVERWRITE_WARNING,
156 CUSTOMIZATION_DELETE,
157 CUSTOMIZATION_MAX
158 };
159
160 typedef void (*RegisterFunc)(FileDialog *);
161
162 inline static Callable get_icon_callback;
163 inline static Callable get_thumbnail_callback;
164
165 inline static RegisterFunc register_func = nullptr;
166 inline static RegisterFunc unregister_func = nullptr;
167
168 private:
169 static inline PropertyListHelper base_property_helper;
170 PropertyListHelper property_helper;
171
172 inline static bool default_show_hidden_files = false;
173 static inline DisplayMode default_display_mode = DISPLAY_THUMBNAILS;
174 bool show_hidden_files = false;
175 bool use_native_dialog = false;
176 bool can_create_folders = true;
177 bool customization_flags[CUSTOMIZATION_MAX]; // Initialized to true in the constructor.
178
179 HashMap<ItemMenu, Ref<Shortcut>> action_shortcuts;
180
181 inline static LocalVector<String> global_favorites;
182 inline static LocalVector<String> global_recents;
183
184 Access access = ACCESS_RESOURCES;
185 FileMode mode = FILE_MODE_SAVE_FILE;
186 DisplayMode display_mode = DISPLAY_THUMBNAILS;
187 FileSortOption file_sort = FileSortOption::NAME;
188
189 Vector<String> filters;
190 Vector<String> processed_filters;
191
192 Vector<Option> options;
193 Dictionary selected_options;
194 bool options_dirty = false;
195
196 String file_name_filter;
197 bool show_filename_filter = false;
198
199 Vector<String> local_history;
200 int local_history_pos = 0;
201
202 bool mode_overrides_title = true;
203 String root_subfolder;
204 String root_prefix;
205 String full_dir;
206
207 bool is_invalidating = false;
208
209 VBoxContainer *main_vbox = nullptr;
210
211 Button *dir_prev = nullptr;
212 Button *dir_next = nullptr;
213 Button *dir_up = nullptr;
214
215 HBoxContainer *drives_container = nullptr;
216 OptionButton *drives = nullptr;
217 LineEdit *directory_edit = nullptr;
218 HBoxContainer *shortcuts_container = nullptr;
219
220 Button *refresh_button = nullptr;
221 Button *favorite_button = nullptr;
222 HBoxContainer *make_dir_container = nullptr;
223 Button *make_dir_button = nullptr;
224
225 Button *show_hidden = nullptr;
226 VSeparator *show_hidden_separator = nullptr;
227 HBoxContainer *layout_container = nullptr;
228 VSeparator *layout_separator = nullptr;
229 Button *thumbnail_mode_button = nullptr;
230 Button *list_mode_button = nullptr;
231 Button *show_filename_filter_button = nullptr;
232 MenuButton *file_sort_button = nullptr;
233
234 VBoxContainer *favorite_vbox = nullptr;
235 Button *fav_up_button = nullptr;
236 Button *fav_down_button = nullptr;
237 ItemList *favorite_list = nullptr;
238 VBoxContainer *recent_vbox = nullptr;
239 ItemList *recent_list = nullptr;
240
241 ItemList *file_list = nullptr;
242 Label *message = nullptr;
243 PopupMenu *item_menu = nullptr;
244
245 HBoxContainer *filename_filter_box = nullptr;
246 LineEdit *filename_filter = nullptr;
247
248 HBoxContainer *file_box = nullptr;
249 LineEdit *filename_edit = nullptr;
250 OptionButton *filter = nullptr;
251
252 FlowContainer *flow_checkbox_options = nullptr;
253 GridContainer *grid_select_options = nullptr;
254
255 ConfirmationDialog *delete_dialog = nullptr;
256 ConfirmationDialog *make_dir_dialog = nullptr;
257 LineEdit *new_dir_name = nullptr;
258 AcceptDialog *mkdirerr = nullptr;
259 AcceptDialog *exterr = nullptr;
260 ConfirmationDialog *confirm_save = nullptr;
261
262 struct ThemeCache {
263 int thumbnail_size = 64;
264
265 Ref<Texture2D> parent_folder;
266 Ref<Texture2D> forward_folder;
267 Ref<Texture2D> back_folder;
268 Ref<Texture2D> reload;
269 Ref<Texture2D> toggle_hidden;
270 Ref<Texture2D> toggle_filename_filter;
271 Ref<Texture2D> thumbnail_mode;
272 Ref<Texture2D> list_mode;
273 Ref<Texture2D> folder;
274 Ref<Texture2D> file;
275 Ref<Texture2D> create_folder;
276 Ref<Texture2D> sort;
277 Ref<Texture2D> favorite;
278 Ref<Texture2D> favorite_up;
279 Ref<Texture2D> favorite_down;
280 Ref<Texture2D> file_thumbnail;
281 Ref<Texture2D> folder_thumbnail;
282
283 Color folder_icon_color;
284 Color file_icon_color;
285 Color file_disabled_color;
286
287 Color icon_normal_color;
288 Color icon_hover_color;
289 Color icon_focus_color;
290 Color icon_pressed_color;
291 } theme_cache;
292
293 void update_dir();
294 void update_file_name();
295 void update_file_list();
296 void update_filename_filter();
297 void update_filename_filter_gui();
298 void update_filters();
299 void update_customization();
300
301 void _empty_clicked(const Vector2 &p_pos, MouseButton p_button);
302 void _item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_button);
303 void _popup_menu(const Vector2 &p_pos, int p_for_item);
304
305 void _focus_file_text();
306
307 int _get_selected_file_idx();
308 String _get_item_path(int p_idx) const;
309 void _file_list_multi_selected(int p_item, bool p_selected);
310 void _file_list_selected(int p_item);
311 void _file_list_item_activated(int p_item);
312
313 void _select_drive(int p_idx);
314 void _dir_submitted(String p_dir);
315 void _action_pressed();
316 void _save_confirm_pressed();
317 void _cancel_pressed();
318 void _filter_selected(int);
319 void _filename_filter_changed();
320 void _filename_filter_selected();
321 void _file_list_select_first();
322 void _delete_confirm();
323 void _make_dir();
324 void _make_dir_confirm();
325 void _go_up();
326 void _go_back();
327 void _go_forward();
328 void _push_history();
329
330 void _change_dir(const String &p_new_dir);
331 void _update_drives(bool p_select = true);
332 void _sort_option_selected(int p_option);
333
334 void _favorite_selected(int p_item);
335 void _favorite_pressed();
336 void _favorite_move_up();
337 void _favorite_move_down();
338 void _update_favorite_list();
339 void _update_fav_buttons();
340
341 void _recent_selected(int p_item);
342 void _save_to_recent();
343 void _update_recent_list();
344 bool _path_matches_access(const String &p_path) const;
345
346 void _invalidate();
347 void _setup_button(Button *p_button, const Ref<Texture2D> &p_icon);
348 void _update_make_dir_visible();
349
350 virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
351
352 void _native_popup();
353 void _native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter);
354 void _native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options);
355
356 bool _is_open_should_be_disabled();
357 void _thumbnail_callback(const Ref<Texture2D> &p_texture, const String &p_path);
358
359 TypedArray<Dictionary> _get_options() const;
360 void _update_option_controls();
361 void _option_changed_checkbox_toggled(bool p_pressed, const String &p_name);
362 void _option_changed_item_selected(int p_idx, const String &p_name);
363
364 virtual void _post_popup() override;
365
366 protected:
367 Ref<DirAccess> dir_access;
368
369 bool favorites_changed = false;
370 bool recents_changed = false;
371
372 bool _can_use_native_popup() const;
373 virtual void _item_menu_id_pressed(int p_option);
374 virtual void _dir_contents_changed() {}
375
376 virtual bool _should_use_native_popup() const;
377 virtual bool _should_hide_file(const String &p_file) const { return false; }
378 virtual Color _get_folder_color(const String &p_path) const { return theme_cache.folder_icon_color; }
379 virtual Vector2i _get_list_mode_icon_size() const;
380
381 virtual void _popup_base(const Rect2i &p_screen_rect = Rect2i()) override;
382 void _clear_changed_status();
383
384 void _validate_property(PropertyInfo &p_property) const;
385 void _notification(int p_what);
386 bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
387 bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
388 void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
389 bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
390 bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
391
392 static void _bind_methods();
393
394 #ifndef DISABLE_DEPRECATED
395 void _add_filter_bind_compat_111439(const String &p_filter, const String &p_description = "");
396
397 static void _bind_compatibility_methods();
398 #endif
399
400 public:
401 virtual void set_visible(bool p_visible) override;
402
403 void popup_file_dialog();
404 void clear_filters();
405 void add_filter(const String &p_filter, const String &p_description = "", const String &p_mime = "");
406 void set_filters(const Vector<String> &p_filters);
407 Vector<String> get_filters() const;
408 void clear_filename_filter();
409 void set_filename_filter(const String &p_filename_filter);
410 String get_filename_filter() const;
411
412 Vector<String> get_selected_files() const;
413
414 String get_current_dir() const;
415 String get_current_file() const;
416 String get_current_path() const;
417 void set_current_dir(const String &p_dir);
418 void set_current_file(const String &p_file);
419 void set_current_path(const String &p_path);
420
421 String get_option_name(int p_option) const;
422 Vector<String> get_option_values(int p_option) const;
423 int get_option_default(int p_option) const;
424 void set_option_name(int p_option, const String &p_name);
425 void set_option_values(int p_option, const Vector<String> &p_values);
426 void set_option_default(int p_option, int p_index);
427
428 void add_option(const String &p_name, const Vector<String> &p_values, int p_index);
429
430 void set_option_count(int p_count);
431 int get_option_count() const;
432
433 Dictionary get_selected_options() const;
434
435 void set_root_subfolder(const String &p_root);
436 String get_root_subfolder() const;
437
438 void set_mode_overrides_title(bool p_override);
439 bool is_mode_overriding_title() const;
440
441 void set_use_native_dialog(bool p_native);
442 bool get_use_native_dialog() const;
443
444 void set_file_mode(FileMode p_mode);
445 FileMode get_file_mode() const;
446
447 void set_display_mode(DisplayMode p_mode);
448 DisplayMode get_display_mode() const;
449
450 static void set_favorite_list(const PackedStringArray &p_favorites);
451 static PackedStringArray get_favorite_list();
452
453 static void set_recent_list(const PackedStringArray &p_recents);
454 static PackedStringArray get_recent_list();
455
456 void set_customization_flag_enabled(Customization p_flag, bool p_enabled);
457 bool is_customization_flag_enabled(Customization p_flag) const;
458
459 VBoxContainer *get_vbox() { return main_vbox; }
460 LineEdit *get_line_edit() { return filename_edit; }
461 ItemList *get_file_item_list() { return file_list; }
462
463 void set_access(Access p_access);
464 Access get_access() const;
465
466 void set_show_hidden_files(bool p_show);
467 bool is_showing_hidden_files() const;
468 void set_show_filename_filter(bool p_show);
469 bool get_show_filename_filter() const;
470
471 static void set_default_show_hidden_files(bool p_show);
472 static void set_default_display_mode(DisplayMode p_mode);
473
474 static void set_get_icon_callback(const Callable &p_callback);
475 static void set_get_thumbnail_callback(const Callable &p_callback);
476
477 void invalidate();
478
479 void deselect_all();
480
481 FileDialog();
482 ~FileDialog();
483 };
484
485 VARIANT_ENUM_CAST(FileDialog::FileMode);
486 VARIANT_ENUM_CAST(FileDialog::Access);
487 VARIANT_ENUM_CAST(FileDialog::DisplayMode);
488 VARIANT_ENUM_CAST(FileDialog::Customization);