Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/file_dialog.h
9896 views
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
};
131
132
enum ItemMenu {
133
ITEM_MENU_COPY_PATH,
134
ITEM_MENU_SHOW_IN_EXPLORER,
135
ITEM_MENU_SHOW_BUNDLE_CONTENT,
136
};
137
138
enum Customization {
139
CUSTOMIZATION_HIDDEN_FILES,
140
CUSTOMIZATION_CREATE_FOLDER,
141
CUSTOMIZATION_FILE_FILTER,
142
CUSTOMIZATION_FILE_SORT,
143
CUSTOMIZATION_FAVORITES,
144
CUSTOMIZATION_RECENT,
145
CUSTOMIZATION_LAYOUT,
146
CUSTOMIZATION_MAX
147
};
148
149
typedef Ref<Texture2D> (*GetIconFunc)(const String &);
150
typedef void (*RegisterFunc)(FileDialog *);
151
152
inline static GetIconFunc get_icon_func = nullptr;
153
inline static RegisterFunc register_func = nullptr;
154
inline static RegisterFunc unregister_func = nullptr;
155
156
private:
157
static inline PropertyListHelper base_property_helper;
158
PropertyListHelper property_helper;
159
160
inline static bool default_show_hidden_files = false;
161
bool show_hidden_files = false;
162
bool use_native_dialog = false;
163
bool customization_flags[CUSTOMIZATION_MAX]; // Initialized to true in the constructor.
164
165
inline static LocalVector<String> global_favorites;
166
inline static LocalVector<String> global_recents;
167
168
Access access = ACCESS_RESOURCES;
169
FileMode mode = FILE_MODE_SAVE_FILE;
170
DisplayMode display_mode = DISPLAY_THUMBNAILS;
171
FileSortOption file_sort = FileSortOption::NAME;
172
Ref<DirAccess> dir_access;
173
174
Vector<String> filters;
175
Vector<String> processed_filters;
176
177
Vector<Option> options;
178
Dictionary selected_options;
179
bool options_dirty = false;
180
181
String file_name_filter;
182
bool show_filename_filter = false;
183
184
Vector<String> local_history;
185
int local_history_pos = 0;
186
187
bool mode_overrides_title = true;
188
String root_subfolder;
189
String root_prefix;
190
String full_dir;
191
192
bool is_invalidating = false;
193
194
VBoxContainer *main_vbox = nullptr;
195
196
Button *dir_prev = nullptr;
197
Button *dir_next = nullptr;
198
Button *dir_up = nullptr;
199
200
HBoxContainer *drives_container = nullptr;
201
OptionButton *drives = nullptr;
202
LineEdit *directory_edit = nullptr;
203
HBoxContainer *shortcuts_container = nullptr;
204
205
Button *refresh_button = nullptr;
206
Button *favorite_button = nullptr;
207
HBoxContainer *make_dir_container = nullptr;
208
Button *make_dir_button = nullptr;
209
210
Button *show_hidden = nullptr;
211
VSeparator *show_hidden_separator = nullptr;
212
HBoxContainer *layout_container = nullptr;
213
VSeparator *layout_separator = nullptr;
214
Button *thumbnail_mode_button = nullptr;
215
Button *list_mode_button = nullptr;
216
Button *show_filename_filter_button = nullptr;
217
MenuButton *file_sort_button = nullptr;
218
219
VBoxContainer *favorite_vbox = nullptr;
220
Button *fav_up_button = nullptr;
221
Button *fav_down_button = nullptr;
222
ItemList *favorite_list = nullptr;
223
VBoxContainer *recent_vbox = nullptr;
224
ItemList *recent_list = nullptr;
225
226
ItemList *file_list = nullptr;
227
Label *message = nullptr;
228
PopupMenu *item_menu = nullptr;
229
230
HBoxContainer *filename_filter_box = nullptr;
231
LineEdit *filename_filter = nullptr;
232
233
HBoxContainer *file_box = nullptr;
234
LineEdit *filename_edit = nullptr;
235
OptionButton *filter = nullptr;
236
237
FlowContainer *flow_checkbox_options = nullptr;
238
GridContainer *grid_select_options = nullptr;
239
240
ConfirmationDialog *make_dir_dialog = nullptr;
241
LineEdit *new_dir_name = nullptr;
242
AcceptDialog *mkdirerr = nullptr;
243
AcceptDialog *exterr = nullptr;
244
ConfirmationDialog *confirm_save = nullptr;
245
246
struct ThemeCache {
247
int thumbnail_size = 64;
248
249
Ref<Texture2D> parent_folder;
250
Ref<Texture2D> forward_folder;
251
Ref<Texture2D> back_folder;
252
Ref<Texture2D> reload;
253
Ref<Texture2D> toggle_hidden;
254
Ref<Texture2D> toggle_filename_filter;
255
Ref<Texture2D> thumbnail_mode;
256
Ref<Texture2D> list_mode;
257
Ref<Texture2D> folder;
258
Ref<Texture2D> file;
259
Ref<Texture2D> create_folder;
260
Ref<Texture2D> sort;
261
Ref<Texture2D> favorite;
262
Ref<Texture2D> favorite_up;
263
Ref<Texture2D> favorite_down;
264
Ref<Texture2D> file_thumbnail;
265
Ref<Texture2D> folder_thumbnail;
266
267
Color folder_icon_color;
268
Color file_icon_color;
269
Color file_disabled_color;
270
271
Color icon_normal_color;
272
Color icon_hover_color;
273
Color icon_focus_color;
274
Color icon_pressed_color;
275
} theme_cache;
276
277
void update_dir();
278
void update_file_name();
279
void update_file_list();
280
void update_filename_filter();
281
void update_filename_filter_gui();
282
void update_filters();
283
void update_customization();
284
285
void _item_menu_id_pressed(int p_option);
286
void _empty_clicked(const Vector2 &p_pos, MouseButton p_button);
287
void _item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_button);
288
289
void _focus_file_text();
290
291
int _get_selected_file_idx();
292
void _file_list_multi_selected(int p_item, bool p_selected);
293
void _file_list_selected(int p_item);
294
void _file_list_item_activated(int p_item);
295
296
void _select_drive(int p_idx);
297
void _dir_submitted(String p_dir);
298
void _file_submitted(const String &p_file);
299
void _action_pressed();
300
void _save_confirm_pressed();
301
void _cancel_pressed();
302
void _filter_selected(int);
303
void _filename_filter_changed();
304
void _filename_filter_selected();
305
void _file_list_select_first();
306
void _make_dir();
307
void _make_dir_confirm();
308
void _go_up();
309
void _go_back();
310
void _go_forward();
311
void _push_history();
312
313
void _change_dir(const String &p_new_dir);
314
void _update_drives(bool p_select = true);
315
void _sort_option_selected(int p_option);
316
317
void _favorite_selected(int p_item);
318
void _favorite_pressed();
319
void _favorite_move_up();
320
void _favorite_move_down();
321
void _update_favorite_list();
322
void _update_fav_buttons();
323
324
void _recent_selected(int p_item);
325
void _save_to_recent();
326
void _update_recent_list();
327
bool _path_matches_access(const String &p_path) const;
328
329
void _invalidate();
330
void _setup_button(Button *p_button, const Ref<Texture2D> &p_icon);
331
void _update_make_dir_visible();
332
333
virtual void shortcut_input(const Ref<InputEvent> &p_event) override;
334
335
bool _can_use_native_popup();
336
void _native_popup();
337
void _native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter);
338
void _native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options);
339
340
bool _is_open_should_be_disabled();
341
342
TypedArray<Dictionary> _get_options() const;
343
void _update_option_controls();
344
void _option_changed_checkbox_toggled(bool p_pressed, const String &p_name);
345
void _option_changed_item_selected(int p_idx, const String &p_name);
346
347
virtual void _post_popup() override;
348
349
protected:
350
void _validate_property(PropertyInfo &p_property) const;
351
void _notification(int p_what);
352
bool _set(const StringName &p_name, const Variant &p_value) { return property_helper.property_set_value(p_name, p_value); }
353
bool _get(const StringName &p_name, Variant &r_ret) const { return property_helper.property_get_value(p_name, r_ret); }
354
void _get_property_list(List<PropertyInfo> *p_list) const { property_helper.get_property_list(p_list); }
355
bool _property_can_revert(const StringName &p_name) const { return property_helper.property_can_revert(p_name); }
356
bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return property_helper.property_get_revert(p_name, r_property); }
357
static void _bind_methods();
358
359
public:
360
virtual void set_visible(bool p_visible) override;
361
virtual void popup(const Rect2i &p_rect = Rect2i()) override;
362
363
void popup_file_dialog();
364
void clear_filters();
365
void add_filter(const String &p_filter, const String &p_description = "");
366
void set_filters(const Vector<String> &p_filters);
367
Vector<String> get_filters() const;
368
void clear_filename_filter();
369
void set_filename_filter(const String &p_filename_filter);
370
String get_filename_filter() const;
371
372
void set_enable_multiple_selection(bool p_enable);
373
Vector<String> get_selected_files() const;
374
375
String get_current_dir() const;
376
String get_current_file() const;
377
String get_current_path() const;
378
void set_current_dir(const String &p_dir);
379
void set_current_file(const String &p_file);
380
void set_current_path(const String &p_path);
381
382
String get_option_name(int p_option) const;
383
Vector<String> get_option_values(int p_option) const;
384
int get_option_default(int p_option) const;
385
void set_option_name(int p_option, const String &p_name);
386
void set_option_values(int p_option, const Vector<String> &p_values);
387
void set_option_default(int p_option, int p_index);
388
389
void add_option(const String &p_name, const Vector<String> &p_values, int p_index);
390
391
void set_option_count(int p_count);
392
int get_option_count() const;
393
394
Dictionary get_selected_options() const;
395
396
void set_root_subfolder(const String &p_root);
397
String get_root_subfolder() const;
398
399
void set_mode_overrides_title(bool p_override);
400
bool is_mode_overriding_title() const;
401
402
void set_use_native_dialog(bool p_native);
403
bool get_use_native_dialog() const;
404
405
void set_file_mode(FileMode p_mode);
406
FileMode get_file_mode() const;
407
408
void set_display_mode(DisplayMode p_mode);
409
DisplayMode get_display_mode() const;
410
411
void set_customization_flag_enabled(Customization p_flag, bool p_enabled);
412
bool is_customization_flag_enabled(Customization p_flag) const;
413
414
VBoxContainer *get_vbox() { return main_vbox; }
415
LineEdit *get_line_edit() { return filename_edit; }
416
417
void set_access(Access p_access);
418
Access get_access() const;
419
420
void set_show_hidden_files(bool p_show);
421
bool is_showing_hidden_files() const;
422
void set_show_filename_filter(bool p_show);
423
bool get_show_filename_filter() const;
424
425
static void set_default_show_hidden_files(bool p_show);
426
427
void invalidate();
428
429
void deselect_all();
430
431
FileDialog();
432
~FileDialog();
433
};
434
435
VARIANT_ENUM_CAST(FileDialog::FileMode);
436
VARIANT_ENUM_CAST(FileDialog::Access);
437
VARIANT_ENUM_CAST(FileDialog::DisplayMode);
438
VARIANT_ENUM_CAST(FileDialog::Customization);
439
440