头像

张小飞

我有一壶酒,足以慰风尘

《GTK 实现文件选择对话框》

 1月前  •   GTK  •   , , ,  •   50  •   0

WPS在deepin上文件选择对话框有些问题,然后去跟Qt源码中,发现Qt的文件选择对话框是使用GTK实现的,然后才会有了这个小demo来确认是deepin操作系统的问题。恩,现在deepin那边已经改好了这个bug。不过还是记录下这个demo。

 

编译参数

shellgcc -o file_select gtk_choose.c `pkg-config --cflags --libs gtk+-2.0`

 

代码如下,保存成文件gtk_choose.c

 

c#include <gtk/gtk.h>

static void _gtk_file_chooser_filter_notify(GtkFileChooser *chooser,
                                            GParamSpec *pspec)
{
    GtkFileFilter *gtkFilter = gtk_file_chooser_get_filter(chooser);
    
    const gchar* name = gtk_file_filter_get_name(gtkFilter);

    gtk_file_chooser_set_current_name(chooser, name);
}

void select_file(GtkWidget *widget,gpointer data)
{
    /* 这个函数在所有的 GTK 程序都要调用。参数由命令行中解析出来并且送到该程序中*/

    GtkWidget *gtkWidget;
    gtkWidget = gtk_file_chooser_dialog_new("", 0,
                                            GTK_FILE_CHOOSER_ACTION_SAVE,
                                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                            GTK_STOCK_OK, GTK_RESPONSE_OK, NULL);
 
    GtkDialog* gtkDailog = (GtkDialog *)gtkWidget;
 
    gtk_window_set_title(GTK_WINDOW(gtkDailog), "gtkwindow's title");
    gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(gtkDailog), 1);
 
    //
    GtkWidget *acceptButton = gtk_dialog_get_widget_for_response(gtkDailog, GTK_RESPONSE_OK);
    GtkWidget *rejectButton = gtk_dialog_get_widget_for_response(gtkDailog, GTK_RESPONSE_CANCEL);
 
    // 文件过滤
    GtkFileFilter *gtkFilter = gtk_file_filter_new();
    gtk_file_filter_set_name(gtkFilter, "(*.wps)");
    gtk_file_filter_add_pattern(gtkFilter, "*.wps");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(gtkDailog), gtkFilter);
 
    gtkFilter = gtk_file_filter_new();
    gtk_file_filter_set_name(gtkFilter, "(*.wpp)");
    gtk_file_filter_add_pattern(gtkFilter, "*.wpp");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(gtkDailog), gtkFilter);

    gtkFilter = gtk_file_filter_new();
    gtk_file_filter_set_name(gtkFilter, "(*.et)");
    gtk_file_filter_add_pattern(gtkFilter, "*.et");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(gtkDailog), gtkFilter);

        gtkFilter = gtk_file_filter_new();
    gtk_file_filter_set_name(gtkFilter, "(*.doc)");
    gtk_file_filter_add_pattern(gtkFilter, "*.doc");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(gtkDailog), gtkFilter);
 
    //
    gboolean confirmOverwrite = 1;
    gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(gtkDailog), confirmOverwrite);
 
    g_signal_connect(gtkDailog, "notify::filter", G_CALLBACK(_gtk_file_chooser_filter_notify), NULL);
 
    gtk_button_set_label(GTK_BUTTON(acceptButton), GTK_STOCK_SAVE);
    gtk_button_set_label(GTK_BUTTON(rejectButton), GTK_STOCK_CANCEL);
    //gtk_file_selection_set_filename(GTK_FILE_SELECTION(gtkWidget), "1111.png");
 
    gtk_dialog_run(gtkDailog);
}
int main(int argc,char **argv)
{
    GtkWidget *win;
    GtkWidget *button;
    /* 这个函数在所有的 GTK 程序都要调用。参数由命令行中解析出来并且送到该程序中*/
    gtk_init(&argc,&argv);

    win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_widget_set_size_request(win,500,400);

    gtk_window_set_title(GTK_WINDOW(win),"FileDialog");
    gtk_window_set_position(GTK_WINDOW(win),GTK_WIN_POS_CENTER);
    g_signal_connect(G_OBJECT(win),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    button=gtk_button_new_with_label("Click Me Click Me Click Me Click Me");
    gtk_container_add(GTK_CONTAINER(win),button);
    g_signal_connect(G_OBJECT(button),"clicked",G_CALLBACK(select_file),NULL);
    gtk_widget_show_all(win);
    gtk_main();
    return 0;
}

 

 

 

上一篇:
下一篇:

 评论


 已有0条评论

    还没有任何评论,你来说两句吧!