Re: swap images on click event.



Actually putting in your 5 lines, replacing mine with the image swap, crashes the applet and gives me the following output on debugging:

(xmms-applet:8282): GLib-GObject-WARNING **: instance of invalid non-instantiatable type `<invalid>'

(xmms-applet:8282): GLib-GObject-CRITICAL **: file gsignal.c: line 2121 (g_signal_emit_valist): assertion `G_TYPE_CHECK_INSTANCE (instance)' failed

(xmms-applet:8282): GLib-GObject-WARNING **: instance with invalid (NULL) class pointer

(xmms-applet:8282): GLib-GObject-CRITICAL **: file gsignal.c: line 1764 (g_signal_handlers_destroy): assertion `G_TYPE_CHECK_INSTANCE (instance)' failed

(gnome_segv:8285): Gtk-WARNING **: Ignoring the separator setting
------------------------------

I'm not really clear on how the ref and unref calls work, I've read the documentation. Is the problem maybe that pause_button might have been a child of play_event_box previous to ref'ing and the removing it?

Scenario: Pause button is the button showing in play_event_box open running the program. I click on the pause button and I want it to swtich to play button (which works if the pause button is being shown first), then I click on the now changed play button (cause I want the pause button to show) but this does not work. This is the problem I'm facing.

Thanks for you help previously too..
Craig.


Tristan Van Berkom wrote:
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]