如何在gtk_recent_chooser_dialog_new小部件中获取文件名

时间:2017-02-27 10:17:47

标签: c# gtk3

如何在gtk_recent_chooser_dialog_new小部件中获取所选文件的名称。下面是一个演示文件来解释.....

#include <gtk/gtk.h>
//recentchooserdialog.c
/*
gcc -std=c11 -Wall -fmax-errors=10 -Wextra recentchooserdialog.c -o recentchooserdialog `pkg-config --cflags --libs gtk+-3.0 `
*/

int main(int argc, char *argv[])
{    
    gtk_init(&argc, &argv);
    gboolean multiple = FALSE;
    //GList * files;
    GtkRecentInfo *info;
    gchar *chemin = NULL;

    GtkWidget *recentchooserdialog = gtk_recent_chooser_dialog_new("RecentChooserDialog", NULL, 
                            ("_Cancel"), GTK_RESPONSE_CANCEL, 
                            ("_Open"), GTK_RESPONSE_OK, NULL);

    gtk_recent_chooser_set_limit(GTK_RECENT_CHOOSER(recentchooserdialog),-1);
    gtk_recent_chooser_set_show_tips(GTK_RECENT_CHOOSER(recentchooserdialog),TRUE);
    gtk_recent_chooser_set_select_multiple(GTK_RECENT_CHOOSER(recentchooserdialog), multiple);

    if (gtk_dialog_run(GTK_DIALOG(recentchooserdialog)) == GTK_RESPONSE_OK)
    {
        info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (recentchooserdialog));
        if (multiple==TRUE)
        {
            /** how to get file names selected here **/

        }
        else
        {
            /** how to get one single filename selected here **/
            // GtkRecentInfo *info;
            info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (recentchooserdialog));
            if (info)
                g_print("structur info exists\n ");
            /* below is not the good casting */
            chemin = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(recentchooserdialog));
            g_print("selected path %s\n",chemin);
        }
        gtk_recent_info_unref (info);
    }
    gtk_widget_destroy (recentchooserdialog);
    return 0;
}
经过多次谷歌搜索后,我没有发现任何事情 在gtk_file_chooser_dialog_new上,我找到了

gtk_file_chooser_get_filename或gtk_file_chooser_get_filenames操作 低于其他背景对我来说没问题

void cb_open (GtkWidget *widget, gpointer user_data)
{
  GtkWidget *dialog = NULL;
  GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;

  dialog = gtk_file_chooser_dialog_new ("Ouvrir un fichier", NULL,
                                          action,
                                          ("_Cancel"),
                                          GTK_RESPONSE_CANCEL,
                                          ("_Open"),
                                          GTK_RESPONSE_ACCEPT,
                                          NULL);

  if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
  {
    gchar *file_name = NULL;

    file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
    g_print("fichier ouvert : %s \n",file_name);

    g_free (file_name), file_name = NULL;
  }
  gtk_widget_destroy (dialog);
}

你能帮我完成单选和多选案例吗? 谢谢提前

2 个答案:

答案 0 :(得分:0)

GtkRecentChooserDialog 实施GtkFileChooser界面。

最近的文件选择器对话框确实实现了GtkRecentChooser界面,这意味着您可以使用gtk_recent_chooser_get_current_item()检索当前选定的最近文件,然后使用GtkRecentInfo API检索文件的the URIthe icon之类的内容。

答案 1 :(得分:0)

我找到了单选的解决方案的一部分

#include <gtk/gtk.h>
//recentchooserdialog.c
/*
gcc -std=c11 -Wall -fmax-errors=10 -Wextra recentchooserdialog.c -o recentchooserdialog `pkg-config --cflags --libs gtk+-3.0 `
*/

int main(int argc, char *argv[])
{    
    gtk_init(&argc, &argv);
    gboolean multiple = FALSE;
    //GList * files;
    GtkRecentInfo *info;
    const gchar *text_info = NULL;

    GtkWidget *recentchooserdialog = gtk_recent_chooser_dialog_new("RecentChooserDialog", NULL, 
                            ("_Cancel"), GTK_RESPONSE_CANCEL, 
                            ("_Open"), GTK_RESPONSE_OK, NULL);

    gtk_recent_chooser_set_limit(GTK_RECENT_CHOOSER(recentchooserdialog),-1);
    gtk_recent_chooser_set_show_tips(GTK_RECENT_CHOOSER(recentchooserdialog),TRUE);
    gtk_recent_chooser_set_select_multiple(GTK_RECENT_CHOOSER(recentchooserdialog), multiple);

    if (gtk_dialog_run(GTK_DIALOG(recentchooserdialog)) == GTK_RESPONSE_OK)
    {
        info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (recentchooserdialog));
        if (multiple==TRUE)
        {
            /** how to get file names selected here **/

        }
        else
        {
            /** how to get one single filename selected here **/
            info = gtk_recent_chooser_get_current_item (GTK_RECENT_CHOOSER (recentchooserdialog));
            if (info)
            {
                text_info = gtk_recent_info_get_uri (info);
                g_print("une structure info existe %s\n", text_info);
            }
        }
        gtk_recent_info_unref (info);
    }
    gtk_widget_destroy (recentchooserdialog);
    return 0;
}

小心unicode字符串 我不测试必须添加的好动作

text_info = g_locale_to_utf8 (text_info, -1, NULL, NULL, NULL);

text_info = g_locale_from_utf8 (text_info, -1, NULL, NULL, NULL);

下一步是测试多重选择的内容