]> Pileus Git - ~andy/gtk/blob - docs/tutorial/gtk_tut.sgml
reflect the change of meaning of the delete_event return value
[~andy/gtk] / docs / tutorial / gtk_tut.sgml
1 <!doctype linuxdoc system>
2
3 <!-- This is the tutorial marked up in SGML
4      (just to show how to write a comment)
5 -->
6
7 <article>
8 <title>GTK Tutorial
9 <author>Ian Main <tt><htmlurl url="mailto:imain@gtk.org"
10                               name="&lt;imain@gtk.org&gt;"></tt>,
11 Tony Gale <tt><htmlurl url="mailto:gale@gtk.org"
12                               name="&lt;gale@gtk.org&gt;"></tt
13 <date>March 29th, 1998
14
15 <!-- ***************************************************************** -->
16 <sect>Introduction
17 <!-- ***************************************************************** -->
18 <p>
19 GTK (GIMP Toolkit) was originally developed as a toolkit for the GIMP
20 (General Image Manipulation Program).  GTK is built on top of GDK (GIMP
21 Drawing Kit)  which is basically wrapper around the Xlib functions.  It's
22 called the GIMP toolkit because it was original written for developing
23 the GIMP, but has now been used in several free software projects.  The
24 authors are
25 <itemize>
26 <item> Peter Mattis   <tt><htmlurl url="mailto:petm@xcf.berkeley.edu"
27                            name="petm@xcf.berkeley.edu"></tt>
28 <item> Spencer Kimball <tt><htmlurl url="mailto:spencer@xcf.berkeley.edu"
29                            name="spencer@xcf.berkeley.edu"></tt>
30 <item> Josh MacDonald <tt><htmlurl url="mailto:jmacd@xcf.berkeley.edu"
31                            name="jmacd@xcf.berkeley.edu"></tt>
32 </itemize>
33
34 <p>
35 GTK is essentially an object oriented application programmers interface (API).  
36 Although written completely in
37 C, it is implemented using the idea of classes and callback functions
38 (pointers to functions).
39 <p>
40 There is also a third component called glib which contains a few
41 replacements for some standard calls, as well as some additional functions
42 for handling linked lists etc.  The replacement functions are used to 
43 increase GTK's portability, as some of the functions implemented 
44 here are not available or are nonstandard on other unicies such as 
45 g_strerror().   Some also contain enhancements to the libc versions, such as
46 g_malloc that has enhanced debugging utilities.
47 <p>
48 This tutorial is an attempt to document as much as possible of GTK, it is by 
49 no means complete.  This
50 tutorial assumes a good understanding of C, and how to create C programs.
51 It would be a great benefit for the reader to have previous X programming
52 experience, but it shouldn't be necessary.  If you are learning GTK as your
53 first widget set, please comment on how you found this tutorial, and what
54 you had trouble with.
55 Note that there is also a C++ API for GTK (GTK--) in the works, so if you
56 prefer to use C++, you should look into this instead.  There's also an
57 Objective C wrapper, and guile bindings available, but I don't follow these.
58 <p>
59 I would very much like to hear any problems you have learning GTK from this
60 document, and would appreciate input as to how it may be improved.
61
62 <!-- ***************************************************************** -->
63 <sect>Getting Started
64 <!-- ***************************************************************** -->
65
66 <p>
67 The first thing to do of course, is download the GTK source and install
68 it.  You can always get the latest version from ftp.gtk.org in /pub/gtk.
69 You can also view other sources of GTK information on http://www.gtk.org/
70 <htmlurl url="http://www.gtk.org/" name="http://www.gtk.org/">.
71 GTK uses GNU autoconf for
72 configuration.  Once untar'd, type ./configure --help to see a list of options.
73 <p>
74 To begin our introduction to GTK, we'll start with the simplest program 
75 possible.  This program will
76 create a 200x200 pixel window and has no way of exiting except to be
77 killed using the shell.
78
79 <tscreen><verb>
80 #include <gtk/gtk.h>
81
82 int main (int argc, char *argv[])
83 {
84     GtkWidget *window;
85     
86     gtk_init (&amp;argc, &amp;argv);
87     
88     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
89     gtk_widget_show (window);
90     
91     gtk_main ();
92     
93     return 0;
94 }
95 </verb></tscreen>
96
97 All programs will of course include the gtk/gtk.h which declares the
98 variables, functions, structures etc. that will be used in your GTK 
99 application.
100 <p>
101 The next line:
102
103 <tscreen><verb>
104 gtk_init (&amp;argc, &amp;argv);
105 </verb></tscreen>
106
107 calls the function gtk_init(gint *argc, gchar ***argv) which will be
108 called in all GTK applications.  This sets up a few things for us such
109 as the default visual and color map and then proceeds to call
110 gdk_init(gint *argc, gchar ***argv).  This function initializes the
111 library for use, sets up default signal handlers, and checks the
112 arguments passed to your application on the command line, looking for one
113 of the following:
114
115 <itemize>
116 <item> <tt/--display/
117 <item> <tt/--debug-level/
118 <item> <tt/--no-xshm/
119 <item> <tt/--sync/
120 <item> <tt/--show-events/
121 <item> <tt/--no-show-events/
122 </itemize>
123 <p>
124 It removes these from the argument list, leaving anything it does
125 not recognize for your application to parse or ignore.  This creates a set
126 of standard arguments accepted by all GTK applications.
127 <p>
128 The next two lines of code create and display a window.
129
130 <tscreen><verb>
131   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
132   gtk_widget_show (window);
133 </verb></tscreen>
134
135 The GTK_WINDOW_TOPLEVEL argument specifies that we want the window to
136 undergo window manager decoration and placement. Rather than create a
137 window of 0x0 size, a window without children is set to 200x200 by default
138 so you can still manipulate it.
139 <p>
140 The gtk_widget_show() function, lets GTK know that we are done setting the
141 attributes of this widget, and it can display it.
142 <p>
143 The last line enters the GTK main processing loop.
144
145 <tscreen><verb>
146 gtk_main ();
147 </verb></tscreen>
148
149 gtk_main() is another call you will see in every GTK application.  When
150 control reaches this point, GTK will sleep waiting for X events (such as
151 button or key presses), timeouts, or file IO notifications to occur.
152 In our simple example however, events are ignored.
153
154 <!-- ----------------------------------------------------------------- -->
155 <sect1>Hello World in GTK
156 <p>
157 OK, now for a program with a widget (a button).  It's the classic hello
158 world ala GTK.
159
160 <tscreen><verb>
161 /* helloworld.c */
162
163 #include <gtk/gtk.h>
164
165 /* this is a callback function. the data arguments are ignored in this example..
166  * More on callbacks below. */
167 void hello (GtkWidget *widget, gpointer data)
168 {
169     g_print ("Hello World\n");
170 }
171
172 gint delete_event(GtkWidget *widget, gpointer data)
173 {
174     g_print ("delete event occured\n");
175     /* if you return FALSE in the "delete_event" signal handler,
176      * GTK will emit the "destroy" signal.  Returning TRUE means
177      * you don't want the window to be destroyed.
178      * This is useful for popping up 'are you sure you want to quit ?'
179      * type dialogs. */
180
181     /* Change TRUE to FALSE and the main window will be destroyed with
182      * a "delete_event". */
183
184     return (TRUE);
185 }
186
187 /* another callback */
188 void destroy (GtkWidget *widget, gpointer data)
189 {
190     gtk_main_quit ();
191 }
192
193 int main (int argc, char *argv[])
194 {
195     /* GtkWidget is the storage type for widgets */
196     GtkWidget *window;
197     GtkWidget *button;
198     
199     /* this is called in all GTK applications.  arguments are parsed from
200      * the command line and are returned to the application. */
201     gtk_init (&amp;argc, &amp;argv);
202     
203     /* create a new window */
204     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
205     
206     /* when the window is given the "delete_event" signal (this is given
207     * by the window manager (usually the 'close' option, or on the
208     * titlebar), we ask it to call the delete_event () function
209     * as defined above.  The data passed to the callback
210     * function is NULL and is ignored in the callback. */
211     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
212                         GTK_SIGNAL_FUNC (delete_event), NULL);
213     
214     /* here we connect the "destroy" event to a signal handler.  
215      * This event occurs when we call gtk_widget_destroy() on the window,
216      * or if we return 'TRUE' in the "delete_event" callback. */
217     gtk_signal_connect (GTK_OBJECT (window), "destroy",
218                         GTK_SIGNAL_FUNC (destroy), NULL);
219     
220     /* sets the border width of the window. */
221     gtk_container_border_width (GTK_CONTAINER (window), 10);
222     
223     /* creates a new button with the label "Hello World". */
224     button = gtk_button_new_with_label ("Hello World");
225     
226     /* When the button receives the "clicked" signal, it will call the
227      * function hello() passing it NULL as it's argument.  The hello() function is
228      * defined above. */
229     gtk_signal_connect (GTK_OBJECT (button), "clicked",
230                         GTK_SIGNAL_FUNC (hello), NULL);
231     
232     /* This will cause the window to be destroyed by calling
233      * gtk_widget_destroy(window) when "clicked".  Again, the destroy
234      * signal could come from here, or the window manager. */
235     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
236                                GTK_SIGNAL_FUNC (gtk_widget_destroy),
237                                GTK_OBJECT (window));
238     
239     /* this packs the button into the window (a gtk container). */
240     gtk_container_add (GTK_CONTAINER (window), button);
241     
242     /* the final step is to display this newly created widget... */
243     gtk_widget_show (button);
244     
245     /* and the window */
246     gtk_widget_show (window);
247     
248     /* all GTK applications must have a gtk_main().     Control ends here
249      * and waits for an event to occur (like a key press or mouse event). */
250     gtk_main ();
251     
252     return 0;
253 }
254 </verb></tscreen>
255
256 <!-- ----------------------------------------------------------------- -->
257 <sect1>Compiling Hello World
258 <p>
259 To compile use:
260
261 <tscreen><verb>
262 gcc -Wall -g helloworld.c -o hello_world `gtk-config --cflags` \
263     `gtk-config --libs`
264 </verb></tscreen>
265 <p>
266
267 This uses the program <tt>gtk-config</>, which comes with gtk. This
268 program 'knows' what compiler switches are needed to compile programs
269 that use gtk. <tt>gtk-config --cflags</> will output a list of include
270 directories for the compiler to look in, and <tt>gtk-config --libs</>
271 will output the list of libraries for the compiler to link with and
272 the directories to find them in.
273
274 <p>
275 The libraries that are usually linked in are:
276 <itemize>
277 <item>The GTK library (-lgtk), the widget library, based on top of GDK.
278 <item>The GDK library (-lgdk), the Xlib wrapper.
279 <item>The glib library (-lglib), containing miscellaneous functions, only
280 g_print() is used in this particular example.  GTK is built on top
281 of glib so you will always require this library.  See the section on 
282 <ref id="sec_glib" name="glib"> for details.  
283 <item>The Xlib library (-lX11) which is used by GDK.
284 <item>The Xext library (-lXext).  This contains code for shared memory
285 pixmaps and other X extensions.
286 <item>The math library (-lm).  This is used by GTK for various purposes.
287 </itemize>
288
289 <!-- ----------------------------------------------------------------- -->
290 <sect1>Theory of Signals and Callbacks
291 <p>
292 Before we look in detail at hello world, we'll discuss events and callbacks.
293 GTK is an event driven toolkit, which means it will sleep in
294 gtk_main until an event occurs and control is passed to the appropriate
295 function.
296 <p>
297 This passing of control is done using the idea of "signals".  When an 
298 event occurs, such as the press of a mouse button, the
299 appropriate signal will be "emitted" by the widget that was pressed.  
300 This is how GTK does
301 most of its useful work.  To make a button perform an action, 
302 we set up a signal handler to catch these
303 signals and call the appropriate function.  This is done by using a 
304 function such as:
305
306 <tscreen><verb>
307 gint gtk_signal_connect (GtkObject *object,
308                          gchar *name,
309                          GtkSignalFunc func,
310                          gpointer func_data);
311 </verb></tscreen>
312 <p>
313 Where the first argument is the widget which will be emitting the signal, and
314 the second, the name of the signal you wish to catch.  The third is the function
315 you wish to be called when it is caught, and the fourth, the data you wish
316 to have passed to this function.
317 <p>
318 The function specified in the third argument is called a "callback
319 function", and should be of the form:
320
321 <tscreen><verb>
322 void callback_func(GtkWidget *widget, gpointer *callback_data);
323 </verb></tscreen>
324 <p>
325 Where the first argument will be a pointer to the widget that emitted the signal, and
326 the second, a pointer to the data given as the last argument to the
327 gtk_signal_connect() function as shown above.
328 <p>
329 Another call used in the hello world example, is:
330
331 <tscreen><verb>
332 gint gtk_signal_connect_object (GtkObject *object,
333                                 gchar  *name,
334                                 GtkSignalFunc func,
335                                 GtkObject *slot_object);
336 </verb></tscreen>
337 <p>
338 gtk_signal_connect_object() is the same as gtk_signal_connect() except that
339 the callback function only uses one argument, a
340 pointer to a GTK 
341 object.  So when using this function to connect signals, the callback should be of
342 the form:
343
344 <tscreen><verb>
345 void callback_func (GtkObject *object);
346 </verb></tscreen>
347 <p>
348 Where the object is usually a widget.  We usually don't setup callbacks for
349 gtk_signal_connect_object however.  They are usually used 
350 to call a GTK function that accept a single widget or object as an
351 argument, as is the case in our hello world example.
352
353 The purpose of having two functions to connect signals is simply to allow
354 the callbacks to have a different number of arguments.  Many functions in
355 the GTK library accept only a single GtkWidget pointer as an argument, so you
356 want to use the gtk_signal_connect_object() for these, whereas for your 
357 functions, you may need to have additional data supplied to the callbacks.
358
359 <!-- ----------------------------------------------------------------- -->
360 <sect1>Stepping Through Hello World
361 <p>
362 Now that we know the theory behind this, lets clarify by walking through 
363 the example hello world program.
364 <p>
365 Here is the callback function that will be called when the button is
366 "clicked".  We ignore both the widget and the data in this example, but it 
367 is not hard to do things with them.  The next example will use the data 
368 argument to tell us which button was pressed.
369
370 <tscreen><verb>
371 void hello (GtkWidget *widget, gpointer *data)
372 {
373     g_print ("Hello World\n");
374 }
375 </verb></tscreen>
376
377 <p>
378 This callback is a bit special.  The "delete_event" occurs when the
379 window manager sends this event to the application.  We have a choice here
380 as to what to do about these events.  We can ignore them, make some sort of
381 response, or simply quit the application.
382
383 The value you return in this callback lets GTK know what action to take.
384 By returning TRUE, we let it know that we don't want to have the "destroy"
385 signal emitted, keeping our application running.  By returning FALSE, we 
386 ask that "destroy" is emitted, which in turn will call our "destroy" 
387 signal handler.
388
389 <tscreen><verb>
390 gint delete_event(GtkWidget *widget, gpointer data)
391 {
392     g_print ("delete event occured\n");
393
394     return (TRUE); 
395 }
396 </verb></tscreen>
397
398 <p>
399 Here is another callback function which causes the program to quit by calling
400 gtk_main_quit().  This function tells GTK that it is to exit from gtk_main
401 when control is returned to it.
402
403 <tscreen><verb>
404 void destroy (GtkWidget *widget, gpointer *data)
405 {
406     gtk_main_quit ();
407 }
408 </verb></tscreen>
409
410 <p>
411 I assume you know about the main() function... yes, as with other
412 applications, all GTK applications will also have one of these.
413 <tscreen><verb>
414 int main (int argc, char *argv[])
415 {
416 </verb></tscreen>
417 <p>
418 This next part, declares a pointer to a structure of type GtkWidget.  These
419 are used below to create a window and a button.
420 <tscreen><verb>
421     GtkWidget *window;
422     GtkWidget *button;
423 </verb></tscreen>
424 <p>
425 Here is our gtk_init again.  As before, this initializes the toolkit, and
426 parses the arguments found on the command line.  Any argument it
427 recognizes from the command line, it removes from the list, and modifies
428 argc and argv to make it look like they never existed, allowing your
429 application to parse the remaining arguments.
430 <tscreen><verb>
431     gtk_init (&amp;argc, &amp;argv);
432 </verb></tscreen>
433 <p>
434 Create a new window.  This is fairly straight forward.  Memory is allocated
435 for the GtkWidget *window structure so it now points to a valid structure.
436 It sets up a new window, but it is not displayed until below where we call
437 gtk_widget_show(window) near the end of our program.
438 <tscreen><verb>
439     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
440 </verb></tscreen>
441 <p>
442 Here is an example of connecting a signal handler to an object, in this case, the
443 window.  Here, the "destroy" signal is caught.  This is emitted when we use
444 the window manager to kill the window (and we return TRUE in the
445 "delete_event" handler), or when we use the
446 gtk_widget_destroy() call passing in the window widget as the object to
447 destroy.  By setting this up, we handle both cases with a single call.
448 Here, it just calls the destroy() function defined above with a NULL
449 argument, which quits GTK for us.  
450 <p>
451 The GTK_OBJECT and GTK_SIGNAL_FUNC are macros that perform type casting and
452 checking for us, as well as aid the readability of the code.
453 <tscreen><verb>
454     gtk_signal_connect (GTK_OBJECT (window), "destroy",
455                         GTK_SIGNAL_FUNC (destroy), NULL);
456 </verb></tscreen>
457 <p>
458 This next function is used to set an attribute of a container object.  
459 This just sets the window
460 so it has a blank area along the inside of it 10 pixels wide where no
461 widgets will go.  There are other similar functions which we will look at 
462 in the section on 
463 <ref id="sec_setting_widget_attributes" name="Setting Widget Attributes">
464 <p>
465 And again, GTK_CONTAINER is a macro to perform type casting.
466 <tscreen><verb>
467     gtk_container_border_width (GTK_CONTAINER (window), 10);
468 </verb></tscreen>
469 <p>
470 This call creates a new button.  It allocates space for a new GtkWidget
471 structure in memory, initializes it, and makes the button pointer point to
472 it.  It will have the label "Hello World" on it when displayed.
473 <tscreen><verb>
474     button = gtk_button_new_with_label ("Hello World");
475 </verb></tscreen>
476 <p>
477 Here, we take this button, and make it do something useful.  We attach a
478 signal handler to it so when it emits the "clicked" signal, our hello()
479 function is called.  The data is ignored, so we simply pass in NULL to the
480 hello() callback function.  Obviously, the "clicked" signal is emitted when
481 we click the button with our mouse pointer.
482
483 <tscreen><verb>
484     gtk_signal_connect (GTK_OBJECT (button), "clicked",
485                         GTK_SIGNAL_FUNC (hello), NULL);
486 </verb></tscreen>
487 <p>
488 We are also going to use this button to exit our program.  This will
489 illustrate how the "destroy"
490 signal may come from either the window manager, or our program.  When the
491 button is "clicked", same as above, it calls the first hello() callback function,
492 and then this one in the order they are set up.  You may have as many
493 callback function as you need, and all will be executed in the order you
494 connected them.  Because the gtk_widget_destroy() function accepts only a
495 GtkWidget *widget as an argument, we use the gtk_signal_connect_object()
496 function here instead of straight gtk_signal_connect().
497
498 <tscreen><verb>
499     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
500                                GTK_SIGNAL_FUNC (gtk_widget_destroy),
501                                GTK_OBJECT (window));
502 </verb></tscreen>
503 <p>
504 This is a packing call, which will be explained in depth later on.  But it
505 is fairly easy to understand.  It simply tells GTK that the button is to be 
506 placed in the window where it will be displayed.
507 <tscreen><verb>
508     gtk_container_add (GTK_CONTAINER (window), button);
509 </verb></tscreen>
510 <p>
511 Now that we have everything setup the way we want it to be.  With all the
512 signal handlers in place, and the button placed in the window where it
513 should be, we ask GTK to "show" the widgets on the screen.  The window
514 widget is shown last so the whole window will pop up at once rather than
515 seeing the window pop up, and then the button form inside of it.  Although
516 with such simple example, you'd never notice.
517 <tscreen><verb>
518     gtk_widget_show (button);
519
520     gtk_widget_show (window);
521 </verb></tscreen>
522 <p>
523 And of course, we call gtk_main() which waits for events to come from the X
524 server and will call on the widgets to emit signals when these events come.
525 <tscreen><verb>
526     gtk_main ();
527 </verb></tscreen>
528 And the final return.  Control returns here after gtk_quit() is called.
529 <tscreen><verb>
530     return 0;
531 </verb></tscreen>
532 <p>
533 Now, when we click the mouse button on a GTK button, the
534 widget emits a "clicked" signal.  In order for us to use this information, our
535 program sets up a signal handler to catch that signal, which dispatches the function 
536 of our choice. In our example, when the button we created is "clicked", the 
537 hello() function is called with a NULL
538 argument, and then the next handler for this signal is called.  This calls
539 the gtk_widget_destroy() function, passing it the window widget as it's
540 argument, destroying the window widget.  This causes the window to emit the 
541 "destroy" signal, which is
542 caught, and calls our destroy() callback function, which simply exits GTK.
543 <p>
544 Another course of events, is to use the window manager to kill the window.
545 This will cause the "delete_event" to be emitted.  This will call our
546 "delete_event" handler.  If we return TRUE here, the window will be left as
547 is and nothing will happen.  Returning FALSE will cause GTK to emit the
548 "destroy" signal which of course, calls the "destroy" callback, exiting GTK.
549 <p>
550 Note that these signals are not the same as the Unix system
551 signals, and are not implemented using them, although the terminology is
552 almost identical.
553
554 <!-- ***************************************************************** -->
555 <sect>Moving On
556 <!-- ***************************************************************** -->
557
558 <!-- ----------------------------------------------------------------- -->
559 <sect1>Data Types
560 <p>
561 There are a few things you probably noticed in the previous examples that
562 need explaining.  The 
563 gint, gchar etc. that you see are typedefs to int and char respectively.  This is done
564 to get around that nasty dependency on the size of simple data types when doing calculations.
565 A good example is "gint32" which will be
566 typedef'd to a 32 bit integer for any given platform, whether it be the 64 bit
567 alpha, or the 32 bit i386.  The
568 typedefs are very straight forward and intuitive.  They are all defined in
569 glib/glib.h (which gets included from gtk.h).
570 <p>
571 You'll also notice the ability to use GtkWidget when the function calls for a GtkObject.  
572 GTK is an object oriented design, and a widget is an object.
573
574 <!-- ----------------------------------------------------------------- -->
575 <sect1>More on Signal Handlers
576 <p>
577 Lets take another look at the gtk_signal_connect declaration.
578
579 <tscreen><verb>
580 gint gtk_signal_connect (GtkObject *object, gchar *name,
581                          GtkSignalFunc func, gpointer func_data);
582 </verb></tscreen>
583
584 Notice the gint return value ?  This is a tag that identifies your callback
585 function.  As said above, you may have as many callbacks per signal and per
586 object as you need, and each will be executed in turn, in the order they 
587 were attached.  
588
589 This tag allows you to remove this callback from the list by using:
590 <tscreen><verb>
591 void gtk_signal_disconnect (GtkObject *object,
592                             gint id);
593 </verb></tscreen>
594 So, by passing in the widget you wish to remove the handler from, and the
595 tag or id returned by one of the signal_connect functions, you can
596 disconnect a signal handler.
597
598 Another function to remove all the signal handers from an object is:
599
600 <tscreen><verb>
601 gtk_signal_handlers_destroy (GtkObject *object);
602 </verb></tscreen>
603
604 This call is fairly self explanatory.  It simply removes all the current
605 signal handlers from the object passed in as the first argument.
606
607 <!-- ----------------------------------------------------------------- -->
608 <sect1>An Upgraded Hello World
609 <p>
610 Let's take a look at a slightly improved hello world with better examples
611 of callbacks.  This will also introduce us to our next topic, packing
612 widgets.
613
614 <tscreen><verb>
615 /* helloworld2.c */
616
617 #include <gtk/gtk.h>
618
619 /* Our new improved callback.  The data passed to this function is printed
620  * to stdout. */
621 void callback (GtkWidget *widget, gpointer *data)
622 {
623     g_print ("Hello again - %s was pressed\n", (char *) data);
624 }
625
626 /* another callback */
627 void delete_event (GtkWidget *widget, gpointer *data)
628 {
629     gtk_main_quit ();
630 }
631
632 int main (int argc, char *argv[])
633 {
634     /* GtkWidget is the storage type for widgets */
635     GtkWidget *window;
636     GtkWidget *button;
637     GtkWidget *box1;
638
639     /* this is called in all GTK applications.  arguments are parsed from
640      * the command line and are returned to the application. */
641     gtk_init (&amp;argc, &amp;argv);
642
643     /* create a new window */
644     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
645
646     /* this is a new call, this just sets the title of our
647      * new window to "Hello Buttons!" */
648     gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
649
650     /* Here we just set a handler for delete_event that immediately
651      * exits GTK. */
652     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
653                         GTK_SIGNAL_FUNC (delete_event), NULL);
654
655
656     /* sets the border width of the window. */
657     gtk_container_border_width (GTK_CONTAINER (window), 10);
658
659     /* we create a box to pack widgets into.  this is described in detail
660      * in the "packing" section below.  The box is not really visible, it
661      * is just used as a tool to arrange widgets. */
662     box1 = gtk_hbox_new(FALSE, 0);
663
664     /* put the box into the main window. */
665     gtk_container_add (GTK_CONTAINER (window), box1);
666
667     /* creates a new button with the label "Button 1". */
668     button = gtk_button_new_with_label ("Button 1");
669
670     /* Now when the button is clicked, we call the "callback" function
671      * with a pointer to "button 1" as it's argument */
672     gtk_signal_connect (GTK_OBJECT (button), "clicked",
673                         GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");
674
675     /* instead of gtk_container_add, we pack this button into the invisible
676      * box, which has been packed into the window. */
677     gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
678
679     /* always remember this step, this tells GTK that our preparation for
680      * this button is complete, and it can be displayed now. */
681     gtk_widget_show(button);
682
683     /* do these same steps again to create a second button */
684     button = gtk_button_new_with_label ("Button 2");
685
686     /* call the same callback function with a different argument,
687      * passing a pointer to "button 2" instead. */
688     gtk_signal_connect (GTK_OBJECT (button), "clicked",
689                         GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");
690
691     gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
692
693     /* The order in which we show the buttons is not really important, but I
694      * recommend showing the window last, so it all pops up at once. */
695     gtk_widget_show(button);
696
697     gtk_widget_show(box1);
698
699     gtk_widget_show (window);
700
701     /* rest in gtk_main and wait for the fun to begin! */
702     gtk_main ();
703
704     return 0;
705 }
706 </verb></tscreen>
707
708 Compile this program using the same linking arguments as our first example.
709 You'll notice this time there is no easy way to exit the program, you have 
710 to use your window manager or command line to kill it.  A good exercise 
711 for the reader would be to insert a third "Quit" button that will exit the
712 program.  You may also wish to play with the options to
713 gtk_box_pack_start() while reading the next section.  
714 Try resizing the window, and observe the behavior.
715
716 Just as a side note, there is another useful define for gtk_window_new() -
717 GTK_WINDOW_DIALOG.  This interacts with the window manager a little
718 differently and should be used for transient windows.
719
720 <!-- ***************************************************************** -->
721 <sect>Packing Widgets
722 <!-- ***************************************************************** -->
723
724 <p>
725 When creating an application, you'll want to put more than one button
726 inside a window.  Our first hello world example only used one widget so we
727 could simply use a gtk_container_add call to "pack" the widget into the
728 window.  But when you want to put more than one widget into a window, how
729 do you control where that widget is positioned ?  This is where packing
730 comes in.
731
732 <!-- ----------------------------------------------------------------- -->
733 <sect1>Theory of Packing Boxes
734 <p>
735 Most packing is done by creating boxes as in the example above.  These are
736 invisible widget containers that we can pack our widgets into and come in
737 two forms, a horizontal box, and a vertical box.  When packing widgets
738 into a horizontal box, the objects are inserted horizontally from left to
739 right or right to left depending on the call used. In a vertical box,
740 widgets are packed from top to bottom or vice versa.  You may use any
741 combination of boxes inside or beside other boxes to create the desired
742 effect.
743 <p>
744 To create a new horizontal box, we use a call to gtk_hbox_new(), and for
745 vertical boxes, gtk_vbox_new().  The gtk_box_pack_start() and
746 gtk_box_pack_end() functions are used to place objects inside of these
747 containers.  The gtk_box_pack_start() function will start at the top and
748 work its way down in a vbox, and pack left to right in an hbox.
749 gtk_box_pack_end() will do the opposite, packing from bottom to top in a
750 vbox, and right to left in an hbox.  Using these functions allow us to
751 right justify or left justify our widgets and may be mixed in any way to
752 achieve the desired effect.  We will use gtk_box_pack_start() in most of
753 our examples.  An object may be another container or a widget.  And in
754 fact, many widgets are actually containers themselves including the
755 button, but we usually only use a label inside a button.
756 <p>
757 By using these calls, GTK knows where you want to place your widgets so it
758 can do automatic resizing and other nifty things.  there's also a number
759 of options as to how your widgets should be packed. As you can imagine,
760 this method gives us a quite a bit of flexibility when placing and
761 creating widgets.
762
763 <!-- ----------------------------------------------------------------- -->
764 <sect1>Details of Boxes
765 <p>
766 Because of this flexibility, packing boxes in GTK can be confusing at
767 first. There are a lot of options, and it's not immediately obvious how
768 they all fit together.  In the end however, there are basically five
769 different styles you can get.
770
771 <p>
772 <? <CENTER> >
773 <?
774 <IMG SRC="gtk_tut_packbox1.gif" VSPACE="15" HSPACE="10" WIDTH="528" HEIGHT="235"
775 ALT="Box Packing Example Image">
776 >
777 <? </CENTER> >
778
779 Each line contains one horizontal box (hbox) with several buttons. The
780 call to gtk_box_pack is shorthand for the call to pack each of the buttons
781 into the hbox. Each of the buttons is packed into the hbox the same way
782 (i.e. same arguments to the gtk_box_pack_start () function).
783 <p>
784 This is the declaration of the gtk_box_pack_start function.
785
786 <tscreen><verb>
787 void gtk_box_pack_start (GtkBox    *box,
788                          GtkWidget *child,
789                          gint       expand,
790                          gint       fill,
791                          gint       padding);
792 </verb></tscreen>
793
794 The first argument is the box you are packing the object into, the second
795 is this object.  The objects will all be buttons for now, so we'll be
796 packing buttons into boxes.
797 <p>
798 The expand argument to gtk_box_pack_start() or gtk_box_pack_end() controls
799 whether the widgets are laid out in the box to fill in all the extra space
800 in the box so the box is expanded to fill the area alloted to it (TRUE).
801 Or the box is shrunk to just fit the widgets (FALSE).  Setting expand to
802 FALSE will allow you to do right and left
803 justifying of your widgets.  Otherwise, they will all expand to fit in the
804 box, and the same effect could be achieved by using only one of
805 gtk_box_pack_start or pack_end functions.
806 <p>
807 The fill argument to the gtk_box_pack functions control whether the extra
808 space is allocated to the objects themselves (TRUE), or as extra padding
809 in the box around these objects (FALSE). It only has an effect if the
810 expand argument is also TRUE.
811 <p>
812 When creating a new box, the function looks like this:
813
814 <tscreen><verb>
815 GtkWidget * gtk_hbox_new (gint homogeneous,
816                           gint spacing);
817 </verb></tscreen>
818
819 The homogeneous argument to gtk_hbox_new (and the same for gtk_vbox_new)
820 controls whether each object in the box has the same size (i.e. the same
821 width in an hbox, or the same height in a vbox). If it is set, the expand
822 argument to the gtk_box_pack routines is always turned on.
823 <p>
824 What's the difference between spacing (set when the box is created) and
825 padding (set when elements are packed)? Spacing is added between objects,
826 and padding is added on either side of an object.  The following figure
827 should make it clearer:
828
829 <? <CENTER> >
830 <?
831 <IMG ALIGN="center" SRC="gtk_tut_packbox2.gif" WIDTH="509" HEIGHT="213"
832 VSPACE="15" HSPACE="10" ALT="Box Packing Example Image">
833 >
834 <? </CENTER> >
835
836 Here is the code used to create the above images.  I've commented it fairly
837 heavily so hopefully you won't have any problems following it.  Compile it yourself
838 and play with it.
839
840 <!-- ----------------------------------------------------------------- -->
841 <sect1>Packing Demonstration Program
842 <p>
843 <tscreen><verb>
844 /* packbox.c */
845
846 #include "gtk/gtk.h"
847
848 void
849 delete_event (GtkWidget *widget, gpointer *data)
850 {
851     gtk_main_quit ();
852 }
853
854 /* Make a new hbox filled with button-labels. Arguments for the 
855  * variables we're interested are passed in to this function. 
856  * We do not show the box, but do show everything inside. */
857 GtkWidget *make_box (gint homogeneous, gint spacing,
858                      gint expand, gint fill, gint padding) 
859 {
860     GtkWidget *box;
861     GtkWidget *button;
862     char padstr[80];
863     
864     /* create a new hbox with the appropriate homogeneous and spacing
865      * settings */
866     box = gtk_hbox_new (homogeneous, spacing);
867     
868     /* create a series of buttons with the appropriate settings */
869     button = gtk_button_new_with_label ("gtk_box_pack");
870     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
871     gtk_widget_show (button);
872     
873     button = gtk_button_new_with_label ("(box,");
874     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
875     gtk_widget_show (button);
876     
877     button = gtk_button_new_with_label ("button,");
878     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
879     gtk_widget_show (button);
880     
881     /* create a button with the label depending on the value of
882      * expand. */
883     if (expand == TRUE)
884             button = gtk_button_new_with_label ("TRUE,");
885     else
886             button = gtk_button_new_with_label ("FALSE,");
887     
888     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
889     gtk_widget_show (button);
890     
891     /* This is the same as the button creation for "expand"
892      * above, but uses the shorthand form. */
893     button = gtk_button_new_with_label (fill ? "TRUE," : "FALSE,");
894     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
895     gtk_widget_show (button);
896     
897     sprintf (padstr, "%d);", padding);
898     
899     button = gtk_button_new_with_label (padstr);
900     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
901     gtk_widget_show (button);
902     
903     return box;
904 }
905
906 int
907 main (int argc, char *argv[])
908 {
909     GtkWidget *window;
910     GtkWidget *button;
911     GtkWidget *box1;
912     GtkWidget *box2;
913     GtkWidget *separator;
914     GtkWidget *label;
915     GtkWidget *quitbox;
916     int which;
917     
918     /* Our init, don't forget this! :) */
919     gtk_init (&amp;argc, &amp;argv);
920     
921     if (argc != 2) {
922         fprintf (stderr, "usage: packbox num, where num is 1, 2, or 3.\n");
923         /* this just does cleanup in GTK, and exits with an exit status of 1. */
924         gtk_exit (1);
925     }
926     
927     which = atoi (argv[1]);
928
929     /* Create our window */
930     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
931
932     /* You should always remember to connect the destroy signal to the
933      * main window.  This is very important for proper intuitive
934      * behavior */
935     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
936                         GTK_SIGNAL_FUNC (delete_event), NULL);
937     gtk_container_border_width (GTK_CONTAINER (window), 10);
938     
939     /* We create a vertical box (vbox) to pack the horizontal boxes into.
940      * This allows us to stack the horizontal boxes filled with buttons one
941      * on top of the other in this vbox. */
942     box1 = gtk_vbox_new (FALSE, 0);
943     
944     /* which example to show.  These correspond to the pictures above. */
945     switch (which) {
946     case 1:
947         /* create a new label. */
948         label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
949         
950         /* Align the label to the left side.  We'll discuss this function and 
951          * others in the section on Widget Attributes. */
952         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
953
954         /* Pack the label into the vertical box (vbox box1).  Remember that 
955          * widgets added to a vbox will be packed one on top of the other in
956          * order. */
957         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
958         
959         /* show the label */
960         gtk_widget_show (label);
961         
962         /* call our make box function - homogeneous = FALSE, spacing = 0,
963          * expand = FALSE, fill = FALSE, padding = 0 */
964         box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
965         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
966         gtk_widget_show (box2);
967
968         /* call our make box function - homogeneous = FALSE, spacing = 0,
969          * expand = FALSE, fill = FALSE, padding = 0 */
970         box2 = make_box (FALSE, 0, TRUE, FALSE, 0);
971         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
972         gtk_widget_show (box2);
973         
974         /* Args are: homogeneous, spacing, expand, fill, padding */
975         box2 = make_box (FALSE, 0, TRUE, TRUE, 0);
976         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
977         gtk_widget_show (box2);
978         
979         /* creates a separator, we'll learn more about these later, 
980          * but they are quite simple. */
981         separator = gtk_hseparator_new ();
982         
983         /* pack the separator into the vbox.  Remember each of these
984          * widgets are being packed into a vbox, so they'll be stacked
985          * vertically. */
986         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
987         gtk_widget_show (separator);
988         
989         /* create another new label, and show it. */
990         label = gtk_label_new ("gtk_hbox_new (TRUE, 0);");
991         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
992         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
993         gtk_widget_show (label);
994         
995         /* Args are: homogeneous, spacing, expand, fill, padding */
996         box2 = make_box (TRUE, 0, TRUE, FALSE, 0);
997         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
998         gtk_widget_show (box2);
999         
1000         /* Args are: homogeneous, spacing, expand, fill, padding */
1001         box2 = make_box (TRUE, 0, TRUE, TRUE, 0);
1002         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1003         gtk_widget_show (box2);
1004         
1005         /* another new separator. */
1006         separator = gtk_hseparator_new ();
1007         /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1008         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1009         gtk_widget_show (separator);
1010         
1011         break;
1012
1013     case 2:
1014
1015         /* create a new label, remember box1 is a vbox as created 
1016          * near the beginning of main() */
1017         label = gtk_label_new ("gtk_hbox_new (FALSE, 10);");
1018         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1019         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
1020         gtk_widget_show (label);
1021         
1022         /* Args are: homogeneous, spacing, expand, fill, padding */
1023         box2 = make_box (FALSE, 10, TRUE, FALSE, 0);
1024         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1025         gtk_widget_show (box2);
1026         
1027         /* Args are: homogeneous, spacing, expand, fill, padding */
1028         box2 = make_box (FALSE, 10, TRUE, TRUE, 0);
1029         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1030         gtk_widget_show (box2);
1031         
1032         separator = gtk_hseparator_new ();
1033         /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1034         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1035         gtk_widget_show (separator);
1036         
1037         label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
1038         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1039         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
1040         gtk_widget_show (label);
1041         
1042         /* Args are: homogeneous, spacing, expand, fill, padding */
1043         box2 = make_box (FALSE, 0, TRUE, FALSE, 10);
1044         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1045         gtk_widget_show (box2);
1046         
1047         /* Args are: homogeneous, spacing, expand, fill, padding */
1048         box2 = make_box (FALSE, 0, TRUE, TRUE, 10);
1049         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1050         gtk_widget_show (box2);
1051         
1052         separator = gtk_hseparator_new ();
1053         /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1054         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1055         gtk_widget_show (separator);
1056         break;
1057     
1058     case 3:
1059
1060     /* This demonstrates the ability to use gtk_box_pack_end() to
1061          * right justify widgets.  First, we create a new box as before. */
1062         box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
1063         /* create the label that will be put at the end. */
1064         label = gtk_label_new ("end");
1065         /* pack it using gtk_box_pack_end(), so it is put on the right side
1066          * of the hbox created in the make_box() call. */
1067         gtk_box_pack_end (GTK_BOX (box2), label, FALSE, FALSE, 0);
1068         /* show the label. */
1069         gtk_widget_show (label);
1070         
1071         /* pack box2 into box1 (the vbox remember ? :) */
1072         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1073         gtk_widget_show (box2);
1074         
1075         /* a separator for the bottom. */
1076         separator = gtk_hseparator_new ();
1077         /* this explicitly sets the separator to 400 pixels wide by 5 pixels
1078          * high.  This is so the hbox we created will also be 400 pixels wide,
1079          * and the "end" label will be separated from the other labels in the
1080          * hbox.  Otherwise, all the widgets in the hbox would be packed as
1081          * close together as possible. */
1082         gtk_widget_set_usize (separator, 400, 5);
1083         /* pack the separator into the vbox (box1) created near the start 
1084          * of main() */
1085         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1086         gtk_widget_show (separator);    
1087     }
1088     
1089     /* Create another new hbox.. remember we can use as many as we need! */
1090     quitbox = gtk_hbox_new (FALSE, 0);
1091     
1092     /* Our quit button. */
1093     button = gtk_button_new_with_label ("Quit");
1094     
1095     /* setup the signal to destroy the window.  Remember that this will send
1096      * the "destroy" signal to the window which will be caught by our signal
1097      * handler as defined above. */
1098     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1099                                GTK_SIGNAL_FUNC (gtk_main_quit),
1100                                GTK_OBJECT (window));
1101     /* pack the button into the quitbox.
1102      * The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1103     gtk_box_pack_start (GTK_BOX (quitbox), button, TRUE, FALSE, 0);
1104     /* pack the quitbox into the vbox (box1) */
1105     gtk_box_pack_start (GTK_BOX (box1), quitbox, FALSE, FALSE, 0);
1106     
1107     /* pack the vbox (box1) which now contains all our widgets, into the
1108      * main window. */
1109     gtk_container_add (GTK_CONTAINER (window), box1);
1110     
1111     /* and show everything left */
1112     gtk_widget_show (button);
1113     gtk_widget_show (quitbox);
1114     
1115     gtk_widget_show (box1);
1116     /* Showing the window last so everything pops up at once. */
1117     gtk_widget_show (window);
1118     
1119     /* And of course, our main function. */
1120     gtk_main ();
1121
1122     /* control returns here when gtk_main_quit() is called, but not when 
1123      * gtk_exit is used. */
1124     
1125     return 0;
1126 }
1127 </verb></tscreen>
1128
1129 <!-- ----------------------------------------------------------------- -->
1130 <sect1>Packing Using Tables
1131 <p>
1132 Let's take a look at another way of packing - Tables.  These can be
1133 extremely useful in certain situations.
1134
1135 Using tables, we create a grid that we can place widgets in.  The widgets
1136 may take up as many spaces as we specify.
1137
1138 The first thing to look at of course, is the gtk_table_new function:
1139
1140 <tscreen><verb>
1141 GtkWidget* gtk_table_new (gint rows,
1142                           gint columns,
1143                           gint homogeneous);
1144 </verb></tscreen>
1145 <p>
1146 The first argument is the number of rows to make in the table, while the
1147 second, obviously, the number of columns.
1148
1149 The homogeneous argument has to do with how the table's boxes are sized.  If homogeneous 
1150 is TRUE, the table boxes are resized to the size of the largest widget in the table.
1151 If homogeneous is FALSE, the size of a table boxes is dictated by the tallest widget 
1152 in its same row, and the widest widget in its column.
1153
1154 The rows and columnts are laid out starting with 0 to n, where n was the
1155 number specified in the call to gtk_table_new.  So, if you specify rows = 2 and 
1156 columns = 2, the layout would look something like this:
1157
1158 <tscreen><verb>
1159  0          1          2
1160 0+----------+----------+
1161  |          |          |
1162 1+----------+----------+
1163  |          |          |
1164 2+----------+----------+
1165 </verb></tscreen>
1166 <p>
1167 Note that the coordinate system starts in the upper left hand corner.  To place a 
1168 widget into a box, use the following function:
1169
1170 <tscreen><verb>
1171 void gtk_table_attach (GtkTable      *table,
1172                        GtkWidget     *child,
1173                        gint           left_attach,
1174                        gint           right_attach,
1175                        gint           top_attach,
1176                        gint           bottom_attach,
1177                        gint           xoptions,
1178                        gint           yoptions,
1179                        gint           xpadding,
1180                        gint           ypadding);
1181 </verb></tscreen>                                      
1182 <p>
1183 Where the first argument ("table") is the table you've created and the second
1184 ("child") the widget you wish to place in the table.
1185
1186 The left and right attach 
1187 arguments specify where to place the widget, and how many boxes to use.  If you want 
1188 a button in the lower right table entry 
1189 of our 2x2 table, and want it to fill that entry ONLY.  left_attach would be = 1, 
1190 right_attach = 2, top_attach = 1, bottom_attach = 2.
1191
1192 Now, if you wanted a widget to take up the whole 
1193 top row of our 2x2 table, you'd use left_attach = 0, right_attach =2, top_attach = 0, 
1194 bottom_attach = 1.
1195
1196 The xoptions and yoptions are used to specify packing options and may be OR'ed 
1197 together to allow multiple options.  
1198
1199 These options are:
1200 <itemize>
1201 <item>GTK_FILL - If the table box is larger than the widget, and GTK_FILL is
1202 specified, the widget will expand to use all the room available.
1203
1204 <item>GTK_SHRINK - If the table widget was allocated less space then was
1205 requested (usually by the user resizing the window), then the widgets would 
1206 normally just be pushed off the bottom of
1207 the window and disappear.  If GTK_SHRINK is specified, the widgets will
1208 shrink with the table.
1209
1210 <item>GTK_EXPAND - This will cause the table to expand to use up any remaining
1211 space in the window.
1212 </itemize>
1213
1214 Padding is just like in boxes, creating a clear area around the widget
1215 specified in pixels.  
1216
1217 gtk_table_attach() has a LOT of options.  So, there's a shortcut:
1218
1219 <tscreen><verb>
1220 void gtk_table_attach_defaults (GtkTable   *table,
1221                                 GtkWidget  *widget,
1222                                 gint        left_attach,
1223                                 gint        right_attach,
1224                                 gint        top_attach,
1225                                 gint        bottom_attach);
1226 </verb></tscreen>
1227
1228 The X and Y options default to GTK_FILL | GTK_EXPAND, and X and Y padding
1229 are set to 0.  The rest of the arguments are identical to the previous
1230 function.
1231
1232 We also have gtk_table_set_row_spacing() and gtk_table_set_col_spacing(). 
1233 This places spacing between the rows at the specified row or column.
1234
1235 <tscreen><verb>
1236 void gtk_table_set_row_spacing (GtkTable      *table,
1237                                 gint           row,
1238                                 gint           spacing);
1239 </verb></tscreen>
1240 and
1241 <tscreen><verb>
1242 void       gtk_table_set_col_spacing  (GtkTable      *table,
1243                                        gint           column,
1244                                        gint           spacing);
1245 </verb></tscreen>
1246
1247 Note that for columns, the space goes to the right of the column, and for 
1248 rows, the space goes below the row.
1249
1250 You can also set a consistent spacing of all rows and/or columns with:
1251
1252 <tscreen><verb>
1253 void gtk_table_set_row_spacings (GtkTable *table,
1254                                  gint      spacing);
1255 </verb></tscreen>
1256 <p>
1257 And,
1258 <tscreen><verb>
1259 void gtk_table_set_col_spacings (GtkTable  *table,
1260                                  gint       spacing);
1261 </verb></tscreen>
1262 <p>
1263 Note that with these calls, the last row and last column do not get any 
1264 spacing.
1265
1266 <!-- ----------------------------------------------------------------- -->
1267 <sect1>Table Packing Example
1268 <p>
1269 Here we make a window with three buttons in a 2x2 table.
1270 The first two buttons will be placed in the upper row.
1271 A third, quit button, is placed in the lower row, spanning both columns.
1272 Which means it should look something like this:
1273 <p>
1274 <? <CENTER> >
1275 <?
1276 <IMG SRC="gtk_tut_table.gif" VSPACE="15" HSPACE="10" 
1277 ALT="Table Packing Example Image" WIDTH="180" HEIGHT="120">
1278 >
1279 <? </CENTER> >
1280
1281 Here's the source code:
1282
1283 <tscreen><verb>
1284 /* table.c */
1285 #include <gtk/gtk.h>
1286
1287 /* our callback.
1288  * the data passed to this function is printed to stdout */
1289 void callback (GtkWidget *widget, gpointer *data)
1290 {
1291     g_print ("Hello again - %s was pressed\n", (char *) data);
1292 }
1293
1294 /* this callback quits the program */
1295 void delete_event (GtkWidget *widget, gpointer *data)
1296 {
1297     gtk_main_quit ();
1298 }
1299
1300 int main (int argc, char *argv[])
1301 {
1302     GtkWidget *window;
1303     GtkWidget *button;
1304     GtkWidget *table;
1305
1306     gtk_init (&amp;argc, &amp;argv);
1307
1308     /* create a new window */
1309     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1310
1311     /* set the window title */
1312     gtk_window_set_title (GTK_WINDOW (window), "Table");
1313
1314     /* set a handler for delete_event that immediately
1315      * exits GTK. */
1316     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
1317                         GTK_SIGNAL_FUNC (delete_event), NULL);
1318
1319     /* sets the border width of the window. */
1320     gtk_container_border_width (GTK_CONTAINER (window), 20);
1321
1322     /* create a 2x2 table */
1323     table = gtk_table_new (2, 2, TRUE);
1324
1325     /* put the table in the main window */
1326     gtk_container_add (GTK_CONTAINER (window), table);
1327
1328     /* create first button */
1329     button = gtk_button_new_with_label ("button 1");
1330
1331     /* when the button is clicked, we call the "callback" function
1332      * with a pointer to "button 1" as it's argument */
1333     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1334               GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");
1335
1336
1337     /* insert button 1 into the upper left quadrant of the table */
1338     gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);
1339
1340     gtk_widget_show (button);
1341
1342     /* create second button */
1343
1344     button = gtk_button_new_with_label ("button 2");
1345
1346     /* when the button is clicked, we call the "callback" function
1347      * with a pointer to "button 2" as it's argument */
1348     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1349               GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");
1350     /* insert button 2 into the upper right quadrant of the table */
1351     gtk_table_attach_defaults (GTK_TABLE(table), button, 1, 2, 0, 1);
1352
1353     gtk_widget_show (button);
1354
1355     /* create "Quit" button */
1356     button = gtk_button_new_with_label ("Quit");
1357
1358     /* when the button is clicked, we call the "delete_event" function
1359      * and the program exits */
1360     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1361                         GTK_SIGNAL_FUNC (delete_event), NULL);
1362
1363     /* insert the quit button into the both 
1364      * lower quadrants of the table */
1365     gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 2, 1, 2);
1366
1367     gtk_widget_show (button);
1368
1369     gtk_widget_show (table);
1370     gtk_widget_show (window);
1371
1372     gtk_main ();
1373
1374     return 0;
1375 }
1376 </verb></tscreen>
1377 <!-- ***************************************************************** -->
1378 <sect>Widget Overview
1379 <!-- ***************************************************************** -->
1380
1381 <p>
1382 The general steps to creating a widget in GTK are:
1383 <enum>
1384 <item> gtk_*_new - one of various functions to create a new widget.  These
1385 are all detailed in this section.
1386
1387 <item> Connect all signals we wish to use to the appropriate handlers.
1388
1389 <item> Set the attributes of the widget.
1390
1391 <item> Pack the widget into a container using the appropriate call such as 
1392 gtk_container_add() or gtk_box_pack_start().
1393
1394 <item> gtk_widget_show() the widget.
1395 </enum>
1396 <p>
1397 gtk_widget_show() lets GTK know that we are done setting the attributes
1398 of the widget, and it is ready to be displayed.  You may also use
1399 gtk_widget_hide to make it disappear again.  The order in which you
1400 show the widgets is not important, but I suggest showing the window
1401 last so the whole window pops up at once rather than seeing the individual
1402 widgets come up on the screen as they're formed.  The children of a widget
1403 (a window is a widget too)
1404 will not be displayed until the window itself is shown using the
1405 gtk_widget_show() function.
1406
1407 <!-- ----------------------------------------------------------------- -->
1408 <sect1> Casting
1409 <p>
1410 You'll notice as you go on, that GTK uses a type casting system.  This is
1411 always done using macros that both test the ability to cast the given item,
1412 and perform the cast.  Some common ones you will see are:
1413
1414 <itemize>
1415 <item> GTK_WIDGET(widget)
1416 <item> GTK_OBJECT(object)
1417 <item> GTK_SIGNAL_FUNC(function)
1418 <item> GTK_CONTAINER(container)
1419 <item> GTK_WINDOW(window)
1420 <item> GTK_BOX(box)
1421 </itemize>
1422
1423 These are all used to cast arguments in functions.  You'll see them in the
1424 examples, and can usually tell when to use them simply by looking at the
1425 function's declaration.
1426
1427 As you can see below in the class hierarchy, all GtkWidgets are derived from
1428 the GtkObject base class.  This means you can use an widget in any place the 
1429 function asks for an object - simply use the GTK_OBJECT() macro.
1430
1431 For example:
1432
1433 <tscreen><verb>
1434 gtk_signal_connect(GTK_OBJECT(button), "clicked",
1435                    GTK_SIGNAL_FUNC(callback_function), callback_data);
1436 </verb></tscreen> 
1437
1438 This casts the button into an object, and provides a cast for the function
1439 pointer to the callback.
1440
1441 Many widgets are also containers.  If you look in the class hierarchy below,
1442 you'll notice that many widgets drive from the GtkContainer class.  Any one
1443 of those widgets may use with the GTK_CONTAINER macro to
1444 pass them to functions that ask for containers.
1445
1446 Unfortunately, these macros are not extensively covered in the tutorial, but I
1447 recomend taking a look through the GTK header files.  It can be very
1448 educational.  In fact, it's not difficult to learn how a widget works just
1449 by looking at the function declarations.
1450
1451 <!-- ----------------------------------------------------------------- -->
1452 <sect1>Widget Hierarchy
1453 <p>
1454 For your reference, here is the class hierarchy tree used to implement widgets.
1455
1456 <tscreen><verb>
1457   GtkObject
1458    +GtkData
1459    | +GtkAdjustment
1460    | `GtkTooltips
1461    `GtkWidget
1462      +GtkContainer
1463      | +GtkBin
1464      | | +GtkAlignment
1465      | | +GtkEventBox
1466      | | +GtkFrame
1467      | | | `GtkAspectFrame
1468      | | +GtkHandleBox
1469      | | +GtkItem
1470      | | | +GtkListItem
1471      | | | +GtkMenuItem
1472      | | | | `GtkCheckMenuItem
1473      | | | |   `GtkRadioMenuItem
1474      | | | `GtkTreeItem
1475      | | +GtkViewport
1476      | | `GtkWindow
1477      | |   +GtkColorSelectionDialog
1478      | |   +GtkDialog
1479      | |   | `GtkInputDialog
1480      | |   `GtkFileSelection
1481      | +GtkBox
1482      | | +GtkButtonBox
1483      | | | +GtkHButtonBox
1484      | | | `GtkVButtonBox
1485      | | +GtkHBox
1486      | | | +GtkCombo
1487      | | | `GtkStatusbar
1488      | | `GtkVBox
1489      | |   +GtkColorSelection
1490      | |   `GtkGammaCurve
1491      | +GtkButton
1492      | | +GtkOptionMenu
1493      | | `GtkToggleButton
1494      | |   `GtkCheckButton
1495      | |     `GtkRadioButton
1496      | +GtkCList
1497      | +GtkFixed
1498      | +GtkList
1499      | +GtkMenuShell
1500      | | +GtkMenuBar
1501      | | `GtkMenu
1502      | +GtkNotebook
1503      | +GtkPaned
1504      | | +GtkHPaned
1505      | | `GtkVPaned
1506      | +GtkScrolledWindow
1507      | +GtkTable
1508      | +GtkToolbar
1509      | `GtkTree
1510      +GtkDrawingArea
1511      | `GtkCurve
1512      +GtkEditable
1513      | +GtkEntry
1514      | | `GtkSpinButton
1515      | `GtkText
1516      +GtkMisc
1517      | +GtkArrow
1518      | +GtkImage
1519      | +GtkLabel
1520      | | `GtkTipsQuery
1521      | `GtkPixmap
1522      +GtkPreview
1523      +GtkProgressBar
1524      +GtkRange
1525      | +GtkScale
1526      | | +GtkHScale
1527      | | `GtkVScale
1528      | `GtkScrollbar
1529      |   +GtkHScrollbar
1530      |   `GtkVScrollbar
1531      +GtkRuler
1532      | +GtkHRuler
1533      | `GtkVRuler
1534      `GtkSeparator
1535        +GtkHSeparator
1536        `GtkVSeparator
1537 </verb></tscreen>
1538
1539 <!-- ----------------------------------------------------------------- -->
1540 <sect1>Widgets Without Windows
1541 <p>
1542 The following widgets do not have an associated window.  If you want to
1543 capture events, you'll have to use the GtkEventBox.  See the section on   
1544 <ref id="sec_The_EventBox_Widget" name="The EventBox Widget">
1545
1546 <tscreen><verb>
1547 GtkAlignment
1548 GtkArrow
1549 GtkBin
1550 GtkBox
1551 GtkImage
1552 GtkItem
1553 GtkLabel
1554 GtkPixmap
1555 GtkScrolledWindow
1556 GtkSeparator
1557 GtkTable
1558 GtkAspectFrame
1559 GtkFrame
1560 GtkVBox
1561 GtkHBox
1562 GtkVSeparator
1563 GtkHSeparator
1564 </verb></tscreen>
1565 <p>
1566 We'll further our exploration of GTK by examining each widget in turn,
1567 creating a few simple functions to display them.  Another good source is
1568 the testgtk.c program that comes with GTK.  It can be found in
1569 gtk/testgtk.c.
1570
1571 <!-- ***************************************************************** -->
1572 <sect>The Button Widget
1573 <!-- ***************************************************************** -->
1574
1575 <!-- ----------------------------------------------------------------- -->
1576 <sect1>Normal Buttons
1577 <p>
1578 We've almost seen all there is to see of the button widget.  It's pretty
1579 simple.  There is however two ways to create a button.  You can use the
1580 gtk_button_new_with_label() to create a button with a label, or use
1581 gtk_button_new() to create a blank button.  It's then up to you to pack a
1582 label or pixmap into this new button.  To do this, create a new box, and
1583 then pack your objects into this box using the usual gtk_box_pack_start,
1584 and then use gtk_container_add to pack the box into the button.
1585 <p>
1586 Here's an example of using gtk_button_new to create a button with a
1587 picture and a label in it.  I've broken the code to create a box up from
1588 the rest so you can use it in your programs.
1589
1590 <tscreen><verb>
1591 /* buttons.c */
1592
1593 #include <gtk/gtk.h>
1594
1595 /* create a new hbox with an image and a label packed into it
1596  * and return the box.. */
1597
1598 GtkWidget *xpm_label_box (GtkWidget *parent, gchar *xpm_filename, gchar *label_text)
1599 {
1600     GtkWidget *box1;
1601     GtkWidget *label;
1602     GtkWidget *pixmapwid;
1603     GdkPixmap *pixmap;
1604     GdkBitmap *mask;
1605     GtkStyle *style;
1606
1607     /* create box for xpm and label */
1608     box1 = gtk_hbox_new (FALSE, 0);
1609     gtk_container_border_width (GTK_CONTAINER (box1), 2);
1610
1611     /* get style of button.. I assume it's to get the background color.
1612      * if someone knows the real reason, please enlighten me. */
1613     style = gtk_widget_get_style(parent);
1614
1615     /* now on to the xpm stuff.. load xpm */
1616     pixmap = gdk_pixmap_create_from_xpm (parent->window, &amp;mask,
1617                                          &amp;style->bg[GTK_STATE_NORMAL],
1618                                          xpm_filename);
1619     pixmapwid = gtk_pixmap_new (pixmap, mask);
1620
1621     /* create label for button */
1622     label = gtk_label_new (label_text);
1623
1624     /* pack the pixmap and label into the box */
1625     gtk_box_pack_start (GTK_BOX (box1),
1626                         pixmapwid, FALSE, FALSE, 3);
1627
1628     gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 3);
1629
1630     gtk_widget_show(pixmapwid);
1631     gtk_widget_show(label);
1632
1633     return (box1);
1634 }
1635
1636 /* our usual callback function */
1637 void callback (GtkWidget *widget, gpointer *data)
1638 {
1639     g_print ("Hello again - %s was pressed\n", (char *) data);
1640 }
1641
1642
1643 int main (int argc, char *argv[])
1644 {
1645     /* GtkWidget is the storage type for widgets */
1646     GtkWidget *window;
1647     GtkWidget *button;
1648     GtkWidget *box1;
1649
1650     gtk_init (&amp;argc, &amp;argv);
1651
1652     /* create a new window */
1653     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1654
1655     gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");
1656
1657     /* It's a good idea to do this for all windows. */
1658     gtk_signal_connect (GTK_OBJECT (window), "destroy",
1659                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
1660
1661     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
1662                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
1663
1664
1665     /* sets the border width of the window. */
1666     gtk_container_border_width (GTK_CONTAINER (window), 10);
1667     gtk_widget_realize(window);
1668
1669     /* create a new button */
1670     button = gtk_button_new ();
1671
1672     /* You should be getting used to seeing most of these functions by now */
1673     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1674                         GTK_SIGNAL_FUNC (callback), (gpointer) "cool button");
1675
1676     /* this calls our box creating function */
1677     box1 = xpm_label_box(window, "info.xpm", "cool button");
1678
1679     /* pack and show all our widgets */
1680     gtk_widget_show(box1);
1681
1682     gtk_container_add (GTK_CONTAINER (button), box1);
1683
1684     gtk_widget_show(button);
1685
1686     gtk_container_add (GTK_CONTAINER (window), button);
1687
1688     gtk_widget_show (window);
1689
1690     /* rest in gtk_main and wait for the fun to begin! */
1691     gtk_main ();
1692
1693     return 0;
1694 }
1695 </verb></tscreen>
1696
1697 The xpm_label_box function could be used to pack xpm's and labels into any
1698 widget that can be a container.
1699
1700 <!-- ----------------------------------------------------------------- -->
1701 <sect1> Toggle Buttons
1702 <p>
1703 Toggle buttons are very similar to normal buttons, except they will always
1704 be in one of two states, alternated by a click.  They may be depressed, and
1705 when you click again, they will pop back up.  Click again, and they will pop
1706 back down. 
1707
1708 Toggle buttons are the basis for check buttons and radio buttons, as such,
1709 many of the calls used for toggle buttons are inherited by radio and check
1710 buttons.  I will point these out when we come to them.
1711
1712 Creating a new toggle button:
1713
1714 <tscreen><verb>
1715 GtkWidget* gtk_toggle_button_new (void);
1716
1717 GtkWidget* gtk_toggle_button_new_with_label (gchar *label);
1718 </verb></tscreen>
1719 <p>
1720 As you can imagine, these work identically to the normal button widget
1721 calls.  The first creates a blank toggle button, and the second, a button 
1722 with a label widget already packed into it.
1723 <p>
1724 To retrieve the state of the toggle widget, including radio and check
1725 buttons, we use a macro as shown in our example below.  This tests the state
1726 of the toggle in a callback.  The signal of interest emitted to us by toggle
1727 buttons (the toggle button, check button, and radio button widgets), is the
1728 "toggled" signal.  To check the state of these buttons, set up a signal
1729 handler to catch the toggled signal, and use the macro to determine it's
1730 state.  The callback will look something like:
1731
1732 <tscreen><verb>
1733 void toggle_button_callback (GtkWidget *widget, gpointer   data)
1734 {
1735     if (GTK_TOGGLE_BUTTON (widget)->active) 
1736     {
1737         /* If control reaches here, the toggle button is down */
1738     
1739     } else {
1740     
1741         /* If control reaches here, the toggle button is up */
1742     }
1743 }
1744 </verb></tscreen>
1745
1746 <!--
1747
1748 COMMENTED!
1749
1750 <tscreen><verb>
1751 guint gtk_toggle_button_get_type (void);
1752 </verb></tscreen>
1753 <p>
1754 No idea... they all have this, but I dunno what it is :)
1755
1756
1757 <tscreen><verb>
1758 void gtk_toggle_button_set_mode (GtkToggleButton *toggle_button,
1759                                  gint draw_indicator);
1760 </verb></tscreen>
1761 <p>
1762 No idea.
1763 -->
1764
1765 <tscreen><verb>
1766 void gtk_toggle_button_set_state (GtkToggleButton *toggle_button,
1767                                   gint state);
1768 </verb></tscreen>
1769 <p>
1770 The above call can be used to set the state of the toggle button, and it's
1771 children the radio and check buttons.  Passing
1772 in your created button as the first argument, and a TRUE or FALSE
1773 for the second state argument to specify whether it should be up (released) or
1774 down (depressed).  Default is up, or FALSE.
1775
1776 Note that when you use the gtk_toggle_button_set_state() function, and the
1777 state is actually changed, it causes
1778 the "clicked" signal to be emitted from the button.
1779
1780 <tscreen><verb>
1781 void       gtk_toggle_button_toggled        (GtkToggleButton *toggle_button);
1782 </verb></tscreen>
1783 <p>
1784 This simply toggles the button, and emits the "toggled" signal.
1785
1786 <!-- ----------------------------------------------------------------- -->
1787 <sect1> Check Buttons
1788 <p>
1789 Check buttons inherent many properties and functions from the the toggle buttons above, 
1790 but look a little
1791 different.  Rather than being buttons with text inside them, they are small
1792 squares with the text to the right of them.  These are often seen for
1793 toggling options on and off in applications.
1794
1795 The two creation functions are the same as for the normal button.
1796
1797 <tscreen><verb>
1798 GtkWidget* gtk_check_button_new (void);
1799
1800 GtkWidget* gtk_check_button_new_with_label (gchar *label);
1801 </verb></tscreen>
1802
1803 The new_with_label function creates a check button with a label beside it.
1804
1805 Checking the state of the check button is identical to that of the toggle
1806 button.
1807
1808 <!-- ----------------------------------------------------------------- -->
1809 <sect1> Radio Buttons
1810 <p>
1811 Radio buttons are similar to check buttons except they are grouped so that
1812 only one may be selected/depressed at a time.  This is good for places in
1813 your application where you need to select from a short list of options.
1814
1815 Creating a new radio button is done with one of these calls:
1816
1817 <tscreen><verb>
1818 GtkWidget* gtk_radio_button_new (GSList *group);
1819
1820 GtkWidget* gtk_radio_button_new_with_label (GSList *group,
1821                                             gchar *label);
1822 </verb></tscreen>
1823 <p>
1824 You'll notice the extra argument to these calls.  They require a group to
1825 perform they're duty properly.  The first call should pass NULL as the first
1826 argument.  Then create a group using:
1827
1828 <tscreen><verb>
1829 GSList* gtk_radio_button_group (GtkRadioButton *radio_button);
1830 </verb></tscreen>
1831
1832 <p>
1833 The important thing to remember is that gtk_radio_button_group must be
1834 called for each new button added to the group, with the previous button
1835 passed in as an argument. The result is then passed into the call to
1836 gtk_radio_button_new or gtk_radio_button_new_with_label. This allows a
1837 chain of buttons to be established. The example below should make this
1838 clear.
1839
1840 It is also a good idea to explicitly set which button should be the 
1841 default depressed button with:
1842
1843 <tscreen><verb>
1844 void gtk_toggle_button_set_state (GtkToggleButton *toggle_button,
1845                                   gint state);
1846 </verb></tscreen>
1847 <p>
1848 This is described in the section on toggle buttons, and works in exactly the
1849 same way.
1850 <p>
1851 The following example creates a radio button group with three buttons.
1852
1853 <tscreen><verb>
1854 /* radiobuttons.c */
1855
1856 #include <gtk/gtk.h>
1857 #include <glib.h>
1858
1859 void close_application( GtkWidget *widget, gpointer *data ) {
1860   gtk_main_quit();
1861 }
1862
1863 main(int argc,char *argv[])
1864 {
1865   static GtkWidget *window = NULL;
1866   GtkWidget *box1;
1867   GtkWidget *box2;
1868   GtkWidget *button;
1869   GtkWidget *separator;
1870   GSList *group;
1871   
1872   gtk_init(&amp;argc,&amp;argv);          
1873   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1874   
1875   gtk_signal_connect (GTK_OBJECT (window), "delete_event",
1876                       GTK_SIGNAL_FUNC(close_application),
1877                       NULL);
1878
1879   gtk_window_set_title (GTK_WINDOW (window), "radio buttons");
1880   gtk_container_border_width (GTK_CONTAINER (window), 0);
1881
1882   box1 = gtk_vbox_new (FALSE, 0);
1883   gtk_container_add (GTK_CONTAINER (window), box1);
1884   gtk_widget_show (box1);
1885
1886   box2 = gtk_vbox_new (FALSE, 10);
1887   gtk_container_border_width (GTK_CONTAINER (box2), 10);
1888   gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
1889   gtk_widget_show (box2);
1890
1891   button = gtk_radio_button_new_with_label (NULL, "button1");
1892   gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1893   gtk_widget_show (button);
1894
1895   group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
1896   button = gtk_radio_button_new_with_label(group, "button2");
1897   gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (button), TRUE);
1898   gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1899   gtk_widget_show (button);
1900
1901   group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
1902   button = gtk_radio_button_new_with_label(group, "button3");
1903   gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1904   gtk_widget_show (button);
1905
1906   separator = gtk_hseparator_new ();
1907   gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
1908   gtk_widget_show (separator);
1909
1910   box2 = gtk_vbox_new (FALSE, 10);
1911   gtk_container_border_width (GTK_CONTAINER (box2), 10);
1912   gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
1913   gtk_widget_show (box2);
1914
1915   button = gtk_button_new_with_label ("close");
1916   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1917                              GTK_SIGNAL_FUNC(close_application),
1918                              GTK_OBJECT (window));
1919   gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
1920   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
1921   gtk_widget_grab_default (button);
1922   gtk_widget_show (button);
1923   gtk_widget_show (window);
1924      
1925   gtk_main();
1926   return(0);
1927 }
1928 </verb></tscreen>
1929
1930 You can shorten this slightly by using the following syntax, which
1931 removes the need for a variable to hold the list of buttons:
1932
1933 <tscreen><verb>
1934      button2 = gtk_radio_button_new_with_label(
1935                  gtk_radio_button_group (GTK_RADIO_BUTTON (button1)),
1936                  "button2");
1937 </verb></tscreen>
1938
1939 <!-- ***************************************************************** -->
1940 <sect> Miscallaneous Widgets
1941 <!-- ***************************************************************** -->
1942
1943 <!-- ----------------------------------------------------------------- -->
1944 <sect1> Labels
1945 <p>
1946 Labels are used a lot in GTK, and are relatively simple.  Labels emit no
1947 signals as they do not have an associated X window.  If you need to catch
1948 signals, or do clipping, use the EventBox widget.
1949
1950 To create a new label, use:
1951
1952 <tscreen><verb>
1953 GtkWidget* gtk_label_new (char *str);
1954 </verb></tscreen>
1955
1956 Where the sole argument is the string you wish the label to display.
1957
1958 To change the label's text after creation, use the function:
1959
1960 <tscreen><verb>
1961 void gtk_label_set (GtkLabel  *label,
1962                     char      *str);
1963 </verb></tscreen>
1964 <p>
1965 Where the first argument is the label you created previously (casted using
1966 the GTK_LABEL() macro), and the second is the new string.
1967
1968 The space needed for the new string will be automatically adjusted if needed.
1969
1970 To retrieve the current string, use:
1971
1972 <tscreen><verb>
1973 void gtk_label_get (GtkLabel  *label,
1974                     char     **str);
1975 </verb></tscreen>
1976
1977 Where the first arguement is the label you've created, and the second, the
1978 return for the string.
1979
1980 <!-- ----------------------------------------------------------------- -->
1981 <sect1>The Tooltips Widget
1982 <p>
1983 These are the little text strings that pop up when you leave your pointer
1984 over a button or other widget for a few seconds.  They are easy to use, so I
1985 will just explain them without giving an example.  If you want to see some
1986 code, take a look at the testgtk.c program distributed with GDK.
1987 <p>
1988 Some widgets (such as the label) will not work with tooltips.
1989 <p>
1990 The first call you will use to create a new tooltip.  You only need to do
1991 this once in a given function.  The GtkTooltip this function returns can be
1992 used to create multiple tooltips.
1993
1994 <tscreen><verb>
1995 GtkTooltips *gtk_tooltips_new (void);
1996 </verb></tscreen>
1997
1998 Once you have created a new tooltip, and the widget you wish to use it on,
1999 simply use this call to set it.
2000
2001 <tscreen><verb>
2002 void gtk_tooltips_set_tips   (GtkTooltips *tooltips,
2003                               GtkWidget   *widget,
2004                               gchar       *tips_text);
2005 </verb></tscreen>
2006
2007 The first argument is the tooltip you've already created, followed by the
2008 widget you wish to have this tooltip pop up for, and the text you wish it to
2009 say.
2010 <p>
2011 Here's a short example:
2012
2013 <tscreen><verb>
2014 GtkTooltips *tooltips;
2015 GtkWidget *button;
2016 ...
2017 tooltips = gtk_tooltips_new ();
2018 button = gtk_button_new_with_label ("button 1");
2019 ...
2020 gtk_tooltips_set_tips (tooltips, button, "This is button 1");
2021 </verb></tscreen>
2022
2023
2024 There are other calls used with tooltips.  I will just list them with a
2025 brief description of what they do.
2026
2027 <tscreen><verb>
2028 void gtk_tooltips_destroy    (GtkTooltips *tooltips);
2029 </verb></tscreen>
2030
2031 Destroy the created tooltips.
2032
2033 <tscreen><verb>
2034 void gtk_tooltips_enable     (GtkTooltips *tooltips);
2035 </verb></tscreen>
2036
2037 Enable a disabled set of tooltips.
2038
2039 <tscreen><verb>
2040 void gtk_tooltips_disable    (GtkTooltips *tooltips);
2041 </verb></tscreen>
2042
2043 Disable an enabled set of tooltips.
2044
2045 <tscreen><verb>
2046 void gtk_tooltips_set_delay  (GtkTooltips *tooltips,
2047                               gint         delay);
2048
2049 </verb></tscreen>
2050 Sets how many milliseconds you have to hold you pointer over the widget before the
2051 tooltip will pop up.  The default is 1000 milliseconds or 1 second.
2052
2053 <tscreen><verb>
2054 void      gtk_tooltips_set_tips (GtkTooltips *tooltips,
2055                                  GtkWidget   *widget,
2056                                  gchar    *tips_text);
2057 </verb></tscreen>
2058
2059 Change the tooltip text of an already created tooltip.
2060
2061 <tscreen><verb>
2062 void gtk_tooltips_set_colors (GtkTooltips *tooltips,
2063                               GdkColor    *background,
2064                               GdkColor    *foreground);
2065 </verb></tscreen>
2066
2067 Set the foreground and background color of the tooltips.  Again, I have no
2068 idea how to specify the colors.
2069 <p>
2070 And that's all the functions associated with tooltips.  More than you'll
2071 ever want to know :)
2072
2073 <!-- ----------------------------------------------------------------- -->
2074 <sect1> Progress Bars
2075 <p>
2076 Progress bars are used to show the status of an operation.  They are pretty 
2077 easy to use, as you will see with the code below. But first lets start out 
2078 with the call to create a new progress bar.
2079
2080 <tscreen><verb>
2081 GtkWidget *gtk_progress_bar_new (void);
2082 </verb></tscreen>
2083
2084 Now that the progress bar has been created we can use it.
2085
2086 <tscreen><verb>
2087 void gtk_progress_bar_update (GtkProgressBar *pbar, gfloat percentage);
2088 </verb></tscreen>
2089
2090 The first argument is the progress bar you wish to operate on, and the second 
2091 argument is the amount 'completed', meaning the amount the progress bar has 
2092 been filled from 0-100% (a real number between 0 and 1).
2093
2094 Progress Bars are usually used with timeouts or other such functions (see
2095 section on <ref id="sec_timeouts" name="Timeouts, I/O and Idle Functions">) 
2096 to give the illusion of multitasking.  All will employ 
2097 the gtk_progress_bar_update function in the same manner.
2098
2099 Here is an example of the progress bar, updated using timeouts.  This 
2100 code also shows you how to reset the Progress Bar.
2101
2102 <tscreen><verb>
2103 /* progressbar.c */
2104
2105 #include <gtk/gtk.h>
2106
2107 static int ptimer = 0;
2108 int pstat = TRUE;
2109
2110 /* This function increments and updates the progress bar, it also resets
2111  the progress bar if pstat is FALSE */
2112 gint progress (gpointer data)
2113 {
2114     gfloat pvalue;
2115     
2116     /* get the current value of the progress bar */
2117     pvalue = GTK_PROGRESS_BAR (data)->percentage;
2118     
2119     if ((pvalue >= 1.0) || (pstat == FALSE)) {
2120         pvalue = 0.0;
2121         pstat = TRUE;
2122     }
2123     pvalue += 0.01;
2124     
2125     gtk_progress_bar_update (GTK_PROGRESS_BAR (data), pvalue);
2126     
2127     return TRUE;
2128 }
2129
2130 /* This function signals a reset of the progress bar */
2131 void progress_r (void)
2132 {  
2133     pstat = FALSE;  
2134 }
2135
2136 void destroy (GtkWidget *widget, gpointer *data)
2137 {
2138     gtk_main_quit ();
2139 }
2140
2141 int main (int argc, char *argv[])
2142 {
2143     GtkWidget *window;
2144     GtkWidget *button;
2145     GtkWidget *label;
2146     GtkWidget *table;
2147     GtkWidget *pbar;
2148     
2149     gtk_init (&amp;argc, &amp;argv);
2150     
2151     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2152     
2153     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
2154                         GTK_SIGNAL_FUNC (destroy), NULL);
2155     
2156     gtk_container_border_width (GTK_CONTAINER (window), 10);
2157     
2158     table = gtk_table_new(3,2,TRUE);
2159     gtk_container_add (GTK_CONTAINER (window), table);
2160     
2161     label = gtk_label_new ("Progress Bar Example");
2162     gtk_table_attach_defaults(GTK_TABLE(table), label, 0,2,0,1);
2163     gtk_widget_show(label);
2164     
2165     /* Create a new progress bar, pack it into the table, and show it */
2166     pbar = gtk_progress_bar_new ();
2167     gtk_table_attach_defaults(GTK_TABLE(table), pbar, 0,2,1,2);
2168     gtk_widget_show (pbar);
2169     
2170     /* Set the timeout to handle automatic updating of the progress bar */
2171     ptimer = gtk_timeout_add (100, progress, pbar);
2172     
2173     /* This button signals the progress bar to be reset */
2174     button = gtk_button_new_with_label ("Reset");
2175     gtk_signal_connect (GTK_OBJECT (button), "clicked",
2176                         GTK_SIGNAL_FUNC (progress_r), NULL);
2177     gtk_table_attach_defaults(GTK_TABLE(table), button, 0,1,2,3);
2178     gtk_widget_show(button);
2179     
2180     button = gtk_button_new_with_label ("Cancel");
2181     gtk_signal_connect (GTK_OBJECT (button), "clicked",
2182                         GTK_SIGNAL_FUNC (destroy), NULL);
2183     
2184     gtk_table_attach_defaults(GTK_TABLE(table), button, 1,2,2,3);
2185     gtk_widget_show (button);
2186     
2187     gtk_widget_show(table);
2188     gtk_widget_show(window);
2189     
2190     gtk_main ();
2191     
2192     return 0;
2193 }
2194 </verb></tscreen>
2195
2196 In this small program there are four areas that concern the general operation
2197 of Progress Bars, we will look at them in the order they are called.
2198
2199 <tscreen><verb>
2200 pbar = gtk_progress_bar_new ();
2201 </verb></tscreen>
2202
2203 This code creates a new progress bar, called pbar.
2204
2205 <tscreen><verb>
2206 ptimer = gtk_timeout_add (100, progress, pbar);
2207 </verb></tscreen>
2208
2209 This code, uses timeouts to enable a constant time interval, timeouts are 
2210 not necessary in the use of Progress Bars. 
2211
2212 <tscreen><verb>
2213 pvalue = GTK_PROGRESS_BAR (data)->percentage;
2214 </verb></tscreen>
2215
2216 This code assigns the current value of the percentage bar to pvalue.
2217
2218 <tscreen><verb>
2219 gtk_progress_bar_update (GTK_PROGRESS_BAR (data), pvalue);
2220 </verb></tscreen>
2221
2222 Finally, this code updates the progress bar with the value of pvalue
2223
2224 And that is all there is to know about Progress Bars, enjoy.
2225
2226 <!-- ----------------------------------------------------------------- -->
2227 <sect1> Dialogs
2228 <p>
2229
2230 The Dialog widget is very simple, and is actually just a window with a few
2231 things pre-packed into it for you.  The structure for a Dialog is:
2232
2233 <tscreen><verb>
2234 struct GtkDialog
2235 {
2236       GtkWindow window;
2237     
2238       GtkWidget *vbox;
2239       GtkWidget *action_area;
2240 };
2241 </verb></tscreen>
2242
2243 So you see, it simple creates a window, and then packs a vbox into the top,
2244 then a seperator, and then an hbox for the "action_area".
2245
2246 The Dialog widget can be used for pop-up messages to the user, and 
2247 other similar tasks.  It is really basic, and there is only one 
2248 function for the dialog box, which is:
2249
2250 <tscreen><verb>
2251 GtkWidget* gtk_dialog_new (void);
2252 </verb></tscreen>
2253
2254 So to create a new dialog box, use,
2255
2256 <tscreen><verb>
2257 GtkWidget window;
2258 window = gtk_dialog_new ();
2259 </verb></tscreen>
2260
2261 This will create the dialog box, and it is now up to you to use it.
2262 you could pack a button in the action_area by doing something like so:
2263
2264 <tscreen><verb>
2265 button = ...
2266 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), button,
2267                     TRUE, TRUE, 0);
2268 gtk_widget_show (button);
2269 </verb></tscreen>
2270
2271 And you could add to the vbox area by packing, for instance, a label 
2272 in it, try something like this:
2273
2274 <tscreen><verb>
2275 label = gtk_label_new ("Dialogs are groovy");
2276 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), label, TRUE,
2277                     TRUE, 0);
2278 gtk_widget_show (label);
2279 </verb></tscreen>
2280
2281 As an example in using the dialog box, you could put two buttons in 
2282 the action_area, a Cancel button and an Ok button, and a label in the vbox 
2283 area, asking the user a question or giving an error etc.  Then you could 
2284 attach a different signal to each of the buttons and perform the 
2285 operation the user selects.
2286
2287 <!-- ----------------------------------------------------------------- -->
2288 <sect1> Pixmaps
2289 <p>
2290 Pixmaps are data structures that contain pictures.  These pictures can be
2291 used in various places, but most visibly as icons on the X-Windows desktop,
2292 or as cursors.  A bitmap is a 2-color pixmap.
2293
2294 To use pixmaps in GTK, we must first build a GdkPixmap structure using
2295 routines from the GDK layer.  Pixmaps can either be created from in-memory
2296 data, or from data read from a file.  We'll go through each of the calls
2297 to create a pixmap.
2298
2299 <tscreen><verb>
2300 GdkPixmap *gdk_bitmap_create_from_data( GdkWindow *window,
2301                                         gchar     *data,
2302                                         gint      width,
2303                                         gint      height );
2304 </verb></tscreen>
2305 <p>
2306 This routine is used to create a single-plane pixmap (2 colors) from data in
2307 memory.  Each bit of the data represents whether that pixel is off or on.
2308 Width and height are in pixels.  The GdkWindow pointer is to the current
2309 window, since a pixmap resources are meaningful only in the context of the
2310 screen where it is to be displayed.
2311
2312 <tscreen><verb>
2313 GdkPixmap* gdk_pixmap_create_from_data( GdkWindow  *window,
2314                                         gchar      *data,
2315                                         gint        width,
2316                                         gint        height,
2317                                         gint        depth,
2318                                         GdkColor   *fg,
2319                                         GdkColor   *bg );
2320 </verb></tscreen>
2321
2322 This is used to create a pixmap of the given depth (number of colors) from
2323 the bitmap data specified.  fg and bg are the foreground and background
2324 color to use.
2325
2326 <tscreen><verb>
2327 GdkPixmap* gdk_pixmap_create_from_xpm( GdkWindow  *window,
2328                                        GdkBitmap **mask,
2329                                        GdkColor   *transparent_color,
2330                                        const gchar *filename );
2331 </verb></tscreen>
2332
2333 XPM format is a readable pixmap representation for the X Window System.  It
2334 is widely used and many different utilities are available for creating image
2335 files in this format.  The file specified by filename must contain an image
2336 in that format and it is loaded into the pixmap structure.  The mask specifies
2337 what bits of the pixmap are opaque.  All other bits are colored using the
2338 color specified by transparent_color.  An example using this follows below.  
2339
2340 <tscreen><verb>
2341 GdkPixmap* gdk_pixmap_create_from_xpm_d (GdkWindow  *window,
2342                                          GdkBitmap **mask,
2343                                          GdkColor   *transparent_color,
2344                                          gchar     **data);
2345 </verb></tscreen>
2346
2347 Small images can be incorporated into a program as data in the XPM format.
2348 A pixmap is created using this data, instead of reading it from a file.
2349 An example of such data is
2350
2351 <tscreen><verb>
2352 /* XPM */
2353 static const char * xpm_data[] = {
2354 "16 16 3 1",
2355 "       c None",
2356 ".      c #000000000000",
2357 "X      c #FFFFFFFFFFFF",
2358 "                ",
2359 "   ......       ",
2360 "   .XXX.X.      ",
2361 "   .XXX.XX.     ",
2362 "   .XXX.XXX.    ",
2363 "   .XXX.....    ",
2364 "   .XXXXXXX.    ",
2365 "   .XXXXXXX.    ",
2366 "   .XXXXXXX.    ",
2367 "   .XXXXXXX.    ",
2368 "   .XXXXXXX.    ",
2369 "   .XXXXXXX.    ",
2370 "   .XXXXXXX.    ",
2371 "   .........    ",
2372 "                ",
2373 "                "};
2374 </verb></tscreen>
2375
2376 <tscreen><verb>
2377 void gdk_pixmap_destroy( GdkPixmap  *pixmap );
2378 </verb></tscreen>
2379 <p>
2380 When we're done using a pixmap and not likely to reuse it again soon,
2381 it is a good idea to release the resource using gdk_pixmap_destroy.  Pixmaps
2382 should be considered a precious resource.
2383
2384
2385 Once we've created a pixmap, we can display it as a GTK widget.  We must
2386 create a pixmap widget to contain the GDK pixmap.  This is done using
2387
2388 <tscreen><verb>
2389 GtkWidget* gtk_pixmap_new( GdkPixmap  *pixmap,
2390                            GdkBitmap  *mask );
2391 </verb></tscreen>
2392 <p>
2393 The other pixmap widget calls are
2394
2395 <tscreen><verb>
2396 guint gtk_pixmap_get_type( void );
2397 void  gtk_pixmap_set( GtkPixmap  *pixmap,
2398                       GdkPixmap  *val,
2399                       GdkBitmap  *mask);
2400 void  gtk_pixmap_get( GtkPixmap  *pixmap,
2401                       GdkPixmap **val,
2402                       GdkBitmap **mask);
2403 </verb></tscreen>
2404 <p>
2405 gtk_pixmap_set is used to change the pixmap that the widget is currently
2406 managing.  Val is the pixmap created using GDK.
2407
2408 The following is an example of using a pixmap in a button.
2409
2410 <tscreen><verb>
2411 /* pixmap.c */
2412
2413 #include <gtk/gtk.h>
2414
2415
2416 /* XPM data of Open-File icon */
2417 static const char * xpm_data[] = {
2418 "16 16 3 1",
2419 "       c None",
2420 ".      c #000000000000",
2421 "X      c #FFFFFFFFFFFF",
2422 "                ",
2423 "   ......       ",
2424 "   .XXX.X.      ",
2425 "   .XXX.XX.     ",
2426 "   .XXX.XXX.    ",
2427 "   .XXX.....    ",
2428 "   .XXXXXXX.    ",
2429 "   .XXXXXXX.    ",
2430 "   .XXXXXXX.    ",
2431 "   .XXXXXXX.    ",
2432 "   .XXXXXXX.    ",
2433 "   .XXXXXXX.    ",
2434 "   .XXXXXXX.    ",
2435 "   .........    ",
2436 "                ",
2437 "                "};
2438
2439
2440 /* when invoked (via signal delete_event), terminates the application.
2441  */
2442 void close_application( GtkWidget *widget, gpointer *data ) {
2443     gtk_main_quit();
2444 }
2445
2446
2447 /* is invoked when the button is clicked.  It just prints a message.
2448  */
2449 void button_clicked( GtkWidget *widget, gpointer *data ) {
2450     printf( "button clicked\n" );
2451 }
2452
2453 int main( int argc, char *argv[] )
2454 {
2455     /* GtkWidget is the storage type for widgets */
2456     GtkWidget *window, *pixmapwid, *button;
2457     GdkPixmap *pixmap;
2458     GdkBitmap *mask;
2459     GtkStyle *style;
2460     
2461     /* create the main window, and attach delete_event signal to terminating
2462        the application */
2463     gtk_init( &amp;argc, &amp;argv );
2464     window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
2465     gtk_signal_connect( GTK_OBJECT (window), "delete_event",
2466                         GTK_SIGNAL_FUNC (close_application), NULL );
2467     gtk_container_border_width( GTK_CONTAINER (window), 10 );
2468     gtk_widget_show( window );
2469
2470     /* now for the pixmap from gdk */
2471     style = gtk_widget_get_style( window );
2472     pixmap = gdk_pixmap_create_from_xpm_d( window->window,  &amp;mask,
2473                                            &amp;style->bg[GTK_STATE_NORMAL],
2474                                            (gchar **)xpm_data );
2475
2476     /* a pixmap widget to contain the pixmap */
2477     pixmapwid = gtk_pixmap_new( pixmap, mask );
2478     gtk_widget_show( pixmapwid );
2479
2480     /* a button to contain the pixmap widget */
2481     button = gtk_button_new();
2482     gtk_container_add( GTK_CONTAINER(button), pixmapwid );
2483     gtk_container_add( GTK_CONTAINER(window), button );
2484     gtk_widget_show( button );
2485
2486     gtk_signal_connect( GTK_OBJECT(button), "clicked",
2487                         GTK_SIGNAL_FUNC(button_clicked), NULL );
2488
2489     /* show the window */
2490     gtk_main ();
2491           
2492     return 0;
2493 }
2494 </verb></tscreen>
2495
2496
2497 To load a file from an XPM data file called icon0.xpm in the current
2498 directory, we would have created the pixmap thus
2499
2500 <tscreen><verb>
2501     /* load a pixmap from a file */
2502     pixmap = gdk_pixmap_create_from_xpm( window->window, &amp;mask,
2503                                          &amp;style->bg[GTK_STATE_NORMAL],
2504                                          "./icon0.xpm" );
2505     pixmapwid = gtk_pixmap_new( pixmap, mask );
2506     gtk_widget_show( pixmapwid );
2507     gtk_container_add( GTK_CONTAINER(window), pixmapwid );
2508 </verb></tscreen>
2509
2510
2511
2512 Using Shapes
2513 <p>
2514 A disadvantage of using pixmaps is that the displayed object is always
2515 rectangular, regardless of the image.  We would like to create desktops
2516 and applications with icons that have more natural shapes.  For example,
2517 for a game interface, we would like to have round buttons to push.  The
2518 way to do this is using shaped windows.
2519
2520 A shaped window is simply a pixmap where the background pixels are
2521 transparent.  This way, when the background image is multi-colored, we
2522 don't overwrite it with a rectangular, non-matching border around our
2523 icon.  The following example displays a full wheelbarrow image on the
2524 desktop.
2525
2526 <tscreen><verb>
2527 /* wheelbarrow.c */
2528
2529 #include <gtk/gtk.h>
2530
2531 /* XPM */
2532 static char * WheelbarrowFull_xpm[] = {
2533 "48 48 64 1",
2534 "       c None",
2535 ".      c #DF7DCF3CC71B",
2536 "X      c #965875D669A6",
2537 "o      c #71C671C671C6",
2538 "O      c #A699A289A699",
2539 "+      c #965892489658",
2540 "@      c #8E38410330C2",
2541 "#      c #D75C7DF769A6",
2542 "$      c #F7DECF3CC71B",
2543 "%      c #96588A288E38",
2544 "&amp;      c #A69992489E79",
2545 "*      c #8E3886178E38",
2546 "=      c #104008200820",
2547 "-      c #596510401040",
2548 ";      c #C71B30C230C2",
2549 ":      c #C71B9A699658",
2550 ">      c #618561856185",
2551 ",      c #20811C712081",
2552 "<      c #104000000000",
2553 "1      c #861720812081",
2554 "2      c #DF7D4D344103",
2555 "3      c #79E769A671C6",
2556 "4      c #861782078617",
2557 "5      c #41033CF34103",
2558 "6      c #000000000000",
2559 "7      c #49241C711040",
2560 "8      c #492445144924",
2561 "9      c #082008200820",
2562 "0      c #69A618611861",
2563 "q      c #B6DA71C65144",
2564 "w      c #410330C238E3",
2565 "e      c #CF3CBAEAB6DA",
2566 "r      c #71C6451430C2",
2567 "t      c #EFBEDB6CD75C",
2568 "y      c #28A208200820",
2569 "u      c #186110401040",
2570 "i      c #596528A21861",
2571 "p      c #71C661855965",
2572 "a      c #A69996589658",
2573 "s      c #30C228A230C2",
2574 "d      c #BEFBA289AEBA",
2575 "f      c #596545145144",
2576 "g      c #30C230C230C2",
2577 "h      c #8E3882078617",
2578 "j      c #208118612081",
2579 "k      c #38E30C300820",
2580 "l      c #30C2208128A2",
2581 "z      c #38E328A238E3",
2582 "x      c #514438E34924",
2583 "c      c #618555555965",
2584 "v      c #30C2208130C2",
2585 "b      c #38E328A230C2",
2586 "n      c #28A228A228A2",
2587 "m      c #41032CB228A2",
2588 "M      c #104010401040",
2589 "N      c #492438E34103",
2590 "B      c #28A2208128A2",
2591 "V      c #A699596538E3",
2592 "C      c #30C21C711040",
2593 "Z      c #30C218611040",
2594 "A      c #965865955965",
2595 "S      c #618534D32081",
2596 "D      c #38E31C711040",
2597 "F      c #082000000820",
2598 "                                                ",
2599 "          .XoO                                  ",
2600 "         +@#$%o&amp;                                ",
2601 "         *=-;#::o+                              ",
2602 "           >,<12#:34                            ",
2603 "             45671#:X3                          ",
2604 "               +89<02qwo                        ",
2605 "e*                >,67;ro                       ",
2606 "ty>                 459@>+&amp;&amp;                    ",
2607 "$2u+                  ><ipas8*                  ",
2608 "%$;=*                *3:.Xa.dfg>                ",
2609 "Oh$;ya             *3d.a8j,Xe.d3g8+             ",
2610 " Oh$;ka          *3d$a8lz,,xxc:.e3g54           ",
2611 "  Oh$;kO       *pd$%svbzz,sxxxxfX..&amp;wn>         ",
2612 "   Oh$@mO    *3dthwlsslszjzxxxxxxx3:td8M4       ",
2613 "    Oh$@g&amp; *3d$XNlvvvlllm,mNwxxxxxxxfa.:,B*     ",
2614 "     Oh$@,Od.czlllllzlmmqV@V#V@fxxxxxxxf:%j5&amp;   ",
2615 "      Oh$1hd5lllslllCCZrV#r#:#2AxxxxxxxxxcdwM*  ",
2616 "       OXq6c.%8vvvllZZiqqApA:mq:Xxcpcxxxxxfdc9* ",
2617 "        2r<6gde3bllZZrVi7S@SV77A::qApxxxxxxfdcM ",
2618 "        :,q-6MN.dfmZZrrSS:#riirDSAX@Af5xxxxxfevo",
2619 "         +A26jguXtAZZZC7iDiCCrVVii7Cmmmxxxxxx%3g",
2620 "          *#16jszN..3DZZZZrCVSA2rZrV7Dmmwxxxx&amp;en",
2621 "           p2yFvzssXe:fCZZCiiD7iiZDiDSSZwwxx8e*>",
2622 "           OA1<jzxwwc:$d%NDZZZZCCCZCCZZCmxxfd.B ",
2623 "            3206Bwxxszx%et.eaAp77m77mmmf3&amp;eeeg* ",
2624 "             @26MvzxNzvlbwfpdettttttttttt.c,n&amp;  ",
2625 "             *;16=lsNwwNwgsvslbwwvccc3pcfu<o    ",
2626 "              p;<69BvwwsszslllbBlllllllu<5+     ",
2627 "              OS0y6FBlvvvzvzss,u=Blllj=54       ",
2628 "               c1-699Blvlllllu7k96MMMg4         ",
2629 "               *10y8n6FjvllllB<166668           ",
2630 "                S-kg+>666<M<996-y6n<8*          ",
2631 "                p71=4 m69996kD8Z-66698&amp;&amp;        ",
2632 "                &amp;i0ycm6n4 ogk17,0<6666g         ",
2633 "                 N-k-<>     >=01-kuu666>        ",
2634 "                 ,6ky&amp;      &amp;46-10ul,66,        ",
2635 "                 Ou0<>       o66y<ulw<66&amp;       ",
2636 "                  *kk5       >66By7=xu664       ",
2637 "                   <<M4      466lj<Mxu66o       ",
2638 "                   *>>       +66uv,zN666*       ",
2639 "                              566,xxj669        ",
2640 "                              4666FF666>        ",
2641 "                               >966666M         ",
2642 "                                oM6668+         ",
2643 "                                  *4            ",
2644 "                                                ",
2645 "                                                "};
2646
2647
2648 /* when invoked (via signal delete_event), terminates the application.
2649  */
2650 void close_application( GtkWidget *widget, gpointer *data ) {
2651     gtk_main_quit();
2652 }
2653
2654 int main (int argc, char *argv[])
2655 {
2656     /* GtkWidget is the storage type for widgets */
2657     GtkWidget *window, *pixmap, *fixed;
2658     GdkPixmap *gdk_pixmap;
2659     GdkBitmap *mask;
2660     GtkStyle *style;
2661     GdkGC *gc;
2662     
2663     /* create the main window, and attach delete_event signal to terminate
2664        the application.  Note that the main window will not have a titlebar
2665        since we're making it a popup. */
2666     gtk_init (&amp;argc, &amp;argv);
2667     window = gtk_window_new( GTK_WINDOW_POPUP );
2668     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
2669                         GTK_SIGNAL_FUNC (close_application), NULL);
2670     gtk_widget_show (window);
2671
2672     /* now for the pixmap and the pixmap widget */
2673     style = gtk_widget_get_default_style();
2674     gc = style->black_gc;
2675     gdk_pixmap = gdk_pixmap_create_from_xpm_d( window->window, &amp;mask,
2676                                              &amp;style->bg[GTK_STATE_NORMAL],
2677                                              WheelbarrowFull_xpm );
2678     pixmap = gtk_pixmap_new( gdk_pixmap, mask );
2679     gtk_widget_show( pixmap );
2680
2681     /* To display the pixmap, we use a fixed widget to place the pixmap */
2682     fixed = gtk_fixed_new();
2683     gtk_widget_set_usize( fixed, 200, 200 );
2684     gtk_fixed_put( GTK_FIXED(fixed), pixmap, 0, 0 );
2685     gtk_container_add( GTK_CONTAINER(window), fixed );
2686     gtk_widget_show( fixed );
2687
2688     /* This masks out everything except for the image itself */
2689     gtk_widget_shape_combine_mask( window, mask, 0, 0 );
2690     
2691     /* show the window */
2692     gtk_widget_set_uposition( window, 20, 400 );
2693     gtk_widget_show( window );
2694     gtk_main ();
2695           
2696     return 0;
2697 }
2698 </verb></tscreen>
2699 <p>
2700 To make the wheelbarrow image sensitive, we could attach the button press
2701 event signal to make it do something.  The following few lines would make
2702 the picture sensitive to a mouse button being pressed which makes the
2703 application terminate.
2704
2705 <tscreen><verb>
2706 gtk_widget_set_events( window,
2707                        gtk_widget_get_events( window ) |
2708                        GDK_BUTTON_PRESS_MASK );
2709
2710 gtk_signal_connect( GTK_OBJECT(window), "button_press_event",
2711                     GTK_SIGNAL_FUNC(close_application), NULL );
2712 </verb></tscreen>
2713
2714 <!-- ----------------------------------------------------------------- -->
2715 <sect1>Rulers
2716 <p>
2717 Ruler widgets are used to indicate the location of the mouse pointer
2718 in a given window.  A window can have a vertical ruler spanning across
2719 the width and a horizontal ruler spanning down the height.  A small
2720 triangular indicator on the ruler shows the exact location of the
2721 pointer relative to the ruler.
2722
2723 A ruler must first be created.  Horizontal and vertical rulers are
2724 created using
2725
2726 <tscreen><verb>
2727 GtkWidget *gtk_hruler_new(void);    /* horizontal ruler */
2728 GtkWidget *gtk_vruler_new(void);    /* vertical ruler   */
2729 </verb></tscreen>
2730
2731 Once a ruler is created, we can define the unit of measurement.  Units
2732 of measure for rulers can be GTK_PIXELS, GTK_INCHES or
2733 GTK_CENTIMETERS.  This is set using
2734
2735 <tscreen><verb>
2736 void gtk_ruler_set_metric( GtkRuler        *ruler,
2737                            GtkMetricType   metric );
2738 </verb></tscreen>
2739
2740 The default measure is GTK_PIXELS.
2741
2742 <tscreen><verb>
2743 gtk_ruler_set_metric( GTK_RULER(ruler), GTK_PIXELS );
2744 </verb></tscreen>
2745
2746 Other important characteristics of a ruler are how to mark the units
2747 of scale and where the position indicator is initially placed.  These
2748 are set for a ruler using
2749
2750 <tscreen><verb>
2751 void  gtk_ruler_set_range  (GtkRuler       *ruler,
2752                             gfloat          lower,
2753                             gfloat          upper,
2754                             gfloat          position,
2755                             gfloat          max_size);
2756 </verb></tscreen>
2757
2758 The lower and upper arguments define the extents of the ruler, and
2759 max_size is the largest possible number that will be displayed.
2760 Position defines the initial position of the pointer indicator within
2761 the ruler.
2762
2763 A vertical ruler can span an 800 pixel wide window thus
2764
2765 <tscreen><verb>
2766 gtk_ruler_set_range( GTK_RULER(vruler), 0, 800, 0, 800);
2767 </verb></tscreen>
2768
2769 The markings displayed on the ruler will be from 0 to 800, with
2770 a number for every 100 pixels.  If instead we wanted the ruler to
2771 range from 7 to 16, we would code
2772
2773 <tscreen><verb>
2774 gtk_ruler_set_range( GTK_RULER(vruler), 7, 16, 0, 20);
2775 </verb></tscreen>
2776
2777 The indicator on the ruler is a small triangular mark that indicates
2778 the position of the pointer relative to the ruler.  If the ruler is
2779 used to follow the mouse pointer, the motion_notify_event signal
2780 should be connected to the motion_notify_event method of the ruler.
2781 To follow all mouse movements within a window area, we would use
2782
2783 <tscreen><verb>
2784 #define EVENT_METHOD(i, x) GTK_WIDGET_CLASS(GTK_OBJECT(i)->klass)->x
2785
2786 gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
2787          (GtkSignalFunc)EVENT_METHOD(ruler, motion_notify_event),
2788          GTK_OBJECT(ruler) );
2789 </verb></tscreen>
2790
2791 The following example creates a drawing area with a horizontal ruler
2792 above it and a vertical ruler to the left of it.  The size of the
2793 drawing area is 600 pixels wide by 400 pixels high.  The horizontal
2794 ruler spans from 7 to 13 with a mark every 100 pixels, while the
2795 vertical ruler spans from 0 to 400 with a mark every 100 pixels.
2796 Placement of the drawing area and the rulers are done using a table.
2797
2798 <tscreen><verb>
2799 /* rulers.c */
2800
2801 #include <gtk/gtk.h>
2802
2803 #define EVENT_METHOD(i, x) GTK_WIDGET_CLASS(GTK_OBJECT(i)->klass)->x
2804
2805 #define XSIZE  600
2806 #define YSIZE  400
2807
2808 /* this routine gets control when the close button is clicked
2809  */
2810 void close_application( GtkWidget *widget, gpointer *data ) {
2811     gtk_main_quit();
2812 }
2813
2814
2815 /* the main routine
2816  */
2817 int main( int argc, char *argv[] ) {
2818     GtkWidget *window, *table, *area, *hrule, *vrule;
2819
2820     /* initialize gtk and create the main window */
2821     gtk_init( &amp;argc, &amp;argv );
2822
2823     window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
2824     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
2825             GTK_SIGNAL_FUNC( close_application ), NULL);
2826     gtk_container_border_width (GTK_CONTAINER (window), 10);
2827
2828     /* create a table for placing the ruler and the drawing area */
2829     table = gtk_table_new( 3, 2, FALSE );
2830     gtk_container_add( GTK_CONTAINER(window), table );
2831
2832     area = gtk_drawing_area_new();
2833     gtk_drawing_area_size( (GtkDrawingArea *)area, XSIZE, YSIZE );
2834     gtk_table_attach( GTK_TABLE(table), area, 1, 2, 1, 2,
2835                       GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 );
2836     gtk_widget_set_events( area, GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK );
2837
2838     /* The horizontal ruler goes on top.  As the mouse moves across the drawing area,
2839        a motion_notify_event is passed to the appropriate event handler for the ruler. */
2840     hrule = gtk_hruler_new();
2841     gtk_ruler_set_metric( GTK_RULER(hrule), GTK_PIXELS );
2842     gtk_ruler_set_range( GTK_RULER(hrule), 7, 13, 0, 20 );
2843     gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
2844                                (GtkSignalFunc)EVENT_METHOD(hrule, motion_notify_event),
2845                                GTK_OBJECT(hrule) );
2846     /*  GTK_WIDGET_CLASS(GTK_OBJECT(hrule)->klass)->motion_notify_event, */
2847     gtk_table_attach( GTK_TABLE(table), hrule, 1, 2, 0, 1,
2848                       GTK_EXPAND|GTK_SHRINK|GTK_FILL, GTK_FILL, 0, 0 );
2849     
2850     /* The vertical ruler goes on the left.  As the mouse moves across the drawing area,
2851        a motion_notify_event is passed to the appropriate event handler for the ruler. */
2852     vrule = gtk_vruler_new();
2853     gtk_ruler_set_metric( GTK_RULER(vrule), GTK_PIXELS );
2854     gtk_ruler_set_range( GTK_RULER(vrule), 0, YSIZE, 10, YSIZE );
2855     gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
2856                                (GtkSignalFunc)
2857                                   GTK_WIDGET_CLASS(GTK_OBJECT(vrule)->klass)->motion_notify_event,
2858                                GTK_OBJECT(vrule) );
2859     gtk_table_attach( GTK_TABLE(table), vrule, 0, 1, 1, 2,
2860                       GTK_FILL, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0 );
2861
2862     /* now show everything */
2863     gtk_widget_show( area );
2864     gtk_widget_show( hrule );
2865     gtk_widget_show( vrule );
2866     gtk_widget_show( table );
2867     gtk_widget_show( window );
2868     gtk_main();
2869
2870     return 0;
2871 }
2872 </verb></tscreen>
2873
2874 <!-- ----------------------------------------------------------------- -->
2875 <sect1>Statusbars
2876 <p>
2877 Statusbars are simple widgets used to display a text message. They keep a stack 
2878 of the messages pushed onto them, so that popping the current message 
2879 will re-display the previous text message.
2880
2881 In order to allow different parts of an application to use the same statusbar to display
2882 messages, the statusbar widget issues Context Identifiers which are used to identify
2883 different 'users'. The message on top of the stack is the one displayed, no matter what context
2884 it is in. Messages are stacked in last-in-first-out order, not context identifier order.
2885
2886 A statusbar is created with a call to:
2887 <tscreen><verb>
2888 GtkWidget* gtk_statusbar_new (void);
2889 </verb></tscreen>
2890
2891 A new Context Identifier is requested using a call to the following function with a short
2892 textual description of the context:
2893 <tscreen><verb>
2894 guint gtk_statusbar_get_context_id (GtkStatusbar *statusbar,
2895                                     const gchar  *context_description);
2896 </verb></tscreen>
2897
2898 There are three functions that can operate on statusbars.
2899 <tscreen><verb>
2900 guint       gtk_statusbar_push           (GtkStatusbar *statusbar,
2901                                          guint          context_id,
2902                                          gchar         *text);
2903
2904 void       gtk_statusbar_pop            (GtkStatusbar *statusbar)
2905                                          guint         context_id);
2906 void       gtk_statusbar_remove         (GtkStatusbar *statusbar,
2907                                          guint         context_id,
2908                                          guint         message_id); 
2909 </verb></tscreen>
2910
2911 The first, gtk_statusbar_push, is used to add a new message to the statusbar. 
2912 It returns a Message Identifier, which can be passed later to the function gtk_statusbar_remove
2913 to remove the message with the given Message and Context Identifiers from the statusbar's stack.
2914
2915 The function gtk_statusbar_pop removes the message highest in the stack with the given
2916 Context Identifier.
2917
2918 The following example creates a statusbar and two buttons, one for pushing items
2919 onto the statusbar, and one for popping the last item back off.
2920
2921 <tscreen><verb>
2922 /* statusbar.c */
2923
2924 #include <gtk/gtk.h>
2925 #include <glib.h>
2926
2927 GtkWidget *status_bar;
2928
2929 void push_item (GtkWidget *widget, gpointer *data)
2930 {
2931   static int count = 1;
2932   char buff[20];
2933
2934   g_snprintf(buff, 20, "Item %d", count++);
2935   gtk_statusbar_push( GTK_STATUSBAR(status_bar), (guint) &amp;data, buff);
2936
2937   return;
2938 }
2939
2940 void pop_item (GtkWidget *widget, gpointer *data)
2941 {
2942   gtk_statusbar_pop( GTK_STATUSBAR(status_bar), (guint) &amp;data );
2943   return;
2944 }
2945
2946 int main (int argc, char *argv[])
2947 {
2948
2949     GtkWidget *window;
2950     GtkWidget *vbox;
2951     GtkWidget *button;
2952
2953     int context_id;
2954
2955     gtk_init (&amp;argc, &amp;argv);
2956
2957     /* create a new window */
2958     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2959     gtk_widget_set_usize( GTK_WIDGET (window), 200, 100);
2960     gtk_window_set_title(GTK_WINDOW (window), "GTK Statusbar Example");
2961     gtk_signal_connect(GTK_OBJECT (window), "delete_event",
2962                        (GtkSignalFunc) gtk_exit, NULL);
2963  
2964     vbox = gtk_vbox_new(FALSE, 1);
2965     gtk_container_add(GTK_CONTAINER(window), vbox);
2966     gtk_widget_show(vbox);
2967           
2968     status_bar = gtk_statusbar_new();      
2969     gtk_box_pack_start (GTK_BOX (vbox), status_bar, TRUE, TRUE, 0);
2970     gtk_widget_show (status_bar);
2971
2972     context_id = gtk_statusbar_get_context_id( GTK_STATUSBAR(status_bar), "Statusbar example");
2973
2974     button = gtk_button_new_with_label("push item");
2975     gtk_signal_connect(GTK_OBJECT(button), "clicked",
2976         GTK_SIGNAL_FUNC (push_item), &amp;context_id);
2977     gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 2);
2978     gtk_widget_show(button);              
2979
2980     button = gtk_button_new_with_label("pop last item");
2981     gtk_signal_connect(GTK_OBJECT(button), "clicked",
2982         GTK_SIGNAL_FUNC (pop_item), &amp;context_id);
2983     gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 2);
2984     gtk_widget_show(button);              
2985
2986     /* always display the window as the last step so it all splashes on
2987      * the screen at once. */
2988     gtk_widget_show(window);
2989
2990     gtk_main ();
2991
2992     return 0;
2993 }
2994 </verb></tscreen>
2995
2996 <!-- ----------------------------------------------------------------- -->
2997 <sect1>Text Entries
2998 <p>
2999 The Entry widget allows text to be typed and displayed in a single line text box. 
3000 The text may be set with functions calls that allow new text to replace, 
3001 prepend or append the current contents of the Entry widget.
3002
3003 There are two functions for creating Entry widgets:
3004 <tscreen><verb>
3005 GtkWidget* gtk_entry_new (void);
3006
3007 GtkWidget* gtk_entry_new_with_max_length (guint16 max);
3008 </verb></tscreen>
3009
3010 The first just creates a new Entry widget, whilst the second creates a new Entry and 
3011 sets a limit on the length of the text within the Entry..
3012
3013 There are several functions for altering the text which is currently within the Entry widget.
3014 <tscreen><verb>
3015 void gtk_entry_set_text       (GtkEntry    *entry,
3016                                const gchar *text);
3017 void gtk_entry_append_text    (GtkEntry    *entry,
3018                                const gchar *text);
3019 void gtk_entry_prepend_text   (GtkEntry    *entry,
3020                                const gchar *text);
3021 </verb></tscreen>
3022
3023 The function gtk_entry_set_text sets the contents of the Entry widget, replacing the
3024 current contents. The functions gtk_entry_append_text and gtk_entry_prepend_text allow
3025 the current contents to be appended and prepended to.
3026
3027 The next function allows the current insertion point to be set.
3028 <tscreen><verb>
3029 void gtk_entry_set_position   (GtkEntry *entry,
3030                                gint     position);
3031 </verb></tscreen>
3032
3033 The contents of the Entry can be retrieved by using a call to the following function. This
3034 is useful in the callback functions described below.
3035 <tscreen><verb>
3036 gchar* gtk_entry_get_text (GtkEntry *entry);
3037 </verb></tscreen>
3038
3039 If we don't want the contents of the Entry to be changed by someone typing into it, we
3040 can change it's edittable state.
3041 <tscreen><verb>
3042 void gtk_entry_set_editable (GtkEntry *entry,
3043                              gboolean editable);
3044 </verb></tscreen>
3045
3046 This function allows us to toggle the edittable state of the Entry widget by passing in
3047 TRUE or FALSE values for the editable argument.
3048
3049 If we are using the Entry where we don't want the text entered to be visible, for
3050 example when a password is being entered, we can use the following function, which
3051 also takes a boolean flag.
3052 <tscreen><verb>
3053 void gtk_entry_set_visibility (GtkEntry *entry,
3054                                gboolean visible);
3055 </verb></tscreen>
3056
3057 A region of the text may be set as selected by using the following function. This would
3058 most often be used after setting some default text in an Entry, making it easy for the user
3059 to remove it.
3060 <tscreen><verb>
3061 void gtk_entry_select_region (GtkEntry *entry,
3062                               gint     start,
3063                               gint     end);
3064 </verb></tscreen>
3065
3066 If we want to catch when the user has entered text, we can connect to the
3067 <tt/activate/ or <tt/changed/ signal. Activate is raised when the user hits 
3068 the enter key within the Entry widget. Changed is raised when the text changes at all, 
3069 e.g. for every character entered or removed.
3070
3071 The following code is an example of using an Entry widget.
3072 <tscreen><verb>
3073 /* entry.c */
3074
3075 #include <gtk/gtk.h>
3076
3077 void enter_callback(GtkWidget *widget, GtkWidget *entry)
3078 {
3079   gchar *entry_text;
3080   entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
3081   printf("Entry contents: %s\n", entry_text);
3082 }
3083
3084 void entry_toggle_editable (GtkWidget *checkbutton,
3085                                    GtkWidget *entry)
3086 {
3087   gtk_entry_set_editable(GTK_ENTRY(entry),
3088                          GTK_TOGGLE_BUTTON(checkbutton)->active);
3089 }
3090
3091 void entry_toggle_visibility (GtkWidget *checkbutton,
3092                                    GtkWidget *entry)
3093 {
3094   gtk_entry_set_visibility(GTK_ENTRY(entry),
3095                          GTK_TOGGLE_BUTTON(checkbutton)->active);
3096 }
3097
3098 int main (int argc, char *argv[])
3099 {
3100
3101     GtkWidget *window;
3102     GtkWidget *vbox, *hbox;
3103     GtkWidget *entry;
3104     GtkWidget *button;
3105     GtkWidget *check;
3106
3107     gtk_init (&amp;argc, &amp;argv);
3108
3109     /* create a new window */
3110     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
3111     gtk_widget_set_usize( GTK_WIDGET (window), 200, 100);
3112     gtk_window_set_title(GTK_WINDOW (window), "GTK Entry");
3113     gtk_signal_connect(GTK_OBJECT (window), "delete_event",
3114                        (GtkSignalFunc) gtk_exit, NULL);
3115
3116     vbox = gtk_vbox_new (FALSE, 0);
3117     gtk_container_add (GTK_CONTAINER (window), vbox);
3118     gtk_widget_show (vbox);
3119
3120     entry = gtk_entry_new_with_max_length (50);
3121     gtk_signal_connect(GTK_OBJECT(entry), "activate",
3122                        GTK_SIGNAL_FUNC(enter_callback),
3123                        entry);
3124     gtk_entry_set_text (GTK_ENTRY (entry), "hello");
3125     gtk_entry_append_text (GTK_ENTRY (entry), " world");
3126     gtk_entry_select_region (GTK_ENTRY (entry),
3127                              0, GTK_ENTRY(entry)->text_length);
3128     gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);
3129     gtk_widget_show (entry);
3130
3131     hbox = gtk_hbox_new (FALSE, 0);
3132     gtk_container_add (GTK_CONTAINER (vbox), hbox);
3133     gtk_widget_show (hbox);
3134                                   
3135     check = gtk_check_button_new_with_label("Editable");
3136     gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0);
3137     gtk_signal_connect (GTK_OBJECT(check), "toggled",
3138                         GTK_SIGNAL_FUNC(entry_toggle_editable), entry);
3139     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(check), TRUE);
3140     gtk_widget_show (check);
3141     
3142     check = gtk_check_button_new_with_label("Visible");
3143     gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0);
3144     gtk_signal_connect (GTK_OBJECT(check), "toggled",
3145                         GTK_SIGNAL_FUNC(entry_toggle_visibility), entry);
3146     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(check), TRUE);
3147     gtk_widget_show (check);
3148                                    
3149     button = gtk_button_new_with_label ("Close");
3150     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3151                                GTK_SIGNAL_FUNC(gtk_exit),
3152                                GTK_OBJECT (window));
3153     gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
3154     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
3155     gtk_widget_grab_default (button);
3156     gtk_widget_show (button);
3157     
3158     gtk_widget_show(window);
3159
3160     gtk_main();
3161     return(0);
3162 }
3163 </verb></tscreen>
3164
3165 <!-- ----------------------------------------------------------------- -->
3166 <sect1> Color Selection
3167 <P>
3168 The color selection widget is, not surprisingly, a widget for interactive
3169 selection of colors. This composite widget lets the user select a color by manipulating
3170 RGB (Red, Green, Blue) and HSV (Hue, Saturation, Value) triples. This is done
3171 either by adjusting single values with sliders or entries, or by picking the desired
3172 color from a hue-saturation wheel/value bar. Optionally, the opacity of the color can also
3173 be set.
3174
3175 The color selection widget currently emits only one signal, "color_changed", which is emitted
3176 whenever the current color in the widget changes, either when the user changes it or if
3177 it's set explicitly through gtk_color_selection_set_color().
3178
3179 Lets have a look at what the color selection widget has to offer us. The widget comes
3180 in two flavours; gtk_color_selection and gtk_color_selection_dialog:
3181
3182 <tscreen><verb>
3183 GtkWidget *gtk_color_selection_new(void);
3184 </verb></tscreen>
3185         
3186 You'll probably not be using this constructor directly. It creates an orphan
3187 GtkColorSelection widget which you'll have to parent yourself. The GtkColorSelection widget
3188 inherits from the GtkVBox widget.
3189
3190 <tscreen><verb> 
3191 GtkWidget *gtk_color_selection_dialog_new(const gchar *title);
3192 </verb></tscreen>
3193
3194 This is the most common color selection constructor. It creates a GtkColorSelectionDialog, which
3195 inherits from a GtkDialog. It consists of a GtkFrame containing a GtkColorSelection widget, a
3196 GtkHSeparator and a GtkHBox with three buttons, "Ok", "Cancel" and "Help". You can reach these
3197 buttons by accessing the "ok_button", "cancel_button" and "help_button" widgets in the
3198 GtkColorSelectionDialog structure, (i.e. GTK_COLOR_SELECTION_DIALOG(colorseldialog)->ok_button).
3199
3200 <tscreen><verb>
3201 void gtk_color_selection_set_update_policy(GtkColorSelection *colorsel, 
3202                                            GtkUpdateType policy);
3203 </verb></tscreen>
3204
3205 This function sets the update policy. The default policy is GTK_UPDATE_CONTINOUS which means that
3206 the current color is updated continously when the user drags the sliders or presses the mouse and drags
3207 in the hue-saturation wheel or value bar. If you experience performance problems, you may
3208 want to set the policy to GTK_UPDATE_DISCONTINOUS or GTK_UPDATE_DELAYED.
3209
3210 <tscreen><verb>
3211 void gtk_color_selection_set_opacity(GtkColorSelection *colorsel,
3212                                      gint use_opacity);
3213 </verb></tscreen>
3214
3215 The color selection widget supports adjusting the opacity of a color (also known as the alpha channel).
3216 This is disabled by default. Calling this function with use_opacity set to TRUE enables opacity.
3217 Likewise, use_opacity set to FALSE will disable opacity.
3218
3219 <tscreen><verb>
3220 void gtk_color_selection_set_color(GtkColorSelection *colorsel,
3221                                    gdouble *color);
3222 </verb></tscreen>
3223
3224 You can set the current color explicitly by calling this function with a pointer to an array
3225 of colors (gdouble). The length of the array depends on whether opacity is enabled or not.
3226 Position 0 contains the red component, 1 is green, 2 is blue and opacity is at position 3 (only if
3227 opacity is enabled, see gtk_color_selection_set_opacity()). All values are between 0.0 and 1.0.
3228
3229 <tscreen><verb>
3230 void gtk_color_selection_get_color(GtkColorSelection *colorsel,
3231                                    gdouble *color);
3232 </verb></tscreen>
3233
3234 When you need to query the current color, typically when you've received a "color_changed" signal,
3235 you use this function. Color is a pointer to the array of colors to fill in. See the
3236 gtk_color_selection_set_color() function for the description of this array.
3237
3238 <!-- Need to do a whole section on DnD - TRG
3239 Drag and drop
3240 -------------
3241
3242 The color sample areas (right under the hue-saturation wheel) supports drag and drop. The type of
3243 drag and drop is "application/x-color". The message data consists of an array of 4
3244 (or 5 if opacity is enabled) gdouble values, where the value at position 0 is 0.0 (opacity
3245 on) or 1.0 (opacity off) followed by the red, green and blue values at positions 1,2 and 3 respectively.
3246 If opacity is enabled, the opacity is passed in the value at position 4.
3247 -->
3248
3249 Here's a simple example demonstrating the use of the GtkColorSelectionDialog. The program displays a window
3250 containing a drawing area. Clicking on it opens a color selection dialog, and changing the color in the
3251 color selection dialog changes the background color.
3252
3253 <tscreen><verb>
3254 #include <glib.h>
3255 #include <gdk/gdk.h>
3256 #include <gtk/gtk.h>
3257
3258 GtkWidget *colorseldlg = NULL;
3259 GtkWidget *drawingarea = NULL;
3260
3261 /* Color changed handler */
3262
3263 void color_changed_cb (GtkWidget *widget, GtkColorSelection *colorsel)
3264 {
3265   gdouble color[3];
3266   GdkColor gdk_color;
3267   GdkColormap *colormap;
3268
3269   /* Get drawingarea colormap */
3270
3271   colormap = gdk_window_get_colormap (drawingarea->window);
3272
3273   /* Get current color */
3274
3275   gtk_color_selection_get_color (colorsel,color);
3276
3277   /* Fit to a unsigned 16 bit integer (0..65535) and insert into the GdkColor structure */
3278
3279   gdk_color.red = (guint16)(color[0]*65535.0);
3280   gdk_color.green = (guint16)(color[1]*65535.0);
3281   gdk_color.blue = (guint16)(color[2]*65535.0);
3282
3283   /* Allocate color */
3284
3285   gdk_color_alloc (colormap, &amp;gdk_color);
3286
3287   /* Set window background color */
3288
3289   gdk_window_set_background (drawingarea->window, &amp;gdk_color);
3290
3291   /* Clear window */
3292
3293   gdk_window_clear (drawingarea->window);
3294 }
3295
3296 /* Drawingarea event handler */
3297
3298 gint area_event (GtkWidget *widget, GdkEvent *event, gpointer client_data)
3299 {
3300   gint handled = FALSE;
3301   GtkWidget *colorsel;
3302
3303   /* Check if we've received a button pressed event */
3304
3305   if (event->type == GDK_BUTTON_PRESS &amp;&amp; colorseldlg == NULL)
3306     {
3307       /* Yes, we have an event and there's no colorseldlg yet! */
3308
3309       handled = TRUE;
3310
3311       /* Create color selection dialog */
3312
3313       colorseldlg = gtk_color_selection_dialog_new("Select background color");
3314
3315       /* Get the GtkColorSelection widget */
3316
3317       colorsel = GTK_COLOR_SELECTION_DIALOG(colorseldlg)->colorsel;
3318
3319       /* Connect to the "color_changed" signal, set the client-data to the colorsel widget */
3320
3321       gtk_signal_connect(GTK_OBJECT(colorsel), "color_changed",
3322         (GtkSignalFunc)color_changed_cb, (gpointer)colorsel);
3323
3324       /* Show the dialog */
3325
3326       gtk_widget_show(colorseldlg);
3327     }
3328
3329   return handled;
3330 }
3331
3332 /* Close down and exit handler */
3333
3334 void destroy_window (GtkWidget *widget, gpointer client_data)
3335 {
3336   gtk_main_quit ();
3337 }
3338
3339 /* Main */
3340
3341 gint main (gint argc, gchar *argv[])
3342 {
3343   GtkWidget *window;
3344
3345   /* Initialize the toolkit, remove gtk-related commandline stuff */
3346
3347   gtk_init (&amp;argc,&amp;argv);
3348
3349   /* Create toplevel window, set title and policies */
3350
3351   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
3352   gtk_window_set_title (GTK_WINDOW(window), "Color selection test");
3353   gtk_window_set_policy (GTK_WINDOW(window), TRUE, TRUE, TRUE);
3354
3355   /* Attach to the "delete" and "destroy" events so we can exit */
3356
3357   gtk_signal_connect (GTK_OBJECT(window), "delete_event",
3358     (GtkSignalFunc)destroy_window, (gpointer)window);
3359
3360   gtk_signal_connect (GTK_OBJECT(window), "destroy",
3361     (GtkSignalFunc)destroy_window, (gpointer)window);
3362   
3363   /* Create drawingarea, set size and catch button events */
3364
3365   drawingarea = gtk_drawing_area_new ();
3366
3367   gtk_drawing_area_size (GTK_DRAWING_AREA(drawingarea), 200, 200);
3368
3369   gtk_widget_set_events (drawingarea, GDK_BUTTON_PRESS_MASK);
3370
3371   gtk_signal_connect (GTK_OBJECT(drawingarea), "event", 
3372     (GtkSignalFunc)area_event, (gpointer)drawingarea);
3373   
3374   /* Add drawingarea to window, then show them both */
3375
3376   gtk_container_add (GTK_CONTAINER(window), drawingarea);
3377
3378   gtk_widget_show (drawingarea);
3379   gtk_widget_show (window);
3380   
3381   /* Enter the gtk main loop (this never returns) */
3382
3383   gtk_main ();
3384
3385   /* Satisfy grumpy compilers */
3386
3387   return 0;
3388 }
3389 </verb></tscreen>
3390 <!-- ----------------------------------------------------------------- -->
3391 <sect1> File Selections
3392 <p>
3393 The file selection widget is a quick and simple way to display a File 
3394 dialog box.  It comes complete with Ok, Cancel, and Help buttons, a great way
3395 to cut down on programming time.
3396
3397 To create a new file selection box use:
3398
3399 <tscreen><verb>
3400 GtkWidget* gtk_file_selection_new (gchar *title);
3401 </verb></tscreen>
3402
3403 To set the filename, for example to bring up a specific directory, or
3404 give a default filename, use this function: 
3405
3406 <tscreen><verb>
3407 void gtk_file_selection_set_filename (GtkFileSelection *filesel, gchar *filename);
3408 </verb></tscreen>
3409
3410 To grab the text that the user has entered or clicked on, use this 
3411 function:
3412
3413 <tscreen><verb>
3414 gchar* gtk_file_selection_get_filename (GtkFileSelection *filesel);
3415 </verb></tscreen>
3416
3417 There are also pointers to the widgets contained within the file 
3418 selection widget. These are:
3419
3420 <itemize>
3421 <item>dir_list
3422 <item>file_list
3423 <item>selection_entry
3424 <item>selection_text
3425 <item>main_vbox
3426 <item>ok_button
3427 <item>cancel_button
3428 <item>help_button
3429 </itemize>
3430
3431 Most likely you will want to use the ok_button, cancel_button, and
3432 help_button pointers in signaling their use.
3433
3434 Included here is an example stolen from testgtk.c, modified to run
3435 on it's own.  As you will see, there is nothing much to creating a file 
3436 selection widget.  While, in this example, the Help button appears on the 
3437 screen, it does nothing as there is not a signal attached to it. 
3438
3439 <tscreen><verb>
3440 /* filesel.c */
3441
3442 #include <gtk/gtk.h>
3443
3444 /* Get the selected filename and print it to the console */
3445 void file_ok_sel (GtkWidget *w, GtkFileSelection *fs)
3446 {
3447     g_print ("%s\n", gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs)));
3448 }
3449
3450 void destroy (GtkWidget *widget, gpointer *data)
3451 {
3452     gtk_main_quit ();
3453 }
3454
3455 int main (int argc, char *argv[])
3456 {
3457     GtkWidget *filew;
3458     
3459     gtk_init (&amp;argc, &amp;argv);
3460     
3461     /* Create a new file selection widget */
3462     filew = gtk_file_selection_new ("File selection");
3463     
3464     gtk_signal_connect (GTK_OBJECT (filew), "destroy",
3465                         (GtkSignalFunc) destroy, &amp;filew);
3466     /* Connect the ok_button to file_ok_sel function */
3467     gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),
3468                         "clicked", (GtkSignalFunc) file_ok_sel, filew );
3469     
3470     /* Connect the cancel_button to destroy the widget */
3471     gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (filew)->cancel_button),
3472                                "clicked", (GtkSignalFunc) gtk_widget_destroy,
3473                                GTK_OBJECT (filew));
3474     
3475     /* Lets set the filename, as if this were a save dialog, and we are giving
3476      a default filename */
3477     gtk_file_selection_set_filename (GTK_FILE_SELECTION(filew), 
3478                                      "penguin.png");
3479     
3480     gtk_widget_show(filew);
3481     gtk_main ();
3482     return 0;
3483 }
3484 </verb></tscreen>
3485
3486 <!-- ***************************************************************** -->
3487 <sect> Container Widgets
3488 <!-- ***************************************************************** -->
3489
3490 <!-- ----------------------------------------------------------------- -->
3491 <sect1> Notebooks
3492 <p>
3493 The NoteBook Widget is a collection of 'pages' that overlap each other,
3494 each page contains different information.  This widget has become more common 
3495 lately in GUI programming, and it is a good way to show blocks similar 
3496 information that warrant separation in their display.
3497
3498 The first function call you will need to know, as you can probably 
3499 guess by now, is used to create a new notebook widget.
3500
3501 <tscreen><verb>
3502 GtkWidget* gtk_notebook_new (void);
3503 </verb></tscreen>
3504
3505 Once the notebook has been created, there are 12 functions that 
3506 operate on the notebook widget.  Let's look at them individually.
3507
3508 The first one we will look at is how to position the page indicators.
3509 These page indicators or 'tabs' as they are referred to, can be positioned
3510 in four ways; top, bottom, left, or right.
3511
3512 <tscreen><verb>
3513 void gtk_notebook_set_tab_pos (GtkNotebook *notebook, GtkPositionType pos);
3514 </verb></tscreen>
3515
3516 GtkPostionType will be one of the following, and they are pretty self explanatory.
3517 <itemize>
3518 <item> GTK_POS_LEFT
3519 <item> GTK_POS_RIGHT
3520 <item> GTK_POS_TOP
3521 <item> GTK_POS_BOTTOM
3522 </itemize>
3523
3524 GTK_POS_TOP is the default.
3525
3526 Next we will look at how to add pages to the notebook.  There are three
3527 ways to add pages to the NoteBook.  Let's look at the first two together as 
3528 they are quite similar.
3529
3530 <tscreen><verb>
3531 void gtk_notebook_append_page (GtkNotebook *notebook, GtkWidget *child, GtkWidget *tab_label);
3532
3533 void gtk_notebook_prepend_page (GtkNotebook *notebook, GtkWidget *child, GtkWidget *tab_label);
3534 </verb></tscreen>
3535
3536 These functions add pages to the notebook by inserting them from the 
3537 back of the notebook (append), or the front of the notebook (prepend).  
3538 *child is the widget that is placed within the notebook page, and *tab_label is
3539 the label for the page being added.
3540
3541 The final function for adding a page to the notebook contains all of 
3542 the properties of the previous two, but it allows you to specify what position
3543 you want the page to be in the notebook.
3544
3545 <tscreen><verb>
3546 void gtk_notebook_insert_page (GtkNotebook *notebook, GtkWidget *child, GtkWidget *tab_label, gint position);
3547 </verb></tscreen>
3548
3549 The parameters are the same as _append_ and _prepend_ except it 
3550 contains an extra parameter, position.  This parameter is used to specify what
3551 place this page will inserted to.
3552
3553 Now that we know how to add a page, lets see how we can remove a page 
3554 from the notebook.
3555
3556 <tscreen><verb>
3557 void gtk_notebook_remove_page (GtkNotebook *notebook, gint page_num);
3558 </verb></tscreen>
3559
3560 This function takes the page specified by page_num and removes it from 
3561 the widget *notebook.
3562
3563 To find out what the current page is in a notebook use the function:
3564
3565 <tscreen><verb>
3566 gint gtk_notebook_current_page (GtkNotebook *notebook);
3567 </verb></tscreen>
3568
3569 These next two functions are simple calls to move the notebook page 
3570 forward or backward.  Simply provide the respective function call with the
3571 notebook widget you wish to operate on.  Note:  When the NoteBook is currently
3572 on the last page, and gtk_notebook_next_page is called, the notebook will 
3573 wrap back to the first page.  Likewise, if the NoteBook is on the first page, 
3574 and gtk_notebook_prev_page is called, the notebook will wrap to the last page.
3575
3576 <tscreen><verb>
3577 void gtk_notebook_next_page (GtkNoteBook *notebook);
3578 void gtk_notebook_prev_page (GtkNoteBook *notebook);
3579 </verb></tscreen>
3580
3581 This next function sets the 'active' page.  If you wish the
3582 notebook to be opened to page 5 for example, you would use this function.  
3583 Without using this function, the notebook defaults to the first page.
3584
3585 <tscreen><verb>
3586 void gtk_notebook_set_page (GtkNotebook *notebook, gint page_num);
3587 </verb></tscreen>
3588
3589 The next two functions add or remove the notebook page tabs and the 
3590 notebook border respectively.
3591
3592 <tscreen><verb>
3593 void gtk_notebook_set_show_tabs (GtkNotebook *notebook, gint show_tabs);
3594 void gtk_notebook_set_show_border (GtkNotebook *notebook, gint show_border);
3595 </verb></tscreen>
3596
3597 show_tabs and show_border can both be either TRUE or FALSE (0 or 1).
3598
3599 Now lets look at an example, it is expanded from the testgtk.c code 
3600 that comes with the GTK distribution, and it shows all 13 functions. This 
3601 small program, creates a window with a notebook and six buttons.  The notebook 
3602 contains 11 pages, added in three different ways, appended, inserted, and 
3603 prepended.  The buttons allow you rotate the tab positions, add/remove the tabs
3604 and border, remove a page, change pages in both a forward and backward manner,
3605 and exit the program. 
3606
3607 <tscreen><verb>
3608 /* notebook.c */
3609
3610 #include <gtk/gtk.h>
3611
3612 /* This function rotates the position of the tabs */
3613 void rotate_book (GtkButton *button, GtkNotebook *notebook)
3614 {
3615     gtk_notebook_set_tab_pos (notebook, (notebook->tab_pos +1) %4);
3616 }
3617
3618 /* Add/Remove the page tabs and the borders */
3619 void tabsborder_book (GtkButton *button, GtkNotebook *notebook)
3620 {
3621     gint tval = FALSE;
3622     gint bval = FALSE;
3623     if (notebook->show_tabs == 0)
3624             tval = TRUE; 
3625     if (notebook->show_border == 0)
3626             bval = TRUE;
3627     
3628     gtk_notebook_set_show_tabs (notebook, tval);
3629     gtk_notebook_set_show_border (notebook, bval);
3630 }
3631
3632 /* Remove a page from the notebook */
3633 void remove_book (GtkButton *button, GtkNotebook *notebook)
3634 {
3635     gint page;
3636     
3637     page = gtk_notebook_current_page(notebook);
3638     gtk_notebook_remove_page (notebook, page);
3639     /* Need to refresh the widget -- 
3640      This forces the widget to redraw itself. */
3641     gtk_widget_draw(GTK_WIDGET(notebook), NULL);
3642 }
3643
3644 void delete (GtkWidget *widget, gpointer *data)
3645 {
3646     gtk_main_quit ();
3647 }
3648
3649 int main (int argc, char *argv[])
3650 {
3651     GtkWidget *window;
3652     GtkWidget *button;
3653     GtkWidget *table;
3654     GtkWidget *notebook;
3655     GtkWidget *frame;
3656     GtkWidget *label;
3657     GtkWidget *checkbutton;
3658     int i;
3659     char bufferf[32];
3660     char bufferl[32];
3661     
3662     gtk_init (&amp;argc, &amp;argv);
3663     
3664     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
3665     
3666     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
3667                         GTK_SIGNAL_FUNC (delete), NULL);
3668     
3669     gtk_container_border_width (GTK_CONTAINER (window), 10);
3670     
3671     table = gtk_table_new(2,6,TRUE);
3672     gtk_container_add (GTK_CONTAINER (window), table);
3673     
3674     /* Create a new notebook, place the position of the tabs */
3675     notebook = gtk_notebook_new ();
3676     gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
3677     gtk_table_attach_defaults(GTK_TABLE(table), notebook, 0,6,0,1);
3678     gtk_widget_show(notebook);
3679     
3680     /* lets append a bunch of pages to the notebook */
3681     for (i=0; i < 5; i++) {
3682         sprintf(bufferf, "Append Frame %d", i+1);
3683         sprintf(bufferl, "Page %d", i+1);
3684         
3685         frame = gtk_frame_new (bufferf);
3686         gtk_container_border_width (GTK_CONTAINER (frame), 10);
3687         gtk_widget_set_usize (frame, 100, 75);
3688         gtk_widget_show (frame);
3689         
3690         label = gtk_label_new (bufferf);
3691         gtk_container_add (GTK_CONTAINER (frame), label);
3692         gtk_widget_show (label);
3693         
3694         label = gtk_label_new (bufferl);
3695         gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
3696     }
3697     
3698     
3699     /* now lets add a page to a specific spot */
3700     checkbutton = gtk_check_button_new_with_label ("Check me please!");
3701     gtk_widget_set_usize(checkbutton, 100, 75);
3702     gtk_widget_show (checkbutton);
3703     
3704     label = gtk_label_new ("Add spot");
3705     gtk_container_add (GTK_CONTAINER (checkbutton), label);
3706     gtk_widget_show (label);
3707     label = gtk_label_new ("Add page");
3708     gtk_notebook_insert_page (GTK_NOTEBOOK (notebook), checkbutton, label, 2);
3709     
3710     /* Now finally lets prepend pages to the notebook */
3711     for (i=0; i < 5; i++) {
3712         sprintf(bufferf, "Prepend Frame %d", i+1);
3713         sprintf(bufferl, "PPage %d", i+1);
3714         
3715         frame = gtk_frame_new (bufferf);
3716         gtk_container_border_width (GTK_CONTAINER (frame), 10);
3717         gtk_widget_set_usize (frame, 100, 75);
3718         gtk_widget_show (frame);
3719         
3720         label = gtk_label_new (bufferf);
3721         gtk_container_add (GTK_CONTAINER (frame), label);
3722         gtk_widget_show (label);
3723         
3724         label = gtk_label_new (bufferl);
3725         gtk_notebook_prepend_page (GTK_NOTEBOOK(notebook), frame, label);
3726     }
3727     
3728     /* Set what page to start at (page 4) */
3729     gtk_notebook_set_page (GTK_NOTEBOOK(notebook), 3);
3730     
3731     
3732     /* create a bunch of buttons */
3733     button = gtk_button_new_with_label ("close");
3734     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3735                                GTK_SIGNAL_FUNC (delete), NULL);
3736     gtk_table_attach_defaults(GTK_TABLE(table), button, 0,1,1,2);
3737     gtk_widget_show(button);
3738     
3739     button = gtk_button_new_with_label ("next page");
3740     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3741                                (GtkSignalFunc) gtk_notebook_next_page,
3742                                GTK_OBJECT (notebook));
3743     gtk_table_attach_defaults(GTK_TABLE(table), button, 1,2,1,2);
3744     gtk_widget_show(button);
3745     
3746     button = gtk_button_new_with_label ("prev page");
3747     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3748                                (GtkSignalFunc) gtk_notebook_prev_page,
3749                                GTK_OBJECT (notebook));
3750     gtk_table_attach_defaults(GTK_TABLE(table), button, 2,3,1,2);
3751     gtk_widget_show(button);
3752     
3753     button = gtk_button_new_with_label ("tab position");
3754     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3755                                (GtkSignalFunc) rotate_book, GTK_OBJECT(notebook));
3756     gtk_table_attach_defaults(GTK_TABLE(table), button, 3,4,1,2);
3757     gtk_widget_show(button);
3758     
3759     button = gtk_button_new_with_label ("tabs/border on/off");
3760     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3761                                (GtkSignalFunc) tabsborder_book,
3762                                GTK_OBJECT (notebook));
3763     gtk_table_attach_defaults(GTK_TABLE(table), button, 4,5,1,2);
3764     gtk_widget_show(button);
3765     
3766     button = gtk_button_new_with_label ("remove page");
3767     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3768                                (GtkSignalFunc) remove_book,
3769                                GTK_OBJECT(notebook));
3770     gtk_table_attach_defaults(GTK_TABLE(table), button, 5,6,1,2);
3771     gtk_widget_show(button);
3772     
3773     gtk_widget_show(table);
3774     gtk_widget_show(window);
3775     
3776     gtk_main ();
3777     
3778     return 0;
3779 }
3780 </verb></tscreen>
3781 <p>
3782 Hopefully this helps you on your way with creating notebooks for your
3783 GTK applications.
3784
3785 <!-- ----------------------------------------------------------------- -->
3786 <sect1> Scrolled Windows
3787 <p>
3788 Scrolled windows are used to create a scrollable area inside a real window.  
3789 You may insert any types of widgets to these scrolled windows, and they will
3790 all be accessable regardless of the size by using the scrollbars.
3791
3792 The following function is used to create a new scolled window.
3793
3794 <tscreen><verb>
3795 GtkWidget* gtk_scrolled_window_new (GtkAdjustment *hadjustment,
3796                                     GtkAdjustment *vadjustment);
3797 </verb></tscreen>
3798 <p>
3799 Where the first argument is the adjustment for the horizontal
3800 direction, and the second, the adjustment for the vertical direction.
3801 These are almost always set to NULL.
3802
3803 <tscreen><verb>
3804 void gtk_scrolled_window_set_policy      (GtkScrolledWindow *scrolled_window,
3805                                           GtkPolicyType      hscrollbar_policy,
3806                                                                                   GtkPolicyType      vscrollbar_policy);
3807 </verb></tscreen>
3808
3809 This sets the policy to be used with respect to the scrollbars.
3810 The first arguement is the scrolled window you wish to change. The second
3811 sets the policiy for the horizontal scrollbar, and the third, 
3812 the vertical scrollbar.  
3813
3814 The policy may be one of GTK_POLICY AUTOMATIC, or GTK_POLICY_ALWAYS.
3815 GTK_POLICY_AUTOMATIC will automatically decide whether you need
3816 scrollbars, wheras GTK_POLICY_ALWAYS will always leave the scrollbars
3817 there.
3818
3819 Here is a simple example that packs 100 toggle buttons into a scrolled window.
3820 I've only commented on the parts that may be new to you.
3821
3822 <tscreen><verb>
3823 /* scrolledwin.c */
3824
3825 #include <gtk/gtk.h>
3826
3827 void destroy(GtkWidget *widget, gpointer *data)
3828 {
3829     gtk_main_quit();
3830 }
3831
3832 int main (int argc, char *argv[])
3833 {
3834     static GtkWidget *window;
3835     GtkWidget *scrolled_window;
3836     GtkWidget *table;
3837     GtkWidget *button;
3838     char buffer[32];
3839     int i, j;
3840     
3841     gtk_init (&amp;argc, &amp;argv);
3842     
3843     /* Create a new dialog window for the scrolled window to be
3844      * packed into.  A dialog is just like a normal window except it has a 
3845      * vbox and a horizontal seperator packed into it.  It's just a shortcut
3846      * for creating dialogs */
3847     window = gtk_dialog_new ();
3848     gtk_signal_connect (GTK_OBJECT (window), "destroy",
3849                         (GtkSignalFunc) destroy, NULL);
3850     gtk_window_set_title (GTK_WINDOW (window), "dialog");
3851     gtk_container_border_width (GTK_CONTAINER (window), 0);
3852     gtk_widget_set_usize(window, 300, 300);
3853     
3854     /* create a new scrolled window. */
3855     scrolled_window = gtk_scrolled_window_new (NULL, NULL);
3856     
3857     gtk_container_border_width (GTK_CONTAINER (scrolled_window), 10);
3858     
3859     /* the policy is one of GTK_POLICY AUTOMATIC, or GTK_POLICY_ALWAYS.
3860      * GTK_POLICY_AUTOMATIC will automatically decide whether you need
3861      * scrollbars, wheras GTK_POLICY_ALWAYS will always leave the scrollbars
3862      * there.  The first one is the horizontal scrollbar, the second, 
3863      * the vertical. */
3864     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
3865                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
3866     /* The dialog window is created with a vbox packed into it. */                                                              
3867     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, 
3868                         TRUE, TRUE, 0);
3869     gtk_widget_show (scrolled_window);
3870     
3871     /* create a table of 10 by 10 squares. */
3872     table = gtk_table_new (10, 10, FALSE);
3873     
3874     /* set the spacing to 10 on x and 10 on y */
3875     gtk_table_set_row_spacings (GTK_TABLE (table), 10);
3876     gtk_table_set_col_spacings (GTK_TABLE (table), 10);
3877     
3878     /* pack the table into the scrolled window */
3879     gtk_container_add (GTK_CONTAINER (scrolled_window), table);
3880     gtk_widget_show (table);
3881     
3882     /* this simply creates a grid of toggle buttons on the table
3883      * to demonstrate the scrolled window. */
3884     for (i = 0; i < 10; i++)
3885             for (j = 0; j < 10; j++) {
3886                 sprintf (buffer, "button (%d,%d)\n", i, j);
3887                 button = gtk_toggle_button_new_with_label (buffer);
3888                 gtk_table_attach_defaults (GTK_TABLE (table), button,
3889                                            i, i+1, j, j+1);
3890                 gtk_widget_show (button);
3891             }
3892     
3893     /* Add a "close" button to the bottom of the dialog */
3894     button = gtk_button_new_with_label ("close");
3895     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3896                                (GtkSignalFunc) gtk_widget_destroy,
3897                                GTK_OBJECT (window));
3898     
3899     /* this makes it so the button is the default. */
3900     
3901     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
3902     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), button, TRUE, TRUE, 0);
3903     
3904     /* This grabs this button to be the default button. Simply hitting
3905      * the "Enter" key will cause this button to activate. */
3906     gtk_widget_grab_default (button);
3907     gtk_widget_show (button);
3908     
3909     gtk_widget_show (window);
3910     
3911     gtk_main();
3912     
3913     return(0);
3914 }
3915 </verb></tscreen>
3916 <p>
3917 Try playing with resizing the window.  You'll notice how the scrollbars
3918 react.  You may also wish to use the gtk_widget_set_usize() call to set the default
3919 size of the window or other widgets.
3920
3921 <!-- ***************************************************************** -->
3922 <sect> List Widgets
3923 <!-- ***************************************************************** -->
3924
3925 <p>
3926 The GtkList widget is designed to act as a vertical container for widgets
3927 that should be of the type GtkListItem.
3928
3929 A GtkList widget has its own window to receive events and it's own
3930 background color which is usualy white.  As it is directly derived from a
3931 GtkContainer it can be treated as such by using the GTK_CONTAINER(List)
3932 macro, see the GtkContainer widget for more on this.
3933 One should already be familar whith the usage of a GList and its
3934 related functions g_list_*() to be able to use the GtkList widget to
3935 its fully extends.
3936
3937 There is one field inside the structure definition of the GtkList widget
3938 that will be of greater interest to us, this is:
3939
3940 <tscreen><verb>
3941 struct _GtkList
3942 {
3943   ...
3944   GList *selection;
3945   guint selection_mode;
3946   ...
3947 }; 
3948 </verb></tscreen>
3949
3950 The selection field of a GtkList points to a linked list of all items
3951 that are cureently selected, or `NULL' if the selection is empty.
3952 So to learn about the current selection we read the GTK_LIST()->selection
3953 field, but do not modify it since the internal fields are maintained by
3954 the gtk_list_*() functions.
3955
3956 The selection_mode of the GtkList determines the selection facilities
3957 of a GtkList and therefore the contents of the GTK_LIST()->selection
3958 field:
3959
3960 The selection_mode may be one of the following:
3961 <itemize>
3962 <item> GTK_SELECTION_SINGLE -   The selection is either `NULL'
3963                         or contains a GList* pointer
3964                         for a single selected item.
3965
3966 <item> GTK_SELECTION_BROWSE -  The selection is `NULL' if the list
3967                         contains no widgets or insensitive
3968                         ones only, otherwise it contains
3969                         a GList pointer for one GList
3970                         structure, and therefore exactly
3971                         one list item.
3972
3973 <item> GTK_SELECTION_MULTIPLE -  The selection is `NULL' if no list
3974                         items are selected or a GList pointer
3975                         for the first selected item. That
3976                         in turn points to a GList structure
3977                         for the second selected item and so
3978                         on.
3979
3980 <item> GTK_SELECTION_EXTENDED - The selection is always `NULL'.
3981 </itemize>
3982 <p>
3983 The default is GTK_SELECTION_MULTIPLE.
3984
3985 <!-- ----------------------------------------------------------------- -->
3986 <sect1> Signals
3987 <p>
3988 <tscreen><verb>
3989 void selection_changed (GtkList *LIST)
3990 </verb></tscreen>
3991
3992 This signal will be invoked whenever a the selection field
3993 of a GtkList has changed. This happens when a child of
3994 the GtkList got selected or unselected.
3995
3996 <tscreen><verb>
3997 void select_child (GtkList *LIST, GtkWidget *CHILD)
3998 </verb></tscreen>
3999
4000 This signal is invoked when a child of the GtkList is about
4001 to get selected. This happens mainly on calls to
4002 gtk_list_select_item(), gtk_list_select_child(), button presses
4003 and sometimes indirectly triggered on some else occasions where
4004 children get added to or removed from the GtkList.
4005
4006 <tscreen><verb>
4007 void unselect_child (GtkList *LIST, GtkWidget *CHILD)
4008 </verb></tscreen>
4009
4010 This signal is invoked when a child of the GtkList is about
4011 to get unselected. This happens mainly on calls to
4012 gtk_list_unselect_item(), gtk_list_unselect_child(), button presses
4013 and sometimes indirectly triggered on some else occasions where
4014 children get added to or removed from the GtkList.
4015
4016 <!-- ----------------------------------------------------------------- -->
4017 <sect1> Functions
4018 <p>
4019 <tscreen><verb>
4020 guint gtk_list_get_type (void)
4021 </verb></tscreen>
4022
4023 Returns the `GtkList' type identifier.
4024
4025 <tscreen><verb>
4026 GtkWidget* gtk_list_new (void)
4027 </verb></tscreen>
4028
4029 Create a new `GtkList' object. The new widget is
4030 returned as a pointer to a `GtkWidget' object.
4031 `NULL' is returned on failure.
4032
4033 <tscreen><verb>
4034 void gtk_list_insert_items (GtkList *LIST, GList *ITEMS, gint POSITION)
4035 </verb></tscreen>
4036
4037 Insert list items into the LIST, starting at POSITION.
4038 ITEMS is a doubly linked list where each nodes data
4039 pointer is expected to point to a newly created GtkListItem.
4040 The GList nodes of ITEMS are taken over by the LIST.
4041
4042 <tscreen><verb>
4043 void gtk_list_append_items (GtkList *LIST, GList *ITEMS)
4044 </verb></tscreen>
4045
4046 Insert list items just like gtk_list_insert_items() at the end
4047 of the LIST. The GList nodes of ITEMS are taken over by the LIST.
4048
4049 <tscreen><verb>
4050 void gtk_list_prepend_items (GtkList *LIST, GList *ITEMS)
4051 </verb></tscreen>
4052
4053 Insert list items just like gtk_list_insert_items() at the very
4054 beginning of the LIST. The GList nodes of ITEMS are taken over
4055 by the LIST.
4056
4057 <tscreen><verb>
4058 void gtk_list_remove_items (GtkList *LIST, GList *ITEMS)
4059 </verb></tscreen>
4060
4061 Remove list items from the LIST. ITEMS is a doubly linked
4062 list where each nodes data pointer is expected to point to a
4063 direct child of LIST. It is the callers responsibility to make a
4064 call to g_list_free(ITEMS) afterwards. Also the caller has to
4065 destroy the list items himself.
4066
4067 <tscreen><verb>
4068 void gtk_list_clear_items (GtkList *LIST, gint START, gint END)
4069 </verb></tscreen>
4070
4071 Remove and destroy list items from the LIST. a widget is affected if
4072 its current position within LIST is in the range specified by START
4073 and END.
4074
4075 <tscreen><verb>
4076 void gtk_list_select_item (GtkList *LIST, gint ITEM)
4077 </verb></tscreen>
4078
4079 Invoke the select_child signal for a list item
4080 specified through its current position within LIST.
4081
4082 <tscreen><verb>
4083 void gtk_list_unselect_item (GtkList *LIST, gint ITEM)
4084 </verb></tscreen>
4085
4086 Invoke the unselect_child signal for a list item
4087 specified through its current position within LIST.
4088
4089 <tscreen><verb>
4090 void gtk_list_select_child (GtkList *LIST, GtkWidget *CHILD)
4091 </verb></tscreen>
4092
4093 Invoke the select_child signal for the specified CHILD.
4094
4095 <tscreen><verb>
4096 void gtk_list_unselect_child (GtkList *LIST, GtkWidget *CHILD)
4097 </verb></tscreen>
4098
4099 Invoke the unselect_child signal for the specified CHILD.
4100
4101 <tscreen><verb>
4102 gint gtk_list_child_position (GtkList *LIST, GtkWidget *CHILD)
4103 </verb></tscreen>
4104
4105 Return the position of CHILD within LIST. `-1' is returned on failure.
4106
4107 <tscreen><verb>
4108 void gtk_list_set_selection_mode (GtkList *LIST, GtkSelectionMode MODE)
4109 </verb></tscreen>
4110
4111 Set LIST to the selection mode MODE wich can be of GTK_SELECTION_SINGLE,
4112 GTK_SELECTION_BROWSE, GTK_SELECTION_MULTIPLE or GTK_SELECTION_EXTENDED.
4113
4114 <tscreen><verb>
4115 GtkList* GTK_LIST (gpointer OBJ)
4116 </verb></tscreen>
4117
4118 Cast a generic pointer to `GtkList*'. *Note Standard Macros::, for
4119 more info.
4120
4121 <tscreen><verb>
4122 GtkListClass* GTK_LIST_CLASS (gpointer CLASS)
4123 </verb></tscreen>
4124
4125 Cast a generic pointer to `GtkListClass*'. *Note Standard Macros::,
4126 for more info.
4127
4128 <tscreen><verb>
4129 gint GTK_IS_LIST (gpointer OBJ)
4130 </verb></tscreen>
4131
4132 Determine if a generic pointer refers to a `GtkList' object. *Note
4133 Standard Macros::, for more info.
4134
4135 <!-- ----------------------------------------------------------------- -->
4136 <sect1> Example
4137 <p>
4138 Following is an example program that will print out the changes
4139 of the selection of a GtkList, and lets you "arrest" list items
4140 into a prison by selecting them with the rightmost mouse button:
4141
4142 <tscreen><verb>
4143 /* list.c */
4144
4145 /* include the gtk+ header files
4146  * include stdio.h, we need that for the printf() function
4147  */
4148 #include        <gtk/gtk.h>
4149 #include        <stdio.h>
4150
4151 /* this is our data identification string to store
4152  * data in list items
4153  */
4154 const   gchar   *list_item_data_key="list_item_data";
4155
4156
4157 /* prototypes for signal handler that we are going to connect
4158  * to the GtkList widget
4159  */
4160 static  void    sigh_print_selection    (GtkWidget      *gtklist,
4161                                          gpointer       func_data);
4162 static  void    sigh_button_event       (GtkWidget      *gtklist,
4163                                          GdkEventButton *event,
4164                                          GtkWidget      *frame);
4165
4166
4167 /* main function to set up the user interface */
4168
4169 gint main (int argc, gchar *argv[])
4170 {                                  
4171     GtkWidget       *separator;
4172     GtkWidget       *window;
4173     GtkWidget       *vbox;
4174     GtkWidget       *scrolled_window;
4175     GtkWidget       *frame;
4176     GtkWidget       *gtklist;
4177     GtkWidget       *button;
4178     GtkWidget       *list_item;
4179     GList           *dlist;
4180     guint           i;
4181     gchar           buffer[64];
4182     
4183     
4184     /* initialize gtk+ (and subsequently gdk) */
4185
4186     gtk_init(&amp;argc, &amp;argv);
4187     
4188     
4189     /* create a window to put all the widgets in
4190      * connect gtk_main_quit() to the "destroy" event of
4191      * the window to handle window manager close-window-events
4192      */
4193     window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
4194     gtk_window_set_title(GTK_WINDOW(window), "GtkList Example");
4195     gtk_signal_connect(GTK_OBJECT(window),
4196                        "destroy",
4197                        GTK_SIGNAL_FUNC(gtk_main_quit),
4198                        NULL);
4199     
4200     
4201     /* inside the window we need a box to arrange the widgets
4202      * vertically */
4203     vbox=gtk_vbox_new(FALSE, 5);
4204     gtk_container_border_width(GTK_CONTAINER(vbox), 5);
4205     gtk_container_add(GTK_CONTAINER(window), vbox);
4206     gtk_widget_show(vbox);
4207     
4208     /* this is the scolled window to put the GtkList widget inside */
4209     scrolled_window=gtk_scrolled_window_new(NULL, NULL);
4210     gtk_widget_set_usize(scrolled_window, 250, 150);
4211     gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
4212     gtk_widget_show(scrolled_window);
4213     
4214     /* create the GtkList widget
4215      * connect the sigh_print_selection() signal handler
4216      * function to the "selection_changed" signal of the GtkList
4217      * to print out the selected items each time the selection
4218      * has changed */
4219     gtklist=gtk_list_new();
4220     gtk_container_add(GTK_CONTAINER(scrolled_window), gtklist);
4221     gtk_widget_show(gtklist);
4222     gtk_signal_connect(GTK_OBJECT(gtklist),
4223                        "selection_changed",
4224                        GTK_SIGNAL_FUNC(sigh_print_selection),
4225                        NULL);
4226     
4227     /* we create a "Prison" to put a list item in ;)
4228      */
4229     frame=gtk_frame_new("Prison");
4230     gtk_widget_set_usize(frame, 200, 50);
4231     gtk_container_border_width(GTK_CONTAINER(frame), 5);
4232     gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
4233     gtk_container_add(GTK_CONTAINER(vbox), frame);
4234     gtk_widget_show(frame);
4235     
4236     /* connect the sigh_button_event() signal handler to the GtkList
4237      * wich will handle the "arresting" of list items
4238      */
4239     gtk_signal_connect(GTK_OBJECT(gtklist),
4240                        "button_release_event",
4241                        GTK_SIGNAL_FUNC(sigh_button_event),
4242                        frame);
4243     
4244     /* create a separator
4245      */
4246     separator=gtk_hseparator_new();
4247     gtk_container_add(GTK_CONTAINER(vbox), separator);
4248     gtk_widget_show(separator);
4249     
4250     /* finaly create a button and connect it´s "clicked" signal
4251      * to the destroyment of the window
4252      */
4253     button=gtk_button_new_with_label("Close");
4254     gtk_container_add(GTK_CONTAINER(vbox), button);
4255     gtk_widget_show(button);
4256     gtk_signal_connect_object(GTK_OBJECT(button),
4257                               "clicked",
4258                               GTK_SIGNAL_FUNC(gtk_widget_destroy),
4259                               GTK_OBJECT(window));
4260     
4261     
4262     /* now we create 5 list items, each having it´s own
4263      * label and add them to the GtkList using gtk_container_add()
4264      * also we query the text string from the label and
4265      * associate it with the list_item_data_key for each list item
4266      */
4267     for (i=0; i<5; i++) {
4268         GtkWidget       *label;
4269         gchar           *string;
4270         
4271         sprintf(buffer, "ListItemContainer with Label #%d", i);
4272         label=gtk_label_new(buffer);
4273         list_item=gtk_list_item_new();
4274         gtk_container_add(GTK_CONTAINER(list_item), label);
4275         gtk_widget_show(label);
4276         gtk_container_add(GTK_CONTAINER(gtklist), list_item);
4277         gtk_widget_show(list_item);
4278         gtk_label_get(GTK_LABEL(label), &amp;string);
4279         gtk_object_set_data(GTK_OBJECT(list_item),
4280                             list_item_data_key,
4281                             string);
4282     }
4283     /* here, we are creating another 5 labels, this time
4284      * we use gtk_list_item_new_with_label() for the creation
4285      * we can´t query the text string from the label because
4286      * we don´t have the labels pointer and therefore
4287      * we just associate the list_item_data_key of each
4288      * list item with the same text string
4289      * for adding of the list items we put them all into a doubly
4290      * linked list (GList), and then add them by a single call to
4291      * gtk_list_append_items()
4292      * because we use g_list_prepend() to put the items into the
4293      * doubly linked list, their order will be descending (instead
4294      * of ascending when using g_list_append())
4295      */
4296     dlist=NULL;
4297     for (; i<10; i++) {
4298         sprintf(buffer, "List Item with Label %d", i);
4299         list_item=gtk_list_item_new_with_label(buffer);
4300         dlist=g_list_prepend(dlist, list_item);
4301         gtk_widget_show(list_item);
4302         gtk_object_set_data(GTK_OBJECT(list_item),
4303                             list_item_data_key,
4304                             "ListItem with integrated Label");
4305     }
4306     gtk_list_append_items(GTK_LIST(gtklist), dlist);
4307     
4308     /* finaly we want to see the window, don´t we? ;)
4309      */
4310     gtk_widget_show(window);
4311     
4312     /* fire up the main event loop of gtk
4313      */
4314     gtk_main();
4315     
4316     /* we get here after gtk_main_quit() has been called which
4317      * happens if the main window gets destroyed
4318      */
4319     return 0;
4320 }
4321
4322 /* this is the signal handler that got connected to button
4323  * press/release events of the GtkList
4324  */
4325 void
4326 sigh_button_event       (GtkWidget      *gtklist,
4327                          GdkEventButton *event,
4328                          GtkWidget      *frame)
4329 {
4330     /* we only do something if the third (rightmost mouse button
4331      * was released
4332      */
4333     if (event->type==GDK_BUTTON_RELEASE &amp;&amp;
4334         event->button==3) {
4335         GList           *dlist, *free_list;
4336         GtkWidget       *new_prisoner;
4337         
4338         /* fetch the currently selected list item which
4339          * will be our next prisoner ;)
4340          */
4341         dlist=GTK_LIST(gtklist)->selection;
4342         if (dlist)
4343                 new_prisoner=GTK_WIDGET(dlist->data);
4344         else
4345                 new_prisoner=NULL;
4346         
4347         /* look for already prisoned list items, we
4348          * will put them back into the list
4349          * remember to free the doubly linked list that
4350          * gtk_container_children() returns
4351          */
4352         dlist=gtk_container_children(GTK_CONTAINER(frame));
4353         free_list=dlist;
4354         while (dlist) {
4355             GtkWidget       *list_item;
4356             
4357             list_item=dlist->data;
4358             
4359             gtk_widget_reparent(list_item, gtklist);
4360             
4361             dlist=dlist->next;
4362         }
4363         g_list_free(free_list);
4364         
4365         /* if we have a new prisoner, remove him from the
4366          * GtkList and put him into the frame "Prison"
4367          * we need to unselect the item before
4368          */
4369         if (new_prisoner) {
4370             GList   static_dlist;
4371             
4372             static_dlist.data=new_prisoner;
4373             static_dlist.next=NULL;
4374             static_dlist.prev=NULL;
4375             
4376             gtk_list_unselect_child(GTK_LIST(gtklist),
4377                                     new_prisoner);
4378             gtk_widget_reparent(new_prisoner, frame);
4379         }
4380     }
4381 }
4382
4383 /* this is the signal handler that gets called if GtkList
4384  * emits the "selection_changed" signal
4385  */
4386 void
4387 sigh_print_selection    (GtkWidget      *gtklist,
4388                          gpointer       func_data)
4389 {
4390     GList   *dlist;
4391     
4392     /* fetch the doubly linked list of selected items
4393      * of the GtkList, remember to treat this as read-only!
4394      */
4395     dlist=GTK_LIST(gtklist)->selection;
4396     
4397     /* if there are no selected items there is nothing more
4398      * to do than just telling the user so
4399      */
4400     if (!dlist) {
4401         g_print("Selection cleared\n");
4402         return;
4403     }
4404     /* ok, we got a selection and so we print it
4405      */
4406     g_print("The selection is a ");
4407     
4408     /* get the list item from the doubly linked list
4409      * and then query the data associated with list_item_data_key
4410      * we then just print it
4411      */
4412     while (dlist) {
4413         GtkObject       *list_item;
4414         gchar           *item_data_string;
4415         
4416         list_item=GTK_OBJECT(dlist->data);
4417         item_data_string=gtk_object_get_data(list_item,
4418                                              list_item_data_key);
4419         g_print("%s ", item_data_string);
4420         
4421         dlist=dlist->next;
4422     }
4423     g_print("\n");
4424 }
4425 </verb></tscreen>
4426
4427 <!-- ----------------------------------------------------------------- -->
4428 <sect1> List Item Widget
4429 <p>
4430 The GtkListItem widget is designed to act as a container holding up
4431 to one child, providing functions for selection/deselection just like
4432 the GtkList widget requires them for its children.
4433
4434 A GtkListItem has its own window to receive events and has its own
4435 background color which is usualy white.  
4436
4437 As it is directly derived from a
4438 GtkItem it can be treated as such by using the GTK_ITEM(ListItem)
4439 macro, see the GtkItem widget for more on this.
4440 Usualy a GtkListItem just holds a label to identify e.g. a filename
4441 within a GtkList -- therefore the convenient function
4442 gtk_list_item_new_with_label() is provided.  The same effect can be
4443 achieved by creating a GtkLabel on its own, setting its alignment
4444 to xalign=0 and yalign=0.5 with a subsequent container addition
4445 to the GtkListItem.
4446
4447 As one is not forced to add a GtkLabel to a GtkListItem, you could
4448 also add a GtkVBox or a GtkArrow etc. to the GtkListItem.
4449
4450 <!-- ----------------------------------------------------------------- -->
4451 <sect1> Signals
4452 <p>
4453 A GtkListItem does not create new signals on its own, but inherits
4454 the signals of a GtkItem. *Note GtkItem::, for more info.
4455
4456 <!-- ----------------------------------------------------------------- -->
4457 <sect1> Functions
4458 <p>
4459
4460 <tscreen><verb>
4461 guint gtk_list_item_get_type (void)
4462 </verb></tscreen>
4463
4464 Returns the `GtkListItem' type identifier.
4465
4466 <tscreen><verb>
4467 GtkWidget* gtk_list_item_new (void)
4468 </verb></tscreen>
4469
4470 Create a new `GtkListItem' object. The new widget is
4471 returned as a pointer to a `GtkWidget' object.
4472 `NULL' is returned on failure.
4473
4474 <tscreen><verb>
4475 GtkWidget* gtk_list_item_new_with_label (gchar *LABEL)
4476 </verb></tscreen>
4477
4478 Create a new `GtkListItem' object, having a single GtkLabel as
4479 the sole child. The new widget is returned as a pointer to a
4480 `GtkWidget' object.
4481 `NULL' is returned on failure.
4482
4483 <tscreen><verb>
4484 void gtk_list_item_select (GtkListItem *LIST_ITEM)
4485 </verb></tscreen>
4486
4487 This function is basicaly a wrapper around a call to
4488 gtk_item_select (GTK_ITEM (list_item)) which will emit the
4489 select signal.
4490 *Note GtkItem::, for more info.
4491
4492 <tscreen><verb>
4493 void gtk_list_item_deselect (GtkListItem *LIST_ITEM)
4494 </verb></tscreen>
4495
4496 This function is basicaly a wrapper around a call to
4497 gtk_item_deselect (GTK_ITEM (list_item)) which will emit the
4498 deselect signal.
4499 *Note GtkItem::, for more info.
4500
4501 <tscreen><verb>
4502 GtkListItem* GTK_LIST_ITEM (gpointer OBJ)
4503 </verb></tscreen>
4504
4505 Cast a generic pointer to `GtkListItem*'. *Note Standard Macros::,
4506 for more info.
4507
4508 <tscreen><verb>
4509 GtkListItemClass* GTK_LIST_ITEM_CLASS (gpointer CLASS)
4510 </verb></tscreen>
4511
4512 Cast a generic pointer to `GtkListItemClass*'. *Note Standard
4513 Macros::, for more info.
4514
4515 <tscreen><verb>
4516 gint GTK_IS_LIST_ITEM (gpointer OBJ)
4517 </verb></tscreen>
4518
4519 Determine if a generic pointer refers to a `GtkListItem' object.
4520 *Note Standard Macros::, for more info.
4521  
4522 <!-- ----------------------------------------------------------------- -->
4523 <sect1> Example
4524 <p>
4525 Please see the GtkList example on this, which covers the usage of a
4526 GtkListItem as well.
4527
4528 <!-- ***************************************************************** -->
4529 <sect>Menu Widgets
4530 <!-- ***************************************************************** -->
4531
4532 <p>
4533 There are two ways to create menus, there's the easy way, and there's the
4534 hard way.  Both have their uses, but you can usually use the menufactory
4535 (the easy way).  The "hard" way is to create all the menus using the calls
4536 directly.  The easy way is to use the gtk_menu_factory calls.  This is
4537 much simpler, but there are advantages and disadvantages to each approach.
4538
4539 The menufactory is much easier to use, and to add new menus to, although
4540 writing a few wrapper functions to create menus using the manual method 
4541 could go a long way towards usability.  With the menufactory, it is not 
4542 possible to add images or the character '/' to the menus.
4543
4544 <!-- ----------------------------------------------------------------- -->
4545 <sect1>Manual Menu Creation
4546 <p>
4547 In the true tradition of teaching, we'll show you the hard
4548 way first. <tt>:)</>
4549 <p>
4550 There are three widgets that go into making a menubar and submenus:
4551 <itemize>
4552 <item>a menu item, which is what the user wants to select, e.g. 'Save'
4553 <item>a menu, which acts as a container for the menu items, and
4554 <item>a menubar, which is a container for each of the individual menus,
4555 </itemize>
4556
4557 This is slightly complicated by the fact that menu item widgets are used for two different things. They are
4558 both the widets that are packed into the menu, and the widget that is packed into the menubar, which,
4559 when selected, activiates the menu.
4560
4561 Let's look at the functions that are used to create menus and menubars.
4562 This first function is used to create a new menubar.
4563
4564 <tscreen><verb>
4565 GtkWidget *gtk_menu_bar_new(void);
4566 </verb></tscreen>
4567
4568 This rather self explanatory function creates a new menubar.  You use
4569 gtk_container_add to pack this into a window, or the box_pack functions to
4570 pack it into a box - the same as buttons.
4571
4572 <tscreen><verb>
4573 GtkWidget *gtk_menu_new();
4574 </verb></tscreen>
4575
4576 This function returns a pointer to a new menu, it is never actually shown
4577 (with gtk_widget_show), it is just a container for the menu items.  Hopefully this will
4578 become more clear when you look at the example below.
4579 <p>
4580 The next two calls are used to create menu items that are packed into
4581 the menu (and menubar).
4582
4583 <tscreen><verb>
4584 GtkWidget *gtk_menu_item_new();
4585 </verb></tscreen>
4586
4587 and
4588
4589 <tscreen><verb>
4590 GtkWidget *gtk_menu_item_new_with_label(const char *label);
4591 </verb></tscreen>
4592
4593 These calls are used to create the menu items that are to be displayed.
4594 Remember to differentiate between a "menu" as created with gtk_menu_new
4595 and a "menu item" as created by the gtk_menu_item_new functions.  The
4596 menu item will be an actual button with an associated action,
4597 whereas a menu will be a container holding menu items.
4598
4599 The gtk_menu_new_with_label and gtk_menu_new functions are just as you'd expect after
4600 reading about the buttons.  One creates a new menu item with a label
4601 already packed into it, and the other just creates a blank menu item.
4602
4603 Once you've created a menu item you have to put it into a menu. This is done using the function
4604 gtk_menu_append. In order to capture when the item is selected by the user, we need to connect
4605 to the <tt/activate/ signal in the usual way.
4606 So, if we wanted to create a standard <tt/File/ menu, with the options <tt/Open/, 
4607 <tt/Save/ and <tt/Quit/ the code would look something like
4608
4609 <tscreen><verb>
4610 file_menu = gtk_menu_new();    /* Don't need to show menus */
4611
4612 /* Create the menu items */
4613 open_item = gtk_menu_item_new_with_label("Open");
4614 save_item = gtk_menu_item_new_with_label("Save");
4615 quit_item = gtk_menu_item_new_with_label("Quit");
4616
4617 /* Add them to the menu */
4618 gtk_menu_append( GTK_MENU(file_menu), open_item);
4619 gtk_menu_append( GTK_MENU(file_menu), save_item);
4620 gtk_menu_append( GTK_MENU(file_menu), quit_item);
4621
4622 /* Attach the callback functions to the activate signal */
4623 gtk_signal_connect_object( GTK_OBJECT(open_items), "activate",
4624                            GTK_SIGNAL_FUNC(menuitem_response), (gpointer) "file.open");
4625 gtk_signal_connect_object( GTK_OBJECT(save_items), "activate",
4626                            GTK_SIGNAL_FUNC(menuitem_response), (gpointer) "file.save");
4627
4628 /* We can attach the Quit menu item to our exit function */
4629 gtk_signal_connect_object( GTK_OBJECT(quit_items), "activate",
4630                            GTK_SIGNAL_FUNC(destroy), (gpointer) "file.quit");
4631
4632 /* We do need to show menu items */
4633 gtk_widget_show( open_item );
4634 gtk_widget_show( save_item );
4635 gtk_widget_show( quit_item );
4636 </verb></tscreen>
4637
4638 At this point we have our menu. Now we need to create a menubar and a menu item for the <tt/File/ entry,
4639 to which we add our menu. The code looks like this 
4640
4641 <tscreen><verb>
4642 menu_bar = gtk_menu_bar_new();
4643 gtk_container_add( GTK_CONTAINER(window), menu_bar);
4644 gtk_widget_show( menu_bar );
4645
4646 file_item = gtk_menu_item_new_with_label("File");
4647 gtk_widget_show(file_item);
4648 </verb></tscreen>
4649
4650 Now we need to associate the menu with <tt/file_item/. This is done with the function
4651
4652 <tscreen>
4653 void gtk_menu_item_set_submenu( GtkMenuItem *menu_item,
4654                                 GtkWidget *submenu);
4655 </tscreen>
4656
4657 So, our example would continue with
4658
4659 <tscreen><verb>
4660 gtk_menu_item_set_submenu( GTK_MENU_ITEM(file_item), file_menu);
4661 </verb></tscreen>
4662
4663 All that is left to do is to add the menu to the menubar, which is accomplished using the function
4664
4665 <tscreen>
4666 void gtk_menu_bar_append( GtkMenuBar *menu_bar, GtkWidget *menu_item);
4667 </tscreen>
4668
4669 which in our case looks like this:
4670
4671 <tscreen><verb>
4672 gtk_menu_bar_append( menu_bar, file_item );
4673 </verb></tscreen>
4674
4675 If we wanted the menu right justified on the menubar, such as help menus often are, we can
4676 use the following function (again on <tt/file_item/ in the current example) before attaching 
4677 it to the menubar.
4678 <tscreen><verb>
4679 void gtk_menu_item_right_justify (GtkMenuItem *menu_item);
4680 </verb></tscreen>
4681
4682 Here is a summary of the steps needed to create a menu bar with menus attached:
4683 <itemize>
4684 <item>  Create a new menu using gtk_menu_new()
4685 <item>  Use multiple calls to gtk_menu_item_new() for each item you wish to have on
4686   your menu.  And use gtk_menu_append() to put each of these new items on
4687   to the menu.
4688 <item>  Create a menu item using gtk_menu_item_new().  This will be the root of
4689   the menu, the text appearing here will be on the menubar itself.
4690 <item>  Use gtk_menu_item_set_submenu() to attach the menu to
4691   the root menu item (The one created in the above step).
4692 <item>  Create a new menubar using gtk_menu_bar_new.  This step only needs
4693   to be done once when creating a series of menus on one menu bar.
4694 <item> Use gtk_menu_bar_append to put the root menu onto the menubar.
4695 </itemize>
4696 <p>
4697 Creating a popup menu is nearly the same.  The difference is that the
4698 menu is not posted `automatically' by a menubar, but explicitly
4699 by calling the function gtk_menu_popup() from a button-press event, for example.
4700 Take these steps:
4701 <itemize>
4702 <item>Create an event handling function.  It needs to have the prototype
4703 <tscreen>
4704 static gint handler(GtkWidget *widget, GdkEvent *event);
4705 </tscreen>
4706 and it will use the event to find out where to pop up the menu.
4707 <item>In the event handler, if event is a mouse button press, treat
4708 <tt>event</tt> as a button event (which it is) and use it as
4709 shown in the sample code to pass information to gtk_menu_popup().
4710 <item>Bind that event handler to a widget with
4711 <tscreen>
4712 gtk_signal_connect_object(GTK_OBJECT(widget), "event",
4713                           GTK_SIGNAL_FUNC (handler), GTK_OBJECT(menu));
4714 </tscreen>
4715 where <tt>widget</tt> is the widget you are binding to, <tt>handler</tt>
4716 is the handling function, and <tt>menu</tt> is a menu created with
4717 gtk_menu_new().  This can be a menu which is also posted by a menu bar,
4718 as shown in the sample code.
4719 </itemize>
4720
4721 <!-- ----------------------------------------------------------------- -->
4722 <sect1>Manual Menu Example
4723 <p>
4724 That should about do it.  Let's take a look at an example to help clarify.
4725
4726 <tscreen><verb>
4727 /* menu.c */
4728
4729 #include <gtk/gtk.h>
4730
4731 static gint button_press (GtkWidget *, GdkEvent *);
4732 static void menuitem_response (gchar *);
4733
4734 int main (int argc, char *argv[])
4735 {
4736
4737     GtkWidget *window;
4738     GtkWidget *menu;
4739     GtkWidget *menu_bar;
4740     GtkWidget *root_menu;
4741     GtkWidget *menu_items;
4742     GtkWidget *vbox;
4743     GtkWidget *button;
4744     char buf[128];
4745     int i;
4746
4747     gtk_init (&amp;argc, &amp;argv);
4748
4749     /* create a new window */
4750     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4751     gtk_widget_set_usize( GTK_WIDGET (window), 200, 100);
4752     gtk_window_set_title(GTK_WINDOW (window), "GTK Menu Test");
4753     gtk_signal_connect(GTK_OBJECT (window), "delete_event",
4754                        (GtkSignalFunc) gtk_exit, NULL);
4755
4756     /* Init the menu-widget, and remember -- never
4757      * gtk_show_widget() the menu widget!! 
4758      * This is the menu that holds the menu items, the one that
4759      * will pop up when you click on the "Root Menu" in the app */
4760     menu = gtk_menu_new();
4761
4762     /* Next we make a little loop that makes three menu-entries for "test-menu".
4763      * Notice the call to gtk_menu_append.  Here we are adding a list of
4764      * menu items to our menu.  Normally, we'd also catch the "clicked"
4765      * signal on each of the menu items and setup a callback for it,
4766      * but it's omitted here to save space. */
4767
4768     for(i = 0; i < 3; i++)
4769         {
4770             /* Copy the names to the buf. */
4771             sprintf(buf, "Test-undermenu - %d", i);
4772
4773             /* Create a new menu-item with a name... */
4774             menu_items = gtk_menu_item_new_with_label(buf);
4775
4776             /* ...and add it to the menu. */
4777             gtk_menu_append(GTK_MENU (menu), menu_items);
4778
4779             /* Do something interesting when the menuitem is selected */
4780             gtk_signal_connect_object(GTK_OBJECT(menu_items), "activate",
4781                 GTK_SIGNAL_FUNC(menuitem_response), (gpointer) g_strdup(buf));
4782
4783             /* Show the widget */
4784             gtk_widget_show(menu_items);
4785         }
4786
4787     /* This is the root menu, and will be the label
4788      * displayed on the menu bar.  There won't be a signal handler attached,
4789      * as it only pops up the rest of the menu when pressed. */
4790     root_menu = gtk_menu_item_new_with_label("Root Menu");
4791
4792     gtk_widget_show(root_menu);
4793
4794     /* Now we specify that we want our newly created "menu" to be the menu
4795      * for the "root menu" */
4796     gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu);
4797
4798     /* A vbox to put a menu and a button in: */
4799     vbox = gtk_vbox_new(FALSE, 0);
4800     gtk_container_add(GTK_CONTAINER(window), vbox);
4801     gtk_widget_show(vbox);
4802
4803     /* Create a menu-bar to hold the menus and add it to our main window */
4804     menu_bar = gtk_menu_bar_new();
4805     gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 2);
4806     gtk_widget_show(menu_bar);
4807
4808     /* Create a button to which to attach menu as a popup */
4809     button = gtk_button_new_with_label("press me");
4810     gtk_signal_connect_object(GTK_OBJECT(button), "event",
4811         GTK_SIGNAL_FUNC (button_press), GTK_OBJECT(menu));
4812     gtk_box_pack_end(GTK_BOX(vbox), button, TRUE, TRUE, 2);
4813     gtk_widget_show(button);
4814
4815     /* And finally we append the menu-item to the menu-bar -- this is the
4816      * "root" menu-item I have been raving about =) */
4817     gtk_menu_bar_append(GTK_MENU_BAR (menu_bar), root_menu);
4818
4819     /* always display the window as the last step so it all splashes on
4820      * the screen at once. */
4821     gtk_widget_show(window);
4822
4823     gtk_main ();
4824
4825     return 0;
4826 }
4827
4828
4829
4830 /* Respond to a button-press by posting a menu passed in as widget.
4831  *
4832  * Note that the "widget" argument is the menu being posted, NOT
4833  * the button that was pressed.
4834  */
4835
4836 static gint button_press (GtkWidget *widget, GdkEvent *event)
4837 {
4838
4839     if (event->type == GDK_BUTTON_PRESS) {
4840         GdkEventButton *bevent = (GdkEventButton *) event; 
4841         gtk_menu_popup (GTK_MENU(widget), NULL, NULL, NULL, NULL,
4842                         bevent->button, bevent->time);
4843         /* Tell calling code that we have handled this event; the buck
4844          * stops here. */
4845         return TRUE;
4846     }
4847
4848     /* Tell calling code that we have not handled this event; pass it on. */
4849     return FALSE;
4850 }
4851
4852
4853 /* Print a string when a menu item is selected */
4854
4855 static void menuitem_response (gchar *string)
4856 {
4857     printf("%s\n", string);
4858 }
4859 </verb></tscreen>
4860
4861 You may also set a menu item to be insensitive and, using an accelerator
4862 table, bind keys to menu functions.
4863
4864 <!-- ----------------------------------------------------------------- -->
4865 <sect1>Using GtkMenuFactory
4866 <p>
4867 Now that we've shown you the hard way, here's how you do it using the
4868 gtk_menu_factory calls.
4869
4870 <!-- ----------------------------------------------------------------- -->
4871 <sect1>Menu Factory Example
4872 <p>
4873 Here is an example using the GTK menu factory.  This is the first file,
4874 menufactory.h.  We keep a separate menufactory.c and mfmain.c because of the global variables used
4875 in the menufactory.c file.  
4876
4877 <tscreen><verb>
4878 /* menufactory.h */
4879
4880 #ifndef __MENUFACTORY_H__
4881 #define __MENUFACTORY_H__
4882
4883 #ifdef __cplusplus
4884 extern "C" {
4885 #endif /* __cplusplus */
4886
4887 void get_main_menu (GtkWidget **menubar, GtkAcceleratorTable **table);
4888 void menus_create(GtkMenuEntry *entries, int nmenu_entries);
4889
4890 #ifdef __cplusplus
4891 }
4892 #endif /* __cplusplus */
4893
4894 #endif /* __MENUFACTORY_H__ */
4895 </verb></tscreen>
4896 <p>
4897 And here is the menufactory.c file.
4898
4899 <tscreen><verb>
4900 /* menufactory.c */
4901
4902 #include <gtk/gtk.h>
4903 #include <strings.h>
4904
4905 #include "mfmain.h"
4906
4907
4908 static void menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * path);
4909 static gint menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar key, gchar modifiers, gchar * path);
4910 void menus_init(void);
4911 void menus_create(GtkMenuEntry * entries, int nmenu_entries);
4912
4913
4914 /* this is the GtkMenuEntry structure used to create new menus.  The
4915  * first member is the menu definition string.  The second, the
4916  * default accelerator key used to access this menu function with
4917  * the keyboard.  The third is the callback function to call when
4918  * this menu item is selected (by the accelerator key, or with the
4919  * mouse.) The last member is the data to pass to your callback function.
4920  */
4921
4922 static GtkMenuEntry menu_items[] =
4923 {
4924         {"<Main>/File/New", "<control>N", NULL, NULL},
4925         {"<Main>/File/Open", "<control>O", NULL, NULL},
4926         {"<Main>/File/Save", "<control>S", NULL, NULL},
4927         {"<Main>/File/Save as", NULL, NULL, NULL},
4928         {"<Main>/File/<separator>", NULL, NULL, NULL},
4929         {"<Main>/File/Quit", "<control>Q", file_quit_cmd_callback, "OK, I'll quit"},
4930         {"<Main>/Options/Test", NULL, NULL, NULL}
4931 };
4932
4933 /* calculate the number of menu_item's */
4934 static int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
4935
4936 static int initialize = TRUE;
4937 static GtkMenuFactory *factory = NULL;
4938 static GtkMenuFactory *subfactory[1];
4939 static GHashTable *entry_ht = NULL;
4940
4941 void get_main_menu(GtkWidget ** menubar, GtkAcceleratorTable ** table)
4942 {
4943     if (initialize)
4944             menus_init();
4945     
4946     if (menubar)
4947             *menubar = subfactory[0]->widget;
4948     if (table)
4949             *table = subfactory[0]->table;
4950 }
4951
4952 void menus_init(void)
4953 {
4954     if (initialize) {
4955         initialize = FALSE;
4956         
4957         factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
4958         subfactory[0] = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
4959         
4960         gtk_menu_factory_add_subfactory(factory, subfactory[0], "<Main>");
4961         menus_create(menu_items, nmenu_items);
4962     }
4963 }
4964
4965 void menus_create(GtkMenuEntry * entries, int nmenu_entries)
4966 {
4967     char *accelerator;
4968     int i;
4969     
4970     if (initialize)
4971             menus_init();
4972     
4973     if (entry_ht)
4974             for (i = 0; i < nmenu_entries; i++) {
4975                 accelerator = g_hash_table_lookup(entry_ht, entries[i].path);
4976                 if (accelerator) {
4977                     if (accelerator[0] == '\0')
4978                             entries[i].accelerator = NULL;
4979                     else
4980                             entries[i].accelerator = accelerator;
4981                 }
4982             }
4983     gtk_menu_factory_add_entries(factory, entries, nmenu_entries);
4984     
4985     for (i = 0; i < nmenu_entries; i++)
4986             if (entries[i].widget) {
4987                 gtk_signal_connect(GTK_OBJECT(entries[i].widget), "install_accelerator",
4988                                    (GtkSignalFunc) menus_install_accel,
4989                                    entries[i].path);
4990                 gtk_signal_connect(GTK_OBJECT(entries[i].widget), "remove_accelerator",
4991                                    (GtkSignalFunc) menus_remove_accel,
4992                                    entries[i].path);
4993             }
4994 }
4995
4996 static gint menus_install_accel(GtkWidget * widget, gchar * signal_name, gchar key, gchar modifiers, gchar * path)
4997 {
4998     char accel[64];
4999     char *t1, t2[2];
5000     
5001     accel[0] = '\0';
5002     if (modifiers & GDK_CONTROL_MASK)
5003             strcat(accel, "<control>");
5004     if (modifiers & GDK_SHIFT_MASK)
5005             strcat(accel, "<shift>");
5006     if (modifiers & GDK_MOD1_MASK)
5007             strcat(accel, "<alt>");
5008     
5009     t2[0] = key;
5010     t2[1] = '\0';
5011     strcat(accel, t2);
5012     
5013     if (entry_ht) {
5014         t1 = g_hash_table_lookup(entry_ht, path);
5015         g_free(t1);
5016     } else
5017             entry_ht = g_hash_table_new(g_str_hash, g_str_equal);
5018     
5019     g_hash_table_insert(entry_ht, path, g_strdup(accel));
5020     
5021     return TRUE;
5022 }
5023
5024 static void menus_remove_accel(GtkWidget * widget, gchar * signal_name, gchar * path)
5025 {
5026     char *t;
5027     
5028     if (entry_ht) {
5029         t = g_hash_table_lookup(entry_ht, path);
5030         g_free(t);
5031         
5032         g_hash_table_insert(entry_ht, path, g_strdup(""));
5033     }
5034 }
5035
5036 void menus_set_sensitive(char *path, int sensitive)
5037 {
5038     GtkMenuPath *menu_path;
5039     
5040     if (initialize)
5041             menus_init();
5042     
5043     menu_path = gtk_menu_factory_find(factory, path);
5044     if (menu_path)
5045             gtk_widget_set_sensitive(menu_path->widget, sensitive);
5046     else
5047             g_warning("Unable to set sensitivity for menu which doesn't exist: %s", path);
5048 }
5049
5050 </verb></tscreen>
5051 <p>
5052 And here's the mfmain.h
5053
5054 <tscreen><verb>
5055 /* mfmain.h */
5056
5057 #ifndef __MFMAIN_H__
5058 #define __MFMAIN_H__
5059
5060
5061 #ifdef __cplusplus
5062 extern "C" {
5063 #endif /* __cplusplus */
5064
5065 void file_quit_cmd_callback(GtkWidget *widget, gpointer data);
5066
5067 #ifdef __cplusplus
5068 }
5069 #endif /* __cplusplus */
5070
5071 #endif /* __MFMAIN_H__ */
5072 </verb></tscreen>
5073 <p>
5074 And mfmain.c
5075
5076 <tscreen><verb>
5077 /* mfmain.c */
5078
5079 #include <gtk/gtk.h>
5080
5081 #include "mfmain.h"
5082 #include "menufactory.h"
5083
5084
5085 int main(int argc, char *argv[])
5086 {
5087     GtkWidget *window;
5088     GtkWidget *main_vbox;
5089     GtkWidget *menubar;
5090     
5091     GtkAcceleratorTable *accel;
5092     
5093     gtk_init(&amp;argc, &amp;argv);
5094     
5095     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
5096     gtk_signal_connect(GTK_OBJECT(window), "destroy", 
5097                        GTK_SIGNAL_FUNC(file_quit_cmd_callback), 
5098                        "WM destroy");
5099     gtk_window_set_title(GTK_WINDOW(window), "Menu Factory");
5100     gtk_widget_set_usize(GTK_WIDGET(window), 300, 200);
5101     
5102     main_vbox = gtk_vbox_new(FALSE, 1);
5103     gtk_container_border_width(GTK_CONTAINER(main_vbox), 1);
5104     gtk_container_add(GTK_CONTAINER(window), main_vbox);
5105     gtk_widget_show(main_vbox);
5106     
5107     get_main_menu(&amp;menubar, &amp;accel);
5108     gtk_window_add_accelerator_table(GTK_WINDOW(window), accel);
5109     gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, TRUE, 0);
5110     gtk_widget_show(menubar);
5111     
5112     gtk_widget_show(window);
5113     gtk_main();
5114     
5115     return(0);
5116 }
5117
5118 /* This is just to demonstrate how callbacks work when using the
5119  * menufactory.  Often, people put all the callbacks from the menus
5120  * in a separate file, and then have them call the appropriate functions
5121  * from there.  Keeps it more organized. */
5122 void file_quit_cmd_callback (GtkWidget *widget, gpointer data)
5123 {
5124     g_print ("%s\n", (char *) data);
5125     gtk_exit(0);
5126 }
5127 </verb></tscreen>
5128 <p>
5129 And a makefile so it'll be easier to compile it.
5130
5131 <tscreen><verb>
5132 # Makefile.mf
5133
5134 CC      = gcc
5135 PROF    = -g
5136 C_FLAGS =  -Wall $(PROF) -L/usr/local/include -DDEBUG
5137 L_FLAGS =  $(PROF) -L/usr/X11R6/lib -L/usr/local/lib 
5138 L_POSTFLAGS = -lgtk -lgdk -lglib -lXext -lX11 -lm
5139 PROGNAME = menufactory
5140
5141 O_FILES = menufactory.o mfmain.o
5142
5143 $(PROGNAME): $(O_FILES)
5144         rm -f $(PROGNAME)
5145         $(CC) $(L_FLAGS) -o $(PROGNAME) $(O_FILES) $(L_POSTFLAGS)
5146
5147 .c.o: 
5148         $(CC) -c $(C_FLAGS) $<
5149
5150 clean: 
5151         rm -f core *.o $(PROGNAME) nohup.out
5152 distclean: clean 
5153         rm -f *~
5154 </verb></tscreen>
5155 <p>
5156 For now, there's only this example.  An explanation and lots 'o' comments
5157 will follow later.
5158
5159 <!-- ***************************************************************** -->
5160 <sect> Text Widget
5161 <!-- ***************************************************************** -->
5162 <p>
5163 The Text widget allows multiple lines of text to be displayed and edited. It supports both
5164 multi-colored and multi-font text, allowing them to be mixed in any way we wish. It also has
5165 a wide set of key based text editing commands, which are compatible with Emacs.
5166
5167 The text widget supports full cut-and-paste facilities, including the use of double- and
5168 triple-click to select a word and a whole line, respectively.
5169
5170 <!-- ----------------------------------------------------------------- -->
5171 <sect1>Creating and Configuring a Text box
5172 <p>
5173 There is only one function for creating a new Text widget.
5174 <tscreen><verb>
5175 GtkWidget* gtk_text_new (GtkAdjustment *hadj,
5176                          GtkAdjustment *vadj);
5177 </verb></tscreen>
5178
5179 The arguments allow us to give the Text widget pointers to Adjustments that can be used 
5180 to track the viewing position of the widget. Passing NULL values to either or both of
5181 these arguments will cause the gtk_text_new function to create it's own.
5182
5183 <tscreen><verb>
5184 void gtk_text_set_adjustments (GtkText       *text,
5185                                GtkAdjustment *hadj,
5186                                GtkAdjustment *vadj);
5187 </verb></tscreen>
5188
5189 The above function allows the horizontal and vertical adjustments of a Text widget to be
5190 changed at any time.
5191
5192 The text widget will not automatically create it's own scrollbars when the amount of text
5193 to be displayed is too long for the display window. We therefore have to create and add
5194 them to the display layout ourselves.
5195
5196 <tscreen><verb>
5197   vscrollbar = gtk_vscrollbar_new (GTK_TEXT(text)->vadj);
5198   gtk_box_pack_start(GTK_BOX(hbox), vscrollbar, FALSE, FALSE, 0);
5199   gtk_widget_show (vscrollbar);
5200 </verb></tscreen>
5201
5202 The above code snippet creates a new vertical scrollbar, and attaches it to the vertical
5203 adjustment of the text widget, <tt/text/. It then packs it into a box in the normal way.
5204
5205 There are two main ways in which a Text widget can be used: to allow the user to edit a
5206 body of text, or to allow us to display multiple lines of text to the user. In order for
5207 us to switch between these modes of operation, the text widget has the following function:
5208
5209 <tscreen><verb>
5210 void gtk_text_set_editable    (GtkText *text,
5211                                gint    editable);
5212 </verb></tscreen>
5213
5214 The <tt/editable/ argument is a TRUE or FALSE value that specifies whether the user is
5215 permitted to edit the contents of the Text widget. When the text widget is editable, it
5216 will display a cursor at the current insertion point.
5217
5218 You are not, however, restricted to just using the text widget in these two modes. You can
5219 toggle the editable state of the text widget at any time, and can insert text at any time.
5220
5221 The text widget is capable of wrapping lines of text that are too long to fit onto a single
5222 line of the display window. It's default behaviour is to break words across line breaks. This
5223 can be changed using the next function:
5224
5225 <tscreen><verb>
5226 void gtk_text_set_word_wrap (GtkText *text,
5227                              gint    word_wrap);
5228 </verb></tscreen>
5229
5230 Using this function allows us to specify that the text widget should wrap long lines on word
5231 boundaries. The <tt/word_wrap/ argument is a TRUE or FALSE value.
5232
5233 <!-- ----------------------------------------------------------------- -->
5234 <sect1>Text Manipulation
5235 <P>
5236 The current insertion point of a Text widget can be set using
5237 <tscreen><verb>
5238 void gtk_text_set_point (GtkText *text,
5239                          guint   index);
5240 </verb></tscreen>
5241 where <tt/index/ is the position to set the insertion point.
5242
5243 Analogous to this is the function for getting the current insertion point:
5244 <tscreen><verb>
5245 guint gtk_text_get_point (GtkText *text);
5246 </verb></tscreen>
5247
5248 A function that is useful in combination with the above two functions is
5249 <tscreen><verb>
5250 guint gtk_text_get_length (GtkText *text);
5251 </verb></tscreen>
5252 which returns the current length of the Text widget. The length is the number of characters
5253 that are within the text block of the widget, including characters such as carriage-return,
5254 which marks the end of lines.
5255
5256 In order to insert text at the current insertion point of a Text widget, the function
5257 gtk_text_insert is used, which also allows us to specify background and foreground colors and a 
5258 font for the text.
5259
5260 <tscreen><verb>
5261 void gtk_text_insert (GtkText    *text,
5262                       GdkFont    *font,
5263                       GdkColor   *fore,
5264                       GdkColor   *back,
5265                       const char *chars,
5266                       gint       length);
5267 </verb></tscreen>
5268
5269 Passing a value of <tt/NULL/ in as the value for the foreground color, background colour or
5270 font will result in the values set within the widget style to be used. Using a value of <tt/-1/ for
5271 the length parameter will result in the whole of the text string given being inserted.
5272
5273 The text widget is one of the few within GTK that redraws itself dynamically, outside of the gtk_main
5274 function. This means that all changes to the contents of the text widget take effect immediately. This
5275 may be undesirable when performing multiple changes to the text widget. In order to allow us to perform
5276 multiple updates to the text widget without it continuously redrawing, we can freeze the widget, which
5277 temporarily stops it from automatically redrawing itself every time it is changed. We can then thaw the
5278 widget after our updates are complete.
5279
5280 The following two functions perform this freeze and thaw action:
5281
5282 <tscreen><verb>
5283 void gtk_text_freeze (GtkText *text);
5284 void gtk_text_thaw   (GtkText *text);         
5285 </verb></tscreen>
5286
5287 Text is deleted from the text widget relative to the current insertion point by the following
5288 two functions:
5289
5290 <tscreen><verb>
5291 gint gtk_text_backward_delete (GtkText *text,
5292                                guint   nchars);
5293 gint gtk_text_forward_delete  (GtkText *text,
5294                                guint   nchars);
5295 </verb></tscreen>
5296
5297 If you want to retrieve the contents of the text widget, then the macro 
5298 <tt/GTK_TEXT_INDEX(t, index)/ allows you to retrieve the character at position
5299 <tt/index/ within the text widget <tt/t/.
5300
5301 To retrieve larger blocks of text, we can use the function
5302
5303 <tscreen><verb>
5304 gchar *gtk_editable_get_chars (GtkEditable *editable,
5305                                gint        start_pos,
5306                                gint        end_pos);   
5307 </verb></tscreen>
5308
5309 This is a function of the parent class of the text widget. A value of -1 as
5310 <tt/end_pos/ signifies the end of the text. The index of the text starts at 0.
5311
5312 The function allocates a new chunk of memory for the text block, so don't forget
5313 to free it with a call to g_free when you have finished with it.
5314  
5315 <!-- ----------------------------------------------------------------- -->
5316 <sect1>Keyboard Shortcuts
5317 <p>
5318 The text widget has a number of pre-installed keyboard shotcuts for common
5319 editing, motion and selection functions. These are accessed using Control and Alt
5320 key combinations.
5321
5322 In addition to these, holding down the Control key whilst using cursor key movement
5323 will move the cursor by words rather than characters. Holding down Shift whilst using
5324 cursor movement will extend the selection.
5325
5326 <sect2>Motion Shotcuts
5327 <p>
5328 <itemize>
5329 <item> Ctrl-A   Beginning of line
5330 <item> Ctrl-E   End of line
5331 <item> Ctrl-N   Next Line
5332 <item> Ctrl-P   Previous Line
5333 <item> Ctrl-B   Backward one character
5334 <item> Ctrl-F   Forward one character
5335 <item> Alt-B    Backward one word
5336 <item> Alt-F    Forward one word
5337 </itemize>
5338
5339 <sect2>Editing Shortcuts
5340 <p>
5341 <itemize>
5342 <item> Ctrl-H   Delete Backward Character (Backspace)
5343 <item> Ctrl-D   Delete Forward Character (Delete)
5344 <item> Ctrl-W   Delete Backward Word
5345 <item> Alt-D    Delete Forward Word
5346 <item> Ctrl-K   Delete to end of line
5347 <item> Ctrl-U   Delete line
5348 </itemize>
5349
5350 <sect2>Selection Shortcuts
5351 <p>
5352 <itemize>
5353 <item> Ctrl-X   Cut to clipboard
5354 <item> Ctrl-C   Copy to clipboard
5355 <item> Ctrl-V   Paste from clipboard
5356 </itemize>
5357
5358 <!-- ***************************************************************** -->
5359 <sect> Undocumented Widgets
5360 <!-- ***************************************************************** -->
5361
5362 <p>
5363 These all require authors! :)  Please consider contributing to our tutorial.
5364
5365 If you must use one of these widgets that are undocumented, I strongly
5366 suggest you take a look at their respective header files in the GTK distro.
5367 GTK's function names are very descriptive.  Once you have an understanding
5368 of how things work, it's not easy to figure out how to use a widget simply
5369 by looking at it's function declarations.  This, along with a few examples
5370 from others' code, and it should be no problem.
5371
5372 When you do come to understand all the functions of a new undocumented
5373 widget, please consider writing a tutorial on it so others may benifit from
5374 your time.
5375
5376 <!-- ----------------------------------------------------------------- -->
5377 <sect1> Range Controls
5378
5379 <!-- ----------------------------------------------------------------- -->
5380 <sect1> Previews
5381 <p>
5382
5383 (This may need to be rewritten to follow the style of the rest of the tutorial)
5384
5385 <tscreen><verb>
5386
5387 Previews serve a number of purposes in GIMP/GTK. The most important one is
5388 this. High quality images may take up to tens of megabytes of memory - easy!
5389 Any operation on an image that big is bound to take a long time. If it takes
5390 you 5-10 trial-and-errors (i.e. 10-20 steps, since you have to revert after
5391 you make an error) to choose the desired modification, it make take you
5392 literally hours to make the right one - if you don't run out of memory
5393 first. People who have spent hours in color darkrooms know the feeling.
5394 Previews to the rescue!
5395
5396 But the annoyance of the delay is not the only issue. Oftentimes it is
5397 helpful to compare the Before and After versions side-by-side or at least
5398 back-to-back. If you're working with big images and 10 second delays,
5399 obtaining the Before and After impressions is, to say the least, difficult.
5400 For 30M images (4"x6", 600dpi, 24 bit) the side-by-side comparison is right
5401 out for most people, while back-to-back is more like back-to-1001, 1002,
5402 ..., 1010-back! Previews to the rescue!
5403
5404 But there's more. Previews allow for side-by-side pre-previews. In other
5405 words, you write a plug-in (e.g. the filterpack simulation) which would have
5406 a number of here's-what-it-would-look-like-if-you-were-to-do-this previews.
5407 An approach like this acts as a sort of a preview palette and is very
5408 effective fow subtle changes. Let's go previews!
5409
5410 There's more. For certain plug-ins real-time image-specific human
5411 intervention maybe necessary. In the SuperNova plug-in, for example, the
5412 user is asked to enter the coordinates of the center of the future
5413 supernova. The easiest way to do this, really, is to present the user with a
5414 preview and ask him to intereactively select the spot. Let's go previews!
5415
5416 Finally, a couple of misc uses. One can use previews even when not working
5417 with big images. For example, they are useful when rendering compicated
5418 patterns. (Just check out the venerable Diffraction plug-in + many other
5419 ones!) As another example, take a look at the colormap rotation plug-in
5420 (work in progress). You can also use previews for little logo's inside you
5421 plug-ins and even for an image of yourself, The Author. Let's go previews!
5422
5423 When Not to Use Previews
5424
5425 Don't use previews for graphs, drawing etc. GDK is much faster for that. Use
5426 previews only for rendered images!
5427
5428 Let's go previews!
5429
5430 You can stick a preview into just about anything. In a vbox, an hbox, a
5431 table, a button, etc. But they look their best in tight frames around them.
5432 Previews by themselves do not have borders and look flat without them. (Of
5433 course, if the flat look is what you want...) Tight frames provide the
5434 necessary borders.
5435
5436                                [Image][Image]
5437
5438 Previews in many ways are like any other widgets in GTK (whatever that
5439 means) except they possess an addtional feature: they need to be filled with
5440 some sort of an image! First, we will deal exclusively with the GTK aspect
5441 of previews and then we'll discuss how to fill them.
5442
5443 GtkWidget *preview!
5444
5445 Without any ado:
5446
5447                               /* Create a preview widget,
5448                               set its size, an show it */
5449 GtkWidget *preview;
5450 preview=gtk_preview_new(GTK_PREVIEW_COLOR)
5451                               /*Other option:
5452                               GTK_PREVIEW_GRAYSCALE);*/
5453 gtk_preview_size (GTK_PREVIEW (preview), WIDTH, HEIGHT);
5454 gtk_widget_show(preview);
5455 my_preview_rendering_function(preview);
5456
5457 Oh yeah, like I said, previews look good inside frames, so how about:
5458
5459 GtkWidget *create_a_preview(int        Width,
5460                             int        Height,
5461                             int        Colorfulness)
5462 {
5463   GtkWidget *preview;
5464   GtkWidget *frame;
5465   
5466   frame = gtk_frame_new(NULL);
5467   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
5468   gtk_container_border_width (GTK_CONTAINER(frame),0);
5469   gtk_widget_show(frame);
5470
5471   preview=gtk_preview_new (Colorfulness?GTK_PREVIEW_COLOR
5472                                        :GTK_PREVIEW_GRAYSCALE);
5473   gtk_preview_size (GTK_PREVIEW (preview), Width, Height);
5474   gtk_container_add(GTK_CONTAINER(frame),preview);
5475   gtk_widget_show(preview);
5476
5477   my_preview_rendering_function(preview);
5478   return frame;
5479 }
5480
5481 That's my basic preview. This routine returns the "parent" frame so you can
5482 place it somewhere else in your interface. Of course, you can pass the
5483 parent frame to this routine as a parameter. In many situations, however,
5484 the contents of the preview are changed continually by your application. In
5485 this case you may want to pass a pointer to the preview to a
5486 "create_a_preview()" and thus have control of it later.
5487
5488 One more important note that may one day save you a lot of time. Sometimes
5489 it is desirable to label you preview. For example, you may label the preview
5490 containing the original image as "Original" and the one containing the
5491 modified image as "Less Original". It might occure to you to pack the
5492 preview along with the appropriate label into a vbox. The unexpected caveat
5493 is that if the label is wider than the preview (which may happen for a
5494 variety of reasons unforseeable to you, from the dynamic decision on the
5495 size of the preview to the size of the font) the frame expands and no longer
5496 fits tightly over the preview. The same problem can probably arise in other
5497 situations as well.
5498
5499                                    [Image]
5500
5501 The solution is to place the preview and the label into a 2x1 table and by
5502 attaching them with the following paramters (this is one possible variations
5503 of course. The key is no GTK_FILL in the second attachment):
5504
5505 gtk_table_attach(GTK_TABLE(table),label,0,1,0,1,
5506                  0,
5507                  GTK_EXPAND|GTK_FILL,
5508                  0,0);
5509 gtk_table_attach(GTK_TABLE(table),frame,0,1,1,2,
5510                  GTK_EXPAND,
5511                  GTK_EXPAND,
5512                  0,0);
5513
5514
5515 And here's the result:
5516
5517                                    [Image]
5518
5519 Misc
5520
5521 Making a preview clickable is achieved most easily by placing it in a
5522 button. It also adds a nice border around the preview and you may not even
5523 need to place it in a frame. See the Filter Pack Simulation plug-in for an
5524 example.
5525
5526 This is pretty much it as far as GTK is concerned.
5527
5528 Filling In a Preview
5529
5530 In order to familiarize ourselves with the basics of filling in previews,
5531 let's create the following pattern (contrived by trial and error):
5532
5533                                    [Image]
5534
5535 void
5536 my_preview_rendering_function(GtkWidget     *preview)
5537 {
5538 #define SIZE 100
5539 #define HALF (SIZE/2)
5540
5541   guchar *row=(guchar *) malloc(3*SIZE); /* 3 bits per dot */
5542   gint i, j;                             /* Coordinates    */
5543   double r, alpha, x, y;
5544
5545   if (preview==NULL) return; /* I usually add this when I want */
5546                              /* to avoid silly crashes. You    */
5547                              /* should probably make sure that */
5548                              /* everything has been nicely     */
5549                              /* initialized!                   */
5550   for (j=0; j < ABS(cos(2*alpha)) ) { /* Are we inside the shape?  */
5551                                          /* glib.h contains ABS(x).   */
5552         row[i*3+0] = sqrt(1-r)*255;      /* Define Red                */
5553         row[i*3+1] = 128;                /* Define Green              */
5554         row[i*3+2] = 224;                /* Define Blue               */
5555       }                                  /* "+0" is for alignment!    */
5556       else {
5557         row[i*3+0] = r*255;
5558         row[i*3+1] = ABS(sin((float)i/SIZE*2*PI))*255;
5559         row[i*3+2] = ABS(sin((float)j/SIZE*2*PI))*255;
5560       }
5561     }
5562     gtk_preview_draw_row( GTK_PREVIEW(preview),row,0,j,SIZE);
5563     /* Insert "row" into "preview" starting at the point with  */
5564     /* coordinates (0,j) first column, j_th row extending SIZE */
5565     /* pixels to the right */
5566   }
5567
5568   free(row); /* save some space */
5569   gtk_widget_draw(preview,NULL); /* what does this do? */
5570   gdk_flush(); /* or this? */
5571 }
5572
5573 Non-GIMP users can have probably seen enough to do a lot of things already.
5574 For the GIMP users I have a few pointers to add.
5575
5576 Image Preview
5577
5578 It is probably wize to keep a reduced version of the image around with just
5579 enough pixels to fill the preview. This is done by selecting every n'th
5580 pixel where n is the ratio of the size of the image to the size of the
5581 preview. All further operations (including filling in the previews) are then
5582 performed on the reduced number of pixels only. The following is my
5583 implementation of reducing the image. (Keep in mind that I've had only basic
5584 C!)
5585
5586 (UNTESTED CODE ALERT!!!)
5587
5588 typedef struct {
5589   gint      width;
5590   gint      height;
5591   gint      bbp;
5592   guchar    *rgb;
5593   guchar    *mask;
5594 } ReducedImage;
5595
5596 enum {
5597   SELECTION_ONLY,
5598   SELCTION_IN_CONTEXT,
5599   ENTIRE_IMAGE
5600 };
5601
5602 ReducedImage *Reduce_The_Image(GDrawable *drawable,
5603                                GDrawable *mask,
5604                                gint LongerSize,
5605                                gint Selection)
5606 {
5607   /* This function reduced the image down to the the selected preview size */
5608   /* The preview size is determine by LongerSize, i.e. the greater of the  */
5609   /* two dimentions. Works for RGB images only!                            */
5610   gint RH, RW;          /* Reduced height and reduced width                */
5611   gint width, height;   /* Width and Height of the area being reduced      */
5612   gint bytes=drawable->bpp;
5613   ReducedImage *temp=(ReducedImage *)malloc(sizeof(ReducedImage));
5614
5615   guchar *tempRGB, *src_row, *tempmask, *src_mask_row,R,G,B;
5616   gint i, j, whichcol, whichrow, x1, x2, y1, y2;
5617   GPixelRgn srcPR, srcMask;
5618   gint NoSelectionMade=TRUE; /* Assume that we're dealing with the entire  */
5619                              /* image.                                     */
5620
5621   gimp_drawable_mask_bounds (drawable->id, &amp;x1, &amp;y1, &amp;x2, &amp;y2);
5622   width  = x2-x1;
5623   height = y2-y1;
5624   /* If there's a SELECTION, we got its bounds!)
5625
5626   if (width != drawable->width &amp;&amp; height != drawable->height)
5627     NoSelectionMade=FALSE;
5628   /* Become aware of whether the user has made an active selection   */
5629   /* This will become important later, when creating a reduced mask. */
5630
5631   /* If we want to preview the entire image, overrule the above!  */
5632   /* Of course, if no selection has been made, this does nothing! */
5633   if (Selection==ENTIRE_IMAGE) {
5634     x1=0;
5635     x2=drawable->width;
5636     y1=0;
5637     y2=drawable->height;
5638   }
5639
5640   /* If we want to preview a selection with some surronding area we */
5641   /* have to expand it a little bit. Consider it a bit of a riddle. */
5642   if (Selection==SELECTION_IN_CONTEXT) {
5643     x1=MAX(0,                x1-width/2.0);
5644     x2=MIN(drawable->width,  x2+width/2.0);
5645     y1=MAX(0,                y1-height/2.0);
5646     y2=MIN(drawable->height, y2+height/2.0);
5647   }
5648
5649   /* How we can determine the width and the height of the area being */
5650   /* reduced.                                                        */
5651   width  = x2-x1;
5652   height = y2-y1;
5653
5654   /* The lines below determine which dimension is to be the longer   */
5655   /* side. The idea borrowed from the supernova plug-in. I suspect I */
5656   /* could've thought of it myself, but the truth must be told.      */
5657   /* Plagiarism stinks!                                               */
5658   if (width>height) {
5659     RW=LongerSize;
5660     RH=(float) height * (float) LongerSize/ (float) width;
5661   }
5662   else {
5663     RH=LongerSize;
5664     RW=(float)width * (float) LongerSize/ (float) height;
5665   }
5666
5667   /* The intire image is stretched into a string! */
5668   tempRGB   = (guchar *) malloc(RW*RH*bytes);
5669   tempmask  = (guchar *) malloc(RW*RH);
5670
5671   gimp_pixel_rgn_init (&amp;srcPR, drawable, x1, y1, width, height, FALSE, FALSE);
5672   gimp_pixel_rgn_init (&amp;srcMask, mask, x1, y1, width, height, FALSE, FALSE);
5673
5674   /* Grab enough to save a row of image and a row of mask. */
5675   src_row       = (guchar *) malloc (width*bytes);
5676   src_mask_row  = (guchar *) malloc (width);
5677
5678   for (i=0; i < RH; i++) {
5679     whichrow=(float)i*(float)height/(float)RH;
5680     gimp_pixel_rgn_get_row (&amp;srcPR, src_row, x1, y1+whichrow, width);
5681     gimp_pixel_rgn_get_row (&amp;srcMask, src_mask_row, x1, y1+whichrow, width);
5682
5683     for (j=0; j < RW; j++) {
5684       whichcol=(float)j*(float)width/(float)RW;
5685
5686       /* No selection made = each point is completely selected! */
5687       if (NoSelectionMade)
5688         tempmask[i*RW+j]=255;
5689       else
5690         tempmask[i*RW+j]=src_mask_row[whichcol];
5691
5692       /* Add the row to the one long string which now contains the image! */
5693       tempRGB[i*RW*bytes+j*bytes+0]=src_row[whichcol*bytes+0];
5694       tempRGB[i*RW*bytes+j*bytes+1]=src_row[whichcol*bytes+1];
5695       tempRGB[i*RW*bytes+j*bytes+2]=src_row[whichcol*bytes+2];
5696
5697       /* Hold on to the alpha as well */
5698       if (bytes==4)
5699         tempRGB[i*RW*bytes+j*bytes+3]=src_row[whichcol*bytes+3];
5700     }
5701   }
5702   temp->bpp=bytes;
5703   temp->width=RW;
5704   temp->height=RH;
5705   temp->rgb=tempRGB;
5706   temp->mask=tempmask;
5707   return temp;
5708 }
5709
5710 The following is a preview function which used the same ReducedImage type!
5711 Note that it uses fakes transparancy (if one is present by means of
5712 fake_transparancy which is defined as follows:
5713
5714 gint fake_transparency(gint i, gint j)
5715 {
5716   if ( ((i%20)- 10) * ((j%20)- 10)>0   )
5717     return 64;
5718   else
5719     return 196;
5720 }
5721
5722 Now here's the preview function:
5723
5724 void
5725 my_preview_render_function(GtkWidget     *preview,
5726                            gint          changewhat,
5727                            gint          changewhich)
5728 {
5729   gint Inten, bytes=drawable->bpp;
5730   gint i, j, k;
5731   float partial;
5732   gint RW=reduced->width;
5733   gint RH=reduced->height;
5734   guchar *row=malloc(bytes*RW);;
5735
5736
5737   for (i=0; i < RH; i++) {
5738     for (j=0; j < RW; j++) {
5739
5740       row[j*3+0] = reduced->rgb[i*RW*bytes + j*bytes + 0];
5741       row[j*3+1] = reduced->rgb[i*RW*bytes + j*bytes + 1];
5742       row[j*3+2] = reduced->rgb[i*RW*bytes + j*bytes + 2];
5743
5744       if (bytes==4)
5745         for (k=0; k<3; k++) {
5746           float transp=reduced->rgb[i*RW*bytes+j*bytes+3]/255.0;
5747           row[3*j+k]=transp*a[3*j+k]+(1-transp)*fake_transparency(i,j);
5748         }
5749     }
5750     gtk_preview_draw_row( GTK_PREVIEW(preview),row,0,i,RW);
5751   }
5752
5753   free(a);
5754   gtk_widget_draw(preview,NULL);
5755   gdk_flush();
5756 }
5757
5758 Applicable Routines
5759
5760 guint           gtk_preview_get_type           (void);
5761 /* No idea */
5762 void            gtk_preview_uninit             (void);
5763 /* No idea */
5764 GtkWidget*      gtk_preview_new                (GtkPreviewType   type);
5765 /* Described above */
5766 void            gtk_preview_size               (GtkPreview      *preview,
5767                                                 gint             width,
5768                                                 gint             height);
5769 /* Allows you to resize an existing preview.    */
5770 /* Apparantly there's a bug in GTK which makes  */
5771 /* this process messy. A way to clean up a mess */
5772 /* is to manually resize the window containing  */
5773 /* the preview after resizing the preview.      */
5774
5775 void            gtk_preview_put                (GtkPreview      *preview,
5776                                                 GdkWindow       *window,
5777                                                 GdkGC           *gc,
5778                                                 gint             srcx,
5779                                                 gint             srcy,
5780                                                 gint             destx,
5781                                                 gint             desty,
5782                                                 gint             width,
5783                                                 gint             height);
5784 /* No idea */
5785
5786 void            gtk_preview_put_row            (GtkPreview      *preview,
5787                                                 guchar          *src,
5788                                                 guchar          *dest,
5789                                                 gint             x,
5790                                                 gint             y,
5791                                                 gint             w);
5792 /* No idea */
5793
5794 void            gtk_preview_draw_row           (GtkPreview      *preview,
5795                                                 guchar          *data,
5796                                                 gint             x,
5797                                                 gint             y,
5798                                                 gint             w);
5799 /* Described in the text */
5800
5801 void            gtk_preview_set_expand         (GtkPreview      *preview,
5802                                                 gint             expand);
5803 /* No idea */
5804
5805 /* No clue for any of the below but    */
5806 /* should be standard for most widgets */
5807 void            gtk_preview_set_gamma          (double           gamma);
5808 void            gtk_preview_set_color_cube     (guint            nred_shades,
5809                                                 guint            ngreen_shades,
5810                                                 guint            nblue_shades,
5811                                                 guint            ngray_shades);
5812 void            gtk_preview_set_install_cmap   (gint             install_cmap);
5813 void            gtk_preview_set_reserved       (gint             nreserved);
5814 GdkVisual*      gtk_preview_get_visual         (void);
5815 GdkColormap*    gtk_preview_get_cmap           (void);
5816 GtkPreviewInfo* gtk_preview_get_info           (void);
5817
5818 That's all, folks!
5819
5820 </verb></tscreen>
5821
5822 <!-- ----------------------------------------------------------------- -->
5823 <sect1> Curves
5824 <p>
5825
5826 <!-- ***************************************************************** -->
5827 <sect>The EventBox Widget<label id="sec_The_EventBox_Widget">
5828 <!-- ***************************************************************** -->
5829
5830 <p> 
5831 Some gtk widgets don't have associated X windows, so they just draw on 
5832 thier parents.  Because of this, they cannot recieve events
5833 and if they are incorrectly sized, they don't clip so you can get
5834 messy overwritting etc.  If you require more from these widgets, the
5835 EventBox is for you.
5836
5837 At first glance, the EventBox widget might appear to be totally
5838 useless. It draws nothing on the screen and responds to no
5839 events.  However, it does serve a function - it provides an X window for
5840 its child widget. This is important as many GTK widgets do not
5841 have an associated X window.  Not having an X window saves memory and
5842 improves performance, but also has some drawbacks. A widget without an
5843 X window cannot receive events, and does not perform any clipping on
5844 it's contents. Although the name ``EventBox'' emphasizes the
5845 event-handling function, the widget also can be used for clipping. 
5846 (And more ... see the example below.)
5847
5848 <p>
5849 To create a new EventBox widget, use:
5850
5851 <tscreen><verb>
5852 GtkWidget* gtk_event_box_new (void);
5853 </verb></tscreen>
5854
5855 <p>
5856 A child widget can then be added to this EventBox:
5857
5858 <tscreen><verb>
5859 gtk_container_add (GTK_CONTAINER(event_box), widget);
5860 </verb></tscreen>
5861
5862 <p>
5863 The following example demonstrates both uses of an EventBox - a label
5864 is created that clipped to a small box, and set up so that a
5865 mouse-click on the label causes the program to exit.
5866
5867 <tscreen><verb>
5868 /* eventbox.c */
5869
5870 #include <gtk/gtk.h>
5871
5872 int 
5873 main (int argc, char *argv[])
5874 {
5875     GtkWidget *window;
5876     GtkWidget *event_box;
5877     GtkWidget *label;
5878     
5879     gtk_init (&amp;argc, &amp;argv);
5880     
5881     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
5882     
5883     gtk_window_set_title (GTK_WINDOW (window), "Event Box");
5884     
5885     gtk_signal_connect (GTK_OBJECT (window), "destroy",
5886                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
5887     
5888     gtk_container_border_width (GTK_CONTAINER (window), 10);
5889     
5890     /* Create an EventBox and add it to our toplevel window */
5891     
5892     event_box = gtk_event_box_new ();
5893     gtk_container_add (GTK_CONTAINER(window), event_box);
5894     gtk_widget_show (event_box);
5895     
5896     /* Create a long label */
5897     
5898     label = gtk_label_new ("Click here to quit, quit, quit, quit, quit");
5899     gtk_container_add (GTK_CONTAINER (event_box), label);
5900     gtk_widget_show (label);
5901     
5902     /* Clip it short. */
5903     gtk_widget_set_usize (label, 110, 20);
5904     
5905     /* And bind an action to it */
5906     gtk_widget_set_events (event_box, GDK_BUTTON_PRESS_MASK);
5907     gtk_signal_connect (GTK_OBJECT(event_box), "button_press_event",
5908                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
5909     
5910     /* Yet one more thing you need an X window for ... */
5911     
5912     gtk_widget_realize (event_box);
5913     gdk_window_set_cursor (event_box->window, gdk_cursor_new (GDK_HAND1));
5914     
5915     gtk_widget_show (window);
5916     
5917     gtk_main ();
5918     
5919     return 0;
5920 }
5921 </verb></tscreen>
5922
5923 <!-- ***************************************************************** -->
5924 <sect>Setting Widget Attributes<label id="sec_setting_widget_attributes">
5925 <!-- ***************************************************************** -->
5926
5927 <p>
5928 This describes the functions used to operate on widgets.  These can be used
5929 to set style, padding, size etc.
5930
5931 (Maybe I should make a whole section on accelerators.)
5932
5933 <tscreen><verb>
5934 void       gtk_widget_install_accelerator (GtkWidget           *widget,
5935                                            GtkAcceleratorTable *table,
5936                                            gchar               *signal_name,
5937                                            gchar                key,
5938                                            guint8               modifiers);
5939
5940 void       gtk_widget_remove_accelerator  (GtkWidget           *widget,
5941                                            GtkAcceleratorTable *table,
5942                                            gchar               *signal_name);
5943
5944 void       gtk_widget_activate            (GtkWidget           *widget);
5945
5946 void       gtk_widget_set_name            (GtkWidget           *widget,
5947                                            gchar               *name);
5948 gchar*     gtk_widget_get_name            (GtkWidget           *widget);
5949
5950 void       gtk_widget_set_sensitive       (GtkWidget           *widget,
5951                                            gint                 sensitive);
5952
5953 void       gtk_widget_set_style           (GtkWidget           *widget,
5954                                            GtkStyle            *style);
5955                                            
5956 GtkStyle*    gtk_widget_get_style     (GtkWidget *widget);
5957
5958 GtkStyle*    gtk_widget_get_default_style    (void);
5959
5960 void       gtk_widget_set_uposition       (GtkWidget           *widget,
5961                                            gint                 x,
5962                                            gint                 y);
5963 void       gtk_widget_set_usize           (GtkWidget           *widget,
5964                                            gint                 width,
5965                                            gint                 height);
5966
5967 void       gtk_widget_grab_focus          (GtkWidget           *widget);
5968
5969 void       gtk_widget_show                (GtkWidget           *widget);
5970
5971 void       gtk_widget_hide                (GtkWidget           *widget);
5972 </verb></tscreen>
5973
5974 <!-- ***************************************************************** -->
5975 <sect>Timeouts, IO and Idle Functions<label id="sec_timeouts">
5976 <!-- ***************************************************************** -->
5977
5978 <!-- ----------------------------------------------------------------- -->
5979 <sect1>Timeouts
5980 <p>
5981 You may be wondering how you make GTK do useful work when in gtk_main.
5982 Well, you have several options.  Using the following functions you can
5983 create a timeout function that will be called every "interval" milliseconds.
5984
5985 <tscreen><verb>
5986 gint gtk_timeout_add (guint32 interval,
5987                       GtkFunction function,
5988                       gpointer data);
5989 </verb></tscreen>
5990
5991 The first argument is the number of milliseconds
5992 between calls to your function.  The second argument is the function
5993 you wish to have called, and
5994 the third, the data passed to this callback function.  The return value is
5995 an integer "tag" which may be used to stop the timeout by calling:
5996
5997 <tscreen><verb>
5998 void gtk_timeout_remove (gint tag);
5999 </verb></tscreen>
6000
6001 You may also stop the timeout function by returning zero or FALSE from
6002 your callback function.  Obviously this means if you want your function to
6003 continue to be called, it should return a non-zero value, ie TRUE.
6004
6005 The declaration of your callback should look something like this:
6006
6007 <tscreen><verb>
6008 gint timeout_callback (gpointer data);
6009 </verb></tscreen>
6010
6011 <!-- ----------------------------------------------------------------- -->
6012 <sect1>Monitoring IO
6013 <p>
6014 Another nifty feature of GTK, is the ability to have it check for data on a
6015 file descriptor for you (as returned by open(2) or socket(2)).  This is
6016 especially useful for networking applications.  The function:
6017
6018 <tscreen><verb>
6019 gint gdk_input_add (gint source,
6020                     GdkInputCondition condition,
6021                     GdkInputFunction  function,
6022                     gpointer data);
6023 </verb></tscreen>
6024
6025 Where the first argument is the file descriptor you wish to have watched,
6026 and the second specifies what you want GDK to look for.  This may be one of:
6027 <p>
6028 GDK_INPUT_READ - Call your function when there is data ready for reading on
6029 your file descriptor.
6030 <p>
6031 GDK_INPUT_WRITE - Call your function when the file descriptor is ready for
6032 writing.
6033 <p>
6034 As I'm sure you've figured out already, the third argument is the function
6035 you wish to have called when the above conditions are satisfied, and the
6036 fourth is the data to pass to this function.
6037 <p>
6038 The return value is a tag that may be used to stop GDK from monitoring this
6039 file descriptor using the following function.
6040 <p>
6041 <tscreen><verb>
6042 void gdk_input_remove (gint tag);
6043 </verb></tscreen>
6044 <p>
6045 The callback function should be declared:
6046 <p>
6047 <tscreen><verb>
6048 void input_callback (gpointer data, gint source, 
6049                      GdkInputCondition condition);
6050 </verb></tscreen>
6051 <p>
6052
6053 <!-- ----------------------------------------------------------------- -->
6054 <sect1>Idle Functions
6055 <p>
6056 What if you have a function you want called when nothing else is
6057 happening ?
6058
6059 <tscreen><verb>
6060 gint gtk_idle_add (GtkFunction function,
6061                    gpointer data);
6062 </verb></tscreen>
6063
6064 This causes GTK to call the specified function whenever nothing else is
6065 happening.
6066
6067 <tscreen><verb>
6068 void gtk_idle_remove (gint tag);
6069 </verb></tscreen>
6070 <p>
6071 I won't explain the meaning of the arguments as they follow very much like
6072 the ones above.  The function pointed to by the first argument to
6073 gtk_idle_add will be called whenever the opportunity arises.  As with the
6074 others, returning FALSE will stop the idle function from being called.
6075
6076 <!-- ***************************************************************** -->
6077 <sect>Managing Selections
6078 <!-- ***************************************************************** -->
6079
6080 <!-- ----------------------------------------------------------------- -->
6081 <sect1> Overview
6082
6083 <p>
6084
6085 One type of interprocess communication supported by GTK is
6086 <em>selections</em>. A selection identifies a chunk of data, for
6087 instance, a portion of text, selected by the user in some fashion, for
6088 instance, by dragging with the mouse. Only one application on a
6089 display, (he <em>owner</em>_ can own a particular selection at one
6090 time, so when a selection is claimed by one application, the previous
6091 owner must indicate to the user that selection has been
6092 relinquished. Other applications can request the contents of a
6093 selection in different forms, called <em>targets</em>.  There can be
6094 any number of selections, but most X applications only handle one, the
6095 <em>primary selection</em>.
6096
6097 <p>
6098 In most cases, it isn't necessary for a GTK application to deal with
6099 selections itself. The standard widgets, such as the Entry widget,
6100 already have the capability to claim the selection when appropriate
6101 (e.g., when the user drags over text), and to retrieve the contents of
6102 the selection owned by another widget, or another application (e.g.,
6103 when the user clicks the second mouse button). However, there may be
6104 cases in which you want to give other widgets the ability to supply
6105 the selection, or you wish to retrieve targets not supported by
6106 default.
6107
6108 <p>
6109 A fundamental concept needed to understand selection handling is that
6110 of the <em>atom</em>. An atom is an integer that uniquely identifies a
6111 string (on a certain display). Certain atoms are predefined by the X
6112 server, and in some cases there are constants in in <tt>gtk.h</tt>
6113 corresponding to these atoms. For instance the constant
6114 <tt>GDK_PRIMARY_SELECTION</tt> corresponds to the string "PRIMARY".
6115 In other cases, you should use the functions
6116 <tt>gdk_atom_intern()</tt>, to get the atom corresponding to a string,
6117 and <tt>gdk_atom_name()</tt>, to get the name of an atom. Both
6118 selections and targets are identifed by atoms.
6119
6120 <!-- ----------------------------------------------------------------- -->
6121 <sect1> Retrieving the selection
6122
6123 <p>
6124
6125 Retrieving the selection is an asynchronous process. To start the
6126 process, you call:
6127
6128 <tscreen><verb>
6129 gint gtk_selection_convert   (GtkWidget           *widget, 
6130                               GdkAtom              selection, 
6131                               GdkAtom              target,
6132                               guint32              time)
6133 </verb</tscreen>
6134
6135 This <em>converts</em> the selection into the form specified by
6136 <tt/target/. If it all possible, the time field should be the time
6137 from the event that triggered the selection. This helps make sure that
6138 events occur in the order that the user requested them.
6139  However, if it is not available (for instance, if the conversion was
6140 triggered by a "clicked" signal), then you can use the constant
6141 <tt>GDK_CURRENT_TIME</tt>.
6142
6143 <p>
6144 When the selection owner responds to the request, a
6145 "selection_received" signal is sent to your application. The handler
6146 for this signal receives a pointer to a <tt>GtkSelectionData</tt>
6147 structure, which is defined as:
6148
6149 <tscreen><verb>
6150 struct _GtkSelectionData
6151 {
6152   GdkAtom selection;
6153   GdkAtom target;
6154   GdkAtom type;
6155   gint    format;
6156   guchar *data;
6157   gint    length;
6158 };
6159 </verb></tscreen>
6160
6161 <tt>selection</tt> and <tt>target</tt> are the values you gave in your
6162 <tt>gtk_selection_convert()</tt> call. <tt>type</tt> is an atom that
6163 identifies the type of data returned by the selection owner. Some
6164 possible values are "STRING", a string of latin-1 characters, "ATOM",
6165 a series of atoms, "INTEGER", an integer, etc. Most targets can only
6166 return one type. <tt/format/ gives the length of the units (for
6167 instance characters) in bits. Usually, you don't care about this when
6168 receiving data.  <tt>data</tt> is a pointer to the returned data, and
6169 <tt>length</tt> gives the length of the returned data, in bytes. If
6170 <tt>length</tt> is negative, then an error occurred and the selection
6171 could not be retrieved. This might happen if no application owned the
6172 selection, or if you requested a target that the application didn't
6173 support. The buffer is actually guaranteed to be one byte longer than
6174 <tt>length</tt>; the extra byte will always be zero, so it isn't
6175 necessary to make a copy of strings just to null terminate them.
6176
6177 <p>
6178 In the following example, we retrieve the special target "TARGETS",
6179 which is a list of all targets into which the selection can be
6180 converted.
6181
6182 <tscreen><verb>
6183 /* gettargets.c */
6184
6185 #include <gtk/gtk.h>
6186
6187 void selection_received (GtkWidget *widget, 
6188                          GtkSelectionData *selection_data, 
6189                          gpointer data);
6190
6191 /* Signal handler invoked when user clicks on the "Get Targets" button */
6192 void
6193 get_targets (GtkWidget *widget, gpointer data)
6194 {
6195   static GdkAtom targets_atom = GDK_NONE;
6196
6197   /* Get the atom corresonding to the string "TARGETS" */
6198   if (targets_atom == GDK_NONE)
6199     targets_atom = gdk_atom_intern ("TARGETS", FALSE);
6200
6201   /* And request the "TARGETS" target for the primary selection */
6202   gtk_selection_convert (widget, GDK_SELECTION_PRIMARY, targets_atom,
6203                          GDK_CURRENT_TIME);
6204 }
6205
6206 /* Signal handler called when the selections owner returns the data */
6207 void
6208 selection_received (GtkWidget *widget, GtkSelectionData *selection_data, 
6209                     gpointer data)
6210 {
6211   GdkAtom *atoms;
6212   GList *item_list;
6213   int i;
6214
6215   /* **** IMPORTANT **** Check to see if retrieval succeeded  */
6216   if (selection_data->length < 0)
6217     {
6218       g_print ("Selection retrieval failed\n");
6219       return;
6220     }
6221   /* Make sure we got the data in the expected form */
6222   if (selection_data->type != GDK_SELECTION_TYPE_ATOM)
6223     {
6224       g_print ("Selection \"TARGETS\" was not returned as atoms!\n");
6225       return;
6226     }
6227   
6228   /* Print out the atoms we received */
6229   atoms = (GdkAtom *)selection_data->data;
6230
6231   item_list = NULL;
6232   for (i=0; i<selection_data->length/sizeof(GdkAtom); i++)
6233     {
6234       char *name;
6235       name = gdk_atom_name (atoms[i]);
6236       if (name != NULL)
6237         g_print ("%s\n",name);
6238       else
6239         g_print ("(bad atom)\n");
6240     }
6241
6242   return;
6243 }
6244
6245 int 
6246 main (int argc, char *argv[])
6247 {
6248   GtkWidget *window;
6249   GtkWidget *button;
6250   
6251   gtk_init (&amp;argc, &amp;argv);
6252
6253   /* Create the toplevel window */
6254
6255   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
6256   gtk_window_set_title (GTK_WINDOW (window), "Event Box");
6257   gtk_container_border_width (GTK_CONTAINER (window), 10);
6258
6259   gtk_signal_connect (GTK_OBJECT (window), "destroy",
6260                       GTK_SIGNAL_FUNC (gtk_exit), NULL);
6261
6262   /* Create a button the user can click to get targets */
6263
6264   button = gtk_button_new_with_label ("Get Targets");
6265   gtk_container_add (GTK_CONTAINER (window), button);
6266
6267   gtk_signal_connect (GTK_OBJECT(button), "clicked",
6268                       GTK_SIGNAL_FUNC (get_targets), NULL);
6269   gtk_signal_connect (GTK_OBJECT(button), "selection_received",
6270                       GTK_SIGNAL_FUNC (selection_received), NULL);
6271
6272   gtk_widget_show (button);
6273   gtk_widget_show (window);
6274   
6275   gtk_main ();
6276   
6277   return 0;
6278 }
6279 </verb></tscreen>
6280
6281 <!-- ----------------------------------------------------------------- -->
6282 <sect1> Supplying the selection 
6283
6284 <p>
6285
6286 Supplying the selection is a bit more complicated. You must register 
6287 handlers that will be called when your selection is requested. For
6288 each selection/target pair you will handle, you make a call to:
6289
6290 <tscreen><verb>
6291 void gtk_selection_add_handler (GtkWidget           *widget, 
6292                                 GdkAtom              selection,
6293                                 GdkAtom              target,
6294                                 GtkSelectionFunction function,
6295                                 GtkRemoveFunction    remove_func,
6296                                 gpointer             data);
6297 </verb></tscreen>
6298
6299 <tt/widget/, <tt/selection/, and <tt/target/ identify the requests
6300 this handler will manage.  <tt/remove_func/ if not
6301 NULL, will be called when the signal handler is removed. This is
6302 useful, for instance, for interpreted languages which need to
6303 keep track of a reference count for <tt/data/.
6304
6305 <p>
6306 The callback function has the signature:
6307
6308 <tscreen><verb>
6309 typedef void (*GtkSelectionFunction) (GtkWidget *widget, 
6310                                       GtkSelectionData *selection_data,
6311                                       gpointer data);
6312
6313 </verb></tscreen>
6314
6315 The GtkSelectionData is the same as above, but this time, we're
6316 responsible for filling in the fields <tt/type/, <tt/format/,
6317 <tt/data/, and <tt/length/. (The <tt/format/ field is actually
6318 important here - the X server uses it to figure out whether the data
6319 needs to be byte-swapped or not. Usually it will be 8 - <em/i.e./ a
6320 character - or 32 - <em/i.e./ a. integer.) This is done by calling the
6321 function:
6322
6323 <tscreen><verb>
6324 void gtk_selection_data_set (GtkSelectionData *selection_data,
6325                              GdkAtom           type,
6326                              gint              format,
6327                              guchar           *data,
6328                              gint              length);
6329 </verb></tscreen>
6330
6331 This function takes care of properly making a copy of the data so that
6332 you don't have to worry about keeping it around. (You should not fill
6333 in the fields of the GtkSelectionData structure by hand.)
6334
6335 <p>
6336 When prompted by the user, you claim ownership of the selection by
6337 calling:
6338
6339 <tscreen><verb>
6340 gint gtk_selection_owner_set (GtkWidget           *widget,
6341                               GdkAtom              selection,
6342                               guint32              time);
6343 </verb></tscreen>
6344
6345 If another application claims ownership of the selection, you will
6346 receive a "selection_clear_event".
6347
6348 As an example of supplying the selection, the following program adds
6349 selection functionality to a toggle button. When the toggle button is
6350 depressed, the program claims the primary selection. The only target
6351 supported (aside from certain targets like "TARGETS" supplied by GTK
6352 itself), is the "STRING" target. When this target is requested, a
6353 string representation of the time is returned.
6354
6355 <tscreen><verb>
6356 /* setselection.c */
6357
6358 #include <gtk/gtk.h>
6359 #include <time.h>
6360
6361 /* Callback when the user toggles the selection */
6362 void
6363 selection_toggled (GtkWidget *widget, gint *have_selection)
6364 {
6365   if (GTK_TOGGLE_BUTTON(widget)->active)
6366     {
6367       *have_selection = gtk_selection_owner_set (widget,
6368                                                  GDK_SELECTION_PRIMARY,
6369                                                  GDK_CURRENT_TIME);
6370       /* if claiming the selection failed, we return the button to
6371          the out state */
6372       if (!*have_selection)
6373         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON(widget), FALSE);
6374     }
6375   else
6376     {
6377       if (*have_selection)
6378         {
6379           /* Before clearing the selection by setting the owner to NULL,
6380              we check if we are the actual owner */
6381           if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
6382             gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,
6383                                      GDK_CURRENT_TIME);
6384           *have_selection = FALSE;
6385         }
6386     }
6387 }
6388
6389 /* Called when another application claims the selection */
6390 gint
6391 selection_clear (GtkWidget *widget, GdkEventSelection *event,
6392                  gint *have_selection)
6393 {
6394   *have_selection = FALSE;
6395   gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON(widget), FALSE);
6396
6397   return TRUE;
6398 }
6399
6400 /* Supplies the current time as the selection. */
6401 void
6402 selection_handle (GtkWidget *widget, 
6403                   GtkSelectionData *selection_data,
6404                   gpointer data)
6405 {
6406   gchar *timestr;
6407   time_t current_time;
6408
6409   current_time = time (NULL);
6410   timestr = asctime (localtime(&amp;current_time)); 
6411   /* When we return a single string, it should not be null terminated.
6412      That will be done for us */
6413
6414   gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,
6415                           8, timestr, strlen(timestr));
6416 }
6417
6418 int
6419 main (int argc, char *argv[])
6420 {
6421   GtkWidget *window;
6422
6423   GtkWidget *selection_button;
6424
6425   static int have_selection = FALSE;
6426   
6427   gtk_init (&amp;argc, &amp;argv);
6428
6429   /* Create the toplevel window */
6430
6431   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
6432   gtk_window_set_title (GTK_WINDOW (window), "Event Box");
6433   gtk_container_border_width (GTK_CONTAINER (window), 10);
6434
6435   gtk_signal_connect (GTK_OBJECT (window), "destroy",
6436                       GTK_SIGNAL_FUNC (gtk_exit), NULL);
6437
6438   /* Create a toggle button to act as the selection */
6439
6440   selection_button = gtk_toggle_button_new_with_label ("Claim Selection");
6441   gtk_container_add (GTK_CONTAINER (window), selection_button);
6442   gtk_widget_show (selection_button);
6443
6444   gtk_signal_connect (GTK_OBJECT(selection_button), "toggled",
6445                       GTK_SIGNAL_FUNC (selection_toggled), &amp;have_selection);
6446   gtk_signal_connect (GTK_OBJECT(selection_button), "selection_clear_event",
6447                       GTK_SIGNAL_FUNC (selection_clear), &amp;have_selection);
6448
6449   gtk_selection_add_handler (selection_button, GDK_SELECTION_PRIMARY,
6450                              GDK_SELECTION_TYPE_STRING,
6451                              selection_handle, NULL);
6452
6453   gtk_widget_show (selection_button);
6454   gtk_widget_show (window);
6455   
6456   gtk_main ();
6457   
6458   return 0;
6459 }
6460 </verb></tscreen>
6461
6462
6463 <!-- ***************************************************************** -->
6464 <sect>glib<label id="sec_glib">
6465 <!-- ***************************************************************** -->
6466
6467 <p>
6468 glib provides many useful functions and definitions available for use 
6469 when creating GDK
6470 and GTK applications.  I will list them all here with a brief explanation.
6471 Many are duplicates of standard libc functions so I won't go into
6472 detail on those.  This is mostly to be used as a reference, so you know what is
6473 available for use.
6474
6475 <!-- ----------------------------------------------------------------- -->
6476 <sect1>Definitions
6477 <p>
6478 Definitions for the extremes of many of the standard types are:
6479
6480 <tscreen><verb>
6481 G_MINFLOAT
6482 G_MAXFLOAT
6483 G_MINDOUBLE
6484 G_MAXDOUBLE
6485 G_MINSHORT
6486 G_MAXSHORT
6487 G_MININT
6488 G_MAXINT
6489 G_MINLONG
6490 G_MAXLONG
6491 </verb></tscreen>
6492
6493 Also, the following typedefs.  The ones left unspecified are dynamically set
6494 depending on the architecture.  Remember to avoid counting on the size of a
6495 pointer if you want to be portable!  Eg, a pointer on an Alpha is 8 bytes, but 4
6496 on Intel.
6497
6498 <tscreen><verb>
6499 char   gchar;
6500 short  gshort;
6501 long   glong;
6502 int    gint;
6503 char   gboolean;
6504
6505 unsigned char   guchar;
6506 unsigned short  gushort;
6507 unsigned long   gulong;
6508 unsigned int    guint;
6509
6510 float   gfloat;
6511 double  gdouble;
6512 long double gldouble;
6513
6514 void* gpointer;
6515
6516 gint8
6517 guint8
6518 gint16
6519 guint16
6520 gint32
6521 guint32
6522 </verb></tscreen>
6523
6524 <!-- ----------------------------------------------------------------- -->
6525 <sect1>Doubly Linked Lists
6526 <p>
6527 The following functions are used to create, manage, and destroy doubly
6528 linked lists.  I assume you know what linked lists are, as it is beyond the scope
6529 of this document to explain them.  Of course, it's not required that you
6530 know these for general use of GTK, but they are nice to know.
6531
6532 <tscreen><verb>
6533 GList* g_list_alloc       (void);
6534
6535 void   g_list_free        (GList     *list);
6536
6537 void   g_list_free_1      (GList     *list);
6538
6539 GList* g_list_append      (GList     *list,
6540                            gpointer   data);
6541                            
6542 GList* g_list_prepend     (GList     *list,
6543                            gpointer   data);
6544                         
6545 GList* g_list_insert      (GList     *list,
6546                            gpointer   data,
6547                            gint       position);
6548
6549 GList* g_list_remove      (GList     *list,
6550                            gpointer   data);
6551                            
6552 GList* g_list_remove_link (GList     *list,
6553                            GList     *link);
6554
6555 GList* g_list_reverse     (GList     *list);
6556
6557 GList* g_list_nth         (GList     *list,
6558                            gint       n);
6559                            
6560 GList* g_list_find        (GList     *list,
6561                            gpointer   data);
6562
6563 GList* g_list_last        (GList     *list);
6564
6565 GList* g_list_first       (GList     *list);
6566
6567 gint   g_list_length      (GList     *list);
6568
6569 void   g_list_foreach     (GList     *list,
6570                            GFunc      func,
6571                            gpointer   user_data);
6572 </verb></tscreen>                                             
6573
6574 <!-- ----------------------------------------------------------------- -->
6575 <sect1>Singly Linked Lists
6576 <p>
6577 Many of the above functions for singly linked lists are identical to the
6578 above.  Here is a complete list:
6579 <tscreen><verb>
6580 GSList* g_slist_alloc       (void);
6581
6582 void    g_slist_free        (GSList   *list);
6583
6584 void    g_slist_free_1      (GSList   *list);
6585
6586 GSList* g_slist_append      (GSList   *list,
6587                              gpointer  data);
6588                 
6589 GSList* g_slist_prepend     (GSList   *list,
6590                              gpointer  data);
6591                              
6592 GSList* g_slist_insert      (GSList   *list,
6593                              gpointer  data,
6594                              gint      position);
6595                              
6596 GSList* g_slist_remove      (GSList   *list,
6597                              gpointer  data);
6598                              
6599 GSList* g_slist_remove_link (GSList   *list,
6600                              GSList   *link);
6601                              
6602 GSList* g_slist_reverse     (GSList   *list);
6603
6604 GSList* g_slist_nth         (GSList   *list,
6605                              gint      n);
6606                              
6607 GSList* g_slist_find        (GSList   *list,
6608                              gpointer  data);
6609                              
6610 GSList* g_slist_last        (GSList   *list);
6611
6612 gint    g_slist_length      (GSList   *list);
6613
6614 void    g_slist_foreach     (GSList   *list,
6615                              GFunc     func,
6616                              gpointer  user_data);
6617         
6618 </verb></tscreen>
6619
6620 <!-- ----------------------------------------------------------------- -->
6621 <sect1>Memory Management
6622 <p>
6623 <tscreen><verb>
6624 gpointer g_malloc      (gulong    size);
6625 </verb></tscreen>
6626
6627 This is a replacement for malloc().  You do not need to check the return
6628 vaule as it is done for you in this function.
6629
6630 <tscreen><verb>
6631 gpointer g_malloc0     (gulong    size);
6632 </verb></tscreen>
6633
6634 Same as above, but zeroes the memory before returning a pointer to it.
6635
6636 <tscreen><verb>
6637 gpointer g_realloc     (gpointer  mem,
6638                         gulong    size);
6639 </verb></tscreen>
6640
6641 Relocates "size" bytes of memory starting at "mem".  Obviously, the memory should have been
6642 previously allocated.
6643
6644 <tscreen><verb>
6645 void     g_free        (gpointer  mem);
6646 </verb></tscreen>
6647
6648 Frees memory.  Easy one.
6649
6650 <tscreen><verb>
6651 void     g_mem_profile (void);
6652 </verb></tscreen>
6653
6654 Dumps a profile of used memory, but requries that you add #define
6655 MEM_PROFILE to the top of glib/gmem.c and re-make and make install.
6656
6657 <tscreen><verb>
6658 void     g_mem_check   (gpointer  mem);
6659 </verb></tscreen>
6660
6661 Checks that a memory location is valid.  Requires you add #define
6662 MEM_CHECK to the top of gmem.c and re-make and make install.
6663
6664 <!-- ----------------------------------------------------------------- -->
6665 <sect1>Timers
6666 <p>
6667 Timer functions..
6668
6669 <tscreen><verb>
6670 GTimer* g_timer_new     (void);
6671
6672 void    g_timer_destroy (GTimer  *timer);
6673
6674 void    g_timer_start   (GTimer  *timer);
6675
6676 void    g_timer_stop    (GTimer  *timer);
6677
6678 void    g_timer_reset   (GTimer  *timer);
6679
6680 gdouble g_timer_elapsed (GTimer  *timer,
6681                          gulong  *microseconds);
6682 </verb></tscreen>                        
6683
6684 <!-- ----------------------------------------------------------------- -->
6685 <sect1>String Handling
6686 <p>
6687 A whole mess of string handling functions.  They all look very interesting, and
6688 probably better for many purposes than the standard C string functions, but
6689 require documentation.
6690
6691 <tscreen><verb>
6692 GString* g_string_new       (gchar   *init);
6693 void     g_string_free      (GString *string,
6694                              gint     free_segment);
6695                              
6696 GString* g_string_assign    (GString *lval,
6697                              gchar   *rval);
6698                              
6699 GString* g_string_truncate  (GString *string,
6700                              gint     len);
6701                              
6702 GString* g_string_append    (GString *string,
6703                              gchar   *val);
6704                             
6705 GString* g_string_append_c  (GString *string,
6706                              gchar    c);
6707         
6708 GString* g_string_prepend   (GString *string,
6709                              gchar   *val);
6710                              
6711 GString* g_string_prepend_c (GString *string,
6712                              gchar    c);
6713         
6714 void     g_string_sprintf   (GString *string,
6715                              gchar   *fmt,
6716                              ...);
6717         
6718 void     g_string_sprintfa  (GString *string,
6719                              gchar   *fmt,
6720                              ...);
6721 </verb></tscreen>                                                         
6722
6723 <!-- ----------------------------------------------------------------- -->
6724 <sect1>Utility and Error Functions
6725 <p>
6726 <tscreen><verb>
6727 gchar* g_strdup    (const gchar *str);
6728 </verb></tscreen>
6729
6730 Replacement strdup function.  Copies the
6731 original strings contents to newly allocated memory, and returns a pointer to it.
6732
6733 <tscreen><verb>
6734 gchar* g_strerror  (gint errnum);
6735 </verb></tscreen>
6736
6737 I recommend using this for all error messages.  It's much nicer, and more
6738 portable than perror() or others.  The output is usually of the form:
6739
6740 <tscreen><verb>
6741 program name:function that failed:file or further description:strerror
6742 </verb></tscreen>
6743
6744 Here's an example of one such call used in our hello_world program:
6745
6746 <tscreen><verb>
6747 g_print("hello_world:open:%s:%s\n", filename, g_strerror(errno));
6748 </verb></tscreen>
6749
6750 <tscreen><verb>
6751 void g_error   (gchar *format, ...);
6752 </verb></tscreen>
6753
6754 Prints an error message.  The format is just like printf, but it
6755 prepends "** ERROR **: " to your message, and exits the program.  
6756 Use only for fatal errors.
6757
6758 <tscreen><verb>
6759 void g_warning (gchar *format, ...);
6760 </verb></tscreen>
6761
6762 Same as above, but prepends "** WARNING **: ", and does not exit the
6763 program.
6764
6765 <tscreen><verb>
6766 void g_message (gchar *format, ...);
6767 </verb></tscreen>
6768
6769 Prints "message: " prepended to the string you pass in.
6770
6771 <tscreen><verb>
6772 void g_print   (gchar *format, ...);
6773 </verb></tscreen>
6774
6775 Replacement for printf().
6776
6777 And our last function:
6778
6779 <tscreen><verb>
6780 gchar* g_strsignal (gint signum);
6781 </verb></tscreen>
6782
6783 Prints out the name of the Unix system signal given the signal number.
6784 Useful in generic signal handling functions.
6785
6786 All of the above are more or less just stolen from glib.h.  If anyone cares
6787 to document any function, just send me an email!
6788
6789 <!-- ***************************************************************** -->
6790 <sect>GTK's rc Files
6791 <!-- ***************************************************************** -->
6792
6793 <p>
6794 GTK has it's own way of dealing with application defaults, by using rc
6795 files.  These can be used to set the colors of just about any widget, and
6796 can also be used to tile pixmaps onto the background of some widgets.  
6797
6798 <!-- ----------------------------------------------------------------- -->
6799 <sect1>Functions For rc Files 
6800 <p>
6801 When your application starts, you should include a call to:
6802 <tscreen><verb>
6803 void gtk_rc_parse (char *filename);
6804 </verb></tscreen>
6805 <p>
6806 Passing in the filename of your rc file.  This will cause GTK to parse this
6807 file, and use the style settings for the widget types defined there.
6808 <p>
6809 If you wish to have a special set of widgets that can take on a different
6810 style from others, or any other logical division of widgets, use a call to:
6811 <tscreen><verb>
6812 void gtk_widget_set_name (GtkWidget *widget,
6813                           gchar *name);
6814 </verb></tscreen>
6815 <p>
6816 Passing your newly created widget as the first argument, and the name
6817 you wish to give it as the second.  This will allow you to change the
6818 attributes of this widget by name through the rc file.
6819 <p>
6820 If we use a call something like this:
6821
6822 <tscreen><verb>
6823 button = gtk_button_new_with_label ("Special Button");
6824 gtk_widget_set_name (button, "special button");
6825 </verb></tscreen>
6826 <p>
6827 Then this button is given the name "special button" and may be addressed by
6828 name in the rc file as "special button.GtkButton".  [<--- Verify ME!]
6829 <p>
6830 The example rc file below, sets the properties of the main window, and lets
6831 all children of that main window inherit the style described by the "main
6832 button" style.  The code used in the application is:
6833
6834 <tscreen><verb>
6835 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
6836 gtk_widget_set_name (window, "main window");
6837 </verb></tscreen>
6838 <p>
6839 And then the style is defined in the rc file using:
6840
6841 <tscreen><verb>
6842 widget "main window.*GtkButton*" style "main_button"
6843 </verb></tscreen>
6844 <p>
6845 Which sets all the GtkButton widgets in the "main window" to the
6846 "main_buttons" style as defined in the rc file.
6847 <p>
6848 As you can see, this is a fairly powerful and flexible system.  Use your
6849 imagination as to how best to take advantage of this.
6850
6851 <!-- ----------------------------------------------------------------- -->
6852 <sect1>GTK's rc File Format
6853 <p>
6854 The format of the GTK file is illustrated in the example below.  This is
6855 the testgtkrc file from the GTK distribution, but I've added a
6856 few comments and things.  You may wish to include this explanation
6857 your application to allow the user to fine tune his application.
6858 <p>
6859 There are several directives to change the attributes of a widget.
6860 <itemize>
6861 <item>fg - Sets the foreground color of a widget.
6862 <item>bg - Sets the background color of a widget.
6863 <item>bg_pixmap - Sets the background of a widget to a tiled pixmap.
6864 <item>font - Sets the font to be used with the given widget.
6865 </itemize>
6866 <p>
6867 In addition to this, there are several states a widget can be in, and you
6868 can set different colors, pixmaps and fonts for each state.  These states are:
6869 <itemize>
6870 <item>NORMAL - The normal state of a widget, without the mouse over top of
6871 it, and not being pressed etc.
6872 <item>PRELIGHT - When the mouse is over top of the widget, colors defined
6873 using this state will be in effect.
6874 <item>ACTIVE - When the widget is pressed or clicked it will be active, and
6875 the attributes assigned by this tag will be in effect.
6876 <item>INSENSITIVE - When a widget is set insensitive, and cannot be
6877 activated, it will take these attributes.
6878 <item>SELECTED - When an object is selected, it takes these attributes.
6879 </itemize>
6880 <p>
6881 When using the "fg" and "bg" keywords to set the colors of widgets, the
6882 format is:
6883 <tscreen><verb>
6884 fg[<STATE>] = { Red, Green, Blue }
6885 </verb></tscreen>
6886 <p>
6887 Where STATE is one of the above states (PRELIGHT, ACTIVE etc), and the Red,
6888 Green and Blue are values in the range of 0 - 1.0,  { 1.0, 1.0, 1.0 } being
6889 white.
6890 They must be in float form, or they will register as 0, so a straight 
6891 "1" will not work, it must
6892 be "1.0".  A straight "0" is fine because it doesn't matter if it's not
6893 recognized.  Unrecognized values are set to 0.
6894 <p>
6895 bg_pixmap is very similar to the above, except the colors are replaced by a
6896 filename.
6897
6898 pixmap_path is a list of paths seperated by ":"'s.  These paths will be
6899 searched for any pixmap you specify.
6900
6901 <p>
6902 The font directive is simply:
6903 <tscreen><verb>
6904 font = "<font name>"
6905 </verb></tscreen>
6906 <p>
6907 Where the only hard part is figuring out the font string.  Using xfontsel or
6908 similar utility should help.
6909 <p>
6910 The "widget_class" sets the style of a class of widgets.  These classes are
6911 listed in the widget overview on the class hierarchy.
6912 <p>
6913 The "widget" directive sets a specificaly named set of widgets to a
6914 given style, overriding any style set for the given widget class.
6915 These widgets are registered inside the application using the
6916 gtk_widget_set_name() call.  This allows you to specify the attributes of a
6917 widget on a per widget basis, rather than setting the attributes of an
6918 entire widget class.  I urge you to document any of these special widgets so
6919 users may customize them.
6920 <p>
6921 When the keyword "<tt>parent</>" is used as an attribute, the widget will take on
6922 the attributes of it's parent in the application.
6923 <p>
6924 When defining a style, you may assign the attributes of a previously defined
6925 style to this new one.
6926 <tscreen><verb>
6927 style "main_button" = "button"
6928 {
6929   font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"
6930   bg[PRELIGHT] = { 0.75, 0, 0 }
6931 }
6932 </verb></tscreen>
6933 <p>
6934 This example takes the "button" style, and creates a new "main_button" style
6935 simply by changing the font and prelight background color of the "button"
6936 style.
6937 <p>
6938 Of course, many of these attributes don't apply to all widgets.  It's a
6939 simple matter of common sense really.  Anything that could apply, should.
6940
6941 <!-- ----------------------------------------------------------------- -->
6942 <sect1>Example rc file
6943 <p>
6944
6945 <tscreen><verb>
6946 # pixmap_path "<dir 1>:<dir 2>:<dir 3>:..."
6947 #
6948 pixmap_path "/usr/include/X11R6/pixmaps:/home/imain/pixmaps"
6949 #
6950 # style <name> [= <name>]
6951 # {
6952 #   <option>
6953 # }
6954 #
6955 # widget <widget_set> style <style_name>
6956 # widget_class <widget_class_set> style <style_name>
6957
6958
6959 # Here is a list of all the possible states.  Note that some do not apply to
6960 # certain widgets.
6961 #
6962 # NORMAL - The normal state of a widget, without the mouse over top of
6963 # it, and not being pressed etc.
6964 #
6965 # PRELIGHT - When the mouse is over top of the widget, colors defined
6966 # using this state will be in effect.
6967 #
6968 # ACTIVE - When the widget is pressed or clicked it will be active, and
6969 # the attributes assigned by this tag will be in effect.
6970 #
6971 # INSENSITIVE - When a widget is set insensitive, and cannot be
6972 # activated, it will take these attributes.
6973 #
6974 # SELECTED - When an object is selected, it takes these attributes.
6975 #
6976 # Given these states, we can set the attributes of the widgets in each of
6977 # these states using the following directives.
6978 #
6979 # fg - Sets the foreground color of a widget.
6980 # fg - Sets the background color of a widget.
6981 # bg_pixmap - Sets the background of a widget to a tiled pixmap.
6982 # font - Sets the font to be used with the given widget.
6983 #
6984
6985 # This sets a style called "button".  The name is not really important, as
6986 # it is assigned to the actual widgets at the bottom of the file.
6987
6988 style "window"
6989 {
6990   #This sets the padding around the window to the pixmap specified.
6991   #bg_pixmap[<STATE>] = "<pixmap filename>"
6992   bg_pixmap[NORMAL] = "warning.xpm"
6993 }
6994
6995 style "scale"
6996 {
6997   #Sets the foreground color (font color) to red when in the "NORMAL"
6998   #state.
6999   
7000   fg[NORMAL] = { 1.0, 0, 0 }
7001   
7002   #Sets the background pixmap of this widget to that of it's parent.
7003   bg_pixmap[NORMAL] = "<parent>"
7004 }
7005
7006 style "button"
7007 {
7008   # This shows all the possible states for a button.  The only one that
7009   # doesn't apply is the SELECTED state.
7010   
7011   fg[PRELIGHT] = { 0, 1.0, 1.0 }
7012   bg[PRELIGHT] = { 0, 0, 1.0 }
7013   bg[ACTIVE] = { 1.0, 0, 0 }
7014   fg[ACTIVE] = { 0, 1.0, 0 }
7015   bg[NORMAL] = { 1.0, 1.0, 0 }
7016   fg[NORMAL] = { .99, 0, .99 }
7017   bg[INSENSITIVE] = { 1.0, 1.0, 1.0 }
7018   fg[INSENSITIVE] = { 1.0, 0, 1.0 }
7019 }
7020
7021 # In this example, we inherit the attributes of the "button" style and then
7022 # override the font and background color when prelit to create a new
7023 # "main_button" style.
7024
7025 style "main_button" = "button"
7026 {
7027   font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"
7028   bg[PRELIGHT] = { 0.75, 0, 0 }
7029 }
7030
7031 style "toggle_button" = "button"
7032 {
7033   fg[NORMAL] = { 1.0, 0, 0 }
7034   fg[ACTIVE] = { 1.0, 0, 0 }
7035   
7036   # This sets the background pixmap of the toggle_button to that of it's
7037   # parent widget (as defined in the application).
7038   bg_pixmap[NORMAL] = "<parent>"
7039 }
7040
7041 style "text"
7042 {
7043   bg_pixmap[NORMAL] = "marble.xpm"
7044   fg[NORMAL] = { 1.0, 1.0, 1.0 }
7045 }
7046
7047 style "ruler"
7048 {
7049   font = "-adobe-helvetica-medium-r-normal--*-80-*-*-*-*-*-*"
7050 }
7051
7052 # pixmap_path "~/.pixmaps"
7053
7054 # These set the widget types to use the styles defined above.
7055 # The widget types are listed in the class hierarchy, but could probably be
7056 # just listed in this document for the users reference.
7057
7058 widget_class "GtkWindow" style "window"
7059 widget_class "GtkDialog" style "window"
7060 widget_class "GtkFileSelection" style "window"
7061 widget_class "*Gtk*Scale" style "scale"
7062 widget_class "*GtkCheckButton*" style "toggle_button"
7063 widget_class "*GtkRadioButton*" style "toggle_button"
7064 widget_class "*GtkButton*" style "button"
7065 widget_class "*Ruler" style "ruler"
7066 widget_class "*GtkText" style "text"
7067
7068 # This sets all the buttons that are children of the "main window" to
7069 # the main_buton style.  These must be documented to be taken advantage of.
7070 widget "main window.*GtkButton*" style "main_button"
7071 </verb></tscreen>
7072
7073 <!-- ***************************************************************** -->
7074 <sect>Writing Your Own Widgets 
7075 <!-- ***************************************************************** -->
7076
7077 <!-- ----------------------------------------------------------------- -->
7078 <sect1> Overview
7079 <p>
7080 Although the GTK distribution comes with many types of widgets that
7081 should cover most basic needs, there may come a time when you need to
7082 create your own new widget type. Since GTK uses widget inheretence
7083 extensively, and there is already a widget that
7084 is close to what you want, it is often possible to make a useful new widget type in
7085 just a few lines of code.  But before starting work on a new widget, check
7086 around first to make sure that someone has not already written
7087 it. This will prevent duplication of effort and keep the number of
7088 GTK widgets out there to a minimum, which will help keep both the code
7089 and the interface of different applications consistent. As a flip side
7090 to this, once you finish your widget, announce it to the world so
7091 other people can benefit. The best place to do this is probably the
7092 <tt>gtk-list</tt>.
7093
7094 Complete sources for the example widgets are available at the place you 
7095 got this tutorial, or from:
7096
7097 <htmlurl url="http://www.msc.cornell.edu/~otaylor/gtk-gimp/tutorial"
7098 name="http://www.msc.cornell.edu/~otaylor/gtk-gimp/tutorial">
7099
7100
7101 <!-- ----------------------------------------------------------------- -->
7102 <sect1> The Anatomy Of A Widget
7103
7104 <p>
7105 In order to create a new widget, it is important to have an
7106 understanding of how GTK objects work. This section is just meant as a
7107 brief overview. See the reference documentation for the details. 
7108
7109 <p>
7110 GTK widgets are implemented in an object oriented fashion. However,
7111 they are implemented in standard C. This greatly improves portability
7112 and stability over using current generation C++ compilers; however,
7113 it does mean that the widget writer has to pay attention to some of
7114 the implementation details. The information common to all instances of
7115 one class of widgets (e.g., to all Button widgets) is stored in the 
7116 <em>class structure</em>. There is only one copy of this in
7117 which is stored information about the class's signals
7118 (which act like virtual functions in C). To support inheritance, the
7119 first field in the class structure must be a copy of the parent's
7120 class structure. The declaration of the class structure of GtkButtton
7121 looks like:
7122
7123 <tscreen><verb>
7124 struct _GtkButtonClass
7125 {
7126   GtkContainerClass parent_class;
7127
7128   void (* pressed)  (GtkButton *button);
7129   void (* released) (GtkButton *button);
7130   void (* clicked)  (GtkButton *button);
7131   void (* enter)    (GtkButton *button);
7132   void (* leave)    (GtkButton *button);
7133 };
7134 </verb></tscreen>
7135
7136 <p>
7137 When a button is treated as a container (for instance, when it is
7138 resized), its class structure can be cast to GtkContainerClass, and
7139 the relevant fields used to handle the signals.
7140
7141 <p>
7142 There is also a structure for each widget that is created on a
7143 per-instance basis. This structure has fields to store information that
7144 is different for each instance of the widget. We'll call this
7145 structure the <em>object structure</em>. For the Button class, it looks
7146 like:
7147
7148 <tscreen><verb>
7149 struct _GtkButton
7150 {
7151   GtkContainer container;
7152
7153   GtkWidget *child;
7154
7155   guint in_button : 1;
7156   guint button_down : 1;
7157 };
7158 </verb></tscreen>
7159
7160 <p>
7161 Note that, similar to the class structure, the first field is the
7162 object structure of the parent class, so that this structure can be
7163 cast to the parent class's object structure as needed.
7164
7165 <!-- ----------------------------------------------------------------- -->
7166 <sect1> Creating a Composite widget
7167
7168 <!-- ----------------------------------------------------------------- -->
7169 <sect2> Introduction
7170
7171 <p>
7172 One type of widget that you may be interested in creating is a
7173 widget that is merely an aggregate of other GTK widgets. This type of
7174 widget does nothing that couldn't be done without creating new
7175 widgets, but provides a convenient way of packaging user interface
7176 elements for reuse. The FileSelection and ColorSelection widgets in
7177 the standard distribution are examples of this type of widget.
7178
7179 <p>
7180 The example widget that we'll create in this section is the Tictactoe
7181 widget, a 3x3 array of toggle buttons which triggers a signal when all
7182 three buttons in a row, column, or on one of the diagonals are
7183 depressed. 
7184
7185 <!-- ----------------------------------------------------------------- -->
7186 <sect2> Choosing a parent class
7187
7188 <p>
7189 The parent class for a composite widget is typically the container
7190 class that holds all of the elements of the composite widget. For
7191 example, the parent class of the FileSelection widget is the
7192 Dialog class. Since our buttons will be arranged in a table, it
7193 might seem natural to make our parent class the GtkTable
7194 class. Unfortunately, this turns out not to work. The creation of a
7195 widget is divided among two functions - a <tt/WIDGETNAME_new()/
7196 function that the user calls, and a <tt/WIDGETNAME_init()/ function
7197 which does the basic work of initializing the widget which is
7198 independent of the arguments passed to the <tt/_new()/
7199 function. Descendent widgets only call the <tt/_init/ function of
7200 their parent widget. But this division of labor doesn't work well for
7201 tables, which when created, need to know the number of rows and
7202 columns in the table. Unless we want to duplicate most of the
7203 functionality of <tt/gtk_table_new()/ in our Tictactoe widget, we had
7204 best avoid deriving it from GtkTable. For that reason, we derive it
7205 from GtkVBox instead, and stick our table inside the VBox.
7206
7207 <!-- ----------------------------------------------------------------- -->
7208 <sect2> The header file
7209
7210 <p>
7211 Each widget class has a header file which declares the object and
7212 class structures for that widget, along with public functions. 
7213 A couple of features are worth pointing out. To prevent duplicate
7214 definitions, we wrap the entire header file in:
7215
7216 <tscreen><verb>
7217 #ifndef __TICTACTOE_H__
7218 #define __TICTACTOE_H__
7219 .
7220 .
7221 .
7222 #endif /* __TICTACTOE_H__ */
7223 </verb></tscreen>
7224
7225 And to keep C++ programs that include the header file happy, in:
7226
7227 <tscreen><verb>
7228 #ifdef __cplusplus
7229 extern "C" {
7230 #endif /* __cplusplus */
7231 .
7232 .
7233 .
7234 #ifdef __cplusplus
7235 }
7236 #endif /* __cplusplus */
7237 </verb></tscreen>
7238
7239 Along with the functions and structures, we declare three standard
7240 macros in our header file, <tt/TICTACTOE(obj)/,
7241 <tt/TICTACTOE_CLASS(klass)/, and <tt/IS_TICTACTOE(obj)/, which cast a
7242 pointer into a pointer to the object or class structure, and check
7243 if an object is a Tictactoe widget respectively.
7244
7245 <p>
7246 Here is the complete header file:
7247
7248 <tscreen><verb>
7249 /* tictactoe.h */
7250
7251 #ifndef __TICTACTOE_H__
7252 #define __TICTACTOE_H__
7253
7254 #include <gdk/gdk.h>
7255 #include <gtk/gtkvbox.h>
7256
7257 #ifdef __cplusplus
7258 extern "C" {
7259 #endif /* __cplusplus */
7260
7261 #define TICTACTOE(obj)          GTK_CHECK_CAST (obj, tictactoe_get_type (), Tictactoe)
7262 #define TICTACTOE_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, tictactoe_get_type (), TictactoeClass)
7263 #define IS_TICTACTOE(obj)       GTK_CHECK_TYPE (obj, tictactoe_get_type ())
7264
7265
7266 typedef struct _Tictactoe       Tictactoe;
7267 typedef struct _TictactoeClass  TictactoeClass;
7268
7269 struct _Tictactoe
7270 {
7271   GtkVBox vbox;
7272   
7273   GtkWidget *buttons[3][3];
7274 };
7275
7276 struct _TictactoeClass
7277 {
7278   GtkVBoxClass parent_class;
7279
7280   void (* tictactoe) (Tictactoe *ttt);
7281 };
7282
7283 guint          tictactoe_get_type        (void);
7284 GtkWidget*     tictactoe_new             (void);
7285 void           tictactoe_clear           (Tictactoe *ttt);
7286
7287 #ifdef __cplusplus
7288 }
7289 #endif /* __cplusplus */
7290
7291 #endif /* __TICTACTOE_H__ */
7292
7293 </verb></tscreen>
7294
7295 <!-- ----------------------------------------------------------------- -->
7296 <sect2> The <tt/_get_type()/ function.
7297
7298 <p>
7299 We now continue on to the implementation of our widget. A core
7300 function for every widget is the function
7301 <tt/WIDGETNAME_get_type()/. This function, when first called, tells
7302 GTK about the widget class, and gets an ID that uniquely identifies
7303 the widget class. Upon subsequent calls, it just returns the ID.
7304
7305 <tscreen><verb>
7306 guint
7307 tictactoe_get_type ()
7308 {
7309   static guint ttt_type = 0;
7310
7311   if (!ttt_type)
7312     {
7313       GtkTypeInfo ttt_info =
7314       {
7315         "Tictactoe",
7316         sizeof (Tictactoe),
7317         sizeof (TictactoeClass),
7318         (GtkClassInitFunc) tictactoe_class_init,
7319         (GtkObjectInitFunc) tictactoe_init,
7320         (GtkArgSetFunc) NULL,
7321         (GtkArgGetFunc) NULL
7322       };
7323
7324       ttt_type = gtk_type_unique (gtk_vbox_get_type (), &amp;ttt_info);
7325     }
7326
7327   return ttt_type;
7328 }
7329 </verb></tscreen>
7330
7331 <p>
7332 The GtkTypeInfo structure has the following definition:
7333
7334 <tscreen><verb>
7335 struct _GtkTypeInfo
7336 {
7337   gchar *type_name;
7338   guint object_size;
7339   guint class_size;
7340   GtkClassInitFunc class_init_func;
7341   GtkObjectInitFunc object_init_func;
7342   GtkArgSetFunc arg_set_func;
7343   GtkArgGetFunc arg_get_func;
7344 };
7345 </verb></tscreen>
7346
7347 <p>
7348 The fields of this structure are pretty self-explanatory. We'll ignore
7349 the <tt/arg_set_func/ and <tt/arg_get_func/ fields here: they have an important, 
7350 but as yet largely
7351 unimplemented, role in allowing widget options to be conveniently set
7352 from interpreted languages. Once GTK has a correctly filled in copy of
7353 this structure, it knows how to create objects of a particular widget
7354 type. 
7355
7356 <!-- ----------------------------------------------------------------- -->
7357 <sect2> The <tt/_class_init()/ function
7358
7359 <p>
7360 The <tt/WIDGETNAME_class_init()/ function initializes the fields of
7361 the widget's class structure, and sets up any signals for the
7362 class. For our Tictactoe widget it looks like:
7363
7364 <tscreen><verb>
7365
7366 enum {
7367   TICTACTOE_SIGNAL,
7368   LAST_SIGNAL
7369 };
7370
7371 static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
7372
7373 static void
7374 tictactoe_class_init (TictactoeClass *class)
7375 {
7376   GtkObjectClass *object_class;
7377
7378   object_class = (GtkObjectClass*) class;
7379   
7380   tictactoe_signals[TICTACTOE_SIGNAL] = gtk_signal_new ("tictactoe",
7381                                          GTK_RUN_FIRST,
7382                                          object_class->type,
7383                                          GTK_SIGNAL_OFFSET (TictactoeClass, tictactoe),
7384                                          gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
7385
7386
7387   gtk_object_class_add_signals (object_class, tictactoe_signals, LAST_SIGNAL);
7388
7389   class->tictactoe = NULL;
7390 }
7391 </verb></tscreen>
7392
7393 <p>
7394 Our widget has just one signal, the ``tictactoe'' signal that is
7395 invoked when a row, column, or diagonal is completely filled in. Not
7396 every composite widget needs signals, so if you are reading this for
7397 the first time, you may want to skip to the next section now, as
7398 things are going to get a bit complicated.
7399
7400 The function:
7401
7402 <tscreen><verb>
7403 gint   gtk_signal_new (const gchar         *name,
7404                        GtkSignalRunType    run_type,
7405                        GtkType             object_type,
7406                        gint                function_offset,
7407                        GtkSignalMarshaller marshaller,
7408                        GtkType             return_val,
7409                        guint               nparams,
7410                        ...);
7411 </verb></tscreen>
7412
7413 Creates a new signal. The parameters are:
7414
7415 <itemize>
7416 <item> <tt/name/: The name of the signal.
7417 <item> <tt/run_type/: Whether the default handler runs before or after
7418 user handlers. Usually this will be <tt/GTK_RUN_FIRST/, or <tt/GTK_RUN_LAST/,
7419 although there are other possibilities.
7420 <item> <tt/object_type/: The ID of the object that this signal applies
7421 to. (It will also apply to that objects descendents)
7422 <item> <tt/function_offset/: The offset within the class structure of
7423 a pointer to the default handler.
7424 <item> <tt/marshaller/: A function that is used to invoke the signal
7425 handler. For signal handlers that have no arguments other than the
7426 object that emitted the signal and user data, we can use the
7427 pre-supplied marshaller function <tt/gtk_signal_default_marshaller/.
7428 <item> <tt/return_val/: The type of the return val.
7429 <item> <tt/nparams/: The number of parameters of the signal handler
7430 (other than the two default ones mentioned above)
7431 <item> <tt/.../: The types of the parameters.
7432 </itemize>
7433
7434 When specifying types, the <tt/GtkType/ enumeration is used:
7435
7436 <tscreen><verb>
7437 typedef enum
7438 {
7439   GTK_TYPE_INVALID,
7440   GTK_TYPE_NONE,
7441   GTK_TYPE_CHAR,
7442   GTK_TYPE_BOOL,
7443   GTK_TYPE_INT,
7444   GTK_TYPE_UINT,
7445   GTK_TYPE_LONG,
7446   GTK_TYPE_ULONG,
7447   GTK_TYPE_FLOAT,
7448   GTK_TYPE_DOUBLE,
7449   GTK_TYPE_STRING,
7450   GTK_TYPE_ENUM,
7451   GTK_TYPE_FLAGS,
7452   GTK_TYPE_BOXED,
7453   GTK_TYPE_FOREIGN,
7454   GTK_TYPE_CALLBACK,
7455   GTK_TYPE_ARGS,
7456
7457   GTK_TYPE_POINTER,
7458
7459   /* it'd be great if the next two could be removed eventually */
7460   GTK_TYPE_SIGNAL,
7461   GTK_TYPE_C_CALLBACK,
7462
7463   GTK_TYPE_OBJECT
7464
7465 } GtkFundamentalType;
7466 </verb></tscreen>
7467
7468 <p>
7469 <tt/gtk_signal_new()/ returns a unique integer identifier for the
7470 signal, that we store in the <tt/tictactoe_signals/ array, which we
7471 index using an enumeration. (Conventionally, the enumeration elements
7472 are the signal name, uppercased, but here there would be a conflict
7473 with the <tt/TICTACTOE()/ macro, so we called it <tt/TICTACTOE_SIGNAL/
7474 instead.
7475
7476 After creating our signals, we need to tell GTK to associate our
7477 signals with the Tictactoe class. We do that by calling
7478 <tt/gtk_object_class_add_signals()/. We then set the pointer which
7479 points to the default handler for the ``tictactoe'' signal to NULL,
7480 indicating that there is no default action.
7481
7482 <!-- ----------------------------------------------------------------- -->
7483 <sect2> The <tt/_init()/ function.
7484
7485 <p>
7486
7487 Each widget class also needs a function to initialize the object
7488 structure. Usually, this function has the fairly limited role of
7489 setting the fields of the structure to default values. For composite
7490 widgets, however, this function also creates the component widgets.
7491
7492 <tscreen><verb>
7493 static void
7494 tictactoe_init (Tictactoe *ttt)
7495 {
7496   GtkWidget *table;
7497   gint i,j;
7498   
7499   table = gtk_table_new (3, 3, TRUE);
7500   gtk_container_add (GTK_CONTAINER(ttt), table);
7501   gtk_widget_show (table);
7502
7503   for (i=0;i<3; i++)
7504     for (j=0;j<3; j++)
7505       {
7506         ttt->buttons[i][j] = gtk_toggle_button_new ();
7507         gtk_table_attach_defaults (GTK_TABLE(table), ttt->buttons[i][j], 
7508                                    i, i+1, j, j+1);
7509         gtk_signal_connect (GTK_OBJECT (ttt->buttons[i][j]), "toggled",
7510                             GTK_SIGNAL_FUNC (tictactoe_toggle), ttt);
7511         gtk_widget_set_usize (ttt->buttons[i][j], 20, 20);
7512         gtk_widget_show (ttt->buttons[i][j]);
7513       }
7514 }
7515 </verb></tscreen>
7516
7517 <!-- ----------------------------------------------------------------- -->
7518 <sect2> And the rest...
7519
7520 <p>
7521
7522 There is one more function that every widget (except for base widget
7523 types like GtkBin that cannot be instantiated) needs to have - the
7524 function that the user calls to create an object of that type. This is
7525 conventionally called <tt/WIDGETNAME_new()/. In some
7526 widgets, though not for the Tictactoe widgets, this function takes
7527 arguments, and does some setup based on the arguments. The other two
7528 functions are specific to the Tictactoe widget. 
7529
7530 <p>
7531 <tt/tictactoe_clear()/ is a public function that resets all the
7532 buttons in the widget to the up position. Note the use of
7533 <tt/gtk_signal_handler_block_by_data()/ to keep our signal handler for
7534 button toggles from being triggered unnecessarily.
7535
7536 <p>
7537 <tt/tictactoe_toggle()/ is the signal handler that is invoked when the
7538 user clicks on a button. It checks to see if there are any winning
7539 combinations that involve the toggled button, and if so, emits
7540 the "tictactoe" signal.
7541
7542 <tscreen><verb>  
7543 GtkWidget*
7544 tictactoe_new ()
7545 {
7546   return GTK_WIDGET ( gtk_type_new (tictactoe_get_type ()));
7547 }
7548
7549 void           
7550 tictactoe_clear (Tictactoe *ttt)
7551 {
7552   int i,j;
7553
7554   for (i=0;i<3;i++)
7555     for (j=0;j<3;j++)
7556       {
7557         gtk_signal_handler_block_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
7558         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
7559                                      FALSE);
7560         gtk_signal_handler_unblock_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
7561       }
7562 }
7563
7564 static void
7565 tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
7566 {
7567   int i,k;
7568
7569   static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
7570                              { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
7571                              { 0, 1, 2 }, { 0, 1, 2 } };
7572   static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
7573                              { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
7574                              { 0, 1, 2 }, { 2, 1, 0 } };
7575
7576   int success, found;
7577
7578   for (k=0; k<8; k++)
7579     {
7580       success = TRUE;
7581       found = FALSE;
7582
7583       for (i=0;i<3;i++)
7584         {
7585           success = success &amp;&amp; 
7586             GTK_TOGGLE_BUTTON(ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
7587           found = found ||
7588             ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
7589         }
7590       
7591       if (success &amp;&amp; found)
7592         {
7593           gtk_signal_emit (GTK_OBJECT (ttt), 
7594                            tictactoe_signals[TICTACTOE_SIGNAL]);
7595           break;
7596         }
7597     }
7598 }
7599 </verb></tscreen>
7600
7601 <p>
7602
7603 And finally, an example program using our Tictactoe widget:
7604
7605 <tscreen><verb>
7606 #include <gtk/gtk.h>
7607 #include "tictactoe.h"
7608
7609 /* Invoked when a row, column or diagonal is completed */
7610 void
7611 win (GtkWidget *widget, gpointer data)
7612 {
7613   g_print ("Yay!\n");
7614   tictactoe_clear (TICTACTOE (widget));
7615 }
7616
7617 int 
7618 main (int argc, char *argv[])
7619 {
7620   GtkWidget *window;
7621   GtkWidget *ttt;
7622   
7623   gtk_init (&amp;argc, &amp;argv);
7624
7625   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
7626   
7627   gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
7628   
7629   gtk_signal_connect (GTK_OBJECT (window), "destroy",
7630                       GTK_SIGNAL_FUNC (gtk_exit), NULL);
7631   
7632   gtk_container_border_width (GTK_CONTAINER (window), 10);
7633
7634   /* Create a new Tictactoe widget */
7635   ttt = tictactoe_new ();
7636   gtk_container_add (GTK_CONTAINER (window), ttt);
7637   gtk_widget_show (ttt);
7638
7639   /* And attach to its "tictactoe" signal */
7640   gtk_signal_connect (GTK_OBJECT (ttt), "tictactoe",
7641                       GTK_SIGNAL_FUNC (win), NULL);
7642
7643   gtk_widget_show (window);
7644   
7645   gtk_main ();
7646   
7647   return 0;
7648 }
7649
7650 </verb></tscreen>
7651
7652 <!-- ----------------------------------------------------------------- -->
7653 <sect1> Creating a widget from scratch.
7654
7655 <!-- ----------------------------------------------------------------- -->
7656 <sect2> Introduction
7657
7658 <p>
7659
7660 In this section, we'll learn more about how widgets display themselves
7661 on the screen and interact with events. As an example of this, we'll
7662 create an analog dial widget with a pointer that the user can drag to
7663 set the value.
7664
7665 <!-- ----------------------------------------------------------------- -->
7666 <sect2> Displaying a widget on the screen
7667
7668 <p>
7669 There are several steps that are involved in displaying on the screen.
7670 After the widget is created with a call to <tt/WIDGETNAME_new()/,
7671 several more functions are needed:
7672
7673 <itemize>
7674 <item> <tt/WIDGETNAME_realize()/ is responsible for creating an X
7675 window for the widget if it has one.
7676 <item> <tt/WIDGETNAME_map()/ is invoked after the user calls
7677 <tt/gtk_widget_show()/. It is responsible for making sure the widget
7678 is actually drawn on the screen (<em/mapped/). For a container class,
7679 it must also make calls to <tt/map()/> functions of any child widgets.
7680 <item> <tt/WIDGETNAME_draw()/ is invoked when <tt/gtk_widget_draw()/
7681 is called for the widget or one of its ancestors. It makes the actual
7682 calls to the drawing functions to draw the widget on the screen. For
7683 container widgets, this function must make calls to
7684 <tt/gtk_widget_draw()/ for its child widgets.
7685 <item> <tt/WIDGETNAME_expose()/ is a handler for expose events for the
7686 widget. It makes the necessary calls to the drawing functions to draw
7687 the exposed portion on the screen. For container widgets, this
7688 function must generate expose events for its child widgets which don't
7689 have their own windows. (If they have their own windows, then X will
7690 generate the necessary expose events)
7691 </itemize>
7692
7693 <p>
7694 You might notice that the last two functions are quite similar - each
7695 is responsible for drawing the widget on the screen. In fact many
7696 types of widgets don't really care about the difference between the
7697 two. The default <tt/draw()/ function in the widget class simply
7698 generates a synthetic expose event for the redrawn area. However, some
7699 types of widgets can save work by distinguishing between the two
7700 functions. For instance, if a widget has multiple X windows, then
7701 since expose events identify the exposed window, it can redraw only
7702 the affected window, which is not possible for calls to <tt/draw()/.
7703
7704 <p>
7705 Container widgets, even if they don't care about the difference for
7706 themselves, can't simply use the default <tt/draw()/ function because
7707 their child widgets might care about the difference. However,
7708 it would be wasteful to duplicate the drawing code between the two
7709 functions. The convention is that such widgets have a function called
7710 <tt/WIDGETNAME_paint()/ that does the actual work of drawing the
7711 widget, that is then called by the <tt/draw()/ and <tt/expose()/
7712 functions.
7713
7714 <p>
7715 In our example approach, since the dial widget is not a container
7716 widget, and only has a single window, we can take the simplest
7717 approach and use the default <tt/draw()/ function and only implement
7718 an <tt/expose()/ function.
7719
7720 <!-- ----------------------------------------------------------------- -->
7721 <sect2> The origins of the Dial Widget
7722
7723 <p>
7724 Just as all land animals are just variants on the first amphibian that
7725 crawled up out of the mud, Gtk widgets tend to start off as variants
7726 of some other, previously written widget.  Thus, although this section
7727 is entilted ``Creating a Widget from Scratch'', the Dial widget really
7728 began with the source code for the Range widget. This was picked as a
7729 starting point because it would be nice if our Dial had the same
7730 interface as the Scale widgets which are just specialized descendents
7731 of the Range widget. So, though the source code is presented below in
7732 finished form, it should not be implied that it was written, <em>deus
7733 ex machina</em> in this fashion. Also, if you aren't yet familiar with
7734 how scale widgets work from the application writer's point of view, it
7735 would be a good idea to look them over before continuing.
7736
7737 <!-- ----------------------------------------------------------------- -->
7738 <sect2> The Basics
7739
7740 <p>
7741 Quite a bit of our widget should look pretty familiar from the
7742 Tictactoe widget. First, we have a header file:
7743
7744 <tscreen><verb>
7745 /* GTK - The GIMP Toolkit
7746  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
7747  *
7748  * This library is free software; you can redistribute it and/or
7749  * modify it under the terms of the GNU Library General Public
7750  * License as published by the Free Software Foundation; either
7751  * version 2 of the License, or (at your option) any later version.
7752  *
7753  * This library is distributed in the hope that it will be useful,
7754  * but WITHOUT ANY WARRANTY; without even the implied warranty of
7755  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7756  * Library General Public License for more details.
7757  *
7758  * You should have received a copy of the GNU Library General Public
7759  * License along with this library; if not, write to the Free
7760  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
7761  */
7762
7763 #ifndef __GTK_DIAL_H__
7764 #define __GTK_DIAL_H__
7765
7766 #include <gdk/gdk.h>
7767 #include <gtk/gtkadjustment.h>
7768 #include <gtk/gtkwidget.h>
7769
7770
7771 #ifdef __cplusplus
7772 extern "C" {
7773 #endif /* __cplusplus */
7774
7775
7776 #define GTK_DIAL(obj)          GTK_CHECK_CAST (obj, gtk_dial_get_type (), GtkDial)
7777 #define GTK_DIAL_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, gtk_dial_get_type (), GtkDialClass)
7778 #define GTK_IS_DIAL(obj)       GTK_CHECK_TYPE (obj, gtk_dial_get_type ())
7779
7780
7781 typedef struct _GtkDial        GtkDial;
7782 typedef struct _GtkDialClass   GtkDialClass;
7783
7784 struct _GtkDial
7785 {
7786   GtkWidget widget;
7787
7788   /* update policy (GTK_UPDATE_[CONTINUOUS/DELAYED/DISCONTINUOUS]) */
7789   guint policy : 2;
7790
7791   /* Button currently pressed or 0 if none */
7792   guint8 button;
7793
7794   /* Dimensions of dial components */
7795   gint radius;
7796   gint pointer_width;
7797
7798   /* ID of update timer, or 0 if none */
7799   guint32 timer;
7800
7801   /* Current angle */
7802   gfloat angle;
7803
7804   /* Old values from adjustment stored so we know when something changes */
7805   gfloat old_value;
7806   gfloat old_lower;
7807   gfloat old_upper;
7808
7809   /* The adjustment object that stores the data for this dial */
7810   GtkAdjustment *adjustment;
7811 };
7812
7813 struct _GtkDialClass
7814 {
7815   GtkWidgetClass parent_class;
7816 };
7817
7818
7819 GtkWidget*     gtk_dial_new                    (GtkAdjustment *adjustment);
7820 guint          gtk_dial_get_type               (void);
7821 GtkAdjustment* gtk_dial_get_adjustment         (GtkDial      *dial);
7822 void           gtk_dial_set_update_policy      (GtkDial      *dial,
7823                                                 GtkUpdateType  policy);
7824
7825 void           gtk_dial_set_adjustment         (GtkDial      *dial,
7826                                                 GtkAdjustment *adjustment);
7827 #ifdef __cplusplus
7828 }
7829 #endif /* __cplusplus */
7830
7831
7832 #endif /* __GTK_DIAL_H__ */
7833 </verb></tscreen>
7834
7835 Since there is quite a bit more going on in this widget, than the last
7836 one, we have more fields in the data structure, but otherwise things
7837 are pretty similar.
7838
7839 Next, after including header files, and declaring a few constants,
7840 we have some functions to provide information about the widget
7841 and initialize it:
7842
7843 <tscreen><verb>
7844 #include <math.h>
7845 #include <stdio.h>
7846 #include <gtk/gtkmain.h>
7847 #include <gtk/gtksignal.h>
7848
7849 #include "gtkdial.h"
7850
7851 #define SCROLL_DELAY_LENGTH  300
7852 #define DIAL_DEFAULT_SIZE 100
7853
7854 /* Forward declararations */
7855
7856 [ omitted to save space ]
7857
7858 /* Local data */
7859
7860 static GtkWidgetClass *parent_class = NULL;
7861
7862 guint
7863 gtk_dial_get_type ()
7864 {
7865   static guint dial_type = 0;
7866
7867   if (!dial_type)
7868     {
7869       GtkTypeInfo dial_info =
7870       {
7871         "GtkDial",
7872         sizeof (GtkDial),
7873         sizeof (GtkDialClass),
7874         (GtkClassInitFunc) gtk_dial_class_init,
7875         (GtkObjectInitFunc) gtk_dial_init,
7876         (GtkArgSetFunc) NULL,
7877         (GtkArgGetFunc) NULL,
7878       };
7879
7880       dial_type = gtk_type_unique (gtk_widget_get_type (), &amp;dial_info);
7881     }
7882
7883   return dial_type;
7884 }
7885
7886 static void
7887 gtk_dial_class_init (GtkDialClass *class)
7888 {
7889   GtkObjectClass *object_class;
7890   GtkWidgetClass *widget_class;
7891
7892   object_class = (GtkObjectClass*) class;
7893   widget_class = (GtkWidgetClass*) class;
7894
7895   parent_class = gtk_type_class (gtk_widget_get_type ());
7896
7897   object_class->destroy = gtk_dial_destroy;
7898
7899   widget_class->realize = gtk_dial_realize;
7900   widget_class->expose_event = gtk_dial_expose;
7901   widget_class->size_request = gtk_dial_size_request;
7902   widget_class->size_allocate = gtk_dial_size_allocate;
7903   widget_class->button_press_event = gtk_dial_button_press;
7904   widget_class->button_release_event = gtk_dial_button_release;
7905   widget_class->motion_notify_event = gtk_dial_motion_notify;
7906 }
7907
7908 static void
7909 gtk_dial_init (GtkDial *dial)
7910 {
7911   dial->button = 0;
7912   dial->policy = GTK_UPDATE_CONTINUOUS;
7913   dial->timer = 0;
7914   dial->radius = 0;
7915   dial->pointer_width = 0;
7916   dial->angle = 0.0;
7917   dial->old_value = 0.0;
7918   dial->old_lower = 0.0;
7919   dial->old_upper = 0.0;
7920   dial->adjustment = NULL;
7921 }
7922
7923 GtkWidget*
7924 gtk_dial_new (GtkAdjustment *adjustment)
7925 {
7926   GtkDial *dial;
7927
7928   dial = gtk_type_new (gtk_dial_get_type ());
7929
7930   if (!adjustment)
7931     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
7932
7933   gtk_dial_set_adjustment (dial, adjustment);
7934
7935   return GTK_WIDGET (dial);
7936 }
7937
7938 static void
7939 gtk_dial_destroy (GtkObject *object)
7940 {
7941   GtkDial *dial;
7942
7943   g_return_if_fail (object != NULL);
7944   g_return_if_fail (GTK_IS_DIAL (object));
7945
7946   dial = GTK_DIAL (object);
7947
7948   if (dial->adjustment)
7949     gtk_object_unref (GTK_OBJECT (dial->adjustment));
7950
7951   if (GTK_OBJECT_CLASS (parent_class)->destroy)
7952     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
7953 }
7954 </verb></tscreen>
7955
7956 Note that this <tt/init()/ function does less than for the Tictactoe
7957 widget, since this is not a composite widget, and the <tt/new()/
7958 function does more, since it now has an argument. Also, note that when
7959 we store a pointer to the Adjustment object, we increment its
7960 reference count, (and correspondingly decrement when we no longer use
7961 it) so that GTK can keep track of when it can be safely destroyed.
7962
7963 <p>
7964 Also, there are a few function to manipulate the widget's options:
7965
7966 <tscreen><verb>
7967 GtkAdjustment*
7968 gtk_dial_get_adjustment (GtkDial *dial)
7969 {
7970   g_return_val_if_fail (dial != NULL, NULL);
7971   g_return_val_if_fail (GTK_IS_DIAL (dial), NULL);
7972
7973   return dial->adjustment;
7974 }
7975
7976 void
7977 gtk_dial_set_update_policy (GtkDial      *dial,
7978                              GtkUpdateType  policy)
7979 {
7980   g_return_if_fail (dial != NULL);
7981   g_return_if_fail (GTK_IS_DIAL (dial));
7982
7983   dial->policy = policy;
7984 }
7985
7986 void
7987 gtk_dial_set_adjustment (GtkDial      *dial,
7988                           GtkAdjustment *adjustment)
7989 {
7990   g_return_if_fail (dial != NULL);
7991   g_return_if_fail (GTK_IS_DIAL (dial));
7992
7993   if (dial->adjustment)
7994     {
7995       gtk_signal_disconnect_by_data (GTK_OBJECT (dial->adjustment), (gpointer) dial);
7996       gtk_object_unref (GTK_OBJECT (dial->adjustment));
7997     }
7998
7999   dial->adjustment = adjustment;
8000   gtk_object_ref (GTK_OBJECT (dial->adjustment));
8001
8002   gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
8003                       (GtkSignalFunc) gtk_dial_adjustment_changed,
8004                       (gpointer) dial);
8005   gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
8006                       (GtkSignalFunc) gtk_dial_adjustment_value_changed,
8007                       (gpointer) dial);
8008
8009   dial->old_value = adjustment->value;
8010   dial->old_lower = adjustment->lower;
8011   dial->old_upper = adjustment->upper;
8012
8013   gtk_dial_update (dial);
8014 }
8015 </verb></tscreen>
8016
8017 <sect2> <tt/gtk_dial_realize()/
8018
8019 <p>
8020 Now we come to some new types of functions. First, we have a function
8021 that does the work of creating the X window. Notice that a mask is
8022 passed to the function <tt/gdk_window_new()/ which specifies which fields of
8023 the GdkWindowAttr structure actually have data in them (the remaining
8024 fields wll be given default values). Also worth noting is the way the
8025 event mask of the widget is created. We call
8026 <tt/gtk_widget_get_events()/ to retrieve the event mask that the user
8027 has specified for this widget (with <tt/gtk_widget_set_events()/, and
8028 add the events that we are interested in ourselves.
8029
8030 <p>
8031 After creating the window, we set its style and background, and put a
8032 pointer to the widget in the user data field of the GdkWindow. This
8033 last step allows GTK to dispatch events for this window to the correct
8034 widget.
8035
8036 <tscreen><verb>
8037 static void
8038 gtk_dial_realize (GtkWidget *widget)
8039 {
8040   GtkDial *dial;
8041   GdkWindowAttr attributes;
8042   gint attributes_mask;
8043
8044   g_return_if_fail (widget != NULL);
8045   g_return_if_fail (GTK_IS_DIAL (widget));
8046
8047   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
8048   dial = GTK_DIAL (widget);
8049
8050   attributes.x = widget->allocation.x;
8051   attributes.y = widget->allocation.y;
8052   attributes.width = widget->allocation.width;
8053   attributes.height = widget->allocation.height;
8054   attributes.wclass = GDK_INPUT_OUTPUT;
8055   attributes.window_type = GDK_WINDOW_CHILD;
8056   attributes.event_mask = gtk_widget_get_events (widget) | 
8057     GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | 
8058     GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
8059     GDK_POINTER_MOTION_HINT_MASK;
8060   attributes.visual = gtk_widget_get_visual (widget);
8061   attributes.colormap = gtk_widget_get_colormap (widget);
8062
8063   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
8064   widget->window = gdk_window_new (widget->parent->window, &amp;attributes, attributes_mask);
8065
8066   widget->style = gtk_style_attach (widget->style, widget->window);
8067
8068   gdk_window_set_user_data (widget->window, widget);
8069
8070   gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
8071 }
8072 </verb></tscreen>
8073
8074 <sect2> Size negotiation
8075
8076 <p>
8077 Before the first time that the window containing a widget is
8078 displayed, and whenever the layout of the window changes, GTK asks
8079 each child widget for its desired size. This request is handled by the
8080 function, <tt/gtk_dial_size_request()/. Since our widget isn't a
8081 container widget, and has no real constraints on its size, we just
8082 return a reasonable default value.
8083
8084 <tscreen><verb>
8085 static void 
8086 gtk_dial_size_request (GtkWidget      *widget,
8087                        GtkRequisition *requisition)
8088 {
8089   requisition->width = DIAL_DEFAULT_SIZE;
8090   requisition->height = DIAL_DEFAULT_SIZE;
8091 }
8092 </verb></tscreen>
8093
8094 <p>
8095 After all the widgets have requested an ideal size, the layout of the
8096 window is computed and each child widget is notified of its actual
8097 size. Usually, this will at least as large as the requested size, but
8098 if for instance, the user has resized the window, it may occasionally
8099 be smaller than the requested size. The size notification is handled
8100 by the function <tt/gtk_dial_size_allocate()/. Notice that as well as
8101 computing the sizes of some component pieces for future use, this
8102 routine also does the grunt work of moving the widgets X window into
8103 the new position and size.
8104
8105 <tscreen><verb>
8106 static void
8107 gtk_dial_size_allocate (GtkWidget     *widget,
8108                         GtkAllocation *allocation)
8109 {
8110   GtkDial *dial;
8111
8112   g_return_if_fail (widget != NULL);
8113   g_return_if_fail (GTK_IS_DIAL (widget));
8114   g_return_if_fail (allocation != NULL);
8115
8116   widget->allocation = *allocation;
8117   if (GTK_WIDGET_REALIZED (widget))
8118     {
8119       dial = GTK_DIAL (widget);
8120
8121       gdk_window_move_resize (widget->window,
8122                               allocation->x, allocation->y,
8123                               allocation->width, allocation->height);
8124
8125       dial->radius = MAX(allocation->width,allocation->height) * 0.45;
8126       dial->pointer_width = dial->radius / 5;
8127     }
8128 }
8129 </verb></tscreen>.
8130
8131 <!-- ----------------------------------------------------------------- -->
8132 <sect2> <tt/gtk_dial_expose()/
8133
8134 <p>
8135 As mentioned above, all the drawing of this widget is done in the
8136 handler for expose events. There's not much to remark on here except
8137 the use of the function <tt/gtk_draw_polygon/ to draw the pointer with
8138 three dimensional shading according to the colors stored in the
8139 widget's style.
8140
8141 <tscreen><verb>
8142 static gint
8143 gtk_dial_expose (GtkWidget      *widget,
8144                  GdkEventExpose *event)
8145 {
8146   GtkDial *dial;
8147   GdkPoint points[3];
8148   gdouble s,c;
8149   gdouble theta;
8150   gint xc, yc;
8151   gint tick_length;
8152   gint i;
8153
8154   g_return_val_if_fail (widget != NULL, FALSE);
8155   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
8156   g_return_val_if_fail (event != NULL, FALSE);
8157
8158   if (event->count > 0)
8159     return FALSE;
8160   
8161   dial = GTK_DIAL (widget);
8162
8163   gdk_window_clear_area (widget->window,
8164                          0, 0,
8165                          widget->allocation.width,
8166                          widget->allocation.height);
8167
8168   xc = widget->allocation.width/2;
8169   yc = widget->allocation.height/2;
8170
8171   /* Draw ticks */
8172
8173   for (i=0; i<25; i++)
8174     {
8175       theta = (i*M_PI/18. - M_PI/6.);
8176       s = sin(theta);
8177       c = cos(theta);
8178
8179       tick_length = (i%6 == 0) ? dial->pointer_width : dial->pointer_width/2;
8180       
8181       gdk_draw_line (widget->window,
8182                      widget->style->fg_gc[widget->state],
8183                      xc + c*(dial->radius - tick_length),
8184                      yc - s*(dial->radius - tick_length),
8185                      xc + c*dial->radius,
8186                      yc - s*dial->radius);
8187     }
8188
8189   /* Draw pointer */
8190
8191   s = sin(dial->angle);
8192   c = cos(dial->angle);
8193
8194
8195   points[0].x = xc + s*dial->pointer_width/2;
8196   points[0].y = yc + c*dial->pointer_width/2;
8197   points[1].x = xc + c*dial->radius;
8198   points[1].y = yc - s*dial->radius;
8199   points[2].x = xc - s*dial->pointer_width/2;
8200   points[2].y = yc - c*dial->pointer_width/2;
8201
8202   gtk_draw_polygon (widget->style,
8203                     widget->window,
8204                     GTK_STATE_NORMAL,
8205                     GTK_SHADOW_OUT,
8206                     points, 3,
8207                     TRUE);
8208   
8209   return FALSE;
8210 }
8211 </verb></tscreen>
8212
8213 <!-- ----------------------------------------------------------------- -->
8214 <sect2> Event handling
8215
8216 <p>
8217
8218 The rest of the widget's code handles various types of events, and
8219 isn't too different from what would be found in many GTK
8220 applications. Two types of events can occur - either the user can
8221 click on the widget with the mouse and drag to move the pointer, or
8222 the value of the Adjustment object can change due to some external
8223 circumstance. 
8224
8225 <p>
8226 When the user clicks on the widget, we check to see if the click was
8227 appropriately near the pointer, and if so, store then button that the
8228 user clicked with in the <tt/button/ field of the widget
8229 structure, and grab all mouse events with a call to
8230 <tt/gtk_grab_add()/. Subsequent motion of the mouse causes the
8231 value of the control to be recomputed (by the function
8232 <tt/gtk_dial_update_mouse/). Depending on the policy that has been
8233 set, "value_changed" events are either generated instantly
8234 (<tt/GTK_UPDATE_CONTINUOUS/), after a delay in a timer added with
8235 <tt/gtk_timeout_add()/ (<tt/GTK_UPDATE_DELAYED/), or only when the
8236 button is released (<tt/GTK_UPDATE_DISCONTINUOUS/).
8237
8238 <tscreen><verb>
8239 static gint
8240 gtk_dial_button_press (GtkWidget      *widget,
8241                        GdkEventButton *event)
8242 {
8243   GtkDial *dial;
8244   gint dx, dy;
8245   double s, c;
8246   double d_parallel;
8247   double d_perpendicular;
8248
8249   g_return_val_if_fail (widget != NULL, FALSE);
8250   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
8251   g_return_val_if_fail (event != NULL, FALSE);
8252
8253   dial = GTK_DIAL (widget);
8254
8255   /* Determine if button press was within pointer region - we 
8256      do this by computing the parallel and perpendicular distance of
8257      the point where the mouse was pressed from the line passing through
8258      the pointer */
8259   
8260   dx = event->x - widget->allocation.width / 2;
8261   dy = widget->allocation.height / 2 - event->y;
8262   
8263   s = sin(dial->angle);
8264   c = cos(dial->angle);
8265   
8266   d_parallel = s*dy + c*dx;
8267   d_perpendicular = fabs(s*dx - c*dy);
8268   
8269   if (!dial->button &&
8270       (d_perpendicular < dial->pointer_width/2) &&
8271       (d_parallel > - dial->pointer_width))
8272     {
8273       gtk_grab_add (widget);
8274
8275       dial->button = event->button;
8276
8277       gtk_dial_update_mouse (dial, event->x, event->y);
8278     }
8279
8280   return FALSE;
8281 }
8282
8283 static gint
8284 gtk_dial_button_release (GtkWidget      *widget,
8285                           GdkEventButton *event)
8286 {
8287   GtkDial *dial;
8288
8289   g_return_val_if_fail (widget != NULL, FALSE);
8290   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
8291   g_return_val_if_fail (event != NULL, FALSE);
8292
8293   dial = GTK_DIAL (widget);
8294
8295   if (dial->button == event->button)
8296     {
8297       gtk_grab_remove (widget);
8298
8299       dial->button = 0;
8300
8301       if (dial->policy == GTK_UPDATE_DELAYED)
8302         gtk_timeout_remove (dial->timer);
8303       
8304       if ((dial->policy != GTK_UPDATE_CONTINUOUS) &&
8305           (dial->old_value != dial->adjustment->value))
8306         gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
8307     }
8308
8309   return FALSE;
8310 }
8311
8312 static gint
8313 gtk_dial_motion_notify (GtkWidget      *widget,
8314                          GdkEventMotion *event)
8315 {
8316   GtkDial *dial;
8317   GdkModifierType mods;
8318   gint x, y, mask;
8319
8320   g_return_val_if_fail (widget != NULL, FALSE);
8321   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
8322   g_return_val_if_fail (event != NULL, FALSE);
8323
8324   dial = GTK_DIAL (widget);
8325
8326   if (dial->button != 0)
8327     {
8328       x = event->x;
8329       y = event->y;
8330
8331       if (event->is_hint || (event->window != widget->window))
8332         gdk_window_get_pointer (widget->window, &amp;x, &amp;y, &amp;mods);
8333
8334       switch (dial->button)
8335         {
8336         case 1:
8337           mask = GDK_BUTTON1_MASK;
8338           break;
8339         case 2:
8340           mask = GDK_BUTTON2_MASK;
8341           break;
8342         case 3:
8343           mask = GDK_BUTTON3_MASK;
8344           break;
8345         default:
8346           mask = 0;
8347           break;
8348         }
8349
8350       if (mods & mask)
8351         gtk_dial_update_mouse (dial, x,y);
8352     }
8353
8354   return FALSE;
8355 }
8356
8357 static gint
8358 gtk_dial_timer (GtkDial *dial)
8359 {
8360   g_return_val_if_fail (dial != NULL, FALSE);
8361   g_return_val_if_fail (GTK_IS_DIAL (dial), FALSE);
8362
8363   if (dial->policy == GTK_UPDATE_DELAYED)
8364     gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
8365
8366   return FALSE;
8367 }
8368
8369 static void
8370 gtk_dial_update_mouse (GtkDial *dial, gint x, gint y)
8371 {
8372   gint xc, yc;
8373   gfloat old_value;
8374
8375   g_return_if_fail (dial != NULL);
8376   g_return_if_fail (GTK_IS_DIAL (dial));
8377
8378   xc = GTK_WIDGET(dial)->allocation.width / 2;
8379   yc = GTK_WIDGET(dial)->allocation.height / 2;
8380
8381   old_value = dial->adjustment->value;
8382   dial->angle = atan2(yc-y, x-xc);
8383
8384   if (dial->angle < -M_PI/2.)
8385     dial->angle += 2*M_PI;
8386
8387   if (dial->angle < -M_PI/6)
8388     dial->angle = -M_PI/6;
8389
8390   if (dial->angle > 7.*M_PI/6.)
8391     dial->angle = 7.*M_PI/6.;
8392
8393   dial->adjustment->value = dial->adjustment->lower + (7.*M_PI/6 - dial->angle) *
8394     (dial->adjustment->upper - dial->adjustment->lower) / (4.*M_PI/3.);
8395
8396   if (dial->adjustment->value != old_value)
8397     {
8398       if (dial->policy == GTK_UPDATE_CONTINUOUS)
8399         {
8400           gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
8401         }
8402       else
8403         {
8404           gtk_widget_draw (GTK_WIDGET(dial), NULL);
8405
8406           if (dial->policy == GTK_UPDATE_DELAYED)
8407             {
8408               if (dial->timer)
8409                 gtk_timeout_remove (dial->timer);
8410
8411               dial->timer = gtk_timeout_add (SCROLL_DELAY_LENGTH,
8412                                              (GtkFunction) gtk_dial_timer,
8413                                              (gpointer) dial);
8414             }
8415         }
8416     }
8417 }
8418 </verb></tscreen>
8419
8420 <p>
8421 Changes to the Adjustment by external means are communicated to our
8422 widget by the ``changed'' and ``value_changed'' signals. The handlers
8423 for these functions call <tt/gtk_dial_update()/ to validate the
8424 arguments, compute the new pointer angle, and redraw the widget (by
8425 calling <tt/gtk_widget_draw()/).
8426
8427 <tscreen><verb>
8428 static void
8429 gtk_dial_update (GtkDial *dial)
8430 {
8431   gfloat new_value;
8432   
8433   g_return_if_fail (dial != NULL);
8434   g_return_if_fail (GTK_IS_DIAL (dial));
8435
8436   new_value = dial->adjustment->value;
8437   
8438   if (new_value < dial->adjustment->lower)
8439     new_value = dial->adjustment->lower;
8440
8441   if (new_value > dial->adjustment->upper)
8442     new_value = dial->adjustment->upper;
8443
8444   if (new_value != dial->adjustment->value)
8445     {
8446       dial->adjustment->value = new_value;
8447       gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
8448     }
8449
8450   dial->angle = 7.*M_PI/6. - (new_value - dial->adjustment->lower) * 4.*M_PI/3. /
8451     (dial->adjustment->upper - dial->adjustment->lower);
8452
8453   gtk_widget_draw (GTK_WIDGET(dial), NULL);
8454 }
8455
8456 static void
8457 gtk_dial_adjustment_changed (GtkAdjustment *adjustment,
8458                               gpointer       data)
8459 {
8460   GtkDial *dial;
8461
8462   g_return_if_fail (adjustment != NULL);
8463   g_return_if_fail (data != NULL);
8464
8465   dial = GTK_DIAL (data);
8466
8467   if ((dial->old_value != adjustment->value) ||
8468       (dial->old_lower != adjustment->lower) ||
8469       (dial->old_upper != adjustment->upper))
8470     {
8471       gtk_dial_update (dial);
8472
8473       dial->old_value = adjustment->value;
8474       dial->old_lower = adjustment->lower;
8475       dial->old_upper = adjustment->upper;
8476     }
8477 }
8478
8479 static void
8480 gtk_dial_adjustment_value_changed (GtkAdjustment *adjustment,
8481                                     gpointer       data)
8482 {
8483   GtkDial *dial;
8484
8485   g_return_if_fail (adjustment != NULL);
8486   g_return_if_fail (data != NULL);
8487
8488   dial = GTK_DIAL (data);
8489
8490   if (dial->old_value != adjustment->value)
8491     {
8492       gtk_dial_update (dial);
8493
8494       dial->old_value = adjustment->value;
8495     }
8496 }
8497 </verb></tscreen>
8498
8499 <!-- ----------------------------------------------------------------- -->
8500 <sect2> Possible Enhancements
8501 <p>
8502
8503 The Dial widget as we've described it so far runs about 670 lines of
8504 code. Although that might sound like a fair bit, we've really
8505 accomplished quite a bit with that much code, especially since much of
8506 that length is headers and boilerplate. However, there are quite a few
8507 more enhancements that could be made to this widget:
8508
8509 <itemize>
8510 <item> If you try this widget out, you'll find that there is some
8511 flashing as the pointer is dragged around. This is because the entire
8512 widget is erased every time the pointer is moved before being
8513 redrawn. Often, the best way to handle this problem is to draw to an
8514 offscreen pixmap, then copy the final results onto the screen in one
8515 step. (The ProgressBar widget draws itself in this fashion.)
8516
8517 <item> The user should be able to use the up and down arrow keys to
8518 increase and decrease the value.
8519
8520 <item> It would be nice if the widget had buttons to increase and
8521 decrease the value in small or large steps. Although it would be
8522 possible to use embedded Button widgets for this, we would also like
8523 the buttons to auto-repeat when held down, as the arrows on a
8524 scrollbar do. Most of the code to implement this type of behavior can
8525 be found in the GtkRange widget.
8526
8527 <item> The Dial widget could be made into a container widget with a
8528 single child widget positioned at the bottom between the buttons
8529 mentioned above. The user could then add their choice of a label or
8530 entry widget to display the current value of the dial.
8531
8532 </itemize>
8533
8534 <!-- ----------------------------------------------------------------- -->
8535 <sect1> Learning More
8536
8537 <p>
8538 Only a small part of the many details involved in creating widgets
8539 could be described above. If you want to write your own widgets, the
8540 best source of examples is the GTK source itself. Ask yourself some
8541 questions about the widget you want to write: is it a Container
8542 widget? does it have its own window? is it a modification of an
8543 existing widget? Then find a similar widget, and start making changes.
8544 Good luck!
8545
8546 <!-- ***************************************************************** -->
8547 <sect>Scribble, A Simple Example Drawing Program
8548 <!-- ***************************************************************** -->
8549
8550 <!-- ----------------------------------------------------------------- -->
8551 <sect1> Overview
8552
8553 <p>
8554 In this section, we will build a simple drawing program. In the
8555 process, we will examine how to handle mouse events, how to draw in a
8556 window, and how to do drawing better by using a backing pixmap. After
8557 creating the simple drawing program, we will extend it by adding
8558 support for XInput devices, such as drawing tablets. GTK provides
8559 support routines which makes getting extended information, such as
8560 pressure and tilt, from such devices quite easy.
8561
8562 <!-- ----------------------------------------------------------------- -->
8563 <sect1> Event Handling
8564
8565 <p>
8566 The GTK signals we have already discussed are for high-level actions,
8567 such as a menu item being selected. However, sometimes it is useful to
8568 learn about lower-level occurrences, such as the mouse being moved, or
8569 a key being pressed. There are also GTK signals corresponding to these
8570 low-level <em>events</em>. The handlers for these signals have an
8571 extra parameter which is a pointer to a structure containing
8572 information about the event. For instance, motion events handlers are
8573 passed a pointer to a GdkEventMotion structure which looks (in part)
8574 like:
8575
8576 <tscreen><verb>
8577 struct _GdkEventMotion
8578 {
8579   GdkEventType type;
8580   GdkWindow *window;
8581   guint32 time;
8582   gdouble x;
8583   gdouble y;
8584   ...
8585   guint state;
8586   ...
8587 };
8588 </verb></tscreen>
8589
8590 <tt/type/ will be set to the event type, in this case
8591 <tt/GDK_MOTION_NOTIFY/, window is the window in which the event
8592 occured. <tt/x/ and <tt/y/ give the coordinates of the event,
8593 and <tt/state/ specifies the modifier state when the event
8594 occurred (that is, it specifies which modifier keys and mouse buttons
8595 were pressed.) It is the bitwise OR of some of the following:
8596
8597 <tscreen><verb>
8598 GDK_SHIFT_MASK  
8599 GDK_LOCK_MASK   
8600 GDK_CONTROL_MASK
8601 GDK_MOD1_MASK   
8602 GDK_MOD2_MASK   
8603 GDK_MOD3_MASK   
8604 GDK_MOD4_MASK   
8605 GDK_MOD5_MASK   
8606 GDK_BUTTON1_MASK
8607 GDK_BUTTON2_MASK
8608 GDK_BUTTON3_MASK
8609 GDK_BUTTON4_MASK
8610 GDK_BUTTON5_MASK
8611 </verb></tscreen>
8612
8613 <p>
8614 As for other signals, to determine what happens when an event occurs
8615 we call <tt>gtk_signal_connect()</tt>. But we also need let GTK
8616 know which events we want to be notified about. To do this, we call
8617 the function:
8618
8619 <tscreen><verb>
8620 void gtk_widget_set_events (GtkWidget *widget,
8621                             gint      events);
8622 </verb></tscreen>
8623
8624 The second field specifies the events we are interested in. It
8625 is the bitwise OR of constants that specify different types
8626 of events. For future reference the event types are:
8627
8628 <tscreen><verb>
8629 GDK_EXPOSURE_MASK
8630 GDK_POINTER_MOTION_MASK
8631 GDK_POINTER_MOTION_HINT_MASK
8632 GDK_BUTTON_MOTION_MASK     
8633 GDK_BUTTON1_MOTION_MASK    
8634 GDK_BUTTON2_MOTION_MASK    
8635 GDK_BUTTON3_MOTION_MASK    
8636 GDK_BUTTON_PRESS_MASK      
8637 GDK_BUTTON_RELEASE_MASK    
8638 GDK_KEY_PRESS_MASK         
8639 GDK_KEY_RELEASE_MASK       
8640 GDK_ENTER_NOTIFY_MASK      
8641 GDK_LEAVE_NOTIFY_MASK      
8642 GDK_FOCUS_CHANGE_MASK      
8643 GDK_STRUCTURE_MASK         
8644 GDK_PROPERTY_CHANGE_MASK   
8645 GDK_PROXIMITY_IN_MASK      
8646 GDK_PROXIMITY_OUT_MASK     
8647 </verb></tscreen>
8648
8649 There are a few subtle points that have to be observed when calling
8650 <tt/gtk_widget_set_events()/. First, it must be called before the X window
8651 for a GTK widget is created. In practical terms, this means you
8652 should call it immediately after creating the widget. Second, the
8653 widget must have an associated X window. For efficiency, many widget
8654 types do not have their own window, but draw in their parent's window.
8655 These widgets are:
8656
8657 <tscreen><verb>
8658 GtkAlignment
8659 GtkArrow
8660 GtkBin
8661 GtkBox
8662 GtkImage
8663 GtkItem
8664 GtkLabel
8665 GtkPixmap
8666 GtkScrolledWindow
8667 GtkSeparator
8668 GtkTable
8669 GtkAspectFrame
8670 GtkFrame
8671 GtkVBox
8672 GtkHBox
8673 GtkVSeparator
8674 GtkHSeparator
8675 </verb></tscreen>
8676
8677 To capture events for these widgets, you need to use an EventBox 
8678 widget. See the section on   
8679 <ref id="sec_The_EventBox_Widget" name="The EventBox Widget"> for
8680 details.
8681
8682 <p>
8683 For our drawing program, we want to know when the mouse button is
8684 pressed and when the mouse is moved, so we specify
8685 <tt/GDK_POINTER_MOTION_MASK/ and <tt/GDK_BUTTON_PRESS_MASK/. We also
8686 want to know when we need to redraw our window, so we specify
8687 <tt/GDK_EXPOSURE_MASK/. Although we want to be notified via a
8688 Configure event when our window size changes, we don't have to specify
8689 the corresponding <tt/GDK_STRUCTURE_MASK/ flag, because it is
8690 automatically specified for all windows.
8691
8692 <p>
8693 It turns out, however, that there is a problem with just specifying
8694 <tt/GDK_POINTER_MOTION_MASK/. This will cause the server to add a new
8695 motion event to the event queue every time the user moves the mouse.
8696 Imagine that it takes us 0.1 seconds to handle a motion event, but the
8697 X server queues a new motion event every 0.05 seconds. We will soon
8698 get way behind the users drawing. If the user draws for 5 seconds,
8699 it will take us another 5 seconds to catch up after they release 
8700 the mouse button! What we would like is to only get one motion
8701 event for each event we process. The way to do this is to 
8702 specify <tt/GDK_POINTER_MOTION_HINT_MASK/. 
8703
8704 <p>
8705 When we specify <tt/GDK_POINTER_MOTION_HINT_MASK/, the server sends
8706 us a motion event the first time the pointer moves after entering
8707 our window, or after a button press or release event. Subsequent 
8708 motion events will be suppressed until we explicitely ask for
8709 the position of the pointer using the function:
8710
8711 <tscreen><verb>
8712 GdkWindow*    gdk_window_get_pointer     (GdkWindow       *window,
8713                                           gint            *x,
8714                                           gint            *y,
8715                                           GdkModifierType *mask);
8716 </verb></tscreen>
8717
8718 (There is another function, <tt>gtk_widget_get_pointer()</tt> which
8719 has a simpler interface, but turns out not to be very useful, since
8720 it only retrieves the position of the mouse, not whether the buttons
8721 are pressed.)
8722
8723 <p>
8724 The code to set the events for our window then looks like:
8725
8726 <tscreen><verb>
8727   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
8728                       (GtkSignalFunc) expose_event, NULL);
8729   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
8730                       (GtkSignalFunc) configure_event, NULL);
8731   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
8732                       (GtkSignalFunc) motion_notify_event, NULL);
8733   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
8734                       (GtkSignalFunc) button_press_event, NULL);
8735
8736   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
8737                          | GDK_LEAVE_NOTIFY_MASK
8738                          | GDK_BUTTON_PRESS_MASK
8739                          | GDK_POINTER_MOTION_MASK
8740                          | GDK_POINTER_MOTION_HINT_MASK);
8741 </verb></tscreen>
8742
8743 We'll save the "expose_event" and "configure_event" handlers for
8744 later. The "motion_notify_event" and "button_press_event" handlers
8745 pretty simple:
8746
8747 <tscreen><verb>
8748 static gint
8749 button_press_event (GtkWidget *widget, GdkEventButton *event)
8750 {
8751   if (event->button == 1 &amp;&amp; pixmap != NULL)
8752       draw_brush (widget, event->x, event->y);
8753
8754   return TRUE;
8755 }
8756
8757 static gint
8758 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
8759 {
8760   int x, y;
8761   GdkModifierType state;
8762
8763   if (event->is_hint)
8764     gdk_window_get_pointer (event->window, &amp;x, &amp;y, &amp;state);
8765   else
8766     {
8767       x = event->x;
8768       y = event->y;
8769       state = event->state;
8770     }
8771     
8772   if (state &amp; GDK_BUTTON1_MASK &amp;&amp; pixmap != NULL)
8773     draw_brush (widget, x, y);
8774   
8775   return TRUE;
8776 }
8777 </verb></tscreen>
8778
8779 <!-- ----------------------------------------------------------------- -->
8780 <sect1> The DrawingArea Widget, And Drawing
8781
8782 <p>
8783 We know turn to the process of drawing on the screen. The 
8784 widget we use for this is the DrawingArea widget. A drawing area
8785 widget is essentially an X window and nothing more. It is a blank
8786 canvas in which we can draw whatever we like. A drawing area
8787 is created using the call:
8788
8789 <tscreen><verb>
8790 GtkWidget* gtk_drawing_area_new        (void);
8791 </verb></tscreen>
8792
8793 A default size for the widget can be specified by calling:
8794
8795 <tscreen><verb>
8796 void       gtk_drawing_area_size       (GtkDrawingArea      *darea,
8797                                         gint                 width,
8798                                         gint                 height);
8799 </verb></tscreen>
8800
8801 This default size can be overriden, as is true for all widgets,
8802 by calling <tt>gtk_widget_set_usize()</tt>, and that, in turn, can
8803 be overridden if the user manually resizes the the window containing
8804 the drawing area.
8805
8806 <p>
8807 It should be noted that when we create a DrawingArea widget, we are,
8808 <em>completely</em> responsible for drawing the contents. If our
8809 window is obscured then uncovered, we get an exposure event and must
8810 redraw what was previously hidden.
8811
8812 <p>
8813 Having to remember everything that was drawn on the screen so we
8814 can properly redraw it can, to say the least, be a nuisance. In
8815 addition, it can be visually distracting if portions of the
8816 window are cleared, then redrawn step by step. The solution to
8817 this problem is to use an offscreen <em>backing pixmap</em>.
8818 Instead of drawing directly to the screen, we draw to an image
8819 stored in server memory but not displayed, then when the image
8820 changes or new portions of the image are displayed, we copy the
8821 relevant portions onto the screen.
8822
8823 <p>
8824 To create an offscreen pixmap, we call the function:
8825
8826 <tscreen><verb>
8827 GdkPixmap* gdk_pixmap_new               (GdkWindow  *window,
8828                                          gint        width,
8829                                          gint        height,
8830                                          gint        depth);
8831 </verb></tscreen>
8832
8833 The <tt>window</tt> parameter specifies a GDK window that this pixmap
8834 takes some of its properties from. <tt>width</tt> and <tt>height</tt>
8835 specify the size of the pixmap. <tt>depth</tt> specifies the <em>color
8836 depth</em>, that is the number of bits per pixel, for the new window.
8837 If the depth is specified as <tt>-1</tt>, it will match the depth
8838 of <tt>window</tt>.
8839
8840 <p>
8841 We create the pixmap in our "configure_event" handler. This event
8842 is generated whenever the window changes size, including when it
8843 is originally created.
8844
8845 <tscreen><verb>
8846 /* Backing pixmap for drawing area */
8847 static GdkPixmap *pixmap = NULL;
8848
8849 /* Create a new backing pixmap of the appropriate size */
8850 static gint
8851 configure_event (GtkWidget *widget, GdkEventConfigure *event)
8852 {
8853   if (pixmap)
8854     {
8855       gdk_pixmap_destroy(pixmap);
8856     }
8857   pixmap = gdk_pixmap_new(widget->window,
8858                           widget->allocation.width,
8859                           widget->allocation.height,
8860                           -1);
8861   gdk_draw_rectangle (pixmap,
8862                       widget->style->white_gc,
8863                       TRUE,
8864                       0, 0,
8865                       widget->allocation.width,
8866                       widget->allocation.height);
8867
8868   return TRUE;
8869 }
8870 </verb></tscreen>
8871
8872 The call to <tt>gdk_draw_rectangle()</tt> clears the pixmap
8873 initially to white. We'll say more about that in a moment.
8874
8875 <p>
8876 Our exposure event handler then simply copies the relevant portion
8877 of the pixmap onto the screen (we determine the area we need
8878 to redraw by using the event->area field of the exposure event):
8879
8880 <tscreen><verb>
8881 /* Refill the screen from the backing pixmap */
8882 static gint
8883 expose_event (GtkWidget *widget, GdkEventExpose *event)
8884 {
8885   gdk_draw_pixmap(widget->window,
8886                   widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
8887                   pixmap,
8888                   event->area.x, event->area.y,
8889                   event->area.x, event->area.y,
8890                   event->area.width, event->area.height);
8891
8892   return FALSE;
8893 }
8894 </verb></tscreen>
8895
8896 We've now seen how to keep the screen up to date with our pixmap, but
8897 how do we actually draw interesting stuff on our pixmap?  There are a
8898 large number of calls in GTK's GDK library for drawing on
8899 <em>drawables</em>. A drawable is simply something that can be drawn
8900 upon. It can be a window, a pixmap, or a bitmap (a black and white
8901 image).  We've already seen two such calls above,
8902 <tt>gdk_draw_rectangle()</tt> and <tt>gdk_draw_pixmap()</tt>. The
8903 complete list is:
8904
8905 <tscreen><verb>
8906 gdk_draw_line ()
8907 gdk_draw_rectangle ()
8908 gdk_draw_arc ()
8909 gdk_draw_polygon ()
8910 gdk_draw_string ()
8911 gdk_draw_text ()
8912 gdk_draw_pixmap ()
8913 gdk_draw_bitmap ()
8914 gdk_draw_image ()
8915 gdk_draw_points ()
8916 gdk_draw_segments ()
8917 </verb></tscreen>
8918
8919 See the reference documentation or the header file
8920 <tt>&lt;gdk/gdk.h&gt;</tt> for further details on these functions.
8921 These functions all share the same first two arguments. The first
8922 argument is the drawable to draw upon, the second argument is a
8923 <em>graphics context</em> (GC). 
8924
8925 <p>
8926 A graphics context encapsulates information about things such as
8927 foreground and background color and line width. GDK has a full set of
8928 functions for creating and modifying graphics contexts, but to keep
8929 things simple we'll just use predefined graphics contexts. Each widget
8930 has an associated style. (Which can be modified in a gtkrc file, see
8931 the section GTK's rc file.) This, among other things, stores a number
8932 of graphics contexts. Some examples of accessing these graphics
8933 contexts are:
8934
8935 <tscreen><verb>
8936 widget->style->white_gc
8937 widget->style->black_gc
8938 widget->style->fg_gc[GTK_STATE_NORMAL]
8939 widget->style->bg_gc[GTK_WIDGET_STATE(widget)]
8940 </verb></tscreen>
8941
8942 The fields <tt>fg_gc</tt>, <tt>bg_gc</tt>, <tt>dark_gc</tt>, and
8943 <tt>light_gc</tt> are indexed by a parameter of type
8944 <tt>GtkStateType</tt> which can take on the values:
8945
8946 <tscreen><verb>
8947 GTK_STATE_NORMAL,
8948 GTK_STATE_ACTIVE,
8949 GTK_STATE_PRELIGHT,
8950 GTK_STATE_SELECTED,
8951 GTK_STATE_INSENSITIVE
8952 </verb></tscreen>
8953
8954 For instance, the for <tt/GTK_STATE_SELECTED/ the default foreground
8955 color is white and the default background color, dark blue.
8956
8957 <p>
8958 Our function <tt>draw_brush()</tt>, which does the actual drawing
8959 on the screen, is then:
8960
8961 <tscreen><verb>
8962 /* Draw a rectangle on the screen */
8963 static void
8964 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
8965 {
8966   GdkRectangle update_rect;
8967
8968   update_rect.x = x - 5;
8969   update_rect.y = y - 5;
8970   update_rect.width = 10;
8971   update_rect.height = 10;
8972   gdk_draw_rectangle (pixmap,
8973                       widget->style->black_gc,
8974                       TRUE,
8975                       update_rect.x, update_rect.y,
8976                       update_rect.width, update_rect.height);
8977   gtk_widget_draw (widget, &amp;update_rect);
8978 }
8979 </verb></tscreen>
8980
8981 After we draw the rectangle representing the brush onto the pixmap,
8982 we call the function:
8983
8984 <tscreen><verb>
8985 void       gtk_widget_draw                (GtkWidget           *widget,
8986                                            GdkRectangle        *area);
8987 </verb></tscreen>
8988
8989 which notifies X that the area given by the <tt>area</tt> parameter
8990 needs to be updated. X will eventually generate an expose event
8991 (possibly combining the areas passed in several calls to
8992 <tt>gtk_widget_draw()</tt>) which will cause our expose event handler
8993 to copy the relevant portions to the screen.
8994
8995 <p>
8996 We have now covered the entire drawing program except for a few
8997 mundane details like creating the main window. The complete
8998 source code is available from the location from which you got
8999 this tutorial, or from:
9000
9001 <htmlurl url="http://www.msc.cornell.edu/~otaylor/gtk-gimp/tutorial"
9002 name="http://www.msc.cornell.edu/~otaylor/gtk-gimp/tutorial">
9003
9004
9005 <!-- ----------------------------------------------------------------- -->
9006 <sect1> Adding XInput support
9007
9008 <p>
9009
9010 It is now possible to buy quite inexpensive input devices such 
9011 as drawing tablets, which allow drawing with a much greater
9012 ease of artistic expression than does a mouse. The simplest way
9013 to use such devices is simply as a replacement for the mouse,
9014 but that misses out many of the advantages of these devices,
9015 such as:
9016
9017 <itemize>
9018 <item> Pressure sensitivity
9019 <item> Tilt reporting
9020 <item> Sub-pixel positioning
9021 <item> Multiple inputs (for example, a stylus with a point and eraser)
9022 </itemize>
9023
9024 For information about the XInput extension, see the <htmlurl
9025 url="http://www.msc.cornell.edu/~otaylor/xinput/XInput-HOWTO.html"
9026 name="XInput-HOWTO">.
9027
9028 <p>
9029 If we examine the full definition of, for example, the GdkEventMotion
9030 structure, we see that it has fields to support extended device
9031 information.
9032
9033 <tscreen><verb>
9034 struct _GdkEventMotion
9035 {
9036   GdkEventType type;
9037   GdkWindow *window;
9038   guint32 time;
9039   gdouble x;
9040   gdouble y;
9041   gdouble pressure;
9042   gdouble xtilt;
9043   gdouble ytilt;
9044   guint state;
9045   gint16 is_hint;
9046   GdkInputSource source;
9047   guint32 deviceid;
9048 };
9049 </verb></tscreen>
9050
9051 <tt/pressure/ gives the pressure as a floating point number between
9052 0 and 1. <tt/xtilt/ and <tt/ytilt/ can take on values between 
9053 -1 and 1, corresponding to the degree of tilt in each direction.
9054 <tt/source/ and <tt/deviceid/ specify the device for which the
9055 event occurred in two different ways. <tt/source/ gives some simple
9056 information about the type of device. It can take the enumeration
9057 values.
9058
9059 <tscreen><verb>
9060 GDK_SOURCE_MOUSE
9061 GDK_SOURCE_PEN
9062 GDK_SOURCE_ERASER
9063 GDK_SOURCE_CURSOR
9064 </verb></tscreen>
9065
9066 <tt/deviceid/ specifies a unique numeric ID for the device. This can
9067 be used to find out further information about the device using the
9068 <tt/gdk_input_list_devices()/ call (see below). The special value
9069 <tt/GDK_CORE_POINTER/ is used for the core pointer device. (Usually
9070 the mouse.)
9071
9072 <sect2> Enabling extended device information
9073
9074 <p>
9075 To let GTK know about our interest in the extended device information,
9076 we merely have to add a single line to our program:
9077
9078 <tscreen><verb>
9079 gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_CURSOR);
9080 </verb></tscreen>
9081
9082 By giving the value <tt/GDK_EXTENSION_EVENTS_CURSOR/ we say that
9083 we are interested in extension events, but only if we don't have
9084 to draw our own cursor. See the section <ref
9085 id="sec_Further_Sophistications" name="Further Sophistications"> below
9086 for more information about drawing the cursor. We could also 
9087 give the values <tt/GDK_EXTENSION_EVENTS_ALL/ if we were willing 
9088 to draw our own cursor, or <tt/GDK_EXTENSION_EVENTS_NONE/ to revert
9089 back to the default condition.
9090
9091 <p>
9092 This is not completely the end of the story however. By default,
9093 no extension devices are enabled. We need a mechanism to allow
9094 users to enable and configure their extension devices. GTK provides
9095 the InputDialog widget to automate this process. The following
9096 procedure manages an InputDialog widget. It creates the dialog if
9097 it isn't present, and raises it to the top otherwise.
9098
9099 <tscreen><verb>
9100 void
9101 input_dialog_destroy (GtkWidget *w, gpointer data)
9102 {
9103   *((GtkWidget **)data) = NULL;
9104 }
9105
9106 void
9107 create_input_dialog ()
9108 {
9109   static GtkWidget *inputd = NULL;
9110
9111   if (!inputd)
9112     {
9113       inputd = gtk_input_dialog_new();
9114
9115       gtk_signal_connect (GTK_OBJECT(inputd), "destroy",
9116                           (GtkSignalFunc)input_dialog_destroy, &amp;inputd);
9117       gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(inputd)->close_button),
9118                                  "clicked",
9119                                  (GtkSignalFunc)gtk_widget_hide,
9120                                  GTK_OBJECT(inputd));
9121       gtk_widget_hide ( GTK_INPUT_DIALOG(inputd)->save_button);
9122
9123       gtk_widget_show (inputd);
9124     }
9125   else
9126     {
9127       if (!GTK_WIDGET_MAPPED(inputd))
9128         gtk_widget_show(inputd);
9129       else
9130         gdk_window_raise(inputd->window);
9131     }
9132 }
9133 </verb></tscreen>
9134
9135 (You might want to take note of the way we handle this dialog.  By
9136 connecting to the "destroy" signal, we make sure that we don't keep a
9137 pointer to dialog around after it is destroyed - that could lead to a
9138 segfault.)
9139
9140 <p>
9141 The InputDialog has two buttons "Close" and "Save", which by default
9142 have no actions assigned to them. In the above function we make
9143 "Close" hide the dialog, hide the "Save" button, since we don't
9144 implement saving of XInput options in this program.
9145
9146 <sect2> Using extended device information
9147
9148 <p>
9149 Once we've enabled the device, we can just use the extended 
9150 device information in the extra fields of the event structures.
9151 In fact, it is always safe to use this information since these
9152 fields will have reasonable default values even when extended
9153 events are not enabled.
9154
9155 <p>
9156 Once change we do have to make is to call
9157 <tt/gdk_input_window_get_pointer()/ instead of
9158 <tt/gdk_window_get_pointer/. This is necessary because
9159 <tt/gdk_window_get_pointer/ doesn't return the extended device
9160 information.
9161
9162 <tscreen><verb>
9163 void gdk_input_window_get_pointer     (GdkWindow       *window,
9164                                        guint32         deviceid,
9165                                        gdouble         *x,
9166                                        gdouble         *y,
9167                                        gdouble         *pressure,
9168                                        gdouble         *xtilt,
9169                                        gdouble         *ytilt,
9170                                        GdkModifierType *mask);
9171 </verb></tscreen>
9172
9173 When calling this function, we need to specify the device ID as
9174 well as the window. Usually, we'll get the device ID from the
9175 <tt/deviceid/ field of an event structure. Again, this function
9176 will return reasonable values when extension events are not
9177 enabled. (In this case, <tt/event->deviceid/ will have the value
9178 <tt/GDK_CORE_POINTER/).
9179
9180 So the basic structure of our button-press and motion event handlers,
9181 doesn't change much - we just need to add code to deal with the
9182 extended information.
9183
9184 <tscreen><verb>
9185 static gint
9186 button_press_event (GtkWidget *widget, GdkEventButton *event)
9187 {
9188   print_button_press (event->deviceid);
9189   
9190   if (event->button == 1 &amp;&amp; pixmap != NULL)
9191     draw_brush (widget, event->source, event->x, event->y, event->pressure);
9192
9193   return TRUE;
9194 }
9195
9196 static gint
9197 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
9198 {
9199   gdouble x, y;
9200   gdouble pressure;
9201   GdkModifierType state;
9202
9203   if (event->is_hint)
9204     gdk_input_window_get_pointer (event->window, event->deviceid,
9205                                   &amp;x, &amp;y, &amp;pressure, NULL, NULL, &amp;state);
9206   else
9207     {
9208       x = event->x;
9209       y = event->y;
9210       pressure = event->pressure;
9211       state = event->state;
9212     }
9213     
9214   if (state &amp; GDK_BUTTON1_MASK &amp;&amp; pixmap != NULL)
9215     draw_brush (widget, event->source, x, y, pressure);
9216   
9217   return TRUE;
9218 }
9219 </verb></tscreen>
9220
9221 We also need to do something with the new information. Our new
9222 <tt/draw_brush()/ function draws with a different color for
9223 each <tt/event->source/ and changes the brush size depending
9224 on the pressure.
9225
9226 <tscreen><verb>
9227 /* Draw a rectangle on the screen, size depending on pressure,
9228    and color on the type of device */
9229 static void
9230 draw_brush (GtkWidget *widget, GdkInputSource source,
9231             gdouble x, gdouble y, gdouble pressure)
9232 {
9233   GdkGC *gc;
9234   GdkRectangle update_rect;
9235
9236   switch (source)
9237     {
9238     case GDK_SOURCE_MOUSE:
9239       gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
9240       break;
9241     case GDK_SOURCE_PEN:
9242       gc = widget->style->black_gc;
9243       break;
9244     case GDK_SOURCE_ERASER:
9245       gc = widget->style->white_gc;
9246       break;
9247     default:
9248       gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
9249     }
9250
9251   update_rect.x = x - 10 * pressure;
9252   update_rect.y = y - 10 * pressure;
9253   update_rect.width = 20 * pressure;
9254   update_rect.height = 20 * pressure;
9255   gdk_draw_rectangle (pixmap, gc, TRUE,
9256                       update_rect.x, update_rect.y,
9257                       update_rect.width, update_rect.height);
9258   gtk_widget_draw (widget, &amp;update_rect);
9259 }
9260 </verb></tscreen>
9261
9262 <sect2> Finding out more about a device
9263
9264 <p>
9265 As an example of how to find out more about a device, our program
9266 will print the name of the device that generates each button
9267 press. To find out the name of a device, we call the function:
9268
9269 <tscreen><verb>
9270 GList *gdk_input_list_devices               (void);
9271 </verb></tscreen>
9272
9273 which returns a GList (a linked list type from the glib library)
9274 of GdkDeviceInfo structures. The GdkDeviceInfo strucure is defined
9275 as:
9276
9277 <tscreen><verb>
9278 struct _GdkDeviceInfo
9279 {
9280   guint32 deviceid;
9281   gchar *name;
9282   GdkInputSource source;
9283   GdkInputMode mode;
9284   gint has_cursor;
9285   gint num_axes;
9286   GdkAxisUse *axes;
9287   gint num_keys;
9288   GdkDeviceKey *keys;
9289 };
9290 </verb></tscreen>
9291
9292 Most of these fields are configuration information that you
9293 can ignore unless you are implemented XInput configuration
9294 saving. The we are interested in here is <tt/name/ which is
9295 simply the name that X assigns to the device. The other field
9296 that isn't configuration information is <tt/has_cursor/. If
9297 <tt/has_cursor/ is false, then we we need to draw our own
9298 cursor. But since we've specified <tt/GDK_EXTENSION_EVENTS_CURSOR/,
9299 we don't have to worry about this.
9300
9301 <p>
9302 Our <tt/print_button_press()/ function simply iterates through
9303 the returned list until it finds a match, then prints out
9304 the name of the device.
9305
9306 <tscreen><verb>
9307 static void
9308 print_button_press (guint32 deviceid)
9309 {
9310   GList *tmp_list;
9311
9312   /* gdk_input_list_devices returns an internal list, so we shouldn't
9313      free it afterwards */
9314   tmp_list = gdk_input_list_devices();
9315
9316   while (tmp_list)
9317     {
9318       GdkDeviceInfo *info = (GdkDeviceInfo *)tmp_list->data;
9319
9320       if (info->deviceid == deviceid)
9321         {
9322           printf("Button press on device '%s'\n", info->name);
9323           return;
9324         }
9325
9326       tmp_list = tmp_list->next;
9327     }
9328 }
9329 </verb></tscreen>
9330
9331 That completes the changes to ``XInputize'' our program. As with
9332 the first version, the complete source is available at the location
9333 from which you got this tutorial, or from:
9334
9335 <htmlurl url="http://www.msc.cornell.edu/~otaylor/gtk-gimp/tutorial"
9336 name="http://www.msc.cornell.edu/~otaylor/gtk-gimp/tutorial">
9337
9338
9339 <sect2> Further sophistications <label id="sec_Further_Sophistications">
9340
9341 <p>
9342 Although our program now supports XInput quite well, it lacks some
9343 features we would want in a full-featured application. First, the user
9344 probably doesn't want to have to configure their device each time they
9345 run the program, so we should allow them to save the device
9346 configuration. This is done by iterating through the return of
9347 <tt/gdk_input_list_devices()/ and writing out the configuration to a
9348 file.
9349
9350 <p>
9351 To restore the state next time the program is run, GDK provides
9352 functions to change device configuration:
9353
9354 <tscreen><verb>
9355 gdk_input_set_extension_events()
9356 gdk_input_set_source()
9357 gdk_input_set_mode()
9358 gdk_input_set_axes()
9359 gdk_input_set_key()
9360 </verb></tscreen>
9361
9362 (The list returned from <tt/gdk_input_list_devices()/ should not be
9363 modified directly.) An example of doing this can be found in the
9364 drawing program gsumi. (Available from <htmlurl
9365 url="http://www.msc.cornell.edu/~otaylor/gsumi/"
9366 name="http://www.msc.cornell.edu/~otaylor/gsumi/">) Eventually, it
9367 would be nice to have a standard way of doing this for all
9368 applications. This probably belongs at a slightly higher level than
9369 GTK, perhaps in the GNOME library.
9370
9371 <p>
9372 Another major ommission that we have mentioned above is the lack of
9373 cursor drawing. Platforms other than XFree86 currently do not allow
9374 simultaneously using a device as both the core pointer and directly by
9375 an application. See the <url
9376 url="http://www.msc.cornell.edu/~otaylor/xinput/XInput-HOWTO.html"
9377 name="XInput-HOWTO"> for more information about this. This means that
9378 applications that want to support the widest audience need to draw
9379 their own cursor.
9380
9381 <p>
9382 An application that draws it's own cursor needs to do two things:
9383 determine if the current device needs a cursor drawn or not, and
9384 determine if the current device is in proximity. (If the current
9385 device is a drawing tablet, it's a nice touch to make the cursor 
9386 disappear when the stylus is lifted from the tablet. When the
9387 device is touching the stylus, that is called "in proximity.")
9388 The first is done by searching the device list, as we did
9389 to find out the device name. The second is achieved by selecting
9390 "proximity_out" events. An example of drawing one's own cursor is
9391 found in the 'testinput' program found in the GTK distribution.
9392
9393 <!-- ***************************************************************** -->
9394 <sect>Tips For Writing GTK Applications
9395 <!-- ***************************************************************** -->
9396
9397 <p>
9398 This section is simply a gathering of wisdom, general style guidelines and hints to
9399 creating good GTK applications. It is totally useless right now cause it's
9400 only a topic sentence :)
9401
9402 Use GNU autoconf and automake!  They are your friends :)  I am planning to
9403 make a quick intro on them here.
9404
9405 <!-- ***************************************************************** -->
9406 <sect>Contributing
9407 <!-- ***************************************************************** -->
9408
9409 <p>
9410 This document, like so much other great software out there, was created for
9411 free by volunteers.  If you are at all knowledgeable about any aspect of GTK
9412 that does not already have documentation, please consider contributing to
9413 this document.
9414 <p>
9415 If you do decide to contribute, please mail your text to Tony Gale, 
9416 <tt><htmlurl url="mailto:gale@gtk.org"
9417 name="gale@gtk.org"></tt>. Also, be aware that the entirety of this 
9418 document is free, and any addition by yourself must also be free.  That is, 
9419 people may use any portion of your examples in their programs, and copies 
9420 of this document may be distributed at will etc.
9421 <p>
9422 Thank you.
9423
9424 <!-- ***************************************************************** -->
9425 <sect>Credits
9426 <!-- ***************************************************************** -->
9427 <p>
9428 I would like to thank the following for their contributions to this text.
9429
9430 <itemize>
9431 <item>Bawer Dagdeviren, <tt><htmlurl url="mailto:chamele0n@geocities.com"
9432 name="chamele0n@geocities.com"></tt> for the menus tutorial.                          
9433
9434 <item>Raph Levien, <tt><htmlurl url="mailto:raph@acm.org"
9435 name="raph@acm.org"></tt>
9436 for hello world ala GTK, widget packing, and general all around wisdom.
9437 He's also generously donated a home for this tutorial.
9438
9439 <item>Peter Mattis, <tt><htmlurl url="mailto:petm@xcf.berkeley.edu"
9440 name="petm@xcf.berkeley.edu"></tt> for the simplest GTK program.. 
9441 and the ability to make it :)
9442
9443 <item>Werner Koch <tt><htmlurl url="mailto:werner.koch@guug.de"
9444 name="werner.koch@guug.de"></tt> for converting the original plain text to
9445 SGML, and the widget class hierarchy.
9446
9447 <item>Mark Crichton <tt><htmlurl url="mailto:crichton@expert.cc.purdue.edu"
9448 name="crichton@expert.cc.purdue.edu"></tt> for the menu factory code, and
9449 the table packing tutorial.
9450
9451 <item>Owen Taylor <tt><htmlurl url="mailto:owt1@cornell.edu"
9452 name="owt1@cornell.edu"></tt> for the EventBox widget section (and
9453 the patch to the distro).  He's also responsible for the selections code and
9454 tutorial, as well as the sections on writing your own GTK widgets, and the
9455 example application.  Thanks a lot Owen for all you help!
9456
9457 <item>Mark VanderBoom <tt><htmlurl url="mailto:mvboom42@calvin.edu"
9458 name="mvboom42@calvin.edu"></tt> for his wonderful work on the Notebook,
9459 Progress Bar, Dialogs, and File selection widgets.  Thanks a lot Mark!
9460 You've been a great help.
9461
9462 <item>Tim Janik <tt><htmlurl url="mailto:timj@psynet.net"
9463 name="timj@psynet.net"></tt> for his great job on the Lists Widget.
9464 Thanks Tim :)
9465
9466 <item>Rajat Datta <tt><htmlurl url="mailto:rajat@ix.netcom.com"
9467 name="rajat@ix.netcom.com"</tt> for the excellent job on the Pixmap tutorial.
9468
9469 <item>Michael K. Johnson <tt><htmlurl url="mailto:johnsonm@redhat.com"
9470 name="johnsonm@redhat.com"></tt> for info and code for popup menus. 
9471
9472 </itemize>
9473 <p>
9474 And to all of you who commented and helped refine this document.
9475 <p>
9476 Thanks.
9477
9478 <!-- ***************************************************************** -->
9479 <sect> Tutorial Copyright and Permissions Notice
9480 <!-- ***************************************************************** -->
9481
9482 <p>
9483 The GTK Tutorial is Copyright (C) 1997 Ian Main. 
9484
9485 Copyright (C) 1998 Tony Gale.
9486 <p>
9487 Permission is granted to make and distribute verbatim copies of this 
9488 manual provided the copyright notice and this permission notice are 
9489 preserved on all copies.
9490 <P>Permission is granted to copy and distribute modified versions of 
9491 this document under the conditions for verbatim copying, provided that 
9492 this copyright notice is included exactly as in the original,
9493 and that the entire resulting derived work is distributed under 
9494 the terms of a permission notice identical to this one.
9495 <P>Permission is granted to copy and distribute translations of this 
9496 document into another language, under the above conditions for modified 
9497 versions.
9498 <P>If you are intending to incorporate this document into a published 
9499 work, please contact the maintainer, and we will make an effort 
9500 to ensure that you have the most up to date information available.
9501 <P>There is no guarentee that this document lives up to its intended
9502 purpose.  This is simply provided as a free resource.  As such,
9503 the authors and maintainers of the information provided within can
9504 not make any guarentee that the information is even accurate.
9505 </article>