📄 ch-gettingstarted.html
字号:
<HTML><HEAD><TITLE>Getting Started</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.49"><LINKREL="HOME"TITLE="GTK+ 1.2 Tutorial"HREF="gtk-tut.html"><LINKREL="PREVIOUS"TITLE="Introduction"HREF="ch-introduction.html"><LINKREL="NEXT"TITLE="Compiling Hello World"HREF="sec-compiling.html"></HEAD></head><body bgcolor="#FFFFFF" marginheight=0 marginwidth=0 width="100%"><table cellspacing=6 border=0 cellpadding=0 width="100%"> <tr> <td bgcolor="#FFFFFF" valign=top nowrap> <centeR><A HREF="/"><img src="/images/gtk-logo-rgb.gif" width=107 height=140 border=0></A> <font face="helvetica,lucidia" color="#000000"><BR><BR><b>GTK+<BR>The GIMP Toolkit</b></center><BR><table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>General</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/">Introduction</A><BR><A HREF="/screenshots/">Screenshots</A><br><A HREF="/download/">Download</A><br><A HREF="/mailinglists.html">Mailing Lists</A><BR><A HREF="/bindings.html">Language Bindings</A><BR><A HREF="http://gtk.themes.org/">Themes</A><BR><A HREF="/bugs.html">Bug Tracker</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Documentation</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="/faq/">FAQ</A><br><A HREF="/tutorial/">Tutorial</A><BR><A HREF="/api/">API Reference</A><br><A HREF="/books.html">Published Books</A><BR></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B>Projects</B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.pango.org/">Pango</A><BR><A HREF="http://sources.redhat.com/inti/">Inti</A><BR><A HREF="http://www.gnome.org/">GNOME</A><BR><A HREF="http://user.sgic.fi/~tml/gimp/win32/">GTK+ for Win32</A><br><A HREF="http://people.redhat.com/sopwith/gtkfb/">GtkFB (Framebuffer)</A><br><A HREF="http://www.directfb.org/gtk.xml">GTK+ on DirectFB</A><BR><A HREF="/beos/">GTK+ for BeOS</A></td></tr></table></td></tr></table><BR> <table width=100% cellspacing=0 cellpadding=2 border=0><tr><td bgcolor="#000000"><table width=100% cellspacing=0 cellpadding=4 border=0><tr><td bgcolor="#AAAAEE" nowrap><B><B>Applications</B></B></td></tr><tr><td bgcolor="#FFFFFF" nowrap><A HREF="http://www.gimp.org/">GIMP</A><BR><A HREF="http://www.abiword.org/">Abiword</A><BR><A HREF="http://www.lysator.liu.se/~alla/dia/dia.html">Dia</A><BR><A HREF="http://glade.pn.org/">Glade</A><BR><A HREF="http://www.gnucash.org/">GnuCash</A><BR><A HREF="http://www.gnome.org/projects/gnumeric/">Gnumeric</A><BR><BR><A HREF="http://www.gnome.org/applist/">GNOME Software Map</A><br></td></tr></table></td></tr></table><BR> </td> <td bgcolor="#ffffff" valign=top width="99%"><font face="lucida,helvetica"><BODYCLASS="CHAPTER"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#0000FF"VLINK="#840084"ALINK="#0000FF"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">GTK+ 1.2 Tutorial</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="ch-introduction.html"><<< Previous</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sec-compiling.html">Next >>></A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="CHAPTER"><H1><ANAME="CH-GETTINGSTARTED">Chapter 2. Getting Started</A></H1><DIVCLASS="TOC"><DL><DT><B>Table of Contents</B></DT><DT>2.1. <AHREF="ch-gettingstarted.html#SEC-HELLOWORLD">Hello World in GTK</A></DT><DT>2.2. <AHREF="sec-compiling.html">Compiling Hello World</A></DT><DT>2.3. <AHREF="sec-theoryofsignalsandcallbacks.html">Theory of Signals and Callbacks</A></DT><DT>2.4. <AHREF="sec-events.html">Events</A></DT><DT>2.5. <AHREF="sec-steppingthroughhelloworld.html">Stepping Through Hello World</A></DT></DL></DIV><P>The first thing to do, of course, is download the GTK source andinstall it. You can always get the latest version from ftp.gtk.org in/pub/gtk. You can also view other sources of GTK information on<AHREF="http://www.gtk.org/"TARGET="_top">http://www.gtk.org/</A>. GTKuses GNU autoconf for configuration. Once untar'd, type ./configure--help to see a list of options.</P><P>The GTK source distribution also contains the complete source to allof the examples used in this tutorial, along with Makefiles to aidcompilation.</P><P>To begin our introduction to GTK, we'll start with the simplestprogram possible. This program will create a 200x200 pixel window andhas no way of exiting except to be killed by using the shell.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start base base.c */#include <gtk/gtk.h>int main( int argc, char *argv[] ){ GtkWidget *window; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_show (window); gtk_main (); return(0);}/* example-end */</PRE></TD></TR></TABLE><P>You can compile the above program with gcc using:</P><P><PCLASS="LITERALLAYOUT"><TTCLASS="LITERAL">gcc base.c -o base `gtk-config --cflags --libs`</TT></P></P><P>The meaning of the unusual compilation options is explained below in<AHREF="sec-compiling.html">Compiling Hello World</A>.</P><P>All programs will of course include gtk/gtk.h which declares thevariables, functions, structures, etc. that will be used in your GTKapplication.</P><P>The next line:</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">gtk_init (&argc, &argv);</PRE></TD></TR></TABLE><P>calls the function gtk_init(gint *argc, gchar ***argv) which will becalled in all GTK applications. This sets up a few things for us suchas the default visual and color map and then proceeds to callgdk_init(gint *argc, gchar ***argv). This function initializes thelibrary for use, sets up default signal handlers, and checks thearguments passed to your application on the command line, looking forone of the following:</P><P></P><ULCOMPACT="COMPACT"><LI><SPAN> <TTCLASS="LITERAL">--gtk-module</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--g-fatal-warnings</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--gtk-debug</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--gtk-no-debug</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--gdk-debug</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--gdk-no-debug</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--display</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--sync</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--no-xshm</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--name</TT></SPAN></LI><LI><SPAN> <TTCLASS="LITERAL">--class</TT></SPAN></LI></UL><P>It removes these from the argument list, leaving anything it does notrecognize for your application to parse or ignore. This creates a setof standard arguments accepted by all GTK applications.</P><P>The next two lines of code create and display a window.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING"> window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_widget_show (window);</PRE></TD></TR></TABLE><P>The <TTCLASS="LITERAL">GTK_WINDOW_TOPLEVEL</TT> argument specifies that we want thewindow to undergo window manager decoration and placement. Rather thancreate a window of 0x0 size, a window without children is set to200x200 by default so you can still manipulate it.</P><P>The gtk_widget_show() function lets GTK know that we are done settingthe attributes of this widget, and that it can display it.</P><P>The last line enters the GTK main processing loop.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="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 tooccur. In our simple example, however, events are ignored.</P><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="SEC-HELLOWORLD">2.1. Hello World in GTK</A></H1><P>Now for a program with a widget (a button). It's the classichello world a la GTK.</P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">/* example-start helloworld helloworld.c */#include <gtk/gtk.h>/* This is a callback function. The data arguments are ignored * in this example. More on callbacks below. */void hello( GtkWidget *widget, gpointer data ){ g_print ("Hello World\n");}gint 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 */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(&argc, &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. */ gtk_signal_connect (GTK_OBJECT (window), "delete_event", GTK_SIGNAL_FUNC (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. */ gtk_signal_connect (GTK_OBJECT (window), "destroy", GTK_SIGNAL_FUNC (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. */ gtk_signal_connect (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (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. */ gtk_signal_connect_object (GTK_OBJECT (button), "clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy), GTK_OBJECT (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);}/* example-end */</PRE></TD></TR></TABLE></DIV></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="ch-introduction.html"><<< Previous</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="gtk-tut.html">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sec-compiling.html">Next >>></A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">Introduction</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"> </TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Compiling Hello World</TD></TR></TABLE></DIV> </td> </tr></table> </td> </tr></table></body></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -