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

📄 gtk_tut.sgml

📁 gtk是linux一款强大的夸平台的图形化开发工具
💻 SGML
📖 第 1 页 / 共 5 页
字号:
<!doctype linuxdoc system><!-- This is the tutorial marked up in SGML     (just to show how to write a comment)--><article><title>GTK v1.2 Tutorial<author>Tony Gale <tt><htmlurl url="mailto:gale@gtk.org"			      name="&lt;gale@gtk.org&gt;"></tt>Ian Main <tt><htmlurl url="mailto:imain@gtk.org"			      name="&lt;imain@gtk.org&gt;"></tt>,<date>February 21st, 1999<abstract>This is a tutorial on how to use GTK (the GIMP Toolkit) through its Cinterface.</abstract><!-- Table of contents --><!-- Older versions of this tutorial did not have a table of contents,     but the tutorial is now so large that having one is very useful. --><toc> <!-- ***************************************************************** --><sect>Introduction<!-- ***************************************************************** --><p>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.It's called the GIMP toolkit because it was originally written fordeveloping the General 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 X windows). The primary authors of GTK are:<itemize><item> Peter Mattis   <tt><htmlurl url="mailto:petm@xcf.berkeley.edu"			   name="petm@xcf.berkeley.edu"></tt><item> Spencer Kimball <tt><htmlurl url="mailto:spencer@xcf.berkeley.edu"			   name="spencer@xcf.berkeley.edu"></tt><item> Josh MacDonald <tt><htmlurl url="mailto:jmacd@xcf.berkeley.edu"			   name="jmacd@xcf.berkeley.edu"></tt></itemize>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).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 functions areused 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.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, and Eiffel. 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.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.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. Note that there is also a C++ API for GTK(GTK--) in the works, so if you prefer to use C++, you should lookinto this instead. There are also Objective C, ADA, Guile and otherlanguage bindings available, but I don't follow these.This document is a 'work in progress'. Please look for updates onhttp://www.gtk.org/ <htmlurl url="http://www.gtk.org/"name="http://www.gtk.org/">.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 <ref id="sec_Contributing"name="Contributing"> for further information.<!-- ***************************************************************** --><sect>Getting Started<!-- ***************************************************************** --><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 onhttp://www.gtk.org/ <htmlurl url="http://www.gtk.org/"name="http://www.gtk.org/">. GTK uses GNU autoconf for configuration.Once untar'd, type ./configure --help to see a list of options.Th GTK source distribution also contains the complete source to all ofthe examples used in this tutorial, along with Makefiles to aidcompilation.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 using the shell.<tscreen><verb>/* example-start base base.c */#include <gtk/gtk.h>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 */</verb></tscreen>You can compile the above program with gcc using:<tscreen><verb>gcc base.c -o base `gtk-config --cflags --libs`</verb></tscreen>The meaning of the unusual compilation options is explained below.All programs will of course include gtk/gtk.h which declares thevariables, functions, structures etc. that will be used in your GTKapplication.The next line:<tscreen><verb>gtk_init (&amp;argc, &amp;argv);</verb></tscreen>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:<itemize><item> <tt/--gtk-module/<item> <tt/--g-fatal-warnings/<item> <tt/--gtk-debug/<item> <tt/--gtk-no-debug/<item> <tt/--gdk-debug/<item> <tt/--gdk-no-debug/<item> <tt/--display/<item> <tt/--sync/<item> <tt/--no-xshm/<item> <tt/--name/<item> <tt/--class/</itemize>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.The next two lines of code create and display a window.<tscreen><verb>  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);  gtk_widget_show (window);</verb></tscreen>The GTK_WINDOW_TOPLEVEL argument specifies that we want the window toundergo window manager decoration and placement. Rather than create awindow of 0x0 size, a window without children is set to 200x200 bydefault so you can still manipulate it.The gtk_widget_show() function lets GTK know that we are done settingthe attributes of this widget, and that it can display it.The last line enters the GTK main processing loop.<tscreen><verb>  gtk_main ();</verb></tscreen>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.<!-- ----------------------------------------------------------------- --><sect1>Hello World in GTK<p>Now for a program with a widget (a button).  It's the classichello world a la GTK.<tscreen><verb>/* 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(&amp;argc, &amp;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 */</verb></tscreen><!-- ----------------------------------------------------------------- --><sect1>Compiling Hello World<p>To compile use:<tscreen><verb>gcc -Wall -g helloworld.c -o helloworld `gtk-config --cflags` \    `gtk-config --libs`</verb></tscreen>This uses the program <tt>gtk-config</>, which comes with gtk. Thisprogram 'knows' what compiler switches are needed to compile programsthat use gtk. <tt>gtk-config --cflags</> will output a list of includedirectories for the compiler to look in, and <tt>gtk-config --libs</>will output the list of libraries for the compiler to link with andthe directories to find them in. In the aboce example they could havebeen combined into a single instance, such as`gtk-config --cflags --libs`.Note that the type of single quote used in the compile command aboveis significant.The libraries that are usually linked in are:<itemize><item>The GTK library (-lgtk), the widget library, based on top of GDK.<item>The GDK library (-lgdk), the Xlib wrapper.<item>The gmodule library (-lgmodule), which is used to load run timeextensions.<item>The glib library (-lglib), containing miscellaneous functions, onlyg_print() is used in this particular example. GTK is built on topof glib so you will always require this library. See the section on <ref id="sec_glib" name="glib"> for details.<item>The Xlib library (-lX11) which is used by GDK.<item>The Xext library (-lXext). This contains code for shared memorypixmaps and other X extensions.<item>The math library (-lm). This is used by GTK for various purposes.</itemize><!-- ----------------------------------------------------------------- --><sect1>Theory of Signals and Callbacks

⌨️ 快捷键说明

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