Re: swap images on click event.



Craig Harding wrote:
Hi, I'm trying to change an image with another when I click on my applet button, but it doesn't work, can anyone help me out?

CODE:
play_button = gtk_image_new_from_file ("/usr/local/pixmaps/play.png");
pause_button = gtk_image_new_from_file("/usr/local/pixmaps/pause.png");

....

gtk_widget_ref(GTK_WIDGET(pause_button));
gtk_container_remove (GTK_CONTAINER (play_event_box), pause_button);

Here you add your reference to pause_button and remove it from
play_event_box, all is good, you should still have one ref_count
on pause_button.

gtk_widget_unref(play_button);
Here you unref play_button, which I assume is not in any container
at the moment, so its finalized and destroyed.

gtk_widget_reparent(play_button, play_event_box);
gtk_container_add (GTK_CONTAINER (play_event_box), play_button);
gtk_widget_show_all(play_event_box);

Here I guess you get errors because your useing a pointer
to freed memory of a finalized play_button.

There are probably many ways to do this, personally I'd probably
 o Create one GtkImage
 o Load the pixbufs into serverside pixmaps (or keep the pixbufs
   around if they have alpha, performance isn't really an issue)
 o Use g_object_set (G_OBJECT (image), "pixbuf", play_pixbuf, NULL);

If you go the reparenting route; I'd say you need code like this:

===============================================
 Assuming pause_button is indeed the child of
 play_event_box and that play_button is not
 floating, not contained and has a user ref_count
 of 1.
===============================================
gtk_widget_ref(GTK_WIDGET(pause_button));
gtk_container_remove (GTK_CONTAINER (play_event_box), pause_button);

gtk_container_add (GTK_CONTAINER (play_event_box), play_button);
gtk_widget_unref(play_button);

gtk_widget_show_all(play_event_box);
===============================================

It would probably make even more sence to just add
a reference count to play_button & pause_button
for the app's lifetime and just reparent them at will.

Cheers,
                             -Tristan



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]