Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 23dbe49bc2fbd86096de282a461e6c66 > files > 246

gtk2-devel-docs-2.24.7-3.fc15.i686.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Getting Started</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="Introduction"
HREF="c24.html"><LINK
REL="NEXT"
TITLE="Compiling Hello World"
HREF="x111.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="c24.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="x111.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="CH-GETTINGSTARTED"
></A
>Getting Started</H1
><P
>The first thing to do, of course, is download the GTK source and
install it. You can always get the latest version from <A
HREF="ftp://ftp.gtk.org/pub/gtk"
TARGET="_top"
>ftp.gtk.org</A
>. You can also view 
other sources of GTK information on
<A
HREF="http://www.gtk.org/"
TARGET="_top"
>http://www.gtk.org/</A
>. GTK
uses GNU autoconf for configuration. Once untar'd, type 
<TT
CLASS="LITERAL"
>./configure --help</TT
> to see a list of options.</P
><P
>The GTK source distribution also contains the complete source to all
of the examples used in this tutorial, along with Makefiles to aid
compilation.</P
><P
>To begin our introduction to GTK, we'll start with the simplest
program possible. This program will create a 200x200 pixel window and
has no way of exiting except to be killed by using the shell.</P
><P
><SPAN
CLASS="INLINEMEDIAOBJECT"
><IMG
SRC="images/base.png"></SPAN
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#13;#include &#60;gtk/gtk.h&#62;

int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
    
    gtk_init (&#38;argc, &#38;argv);
    
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show  (window);
    
    gtk_main ();
    
    return 0;
}</PRE
></TD
></TR
></TABLE
><P
>You can compile the above program with gcc using:</P
><P
><P
CLASS="LITERALLAYOUT"
><TT
CLASS="LITERAL"
>gcc base.c -o base `pkg-config --cflags --libs gtk+-2.0`</TT
></P
></P
><P
>The meaning of the unusual compilation options is explained below in
<A
HREF="x111.html"
>Compiling Hello World</A
>.</P
><P
>All programs will of course include <TT
CLASS="FILENAME"
>gtk/gtk.h</TT
> which 
declares the variables, functions, structures, etc. that will be used in your GTK
application.</P
><P
>The next line:</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>gtk_init (&#38;argc, &#38;argv);</PRE
></TD
></TR
></TABLE
><P
>calls the function gtk_init(gint *argc, gchar ***argv) which will be called 
in all GTK applications. This sets up a few things for us such as the default visual 
and color map and then proceeds to call gdk_init(gint *argc, gchar ***argv). 
This function initializes the library for use, sets up default signal handlers, and 
checks the arguments passed to your application on the command line, looking for
one of the following:</P
><P
></P
><UL
COMPACT="COMPACT"
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--gtk-module</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--g-fatal-warnings</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--gtk-debug</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--gtk-no-debug</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--gdk-debug</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--gdk-no-debug</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--display</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--sync</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--name</TT
></SPAN
></LI
><LI
><SPAN
> <TT
CLASS="LITERAL"
>--class</TT
></SPAN
></LI
></UL
><P
>It removes these from the argument list, leaving anything it does not
recognize for your application to parse or ignore. This creates a set
of standard arguments accepted by all GTK applications.</P
><P
>The next two lines of code create and display a window.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_show (window);</PRE
></TD
></TR
></TABLE
><P
>The <TT
CLASS="LITERAL"
>GTK_WINDOW_TOPLEVEL</TT
> argument specifies that we want the
window to undergo window manager decoration and placement. Rather than
create a window of 0x0 size, a window without children is set to
200x200 by default so you can still manipulate it.</P
><P
>The gtk_widget_show() function lets GTK know that we are done setting
the attributes of this widget, and that it can display it.</P
><P
>The last line enters the GTK main processing loop.</P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>  gtk_main ();</PRE
></TD
></TR
></TABLE
><P
>gtk_main() is another call you will see in every GTK application.
When control reaches this point, GTK will sleep waiting for X events
(such as button or key presses), timeouts, or file IO notifications to
occur. In our simple example, however, events are ignored.</P
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="SEC-HELLOWORLD"
>Hello World in GTK</A
></H1
><P
>Now for a program with a widget (a button). It's the classic
hello world a la GTK.</P
><P
><SPAN
CLASS="INLINEMEDIAOBJECT"
><IMG
SRC="images/helloworld.png"></SPAN
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="PROGRAMLISTING"
>&#13;#include &#60;gtk/gtk.h&#62;

/* This is a callback function. The data arguments are ignored
 * in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
                   gpointer   data )
{
    g_print ("Hello World\n");
}

static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
{
    /* If you return FALSE in the "delete-event" signal handler,
     * GTK will emit the "destroy" signal. Returning TRUE means
     * you don't want the window to be destroyed.
     * This is useful for popping up 'are you sure you want to quit?'
     * type dialogs. */

    g_print ("delete event occurred\n");

    /* Change TRUE to FALSE and the main window will be destroyed with
     * a "delete-event". */

    return TRUE;
}

/* Another callback */
static void destroy( GtkWidget *widget,
                     gpointer   data )
{
    gtk_main_quit ();
}

int main( int   argc,
          char *argv[] )
{
    /* GtkWidget is the storage type for widgets */
    GtkWidget *window;
    GtkWidget *button;
    
    /* This is called in all GTK applications. Arguments are parsed
     * from the command line and are returned to the application. */
    gtk_init (&#38;argc, &#38;argv);
    
    /* create a new window */
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
    /* When the window is given the "delete-event" signal (this is given
     * by the window manager, usually by the "close" option, or on the
     * titlebar), we ask it to call the delete_event () function
     * as defined above. The data passed to the callback
     * function is NULL and is ignored in the callback function. */
    g_signal_connect (window, "delete-event",
		      G_CALLBACK (delete_event), NULL);
    
    /* Here we connect the "destroy" event to a signal handler.  
     * This event occurs when we call gtk_widget_destroy() on the window,
     * or if we return FALSE in the "delete-event" callback. */
    g_signal_connect (window, "destroy",
		      G_CALLBACK (destroy), NULL);
    
    /* Sets the border width of the window. */
    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
    
    /* Creates a new button with the label "Hello World". */
    button = gtk_button_new_with_label ("Hello World");
    
    /* When the button receives the "clicked" signal, it will call the
     * function hello() passing it NULL as its argument.  The hello()
     * function is defined above. */
    g_signal_connect (button, "clicked",
		      G_CALLBACK (hello), NULL);
    
    /* This will cause the window to be destroyed by calling
     * gtk_widget_destroy(window) when "clicked".  Again, the destroy
     * signal could come from here, or the window manager. */
    g_signal_connect_swapped (button, "clicked",
			      G_CALLBACK (gtk_widget_destroy),
                              window);
    
    /* This packs the button into the window (a gtk container). */
    gtk_container_add (GTK_CONTAINER (window), button);
    
    /* The final step is to display this newly created widget. */
    gtk_widget_show (button);
    
    /* and the window */
    gtk_widget_show (window);
    
    /* All GTK applications must have a gtk_main(). Control ends here
     * and waits for an event to occur (like a key press or
     * mouse event). */
    gtk_main ();
    
    return 0;
}</PRE
></TD
></TR
></TABLE
></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="c24.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="x111.html"
ACCESSKEY="N"
>Next &#62;&#62;&#62;</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Introduction</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
>&nbsp;</TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Compiling Hello World</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>