summaryrefslogtreecommitdiff
path: root/scene/gui/file_dialog.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'scene/gui/file_dialog.cpp')
-rw-r--r--scene/gui/file_dialog.cpp121
1 files changed, 102 insertions, 19 deletions
diff --git a/scene/gui/file_dialog.cpp b/scene/gui/file_dialog.cpp
index 6aba535572..6af869c503 100644
--- a/scene/gui/file_dialog.cpp
+++ b/scene/gui/file_dialog.cpp
@@ -47,12 +47,7 @@ void FileDialog::_notification(int p_what) {
if (p_what == NOTIFICATION_ENTER_TREE) {
refresh->set_icon(get_icon("reload"));
- }
-
- if (p_what == NOTIFICATION_DRAW) {
-
- //RID ci = get_canvas_item();
- //get_stylebox("panel","PopupMenu")->draw(ci,Rect2(Point2(),get_size()));
+ dir_up->set_icon(get_icon("parent_folder"));
}
if (p_what == NOTIFICATION_POPUP_HIDE) {
@@ -85,6 +80,10 @@ void FileDialog::_unhandled_input(const Ref<InputEvent> &p_event) {
invalidate();
} break;
+ case KEY_BACKSPACE: {
+
+ _dir_entered("..");
+ } break;
default: { handled = false; }
}
@@ -118,6 +117,9 @@ void FileDialog::update_dir() {
if (drives->is_visible()) {
drives->select(dir_access->get_current_drive());
}
+
+ // Deselect any item, to make "Select Current Folder" button text by default.
+ deselect_items();
}
void FileDialog::_dir_entered(String p_dir) {
@@ -152,6 +154,10 @@ void FileDialog::_post_popup() {
tree->grab_focus();
set_process_unhandled_input(true);
+
+ // For open dir mode, deselect all items on file dialog open.
+ if (mode == MODE_OPEN_DIR)
+ deselect_items();
}
void FileDialog::_action_pressed() {
@@ -189,7 +195,7 @@ void FileDialog::_action_pressed() {
TreeItem *item = tree->get_selected();
if (item) {
Dictionary d = item->get_metadata(0);
- if (d["dir"]) {
+ if (d["dir"] && d["name"] != "..") {
path = path.plus_file(d["name"]);
}
}
@@ -272,6 +278,57 @@ void FileDialog::_cancel_pressed() {
hide();
}
+bool FileDialog::_is_open_should_be_disabled() {
+
+ if (mode == MODE_OPEN_ANY || mode == MODE_SAVE_FILE)
+ return false;
+
+ TreeItem *ti = tree->get_selected();
+ // We have something that we can't select?
+ if (!ti)
+ return true;
+
+ Dictionary d = ti->get_metadata(0);
+
+ // Opening a file, but selected a folder? Forbidden.
+ if (((mode == MODE_OPEN_FILE || mode == MODE_OPEN_FILES) && d["dir"]) || // Flipped case, also forbidden.
+ (mode == MODE_OPEN_DIR && !d["dir"]))
+ return true;
+
+ return false;
+}
+
+void FileDialog::_go_up() {
+
+ dir_access->change_dir("..");
+ update_file_list();
+ update_dir();
+}
+
+void FileDialog::deselect_items() {
+
+ // Clear currently selected items in file manager.
+ tree->deselect_all();
+
+ // And change get_ok title.
+ if (!tree->is_anything_selected()) {
+ get_ok()->set_disabled(_is_open_should_be_disabled());
+
+ switch (mode) {
+
+ case MODE_OPEN_FILE:
+ case MODE_OPEN_FILES:
+ get_ok()->set_text(TTR("Open"));
+ get_ok()->set_disabled(false);
+ break;
+
+ case MODE_OPEN_DIR:
+ get_ok()->set_text(TTR("Select Current Folder"));
+ get_ok()->set_disabled(false);
+ break;
+ }
+ }
+}
void FileDialog::_tree_selected() {
TreeItem *ti = tree->get_selected();
@@ -282,7 +339,11 @@ void FileDialog::_tree_selected() {
if (!d["dir"]) {
file->set_text(d["name"]);
+ } else if (mode == MODE_OPEN_DIR) {
+ get_ok()->set_text(TTR("Select this Folder"));
}
+
+ get_ok()->set_disabled(_is_open_should_be_disabled());
}
void FileDialog::_tree_dc_selected() {
@@ -323,7 +384,7 @@ void FileDialog::update_file_list() {
while ((item = dir_access->get_next(&isdir)) != "") {
- if (item == ".")
+ if (item == "." || item == "..")
continue;
ishidden = dir_access->current_is_hidden();
@@ -336,11 +397,6 @@ void FileDialog::update_file_list() {
}
}
- if (dirs.find("..") == NULL) {
- //may happen if lacking permissions
- dirs.push_back("..");
- }
-
dirs.sort_custom<NaturalNoCaseComparator>();
files.sort_custom<NaturalNoCaseComparator>();
@@ -547,6 +603,14 @@ void FileDialog::set_current_path(const String &p_path) {
}
}
+void FileDialog::set_mode_overrides_title(bool p_override) {
+ mode_overrides_title = p_override;
+}
+
+bool FileDialog::is_mode_overriding_title() const {
+ return mode_overrides_title;
+}
+
void FileDialog::set_mode(Mode p_mode) {
mode = p_mode;
@@ -554,27 +618,32 @@ void FileDialog::set_mode(Mode p_mode) {
case MODE_OPEN_FILE:
get_ok()->set_text(RTR("Open"));
- set_title(RTR("Open a File"));
+ if (mode_overrides_title)
+ set_title(RTR("Open a File"));
makedir->hide();
break;
case MODE_OPEN_FILES:
get_ok()->set_text(RTR("Open"));
- set_title(RTR("Open File(s)"));
+ if (mode_overrides_title)
+ set_title(RTR("Open File(s)"));
makedir->hide();
break;
case MODE_OPEN_DIR:
- get_ok()->set_text(RTR("Open"));
- set_title(RTR("Open a Directory"));
+ get_ok()->set_text(RTR("Select Current Folder"));
+ if (mode_overrides_title)
+ set_title(RTR("Open a Directory"));
makedir->show();
break;
case MODE_OPEN_ANY:
get_ok()->set_text(RTR("Open"));
- set_title(RTR("Open a File or Directory"));
+ if (mode_overrides_title)
+ set_title(RTR("Open a File or Directory"));
makedir->show();
break;
case MODE_SAVE_FILE:
get_ok()->set_text(RTR("Save"));
- set_title(RTR("Save a File"));
+ if (mode_overrides_title)
+ set_title(RTR("Save a File"));
makedir->show();
break;
}
@@ -704,6 +773,8 @@ void FileDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_current_dir", "dir"), &FileDialog::set_current_dir);
ClassDB::bind_method(D_METHOD("set_current_file", "file"), &FileDialog::set_current_file);
ClassDB::bind_method(D_METHOD("set_current_path", "path"), &FileDialog::set_current_path);
+ ClassDB::bind_method(D_METHOD("set_mode_overrides_title", "override"), &FileDialog::set_mode_overrides_title);
+ ClassDB::bind_method(D_METHOD("is_mode_overriding_title"), &FileDialog::is_mode_overriding_title);
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &FileDialog::set_mode);
ClassDB::bind_method(D_METHOD("get_mode"), &FileDialog::get_mode);
ClassDB::bind_method(D_METHOD("get_vbox"), &FileDialog::get_vbox);
@@ -716,6 +787,8 @@ void FileDialog::_bind_methods() {
ClassDB::bind_method(D_METHOD("_make_dir_confirm"), &FileDialog::_make_dir_confirm);
ClassDB::bind_method(D_METHOD("_update_file_list"), &FileDialog::update_file_list);
ClassDB::bind_method(D_METHOD("_update_dir"), &FileDialog::update_dir);
+ ClassDB::bind_method(D_METHOD("_go_up"), &FileDialog::_go_up);
+ ClassDB::bind_method(D_METHOD("deselect_items"), &FileDialog::deselect_items);
ClassDB::bind_method(D_METHOD("invalidate"), &FileDialog::invalidate);
@@ -733,6 +806,7 @@ void FileDialog::_bind_methods() {
BIND_ENUM_CONSTANT(ACCESS_USERDATA);
BIND_ENUM_CONSTANT(ACCESS_FILESYSTEM);
+ ADD_PROPERTY(PropertyInfo(Variant::BOOL, "mode_overrides_title"), "set_mode_overrides_title", "is_mode_overriding_title");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Open one,Open many,Open folder,Open any,Save"), "set_mode", "get_mode");
ADD_PROPERTY(PropertyInfo(Variant::INT, "access", PROPERTY_HINT_ENUM, "Resources,User data,File system"), "set_access", "get_access");
ADD_PROPERTY(PropertyInfo(Variant::POOL_STRING_ARRAY, "filters"), "set_filters", "get_filters");
@@ -756,6 +830,8 @@ FileDialog::FileDialog() {
show_hidden_files = default_show_hidden_files;
+ mode_overrides_title = true;
+
VBoxContainer *vbc = memnew(VBoxContainer);
add_child(vbc);
@@ -763,6 +839,12 @@ FileDialog::FileDialog() {
set_title(RTR("Save a File"));
HBoxContainer *hbc = memnew(HBoxContainer);
+
+ dir_up = memnew(ToolButton);
+ dir_up->set_tooltip(TTR("Go to parent folder"));
+ hbc->add_child(dir_up);
+ dir_up->connect("pressed", this, "_go_up");
+
hbc->add_child(memnew(Label(RTR("Path:"))));
dir = memnew(LineEdit);
hbc->add_child(dir);
@@ -807,6 +889,7 @@ FileDialog::FileDialog() {
//cancel->connect("pressed", this,"_cancel_pressed");
tree->connect("cell_selected", this, "_tree_selected", varray(), CONNECT_DEFERRED);
tree->connect("item_activated", this, "_tree_db_selected", varray());
+ tree->connect("nothing_selected", this, "deselect_items");
dir->connect("text_entered", this, "_dir_entered");
file->connect("text_entered", this, "_file_entered");
filter->connect("item_selected", this, "_filter_selected");