文件对话框¶
您可以使用文件选择对话框来允许用户选择一个文件以将其内容加载到他们的应用程序中,或者保存应用程序的当前内容。
注意
GTK 具有两种类型的文件选择对话框:一种是工具包本身提供的“内部”对话框;另一种是“原生”对话框,它将使用平台的自有对话框。
我们将始终使用“原生”文件选择对话框,因为它们也是与沙盒环境交互的首选方式。
重要提示
原生文件选择对话框不是小部件;您必须自行管理它们的生命周期。
打开文件¶
static void
on_file_open_response (GtkNativeDialog *native,
int response_id)
{
if (response_id == GTK_RESPONSE_ACCEPT) {
GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
g_autoptr (GFile) file = gtk_file_chooser_get_file (chooser);
open_file (file);
}
g_object_unref (native);
}
// ...
GtkFileChooserNative *native =
gtk_file_chooser_native_new ("Open File",
parent_window,
GTK_FILE_CHOOSER_ACTION_OPEN,
"_Open",
"_Cancel");
g_signal_connect (native, "response",
G_CALLBACK (on_file_open_response),
NULL);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
# Keep a reference on the native dialog; "self", in this case, is
# the application singleton instance
self._native = Gtk.FileChooserNative(
title="Open File",
# "self.main_window" is defined elsewhere as a Gtk.Window
transient_for=self.main_window,
action=Gtk.FileChooserAction.OPEN,
accept_label="_Open",
cancel_label="_Cancel",
)
def on_file_open_response(native, response):
if response == Gtk.ResponseType.ACCEPT:
self.open_file(native.get_file())
self._native = None
self._native.connect("response", on_file_open_response)
self._native.show()
var native = new Gtk.FileChooserNative ("Open File",
parent_window,
Gtk.FileChooserAction.OPEN,
"_Open",
"_Cancel");
native.response.connect ((response_id) => {
if (response_id == Gtk.ResponseType.ACCEPT)
open_file (native.get_file ());
native = null;
};
native.show ();
const native = new Gtk.FileChooserNative({
title: "Open File",
transient_for: parent_window,
action: Gtk.FileChooserAction.OPEN,
accept_label: "_Open",
cancel_label: "_Cancel",
});
native.connect("response", (self, response_id) => {
if (response_id === Gtk.ResponseType.ACCEPT) {
open_file(native.get_file());
}
});
native.show();
保存文件¶
static void
on_file_save_response (GtkNativeDialog *native,
int response_id)
{
if (response_id == GTK_RESPONSE_ACCEPT) {
GtkFileChooser *chooser = GTK_FILE_CHOOSER (native);
g_autoptr (GFile) file = gtk_file_chooser_get_file (chooser);
save_file (file);
}
g_object_unref (native);
}
// ...
GtkFileChooserNative *native =
gtk_file_chooser_native_new ("Save File",
parent_window,
GTK_FILE_CHOOSER_ACTION_SAVE,
"_Save",
"_Cancel");
g_signal_connect (native, "response",
G_CALLBACK (on_file_save_response),
NULL);
gtk_native_dialog_show (GTK_NATIVE_DIALOG (native));
# Keep a reference on the native dialog; "self", in this case, is
# the application singleton instance
self._native = Gtk.FileChooserNative(
title="Save File",
# "self.main_window" is defined elsewhere as a Gtk.Window
transient_for=self.main_window,
action=Gtk.FileChooserAction.SAVE,
accept_label="_Save",
cancel_label="_Cancel",
)
def on_file_save_response(native, response):
if response == Gtk.ResponseType.ACCEPT:
self.save_file(native.get_file())
self._native = None
self._native.connect("response", on_file_save_response)
self._native.show()
var native = new Gtk.FileChooserNative ("Save File",
parent_window,
Gtk.FileChooserAction.SAVE,
"_Save",
"_Cancel");
native.response.connect ((response_id) => {
if (response_id == Gtk.ResponseType.ACCEPT)
save_file (native.get_file ());
});
native.show ();
const native = new Gtk.FileChooserNative({
title: "Save File",
transient_for: parent_window,
action: Gtk.FileChooserAction.SAVE,
accept_label: "_Save",
cancel_label: "_Cancel",
});
native.connect("response", (self, response_id) => {
if (response_id === Gtk.ResponseType.ACCEPT) {
save_file(native.get_file());
}
});
native.show();
选项¶
文件选择对话框可以向用户公开额外的控件,形式为“选项”。选项可以是布尔型的“是或否”选项,也可以是一组可能的值
// Boolean choice
gtk_file_chooser_add_choice (file_chooser, "validate",
"Enable validation on load",
NULL, NULL);
// Multiple choices
gtk_file_chooser_add_choice (file_chooser, "action",
"Action when loading",
(const char *[]) {
"live",
"laugh",
"love",
NULL,
},
(const char *[]) {
"Live",
"Laugh",
"Love",
NULL,
});
# Boolean choice
file_chooser.add_choice("validate", "Enable validation on load", None, None)
# Multiple choices
file_chooser.add_choice("action", "Action when loading",
["live", "laugh", "love"],
["Live", "Laugh", "Love"])
// Boolean choice
filechooser.add_choice ("validate", "Enable validation on load", null, null);
// Multiple choice
filechooser.add_choice ("action",
"Action when loading",
{ "live", "laugh", "love" },
{ "Live", "Laugh", "Love" });
// Boolean choice
file_chooser.add_choice("validate", "Enable validation on load", null, null);
// Multiple choice
file_chooser.add_choice(
"action",
"Action when loading",
["live", "laugh", "love"],
["Live", "Laugh", "Love"],
);
然后,您可以使用“response”信号处理程序中的 get_choice() 方法来检索给定选项的值。
过滤器¶
您可以使用过滤器来控制对话框应向用户显示哪些类型的文件。文件过滤器可以使用
MIME 类型
匹配文件名的模式
匹配文件扩展名的后缀
组件的常用方法¶
在保存文件时,您可以使用
set_current_name()方法向用户建议一个文件名。为了选择多个文件进行打开,您可以使用
set_select_multiple()方法,然后调用get_files()而不是get_file()来检索所有选定文件列表作为列表模型。
API 参考¶
在示例中,我们使用了以下类