Re: Change an image on a button



Thanks for your answer, I've find a solution with

gtk_container_remove() and gtk_container_add()

But I must do a gtk_widget_show_all(pButton)  !!!

Guillaume Charhon

David Nec(as (Yeti) a écrit :
On Wed, Nov 15, 2006 at 10:40:06PM +0100, Guillaume Charhon wrote:
But I have read everything and it doesn't work!

You are right.  The problem is not with reference counting
any more.  Something strange happens to the image widget
that makes it invisible after removal from the button.  This
is specific to gtk_button_set_image() because it works
normally.  I attach two demonstration programs for
reference, one using gtk_button_set_image(), the other one
normal gtk_container_remove() and gtk_container_add(). The
first one does not work (image becomes invisible), the
second one works.  I suggest to file a bugzilla bug and
attach a program demonstrating the problem to it.

Yeti


--
Whatever.

---------------------------------------------------------------------------------------
Orange vous informe que cet e-mail a ete controle par l'anti-virus mail. Aucun virus connu a ce jour par nos services n'a ete detecte.

------------------------------------------------------------------------

#include <gtk/gtk.h>

static void
switch_images(GtkWidget *button)
{
    GtkWidget *image1, *image2;

    image1 = gtk_button_get_image(GTK_BUTTON(button));
    image2 = g_object_get_data(G_OBJECT(button), "other-image");
    g_print("Current image: %s, switching to %s\n",
            (const gchar*)g_object_get_data(G_OBJECT(image1), "id"),
            (const gchar*)g_object_get_data(G_OBJECT(image2), "id"));
    gtk_button_set_image(GTK_BUTTON(button), image2);
    g_object_set_data(G_OBJECT(button), "other-image", image1);
    g_print("New image realized: %d, mapped: %d, visible: %d\n",
            GTK_WIDGET_REALIZED(image2),
            GTK_WIDGET_MAPPED(image2),
            GTK_WIDGET_VISIBLE(image2));
}

int
main(int argc, char *argv[])
{
    GtkWidget *window, *target, *switcher, *vbox, *image1, *image2;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    image1 = gtk_image_new_from_stock(GTK_STOCK_YES, GTK_ICON_SIZE_BUTTON);
    g_object_set_data(G_OBJECT(image1), "id", "Yes");
    g_object_ref(image1);

    image2 = gtk_image_new_from_stock(GTK_STOCK_NO, GTK_ICON_SIZE_BUTTON);
    g_object_set_data(G_OBJECT(image2), "id", "No");
    g_object_ref(image2);

    target = gtk_button_new_with_label("Target");
    gtk_button_set_image(GTK_BUTTON(target), image1);
    g_object_set_data(G_OBJECT(target), "other-image", image2);
    gtk_box_pack_start(GTK_BOX(vbox), target, FALSE, FALSE, 0);

    switcher = gtk_button_new_with_label("Switch");
    gtk_box_pack_start(GTK_BOX(vbox), switcher, FALSE, FALSE, 0);
    g_signal_connect_swapped(switcher, "clicked",
                             G_CALLBACK(switch_images), target);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
------------------------------------------------------------------------

#include <gtk/gtk.h>

static void
switch_images(GtkWidget *button)
{
    GtkWidget *image1, *image2;

    image1 = gtk_bin_get_child(GTK_BIN(button));
    image2 = g_object_get_data(G_OBJECT(button), "other-image");
    g_print("Current image: %s, switching to %s\n",
            (const gchar*)g_object_get_data(G_OBJECT(image1), "id"),
            (const gchar*)g_object_get_data(G_OBJECT(image2), "id"));
    gtk_container_remove(GTK_CONTAINER(button), image1);
    gtk_container_add(GTK_CONTAINER(button), image2);
    g_object_set_data(G_OBJECT(button), "other-image", image1);
    g_print("New image realized: %d, mapped: %d, visible: %d\n",
            GTK_WIDGET_REALIZED(image2),
            GTK_WIDGET_MAPPED(image2),
            GTK_WIDGET_VISIBLE(image2));
}

int
main(int argc, char *argv[])
{
    GtkWidget *window, *target, *switcher, *vbox, *image1, *image2;

    gtk_init(&argc, &argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    image1 = gtk_image_new_from_stock(GTK_STOCK_YES, GTK_ICON_SIZE_BUTTON);
    g_object_set_data(G_OBJECT(image1), "id", "Yes");
    gtk_widget_show(image1);
    g_object_ref(image1);

    image2 = gtk_image_new_from_stock(GTK_STOCK_NO, GTK_ICON_SIZE_BUTTON);
    g_object_set_data(G_OBJECT(image2), "id", "No");
    gtk_widget_show(image2);
    g_object_ref(image2);

    target = gtk_button_new();
    gtk_container_add(GTK_CONTAINER(target), image1);
    g_object_set_data(G_OBJECT(target), "other-image", image2);
    gtk_box_pack_start(GTK_BOX(vbox), target, FALSE, FALSE, 0);

    switcher = gtk_button_new_with_label("Switch");
    gtk_box_pack_start(GTK_BOX(vbox), switcher, FALSE, FALSE, 0);
    g_signal_connect_swapped(switcher, "clicked",
                             G_CALLBACK(switch_images), target);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}
------------------------------------------------------------------------

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




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