Re: Newbie question: How to set a button's image?



On Sat, Jan 22, 2005 at 12:14:59PM -1000, Neo Anderson wrote:
I am just beginning on GTK+. I know GtkButton has a property "image", but I 
don't know if it is the right thing to do with; I don't know to set a 
*property* of a widget.

With g_object_set().  For example

    button = gtk_button_new_with_mnemonic("_Foo");
    image = gtk_image_new_from_stock(GTK_STOCK_QUIT, GTK_ICON_SIZE_BUTTON);
    g_object_set(button, "image", image, NULL);

(there are other ways to create a GtkImage, see API
documentation).

GtkButton API documentation unfortunately doesn't mention
that "image" property doesn't exist in Gtk+ < 2.6, so you
may want to create stock-like buttons manually if you care
about compatibility, like

    button = gtk_button_new();

    alignment = gtk_alignment_new(0.5, 0.5, 0, 0);
    gtk_container_add(GTK_CONTAINER(button), alignment);

    hbox = gtk_hbox_new(FALSE, 2);
    gtk_container_add(GTK_CONTAINER(alignment), hbox);

    image = gtk_image_new_from_stock(GTK_STOCK_QUIT, GTK_ICON_SIZE_BUTTON);
    gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);

    label = gtk_label_new_with_mnemonic("_Foo");
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);

OTOH if you don't mind depending on 2.6+, you can simply
use

    gtk_button_set_image(GTK_BUTTON(button), image);

instead of g_object_set().  (This function does not appear in
the documentation, but it exists and seems public.)

But if you want a button that contains *only* image, simply
use

    button = gtk_button_new();
    gtk_container_add(GTK_CONTAINER(button), image);

Yeti


--
Dynamic IP address is not a crime.



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