📄 book.html
字号:
<html> <head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> <title>Book</title> </head> <BODY TEXT="#333333" BGCOLOR="#FFFFFF" LINK="#006699" VLINK="#993300" ALINK="#339900"><body><h1>Gtk+/Gnome Application Development</h1><h2>Havoc Pennington</h2><p><h1>Part 1: Overview</h1></p><h1>1</h1><h1>Introduction</h1><p> This chapter gives you an overview of the technologies described in this book.</p> <h2>1.1: What is Gnome?</h2><p> Gnome is a free (or "open source") software development project started in 1997 by Miguel de Icaza of the Mexican Autonomous National University and a small team of programmers from around the world. Inspired by the success of the similar K Desktop Environment (KDE) project, the burgeoning popularity of the GNU/Linux operating system, and and the power of the Gtk+ graphical toolkit, Gnome grew quickly --- within a year, hundreds of programmers were involved and many thousands of lines of code had been written. Gnome has become a powerful framework for GUI application development which runs on any modern variety of UNIX.</p> <p> "Gnome" is actually an acronym: GNU Network Object Model Environment. Originally, the project was intended to create a framework for application objects, similar to Microsoft's OLE and COM technologies. However, the scope of the project rapidly expanded; it became clear that substantial groundwork was required before the "network object" part of the name could become reality. The latest development versions of Gnome include an object embedding architecture called Bonobo, and Gnome 1.0 included a fast, light CORBA 2.2 ORB called ORBit.</p> <p> Gnome is a part of the GNU Project, whose overall goal is developing a free operating system (named GNU) plus applications to go with it. GNU stands for "GNU's Not UNIX", a humorous way of saying that the GNU operating system is UNIX-compatible. You can learn more about GNU at <a href="http://www.gnu.org">http://www.gnu.org</a>.</p> <p> Gnome has two important faces. From the user's perspective, it is an integrated desktop environment and application suite. From the programmer's perspective, it is an application development framework (made up of numerous useful libraries). Applications written with the Gnome libraries run fine even if the user isn't running the desktop environment, but they integrate nicely with the Gnome desktop if it's available.</p> <p> The desktop environment includes a file manager, a "panel" for task switching, launching programs, and docking applets, a "control center" for configuration, and several smaller bells and whistles. These programs hide the traditional UNIX shell behind an easy-to-use graphical interface.</p> <p> Gnome's development framework makes it possible to write consistent, easy-to-use, interoperable applications. The X Window System designers made a deliberate decision not to impose any user interface policy on developers; Gnome adds a "policy layer," creating a consistent look-and-feel. Finished Gnome applications work well with the Gnome desktop, but can also be used "standalone" --- users only need to install Gnome's shared libraries. It's even possible to write Gnome applications which do not rely on the X Window System; you might want to provide a non-graphical CORBA service, for example.</p> <p> This book is about Gnome from a developer's point of view; it describes how to write a Gnome application using the Gnome libraries and tools.</p> <h2>1.2: The Gnome Development Framework</h2><p> Gnome's application development framework centers around a suite of libraries, all written in portable ANSI C and intended to be used on UNIX-like systems. Libraries which involve graphics realy on the X Window System. Wrappers are available which export the Gnome API to nearly any language you can think of, including Ada, Scheme, Python, Perl, Tom, Eiffel, Dylan, and Objective C. There are at least three different C++ wrappers as well.</p> <p> This book will cover the C interface to the libraries; however, it should be useful for users of any language binding, since the mapping from C to your preferred language is typically straightforward. The book covers version 1.0 of the Gnome libraries (including the compatible bug fix releases, such as 1.0.9---all 1.0.x versions are compatible).</p> <h3>1.2.1: Non-Gnome Libraries</h3><p> Taking full advantage of the free software tradition, Gnome didn't start from scratch. It uses several libraries which are maintained separately from the Gnome project. These are a part of the Gnome application development framework, and you can count on their presence in a Gnome environment.</p> <h4>glib</h4><p> glib is the base of the Gnome infrastructure. It's a C utility library, providing routines to create and manipulate common data structures. It also addresses portability issues; for example, many systems lack the <tt>snprintf()</tt> function, but glib contains an implementation called <tt>g_snprintf()</tt> which is both guaranteed to exist on all platforms and slightly safer than <tt>snprintf()</tt> (it always <tt>NULL</tt>-terminates the target buffer).</p> <p> Gnome 1.0 uses glib version 1.2 and works with any glib in the 1.2 series (1.2.1, 1.2.2, etc.). All glib versions beginning with 1.2 are compatible bug-fix releases.</p> <h4>Gtk+</h4><p> Gtk+, or the Gimp Tool Kit, is the GUI toolkit used in Gnome applications. Gtk+ was originally written for the Gimp (GNU Image Manipulation Program --- <a href="http://www.gimp.org">http://www.gimp.org</a>), but has become a general-purpose library. Gtk+ depends on glib.</p> <p> The Gtk+ package includes Gdk, the Gimp Drawing Kit, which is a simplification and abstraction of the low-level X Window System libraries. Since Gtk+ uses Gdk rather than calling X directly, a port of Gdk permits Gtk+ to run on windowing systems other than X with relatively few modifications. Gtk+ and the Gimp have already been ported to the Win32 platform in this way.</p> <p> Gtk+ provides several features for Gnome applications:</p> <ul><li> A dynamic type system.<li> An object system written in C, complete with inheritance, type checking, and a signal/callback infrastructure. The type and object systems are not GUI-specific.<li> A <tt>GtkWidget</tt> object written using the object system, which defines the interface Gtk+'s graphical components implement.<li> A large collection of useful <tt>GtkWidget</tt> subclasses (<i>widgets</i>); this collection forms the bulk of Gtk+'s code.</ul><p> Gnome adds a number of additional widgets to the basic Gtk+ collection.</p> <p> Gnome 1.0 is based on Gtk+ version 1.2. All Gtk+ versions beginning with 1.2 are compatible bug-fix releases; 1.2.1, for example.</p> <h4>ORBit</h4><p> ORBit is a CORBA 2.2 ORB written in C. It was designed to be small and fast compared to other ORBs, and supports the C language mapping. ORBit is implemented as a suite of libraries.</p> <p> <i>CORBA</i>, or Common Object Request Broker Architecture, is a specification for Object Request Brokers, or <i>ORB</i>s. An ORB is much like a dynamic linker, but it works with objects, rather than subroutines. At runtime, a program can request the services of a particular object; the ORB locates the object and creates a connection between it and the program. For example, an email program might request an ``addressbook'' object, and use it to look up a person's name. Unlike dynamic linking, CORBA works fine across a network, and even allows different programming languages and operating systems to interact with one another. If you're familiar with DCOM on the Windows operating system, CORBA is analagous.</p> <h4>Imlib</h4><p> Imlib ("Image Library") provides routines for loading, saving, displaying, and scaling images in a variety of popular formats (including GIF, JPEG, PNG, and TIFF). It comes in two versions; an Xlib-only version, and a Gdk-based version. Gnome uses the Gdk version.</p> <h3>1.2.2: Gnome Libraries</h3><p> The libraries in this section are a part of the <tt>gnome-libs</tt> package and were developed specifically for the Gnome Project.</p> <h4><tt>libgnome</tt></h4><p> <tt>libgnome</tt> is a collection of non-GUI-related routines for use by Gnome applications. It includes code to parse configuration files, for example. It also includes interfaces to some external facilities, such as internationalization (via the GNU <tt>gettext</tt> package), argument parsing (via the <tt>popt</tt> package), and sound (via the Enlightenment Sound Daemon, <tt>esound</tt>). The <tt>gnome-libs</tt> package takes care of interacting with these external libraries, so the programmer does not need to concern herself with their implementation or availability.</p> <h4><tt>libgnomeui</tt></h4><p> <tt>libgnomeui</tt> collects GUI-related Gnome code. It consists primarily of widgets designed to enhance and extend Gtk+. Gnome widgets generally impose user interface policy, which permits a more convenient API (since there is less for the programmer to specify). It also results in applications with more consistent interfaces, of course.</p> <p> Highlights of <tt>libgnomeui</tt> include:</p> <ul><li> The <tt>GnomeApp</tt> widget, which makes it easy to create a nice main window for your application. It uses another widget called <tt>GnomeDock</tt> which enables users to rearrange and "undock" toolbars.<li> The <tt>GnomeCanvas</tt> widget which makes it easy to write intricate, flicker-free custom displays.<li> The Gnome stock pixmaps (icons for open, close, save, and other operations).<li> Convenient routines for creating and using dialogs.<li> The <tt>GnomePixmap</tt> widget which is more versatile than <tt>GtkPixmap</tt>.</ul><h4><tt>libgnorba</tt></h4><p> <tt>libgnorba</tt> provides CORBA-related facilities, including a security mechanism and object activation. (Object activation is the process of obtaining a reference to an object that implements a given interface; it can involve executing a server program, loading a shared library module, or asking an existing program for a new object instance.)</p> <h4><tt>libzvt</tt></h4><p> This small library contains a terminal widget (<tt>ZvtTerm</tt>) you can use in your Gnome programs.</p> <h4><tt>libart_lgpl</tt></h4><p> This library contains graphics rendering routines by Raph Levien. The routines included here are released under the GNU Library General Public License and used in the <tt>GnomeCanvas</tt> widget; Raph Levien also sells an enhanced proprietary version. <tt>libart_lgpl</tt> provides antialiasing, microtile refresh regions, and other magic. In essence it is a vector graphics rasterization library, functionally analogous to the PostScript language.</p> <h3>1.2.3: Other Libraries</h3><p> These libraries are commonly used in Gnome applications, but are not a part of <tt>gnome-libs</tt> proper.</p> <h4><tt>gnome-print</tt></h4><p> <tt>gnome-print</tt> is still somewhat experimental, but very promising. It uses <tt>libart_lgpl</tt> and works nicely with <tt>GnomeCanvas</tt>. It provides virtual output devices (called "print contexts"), so a single codebase can output to a print preview widget, to PostScript, and eventually to other printer formats. <tt>gnome-print</tt> also includes printing-related GUI elements, like a print setup dialog, and a virtual font interface (to deal with the problem that X fonts are not printable).</p> <h4><tt>gnome-xml</tt></h4><p> <tt>gnome-xml</tt> is a non-validating XML engine written by Daniel Veillard of the World Wide Web Consortium. It can parse XML into a tree structure, and output a tree structure as XML. It's useful for any application that needs to load and save structured data; many Gnome applications use it as a file format. This library does not depend on any of the others, not even glib --- so it is tied to Gnome in name only. However, you can expect most Gnome users to have it installed, so it should not inconvenience your users if your application uses this library.</p> <h4>Guile</h4><p> Guile is an implementation of the Scheme programming language in a library, so that any application can have an embedded Scheme interpreter. It is the official extension language of the GNU Project, and is used by several Gnome applications. Adding an extension language to your application might sound complex, but Guile makes it almost trivial. (Several Gnome applications support Perl and Python as well; it is usually easy to support several languages once you implement the first. But Guile has a special place in the Gnome developer's hearts.)</p> <h4>Bonobo</h4><p> At press time, the Gnome hackers were putting the finishing touches on Bonobo. Bonobo is a compound document architecture in the tradition of Microsoft's OLE; it allows you to embed charts in spreadsheets, for example. It will be used pervasively throughout Gnome; any application will be able to display MIME-typed data such as plain text, HTML, or images by asking the Gnome libraries for an appropriate Bonobo component. Look for Bonobo technology in the next major Gnome release.</p> <h3>1.2.4: A Word About Header Files</h3><p> Throughout the book, the exact header file which declares each function is given alongside the function prototype. This is to facilitate your exploration of the source code. However, you probably don't want to manually include the hundreds of headers found in Gtk+ and Gnome. You can include all Gtk+ headers <i>en masse</i> by including the <tt>gtk/gtk.h</tt> header file. <tt>gtk/gtk.h</tt> also includes <tt>gdk/gdk.h</tt> for you. You can include all Gnome headers by including <tt>gnome.h</tt>; <tt>gnome.h</tt> includes <tt>gtk/gtk.h</tt> for you. Most Gnome application files simply include <tt>gnome.h</tt>.</p> <h2>1.3: Structure of the Book</h2><p> This book is divided into several parts:</p> <ul><li> Right now you're reading Part 1. This chapter gives you an overview of the Gnome application development framework. The following two chapters quickly introduce glib and Gtk+ -- if you're already familiar with Gtk+ programming, you may want to move directly to Part 2. <li> Part 2 guides you through the development of a skeletal Gnome application. It starts with the creation of the source tree, and then explains how to write menus, dialogs, and other essential application components. At the end of Part 2, there's a checklist of features every application should have.<li> Part 3 describes some advanced topics, including the internals of the Gtk+ object and type system, writing a custom <tt>GtkWidget</tt> subclass, and using the <tt>GnomeCanvas</tt> widget. It also covers Gdk.<li> The appendices include a map of the Gtk+ and Gnome object hierarchy, with a short description of each object; a map of the Gtk+ and Gnome header files; and some pointers to online programming resources.</ul><p> This book assumes some knowledge of Gtk+ programming; chapters 2 and 3 will give you a quick overview if your knowledge is minimal, but they move very quickly. Most of the specific widgets in Gtk+ are not covered. The book is intended to introduce Gnome programming, and some advanced Gtk+ topics; it is a supplement to an introductory Gtk+ book.</p> <p> Due to time and space limitations, only the central Gnome libraries are covered; in particular, CORBA, printing, XML, plugins, and scripting are not covered. Most large applications will use these features in addition to the core libraries.</p> <h1>2</h1><h1>glib: Portability and Utility</h1><p> glib is a C portability and utility library for UNIX-like systems and Windows. This chapter covers some of the most commonly-used library features in Gtk+ and Gnome applications. glib is simple, and the concepts are familiar; so we'll move quickly. For more complete coverage of glib, see <tt>glib.h</tt> or the free glib reference manual that comes with the library. (By the way: don't be afraid of using the glib, Gtk+, or Gnome header files; they are very clean and easy to read, and are handy as a quick reference. For that matter, don't be afraid to look at the source code, if you have very specific questions about the implementation.)</p> <p> glib's various facilities are intended to have a consistent interface; the coding style is semi-object-oriented, and identifiers are prefixed with "g" to create a kind of namespace.</p> <p> glib has a single header file, <tt>glib.h</tt>.</p> <h2>2.1: Basics</h2><p> glib provides substitutes for many standard and commonly-used C language constructs. This section describes glib's fundamental type definitions, macros, memory allocation routines, and string utility functions.</p> <h3>2.1.1: Type Definitions</h3><p> Rather than using C's standard types (<tt>int</tt>, <tt>long</tt>, etc.) glib defines its own. These serve a variety of purposes. For example, <tt>gint32</tt> is guaranteed to be 32 bits wide, something no standard C type can ensure. <tt>guint</tt> is simply easier to type than <tt>unsigned</tt>. A few of the typedefs exist only for consistency; for example, <tt>gchar</tt> is always equivalent to the standard <tt>char</tt>.</p> <p> The following primitive types are defined by glib:</p> <ul><li> <tt>gint8</tt>, <tt>guint8</tt>, <tt>gint16</tt>, <tt>guint16</tt>, <tt>gint32</tt>, <tt>guint32</tt>, <tt>gint64</tt>, <tt>guint64</tt>---these give you integers of a guaranteed size. Not all platforms provide 64-bit integers; if a platform has them, glib will define <tt>G_HAVE_GINT64</tt>. (If it isn't obvious, the <tt>guint</tt> types are unsigned, the <tt>gint</tt> types are signed.)<li> <tt>gboolean</tt> is useful to make your code more readable, since C has no <tt>bool</tt> type. <li> <tt>gchar</tt>, <tt>gshort</tt>, <tt>glong</tt>, <tt>gint</tt>, <tt>gfloat</tt>, <tt>gdouble</tt> are purely cosmetic.<li> <tt>gpointer</tt> may be more convenient to type than <tt>void *</tt>. <tt>gconstpointer</tt> gives you <tt>const void*</tt>. (<tt>const gpointer</tt> will <i>not</i> do what you typically mean it to; spend some time with a good book on C if you don't see why.)</ul><h3>2.1.2: Frequently Used Macros</h3><p> glib defines a number of familiar macros used in many C programs; these are shown in Macro Listing 2.1. All of these should be self-explanatory. <tt>TRUE</tt>/<tt>FALSE</tt>/<tt>NULL</tt> are the usual <tt>1</tt>/<tt>0</tt>/<tt>((void*)0)</tt>; <tt>MIN()</tt>/<tt>MAX()</tt> return the smaller or larger of their arguments. <tt>ABS()</tt> returns the absolute value of its argument. <tt>CLAMP(x, low, high)</tt> means <tt>x</tt>, unless <tt>x</tt> is outside the range [<tt>low</tt>, <tt>high</tt>]; if <tt>x</tt> is below the range, <tt>low</tt> is returned; if <tt>x</tt> is above the range, <tt>high</tt> is returned.</p> <p><table bgcolor="#FFEEEE" width="100%"><caption><b>Macro Listing 2.1:</b> Familiar C Macros</caption><tr align="right"><td> <font color="black"> <tt>#include <glib.h></tt> </font> </td></tr><tr><td> <font color="black"><pre><tt>TRUE</tt></pre></font> </td></tr><tr><td> <font color="black"><pre><tt>FALSE</tt></pre></font> </td></tr><tr><td> <font color="black"><pre><tt>NULL</tt></pre></font> </td></tr><tr><td> <font color="black"><pre><tt>MAX(a, b)</tt></pre></font> </td></tr><tr><td> <font color="black"><pre><tt>MIN(a, b)</tt></pre></font> </td></tr><tr><td> <font color="black"><pre><tt>ABS(x)</tt></pre></font> </td></tr><tr><td> <font color="black"><pre><tt>CLAMP(x, low, high)</tt></pre></font> </td></tr></table></p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -