GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 01, 2014 3:25 am

All times are UTC




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: 2 questions about gtk_button_set_image
PostPosted: Mon Jan 14, 2013 6:43 pm 
Offline

Joined: Mon Jan 14, 2013 5:11 pm
Posts: 1
Hello! Can I use one image to many buttons? Is there any way to do that? I know that this code won't work :
Code:
GtkWidget *button, *button2;
GtkWidget *image;
image = gtk_image_new_from_file("picture.png");
button = gtk_button_new_with_label ("");
button2 = gtk_button_new_with_label ("");
gtk_button_set_image(GTK_BUTTON(button), image);
gtk_button_set_image(GTK_BUTTON(button2), image);

I know why this isn't working, but I am wondering if I can use one widget instead of table of widgets to add one picture to many buttons?
And question number two:) Why this code won't work? :
Code:
GtkWidget *button;
GtkWidget *image;
button = gtk_button_new_with_label ("");
image = gtk_image_new_from_file("picture.png");
gtk_button_set_image(GTK_BUTTON(button), image);
gtk_button_set_image(GTK_BUTTON(button), image);

I would be very grateful for any response:)
Oh, I forgot. I am using win 7 (64 bits), code:blocks 10.05, and gtk+ 2.24


Top
 Profile  
 
 Post subject: Re: 2 questions about gtk_button_set_image
PostPosted: Tue Jan 15, 2013 8:21 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
The reason it doesn't work is because of ownership being transferred to the button. From the code for GtkButton(http://git.gnome.org/browse/gtk+/tree/gtk/gtkbutton.c):
Code:
if (priv->image)
    {
      parent = gtk_widget_get_parent (priv->image);
      if (parent)
        gtk_container_remove (GTK_CONTAINER (parent), priv->image);
    }

i.e. second assignment of the image will de-parent its first assignment.
I think (untested) you can get your code to work by calling g_object_ref(image) before your first assignment. This will give two references to your image. The other alternative is to call gtk_image_new_from_file again, but try out the former method first as it would be preferable.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group