| 1 | /**************************************************************************/ |
| 2 | /* file_dialog.cpp */ |
| 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 | #include "file_dialog.h" |
| 32 | #include "file_dialog.compat.inc" |
| 33 | |
| 34 | #include "core/config/project_settings.h" |
| 35 | #include "core/io/dir_access.h" |
| 36 | #include "core/io/file_access.h" |
| 37 | #include "core/os/keyboard.h" |
| 38 | #include "scene/gui/box_container.h" |
| 39 | #include "scene/gui/check_box.h" |
| 40 | #include "scene/gui/flow_container.h" |
| 41 | #include "scene/gui/grid_container.h" |
| 42 | #include "scene/gui/item_list.h" |
| 43 | #include "scene/gui/label.h" |
| 44 | #include "scene/gui/line_edit.h" |
| 45 | #include "scene/gui/menu_button.h" |
| 46 | #include "scene/gui/option_button.h" |
| 47 | #include "scene/gui/separator.h" |
| 48 | #include "scene/gui/split_container.h" |
| 49 | #include "scene/theme/theme_db.h" |
| 50 | |
| 51 | void FileDialog::popup_file_dialog() { |
| 52 | popup_centered_clamped(Vector2(1050, 700) * get_theme_default_base_scale(), 0.8f); |
| 53 | _focus_file_text(); |
| 54 | } |
| 55 | |
| 56 | void FileDialog::_focus_file_text() { |
| 57 | int lp = filename_edit->get_text().rfind_char('.'); |
| 58 | if (lp != -1) { |
| 59 | filename_edit->select(0, lp); |
| 60 | if (filename_edit->is_inside_tree() && !is_part_of_edited_scene()) { |
| 61 | filename_edit->grab_focus(); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void FileDialog::_native_popup() { |
| 67 | // Show native dialog directly. |
| 68 | String root; |
| 69 | if (!root_prefix.is_empty()) { |
| 70 | root = ProjectSettings::get_singleton()->globalize_path(root_prefix); |
| 71 | } else if (access == ACCESS_RESOURCES) { |
| 72 | root = ProjectSettings::get_singleton()->get_resource_path(); |
| 73 | } else if (access == ACCESS_USERDATA) { |
| 74 | root = OS::get_singleton()->get_user_data_dir(); |
| 75 | } |
| 76 | |
| 77 | // Attach native file dialog to first persistent parent window. |
| 78 | Window *w = (is_transient() || is_transient_to_focused()) ? get_parent_visible_window() : nullptr; |
| 79 | while (w && w->get_flag(FLAG_POPUP) && w->get_parent_visible_window()) { |
| 80 | w = w->get_parent_visible_window(); |
| 81 | } |
| 82 | DisplayServer::WindowID wid = w ? w->get_window_id() : DisplayServer::INVALID_WINDOW_ID; |
| 83 | |
| 84 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_EXTRA)) { |
| 85 | DisplayServer::get_singleton()->file_dialog_with_options_show(get_displayed_title(), ProjectSettings::get_singleton()->globalize_path(full_dir), root, filename_edit->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), processed_filters, _get_options(), callable_mp(this, &FileDialog::_native_dialog_cb_with_options), wid); |
| 86 | } else { |
| 87 | DisplayServer::get_singleton()->file_dialog_show(get_displayed_title(), ProjectSettings::get_singleton()->globalize_path(full_dir), filename_edit->get_text().get_file(), show_hidden_files, DisplayServer::FileDialogMode(mode), processed_filters, callable_mp(this, &FileDialog::_native_dialog_cb), wid); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | bool FileDialog::_can_use_native_popup() const { |
| 92 | if (access == ACCESS_RESOURCES || access == ACCESS_USERDATA || options.size() > 0) { |
| 93 | return DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_EXTRA); |
| 94 | } |
| 95 | return DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE); |
| 96 | } |
| 97 | |
| 98 | Vector2i FileDialog::_get_list_mode_icon_size() const { |
| 99 | return theme_cache.file->get_size(); |
| 100 | } |
| 101 | |
| 102 | void FileDialog::_popup_base(const Rect2i &p_screen_rect) { |
| 103 | #ifdef TOOLS_ENABLED |
| 104 | if (is_part_of_edited_scene()) { |
| 105 | ConfirmationDialog::_popup_base(p_screen_rect); |
| 106 | return; |
| 107 | } |
| 108 | #endif |
| 109 | |
| 110 | if (_should_use_native_popup()) { |
| 111 | _native_popup(); |
| 112 | } else { |
| 113 | ConfirmationDialog::_popup_base(p_screen_rect); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void FileDialog::_clear_changed_status() { |
| 118 | favorites_changed = false; |
| 119 | recents_changed = false; |
| 120 | } |
| 121 | |
| 122 | void FileDialog::set_visible(bool p_visible) { |
| 123 | if (p_visible) { |
| 124 | _update_option_controls(); |
| 125 | } |
| 126 | |
| 127 | #ifdef TOOLS_ENABLED |
| 128 | if (is_part_of_edited_scene()) { |
| 129 | ConfirmationDialog::set_visible(p_visible); |
| 130 | return; |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | if (_should_use_native_popup()) { |
| 135 | if (p_visible) { |
| 136 | _native_popup(); |
| 137 | } |
| 138 | } else { |
| 139 | ConfirmationDialog::set_visible(p_visible); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void FileDialog::_native_dialog_cb(bool p_ok, const Vector<String> &p_files, int p_filter) { |
| 144 | _native_dialog_cb_with_options(p_ok, p_files, p_filter, Dictionary()); |
| 145 | } |
| 146 | |
| 147 | void FileDialog::_native_dialog_cb_with_options(bool p_ok, const Vector<String> &p_files, int p_filter, const Dictionary &p_selected_options) { |
| 148 | if (!p_ok) { |
| 149 | filename_edit->set_text(""); |
| 150 | emit_signal(SNAME("canceled")); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | if (p_files.is_empty()) { |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | Vector<String> files = p_files; |
| 159 | if (access != ACCESS_FILESYSTEM) { |
| 160 | for (String &file_name : files) { |
| 161 | file_name = ProjectSettings::get_singleton()->localize_path(file_name); |
| 162 | } |
| 163 | } |
| 164 | selected_options = p_selected_options; |
| 165 | |
| 166 | String f = files[0]; |
| 167 | filter->select(p_filter); |
| 168 | directory_edit->set_text(f.get_base_dir()); |
| 169 | filename_edit->set_text(f.get_file()); |
| 170 | _change_dir(f.get_base_dir()); |
| 171 | |
| 172 | if (mode == FILE_MODE_OPEN_FILES) { |
| 173 | emit_signal(SNAME("files_selected"), files); |
| 174 | } else { |
| 175 | if (mode == FILE_MODE_SAVE_FILE) { |
| 176 | bool valid = false; |
| 177 | |
| 178 | if (p_filter == filter->get_item_count() - 1) { |
| 179 | valid = true; // Match none. |
| 180 | } else if (filters.size() > 1 && p_filter == 0) { |
| 181 | // Match all filters. |
| 182 | for (int i = 0; i < filters.size(); i++) { |
| 183 | String flt = filters[i].get_slicec(';', 0); |
| 184 | for (int j = 0; j < flt.get_slice_count(","); j++) { |
| 185 | String str = flt.get_slicec(',', j).strip_edges(); |
| 186 | if (f.matchn(str)) { |
| 187 | valid = true; |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | if (valid) { |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } else { |
| 196 | int idx = p_filter; |
| 197 | if (filters.size() > 1) { |
| 198 | idx--; |
| 199 | } |
| 200 | if (idx >= 0 && idx < filters.size()) { |
| 201 | String flt = filters[idx].get_slicec(';', 0); |
| 202 | int filter_slice_count = flt.get_slice_count(","); |
| 203 | for (int j = 0; j < filter_slice_count; j++) { |
| 204 | String str = flt.get_slicec(',', j).strip_edges(); |
| 205 | if (f.matchn(str)) { |
| 206 | valid = true; |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | if (!valid && filter_slice_count > 0) { |
| 212 | String str = flt.get_slicec(',', 0).strip_edges(); |
| 213 | f += str.substr(1); |
| 214 | filename_edit->set_text(f.get_file()); |
| 215 | valid = true; |
| 216 | } |
| 217 | } else { |
| 218 | valid = true; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // Add first extension of filter if no valid extension is found. |
| 223 | if (!valid) { |
| 224 | int idx = p_filter; |
| 225 | String flt = filters[idx].get_slicec(';', 0); |
| 226 | String ext = flt.get_slicec(',', 0).strip_edges().get_extension(); |
| 227 | f += "." + ext; |
| 228 | } |
| 229 | emit_signal(SNAME("file_selected"), f); |
| 230 | } else if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && dir_access->file_exists(f)) { |
| 231 | emit_signal(SNAME("file_selected"), f); |
| 232 | } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) { |
| 233 | emit_signal(SNAME("dir_selected"), f); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | bool FileDialog::_should_use_native_popup() const { |
| 239 | return _can_use_native_popup() && (use_native_dialog || OS::get_singleton()->is_sandboxed()); |
| 240 | } |
| 241 | |
| 242 | void FileDialog::_validate_property(PropertyInfo &p_property) const { |
| 243 | if (p_property.name == "dialog_text") { |
| 244 | // File dialogs have a custom layout, and dialog nodes can't have both a text and a layout. |
| 245 | p_property.usage = PROPERTY_USAGE_NONE; |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | void FileDialog::_notification(int p_what) { |
| 250 | switch (p_what) { |
| 251 | case NOTIFICATION_READY: { |
| 252 | #ifdef TOOLS_ENABLED |
| 253 | if (is_part_of_edited_scene()) { |
| 254 | return; |
| 255 | } |
| 256 | #endif |
| 257 | |
| 258 | // Replace the built-in dialog with the native one if it started visible. |
| 259 | if (is_visible() && _should_use_native_popup()) { |
| 260 | ConfirmationDialog::set_visible(false); |
| 261 | _native_popup(); |
| 262 | } |
| 263 | } break; |
| 264 | |
| 265 | case NOTIFICATION_VISIBILITY_CHANGED: { |
| 266 | if (is_visible()) { |
| 267 | _update_favorite_list(); |
| 268 | _update_recent_list(); |
| 269 | invalidate(); // Put it here to preview in the editor. |
| 270 | } else { |
| 271 | callable_mp(this, &FileDialog::_clear_changed_status).call_deferred(); |
| 272 | } |
| 273 | } break; |
| 274 | |
| 275 | case NOTIFICATION_TRANSLATION_CHANGED: { |
| 276 | update_filters(); |
| 277 | [[fallthrough]]; |
| 278 | } |
| 279 | |
| 280 | case Control::NOTIFICATION_LAYOUT_DIRECTION_CHANGED: { |
| 281 | if (main_vbox->is_layout_rtl()) { |
| 282 | _setup_button(dir_prev, theme_cache.forward_folder); |
| 283 | _setup_button(dir_next, theme_cache.back_folder); |
| 284 | } else { |
| 285 | _setup_button(dir_prev, theme_cache.back_folder); |
| 286 | _setup_button(dir_next, theme_cache.forward_folder); |
| 287 | } |
| 288 | } break; |
| 289 | |
| 290 | case NOTIFICATION_THEME_CHANGED: { |
| 291 | favorite_list->set_custom_minimum_size(Vector2(128 * get_theme_default_base_scale(), 0)); |
| 292 | recent_list->set_custom_minimum_size(Vector2(128 * get_theme_default_base_scale(), 0)); |
| 293 | |
| 294 | if (main_vbox->is_layout_rtl()) { |
| 295 | _setup_button(dir_prev, theme_cache.forward_folder); |
| 296 | _setup_button(dir_next, theme_cache.back_folder); |
| 297 | } else { |
| 298 | _setup_button(dir_prev, theme_cache.back_folder); |
| 299 | _setup_button(dir_next, theme_cache.forward_folder); |
| 300 | } |
| 301 | _setup_button(dir_up, theme_cache.parent_folder); |
| 302 | _setup_button(refresh_button, theme_cache.reload); |
| 303 | _setup_button(favorite_button, theme_cache.favorite); |
| 304 | _setup_button(make_dir_button, theme_cache.create_folder); |
| 305 | _setup_button(show_hidden, theme_cache.toggle_hidden); |
| 306 | _setup_button(thumbnail_mode_button, theme_cache.thumbnail_mode); |
| 307 | _setup_button(list_mode_button, theme_cache.list_mode); |
| 308 | _setup_button(show_filename_filter_button, theme_cache.toggle_filename_filter); |
| 309 | _setup_button(file_sort_button, theme_cache.sort); |
| 310 | _setup_button(fav_up_button, theme_cache.favorite_up); |
| 311 | _setup_button(fav_down_button, theme_cache.favorite_down); |
| 312 | invalidate(); |
| 313 | } break; |
| 314 | |
| 315 | case NOTIFICATION_WM_WINDOW_FOCUS_IN: { |
| 316 | if (!is_visible()) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | // Check if the current directory was removed externally (much less likely to happen while the window is focused). |
| 321 | const String previous_dir = get_current_dir(); |
| 322 | while (!dir_access->dir_exists(get_current_dir())) { |
| 323 | _go_up(); |
| 324 | |
| 325 | // In case we can't go further up, use some fallback and break. |
| 326 | if (get_current_dir() == previous_dir) { |
| 327 | _dir_submitted(OS::get_singleton()->get_user_data_dir()); |
| 328 | break; |
| 329 | } |
| 330 | } |
| 331 | } break; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void FileDialog::shortcut_input(const Ref<InputEvent> &p_event) { |
| 336 | if (p_event.is_null() || p_event->is_released() || p_event->is_echo()) { |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | for (const KeyValue<ItemMenu, Ref<Shortcut>> &action : action_shortcuts) { |
| 341 | if (action.value->matches_event(p_event)) { |
| 342 | _item_menu_id_pressed(action.key); |
| 343 | set_input_as_handled(); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | Vector<String> FileDialog::get_selected_files() const { |
| 350 | const String current_dir = dir_access->get_current_dir(); |
| 351 | Vector<String> list; |
| 352 | for (int idx : file_list->get_selected_items()) { |
| 353 | list.push_back(current_dir.path_join(file_list->get_item_text(idx))); |
| 354 | } |
| 355 | return list; |
| 356 | } |
| 357 | |
| 358 | void FileDialog::update_dir() { |
| 359 | full_dir = dir_access->get_current_dir(); |
| 360 | if (root_prefix.is_empty()) { |
| 361 | directory_edit->set_text(dir_access->get_current_dir(false)); |
| 362 | } else { |
| 363 | directory_edit->set_text(dir_access->get_current_dir(false).trim_prefix(root_prefix).trim_prefix("/")); |
| 364 | } |
| 365 | |
| 366 | if (drives->is_visible()) { |
| 367 | if (dir_access->get_current_dir().is_network_share_path()) { |
| 368 | _update_drives(false); |
| 369 | drives->add_item(ETR("Network")); |
| 370 | drives->set_item_disabled(-1, true); |
| 371 | drives->select(drives->get_item_count() - 1); |
| 372 | } else { |
| 373 | int cur = dir_access->get_current_drive(); |
| 374 | for (int i = 0; i < drives->get_item_count(); i++) { |
| 375 | if (drives->get_item_metadata(i).operator int() == cur) { |
| 376 | drives->select(i); |
| 377 | break; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | // Deselect any item, to make "Select Current Folder" button text by default. |
| 384 | deselect_all(); |
| 385 | } |
| 386 | |
| 387 | void FileDialog::_dir_submitted(String p_dir) { |
| 388 | String new_dir = p_dir; |
| 389 | #ifdef WINDOWS_ENABLED |
| 390 | if (root_prefix.is_empty() && drives->is_visible() && !new_dir.is_network_share_path() && new_dir.is_absolute_path() && new_dir.find(":/") == -1 && new_dir.find(":\\") == -1) { |
| 391 | // Non network path without X:/ prefix on Windows, add drive letter. |
| 392 | new_dir = drives->get_item_text(drives->get_selected()).path_join(new_dir); |
| 393 | } |
| 394 | #endif |
| 395 | if (!root_prefix.is_empty()) { |
| 396 | new_dir = root_prefix.path_join(new_dir); |
| 397 | } |
| 398 | _change_dir(new_dir); |
| 399 | if (mode != FILE_MODE_SAVE_FILE) { |
| 400 | filename_edit->set_text(""); |
| 401 | } |
| 402 | _push_history(); |
| 403 | } |
| 404 | |
| 405 | void FileDialog::_save_confirm_pressed() { |
| 406 | _save_to_recent(); |
| 407 | |
| 408 | String f = dir_access->get_current_dir().path_join(filename_edit->get_text()); |
| 409 | hide(); |
| 410 | emit_signal(SNAME("file_selected"), f); |
| 411 | } |
| 412 | |
| 413 | void FileDialog::_post_popup() { |
| 414 | ConfirmationDialog::_post_popup(); |
| 415 | if (mode == FILE_MODE_SAVE_FILE) { |
| 416 | filename_edit->grab_focus(true); |
| 417 | } else { |
| 418 | file_list->grab_focus(true); |
| 419 | } |
| 420 | |
| 421 | // For open dir mode, deselect all items on file dialog open. |
| 422 | if (mode == FILE_MODE_OPEN_DIR) { |
| 423 | deselect_all(); |
| 424 | file_box->set_visible(false); |
| 425 | } else { |
| 426 | file_box->set_visible(true); |
| 427 | } |
| 428 | |
| 429 | local_history.clear(); |
| 430 | local_history_pos = -1; |
| 431 | _push_history(); |
| 432 | } |
| 433 | |
| 434 | void FileDialog::_push_history() { |
| 435 | local_history.resize(local_history_pos + 1); |
| 436 | String new_path = dir_access->get_current_dir(); |
| 437 | if (local_history.is_empty() || new_path != local_history[local_history_pos]) { |
| 438 | local_history.push_back(new_path); |
| 439 | local_history_pos++; |
| 440 | dir_prev->set_disabled(local_history_pos == 0); |
| 441 | dir_next->set_disabled(true); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | void FileDialog::_action_pressed() { |
| 446 | if (mode == FILE_MODE_OPEN_FILES) { |
| 447 | const Vector<String> files = get_selected_files(); |
| 448 | if (!files.is_empty()) { |
| 449 | _save_to_recent(); |
| 450 | hide(); |
| 451 | emit_signal(SNAME("files_selected"), files); |
| 452 | } |
| 453 | return; |
| 454 | } |
| 455 | |
| 456 | String file_text = filename_edit->get_text(); |
| 457 | String f = file_text.is_absolute_path() ? file_text : dir_access->get_current_dir().path_join(file_text); |
| 458 | |
| 459 | if ((mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_FILE) && (dir_access->file_exists(f) || dir_access->is_bundle(f))) { |
| 460 | _save_to_recent(); |
| 461 | hide(); |
| 462 | emit_signal(SNAME("file_selected"), f); |
| 463 | } else if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_OPEN_DIR) { |
| 464 | String path = dir_access->get_current_dir(); |
| 465 | |
| 466 | path = path.replace_char('\\', '/'); |
| 467 | int selected = _get_selected_file_idx(); |
| 468 | if (selected > -1) { |
| 469 | Dictionary d = file_list->get_item_metadata(selected); |
| 470 | if (d["dir"] && d["name"] != "..") { |
| 471 | path = path.path_join(d["name"]); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | _save_to_recent(); |
| 476 | hide(); |
| 477 | emit_signal(SNAME("dir_selected"), path); |
| 478 | } |
| 479 | |
| 480 | if (mode == FILE_MODE_SAVE_FILE) { |
| 481 | bool valid = false; |
| 482 | |
| 483 | if (filter->get_selected() == filter->get_item_count() - 1) { |
| 484 | valid = true; // Match none. |
| 485 | } else if (filters.size() > 1 && filter->get_selected() == 0) { |
| 486 | // Match all filters. |
| 487 | for (int i = 0; i < filters.size(); i++) { |
| 488 | String flt = filters[i].get_slicec(';', 0); |
| 489 | for (int j = 0; j < flt.get_slice_count(","); j++) { |
| 490 | String str = flt.get_slicec(',', j).strip_edges(); |
| 491 | if (f.matchn(str)) { |
| 492 | valid = true; |
| 493 | break; |
| 494 | } |
| 495 | } |
| 496 | if (valid) { |
| 497 | break; |
| 498 | } |
| 499 | } |
| 500 | } else { |
| 501 | int idx = filter->get_selected(); |
| 502 | if (filters.size() > 1) { |
| 503 | idx--; |
| 504 | } |
| 505 | if (idx >= 0 && idx < filters.size()) { |
| 506 | String flt = filters[idx].get_slicec(';', 0); |
| 507 | int filter_slice_count = flt.get_slice_count(","); |
| 508 | for (int j = 0; j < filter_slice_count; j++) { |
| 509 | String str = (flt.get_slicec(',', j).strip_edges()); |
| 510 | if (f.matchn(str)) { |
| 511 | valid = true; |
| 512 | break; |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | if (!valid && filter_slice_count > 0) { |
| 517 | String str = flt.get_slicec(',', 0).strip_edges(); |
| 518 | f += str.substr(1); |
| 519 | filename_edit->set_text(f.get_file()); |
| 520 | valid = true; |
| 521 | } |
| 522 | } else { |
| 523 | valid = true; |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | String file_name = file_text.strip_edges().get_file(); |
| 528 | if (!valid || file_name.is_empty()) { |
| 529 | exterr->popup_centered(Size2(250, 80)); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | if (customization_flags[CUSTOMIZATION_OVERWRITE_WARNING] && (dir_access->file_exists(f) || dir_access->is_bundle(f))) { |
| 534 | confirm_save->set_text(vformat(atr(ETR("File \"%s\" already exists.\nDo you want to overwrite it?")), f)); |
| 535 | confirm_save->popup_centered(Size2(250, 80)); |
| 536 | } else { |
| 537 | _save_to_recent(); |
| 538 | hide(); |
| 539 | emit_signal(SNAME("file_selected"), f); |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | void FileDialog::_cancel_pressed() { |
| 545 | filename_edit->set_text(""); |
| 546 | hide(); |
| 547 | } |
| 548 | |
| 549 | bool FileDialog::_is_open_should_be_disabled() { |
| 550 | if (mode == FILE_MODE_OPEN_ANY || mode == FILE_MODE_SAVE_FILE) { |
| 551 | return false; |
| 552 | } |
| 553 | |
| 554 | Vector<int> items = file_list->get_selected_items(); |
| 555 | if (items.is_empty()) { |
| 556 | return mode != FILE_MODE_OPEN_DIR; // In "Open folder" mode, having nothing selected picks the current folder. |
| 557 | } |
| 558 | |
| 559 | for (const int idx : items) { |
| 560 | Dictionary d = file_list->get_item_metadata(idx); |
| 561 | |
| 562 | if (((mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES) && d["dir"]) || (mode == FILE_MODE_OPEN_DIR && !d["dir"])) { |
| 563 | return true; |
| 564 | } |
| 565 | } |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | void FileDialog::_go_up() { |
| 570 | _change_dir(get_current_dir().trim_suffix("/").get_base_dir()); |
| 571 | _push_history(); |
| 572 | } |
| 573 | |
| 574 | void FileDialog::_go_back() { |
| 575 | if (local_history_pos <= 0) { |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | local_history_pos--; |
| 580 | _change_dir(local_history[local_history_pos]); |
| 581 | |
| 582 | dir_prev->set_disabled(local_history_pos == 0); |
| 583 | dir_next->set_disabled(local_history_pos == local_history.size() - 1); |
| 584 | } |
| 585 | |
| 586 | void FileDialog::_go_forward() { |
| 587 | if (local_history_pos >= local_history.size() - 1) { |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | local_history_pos++; |
| 592 | _change_dir(local_history[local_history_pos]); |
| 593 | |
| 594 | dir_prev->set_disabled(local_history_pos == 0); |
| 595 | dir_next->set_disabled(local_history_pos == local_history.size() - 1); |
| 596 | } |
| 597 | |
| 598 | void FileDialog::deselect_all() { |
| 599 | // Clear currently selected items in file manager. |
| 600 | file_list->deselect_all(); |
| 601 | |
| 602 | // And change get_ok title. |
| 603 | get_ok_button()->set_disabled(_is_open_should_be_disabled()); |
| 604 | |
| 605 | switch (mode) { |
| 606 | case FILE_MODE_OPEN_FILE: |
| 607 | case FILE_MODE_OPEN_FILES: |
| 608 | set_default_ok_text(ETR("Open")); |
| 609 | break; |
| 610 | case FILE_MODE_OPEN_DIR: |
| 611 | set_default_ok_text(ETR("Select Current Folder")); |
| 612 | break; |
| 613 | case FILE_MODE_OPEN_ANY: |
| 614 | set_default_ok_text(ETR("Open")); |
| 615 | break; |
| 616 | case FILE_MODE_SAVE_FILE: |
| 617 | set_default_ok_text(ETR("Save")); |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | int FileDialog::_get_selected_file_idx() { |
| 623 | const PackedInt32Array selected = file_list->get_selected_items(); |
| 624 | return selected.is_empty() ? -1 : selected[0]; |
| 625 | } |
| 626 | |
| 627 | String FileDialog::_get_item_path(int p_idx) const { |
| 628 | const Dictionary meta = file_list->get_item_metadata(p_idx); |
| 629 | return ProjectSettings::get_singleton()->globalize_path(dir_access->get_current_dir().path_join(meta["name"])); |
| 630 | } |
| 631 | |
| 632 | void FileDialog::_file_list_multi_selected(int p_item, bool p_selected) { |
| 633 | if (p_selected) { |
| 634 | _file_list_selected(p_item); |
| 635 | } else { |
| 636 | get_ok_button()->set_disabled(_is_open_should_be_disabled()); |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | void FileDialog::_file_list_selected(int p_item) { |
| 641 | Dictionary d = file_list->get_item_metadata(p_item); |
| 642 | |
| 643 | if (!d["dir"]) { |
| 644 | filename_edit->set_text(d["name"]); |
| 645 | if (mode == FILE_MODE_SAVE_FILE) { |
| 646 | set_default_ok_text(ETR("Save")); |
| 647 | } else { |
| 648 | set_default_ok_text(ETR("Open")); |
| 649 | } |
| 650 | } else if (mode != FILE_MODE_SAVE_FILE) { |
| 651 | filename_edit->set_text(""); |
| 652 | if (mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) { |
| 653 | set_default_ok_text(ETR("Select This Folder")); |
| 654 | } |
| 655 | } |
| 656 | |
| 657 | get_ok_button()->set_disabled(_is_open_should_be_disabled()); |
| 658 | } |
| 659 | |
| 660 | void FileDialog::_file_list_item_activated(int p_item) { |
| 661 | Dictionary d = file_list->get_item_metadata(p_item); |
| 662 | |
| 663 | if (d["dir"]) { |
| 664 | _change_dir(d["name"]); |
| 665 | if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) { |
| 666 | filename_edit->set_text(""); |
| 667 | } |
| 668 | _push_history(); |
| 669 | } else { |
| 670 | _action_pressed(); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | void FileDialog::update_file_name() { |
| 675 | int idx = filter->get_selected() - 1; |
| 676 | if ((idx == -1 && filter->get_item_count() == 2) || (filter->get_item_count() > 2 && idx >= 0 && idx < filter->get_item_count() - 2)) { |
| 677 | if (idx == -1) { |
| 678 | idx += 1; |
| 679 | } |
| 680 | String filter_str = filters[idx]; |
| 681 | String file_str = filename_edit->get_text(); |
| 682 | String base_name = file_str.get_basename(); |
| 683 | Vector<String> filter_substr = filter_str.split(";"); |
| 684 | if (filter_substr.size() >= 2) { |
| 685 | file_str = base_name + "." + filter_substr[0].strip_edges().get_extension().to_lower(); |
| 686 | } else { |
| 687 | file_str = base_name + "." + filter_str.strip_edges().get_extension().to_lower(); |
| 688 | } |
| 689 | filename_edit->set_text(file_str); |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | void FileDialog::_item_menu_id_pressed(int p_option) { |
| 694 | int selected = _get_selected_file_idx(); |
| 695 | switch (p_option) { |
| 696 | case ITEM_MENU_COPY_PATH: { |
| 697 | if (selected > -1) { |
| 698 | DisplayServer::get_singleton()->clipboard_set(_get_item_path(selected)); |
| 699 | } |
| 700 | } break; |
| 701 | |
| 702 | case ITEM_MENU_DELETE: { |
| 703 | if (selected > -1) { |
| 704 | delete_dialog->popup_centered(Size2(250, 80)); |
| 705 | } |
| 706 | } break; |
| 707 | |
| 708 | case ITEM_MENU_REFRESH: { |
| 709 | invalidate(); |
| 710 | } break; |
| 711 | |
| 712 | case ITEM_MENU_NEW_FOLDER: { |
| 713 | _make_dir(); |
| 714 | } break; |
| 715 | |
| 716 | case ITEM_MENU_SHOW_IN_EXPLORER: { |
| 717 | String path; |
| 718 | if (selected > -1) { |
| 719 | path = _get_item_path(selected); |
| 720 | } else { |
| 721 | path = ProjectSettings::get_singleton()->globalize_path(dir_access->get_current_dir()); |
| 722 | } |
| 723 | |
| 724 | OS::get_singleton()->shell_show_in_file_manager(path, true); |
| 725 | } break; |
| 726 | |
| 727 | case ITEM_MENU_SHOW_BUNDLE_CONTENT: { |
| 728 | if (selected == -1) { |
| 729 | return; |
| 730 | } |
| 731 | Dictionary meta = file_list->get_item_metadata(selected); |
| 732 | _change_dir(meta["name"]); |
| 733 | if (mode == FILE_MODE_OPEN_FILE || mode == FILE_MODE_OPEN_FILES || mode == FILE_MODE_OPEN_DIR || mode == FILE_MODE_OPEN_ANY) { |
| 734 | filename_edit->set_text(""); |
| 735 | } |
| 736 | _push_history(); |
| 737 | } break; |
| 738 | |
| 739 | case ITEM_MENU_GO_UP: { |
| 740 | _dir_submitted(".."); |
| 741 | } break; |
| 742 | |
| 743 | case ITEM_MENU_TOGGLE_HIDDEN: { |
| 744 | set_show_hidden_files(!show_hidden_files); |
| 745 | } break; |
| 746 | |
| 747 | case ITEM_MENU_FIND: { |
| 748 | show_filename_filter_button->set_pressed(!show_filename_filter_button->is_pressed()); |
| 749 | } break; |
| 750 | |
| 751 | case ITEM_MENU_FOCUS_PATH: { |
| 752 | directory_edit->grab_focus(); |
| 753 | directory_edit->select_all(); |
| 754 | } break; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | void FileDialog::_empty_clicked(const Vector2 &p_pos, MouseButton p_button) { |
| 759 | if (p_button == MouseButton::RIGHT) { |
| 760 | _popup_menu(p_pos, -1); |
| 761 | } else if (p_button == MouseButton::LEFT) { |
| 762 | deselect_all(); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | void FileDialog::_item_clicked(int p_item, const Vector2 &p_pos, MouseButton p_button) { |
| 767 | if (p_button == MouseButton::RIGHT) { |
| 768 | _popup_menu(p_pos, p_item); |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | void FileDialog::_popup_menu(const Vector2 &p_pos, int p_for_item) { |
| 773 | item_menu->clear(); |
| 774 | |
| 775 | if (p_for_item > -1) { |
| 776 | item_menu->add_item(ETR("Copy Path"), ITEM_MENU_COPY_PATH); |
| 777 | if (customization_flags[CUSTOMIZATION_DELETE]) { |
| 778 | item_menu->add_item(ETR("Delete"), ITEM_MENU_DELETE); |
| 779 | item_menu->set_item_shortcut(-1, action_shortcuts[ITEM_MENU_DELETE]); |
| 780 | } |
| 781 | } else { |
| 782 | if (can_create_folders) { |
| 783 | item_menu->add_item(ETR("New Folder..."), ITEM_MENU_NEW_FOLDER); |
| 784 | } |
| 785 | item_menu->add_item(ETR("Refresh"), ITEM_MENU_REFRESH); |
| 786 | item_menu->set_item_shortcut(-1, action_shortcuts[ITEM_MENU_REFRESH]); |
| 787 | } |
| 788 | |
| 789 | #if !defined(ANDROID_ENABLED) && !defined(WEB_ENABLED) |
| 790 | // Opening the system file manager is not supported on the Android and web editors. |
| 791 | item_menu->add_separator(); |
| 792 | |
| 793 | Dictionary meta; |
| 794 | if (p_for_item > -1) { |
| 795 | meta = file_list->get_item_metadata(p_for_item); |
| 796 | } |
| 797 | |
| 798 | item_menu->add_item((p_for_item == -1 || meta["dir"]) ? ETR("Open in File Manager") : ETR("Show in File Manager"), ITEM_MENU_SHOW_IN_EXPLORER); |
| 799 | if (meta["bundle"]) { |
| 800 | item_menu->add_item(ETR("Show Package Contents"), ITEM_MENU_SHOW_BUNDLE_CONTENT); |
| 801 | } |
| 802 | #endif |
| 803 | |
| 804 | if (item_menu->get_item_count() == 0) { |
| 805 | return; |
| 806 | } |
| 807 | |
| 808 | item_menu->set_position(file_list->get_screen_position() + p_pos); |
| 809 | item_menu->reset_size(); |
| 810 | item_menu->popup(); |
| 811 | } |
| 812 | |
| 813 | void FileDialog::update_file_list() { |
| 814 | file_list->clear(); |
| 815 | |
| 816 | // Scroll back to the top after opening a directory |
| 817 | file_list->get_v_scroll_bar()->set_value(0); |
| 818 | |
| 819 | if (display_mode == DISPLAY_THUMBNAILS) { |
| 820 | int thumbnail_size = theme_cache.thumbnail_size * get_theme_default_base_scale(); |
| 821 | file_list->set_max_columns(0); |
| 822 | file_list->set_icon_mode(ItemList::ICON_MODE_TOP); |
| 823 | file_list->set_fixed_column_width(thumbnail_size * 3 / 2); |
| 824 | file_list->set_max_text_lines(2); |
| 825 | file_list->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS); |
| 826 | file_list->set_fixed_icon_size(Size2(thumbnail_size, thumbnail_size)); |
| 827 | file_list->set_fixed_tag_icon_size(_get_list_mode_icon_size()); |
| 828 | } else { |
| 829 | file_list->set_icon_mode(ItemList::ICON_MODE_LEFT); |
| 830 | file_list->set_max_columns(1); |
| 831 | file_list->set_max_text_lines(1); |
| 832 | file_list->set_fixed_column_width(0); |
| 833 | file_list->set_fixed_icon_size(_get_list_mode_icon_size()); |
| 834 | } |
| 835 | |
| 836 | dir_access->list_dir_begin(); |
| 837 | if (dir_access->is_readable(dir_access->get_current_dir().utf8().get_data())) { |
| 838 | message->hide(); |
| 839 | } else { |
| 840 | message->set_text(ETR("You don't have permission to access contents of this folder.")); |
| 841 | message->show(); |
| 842 | } |
| 843 | |
| 844 | LocalVector<String> files; |
| 845 | LocalVector<String> dirs; |
| 846 | |
| 847 | String item = dir_access->get_next(); |
| 848 | |
| 849 | while (!item.is_empty()) { |
| 850 | if (item == "." || item == "..") { |
| 851 | item = dir_access->get_next(); |
| 852 | continue; |
| 853 | } |
| 854 | |
| 855 | if (show_hidden_files || (!dir_access->current_is_hidden() && !_should_hide_file(item))) { |
| 856 | if (!dir_access->current_is_dir()) { |
| 857 | files.push_back(item); |
| 858 | } else { |
| 859 | dirs.push_back(item); |
| 860 | } |
| 861 | } |
| 862 | item = dir_access->get_next(); |
| 863 | } |
| 864 | |
| 865 | String filename_filter_lower = file_name_filter.to_lower(); |
| 866 | |
| 867 | List<String> patterns; |
| 868 | // Build filter. |
| 869 | if (filter->get_selected() == filter->get_item_count() - 1) { |
| 870 | // Match all. |
| 871 | } else if (filters.size() > 1 && filter->get_selected() == 0) { |
| 872 | // Match all filters. |
| 873 | for (int i = 0; i < filters.size(); i++) { |
| 874 | String f = filters[i].get_slicec(';', 0); |
| 875 | for (int j = 0; j < f.get_slice_count(","); j++) { |
| 876 | patterns.push_back(f.get_slicec(',', j).strip_edges()); |
| 877 | } |
| 878 | } |
| 879 | } else { |
| 880 | int idx = filter->get_selected(); |
| 881 | if (filters.size() > 1) { |
| 882 | idx--; |
| 883 | } |
| 884 | |
| 885 | if (idx >= 0 && idx < filters.size()) { |
| 886 | String f = filters[idx].get_slicec(';', 0); |
| 887 | for (int j = 0; j < f.get_slice_count(","); j++) { |
| 888 | patterns.push_back(f.get_slicec(',', j).strip_edges()); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | |
| 893 | LocalVector<DirInfo> filtered_dirs; |
| 894 | filtered_dirs.reserve(dirs.size()); |
| 895 | const String base_dir = dir_access->get_current_dir(); |
| 896 | |
| 897 | for (const String &dir_name : dirs) { |
| 898 | bool bundle = dir_access->is_bundle(dir_name); |
| 899 | bool found = true; |
| 900 | if (bundle) { |
| 901 | bool match = patterns.is_empty(); |
| 902 | for (const String &E : patterns) { |
| 903 | if (dir_name.matchn(E)) { |
| 904 | match = true; |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | found = match; |
| 909 | } |
| 910 | |
| 911 | if (found && (filename_filter_lower.is_empty() || dir_name.to_lower().contains(filename_filter_lower))) { |
| 912 | DirInfo di; |
| 913 | di.name = dir_name; |
| 914 | di.bundle = bundle; |
| 915 | if (file_sort == FileSortOption::MODIFIED_TIME || file_sort == FileSortOption::MODIFIED_TIME_REVERSE) { |
| 916 | di.modified_time = FileAccess::get_modified_time(base_dir.path_join(dir_name)); |
| 917 | } |
| 918 | filtered_dirs.push_back(di); |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | if (file_sort == FileSortOption::MODIFIED_TIME || file_sort == FileSortOption::MODIFIED_TIME_REVERSE) { |
| 923 | filtered_dirs.sort_custom<DirInfo::TimeComparator>(); |
| 924 | } else { |
| 925 | filtered_dirs.sort_custom<DirInfo::NameComparator>(); |
| 926 | } |
| 927 | |
| 928 | if (file_sort == FileSortOption::NAME_REVERSE || file_sort == FileSortOption::MODIFIED_TIME_REVERSE) { |
| 929 | filtered_dirs.reverse(); |
| 930 | } |
| 931 | |
| 932 | for (const DirInfo &info : filtered_dirs) { |
| 933 | if (display_mode == DISPLAY_THUMBNAILS) { |
| 934 | file_list->add_item(info.name, info.bundle ? theme_cache.file_thumbnail : theme_cache.folder_thumbnail); |
| 935 | } else { |
| 936 | file_list->add_item(info.name, info.bundle ? theme_cache.file : theme_cache.folder); |
| 937 | } |
| 938 | file_list->set_item_icon_modulate(-1, _get_folder_color(base_dir.path_join(info.name))); |
| 939 | |
| 940 | Dictionary d; |
| 941 | d["name"] = info.name; |
| 942 | d["dir"] = !info.bundle; |
| 943 | d["bundle"] = info.bundle; |
| 944 | file_list->set_item_metadata(-1, d); |
| 945 | } |
| 946 | |
| 947 | LocalVector<FileInfo> filtered_files; |
| 948 | filtered_files.reserve(files.size()); |
| 949 | |
| 950 | for (const String &filename : files) { |
| 951 | bool match = patterns.is_empty(); |
| 952 | String match_str; |
| 953 | |
| 954 | for (const String &E : patterns) { |
| 955 | if (filename.matchn(E)) { |
| 956 | match_str = E; |
| 957 | match = true; |
| 958 | break; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | if (match && (filename_filter_lower.is_empty() || filename.to_lower().contains(filename_filter_lower))) { |
| 963 | FileInfo fi; |
| 964 | fi.name = filename; |
| 965 | fi.match_string = match_str; |
| 966 | |
| 967 | // Only assign sorting fields when needed. |
| 968 | if (file_sort == FileSortOption::TYPE || file_sort == FileSortOption::TYPE_REVERSE) { |
| 969 | fi.type_sort = filename.get_extension() + filename.get_basename(); |
| 970 | } else if (file_sort == FileSortOption::MODIFIED_TIME || file_sort == FileSortOption::MODIFIED_TIME_REVERSE) { |
| 971 | fi.modified_time = FileAccess::get_modified_time(base_dir.path_join(filename)); |
| 972 | } |
| 973 | filtered_files.push_back(fi); |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | switch (file_sort) { |
| 978 | case FileSortOption::NAME: |
| 979 | case FileSortOption::NAME_REVERSE: |
| 980 | filtered_files.sort_custom<FileInfo::NameComparator>(); |
| 981 | break; |
| 982 | case FileSortOption::TYPE: |
| 983 | case FileSortOption::TYPE_REVERSE: |
| 984 | filtered_files.sort_custom<FileInfo::TypeComparator>(); |
| 985 | break; |
| 986 | case FileSortOption::MODIFIED_TIME: |
| 987 | case FileSortOption::MODIFIED_TIME_REVERSE: |
| 988 | filtered_files.sort_custom<FileInfo::TimeComparator>(); |
| 989 | break; |
| 990 | default: |
| 991 | ERR_PRINT(vformat("Invalid FileDialog sort option: %d", int(file_sort))); |
| 992 | } |
| 993 | |
| 994 | if (file_sort == FileSortOption::NAME_REVERSE || file_sort == FileSortOption::TYPE_REVERSE || file_sort == FileSortOption::MODIFIED_TIME_REVERSE) { |
| 995 | filtered_files.reverse(); |
| 996 | } |
| 997 | |
| 998 | for (const FileInfo &info : filtered_files) { |
| 999 | file_list->add_item(info.name); |
| 1000 | const String path = base_dir.path_join(info.name); |
| 1001 | |
| 1002 | Ref<Texture2D> icon; |
| 1003 | if (get_icon_callback.is_valid()) { |
| 1004 | const Variant &v = path; |
| 1005 | const Variant *argptrs[1] = { &v }; |
| 1006 | Variant vicon; |
| 1007 | |
| 1008 | Callable::CallError ce; |
| 1009 | get_icon_callback.callp(argptrs, 1, vicon, ce); |
| 1010 | if (unlikely(ce.error != Callable::CallError::CALL_OK)) { |
| 1011 | ERR_PRINT(vformat("Error calling FileDialog's icon callback: %s.", Variant::get_callable_error_text(get_icon_callback, argptrs, 1, ce))); |
| 1012 | } |
| 1013 | icon = vicon; |
| 1014 | } |
| 1015 | |
| 1016 | if (display_mode == DISPLAY_LIST) { |
| 1017 | if (icon.is_null()) { |
| 1018 | icon = theme_cache.file; |
| 1019 | } |
| 1020 | file_list->set_item_icon(-1, icon); |
| 1021 | } else { // DISPLAY_THUMBNAILS |
| 1022 | Ref<Texture2D> thumbnail; |
| 1023 | if (get_thumbnail_callback.is_valid()) { |
| 1024 | const Variant &v = path; |
| 1025 | const Variant *argptrs[1] = { &v }; |
| 1026 | Variant vicon; |
| 1027 | |
| 1028 | Callable::CallError ce; |
| 1029 | get_thumbnail_callback.callp(argptrs, 1, vicon, ce); |
| 1030 | if (unlikely(ce.error != Callable::CallError::CALL_OK)) { |
| 1031 | ERR_PRINT(vformat("Error calling FileDialog's thumbnail callback: %s.", Variant::get_callable_error_text(get_thumbnail_callback, argptrs, 1, ce))); |
| 1032 | } |
| 1033 | thumbnail = vicon; |
| 1034 | } |
| 1035 | if (thumbnail.is_null()) { |
| 1036 | thumbnail = theme_cache.file_thumbnail; |
| 1037 | } |
| 1038 | file_list->set_item_icon(-1, thumbnail); |
| 1039 | if (icon.is_valid()) { |
| 1040 | file_list->set_item_tag_icon(-1, icon); |
| 1041 | } |
| 1042 | } |
| 1043 | file_list->set_item_icon_modulate(-1, theme_cache.file_icon_color); |
| 1044 | |
| 1045 | if (mode == FILE_MODE_OPEN_DIR) { |
| 1046 | file_list->set_item_disabled(-1, true); |
| 1047 | } |
| 1048 | Dictionary d; |
| 1049 | d["name"] = info.name; |
| 1050 | d["dir"] = false; |
| 1051 | d["bundle"] = false; |
| 1052 | file_list->set_item_metadata(-1, d); |
| 1053 | |
| 1054 | if (filename_edit->get_text() == info.name || info.match_string == info.name) { |
| 1055 | file_list->select(file_list->get_item_count() - 1); |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | if (mode != FILE_MODE_SAVE_FILE && mode != FILE_MODE_OPEN_DIR) { |
| 1060 | // Select the first file from list if nothing is selected. |
| 1061 | int selected = _get_selected_file_idx(); |
| 1062 | if (selected == -1) { |
| 1063 | _file_list_select_first(); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | favorite_list->deselect_all(); |
| 1068 | favorite_button->set_pressed(false); |
| 1069 | |
| 1070 | const int fav_count = favorite_list->get_item_count(); |
| 1071 | for (int i = 0; i < fav_count; i++) { |
| 1072 | const String fav_dir = favorite_list->get_item_metadata(i); |
| 1073 | if (fav_dir != base_dir && fav_dir != base_dir + "/") { |
| 1074 | continue; |
| 1075 | } |
| 1076 | favorite_list->select(i); |
| 1077 | favorite_button->set_pressed(true); |
| 1078 | break; |
| 1079 | } |
| 1080 | _update_fav_buttons(); |
| 1081 | } |
| 1082 | |
| 1083 | void FileDialog::_filter_selected(int) { |
| 1084 | update_file_name(); |
| 1085 | update_file_list(); |
| 1086 | } |
| 1087 | |
| 1088 | void FileDialog::_filename_filter_changed() { |
| 1089 | update_filename_filter(); |
| 1090 | update_file_list(); |
| 1091 | callable_mp(this, &FileDialog::_file_list_select_first).call_deferred(); |
| 1092 | } |
| 1093 | |
| 1094 | void FileDialog::_file_list_select_first() { |
| 1095 | if (file_list->get_item_count() > 0) { |
| 1096 | file_list->select(0); |
| 1097 | _file_list_selected(0); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | void FileDialog::_delete_confirm() { |
| 1102 | Error err = OS::get_singleton()->move_to_trash(_get_item_path(_get_selected_file_idx())); |
| 1103 | if (err == OK) { |
| 1104 | invalidate(); |
| 1105 | _dir_contents_changed(); |
| 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | void FileDialog::_filename_filter_selected() { |
| 1110 | int selected = _get_selected_file_idx(); |
| 1111 | if (selected > -1) { |
| 1112 | filename_edit->set_text(file_list->get_item_text(selected)); |
| 1113 | filename_edit->emit_signal(SceneStringName(text_submitted), filename_edit->get_text()); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | void FileDialog::update_filters() { |
| 1118 | filter->clear(); |
| 1119 | processed_filters.clear(); |
| 1120 | |
| 1121 | if (filters.size() > 1) { |
| 1122 | String all_filters; |
| 1123 | String all_mime; |
| 1124 | String all_filters_full; |
| 1125 | String all_mime_full; |
| 1126 | |
| 1127 | const int max_filters = 5; |
| 1128 | |
| 1129 | // "All Recognized" display name. |
| 1130 | for (int i = 0; i < MIN(max_filters, filters.size()); i++) { |
| 1131 | String flt = filters[i].get_slicec(';', 0).strip_edges(); |
| 1132 | if (!all_filters.is_empty() && !flt.is_empty()) { |
| 1133 | all_filters += ", "; |
| 1134 | } |
| 1135 | all_filters += flt; |
| 1136 | |
| 1137 | String mime = filters[i].get_slicec(';', 2).strip_edges(); |
| 1138 | if (!all_mime.is_empty() && !mime.is_empty()) { |
| 1139 | all_mime += ", "; |
| 1140 | } |
| 1141 | all_mime += mime; |
| 1142 | } |
| 1143 | |
| 1144 | // "All Recognized" filter. |
| 1145 | for (int i = 0; i < filters.size(); i++) { |
| 1146 | String flt = filters[i].get_slicec(';', 0).strip_edges(); |
| 1147 | if (!all_filters_full.is_empty() && !flt.is_empty()) { |
| 1148 | all_filters_full += ","; |
| 1149 | } |
| 1150 | all_filters_full += flt; |
| 1151 | |
| 1152 | String mime = filters[i].get_slicec(';', 2).strip_edges(); |
| 1153 | if (!all_mime_full.is_empty() && !mime.is_empty()) { |
| 1154 | all_mime_full += ","; |
| 1155 | } |
| 1156 | all_mime_full += mime; |
| 1157 | } |
| 1158 | |
| 1159 | String native_all_name; |
| 1160 | native_all_name += all_filters; |
| 1161 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_MIME)) { |
| 1162 | if (!native_all_name.is_empty()) { |
| 1163 | native_all_name += ", "; |
| 1164 | } |
| 1165 | native_all_name += all_mime; |
| 1166 | } |
| 1167 | |
| 1168 | if (max_filters < filters.size()) { |
| 1169 | all_filters += ", ..."; |
| 1170 | native_all_name += ", ..."; |
| 1171 | } |
| 1172 | |
| 1173 | filter->add_item(atr(ETR("All Recognized")) + " (" + all_filters + ")"); |
| 1174 | processed_filters.push_back(all_filters_full + ";" + atr(ETR("All Recognized")) + " (" + native_all_name + ")" + ";" + all_mime_full); |
| 1175 | } |
| 1176 | for (int i = 0; i < filters.size(); i++) { |
| 1177 | String flt = filters[i].get_slicec(';', 0).strip_edges(); |
| 1178 | String desc = filters[i].get_slicec(';', 1).strip_edges(); |
| 1179 | String mime = filters[i].get_slicec(';', 2).strip_edges(); |
| 1180 | String native_name; |
| 1181 | |
| 1182 | native_name += flt; |
| 1183 | if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_DIALOG_FILE_MIME)) { |
| 1184 | if (!native_name.is_empty() && !mime.is_empty()) { |
| 1185 | native_name += ", "; |
| 1186 | } |
| 1187 | native_name += mime; |
| 1188 | } |
| 1189 | if (!desc.is_empty()) { |
| 1190 | filter->add_item(atr(desc) + " (" + flt + ")"); |
| 1191 | processed_filters.push_back(flt + ";" + atr(desc) + " (" + native_name + ");" + mime); |
| 1192 | } else { |
| 1193 | filter->add_item("(" + flt + ")"); |
| 1194 | processed_filters.push_back(flt + ";(" + native_name + ");" + mime); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | String f = atr(ETR("All Files")) + " (*.*)"; |
| 1199 | filter->add_item(f); |
| 1200 | processed_filters.push_back("*.*;" + f + ";application/octet-stream"); |
| 1201 | } |
| 1202 | |
| 1203 | void FileDialog::update_customization() { |
| 1204 | _update_make_dir_visible(); |
| 1205 | show_hidden->set_visible(customization_flags[CUSTOMIZATION_HIDDEN_FILES]); |
| 1206 | layout_container->set_visible(customization_flags[CUSTOMIZATION_LAYOUT]); |
| 1207 | layout_separator->set_visible(customization_flags[CUSTOMIZATION_FILE_FILTER] || customization_flags[CUSTOMIZATION_FILE_SORT]); |
| 1208 | show_filename_filter_button->set_visible(customization_flags[CUSTOMIZATION_FILE_FILTER]); |
| 1209 | file_sort_button->set_visible(customization_flags[CUSTOMIZATION_FILE_SORT]); |
| 1210 | show_hidden_separator->set_visible(customization_flags[CUSTOMIZATION_HIDDEN_FILES] && (customization_flags[CUSTOMIZATION_LAYOUT] || customization_flags[CUSTOMIZATION_FILE_FILTER] || customization_flags[CUSTOMIZATION_FILE_SORT])); |
| 1211 | favorite_button->set_visible(customization_flags[CUSTOMIZATION_FAVORITES]); |
| 1212 | favorite_vbox->set_visible(customization_flags[CUSTOMIZATION_FAVORITES]); |
| 1213 | recent_vbox->set_visible(customization_flags[CUSTOMIZATION_RECENT]); |
| 1214 | } |
| 1215 | |
| 1216 | void FileDialog::clear_filename_filter() { |
| 1217 | set_filename_filter(""); |
| 1218 | update_filename_filter_gui(); |
| 1219 | invalidate(); |
| 1220 | } |
| 1221 | |
| 1222 | void FileDialog::update_filename_filter_gui() { |
| 1223 | filename_filter_box->set_visible(show_filename_filter); |
| 1224 | if (!show_filename_filter) { |
| 1225 | file_name_filter.clear(); |
| 1226 | } |
| 1227 | if (filename_filter->get_text() == file_name_filter) { |
| 1228 | return; |
| 1229 | } |
| 1230 | filename_filter->set_text(file_name_filter); |
| 1231 | } |
| 1232 | |
| 1233 | void FileDialog::update_filename_filter() { |
| 1234 | if (filename_filter->get_text() == file_name_filter) { |
| 1235 | return; |
| 1236 | } |
| 1237 | set_filename_filter(filename_filter->get_text()); |
| 1238 | } |
| 1239 | |
| 1240 | void FileDialog::clear_filters() { |
| 1241 | filters.clear(); |
| 1242 | update_filters(); |
| 1243 | invalidate(); |
| 1244 | } |
| 1245 | |
| 1246 | void FileDialog::add_filter(const String &p_filter, const String &p_description, const String &p_mime) { |
| 1247 | ERR_FAIL_COND_MSG(p_filter.begins_with("."), "Filter must be \"filename.extension\", can't start with dot."); |
| 1248 | if (p_description.is_empty() && p_mime.is_empty()) { |
| 1249 | filters.push_back(p_filter); |
| 1250 | } else if (p_mime.is_empty()) { |
| 1251 | filters.push_back(vformat("%s ; %s", p_filter, p_description)); |
| 1252 | } else { |
| 1253 | filters.push_back(vformat("%s ; %s ; %s", p_filter, p_description, p_mime)); |
| 1254 | } |
| 1255 | update_filters(); |
| 1256 | invalidate(); |
| 1257 | } |
| 1258 | |
| 1259 | void FileDialog::set_filters(const Vector<String> &p_filters) { |
| 1260 | if (filters == p_filters) { |
| 1261 | return; |
| 1262 | } |
| 1263 | filters = p_filters; |
| 1264 | update_filters(); |
| 1265 | invalidate(); |
| 1266 | } |
| 1267 | |
| 1268 | void FileDialog::set_filename_filter(const String &p_filename_filter) { |
| 1269 | if (file_name_filter == p_filename_filter) { |
| 1270 | return; |
| 1271 | } |
| 1272 | file_name_filter = p_filename_filter; |
| 1273 | update_filename_filter_gui(); |
| 1274 | emit_signal(SNAME("filename_filter_changed"), filter); |
| 1275 | invalidate(); |
| 1276 | } |
| 1277 | |
| 1278 | Vector<String> FileDialog::get_filters() const { |
| 1279 | return filters; |
| 1280 | } |
| 1281 | |
| 1282 | String FileDialog::get_filename_filter() const { |
| 1283 | return file_name_filter; |
| 1284 | } |
| 1285 | |
| 1286 | String FileDialog::get_current_dir() const { |
| 1287 | return full_dir; |
| 1288 | } |
| 1289 | |
| 1290 | String FileDialog::get_current_file() const { |
| 1291 | return filename_edit->get_text(); |
| 1292 | } |
| 1293 | |
| 1294 | String FileDialog::get_current_path() const { |
| 1295 | return full_dir.path_join(filename_edit->get_text()); |
| 1296 | } |
| 1297 | |
| 1298 | void FileDialog::set_current_dir(const String &p_dir) { |
| 1299 | if (p_dir.is_relative_path()) { |
| 1300 | dir_access->change_dir(OS::get_singleton()->get_resource_dir()); |
| 1301 | } |
| 1302 | _change_dir(p_dir); |
| 1303 | |
| 1304 | _push_history(); |
| 1305 | } |
| 1306 | |
| 1307 | void FileDialog::set_current_file(const String &p_file) { |
| 1308 | if (filename_edit->get_text() == p_file) { |
| 1309 | return; |
| 1310 | } |
| 1311 | filename_edit->set_text(p_file); |
| 1312 | update_dir(); |
| 1313 | invalidate(); |
| 1314 | _focus_file_text(); |
| 1315 | } |
| 1316 | |
| 1317 | void FileDialog::set_current_path(const String &p_path) { |
| 1318 | if (!p_path.size()) { |
| 1319 | return; |
| 1320 | } |
| 1321 | int pos = MAX(p_path.rfind_char('/'), p_path.rfind_char('\\')); |
| 1322 | if (pos == -1) { |
| 1323 | set_current_file(p_path); |
| 1324 | } else { |
| 1325 | String path_dir = p_path.substr(0, pos); |
| 1326 | String path_file = p_path.substr(pos + 1); |
| 1327 | set_current_dir(path_dir); |
| 1328 | set_current_file(path_file); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | void FileDialog::set_root_subfolder(const String &p_root) { |
| 1333 | root_subfolder = p_root; |
| 1334 | ERR_FAIL_COND_MSG(!dir_access->dir_exists(p_root), "root_subfolder must be an existing sub-directory."); |
| 1335 | |
| 1336 | local_history.clear(); |
| 1337 | local_history_pos = -1; |
| 1338 | |
| 1339 | dir_access->change_dir(root_subfolder); |
| 1340 | if (root_subfolder.is_empty()) { |
| 1341 | root_prefix = ""; |
| 1342 | } else { |
| 1343 | root_prefix = dir_access->get_current_dir(); |
| 1344 | } |
| 1345 | invalidate(); |
| 1346 | _update_drives(); |
| 1347 | update_dir(); |
| 1348 | } |
| 1349 | |
| 1350 | String FileDialog::get_root_subfolder() const { |
| 1351 | return root_subfolder; |
| 1352 | } |
| 1353 | |
| 1354 | void FileDialog::set_mode_overrides_title(bool p_override) { |
| 1355 | mode_overrides_title = p_override; |
| 1356 | } |
| 1357 | |
| 1358 | bool FileDialog::is_mode_overriding_title() const { |
| 1359 | return mode_overrides_title; |
| 1360 | } |
| 1361 | |
| 1362 | void FileDialog::set_file_mode(FileMode p_mode) { |
| 1363 | ERR_FAIL_INDEX((int)p_mode, 5); |
| 1364 | if (mode == p_mode) { |
| 1365 | return; |
| 1366 | } |
| 1367 | mode = p_mode; |
| 1368 | switch (mode) { |
| 1369 | case FILE_MODE_OPEN_FILE: |
| 1370 | set_default_ok_text(ETR("Open")); |
| 1371 | if (mode_overrides_title) { |
| 1372 | set_title(ETR("Open a File")); |
| 1373 | } |
| 1374 | break; |
| 1375 | case FILE_MODE_OPEN_FILES: |
| 1376 | set_default_ok_text(ETR("Open")); |
| 1377 | if (mode_overrides_title) { |
| 1378 | set_title(ETR("Open File(s)")); |
| 1379 | } |
| 1380 | break; |
| 1381 | case FILE_MODE_OPEN_DIR: |
| 1382 | set_default_ok_text(ETR("Select Current Folder")); |
| 1383 | if (mode_overrides_title) { |
| 1384 | set_title(ETR("Open a Directory")); |
| 1385 | } |
| 1386 | break; |
| 1387 | case FILE_MODE_OPEN_ANY: |
| 1388 | set_default_ok_text(ETR("Open")); |
| 1389 | if (mode_overrides_title) { |
| 1390 | set_title(ETR("Open a File or Directory")); |
| 1391 | } |
| 1392 | make_dir_button->show(); |
| 1393 | break; |
| 1394 | case FILE_MODE_SAVE_FILE: |
| 1395 | set_default_ok_text(ETR("Save")); |
| 1396 | if (mode_overrides_title) { |
| 1397 | set_title(ETR("Save a File")); |
| 1398 | } |
| 1399 | break; |
| 1400 | } |
| 1401 | _update_make_dir_visible(); |
| 1402 | |
| 1403 | if (mode == FILE_MODE_OPEN_FILES) { |
| 1404 | file_list->set_select_mode(ItemList::SELECT_MULTI); |
| 1405 | } else { |
| 1406 | file_list->set_select_mode(ItemList::SELECT_SINGLE); |
| 1407 | } |
| 1408 | |
| 1409 | get_ok_button()->set_disabled(_is_open_should_be_disabled()); |
| 1410 | } |
| 1411 | |
| 1412 | FileDialog::FileMode FileDialog::get_file_mode() const { |
| 1413 | return mode; |
| 1414 | } |
| 1415 | |
| 1416 | void FileDialog::set_display_mode(DisplayMode p_mode) { |
| 1417 | ERR_FAIL_INDEX((int)p_mode, DISPLAY_MAX); |
| 1418 | if (display_mode == p_mode) { |
| 1419 | return; |
| 1420 | } |
| 1421 | display_mode = p_mode; |
| 1422 | |
| 1423 | if (p_mode == DISPLAY_THUMBNAILS) { |
| 1424 | thumbnail_mode_button->set_pressed(true); |
| 1425 | list_mode_button->set_pressed(false); |
| 1426 | } else { |
| 1427 | thumbnail_mode_button->set_pressed(false); |
| 1428 | list_mode_button->set_pressed(true); |
| 1429 | } |
| 1430 | invalidate(); |
| 1431 | } |
| 1432 | |
| 1433 | FileDialog::DisplayMode FileDialog::get_display_mode() const { |
| 1434 | return display_mode; |
| 1435 | } |
| 1436 | |
| 1437 | void FileDialog::set_favorite_list(const PackedStringArray &p_favorites) { |
| 1438 | ERR_FAIL_COND_MSG(Thread::get_caller_id() != Thread::get_main_id(), "Setting favorite list can only be done on the main thread."); |
| 1439 | |
| 1440 | global_favorites.clear(); |
| 1441 | global_favorites.reserve(p_favorites.size()); |
| 1442 | for (const String &fav : p_favorites) { |
| 1443 | if (fav.ends_with("/")) { |
| 1444 | global_favorites.push_back(fav); |
| 1445 | } else { |
| 1446 | global_favorites.push_back(fav + "/"); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | PackedStringArray FileDialog::get_favorite_list() { |
| 1452 | PackedStringArray ret; |
| 1453 | ERR_FAIL_COND_V_MSG(Thread::get_caller_id() != Thread::get_main_id(), ret, "Getting favorite list can only be done on the main thread."); |
| 1454 | |
| 1455 | ret.resize(global_favorites.size()); |
| 1456 | |
| 1457 | String *fav_write = ret.ptrw(); |
| 1458 | int i = 0; |
| 1459 | for (const String &fav : global_favorites) { |
| 1460 | fav_write[i] = fav; |
| 1461 | i++; |
| 1462 | } |
| 1463 | return ret; |
| 1464 | } |
| 1465 | |
| 1466 | void FileDialog::set_recent_list(const PackedStringArray &p_recents) { |
| 1467 | ERR_FAIL_COND_MSG(Thread::get_caller_id() != Thread::get_main_id(), "Setting recent list can only be done on the main thread."); |
| 1468 | |
| 1469 | global_recents.clear(); |
| 1470 | global_recents.reserve(p_recents.size()); |
| 1471 | for (const String &recent : p_recents) { |
| 1472 | if (recent.ends_with("/")) { |
| 1473 | global_recents.push_back(recent); |
| 1474 | } else { |
| 1475 | global_recents.push_back(recent + "/"); |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | PackedStringArray FileDialog::get_recent_list() { |
| 1481 | PackedStringArray ret; |
| 1482 | ERR_FAIL_COND_V_MSG(Thread::get_caller_id() != Thread::get_main_id(), ret, "Getting recent list can only be done on the main thread."); |
| 1483 | |
| 1484 | ret.resize(global_recents.size()); |
| 1485 | |
| 1486 | String *recent_write = ret.ptrw(); |
| 1487 | int i = 0; |
| 1488 | for (const String &recent : global_recents) { |
| 1489 | recent_write[i] = recent; |
| 1490 | i++; |
| 1491 | } |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
| 1495 | void FileDialog::set_customization_flag_enabled(Customization p_flag, bool p_enabled) { |
| 1496 | ERR_FAIL_INDEX(p_flag, CUSTOMIZATION_MAX); |
| 1497 | if (customization_flags[p_flag] == p_enabled) { |
| 1498 | return; |
| 1499 | } |
| 1500 | customization_flags[p_flag] = p_enabled; |
| 1501 | update_customization(); |
| 1502 | } |
| 1503 | |
| 1504 | bool FileDialog::is_customization_flag_enabled(Customization p_flag) const { |
| 1505 | ERR_FAIL_INDEX_V(p_flag, CUSTOMIZATION_MAX, false); |
| 1506 | return customization_flags[p_flag]; |
| 1507 | } |
| 1508 | |
| 1509 | void FileDialog::set_access(Access p_access) { |
| 1510 | ERR_FAIL_INDEX(p_access, 3); |
| 1511 | if (access == p_access) { |
| 1512 | return; |
| 1513 | } |
| 1514 | access = p_access; |
| 1515 | root_prefix = ""; |
| 1516 | root_subfolder = ""; |
| 1517 | |
| 1518 | switch (p_access) { |
| 1519 | case ACCESS_FILESYSTEM: { |
| 1520 | dir_access = DirAccess::create(DirAccess::ACCESS_FILESYSTEM); |
| 1521 | #ifdef ANDROID_ENABLED |
| 1522 | set_current_dir(OS::get_singleton()->get_system_dir(OS::SYSTEM_DIR_DESKTOP)); |
| 1523 | #endif |
| 1524 | } break; |
| 1525 | case ACCESS_RESOURCES: { |
| 1526 | dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 1527 | } break; |
| 1528 | case ACCESS_USERDATA: { |
| 1529 | dir_access = DirAccess::create(DirAccess::ACCESS_USERDATA); |
| 1530 | } break; |
| 1531 | } |
| 1532 | _update_drives(); |
| 1533 | invalidate(); |
| 1534 | update_filters(); |
| 1535 | update_dir(); |
| 1536 | _update_favorite_list(); |
| 1537 | _update_recent_list(); |
| 1538 | } |
| 1539 | |
| 1540 | void FileDialog::invalidate() { |
| 1541 | if (!is_visible() || is_invalidating) { |
| 1542 | return; |
| 1543 | } |
| 1544 | |
| 1545 | is_invalidating = true; |
| 1546 | callable_mp(this, &FileDialog::_invalidate).call_deferred(); |
| 1547 | } |
| 1548 | |
| 1549 | void FileDialog::_invalidate() { |
| 1550 | if (!is_invalidating) { |
| 1551 | return; |
| 1552 | } |
| 1553 | |
| 1554 | update_file_list(); |
| 1555 | |
| 1556 | is_invalidating = false; |
| 1557 | } |
| 1558 | |
| 1559 | void FileDialog::_setup_button(Button *p_button, const Ref<Texture2D> &p_icon) { |
| 1560 | p_button->set_button_icon(p_icon); |
| 1561 | |
| 1562 | p_button->begin_bulk_theme_override(); |
| 1563 | p_button->add_theme_color_override(SNAME("icon_normal_color"), theme_cache.icon_normal_color); |
| 1564 | p_button->add_theme_color_override(SNAME("icon_hover_color"), theme_cache.icon_hover_color); |
| 1565 | p_button->add_theme_color_override(SNAME("icon_focus_color"), theme_cache.icon_focus_color); |
| 1566 | p_button->add_theme_color_override(SNAME("icon_pressed_color"), theme_cache.icon_pressed_color); |
| 1567 | p_button->end_bulk_theme_override(); |
| 1568 | } |
| 1569 | |
| 1570 | void FileDialog::_update_make_dir_visible() { |
| 1571 | can_create_folders = customization_flags[CUSTOMIZATION_CREATE_FOLDER] && mode != FILE_MODE_OPEN_FILE && mode != FILE_MODE_OPEN_FILES; |
| 1572 | make_dir_container->set_visible(can_create_folders); |
| 1573 | } |
| 1574 | |
| 1575 | FileDialog::Access FileDialog::get_access() const { |
| 1576 | return access; |
| 1577 | } |
| 1578 | |
| 1579 | void FileDialog::_make_dir_confirm() { |
| 1580 | Error err = dir_access->make_dir(new_dir_name->get_text().strip_edges()); |
| 1581 | if (err == OK) { |
| 1582 | _dir_contents_changed(); |
| 1583 | _change_dir(new_dir_name->get_text().strip_edges()); |
| 1584 | update_filters(); |
| 1585 | _push_history(); |
| 1586 | } else { |
| 1587 | mkdirerr->popup_centered(Size2(250, 50)); |
| 1588 | } |
| 1589 | new_dir_name->set_text(""); // reset label |
| 1590 | } |
| 1591 | |
| 1592 | void FileDialog::_make_dir() { |
| 1593 | make_dir_dialog->popup_centered(Size2(250, 80)); |
| 1594 | new_dir_name->grab_focus(); |
| 1595 | } |
| 1596 | |
| 1597 | void FileDialog::_select_drive(int p_idx) { |
| 1598 | String d = drives->get_item_text(p_idx); |
| 1599 | _change_dir(d); |
| 1600 | filename_edit->set_text(""); |
| 1601 | _push_history(); |
| 1602 | } |
| 1603 | |
| 1604 | void FileDialog::_change_dir(const String &p_new_dir) { |
| 1605 | if (root_prefix.is_empty()) { |
| 1606 | dir_access->change_dir(p_new_dir); |
| 1607 | } else { |
| 1608 | String old_dir = dir_access->get_current_dir(); |
| 1609 | dir_access->change_dir(p_new_dir); |
| 1610 | if (!dir_access->get_current_dir().begins_with(root_prefix)) { |
| 1611 | dir_access->change_dir(old_dir); |
| 1612 | return; |
| 1613 | } |
| 1614 | } |
| 1615 | |
| 1616 | invalidate(); |
| 1617 | update_dir(); |
| 1618 | } |
| 1619 | |
| 1620 | void FileDialog::_update_drives(bool p_select) { |
| 1621 | if (access != ACCESS_FILESYSTEM) { |
| 1622 | drives->hide(); |
| 1623 | return; |
| 1624 | } |
| 1625 | |
| 1626 | HashMap<int, String> drive_map; |
| 1627 | int dc = dir_access->get_drive_count(); |
| 1628 | int cur = dir_access->get_current_drive(); |
| 1629 | for (int i = 0; i < dc; i++) { |
| 1630 | String drv = dir_access->get_drive(i); |
| 1631 | if (!root_prefix.is_empty() && !drv.begins_with(root_prefix)) { |
| 1632 | continue; |
| 1633 | } |
| 1634 | drive_map[i] = drv; |
| 1635 | } |
| 1636 | if (drive_map.size() == 0) { |
| 1637 | drives->hide(); |
| 1638 | } else { |
| 1639 | drives->clear(); |
| 1640 | Node *dp = drives->get_parent(); |
| 1641 | if (dp) { |
| 1642 | dp->remove_child(drives); |
| 1643 | } |
| 1644 | dp = dir_access->drives_are_shortcuts() ? shortcuts_container : drives_container; |
| 1645 | dp->add_child(drives); |
| 1646 | drives->show(); |
| 1647 | |
| 1648 | for (const KeyValue<int, String> &drv : drive_map) { |
| 1649 | drives->add_item(drv.value); |
| 1650 | drives->set_item_metadata(-1, drv.key); |
| 1651 | if (p_select && drv.key == cur) { |
| 1652 | drives->select(drives->get_item_count() - 1); |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | |
| 1658 | void FileDialog::_sort_option_selected(int p_option) { |
| 1659 | for (int i = 0; i < int(FileSortOption::MAX); i++) { |
| 1660 | file_sort_button->get_popup()->set_item_checked(i, (i == p_option)); |
| 1661 | } |
| 1662 | file_sort = FileSortOption(p_option); |
| 1663 | invalidate(); |
| 1664 | } |
| 1665 | |
| 1666 | void FileDialog::_favorite_selected(int p_item) { |
| 1667 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_item, global_favorites.size()); |
| 1668 | _change_dir(favorite_list->get_item_metadata(p_item)); |
| 1669 | _push_history(); |
| 1670 | } |
| 1671 | |
| 1672 | void FileDialog::_favorite_pressed() { |
| 1673 | String directory = get_current_dir(); |
| 1674 | if (!directory.ends_with("/")) { |
| 1675 | directory += "/"; |
| 1676 | } |
| 1677 | |
| 1678 | bool found = false; |
| 1679 | for (const String &name : global_favorites) { |
| 1680 | if (!_path_matches_access(name)) { |
| 1681 | continue; |
| 1682 | } |
| 1683 | |
| 1684 | if (name == directory) { |
| 1685 | found = true; |
| 1686 | break; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | if (found) { |
| 1691 | global_favorites.erase(directory); |
| 1692 | } else { |
| 1693 | global_favorites.push_back(directory); |
| 1694 | } |
| 1695 | favorites_changed = true; |
| 1696 | _update_favorite_list(); |
| 1697 | } |
| 1698 | |
| 1699 | void FileDialog::_favorite_move_up() { |
| 1700 | int current = favorite_list->get_current(); |
| 1701 | if (current <= 0) { |
| 1702 | return; |
| 1703 | } |
| 1704 | |
| 1705 | int a_idx = global_favorites.find(favorite_list->get_item_metadata(current - 1)); |
| 1706 | int b_idx = global_favorites.find(favorite_list->get_item_metadata(current)); |
| 1707 | |
| 1708 | if (a_idx == -1 || b_idx == -1) { |
| 1709 | return; |
| 1710 | } |
| 1711 | SWAP(global_favorites[a_idx], global_favorites[b_idx]); |
| 1712 | favorites_changed = true; |
| 1713 | _update_favorite_list(); |
| 1714 | } |
| 1715 | |
| 1716 | void FileDialog::_favorite_move_down() { |
| 1717 | int current = favorite_list->get_current(); |
| 1718 | if (current == -1 || current >= favorite_list->get_item_count() - 1) { |
| 1719 | return; |
| 1720 | } |
| 1721 | |
| 1722 | int a_idx = global_favorites.find(favorite_list->get_item_metadata(current)); |
| 1723 | int b_idx = global_favorites.find(favorite_list->get_item_metadata(current + 1)); |
| 1724 | |
| 1725 | if (a_idx == -1 || b_idx == -1) { |
| 1726 | return; |
| 1727 | } |
| 1728 | SWAP(global_favorites[a_idx], global_favorites[b_idx]); |
| 1729 | favorites_changed = true; |
| 1730 | _update_favorite_list(); |
| 1731 | } |
| 1732 | |
| 1733 | void FileDialog::_update_favorite_list() { |
| 1734 | const String current = get_current_dir(); |
| 1735 | |
| 1736 | favorite_list->clear(); |
| 1737 | favorite_button->set_pressed(false); |
| 1738 | |
| 1739 | Vector<String> favorited_paths; |
| 1740 | Vector<String> favorited_names; |
| 1741 | |
| 1742 | int current_favorite = -1; |
| 1743 | for (uint32_t i = 0; i < global_favorites.size(); i++) { |
| 1744 | String name = global_favorites[i]; |
| 1745 | if (!_path_matches_access(name)) { |
| 1746 | continue; |
| 1747 | } |
| 1748 | |
| 1749 | if (!name.ends_with("/") || !name.begins_with(root_prefix)) { |
| 1750 | continue; |
| 1751 | } |
| 1752 | |
| 1753 | if (!dir_access->dir_exists(name)) { |
| 1754 | // Remove invalid directory from the list of favorited directories. |
| 1755 | global_favorites.remove_at(i); |
| 1756 | i--; |
| 1757 | continue; |
| 1758 | } |
| 1759 | |
| 1760 | if (name == current) { |
| 1761 | current_favorite = favorited_names.size(); |
| 1762 | } |
| 1763 | favorited_paths.append(name); |
| 1764 | |
| 1765 | // Compute favorite display text. |
| 1766 | if (name == "res://" || name == "user://") { |
| 1767 | name = "/"; |
| 1768 | } else { |
| 1769 | if (current_favorite == -1 && name == current + "/") { |
| 1770 | current_favorite = favorited_names.size(); |
| 1771 | } |
| 1772 | name = name.trim_suffix("/"); |
| 1773 | name = name.get_file(); |
| 1774 | } |
| 1775 | favorited_names.append(name); |
| 1776 | } |
| 1777 | |
| 1778 | // EditorNode::disambiguate_filenames(favorited_paths, favorited_names); // TODO Needs a non-editor method. |
| 1779 | |
| 1780 | const int favorites_size = favorited_paths.size(); |
| 1781 | for (int i = 0; i < favorites_size; i++) { |
| 1782 | favorite_list->add_item(favorited_names[i], theme_cache.folder); |
| 1783 | favorite_list->set_item_tooltip(-1, favorited_paths[i]); |
| 1784 | favorite_list->set_item_metadata(-1, favorited_paths[i]); |
| 1785 | favorite_list->set_item_icon_modulate(-1, _get_folder_color(favorited_paths[i])); |
| 1786 | |
| 1787 | if (i == current_favorite) { |
| 1788 | favorite_button->set_pressed(true); |
| 1789 | favorite_list->set_current(favorite_list->get_item_count() - 1); |
| 1790 | recent_list->deselect_all(); |
| 1791 | } |
| 1792 | } |
| 1793 | _update_fav_buttons(); |
| 1794 | } |
| 1795 | |
| 1796 | void FileDialog::_update_fav_buttons() { |
| 1797 | const int current = favorite_list->get_current(); |
| 1798 | fav_up_button->set_disabled(current < 1); |
| 1799 | fav_down_button->set_disabled(current == -1 || current >= favorite_list->get_item_count() - 1); |
| 1800 | } |
| 1801 | |
| 1802 | void FileDialog::_recent_selected(int p_item) { |
| 1803 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_item, global_recents.size()); |
| 1804 | _change_dir(recent_list->get_item_metadata(p_item)); |
| 1805 | _push_history(); |
| 1806 | } |
| 1807 | |
| 1808 | void FileDialog::_save_to_recent() { |
| 1809 | String directory = get_current_dir(); |
| 1810 | if (!directory.ends_with("/")) { |
| 1811 | directory += "/"; |
| 1812 | } |
| 1813 | |
| 1814 | int count = 0; |
| 1815 | for (uint32_t i = 0; i < global_recents.size(); i++) { |
| 1816 | const String &dir = global_recents[i]; |
| 1817 | if (!_path_matches_access(dir)) { |
| 1818 | continue; |
| 1819 | } |
| 1820 | |
| 1821 | if (dir == directory || count > MAX_RECENTS) { |
| 1822 | global_recents.remove_at(i); |
| 1823 | i--; |
| 1824 | } else { |
| 1825 | count++; |
| 1826 | } |
| 1827 | } |
| 1828 | global_recents.insert(0, directory); |
| 1829 | recents_changed = true; |
| 1830 | |
| 1831 | _update_recent_list(); |
| 1832 | } |
| 1833 | |
| 1834 | void FileDialog::_update_recent_list() { |
| 1835 | recent_list->clear(); |
| 1836 | |
| 1837 | Vector<String> recent_dir_paths; |
| 1838 | Vector<String> recent_dir_names; |
| 1839 | |
| 1840 | for (uint32_t i = 0; i < global_recents.size(); i++) { |
| 1841 | String name = global_recents[i]; |
| 1842 | if (!_path_matches_access(name)) { |
| 1843 | continue; |
| 1844 | } |
| 1845 | |
| 1846 | if (!name.begins_with(root_prefix)) { |
| 1847 | continue; |
| 1848 | } |
| 1849 | |
| 1850 | if (!dir_access->dir_exists(name)) { |
| 1851 | // Remove invalid directory from the list of recent directories. |
| 1852 | global_recents.remove_at(i); |
| 1853 | i--; |
| 1854 | continue; |
| 1855 | } |
| 1856 | recent_dir_paths.append(name); |
| 1857 | |
| 1858 | // Compute recent directory display text. |
| 1859 | if (name == "res://" || name == "user://") { |
| 1860 | name = "/"; |
| 1861 | } else { |
| 1862 | name = name.trim_suffix("/").get_file(); |
| 1863 | } |
| 1864 | recent_dir_names.append(name); |
| 1865 | } |
| 1866 | |
| 1867 | // EditorNode::disambiguate_filenames(recent_dir_paths, recent_dir_names); // TODO Needs a non-editor method. |
| 1868 | |
| 1869 | const int recent_size = recent_dir_paths.size(); |
| 1870 | for (int i = 0; i < recent_size; i++) { |
| 1871 | recent_list->add_item(recent_dir_names[i], theme_cache.folder); |
| 1872 | recent_list->set_item_tooltip(-1, recent_dir_paths[i]); |
| 1873 | recent_list->set_item_metadata(-1, recent_dir_paths[i]); |
| 1874 | recent_list->set_item_icon_modulate(-1, _get_folder_color(recent_dir_paths[i])); |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | bool FileDialog::_path_matches_access(const String &p_path) const { |
| 1879 | bool is_res = p_path.begins_with("res://"); |
| 1880 | bool is_user = p_path.begins_with("user://"); |
| 1881 | if (access == ACCESS_RESOURCES) { |
| 1882 | return is_res; |
| 1883 | } else if (access == ACCESS_USERDATA) { |
| 1884 | return is_user; |
| 1885 | } |
| 1886 | return !is_res && !is_user; |
| 1887 | } |
| 1888 | |
| 1889 | TypedArray<Dictionary> FileDialog::_get_options() const { |
| 1890 | TypedArray<Dictionary> out; |
| 1891 | for (const FileDialog::Option &opt : options) { |
| 1892 | Dictionary dict; |
| 1893 | dict["name"] = opt.name; |
| 1894 | dict["values"] = opt.values; |
| 1895 | dict["default"] = (int)selected_options.get(opt.name, opt.default_idx); |
| 1896 | out.push_back(dict); |
| 1897 | } |
| 1898 | return out; |
| 1899 | } |
| 1900 | |
| 1901 | void FileDialog::_option_changed_checkbox_toggled(bool p_pressed, const String &p_name) { |
| 1902 | if (selected_options.has(p_name)) { |
| 1903 | selected_options[p_name] = p_pressed; |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | void FileDialog::_option_changed_item_selected(int p_idx, const String &p_name) { |
| 1908 | if (selected_options.has(p_name)) { |
| 1909 | selected_options[p_name] = p_idx; |
| 1910 | } |
| 1911 | } |
| 1912 | |
| 1913 | void FileDialog::_update_option_controls() { |
| 1914 | if (!options_dirty) { |
| 1915 | return; |
| 1916 | } |
| 1917 | options_dirty = false; |
| 1918 | |
| 1919 | while (flow_checkbox_options->get_child_count() > 0) { |
| 1920 | Node *child = flow_checkbox_options->get_child(0); |
| 1921 | flow_checkbox_options->remove_child(child); |
| 1922 | child->queue_free(); |
| 1923 | } |
| 1924 | while (grid_select_options->get_child_count() > 0) { |
| 1925 | Node *child = grid_select_options->get_child(0); |
| 1926 | grid_select_options->remove_child(child); |
| 1927 | child->queue_free(); |
| 1928 | } |
| 1929 | selected_options.clear(); |
| 1930 | |
| 1931 | for (const FileDialog::Option &opt : options) { |
| 1932 | if (opt.values.is_empty()) { |
| 1933 | CheckBox *cb = memnew(CheckBox); |
| 1934 | cb->set_text(opt.name); |
| 1935 | cb->set_accessibility_name(opt.name); |
| 1936 | cb->set_pressed(opt.default_idx); |
| 1937 | flow_checkbox_options->add_child(cb); |
| 1938 | cb->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::_option_changed_checkbox_toggled).bind(opt.name)); |
| 1939 | selected_options[opt.name] = (bool)opt.default_idx; |
| 1940 | } else { |
| 1941 | Label *lbl = memnew(Label); |
| 1942 | lbl->set_text(opt.name); |
| 1943 | grid_select_options->add_child(lbl); |
| 1944 | |
| 1945 | OptionButton *ob = memnew(OptionButton); |
| 1946 | for (const String &val : opt.values) { |
| 1947 | ob->add_item(val); |
| 1948 | } |
| 1949 | ob->set_accessibility_name(opt.name); |
| 1950 | ob->select(opt.default_idx); |
| 1951 | grid_select_options->add_child(ob); |
| 1952 | ob->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_option_changed_item_selected).bind(opt.name)); |
| 1953 | selected_options[opt.name] = opt.default_idx; |
| 1954 | } |
| 1955 | } |
| 1956 | } |
| 1957 | |
| 1958 | Dictionary FileDialog::get_selected_options() const { |
| 1959 | return selected_options; |
| 1960 | } |
| 1961 | |
| 1962 | String FileDialog::get_option_name(int p_option) const { |
| 1963 | ERR_FAIL_INDEX_V(p_option, options.size(), String()); |
| 1964 | return options[p_option].name; |
| 1965 | } |
| 1966 | |
| 1967 | Vector<String> FileDialog::get_option_values(int p_option) const { |
| 1968 | ERR_FAIL_INDEX_V(p_option, options.size(), Vector<String>()); |
| 1969 | return options[p_option].values; |
| 1970 | } |
| 1971 | |
| 1972 | int FileDialog::get_option_default(int p_option) const { |
| 1973 | ERR_FAIL_INDEX_V(p_option, options.size(), -1); |
| 1974 | return options[p_option].default_idx; |
| 1975 | } |
| 1976 | |
| 1977 | void FileDialog::set_option_name(int p_option, const String &p_name) { |
| 1978 | if (p_option < 0) { |
| 1979 | p_option += get_option_count(); |
| 1980 | } |
| 1981 | ERR_FAIL_INDEX(p_option, options.size()); |
| 1982 | options.write[p_option].name = p_name; |
| 1983 | options_dirty = true; |
| 1984 | if (is_visible()) { |
| 1985 | _update_option_controls(); |
| 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | void FileDialog::set_option_values(int p_option, const Vector<String> &p_values) { |
| 1990 | if (p_option < 0) { |
| 1991 | p_option += get_option_count(); |
| 1992 | } |
| 1993 | ERR_FAIL_INDEX(p_option, options.size()); |
| 1994 | options.write[p_option].values = p_values; |
| 1995 | if (p_values.is_empty()) { |
| 1996 | options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, 1); |
| 1997 | } else { |
| 1998 | options.write[p_option].default_idx = CLAMP(options[p_option].default_idx, 0, options[p_option].values.size() - 1); |
| 1999 | } |
| 2000 | options_dirty = true; |
| 2001 | if (is_visible()) { |
| 2002 | _update_option_controls(); |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | void FileDialog::set_option_default(int p_option, int p_index) { |
| 2007 | if (p_option < 0) { |
| 2008 | p_option += get_option_count(); |
| 2009 | } |
| 2010 | ERR_FAIL_INDEX(p_option, options.size()); |
| 2011 | if (options[p_option].values.is_empty()) { |
| 2012 | options.write[p_option].default_idx = CLAMP(p_index, 0, 1); |
| 2013 | } else { |
| 2014 | options.write[p_option].default_idx = CLAMP(p_index, 0, options[p_option].values.size() - 1); |
| 2015 | } |
| 2016 | options_dirty = true; |
| 2017 | if (is_visible()) { |
| 2018 | _update_option_controls(); |
| 2019 | } |
| 2020 | } |
| 2021 | |
| 2022 | void FileDialog::add_option(const String &p_name, const Vector<String> &p_values, int p_index) { |
| 2023 | Option opt; |
| 2024 | opt.name = p_name; |
| 2025 | opt.values = p_values; |
| 2026 | if (opt.values.is_empty()) { |
| 2027 | opt.default_idx = CLAMP(p_index, 0, 1); |
| 2028 | } else { |
| 2029 | opt.default_idx = CLAMP(p_index, 0, opt.values.size() - 1); |
| 2030 | } |
| 2031 | options.push_back(opt); |
| 2032 | options_dirty = true; |
| 2033 | if (is_visible()) { |
| 2034 | _update_option_controls(); |
| 2035 | } |
| 2036 | } |
| 2037 | |
| 2038 | void FileDialog::set_option_count(int p_count) { |
| 2039 | ERR_FAIL_COND(p_count < 0); |
| 2040 | |
| 2041 | if (options.size() == p_count) { |
| 2042 | return; |
| 2043 | } |
| 2044 | options.resize(p_count); |
| 2045 | |
| 2046 | options_dirty = true; |
| 2047 | notify_property_list_changed(); |
| 2048 | if (is_visible()) { |
| 2049 | _update_option_controls(); |
| 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | int FileDialog::get_option_count() const { |
| 2054 | return options.size(); |
| 2055 | } |
| 2056 | |
| 2057 | void FileDialog::_bind_methods() { |
| 2058 | ClassDB::bind_method(D_METHOD("_cancel_pressed"), &FileDialog::_cancel_pressed); |
| 2059 | |
| 2060 | ClassDB::bind_method(D_METHOD("clear_filters"), &FileDialog::clear_filters); |
| 2061 | ClassDB::bind_method(D_METHOD("add_filter", "filter", "description", "mime_type"), &FileDialog::add_filter, DEFVAL(""), DEFVAL("")); |
| 2062 | ClassDB::bind_method(D_METHOD("set_filters", "filters"), &FileDialog::set_filters); |
| 2063 | ClassDB::bind_method(D_METHOD("get_filters"), &FileDialog::get_filters); |
| 2064 | ClassDB::bind_method(D_METHOD("clear_filename_filter"), &FileDialog::clear_filename_filter); |
| 2065 | ClassDB::bind_method(D_METHOD("set_filename_filter", "filter"), &FileDialog::set_filename_filter); |
| 2066 | ClassDB::bind_method(D_METHOD("get_filename_filter"), &FileDialog::get_filename_filter); |
| 2067 | ClassDB::bind_method(D_METHOD("get_option_name", "option"), &FileDialog::get_option_name); |
| 2068 | ClassDB::bind_method(D_METHOD("get_option_values", "option"), &FileDialog::get_option_values); |
| 2069 | ClassDB::bind_method(D_METHOD("get_option_default", "option"), &FileDialog::get_option_default); |
| 2070 | ClassDB::bind_method(D_METHOD("set_option_name", "option", "name"), &FileDialog::set_option_name); |
| 2071 | ClassDB::bind_method(D_METHOD("set_option_values", "option", "values"), &FileDialog::set_option_values); |
| 2072 | ClassDB::bind_method(D_METHOD("set_option_default", "option", "default_value_index"), &FileDialog::set_option_default); |
| 2073 | ClassDB::bind_method(D_METHOD("set_option_count", "count"), &FileDialog::set_option_count); |
| 2074 | ClassDB::bind_method(D_METHOD("get_option_count"), &FileDialog::get_option_count); |
| 2075 | ClassDB::bind_method(D_METHOD("add_option", "name", "values", "default_value_index"), &FileDialog::add_option); |
| 2076 | ClassDB::bind_method(D_METHOD("get_selected_options"), &FileDialog::get_selected_options); |
| 2077 | ClassDB::bind_method(D_METHOD("get_current_dir"), &FileDialog::get_current_dir); |
| 2078 | ClassDB::bind_method(D_METHOD("get_current_file"), &FileDialog::get_current_file); |
| 2079 | ClassDB::bind_method(D_METHOD("get_current_path"), &FileDialog::get_current_path); |
| 2080 | ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir); |
| 2081 | ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file); |
| 2082 | ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path); |
| 2083 | ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title); |
| 2084 | ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title); |
| 2085 | ClassDB::bind_method(D_METHOD("set_file_mode", "mode"), &FileDialog::set_file_mode); |
| 2086 | ClassDB::bind_method(D_METHOD("get_file_mode"), &FileDialog::get_file_mode); |
| 2087 | ClassDB::bind_method(D_METHOD("set_display_mode", "mode"), &FileDialog::set_display_mode); |
| 2088 | ClassDB::bind_method(D_METHOD("get_display_mode"), &FileDialog::get_display_mode); |
| 2089 | ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox); |
| 2090 | ClassDB::bind_method(D_METHOD("get_line_edit"), &FileDialog::get_line_edit); |
| 2091 | ClassDB::bind_method(D_METHOD("set_access", "access"), &FileDialog::set_access); |
| 2092 | ClassDB::bind_method(D_METHOD("get_access"), &FileDialog::get_access); |
| 2093 | ClassDB::bind_method(D_METHOD("set_root_subfolder", "dir"), &FileDialog::set_root_subfolder); |
| 2094 | ClassDB::bind_method(D_METHOD("get_root_subfolder"), &FileDialog::get_root_subfolder); |
| 2095 | ClassDB::bind_method(D_METHOD("set_show_hidden_files", "show"), &FileDialog::set_show_hidden_files); |
| 2096 | ClassDB::bind_method(D_METHOD("is_showing_hidden_files"), &FileDialog::is_showing_hidden_files); |
| 2097 | ClassDB::bind_method(D_METHOD("set_use_native_dialog", "native"), &FileDialog::set_use_native_dialog); |
| 2098 | ClassDB::bind_method(D_METHOD("get_use_native_dialog"), &FileDialog::get_use_native_dialog); |
| 2099 | ClassDB::bind_method(D_METHOD("set_customization_flag_enabled", "flag", "enabled"), &FileDialog::set_customization_flag_enabled); |
| 2100 | ClassDB::bind_method(D_METHOD("is_customization_flag_enabled", "flag"), &FileDialog::is_customization_flag_enabled); |
| 2101 | ClassDB::bind_method(D_METHOD("deselect_all"), &FileDialog::deselect_all); |
| 2102 | |
| 2103 | ClassDB::bind_static_method("FileDialog", D_METHOD("set_favorite_list", "favorites"), &FileDialog::set_favorite_list); |
| 2104 | ClassDB::bind_static_method("FileDialog", D_METHOD("get_favorite_list"), &FileDialog::get_favorite_list); |
| 2105 | ClassDB::bind_static_method("FileDialog", D_METHOD("set_recent_list", "recents"), &FileDialog::set_recent_list); |
| 2106 | ClassDB::bind_static_method("FileDialog", D_METHOD("get_recent_list"), &FileDialog::get_recent_list); |
| 2107 | ClassDB::bind_static_method("FileDialog", D_METHOD("set_get_icon_callback", "callback"), &FileDialog::set_get_icon_callback); |
| 2108 | ClassDB::bind_static_method("FileDialog", D_METHOD("set_get_thumbnail_callback", "callback"), &FileDialog::set_get_thumbnail_callback); |
| 2109 | |
| 2110 | ClassDB::bind_method(D_METHOD("popup_file_dialog"), &FileDialog::popup_file_dialog); |
| 2111 | ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate); |
| 2112 | |
| 2113 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title"); |
| 2114 | ADD_PROPERTY(PropertyInfo(Variant::INT, "file_mode", PROPERTY_HINT_ENUM, "Open File,Open Files,Open Folder,Open Any,Save"), "set_file_mode", "get_file_mode"); |
| 2115 | ADD_PROPERTY(PropertyInfo(Variant::INT, "display_mode", PROPERTY_HINT_ENUM, "Thumbnails,List"), "set_display_mode", "get_display_mode"); |
| 2116 | ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User Data,File System"), "set_access", "get_access"); |
| 2117 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "root_subfolder"), "set_root_subfolder", "get_root_subfolder"); |
| 2118 | ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "filters"), "set_filters", "get_filters"); |
| 2119 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "filename_filter"), "set_filename_filter", "get_filename_filter"); |
| 2120 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "show_hidden_files"), "set_show_hidden_files", "is_showing_hidden_files"); |
| 2121 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_native_dialog"), "set_use_native_dialog", "get_use_native_dialog"); |
| 2122 | |
| 2123 | ADD_ARRAY_COUNT("Options", "option_count", "set_option_count", "get_option_count", "option_"); |
| 2124 | |
| 2125 | ADD_GROUP("Customization", ""); |
| 2126 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "hidden_files_toggle_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_HIDDEN_FILES); |
| 2127 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "file_filter_toggle_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_FILE_FILTER); |
| 2128 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "file_sort_options_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_FILE_SORT); |
| 2129 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "folder_creation_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_CREATE_FOLDER); |
| 2130 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "favorites_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_FAVORITES); |
| 2131 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "recent_list_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_RECENT); |
| 2132 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "layout_toggle_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_LAYOUT); |
| 2133 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "overwrite_warning_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_OVERWRITE_WARNING); |
| 2134 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "deleting_enabled"), "set_customization_flag_enabled", "is_customization_flag_enabled", CUSTOMIZATION_DELETE); |
| 2135 | |
| 2136 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_dir", PROPERTY_HINT_DIR, "", PROPERTY_USAGE_NONE), "set_current_dir", "get_current_dir"); |
| 2137 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_file", PROPERTY_HINT_FILE_PATH, "*", PROPERTY_USAGE_NONE), "set_current_file", "get_current_file"); |
| 2138 | ADD_PROPERTY(PropertyInfo(Variant::STRING, "current_path", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_current_path", "get_current_path"); |
| 2139 | |
| 2140 | ADD_SIGNAL(MethodInfo("file_selected", PropertyInfo(Variant::STRING, "path"))); |
| 2141 | ADD_SIGNAL(MethodInfo("files_selected", PropertyInfo(Variant::PACKED_STRING_ARRAY, "paths"))); |
| 2142 | ADD_SIGNAL(MethodInfo("dir_selected", PropertyInfo(Variant::STRING, "dir"))); |
| 2143 | ADD_SIGNAL(MethodInfo("filename_filter_changed", PropertyInfo(Variant::STRING, "filter"))); |
| 2144 | |
| 2145 | BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILE); |
| 2146 | BIND_ENUM_CONSTANT(FILE_MODE_OPEN_FILES); |
| 2147 | BIND_ENUM_CONSTANT(FILE_MODE_OPEN_DIR); |
| 2148 | BIND_ENUM_CONSTANT(FILE_MODE_OPEN_ANY); |
| 2149 | BIND_ENUM_CONSTANT(FILE_MODE_SAVE_FILE); |
| 2150 | |
| 2151 | BIND_ENUM_CONSTANT(ACCESS_RESOURCES); |
| 2152 | BIND_ENUM_CONSTANT(ACCESS_USERDATA); |
| 2153 | BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM); |
| 2154 | |
| 2155 | BIND_ENUM_CONSTANT(DISPLAY_THUMBNAILS); |
| 2156 | BIND_ENUM_CONSTANT(DISPLAY_LIST); |
| 2157 | |
| 2158 | BIND_ENUM_CONSTANT(CUSTOMIZATION_HIDDEN_FILES); |
| 2159 | BIND_ENUM_CONSTANT(CUSTOMIZATION_CREATE_FOLDER); |
| 2160 | BIND_ENUM_CONSTANT(CUSTOMIZATION_FILE_FILTER); |
| 2161 | BIND_ENUM_CONSTANT(CUSTOMIZATION_FILE_SORT); |
| 2162 | BIND_ENUM_CONSTANT(CUSTOMIZATION_FAVORITES); |
| 2163 | BIND_ENUM_CONSTANT(CUSTOMIZATION_RECENT); |
| 2164 | BIND_ENUM_CONSTANT(CUSTOMIZATION_LAYOUT); |
| 2165 | BIND_ENUM_CONSTANT(CUSTOMIZATION_OVERWRITE_WARNING); |
| 2166 | BIND_ENUM_CONSTANT(CUSTOMIZATION_DELETE); |
| 2167 | |
| 2168 | BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, FileDialog, thumbnail_size); |
| 2169 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, parent_folder); |
| 2170 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, forward_folder); |
| 2171 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, back_folder); |
| 2172 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, reload); |
| 2173 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, favorite); |
| 2174 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_hidden); |
| 2175 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, folder); |
| 2176 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, toggle_filename_filter); |
| 2177 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, file); |
| 2178 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, create_folder); |
| 2179 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, sort); |
| 2180 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, favorite_up); |
| 2181 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, favorite_down); |
| 2182 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, thumbnail_mode); |
| 2183 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, list_mode); |
| 2184 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, file_thumbnail); |
| 2185 | BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, FileDialog, folder_thumbnail); |
| 2186 | |
| 2187 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, folder_icon_color); |
| 2188 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_icon_color); |
| 2189 | BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, FileDialog, file_disabled_color); |
| 2190 | |
| 2191 | // TODO: Define own colors? |
| 2192 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_normal_color, "icon_normal_color", "Button"); |
| 2193 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_hover_color, "icon_hover_color", "Button"); |
| 2194 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_focus_color, "icon_focus_color", "Button"); |
| 2195 | BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, FileDialog, icon_pressed_color, "icon_pressed_color", "Button"); |
| 2196 | |
| 2197 | Option defaults; |
| 2198 | |
| 2199 | base_property_helper.set_prefix("option_"); |
| 2200 | base_property_helper.set_array_length_getter(&FileDialog::get_option_count); |
| 2201 | base_property_helper.register_property(PropertyInfo(Variant::STRING, "name"), defaults.name, &FileDialog::set_option_name, &FileDialog::get_option_name); |
| 2202 | base_property_helper.register_property(PropertyInfo(Variant::PACKED_STRING_ARRAY, "values"), defaults.values, &FileDialog::set_option_values, &FileDialog::get_option_values); |
| 2203 | base_property_helper.register_property(PropertyInfo(Variant::INT, "default"), defaults.default_idx, &FileDialog::set_option_default, &FileDialog::get_option_default); |
| 2204 | PropertyListHelper::register_base_helper(&base_property_helper); |
| 2205 | |
| 2206 | ADD_CLASS_DEPENDENCY("Button"); |
| 2207 | ADD_CLASS_DEPENDENCY("ConfirmationDialog"); |
| 2208 | ADD_CLASS_DEPENDENCY("LineEdit"); |
| 2209 | ADD_CLASS_DEPENDENCY("OptionButton"); |
| 2210 | ADD_CLASS_DEPENDENCY("Tree"); |
| 2211 | } |
| 2212 | |
| 2213 | void FileDialog::set_show_hidden_files(bool p_show) { |
| 2214 | if (show_hidden_files == p_show) { |
| 2215 | return; |
| 2216 | } |
| 2217 | show_hidden_files = p_show; |
| 2218 | invalidate(); |
| 2219 | } |
| 2220 | |
| 2221 | void FileDialog::set_show_filename_filter(bool p_show) { |
| 2222 | if (p_show == show_filename_filter) { |
| 2223 | return; |
| 2224 | } |
| 2225 | if (p_show) { |
| 2226 | filename_filter->grab_focus(); |
| 2227 | } else { |
| 2228 | if (filename_filter->has_focus()) { |
| 2229 | callable_mp((Control *)file_list, &Control::grab_focus).call_deferred(false); |
| 2230 | } |
| 2231 | } |
| 2232 | show_filename_filter = p_show; |
| 2233 | update_filename_filter_gui(); |
| 2234 | invalidate(); |
| 2235 | } |
| 2236 | |
| 2237 | bool FileDialog::get_show_filename_filter() const { |
| 2238 | return show_filename_filter; |
| 2239 | } |
| 2240 | |
| 2241 | bool FileDialog::is_showing_hidden_files() const { |
| 2242 | return show_hidden_files; |
| 2243 | } |
| 2244 | |
| 2245 | void FileDialog::set_default_show_hidden_files(bool p_show) { |
| 2246 | default_show_hidden_files = p_show; |
| 2247 | } |
| 2248 | |
| 2249 | void FileDialog::set_default_display_mode(DisplayMode p_mode) { |
| 2250 | default_display_mode = p_mode; |
| 2251 | } |
| 2252 | |
| 2253 | void FileDialog::set_get_icon_callback(const Callable &p_callback) { |
| 2254 | get_icon_callback = p_callback; |
| 2255 | } |
| 2256 | |
| 2257 | void FileDialog::set_get_thumbnail_callback(const Callable &p_callback) { |
| 2258 | get_thumbnail_callback = p_callback; |
| 2259 | } |
| 2260 | |
| 2261 | void FileDialog::set_use_native_dialog(bool p_native) { |
| 2262 | use_native_dialog = p_native; |
| 2263 | |
| 2264 | #ifdef TOOLS_ENABLED |
| 2265 | if (is_part_of_edited_scene()) { |
| 2266 | return; |
| 2267 | } |
| 2268 | #endif |
| 2269 | |
| 2270 | // Replace the built-in dialog with the native one if it's currently visible. |
| 2271 | if (is_inside_tree() && is_visible() && _should_use_native_popup()) { |
| 2272 | ConfirmationDialog::set_visible(false); |
| 2273 | _native_popup(); |
| 2274 | } |
| 2275 | } |
| 2276 | |
| 2277 | bool FileDialog::get_use_native_dialog() const { |
| 2278 | return use_native_dialog; |
| 2279 | } |
| 2280 | |
| 2281 | FileDialog::FileDialog() { |
| 2282 | set_title(ETR("Save a File")); |
| 2283 | set_hide_on_ok(false); |
| 2284 | set_size(Size2(640, 360)); |
| 2285 | set_default_ok_text(ETR("Save")); // Default mode text. |
| 2286 | set_process_shortcut_input(true); |
| 2287 | |
| 2288 | for (int i = 0; i < CUSTOMIZATION_MAX; i++) { |
| 2289 | customization_flags[i] = true; |
| 2290 | } |
| 2291 | |
| 2292 | action_shortcuts[ITEM_MENU_DELETE] = Shortcut::make_from_action("ui_filedialog_delete"); |
| 2293 | action_shortcuts[ITEM_MENU_GO_UP] = Shortcut::make_from_action("ui_filedialog_up_one_level"); |
| 2294 | action_shortcuts[ITEM_MENU_REFRESH] = Shortcut::make_from_action("ui_filedialog_refresh"); |
| 2295 | action_shortcuts[ITEM_MENU_TOGGLE_HIDDEN] = Shortcut::make_from_action("ui_filedialog_show_hidden"); |
| 2296 | action_shortcuts[ITEM_MENU_FIND] = Shortcut::make_from_action("ui_filedialog_find"); |
| 2297 | action_shortcuts[ITEM_MENU_FOCUS_PATH] = Shortcut::make_from_action("ui_filedialog_focus_path"); |
| 2298 | |
| 2299 | show_hidden_files = default_show_hidden_files; |
| 2300 | display_mode = default_display_mode; |
| 2301 | dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES); |
| 2302 | |
| 2303 | main_vbox = memnew(VBoxContainer); |
| 2304 | add_child(main_vbox, false, INTERNAL_MODE_FRONT); |
| 2305 | |
| 2306 | HBoxContainer *top_toolbar = memnew(HBoxContainer); |
| 2307 | main_vbox->add_child(top_toolbar); |
| 2308 | |
| 2309 | dir_prev = memnew(Button); |
| 2310 | dir_prev->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2311 | dir_prev->set_tooltip_text(ETR("Go to previous folder.")); |
| 2312 | top_toolbar->add_child(dir_prev); |
| 2313 | dir_prev->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_back)); |
| 2314 | |
| 2315 | dir_next = memnew(Button); |
| 2316 | dir_next->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2317 | dir_next->set_tooltip_text(ETR("Go to next folder.")); |
| 2318 | top_toolbar->add_child(dir_next); |
| 2319 | dir_next->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_forward)); |
| 2320 | |
| 2321 | dir_up = memnew(Button); |
| 2322 | dir_up->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2323 | dir_up->set_tooltip_text(ETR("Go to parent folder.")); |
| 2324 | dir_up->set_shortcut(action_shortcuts[ITEM_MENU_GO_UP]); |
| 2325 | top_toolbar->add_child(dir_up); |
| 2326 | dir_up->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_go_up)); |
| 2327 | |
| 2328 | { |
| 2329 | Label *label = memnew(Label(ETR("Path:"))); |
| 2330 | top_toolbar->add_child(label); |
| 2331 | } |
| 2332 | |
| 2333 | drives_container = memnew(HBoxContainer); |
| 2334 | top_toolbar->add_child(drives_container); |
| 2335 | |
| 2336 | drives = memnew(OptionButton); |
| 2337 | drives->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_select_drive)); |
| 2338 | drives->set_accessibility_name(ETR("Drive")); |
| 2339 | top_toolbar->add_child(drives); |
| 2340 | |
| 2341 | directory_edit = memnew(LineEdit); |
| 2342 | directory_edit->set_accessibility_name(ETR("Path:")); |
| 2343 | directory_edit->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE); |
| 2344 | directory_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2345 | top_toolbar->add_child(directory_edit); |
| 2346 | directory_edit->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_dir_submitted)); |
| 2347 | |
| 2348 | shortcuts_container = memnew(HBoxContainer); |
| 2349 | top_toolbar->add_child(shortcuts_container); |
| 2350 | |
| 2351 | refresh_button = memnew(Button); |
| 2352 | refresh_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2353 | refresh_button->set_tooltip_text(ETR("Refresh files.")); |
| 2354 | refresh_button->set_shortcut(action_shortcuts[ITEM_MENU_REFRESH]); |
| 2355 | top_toolbar->add_child(refresh_button); |
| 2356 | refresh_button->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::update_file_list)); |
| 2357 | |
| 2358 | favorite_button = memnew(Button); |
| 2359 | favorite_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2360 | favorite_button->set_tooltip_text(ETR("(Un)favorite current folder.")); |
| 2361 | top_toolbar->add_child(favorite_button); |
| 2362 | favorite_button->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_favorite_pressed)); |
| 2363 | |
| 2364 | make_dir_container = memnew(HBoxContainer); |
| 2365 | top_toolbar->add_child(make_dir_container); |
| 2366 | |
| 2367 | make_dir_container->add_child(memnew(VSeparator)); |
| 2368 | |
| 2369 | make_dir_button = memnew(Button); |
| 2370 | make_dir_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2371 | make_dir_button->set_tooltip_text(ETR("Create a new folder.")); |
| 2372 | make_dir_container->add_child(make_dir_button); |
| 2373 | make_dir_button->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_make_dir)); |
| 2374 | |
| 2375 | HSplitContainer *main_split = memnew(HSplitContainer); |
| 2376 | main_split->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2377 | main_vbox->add_child(main_split); |
| 2378 | |
| 2379 | { |
| 2380 | VSplitContainer *fav_split = memnew(VSplitContainer); |
| 2381 | main_split->add_child(fav_split); |
| 2382 | |
| 2383 | favorite_vbox = memnew(VBoxContainer); |
| 2384 | favorite_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2385 | fav_split->add_child(favorite_vbox); |
| 2386 | |
| 2387 | HBoxContainer *fav_hbox = memnew(HBoxContainer); |
| 2388 | favorite_vbox->add_child(fav_hbox); |
| 2389 | |
| 2390 | { |
| 2391 | Label *label = memnew(Label(ETR("Favorites:"))); |
| 2392 | label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2393 | label->set_theme_type_variation("HeaderSmall"); |
| 2394 | fav_hbox->add_child(label); |
| 2395 | } |
| 2396 | |
| 2397 | fav_up_button = memnew(Button); |
| 2398 | fav_up_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2399 | fav_hbox->add_child(fav_up_button); |
| 2400 | fav_up_button->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_favorite_move_up)); |
| 2401 | |
| 2402 | fav_down_button = memnew(Button); |
| 2403 | fav_down_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2404 | fav_hbox->add_child(fav_down_button); |
| 2405 | fav_down_button->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::_favorite_move_down)); |
| 2406 | |
| 2407 | favorite_list = memnew(ItemList); |
| 2408 | favorite_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2409 | favorite_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2410 | favorite_list->set_theme_type_variation("ItemListSecondary"); |
| 2411 | favorite_list->set_accessibility_name(ETR("Favorites:")); |
| 2412 | favorite_vbox->add_child(favorite_list); |
| 2413 | favorite_list->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_favorite_selected)); |
| 2414 | |
| 2415 | recent_vbox = memnew(VBoxContainer); |
| 2416 | recent_vbox->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2417 | fav_split->add_child(recent_vbox); |
| 2418 | |
| 2419 | { |
| 2420 | Label *label = memnew(Label(ETR("Recent:"))); |
| 2421 | label->set_theme_type_variation("HeaderSmall"); |
| 2422 | recent_vbox->add_child(label); |
| 2423 | } |
| 2424 | |
| 2425 | recent_list = memnew(ItemList); |
| 2426 | recent_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2427 | recent_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2428 | recent_list->set_theme_type_variation("ItemListSecondary"); |
| 2429 | recent_list->set_accessibility_name(ETR("Recent:")); |
| 2430 | recent_vbox->add_child(recent_list); |
| 2431 | recent_list->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_recent_selected)); |
| 2432 | } |
| 2433 | |
| 2434 | VBoxContainer *file_vbox = memnew(VBoxContainer); |
| 2435 | main_split->add_child(file_vbox); |
| 2436 | |
| 2437 | HBoxContainer *lower_toolbar = memnew(HBoxContainer); |
| 2438 | file_vbox->add_child(lower_toolbar); |
| 2439 | |
| 2440 | { |
| 2441 | Label *label = memnew(Label(ETR("Directories & Files:"))); |
| 2442 | label->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2443 | label->set_theme_type_variation("HeaderSmall"); |
| 2444 | lower_toolbar->add_child(label); |
| 2445 | } |
| 2446 | |
| 2447 | show_hidden = memnew(Button); |
| 2448 | show_hidden->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2449 | show_hidden->set_toggle_mode(true); |
| 2450 | show_hidden->set_pressed(is_showing_hidden_files()); |
| 2451 | show_hidden->set_tooltip_text(ETR("Toggle the visibility of hidden files.")); |
| 2452 | show_hidden->set_shortcut(action_shortcuts[ITEM_MENU_TOGGLE_HIDDEN]); |
| 2453 | lower_toolbar->add_child(show_hidden); |
| 2454 | show_hidden->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_hidden_files)); |
| 2455 | |
| 2456 | show_hidden_separator = memnew(VSeparator); |
| 2457 | lower_toolbar->add_child(show_hidden_separator); |
| 2458 | |
| 2459 | layout_container = memnew(HBoxContainer); |
| 2460 | lower_toolbar->add_child(layout_container); |
| 2461 | |
| 2462 | Ref<ButtonGroup> view_mode_group; |
| 2463 | view_mode_group.instantiate(); |
| 2464 | |
| 2465 | thumbnail_mode_button = memnew(Button); |
| 2466 | thumbnail_mode_button->set_toggle_mode(true); |
| 2467 | thumbnail_mode_button->set_pressed(true); |
| 2468 | thumbnail_mode_button->set_button_group(view_mode_group); |
| 2469 | thumbnail_mode_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2470 | thumbnail_mode_button->set_tooltip_text(ETR("View items as a grid of thumbnails.")); |
| 2471 | layout_container->add_child(thumbnail_mode_button); |
| 2472 | thumbnail_mode_button->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::set_display_mode).bind(DISPLAY_THUMBNAILS)); |
| 2473 | |
| 2474 | list_mode_button = memnew(Button); |
| 2475 | list_mode_button->set_toggle_mode(true); |
| 2476 | list_mode_button->set_button_group(view_mode_group); |
| 2477 | list_mode_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2478 | list_mode_button->set_tooltip_text(ETR("View items as a list.")); |
| 2479 | layout_container->add_child(list_mode_button); |
| 2480 | list_mode_button->connect(SceneStringName(pressed), callable_mp(this, &FileDialog::set_display_mode).bind(DISPLAY_LIST)); |
| 2481 | |
| 2482 | layout_separator = memnew(VSeparator); |
| 2483 | layout_container->add_child(layout_separator); |
| 2484 | |
| 2485 | show_filename_filter_button = memnew(Button); |
| 2486 | show_filename_filter_button->set_theme_type_variation(SceneStringName(FlatButton)); |
| 2487 | show_filename_filter_button->set_toggle_mode(true); |
| 2488 | show_filename_filter_button->set_tooltip_text(ETR("Toggle the visibility of the filter for file names.")); |
| 2489 | show_filename_filter_button->set_shortcut(action_shortcuts[ITEM_MENU_FIND]); |
| 2490 | lower_toolbar->add_child(show_filename_filter_button); |
| 2491 | show_filename_filter_button->connect(SceneStringName(toggled), callable_mp(this, &FileDialog::set_show_filename_filter)); |
| 2492 | |
| 2493 | file_sort_button = memnew(MenuButton); |
| 2494 | file_sort_button->set_flat(false); |
| 2495 | file_sort_button->set_theme_type_variation("FlatMenuButton"); |
| 2496 | file_sort_button->set_tooltip_text(ETR("Sort files")); |
| 2497 | |
| 2498 | PopupMenu *sort_menu = file_sort_button->get_popup(); |
| 2499 | sort_menu->add_radio_check_item(ETR("Sort by Name (Ascending)"), int(FileSortOption::NAME)); |
| 2500 | sort_menu->add_radio_check_item(ETR("Sort by Name (Descending)"), int(FileSortOption::NAME_REVERSE)); |
| 2501 | sort_menu->add_radio_check_item(ETR("Sort by Type (Ascending)"), int(FileSortOption::TYPE)); |
| 2502 | sort_menu->add_radio_check_item(ETR("Sort by Type (Descending)"), int(FileSortOption::TYPE_REVERSE)); |
| 2503 | sort_menu->add_radio_check_item(ETR("Sort by Modified Time (Newest First)"), int(FileSortOption::MODIFIED_TIME)); |
| 2504 | sort_menu->add_radio_check_item(ETR("Sort by Modified Time (Oldest First)"), int(FileSortOption::MODIFIED_TIME_REVERSE)); |
| 2505 | sort_menu->set_item_checked(0, true); |
| 2506 | lower_toolbar->add_child(file_sort_button); |
| 2507 | sort_menu->connect(SceneStringName(id_pressed), callable_mp(this, &FileDialog::_sort_option_selected)); |
| 2508 | |
| 2509 | file_list = memnew(ItemList); |
| 2510 | file_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); |
| 2511 | file_list->set_v_size_flags(Control::SIZE_EXPAND_FILL); |
| 2512 | file_list->set_theme_type_variation("ItemListSecondary"); |
| 2513 | file_list->set_accessibility_name(ETR("Directories & Files:")); |
| 2514 | file_list->set_allow_rmb_select(true); |
| 2515 | file_vbox->add_child(file_list); |
| 2516 | file_list->connect("multi_selected", callable_mp(this, &FileDialog::_file_list_multi_selected)); |
| 2517 | file_list->connect("item_selected", callable_mp(this, &FileDialog::_file_list_selected)); |
| 2518 | file_list->connect("item_activated", callable_mp(this, &FileDialog::_file_list_item_activated)); |
| 2519 | file_list->connect("item_clicked", callable_mp(this, &FileDialog::_item_clicked)); |
| 2520 | file_list->connect("empty_clicked", callable_mp(this, &FileDialog::_empty_clicked)); |
| 2521 | |
| 2522 | message = memnew(Label); |
| 2523 | message->set_focus_mode(Control::FOCUS_ACCESSIBILITY); |
| 2524 | message->hide(); |
| 2525 | message->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT); |
| 2526 | message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER); |
| 2527 | message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER); |
| 2528 | file_list->add_child(message); |
| 2529 | |
| 2530 | filename_filter_box = memnew(HBoxContainer); |
| 2531 | filename_filter_box->set_visible(false); |
| 2532 | file_vbox->add_child(filename_filter_box); |
| 2533 | |
| 2534 | { |
| 2535 | Label *label = memnew(Label(ETR("Filter:"))); |
| 2536 | filename_filter_box->add_child(label); |
| 2537 | } |
| 2538 | |
| 2539 | filename_filter = memnew(LineEdit); |
| 2540 | filename_filter->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE); |
| 2541 | filename_filter->set_stretch_ratio(4); |
| 2542 | filename_filter->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2543 | filename_filter->set_clear_button_enabled(true); |
| 2544 | filename_filter->set_accessibility_name(ETR("Filename Filter:")); |
| 2545 | filename_filter_box->add_child(filename_filter); |
| 2546 | filename_filter->connect(SceneStringName(text_changed), callable_mp(this, &FileDialog::_filename_filter_changed).unbind(1)); |
| 2547 | filename_filter->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_filename_filter_selected).unbind(1)); |
| 2548 | |
| 2549 | file_box = memnew(HBoxContainer); |
| 2550 | file_vbox->add_child(file_box); |
| 2551 | |
| 2552 | { |
| 2553 | Label *label = memnew(Label(ETR("File:"))); |
| 2554 | file_box->add_child(label); |
| 2555 | } |
| 2556 | |
| 2557 | filename_edit = memnew(LineEdit); |
| 2558 | filename_edit->set_accessibility_name(ETR("File:")); |
| 2559 | filename_edit->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE); |
| 2560 | filename_edit->set_stretch_ratio(4); |
| 2561 | filename_edit->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2562 | file_box->add_child(filename_edit); |
| 2563 | filename_edit->connect(SceneStringName(text_submitted), callable_mp(this, &FileDialog::_action_pressed).unbind(1)); |
| 2564 | |
| 2565 | filter = memnew(OptionButton); |
| 2566 | filter->set_stretch_ratio(3); |
| 2567 | filter->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2568 | filter->set_clip_text(true); // Too many extensions overflows it. |
| 2569 | file_box->add_child(filter); |
| 2570 | filter->connect(SceneStringName(item_selected), callable_mp(this, &FileDialog::_filter_selected)); |
| 2571 | |
| 2572 | flow_checkbox_options = memnew(HFlowContainer); |
| 2573 | flow_checkbox_options->set_h_size_flags(Control::SIZE_EXPAND_FILL); |
| 2574 | flow_checkbox_options->set_alignment(FlowContainer::ALIGNMENT_CENTER); |
| 2575 | main_vbox->add_child(flow_checkbox_options); |
| 2576 | |
| 2577 | grid_select_options = memnew(GridContainer); |
| 2578 | grid_select_options->set_h_size_flags(Control::SIZE_SHRINK_CENTER); |
| 2579 | grid_select_options->set_columns(2); |
| 2580 | main_vbox->add_child(grid_select_options); |
| 2581 | |
| 2582 | confirm_save = memnew(ConfirmationDialog); |
| 2583 | add_child(confirm_save, false, INTERNAL_MODE_FRONT); |
| 2584 | confirm_save->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_save_confirm_pressed)); |
| 2585 | |
| 2586 | delete_dialog = memnew(ConfirmationDialog); |
| 2587 | delete_dialog->set_text(ETR("Delete the selected file?\nDepending on your filesystem configuration, the files will either be moved to the system trash or deleted permanently.")); |
| 2588 | add_child(delete_dialog, false, INTERNAL_MODE_FRONT); |
| 2589 | delete_dialog->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_delete_confirm)); |
| 2590 | |
| 2591 | make_dir_dialog = memnew(ConfirmationDialog); |
| 2592 | make_dir_dialog->set_title(ETR("Create Folder")); |
| 2593 | add_child(make_dir_dialog, false, INTERNAL_MODE_FRONT); |
| 2594 | make_dir_dialog->connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_make_dir_confirm)); |
| 2595 | |
| 2596 | VBoxContainer *makevb = memnew(VBoxContainer); |
| 2597 | make_dir_dialog->add_child(makevb); |
| 2598 | |
| 2599 | new_dir_name = memnew(LineEdit); |
| 2600 | new_dir_name->set_structured_text_bidi_override(TextServer::STRUCTURED_TEXT_FILE); |
| 2601 | makevb->add_margin_child(ETR("Name:"), new_dir_name); |
| 2602 | make_dir_dialog->register_text_enter(new_dir_name); |
| 2603 | |
| 2604 | mkdirerr = memnew(AcceptDialog); |
| 2605 | mkdirerr->set_text(ETR("Could not create folder.")); |
| 2606 | add_child(mkdirerr, false, INTERNAL_MODE_FRONT); |
| 2607 | |
| 2608 | exterr = memnew(AcceptDialog); |
| 2609 | exterr->set_text(ETR("Invalid extension, or empty filename.")); |
| 2610 | add_child(exterr, false, INTERNAL_MODE_FRONT); |
| 2611 | |
| 2612 | item_menu = memnew(PopupMenu); |
| 2613 | item_menu->connect(SceneStringName(id_pressed), callable_mp(this, &FileDialog::_item_menu_id_pressed)); |
| 2614 | add_child(item_menu, false, INTERNAL_MODE_FRONT); |
| 2615 | |
| 2616 | connect(SceneStringName(confirmed), callable_mp(this, &FileDialog::_action_pressed)); |
| 2617 | |
| 2618 | _update_drives(); |
| 2619 | update_filters(); |
| 2620 | update_filename_filter_gui(); |
| 2621 | update_dir(); |
| 2622 | |
| 2623 | if (register_func) { |
| 2624 | register_func(this); |
| 2625 | } |
| 2626 | |
| 2627 | property_helper.setup_for_instance(base_property_helper, this); |
| 2628 | } |
| 2629 | |
| 2630 | FileDialog::~FileDialog() { |
| 2631 | if (unregister_func) { |
| 2632 | unregister_func(this); |
| 2633 | } |
| 2634 | } |