?? cha-gtk.html
字號:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html> <head> <title> GTK+ Basics </title> <meta name="GENERATOR" content= "Modular DocBook HTML Stylesheet Version 1.45"> <link rel="HOME" title="GTK+ / Gnome Application Development" href="ggad.html"> <link rel="UP" title="Overview" href="overview.html"> <link rel="PREVIOUS" title="Other Features" href="z35.html"> <link rel="NEXT" title="Containers And Widget Layout" href= "sec-containers.html"> </head> <body bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink= "#840084" alink="#0000FF"> <div class="NAVHEADER"> <table width="100%" border="0" bgcolor="#ffffff" cellpadding= "1" cellspacing="0"> <tr> <th colspan="4" align="center"> <font color="#000000" size="2">GTK+ / Gnome Application Development</font> </th> </tr> <tr> <td width="25%" bgcolor="#ffffff" align="left"> <a href="z35.html"><font color="#0000ff" size="2"><b> <<< Previous</b></font></a> </td> <td width="25%" colspan="2" bgcolor="#ffffff" align= "center"> <font color="#0000ff" size="2"><b><a href="ggad.html"> <font color="#0000ff" size="2"><b> Home</b></font></a></b></font> </td> <td width="25%" bgcolor="#ffffff" align="right"> <a href="sec-containers.html"><font color="#0000ff" size="2"><b>Next >>></b></font></a> </td> </tr> </table> </div> <div class="CHAPTER"> <h1> <a name="CHA-GTK">GTK+ Basics</a> </h1> <div class="TOC"> <dl> <dt> <b>Table of Contents</b> </dt> <dt> <a href="cha-gtk.html#Z37">Whirlwind Tour of GTK+</a> </dt> <dt> <a href="sec-containers.html">Containers And Widget Layout</a> </dt> <dt> <a href="z57.html">Widget Concepts</a> </dt> <dt> <a href="sec-mainloop.html">The Main Loop</a> </dt> </dl> </div> <p> This chapter does the usual Hello, World to give you an overview of GTK+, then moves on to discuss some of the essential details you need to start developing GTK+ applications. </p> <p> If you've already read the GTK+ Tutorial from <a href= "http://www.gtk.org/" target="_top"> http://www.gtk.org/</a>, or the book <i class="EMPHASIS"> Developing Linux Applications with Gtk+ and Gdk</i> (also from New Riders), you may be able to skip or just skim this chapter. If you haven't used GTK+ before, this chapter is going to be very fast; read with care. </p> <div class="SECT1"> <h1 class="SECT1"> <a name="Z37">Whirlwind Tour of GTK+</a> </h1> <p> GTK+'s object-oriented coding style, clean design, and carefully followed API-naming conventions make programs simple to write and simple to understand. To make the point, here's a complete "Hello, World" in GTK+; most likely you can guess what 80% of the code does with no GTK+ experience whatsoever. </p> <div class="SECT2"> <h2 class="SECT2"> <a name="Z38">A Complete Hello, World</a> </h2> <div class="FIGURE"> <a name="Z39"></a> <p> <img src="figures/hello.png"> </p> <p> <b>Figure 1. Hello, World</b> </p> </div> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> #include <gtk/gtk.h>static gint delete_event_cb(GtkWidget* w, GdkEventAny* e, gpointer data);static void button_click_cb(GtkWidget* w, gpointer data);int main(int argc, char* argv[]){ GtkWidget* window; GtkWidget* button; GtkWidget* label; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); button = gtk_button_new(); label = gtk_label_new("Hello, World!"); gtk_container_add(GTK_CONTAINER(button), label); gtk_container_add(GTK_CONTAINER(window), button); gtk_window_set_title(GTK_WINDOW(window), "Hello"); gtk_container_set_border_width(GTK_CONTAINER(button), 10); gtk_signal_connect(GTK_OBJECT(window), "delete_event", GTK_SIGNAL_FUNC(delete_event_cb), NULL); gtk_signal_connect(GTK_OBJECT(button), "clicked", GTK_SIGNAL_FUNC(button_click_cb), label); gtk_widget_show_all(window); gtk_main(); return 0;}static gint delete_event_cb(GtkWidget* window, GdkEventAny* e, gpointer data){ gtk_main_quit(); return FALSE;}static void button_click_cb(GtkWidget* w, gpointer data){ GtkWidget* label; gchar* text; gchar* tmp; label = GTK_WIDGET(data); gtk_label_get(GTK_LABEL(label), &text); tmp = g_strdup(text); g_strreverse(tmp); gtk_label_set_text(GTK_LABEL(label), tmp); g_free(tmp);} </pre> </td> </tr> </table> <div class="SECT3"> <h3 class="SECT3"> <a name="Z40">Compiling Hello, World</a> </h3> <p> GTK+ comes with a shell script called <tt class= "APPLICATION">gtk-config</tt>; this script is created when GTK+ is built. Its purpose is to report the compiler flags you need to compile GTK+ programs. The following shell session demonstrates its features: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> $ gtk-config --version1.2.0$ gtk-config --prefix/home/hp/local$ gtk-config --exec-prefix/home/hp/local$ gtk-config --libs -L/home/hp/local/lib -L/usr/X11R6/lib -lgtk -lgdk -rdynamic -lgmodule -lglib -ldl -lXext -lX11 -lm$ gtk-config --libs gthread-L/home/hp/local/lib -L/usr/X11R6/lib -lgtk -lgdk -rdynamic -lgmodule -lgthread -lglib -lpthread -ldl -lXext -lX11 -lm$ gtk-config --cflags -I/usr/X11R6/include -I/home/hp/local/lib/glib/include -I/home/hp/local/include$ </pre> </td> </tr> </table> <p> If you're using a Bourne shell variant, such as <tt class="APPLICATION">bash</tt>, you can use backticks (<i class="EMPHASIS">not</i> single quotes!) to execute <tt class="APPLICATION">gtk-config</tt> and substitute its output. A simple <tt class="FILENAME"> Makefile</tt> for compiling Hello, World might look like this: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> CC=gccall: hello.c $(CC) `gtk-config --libs` `gtk-config --cflags` -o hello hello.cclean: /bin/rm -f *.o *~ </pre> </td> </tr> </table> <p> Of course, this <tt class="FILENAME">Makefile</tt> is far too simple for real-world applications; <a href= "cha-source.html">the chapter called <i>Creating Your Source Tree</i></a> describes how to set up a more realistic build using <tt class="APPLICATION"> automake</tt> and <tt class="APPLICATION"> autoconf</tt>. </p> <p> <tt class="APPLICATION">gtk-config</tt> allows you to locate GTK+ on the user's system, instead of hard-coding a location in your <tt class="FILENAME"> Makefile</tt>. It also comes in handy if you have two versions of GTK+ on your own system; if you install them each in a dedicated directory tree, you can choose one or the other by placing the correct <tt class="APPLICATION">gtk-config</tt> in your shell's search path. </p> </div> </div> <div class="SECT2"> <h2 class="SECT2"> <a name="Z41">How It Works</a> </h2> <p> This simple program contains all the essential elements of a GTK+ application. It doesn't contain any Gnome features; but since Gnome builds on GTK+, the same concepts will apply. </p> <div class="SECT3"> <h3 class="SECT3"> <a name="Z42">Initialization</a> </h3> <p> First, GTK+ must be initialized: </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> gtk_init(&argc, &argv); </pre> </td> </tr> </table> <p> This call connects to an X server, and parses some default arguments understood by all GTK+ programs. Parsed arguments are removed from <span class= "STRUCTNAME">argv</span>, and <span class= "STRUCTNAME">argc</span> is decremented accordingly. <tt class="FUNCTION">gtk_init()</tt> also registers a "cleanup function" using <tt class="FUNCTION"> atexit()</tt>. In practice, this is only important when you <tt class="FUNCTION">fork()</tt>; the child process must exit with <tt class="FUNCTION"> _exit()</tt> rather than <tt class="FUNCTION"> exit()</tt> to avoid shutting down GTK+ in the parent. </p> </div> <div class="SECT3"> <h3 class="SECT3"> <a name="Z43">Widgets</a> </h3> <p> Next, any program will have some user interface elements. In the X tradition, these are called <i class="FIRSTTERM">widgets</i>. All widgets are subclasses of the <tt class="CLASSNAME"> GtkWidget</tt> base class, so you can use a <span class="STRUCTNAME">GtkWidget*</span> to refer to them. (Since C has no native support for object inheritance, GTK+ has its own mechanism---<a href= "cha-objects.html">the chapter called <i>The GTK+ Object and Type System</i></a> describes this.) </p> <table border="0" bgcolor="#E0E0E0" width="100%"> <tr> <td><pre class="PROGRAMLISTING"> window = gtk_window_new(GTK_WINDOW_TOPLEVEL); button = gtk_button_new(); label = gtk_label_new("Hello, World!"); gtk_container_add(GTK_CONTAINER(button), label); gtk_container_add(GTK_CONTAINER(window), button); gtk_window_set_title(GTK_WINDOW(window), "Hello"); gtk_container_set_border_width(GTK_CONTAINER(button), 10); </pre> </td> </tr> </table> <p> Each widget has a function called <tt class= "FUNCTION">gtk_widgetname_new()</tt>, analagous to a constructor in C++ or Java. This function allocates a new object, initializes it, and returns a pointer to it. All of the <tt class="FUNCTION">_new()</tt> routines return a <span class="STRUCTNAME"> GtkWidget*</span>, even though they allocate a subclass; this is for convenience.
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -