source: trunk/third/gtk/docs/html/gtk_tut-5.html @ 14482

Revision 14482, 6.0 KB checked in by ghudson, 25 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r14481, which included commits to RCS files with non-trunk default branches.
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
2<HTML>
3<HEAD>
4 <META NAME="GENERATOR" CONTENT="SGML-Tools 1.0.9">
5 <TITLE>GTK v1.2 Tutorial: Widget Overview</TITLE>
6 <LINK HREF="gtk_tut-6.html" REL=next>
7 <LINK HREF="gtk_tut-4.html" REL=previous>
8 <LINK HREF="gtk_tut.html#toc5" REL=contents>
9</HEAD>
10<BODY BGCOLOR="#FFFFFF">
11<A HREF="gtk_tut-6.html">Next</A>
12<A HREF="gtk_tut-4.html">Previous</A>
13<A HREF="gtk_tut.html#toc5">Contents</A>
14<HR NOSHADE>
15<H2><A NAME="s5">5. Widget Overview</A></H2>
16
17<P>The general steps to creating a widget in GTK are:
18<OL>
19<LI> gtk_*_new - one of various functions to create a new widget.
20These are all detailed in this section.
21</LI>
22<LI> Connect all signals and events we wish to use to the
23appropriate handlers.
24</LI>
25<LI> Set the attributes of the widget.
26</LI>
27<LI> Pack the widget into a container using the appropriate call
28such as gtk_container_add() or gtk_box_pack_start().
29</LI>
30<LI> gtk_widget_show() the widget.</LI>
31</OL>
32<P>gtk_widget_show() lets GTK know that we are done setting the
33attributes of the widget, and it is ready to be displayed. You may
34also use gtk_widget_hide to make it disappear again. The order in
35which you show the widgets is not important, but I suggest showing the
36window last so the whole window pops up at once rather than seeing the
37individual widgets come up on the screen as they're formed. The
38children of a widget (a window is a widget too) will not be displayed
39until the window itself is shown using the gtk_widget_show() function.
40<P>
41<H2><A NAME="ss5.1">5.1 Casting</A>
42</H2>
43
44<P>You'll notice as you go on that GTK uses a type casting system. This
45is always done using macros that both test the ability to cast the
46given item, and perform the cast. Some common ones you will see are:
47<P>
48<BLOCKQUOTE><CODE>
49<PRE>
50  GTK_WIDGET(widget)
51  GTK_OBJECT(object)
52  GTK_SIGNAL_FUNC(function)
53  GTK_CONTAINER(container)
54  GTK_WINDOW(window)
55  GTK_BOX(box)
56</PRE>
57</CODE></BLOCKQUOTE>
58<P>These are all used to cast arguments in functions. You'll see them in the
59examples, and can usually tell when to use them simply by looking at the
60function's declaration.
61<P>As you can see below in the class hierarchy, all GtkWidgets are
62derived from the Object base class. This means you can use a widget
63in any place the function asks for an object - simply use the
64<CODE>GTK_OBJECT()</CODE> macro.
65<P>For example:
66<P>
67<BLOCKQUOTE><CODE>
68<PRE>
69gtk_signal_connect( GTK_OBJECT(button), "clicked",
70                    GTK_SIGNAL_FUNC(callback_function), callback_data);
71</PRE>
72</CODE></BLOCKQUOTE>
73 
74<P>This casts the button into an object, and provides a cast for the
75function pointer to the callback.
76<P>Many widgets are also containers. If you look in the class hierarchy
77below, you'll notice that many widgets derive from the Container
78class. Any one of these widgets may be used with the
79<CODE>GTK_CONTAINER</CODE> macro to pass them to functions that ask for
80containers.
81<P>Unfortunately, these macros are not extensively covered in the
82tutorial, but I recommend taking a look through the GTK header
83files. It can be very educational. In fact, it's not difficult to
84learn how a widget works just by looking at the function declarations.
85<P>
86<H2><A NAME="ss5.2">5.2 Widget Hierarchy</A>
87</H2>
88
89<P>For your reference, here is the class hierarchy tree used to implement widgets.
90<P>
91<BLOCKQUOTE><CODE>
92<PRE>
93 GtkObject
94  +GtkWidget
95  | +GtkMisc
96  | | +GtkLabel
97  | | | +GtkAccelLabel
98  | | | `GtkTipsQuery
99  | | +GtkArrow
100  | | +GtkImage
101  | | `GtkPixmap
102  | +GtkContainer
103  | | +GtkBin
104  | | | +GtkAlignment
105  | | | +GtkFrame
106  | | | | `GtkAspectFrame
107  | | | +GtkButton
108  | | | | +GtkToggleButton
109  | | | | | `GtkCheckButton
110  | | | | |   `GtkRadioButton
111  | | | | `GtkOptionMenu
112  | | | +GtkItem
113  | | | | +GtkMenuItem
114  | | | | | +GtkCheckMenuItem
115  | | | | | | `GtkRadioMenuItem
116  | | | | | `GtkTearoffMenuItem
117  | | | | +GtkListItem
118  | | | | `GtkTreeItem
119  | | | +GtkWindow
120  | | | | +GtkColorSelectionDialog
121  | | | | +GtkDialog
122  | | | | | `GtkInputDialog
123  | | | | +GtkDrawWindow
124  | | | | +GtkFileSelection
125  | | | | +GtkFontSelectionDialog
126  | | | | `GtkPlug
127  | | | +GtkEventBox
128  | | | +GtkHandleBox
129  | | | +GtkScrolledWindow
130  | | | `GtkViewport
131  | | +GtkBox
132  | | | +GtkButtonBox
133  | | | | +GtkHButtonBox
134  | | | | `GtkVButtonBox
135  | | | +GtkVBox
136  | | | | +GtkColorSelection
137  | | | | `GtkGammaCurve
138  | | | `GtkHBox
139  | | |   +GtkCombo
140  | | |   `GtkStatusbar
141  | | +GtkCList
142  | | | `GtkCTree
143  | | +GtkFixed
144  | | +GtkNotebook
145  | | | `GtkFontSelection
146  | | +GtkPaned
147  | | | +GtkHPaned
148  | | | `GtkVPaned
149  | | +GtkLayout
150  | | +GtkList
151  | | +GtkMenuShell
152  | | | +GtkMenuBar
153  | | | `GtkMenu
154  | | +GtkPacker
155  | | +GtkSocket
156  | | +GtkTable
157  | | +GtkToolbar
158  | | `GtkTree
159  | +GtkCalendar
160  | +GtkDrawingArea
161  | | `GtkCurve
162  | +GtkEditable
163  | | +GtkEntry
164  | | | `GtkSpinButton
165  | | `GtkText
166  | +GtkRuler
167  | | +GtkHRuler
168  | | `GtkVRuler
169  | +GtkRange
170  | | +GtkScale
171  | | | +GtkHScale
172  | | | `GtkVScale
173  | | `GtkScrollbar
174  | |   +GtkHScrollbar
175  | |   `GtkVScrollbar
176  | +GtkSeparator
177  | | +GtkHSeparator
178  | | `GtkVSeparator
179  | +GtkPreview
180  | `GtkProgress
181  |   `GtkProgressBar
182  +GtkData
183  | +GtkAdjustment
184  | `GtkTooltips
185  `GtkItemFactory
186</PRE>
187</CODE></BLOCKQUOTE>
188<P>
189<H2><A NAME="ss5.3">5.3 Widgets Without Windows</A>
190</H2>
191
192<P>The following widgets do not have an associated window. If you want to
193capture events, you'll have to use the EventBox. See the section on
194the
195<A HREF="gtk_tut-10.html#sec_EventBox">EventBox</A> widget.
196<P>
197<BLOCKQUOTE><CODE>
198<PRE>
199GtkAlignment
200GtkArrow
201GtkBin
202GtkBox
203GtkImage
204GtkItem
205GtkLabel
206GtkPixmap
207GtkScrolledWindow
208GtkSeparator
209GtkTable
210GtkAspectFrame
211GtkFrame
212GtkVBox
213GtkHBox
214GtkVSeparator
215GtkHSeparator
216</PRE>
217</CODE></BLOCKQUOTE>
218<P>We'll further our exploration of GTK by examining each widget in turn,
219creating a few simple functions to display them. Another good source
220is the testgtk.c program that comes with GTK. It can be found in
221gtk/testgtk.c.
222<P>
223<HR NOSHADE>
224<A HREF="gtk_tut-6.html">Next</A>
225<A HREF="gtk_tut-4.html">Previous</A>
226<A HREF="gtk_tut.html#toc5">Contents</A>
227</BODY>
228</HTML>
Note: See TracBrowser for help on using the repository browser.