GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how can i get a filename by menu item?

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
gtkguan
Familiar Face


Joined: 07 Jun 2008
Posts: 23
Location: Guangzhou, China

PostPosted: Mon Sep 01, 2008 8:29 pm    Post subject: how can i get a filename by menu item? Reply with quote

there is an "open " menu item,
Code: (C)
1
2
3
4
5
6
menuitem = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN, accel_group);
gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), menuitem);
g_signal_connect(G_OBJECT(menuitem), "activate",
                     G_CALLBACK(file_open),
                     (gchar *)pfilename);

when I press "Open", a file chooser dialog opens. And when I chooser a file, I want to get this name of file. How does it returned?
Thank you?
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 643
Location: Falun, WI USA

PostPosted: Mon Sep 01, 2008 10:30 pm    Post subject: Reply with quote

in your file_open function, create a GtkFileChooserDialog and run it,
then use gtk_file_chooser_get_filename to get the file name:
http://library.gnome.org/devel/gtk/stable/GtkFileChooserDialog.html#gtk-file-chooser-dialog-new
http://library.gnome.org/devel/gtk/stable/GtkDialog.html#gtk-dialog-run
http://library.gnome.org/devel/gtk/stable/GtkFileChooser.html#gtk-file-chooser-get-filename
Here's an example:
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void file_open(GtkMenuItem *item, gpointer user_data)
{
  GtkWidget *dlg;
  GtkResponseType response;
  gchar *filename;

  dlg = gtk_file_chooser_dialog_new("Open", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, "_Open", GTK_RESPONSE_OK, "_Cancel", GTK_RESPONSE_CANCEL, NULL);
  response = gtk_dialog_run(GTK_DIALOG(dlg));
  switch(response)
  {
    case GTK_RESPONSE_OK:
     
filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dlg));
      break;
    default:
      break;
  }
  gtk_widget_destroy(dlg);
}
Back to top
gtkguan
Familiar Face


Joined: 07 Jun 2008
Posts: 23
Location: Guangzhou, China

PostPosted: Tue Sep 02, 2008 5:46 am    Post subject: Reply with quote

Thank you, dreblen.
I have used gtk_file_chooser_dialog_new. I want to use file chooser dialog to return the file name. And so I can create a widget displaying a image. The following is code slice. I think the trouble is the argument "filename" in function gdk_pixbuf_new_from_file
Code: (C)
1
2
3
4
pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
    image = gtk_image_new_from_pixbuf(gdk_pixbuf_scale_simple(pixbuf, (int)(gdk_pixbuf_get_width(pixbuf)*0.25),
                                (int)(gdk_pixbuf_get_height(pixbuf)*0.25), GDK_INTERP_BILINEAR));
    gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0);

I have resolve this problem by adding the upper code silce in callback function file_open, just like this:
Code: (C)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
static void file_open (GtkMenuItem *menuitem, gpointer vbox)
{
    GtkWidget *file_open_dialog;
    GSList *filenames;
    GtkFileFilter *file_filter_bmp, *file_filter_all;
    gint result;
    gchar *filename;

    GtkWidget *table;
    GtkWidget *image;
    GdkPixbuf *pixbuf;

    file_open_dialog = gtk_file_chooser_dialog_new(_("打开文件"), NULL,
                                         GTK_FILE_CHOOSER_ACTION_OPEN,
                                         GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                                         GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
                                         NULL);

    gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(file_open_dialog), g_get_home_dir ());
   
    file_filter_bmp = gtk_file_filter_new();
    file_filter_all = gtk_file_filter_new();
    gtk_file_filter_set_name(file_filter_bmp, "BMP files (*.bmp)");
    gtk_file_filter_set_name(file_filter_all, "All files (*.*)");
    gtk_file_filter_add_pattern(file_filter_bmp, "*.bmp");
    gtk_file_filter_add_pattern(file_filter_all, "*");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_open_dialog), file_filter_bmp);
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(file_open_dialog), file_filter_all);
    gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(file_open_dialog), file_filter_bmp);
    gtk_file_chooser_set_filter(GTK_FILE_CHOOSER(file_open_dialog), file_filter_all);

    /* allow the user to choose only ONE file at a time. */
   
gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(file_open_dialog), FALSE);
   
    result = gtk_dialog_run(GTK_DIALOG(file_open_dialog));
    if(GTK_RESPONSE_ACCEPT == result)
    {
        filenames = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(file_open_dialog));
        filename = (gchar *)filenames->data;
    }
    gtk_widget_destroy(file_open_dialog);

   
    pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
    image = gtk_image_new_from_pixbuf(gdk_pixbuf_scale_simple(pixbuf, (int)(gdk_pixbuf_get_width(pixbuf)*0.25),
                                (int)(gdk_pixbuf_get_height(pixbuf)*0.25), GDK_INTERP_BILINEAR));
    gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0);
    gtk_widget_show(image);

}

But I do not want let this two kinds of code be in one function. So is there other solutions? Thank you!
Back to top
dreblen
Never Seen the Sunlight


Joined: 14 Jun 2007
Posts: 643
Location: Falun, WI USA

PostPosted: Tue Sep 02, 2008 1:31 pm    Post subject: Reply with quote

I guess I don't understand the problem, is the pixbuf not loading?
what two pieces of code don't you want to mix?

if the pixbuf isn't loading, I'd recommend using the GError argument of gdk_pixbuf_new_from_file:
Code: (C)
1
2
3
4
5
6
7
8
9
GError *err;
err = NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, &err);
if(err != NULL)
{
  g_warning(err->message);
  g_error_free(err);
  return;
}
Back to top
gtkguan
Familiar Face


Joined: 07 Jun 2008
Posts: 23
Location: Guangzhou, China

PostPosted: Wed Sep 03, 2008 3:03 pm    Post subject: Reply with quote

dreblen, thanks for your help. I have solved this problem.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP