⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gtk-tut.sgml

📁 linux下电话本所依赖的一些图形库
💻 SGML
📖 第 1 页 / 共 5 页
字号:
<!doctype book PUBLIC "-//OASIS//DTD DocBook V3.1//EN" [<!notation PNG system "PNG"><!entity % local.notation.class "| PNG">]><book id="gtk-tut"><bookinfo>    <date>January 24th, 2003</date>    <title>GTK+ 2.0 Tutorial</title>    <authorgroup>      <author>        <firstname>Tony</firstname>        <surname>Gale</surname>      </author>      <author>        <firstname>Ian</firstname>        <surname>Main</surname>      </author>      <author>	<firstname>&amp; the GTK team</firstname>      </author>    </authorgroup>    <abstract>      <para>This is a tutorial on how to use GTK (the GIMP Toolkit) through its C            interface.</para>    </abstract>  </bookinfo><toc></toc><!-- ***************************************************************** --><chapter id="ch-TutorialDownload"><title>Tutorial Availability</title><para>A copy of this tutorial in SGML and HTML is distributed with eachsource code release of GTK+. For binary distributions, please check withyour vendor.</para><para>A copy is available online for reference at <ulink url="http://www.gtk.org/tutorial/">http://www.gtk.org/tutorial</ulink>.</para><para>A packaged verion of this tutorial is available from<ulink url="ftp://ftp.gtk.org/pub/gtk/tutorial/">ftp://ftp.gtk.org/pub/gtk/tutorial</ulink> which contains the tutorial invarious different formats. Thispackage is primary for those people wanting to have the tutorialavailable for offline reference and for printing.</para></chapter><!-- ***************************************************************** --><chapter id="ch-Introduction"><title>Introduction</title><para>GTK (GIMP Toolkit) is a library for creating graphical userinterfaces. It is licensed using the LGPL license, so you can developopen software, free software, or even commercial non-free softwareusing GTK without having to spend anything for licenses or royalties.</para><para>It's called the GIMP toolkit because it was originally written fordeveloping the GNU Image Manipulation Program (GIMP), but GTK hasnow been used in a large number of software projects, including theGNU Network Object Model Environment (GNOME) project. GTK is built ontop of GDK (GIMP Drawing Kit) which is basically a wrapper around thelow-level functions for accessing the underlying windowing functions(Xlib in the case of the X windows system), and gdk-pixbuf, a library forclient-side image manipulation.</para><para>The primary authors of GTK are:</para><itemizedlist><listitem><simpara> Peter Mattis <ulink url="mailto:petm@xcf.berkeley.edu">petm@xcf.berkeley.edu</ulink></simpara></listitem><listitem><simpara> Spencer Kimball <ulink url="mailto:spencer@xcf.berkeley.edu">spencer@xcf.berkeley.edu</ulink></simpara></listitem><listitem><simpara> Josh MacDonald <ulink url="mailto:jmacd@xcf.berkeley.edu">jmacd@xcf.berkeley.edu</ulink></simpara></listitem></itemizedlist><para>GTK is currently maintained by:</para><itemizedlist><listitem><simpara> Owen Taylor <ulink url="mailto:otaylor@redhat.com">otaylor@redhat.com</ulink></simpara></listitem><listitem><simpara> Tim Janik <ulink url="mailto:timj@gtk.org">timj@gtk.org</ulink></simpara></listitem></itemizedlist><para>GTK is essentially an object oriented application programmersinterface (API). Although written completely in C, it is implementedusing the idea of classes and callback functions (pointers tofunctions).</para><para>There is also a third component called GLib which contains a fewreplacements for some standard calls, as well as some additionalfunctions for handling linked lists, etc. The replacement functionsare used to increase GTK's portability, as some of the functionsimplemented here are not available or are nonstandard on other Unixessuch as g_strerror(). Some also contain enhancements to the libcversions, such as g_malloc() that has enhanced debugging utilities.</para><para>In version 2.0, GLib has picked up the type system which forms thefoundation for GTK's class hierarchy, the signal system which is usedthroughout GTK, a thread API which abstracts the different native thread APIs of the various platforms and a facility for loading modules.</para><para>As the last component, GTK uses the Pango library for internationalizedtext output.</para><para>This tutorial describes the C interface to GTK. There are GTKbindings for many other languages including C++, Guile, Perl, Python,TOM, Ada95, Objective C, Free Pascal, Eiffel, Java and C#. If you intend touse another language's bindings to GTK, look at that binding'sdocumentation first. In some cases that documentation may describesome important conventions (which you should know first) and thenrefer you back to this tutorial. There are also some cross-platformAPIs (such as wxWindows and V) which use GTK as one of their targetplatforms; again, consult their documentation first.</para><para>If you're developing your GTK application in C++, a few extra notesare in order. There's a C++ binding to GTK called GTK--, whichprovides a more C++-like interface to GTK; you should probably lookinto this instead. If you don't like that approach for whateverreason, there are two alternatives for using GTK. First, you can useonly the C subset of C++ when interfacing with GTK and then use the Cinterface as described in this tutorial. Second, you can use GTK andC++ together by declaring all callbacks as static functions in C++classes, and again calling GTK using its C interface. If you choosethis last approach, you can include as the callback's data value apointer to the object to be manipulated (the so-called "this" value).Selecting between these options is simply a matter of preference,since in all three approaches you get C++ and GTK. None of theseapproaches requires the use of a specialized preprocessor, so nomatter what you choose you can use standard C++ with GTK.</para><para>This tutorial is an attempt to document as much as possible of GTK,but it is by no means complete. This tutorial assumes a goodunderstanding of C, and how to create C programs. It would be a greatbenefit for the reader to have previous X programming experience, butit shouldn't be necessary. If you are learning GTK as your firstwidget set, please comment on how you found this tutorial, and whatyou had trouble with. There are also C++, Objective C, ADA, Guile andother language bindings available, but I don't follow these.</para><para>This document is a "work in progress". Please look for updates on<ulink url="http://www.gtk.org/">http://www.gtk.org/</ulink>.</para><para>I would very much like to hear of any problems you have learning GTKfrom this document, and would appreciate input as to how it may beimproved. Please see the section on <link linkend="ch-Contributing">Contributing</link> for further information.</para></chapter><!-- ***************************************************************** --><chapter id="ch-GettingStarted"><title>Getting Started</title><para>The first thing to do, of course, is download the GTK source andinstall it. You can always get the latest version from <ulink url="ftp://ftp.gtk.org/pub/gtk">ftp.gtk.org</ulink>. You can also view other sources of GTK information on<ulink url="http://www.gtk.org/">http://www.gtk.org/</ulink>. GTKuses GNU autoconf for configuration. Once untar'd, type <literal>./configure --help</literal> to see a list of options.</para><para>The GTK source distribution also contains the complete source to allof the examples used in this tutorial, along with Makefiles to aidcompilation.</para><para>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.</para><para><inlinemediaobject><imageobject><imagedata fileref="images/base.png" format="png"></imageobject></inlinemediaobject></para><programlisting role="C"><!-- example-start base base.c -->#include &lt;gtk/gtk.h&gt;int main( int   argc,          char *argv[] ){    GtkWidget *window;        gtk_init (&amp;argc, &amp;argv);        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);    gtk_widget_show  (window);        gtk_main ();        return 0;}<!-- example-end --></programlisting><para>You can compile the above program with gcc using:</para><para><literallayout><literal>gcc base.c -o base `pkg-config --cflags --libs gtk+-2.0`</literal></literallayout></para><para>The meaning of the unusual compilation options is explained below in<link linkend="sec-Compiling">Compiling Hello World</link>.</para><para>All programs will of course include <filename>gtk/gtk.h</filename> which declares the variables, functions, structures, etc. that will be used in your GTKapplication.</para><para>The next line:</para><programlisting role="C">gtk_init (&amp;argc, &amp;argv);</programlisting><para>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 forone of the following:</para><itemizedlist spacing=Compact><listitem><simpara> <literal>--gtk-module</literal></simpara></listitem><listitem><simpara> <literal>--g-fatal-warnings</literal></simpara></listitem><listitem><simpara> <literal>--gtk-debug</literal></simpara></listitem><listitem><simpara> <literal>--gtk-no-debug</literal></simpara></listitem><listitem><simpara> <literal>--gdk-debug</literal></simpara></listitem><listitem><simpara> <literal>--gdk-no-debug</literal></simpara></listitem><listitem><simpara> <literal>--display</literal></simpara></listitem><listitem><simpara> <literal>--sync</literal></simpara></listitem><listitem><simpara> <literal>--name</literal></simpara></listitem><listitem><simpara> <literal>--class</literal></simpara></listitem></itemizedlist><para>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.</para><para>The next two lines of code create and display a window.</para><programlisting role="C">  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);  gtk_widget_show (window);</programlisting><para>The <literal>GTK_WINDOW_TOPLEVEL</literal> 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.</para><para>The gtk_widget_show() function lets GTK know that we are done settingthe attributes of this widget, and that it can display it.</para><para>The last line enters the GTK main processing loop.</para><programlisting role="C">  gtk_main ();</programlisting><para>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.</para><!-- ----------------------------------------------------------------- --><sect1 id="sec-HelloWorld"><title>Hello World in GTK</title><para>Now for a program with a widget (a button). It's the classichello world a la GTK.</para><para><inlinemediaobject><imageobject><imagedata fileref="images/helloworld.png" format="png"></imageobject></inlinemediaobject></para><programlisting role="C"><!-- example-start helloworld helloworld.c -->#include &lt;gtk/gtk.h&gt;/* 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;    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -