Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 9f53138a531066fad3ff013246a3463c > files > 248

gtk2-devel-docs-2.24.4-1.fc15.i686.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>The Button Widget</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="GTK+ 2.0 Tutorial"
HREF="book1.html"><LINK
REL="PREVIOUS"
TITLE="Widgets Without Windows"
HREF="x481.html"><LINK
REL="NEXT"
TITLE="Toggle Buttons"
HREF="x518.html"></HEAD
><BODY
CLASS="CHAPTER"
BGCOLOR="#FFFFFF"
TEXT="#000000"
LINK="#0000FF"
VLINK="#840084"
ALINK="#0000FF"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="3"
ALIGN="center"
>GTK+ 2.0 Tutorial</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="x481.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="x518.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="CH-BUTTONWIDGET"
></A
>The Button Widget</H1
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="SEC-NORMALBUTTONS"
>Normal Buttons</A
></H1
><P
>We've almost seen all there is to see of the button widget. It's
pretty simple. There is however more than one way to create a button. You can
use the gtk_button_new_with_label() or gtk_button_new_with_mnemonic() to create 
a button with a label, use gtk_button_new_from_stock() to create a button
containing the image and text from a stock item or use gtk_button_new() to
create a blank button. It's then up to you to pack a label or pixmap into 
this new button. To do this, create a new box, and then pack your objects into 
this box using the usual gtk_box_pack_start(), and then use gtk_container_add() 
to pack the box into the button.</P
><P
>Here's an example of using gtk_button_new() to create a button with a
image and a label in it. I've broken up the code to create a box from the rest 
so you can use it in your programs. There are further examples of using images 
later in the tutorial.</P
><P
><SPAN
CLASS="INLINEMEDIAOBJECT"
><IMG
SRC="images/buttons.png"></SPAN
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#13;#include &#60;stdlib.h&#62;
#include &#60;gtk/gtk.h&#62;

/* Create a new hbox with an image and a label packed into it
 * and return the box. */

static GtkWidget *xpm_label_box( gchar     *xpm_filename,
                                 gchar     *label_text )
{
    GtkWidget *box;
    GtkWidget *label;
    GtkWidget *image;

    /* Create box for image and label */
    box = gtk_hbox_new (FALSE, 0);
    gtk_container_set_border_width (GTK_CONTAINER (box), 2);

    /* Now on to the image stuff */
    image = gtk_image_new_from_file (xpm_filename);

    /* Create a label for the button */
    label = gtk_label_new (label_text);

    /* Pack the image and label into the box */
    gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 3);
    gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 3);

    gtk_widget_show (image);
    gtk_widget_show (label);

    return box;
}

/* Our usual callback function */
static void callback( GtkWidget *widget,
                      gpointer   data )
{
    g_print ("Hello again - %s was pressed\n", (char *) data);
}

int main( int   argc,
          char *argv[] )
{
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GtkWidget *button;
    GtkWidget *box;

    gtk_init (&#38;argc, &#38;argv);

    /* Create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");

    /* It's a good idea to do this for all windows. */
    g_signal_connect (window, "destroy",
	              G_CALLBACK (gtk_main_quit), NULL);

    g_signal_connect (window, "delete-event",
	 	      G_CALLBACK (gtk_main_quit), NULL);

    /* Sets the border width of the window. */
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);

    /* Create a new button */
    button = gtk_button_new ();

    /* Connect the "clicked" signal of the button to our callback */
    g_signal_connect (button, "clicked",
		      G_CALLBACK (callback), (gpointer) "cool button");

    /* This calls our box creating function */
    box = xpm_label_box ("info.xpm", "cool button");

    /* Pack and show all our widgets */
    gtk_widget_show (box);

    gtk_container_add (GTK_CONTAINER (button), box);

    gtk_widget_show (button);

    gtk_container_add (GTK_CONTAINER (window), button);

    gtk_widget_show (window);

    /* Rest in gtk_main and wait for the fun to begin! */
    gtk_main ();

    return 0;
}</PRE
></TD
></TR
></TABLE
><P
>The xpm_label_box() function could be used to pack images and labels into
any widget that can be a container.</P
><P
>The Button widget has the following signals:</P
><P
></P
><UL
><LI
><P
><TT
CLASS="LITERAL"
>pressed</TT
> - emitted when pointer button is pressed within
Button widget</P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>released</TT
> - emitted when pointer button is released within
Button widget</P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>clicked</TT
> - emitted when pointer button is pressed and then
released within Button widget</P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>enter</TT
> - emitted when pointer enters Button widget</P
></LI
><LI
><P
><TT
CLASS="LITERAL"
>leave</TT
> - emitted when pointer leaves Button widget</P
></LI
></UL
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="x481.html"
ACCESSKEY="P"
>&#60;&#60;&#60; Previous</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="book1.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x518.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Widgets Without Windows</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Toggle Buttons</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>