GTK + 3本机文件选择器-允许的文件类型和默认文件名

时间:2019-03-01 19:48:29

标签: c++ gtk gtk3 filechooser

我正在使用GTK +创建C ++程序。选择文件时,我使用本机文件选择器,因为gtk文件选择器存在内存泄漏。

这是我的问题:如何在GTK +中为本机文件选择器设置默认文件名(保存时),以及如何为其设置文件过滤器-以便仅打开或保存特定的文件扩展名?

我的程序在Win32上可以运行。 示例代码:

static void cb_SaveFileAs(GtkWidget *caller)
{
    GtkWindow *parent_window = GTK_WINDOW(caller);
    GtkFileChooserNative *native;
    GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
    gint res;

    native = gtk_file_chooser_native_new("Save File",
                                         parent_window,
                                         action,
                                         "_Save",
                                         "_Cancel");

    res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native));
    if (res == GTK_RESPONSE_ACCEPT)
    {
        char *filename;
        GtkFileChooser *chooser = GTK_FILE_CHOOSER(native);
        filename = gtk_file_chooser_get_filename(chooser);

        // save the file 
        save_to_file(filename);

        g_free(filename);
    }

    g_object_unref(native);
}

2 个答案:

答案 0 :(得分:2)

这里是将GtkFileFilter模式设置为*.cpp以仅显示C ++文件的示例。还将默认文档名称设置为test.cpp

使用以下命令进行编译:g ++ filechooser.cpp`pkg-config --cflags gtk + -3.0``pkg-config --libs gtk + -3.0`

#include <gtk/gtk.h>

static void dialog_result(GtkWidget *dialog, gint resp, gpointer data)
{
    if (resp == GTK_RESPONSE_OK) {
        // do nothing
    } else {
        gtk_widget_destroy(dialog);
    }
}

static void open_dialog(GtkWidget *button, gpointer window)
{
    GtkWidget *dialog;
    GtkFileFilter *filter;

    dialog = gtk_file_chooser_dialog_new("Choose a file:", GTK_WINDOW(window), 
                        GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_SAVE,
                         GTK_RESPONSE_OK, GTK_STOCK_CANCEL,
                         GTK_RESPONSE_CANCEL, NULL);

    filter = gtk_file_filter_new();
    gtk_file_filter_add_pattern(filter, "*.cpp");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
    gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), "test.cpp");
    gtk_widget_show_all(dialog);
    gint resp = gtk_dialog_run(GTK_DIALOG(dialog));
    if (resp == GTK_RESPONSE_OK)
        g_print("%s\n", gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)));
    else
        g_print("You pressed the cancel\n");
    gtk_widget_destroy(dialog);
}

int main()
{
    gtk_init(NULL, NULL);
    GtkWidget *window, *button;
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "delete_event", G_CALLBACK(gtk_main_quit), NULL);
    button = gtk_button_new_with_label("Save");
    g_signal_connect(button, "clicked", G_CALLBACK(open_dialog), window);
    gtk_container_set_border_width(GTK_CONTAINER(window), 100);
    gtk_container_add(GTK_CONTAINER(window), button);
    gtk_widget_show_all(window);
    gtk_main();
}

答案 1 :(得分:0)

以下是来自jackw11111的解决方案的GTK本机文件选择器的完整代码:

static void cb_SaveFileAs(GtkWidget *caller) // callback function
{
    GtkWindow *parent_window = GTK_WINDOW(caller); // assuming that the caller is GtkWindow
    GtkFileChooserNative *native;
    GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_SAVE;
    gint res;

    native = gtk_file_chooser_native_new("Save File",
                                         parent_window,
                                         action,
                                         "_Save",
                                         "_Cancel");

    // Filters
    filter = gtk_file_filter_new(); // filter 1
    gtk_file_filter_set_name(filter, "Project file");
    gtk_file_filter_add_pattern(filter, ".proj");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(native), filter);
    filter = gtk_file_filter_new(); // filter 2
    gtk_file_filter_set_name(filter, "All files");
    gtk_file_filter_add_pattern(filter, "*");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(native), filter);

    // default file name
    gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(native), "my_file_name");

    res = gtk_native_dialog_run(GTK_NATIVE_DIALOG(native));
    if (res == GTK_RESPONSE_ACCEPT)
    {
        char *filename;
        GtkFileChooser *chooser = GTK_FILE_CHOOSER(native);
        filename = gtk_file_chooser_get_filename(chooser);

        // save the file
        save_to_file(filename);

        g_free(filename);
    }

    g_object_unref(native);
}