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. |
---|
20 | These are all detailed in this section. |
---|
21 | </LI> |
---|
22 | <LI> Connect all signals and events we wish to use to the |
---|
23 | appropriate 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 |
---|
28 | such 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 |
---|
33 | attributes of the widget, and it is ready to be displayed. You may |
---|
34 | also use gtk_widget_hide to make it disappear again. The order in |
---|
35 | which you show the widgets is not important, but I suggest showing the |
---|
36 | window last so the whole window pops up at once rather than seeing the |
---|
37 | individual widgets come up on the screen as they're formed. The |
---|
38 | children of a widget (a window is a widget too) will not be displayed |
---|
39 | until 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 |
---|
45 | is always done using macros that both test the ability to cast the |
---|
46 | given 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 |
---|
59 | examples, and can usually tell when to use them simply by looking at the |
---|
60 | function's declaration. |
---|
61 | <P>As you can see below in the class hierarchy, all GtkWidgets are |
---|
62 | derived from the Object base class. This means you can use a widget |
---|
63 | in 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> |
---|
69 | gtk_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 |
---|
75 | function pointer to the callback. |
---|
76 | <P>Many widgets are also containers. If you look in the class hierarchy |
---|
77 | below, you'll notice that many widgets derive from the Container |
---|
78 | class. Any one of these widgets may be used with the |
---|
79 | <CODE>GTK_CONTAINER</CODE> macro to pass them to functions that ask for |
---|
80 | containers. |
---|
81 | <P>Unfortunately, these macros are not extensively covered in the |
---|
82 | tutorial, but I recommend taking a look through the GTK header |
---|
83 | files. It can be very educational. In fact, it's not difficult to |
---|
84 | learn 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 |
---|
193 | capture events, you'll have to use the EventBox. See the section on |
---|
194 | the |
---|
195 | <A HREF="gtk_tut-10.html#sec_EventBox">EventBox</A> widget. |
---|
196 | <P> |
---|
197 | <BLOCKQUOTE><CODE> |
---|
198 | <PRE> |
---|
199 | GtkAlignment |
---|
200 | GtkArrow |
---|
201 | GtkBin |
---|
202 | GtkBox |
---|
203 | GtkImage |
---|
204 | GtkItem |
---|
205 | GtkLabel |
---|
206 | GtkPixmap |
---|
207 | GtkScrolledWindow |
---|
208 | GtkSeparator |
---|
209 | GtkTable |
---|
210 | GtkAspectFrame |
---|
211 | GtkFrame |
---|
212 | GtkVBox |
---|
213 | GtkHBox |
---|
214 | GtkVSeparator |
---|
215 | GtkHSeparator |
---|
216 | </PRE> |
---|
217 | </CODE></BLOCKQUOTE> |
---|
218 | <P>We'll further our exploration of GTK by examining each widget in turn, |
---|
219 | creating a few simple functions to display them. Another good source |
---|
220 | is the testgtk.c program that comes with GTK. It can be found in |
---|
221 | gtk/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> |
---|