📄 gtk.html
字号:
<HTML><HEAD><TITLE>GTK+</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.61"><LINKREL="HOME"TITLE="Writing GNOME Applications"HREF="index.html"><LINKREL="UP"TITLE="The GTK+/GNOME System"HREF="gtk-gnome-intro.html"><LINKREL="PREVIOUS"TITLE="GDK"HREF="gdk.html"><LINKREL="NEXT"TITLE="GNOME"HREF="gnome-intro.html"></HEAD><BODYCLASS="SECT1"><DIVCLASS="NAVHEADER"><TABLEWIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Writing GNOME Applications</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="gdk.html">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 2. The GTK+/GNOME System</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="gnome-intro.html">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="GTK">GTK+</A></H1><P> The GIMP Tookit (GTK+) contains the basic visual building blocks in a GNOME application. These building blocks make use of the resources provided by GDK, like fonts, windows, colors, and events, to create an interface that looks and behaves like a modern GUI application. </P><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN177">Widgets</A></H2><P> In GTK+, anything that can respond to your clicks, drags, and key presses is a widget of some sort. Windows, menus, push-buttons, and edit boxes are all widgets. Widgets can be grouped into larger composite widgets, which then act as a single widget. Menus work like this. The menu bar is a widget that contains smaller individual label widgets, each of which pops up another menu widget when you click on it. Another example of a composite widget is the dialog box, a pop-up window that contains an assortment of widgets and one or more push-buttons to accept or dismiss the changes or information contained within the dialog box. </P><P> All widgets have a set of basic properties that tell GTK+ how to interact with them. The most obvious state of a widget is visibility. You can show a widget to make it visible, or you can hide it to make it invisible. Users can interact only with visible widgets. Nested widgets always hide when their parents hide; if you hide a dialog box widget, GTK+ will hide all of its contents as well. </P><P> If you want to turn off a widget without hiding it, you can set its sensitivity to FALSE. An insensitive widget takes on a different appearance, usually grayed out or dulled, and ignores all input. An insensitive push-button will not depress when you click on it. By default, all widgets are created with a sensitivity of TRUE. </P><P> When you interact with widgets using the mouse, it's obvious which widget you are referring to: the one beneath the mouse cursor. Keyboard interaction is a little different. You wouldn't want the current position of the cursor to indicate which widget should receive the key presses. If you hap- pened to bump the mouse while you were typing, the cursor might move off of that widget and onto another one. Half of your text would go into the text widget, and the rest would disappear forever into that nearby button widget. Fur- thermore, if you were typing into a particularly small text widget, the cursor might end up blocking your view of what you've typed. </P><P> By established GUI convention, the latest mouse click determines the focus. You can click on the text widget (or another focus-bearing widget), and the widget will receive all key presses until you explicitly change the focus by clicking on something different, or by hitting the Tab key. Each window in an application will have a single widget with the default focus. If you start typing without first clicking on a widget, the default widget will receive all the key presses. </P><P> Figure 2.6 shows some common widgets as used in the GNOME menu editor application, including the menu bar and toolbar, a tree control on the left, and a notebook widget on the right, with two tabs, Basic and Advanced. Within the Advanced notebook tab, we see a couple grayed-out (insensitive) text widgets at the top, a GtkCList column list widget for the translations, three active (sensitive) text widgets, and four push-buttons at the bottom. The Save and Revert buttons have icons in them and are insensitive. </P><DIVCLASS="FIGURE"><ANAME="AEN185"></A><P><B>Figure 2-6. Widget Appearances</B></P><DIVCLASS="MEDIAOBJECT"><P><IMGSRC="figures/2f6.png"></IMG></P></DIV></DIV></DIV><DIVCLASS="SECT2"><H2CLASS="SECT2"><ANAME="AEN190">The GTK+ Type System</A></H2><P> One of the more impressive features of GTK+ is its dynamic, hierarchical data-typing system. GTK+ uses dynamic typing for various things, including object comparisons, type checking in variable parameter lists, and signal mar- shaling. The dynamic nature of the type system allows GTK+ to initialize types only as it needs them, rather than loading a huge master list of types that it may never use. Also, bindings to interactive programming languages (like Python or Perl) can make better use of the type system during runtime than they could during compile time if the type system were static. Dynamic typing also makes it possible to load in new types without recompiling GTK+ -for example, if your application creates its own set of private widgets. </P><P> GTK+ registers each type the first time it is accessed,2 referring to each type by a unique integer value. You can access this type value through a function that, by convention, has the form nnn_www_get_type( ), where the nnn is the namespace, and www is the name of the object or widget in question. Thus you can get GtkWidget's type with gtk_widget_get_type( ), and GnomeApp's type with gnome_app_get_type( ). The get_type( ) functions are global, so all instances of GtkWidget will have the same type, and so on. Often a widget defines a macro for its type function. For example, GTK_TYPE_WIDGET is equivalent to gtk_widget_get_type( ). </P><P> A more useful macro is the nnn_IS_www(obj) Boolean query-for example, GTK_IS_WINDOW(obj) and GNOME_IS_APP(obj). This query macro returns TRUE if the type of the supplied obj instance is a derivative of the www type, and FALSE if obj is not derived from www. This brings us to the hierarchical nature of the GTK+ type system. Any new widget must supply a base type that it wraps and extends. To illustrate this concept, consider the derivation path of the GnomeApp widget: </P><TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">GtkObject
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -