]> Pileus Git - ~andy/gtk/blob - docs/tutorial/gtk_tut.sgml
- re-write the GtkProgressBar section to the 1.1 API. - add an Appendix
[~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 v1.1 Tutorial
9 <author>
10 Tony Gale <tt><htmlurl url="mailto:gale@gtk.org"
11                               name="&lt;gale@gtk.org&gt;"></tt>
12 Ian Main <tt><htmlurl url="mailto:imain@gtk.org"
13                               name="&lt;imain@gtk.org&gt;"></tt>,
14 <date>December 6th, 1998
15
16 <!-- ***************************************************************** -->
17 <sect>Introduction
18 <!-- ***************************************************************** -->
19 <p>
20 GTK (GIMP Toolkit) was originally developed as a toolkit for the GIMP
21 (General Image Manipulation Program).  GTK is built on top of GDK
22 (GIMP Drawing Kit) which is basically a wrapper around the Xlib
23 functions.  It's called the GIMP toolkit because it was originally
24 written for developing the GIMP, but has now been used in several free
25 software projects.  The authors are:
26 <itemize>
27 <item> Peter Mattis   <tt><htmlurl url="mailto:petm@xcf.berkeley.edu"
28                            name="petm@xcf.berkeley.edu"></tt>
29 <item> Spencer Kimball <tt><htmlurl url="mailto:spencer@xcf.berkeley.edu"
30                            name="spencer@xcf.berkeley.edu"></tt>
31 <item> Josh MacDonald <tt><htmlurl url="mailto:jmacd@xcf.berkeley.edu"
32                            name="jmacd@xcf.berkeley.edu"></tt>
33 </itemize>
34
35 GTK is essentially an object oriented application programmers
36 interface (API).  Although written completely in C, it is implemented
37 using the idea of classes and callback functions (pointers to
38 functions).
39
40 There is also a third component called glib which contains a few
41 replacements for some standard calls, as well as some additional
42 functions for handling linked lists etc.  The replacement functions
43 are used to increase GTK's portability, as some of the functions
44 implemented here are not available or are nonstandard on other unixes
45 such as g_strerror().  Some also contain enhancements to the libc
46 versions, such as g_malloc that has enhanced debugging utilities.
47
48 This tutorial is an attempt to document as much as possible of GTK, it
49 is by no means complete.  This tutorial assumes a good understanding
50 of C, and how to create C programs.  It would be a great benefit for
51 the reader to have previous X programming experience, but it shouldn't
52 be necessary.  If you are learning GTK as your first widget set,
53 please comment on how you found this tutorial, and what you had
54 trouble with.  Note that there is also a C++ API for GTK (GTK--) in
55 the works, so if you prefer to use C++, you should look into this
56 instead.  There's also an Objective C wrapper, and Guile bindings
57 available, but I don't follow these.
58
59 I would very much like to hear of any problems you have learning GTK
60 from this document, and would appreciate input as to how it may be
61 improved. Please see the section on <ref id="sec_Contributing"
62 name="Contributing"> for further information.
63
64 <!-- ***************************************************************** -->
65 <sect>Getting Started
66 <!-- ***************************************************************** -->
67
68 <p>
69 The first thing to do of course, is download the GTK source and
70 install it. You can always get the latest version from ftp.gtk.org in
71 /pub/gtk. You can also view other sources of GTK information on
72 http://www.gtk.org/ <htmlurl url="http://www.gtk.org/"
73 name="http://www.gtk.org/">. GTK uses GNU autoconf for configuration.
74 Once untar'd, type ./configure --help to see a list of options.
75
76 Th GTK source distribution also contains the complete source to all of
77 the examples used in this tutorial, along with Makefiles to aid
78 compilation.
79
80 To begin our introduction to GTK, we'll start with the simplest
81 program possible. This program will create a 200x200 pixel window and
82 has no way of exiting except to be killed using the shell.
83
84 <tscreen><verb>
85 /* example-start base base.c */
86
87 #include <gtk/gtk.h>
88
89 int main( int   argc,
90           char *argv[] )
91 {
92     GtkWidget *window;
93     
94     gtk_init (&amp;argc, &amp;argv);
95     
96     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
97     gtk_widget_show  (window);
98     
99     gtk_main ();
100     
101     return(0);
102 }
103 /* example-end */
104 </verb></tscreen>
105
106 You can compile the above program with gcc using:
107 <tscreen><verb>
108 gcc base.c -o base `gtk-config --cflags --libs`
109 </verb></tscreen>
110
111 The meaning of the unusual compilation options is explained below.
112
113 All programs will of course include gtk/gtk.h which declares the
114 variables, functions, structures etc. that will be used in your GTK
115 application.
116
117 The next line:
118
119 <tscreen><verb>
120 gtk_init (&amp;argc, &amp;argv);
121 </verb></tscreen>
122
123 calls the function gtk_init(gint *argc, gchar ***argv) which will be
124 called in all GTK applications. This sets up a few things for us such
125 as the default visual and color map and then proceeds to call
126 gdk_init(gint *argc, gchar ***argv). This function initializes the
127 library for use, sets up default signal handlers, and checks the
128 arguments passed to your application on the command line, looking for
129 one of the following:
130
131 <itemize>
132 <item> <tt/--gtk-module/
133 <item> <tt/--g-fatal-warnings/
134 <item> <tt/--gtk-debug/
135 <item> <tt/--gtk-no-debug/
136 <item> <tt/--gdk-debug/
137 <item> <tt/--gdk-no-debug/
138 <item> <tt/--display/
139 <item> <tt/--sync/
140 <item> <tt/--no-xshm/
141 <item> <tt/--name/
142 <item> <tt/--class/
143 </itemize>
144
145 It removes these from the argument list, leaving anything it does not
146 recognize for your application to parse or ignore. This creates a set
147 of standard arguments accepted by all GTK applications.
148
149 The next two lines of code create and display a window.
150
151 <tscreen><verb>
152   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
153   gtk_widget_show (window);
154 </verb></tscreen>
155
156 The GTK_WINDOW_TOPLEVEL argument specifies that we want the window to
157 undergo window manager decoration and placement. Rather than create a
158 window of 0x0 size, a window without children is set to 200x200 by
159 default so you can still manipulate it.
160
161 The gtk_widget_show() function lets GTK know that we are done setting
162 the attributes of this widget, and that it can display it.
163
164 The last line enters the GTK main processing loop.
165
166 <tscreen><verb>
167   gtk_main ();
168 </verb></tscreen>
169
170 gtk_main() is another call you will see in every GTK application.
171 When control reaches this point, GTK will sleep waiting for X events
172 (such as button or key presses), timeouts, or file IO notifications to
173 occur. In our simple example however, events are ignored.
174
175 <!-- ----------------------------------------------------------------- -->
176 <sect1>Hello World in GTK
177 <p>
178 Now for a program with a widget (a button).  It's the classic
179 hello world a la GTK.
180
181 <tscreen><verb>
182 /* example-start helloworld helloworld.c */
183
184 #include <gtk/gtk.h>
185
186 /* This is a callback function. The data arguments are ignored
187  * in this example. More on callbacks below. */
188 void hello( GtkWidget *widget,
189             gpointer   data )
190 {
191     g_print ("Hello World\n");
192 }
193
194 gint delete_event( GtkWidget *widget,
195                    GdkEvent  *event,
196                    gpointer   data )
197 {
198     /* If you return FALSE in the "delete_event" signal handler,
199      * GTK will emit the "destroy" signal. Returning TRUE means
200      * you don't want the window to be destroyed.
201      * This is useful for popping up 'are you sure you want to quit?'
202      * type dialogs. */
203
204     g_print ("delete event occurred\n");
205
206     /* Change TRUE to FALSE and the main window will be destroyed with
207      * a "delete_event". */
208
209     return(TRUE);
210 }
211
212 /* Another callback */
213 void destroy( GtkWidget *widget,
214               gpointer   data )
215 {
216     gtk_main_quit();
217 }
218
219 int main( int   argc,
220           char *argv[] )
221 {
222     /* GtkWidget is the storage type for widgets */
223     GtkWidget *window;
224     GtkWidget *button;
225     
226     /* This is called in all GTK applications. Arguments are parsed
227      * from the command line and are returned to the application. */
228     gtk_init(&amp;argc, &amp;argv);
229     
230     /* create a new window */
231     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
232     
233     /* When the window is given the "delete_event" signal (this is given
234      * by the window manager, usually by the 'close' option, or on the
235      * titlebar), we ask it to call the delete_event () function
236      * as defined above. The data passed to the callback
237      * function is NULL and is ignored in the callback function. */
238     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
239                         GTK_SIGNAL_FUNC (delete_event), NULL);
240     
241     /* Here we connect the "destroy" event to a signal handler.  
242      * This event occurs when we call gtk_widget_destroy() on the window,
243      * or if we return 'FALSE' in the "delete_event" callback. */
244     gtk_signal_connect (GTK_OBJECT (window), "destroy",
245                         GTK_SIGNAL_FUNC (destroy), NULL);
246     
247     /* Sets the border width of the window. */
248     gtk_container_border_width (GTK_CONTAINER (window), 10);
249     
250     /* Creates a new button with the label "Hello World". */
251     button = gtk_button_new_with_label ("Hello World");
252     
253     /* When the button receives the "clicked" signal, it will call the
254      * function hello() passing it NULL as its argument.  The hello()
255      * function is defined above. */
256     gtk_signal_connect (GTK_OBJECT (button), "clicked",
257                         GTK_SIGNAL_FUNC (hello), NULL);
258     
259     /* This will cause the window to be destroyed by calling
260      * gtk_widget_destroy(window) when "clicked".  Again, the destroy
261      * signal could come from here, or the window manager. */
262     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
263                                GTK_SIGNAL_FUNC (gtk_widget_destroy),
264                                GTK_OBJECT (window));
265     
266     /* This packs the button into the window (a gtk container). */
267     gtk_container_add (GTK_CONTAINER (window), button);
268     
269     /* The final step is to display this newly created widget. */
270     gtk_widget_show (button);
271     
272     /* and the window */
273     gtk_widget_show (window);
274     
275     /* All GTK applications must have a gtk_main(). Control ends here
276      * and waits for an event to occur (like a key press or
277      * mouse event). */
278     gtk_main ();
279     
280     return(0);
281 }
282 /* example-end */
283 </verb></tscreen>
284
285 <!-- ----------------------------------------------------------------- -->
286 <sect1>Compiling Hello World
287 <p>
288 To compile use:
289
290 <tscreen><verb>
291 gcc -Wall -g helloworld.c -o helloworld `gtk-config --cflags` \
292     `gtk-config --libs`
293 </verb></tscreen>
294
295 This uses the program <tt>gtk-config</>, which comes with gtk. This
296 program 'knows' what compiler switches are needed to compile programs
297 that use gtk. <tt>gtk-config --cflags</> will output a list of include
298 directories for the compiler to look in, and <tt>gtk-config --libs</>
299 will output the list of libraries for the compiler to link with and
300 the directories to find them in. In the aboce example they could have
301 been combined into a single instance, such as
302 `gtk-config --cflags --libs`.
303
304 Note that the type of single quote used in the compile command above
305 is significant.
306
307 The libraries that are usually linked in are:
308 <itemize>
309 <item>The GTK library (-lgtk), the widget library, based on top of GDK.
310 <item>The GDK library (-lgdk), the Xlib wrapper.
311 <item>The gmodule library (-lgmodule), which is used to load run time
312 extensions.
313 <item>The glib library (-lglib), containing miscellaneous functions, only
314 g_print() is used in this particular example. GTK is built on top
315 of glib so you will always require this library. See the section on 
316 <ref id="sec_glib" name="glib"> for details.
317 <item>The Xlib library (-lX11) which is used by GDK.
318 <item>The Xext library (-lXext). This contains code for shared memory
319 pixmaps and other X extensions.
320 <item>The math library (-lm). This is used by GTK for various purposes.
321 </itemize>
322
323 <!-- ----------------------------------------------------------------- -->
324 <sect1>Theory of Signals and Callbacks
325 <p>
326 Before we look in detail at <em>helloworld</em>, we'll discuss signals
327 and callbacks. GTK is an event driven toolkit, which means it will
328 sleep in gtk_main until an event occurs and control is passed to the
329 appropriate function.
330
331 This passing of control is done using the idea of "signals". When an
332 event occurs, such as the press of a mouse button, the appropriate
333 signal will be "emitted" by the widget that was pressed.  This is how
334 GTK does most of its useful work. There are a set of signals that all
335 widgets inherit, such as "destroy", and there are signals that are
336 widget specific, such as "toggled" on a toggle button.
337
338 To make a button perform an action, we set up a signal handler to
339 catch these signals and call the appropriate function. This is done by
340 using a function such as:
341
342 <tscreen><verb>
343 gint gtk_signal_connect( GtkObject     *object,
344                          gchar         *name,
345                          GtkSignalFunc  func,
346                          gpointer       func_data );
347 </verb></tscreen>
348
349 Where the first argument is the widget which will be emitting the
350 signal, and the second, the name of the signal you wish to catch. The
351 third is the function you wish to be called when it is caught, and the
352 fourth, the data you wish to have passed to this function.
353
354 The function specified in the third argument is called a "callback
355 function", and should generally be of the form:
356
357 <tscreen><verb>
358 void callback_func( GtkWidget *widget,
359                     gpointer   callback_data );
360 </verb></tscreen>
361
362 Where the first argument will be a pointer to the widget that emitted
363 the signal, and the second, a pointer to the data given as the last
364 argument to the gtk_signal_connect() function as shown above.
365
366 Note that the above form for a signal callback function declaration is
367 only a general guide, as some widget specific signals generate
368 different calling parameters. For example, the GtkCList "select_row"
369 signal provides both row and column parameters.
370
371 Another call used in the <em>helloworld</em> example, is:
372
373 <tscreen><verb>
374 gint gtk_signal_connect_object( GtkObject     *object,
375                                 gchar         *name,
376                                 GtkSignalFunc  func,
377                                 GtkObject     *slot_object );
378 </verb></tscreen>
379
380 gtk_signal_connect_object() is the same as gtk_signal_connect() except
381 that the callback function only uses one argument, a pointer to a GTK
382 object. So when using this function to connect signals, the callback
383 should be of the form:
384
385 <tscreen><verb>
386 void callback_func( GtkObject *object );
387 </verb></tscreen>
388
389 Where the object is usually a widget. We usually don't setup callbacks
390 for gtk_signal_connect_object however. They are usually used to call a
391 GTK function that accepts a single widget or object as an argument, as
392 is the case in our <em>helloworld</em> example.
393
394 The purpose of having two functions to connect signals is simply to
395 allow the callbacks to have a different number of arguments. Many
396 functions in the GTK library accept only a single GtkWidget pointer as
397 an argument, so you want to use the gtk_signal_connect_object() for
398 these, whereas for your functions, you may need to have additional
399 data supplied to the callbacks.
400
401 <!-- ----------------------------------------------------------------- -->
402 <sect1>Events
403 <p>
404 In addition to the signal mechanism described above, there are a set
405 of <em>events</em> that reflect the X event mechanism. Callbacks may
406 also be attached to these events. These events are:
407
408 <itemize>
409 <item> event
410 <item> button_press_event
411 <item> button_release_event
412 <item> motion_notify_event
413 <item> delete_event
414 <item> destroy_event
415 <item> expose_event
416 <item> key_press_event
417 <item> key_release_event
418 <item> enter_notify_event
419 <item> leave_notify_event
420 <item> configure_event
421 <item> focus_in_event
422 <item> focus_out_event
423 <item> map_event
424 <item> unmap_event
425 <item> property_notify_event
426 <item> selection_clear_event
427 <item> selection_request_event
428 <item> selection_notify_event
429 <item> proximity_in_event
430 <item> proximity_out_event
431 <item> drag_begin_event
432 <item> drag_request_event
433 <item> drag_end_event
434 <item> drop_enter_event
435 <item> drop_leave_event
436 <item> drop_data_available_event
437 <item> other_event
438 </itemize>
439
440 In order to connect a callback function to one of these events, you
441 use the function gtk_signal_connect, as described above, using one of
442 the above event names as the <tt/name/ parameter. The callback
443 function for events has a slightly different form than that for
444 signals:
445
446 <tscreen><verb>
447 void callback_func( GtkWidget *widget,
448                     GdkEvent  *event,
449                     gpointer   callback_data );
450 </verb></tscreen>
451
452 GdkEvent is a C <tt/union/ structure whose type will depend upon which
453 of the above events has occurred. In order for us to tell which event
454 has been issued each of the possible alternatives has a <tt/type/
455 parameter which reflects the event being issued. The other components
456 of the event structure will depend upon the type of the
457 event. Possible values for the type are:
458
459 <tscreen><verb>
460   GDK_NOTHING
461   GDK_DELETE
462   GDK_DESTROY
463   GDK_EXPOSE
464   GDK_MOTION_NOTIFY
465   GDK_BUTTON_PRESS
466   GDK_2BUTTON_PRESS
467   GDK_3BUTTON_PRESS
468   GDK_BUTTON_RELEASE
469   GDK_KEY_PRESS
470   GDK_KEY_RELEASE
471   GDK_ENTER_NOTIFY
472   GDK_LEAVE_NOTIFY
473   GDK_FOCUS_CHANGE
474   GDK_CONFIGURE
475   GDK_MAP
476   GDK_UNMAP
477   GDK_PROPERTY_NOTIFY
478   GDK_SELECTION_CLEAR
479   GDK_SELECTION_REQUEST
480   GDK_SELECTION_NOTIFY
481   GDK_PROXIMITY_IN
482   GDK_PROXIMITY_OUT
483   GDK_DRAG_BEGIN
484   GDK_DRAG_REQUEST
485   GDK_DROP_ENTER
486   GDK_DROP_LEAVE
487   GDK_DROP_DATA_AVAIL
488   GDK_CLIENT_EVENT
489   GDK_VISIBILITY_NOTIFY
490   GDK_NO_EXPOSE
491   GDK_OTHER_EVENT       /* Deprecated, use filters instead */
492 </verb></tscreen>
493
494 So, to connect a callback function to one of these events we would use
495 something like:
496
497 <tscreen><verb>
498 gtk_signal_connect( GTK_OBJECT(button), "button_press_event",
499                     GTK_SIGNAL_FUNC(button_press_callback), 
500                         NULL);
501 </verb></tscreen>
502
503 This assumes that <tt/button/ is a GtkButton widget. Now, when the
504 mouse is over the button and a mouse button is pressed, the function
505 <tt/button_press_callback/ will be called. This function may be
506 declared as:
507
508 <tscreen><verb>
509 static gint button_press_event (GtkWidget      *widget, 
510                                 GdkEventButton *event,
511                                 gpointer        data);
512 </verb></tscreen>
513
514 Note that we can declare the second argument as type
515 <tt/GdkEventButton/ as we know what type of event will occur for this
516 function to be called.
517
518 The value returned from this function indicates whether the event
519 should be propagated further by the GTK event handling
520 mechanism. Returning TRUE indicates that the event has been handled,
521 and that it should not propagate further. Returning FALSE continues
522 the normal event handling.  See the section on
523 <ref id="sec_Adv_Events_and_Signals"
524 name="Advanced Event and Signal Handling"> for more details on this
525 propagation process.
526
527 For details on the GdkEvent data types, see the appendix entitled
528 <ref id="sec_GDK_Event_Types" name="GDK Event Types">.
529
530 <!-- ----------------------------------------------------------------- -->
531 <sect1>Stepping Through Hello World
532 <p>
533 Now that we know the theory behind this, lets clarify by walking through 
534 the example <em>helloworld</em> program.
535
536 Here is the callback function that will be called when the button is
537 "clicked". We ignore both the widget and the data in this example, but
538 it is not hard to do things with them. The next example will use the
539 data argument to tell us which button was pressed.
540
541 <tscreen><verb>
542 void hello( GtkWidget *widget,
543             gpointer   data )
544 {
545     g_print ("Hello World\n");
546 }
547 </verb></tscreen>
548
549 The next callback is a bit special. The "delete_event" occurs when the
550 window manager sends this event to the application. We have a choice
551 here as to what to do about these events. We can ignore them, make
552 some sort of response, or simply quit the application.
553
554 The value you return in this callback lets GTK know what action to
555 take.  By returning TRUE, we let it know that we don't want to have
556 the "destroy" signal emitted, keeping our application running. By
557 returning FALSE, we ask that "destroy" is emitted, which in turn will
558 call our "destroy" signal handler.
559
560 <tscreen><verb>
561 gint delete_event( GtkWidget *widget,
562                    GdkEvent  *event,
563                    gpointer   data )
564 {
565     g_print ("delete event occurred\n");
566
567     return (TRUE); 
568 }
569 </verb></tscreen>
570
571 Here is another callback function which causes the program to quit by
572 calling gtk_main_quit(). This function tells GTK that it is to exit
573 from gtk_main when control is returned to it.
574
575 <tscreen><verb>
576 void destroy( GtkWidget *widget,
577               gpointer   data )
578 {
579     gtk_main_quit ();
580 }
581 </verb></tscreen>
582
583 I assume you know about the main() function... yes, as with other
584 applications, all GTK applications will also have one of these.
585
586 <tscreen><verb>
587 int main( int   argc,
588           char *argv[] )
589 {
590 </verb></tscreen>
591
592 This next part, declares a pointer to a structure of type
593 GtkWidget. These are used below to create a window and a button.
594
595 <tscreen><verb>
596     GtkWidget *window;
597     GtkWidget *button;
598 </verb></tscreen>
599
600 Here is our gtk_init again. As before, this initializes the toolkit,
601 and parses the arguments found on the command line. Any argument it
602 recognizes from the command line, it removes from the list, and
603 modifies argc and argv to make it look like they never existed,
604 allowing your application to parse the remaining arguments.
605
606 <tscreen><verb>
607     gtk_init (&amp;argc, &amp;argv);
608 </verb></tscreen>
609
610 Create a new window. This is fairly straight forward. Memory is
611 allocated for the GtkWidget *window structure so it now points to a
612 valid structure. It sets up a new window, but it is not displayed
613 until we call gtk_widget_show(window) near the end of our program.
614
615 <tscreen><verb>
616     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
617 </verb></tscreen>
618
619 Here is an example of connecting a signal handler to an object, in
620 this case, the window. Here, the "destroy" signal is caught. This is
621 emitted when we use the window manager to kill the window (and we
622 return TRUE in the "delete_event" handler), or when we use the
623 gtk_widget_destroy() call passing in the window widget as the object
624 to destroy. By setting this up, we handle both cases with a single
625 call.  Here, it just calls the destroy() function defined above with a
626 NULL argument, which quits GTK for us.
627
628 The GTK_OBJECT and GTK_SIGNAL_FUNC are macros that perform type
629 casting and checking for us, as well as aid the readability of the
630 code.
631
632 <tscreen><verb>
633     gtk_signal_connect (GTK_OBJECT (window), "destroy",
634                         GTK_SIGNAL_FUNC (destroy), NULL);
635 </verb></tscreen>
636
637 This next function is used to set an attribute of a container object.
638 This just sets the window so it has a blank area along the inside of
639 it 10 pixels wide where no widgets will go. There are other similar
640 functions which we will look at in the section on
641 <ref id="sec_setting_widget_attributes" name="Setting Widget Attributes">
642
643 And again, GTK_CONTAINER is a macro to perform type casting.
644
645 <tscreen><verb>
646     gtk_container_border_width (GTK_CONTAINER (window), 10);
647 </verb></tscreen>
648
649 This call creates a new button. It allocates space for a new GtkWidget
650 structure in memory, initializes it, and makes the button pointer
651 point to it. It will have the label "Hello World" on it when
652 displayed.
653
654 <tscreen><verb>
655     button = gtk_button_new_with_label ("Hello World");
656 </verb></tscreen>
657
658 Here, we take this button, and make it do something useful. We attach
659 a signal handler to it so when it emits the "clicked" signal, our
660 hello() function is called. The data is ignored, so we simply pass in
661 NULL to the hello() callback function. Obviously, the "clicked" signal
662 is emitted when we click the button with our mouse pointer.
663
664 <tscreen><verb>
665     gtk_signal_connect (GTK_OBJECT (button), "clicked",
666                         GTK_SIGNAL_FUNC (hello), NULL);
667 </verb></tscreen>
668
669 We are also going to use this button to exit our program. This will
670 illustrate how the "destroy" signal may come from either the window
671 manager, or our program. When the button is "clicked", same as above,
672 it calls the first hello() callback function, and then this one in the
673 order they are set up. You may have as many callback functions as you
674 need, and all will be executed in the order you connected
675 them. Because the gtk_widget_destroy() function accepts only a
676 GtkWidget *widget as an argument, we use the
677 gtk_signal_connect_object() function here instead of straight
678 gtk_signal_connect().
679
680 <tscreen><verb>
681     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
682                                GTK_SIGNAL_FUNC (gtk_widget_destroy),
683                                GTK_OBJECT (window));
684 </verb></tscreen>
685
686 This is a packing call, which will be explained in depth later on. But
687 it is fairly easy to understand. It simply tells GTK that the button
688 is to be placed in the window where it will be displayed. Note that a
689 GTK container can only contain one widget. There are other widgets,
690 that are described later, which are designed to layout multiple
691 widgets in various ways.
692  
693 <tscreen><verb>
694     gtk_container_add (GTK_CONTAINER (window), button);
695 </verb></tscreen>
696
697 Now we have everything set up the way we want it to be. With all the
698 signal handlers in place, and the button placed in the window where it
699 should be, we ask GTK to "show" the widgets on the screen. The window
700 widget is shown last so the whole window will pop up at once rather
701 than seeing the window pop up, and then the button form inside of
702 it. Although with such a simple example, you'd never notice.
703
704 <tscreen><verb>
705     gtk_widget_show (button);
706
707     gtk_widget_show (window);
708 </verb></tscreen>
709
710 And of course, we call gtk_main() which waits for events to come from
711 the X server and will call on the widgets to emit signals when these
712 events come.
713
714 <tscreen><verb>
715     gtk_main ();
716 </verb></tscreen>
717
718 And the final return. Control returns here after gtk_quit() is called.
719
720 <tscreen><verb>
721     return 0;
722 </verb></tscreen>
723
724 Now, when we click the mouse button on a GTK button, the widget emits
725 a "clicked" signal. In order for us to use this information, our
726 program sets up a signal handler to catch that signal, which
727 dispatches the function of our choice. In our example, when the button
728 we created is "clicked", the hello() function is called with a NULL
729 argument, and then the next handler for this signal is called. This
730 calls the gtk_widget_destroy() function, passing it the window widget
731 as its argument, destroying the window widget. This causes the window
732 to emit the "destroy" signal, which is caught, and calls our destroy()
733 callback function, which simply exits GTK.
734
735 Another course of events, is to use the window manager to kill the
736 window. This will cause the "delete_event" to be emitted. This will
737 call our "delete_event" handler. If we return TRUE here, the window
738 will be left as is and nothing will happen. Returning FALSE will cause
739 GTK to emit the "destroy" signal which of course, calls the "destroy"
740 callback, exiting GTK.
741
742 Note that these signals are not the same as the Unix system signals,
743 and are not implemented using them, although the terminology is almost
744 identical.
745
746 <!-- ***************************************************************** -->
747 <sect>Moving On
748 <!-- ***************************************************************** -->
749
750 <!-- ----------------------------------------------------------------- -->
751 <sect1>Data Types
752 <p>
753 There are a few things you probably noticed in the previous examples
754 that need explaining. The gint, gchar etc. that you see are typedefs
755 to int and char respectively. This is done to get around that nasty
756 dependency on the size of simple data types when doing calculations.
757
758 A good example is "gint32" which will be typedef'd to a 32 bit integer
759 for any given platform, whether it be the 64 bit alpha, or the 32 bit
760 i386. The typedefs are very straight forward and intuitive. They are
761 all defined in glib/glib.h (which gets included from gtk.h).
762
763 You'll also notice the ability to use GtkWidget when the function
764 calls for a GtkObject. GTK is an object oriented design, and a widget
765 is an object.
766
767 <!-- ----------------------------------------------------------------- -->
768 <sect1>More on Signal Handlers
769 <p>
770 Lets take another look at the gtk_signal_connect declaration.
771
772 <tscreen><verb>
773 gint gtk_signal_connect( GtkObject *object,
774                          gchar *name,
775                          GtkSignalFunc func,
776                          gpointer func_data );
777 </verb></tscreen>
778
779 Notice the gint return value? This is a tag that identifies your
780 callback function. As stated above, you may have as many callbacks per
781 signal and per object as you need, and each will be executed in turn,
782 in the order they were attached.
783
784 This tag allows you to remove this callback from the list by using:
785
786 <tscreen><verb>
787 void gtk_signal_disconnect( GtkObject *object,
788                             gint id );
789 </verb></tscreen>
790
791 So, by passing in the widget you wish to remove the handler from, and
792 the tag returned by one of the signal_connect functions, you can
793 disconnect a signal handler.
794
795 Another function to remove all the signal handers from an object is:
796
797 <tscreen><verb>
798 void gtk_signal_handlers_destroy( GtkObject *object );
799 </verb></tscreen>
800
801 This call is fairly self explanatory. It simply removes all the
802 current signal handlers from the object passed in as the first
803 argument.
804
805 <!-- ----------------------------------------------------------------- -->
806 <sect1>An Upgraded Hello World
807 <p>
808 Let's take a look at a slightly improved <em>helloworld</em> with
809 better examples of callbacks. This will also introduce us to our next
810 topic, packing widgets.
811
812 <tscreen><verb>
813 /* example-start helloworld2 helloworld2.c */
814
815 #include <gtk/gtk.h>
816
817 /* Our new improved callback.  The data passed to this function
818  * is printed to stdout. */
819 void callback( GtkWidget *widget,
820                gpointer   data )
821 {
822     g_print ("Hello again - %s was pressed\n", (char *) data);
823 }
824
825 /* another callback */
826 void delete_event( GtkWidget *widget,
827                    GdkEvent  *event,
828                    gpointer   data )
829 {
830     gtk_main_quit ();
831 }
832
833 int main( int   argc,
834           char *argv[] )
835 {
836     /* GtkWidget is the storage type for widgets */
837     GtkWidget *window;
838     GtkWidget *button;
839     GtkWidget *box1;
840
841     /* This is called in all GTK applications. Arguments are parsed
842      * from the command line and are returned to the application. */
843     gtk_init (&amp;argc, &amp;argv);
844
845     /* Create a new window */
846     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
847
848     /* This is a new call, this just sets the title of our
849      * new window to "Hello Buttons!" */
850     gtk_window_set_title (GTK_WINDOW (window), "Hello Buttons!");
851
852     /* Here we just set a handler for delete_event that immediately
853      * exits GTK. */
854     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
855                         GTK_SIGNAL_FUNC (delete_event), NULL);
856
857     /* Sets the border width of the window. */
858     gtk_container_border_width (GTK_CONTAINER (window), 10);
859
860     /* We create a box to pack widgets into.  This is described in detail
861      * in the "packing" section. The box is not really visible, it
862      * is just used as a tool to arrange widgets. */
863     box1 = gtk_hbox_new(FALSE, 0);
864
865     /* Put the box into the main window. */
866     gtk_container_add (GTK_CONTAINER (window), box1);
867
868     /* Creates a new button with the label "Button 1". */
869     button = gtk_button_new_with_label ("Button 1");
870
871     /* Now when the button is clicked, we call the "callback" function
872      * with a pointer to "button 1" as its argument */
873     gtk_signal_connect (GTK_OBJECT (button), "clicked",
874                         GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");
875
876     /* Instead of gtk_container_add, we pack this button into the invisible
877      * box, which has been packed into the window. */
878     gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
879
880     /* Always remember this step, this tells GTK that our preparation for
881      * this button is complete, and it can now be displayed. */
882     gtk_widget_show(button);
883
884     /* Do these same steps again to create a second button */
885     button = gtk_button_new_with_label ("Button 2");
886
887     /* Call the same callback function with a different argument,
888      * passing a pointer to "button 2" instead. */
889     gtk_signal_connect (GTK_OBJECT (button), "clicked",
890                         GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");
891
892     gtk_box_pack_start(GTK_BOX(box1), button, TRUE, TRUE, 0);
893
894     /* The order in which we show the buttons is not really important, but I
895      * recommend showing the window last, so it all pops up at once. */
896     gtk_widget_show(button);
897
898     gtk_widget_show(box1);
899
900     gtk_widget_show (window);
901
902     /* Rest in gtk_main and wait for the fun to begin! */
903     gtk_main ();
904
905     return(0);
906 }
907 /* example-end */
908 </verb></tscreen>
909
910 Compile this program using the same linking arguments as our first
911 example.  You'll notice this time there is no easy way to exit the
912 program, you have to use your window manager or command line to kill
913 it. A good exercise for the reader would be to insert a third "Quit"
914 button that will exit the program. You may also wish to play with the
915 options to gtk_box_pack_start() while reading the next section.  Try
916 resizing the window, and observe the behavior.
917
918 Just as a side note, there is another useful define for
919 gtk_window_new() - GTK_WINDOW_DIALOG. This interacts with the window
920 manager a little differently and should be used for transient windows.
921
922 <!-- ***************************************************************** -->
923 <sect>Packing Widgets
924 <!-- ***************************************************************** -->
925 <p>
926 When creating an application, you'll want to put more than one widget
927 inside a window. Our first <em>helloworld</em> example only used one
928 widget so we could simply use a gtk_container_add call to "pack" the
929 widget into the window. But when you want to put more than one widget
930 into a window, how do you control where that widget is positioned?
931 This is where packing comes in.
932
933 <!-- ----------------------------------------------------------------- -->
934 <sect1>Theory of Packing Boxes
935 <p>
936 Most packing is done by creating boxes as in the example above. These
937 are invisible widget containers that we can pack our widgets into
938 which come in two forms, a horizontal box, and a vertical box. When
939 packing widgets into a horizontal box, the objects are inserted
940 horizontally from left to right or right to left depending on the call
941 used. In a vertical box, widgets are packed from top to bottom or vice
942 versa. You may use any combination of boxes inside or beside other
943 boxes to create the desired effect.
944
945 To create a new horizontal box, we use a call to gtk_hbox_new(), and
946 for vertical boxes, gtk_vbox_new().The gtk_box_pack_start() and
947 gtk_box_pack_end() functions are used to place objects inside of these
948 containers. The gtk_box_pack_start() function will start at the top
949 and work its way down in a vbox, and pack left to right in an hbox.
950 gtk_box_pack_end() will do the opposite, packing from bottom to top in
951 a vbox, and right to left in an hbox. Using these functions allow us
952 to right justify or left justify our widgets and may be mixed in any
953 way to achieve the desired effect. We will use gtk_box_pack_start() in
954 most of our examples. An object may be another container or a
955 widget. In fact, many widgets are actually containers themselves,
956 including the button, but we usually only use a label inside a button.
957
958 By using these calls, GTK knows where you want to place your widgets
959 so it can do automatic resizing and other nifty things. There's also a
960 number of options as to how your widgets should be packed. As you can
961 imagine, this method gives us a quite a bit of flexibility when
962 placing and creating widgets.
963
964 <!-- ----------------------------------------------------------------- -->
965 <sect1>Details of Boxes
966 <p>
967 Because of this flexibility, packing boxes in GTK can be confusing at
968 first. There are a lot of options, and it's not immediately obvious how
969 they all fit together. In the end however, there are basically five
970 different styles.
971
972 <? <CENTER> >
973 <?
974 <IMG SRC="gtk_tut_packbox1.gif" VSPACE="15" HSPACE="10" WIDTH="528"
975 HEIGHT="235" ALT="Box Packing Example Image">
976 >
977 <? </CENTER> >
978
979 Each line contains one horizontal box (hbox) with several buttons. The
980 call to gtk_box_pack is shorthand for the call to pack each of the
981 buttons into the hbox. Each of the buttons is packed into the hbox the
982 same way (i.e. same arguments to the gtk_box_pack_start() function).
983
984 This is the declaration of the gtk_box_pack_start function.
985
986 <tscreen><verb>
987 void gtk_box_pack_start( GtkBox    *box,
988                          GtkWidget *child,
989                          gint       expand,
990                          gint       fill,
991                          gint       padding );
992 </verb></tscreen>
993
994 The first argument is the box you are packing the object into, the
995 second is the object. The objects will all be buttons for now, so
996 we'll be packing buttons into boxes.
997
998 The expand argument to gtk_box_pack_start() and gtk_box_pack_end()
999 controls whether the widgets are laid out in the box to fill in all
1000 the extra space in the box so the box is expanded to fill the area
1001 alloted to it (TRUE).  Or the box is shrunk to just fit the widgets
1002 (FALSE). Setting expand to FALSE will allow you to do right and left
1003 justification of your widgets.  Otherwise, they will all expand to fit
1004 into the box, and the same effect could be achieved by using only one
1005 of gtk_box_pack_start or gtk_box_pack_end.
1006
1007 The fill argument to the gtk_box_pack functions control whether the
1008 extra space is allocated to the objects themselves (TRUE), or as extra
1009 padding in the box around these objects (FALSE). It only has an effect
1010 if the expand argument is also TRUE.
1011
1012 When creating a new box, the function looks like this:
1013
1014 <tscreen><verb>
1015 GtkWidget *gtk_hbox_new (gint homogeneous,
1016                          gint spacing);
1017 </verb></tscreen>
1018
1019 The homogeneous argument to gtk_hbox_new (and the same for
1020 gtk_vbox_new) controls whether each object in the box has the same
1021 size (i.e. the same width in an hbox, or the same height in a
1022 vbox). If it is set, the expand argument to the gtk_box_pack routines
1023 is always turned on.
1024
1025 What's the difference between spacing (set when the box is created)
1026 and padding (set when elements are packed)? Spacing is added between
1027 objects, and padding is added on either side of an object. The
1028 following figure should make it clearer:
1029
1030 <? <CENTER> >
1031 <?
1032 <IMG ALIGN="center" SRC="gtk_tut_packbox2.gif" WIDTH="509"
1033 HEIGHT="213" VSPACE="15" HSPACE="10"
1034 ALT="Box Packing Example Image">
1035 >
1036 <? </CENTER> >
1037
1038 Here is the code used to create the above images. I've commented it
1039 fairly heavily so hopefully you won't have any problems following
1040 it. Compile it yourself and play with it.
1041
1042 <!-- ----------------------------------------------------------------- -->
1043 <sect1>Packing Demonstration Program
1044 <p>
1045 <tscreen><verb>
1046 /* example-start packbox packbox.c */
1047
1048 #include <stdio.h>
1049 #include "gtk/gtk.h"
1050
1051 void delete_event( GtkWidget *widget,
1052                    GdkEvent  *event,
1053                    gpointer   data )
1054 {
1055     gtk_main_quit ();
1056 }
1057
1058 /* Make a new hbox filled with button-labels. Arguments for the 
1059  * variables we're interested are passed in to this function. 
1060  * We do not show the box, but do show everything inside. */
1061 GtkWidget *make_box( gint homogeneous,
1062                      gint spacing,
1063                      gint expand,
1064                      gint fill,
1065                      gint padding ) 
1066 {
1067     GtkWidget *box;
1068     GtkWidget *button;
1069     char padstr[80];
1070     
1071     /* Create a new hbox with the appropriate homogeneous
1072      * and spacing settings */
1073     box = gtk_hbox_new (homogeneous, spacing);
1074     
1075     /* Create a series of buttons with the appropriate settings */
1076     button = gtk_button_new_with_label ("gtk_box_pack");
1077     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
1078     gtk_widget_show (button);
1079     
1080     button = gtk_button_new_with_label ("(box,");
1081     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
1082     gtk_widget_show (button);
1083     
1084     button = gtk_button_new_with_label ("button,");
1085     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
1086     gtk_widget_show (button);
1087     
1088     /* Create a button with the label depending on the value of
1089      * expand. */
1090     if (expand == TRUE)
1091             button = gtk_button_new_with_label ("TRUE,");
1092     else
1093             button = gtk_button_new_with_label ("FALSE,");
1094     
1095     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
1096     gtk_widget_show (button);
1097     
1098     /* This is the same as the button creation for "expand"
1099      * above, but uses the shorthand form. */
1100     button = gtk_button_new_with_label (fill ? "TRUE," : "FALSE,");
1101     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
1102     gtk_widget_show (button);
1103     
1104     sprintf (padstr, "%d);", padding);
1105     
1106     button = gtk_button_new_with_label (padstr);
1107     gtk_box_pack_start (GTK_BOX (box), button, expand, fill, padding);
1108     gtk_widget_show (button);
1109     
1110     return box;
1111 }
1112
1113 int main( int   argc,
1114           char *argv[]) 
1115 {
1116     GtkWidget *window;
1117     GtkWidget *button;
1118     GtkWidget *box1;
1119     GtkWidget *box2;
1120     GtkWidget *separator;
1121     GtkWidget *label;
1122     GtkWidget *quitbox;
1123     int which;
1124     
1125     /* Our init, don't forget this! :) */
1126     gtk_init (&amp;argc, &amp;argv);
1127     
1128     if (argc != 2) {
1129         fprintf (stderr, "usage: packbox num, where num is 1, 2, or 3.\n");
1130         /* this just does cleanup in GTK, and exits with an exit status of 1. */
1131         gtk_exit (1);
1132     }
1133     
1134     which = atoi (argv[1]);
1135
1136     /* Create our window */
1137     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1138
1139     /* You should always remember to connect the destroy signal to the
1140      * main window. This is very important for proper intuitive
1141      * behavior */
1142     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
1143                         GTK_SIGNAL_FUNC (delete_event), NULL);
1144     gtk_container_border_width (GTK_CONTAINER (window), 10);
1145     
1146     /* We create a vertical box (vbox) to pack the horizontal boxes into.
1147      * This allows us to stack the horizontal boxes filled with buttons one
1148      * on top of the other in this vbox. */
1149     box1 = gtk_vbox_new (FALSE, 0);
1150     
1151     /* which example to show. These correspond to the pictures above. */
1152     switch (which) {
1153     case 1:
1154         /* create a new label. */
1155         label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
1156         
1157         /* Align the label to the left side.  We'll discuss this function and 
1158          * others in the section on Widget Attributes. */
1159         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1160
1161         /* Pack the label into the vertical box (vbox box1).  Remember that 
1162          * widgets added to a vbox will be packed one on top of the other in
1163          * order. */
1164         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
1165         
1166         /* Show the label */
1167         gtk_widget_show (label);
1168         
1169         /* Call our make box function - homogeneous = FALSE, spacing = 0,
1170          * expand = FALSE, fill = FALSE, padding = 0 */
1171         box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
1172         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1173         gtk_widget_show (box2);
1174
1175         /* Call our make box function - homogeneous = FALSE, spacing = 0,
1176          * expand = FALSE, fill = FALSE, padding = 0 */
1177         box2 = make_box (FALSE, 0, TRUE, FALSE, 0);
1178         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1179         gtk_widget_show (box2);
1180         
1181         /* Args are: homogeneous, spacing, expand, fill, padding */
1182         box2 = make_box (FALSE, 0, TRUE, TRUE, 0);
1183         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1184         gtk_widget_show (box2);
1185         
1186         /* Creates a separator, we'll learn more about these later, 
1187          * but they are quite simple. */
1188         separator = gtk_hseparator_new ();
1189         
1190         /* Cack the separator into the vbox. Remember each of these
1191          * widgets are being packed into a vbox, so they'll be stacked
1192          * vertically. */
1193         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1194         gtk_widget_show (separator);
1195         
1196         /* Create another new label, and show it. */
1197         label = gtk_label_new ("gtk_hbox_new (TRUE, 0);");
1198         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1199         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
1200         gtk_widget_show (label);
1201         
1202         /* Args are: homogeneous, spacing, expand, fill, padding */
1203         box2 = make_box (TRUE, 0, TRUE, FALSE, 0);
1204         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1205         gtk_widget_show (box2);
1206         
1207         /* Args are: homogeneous, spacing, expand, fill, padding */
1208         box2 = make_box (TRUE, 0, TRUE, TRUE, 0);
1209         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1210         gtk_widget_show (box2);
1211         
1212         /* Another new separator. */
1213         separator = gtk_hseparator_new ();
1214         /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1215         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1216         gtk_widget_show (separator);
1217         
1218         break;
1219
1220     case 2:
1221
1222         /* Create a new label, remember box1 is a vbox as created 
1223          * near the beginning of main() */
1224         label = gtk_label_new ("gtk_hbox_new (FALSE, 10);");
1225         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1226         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
1227         gtk_widget_show (label);
1228         
1229         /* Args are: homogeneous, spacing, expand, fill, padding */
1230         box2 = make_box (FALSE, 10, TRUE, FALSE, 0);
1231         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1232         gtk_widget_show (box2);
1233         
1234         /* Args are: homogeneous, spacing, expand, fill, padding */
1235         box2 = make_box (FALSE, 10, TRUE, TRUE, 0);
1236         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1237         gtk_widget_show (box2);
1238         
1239         separator = gtk_hseparator_new ();
1240         /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1241         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1242         gtk_widget_show (separator);
1243         
1244         label = gtk_label_new ("gtk_hbox_new (FALSE, 0);");
1245         gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
1246         gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 0);
1247         gtk_widget_show (label);
1248         
1249         /* Args are: homogeneous, spacing, expand, fill, padding */
1250         box2 = make_box (FALSE, 0, TRUE, FALSE, 10);
1251         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1252         gtk_widget_show (box2);
1253         
1254         /* Args are: homogeneous, spacing, expand, fill, padding */
1255         box2 = make_box (FALSE, 0, TRUE, TRUE, 10);
1256         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1257         gtk_widget_show (box2);
1258         
1259         separator = gtk_hseparator_new ();
1260         /* The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1261         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1262         gtk_widget_show (separator);
1263         break;
1264     
1265     case 3:
1266
1267         /* This demonstrates the ability to use gtk_box_pack_end() to
1268          * right justify widgets. First, we create a new box as before. */
1269         box2 = make_box (FALSE, 0, FALSE, FALSE, 0);
1270
1271         /* Create the label that will be put at the end. */
1272         label = gtk_label_new ("end");
1273         /* Pack it using gtk_box_pack_end(), so it is put on the right
1274          * side of the hbox created in the make_box() call. */
1275         gtk_box_pack_end (GTK_BOX (box2), label, FALSE, FALSE, 0);
1276         /* Show the label. */
1277         gtk_widget_show (label);
1278         
1279         /* Pack box2 into box1 (the vbox remember ? :) */
1280         gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, FALSE, 0);
1281         gtk_widget_show (box2);
1282         
1283         /* A separator for the bottom. */
1284         separator = gtk_hseparator_new ();
1285         /* This explicitly sets the separator to 400 pixels wide by 5 pixels
1286          * high. This is so the hbox we created will also be 400 pixels wide,
1287          * and the "end" label will be separated from the other labels in the
1288          * hbox. Otherwise, all the widgets in the hbox would be packed as
1289          * close together as possible. */
1290         gtk_widget_set_usize (separator, 400, 5);
1291         /* pack the separator into the vbox (box1) created near the start 
1292          * of main() */
1293         gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 5);
1294         gtk_widget_show (separator);    
1295     }
1296     
1297     /* Create another new hbox.. remember we can use as many as we need! */
1298     quitbox = gtk_hbox_new (FALSE, 0);
1299     
1300     /* Our quit button. */
1301     button = gtk_button_new_with_label ("Quit");
1302     
1303     /* Setup the signal to destroy the window. Remember that this will send
1304      * the "destroy" signal to the window which will be caught by our signal
1305      * handler as defined above. */
1306     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1307                                GTK_SIGNAL_FUNC (gtk_main_quit),
1308                                GTK_OBJECT (window));
1309     /* Pack the button into the quitbox.
1310      * The last 3 arguments to gtk_box_pack_start are: expand, fill, padding. */
1311     gtk_box_pack_start (GTK_BOX (quitbox), button, TRUE, FALSE, 0);
1312     /* pack the quitbox into the vbox (box1) */
1313     gtk_box_pack_start (GTK_BOX (box1), quitbox, FALSE, FALSE, 0);
1314     
1315     /* Pack the vbox (box1) which now contains all our widgets, into the
1316      * main window. */
1317     gtk_container_add (GTK_CONTAINER (window), box1);
1318     
1319     /* And show everything left */
1320     gtk_widget_show (button);
1321     gtk_widget_show (quitbox);
1322     
1323     gtk_widget_show (box1);
1324     /* Showing the window last so everything pops up at once. */
1325     gtk_widget_show (window);
1326     
1327     /* And of course, our main function. */
1328     gtk_main ();
1329
1330     /* Control returns here when gtk_main_quit() is called, but not when 
1331      * gtk_exit is used. */
1332     
1333     return(0);
1334 }
1335 /* example-end */
1336 </verb></tscreen>
1337
1338 <!-- ----------------------------------------------------------------- -->
1339 <sect1>Packing Using Tables
1340 <p>
1341 Let's take a look at another way of packing - Tables. These can be
1342 extremely useful in certain situations.
1343
1344 Using tables, we create a grid that we can place widgets in. The
1345 widgets may take up as many spaces as we specify.
1346
1347 The first thing to look at of course, is the gtk_table_new function:
1348
1349 <tscreen><verb>
1350 GtkWidget *gtk_table_new( gint rows,
1351                           gint columns,
1352                           gint homogeneous );
1353 </verb></tscreen>
1354
1355 The first argument is the number of rows to make in the table, while
1356 the second, obviously, is the number of columns.
1357
1358 The homogeneous argument has to do with how the table's boxes are
1359 sized. If homogeneous is TRUE, the table boxes are resized to the size
1360 of the largest widget in the table. If homogeneous is FALSE, the size
1361 of a table boxes is dictated by the tallest widget in its same row,
1362 and the widest widget in its column.
1363
1364 The rows and columns are laid out from 0 to n, where n was the number
1365 specified in the call to gtk_table_new. So, if you specify rows = 2
1366 and columns = 2, the layout would look something like this:
1367
1368 <tscreen><verb>
1369  0          1          2
1370 0+----------+----------+
1371  |          |          |
1372 1+----------+----------+
1373  |          |          |
1374 2+----------+----------+
1375 </verb></tscreen>
1376
1377 Note that the coordinate system starts in the upper left hand corner.
1378 To place a widget into a box, use the following function:
1379
1380 <tscreen><verb>
1381 void gtk_table_attach( GtkTable  *table,
1382                        GtkWidget *child,
1383                        gint       left_attach,
1384                        gint       right_attach,
1385                        gint       top_attach,
1386                        gint       bottom_attach,
1387                        gint       xoptions,
1388                        gint       yoptions,
1389                        gint       xpadding,
1390                        gint       ypadding );
1391 </verb></tscreen>                                      
1392
1393 Where the first argument ("table") is the table you've created and the
1394 second ("child") the widget you wish to place in the table.
1395
1396 The left and right attach arguments specify where to place the widget,
1397 and how many boxes to use. If you want a button in the lower right
1398 table entry of our 2x2 table, and want it to fill that entry ONLY,
1399 left_attach would be = 1, right_attach = 2, top_attach = 1,
1400 bottom_attach = 2.
1401
1402 Now, if you wanted a widget to take up the whole top row of our 2x2
1403 table, you'd use left_attach = 0, right_attach = 2, top_attach = 0,
1404 bottom_attach = 1.
1405
1406 The xoptions and yoptions are used to specify packing options and may
1407 be bitwise OR'ed together to allow multiple options.
1408
1409 These options are:
1410 <itemize>
1411 <item>GTK_FILL - If the table box is larger than the widget, and
1412 GTK_FILL is specified, the widget will expand to use all the room
1413 available.
1414
1415 <item>GTK_SHRINK - If the table widget was allocated less space then
1416 was requested (usually by the user resizing the window), then the
1417 widgets would normally just be pushed off the bottom of the window and
1418 disappear. If GTK_SHRINK is specified, the widgets will shrink with
1419 the table.
1420
1421 <item>GTK_EXPAND - This will cause the table to expand to use up any
1422 remaining space in the window.
1423 </itemize>
1424
1425 Padding is just like in boxes, creating a clear area around the widget
1426 specified in pixels.
1427
1428 gtk_table_attach() has a LOT of options.  So, there's a shortcut:
1429
1430 <tscreen><verb>
1431 void gtk_table_attach_defaults( GtkTable  *table,
1432                                 GtkWidget *widget,
1433                                 gint       left_attach,
1434                                 gint       right_attach,
1435                                 gint       top_attach,
1436                                 gint       bottom_attach );
1437 </verb></tscreen>
1438
1439 The X and Y options default to GTK_FILL | GTK_EXPAND, and X and Y
1440 padding are set to 0. The rest of the arguments are identical to the
1441 previous function.
1442
1443 We also have gtk_table_set_row_spacing() and
1444 gtk_table_set_col_spacing().  This places spacing between the rows at
1445 the specified row or column.
1446
1447 <tscreen><verb>
1448 void gtk_table_set_row_spacing( GtkTable *table,
1449                                 gint      row,
1450                                 gint      spacing );
1451 </verb></tscreen>
1452
1453 and
1454
1455 <tscreen><verb>
1456 void gtk_table_set_col_spacing ( GtkTable *table,
1457                                  gint      column,
1458                                  gint      spacing );
1459 </verb></tscreen>
1460
1461 Note that for columns, the space goes to the right of the column, and
1462 for rows, the space goes below the row.
1463
1464 You can also set a consistent spacing of all rows and/or columns with:
1465
1466 <tscreen><verb>
1467 void gtk_table_set_row_spacings( GtkTable *table,
1468                                  gint      spacing );
1469 </verb></tscreen>
1470
1471 And,
1472
1473 <tscreen><verb>
1474 void gtk_table_set_col_spacings( GtkTable *table,
1475                                  gint      spacing );
1476 </verb></tscreen>
1477
1478 Note that with these calls, the last row and last column do not get
1479 any spacing.
1480
1481 <!-- ----------------------------------------------------------------- -->
1482 <sect1>Table Packing Example
1483 <p>
1484 Here we make a window with three buttons in a 2x2 table.
1485 The first two buttons will be placed in the upper row.
1486 A third, quit button, is placed in the lower row, spanning both columns.
1487 Which means it should look something like this:
1488
1489 <? <CENTER> >
1490 <?
1491 <IMG SRC="gtk_tut_table.gif" VSPACE="15" HSPACE="10" 
1492 ALT="Table Packing Example Image" WIDTH="180" HEIGHT="120">
1493 >
1494 <? </CENTER> >
1495
1496 Here's the source code:
1497
1498 <tscreen><verb>
1499 /* example-start table table.c */
1500
1501 #include <gtk/gtk.h>
1502
1503 /* Our callback.
1504  * The data passed to this function is printed to stdout */
1505 void callback( GtkWidget *widget,
1506                gpointer   data )
1507 {
1508     g_print ("Hello again - %s was pressed\n", (char *) data);
1509 }
1510
1511 /* This callback quits the program */
1512 void delete_event( GtkWidget *widget,
1513                    GdkEvent  *event,
1514                    gpointer   data )
1515 {
1516     gtk_main_quit ();
1517 }
1518
1519 int main( int   argc,
1520           char *argv[] )
1521 {
1522     GtkWidget *window;
1523     GtkWidget *button;
1524     GtkWidget *table;
1525
1526     gtk_init (&amp;argc, &amp;argv);
1527
1528     /* Create a new window */
1529     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1530
1531     /* Set the window title */
1532     gtk_window_set_title (GTK_WINDOW (window), "Table");
1533
1534     /* Set a handler for delete_event that immediately
1535      * exits GTK. */
1536     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
1537                         GTK_SIGNAL_FUNC (delete_event), NULL);
1538
1539     /* Sets the border width of the window. */
1540     gtk_container_border_width (GTK_CONTAINER (window), 20);
1541
1542     /* Create a 2x2 table */
1543     table = gtk_table_new (2, 2, TRUE);
1544
1545     /* Put the table in the main window */
1546     gtk_container_add (GTK_CONTAINER (window), table);
1547
1548     /* Create first button */
1549     button = gtk_button_new_with_label ("button 1");
1550
1551     /* When the button is clicked, we call the "callback" function
1552      * with a pointer to "button 1" as its argument */
1553     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1554               GTK_SIGNAL_FUNC (callback), (gpointer) "button 1");
1555
1556
1557     /* Insert button 1 into the upper left quadrant of the table */
1558     gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 1, 0, 1);
1559
1560     gtk_widget_show (button);
1561
1562     /* Create second button */
1563
1564     button = gtk_button_new_with_label ("button 2");
1565
1566     /* When the button is clicked, we call the "callback" function
1567      * with a pointer to "button 2" as its argument */
1568     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1569               GTK_SIGNAL_FUNC (callback), (gpointer) "button 2");
1570     /* Insert button 2 into the upper right quadrant of the table */
1571     gtk_table_attach_defaults (GTK_TABLE(table), button, 1, 2, 0, 1);
1572
1573     gtk_widget_show (button);
1574
1575     /* Create "Quit" button */
1576     button = gtk_button_new_with_label ("Quit");
1577
1578     /* When the button is clicked, we call the "delete_event" function
1579      * and the program exits */
1580     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1581                         GTK_SIGNAL_FUNC (delete_event), NULL);
1582
1583     /* Insert the quit button into the both 
1584      * lower quadrants of the table */
1585     gtk_table_attach_defaults (GTK_TABLE(table), button, 0, 2, 1, 2);
1586
1587     gtk_widget_show (button);
1588
1589     gtk_widget_show (table);
1590     gtk_widget_show (window);
1591
1592     gtk_main ();
1593
1594     return 0;
1595 }
1596 /* example-end */
1597 </verb></tscreen>
1598
1599 <!-- ***************************************************************** -->
1600 <sect>Widget Overview
1601 <!-- ***************************************************************** -->
1602 <p>
1603 The general steps to creating a widget in GTK are:
1604 <enum>
1605 <item> gtk_*_new - one of various functions to create a new widget.
1606 These are all detailed in this section.
1607
1608 <item> Connect all signals and events we wish to use to the
1609 appropriate handlers.
1610
1611 <item> Set the attributes of the widget.
1612
1613 <item> Pack the widget into a container using the appropriate call
1614 such as gtk_container_add() or gtk_box_pack_start().
1615
1616 <item> gtk_widget_show() the widget.
1617 </enum>
1618
1619 gtk_widget_show() lets GTK know that we are done setting the
1620 attributes of the widget, and it is ready to be displayed. You may
1621 also use gtk_widget_hide to make it disappear again. The order in
1622 which you show the widgets is not important, but I suggest showing the
1623 window last so the whole window pops up at once rather than seeing the
1624 individual widgets come up on the screen as they're formed. The
1625 children of a widget (a window is a widget too) will not be displayed
1626 until the window itself is shown using the gtk_widget_show() function.
1627
1628 <!-- ----------------------------------------------------------------- -->
1629 <sect1> Casting
1630 <p>
1631 You'll notice as you go on, that GTK uses a type casting system. This
1632 is always done using macros that both test the ability to cast the
1633 given item, and perform the cast. Some common ones you will see are:
1634
1635 <itemize>
1636 <item> GTK_WIDGET(widget)
1637 <item> GTK_OBJECT(object)
1638 <item> GTK_SIGNAL_FUNC(function)
1639 <item> GTK_CONTAINER(container)
1640 <item> GTK_WINDOW(window)
1641 <item> GTK_BOX(box)
1642 </itemize>
1643
1644 These are all used to cast arguments in functions. You'll see them in the
1645 examples, and can usually tell when to use them simply by looking at the
1646 function's declaration.
1647
1648 As you can see below in the class hierarchy, all GtkWidgets are
1649 derived from the GtkObject base class. This means you can use a widget
1650 in any place the function asks for an object - simply use the
1651 GTK_OBJECT() macro.
1652
1653 For example:
1654
1655 <tscreen><verb>
1656 gtk_signal_connect( GTK_OBJECT(button), "clicked",
1657                     GTK_SIGNAL_FUNC(callback_function), callback_data);
1658 </verb></tscreen> 
1659
1660 This casts the button into an object, and provides a cast for the
1661 function pointer to the callback.
1662
1663 Many widgets are also containers. If you look in the class hierarchy
1664 below, you'll notice that many widgets derive from the GtkContainer
1665 class. Any one of these widgets may be used with the GTK_CONTAINER
1666 macro to pass them to functions that ask for containers.
1667
1668 Unfortunately, these macros are not extensively covered in the
1669 tutorial, but I recommend taking a look through the GTK header
1670 files. It can be very educational. In fact, it's not difficult to
1671 learn how a widget works just by looking at the function declarations.
1672
1673 <!-- ----------------------------------------------------------------- -->
1674 <sect1>Widget Hierarchy
1675 <p>
1676 For your reference, here is the class hierarchy tree used to implement widgets.
1677
1678 <tscreen><verb>
1679  GtkObject
1680   +GtkWidget
1681   | +GtkMisc
1682   | | +GtkLabel
1683   | | | +GtkAccelLabel
1684   | | | `GtkTipsQuery
1685   | | +GtkArrow
1686   | | +GtkImage
1687   | | `GtkPixmap
1688   | +GtkContainer
1689   | | +GtkBin
1690   | | | +GtkAlignment
1691   | | | +GtkFrame
1692   | | | | `GtkAspectFrame
1693   | | | +GtkButton
1694   | | | | +GtkToggleButton
1695   | | | | | `GtkCheckButton
1696   | | | | |   `GtkRadioButton
1697   | | | | `GtkOptionMenu
1698   | | | +GtkItem
1699   | | | | +GtkMenuItem
1700   | | | | | +GtkCheckMenuItem
1701   | | | | | | `GtkRadioMenuItem
1702   | | | | | `GtkTearoffMenuItem
1703   | | | | +GtkListItem
1704   | | | | `GtkTreeItem
1705   | | | +GtkWindow
1706   | | | | +GtkColorSelectionDialog
1707   | | | | +GtkDialog
1708   | | | | | `GtkInputDialog
1709   | | | | +GtkDrawWindow
1710   | | | | +GtkFileSelection
1711   | | | | +GtkFontSelectionDialog
1712   | | | | `GtkPlug
1713   | | | +GtkEventBox
1714   | | | +GtkHandleBox
1715   | | | +GtkScrolledWindow
1716   | | | `GtkViewport
1717   | | +GtkBox
1718   | | | +GtkButtonBox
1719   | | | | +GtkHButtonBox
1720   | | | | `GtkVButtonBox
1721   | | | +GtkVBox
1722   | | | | +GtkColorSelection
1723   | | | | `GtkGammaCurve
1724   | | | `GtkHBox
1725   | | |   +GtkCombo
1726   | | |   `GtkStatusbar
1727   | | +GtkCList
1728   | | | `GtkCTree
1729   | | +GtkFixed
1730   | | +GtkNotebook
1731   | | | `GtkFontSelection
1732   | | +GtkPaned
1733   | | | +GtkHPaned
1734   | | | `GtkVPaned
1735   | | +GtkLayout
1736   | | +GtkList
1737   | | +GtkMenuShell
1738   | | | +GtkMenuBar
1739   | | | `GtkMenu
1740   | | +GtkPacker
1741   | | +GtkSocket
1742   | | +GtkTable
1743   | | +GtkToolbar
1744   | | `GtkTree
1745   | +GtkCalendar
1746   | +GtkDrawingArea
1747   | | `GtkCurve
1748   | +GtkEditable
1749   | | +GtkEntry
1750   | | | `GtkSpinButton
1751   | | `GtkText
1752   | +GtkRuler
1753   | | +GtkHRuler
1754   | | `GtkVRuler
1755   | +GtkRange
1756   | | +GtkScale
1757   | | | +GtkHScale
1758   | | | `GtkVScale
1759   | | `GtkScrollbar
1760   | |   +GtkHScrollbar
1761   | |   `GtkVScrollbar
1762   | +GtkSeparator
1763   | | +GtkHSeparator
1764   | | `GtkVSeparator
1765   | +GtkPreview
1766   | `GtkProgress
1767   |   `GtkProgressBar
1768   +GtkData
1769   | +GtkAdjustment
1770   | `GtkTooltips
1771   `GtkItemFactory
1772 </verb></tscreen>
1773
1774 <!-- ----------------------------------------------------------------- -->
1775 <sect1>Widgets Without Windows
1776 <p>
1777 The following widgets do not have an associated window. If you want to
1778 capture events, you'll have to use the GtkEventBox. See the section on
1779 the <ref id="sec_EventBox" name="EventBox"> widget.
1780
1781 <tscreen><verb>
1782 GtkAlignment
1783 GtkArrow
1784 GtkBin
1785 GtkBox
1786 GtkImage
1787 GtkItem
1788 GtkLabel
1789 GtkPixmap
1790 GtkScrolledWindow
1791 GtkSeparator
1792 GtkTable
1793 GtkAspectFrame
1794 GtkFrame
1795 GtkVBox
1796 GtkHBox
1797 GtkVSeparator
1798 GtkHSeparator
1799 </verb></tscreen>
1800
1801 We'll further our exploration of GTK by examining each widget in turn,
1802 creating a few simple functions to display them. Another good source
1803 is the testgtk.c program that comes with GTK. It can be found in
1804 gtk/testgtk.c.
1805
1806 <!-- ***************************************************************** -->
1807 <sect>The Button Widget
1808 <!-- ***************************************************************** -->
1809
1810 <!-- ----------------------------------------------------------------- -->
1811 <sect1>Normal Buttons
1812 <p>
1813 We've almost seen all there is to see of the button widget. It's
1814 pretty simple. There are however two ways to create a button. You can
1815 use the gtk_button_new_with_label() to create a button with a label,
1816 or use gtk_button_new() to create a blank button. It's then up to you
1817 to pack a label or pixmap into this new button. To do this, create a
1818 new box, and then pack your objects into this box using the usual
1819 gtk_box_pack_start, and then use gtk_container_add to pack the box
1820 into the button.
1821
1822 Here's an example of using gtk_button_new to create a button with a
1823 picture and a label in it. I've broken up the code to create a box
1824 from the rest so you can use it in your programs. There are further
1825 examples of using pixmaps later in the tutorial.
1826
1827 <tscreen><verb>
1828 /* example-start buttons buttons.c */
1829
1830 #include <gtk/gtk.h>
1831
1832 /* Create a new hbox with an image and a label packed into it
1833  * and return the box. */
1834
1835 GtkWidget *xpm_label_box( GtkWidget *parent,
1836                           gchar     *xpm_filename,
1837                           gchar     *label_text )
1838 {
1839     GtkWidget *box1;
1840     GtkWidget *label;
1841     GtkWidget *pixmapwid;
1842     GdkPixmap *pixmap;
1843     GdkBitmap *mask;
1844     GtkStyle *style;
1845
1846     /* Create box for xpm and label */
1847     box1 = gtk_hbox_new (FALSE, 0);
1848     gtk_container_border_width (GTK_CONTAINER (box1), 2);
1849
1850     /* Get the style of the button to get the
1851      * background color. */
1852     style = gtk_widget_get_style(parent);
1853
1854     /* Now on to the xpm stuff */
1855     pixmap = gdk_pixmap_create_from_xpm (parent->window, &amp;mask,
1856                                          &amp;style->bg[GTK_STATE_NORMAL],
1857                                          xpm_filename);
1858     pixmapwid = gtk_pixmap_new (pixmap, mask);
1859
1860     /* Create a label for the button */
1861     label = gtk_label_new (label_text);
1862
1863     /* Pack the pixmap and label into the box */
1864     gtk_box_pack_start (GTK_BOX (box1),
1865                         pixmapwid, FALSE, FALSE, 3);
1866
1867     gtk_box_pack_start (GTK_BOX (box1), label, FALSE, FALSE, 3);
1868
1869     gtk_widget_show(pixmapwid);
1870     gtk_widget_show(label);
1871
1872     return(box1);
1873 }
1874
1875 /* Our usual callback function */
1876 void callback( GtkWidget *widget,
1877                gpointer   data )
1878 {
1879     g_print ("Hello again - %s was pressed\n", (char *) data);
1880 }
1881
1882
1883 int main( int   argc,
1884           char *argv[] )
1885 {
1886     /* GtkWidget is the storage type for widgets */
1887     GtkWidget *window;
1888     GtkWidget *button;
1889     GtkWidget *box1;
1890
1891     gtk_init (&amp;argc, &amp;argv);
1892
1893     /* Create a new window */
1894     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1895
1896     gtk_window_set_title (GTK_WINDOW (window), "Pixmap'd Buttons!");
1897
1898     /* It's a good idea to do this for all windows. */
1899     gtk_signal_connect (GTK_OBJECT (window), "destroy",
1900                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
1901
1902     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
1903                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
1904
1905
1906     /* Sets the border width of the window. */
1907     gtk_container_border_width (GTK_CONTAINER (window), 10);
1908     gtk_widget_realize(window);
1909
1910     /* Create a new button */
1911     button = gtk_button_new ();
1912
1913     /* Connect the "clicked" signal of the button to our callback */
1914     gtk_signal_connect (GTK_OBJECT (button), "clicked",
1915                         GTK_SIGNAL_FUNC (callback), (gpointer) "cool button");
1916
1917     /* This calls our box creating function */
1918     box1 = xpm_label_box(window, "info.xpm", "cool button");
1919
1920     /* Pack and show all our widgets */
1921     gtk_widget_show(box1);
1922
1923     gtk_container_add (GTK_CONTAINER (button), box1);
1924
1925     gtk_widget_show(button);
1926
1927     gtk_container_add (GTK_CONTAINER (window), button);
1928
1929     gtk_widget_show (window);
1930
1931     /* Rest in gtk_main and wait for the fun to begin! */
1932     gtk_main ();
1933
1934     return(0);
1935 }
1936 /* example-end */
1937 </verb></tscreen>
1938
1939 The xpm_label_box function could be used to pack xpm's and labels into
1940 any widget that can be a container.
1941
1942 The Button widget has the following signals:
1943
1944 <itemize>
1945 <item> pressed
1946 <item> released
1947 <item> clicked
1948 <item> enter
1949 <item> leave
1950 </itemize>
1951
1952 <!-- ----------------------------------------------------------------- -->
1953 <sect1> Toggle Buttons
1954 <p>
1955 Toggle buttons are derived from normal buttons and are very similar,
1956 except they will always be in one of two states, alternated by a
1957 click. They may be depressed, and when you click again, they will pop
1958 back up. Click again, and they will pop back down.
1959
1960 Toggle buttons are the basis for check buttons and radio buttons, as
1961 such, many of the calls used for toggle buttons are inherited by radio
1962 and check buttons. I will point these out when we come to them.
1963
1964 Creating a new toggle button:
1965
1966 <tscreen><verb>
1967 GtkWidget *gtk_toggle_button_new( void );
1968
1969 GtkWidget *gtk_toggle_button_new_with_label( gchar *label );
1970 </verb></tscreen>
1971
1972 As you can imagine, these work identically to the normal button widget
1973 calls. The first creates a blank toggle button, and the second, a
1974 button with a label widget already packed into it.
1975
1976 To retrieve the state of the toggle widget, including radio and check
1977 buttons, we use a GTK macro as shown in our example below. This tests
1978 the state of the toggle in a callback. The signal of interest emitted
1979 to us by toggle buttons (the toggle button, check button, and radio
1980 button widgets), is the "toggled" signal. To check the state of these
1981 buttons, set up a signal handler to catch the toggled signal, and use
1982 the macro to determine its state. The callback will look something
1983 like:
1984
1985 <tscreen><verb>
1986 void toggle_button_callback (GtkWidget *widget, gpointer data)
1987 {
1988     if (GTK_TOGGLE_BUTTON (widget)->active) 
1989     {
1990         /* If control reaches here, the toggle button is down */
1991     
1992     } else {
1993     
1994         /* If control reaches here, the toggle button is up */
1995     }
1996 }
1997 </verb></tscreen>
1998
1999 <tscreen><verb>
2000 void gtk_toggle_button_set_state( GtkToggleButton *toggle_button,
2001                                   gint             state );
2002 </verb></tscreen>
2003
2004 The above call can be used to set the state of the toggle button, and
2005 its children the radio and check buttons. Passing in your created
2006 button as the first argument, and a TRUE or FALSE for the second state
2007 argument to specify whether it should be down (depressed) or up
2008 (released). Default is up, or FALSE.
2009
2010 Note that when you use the gtk_toggle_button_set_state() function, and
2011 the state is actually changed, it causes the "clicked" signal to be
2012 emitted from the button.
2013
2014 <tscreen><verb>
2015 void gtk_toggle_button_toggled (GtkToggleButton *toggle_button);
2016 </verb></tscreen>
2017
2018 This simply toggles the button, and emits the "toggled" signal.
2019
2020 <!-- ----------------------------------------------------------------- -->
2021 <sect1> Check Buttons
2022 <p>
2023 Check buttons inherent many properties and functions from the the
2024 toggle buttons above, but look a little different. Rather than being
2025 buttons with text inside them, they are small squares with the text to
2026 the right of them. These are often used for toggling options on and
2027 off in applications.
2028
2029 The two creation functions are similar to those of the normal button.
2030
2031 <tscreen><verb>
2032 GtkWidget *gtk_check_button_new( void );
2033
2034 GtkWidget *gtk_check_button_new_with_label ( gchar *label );
2035 </verb></tscreen>
2036
2037 The new_with_label function creates a check button with a label beside
2038 it.
2039
2040 Checking the state of the check button is identical to that of the
2041 toggle button.
2042
2043 <!-- ----------------------------------------------------------------- -->
2044 <sect1> Radio Buttons <label id="sec_Radio_Buttons">
2045 <p>
2046 Radio buttons are similar to check buttons except they are grouped so
2047 that only one may be selected/depressed at a time. This is good for
2048 places in your application where you need to select from a short list
2049 of options.
2050
2051 Creating a new radio button is done with one of these calls:
2052
2053 <tscreen><verb>
2054 GtkWidget *gtk_radio_button_new( GSList *group );
2055
2056 GtkWidget *gtk_radio_button_new_with_label( GSList *group,
2057                                             gchar  *label );
2058 </verb></tscreen>
2059
2060 You'll notice the extra argument to these calls. They require a group
2061 to perform their duty properly. The first call to
2062 gtk_radio_button_new_with_label or gtk_radio_button_new_with_label
2063 should pass NULL as the first argument. Then create a group using:
2064
2065 <tscreen><verb>
2066 GSList *gtk_radio_button_group( GtkRadioButton *radio_button );
2067 </verb></tscreen>
2068
2069 The important thing to remember is that gtk_radio_button_group must be
2070 called for each new button added to the group, with the previous
2071 button passed in as an argument. The result is then passed into the
2072 call to gtk_radio_button_new or gtk_radio_button_new_with_label. This
2073 allows a chain of buttons to be established. The example below should
2074 make this clear.
2075
2076 You can shorten this slightly by using the following syntax, which
2077 removes the need for a variable to hold the list of buttons. This form
2078 is used in the example to create the third button:
2079
2080 <tscreen><verb>
2081      button2 = gtk_radio_button_new_with_label(
2082                  gtk_radio_button_group (GTK_RADIO_BUTTON (button1)),
2083                  "button2");
2084 </verb></tscreen>
2085
2086 It is also a good idea to explicitly set which button should be the
2087 default depressed button with:
2088
2089 <tscreen><verb>
2090 void gtk_toggle_button_set_state( GtkToggleButton *toggle_button,
2091                                   gint             state );
2092 </verb></tscreen>
2093
2094 This is described in the section on toggle buttons, and works in
2095 exactly the same way.
2096
2097 The following example creates a radio button group with three buttons.
2098
2099 <tscreen><verb>
2100 /* example-start radiobuttons radiobuttons.c */
2101
2102 #include <gtk/gtk.h>
2103 #include <glib.h>
2104
2105 void close_application( GtkWidget *widget,
2106                         GdkEvent  *event,
2107                         gpointer   data )
2108 {
2109   gtk_main_quit();
2110 }
2111
2112 int main( int   argc,
2113           char *argv[] )
2114 {
2115     GtkWidget *window = NULL;
2116     GtkWidget *box1;
2117     GtkWidget *box2;
2118     GtkWidget *button;
2119     GtkWidget *separator;
2120     GSList *group;
2121   
2122     gtk_init(&amp;argc,&amp;argv);    
2123       
2124     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2125   
2126     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
2127                         GTK_SIGNAL_FUNC(close_application),
2128                         NULL);
2129
2130     gtk_window_set_title (GTK_WINDOW (window), "radio buttons");
2131     gtk_container_border_width (GTK_CONTAINER (window), 0);
2132
2133     box1 = gtk_vbox_new (FALSE, 0);
2134     gtk_container_add (GTK_CONTAINER (window), box1);
2135     gtk_widget_show (box1);
2136
2137     box2 = gtk_vbox_new (FALSE, 10);
2138     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2139     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2140     gtk_widget_show (box2);
2141
2142     button = gtk_radio_button_new_with_label (NULL, "button1");
2143     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2144     gtk_widget_show (button);
2145
2146     group = gtk_radio_button_group (GTK_RADIO_BUTTON (button));
2147     button = gtk_radio_button_new_with_label(group, "button2");
2148     gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (button), TRUE);
2149     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2150     gtk_widget_show (button);
2151
2152     button = gtk_radio_button_new_with_label(
2153                  gtk_radio_button_group (GTK_RADIO_BUTTON (button)),
2154                  "button3");
2155     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2156     gtk_widget_show (button);
2157
2158     separator = gtk_hseparator_new ();
2159     gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2160     gtk_widget_show (separator);
2161
2162     box2 = gtk_vbox_new (FALSE, 10);
2163     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2164     gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2165     gtk_widget_show (box2);
2166
2167     button = gtk_button_new_with_label ("close");
2168     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2169                                GTK_SIGNAL_FUNC(close_application),
2170                                GTK_OBJECT (window));
2171     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2172     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2173     gtk_widget_grab_default (button);
2174     gtk_widget_show (button);
2175     gtk_widget_show (window);
2176      
2177     gtk_main();
2178
2179     return(0);
2180 }
2181 /* example-end */
2182 </verb></tscreen>
2183
2184 <!-- TODO: check out gtk_radio_button_new_from_widget function - TRG -->
2185
2186 <!-- ***************************************************************** -->
2187 <sect> Adjustments <label id="sec_Adjustment">
2188 <!-- ***************************************************************** -->
2189 <p>
2190 GTK+ has various widgets that can be visually adjusted by the user
2191 using the mouse or the keyboard, such as the range widgets, described
2192 in the <ref id="sec_Range_Widgets" name="Range Widgets">
2193 section. There are also a few widgets that display some adjustable
2194 portion of a larger area of data, such as the text widget and the
2195 viewport widget.
2196
2197 Obviously, an application needs to be able to react to changes the
2198 user makes in range widgets. One way to do this would be to have each
2199 widget emit its own type of signal when its adjustment changes, and
2200 either pass the new value to the signal handler, or require it to look
2201 inside the widget's data structure in order to ascertain the value.
2202 But you may also want to connect the adjustments of several widgets
2203 together, so that adjusting one adjusts the others. The most obvious
2204 example of this is connecting a scrollbar to a panning viewport or a
2205 scrolling text area. If each widget has its own way of setting or
2206 getting the adjustment value, then the programmer may have to write
2207 their own signal handlers to translate between the output of one
2208 widget's signal and the "input" of another's adjustment setting
2209 function.
2210
2211 GTK+ solves this problem using the GtkAdjustment object, which is a
2212 way for widgets to store and pass adjustment information in an
2213 abstract and flexible form. The most obvious use of GtkAdjustment is
2214 to store the configuration parameters and values of range widgets,
2215 such as scrollbars and scale controls. However, since GtkAdjustments
2216 are derived from GtkObject, they have some special powers beyond those
2217 of normal data structures. Most importantly, they can emit signals,
2218 just like widgets, and these signals can be used not only to allow
2219 your program to react to user input on adjustable widgets, but also to
2220 propagate adjustment values transparently between adjustable widgets.
2221
2222 <sect1> Creating an Adjustment
2223 <p>
2224 You create an adjustment using:
2225
2226 <tscreen><verb>
2227 GtkObject *gtk_adjustment_new( gfloat value,
2228                                gfloat lower,
2229                                gfloat upper,
2230                                gfloat step_increment,
2231                                gfloat page_increment,
2232                                gfloat page_size );
2233 </verb></tscreen>
2234
2235 The <tt/value/ argument is the initial value you want to give to the
2236 adjustment, usually corresponding to the topmost or leftmost position
2237 of an adjustable widget. The <tt/lower/ argument specifies the lowest
2238 value which the adjustment can hold. The <tt/step_increment/ argument
2239 specifies the "smaller" of the two increments by which the user can
2240 change the value, while the <tt/page_increment/ is the "larger" one.
2241 The <tt/page_size/ argument usually corresponds somehow to the visible
2242 area of a panning widget. The <tt/upper/ argument is used to represent
2243 the bottom most or right most coordinate in a panning widget's
2244 child. Therefore it is <em/not/ always the largest number that
2245 <tt/value/ can take, since the <tt/page_size/ of such widgets is
2246 usually non-zero.
2247
2248 <!-- ----------------------------------------------------------------- -->
2249 <sect1> Using Adjustments the Easy Way
2250 <p>
2251 The adjustable widgets can be roughly divided into those which use and
2252 require specific units for these values and those which treat them as
2253 arbitrary numbers. The group which treats the values as arbitrary
2254 numbers includes the range widgets (scrollbars and scales, the
2255 progress bar widget, and the spin button widget). These widgets are
2256 all the widgets which are typically "adjusted" directly by the user
2257 with the mouse or keyboard. They will treat the <tt/lower/ and
2258 <tt/upper/ values of an adjustment as a range within which the user
2259 can manipulate the adjustment's <tt/value/. By default, they will only
2260 modify the <tt/value/ of an adjustment.
2261
2262 The other group includes the text widget, the viewport widget, the
2263 compound list widget, and the scrolled window widget. All of these
2264 widgets use pixel values for their adjustments. These are also all
2265 widgets which are typically "adjusted" indirectly using scrollbars.
2266 While all widgets which use adjustments can either create their own
2267 adjustments or use ones you supply, you'll generally want to let this
2268 particular category of widgets create its own adjustments. Usually,
2269 they will eventually override all the values except the <tt/value/
2270 itself in whatever adjustments you give them, but the results are, in
2271 general, undefined (meaning, you'll have to read the source code to
2272 find out, and it may be different from widget to widget).
2273
2274 Now, you're probably thinking, since text widgets and viewports insist
2275 on setting everything except the <tt/value/ of their adjustments,
2276 while scrollbars will <em/only/ touch the adjustment's <tt/value/, if
2277 you <em/share/ an adjustment object between a scrollbar and a text
2278 widget, manipulating the scrollbar will automagically adjust the text
2279 widget?  Of course it will! Just like this:
2280
2281 <tscreen><verb>
2282   /* creates its own adjustments */
2283   text = gtk_text_new (NULL, NULL);
2284   /* uses the newly-created adjustment for the scrollbar as well */
2285   vscrollbar = gtk_vscrollbar_new (GTK_TEXT(text)->vadj);
2286 </verb></tscreen>
2287
2288 </sect1>
2289 <!-- ----------------------------------------------------------------- -->
2290 <sect1> Adjustment Internals
2291 <p>
2292 Ok, you say, that's nice, but what if I want to create my own handlers
2293 to respond when the user adjusts a range widget or a spin button, and
2294 how do I get at the value of the adjustment in these handlers?  To
2295 answer these questions and more, let's start by taking a look at
2296 <tt>struct _GtkAdjustment</tt> itself:
2297
2298 <tscreen><verb>
2299 struct _GtkAdjustment
2300 {
2301   GtkData data;
2302   
2303   gfloat lower;
2304   gfloat upper;
2305   gfloat value;
2306   gfloat step_increment;
2307   gfloat page_increment;
2308   gfloat page_size;
2309 };     
2310 </verb></tscreen>
2311
2312 The first thing you should know is that there aren't any handy-dandy
2313 macros or accessor functions for getting the <tt/value/ out of a
2314 GtkAdjustment, so you'll have to (horror of horrors) do it like a
2315 <em/real/ C programmer.  Don't worry - the <tt>GTK_ADJUSTMENT
2316 (Object)</tt> macro does run-time type checking (as do all the GTK+
2317 type-casting macros, actually).
2318
2319 Since, when you set the <tt/value/ of an adjustment, you generally
2320 want the change to be reflected by every widget that uses this
2321 adjustment, GTK+ provides this convenience function to do this:
2322
2323 <tscreen><verb>
2324 void gtk_adjustment_set_value( GtkAdjustment *adjustment,
2325                                gfloat         value );
2326 </verb></tscreen>
2327
2328 As mentioned earlier, GtkAdjustment is a subclass of GtkObject just
2329 like all the various widgets, and thus it is able to emit signals.
2330 This is, of course, why updates happen automagically when you share an
2331 adjustment object between a scrollbar and another adjustable widget;
2332 all adjustable widgets connect signal handlers to their adjustment's
2333 <tt/value_changed/ signal, as can your program. Here's the definition
2334 of this signal in <tt/struct _GtkAdjustmentClass/:
2335
2336 <tscreen><verb>
2337   void (* value_changed) (GtkAdjustment *adjustment);
2338 </verb></tscreen>
2339
2340 The various widgets that use the GtkAdjustment object will emit this
2341 signal on an adjustment whenever they change its value. This happens
2342 both when user input causes the slider to move on a range widget, as
2343 well as when the program explicitly changes the value with
2344 <tt/gtk_adjustment_set_value()/. So, for example, if you have a scale
2345 widget, and you want to change the rotation of a picture whenever its
2346 value changes, you would create a callback like this:
2347
2348 <tscreen><verb>
2349 void cb_rotate_picture (GtkAdjustment *adj, GtkWidget *picture)
2350 {
2351   set_picture_rotation (picture, adj->value);
2352 ...
2353 </verb></tscreen>
2354
2355 and connect it to the scale widget's adjustment like this:
2356
2357 <tscreen><verb>
2358 gtk_signal_connect (GTK_OBJECT (adj), "value_changed",
2359                     GTK_SIGNAL_FUNC (cb_rotate_picture), picture);
2360 </verb></tscreen>
2361
2362 What about when a widget reconfigures the <tt/upper/ or <tt/lower/
2363 fields of its adjustment, such as when a user adds more text to a text
2364 widget?  In this case, it emits the <tt/changed/ signal, which looks
2365 like this:
2366
2367 <tscreen><verb>
2368   void (* changed)       (GtkAdjustment *adjustment);
2369 </verb></tscreen>
2370
2371 Range widgets typically connect a handler to this signal, which
2372 changes their appearance to reflect the change - for example, the size
2373 of the slider in a scrollbar will grow or shrink in inverse proportion
2374 to the difference between the <tt/lower/ and <tt/upper/ values of its
2375 adjustment.
2376
2377 You probably won't ever need to attach a handler to this signal,
2378 unless you're writing a new type of range widget.  However, if you
2379 change any of the values in a GtkAdjustment directly, you should emit
2380 this signal on it to reconfigure whatever widgets are using it, like
2381 this:
2382
2383 <tscreen><verb>
2384 gtk_signal_emit_by_name (GTK_OBJECT (adjustment), "changed");
2385 </verb></tscreen>
2386
2387 Now go forth and adjust!
2388 </sect1>
2389 </sect>
2390
2391 <!-- ***************************************************************** -->
2392 <sect> Range Widgets<label id="sec_Range_Widgets">
2393 <!-- ***************************************************************** -->
2394
2395 <p>
2396 The category of range widgets includes the ubiquitous scrollbar widget
2397 and the less common "scale" widget. Though these two types of widgets
2398 are generally used for different purposes, they are quite similar in
2399 function and implementation. All range widgets share a set of common
2400 graphic elements, each of which has its own X window and receives
2401 events. They all contain a "trough" and a "slider" (what is sometimes
2402 called a "thumbwheel" in other GUI environments). Dragging the slider
2403 with the pointer moves it back and forth within the trough, while
2404 clicking in the trough advances the slider towards the location of the
2405 click, either completely, or by a designated amount, depending on
2406 which mouse button is used.
2407
2408 As mentioned in <ref id="sec_Adjustment" name="Adjustments"> above,
2409 all range widgets are associated with an adjustment object, from which
2410 they calculate the length of the slider and it's position within the
2411 trough. When the user manipulates the slider, the range widget will
2412 change the value of the adjustment.
2413
2414 <!-- ----------------------------------------------------------------- -->
2415 <sect1> Scrollbar Widgets
2416 <p>
2417 These are your standard, run-of-the-mill scrollbars. These should be
2418 used only for scrolling some other widget, such as a list, a text box,
2419 or a viewport (and it's generally easier to use the scrolled window
2420 widget in most cases).  For other purposes, you should use scale
2421 widgets, as they are friendlier and more featureful.
2422
2423 There are separate types for horizontal and vertical scrollbars.
2424 There really isn't much to say about these. You create them with the
2425 following functions, defined in <tt>&lt;gtk/gtkhscrollbar.h&gt;</tt>
2426 and <tt>&lt;gtk/gtkvscrollbar.h&gt;</tt>:
2427
2428 <tscreen><verb>
2429 GtkWidget *gtk_hscrollbar_new( GtkAdjustment *adjustment );
2430
2431 GtkWidget *gtk_vscrollbar_new( GtkAdjustment *adjustment );
2432 </verb></tscreen>
2433
2434 and that's about it (if you don't believe me, look in the header
2435 files!).  The <tt/adjustment/ argument can either be a pointer to an
2436 existing GtkAdjustment, or NULL, in which case one will be created for
2437 you. Specifying NULL might actually be useful in this case, if you
2438 wish to pass the newly-created adjustment to the constructor function
2439 of some other widget which will configure it for you, such as a text
2440 widget.
2441 </sect1>
2442
2443 <!-- ----------------------------------------------------------------- -->
2444 <sect1> Scale Widgets
2445 <p>
2446 Scale widgets are used to allow the user to visually select and
2447 manipulate a value within a specific range. You might want to use a
2448 scale widget, for example, to adjust the magnification level on a
2449 zoomed preview of a picture, or to control the brightness of a colour,
2450 or to specify the number of minutes of inactivity before a screensaver
2451 takes over the screen.
2452
2453 <!-- ----------------------------------------------------------------- -->
2454 <sect2>Creating a Scale Widget
2455 <p>
2456 As with scrollbars, there are separate widget types for horizontal and
2457 vertical scale widgets. (Most programmers seem to favour horizontal
2458 scale widgets). Since they work essentially the same way, there's no
2459 need to treat them separately here. The following functions, defined
2460 in <tt>&lt;gtk/gtkvscale.h&gt;</tt> and
2461 <tt>&lt;gtk/gtkhscale.h&gt;</tt>, create vertical and horizontal scale
2462 widgets, respectively:
2463
2464 <tscreen>
2465 <verb>
2466 GtkWidget *gtk_vscale_new( GtkAdjustment *adjustment );
2467
2468 GtkWidget *gtk_hscale_new( GtkAdjustment *adjustment );
2469 </verb>
2470 </tscreen>
2471
2472 The <tt/adjustment/ argument can either be an adjustment which has
2473 already been created with <tt/gtk_adjustment_new()/, or <tt/NULL/, in
2474 which case, an anonymous GtkAdjustment is created with all of its
2475 values set to <tt/0.0/ (which isn't very useful in this case). In
2476 order to avoid confusing yourself, you probably want to create your
2477 adjustment with a <tt/page_size/ of <tt/0.0/ so that its <tt/upper/
2478 value actually corresponds to the highest value the user can select.
2479 (If you're <em/already/ thoroughly confused, read the section on <ref
2480 id="sec_Adjustment" name="Adjustments"> again for an explanation of
2481 what exactly adjustments do and how to create and manipulate them).
2482
2483 <!-- ----------------------------------------------------------------- -->
2484 <sect2> Functions and Signals (well, functions, at least)
2485 <p>
2486 Scale widgets can display their current value as a number beside the
2487 trough. The default behaviour is to show the value, but you can change
2488 this with this function:
2489
2490 <tscreen><verb>
2491 void gtk_scale_set_draw_value( GtkScale *scale,
2492                                gint      draw_value );
2493 </verb></tscreen>
2494
2495 As you might have guessed, <tt/draw_value/ is either <tt/TRUE/ or
2496 <tt/FALSE/, with predictable consequences for either one.
2497
2498 The value displayed by a scale widget is rounded to one decimal point
2499 by default, as is the <tt/value/ field in its GtkAdjustment. You can
2500 change this with:
2501
2502 <tscreen>
2503 <verb>
2504 void gtk_scale_set_digits( GtkScale *scale,
2505                             gint     digits );
2506 </verb>
2507 </tscreen>
2508
2509 where <tt/digits/ is the number of decimal places you want. You can
2510 set <tt/digits/ to anything you like, but no more than 13 decimal
2511 places will actually be drawn on screen.
2512
2513 Finally, the value can be drawn in different positions
2514 relative to the trough:
2515
2516 <tscreen>
2517 <verb>
2518 void gtk_scale_set_value_pos( GtkScale        *scale,
2519                               GtkPositionType  pos );
2520 </verb>
2521 </tscreen>
2522
2523 The argument <tt/pos/ is of type <tt>GtkPositionType</tt>, which is
2524 defined in <tt>&lt;gtk/gtkenums.h&gt;</tt>, and can take one of the
2525 following values:
2526
2527 <itemize>
2528 <item> GTK_POS_LEFT
2529 <item> GTK_POS_RIGHT
2530 <item> GTK_POS_TOP
2531 <item> GTK_POS_BOTTOM
2532 </itemize>
2533
2534 If you position the value on the "side" of the trough (e.g. on the top
2535 or bottom of a horizontal scale widget), then it will follow the
2536 slider up and down the trough.
2537
2538 All the preceding functions are defined in
2539 <tt>&lt;gtk/gtkscale.h&gt;</tt>.
2540 </sect2>
2541 </sect1>
2542
2543 <!-- ----------------------------------------------------------------- -->
2544 <sect1> Common Functions <label id="sec_Range_Functions">
2545 <p>
2546 The GtkRange widget class is fairly complicated internally, but, like
2547 all the "base class" widgets, most of its complexity is only
2548 interesting if you want to hack on it. Also, almost all of the
2549 functions and signals it defines are only really used in writing
2550 derived widgets. There are, however, a few useful functions that are
2551 defined in <tt>&lt;gtk/gtkrange.h&gt;</tt> and will work on all range
2552 widgets.
2553
2554 <!-- ----------------------------------------------------------------- -->
2555 <sect2> Setting the Update Policy
2556 <p>
2557 The "update policy" of a range widget defines at what points during
2558 user interaction it will change the <tt/value/ field of its
2559 GtkAdjustment and emit the "value_changed" signal on this
2560 GtkAdjustment. The update policies, defined in
2561 <tt>&lt;gtk/gtkenums.h&gt;</tt> as type <tt>enum GtkUpdateType</tt>,
2562 are:
2563
2564 <itemize>
2565 <item>GTK_UPDATE_POLICY_CONTINUOUS - This is the default. The
2566 "value_changed" signal is emitted continuously, i.e. whenever the
2567 slider is moved by even the tiniest amount.
2568 </item>
2569 <item>GTK_UPDATE_POLICY_DISCONTINUOUS - The "value_changed" signal is
2570 only emitted once the slider has stopped moving and the user has
2571 released the mouse button.
2572 </item>
2573 <item>GTK_UPDATE_POLICY_DELAYED - The "value_change" signal is emitted
2574 when the user releases the mouse button, or if the slider stops moving
2575 for a short period of time.
2576 </item>
2577 </itemize>
2578
2579 The update policy of a range widget can be set by casting it using the
2580 <tt>GTK_RANGE (Widget)</tt> macro and passing it to this function:
2581
2582 <tscreen><verb>
2583 void gtk_range_set_update_policy( GtkRange      *range,
2584                                   GtkUpdateType  policy) ;
2585 </verb></tscreen>
2586
2587 <!-- ----------------------------------------------------------------- -->
2588 <sect2>Getting and Setting Adjustments
2589 <p>
2590 Getting and setting the adjustment for a range widget "on the fly" is
2591 done, predictably, with:
2592
2593 <tscreen><verb>
2594 GtkAdjustment* gtk_range_get_adjustment( GtkRange *range );
2595
2596 void gtk_range_set_adjustment( GtkRange      *range,
2597                                GtkAdjustment *adjustment );
2598 </verb></tscreen>
2599
2600 <tt/gtk_range_get_adjustment()/ returns a pointer to the adjustment to
2601 which <tt/range/ is connected.
2602
2603 <tt/gtk_range_set_adjustment()/ does absolutely nothing if you pass it
2604 the adjustment that <tt/range/ is already using, regardless of whether
2605 you changed any of its fields or not. If you pass it a new
2606 GtkAdjustment, it will unreference the old one if it exists (possibly
2607 destroying it), connect the appropriate signals to the new one, and
2608 call the private function <tt/gtk_range_adjustment_changed()/, which
2609 will (or at least, is supposed to...) recalculate the size and/or
2610 position of the slider and redraw if necessary. As mentioned in the
2611 section on adjustments, if you wish to reuse the same GtkAdjustment,
2612 when you modify its values directly, you should emit the "changed"
2613 signal on it, like this:
2614
2615 <tscreen><verb>
2616 gtk_signal_emit_by_name (GTK_OBJECT (adjustment), "changed");
2617 </verb></tscreen>
2618 </sect2>
2619 </sect1>
2620
2621 <!-- ----------------------------------------------------------------- -->
2622 <sect1> Key and Mouse bindings
2623 <p>
2624 All of the GTK+ range widgets react to mouse clicks in more or less
2625 the same way. Clicking button-1 in the trough will cause its
2626 adjustment's <tt/page_increment/ to be added or subtracted from its
2627 <tt/value/, and the slider to be moved accordingly. Clicking mouse
2628 button-2 in the trough will jump the slider to the point at which the
2629 button was clicked. Clicking any button on a scrollbar's arrows will
2630 cause its adjustment's value to change <tt/step_increment/ at a time.
2631
2632 It may take a little while to get used to, but by default, scrollbars
2633 as well as scale widgets can take the keyboard focus in GTK+. If you
2634 think your users will find this too confusing, you can always disable
2635 this by unsetting the GTK_CAN_FOCUS flag on the scrollbar, like this:
2636
2637 <tscreen><verb>
2638 GTK_WIDGET_UNSET_FLAGS (scrollbar, GTK_CAN_FOCUS);
2639 </verb></tscreen>
2640
2641 The key bindings (which are, of course, only active when the widget
2642 has focus) are slightly different between horizontal and vertical
2643 range widgets, for obvious reasons. They are also not quite the same
2644 for scale widgets as they are for scrollbars, for somewhat less
2645 obvious reasons (possibly to avoid confusion between the keys for
2646 horizontal and vertical scrollbars in scrolled windows, where both
2647 operate on the same area).
2648
2649 <sect2> Vertical Range Widgets
2650 <p>
2651 All vertical range widgets can be operated with the up and down arrow
2652 keys, as well as with the <tt/Page Up/ and <tt/Page Down/ keys. The
2653 arrows move the slider up and down by <tt/step_increment/, while
2654 <tt/Page Up/ and <tt/Page Down/ move it by <tt/page_increment/.
2655
2656 The user can also move the slider all the way to one end or the other
2657 of the trough using the keyboard. With the GtkVScale widget, this is
2658 done with the <tt/Home/ and <tt/End/ keys, whereas with the
2659 GtkVScrollbar widget, this is done by typing <tt>Control-Page Up</tt>
2660 and <tt>Control-Page Down</tt>.
2661
2662 <!-- ----------------------------------------------------------------- -->
2663 <sect2> Horizontal Range Widgets
2664 <p>
2665 The left and right arrow keys work as you might expect in these
2666 widgets, moving the slider back and forth by <tt/step_increment/. The
2667 <tt/Home/ and <tt/End/ keys move the slider to the ends of the trough.
2668 For the GtkHScale widget, moving the slider by <tt/page_increment/ is
2669 accomplished with <tt>Control-Left</tt> and <tt>Control-Right</tt>,
2670 while for GtkHScrollbar, it's done with <tt>Control-Home</tt> and
2671 <tt>Control-End</tt>.
2672 </sect2>
2673 </sect1>
2674
2675 <!-- ----------------------------------------------------------------- -->
2676 <sect1> Example<label id="sec_Range_Example">
2677 <p>
2678 This example is a somewhat modified version of the "range controls"
2679 test from <tt/testgtk.c/. It basically puts up a window with three
2680 range widgets all connected to the same adjustment, and a couple of
2681 controls for adjusting some of the parameters mentioned above and in
2682 the seciton on adjustments, so you can see how they affect the way
2683 these widgets work for the user.
2684
2685 <tscreen><verb>
2686 /* example-start rangewidgets rangewidgets.c */
2687
2688 #include <gtk/gtk.h>
2689
2690 GtkWidget *hscale, *vscale;
2691
2692 void cb_pos_menu_select( GtkWidget       *item,
2693                          GtkPositionType  pos )
2694 {
2695     /* Set the value position on both scale widgets */
2696     gtk_scale_set_value_pos (GTK_SCALE (hscale), pos);
2697     gtk_scale_set_value_pos (GTK_SCALE (vscale), pos);
2698 }
2699
2700 void cb_update_menu_select( GtkWidget     *item,
2701                             GtkUpdateType  policy )
2702 {
2703     /* Set the update policy for both scale widgets */
2704     gtk_range_set_update_policy (GTK_RANGE (hscale), policy);
2705     gtk_range_set_update_policy (GTK_RANGE (vscale), policy);
2706 }
2707
2708 void cb_digits_scale( GtkAdjustment *adj )
2709 {
2710     /* Set the number of decimal places to which adj->value is rounded */
2711     gtk_scale_set_digits (GTK_SCALE (hscale), (gint) adj->value);
2712     gtk_scale_set_digits (GTK_SCALE (vscale), (gint) adj->value);
2713 }
2714
2715 void cb_page_size( GtkAdjustment *get,
2716                    GtkAdjustment *set )
2717 {
2718     /* Set the page size and page increment size of the sample
2719      * adjustment to the value specified by the "Page Size" scale */
2720     set->page_size = get->value;
2721     set->page_increment = get->value;
2722     /* Now emit the "changed" signal to reconfigure all the widgets that
2723      * are attached to this adjustment */
2724     gtk_signal_emit_by_name (GTK_OBJECT (set), "changed");
2725 }
2726
2727 void cb_draw_value( GtkToggleButton *button )
2728 {
2729     /* Turn the value display on the scale widgets off or on depending
2730      *  on the state of the checkbutton */
2731     gtk_scale_set_draw_value (GTK_SCALE (hscale), button->active);
2732     gtk_scale_set_draw_value (GTK_SCALE (vscale), button->active);  
2733 }
2734
2735 /* Convenience functions */
2736
2737 GtkWidget *make_menu_item( gchar         *name,
2738                            GtkSignalFunc  callback,
2739                            gpointer       data )
2740 {
2741     GtkWidget *item;
2742   
2743     item = gtk_menu_item_new_with_label (name);
2744     gtk_signal_connect (GTK_OBJECT (item), "activate",
2745                         callback, data);
2746     gtk_widget_show (item);
2747
2748     return(item);
2749 }
2750
2751 void scale_set_default_values( GtkScale *scale )
2752 {
2753     gtk_range_set_update_policy (GTK_RANGE (scale),
2754                                  GTK_UPDATE_CONTINUOUS);
2755     gtk_scale_set_digits (scale, 1);
2756     gtk_scale_set_value_pos (scale, GTK_POS_TOP);
2757     gtk_scale_set_draw_value (scale, TRUE);
2758 }
2759
2760 /* makes the sample window */
2761
2762 void create_range_controls( void )
2763 {
2764     GtkWidget *window;
2765     GtkWidget *box1, *box2, *box3;
2766     GtkWidget *button;
2767     GtkWidget *scrollbar;
2768     GtkWidget *separator;
2769     GtkWidget *opt, *menu, *item;
2770     GtkWidget *label;
2771     GtkWidget *scale;
2772     GtkObject *adj1, *adj2;
2773
2774     /* Standard window-creating stuff */
2775     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
2776     gtk_signal_connect (GTK_OBJECT (window), "destroy",
2777                         GTK_SIGNAL_FUNC(gtk_main_quit),
2778                         NULL);
2779     gtk_window_set_title (GTK_WINDOW (window), "range controls");
2780
2781     box1 = gtk_vbox_new (FALSE, 0);
2782     gtk_container_add (GTK_CONTAINER (window), box1);
2783     gtk_widget_show (box1);
2784
2785     box2 = gtk_hbox_new (FALSE, 10);
2786     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2787     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2788     gtk_widget_show (box2);
2789
2790     /* calue, lower, upper, step_increment, page_increment, page_size */
2791     /* Note that the page_size value only makes a difference for
2792      * scrollbar widgets, and the highest value you'll get is actually
2793      * (upper - page_size). */
2794     adj1 = gtk_adjustment_new (0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
2795   
2796     vscale = gtk_vscale_new (GTK_ADJUSTMENT (adj1));
2797     scale_set_default_values (GTK_SCALE (vscale));
2798     gtk_box_pack_start (GTK_BOX (box2), vscale, TRUE, TRUE, 0);
2799     gtk_widget_show (vscale);
2800
2801     box3 = gtk_vbox_new (FALSE, 10);
2802     gtk_box_pack_start (GTK_BOX (box2), box3, TRUE, TRUE, 0);
2803     gtk_widget_show (box3);
2804
2805     /* Reuse the same adjustment */
2806     hscale = gtk_hscale_new (GTK_ADJUSTMENT (adj1));
2807     gtk_widget_set_usize (GTK_WIDGET (hscale), 200, 30);
2808     scale_set_default_values (GTK_SCALE (hscale));
2809     gtk_box_pack_start (GTK_BOX (box3), hscale, TRUE, TRUE, 0);
2810     gtk_widget_show (hscale);
2811
2812     /* Reuse the same adjustment again */
2813     scrollbar = gtk_hscrollbar_new (GTK_ADJUSTMENT (adj1));
2814     /* Notice how this causes the scales to always be updated
2815      * continuously when the scrollbar is moved */
2816     gtk_range_set_update_policy (GTK_RANGE (scrollbar), 
2817                                  GTK_UPDATE_CONTINUOUS);
2818     gtk_box_pack_start (GTK_BOX (box3), scrollbar, TRUE, TRUE, 0);
2819     gtk_widget_show (scrollbar);
2820
2821     box2 = gtk_hbox_new (FALSE, 10);
2822     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2823     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2824     gtk_widget_show (box2);
2825
2826     /* A checkbutton to control whether the value is displayed or not */
2827     button = gtk_check_button_new_with_label("Display value on scale widgets");
2828     gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (button), TRUE);
2829     gtk_signal_connect (GTK_OBJECT (button), "toggled",
2830                         GTK_SIGNAL_FUNC(cb_draw_value), NULL);
2831     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2832     gtk_widget_show (button);
2833   
2834     box2 = gtk_hbox_new (FALSE, 10);
2835     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2836
2837     /* An option menu to change the position of the value */
2838     label = gtk_label_new ("Scale Value Position:");
2839     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
2840     gtk_widget_show (label);
2841   
2842     opt = gtk_option_menu_new();
2843     menu = gtk_menu_new();
2844
2845     item = make_menu_item ("Top",
2846                            GTK_SIGNAL_FUNC(cb_pos_menu_select),
2847                            GINT_TO_POINTER (GTK_POS_TOP));
2848     gtk_menu_append (GTK_MENU (menu), item);
2849   
2850     item = make_menu_item ("Bottom", GTK_SIGNAL_FUNC (cb_pos_menu_select), 
2851                            GINT_TO_POINTER (GTK_POS_BOTTOM));
2852     gtk_menu_append (GTK_MENU (menu), item);
2853   
2854     item = make_menu_item ("Left", GTK_SIGNAL_FUNC (cb_pos_menu_select),
2855                            GINT_TO_POINTER (GTK_POS_LEFT));
2856     gtk_menu_append (GTK_MENU (menu), item);
2857   
2858     item = make_menu_item ("Right", GTK_SIGNAL_FUNC (cb_pos_menu_select),
2859                             GINT_TO_POINTER (GTK_POS_RIGHT));
2860     gtk_menu_append (GTK_MENU (menu), item);
2861   
2862     gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
2863     gtk_box_pack_start (GTK_BOX (box2), opt, TRUE, TRUE, 0);
2864     gtk_widget_show (opt);
2865
2866     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2867     gtk_widget_show (box2);
2868
2869     box2 = gtk_hbox_new (FALSE, 10);
2870     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2871
2872     /* Yet another option menu, this time for the update policy of the
2873      * scale widgets */
2874     label = gtk_label_new ("Scale Update Policy:");
2875     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
2876     gtk_widget_show (label);
2877   
2878     opt = gtk_option_menu_new();
2879     menu = gtk_menu_new();
2880   
2881     item = make_menu_item ("Continuous",
2882                            GTK_SIGNAL_FUNC (cb_update_menu_select),
2883                            GINT_TO_POINTER (GTK_UPDATE_CONTINUOUS));
2884     gtk_menu_append (GTK_MENU (menu), item);
2885   
2886     item = make_menu_item ("Discontinuous",
2887                             GTK_SIGNAL_FUNC (cb_update_menu_select),
2888                             GINT_TO_POINTER (GTK_UPDATE_DISCONTINUOUS));
2889     gtk_menu_append (GTK_MENU (menu), item);
2890   
2891     item = make_menu_item ("Delayed",
2892                            GTK_SIGNAL_FUNC (cb_update_menu_select),
2893                            GINT_TO_POINTER (GTK_UPDATE_DELAYED));
2894     gtk_menu_append (GTK_MENU (menu), item);
2895   
2896     gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
2897     gtk_box_pack_start (GTK_BOX (box2), opt, TRUE, TRUE, 0);
2898     gtk_widget_show (opt);
2899   
2900     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2901     gtk_widget_show (box2);
2902
2903     box2 = gtk_hbox_new (FALSE, 10);
2904     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2905   
2906     /* A GtkHScale widget for adjusting the number of digits on the
2907      * sample scales. */
2908     label = gtk_label_new ("Scale Digits:");
2909     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
2910     gtk_widget_show (label);
2911
2912     adj2 = gtk_adjustment_new (1.0, 0.0, 5.0, 1.0, 1.0, 0.0);
2913     gtk_signal_connect (GTK_OBJECT (adj2), "value_changed",
2914                         GTK_SIGNAL_FUNC (cb_digits_scale), NULL);
2915     scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2));
2916     gtk_scale_set_digits (GTK_SCALE (scale), 0);
2917     gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0);
2918     gtk_widget_show (scale);
2919
2920     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2921     gtk_widget_show (box2);
2922   
2923     box2 = gtk_hbox_new (FALSE, 10);
2924     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2925   
2926     /* And, one last GtkHScale widget for adjusting the page size of the
2927      * scrollbar. */
2928     label = gtk_label_new ("Scrollbar Page Size:");
2929     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
2930     gtk_widget_show (label);
2931
2932     adj2 = gtk_adjustment_new (1.0, 1.0, 101.0, 1.0, 1.0, 0.0);
2933     gtk_signal_connect (GTK_OBJECT (adj2), "value_changed",
2934                         GTK_SIGNAL_FUNC (cb_page_size), adj1);
2935     scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2));
2936     gtk_scale_set_digits (GTK_SCALE (scale), 0);
2937     gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0);
2938     gtk_widget_show (scale);
2939
2940     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
2941     gtk_widget_show (box2);
2942
2943     separator = gtk_hseparator_new ();
2944     gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
2945     gtk_widget_show (separator);
2946
2947     box2 = gtk_vbox_new (FALSE, 10);
2948     gtk_container_border_width (GTK_CONTAINER (box2), 10);
2949     gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
2950     gtk_widget_show (box2);
2951
2952     button = gtk_button_new_with_label ("Quit");
2953     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
2954                                GTK_SIGNAL_FUNC(gtk_main_quit),
2955                                NULL);
2956     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
2957     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
2958     gtk_widget_grab_default (button);
2959     gtk_widget_show (button);
2960
2961     gtk_widget_show (window);
2962 }
2963
2964 int main( int   argc,
2965           char *argv[] )
2966 {
2967     gtk_init(&amp;argc, &amp;argv);
2968
2969     create_range_controls();
2970
2971     gtk_main();
2972
2973     return(0);
2974 }
2975
2976 /* example-end */
2977 </verb></tscreen>
2978 </sect1>
2979 </sect>
2980
2981 <!-- ***************************************************************** -->
2982 <sect> Miscellaneous Widgets
2983 <!-- ***************************************************************** -->
2984
2985 <!-- ----------------------------------------------------------------- -->
2986 <sect1> Labels
2987 <p>
2988 Labels are used a lot in GTK, and are relatively simple. Labels emit
2989 no signals as they do not have an associated X window. If you need to
2990 catch signals, or do clipping, use the <ref id="sec_EventBox"
2991 name="EventBox"> widget.
2992
2993 To create a new label, use:
2994
2995 <tscreen><verb>
2996 GtkWidget *gtk_label_new( char *str );
2997 </verb></tscreen>
2998
2999 Where the sole argument is the string you wish the label to display.
3000
3001 To change the label's text after creation, use the function:
3002
3003 <tscreen><verb>
3004 void gtk_label_set( GtkLabel *label,
3005                     char     *str );
3006 </verb></tscreen>
3007
3008 Where the first argument is the label you created previously (cast
3009 using the GTK_LABEL() macro), and the second is the new string.
3010
3011 The space needed for the new string will be automatically adjusted if
3012 needed.
3013
3014 To retrieve the current string, use:
3015
3016 <tscreen><verb>
3017 void gtk_label_get( GtkLabel  *label,
3018                     char     **str );
3019 </verb></tscreen>
3020
3021 Where the first argument is the label you've created, and the second,
3022 the return for the string.
3023
3024 <!-- ----------------------------------------------------------------- -->
3025 <sect1>The Tooltips Widget
3026 <p>
3027 These are the little text strings that pop up when you leave your
3028 pointer over a button or other widget for a few seconds. They are easy
3029 to use, so I will just explain them without giving an example. If you
3030 want to see some code, take a look at the testgtk.c program
3031 distributed with GTK.
3032
3033 Widgets that do not receieve events (widgets that do not have their
3034 own window) will not work with tooltips.
3035
3036 The first call you will use creates a new tooltip. You only need to do
3037 this once for a set of tooltips as the <tt/GtkTooltip/ object this
3038 function returns can be used to create multiple tooltips.
3039
3040 <tscreen><verb>
3041 GtkTooltips *gtk_tooltips_new( void );
3042 </verb></tscreen>
3043
3044 Once you have created a new tooltip, and the widget you wish to use it
3045 on, simply use this call to set it:
3046
3047 <tscreen><verb>
3048 void gtk_tooltips_set_tip( GtkTooltips *tooltips,
3049                            GtkWidget   *widget,
3050                            const gchar *tip_text,
3051                            const gchar *tip_private );
3052 </verb></tscreen>
3053
3054 The first argument is the tooltip you've already created, followed by
3055 the widget you wish to have this tooltip pop up for, and the text you
3056 wish it to say. The last argument is a text string that can be used as
3057 an identifier when using GtkTipsQuery to implement context sensitive
3058 help. For now, you can set it to NULL.
3059
3060 <!-- TODO: sort out what how to do the context sensitive help -->
3061
3062 Here's a short example:
3063
3064 <tscreen><verb>
3065 GtkTooltips *tooltips;
3066 GtkWidget *button;
3067 .
3068 .
3069 .
3070 tooltips = gtk_tooltips_new ();
3071 button = gtk_button_new_with_label ("button 1");
3072 .
3073 .
3074 .
3075 gtk_tooltips_set_tip (tooltips, button, "This is button 1", NULL);
3076 </verb></tscreen>
3077
3078 There are other calls that can be used with tooltips. I will just list
3079 them with a brief description of what they do.
3080
3081 <tscreen><verb>
3082 void gtk_tooltips_enable( GtkTooltips *tooltips );
3083 </verb></tscreen>
3084
3085 Enable a disabled set of tooltips.
3086
3087 <tscreen><verb>
3088 void gtk_tooltips_disable( GtkTooltips *tooltips );
3089 </verb></tscreen>
3090
3091 Disable an enabled set of tooltips.
3092
3093 <tscreen><verb>
3094 void gtk_tooltips_set_delay( GtkTooltips *tooltips,
3095                              gint         delay );
3096
3097 </verb></tscreen>
3098
3099 Sets how many milliseconds you have to hold your pointer over the
3100 widget before the tooltip will pop up. The default is 500
3101 milliseconds (half a second).
3102
3103 <tscreen><verb>
3104 void gtk_tooltips_set_colors( GtkTooltips *tooltips,
3105                               GdkColor    *background,
3106                               GdkColor    *foreground );
3107 </verb></tscreen>
3108
3109 Set the foreground and background color of the tooltips.
3110
3111 And that's all the functions associated with tooltips. More than
3112 you'll ever want to know :-)
3113
3114 <!-- ----------------------------------------------------------------- -->
3115 <sect1> Progress Bars
3116 <p>
3117 Progress bars are used to show the status of an operation. They are
3118 pretty easy to use, as you will see with the code below. But first
3119 lets start out with the calls to create a new progress bar.
3120
3121 There are two ways to create a progress bar, one simple one takes
3122 no arguments, and one that takes a GtkAdjustment object as an
3123 argument. If the former is used, the progress bar creates it's own
3124 adjustment object.
3125
3126 <tscreen><verb>
3127 GtkWidget *gtk_progress_bar_new( void );
3128
3129 GtkWidget *gtk_progress_bar_new_with_adjustment( GtkAdjustment *adjustment );
3130 </verb></tscreen>
3131
3132 The second method has the advantage that we can use the adjustment
3133 object to specify our own range parameters for the progress bar.
3134
3135 Now that the progress bar has been created we can use it.
3136
3137 <tscreen><verb>
3138 void gtk_progress_bar_update( GtkProgressBar *pbar,
3139                               gfloat          percentage );
3140 </verb></tscreen>
3141
3142 The first argument is the progress bar you wish to operate on, and the
3143 second argument is the amount 'completed', meaning the amount the
3144 progress bar has been filled from 0-100%. This is passed to the
3145 function as a real number ranging from 0 to 1.
3146
3147 GTK v1.1 has added new functionality to the progress bar that enables
3148 it to display it's value in different ways, and to inform the user of
3149 its current value and its range.
3150
3151 A progress bar may be set to one of a number of orientations using the
3152 function
3153
3154 <tscreen><verb>
3155 void gtk_progress_bar_set_orientation( GtkProgressBar *pbar,
3156                                        GtkProgressBarOrientation orientation );
3157 </verb></tscreen>
3158
3159 Where the <tt/orientation/ argument may take one of the following
3160 values to indicate the direction in which the progress bar moves:
3161
3162 <itemize>
3163 <item> GTK_PROGRESS_LEFT_TO_RIGHT
3164 <item> GTK_PROGRESS_RIGHT_TO_LEFT
3165 <item> GTK_PROGRESS_BOTTOM_TO_TOP
3166 <item> GTK_PROGRESS_TOP_TO_BOTTOM
3167 </itemize>
3168
3169 When used as a measure of how far a process has progressed, the
3170 GtkProgressBar can be set to display it's value in either a continuous
3171 or discrete mode. In continuous mode, the progress bar is updated for
3172 each value. In discrete mode, the progress bar is updated in a number
3173 of discrete blocks. The number of blocks is also configurable.
3174
3175 The style of a progress bar can be set using the following function.
3176
3177 <tscreen><verb>
3178 void gtk_progress_bar_set_bar_style( GtkProgressBar      *pbar,
3179                                      GtkProgressBarStyle  style );
3180 </verb></tscreen>
3181
3182 The <tt/style/ parameter can take one of two values:
3183
3184 <itemize>
3185 <item>GTK_PROGRESS_CONTINUOUS
3186 <item>GTK_PROGRESS_DISCRETE
3187 </itemize>
3188
3189 The number of discrete blocks can be set by calling
3190
3191 <tscreen><verb>
3192 void gtk_progress_bar_set_discrete_blocks( GtkProgressBar *pbar,
3193                                            guint           blocks );
3194 </verb></tscreen>
3195
3196 As well as indicating the amount of progress that has occured, the
3197 progress bar may be set to just indicate that there is some
3198 activity. This can be useful in situations where progress cannot be
3199 measured against a value range. Activity mode is not effected by the
3200 bar style that is described above, and overrides it.This mode is
3201 selected by the following function.
3202
3203 <tscreen><verb>
3204 void gtk_progress_set_activity_mode( GtkProgress *progress,
3205                                      guint        activity_mode );
3206 </verb></tscreen>
3207
3208 The step size of the activity indicator, and the number of blocks are
3209 set using the following functions.
3210
3211 <tscreen><verb>
3212 void gtk_progress_bar_set_activity_step( GtkProgressBar *pbar,
3213                                          guint           step );
3214
3215 void gtk_progress_bar_set_activity_blocks( GtkProgressBar *pbar,
3216                                            guint           blocks );
3217 </verb></tscreen>
3218
3219 When in continuous mode, the progress bar can also display a
3220 configurable text string within it's trough, using the following
3221 function.
3222
3223 <tscreen><verb>
3224 void gtk_progress_set_format_string( GtkProgress *progress,
3225                                      gchar       *format);
3226 </verb></tscreen>
3227
3228 The <tt/format/ argument is similiar to one that would be used in a C
3229 <tt/printf/ statement. The following directives may be used within the
3230 format string:
3231
3232 <itemize>
3233 <item> %p - percentage
3234 <item> %v - value
3235 <item> %l - lower range value
3236 <item> %u - upper range value
3237 </itemize>
3238  
3239 Progress Bars are usually used with timeouts or other such functions
3240 (see section on <ref id="sec_timeouts" name="Timeouts, I/O and Idle
3241 Functions">) to give the illusion of multitasking. All will employ the
3242 gtk_progress_bar_update function in the same manner.
3243
3244 Here is an example of the progress bar, updated using timeouts.  This
3245 code also shows you how to reset the Progress Bar.
3246
3247 <tscreen><verb>
3248 /* example-start progressbar progressbar.c */
3249
3250 #include <gtk/gtk.h>
3251
3252 typedef struct _ProgressData {
3253     GtkWidget *window;
3254     GtkWidget *pbar;
3255     int timer;
3256 } ProgressData;
3257
3258 /* Update the value of the progress bar so that we get
3259  * some movement */
3260 gint progress_timeout( gpointer data )
3261 {
3262     gfloat new_val;
3263     GtkAdjustment *adj;
3264
3265     adj = GTK_PROGRESS (data)->adjustment;
3266
3267     /* Calculate the value of the progress bar using the
3268      * value range set in the adjustment object */
3269     new_val = adj->value + 1;
3270     if (new_val > adj->upper)
3271       new_val = adj->lower;
3272
3273     /* Set the new value */
3274     gtk_progress_set_value (GTK_PROGRESS (data), new_val);
3275
3276     /* As this is a timeout function, return TRUE so that it
3277      * continues to get called */
3278     return(TRUE);
3279
3280
3281 /* Callback that toggles the text display within the progress
3282  * bar trough */
3283 void toggle_show_text( GtkWidget    *widget,
3284                        ProgressData *pdata )
3285 {
3286     gtk_progress_set_show_text (GTK_PROGRESS (pdata->pbar),
3287                                 GTK_TOGGLE_BUTTON (widget)->active);
3288 }
3289
3290 /* Callback that toggles the activity mode of the progress
3291  * bar */
3292 void toggle_activity_mode( GtkWidget    *widget,
3293                            ProgressData *pdata )
3294 {
3295     gtk_progress_set_activity_mode (GTK_PROGRESS (pdata->pbar),
3296                                     GTK_TOGGLE_BUTTON (widget)->active);
3297 }
3298
3299 /* Callback that toggles the continuous mode of the progress
3300  * bar */
3301 void set_continuous_mode( GtkWidget    *widget,
3302                           ProgressData *pdata )
3303 {
3304     gtk_progress_bar_set_bar_style (GTK_PROGRESS_BAR (pdata->pbar),
3305                                     GTK_PROGRESS_CONTINUOUS);
3306 }
3307
3308 /* Callback that toggles the discrete mode of the progress
3309  * bar */
3310 void set_discrete_mode( GtkWidget    *widget,
3311                         ProgressData *pdata )
3312 {
3313     gtk_progress_bar_set_bar_style (GTK_PROGRESS_BAR (pdata->pbar),
3314                                     GTK_PROGRESS_DISCRETE);
3315 }
3316  
3317 /* Clean up allocated memory and remove the timer */
3318 void destroy_progress( GtkWidget     *widget,
3319                        ProgressData *pdata)
3320 {
3321     gtk_timeout_remove (pdata->timer);
3322     pdata->timer = 0;
3323     pdata->window = NULL;
3324     g_free(pdata);
3325     gtk_main_quit();
3326 }
3327
3328 int main( int   argc,
3329           char *argv[])
3330 {
3331     ProgressData *pdata;
3332     GtkWidget *align;
3333     GtkWidget *separator;
3334     GtkWidget *table;
3335     GtkAdjustment *adj;
3336     GtkWidget *button;
3337     GtkWidget *check;
3338     GtkWidget *vbox;
3339
3340     gtk_init (&amp;argc, &amp;argv);
3341
3342     /* Allocate memory for the data that is passwd to the callbacks */
3343     pdata = g_malloc( sizeof(ProgressData) );
3344   
3345     pdata->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
3346     gtk_window_set_policy (GTK_WINDOW (pdata->window), FALSE, FALSE, TRUE);
3347
3348     gtk_signal_connect (GTK_OBJECT (pdata->window), "destroy",
3349                         GTK_SIGNAL_FUNC (destroy_progress),
3350                         pdata);
3351     gtk_window_set_title (GTK_WINDOW (pdata->window), "GtkProgressBar");
3352     gtk_container_set_border_width (GTK_CONTAINER (pdata->window), 0);
3353
3354     vbox = gtk_vbox_new (FALSE, 5);
3355     gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
3356     gtk_container_add (GTK_CONTAINER (pdata->window), vbox);
3357     gtk_widget_show(vbox);
3358   
3359     /* Create a centering alignment object */
3360     align = gtk_alignment_new (0.5, 0.5, 0, 0);
3361     gtk_box_pack_start (GTK_BOX (vbox), align, FALSE, FALSE, 5);
3362     gtk_widget_show(align);
3363
3364     /* Create a GtkAdjusment object to hold the range of the
3365      * progress bar */
3366     adj = (GtkAdjustment *) gtk_adjustment_new (0, 1, 150, 0, 0, 0);
3367
3368     /* Create the GtkProgressBar using the adjustment */
3369     pdata->pbar = gtk_progress_bar_new_with_adjustment (adj);
3370
3371     /* Set the format of the string that can be displayed in the
3372      * trough of the progress bar:
3373      * %p - percentage
3374      * %v - value
3375      * %l - lower range value
3376      * %u - upper range value */
3377     gtk_progress_set_format_string (GTK_PROGRESS (pdata->pbar),
3378                                     "%v from [%l-%u] (=%p%%)");
3379     gtk_container_add (GTK_CONTAINER (align), pdata->pbar);
3380     gtk_widget_show(pdata->pbar);
3381
3382     /* Add a timer callback to update the value of the progress bar */
3383     pdata->timer = gtk_timeout_add (100, progress_timeout, pdata->pbar);
3384
3385     separator = gtk_hseparator_new ();
3386     gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
3387     gtk_widget_show(separator);
3388
3389     /* rows, columns, homogeneous */
3390     table = gtk_table_new (2, 3, FALSE);
3391     gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, TRUE, 0);
3392     gtk_widget_show(table);
3393
3394     /* Add a check button to select displaying of the trough text */
3395     check = gtk_check_button_new_with_label ("Show text");
3396     gtk_table_attach (GTK_TABLE (table), check, 0, 1, 0, 1,
3397                       GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
3398                       5, 5);
3399     gtk_signal_connect (GTK_OBJECT (check), "clicked",
3400                         GTK_SIGNAL_FUNC (toggle_show_text),
3401                         pdata);
3402     gtk_widget_show(check);
3403
3404     /* Add a check button to toggle activity mode */
3405     check = gtk_check_button_new_with_label ("Activity mode");
3406     gtk_table_attach (GTK_TABLE (table), check, 0, 1, 1, 2,
3407                       GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
3408                       5, 5);
3409     gtk_signal_connect (GTK_OBJECT (check), "clicked",
3410                         GTK_SIGNAL_FUNC (toggle_activity_mode),
3411                         pdata);
3412     gtk_widget_show(check);
3413
3414     separator = gtk_vseparator_new ();
3415     gtk_table_attach (GTK_TABLE (table), separator, 1, 2, 0, 2,
3416                       GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
3417                       5, 5);
3418     gtk_widget_show(separator);
3419
3420     /* Add a radio button to select continuous display mode */
3421     button = gtk_radio_button_new_with_label (NULL, "Continuous");
3422     gtk_table_attach (GTK_TABLE (table), button, 2, 3, 0, 1,
3423                       GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
3424                       5, 5);
3425     gtk_signal_connect (GTK_OBJECT (button), "clicked",
3426                         GTK_SIGNAL_FUNC (set_continuous_mode),
3427                         pdata);
3428     gtk_widget_show (button);
3429
3430     /* Add a radio button to select discrete display mode */
3431     button = gtk_radio_button_new_with_label(
3432                gtk_radio_button_group (GTK_RADIO_BUTTON (button)),
3433                "Discrete");
3434     gtk_table_attach (GTK_TABLE (table), button, 2, 3, 1, 2,
3435                       GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL,
3436                       5, 5);
3437     gtk_signal_connect (GTK_OBJECT (button), "clicked",
3438                         GTK_SIGNAL_FUNC (set_discrete_mode),
3439                         pdata);
3440     gtk_widget_show (button);
3441
3442     separator = gtk_hseparator_new ();
3443     gtk_box_pack_start (GTK_BOX (vbox), separator, FALSE, FALSE, 0);
3444     gtk_widget_show(separator);
3445
3446     /* Add a button to exit the program */
3447     button = gtk_button_new_with_label ("close");
3448     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
3449                                (GtkSignalFunc) gtk_widget_destroy,
3450                                GTK_OBJECT (pdata->window));
3451     gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
3452
3453     /* This makes it so the button is the default. */
3454     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
3455
3456     /* This grabs this button to be the default button. Simply hitting
3457      * the "Enter" key will cause this button to activate. */
3458     gtk_widget_grab_default (button);
3459     gtk_widget_show(button);
3460
3461     gtk_widget_show (pdata->window);
3462
3463     gtk_main ();
3464     
3465     return(0);
3466 }
3467 /* example-end */
3468 </verb></tscreen>
3469
3470 <!-- ----------------------------------------------------------------- -->
3471 <sect1> Dialogs
3472 <p>
3473 The Dialog widget is very simple, and is actually just a window with a
3474 few things pre-packed into it for you. The structure for a Dialog is:
3475
3476 <tscreen><verb>
3477 struct GtkDialog
3478 {
3479       GtkWindow window;
3480     
3481       GtkWidget *vbox;
3482       GtkWidget *action_area;
3483 };
3484 </verb></tscreen>
3485
3486 So you see, it simply creates a window, and then packs a vbox into the
3487 top, then a separator, and then an hbox for the "action_area".
3488
3489 The Dialog widget can be used for pop-up messages to the user, and
3490 other similar tasks. It is really basic, and there is only one
3491 function for the dialog box, which is:
3492
3493 <tscreen><verb>
3494 GtkWidget *gtk_dialog_new( void );
3495 </verb></tscreen>
3496
3497 So to create a new dialog box, use,
3498
3499 <tscreen><verb>
3500     GtkWidget *window;
3501     window = gtk_dialog_new ();
3502 </verb></tscreen>
3503
3504 This will create the dialog box, and it is now up to you to use it.
3505 you could pack a button in the action_area by doing something like this:
3506
3507 <tscreen><verb>
3508     button = ...
3509     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area),
3510                         button, TRUE, TRUE, 0);
3511     gtk_widget_show (button);
3512 </verb></tscreen>
3513
3514 And you could add to the vbox area by packing, for instance, a label 
3515 in it, try something like this:
3516
3517 <tscreen><verb>
3518     label = gtk_label_new ("Dialogs are groovy");
3519     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox),
3520                         label, TRUE, TRUE, 0);
3521     gtk_widget_show (label);
3522 </verb></tscreen>
3523
3524 As an example in using the dialog box, you could put two buttons in
3525 the action_area, a Cancel button and an Ok button, and a label in the
3526 vbox area, asking the user a question or giving an error etc. Then
3527 you could attach a different signal to each of the buttons and perform
3528 the operation the user selects.
3529
3530 If the simple functionality provided by the default vertical and
3531 horizontal boxes in the two areas does't give you enough control for
3532 your application, then you can simply pack another layout widget into
3533 the boxes provided. For example, you could pack a table into the
3534 vertical box.
3535
3536 <!-- ----------------------------------------------------------------- -->
3537 <sect1> Pixmaps <label id="sec_Pixmaps">
3538 <p>
3539 Pixmaps are data structures that contain pictures. These pictures can
3540 be used in various places, but most visibly as icons on the X-Windows
3541 desktop, or as cursors. A bitmap is a 2-color pixmap.
3542
3543 To use pixmaps in GTK, we must first build a GdkPixmap structure using
3544 routines from the GDK layer. Pixmaps can either be created from
3545 in-memory data, or from data read from a file. We'll go through each
3546 of the calls to create a pixmap.
3547
3548 <tscreen><verb>
3549 GdkPixmap *gdk_bitmap_create_from_data( GdkWindow *window,
3550                                         gchar     *data,
3551                                         gint       width,
3552                                         gint       height );
3553 </verb></tscreen>
3554
3555 This routine is used to create a single-plane pixmap (2 colors) from
3556 data in memory. Each bit of the data represents whether that pixel is
3557 off or on. Width and height are in pixels. The GdkWindow pointer is
3558 to the current window, since a pixmap resources are meaningful only in
3559 the context of the screen where it is to be displayed.
3560
3561 <tscreen><verb>
3562 GdkPixmap *gdk_pixmap_create_from_data( GdkWindow *window,
3563                                         gchar     *data,
3564                                         gint       width,
3565                                         gint       height,
3566                                         gint       depth,
3567                                         GdkColor  *fg,
3568                                         GdkColor  *bg );
3569 </verb></tscreen>
3570
3571 This is used to create a pixmap of the given depth (number of colors) from
3572 the bitmap data specified. <tt/fg/ and <tt/bg/ are the foreground and
3573 background color to use.
3574
3575 <tscreen><verb>
3576 GdkPixmap *gdk_pixmap_create_from_xpm( GdkWindow   *window,
3577                                        GdkBitmap  **mask,
3578                                        GdkColor    *transparent_color,
3579                                        const gchar *filename );
3580 </verb></tscreen>
3581
3582 XPM format is a readable pixmap representation for the X Window
3583 System. It is widely used and many different utilities are available
3584 for creating image files in this format. The file specified by
3585 filename must contain an image in that format and it is loaded into
3586 the pixmap structure. The mask specifies which bits of the pixmap are
3587 opaque. All other bits are colored using the color specified by
3588 transparent_color. An example using this follows below.
3589
3590 <tscreen><verb>
3591 GdkPixmap *gdk_pixmap_create_from_xpm_d( GdkWindow  *window,
3592                                          GdkBitmap **mask,
3593                                          GdkColor   *transparent_color,
3594                                          gchar     **data );
3595 </verb></tscreen>
3596
3597 Small images can be incorporated into a program as data in the XPM
3598 format.  A pixmap is created using this data, instead of reading it
3599 from a file.  An example of such data is
3600
3601 <tscreen><verb>
3602 /* XPM */
3603 static const char * xpm_data[] = {
3604 "16 16 3 1",
3605 "       c None",
3606 ".      c #000000000000",
3607 "X      c #FFFFFFFFFFFF",
3608 "                ",
3609 "   ......       ",
3610 "   .XXX.X.      ",
3611 "   .XXX.XX.     ",
3612 "   .XXX.XXX.    ",
3613 "   .XXX.....    ",
3614 "   .XXXXXXX.    ",
3615 "   .XXXXXXX.    ",
3616 "   .XXXXXXX.    ",
3617 "   .XXXXXXX.    ",
3618 "   .XXXXXXX.    ",
3619 "   .XXXXXXX.    ",
3620 "   .XXXXXXX.    ",
3621 "   .........    ",
3622 "                ",
3623 "                "};
3624 </verb></tscreen>
3625
3626 When we're done using a pixmap and not likely to reuse it again soon,
3627 it is a good idea to release the resource using
3628 gdk_pixmap_unref(). Pixmaps should be considered a precious resource.
3629
3630 Once we've created a pixmap, we can display it as a GTK widget. We
3631 must create a GTK pixmap widget to contain the GDK pixmap. This is
3632 done using
3633
3634 <tscreen><verb>
3635 GtkWidget *gtk_pixmap_new( GdkPixmap *pixmap,
3636                            GdkBitmap *mask );
3637 </verb></tscreen>
3638
3639 The other pixmap widget calls are
3640
3641 <tscreen><verb>
3642 guint gtk_pixmap_get_type( void );
3643
3644 void  gtk_pixmap_set( GtkPixmap  *pixmap,
3645                       GdkPixmap  *val,
3646                       GdkBitmap  *mask );
3647
3648 void  gtk_pixmap_get( GtkPixmap  *pixmap,
3649                       GdkPixmap **val,
3650                       GdkBitmap **mask);
3651 </verb></tscreen>
3652
3653 gtk_pixmap_set is used to change the pixmap that the widget is currently
3654 managing. Val is the pixmap created using GDK.
3655
3656 The following is an example of using a pixmap in a button.
3657
3658 <tscreen><verb>
3659 /* example-start pixmap pixmap.c */
3660
3661 #include <gtk/gtk.h>
3662
3663
3664 /* XPM data of Open-File icon */
3665 static const char * xpm_data[] = {
3666 "16 16 3 1",
3667 "       c None",
3668 ".      c #000000000000",
3669 "X      c #FFFFFFFFFFFF",
3670 "                ",
3671 "   ......       ",
3672 "   .XXX.X.      ",
3673 "   .XXX.XX.     ",
3674 "   .XXX.XXX.    ",
3675 "   .XXX.....    ",
3676 "   .XXXXXXX.    ",
3677 "   .XXXXXXX.    ",
3678 "   .XXXXXXX.    ",
3679 "   .XXXXXXX.    ",
3680 "   .XXXXXXX.    ",
3681 "   .XXXXXXX.    ",
3682 "   .XXXXXXX.    ",
3683 "   .........    ",
3684 "                ",
3685 "                "};
3686
3687
3688 /* when invoked (via signal delete_event), terminates the application.
3689  */
3690 void close_application( GtkWidget *widget, GdkEvent *event, gpointer data ) {
3691     gtk_main_quit();
3692 }
3693
3694
3695 /* is invoked when the button is clicked.  It just prints a message.
3696  */
3697 void button_clicked( GtkWidget *widget, gpointer data ) {
3698     printf( "button clicked\n" );
3699 }
3700
3701 int main( int argc, char *argv[] )
3702 {
3703     /* GtkWidget is the storage type for widgets */
3704     GtkWidget *window, *pixmapwid, *button;
3705     GdkPixmap *pixmap;
3706     GdkBitmap *mask;
3707     GtkStyle *style;
3708     
3709     /* create the main window, and attach delete_event signal to terminating
3710        the application */
3711     gtk_init( &amp;argc, &amp;argv );
3712     window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
3713     gtk_signal_connect( GTK_OBJECT (window), "delete_event",
3714                         GTK_SIGNAL_FUNC (close_application), NULL );
3715     gtk_container_border_width( GTK_CONTAINER (window), 10 );
3716     gtk_widget_show( window );
3717
3718     /* now for the pixmap from gdk */
3719     style = gtk_widget_get_style( window );
3720     pixmap = gdk_pixmap_create_from_xpm_d( window->window,  &amp;mask,
3721                                            &amp;style->bg[GTK_STATE_NORMAL],
3722                                            (gchar **)xpm_data );
3723
3724     /* a pixmap widget to contain the pixmap */
3725     pixmapwid = gtk_pixmap_new( pixmap, mask );
3726     gtk_widget_show( pixmapwid );
3727
3728     /* a button to contain the pixmap widget */
3729     button = gtk_button_new();
3730     gtk_container_add( GTK_CONTAINER(button), pixmapwid );
3731     gtk_container_add( GTK_CONTAINER(window), button );
3732     gtk_widget_show( button );
3733
3734     gtk_signal_connect( GTK_OBJECT(button), "clicked",
3735                         GTK_SIGNAL_FUNC(button_clicked), NULL );
3736
3737     /* show the window */
3738     gtk_main ();
3739           
3740     return 0;
3741 }
3742 /* example-end */
3743 </verb></tscreen>
3744
3745 To load a file from an XPM data file called icon0.xpm in the current
3746 directory, we would have created the pixmap thus
3747
3748 <tscreen><verb>
3749     /* load a pixmap from a file */
3750     pixmap = gdk_pixmap_create_from_xpm( window->window, &amp;mask,
3751                                          &amp;style->bg[GTK_STATE_NORMAL],
3752                                          "./icon0.xpm" );
3753     pixmapwid = gtk_pixmap_new( pixmap, mask );
3754     gtk_widget_show( pixmapwid );
3755     gtk_container_add( GTK_CONTAINER(window), pixmapwid );
3756 </verb></tscreen>
3757
3758 A disadvantage of using pixmaps is that the displayed object is always
3759 rectangular, regardless of the image. We would like to create desktops
3760 and applications with icons that have more natural shapes. For
3761 example, for a game interface, we would like to have round buttons to
3762 push. The way to do this is using shaped windows.
3763
3764 A shaped window is simply a pixmap where the background pixels are
3765 transparent. This way, when the background image is multi-colored, we
3766 don't overwrite it with a rectangular, non-matching border around our
3767 icon. The following example displays a full wheelbarrow image on the
3768 desktop.
3769
3770 <tscreen><verb>
3771 /* example-start wheelbarrow wheelbarrow.c */
3772
3773 #include <gtk/gtk.h>
3774
3775 /* XPM */
3776 static char * WheelbarrowFull_xpm[] = {
3777 "48 48 64 1",
3778 "       c None",
3779 ".      c #DF7DCF3CC71B",
3780 "X      c #965875D669A6",
3781 "o      c #71C671C671C6",
3782 "O      c #A699A289A699",
3783 "+      c #965892489658",
3784 "@      c #8E38410330C2",
3785 "#      c #D75C7DF769A6",
3786 "$      c #F7DECF3CC71B",
3787 "%      c #96588A288E38",
3788 "&amp;      c #A69992489E79",
3789 "*      c #8E3886178E38",
3790 "=      c #104008200820",
3791 "-      c #596510401040",
3792 ";      c #C71B30C230C2",
3793 ":      c #C71B9A699658",
3794 ">      c #618561856185",
3795 ",      c #20811C712081",
3796 "<      c #104000000000",
3797 "1      c #861720812081",
3798 "2      c #DF7D4D344103",
3799 "3      c #79E769A671C6",
3800 "4      c #861782078617",
3801 "5      c #41033CF34103",
3802 "6      c #000000000000",
3803 "7      c #49241C711040",
3804 "8      c #492445144924",
3805 "9      c #082008200820",
3806 "0      c #69A618611861",
3807 "q      c #B6DA71C65144",
3808 "w      c #410330C238E3",
3809 "e      c #CF3CBAEAB6DA",
3810 "r      c #71C6451430C2",
3811 "t      c #EFBEDB6CD75C",
3812 "y      c #28A208200820",
3813 "u      c #186110401040",
3814 "i      c #596528A21861",
3815 "p      c #71C661855965",
3816 "a      c #A69996589658",
3817 "s      c #30C228A230C2",
3818 "d      c #BEFBA289AEBA",
3819 "f      c #596545145144",
3820 "g      c #30C230C230C2",
3821 "h      c #8E3882078617",
3822 "j      c #208118612081",
3823 "k      c #38E30C300820",
3824 "l      c #30C2208128A2",
3825 "z      c #38E328A238E3",
3826 "x      c #514438E34924",
3827 "c      c #618555555965",
3828 "v      c #30C2208130C2",
3829 "b      c #38E328A230C2",
3830 "n      c #28A228A228A2",
3831 "m      c #41032CB228A2",
3832 "M      c #104010401040",
3833 "N      c #492438E34103",
3834 "B      c #28A2208128A2",
3835 "V      c #A699596538E3",
3836 "C      c #30C21C711040",
3837 "Z      c #30C218611040",
3838 "A      c #965865955965",
3839 "S      c #618534D32081",
3840 "D      c #38E31C711040",
3841 "F      c #082000000820",
3842 "                                                ",
3843 "          .XoO                                  ",
3844 "         +@#$%o&amp;                                ",
3845 "         *=-;#::o+                              ",
3846 "           >,<12#:34                            ",
3847 "             45671#:X3                          ",
3848 "               +89<02qwo                        ",
3849 "e*                >,67;ro                       ",
3850 "ty>                 459@>+&amp;&amp;                    ",
3851 "$2u+                  ><ipas8*                  ",
3852 "%$;=*                *3:.Xa.dfg>                ",
3853 "Oh$;ya             *3d.a8j,Xe.d3g8+             ",
3854 " Oh$;ka          *3d$a8lz,,xxc:.e3g54           ",
3855 "  Oh$;kO       *pd$%svbzz,sxxxxfX..&amp;wn>         ",
3856 "   Oh$@mO    *3dthwlsslszjzxxxxxxx3:td8M4       ",
3857 "    Oh$@g&amp; *3d$XNlvvvlllm,mNwxxxxxxxfa.:,B*     ",
3858 "     Oh$@,Od.czlllllzlmmqV@V#V@fxxxxxxxf:%j5&amp;   ",
3859 "      Oh$1hd5lllslllCCZrV#r#:#2AxxxxxxxxxcdwM*  ",
3860 "       OXq6c.%8vvvllZZiqqApA:mq:Xxcpcxxxxxfdc9* ",
3861 "        2r<6gde3bllZZrVi7S@SV77A::qApxxxxxxfdcM ",
3862 "        :,q-6MN.dfmZZrrSS:#riirDSAX@Af5xxxxxfevo",
3863 "         +A26jguXtAZZZC7iDiCCrVVii7Cmmmxxxxxx%3g",
3864 "          *#16jszN..3DZZZZrCVSA2rZrV7Dmmwxxxx&amp;en",
3865 "           p2yFvzssXe:fCZZCiiD7iiZDiDSSZwwxx8e*>",
3866 "           OA1<jzxwwc:$d%NDZZZZCCCZCCZZCmxxfd.B ",
3867 "            3206Bwxxszx%et.eaAp77m77mmmf3&amp;eeeg* ",
3868 "             @26MvzxNzvlbwfpdettttttttttt.c,n&amp;  ",
3869 "             *;16=lsNwwNwgsvslbwwvccc3pcfu<o    ",
3870 "              p;<69BvwwsszslllbBlllllllu<5+     ",
3871 "              OS0y6FBlvvvzvzss,u=Blllj=54       ",
3872 "               c1-699Blvlllllu7k96MMMg4         ",
3873 "               *10y8n6FjvllllB<166668           ",
3874 "                S-kg+>666<M<996-y6n<8*          ",
3875 "                p71=4 m69996kD8Z-66698&amp;&amp;        ",
3876 "                &amp;i0ycm6n4 ogk17,0<6666g         ",
3877 "                 N-k-<>     >=01-kuu666>        ",
3878 "                 ,6ky&amp;      &amp;46-10ul,66,        ",
3879 "                 Ou0<>       o66y<ulw<66&amp;       ",
3880 "                  *kk5       >66By7=xu664       ",
3881 "                   <<M4      466lj<Mxu66o       ",
3882 "                   *>>       +66uv,zN666*       ",
3883 "                              566,xxj669        ",
3884 "                              4666FF666>        ",
3885 "                               >966666M         ",
3886 "                                oM6668+         ",
3887 "                                  *4            ",
3888 "                                                ",
3889 "                                                "};
3890
3891
3892 /* When invoked (via signal delete_event), terminates the application */
3893 void close_application( GtkWidget *widget, GdkEvent *event, gpointer data ) {
3894     gtk_main_quit();
3895 }
3896
3897 int main (int argc, char *argv[])
3898 {
3899     /* GtkWidget is the storage type for widgets */
3900     GtkWidget *window, *pixmap, *fixed;
3901     GdkPixmap *gdk_pixmap;
3902     GdkBitmap *mask;
3903     GtkStyle *style;
3904     GdkGC *gc;
3905     
3906     /* Create the main window, and attach delete_event signal to terminate
3907      * the application.  Note that the main window will not have a titlebar
3908      * since we're making it a popup. */
3909     gtk_init (&amp;argc, &amp;argv);
3910     window = gtk_window_new( GTK_WINDOW_POPUP );
3911     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
3912                         GTK_SIGNAL_FUNC (close_application), NULL);
3913     gtk_widget_show (window);
3914
3915     /* Now for the pixmap and the pixmap widget */
3916     style = gtk_widget_get_default_style();
3917     gc = style->black_gc;
3918     gdk_pixmap = gdk_pixmap_create_from_xpm_d( window->window, &amp;mask,
3919                                              &amp;style->bg[GTK_STATE_NORMAL],
3920                                              WheelbarrowFull_xpm );
3921     pixmap = gtk_pixmap_new( gdk_pixmap, mask );
3922     gtk_widget_show( pixmap );
3923
3924     /* To display the pixmap, we use a fixed widget to place the pixmap */
3925     fixed = gtk_fixed_new();
3926     gtk_widget_set_usize( fixed, 200, 200 );
3927     gtk_fixed_put( GTK_FIXED(fixed), pixmap, 0, 0 );
3928     gtk_container_add( GTK_CONTAINER(window), fixed );
3929     gtk_widget_show( fixed );
3930
3931     /* This masks out everything except for the image itself */
3932     gtk_widget_shape_combine_mask( window, mask, 0, 0 );
3933     
3934     /* show the window */
3935     gtk_widget_set_uposition( window, 20, 400 );
3936     gtk_widget_show( window );
3937     gtk_main ();
3938           
3939     return(0);
3940 }
3941 /* example-end */
3942 </verb></tscreen>
3943
3944 To make the wheelbarrow image sensitive, we could attach the button
3945 press event signal to make it do something. The following few lines
3946 would make the picture sensitive to a mouse button being pressed which
3947 makes the application terminate.
3948
3949 <tscreen><verb>
3950     gtk_widget_set_events( window,
3951                           gtk_widget_get_events( window ) |
3952                           GDK_BUTTON_PRESS_MASK );
3953
3954    gtk_signal_connect( GTK_OBJECT(window), "button_press_event",
3955                        GTK_SIGNAL_FUNC(close_application), NULL );
3956 </verb></tscreen>
3957
3958 <!-- ----------------------------------------------------------------- -->
3959 <sect1>Rulers
3960 <p>
3961 Ruler widgets are used to indicate the location of the mouse pointer
3962 in a given window. A window can have a vertical ruler spanning across
3963 the width and a horizontal ruler spanning down the height. A small
3964 triangular indicator on the ruler shows the exact location of the
3965 pointer relative to the ruler.
3966
3967 A ruler must first be created. Horizontal and vertical rulers are
3968 created using
3969
3970 <tscreen><verb>
3971 GtkWidget *gtk_hruler_new( void );    /* horizontal ruler */
3972
3973 GtkWidget *gtk_vruler_new( void );    /* vertical ruler   */
3974 </verb></tscreen>
3975
3976 Once a ruler is created, we can define the unit of measurement. Units
3977 of measure for rulers can be GTK_PIXELS, GTK_INCHES or
3978 GTK_CENTIMETERS. This is set using
3979
3980 <tscreen><verb>
3981 void gtk_ruler_set_metric( GtkRuler      *ruler,
3982                            GtkMetricType  metric );
3983 </verb></tscreen>
3984
3985 The default measure is GTK_PIXELS.
3986
3987 <tscreen><verb>
3988     gtk_ruler_set_metric( GTK_RULER(ruler), GTK_PIXELS );
3989 </verb></tscreen>
3990
3991 Other important characteristics of a ruler are how to mark the units
3992 of scale and where the position indicator is initially placed. These
3993 are set for a ruler using
3994
3995 <tscreen><verb>
3996 void gtk_ruler_set_range( GtkRuler *ruler,
3997                           gfloat    lower,
3998                           gfloat    upper,
3999                           gfloat    position,
4000                           gfloat    max_size );
4001 </verb></tscreen>
4002
4003 The lower and upper arguments define the extent of the ruler, and
4004 max_size is the largest possible number that will be displayed.
4005 Position defines the initial position of the pointer indicator within
4006 the ruler.
4007
4008 A vertical ruler can span an 800 pixel wide window thus
4009
4010 <tscreen><verb>
4011     gtk_ruler_set_range( GTK_RULER(vruler), 0, 800, 0, 800);
4012 </verb></tscreen>
4013
4014 The markings displayed on the ruler will be from 0 to 800, with a
4015 number for every 100 pixels. If instead we wanted the ruler to range
4016 from 7 to 16, we would code
4017
4018 <tscreen><verb>
4019     gtk_ruler_set_range( GTK_RULER(vruler), 7, 16, 0, 20);
4020 </verb></tscreen>
4021
4022 The indicator on the ruler is a small triangular mark that indicates
4023 the position of the pointer relative to the ruler. If the ruler is
4024 used to follow the mouse pointer, the motion_notify_event signal
4025 should be connected to the motion_notify_event method of the ruler.
4026 To follow all mouse movements within a window area, we would use
4027
4028 <tscreen><verb>
4029 #define EVENT_METHOD(i, x) GTK_WIDGET_CLASS(GTK_OBJECT(i)->klass)->x
4030
4031     gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
4032            (GtkSignalFunc)EVENT_METHOD(ruler, motion_notify_event),
4033            GTK_OBJECT(ruler) );
4034 </verb></tscreen>
4035
4036 The following example creates a drawing area with a horizontal ruler
4037 above it and a vertical ruler to the left of it. The size of the
4038 drawing area is 600 pixels wide by 400 pixels high. The horizontal
4039 ruler spans from 7 to 13 with a mark every 100 pixels, while the
4040 vertical ruler spans from 0 to 400 with a mark every 100 pixels.
4041 Placement of the drawing area and the rulers is done using a table.
4042
4043 <tscreen><verb>
4044 /* example-start rulers rulers.c */
4045
4046 #include <gtk/gtk.h>
4047
4048 #define EVENT_METHOD(i, x) GTK_WIDGET_CLASS(GTK_OBJECT(i)->klass)->x
4049
4050 #define XSIZE  600
4051 #define YSIZE  400
4052
4053 /* This routine gets control when the close button is clicked */
4054 void close_application( GtkWidget *widget, GdkEvent *event, gpointer data ) {
4055     gtk_main_quit();
4056 }
4057
4058 /* The main routine */
4059 int main( int argc, char *argv[] ) {
4060     GtkWidget *window, *table, *area, *hrule, *vrule;
4061
4062     /* Initialize GTK and create the main window */
4063     gtk_init( &amp;argc, &amp;argv );
4064
4065     window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
4066     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
4067             GTK_SIGNAL_FUNC( close_application ), NULL);
4068     gtk_container_border_width (GTK_CONTAINER (window), 10);
4069
4070     /* Create a table for placing the ruler and the drawing area */
4071     table = gtk_table_new( 3, 2, FALSE );
4072     gtk_container_add( GTK_CONTAINER(window), table );
4073
4074     area = gtk_drawing_area_new();
4075     gtk_drawing_area_size( (GtkDrawingArea *)area, XSIZE, YSIZE );
4076     gtk_table_attach( GTK_TABLE(table), area, 1, 2, 1, 2,
4077                       GTK_EXPAND|GTK_FILL, GTK_FILL, 0, 0 );
4078     gtk_widget_set_events( area, GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK );
4079
4080     /* The horizontal ruler goes on top. As the mouse moves across the drawing area,
4081      * a motion_notify_event is passed to the appropriate event handler for the ruler. */
4082     hrule = gtk_hruler_new();
4083     gtk_ruler_set_metric( GTK_RULER(hrule), GTK_PIXELS );
4084     gtk_ruler_set_range( GTK_RULER(hrule), 7, 13, 0, 20 );
4085     gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
4086                                (GtkSignalFunc)EVENT_METHOD(hrule, motion_notify_event),
4087                                GTK_OBJECT(hrule) );
4088     /*  GTK_WIDGET_CLASS(GTK_OBJECT(hrule)->klass)->motion_notify_event, */
4089     gtk_table_attach( GTK_TABLE(table), hrule, 1, 2, 0, 1,
4090                       GTK_EXPAND|GTK_SHRINK|GTK_FILL, GTK_FILL, 0, 0 );
4091     
4092     /* The vertical ruler goes on the left. As the mouse moves across the drawing area,
4093      * a motion_notify_event is passed to the appropriate event handler for the ruler. */
4094     vrule = gtk_vruler_new();
4095     gtk_ruler_set_metric( GTK_RULER(vrule), GTK_PIXELS );
4096     gtk_ruler_set_range( GTK_RULER(vrule), 0, YSIZE, 10, YSIZE );
4097     gtk_signal_connect_object( GTK_OBJECT(area), "motion_notify_event",
4098                                (GtkSignalFunc)
4099                                   GTK_WIDGET_CLASS(GTK_OBJECT(vrule)->klass)->motion_notify_event,
4100                                GTK_OBJECT(vrule) );
4101     gtk_table_attach( GTK_TABLE(table), vrule, 0, 1, 1, 2,
4102                       GTK_FILL, GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0 );
4103
4104     /* Now show everything */
4105     gtk_widget_show( area );
4106     gtk_widget_show( hrule );
4107     gtk_widget_show( vrule );
4108     gtk_widget_show( table );
4109     gtk_widget_show( window );
4110     gtk_main();
4111
4112     return(0);
4113 }
4114 /* example-end */
4115 </verb></tscreen>
4116
4117 <!-- ----------------------------------------------------------------- -->
4118 <sect1>Statusbars
4119 <p>
4120 Statusbars are simple widgets used to display a text message. They
4121 keep a stack of the messages pushed onto them, so that popping the
4122 current message will re-display the previous text message.
4123
4124 In order to allow different parts of an application to use the same
4125 statusbar to display messages, the statusbar widget issues Context
4126 Identifiers which are used to identify different 'users'. The message
4127 on top of the stack is the one displayed, no matter what context it is
4128 in. Messages are stacked in last-in-first-out order, not context
4129 identifier order.
4130
4131 A statusbar is created with a call to:
4132
4133 <tscreen><verb>
4134 GtkWidget *gtk_statusbar_new( void );
4135 </verb></tscreen>
4136
4137 A new Context Identifier is requested using a call to the following 
4138 function with a short textual description of the context:
4139
4140 <tscreen><verb>
4141 guint gtk_statusbar_get_context_id( GtkStatusbar *statusbar,
4142                                     const gchar  *context_description );
4143 </verb></tscreen>
4144
4145 There are three functions that can operate on statusbars:
4146
4147 <tscreen><verb>
4148 guint gtk_statusbar_push( GtkStatusbar *statusbar,
4149                           guint         context_id,
4150                           gchar        *text );
4151
4152 void gtk_statusbar_pop( GtkStatusbar *statusbar)
4153                         guint         context_id );
4154
4155 void gtk_statusbar_remove( GtkStatusbar *statusbar,
4156                            guint         context_id,
4157                            guint         message_id ); 
4158 </verb></tscreen>
4159
4160 The first, gtk_statusbar_push, is used to add a new message to the
4161 statusbar.  It returns a Message Identifier, which can be passed later
4162 to the function gtk_statusbar_remove to remove the message with the
4163 given Message and Context Identifiers from the statusbar's stack.
4164
4165 The function gtk_statusbar_pop removes the message highest in the
4166 stack with the given Context Identifier.
4167
4168 The following example creates a statusbar and two buttons, one for
4169 pushing items onto the statusbar, and one for popping the last item
4170 back off.
4171
4172 <tscreen><verb>
4173 /* example-start statusbar statusbar.c */
4174
4175 #include <gtk/gtk.h>
4176 #include <glib.h>
4177
4178 GtkWidget *status_bar;
4179
4180 void push_item (GtkWidget *widget, gpointer data)
4181 {
4182   static int count = 1;
4183   char buff[20];
4184
4185   g_snprintf(buff, 20, "Item %d", count++);
4186   gtk_statusbar_push( GTK_STATUSBAR(status_bar), (guint) &amp;data, buff);
4187
4188   return;
4189 }
4190
4191 void pop_item (GtkWidget *widget, gpointer data)
4192 {
4193   gtk_statusbar_pop( GTK_STATUSBAR(status_bar), (guint) &amp;data );
4194   return;
4195 }
4196
4197 int main (int argc, char *argv[])
4198 {
4199
4200     GtkWidget *window;
4201     GtkWidget *vbox;
4202     GtkWidget *button;
4203
4204     int context_id;
4205
4206     gtk_init (&amp;argc, &amp;argv);
4207
4208     /* create a new window */
4209     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4210     gtk_widget_set_usize( GTK_WIDGET (window), 200, 100);
4211     gtk_window_set_title(GTK_WINDOW (window), "GTK Statusbar Example");
4212     gtk_signal_connect(GTK_OBJECT (window), "delete_event",
4213                        (GtkSignalFunc) gtk_exit, NULL);
4214  
4215     vbox = gtk_vbox_new(FALSE, 1);
4216     gtk_container_add(GTK_CONTAINER(window), vbox);
4217     gtk_widget_show(vbox);
4218           
4219     status_bar = gtk_statusbar_new();      
4220     gtk_box_pack_start (GTK_BOX (vbox), status_bar, TRUE, TRUE, 0);
4221     gtk_widget_show (status_bar);
4222
4223     context_id = gtk_statusbar_get_context_id( GTK_STATUSBAR(status_bar), "Statusbar example");
4224
4225     button = gtk_button_new_with_label("push item");
4226     gtk_signal_connect(GTK_OBJECT(button), "clicked",
4227         GTK_SIGNAL_FUNC (push_item), &amp;context_id);
4228     gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 2);
4229     gtk_widget_show(button);              
4230
4231     button = gtk_button_new_with_label("pop last item");
4232     gtk_signal_connect(GTK_OBJECT(button), "clicked",
4233         GTK_SIGNAL_FUNC (pop_item), &amp;context_id);
4234     gtk_box_pack_start(GTK_BOX(vbox), button, TRUE, TRUE, 2);
4235     gtk_widget_show(button);              
4236
4237     /* always display the window as the last step so it all splashes on
4238      * the screen at once. */
4239     gtk_widget_show(window);
4240
4241     gtk_main ();
4242
4243     return 0;
4244 }
4245 /* example-end */
4246 </verb></tscreen>
4247
4248 <!-- ----------------------------------------------------------------- -->
4249 <sect1>Text Entries
4250 <p>
4251 The Entry widget allows text to be typed and displayed in a single line
4252 text box. The text may be set with function calls that allow new text
4253 to replace, prepend or append the current contents of the Entry widget.
4254
4255 There are two functions for creating Entry widgets:
4256
4257 <tscreen><verb>
4258 GtkWidget *gtk_entry_new( void );
4259
4260 GtkWidget *gtk_entry_new_with_max_length( guint16 max );
4261 </verb></tscreen>
4262
4263 The first just creates a new Entry widget, whilst the second creates a
4264 new Entry and sets a limit on the length of the text within the Entry.
4265
4266 There are several functions for altering the text which is currently
4267 within the Entry widget.
4268
4269 <tscreen><verb>
4270 void gtk_entry_set_text( GtkEntry    *entry,
4271                          const gchar *text );
4272
4273 void gtk_entry_append_text( GtkEntry    *entry,
4274                             const gchar *text );
4275
4276 void gtk_entry_prepend_text( GtkEntry    *entry,
4277                              const gchar *text );
4278 </verb></tscreen>
4279
4280 The function gtk_entry_set_text sets the contents of the Entry widget,
4281 replacing the current contents. The functions gtk_entry_append_text
4282 and gtk_entry_prepend_text allow the current contents to be appended
4283 and prepended to.
4284
4285 The next function allows the current insertion point to be set.
4286
4287 <tscreen><verb>
4288 void gtk_entry_set_position( GtkEntry *entry,
4289                              gint      position );
4290 </verb></tscreen>
4291
4292 The contents of the Entry can be retrieved by using a call to the
4293 following function. This is useful in the callback functions described below.
4294
4295 <tscreen><verb>
4296 gchar *gtk_entry_get_text( GtkEntry *entry );
4297 </verb></tscreen>
4298
4299 If we don't want the contents of the Entry to be changed by someone typing
4300 into it, we can change its editable state.
4301
4302 <tscreen><verb>
4303 void gtk_entry_set_editable( GtkEntry *entry,
4304                              gboolean  editable );
4305 </verb></tscreen>
4306
4307 The function above allows us to toggle the editable state of the
4308 Entry widget by passing in a TRUE or FALSE value for the <tt/editable/
4309 argument.
4310
4311 If we are using the Entry where we don't want the text entered to be
4312 visible, for example when a password is being entered, we can use the
4313 following function, which also takes a boolean flag.
4314
4315 <tscreen><verb>
4316 void gtk_entry_set_visibility( GtkEntry *entry,
4317                                gboolean  visible );
4318 </verb></tscreen>
4319
4320 A region of the text may be set as selected by using the following
4321 function. This would most often be used after setting some default
4322 text in an Entry, making it easy for the user to remove it.
4323
4324 <tscreen><verb>
4325 void gtk_entry_select_region( GtkEntry *entry,
4326                               gint      start,
4327                               gint      end );
4328 </verb></tscreen>
4329
4330 If we want to catch when the user has entered text, we can connect to
4331 the <tt/activate/ or <tt/changed/ signal. Activate is raised when the
4332 user hits the enter key within the Entry widget. Changed is raised
4333 when the text changes at all, e.g. for every character entered or
4334 removed.
4335
4336 The following code is an example of using an Entry widget.
4337
4338 <tscreen><verb>
4339 /* example-start entry entry.c */
4340
4341 #include <gtk/gtk.h>
4342
4343 void enter_callback(GtkWidget *widget, GtkWidget *entry)
4344 {
4345   gchar *entry_text;
4346   entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
4347   printf("Entry contents: %s\n", entry_text);
4348 }
4349
4350 void entry_toggle_editable (GtkWidget *checkbutton,
4351                                    GtkWidget *entry)
4352 {
4353   gtk_entry_set_editable(GTK_ENTRY(entry),
4354                          GTK_TOGGLE_BUTTON(checkbutton)->active);
4355 }
4356
4357 void entry_toggle_visibility (GtkWidget *checkbutton,
4358                                    GtkWidget *entry)
4359 {
4360   gtk_entry_set_visibility(GTK_ENTRY(entry),
4361                          GTK_TOGGLE_BUTTON(checkbutton)->active);
4362 }
4363
4364 int main (int argc, char *argv[])
4365 {
4366
4367     GtkWidget *window;
4368     GtkWidget *vbox, *hbox;
4369     GtkWidget *entry;
4370     GtkWidget *button;
4371     GtkWidget *check;
4372
4373     gtk_init (&amp;argc, &amp;argv);
4374
4375     /* create a new window */
4376     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
4377     gtk_widget_set_usize( GTK_WIDGET (window), 200, 100);
4378     gtk_window_set_title(GTK_WINDOW (window), "GTK Entry");
4379     gtk_signal_connect(GTK_OBJECT (window), "delete_event",
4380                        (GtkSignalFunc) gtk_exit, NULL);
4381
4382     vbox = gtk_vbox_new (FALSE, 0);
4383     gtk_container_add (GTK_CONTAINER (window), vbox);
4384     gtk_widget_show (vbox);
4385
4386     entry = gtk_entry_new_with_max_length (50);
4387     gtk_signal_connect(GTK_OBJECT(entry), "activate",
4388                        GTK_SIGNAL_FUNC(enter_callback),
4389                        entry);
4390     gtk_entry_set_text (GTK_ENTRY (entry), "hello");
4391     gtk_entry_append_text (GTK_ENTRY (entry), " world");
4392     gtk_entry_select_region (GTK_ENTRY (entry),
4393                              0, GTK_ENTRY(entry)->text_length);
4394     gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);
4395     gtk_widget_show (entry);
4396
4397     hbox = gtk_hbox_new (FALSE, 0);
4398     gtk_container_add (GTK_CONTAINER (vbox), hbox);
4399     gtk_widget_show (hbox);
4400                                   
4401     check = gtk_check_button_new_with_label("Editable");
4402     gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0);
4403     gtk_signal_connect (GTK_OBJECT(check), "toggled",
4404                         GTK_SIGNAL_FUNC(entry_toggle_editable), entry);
4405     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(check), TRUE);
4406     gtk_widget_show (check);
4407     
4408     check = gtk_check_button_new_with_label("Visible");
4409     gtk_box_pack_start (GTK_BOX (hbox), check, TRUE, TRUE, 0);
4410     gtk_signal_connect (GTK_OBJECT(check), "toggled",
4411                         GTK_SIGNAL_FUNC(entry_toggle_visibility), entry);
4412     gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(check), TRUE);
4413     gtk_widget_show (check);
4414                                    
4415     button = gtk_button_new_with_label ("Close");
4416     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
4417                                GTK_SIGNAL_FUNC(gtk_exit),
4418                                GTK_OBJECT (window));
4419     gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);
4420     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
4421     gtk_widget_grab_default (button);
4422     gtk_widget_show (button);
4423     
4424     gtk_widget_show(window);
4425
4426     gtk_main();
4427     return(0);
4428 }
4429 /* example-end */
4430 </verb></tscreen>
4431
4432 <!-- ----------------------------------------------------------------- -->
4433 <sect1> Color Selection
4434 <p>
4435 The color selection widget is, not surprisingly, a widget for
4436 interactive selection of colors. This composite widget lets the user
4437 select a color by manipulating RGB (Red, Green, Blue) and HSV (Hue,
4438 Saturation, Value) triples.  This is done either by adjusting single
4439 values with sliders or entries, or by picking the desired color from a
4440 hue-saturation wheel/value bar.  Optionally, the opacity of the color
4441 can also be set.
4442
4443 The color selection widget currently emits only one signal,
4444 "color_changed", which is emitted whenever the current color in the
4445 widget changes, either when the user changes it or if it's set
4446 explicitly through gtk_color_selection_set_color().
4447
4448 Lets have a look at what the color selection widget has to offer
4449 us. The widget comes in two flavours: gtk_color_selection and
4450 gtk_color_selection_dialog.
4451
4452 <tscreen><verb>
4453 GtkWidget *gtk_color_selection_new( void );
4454 </verb></tscreen>
4455         
4456 You'll probably not be using this constructor directly. It creates an
4457 orphan GtkColorSelection widget which you'll have to parent
4458 yourself. The GtkColorSelection widget inherits from the GtkVBox
4459 widget.
4460
4461 <tscreen><verb> 
4462 GtkWidget *gtk_color_selection_dialog_new( const gchar *title );
4463 </verb></tscreen>
4464
4465 This is the most common color selection constructor. It creates a
4466 GtkColorSelectionDialog, which inherits from a GtkDialog. It consists
4467 of a GtkFrame containing a GtkColorSelection widget, a GtkHSeparator
4468 and a GtkHBox with three buttons, "Ok", "Cancel" and "Help". You can
4469 reach these buttons by accessing the "ok_button", "cancel_button" and
4470 "help_button" widgets in the GtkColorSelectionDialog structure,
4471 (i.e. GTK_COLOR_SELECTION_DIALOG(colorseldialog)->ok_button).
4472
4473 <tscreen><verb>
4474 void gtk_color_selection_set_update_policy( GtkColorSelection *colorsel, 
4475                                             GtkUpdateType      policy );
4476 </verb></tscreen>
4477
4478 This function sets the update policy. The default policy is
4479 GTK_UPDATE_CONTINUOUS which means that the current color is updated
4480 continuously when the user drags the sliders or presses the mouse and
4481 drags in the hue-saturation wheel or value bar. If you experience
4482 performance problems, you may want to set the policy to
4483 GTK_UPDATE_DISCONTINUOUS or GTK_UPDATE_DELAYED.
4484
4485 <tscreen><verb>
4486 void gtk_color_selection_set_opacity( GtkColorSelection *colorsel,
4487                                       gint               use_opacity );
4488 </verb></tscreen>
4489
4490 The color selection widget supports adjusting the opacity of a color
4491 (also known as the alpha channel). This is disabled by
4492 default. Calling this function with use_opacity set to TRUE enables
4493 opacity. Likewise, use_opacity set to FALSE will disable opacity.
4494
4495 <tscreen><verb>
4496 void gtk_color_selection_set_color( GtkColorSelection *colorsel,
4497                                     gdouble           *color );
4498 </verb></tscreen>
4499
4500 You can set the current color explicitly by calling this function with
4501 a pointer to an array of colors (gdouble). The length of the array
4502 depends on whether opacity is enabled or not. Position 0 contains the
4503 red component, 1 is green, 2 is blue and opacity is at position 3
4504 (only if opacity is enabled, see
4505 gtk_color_selection_set_opacity()). All values are between 0.0 and
4506 1.0.
4507
4508 <tscreen><verb>
4509 void gtk_color_selection_get_color( GtkColorSelection *colorsel,
4510                                     gdouble           *color );
4511 </verb></tscreen>
4512
4513 When you need to query the current color, typically when you've
4514 received a "color_changed" signal, you use this function. Color is a
4515 pointer to the array of colors to fill in. See the
4516 gtk_color_selection_set_color() function for the description of this
4517 array.
4518
4519 <!-- Need to do a whole section on DnD - TRG
4520 Drag and drop
4521 -------------
4522
4523 The color sample areas (right under the hue-saturation wheel) supports
4524 drag and drop. The type of drag and drop is "application/x-color". The
4525 message data consists of an array of 4 (or 5 if opacity is enabled)
4526 gdouble values, where the value at position 0 is 0.0 (opacity on) or
4527 1.0 (opacity off) followed by the red, green and blue values at
4528 positions 1,2 and 3 respectively.  If opacity is enabled, the opacity
4529 is passed in the value at position 4.
4530 -->
4531
4532 Here's a simple example demonstrating the use of the
4533 GtkColorSelectionDialog. The program displays a window containing a
4534 drawing area. Clicking on it opens a color selection dialog, and
4535 changing the color in the color selection dialog changes the
4536 background color.
4537
4538 <tscreen><verb>
4539 /* example-start colorsel colorsel.c */
4540
4541 #include <glib.h>
4542 #include <gdk/gdk.h>
4543 #include <gtk/gtk.h>
4544
4545 GtkWidget *colorseldlg = NULL;
4546 GtkWidget *drawingarea = NULL;
4547
4548 /* Color changed handler */
4549
4550 void color_changed_cb (GtkWidget *widget, GtkColorSelection *colorsel)
4551 {
4552   gdouble color[3];
4553   GdkColor gdk_color;
4554   GdkColormap *colormap;
4555
4556   /* Get drawingarea colormap */
4557
4558   colormap = gdk_window_get_colormap (drawingarea->window);
4559
4560   /* Get current color */
4561
4562   gtk_color_selection_get_color (colorsel,color);
4563
4564   /* Fit to a unsigned 16 bit integer (0..65535) and insert into the GdkColor structure */
4565
4566   gdk_color.red = (guint16)(color[0]*65535.0);
4567   gdk_color.green = (guint16)(color[1]*65535.0);
4568   gdk_color.blue = (guint16)(color[2]*65535.0);
4569
4570   /* Allocate color */
4571
4572   gdk_color_alloc (colormap, &amp;gdk_color);
4573
4574   /* Set window background color */
4575
4576   gdk_window_set_background (drawingarea->window, &amp;gdk_color);
4577
4578   /* Clear window */
4579
4580   gdk_window_clear (drawingarea->window);
4581 }
4582
4583 /* Drawingarea event handler */
4584
4585 gint area_event (GtkWidget *widget, GdkEvent *event, gpointer client_data)
4586 {
4587   gint handled = FALSE;
4588   GtkWidget *colorsel;
4589
4590   /* Check if we've received a button pressed event */
4591
4592   if (event->type == GDK_BUTTON_PRESS &amp;&amp; colorseldlg == NULL)
4593     {
4594       /* Yes, we have an event and there's no colorseldlg yet! */
4595
4596       handled = TRUE;
4597
4598       /* Create color selection dialog */
4599
4600       colorseldlg = gtk_color_selection_dialog_new("Select background color");
4601
4602       /* Get the GtkColorSelection widget */
4603
4604       colorsel = GTK_COLOR_SELECTION_DIALOG(colorseldlg)->colorsel;
4605
4606       /* Connect to the "color_changed" signal, set the client-data to the colorsel widget */
4607
4608       gtk_signal_connect(GTK_OBJECT(colorsel), "color_changed",
4609         (GtkSignalFunc)color_changed_cb, (gpointer)colorsel);
4610
4611       /* Show the dialog */
4612
4613       gtk_widget_show(colorseldlg);
4614     }
4615
4616   return handled;
4617 }
4618
4619 /* Close down and exit handler */
4620
4621 void destroy_window (GtkWidget *widget, gpointer client_data)
4622 {
4623   gtk_main_quit ();
4624 }
4625
4626 /* Main */
4627
4628 gint main (gint argc, gchar *argv[])
4629 {
4630   GtkWidget *window;
4631
4632   /* Initialize the toolkit, remove gtk-related commandline stuff */
4633
4634   gtk_init (&amp;argc,&amp;argv);
4635
4636   /* Create toplevel window, set title and policies */
4637
4638   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
4639   gtk_window_set_title (GTK_WINDOW(window), "Color selection test");
4640   gtk_window_set_policy (GTK_WINDOW(window), TRUE, TRUE, TRUE);
4641
4642   /* Attach to the "delete" and "destroy" events so we can exit */
4643
4644   gtk_signal_connect (GTK_OBJECT(window), "delete_event",
4645     (GtkSignalFunc)destroy_window, (gpointer)window);
4646
4647   gtk_signal_connect (GTK_OBJECT(window), "destroy",
4648     (GtkSignalFunc)destroy_window, (gpointer)window);
4649   
4650   /* Create drawingarea, set size and catch button events */
4651
4652   drawingarea = gtk_drawing_area_new ();
4653
4654   gtk_drawing_area_size (GTK_DRAWING_AREA(drawingarea), 200, 200);
4655
4656   gtk_widget_set_events (drawingarea, GDK_BUTTON_PRESS_MASK);
4657
4658   gtk_signal_connect (GTK_OBJECT(drawingarea), "event", 
4659     (GtkSignalFunc)area_event, (gpointer)drawingarea);
4660   
4661   /* Add drawingarea to window, then show them both */
4662
4663   gtk_container_add (GTK_CONTAINER(window), drawingarea);
4664
4665   gtk_widget_show (drawingarea);
4666   gtk_widget_show (window);
4667   
4668   /* Enter the gtk main loop (this never returns) */
4669
4670   gtk_main ();
4671
4672   /* Satisfy grumpy compilers */
4673
4674   return(0);
4675 }
4676 /* example-end */
4677 </verb></tscreen>
4678
4679 <!-- ----------------------------------------------------------------- -->
4680 <sect1> File Selections
4681 <p>
4682 The file selection widget is a quick and simple way to display a File
4683 dialog box. It comes complete with Ok, Cancel, and Help buttons, a
4684 great way to cut down on programming time.
4685
4686 To create a new file selection box use:
4687
4688 <tscreen><verb>
4689 GtkWidget *gtk_file_selection_new( gchar *title );
4690 </verb></tscreen>
4691
4692 To set the filename, for example to bring up a specific directory, or
4693 give a default filename, use this function:
4694
4695 <tscreen><verb>
4696 void gtk_file_selection_set_filename( GtkFileSelection *filesel,
4697                                       gchar            *filename );
4698 </verb></tscreen>
4699
4700 To grab the text that the user has entered or clicked on, use this 
4701 function:
4702
4703 <tscreen><verb>
4704 gchar *gtk_file_selection_get_filename( GtkFileSelection *filesel );
4705 </verb></tscreen>
4706
4707 There are also pointers to the widgets contained within the file 
4708 selection widget. These are:
4709
4710 <itemize>
4711 <item>dir_list
4712 <item>file_list
4713 <item>selection_entry
4714 <item>selection_text
4715 <item>main_vbox
4716 <item>ok_button
4717 <item>cancel_button
4718 <item>help_button
4719 </itemize>
4720
4721 Most likely you will want to use the ok_button, cancel_button, and
4722 help_button pointers in signaling their use.
4723
4724 Included here is an example stolen from testgtk.c, modified to run on
4725 its own. As you will see, there is nothing much to creating a file
4726 selection widget. While in this example the Help button appears on the
4727 screen, it does nothing as there is not a signal attached to it.
4728
4729 <tscreen><verb>
4730 /* example-start filesel filesel.c */
4731
4732 #include <gtk/gtk.h>
4733
4734 /* Get the selected filename and print it to the console */
4735 void file_ok_sel (GtkWidget *w, GtkFileSelection *fs)
4736 {
4737     g_print ("%s\n", gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs)));
4738 }
4739
4740 void destroy (GtkWidget *widget, gpointer data)
4741 {
4742     gtk_main_quit ();
4743 }
4744
4745 int main (int argc, char *argv[])
4746 {
4747     GtkWidget *filew;
4748     
4749     gtk_init (&amp;argc, &amp;argv);
4750     
4751     /* Create a new file selection widget */
4752     filew = gtk_file_selection_new ("File selection");
4753     
4754     gtk_signal_connect (GTK_OBJECT (filew), "destroy",
4755                         (GtkSignalFunc) destroy, &amp;filew);
4756     /* Connect the ok_button to file_ok_sel function */
4757     gtk_signal_connect (GTK_OBJECT (GTK_FILE_SELECTION (filew)->ok_button),
4758                         "clicked", (GtkSignalFunc) file_ok_sel, filew );
4759     
4760     /* Connect the cancel_button to destroy the widget */
4761     gtk_signal_connect_object (GTK_OBJECT (GTK_FILE_SELECTION (filew)->cancel_button),
4762                                "clicked", (GtkSignalFunc) gtk_widget_destroy,
4763                                GTK_OBJECT (filew));
4764     
4765     /* Lets set the filename, as if this were a save dialog, and we are giving
4766      a default filename */
4767     gtk_file_selection_set_filename (GTK_FILE_SELECTION(filew), 
4768                                      "penguin.png");
4769     
4770     gtk_widget_show(filew);
4771     gtk_main ();
4772     return 0;
4773 }
4774 /* example-end */
4775 </verb></tscreen>
4776
4777 <!-- ***************************************************************** -->
4778 <sect> Container Widgets
4779 <!-- ***************************************************************** -->
4780
4781 <!-- ----------------------------------------------------------------- -->
4782 <sect1> Notebooks
4783 <p>
4784 The NoteBook Widget is a collection of 'pages' that overlap each other,
4785 each page contains different information.  This widget has become more common 
4786 lately in GUI programming, and it is a good way to show blocks of similar 
4787 information that warrant separation in their display.
4788
4789 The first function call you will need to know, as you can probably 
4790 guess by now, is used to create a new notebook widget.
4791
4792 <tscreen><verb>
4793 GtkWidget *gtk_notebook_new( void );
4794 </verb></tscreen>
4795
4796 Once the notebook has been created, there are 12 functions that 
4797 operate on the notebook widget. Let's look at them individually.
4798
4799 The first one we will look at is how to position the page indicators.
4800 These page indicators or 'tabs' as they are referred to, can be positioned
4801 in four ways: top, bottom, left, or right.
4802
4803 <tscreen><verb>
4804 void gtk_notebook_set_tab_pos( GtkNotebook     *notebook,
4805                                GtkPositionType  pos );
4806 </verb></tscreen>
4807
4808 GtkPostionType will be one of the following, and they are pretty self explanatory:
4809 <itemize>
4810 <item> GTK_POS_LEFT
4811 <item> GTK_POS_RIGHT
4812 <item> GTK_POS_TOP
4813 <item> GTK_POS_BOTTOM
4814 </itemize>
4815
4816 GTK_POS_TOP is the default.
4817
4818 Next we will look at how to add pages to the notebook. There are three
4819 ways to add pages to the NoteBook.  Let's look at the first two together as 
4820 they are quite similar.
4821
4822 <tscreen><verb>
4823 void gtk_notebook_append_page( GtkNotebook *notebook,
4824                                GtkWidget   *child,
4825                                GtkWidget   *tab_label );
4826
4827 void gtk_notebook_prepend_page( GtkNotebook *notebook,
4828                                 GtkWidget   *child,
4829                                 GtkWidget   *tab_label );
4830 </verb></tscreen>
4831
4832 These functions add pages to the notebook by inserting them from the 
4833 back of the notebook (append), or the front of the notebook (prepend).  
4834 <tt/child/ is the widget that is placed within the notebook page, and
4835 <tt/tab_label/ is the label for the page being added.
4836
4837 The final function for adding a page to the notebook contains all of 
4838 the properties of the previous two, but it allows you to specify what position
4839 you want the page to be in the notebook.
4840
4841 <tscreen><verb>
4842 void gtk_notebook_insert_page( GtkNotebook *notebook,
4843                                GtkWidget   *child,
4844                                GtkWidget   *tab_label,
4845                                gint         position );
4846 </verb></tscreen>
4847
4848 The parameters are the same as _append_ and _prepend_ except it 
4849 contains an extra parameter, <tt/position/.  This parameter is used to
4850 specify what place this page will be inserted into.
4851
4852 Now that we know how to add a page, lets see how we can remove a page 
4853 from the notebook.
4854
4855 <tscreen><verb>
4856 void gtk_notebook_remove_page( GtkNotebook *notebook,
4857                                gint         page_num );
4858 </verb></tscreen>
4859
4860 This function takes the page specified by page_num and removes it from 
4861 the widget pointed to by <tt/notebook/.
4862
4863 To find out what the current page is in a notebook use the function:
4864
4865 <tscreen><verb>
4866 gint gtk_notebook_current_page( GtkNotebook *notebook );
4867 </verb></tscreen>
4868
4869 These next two functions are simple calls to move the notebook page 
4870 forward or backward. Simply provide the respective function call with the
4871 notebook widget you wish to operate on.  Note: when the NoteBook is currently
4872 on the last page, and gtk_notebook_next_page is called, the notebook will 
4873 wrap back to the first page. Likewise, if the NoteBook is on the first page, 
4874 and gtk_notebook_prev_page is called, the notebook will wrap to the last page.
4875
4876 <tscreen><verb>
4877 void gtk_notebook_next_page( GtkNoteBook *notebook );
4878
4879 void gtk_notebook_prev_page( GtkNoteBook *notebook );
4880 </verb></tscreen>
4881
4882 This next function sets the 'active' page. If you wish the
4883 notebook to be opened to page 5 for example, you would use this function.  
4884 Without using this function, the notebook defaults to the first page.
4885
4886 <tscreen><verb>
4887 void gtk_notebook_set_page( GtkNotebook *notebook,
4888                             gint         page_num );
4889 </verb></tscreen>
4890
4891 The next two functions add or remove the notebook page tabs and the 
4892 notebook border respectively.
4893
4894 <tscreen><verb>
4895 void gtk_notebook_set_show_tabs( GtkNotebook *notebook,
4896                                  gint         show_tabs);
4897
4898 void gtk_notebook_set_show_border( GtkNotebook *notebook,
4899                                    gint         show_border );
4900 </verb></tscreen>
4901
4902 show_tabs and show_border can be either TRUE or FALSE.
4903
4904 Now lets look at an example, it is expanded from the testgtk.c code 
4905 that comes with the GTK distribution, and it shows all 13 functions. This 
4906 small program creates a window with a notebook and six buttons. The notebook 
4907 contains 11 pages, added in three different ways, appended, inserted, and 
4908 prepended. The buttons allow you rotate the tab positions, add/remove the tabs
4909 and border, remove a page, change pages in both a forward and backward manner,
4910 and exit the program. 
4911
4912 <tscreen><verb>
4913 /* example-start notebook notebook.c */
4914
4915 #include <gtk/gtk.h>
4916
4917 /* This function rotates the position of the tabs */
4918 void rotate_book (GtkButton *button, GtkNotebook *notebook)
4919 {
4920     gtk_notebook_set_tab_pos (notebook, (notebook->tab_pos +1) %4);
4921 }
4922
4923 /* Add/Remove the page tabs and the borders */
4924 void tabsborder_book (GtkButton *button, GtkNotebook *notebook)
4925 {
4926     gint tval = FALSE;
4927     gint bval = FALSE;
4928     if (notebook->show_tabs == 0)
4929             tval = TRUE; 
4930     if (notebook->show_border == 0)
4931             bval = TRUE;
4932     
4933     gtk_notebook_set_show_tabs (notebook, tval);
4934     gtk_notebook_set_show_border (notebook, bval);
4935 }
4936
4937 /* Remove a page from the notebook */
4938 void remove_book (GtkButton *button, GtkNotebook *notebook)
4939 {
4940     gint page;
4941     
4942     page = gtk_notebook_current_page(notebook);
4943     gtk_notebook_remove_page (notebook, page);
4944     /* Need to refresh the widget -- 
4945      This forces the widget to redraw itself. */
4946     gtk_widget_draw(GTK_WIDGET(notebook), NULL);
4947 }
4948
4949 void delete (GtkWidget *widget, GtkWidget *event, gpointer data)
4950 {
4951     gtk_main_quit ();
4952 }
4953
4954 int main (int argc, char *argv[])
4955 {
4956     GtkWidget *window;
4957     GtkWidget *button;
4958     GtkWidget *table;
4959     GtkWidget *notebook;
4960     GtkWidget *frame;
4961     GtkWidget *label;
4962     GtkWidget *checkbutton;
4963     int i;
4964     char bufferf[32];
4965     char bufferl[32];
4966     
4967     gtk_init (&amp;argc, &amp;argv);
4968     
4969     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
4970     
4971     gtk_signal_connect (GTK_OBJECT (window), "delete_event",
4972                         GTK_SIGNAL_FUNC (delete), NULL);
4973     
4974     gtk_container_border_width (GTK_CONTAINER (window), 10);
4975     
4976     table = gtk_table_new(2,6,TRUE);
4977     gtk_container_add (GTK_CONTAINER (window), table);
4978     
4979     /* Create a new notebook, place the position of the tabs */
4980     notebook = gtk_notebook_new ();
4981     gtk_notebook_set_tab_pos (GTK_NOTEBOOK (notebook), GTK_POS_TOP);
4982     gtk_table_attach_defaults(GTK_TABLE(table), notebook, 0,6,0,1);
4983     gtk_widget_show(notebook);
4984     
4985     /* lets append a bunch of pages to the notebook */
4986     for (i=0; i < 5; i++) {
4987         sprintf(bufferf, "Append Frame %d", i+1);
4988         sprintf(bufferl, "Page %d", i+1);
4989         
4990         frame = gtk_frame_new (bufferf);
4991         gtk_container_border_width (GTK_CONTAINER (frame), 10);
4992         gtk_widget_set_usize (frame, 100, 75);
4993         gtk_widget_show (frame);
4994         
4995         label = gtk_label_new (bufferf);
4996         gtk_container_add (GTK_CONTAINER (frame), label);
4997         gtk_widget_show (label);
4998         
4999         label = gtk_label_new (bufferl);
5000         gtk_notebook_append_page (GTK_NOTEBOOK (notebook), frame, label);
5001     }
5002     
5003     
5004     /* now lets add a page to a specific spot */
5005     checkbutton = gtk_check_button_new_with_label ("Check me please!");
5006     gtk_widget_set_usize(checkbutton, 100, 75);
5007     gtk_widget_show (checkbutton);
5008     
5009     label = gtk_label_new ("Add spot");
5010     gtk_container_add (GTK_CONTAINER (checkbutton), label);
5011     gtk_widget_show (label);
5012     label = gtk_label_new ("Add page");
5013     gtk_notebook_insert_page (GTK_NOTEBOOK (notebook), checkbutton, label, 2);
5014     
5015     /* Now finally lets prepend pages to the notebook */
5016     for (i=0; i < 5; i++) {
5017         sprintf(bufferf, "Prepend Frame %d", i+1);
5018         sprintf(bufferl, "PPage %d", i+1);
5019         
5020         frame = gtk_frame_new (bufferf);
5021         gtk_container_border_width (GTK_CONTAINER (frame), 10);
5022         gtk_widget_set_usize (frame, 100, 75);
5023         gtk_widget_show (frame);
5024         
5025         label = gtk_label_new (bufferf);
5026         gtk_container_add (GTK_CONTAINER (frame), label);
5027         gtk_widget_show (label);
5028         
5029         label = gtk_label_new (bufferl);
5030         gtk_notebook_prepend_page (GTK_NOTEBOOK(notebook), frame, label);
5031     }
5032     
5033     /* Set what page to start at (page 4) */
5034     gtk_notebook_set_page (GTK_NOTEBOOK(notebook), 3);
5035     
5036     
5037     /* create a bunch of buttons */
5038     button = gtk_button_new_with_label ("close");
5039     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
5040                                GTK_SIGNAL_FUNC (delete), NULL);
5041     gtk_table_attach_defaults(GTK_TABLE(table), button, 0,1,1,2);
5042     gtk_widget_show(button);
5043     
5044     button = gtk_button_new_with_label ("next page");
5045     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
5046                                (GtkSignalFunc) gtk_notebook_next_page,
5047                                GTK_OBJECT (notebook));
5048     gtk_table_attach_defaults(GTK_TABLE(table), button, 1,2,1,2);
5049     gtk_widget_show(button);
5050     
5051     button = gtk_button_new_with_label ("prev page");
5052     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
5053                                (GtkSignalFunc) gtk_notebook_prev_page,
5054                                GTK_OBJECT (notebook));
5055     gtk_table_attach_defaults(GTK_TABLE(table), button, 2,3,1,2);
5056     gtk_widget_show(button);
5057     
5058     button = gtk_button_new_with_label ("tab position");
5059     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
5060                                (GtkSignalFunc) rotate_book, GTK_OBJECT(notebook));
5061     gtk_table_attach_defaults(GTK_TABLE(table), button, 3,4,1,2);
5062     gtk_widget_show(button);
5063     
5064     button = gtk_button_new_with_label ("tabs/border on/off");
5065     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
5066                                (GtkSignalFunc) tabsborder_book,
5067                                GTK_OBJECT (notebook));
5068     gtk_table_attach_defaults(GTK_TABLE(table), button, 4,5,1,2);
5069     gtk_widget_show(button);
5070     
5071     button = gtk_button_new_with_label ("remove page");
5072     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
5073                                (GtkSignalFunc) remove_book,
5074                                GTK_OBJECT(notebook));
5075     gtk_table_attach_defaults(GTK_TABLE(table), button, 5,6,1,2);
5076     gtk_widget_show(button);
5077     
5078     gtk_widget_show(table);
5079     gtk_widget_show(window);
5080     
5081     gtk_main ();
5082     
5083     return 0;
5084 }
5085 /* example-end */
5086 </verb></tscreen>
5087
5088 Hopefully this helps you on your way with creating notebooks for your
5089 GTK applications.
5090
5091 <!-- ----------------------------------------------------------------- -->
5092 <sect1>Scrolled Windows
5093 <p>
5094 Scrolled windows are used to create a scrollable area inside a real window.  
5095 You may insert any type of widget into a scrolled window, and it will
5096 be accessible regardless of the size by using the scrollbars.
5097
5098 The following function is used to create a new scrolled window.
5099
5100 <tscreen><verb>
5101 GtkWidget *gtk_scrolled_window_new( GtkAdjustment *hadjustment,
5102                                     GtkAdjustment *vadjustment );
5103 </verb></tscreen>
5104
5105 Where the first argument is the adjustment for the horizontal
5106 direction, and the second, the adjustment for the vertical direction.
5107 These are almost always set to NULL.
5108
5109 <tscreen><verb>
5110 void gtk_scrolled_window_set_policy( GtkScrolledWindow *scrolled_window,
5111                                      GtkPolicyType      hscrollbar_policy,
5112                                      GtkPolicyType      vscrollbar_policy );
5113 </verb></tscreen>
5114
5115 This sets the policy to be used with respect to the scrollbars.
5116 The first argument is the scrolled window you wish to change. The second
5117 sets the policy for the horizontal scrollbar, and the third the policy for 
5118 the vertical scrollbar.
5119
5120 The policy may be one of GTK_POLICY AUTOMATIC, or GTK_POLICY_ALWAYS.
5121 GTK_POLICY_AUTOMATIC will automatically decide whether you need
5122 scrollbars, whereas GTK_POLICY_ALWAYS will always leave the scrollbars
5123 there.
5124
5125 Here is a simple example that packs 100 toggle buttons into a scrolled window.
5126 I've only commented on the parts that may be new to you.
5127
5128 <tscreen><verb>
5129 /* example-start scrolledwin scrolledwin.c */
5130
5131 #include <gtk/gtk.h>
5132
5133 void destroy(GtkWidget *widget, gpointer data)
5134 {
5135     gtk_main_quit();
5136 }
5137
5138 int main (int argc, char *argv[])
5139 {
5140     static GtkWidget *window;
5141     GtkWidget *scrolled_window;
5142     GtkWidget *table;
5143     GtkWidget *button;
5144     char buffer[32];
5145     int i, j;
5146     
5147     gtk_init (&amp;argc, &amp;argv);
5148     
5149     /* Create a new dialog window for the scrolled window to be
5150      * packed into.  A dialog is just like a normal window except it has a 
5151      * vbox and a horizontal separator packed into it.  It's just a shortcut
5152      * for creating dialogs */
5153     window = gtk_dialog_new ();
5154     gtk_signal_connect (GTK_OBJECT (window), "destroy",
5155                         (GtkSignalFunc) destroy, NULL);
5156     gtk_window_set_title (GTK_WINDOW (window), "dialog");
5157     gtk_container_border_width (GTK_CONTAINER (window), 0);
5158     gtk_widget_set_usize(window, 300, 300);
5159     
5160     /* create a new scrolled window. */
5161     scrolled_window = gtk_scrolled_window_new (NULL, NULL);
5162     
5163     gtk_container_border_width (GTK_CONTAINER (scrolled_window), 10);
5164     
5165     /* the policy is one of GTK_POLICY AUTOMATIC, or GTK_POLICY_ALWAYS.
5166      * GTK_POLICY_AUTOMATIC will automatically decide whether you need
5167      * scrollbars, whereas GTK_POLICY_ALWAYS will always leave the scrollbars
5168      * there.  The first one is the horizontal scrollbar, the second, 
5169      * the vertical. */
5170     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
5171                                     GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
5172     /* The dialog window is created with a vbox packed into it. */                                                              
5173     gtk_box_pack_start (GTK_BOX (GTK_DIALOG(window)->vbox), scrolled_window, 
5174                         TRUE, TRUE, 0);
5175     gtk_widget_show (scrolled_window);
5176     
5177     /* create a table of 10 by 10 squares. */
5178     table = gtk_table_new (10, 10, FALSE);
5179     
5180     /* set the spacing to 10 on x and 10 on y */
5181     gtk_table_set_row_spacings (GTK_TABLE (table), 10);
5182     gtk_table_set_col_spacings (GTK_TABLE (table), 10);
5183     
5184     /* pack the table into the scrolled window */
5185     gtk_container_add (GTK_CONTAINER (scrolled_window), table);
5186     gtk_widget_show (table);
5187     
5188     /* this simply creates a grid of toggle buttons on the table
5189      * to demonstrate the scrolled window. */
5190     for (i = 0; i < 10; i++)
5191        for (j = 0; j < 10; j++) {
5192           sprintf (buffer, "button (%d,%d)\n", i, j);
5193           button = gtk_toggle_button_new_with_label (buffer);
5194           gtk_table_attach_defaults (GTK_TABLE (table), button,
5195                                      i, i+1, j, j+1);
5196           gtk_widget_show (button);
5197        }
5198     
5199     /* Add a "close" button to the bottom of the dialog */
5200     button = gtk_button_new_with_label ("close");
5201     gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
5202                                (GtkSignalFunc) gtk_widget_destroy,
5203                                GTK_OBJECT (window));
5204     
5205     /* this makes it so the button is the default. */
5206     
5207     GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
5208     gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->action_area), button, TRUE, TRUE, 0);
5209     
5210     /* This grabs this button to be the default button. Simply hitting
5211      * the "Enter" key will cause this button to activate. */
5212     gtk_widget_grab_default (button);
5213     gtk_widget_show (button);
5214     
5215     gtk_widget_show (window);
5216     
5217     gtk_main();
5218     
5219     return(0);
5220 }
5221 /* example-end */
5222 </verb></tscreen>
5223
5224 Try playing with resizing the window. You'll notice how the scrollbars
5225 react. You may also wish to use the gtk_widget_set_usize() call to set
5226 the default size of the window or other widgets.
5227
5228 <!-- ----------------------------------------------------------------- -->   
5229 <sect1> Paned Window Widgets
5230 <p>
5231 The paned window widgets are useful when you want to divide an area
5232 into two parts, with the relative size of the two parts controlled by
5233 the user. A groove is drawn between the two portions with a handle
5234 that the user can drag to change the ratio. The division can either
5235 be horizontal (HPaned) or vertical (VPaned).
5236    
5237 To create a new paned window, call one of:
5238    
5239 <tscreen><verb>
5240 GtkWidget *gtk_hpaned_new (void);
5241
5242 GtkWidget *gtk_vpaned_new (void);
5243 </verb></tscreen>
5244
5245 After creating the paned window widget, you need to add child widgets
5246 to its two halves. To do this, use the functions:
5247    
5248 <tscreen><verb>
5249 void gtk_paned_add1 (GtkPaned *paned, GtkWidget *child);
5250
5251 void gtk_paned_add2 (GtkPaned *paned, GtkWidget *child);
5252 </verb></tscreen>
5253    
5254 <tt/gtk_paned_add1()/ adds the child widget to the left or top half of
5255 the paned window. <tt/gtk_paned_add2()/ adds the child widget to the
5256 right or bottom half of the paned window.
5257    
5258 As an example, we will create part of the user interface of an
5259 imaginary email program. A window is divided into two portions
5260 vertically, with the top portion being a list of email messages and
5261 the bottom portion the text of the email message. Most of the program
5262 is pretty straightforward. A couple of points to note: text can't
5263 be added to a Text widget until it is realized. This could be done by
5264 calling <tt/gtk_widget_realize()/, but as a demonstration of an alternate
5265 technique, we connect a handler to the "realize" signal to add the
5266 text. Also, we need to add the <tt/GTK_SHRINK/ option to some of the
5267 items in the table containing the text window and its scrollbars, so
5268 that when the bottom portion is made smaller, the correct portions
5269 shrink instead of being pushed off the bottom of the window.
5270
5271 <tscreen><verb>
5272 /* example-start paned paned.c */
5273
5274 #include <gtk/gtk.h>
5275    
5276 /* Create the list of "messages" */
5277 GtkWidget *
5278 create_list (void)
5279 {
5280
5281     GtkWidget *scrolled_window;
5282     GtkWidget *list;
5283     GtkWidget *list_item;
5284    
5285     int i;
5286     char buffer[16];
5287    
5288     /* Create a new scrolled window, with scrollbars only if needed */
5289     scrolled_window = gtk_scrolled_window_new (NULL, NULL);
5290     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
5291                                     GTK_POLICY_AUTOMATIC, 
5292                                     GTK_POLICY_AUTOMATIC);
5293    
5294     /* Create a new list and put it in the scrolled window */
5295     list = gtk_list_new ();
5296     gtk_container_add (GTK_CONTAINER(scrolled_window), list);
5297     gtk_widget_show (list);
5298    
5299     /* Add some messages to the window */
5300     for (i=0; i<10; i++) {
5301
5302         sprintf(buffer,"Message #%d",i);
5303         list_item = gtk_list_item_new_with_label (buffer);
5304         gtk_container_add (GTK_CONTAINER(list), list_item);
5305         gtk_widget_show (list_item);
5306
5307     }
5308    
5309     return scrolled_window;
5310 }
5311    
5312 /* Add some text to our text widget - this is a callback that is invoked
5313 when our window is realized. We could also force our window to be
5314 realized with gtk_widget_realize, but it would have to be part of
5315 a hierarchy first */
5316
5317 void
5318 realize_text (GtkWidget *text, gpointer data)
5319 {
5320     gtk_text_freeze (GTK_TEXT (text));
5321     gtk_text_insert (GTK_TEXT (text), NULL, &amp;text->style->black, NULL,
5322     "From: pathfinder@nasa.gov\n"
5323     "To: mom@nasa.gov\n"
5324     "Subject: Made it!\n"
5325     "\n"
5326     "We just got in this morning. The weather has been\n"
5327     "great - clear but cold, and there are lots of fun sights.\n"
5328     "Sojourner says hi. See you soon.\n"
5329     " -Path\n", -1);
5330    
5331     gtk_text_thaw (GTK_TEXT (text));
5332 }
5333    
5334 /* Create a scrolled text area that displays a "message" */
5335 GtkWidget *
5336 create_text (void)
5337 {
5338     GtkWidget *table;
5339     GtkWidget *text;
5340     GtkWidget *hscrollbar;
5341     GtkWidget *vscrollbar;
5342    
5343     /* Create a table to hold the text widget and scrollbars */
5344     table = gtk_table_new (2, 2, FALSE);
5345    
5346     /* Put a text widget in the upper left hand corner. Note the use of
5347      * GTK_SHRINK in the y direction */
5348     text = gtk_text_new (NULL, NULL);
5349     gtk_table_attach (GTK_TABLE (table), text, 0, 1, 0, 1,
5350                       GTK_FILL | GTK_EXPAND,
5351                       GTK_FILL | GTK_EXPAND | GTK_SHRINK, 0, 0);
5352     gtk_widget_show (text);
5353    
5354     /* Put a HScrollbar in the lower left hand corner */
5355     hscrollbar = gtk_hscrollbar_new (GTK_TEXT (text)->hadj);
5356     gtk_table_attach (GTK_TABLE (table), hscrollbar, 0, 1, 1, 2,
5357                       GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
5358     gtk_widget_show (hscrollbar);
5359    
5360     /* And a VScrollbar in the upper right */
5361     vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
5362     gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
5363                       GTK_FILL, GTK_EXPAND | GTK_FILL | GTK_SHRINK, 0, 0);
5364     gtk_widget_show (vscrollbar);
5365    
5366     /* Add a handler to put a message in the text widget when it is realized */
5367     gtk_signal_connect (GTK_OBJECT (text), "realize",
5368                         GTK_SIGNAL_FUNC (realize_text), NULL);
5369    
5370     return table;
5371 }
5372    
5373 int
5374 main (int argc, char *argv[])
5375 {
5376     GtkWidget *window;
5377     GtkWidget *vpaned;
5378     GtkWidget *list;
5379     GtkWidget *text;
5380
5381     gtk_init (&amp;argc, &amp;argv);
5382    
5383     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
5384     gtk_window_set_title (GTK_WINDOW (window), "Paned Windows");
5385     gtk_signal_connect (GTK_OBJECT (window), "destroy",
5386                         GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
5387     gtk_container_border_width (GTK_CONTAINER (window), 10);
5388    
5389     /* create a vpaned widget and add it to our toplevel window */
5390    
5391     vpaned = gtk_vpaned_new ();
5392     gtk_container_add (GTK_CONTAINER(window), vpaned);
5393     gtk_widget_show (vpaned);
5394    
5395     /* Now create the contents of the two halves of the window */
5396    
5397     list = create_list ();
5398     gtk_paned_add1 (GTK_PANED(vpaned), list);
5399     gtk_widget_show (list);
5400    
5401     text = create_text ();
5402     gtk_paned_add2 (GTK_PANED(vpaned), text);
5403     gtk_widget_show (text);
5404     gtk_widget_show (window);
5405     gtk_main ();
5406     return 0;
5407 }
5408 /* example-end */
5409 </verb></tscreen>
5410
5411 <!-- ----------------------------------------------------------------- -->   
5412 <sect1>Toolbar
5413 <p>
5414 Toolbars are usually used to group some number of widgets in order to simplify
5415 customization of their look and layout. Typically a toolbar consists of buttons
5416 with icons, labels and tooltips, but any other widget can also
5417 be put inside a toolbar.  Finally, items can be arranged horizontally
5418 or vertically and buttons can be displayed with icons, labels or both.
5419
5420 Creating a toolbar is (as one may already suspect) done with the following
5421 function:
5422
5423 <tscreen><verb>
5424 GtkWidget *gtk_toolbar_new( GtkOrientation orientation,
5425                             GtkToolbarStyle  style );
5426 </verb></tscreen>
5427
5428 where orientation may be one of:
5429
5430 <tscreen><verb>
5431   GTK_ORIENTATION_HORIZONTAL    
5432   GTK_ORIENTATION_VERTICAL
5433 </verb></tscreen>
5434
5435 and style one of:
5436
5437 <tscreen><verb>
5438   GTK_TOOLBAR_TEXT
5439   GTK_TOOLBAR_ICONS
5440   GTK_TOOLBAR_BOTH
5441 </verb></tscreen>
5442
5443 The style applies to all the buttons created with the `item' functions
5444 (not to buttons inserted into toolbar as separate widgets).
5445
5446 After creating a toolbar one can append,prepend and insert items (that
5447 means simple buttons) into the toolbar. To describe an item we need a
5448 label text, a tooltip text, a private tooltip text, an icon
5449 for the button and a callback function for it. For example, to append
5450 an item you may use the following function:
5451
5452 <tscreen><verb>
5453 GtkWidget *gtk_toolbar_append_item( GtkToolbar    *toolbar,
5454                                     const char    *text,
5455                                     const char    *tooltip_text,
5456                                     const char    *tooltip_private_text,
5457                                     GtkWidget     *icon,
5458                                     GtkSignalFunc  callback,
5459                                     gpointer       user_data );
5460 </verb></tscreen>
5461
5462 If you want to use gtk_toolbar_insert_item, the only additional parameter
5463 which must be specified is the position in which the item should be inserted.
5464
5465 To simplify adding spaces between toolbar items, you may use the following
5466 function:
5467
5468 <tscreen><verb>
5469 void gtk_toolbar_append_space( GtkToolbar *toolbar );
5470
5471 void gtk_toolbar_prepend_space( GtkToolbar *toolbar );
5472
5473 void gtk_toolbar_insert_space( GtkToolbar *toolbar,
5474                                gint        position );
5475  
5476 </verb></tscreen>
5477
5478 While the size of the added space can be set globally for a
5479 whole toolbar with the function:
5480
5481 <tscreen><verb>
5482 void gtk_toolbar_set_space_size( GtkToolbar *toolbar,
5483                                  gint        space_size) ;
5484 </verb></tscreen>
5485
5486 If it's needed the orientation of a toolbar and its style can be changed
5487 `on the fly' using the following functions:
5488
5489 <tscreen><verb>
5490 void gtk_toolbar_set_orientation( GtkToolbar     *toolbar,
5491                                   GtkOrientation  orientation );
5492
5493 void gtk_toolbar_set_style( GtkToolbar      *toolbar,
5494                             GtkToolbarStyle  style );
5495
5496 void gtk_toolbar_set_tooltips( GtkToolbar *toolbar,
5497                                gint        enable );
5498 </verb></tscreen>
5499
5500 To show some other things that can be done with a toolbar, let's take the
5501 following program (we'll interrupt the listing with some additional explanations):
5502
5503 <tscreen><verb>
5504 #include <gtk/gtk.h>
5505
5506 #include "gtk.xpm"
5507
5508 /* This function is connected to the Close button or
5509  * closing the window from the WM */
5510 void delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
5511 {
5512   gtk_main_quit ();
5513 }
5514 </verb></tscreen>
5515
5516 The above beginning seems for sure familiar to you if it's not your first
5517 GTK program. There is one additional thing though, we include a nice XPM
5518 picture to serve as an icon for all of the buttons.
5519
5520 <tscreen><verb>
5521 GtkWidget* close_button; // this button will emit signal to close application
5522 GtkWidget* tooltips_button; // to enable/disable tooltips
5523 GtkWidget* text_button,
5524          * icon_button,
5525          * both_button; // radio buttons for toolbar style
5526 GtkWidget* entry; // a text entry to show packing any widget into toolbar
5527 </verb></tscreen>
5528
5529 In fact not all of the above widgets are needed here, but to make things
5530 clearer I put them all together.
5531
5532 <tscreen><verb>
5533 /* that's easy... when one of the buttons is toggled, we just
5534  * check which one is active and set the style of the toolbar
5535  * accordingly
5536  * ATTENTION: our toolbar is passed as data to callback ! */
5537 void radio_event (GtkWidget *widget, gpointer data)
5538 {
5539   if (GTK_TOGGLE_BUTTON (text_button)->active) 
5540     gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_TEXT);
5541   else if (GTK_TOGGLE_BUTTON (icon_button)->active)
5542     gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_ICONS);
5543   else if (GTK_TOGGLE_BUTTON (both_button)->active)
5544     gtk_toolbar_set_style(GTK_TOOLBAR ( data ), GTK_TOOLBAR_BOTH);
5545 }
5546
5547 /* even easier, just check given toggle button and enable/disable 
5548  * tooltips */
5549 void toggle_event (GtkWidget *widget, gpointer data)
5550 {
5551   gtk_toolbar_set_tooltips (GTK_TOOLBAR ( data ),
5552                             GTK_TOGGLE_BUTTON (widget)->active );
5553 }
5554 </verb></tscreen>
5555
5556 The above are just two callback functions that will be called when
5557 one of the buttons on a toolbar is pressed. You should already be
5558 familiar with things like this if you've already used toggle buttons (and
5559 radio buttons).
5560
5561 <tscreen><verb>
5562 int main (int argc, char *argv[])
5563 {
5564   /* Here is our main window (a dialog) and a handle for the handlebox */
5565   GtkWidget* dialog;
5566   GtkWidget* handlebox;
5567
5568   /* Ok, we need a toolbar, an icon with a mask (one for all of 
5569      the buttons) and an icon widget to put this icon in (but 
5570      we'll create a separate widget for each button) */
5571   GtkWidget * toolbar;
5572   GdkPixmap * icon;
5573   GdkBitmap * mask;
5574   GtkWidget * iconw;
5575
5576   /* this is called in all GTK application. */
5577   gtk_init (&amp;argc, &amp;argv);
5578   
5579   /* create a new window with a given title, and nice size */
5580   dialog = gtk_dialog_new ();
5581   gtk_window_set_title ( GTK_WINDOW ( dialog ) , "GTKToolbar Tutorial");
5582   gtk_widget_set_usize( GTK_WIDGET ( dialog ) , 600 , 300 );
5583   GTK_WINDOW ( dialog ) ->allow_shrink = TRUE;
5584
5585   /* typically we quit if someone tries to close us */
5586   gtk_signal_connect ( GTK_OBJECT ( dialog ), "delete_event",
5587                        GTK_SIGNAL_FUNC ( delete_event ), NULL);
5588
5589   /* we need to realize the window because we use pixmaps for 
5590    * items on the toolbar in the context of it */
5591   gtk_widget_realize ( dialog );
5592
5593   /* to make it nice we'll put the toolbar into the handle box, 
5594    * so that it can be detached from the main window */
5595   handlebox = gtk_handle_box_new ();
5596   gtk_box_pack_start ( GTK_BOX ( GTK_DIALOG(dialog)->vbox ),
5597                        handlebox, FALSE, FALSE, 5 );
5598 </verb></tscreen>
5599
5600 The above should be similar to any other GTK application. Just initialization
5601 of GTK, creating the window etc.. There is only one thing that probably
5602 needs some explanation: a handle box. A handle box is just another box
5603 that can be used to pack widgets in to. The difference between it and typical
5604 boxes is that it can be detached from a parent window (or, in fact, the handle
5605 box remains in the parent, but it is reduced to a very small rectangle, while
5606 all of its contents are reparented to a new freely floating window). It is
5607 usually nice to have a detachable toolbar, so these two widgets occur together
5608 quite often.
5609
5610 <tscreen><verb>
5611   /* toolbar will be horizontal, with both icons and text, and
5612    * with 5pxl spaces between items and finally, 
5613    * we'll also put it into our handlebox */
5614   toolbar = gtk_toolbar_new ( GTK_ORIENTATION_HORIZONTAL,
5615                               GTK_TOOLBAR_BOTH );
5616   gtk_container_border_width ( GTK_CONTAINER ( toolbar ) , 5 );
5617   gtk_toolbar_set_space_size ( GTK_TOOLBAR ( toolbar ), 5 );
5618   gtk_container_add ( GTK_CONTAINER ( handlebox ) , toolbar );
5619
5620   /* now we create icon with mask: we'll reuse it to create
5621    * icon widgets for toolbar items */
5622   icon = gdk_pixmap_create_from_xpm_d ( dialog->window, &amp;mask,
5623       &amp;dialog->style->white, gtk_xpm );
5624 </verb></tscreen>
5625
5626 Well, what we do above is just a straight-forward initialization of the toolbar
5627 widget and creation of a GDK pixmap with its mask. If you want to know
5628 something more about using pixmaps, refer to GDK documentation
5629 or to the <ref id="sec_Pixmaps" name="Pixmaps"> section earlier in this tutorial.
5630
5631 <tscreen><verb>
5632   /* our first item is <close> button */
5633   iconw = gtk_pixmap_new ( icon, mask ); // icon widget
5634   close_button = 
5635     gtk_toolbar_append_item ( GTK_TOOLBAR (toolbar), // our toolbar
5636                               "Close",               // button label
5637                               "Closes this app",     // tooltip for this button
5638                               "Private",             // tooltip private string
5639                               iconw,                 // icon widget
5640                               GTK_SIGNAL_FUNC (delete_event), // a signal
5641                               NULL );
5642   gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) ); // space after item
5643 </verb></tscreen>
5644
5645 In the above code you see the simplest case: adding a button to toolbar.
5646 Just before appending a new item, we have to construct a pixmap widget
5647 to serve as an icon for this item; this step will have to be repeated for
5648 each new item. Just after the item we also add a space, so the following
5649 items will not touch each other. As you see gtk_toolbar_append_item returns
5650 a pointer to our newly created button widget, so that we can work with it in
5651 the normal way.
5652
5653 <tscreen><verb>
5654   /* now, let's make our radio buttons group... */
5655   iconw = gtk_pixmap_new ( icon, mask );
5656   icon_button = 
5657     gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
5658                                GTK_TOOLBAR_CHILD_RADIOBUTTON, // a type of element
5659                                NULL,                          // pointer to widget
5660                                "Icon",                        // label
5661                                "Only icons in toolbar",       // tooltip
5662                                "Private",                     // tooltip private string
5663                                iconw,                         // icon
5664                                GTK_SIGNAL_FUNC (radio_event), // signal
5665                                toolbar);                      // data for signal
5666   gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
5667 </verb></tscreen>
5668
5669 Here we begin creating a radio buttons group. To do this we use gtk_toolbar_append_element.
5670 In fact, using this function one can also add simple items or even spaces
5671 (type = GTK_TOOLBAR_CHILD_SPACE or GTK_TOOLBAR_CHILD_BUTTON). In the above
5672 case we start creating a radio group. In creating other radio buttons for
5673 this group a pointer to the previous button in the group is required,
5674 so that a list of buttons can be easily constructed (see the section on
5675 <ref id="sec_Radio_Buttons" name="Radio Buttons"> earlier in this tutorial).
5676
5677 <tscreen><verb>
5678   /* following radio buttons refer to previous ones */
5679   iconw = gtk_pixmap_new ( icon, mask );
5680   text_button = 
5681     gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
5682                                GTK_TOOLBAR_CHILD_RADIOBUTTON,
5683                                icon_button,
5684                                "Text",
5685                                "Only texts in toolbar",
5686                                "Private",
5687                                iconw,
5688                                GTK_SIGNAL_FUNC (radio_event),
5689                                toolbar);
5690   gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
5691                                           
5692   iconw = gtk_pixmap_new ( icon, mask );
5693   both_button = 
5694     gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
5695                                GTK_TOOLBAR_CHILD_RADIOBUTTON,
5696                                text_button,
5697                                "Both",
5698                                "Icons and text in toolbar",
5699                                "Private",
5700                                iconw,
5701                                GTK_SIGNAL_FUNC (radio_event),
5702                                toolbar);
5703   gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
5704   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(both_button),TRUE);
5705 </verb></tscreen>
5706
5707 In the end we have set the state of one of the buttons manually (otherwise
5708 they all stay in active state, preventing us from switching between them).
5709
5710 <tscreen><verb>
5711   /* here we have just a simple toggle button */
5712   iconw = gtk_pixmap_new ( icon, mask );
5713   tooltips_button = 
5714     gtk_toolbar_append_element(GTK_TOOLBAR(toolbar),
5715                                GTK_TOOLBAR_CHILD_TOGGLEBUTTON,
5716                                NULL,
5717                                "Tooltips",
5718                                "Toolbar with or without tips",
5719                                "Private",
5720                                iconw,
5721                                GTK_SIGNAL_FUNC (toggle_event),
5722                                toolbar);
5723   gtk_toolbar_append_space ( GTK_TOOLBAR ( toolbar ) );
5724   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(tooltips_button),TRUE);
5725 </verb></tscreen>
5726
5727 A toggle button can be created in the obvious way (if one knows how to create
5728 radio buttons already).
5729
5730 <tscreen><verb>
5731   /* to pack a widget into toolbar, we only have to 
5732    * create it and append it with an appropriate tooltip */
5733   entry = gtk_entry_new ();
5734   gtk_toolbar_append_widget( GTK_TOOLBAR (toolbar), 
5735                              entry, 
5736                              "This is just an entry", 
5737                              "Private" );
5738
5739   /* well, it isn't created within thetoolbar, so we must still show it */
5740   gtk_widget_show ( entry );
5741 </verb></tscreen>
5742
5743 As you see, adding any kind of widget to a toolbar is simple. The
5744 one thing you have to remember is that this widget must be shown manually
5745 (contrary to other items which will be shown together with the toolbar).
5746
5747 <tscreen><verb>
5748   /* that's it ! let's show everything. */
5749   gtk_widget_show ( toolbar );
5750   gtk_widget_show (handlebox);
5751   gtk_widget_show ( dialog );
5752
5753   /* rest in gtk_main and wait for the fun to begin! */
5754   gtk_main ();
5755   
5756   return 0;
5757 }
5758 </verb></tscreen>
5759
5760 So, here we are at the end of toolbar tutorial. Of course, to appreciate
5761 it in full you need also this nice XPM icon, so here it is:
5762
5763 <tscreen><verb>
5764 /* XPM */
5765 static char * gtk_xpm[] = {
5766 "32 39 5 1",
5767 ".      c none",
5768 "+      c black",
5769 "@      c #3070E0",
5770 "#      c #F05050",
5771 "$      c #35E035",
5772 "................+...............",
5773 "..............+++++.............",
5774 "............+++++@@++...........",
5775 "..........+++++@@@@@@++.........",
5776 "........++++@@@@@@@@@@++........",
5777 "......++++@@++++++++@@@++.......",
5778 ".....+++@@@+++++++++++@@@++.....",
5779 "...+++@@@@+++@@@@@@++++@@@@+....",
5780 "..+++@@@@+++@@@@@@@@+++@@@@@++..",
5781 ".++@@@@@@+++@@@@@@@@@@@@@@@@@@++",
5782 ".+#+@@@@@@++@@@@+++@@@@@@@@@@@@+",
5783 ".+##++@@@@+++@@@+++++@@@@@@@@$@.",
5784 ".+###++@@@@+++@@@+++@@@@@++$$$@.",
5785 ".+####+++@@@+++++++@@@@@+@$$$$@.",
5786 ".+#####+++@@@@+++@@@@++@$$$$$$+.",
5787 ".+######++++@@@@@@@++@$$$$$$$$+.",
5788 ".+#######+##+@@@@+++$$$$$$@@$$+.",
5789 ".+###+++##+##+@@++@$$$$$$++$$$+.",
5790 ".+###++++##+##+@@$$$$$$$@+@$$@+.",
5791 ".+###++++++#+++@$$@+@$$@++$$$@+.",
5792 ".+####+++++++#++$$@+@$$++$$$$+..",
5793 ".++####++++++#++$$@+@$++@$$$$+..",
5794 ".+#####+++++##++$$++@+++$$$$$+..",
5795 ".++####+++##+#++$$+++++@$$$$$+..",
5796 ".++####+++####++$$++++++@$$$@+..",
5797 ".+#####++#####++$$+++@++++@$@+..",
5798 ".+#####++#####++$$++@$$@+++$@@..",
5799 ".++####++#####++$$++$$$$$+@$@++.",
5800 ".++####++#####++$$++$$$$$$$$+++.",
5801 ".+++####+#####++$$++$$$$$$$@+++.",
5802 "..+++#########+@$$+@$$$$$$+++...",
5803 "...+++########+@$$$$$$$$@+++....",
5804 ".....+++######+@$$$$$$$+++......",
5805 "......+++#####+@$$$$$@++........",
5806 ".......+++####+@$$$$+++.........",
5807 ".........++###+$$$@++...........",
5808 "..........++##+$@+++............",
5809 "...........+++++++..............",
5810 ".............++++..............."};
5811 </verb></tscreen>
5812
5813 <!-- ----------------------------------------------------------------- -->   
5814 <sect1> Aspect Frames
5815 <p>
5816 The aspect frame widget is like a frame widget, except that it also
5817 enforces the aspect ratio (that is, the ratio of the width to the
5818 height) of the child widget to have a certain value, adding extra
5819 space if necessary. This is useful, for instance, if you want to
5820 preview a larger image. The size of the preview should vary when
5821 the user resizes the window, but the aspect ratio needs to always match
5822 the original image.
5823    
5824 To create a new aspect frame use:
5825    
5826 <tscreen><verb>
5827 GtkWidget *gtk_aspect_frame_new( const gchar *label,
5828                                  gfloat       xalign,
5829                                  gfloat       yalign,
5830                                  gfloat       ratio,
5831                                  gint         obey_child);
5832 </verb></tscreen>
5833    
5834 <tt/xalign/ and <tt/yalign/ specify alignment as with Alignment
5835 widgets. If <tt/obey_child/ is true, the aspect ratio of a child
5836 widget will match the aspect ratio of the ideal size it requests.
5837 Otherwise, it is given by <tt/ratio/.
5838    
5839 To change the options of an existing aspect frame, you can use:
5840    
5841 <tscreen><verb>
5842 void gtk_aspect_frame_set( GtkAspectFrame *aspect_frame,
5843                            gfloat          xalign,
5844                            gfloat          yalign,
5845                            gfloat          ratio,
5846                            gint            obey_child);
5847 </verb></tscreen>
5848    
5849 As an example, the following program uses an AspectFrame to
5850 present a drawing area whose aspect ratio will always be 2:1, no
5851 matter how the user resizes the top-level window.
5852    
5853 <tscreen><verb>
5854 /* example-start aspectframe aspectframe.c */
5855
5856 #include <gtk/gtk.h>
5857    
5858 int
5859 main (int argc, char *argv[])
5860 {
5861     GtkWidget *window;
5862     GtkWidget *aspect_frame;
5863     GtkWidget *drawing_area;
5864     gtk_init (&amp;argc, &amp;argv);
5865    
5866     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
5867     gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
5868     gtk_signal_connect (GTK_OBJECT (window), "destroy",
5869                         GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
5870     gtk_container_border_width (GTK_CONTAINER (window), 10);
5871    
5872     /* Create an aspect_frame and add it to our toplevel window */
5873    
5874     aspect_frame = gtk_aspect_frame_new ("2x1", /* label */
5875                                          0.5, /* center x */
5876                                          0.5, /* center y */
5877                                          2, /* xsize/ysize = 2 */
5878                                          FALSE /* ignore child's aspect */);
5879    
5880     gtk_container_add (GTK_CONTAINER(window), aspect_frame);
5881     gtk_widget_show (aspect_frame);
5882    
5883     /* Now add a child widget to the aspect frame */
5884    
5885     drawing_area = gtk_drawing_area_new ();
5886    
5887     /* Ask for a 200x200 window, but the AspectFrame will give us a 200x100
5888      * window since we are forcing a 2x1 aspect ratio */
5889     gtk_widget_set_usize (drawing_area, 200, 200);
5890     gtk_container_add (GTK_CONTAINER(aspect_frame), drawing_area);
5891     gtk_widget_show (drawing_area);
5892    
5893     gtk_widget_show (window);
5894     gtk_main ();
5895     return 0;
5896 }
5897 /* example-end */
5898 </verb></tscreen>
5899
5900 <!-- ***************************************************************** -->
5901 <sect>CList Widget
5902 <!-- ***************************************************************** -->
5903
5904 <!-- ----------------------------------------------------------------- -->
5905 <p>
5906 The GtkCList widget has replaced the GtkList widget (which is still
5907 available).
5908
5909 The GtkCList widget is a multi-column list widget that is capable of
5910 handling literally thousands of rows of information. Each column can
5911 optionally have a title, which itself is optionally active, allowing
5912 us to bind a function to its selection.
5913
5914 <!-- ----------------------------------------------------------------- -->
5915 <sect1>Creating a GtkCList widget
5916 <p>
5917 Creating a GtkCList is quite straightforward, once you have learned about
5918 widgets in general. It provides the almost standard two ways, that is the
5919 hard way, and the easy way. But before we create it, there is one thing we
5920 should figure out beforehand: how many columns should it have?
5921
5922 Not all columns have to be visible and can be used to store data that is
5923 related to a certain cell in the list.
5924
5925 <tscreen><verb>
5926 GtkWidget *gtk_clist_new ( gint columns );
5927
5928 GtkWidget *gtk_clist_new_with_titles( gint   columns,
5929                                       gchar *titles[] );
5930 </verb></tscreen>
5931
5932 The first form is very straight forward, the second might require some
5933 explanation. Each column can have a title associated with it, and this
5934 title can be a label or a button that reacts when we click on it. If we
5935 use the second form, we must provide pointers to the title texts, and the
5936 number of pointers should equal the number of columns specified. Of course
5937 we can always use the first form, and manually add titles later.
5938
5939 <!-- ----------------------------------------------------------------- -->
5940 <sect1>Modes of operation
5941 <p>
5942 There are several attributes that can be used to alter the behaviour of
5943 a GtkCList. First there is
5944
5945 <tscreen><verb>
5946 void gtk_clist_set_selection_mode( GtkCList         *clist,
5947                                    GtkSelectionMode  mode );
5948 </verb></tscreen>
5949
5950 which, as the name implies, sets the selection mode of the GtkCList. The first
5951 argument is the GtkCList widget, and the second specifies the cell selection
5952 mode (they are defined in gtkenums.h). At the time of this writing, the following
5953 modes are available to us:
5954
5955 <itemize>
5956 <item> GTK_SELECTION_SINGLE - The selection is either NULL or contains a GList
5957 pointer for a single selected item.
5958
5959 <item> GTK_SELECTION_BROWSE -  The selection is NULL if the list contains no
5960 widgets or insensitive ones only, otherwise it contains a GList pointer for
5961 one GList structure, and therefore exactly one list item.
5962
5963 <item> GTK_SELECTION_MULTIPLE -  The selection is NULL if no list items are
5964 selected or a GList pointer for the first selected item. That in turn points
5965 to a GList structure for the second selected item and so on. This is currently
5966 the <bf>default</bf> for the GtkCList widget.
5967
5968 <item> GTK_SELECTION_EXTENDED - The selection is always NULL.
5969 </itemize>
5970
5971 Others might be added in later revisions of GTK.
5972
5973 Then there is
5974
5975 <tscreen><verb>
5976 void gtk_clist_set_policy (GtkCList * clist,
5977                            GtkPolicyType vscrollbar_policy,
5978                            GtkPolicyType hscrollbar_policy);
5979 </verb></tscreen>
5980
5981 which defines what happens to the scrollbars. The following values are possible
5982 for both the vertical and the horizontal scrollbar:
5983
5984 <itemize>
5985 <item> GTK_POLICY_ALWAYS - The scrollbar will always be there.
5986
5987 <item> GTK_POLICY_AUTOMATIC - The scrollbar will be there only when the number
5988 of items in the GtkCList exceeds the number that can be shown in the widget.
5989 </itemize>
5990
5991 We can also define what the border of the GtkCList widget should look like. It is
5992 done through
5993
5994 <tscreen><verb>
5995 void gtk_clist_set_border( GtkCList      *clist,
5996                            GtkShadowType  border );
5997 </verb></tscreen>
5998
5999 And the possible values for the second argument are
6000
6001 <itemize>
6002 <item> GTK_SHADOW_NONE
6003
6004 <item> GTK_SHADOW_IN
6005
6006 <item> GTK_SHADOW_OUT
6007
6008 <item> GTK_SHADOW_ETCHED_IN
6009
6010 <item> GTK_SHADOW_ETCHED_OUT
6011 </itemize>
6012
6013 <!-- ----------------------------------------------------------------- -->
6014 <sect1>Working with titles
6015 <p>
6016 When you create a GtkCList widget, you will also get a set of title buttons
6017 automatically. They live in the top of the CList window, and can act either
6018 as normal buttons that respond to being pressed, or they can be passive,
6019 in which case they are nothing more than a title. There are four different
6020 calls that aid us in setting the status of the title buttons.
6021
6022 <tscreen><verb>
6023 void gtk_clist_column_title_active( GtkCList *clist,
6024                                      gint     column );
6025
6026 void gtk_clist_column_title_passive( GtkCList *clist,
6027                                      gint      column );
6028
6029 void gtk_clist_column_titles_active( GtkCList *clist );
6030
6031 void gtk_clist_column_titles_passive( GtkCList *clist );
6032 </verb></tscreen>
6033
6034 An active title is one which acts as a normal button, a passive one is just
6035 a label. The first two calls above will activate/deactivate the title button
6036 above the specific column, while the last two calls activate/deactivate all
6037 title buttons in the supplied clist widget.
6038
6039 But of course there are those cases when we don't want them at all, and so
6040 they can be hidden and shown at will using the following two calls.
6041
6042 <tscreen><verb>
6043 void gtk_clist_column_titles_show( GtkCList *clist );
6044
6045 void gtk_clist_column_titles_hide( GtkCList *clist );
6046 </verb></tscreen>
6047
6048 For titles to be really useful we need a mechanism to set and change them,
6049 and this is done using
6050
6051 <tscreen><verb>
6052 void gtk_clist_set_column_title( GtkCList *clist,
6053                                  gint      column,
6054                                  gchar    *title );
6055 </verb></tscreen>
6056
6057 Note that only the title of one column can be set at a time, so if all the
6058 titles are known from the beginning, then I really suggest using
6059 gtk_clist_new_with_titles (as described above) to set them. Saves you
6060 coding time, and makes your program smaller. There are some cases where
6061 getting the job done the manual way is better, and that's when not all
6062 titles will be text. GtkCList provides us with title buttons that can in fact
6063 incorporate whole widgets, for example a pixmap. It's all done through
6064
6065 <tscreen><verb>
6066 void gtk_clist_set_column_widget( GtkCList  *clist,
6067                                   gint       column,
6068                                   GtkWidget *widget );
6069 </verb></tscreen>
6070
6071 which should require no special explanation.
6072
6073 <!-- ----------------------------------------------------------------- -->
6074 <sect1>Manipulating the list itself
6075 <p>
6076 It is possible to change the justification for a column, and it is done through
6077
6078 <tscreen><verb>
6079 void gtk_clist_set_column_justification( GtkCList         *clist,
6080                                          gint              column,
6081                                          GtkJustification  justification );
6082 </verb></tscreen>
6083
6084 The GtkJustification type can take the following values:
6085
6086 <itemize>
6087 <item>GTK_JUSTIFY_LEFT - The text in the column will begin from the left edge.
6088
6089 <item>GTK_JUSTIFY_RIGHT - The text in the column will begin from the right edge.
6090
6091 <item>GTK_JUSTIFY_CENTER - The text is placed in the center of the column.
6092
6093 <item>GTK_JUSTIFY_FILL - The text will use up all available space in the
6094 column. It is normally done by inserting extra blank spaces between words
6095 (or between individual letters if it's a single word). Much in the same way as
6096 any ordinary WYSIWYG text editor.
6097 </itemize>
6098
6099 The next function is a very important one, and should be standard in the setup
6100 of all GtkCList widgets. When the list is created, the width of the various
6101 columns are chosen to match their titles, and since this is seldom the right
6102 width we have to set it using
6103
6104 <tscreen><verb>
6105 void gtk_clist_set_column_width( GtkCList *clist,
6106                                  gint      column,
6107                                  gint      width );
6108 </verb></tscreen>
6109
6110 Note that the width is given in pixels and not letters. The same goes for the
6111 height of the cells in the columns, but as the default value is the height of
6112 the current font this isn't as critical to the application. Still, it is done through
6113
6114 <tscreen><verb>
6115 void gtk_clist_set_row_height( GtkCList *clist,
6116                                gint      height );
6117 </verb></tscreen>
6118
6119 Again, note that the height is given in pixels.
6120
6121 We can also move the list around without user interaction, however, it does
6122 require that we know what we are looking for. Or in other words, we need the row
6123 and column of the item we want to scroll to.
6124
6125 <tscreen><verb>
6126 void gtk_clist_moveto( GtkCList *clist,
6127                        gint      row,
6128                        gint      column,
6129                        gfloat    row_align,
6130                        gfloat    col_align );
6131 </verb></tscreen>
6132
6133 The gfloat row_align is pretty important to understand. It's a value between 0.0 and
6134 1.0, where 0.0 means that we should scroll the list so the row appears at the top,
6135 while if the value of row_align is 1.0, the row will appear at the bottom instead. All
6136 other values between 0.0 and 1.0 are also valid and will place the row between the top
6137 and the bottom. The last argument, gfloat col_align works in the same way, though 0.0
6138 marks left and 1.0 marks right instead.
6139
6140 Depending on the application's needs, we don't have to scroll to an item that is
6141 already visible to us. So how do we know if it is visible? As usual, there is a function
6142 to find that out as well.
6143
6144 <tscreen><verb>
6145 GtkVisibility gtk_clist_row_is_visible( GtkCList *clist,
6146                                         gint      row );
6147 </verb></tscreen>
6148
6149 The return value is is one of the following:
6150
6151 <itemize>
6152 <item>GTK_VISIBILITY_NONE
6153
6154 <item>GTK_VISIBILITY_PARTIAL
6155
6156 <item>GTK_VISIBILITY_FULL
6157 </itemize>
6158
6159 Note that it will only tell us if a row is visible. Currently there is no way to
6160 determine this for a column. We can get partial information though, because if
6161 the return is GTK_VISIBILITY_PARTIAL, then some of it is hidden, but we don't know if
6162 it is the row that is being cut by the lower edge of the listbox, or if the row has
6163 columns that are outside.
6164
6165 We can also change both the foreground and background colors of a particular
6166 row. This is useful for marking the row selected by the user, and the two functions
6167 that is used to do it are
6168
6169 <tscreen><verb>
6170 void gtk_clist_set_foreground( GtkCList *clist,
6171                                gint      row,
6172                                GdkColor *color );
6173
6174 void gtk_clist_set_background( GtkCList *clist,
6175                                gint      row,
6176                                GdkColor *color );
6177 </verb></tscreen>
6178
6179 Please note that the colors must have been previously allocated.
6180
6181 <!-- ----------------------------------------------------------------- -->
6182 <sect1>Adding rows to the list
6183 <p>
6184 We can add rows in two ways. They can be appended at the end to the list using
6185
6186 <tscreen><verb>
6187 gint gtk_clist_append( GtkCList *clist,
6188                        gchar    *text[] );
6189 </verb></tscreen>
6190
6191 or we can insert a row at a given place using
6192
6193 <tscreen><verb>
6194 void gtk_clist_insert( GtkCList *clist,
6195                        gint      row,
6196                        gchar    *text[] );
6197 </verb></tscreen>
6198
6199 In both calls we have to provide a collection of pointers that are the texts
6200 we want to put in the columns. The number of pointers should equal the number
6201 of columns in the list. If the text[] argument is NULL, then there will be no
6202 text in the columns of the row. This is useful, for example,  if we want to
6203 add pixmaps instead (something that has to be done manually).
6204
6205 Also, please note that the numbering of both rows and columns start at 0.
6206
6207 To remove an individual row we use
6208
6209 <tscreen><verb>
6210 void gtk_clist_remove( GtkCList *clist,
6211                        gint      row );
6212 </verb></tscreen>
6213
6214 There is also a call that removes all rows in the list. This is a lot faster
6215 than calling gtk_clist_remove once for each row, which is the only alternative.
6216
6217 <tscreen><verb>
6218 void gtk_clist_clear( GtkCList *clist );
6219 </verb></tscreen>
6220
6221 There are also two convenience functions that should be used when a lot of
6222 changes have to be made to the list. This is to prevent the list flickering while
6223 being repeatedly updated, which may be highly annoying to the user. So instead it
6224 is a good idea to freeze the list, do the updates to it, and finally thaw it which
6225 causes the list to be updated on the screen.
6226
6227 <tscreen><verb>
6228 void gtk_clist_freeze( GtkCList * clist );
6229
6230 void gtk_clist_thaw( GtkCList * clist );
6231 </verb></tscreen>
6232
6233 <!-- ----------------------------------------------------------------- -->
6234 <sect1>Setting text and pixmaps in the cells
6235 <p>
6236 A cell can contain a pixmap, text or both. To set them the following
6237 functions are used.
6238
6239 <tscreen><verb>
6240 void gtk_clist_set_text( GtkCList *clist,
6241                          gint      row,
6242                          gint      column,
6243                          gchar    *text );
6244
6245 void gtk_clist_set_pixmap( GtkCList  *clist,
6246                            gint       row,
6247                            gint       column,
6248                            GdkPixmap *pixmap,
6249                            GdkBitmap *mask );
6250
6251 void gtk_clist_set_pixtext( GtkCList  *clist,
6252                             gint       row,
6253                             gint       column,
6254                             gchar     *text,
6255                             guint8     spacing,
6256                             GdkPixmap *pixmap,
6257                             GdkBitmap *mask );
6258 </verb></tscreen>
6259
6260 It's quite straightforward. All the calls have the GtkCList as the first
6261 argument, followed by the row and column of the cell, followed by the data to be
6262 set. The gint8 spacing argument in gtk_clist_set_pixtext is the number of pixels
6263 between the pixmap and the beginning of the text.
6264
6265 To read back the data, we instead use
6266
6267 <tscreen><verb>
6268 gint gtk_clist_get_text( GtkCList  *clist,
6269                          gint       row,
6270                          gint       column,
6271                          gchar    **text );
6272
6273 gint gtk_clist_get_pixmap( GtkCList   *clist,
6274                            gint        row,
6275                            gint        column,
6276                            GdkPixmap **pixmap,
6277                            GdkBitmap **mask );
6278
6279 gint gtk_clist_get_pixtext( GtkCList   *clist,
6280                             gint        row,
6281                             gint        column,
6282                             gchar     **text,
6283                             guint8     *spacing,
6284                             GdkPixmap **pixmap,
6285                             GdkBitmap **mask );
6286 </verb></tscreen>
6287
6288 It isn't necessary to read it all back in case you aren't interested. Any
6289 of the pointers that are meant for return values (all except the clist) can
6290 be NULL. So if we want to read back only the text from a cell that is of
6291 type pixtext, then we would do the following, assuming that clist, row and
6292 column already exist:
6293
6294 <tscreen><verb>
6295 gchar *mytext;
6296
6297 gtk_clist_get_pixtext(clist, row, column, &amp;mytext, NULL, NULL, NULL);
6298 </verb></tscreen>
6299
6300 There is one more call that is related to what's inside a cell in the
6301 clist, and that's
6302
6303 <tscreen><verb>
6304 GtkCellType gtk_clist_get_cell_type( GtkCList *clist,
6305                                      gint      row,
6306                                      gint      column );
6307 </verb></tscreen>
6308
6309 which returns the type of data in a cell. The return value is one of
6310
6311 <itemize>
6312 <item>GTK_CELL_EMPTY
6313
6314 <item>GTK_CELL_TEXT
6315
6316 <item>GTK_CELL_PIXMAP
6317
6318 <item>GTK_CELL_PIXTEXT
6319
6320 <item>GTK_CELL_WIDGET
6321 </itemize>
6322
6323 There is also a function that will let us set the indentation, both
6324 vertical and horizontal, of a cell. The indentation value is of type gint,
6325 given in pixels, and can be both positive and negative.
6326
6327 <tscreen><verb>
6328 void gtk_clist_set_shift( GtkCList *clist,
6329                           gint      row,
6330                           gint      column,
6331                           gint      vertical,
6332                           gint      horizontal );
6333 </verb></tscreen>
6334
6335 <!-- ----------------------------------------------------------------- -->
6336 <sect1>Storing data pointers
6337 <p>
6338 With a GtkCList it is possible to set a data pointer for a row. This
6339 pointer will not be visible for the user, but is merely a convenience for
6340 the programmer to associate a row with a pointer to some additional data.
6341
6342 The functions should be fairly self-explanatory by now
6343
6344 <tscreen><verb>
6345 void gtk_clist_set_row_data( GtkCList *clist,
6346                              gint      row,
6347                              gpointer  data );
6348
6349 void gtk_clist_set_row_data_full( GtkCList         *clist,
6350                                   gint              row,
6351                                   gpointer          data,
6352                                   GtkDestroyNotify  destroy );
6353
6354 gpointer gtk_clist_get_row_data( GtkCList *clist,
6355                                  gint      row );
6356
6357 gint gtk_clist_find_row_from_data( GtkCList *clist,
6358                                    gpointer  data );
6359 </verb></tscreen>
6360
6361 <!-- ----------------------------------------------------------------- -->
6362 <sect1>Working with selections
6363 <p>
6364 There are also functions available that let us force the (un)selection
6365 of a row. These are
6366
6367 <tscreen><verb>
6368 void gtk_clist_select_row( GtkCList *clist,
6369                            gint      row,
6370                            gint      column );
6371
6372 void gtk_clist_unselect_row( GtkCList *clist,
6373                              gint      row,
6374                              gint      column );
6375 </verb></tscreen>
6376
6377 And also a function that will take x and y coordinates (for example, read from
6378 the mousepointer), and map that onto the list, returning the
6379 corresponding row and column.
6380
6381 <tscreen><verb>
6382 gint gtk_clist_get_selection_info( GtkCList *clist,
6383                                    gint      x,
6384                                    gint      y,
6385                                    gint     *row,
6386                                    gint     *column );
6387 </verb></tscreen>
6388
6389 When we detect something of interest, it might be movement of the pointer, a
6390 click somewhere in the list, we can read the pointer coordinates and find out
6391 where in the list the pointer is. Cumbersome? Luckily, there is a more simple way...
6392
6393 <!-- ----------------------------------------------------------------- -->
6394 <sect1>The signals that bring it together
6395 <p>
6396 As with all other widgets, there are a few signals that can be used. The
6397 GtkCList widget is derived from the GtkContainer widget, and so has all the
6398 same signals, but also the adds following:
6399
6400 <itemize>
6401 <item>select_row - This signal will send the following information, in
6402 order: GtkCList *clist, gint row, gint column, GtkEventButton *event
6403
6404 <item>unselect_row - When the user unselects a row, this signal is activated. It
6405 sends the same information as select_row
6406
6407 <item>click_column - Send GtkCList *clist, gint column
6408 </itemize>
6409
6410 So if we want to connect a callback to select_row, the callback function would
6411 be declared like this
6412
6413 <tscreen><verb>
6414 void select_row_callback(GtkWidget *widget,
6415                          gint row,
6416                          gint column,
6417                          GdkEventButton *event,
6418                          gpointer data);
6419 </verb></tscreen>
6420
6421 The callback is connected as usual with
6422
6423 <tscreen><verb>
6424 gtk_signal_connect(GTK_OBJECT( clist),
6425                    "select_row"
6426                    GTK_SIGNAL_FUNC(select_row_callback),
6427                    NULL);
6428 </verb></tscreen>
6429
6430 <!-- ----------------------------------------------------------------- -->
6431 <sect1>A GtkCList example
6432 <p>
6433
6434 <tscreen><verb>
6435 /* example-start clist clist.c */
6436
6437 #include        <gtk/gtk.h>
6438 #include        <glib.h>
6439
6440 /* These are just the prototypes of the various callbacks */
6441 void button_add_clicked( GtkWidget *button, gpointer data);
6442 void button_clear_clicked( GtkWidget *button, gpointer data);
6443 void button_hide_show_clicked( GtkWidget *button, gpointer data);
6444 void selection_made( GtkWidget *clist, gint row, gint column,
6445                      GdkEventButton *event, gpointer data);
6446
6447 gint main (int argc, gchar *argv[])
6448 {                                  
6449     GtkWidget       *window;
6450     GtkWidget       *vbox, *hbox;
6451     GtkWidget       *clist;
6452     GtkWidget       *button_add, *button_clear, *button_hide_show;    
6453     gchar           *titles[2] = {"Ingredients","Amount"};
6454
6455     gtk_init(&amp;argc, &amp;argv);
6456     
6457     
6458     window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
6459     gtk_widget_set_usize(GTK_WIDGET(window), 300, 150);
6460
6461     gtk_window_set_title(GTK_WINDOW(window), "GtkCList Example");
6462     gtk_signal_connect(GTK_OBJECT(window),
6463                        "destroy",
6464                        GTK_SIGNAL_FUNC(gtk_main_quit),
6465                        NULL);
6466     
6467     vbox=gtk_vbox_new(FALSE, 5);
6468     gtk_container_border_width(GTK_CONTAINER(vbox), 5);
6469     gtk_container_add(GTK_CONTAINER(window), vbox);
6470     gtk_widget_show(vbox);
6471     
6472     /* Create the GtkCList. For this example we use 2 columns */
6473     clist = gtk_clist_new_with_titles( 2, titles);
6474
6475     /* When a selection is made, we want to know about it. The callback
6476      * used is selection_made, and its code can be found further down */
6477     gtk_signal_connect(GTK_OBJECT(clist), "select_row",
6478                        GTK_SIGNAL_FUNC(selection_made),
6479                        NULL);
6480
6481     /* It isn't necessary to shadow the border, but it looks nice :) */
6482     gtk_clist_set_border(GTK_CLIST(clist), GTK_SHADOW_OUT);
6483
6484     /* What however is important, is that we set the column widths as
6485      * they will never be right otherwise. Note that the columns are
6486      * numbered from 0 and up (to 1 in this case).
6487      */
6488     gtk_clist_set_column_width (GTK_CLIST(clist), 0, 150);
6489
6490     /* Scollbars _only when needed_ */
6491     gtk_clist_set_policy(GTK_CLIST(clist), GTK_POLICY_AUTOMATIC,
6492                                            GTK_POLICY_AUTOMATIC);
6493
6494     /* Add the GtkCList widget to the vertical box and show it. */
6495     gtk_box_pack_start(GTK_BOX(vbox), clist, TRUE, TRUE, 0);
6496     gtk_widget_show(clist);
6497
6498     /* Create the buttons and add them to the window. See the button
6499      * tutorial for more examples and comments on this.
6500      */
6501     hbox = gtk_hbox_new(FALSE, 0);
6502     gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
6503     gtk_widget_show(hbox);
6504
6505     button_add = gtk_button_new_with_label("Add List");
6506     button_clear = gtk_button_new_with_label("Clear List");
6507     button_hide_show = gtk_button_new_with_label("Hide/Show titles");
6508
6509     gtk_box_pack_start(GTK_BOX(hbox), button_add, TRUE, TRUE, 0);
6510     gtk_box_pack_start(GTK_BOX(hbox), button_clear, TRUE, TRUE, 0);
6511     gtk_box_pack_start(GTK_BOX(hbox), button_hide_show, TRUE, TRUE, 0);
6512
6513     /* Connect our callbacks to the three buttons */
6514     gtk_signal_connect_object(GTK_OBJECT(button_add), "clicked",
6515                               GTK_SIGNAL_FUNC(button_add_clicked),
6516                               (gpointer) clist);
6517     gtk_signal_connect_object(GTK_OBJECT(button_clear), "clicked",
6518                               GTK_SIGNAL_FUNC(button_clear_clicked),
6519                               (gpointer) clist);
6520     gtk_signal_connect_object(GTK_OBJECT(button_hide_show), "clicked",
6521                               GTK_SIGNAL_FUNC(button_hide_show_clicked),
6522                               (gpointer) clist);
6523
6524     gtk_widget_show(button_add);
6525     gtk_widget_show(button_clear);
6526     gtk_widget_show(button_hide_show);
6527
6528     /* The interface is completely set up so we show the window and
6529      * enter the gtk_main loop.
6530      */
6531     gtk_widget_show(window);
6532     gtk_main();
6533     
6534     return 0;
6535 }
6536
6537 /* User clicked the "Add List" button. */
6538 void button_add_clicked( GtkWidget *button, gpointer data)
6539 {
6540     int         indx;
6541
6542     /* Something silly to add to the list. 4 rows of 2 columns each */
6543     gchar      *drink[4][2] = {{"Milk", "3 Oz"},
6544                                {"Water", "6 l"},
6545                                {"Carrots", "2"},
6546                                {"Snakes", "55"}};
6547
6548     /* Here we do the actual adding of the text. It's done once for
6549      * each row.
6550      */
6551     for( indx=0; indx < 4; indx++)
6552         gtk_clist_append( (GtkCList*) data, drink[indx]);
6553
6554     return;
6555 }
6556
6557 /* User clicked the "Clear List" button. */
6558 void button_clear_clicked( GtkWidget *button, gpointer data)
6559 {
6560     /* Clear the list using gtk_clist_clear. This is much faster than
6561      * calling gtk_clist_remove once for each row.
6562      */
6563     gtk_clist_clear((GtkCList*) data);
6564
6565     return;
6566 }
6567
6568 /* The user clicked the "Hide/Show titles" button. */
6569 void button_hide_show_clicked( GtkWidget *button, gpointer data)
6570 {
6571     /* Just a flag to remember the status. 0 = currently visible */
6572     static short int flag = 0;
6573
6574     if (flag == 0)
6575     {
6576         /* Hide the titles and set the flag to 1 */
6577         gtk_clist_column_titles_hide((GtkCList*) data);
6578         flag++;
6579     }
6580     else
6581     {
6582         /* Show the titles and reset flag to 0 */
6583         gtk_clist_column_titles_show((GtkCList*) data);
6584         flag--;
6585     }
6586
6587     return;
6588 }
6589
6590 /* If we come here, then the user has selected a row in the list. */
6591 void selection_made( GtkWidget *clist, gint row, gint column,
6592                      GdkEventButton *event, gpointer data)
6593 {
6594     gchar       *text;
6595
6596     /* Get the text that is stored in the selected row and column
6597      * which was clicked in. We will receive it as a pointer in the
6598      * argument text.
6599      */
6600     gtk_clist_get_text(GTK_CLIST(clist), row, column, &amp;text);
6601
6602     /* Just prints some information about the selected row */
6603     g_print("You selected row %d. More specifically you clicked in column %d, and the text in this cell is %s\n\n", row, column, text);
6604
6605     return;
6606 }
6607 /* example-end */
6608 </verb></tscreen>
6609                     
6610 <!-- ***************************************************************** -->
6611 <sect> List Widget
6612 <!-- ***************************************************************** -->
6613 <p>
6614 NOTE: The GtkList widget has been superseded by the GtkCList widget.
6615
6616 The GtkList widget is designed to act as a vertical container for widgets
6617 that should be of the type GtkListItem.
6618
6619 A GtkList widget has its own window to receive events and its own
6620 background color which is usually white.  As it is directly derived from a
6621 GtkContainer it can be treated as such by using the GTK_CONTAINER(List)
6622 macro, see the GtkContainer widget for more on this.
6623 One should already be familiar with the usage of a GList and its
6624 related functions g_list_*() to be able to use the GtkList widget to
6625 it full extent.
6626
6627 There is one field inside the structure definition of the GtkList widget
6628 that will be of greater interest to us, this is:
6629
6630 <tscreen><verb>
6631 struct _GtkList
6632 {
6633   ...
6634   GList *selection;
6635   guint selection_mode;
6636   ...
6637 }; 
6638 </verb></tscreen>
6639
6640 The selection field of a GtkList points to a linked list of all items
6641 that are currently selected, or NULL if the selection is empty.
6642 So to learn about the current selection we read the GTK_LIST()->selection
6643 field, but do not modify it since the internal fields are maintained by
6644 the gtk_list_*() functions.
6645
6646 The selection_mode of the GtkList determines the selection facilities
6647 of a GtkList and therefore the contents of the GTK_LIST()->selection
6648 field. The selection_mode may be one of the following:
6649
6650 <itemize>
6651 <item> GTK_SELECTION_SINGLE - The selection is either NULL
6652                         or contains a GList pointer
6653                         for a single selected item.
6654
6655 <item> GTK_SELECTION_BROWSE -  The selection is NULL if the list
6656                         contains no widgets or insensitive
6657                         ones only, otherwise it contains
6658                         a GList pointer for one GList
6659                         structure, and therefore exactly
6660                         one list item.
6661
6662 <item> GTK_SELECTION_MULTIPLE -  The selection is NULL if no list
6663                         items are selected or a GList pointer
6664                         for the first selected item. That
6665                         in turn points to a GList structure
6666                         for the second selected item and so
6667                         on.
6668
6669 <item> GTK_SELECTION_EXTENDED - The selection is always NULL.
6670 </itemize>
6671
6672 The default is GTK_SELECTION_MULTIPLE.
6673
6674 <!-- ----------------------------------------------------------------- -->
6675 <sect1> Signals
6676 <p>
6677 <tscreen><verb>
6678 void selection_changed( GtkList *list );
6679 </verb></tscreen>
6680
6681 This signal will be invoked whenever the selection field
6682 of a GtkList has changed. This happens when a child of
6683 the GtkList got selected or deselected.
6684
6685 <tscreen><verb>
6686 void select_child( GtkList   *list,
6687                    GtkWidget *child);
6688 </verb></tscreen>
6689
6690 This signal is invoked when a child of the GtkList is about
6691 to get selected. This happens mainly on calls to
6692 gtk_list_select_item(), gtk_list_select_child(), button presses
6693 and sometimes indirectly triggered on some else occasions where
6694 children get added to or removed from the GtkList.
6695
6696 <tscreen><verb>
6697 void unselect_child( GtkList   *list,
6698                      GtkWidget *child );
6699 </verb></tscreen>
6700
6701 This signal is invoked when a child of the GtkList is about
6702 to get deselected. This happens mainly on calls to
6703 gtk_list_unselect_item(), gtk_list_unselect_child(), button presses
6704 and sometimes indirectly triggered on some else occasions where
6705 children get added to or removed from the GtkList.
6706
6707 <!-- ----------------------------------------------------------------- -->
6708 <sect1> Functions
6709 <p>
6710 <tscreen><verb>
6711 guint gtk_list_get_type( void );
6712 </verb></tscreen>
6713
6714 Returns the `GtkList' type identifier.
6715
6716 <tscreen><verb>
6717 GtkWidget *gtk_list_new( void );
6718 </verb></tscreen>
6719
6720 Create a new GtkList object. The new widget is returned as a pointer to a
6721 GtkWidget object. NULL is returned on failure.
6722
6723 <tscreen><verb>
6724 void gtk_list_insert_items( GtkList *list,
6725                             GList   *items,
6726                             gint     position );
6727 </verb></tscreen>
6728
6729 Insert list items into the list, starting at <tt/position/.
6730 <tt/items/ is a doubly linked list where each nodes data
6731 pointer is expected to point to a newly created GtkListItem.
6732 The GList nodes of <tt/items/ are taken over by the list.
6733
6734 <tscreen><verb>
6735 void gtk_list_append_items( GtkList *list,
6736                             GList   *items);
6737 </verb></tscreen>
6738
6739 Insert list items just like gtk_list_insert_items() at the end
6740 of the list. The GList nodes of <tt/items/ are taken over by the list.
6741
6742 <tscreen><verb>
6743 void gtk_list_prepend_items( GtkList *list,
6744                              GList   *items);
6745 </verb></tscreen>
6746
6747 Insert list items just like gtk_list_insert_items() at the very
6748 beginning of the list. The GList nodes of <tt/items/ are taken over
6749 by the list.
6750
6751 <tscreen><verb>
6752 void gtk_list_remove_items( GtkList *list,
6753                             GList   *items);
6754 </verb></tscreen>
6755
6756 Remove list items from the list. <tt/items/ is a doubly linked
6757 list where each nodes data pointer is expected to point to a
6758 direct child of list. It is the callers responsibility to make a
6759 call to g_list_free(items) afterwards. Also the caller has to
6760 destroy the list items himself.
6761
6762 <tscreen><verb>
6763 void gtk_list_clear_items( GtkList *list,
6764                            gint start,
6765                            gint end );
6766 </verb></tscreen>
6767
6768 Remove and destroy list items from the list. A widget is affected if
6769 its current position within the list is in the range specified by
6770 <tt/start/ and <tt/end/.
6771
6772 <tscreen><verb>
6773 void gtk_list_select_item( GtkList *list,
6774                            gint     item );
6775 </verb></tscreen>
6776
6777 Invoke the select_child signal for a list item
6778 specified through its current position within the list.
6779
6780 <tscreen><verb>
6781 void gtk_list_unselect_item( GtkList *list,
6782                              gint     item);
6783 </verb></tscreen>
6784
6785 Invoke the unselect_child signal for a list item
6786 specified through its current position within the list.
6787
6788 <tscreen><verb>
6789 void gtk_list_select_child( GtkList *list,
6790                             GtkWidget *child);
6791 </verb></tscreen>
6792
6793 Invoke the select_child signal for the specified child.
6794
6795 <tscreen><verb>
6796 void gtk_list_unselect_child( GtkList   *list,
6797                               GtkWidget *child);
6798 </verb></tscreen>
6799
6800 Invoke the unselect_child signal for the specified child.
6801
6802 <tscreen><verb>
6803 gint gtk_list_child_position( GtkList *list,
6804                               GtkWidget *child);
6805 </verb></tscreen>
6806
6807 Return the position of <tt/child/ within the list. "-1" is returned on failure.
6808
6809 <tscreen><verb>
6810 void gtk_list_set_selection_mode( GtkList         *list,
6811                                   GtkSelectionMode mode );
6812 </verb></tscreen>
6813
6814 Set the selection mode MODE which can be of GTK_SELECTION_SINGLE,
6815 GTK_SELECTION_BROWSE, GTK_SELECTION_MULTIPLE or GTK_SELECTION_EXTENDED.
6816
6817 <tscreen><verb>
6818 GtkList *GTK_LIST( gpointer obj );
6819 </verb></tscreen>
6820
6821 Cast a generic pointer to `GtkList *'. *Note Standard Macros::, for
6822 more info.
6823
6824 <tscreen><verb>
6825 GtkListClass *GTK_LIST_CLASS( gpointer class);
6826 </verb></tscreen>
6827
6828 Cast a generic pointer to `GtkListClass*'. *Note Standard Macros::,
6829 for more info.
6830
6831 <tscreen><verb>
6832 gint GTK_IS_LIST( gpointer obj);
6833 </verb></tscreen>
6834
6835 Determine if a generic pointer refers to a `GtkList' object. *Note
6836 Standard Macros::, for more info.
6837
6838 <!-- ----------------------------------------------------------------- -->
6839 <sect1> Example
6840 <p>
6841 Following is an example program that will print out the changes
6842 of the selection of a GtkList, and lets you "arrest" list items
6843 into a prison by selecting them with the rightmost mouse button.
6844
6845 <tscreen><verb>
6846 /* example-start list list.c */
6847
6848 /* include the gtk+ header files
6849  * include stdio.h, we need that for the printf() function
6850  */
6851 #include        <gtk/gtk.h>
6852 #include        <stdio.h>
6853
6854 /* this is our data identification string to store
6855  * data in list items
6856  */
6857 const   gchar   *list_item_data_key="list_item_data";
6858
6859
6860 /* prototypes for signal handler that we are going to connect
6861  * to the GtkList widget
6862  */
6863 static  void    sigh_print_selection    (GtkWidget      *gtklist,
6864                                          gpointer       func_data);
6865 static  void    sigh_button_event       (GtkWidget      *gtklist,
6866                                          GdkEventButton *event,
6867                                          GtkWidget      *frame);
6868
6869
6870 /* main function to set up the user interface */
6871
6872 gint main (int argc, gchar *argv[])
6873 {                                  
6874     GtkWidget       *separator;
6875     GtkWidget       *window;
6876     GtkWidget       *vbox;
6877     GtkWidget       *scrolled_window;
6878     GtkWidget       *frame;
6879     GtkWidget       *gtklist;
6880     GtkWidget       *button;
6881     GtkWidget       *list_item;
6882     GList           *dlist;
6883     guint           i;
6884     gchar           buffer[64];
6885     
6886     
6887     /* initialize gtk+ (and subsequently gdk) */
6888
6889     gtk_init(&amp;argc, &amp;argv);
6890     
6891     
6892     /* create a window to put all the widgets in
6893      * connect gtk_main_quit() to the "destroy" event of
6894      * the window to handle window manager close-window-events
6895      */
6896     window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
6897     gtk_window_set_title(GTK_WINDOW(window), "GtkList Example");
6898     gtk_signal_connect(GTK_OBJECT(window),
6899                        "destroy",
6900                        GTK_SIGNAL_FUNC(gtk_main_quit),
6901                        NULL);
6902     
6903     
6904     /* inside the window we need a box to arrange the widgets
6905      * vertically */
6906     vbox=gtk_vbox_new(FALSE, 5);
6907     gtk_container_border_width(GTK_CONTAINER(vbox), 5);
6908     gtk_container_add(GTK_CONTAINER(window), vbox);
6909     gtk_widget_show(vbox);
6910     
6911     /* this is the scrolled window to put the GtkList widget inside */
6912     scrolled_window=gtk_scrolled_window_new(NULL, NULL);
6913     gtk_widget_set_usize(scrolled_window, 250, 150);
6914     gtk_container_add(GTK_CONTAINER(vbox), scrolled_window);
6915     gtk_widget_show(scrolled_window);
6916     
6917     /* create the GtkList widget
6918      * connect the sigh_print_selection() signal handler
6919      * function to the "selection_changed" signal of the GtkList
6920      * to print out the selected items each time the selection
6921      * has changed */
6922     gtklist=gtk_list_new();
6923     gtk_container_add(GTK_CONTAINER(scrolled_window), gtklist);
6924     gtk_widget_show(gtklist);
6925     gtk_signal_connect(GTK_OBJECT(gtklist),
6926                        "selection_changed",
6927                        GTK_SIGNAL_FUNC(sigh_print_selection),
6928                        NULL);
6929     
6930     /* we create a "Prison" to put a list item in ;)
6931      */
6932     frame=gtk_frame_new("Prison");
6933     gtk_widget_set_usize(frame, 200, 50);
6934     gtk_container_border_width(GTK_CONTAINER(frame), 5);
6935     gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
6936     gtk_container_add(GTK_CONTAINER(vbox), frame);
6937     gtk_widget_show(frame);
6938     
6939     /* connect the sigh_button_event() signal handler to the GtkList
6940      * which will handle the "arresting" of list items
6941      */
6942     gtk_signal_connect(GTK_OBJECT(gtklist),
6943                        "button_release_event",
6944                        GTK_SIGNAL_FUNC(sigh_button_event),
6945                        frame);
6946     
6947     /* create a separator
6948      */
6949     separator=gtk_hseparator_new();
6950     gtk_container_add(GTK_CONTAINER(vbox), separator);
6951     gtk_widget_show(separator);
6952     
6953     /* finally create a button and connect it´s "clicked" signal
6954      * to the destruction of the window
6955      */
6956     button=gtk_button_new_with_label("Close");
6957     gtk_container_add(GTK_CONTAINER(vbox), button);
6958     gtk_widget_show(button);
6959     gtk_signal_connect_object(GTK_OBJECT(button),
6960                               "clicked",
6961                               GTK_SIGNAL_FUNC(gtk_widget_destroy),
6962                               GTK_OBJECT(window));
6963     
6964     
6965     /* now we create 5 list items, each having it´s own
6966      * label and add them to the GtkList using gtk_container_add()
6967      * also we query the text string from the label and
6968      * associate it with the list_item_data_key for each list item
6969      */
6970     for (i=0; i<5; i++) {
6971         GtkWidget       *label;
6972         gchar           *string;
6973         
6974         sprintf(buffer, "ListItemContainer with Label #%d", i);
6975         label=gtk_label_new(buffer);
6976         list_item=gtk_list_item_new();
6977         gtk_container_add(GTK_CONTAINER(list_item), label);
6978         gtk_widget_show(label);
6979         gtk_container_add(GTK_CONTAINER(gtklist), list_item);
6980         gtk_widget_show(list_item);
6981         gtk_label_get(GTK_LABEL(label), &amp;string);
6982         gtk_object_set_data(GTK_OBJECT(list_item),
6983                             list_item_data_key,
6984                             string);
6985     }
6986     /* here, we are creating another 5 labels, this time
6987      * we use gtk_list_item_new_with_label() for the creation
6988      * we can´t query the text string from the label because
6989      * we don´t have the labels pointer and therefore
6990      * we just associate the list_item_data_key of each
6991      * list item with the same text string
6992      * for adding of the list items we put them all into a doubly
6993      * linked list (GList), and then add them by a single call to
6994      * gtk_list_append_items()
6995      * because we use g_list_prepend() to put the items into the
6996      * doubly linked list, their order will be descending (instead
6997      * of ascending when using g_list_append())
6998      */
6999     dlist=NULL;
7000     for (; i<10; i++) {
7001         sprintf(buffer, "List Item with Label %d", i);
7002         list_item=gtk_list_item_new_with_label(buffer);
7003         dlist=g_list_prepend(dlist, list_item);
7004         gtk_widget_show(list_item);
7005         gtk_object_set_data(GTK_OBJECT(list_item),
7006                             list_item_data_key,
7007                             "ListItem with integrated Label");
7008     }
7009     gtk_list_append_items(GTK_LIST(gtklist), dlist);
7010     
7011     /* finally we want to see the window, don't we? ;)
7012      */
7013     gtk_widget_show(window);
7014     
7015     /* fire up the main event loop of gtk
7016      */
7017     gtk_main();
7018     
7019     /* we get here after gtk_main_quit() has been called which
7020      * happens if the main window gets destroyed
7021      */
7022     return 0;
7023 }
7024
7025 /* this is the signal handler that got connected to button
7026  * press/release events of the GtkList
7027  */
7028 void
7029 sigh_button_event       (GtkWidget      *gtklist,
7030                          GdkEventButton *event,
7031                          GtkWidget      *frame)
7032 {
7033     /* we only do something if the third (rightmost mouse button
7034      * was released
7035      */
7036     if (event->type==GDK_BUTTON_RELEASE &amp;&amp;
7037         event->button==3) {
7038         GList           *dlist, *free_list;
7039         GtkWidget       *new_prisoner;
7040         
7041         /* fetch the currently selected list item which
7042          * will be our next prisoner ;)
7043          */
7044         dlist=GTK_LIST(gtklist)->selection;
7045         if (dlist)
7046                 new_prisoner=GTK_WIDGET(dlist->data);
7047         else
7048                 new_prisoner=NULL;
7049         
7050         /* look for already imprisoned list items, we
7051          * will put them back into the list
7052          * remember to free the doubly linked list that
7053          * gtk_container_children() returns
7054          */
7055         dlist=gtk_container_children(GTK_CONTAINER(frame));
7056         free_list=dlist;
7057         while (dlist) {
7058             GtkWidget       *list_item;
7059             
7060             list_item=dlist->data;
7061             
7062             gtk_widget_reparent(list_item, gtklist);
7063             
7064             dlist=dlist->next;
7065         }
7066         g_list_free(free_list);
7067         
7068         /* if we have a new prisoner, remove him from the
7069          * GtkList and put him into the frame "Prison"
7070          * we need to unselect the item before
7071          */
7072         if (new_prisoner) {
7073             GList   static_dlist;
7074             
7075             static_dlist.data=new_prisoner;
7076             static_dlist.next=NULL;
7077             static_dlist.prev=NULL;
7078             
7079             gtk_list_unselect_child(GTK_LIST(gtklist),
7080                                     new_prisoner);
7081             gtk_widget_reparent(new_prisoner, frame);
7082         }
7083     }
7084 }
7085
7086 /* this is the signal handler that gets called if GtkList
7087  * emits the "selection_changed" signal
7088  */
7089 void
7090 sigh_print_selection    (GtkWidget      *gtklist,
7091                          gpointer       func_data)
7092 {
7093     GList   *dlist;
7094     
7095     /* fetch the doubly linked list of selected items
7096      * of the GtkList, remember to treat this as read-only!
7097      */
7098     dlist=GTK_LIST(gtklist)->selection;
7099     
7100     /* if there are no selected items there is nothing more
7101      * to do than just telling the user so
7102      */
7103     if (!dlist) {
7104         g_print("Selection cleared\n");
7105         return;
7106     }
7107     /* ok, we got a selection and so we print it
7108      */
7109     g_print("The selection is a ");
7110     
7111     /* get the list item from the doubly linked list
7112      * and then query the data associated with list_item_data_key
7113      * we then just print it
7114      */
7115     while (dlist) {
7116         GtkObject       *list_item;
7117         gchar           *item_data_string;
7118         
7119         list_item=GTK_OBJECT(dlist->data);
7120         item_data_string=gtk_object_get_data(list_item,
7121                                              list_item_data_key);
7122         g_print("%s ", item_data_string);
7123         
7124         dlist=dlist->next;
7125     }
7126     g_print("\n");
7127 }
7128 /* example-end */
7129 </verb></tscreen>
7130
7131 <!-- ----------------------------------------------------------------- -->
7132 <sect1> List Item Widget
7133 <p>
7134 The GtkListItem widget is designed to act as a container holding up
7135 to one child, providing functions for selection/deselection just like
7136 the GtkList widget requires them for its children.
7137
7138 A GtkListItem has its own window to receive events and has its own
7139 background color which is usually white.  
7140
7141 As it is directly derived from a
7142 GtkItem it can be treated as such by using the GTK_ITEM(ListItem)
7143 macro, see the GtkItem widget for more on this.
7144 Usually a GtkListItem just holds a label to identify e.g. a filename
7145 within a GtkList -- therefore the convenience function
7146 gtk_list_item_new_with_label() is provided.  The same effect can be
7147 achieved by creating a GtkLabel on its own, setting its alignment
7148 to xalign=0 and yalign=0.5 with a subsequent container addition
7149 to the GtkListItem.
7150
7151 As one is not forced to add a GtkLabel to a GtkListItem, you could
7152 also add a GtkVBox or a GtkArrow etc. to the GtkListItem.
7153
7154 <!-- ----------------------------------------------------------------- -->
7155 <sect1> Signals
7156 <p>
7157 A GtkListItem does not create new signals on its own, but inherits
7158 the signals of a GtkItem. *Note GtkItem::, for more info.
7159
7160 <!-- ----------------------------------------------------------------- -->
7161 <sect1> Functions
7162 <p>
7163 <tscreen><verb>
7164 guint gtk_list_item_get_type( void );
7165 </verb></tscreen>
7166
7167 Returns the `GtkListItem' type identifier.
7168
7169 <tscreen><verb>
7170 GtkWidget *gtk_list_item_new( void );
7171 </verb></tscreen>
7172
7173 Create a new GtkListItem object. The new widget is returned as a pointer
7174 to a GtkWidget object. NULL is returned on failure.
7175
7176 <tscreen><verb>
7177 GtkWidget *gtk_list_item_new_with_label( gchar *label );
7178 </verb></tscreen>
7179
7180 Create a new GtkListItem object, having a single GtkLabel as
7181 the sole child. The new widget is returned as a pointer to a
7182 GtkWidget object. NULL is returned on failure.
7183
7184 <tscreen><verb>
7185 void gtk_list_item_select( GtkListItem *list_item );
7186 </verb></tscreen>
7187
7188 This function is basically a wrapper around a call to
7189 gtk_item_select (GTK_ITEM (list_item)) which will emit the
7190 select signal.
7191 *Note GtkItem::, for more info.
7192
7193 <tscreen><verb>
7194 void gtk_list_item_deselect( GtkListItem *list_item );
7195 </verb></tscreen>
7196
7197 This function is basically a wrapper around a call to
7198 gtk_item_deselect (GTK_ITEM (list_item)) which will emit the
7199 deselect signal.
7200 *Note GtkItem::, for more info.
7201
7202 <tscreen><verb>
7203 GtkListItem *GTK_LIST_ITEM( gpointer obj );
7204 </verb></tscreen>
7205
7206 Cast a generic pointer to `GtkListItem*'. *Note Standard Macros::,
7207 for more info.
7208
7209 <tscreen><verb>
7210 GtkListItemClass *GTK_LIST_ITEM_CLASS( gpointer class );
7211 </verb></tscreen>
7212
7213 Cast a generic pointer to GtkListItemClass*. *Note Standard
7214 Macros::, for more info.
7215
7216 <tscreen><verb>
7217 gint GTK_IS_LIST_ITEM( gpointer obj );
7218 </verb></tscreen>
7219
7220 Determine if a generic pointer refers to a `GtkListItem' object.
7221 *Note Standard Macros::, for more info.
7222  
7223 <!-- ----------------------------------------------------------------- -->
7224 <sect1> Example
7225 <p>
7226 Please see the GtkList example on this, which covers the usage of a
7227 GtkListItem as well.
7228
7229 <!-- ***************************************************************** -->
7230 <sect> Tree Widget<label id="sec_Tree_Widgets">
7231 <!-- ***************************************************************** -->
7232 <p>
7233
7234 The purpose of tree widgets is to display hierarchically-organized
7235 data. The GtkTree widget itself is a vertical container for widgets
7236 of type GtkTreeItem. GtkTree itself is not terribly different from
7237 GtkList - both are derived directly from GtkContainer, and the
7238 GtkContainer methods work in the same way on GtkTree widgets as on
7239 GtkList widgets. The difference is that GtkTree widgets can be nested
7240 within other GtkTree widgets. We'll see how to do this shortly.
7241
7242 The GtkTree widget has its own window, and defaults to a white
7243 background, as does GtkList. Also, most of the GtkTree methods work
7244 in the same way as the corresponding GtkList ones. However, GtkTree
7245 is not derived from GtkList, so you cannot use them interchangeably.
7246
7247 <sect1> Creating a Tree
7248 <p>
7249 A GtkTree is created in the usual way, using:
7250
7251 <tscreen><verb>
7252 GtkWidget* gtk_tree_new( void );
7253 </verb></tscreen>
7254
7255 Like the GtkList widget, a GtkTree will simply keep growing as more
7256 items are added to it, as well as when subtrees are expanded.
7257 For this reason, they are almost always packed into a
7258 GtkScrolledWindow. You might want to use gtk_widget_set_usize() on
7259 the scrolled window to ensure that it is big enough to see the tree's
7260 items, as the default size for GtkScrolledWindow is quite small.
7261
7262 Now that you have a tree, you'll probably want to add some items to
7263 it.  <ref id="sec_Tree_Item_Widget" name="The Tree Item Widget"> below
7264 explains the gory details of GtkTreeItem. For now, it'll suffice to
7265 create one, using:
7266
7267 <tscreen><verb>
7268 GtkWidget* gtk_tree_item_new_with_label( gchar *label );
7269 </verb></tscreen>
7270
7271 You can then add it to the tree using one of the following (see
7272 <ref id="sec_GtkTree_Functions" name="Functions and Macros">
7273 below for more options):
7274
7275 <tscreen><verb>
7276 void gtk_tree_append( GtkTree    *tree,
7277                        GtkWidget *tree_item );
7278
7279 void gtk_tree_prepend( GtkTree   *tree,
7280                        GtkWidget *tree_item );
7281 </verb></tscreen>
7282
7283 Note that you must add items to a GtkTree one at a time - there is no
7284 equivalent to gtk_list_*_items().
7285
7286 <sect1> Adding a Subtree
7287 <p>
7288 A subtree is created like any other GtkTree widget. A subtree is added
7289 to another tree beneath a tree item, using:
7290
7291 <tscreen><verb>
7292 void gtk_tree_item_set_subtree( GtkTreeItem *tree_item,
7293                                 GtkWidget   *subtree );
7294 </verb></tscreen>
7295
7296 You do not need to call gtk_widget_show() on a subtree before or after
7297 adding it to a GtkTreeItem. However, you <em>must</em> have added the
7298 GtkTreeItem in question to a parent tree before calling
7299 gtk_tree_item_set_subtree(). This is because, technically, the parent
7300 of the subtree is <em>not</em> the GtkTreeItem which "owns" it, but
7301 rather the GtkTree which holds that GtkTreeItem.
7302
7303 When you add a subtree to a GtkTreeItem, a plus or minus sign appears
7304 beside it, which the user can click on to "expand" or "collapse" it,
7305 meaning, to show or hide its subtree. GtkTreeItems are collapsed by
7306 default. Note that when you collapse a GtkTreeItem, any selected
7307 items in its subtree remain selected, which may not be what the user
7308 expects.
7309
7310 <sect1> Handling the Selection List
7311 <p>
7312 As with GtkList, the GtkTree type has a <tt>selection</tt> field, and
7313 it is possible to control the behaviour of the tree (somewhat) by
7314 setting the selection type using:
7315
7316 <tscreen><verb>
7317 void gtk_tree_set_selection_mode( GtkTree          *tree,
7318                                   GtkSelectionMode  mode );
7319 </verb></tscreen>
7320
7321 The semantics associated with the various selection modes are
7322 described in the section on the GtkList widget.  As with the GtkList
7323 widget, the "select_child", "unselect_child" (not really - see <ref
7324 id="sec_GtkTree_Signals" name="Signals"> below for an explanation),
7325 and "selection_changed" signals are emitted when list items are
7326 selected or unselected.  However, in order to take advantage of these
7327 signals, you need to know <em>which</em> GtkTree widget they will be
7328 emitted by, and where to find the list of selected items.
7329
7330 This is a source of potential confusion. The best way to explain this
7331 is that though all GtkTree widgets are created equal, some are more
7332 equal than others. All GtkTree widgets have their own X window, and
7333 can therefore receive events such as mouse clicks (if their
7334 GtkTreeItems or their children don't catch them first!). However, to
7335 make GTK_SELECTION_SINGLE and GTK_SELECTION_BROWSE selection types
7336 behave in a sane manner, the list of selected items is specific to the
7337 topmost GtkTree widget in a hierarchy, known as the "root tree".
7338
7339 Thus, accessing the <tt>selection</tt>field directly in an arbitrary
7340 GtkTree widget is not a good idea unless you <em>know</em> it's the
7341 root tree.  Instead, use the GTK_TREE_SELECTION (Tree) macro, which
7342 gives the root tree's selection list as a GList pointer. Of course,
7343 this list can include items that are not in the subtree in question if 
7344 the selection type is GTK_SELECTION_MULTIPLE.
7345
7346 Finally, the "select_child" (and "unselect_child", in theory) signals
7347 are emitted by all trees, but the "selection_changed" signal is only
7348 emitted by the root tree. Consequently, if you want to handle the
7349 "select_child" signal for a tree and all its subtrees, you will have
7350 to call gtk_signal_connect() for every subtree.
7351
7352 <sect1> Tree Widget Internals
7353 <p>
7354 The GtkTree's struct definition looks like this:
7355
7356 <tscreen><verb>
7357 struct _GtkTree
7358 {
7359   GtkContainer container;
7360
7361   GList *children;
7362   
7363   GtkTree* root_tree; /* owner of selection list */
7364   GtkWidget* tree_owner;
7365   GList *selection;
7366   guint level;
7367   guint indent_value;
7368   guint current_indent;
7369   guint selection_mode : 2;
7370   guint view_mode : 1;
7371   guint view_line : 1;
7372 };
7373 </verb></tscreen>
7374
7375 The perils associated with accessing the <tt>selection</tt> field
7376 directly have already been mentioned.  The other important fields of
7377 the struct can also be accessed with handy macros or class functions.
7378 GTK_TREE_IS_ROOT_TREE (Tree) returns a boolean value which indicates
7379 whether a tree is the root tree in a GtkTree hierarchy, while
7380 GTK_TREE_ROOT_TREE (Tree) returns the root tree, an object of type
7381 GtkTree (so, remember to cast it using GTK_WIDGET (Tree) if you want
7382 to use one of the gtk_widget_*() functions on it).
7383
7384 Instead of directly accessing the children field of a GtkTree widget,
7385 it's probably best to cast it using GTK_CONTAINER (Tree), and pass it
7386 to the gtk_container_children() function. This creates a duplicate of
7387 the original list, so it's advisable to free it up using g_list_free() 
7388 after you're done with it, or to iterate on it destructively, like
7389 this:
7390
7391 <tscreen><verb>
7392 children = gtk_container_children (GTK_CONTAINER (tree));
7393 while (children) {
7394   do_something_nice (GTK_TREE_ITEM (children->data));
7395   children = g_list_remove_link (children, children);
7396 }
7397 </verb></tscreen>
7398
7399 The <tt>tree_owner</tt> field is defined only in subtrees, where it
7400 points to the GtkTreeItem widget which holds the tree in question.
7401 The <tt>level</tt> field indicates how deeply nested a particular tree
7402 is; root trees have level 0, and each successive level of subtrees has
7403 a level one greater than the parent level.  This field is set only
7404 after a GtkTree widget is actually mapped (i.e. drawn on the screen).
7405
7406 <sect2> Signals<label id="sec_GtkTree_Signals">
7407 <p>
7408 <tscreen><verb>
7409 void selection_changed( GtkTree *tree );
7410 </verb></tscreen>
7411
7412 This signal will be emitted whenever the <tt>selection</tt> field of a
7413 GtkTree has changed. This happens when a child of the GtkTree is
7414 selected or deselected.
7415
7416 <tscreen><verb>
7417 void select_child( GtkTree   *tree,
7418                    GtkWidget *child );
7419 </verb></tscreen>
7420
7421 This signal is emitted when a child of the GtkTree is about to get
7422 selected. This happens on calls to gtk_tree_select_item(),
7423 gtk_tree_select_child(), on <em>all</em> button presses and calls to
7424 gtk_tree_item_toggle() and gtk_item_toggle().  It may sometimes be
7425 indirectly triggered on other occasions where children get added to or
7426 removed from the GtkTree.
7427
7428 <tscreen><verb>
7429 void unselect_child (GtkTree   *tree,
7430                      GtkWidget *child);
7431 </verb></tscreen>
7432
7433 This signal is emitted when a child of the GtkTree is about to get
7434 deselected. As of GTK+ 1.0.4, this seems to only occur on calls to
7435 gtk_tree_unselect_item() or gtk_tree_unselect_child(), and perhaps on
7436 other occasions, but <em>not</em> when a button press deselects a
7437 child, nor on emission of the "toggle" signal by gtk_item_toggle().
7438
7439 <sect2> Functions and Macros<label id="sec_GtkTree_Functions">
7440 <p>
7441 <tscreen><verb>
7442 guint gtk_tree_get_type( void );
7443 </verb></tscreen>
7444
7445 Returns the `GtkTree' type identifier.
7446
7447 <tscreen><verb>
7448 GtkWidget* gtk_tree_new( void );
7449 </verb></tscreen>
7450
7451 Create a new GtkTree object. The new widget is returned as a pointer to a
7452 GtkWidget object. NULL is returned on failure.
7453
7454 <tscreen><verb>
7455 void gtk_tree_append( GtkTree   *tree,
7456                       GtkWidget *tree_item );
7457 </verb></tscreen>
7458
7459 Append a tree item to a GtkTree.
7460
7461 <tscreen><verb>
7462 void gtk_tree_prepend( GtkTree   *tree,
7463                        GtkWidget *tree_item );
7464 </verb></tscreen>
7465
7466 Prepend a tree item to a GtkTree.
7467
7468 <tscreen><verb>
7469 void gtk_tree_insert( GtkTree   *tree,
7470                       GtkWidget *tree_item,
7471                       gint       position );
7472 </verb></tscreen>
7473
7474 Insert a tree item into a GtkTree at the position in the list
7475 specified by <tt>position.</tt>
7476
7477 <tscreen><verb>
7478 void gtk_tree_remove_items( GtkTree *tree,
7479                             GList   *items );
7480 </verb></tscreen>
7481
7482 Remove a list of items (in the form of a GList *) from a GtkTree.
7483 Note that removing an item from a tree dereferences (and thus usually)
7484 destroys it <em>and</em> its subtree, if it has one, <em>and</em> all
7485 subtrees in that subtree.  If you want to remove only one item, you
7486 can use gtk_container_remove().
7487
7488 <tscreen><verb>
7489 void gtk_tree_clear_items( GtkTree *tree,
7490                            gint     start,
7491                            gint     end );
7492 </verb></tscreen>
7493
7494 Remove the items from position <tt>start</tt> to position <tt>end</tt>
7495 from a GtkTree.  The same warning about dereferencing applies here, as
7496 gtk_tree_clear_items() simply constructs a list and passes it to
7497 gtk_tree_remove_items().
7498
7499 <tscreen><verb>
7500 void gtk_tree_select_item( GtkTree *tree,
7501                            gint     item );
7502 </verb></tscreen>
7503
7504 Emits the "select_item" signal for the child at position
7505 <tt>item</tt>, thus selecting the child (unless you unselect it in a
7506 signal handler...)
7507
7508 <tscreen><verb>
7509 void gtk_tree_unselect_item( GtkTree *tree,
7510                              gint     item );
7511 </verb></tscreen>
7512
7513 Emits the "unselect_item" signal for the child at position
7514 <tt>item</tt>, thus unselecting the child.
7515
7516 <tscreen><verb>
7517 void gtk_tree_select_child( GtkTree   *tree,
7518                             GtkWidget *tree_item );
7519 </verb></tscreen>
7520
7521 Emits the "select_item" signal for the child <tt>tree_item</tt>, thus
7522 selecting it.
7523
7524 <tscreen><verb>
7525 void gtk_tree_unselect_child( GtkTree   *tree,
7526                               GtkWidget *tree_item );
7527 </verb></tscreen>
7528
7529 Emits the "unselect_item" signal for the child <tt>tree_item</tt>,
7530 thus unselecting it.
7531
7532 <tscreen><verb>
7533 gint gtk_tree_child_position( GtkTree   *tree,
7534                               GtkWidget *child );
7535 </verb></tscreen>
7536
7537 Returns the position in the tree of <tt>child</tt>, unless
7538 <tt>child</tt> is not in the tree, in which case it returns -1.
7539
7540 <tscreen><verb>
7541 void gtk_tree_set_selection_mode( GtkTree          *tree,
7542                                   GtkSelectionMode  mode );
7543 </verb></tscreen>
7544
7545 Sets the selection mode, which can be one of GTK_SELECTION_SINGLE (the
7546 default), GTK_SELECTION_BROWSE, GTK_SELECTION_MULTIPLE, or
7547 GTK_SELECTION_EXTENDED. This is only defined for root trees, which
7548 makes sense, since the root tree "owns" the selection. Setting it for
7549 subtrees has no effect at all; the value is simply ignored.
7550
7551 <tscreen><verb>
7552 void gtk_tree_set_view_mode( GtkTree         *tree,
7553                              GtkTreeViewMode  mode ); 
7554 </verb></tscreen>
7555
7556 Sets the "view mode", which can be either GTK_TREE_VIEW_LINE (the
7557 default) or GTK_TREE_VIEW_ITEM.  The view mode propagates from a tree
7558 to its subtrees, and can't be set exclusively to a subtree (this is
7559 not exactly true - see the example code comments).
7560
7561 The term "view mode" is rather ambiguous - basically, it controls the
7562 way the highlight is drawn when one of a tree's children is selected.
7563 If it's GTK_TREE_VIEW_LINE, the entire GtkTreeItem widget is
7564 highlighted, while for GTK_TREE_VIEW_ITEM, only the child widget
7565 (i.e. usually the label) is highlighted.
7566
7567 <tscreen><verb>
7568 void gtk_tree_set_view_lines( GtkTree *tree,
7569                               guint    flag );
7570 </verb></tscreen>
7571
7572 Controls whether connecting lines between tree items are drawn.
7573 <tt>flag</tt> is either TRUE, in which case they are, or FALSE, in
7574 which case they aren't.
7575
7576 <tscreen><verb>
7577 GtkTree *GTK_TREE (gpointer obj);
7578 </verb></tscreen>
7579
7580 Cast a generic pointer to `GtkTree *'.
7581
7582 <tscreen><verb>
7583 GtkTreeClass *GTK_TREE_CLASS (gpointer class);
7584 </verb></tscreen>
7585
7586 Cast a generic pointer to `GtkTreeClass*'.
7587
7588 <tscreen><verb>
7589 gint GTK_IS_TREE (gpointer obj);
7590 </verb></tscreen>
7591
7592 Determine if a generic pointer refers to a `GtkTree' object.
7593
7594 <tscreen><verb>
7595 gint GTK_IS_ROOT_TREE (gpointer obj)
7596 </verb></tscreen>
7597
7598 Determine if a generic pointer refers to a `GtkTree' object
7599 <em>and</em> is a root tree. Though this will accept any pointer, the
7600 results of passing it a pointer that does not refer to a GtkTree are
7601 undefined and possibly harmful.
7602
7603 <tscreen><verb>
7604 GtkTree *GTK_TREE_ROOT_TREE (gpointer obj)
7605 </verb></tscreen>
7606
7607 Return the root tree of a pointer to a `GtkTree' object. The above
7608 warning applies.
7609
7610 <tscreen><verb>
7611 GList *GTK_TREE_SELECTION( gpointer obj)
7612 </verb></tscreen>
7613
7614 Return the selection list of the root tree of a `GtkTree' object. The 
7615 above warning applies here, too.
7616
7617 <sect1> Tree Item Widget<label id="sec_Tree_Item_Widget">
7618 <p>
7619 The GtkTreeItem widget, like GtkListItem, is derived from GtkItem,
7620 which in turn is derived from GtkBin.  Therefore, the item itself is a
7621 generic container holding exactly one child widget, which can be of
7622 any type.  The GtkTreeItem widget has a number of extra fields, but
7623 the only one we need be concerned with is the <tt>subtree</tt> field.
7624
7625 The definition for the GtkTreeItem struct looks like this:
7626
7627 <tscreen><verb>
7628 struct _GtkTreeItem
7629 {
7630   GtkItem item;
7631
7632   GtkWidget *subtree;
7633   GtkWidget *pixmaps_box;
7634   GtkWidget *plus_pix_widget, *minus_pix_widget;
7635
7636   GList *pixmaps;               /* pixmap node for this items color depth */
7637
7638   guint expanded : 1;
7639 };
7640 </verb></tscreen>
7641
7642 The <tt>pixmaps_box</tt> field is a GtkEventBox which catches clicks
7643 on the plus/minus symbol which controls expansion and collapsing.  The
7644 <tt>pixmaps</tt> field points to an internal data structure.  Since
7645 you can always obtain the subtree of a GtkTreeItem in a (relatively)
7646 type-safe manner with the GTK_TREE_ITEM_SUBTREE (Item) macro, it's
7647 probably advisable never to touch the insides of a GtkTreeItem unless
7648 you <em>really</em> know what you're doing.
7649
7650 Since it is directly derived from a GtkItem it can be treated as such
7651 by using the GTK_ITEM (TreeItem) macro. A GtkTreeItem usually holds a
7652 label, so the convenience function gtk_list_item_new_with_label() is
7653 provided. The same effect can be achieved using code like the
7654 following, which is actually copied verbatim from
7655 gtk_tree_item_new_with_label():
7656
7657 <tscreen><verb>
7658 tree_item = gtk_tree_item_new ();
7659 label_widget = gtk_label_new (label);
7660 gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
7661
7662 gtk_container_add (GTK_CONTAINER (tree_item), label_widget);
7663 gtk_widget_show (label_widget);
7664 </verb></tscreen>
7665
7666 As one is not forced to add a GtkLabel to a GtkTreeItem, you could
7667 also add a GtkHBox or a GtkArrow, or even a GtkNotebook (though your
7668 app will likely be quite unpopular in this case) to the GtkTreeItem.
7669
7670 If you remove all the items from a subtree, it will be destroyed and
7671 unparented, unless you reference it beforehand, and the GtkTreeItem
7672 which owns it will be collapsed.  So, if you want it to stick around,
7673 do something like the following:
7674
7675 <tscreen><verb>
7676 gtk_widget_ref (tree);
7677 owner = GTK_TREE(tree)->tree_owner;
7678 gtk_container_remove (GTK_CONTAINER(tree), item);
7679 if (tree->parent == NULL){
7680   gtk_tree_item_expand (GTK_TREE_ITEM(owner));
7681   gtk_tree_item_set_subtree (GTK_TREE_ITEM(owner), tree);
7682 }
7683 else
7684   gtk_widget_unref (tree);
7685 </verb></tscreen>
7686
7687 Finally, drag-n-drop <em>does</em> work with GtkTreeItems.  You just
7688 have to make sure that the GtkTreeItem you want to make into a drag
7689 item or a drop site has not only been added to a GtkTree, but that
7690 each successive parent widget has a parent itself, all the way back to
7691 a toplevel or dialog window, when you call gtk_widget_dnd_drag_set()
7692 or gtk_widget_dnd_drop_set().  Otherwise, strange things will happen.
7693
7694 <sect2> Signals
7695 <p>
7696 GtkTreeItem inherits the "select", "deselect", and "toggle" signals
7697 from GtkItem.  In addition, it adds two signals of its own, "expand"
7698 and "collapse".
7699
7700 <tscreen><verb>
7701 void select( GtkItem *tree_item );
7702 </verb></tscreen>
7703
7704 This signal is emitted when an item is about to be selected, either
7705 after it has been clicked on by the user, or when the program calls
7706 gtk_tree_item_select(), gtk_item_select(), or gtk_tree_select_child().
7707
7708 <tscreen><verb>
7709 void deselect( GtkItem *tree_item );
7710 </verb></tscreen>
7711
7712 This signal is emitted when an item is about to be unselected, either
7713 after it has been clicked on by the user, or when the program calls
7714 gtk_tree_item_deselect() or gtk_item_deselect(). In the case of
7715 GtkTreeItems, it is also emitted by gtk_tree_unselect_child(), and
7716 sometimes gtk_tree_select_child().
7717
7718 <tscreen><verb>
7719 void toggle( GtkItem *tree_item );
7720 </verb></tscreen>
7721
7722 This signal is emitted when the program calls gtk_item_toggle().  The
7723 effect it has when emitted on a GtkTreeItem is to call
7724 gtk_tree_select_child() (and never gtk_tree_unselect_child()) on the
7725 item's parent tree, if the item has a parent tree.  If it doesn't,
7726 then the highlight is reversed on the item.
7727
7728 <tscreen><verb>
7729 void expand( GtkTreeItem *tree_item );
7730 </verb></tscreen>
7731
7732 This signal is emitted when the tree item's subtree is about to be
7733 expanded, that is, when the user clicks on the plus sign next to the
7734 item, or when the program calls gtk_tree_item_expand().
7735
7736 <tscreen><verb>
7737 void collapse( GtkTreeItem *tree_item );
7738 </verb></tscreen>
7739
7740 This signal is emitted when the tree item's subtree is about to be
7741 collapsed, that is, when the user clicks on the minus sign next to the
7742 item, or when the program calls gtk_tree_item_collapse().
7743
7744 <sect2> Functions and Macros
7745 <p>
7746 <tscreen><verb>
7747 guint gtk_tree_item_get_type( void );
7748 </verb></tscreen>
7749
7750 Returns the `GtkTreeItem' type identifier.
7751
7752 <tscreen><verb>
7753 GtkWidget* gtk_tree_item_new( void );
7754 </verb></tscreen>
7755
7756 Create a new GtkTreeItem object. The new widget is returned as a pointer
7757 to a GtkWidget object. NULL is returned on failure.
7758
7759 <tscreen><verb>
7760 GtkWidget* gtk_tree_item_new_with_label (gchar       *label);
7761 </verb></tscreen>
7762
7763 Create a new GtkTreeItem object, having a single GtkLabel as
7764 the sole child. The new widget is returned as a pointer to a
7765 GtkWidget object. NULL is returned on failure.
7766
7767 <tscreen><verb>
7768 void gtk_tree_item_select( GtkTreeItem *tree_item );
7769 </verb></tscreen>
7770
7771 This function is basically a wrapper around a call to
7772 gtk_item_select (GTK_ITEM (tree_item)) which will emit the
7773 select signal.
7774
7775 <tscreen><verb>
7776 void gtk_tree_item_deselect( GtkTreeItem *tree_item );
7777 </verb></tscreen>
7778
7779 This function is basically a wrapper around a call to
7780 gtk_item_deselect (GTK_ITEM (tree_item)) which will emit the
7781 deselect signal.
7782
7783 <tscreen><verb>
7784 void gtk_tree_item_set_subtree( GtkTreeItem *tree_item,
7785                                 GtkWidget   *subtree );
7786 </verb></tscreen>
7787
7788 This function adds subtree to tree_item, showing it if tree_item is
7789 expanded, or hiding it if tree_item is collapsed. Again, remember
7790 that the tree_item must have already been added to a tree for this to
7791 work.
7792
7793 <tscreen><verb>
7794 void gtk_tree_item_remove_subtree( GtkTreeItem *tree_item );
7795 </verb></tscreen>
7796
7797 This removes all of tree_item's subtree's children (thus unreferencing
7798 and destroying it, any of its children's subtrees, and so on...), then
7799 removes the subtree itself, and hides the plus/minus sign.
7800
7801 <tscreen><verb>
7802 void gtk_tree_item_expand( GtkTreeItem *tree_item );
7803 </verb></tscreen>
7804
7805 This emits the "expand" signal on tree_item, which expands it.
7806
7807 <tscreen><verb>
7808 void gtk_tree_item_collapse( GtkTreeItem *tree_item );
7809 </verb></tscreen>
7810
7811 This emits the "collapse" signal on tree_item, which collapses it.
7812
7813 <tscreen><verb>
7814 GtkTreeItem *GTK_TREE_ITEM (gpointer obj)
7815 </verb></tscreen>
7816
7817 Cast a generic pointer to `GtkTreeItem*'.
7818
7819 <tscreen><verb>
7820 GtkTreeItemClass *GTK_TREE_ITEM_CLASS (gpointer obj)
7821 </verb></tscreen>
7822
7823 Cast a generic pointer to `GtkTreeItemClass'.
7824
7825 <tscreen><verb>
7826 gint GTK_IS_TREE_ITEM (gpointer obj)
7827 </verb></tscreen>
7828
7829 Determine if a generic pointer refers to a `GtkTreeItem' object.
7830  
7831 <tscreen><verb>
7832 GtkWidget GTK_TREE_ITEM_SUBTREE (gpointer obj)
7833 </verb></tscreen>
7834
7835 Returns a tree item's subtree (obj should point to a `GtkTreeItem'
7836 object).
7837
7838 <sect1> Tree Example
7839 <p>
7840 This is somewhat like the tree example in testgtk.c, but a lot less
7841 complete (although much better commented).  It puts up a window with a
7842 tree, and connects all the signals for the relevant objects, so you
7843 can see when they are emitted.
7844
7845 <tscreen><verb>
7846 /* example-start tree tree.c */
7847
7848 #include <gtk/gtk.h>
7849
7850 /* for all the GtkItem:: and GtkTreeItem:: signals */
7851 static void cb_itemsignal (GtkWidget *item, gchar *signame)
7852 {
7853   gchar *name;
7854   GtkLabel *label;
7855
7856   /* It's a GtkBin, so it has one child, which we know to be a
7857      label, so get that */
7858   label = GTK_LABEL (GTK_BIN (item)->child);
7859   /* Get the text of the label */
7860   gtk_label_get (label, &amp;name);
7861   /* Get the level of the tree which the item is in */
7862   g_print ("%s called for item %s->%p, level %d\n", signame, name,
7863            item, GTK_TREE (item->parent)->level);
7864 }
7865
7866 /* Note that this is never called */
7867 static void cb_unselect_child (GtkWidget *root_tree, GtkWidget *child,
7868                                GtkWidget *subtree)
7869 {
7870   g_print ("unselect_child called for root tree %p, subtree %p, child %p\n",
7871            root_tree, subtree, child);
7872 }
7873
7874 /* Note that this is called every time the user clicks on an item,
7875    whether it is already selected or not. */
7876 static void cb_select_child (GtkWidget *root_tree, GtkWidget *child,
7877                              GtkWidget *subtree)
7878 {
7879   g_print ("select_child called for root tree %p, subtree %p, child %p\n",
7880            root_tree, subtree, child);
7881 }
7882
7883 static void cb_selection_changed (GtkWidget *tree)
7884 {
7885   GList *i;
7886   
7887   g_print ("selection_change called for tree %p\n", tree);
7888   g_print ("selected objects are:\n");
7889
7890   i = GTK_TREE_SELECTION(tree);
7891   while (i){
7892     gchar *name;
7893     GtkLabel *label;
7894     GtkWidget *item;
7895
7896     /* Get a GtkWidget pointer from the list node */
7897     item = GTK_WIDGET (i->data);
7898     label = GTK_LABEL (GTK_BIN (item)->child);
7899     gtk_label_get (label, &amp;name);
7900     g_print ("\t%s on level %d\n", name, GTK_TREE
7901              (item->parent)->level);
7902     i = i->next;
7903   }
7904 }
7905
7906 int main (int argc, char *argv[])
7907 {
7908   GtkWidget *window, *scrolled_win, *tree;
7909   static gchar *itemnames[] = {"Foo", "Bar", "Baz", "Quux",
7910                                "Maurice"};
7911   gint i;
7912
7913   gtk_init (&amp;argc, &amp;argv);
7914
7915   /* a generic toplevel window */
7916   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
7917   gtk_signal_connect (GTK_OBJECT(window), "delete_event",
7918                       GTK_SIGNAL_FUNC (gtk_main_quit), NULL);
7919   gtk_container_border_width (GTK_CONTAINER(window), 5);
7920
7921   /* A generic scrolled window */
7922   scrolled_win = gtk_scrolled_window_new (NULL, NULL);
7923   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
7924                                   GTK_POLICY_AUTOMATIC,
7925                                   GTK_POLICY_AUTOMATIC);
7926   gtk_widget_set_usize (scrolled_win, 150, 200);
7927   gtk_container_add (GTK_CONTAINER(window), scrolled_win);
7928   gtk_widget_show (scrolled_win);
7929   
7930   /* Create the root tree */
7931   tree = gtk_tree_new();
7932   g_print ("root tree is %p\n", tree);
7933   /* connect all GtkTree:: signals */
7934   gtk_signal_connect (GTK_OBJECT(tree), "select_child",
7935                       GTK_SIGNAL_FUNC(cb_select_child), tree);
7936   gtk_signal_connect (GTK_OBJECT(tree), "unselect_child",
7937                       GTK_SIGNAL_FUNC(cb_unselect_child), tree);
7938   gtk_signal_connect (GTK_OBJECT(tree), "selection_changed",
7939                       GTK_SIGNAL_FUNC(cb_selection_changed), tree);
7940   /* Add it to the scrolled window */
7941   gtk_container_add (GTK_CONTAINER(scrolled_win), tree);
7942   /* Set the selection mode */
7943   gtk_tree_set_selection_mode (GTK_TREE(tree),
7944                                GTK_SELECTION_MULTIPLE);
7945   /* Show it */
7946   gtk_widget_show (tree);
7947
7948   for (i = 0; i < 5; i++){
7949     GtkWidget *subtree, *item;
7950     gint j;
7951
7952     /* Create a tree item */
7953     item = gtk_tree_item_new_with_label (itemnames[i]);
7954     /* Connect all GtkItem:: and GtkTreeItem:: signals */
7955     gtk_signal_connect (GTK_OBJECT(item), "select",
7956                         GTK_SIGNAL_FUNC(cb_itemsignal), "select");
7957     gtk_signal_connect (GTK_OBJECT(item), "deselect",
7958                         GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
7959     gtk_signal_connect (GTK_OBJECT(item), "toggle",
7960                         GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
7961     gtk_signal_connect (GTK_OBJECT(item), "expand",
7962                         GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
7963     gtk_signal_connect (GTK_OBJECT(item), "collapse",
7964                         GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
7965     /* Add it to the parent tree */
7966     gtk_tree_append (GTK_TREE(tree), item);
7967     /* Show it - this can be done at any time */
7968     gtk_widget_show (item);
7969     /* Create this item's subtree */
7970     subtree = gtk_tree_new();
7971     g_print ("-> item %s->%p, subtree %p\n", itemnames[i], item,
7972              subtree);
7973
7974     /* This is still necessary if you want these signals to be called
7975        for the subtree's children.  Note that selection_change will be 
7976        signalled for the root tree regardless. */
7977     gtk_signal_connect (GTK_OBJECT(subtree), "select_child",
7978                         GTK_SIGNAL_FUNC(cb_select_child), subtree);
7979     gtk_signal_connect (GTK_OBJECT(subtree), "unselect_child",
7980                         GTK_SIGNAL_FUNC(cb_unselect_child), subtree);
7981     /* This has absolutely no effect, because it is completely ignored 
7982        in subtrees */
7983     gtk_tree_set_selection_mode (GTK_TREE(subtree),
7984                                  GTK_SELECTION_SINGLE);
7985     /* Neither does this, but for a rather different reason - the
7986        view_mode and view_line values of a tree are propagated to
7987        subtrees when they are mapped.  So, setting it later on would
7988        actually have a (somewhat unpredictable) effect */
7989     gtk_tree_set_view_mode (GTK_TREE(subtree), GTK_TREE_VIEW_ITEM);
7990     /* Set this item's subtree - note that you cannot do this until
7991        AFTER the item has been added to its parent tree! */
7992     gtk_tree_item_set_subtree (GTK_TREE_ITEM(item), subtree);
7993
7994     for (j = 0; j < 5; j++){
7995       GtkWidget *subitem;
7996
7997       /* Create a subtree item, in much the same way */
7998       subitem = gtk_tree_item_new_with_label (itemnames[j]);
7999       /* Connect all GtkItem:: and GtkTreeItem:: signals */
8000       gtk_signal_connect (GTK_OBJECT(subitem), "select",
8001                           GTK_SIGNAL_FUNC(cb_itemsignal), "select");
8002       gtk_signal_connect (GTK_OBJECT(subitem), "deselect",
8003                           GTK_SIGNAL_FUNC(cb_itemsignal), "deselect");
8004       gtk_signal_connect (GTK_OBJECT(subitem), "toggle",
8005                           GTK_SIGNAL_FUNC(cb_itemsignal), "toggle");
8006       gtk_signal_connect (GTK_OBJECT(subitem), "expand",
8007                           GTK_SIGNAL_FUNC(cb_itemsignal), "expand");
8008       gtk_signal_connect (GTK_OBJECT(subitem), "collapse",
8009                           GTK_SIGNAL_FUNC(cb_itemsignal), "collapse");
8010       g_print ("-> -> item %s->%p\n", itemnames[j], subitem);
8011       /* Add it to its parent tree */
8012       gtk_tree_append (GTK_TREE(subtree), subitem);
8013       /* Show it */
8014       gtk_widget_show (subitem);
8015     }
8016   }
8017
8018   /* Show the window and loop endlessly */
8019   gtk_widget_show (window);
8020   gtk_main();
8021   return 0;
8022 }
8023 /* example-end */
8024 </verb></tscreen>
8025
8026 <!-- ***************************************************************** -->
8027 <sect>Menu Widget
8028 <!-- ***************************************************************** -->
8029 <p>
8030 There are two ways to create menus, there's the easy way, and there's the
8031 hard way. Both have their uses, but you can usually use the menufactory
8032 (the easy way). The "hard" way is to create all the menus using the calls
8033 directly. The easy way is to use the gtk_menu_factory calls. This is
8034 much simpler, but there are advantages and disadvantages to each approach.
8035
8036 The menufactory is much easier to use, and to add new menus to, although
8037 writing a few wrapper functions to create menus using the manual method 
8038 could go a long way towards usability. With the menufactory, it is not 
8039 possible to add images or the character '/' to the menus.
8040
8041 <!-- ----------------------------------------------------------------- -->
8042 <sect1>Manual Menu Creation
8043 <p>
8044 In the true tradition of teaching, we'll show you the hard
8045 way first. <tt>:)</>
8046
8047 There are three widgets that go into making a menubar and submenus:
8048 <itemize>
8049 <item>a menu item, which is what the user wants to select, e.g. 'Save'
8050 <item>a menu, which acts as a container for the menu items, and
8051 <item>a menubar, which is a container for each of the individual menus,
8052 </itemize>
8053
8054 This is slightly complicated by the fact that menu item widgets are used
8055 for two different things. They are both the widgets that are packed into
8056 the menu, and the widget that is packed into the menubar, which,
8057 when selected, activates the menu.
8058
8059 Let's look at the functions that are used to create menus and menubars.
8060 This first function is used to create a new menubar.
8061
8062 <tscreen><verb>
8063 GtkWidget *gtk_menu_bar_new( void );
8064 </verb></tscreen>
8065
8066 This rather self explanatory function creates a new menubar.  You use
8067 gtk_container_add to pack this into a window, or the box_pack functions to
8068 pack it into a box - the same as buttons.
8069
8070 <tscreen><verb>
8071 GtkWidget *gtk_menu_new( void );
8072 </verb></tscreen>
8073
8074 This function returns a pointer to a new menu, it is never actually shown
8075 (with gtk_widget_show), it is just a container for the menu items.  Hopefully this will
8076 become more clear when you look at the example below.
8077
8078 The next two calls are used to create menu items that are packed into
8079 the menu (and menubar).
8080
8081 <tscreen><verb>
8082 GtkWidget *gtk_menu_item_new( void );
8083 </verb></tscreen>
8084
8085 and
8086
8087 <tscreen><verb>
8088 GtkWidget *gtk_menu_item_new_with_label( const char *label );
8089 </verb></tscreen>
8090
8091 These calls are used to create the menu items that are to be displayed.
8092 Remember to differentiate between a "menu" as created with gtk_menu_new
8093 and a "menu item" as created by the gtk_menu_item_new functions. The
8094 menu item will be an actual button with an associated action,
8095 whereas a menu will be a container holding menu items.
8096
8097 The gtk_menu_new_with_label and gtk_menu_new functions are just as you'd expect after
8098 reading about the buttons. One creates a new menu item with a label
8099 already packed into it, and the other just creates a blank menu item.
8100
8101 Once you've created a menu item you have to put it into a menu. This is 
8102 done using the function gtk_menu_append. In order to capture when the item
8103 is selected by the user, we need to connect to the <tt/activate/ signal in
8104 the usual way. So, if we wanted to create a standard <tt/File/ menu, with
8105 the options <tt/Open/, <tt/Save/ and <tt/Quit/ the code would look something like
8106
8107 <tscreen><verb>
8108 file_menu = gtk_menu_new();    /* Don't need to show menus */
8109
8110 /* Create the menu items */
8111 open_item = gtk_menu_item_new_with_label("Open");
8112 save_item = gtk_menu_item_new_with_label("Save");
8113 quit_item = gtk_menu_item_new_with_label("Quit");
8114
8115 /* Add them to the menu */
8116 gtk_menu_append( GTK_MENU(file_menu), open_item);
8117 gtk_menu_append( GTK_MENU(file_menu), save_item);
8118 gtk_menu_append( GTK_MENU(file_menu), quit_item);
8119
8120 /* Attach the callback functions to the activate signal */
8121 gtk_signal_connect_object( GTK_OBJECT(open_items), "activate",
8122                            GTK_SIGNAL_FUNC(menuitem_response), (gpointer) "file.open");
8123 gtk_signal_connect_object( GTK_OBJECT(save_items), "activate",
8124                            GTK_SIGNAL_FUNC(menuitem_response), (gpointer) "file.save");
8125
8126 /* We can attach the Quit menu item to our exit function */
8127 gtk_signal_connect_object( GTK_OBJECT(quit_items), "activate",
8128                            GTK_SIGNAL_FUNC(destroy), (gpointer) "file.quit");
8129
8130 /* We do need to show menu items */
8131 gtk_widget_show( open_item );
8132 gtk_widget_show( save_item );
8133 gtk_widget_show( quit_item );
8134 </verb></tscreen>
8135
8136 At this point we have our menu. Now we need to create a menubar and a menu
8137 item for the <tt/File/ entry, to which we add our menu. The code looks like this 
8138
8139 <tscreen><verb>
8140 menu_bar = gtk_menu_bar_new();
8141 gtk_container_add( GTK_CONTAINER(window), menu_bar);
8142 gtk_widget_show( menu_bar );
8143
8144 file_item = gtk_menu_item_new_with_label("File");
8145 gtk_widget_show(file_item);
8146 </verb></tscreen>
8147
8148 Now we need to associate the menu with <tt/file_item/. This is done with the
8149 function
8150
8151 <tscreen>
8152 void gtk_menu_item_set_submenu( GtkMenuItem *menu_item,
8153                                 GtkWidget   *submenu );
8154 </tscreen>
8155
8156 So, our example would continue with
8157
8158 <tscreen><verb>
8159 gtk_menu_item_set_submenu( GTK_MENU_ITEM(file_item), file_menu );
8160 </verb></tscreen>
8161
8162 All that is left to do is to add the menu to the menubar, which is accomplished
8163 using the function
8164
8165 <tscreen>
8166 void gtk_menu_bar_append( GtkMenuBar *menu_bar, GtkWidget *menu_item);
8167 </tscreen>
8168
8169 which in our case looks like this:
8170
8171 <tscreen><verb>
8172 gtk_menu_bar_append( GTK_MENU_BAR (menu_bar), file_item );
8173 </verb></tscreen>
8174
8175 If we wanted the menu right justified on the menubar, such as help menus
8176 often are, we can use the following function (again on <tt/file_item/
8177 in the current example) before attaching it to the menubar.
8178
8179 <tscreen><verb>
8180 void gtk_menu_item_right_justify( GtkMenuItem *menu_item );
8181 </verb></tscreen>
8182
8183 Here is a summary of the steps needed to create a menu bar with menus attached:
8184
8185 <itemize>
8186 <item> Create a new menu using gtk_menu_new()
8187 <item> Use multiple calls to gtk_menu_item_new() for each item you wish to have
8188 on your menu. And use gtk_menu_append() to put each of these new items on 
8189 to the menu.
8190 <item> Create a menu item using gtk_menu_item_new(). This will be the root of
8191 the menu, the text appearing here will be on the menubar itself.
8192 <item>Use gtk_menu_item_set_submenu() to attach the menu to the root menu 
8193 item (the one created in the above step).
8194 <item> Create a new menubar using gtk_menu_bar_new. This step only needs
8195 to be done once when creating a series of menus on one menu bar.
8196 <item> Use gtk_menu_bar_append to put the root menu onto the menubar.
8197 </itemize>
8198
8199 Creating a popup menu is nearly the same. The difference is that the
8200 menu is not posted `automatically' by a menubar, but explicitly by calling
8201 the function gtk_menu_popup() from a button-press event, for example.
8202 Take these steps:
8203
8204 <itemize>
8205 <item>Create an event handling function. It needs to have the prototype
8206 <tscreen>
8207 static gint handler( GtkWidget *widget,
8208                      GdkEvent  *event );
8209 </tscreen>
8210 and it will use the event to find out where to pop up the menu.
8211 <item>In the event handler, if the event is a mouse button press, treat
8212 <tt>event</tt> as a button event (which it is) and use it as
8213 shown in the sample code to pass information to gtk_menu_popup().
8214 <item>Bind that event handler to a widget with
8215 <tscreen>
8216 gtk_signal_connect_object(GTK_OBJECT(widget), "event",
8217                           GTK_SIGNAL_FUNC (handler), GTK_OBJECT(menu));
8218 </tscreen>
8219 where <tt>widget</tt> is the widget you are binding to, <tt>handler</tt>
8220 is the handling function, and <tt>menu</tt> is a menu created with
8221 gtk_menu_new().  This can be a menu which is also posted by a menu bar,
8222 as shown in the sample code.
8223 </itemize>
8224
8225 <!-- ----------------------------------------------------------------- -->
8226 <sect1>Manual Menu Example
8227 <p>
8228 That should about do it.  Let's take a look at an example to help clarify.
8229
8230 <tscreen><verb>
8231 /* example-start menu menu.c */
8232
8233 #include <gtk/gtk.h>
8234
8235 static gint button_press (GtkWidget *, GdkEvent *);
8236 static void menuitem_response (gchar *);
8237
8238 int main (int argc, char *argv[])
8239 {
8240
8241     GtkWidget *window;
8242     GtkWidget *menu;
8243     GtkWidget *menu_bar;
8244     GtkWidget *root_menu;
8245     GtkWidget *menu_items;
8246     GtkWidget *vbox;
8247     GtkWidget *button;
8248     char buf[128];
8249     int i;
8250
8251     gtk_init (&amp;argc, &amp;argv);
8252
8253     /* create a new window */
8254     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
8255     gtk_widget_set_usize( GTK_WIDGET (window), 200, 100);
8256     gtk_window_set_title(GTK_WINDOW (window), "GTK Menu Test");
8257     gtk_signal_connect(GTK_OBJECT (window), "delete_event",
8258                        (GtkSignalFunc) gtk_main_quit, NULL);
8259
8260     /* Init the menu-widget, and remember -- never
8261      * gtk_show_widget() the menu widget!! 
8262      * This is the menu that holds the menu items, the one that
8263      * will pop up when you click on the "Root Menu" in the app */
8264     menu = gtk_menu_new();
8265
8266     /* Next we make a little loop that makes three menu-entries for "test-menu".
8267      * Notice the call to gtk_menu_append.  Here we are adding a list of
8268      * menu items to our menu.  Normally, we'd also catch the "clicked"
8269      * signal on each of the menu items and setup a callback for it,
8270      * but it's omitted here to save space. */
8271
8272     for(i = 0; i < 3; i++)
8273         {
8274             /* Copy the names to the buf. */
8275             sprintf(buf, "Test-undermenu - %d", i);
8276
8277             /* Create a new menu-item with a name... */
8278             menu_items = gtk_menu_item_new_with_label(buf);
8279
8280             /* ...and add it to the menu. */
8281             gtk_menu_append(GTK_MENU (menu), menu_items);
8282
8283             /* Do something interesting when the menuitem is selected */
8284             gtk_signal_connect_object(GTK_OBJECT(menu_items), "activate",
8285                 GTK_SIGNAL_FUNC(menuitem_response), (gpointer) g_strdup(buf));
8286
8287             /* Show the widget */
8288             gtk_widget_show(menu_items);
8289         }
8290
8291     /* This is the root menu, and will be the label
8292      * displayed on the menu bar.  There won't be a signal handler attached,
8293      * as it only pops up the rest of the menu when pressed. */
8294     root_menu = gtk_menu_item_new_with_label("Root Menu");
8295
8296     gtk_widget_show(root_menu);
8297
8298     /* Now we specify that we want our newly created "menu" to be the menu
8299      * for the "root menu" */
8300     gtk_menu_item_set_submenu(GTK_MENU_ITEM (root_menu), menu);
8301
8302     /* A vbox to put a menu and a button in: */
8303     vbox = gtk_vbox_new(FALSE, 0);
8304     gtk_container_add(GTK_CONTAINER(window), vbox);
8305     gtk_widget_show(vbox);
8306
8307     /* Create a menu-bar to hold the menus and add it to our main window */
8308     menu_bar = gtk_menu_bar_new();
8309     gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 2);
8310     gtk_widget_show(menu_bar);
8311
8312     /* Create a button to which to attach menu as a popup */
8313     button = gtk_button_new_with_label("press me");
8314     gtk_signal_connect_object(GTK_OBJECT(button), "event",
8315         GTK_SIGNAL_FUNC (button_press), GTK_OBJECT(menu));
8316     gtk_box_pack_end(GTK_BOX(vbox), button, TRUE, TRUE, 2);
8317     gtk_widget_show(button);
8318
8319     /* And finally we append the menu-item to the menu-bar -- this is the
8320      * "root" menu-item I have been raving about =) */
8321     gtk_menu_bar_append(GTK_MENU_BAR (menu_bar), root_menu);
8322
8323     /* always display the window as the last step so it all splashes on
8324      * the screen at once. */
8325     gtk_widget_show(window);
8326
8327     gtk_main ();
8328
8329     return 0;
8330 }
8331
8332 /* Respond to a button-press by posting a menu passed in as widget.
8333  *
8334  * Note that the "widget" argument is the menu being posted, NOT
8335  * the button that was pressed.
8336  */
8337
8338 static gint button_press (GtkWidget *widget, GdkEvent *event)
8339 {
8340
8341     if (event->type == GDK_BUTTON_PRESS) {
8342         GdkEventButton *bevent = (GdkEventButton *) event; 
8343         gtk_menu_popup (GTK_MENU(widget), NULL, NULL, NULL, NULL,
8344                         bevent->button, bevent->time);
8345         /* Tell calling code that we have handled this event; the buck
8346          * stops here. */
8347         return TRUE;
8348     }
8349
8350     /* Tell calling code that we have not handled this event; pass it on. */
8351     return FALSE;
8352 }
8353
8354
8355 /* Print a string when a menu item is selected */
8356
8357 static void menuitem_response (gchar *string)
8358 {
8359     printf("%s\n", string);
8360 }
8361 /* example-end */
8362 </verb></tscreen>
8363
8364 You may also set a menu item to be insensitive and, using an accelerator
8365 table, bind keys to menu functions.
8366
8367 <!-- ----------------------------------------------------------------- -->
8368 <sect1>Using GtkMenuFactory
8369 <p>
8370 Now that we've shown you the hard way, here's how you do it using the
8371 gtk_menu_factory calls.
8372
8373 <!-- ----------------------------------------------------------------- -->
8374 <sect1>Menu Factory Example
8375 <p>
8376 Here is an example using the GTK menu factory.  This is the first file,
8377 menufactory.h.  We keep a separate menufactory.c and mfmain.c because
8378 of the global variables used in the menufactory.c file.  
8379
8380 <tscreen><verb>
8381 /* example-start menu menufactory.h */
8382
8383 #ifndef __MENUFACTORY_H__
8384 #define __MENUFACTORY_H__
8385
8386 #ifdef __cplusplus
8387 extern "C" {
8388 #endif /* __cplusplus */
8389
8390 void get_main_menu (GtkWidget *, GtkWidget **menubar);
8391
8392 #ifdef __cplusplus
8393 }
8394 #endif /* __cplusplus */
8395
8396 #endif /* __MENUFACTORY_H__ */
8397
8398 /* example-end */
8399 </verb></tscreen>
8400
8401 And here is the menufactory.c file.
8402
8403 <tscreen><verb>
8404 /* example-start menu menufactory.c */
8405
8406 #include <gtk/gtk.h>
8407 #include <strings.h>
8408
8409 #include "mfmain.h"
8410
8411 static void print_hello(GtkWidget *widget, gpointer data);
8412
8413
8414 /* this is the GtkMenuEntry structure used to create new menus.  The
8415  * first member is the menu definition string.  The second, the
8416  * default accelerator key used to access this menu function with
8417  * the keyboard.  The third is the callback function to call when
8418  * this menu item is selected (by the accelerator key, or with the
8419  * mouse.) The last member is the data to pass to your callback function.
8420  */
8421
8422 static GtkMenuEntry menu_items[] =
8423 {
8424     {"<Main>/File/New", "<control>N", print_hello, NULL},
8425     {"<Main>/File/Open", "<control>O", print_hello, NULL},
8426     {"<Main>/File/Save", "<control>S", print_hello, NULL},
8427     {"<Main>/File/Save as", NULL, NULL, NULL},
8428     {"<Main>/File/<separator>", NULL, NULL, NULL},
8429     {"<Main>/File/Quit", "<control>Q", file_quit_cmd_callback, "OK, I'll quit"},
8430     {"<Main>/Options/Test", NULL, NULL, NULL}
8431 };
8432
8433
8434 static void
8435 print_hello(GtkWidget *widget, gpointer data)
8436 {
8437     printf("hello!\n");
8438 }
8439
8440 void get_main_menu(GtkWidget *window, GtkWidget ** menubar)
8441 {
8442     int nmenu_items = sizeof(menu_items) / sizeof(menu_items[0]);
8443     GtkMenuFactory *factory;
8444     GtkMenuFactory *subfactory;
8445
8446     factory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
8447     subfactory = gtk_menu_factory_new(GTK_MENU_FACTORY_MENU_BAR);
8448
8449     gtk_menu_factory_add_subfactory(factory, subfactory, "<Main>");
8450     gtk_menu_factory_add_entries(factory, menu_items, nmenu_items);
8451     gtk_window_add_accelerator_table(GTK_WINDOW(window), subfactory->table);
8452     
8453     if (menubar)
8454         *menubar = subfactory->widget;
8455 }
8456
8457 /* example-end */
8458 </verb></tscreen>
8459
8460 And here's the mfmain.h
8461
8462 <tscreen><verb>
8463 /* example-start menu mfmain.h */
8464
8465 #ifndef __MFMAIN_H__
8466 #define __MFMAIN_H__
8467
8468
8469 #ifdef __cplusplus
8470 extern "C" {
8471 #endif /* __cplusplus */
8472
8473 void file_quit_cmd_callback(GtkWidget *widget, gpointer data);
8474
8475 #ifdef __cplusplus
8476 }
8477 #endif /* __cplusplus */
8478
8479 #endif /* __MFMAIN_H__ */
8480
8481 /* example-end */
8482 </verb></tscreen>
8483
8484 And mfmain.c
8485
8486 <tscreen><verb>
8487 /* example-start menu mfmain.c */
8488
8489 #include <gtk/gtk.h>
8490
8491 #include "mfmain.h"
8492 #include "menufactory.h"
8493
8494 int main(int argc, char *argv[])
8495 {
8496     GtkWidget *window;
8497     GtkWidget *main_vbox;
8498     GtkWidget *menubar;
8499     
8500     gtk_init(&amp;argc, &amp;argv);
8501     
8502     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
8503     gtk_signal_connect(GTK_OBJECT(window), "destroy", 
8504                        GTK_SIGNAL_FUNC(file_quit_cmd_callback), 
8505                        "WM destroy");
8506     gtk_window_set_title(GTK_WINDOW(window), "Menu Factory");
8507     gtk_widget_set_usize(GTK_WIDGET(window), 300, 200);
8508     
8509     main_vbox = gtk_vbox_new(FALSE, 1);
8510     gtk_container_border_width(GTK_CONTAINER(main_vbox), 1);
8511     gtk_container_add(GTK_CONTAINER(window), main_vbox);
8512     gtk_widget_show(main_vbox);
8513     
8514     get_main_menu(window, &amp;menubar);
8515     gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, TRUE, 0);
8516     gtk_widget_show(menubar);
8517     
8518     gtk_widget_show(window);
8519     gtk_main();
8520     
8521     return(0);
8522 }
8523
8524 /* This is just to demonstrate how callbacks work when using the
8525  * menufactory.  Often, people put all the callbacks from the menus
8526  * in a separate file, and then have them call the appropriate functions
8527  * from there.  Keeps it more organized. */
8528 void file_quit_cmd_callback (GtkWidget *widget, gpointer data)
8529 {
8530     g_print ("%s\n", (char *) data);
8531     gtk_exit(0);
8532 }
8533
8534 /* example-end */
8535 </verb></tscreen>
8536
8537 And a makefile so it'll be easier to compile it.
8538
8539 <tscreen><verb>
8540 # Makefile.mf
8541
8542 CC      = gcc
8543 PROF    = -g
8544 C_FLAGS =  -Wall $(PROF) -L/usr/local/include -DDEBUG
8545 L_FLAGS =  $(PROF) -L/usr/X11R6/lib -L/usr/local/lib 
8546 L_POSTFLAGS = -lgtk -lgdk -lglib -lXext -lX11 -lm
8547 PROGNAME = menufactory
8548
8549 O_FILES = menufactory.o mfmain.o
8550
8551 $(PROGNAME): $(O_FILES)
8552         rm -f $(PROGNAME)
8553         $(CC) $(L_FLAGS) -o $(PROGNAME) $(O_FILES) $(L_POSTFLAGS)
8554
8555 .c.o: 
8556         $(CC) -c $(C_FLAGS) $<
8557
8558 clean: 
8559         rm -f core *.o $(PROGNAME) nohup.out
8560 distclean: clean 
8561         rm -f *~
8562 </verb></tscreen>
8563
8564 For now, there's only this example.  An explanation and lots 'o' comments
8565 will follow later.
8566
8567 <!-- ***************************************************************** -->
8568 <sect> Text Widget
8569 <!-- ***************************************************************** -->
8570 <p>
8571 The Text widget allows multiple lines of text to be displayed and edited.
8572 It supports both multi-colored and multi-font text, allowing them to be
8573 mixed in any way we wish. It also has a wide set of key based text editing
8574 commands, which are compatible with Emacs.
8575
8576 The text widget supports full cut-and-paste facilities, including the use
8577 of double- and triple-click to select a word and a whole line, respectively.
8578
8579 <!-- ----------------------------------------------------------------- -->
8580 <sect1>Creating and Configuring a Text box
8581 <p>
8582 There is only one function for creating a new Text widget.
8583 <tscreen><verb>
8584 GtkWidget *gtk_text_new( GtkAdjustment *hadj,
8585                          GtkAdjustment *vadj );
8586 </verb></tscreen>
8587
8588 The arguments allow us to give the Text widget pointers to Adjustments
8589 that can be used to track the viewing position of the widget. Passing NULL
8590 values to either or both of these arguments will cause the gtk_text_new
8591 function to create its own.
8592
8593 <tscreen><verb>
8594 void gtk_text_set_adjustments( GtkText       *text,
8595                                GtkAdjustment *hadj,
8596                                GtkAdjustment *vadj );
8597 </verb></tscreen>
8598
8599 The above function allows the horizontal and vertical adjustments of a
8600 Text widget to be changed at any time.
8601
8602 The text widget will not automatically create its own scrollbars when
8603 the amount of text to be displayed is too long for the display window. We
8604 therefore have to create and add them to the display layout ourselves.
8605
8606 <tscreen><verb>
8607   vscrollbar = gtk_vscrollbar_new (GTK_TEXT(text)->vadj);
8608   gtk_box_pack_start(GTK_BOX(hbox), vscrollbar, FALSE, FALSE, 0);
8609   gtk_widget_show (vscrollbar);
8610 </verb></tscreen>
8611
8612 The above code snippet creates a new vertical scrollbar, and attaches
8613 it to the vertical adjustment of the text widget, <tt/text/. It then packs
8614 it into a box in the normal way.
8615
8616 Note, currently the GtkText widget does not support horizontal scrollbars.
8617
8618 There are two main ways in which a Text widget can be used: to allow the
8619 user to edit a body of text, or to allow us to display multiple lines of
8620 text to the user. In order for us to switch between these modes of
8621 operation, the text widget has the following function:
8622
8623 <tscreen><verb>
8624 void gtk_text_set_editable( GtkText *text,
8625                             gint     editable );
8626 </verb></tscreen>
8627
8628 The <tt/editable/ argument is a TRUE or FALSE value that specifies whether
8629 the user is permitted to edit the contents of the Text widget. When the
8630 text widget is editable, it will display a cursor at the current insertion
8631 point.
8632
8633 You are not, however, restricted to just using the text widget in these
8634 two modes. You can toggle the editable state of the text widget at any
8635 time, and can insert text at any time.
8636
8637 The text widget wraps lines of text that are too long to
8638 fit onto a single line of the display window. Its default behaviour is
8639 to break words across line breaks. This can be changed using the next
8640 function:
8641
8642 <tscreen><verb>
8643 void gtk_text_set_word_wrap( GtkText *text,
8644                              gint     word_wrap );
8645 </verb></tscreen>
8646
8647 Using this function allows us to specify that the text widget should
8648 wrap long lines on word boundaries. The <tt/word_wrap/ argument is a
8649 TRUE or FALSE value.
8650
8651 <!-- ----------------------------------------------------------------- -->
8652 <sect1>Text Manipulation
8653 <P>
8654 The current insertion point of a Text widget can be set using
8655 <tscreen><verb>
8656 void gtk_text_set_point( GtkText *text,
8657                          guint    index );
8658 </verb></tscreen>
8659
8660 where <tt/index/ is the position to set the insertion point.
8661
8662 Analogous to this is the function for getting the current insertion point:
8663
8664 <tscreen><verb>
8665 guint gtk_text_get_point( GtkText *text );
8666 </verb></tscreen>
8667
8668 A function that is useful in combination with the above two functions is
8669
8670 <tscreen><verb>
8671 guint gtk_text_get_length( GtkText *text );
8672 </verb></tscreen>
8673
8674 which returns the current length of the Text widget. The length is the
8675 number of characters that are within the text block of the widget,
8676 including characters such as carriage-return, which marks the end of lines.
8677
8678 In order to insert text at the current insertion point of a Text
8679 widget, the function gtk_text_insert is used, which also allows us to
8680 specify background and foreground colors and a font for the text.
8681
8682 <tscreen><verb>
8683 void gtk_text_insert( GtkText    *text,
8684                       GdkFont    *font,
8685                       GdkColor   *fore,
8686                       GdkColor   *back,
8687                       const char *chars,
8688                       gint        length );
8689 </verb></tscreen>
8690
8691 Passing a value of <tt/NULL/ in as the value for the foreground color,
8692 background colour or font will result in the values set within the widget
8693 style to be used. Using a value of <tt/-1/ for the length parameter will
8694 result in the whole of the text string given being inserted.
8695
8696 The text widget is one of the few within GTK that redraws itself
8697 dynamically, outside of the gtk_main function. This means that all changes
8698 to the contents of the text widget take effect immediately. This may be
8699 undesirable when performing multiple changes to the text widget. In order
8700 to allow us to perform multiple updates to the text widget without it
8701 continuously redrawing, we can freeze the widget, which temporarily stops
8702 it from automatically redrawing itself every time it is changed. We can
8703 then thaw the widget after our updates are complete.
8704
8705 The following two functions perform this freeze and thaw action:
8706
8707 <tscreen><verb>
8708 void gtk_text_freeze( GtkText *text );
8709
8710 void gtk_text_thaw( GtkText *text );         
8711 </verb></tscreen>
8712
8713 Text is deleted from the text widget relative to the current insertion
8714 point by the following two functions. The return value is a TRUE or
8715 FALSE indicator of whether the operation was successful.
8716
8717 <tscreen><verb>
8718 gint gtk_text_backward_delete( GtkText *text,
8719                                guint    nchars );
8720
8721 gint gtk_text_forward_delete ( GtkText *text,
8722                                guint    nchars );
8723 </verb></tscreen>
8724
8725 If you want to retrieve the contents of the text widget, then the macro 
8726 <tt/GTK_TEXT_INDEX(t, index)/ allows you to retrieve the character at
8727 position <tt/index/ within the text widget <tt/t/.
8728
8729 To retrieve larger blocks of text, we can use the function
8730
8731 <tscreen><verb>
8732 gchar *gtk_editable_get_chars( GtkEditable *editable,
8733                                gint         start_pos,
8734                                gint         end_pos );   
8735 </verb></tscreen>
8736
8737 This is a function of the parent class of the text widget. A value of -1 as
8738 <tt/end_pos/ signifies the end of the text. The index of the text starts at 0.
8739
8740 The function allocates a new chunk of memory for the text block, so don't forget
8741 to free it with a call to g_free when you have finished with it.
8742  
8743 <!-- ----------------------------------------------------------------- -->
8744 <sect1>Keyboard Shortcuts
8745 <p>
8746 The text widget has a number of pre-installed keyboard shortcuts for common
8747 editing, motion and selection functions. These are accessed using Control
8748 and Alt key combinations.
8749
8750 In addition to these, holding down the Control key whilst using cursor key
8751 movement will move the cursor by words rather than characters. Holding down
8752 Shift whilst using cursor movement will extend the selection.
8753
8754 <sect2>Motion Shortcuts
8755 <p>
8756 <itemize>
8757 <item> Ctrl-A   Beginning of line
8758 <item> Ctrl-E   End of line
8759 <item> Ctrl-N   Next Line
8760 <item> Ctrl-P   Previous Line
8761 <item> Ctrl-B   Backward one character
8762 <item> Ctrl-F   Forward one character
8763 <item> Alt-B    Backward one word
8764 <item> Alt-F    Forward one word
8765 </itemize>
8766
8767 <sect2>Editing Shortcuts
8768 <p>
8769 <itemize>
8770 <item> Ctrl-H   Delete Backward Character (Backspace)
8771 <item> Ctrl-D   Delete Forward Character (Delete)
8772 <item> Ctrl-W   Delete Backward Word
8773 <item> Alt-D    Delete Forward Word
8774 <item> Ctrl-K   Delete to end of line
8775 <item> Ctrl-U   Delete line
8776 </itemize>
8777
8778 <sect2>Selection Shortcuts
8779 <p>
8780 <itemize>
8781 <item> Ctrl-X   Cut to clipboard
8782 <item> Ctrl-C   Copy to clipboard
8783 <item> Ctrl-V   Paste from clipboard
8784 </itemize>
8785
8786 <!-- ----------------------------------------------------------------- -->
8787 <sect1>A GtkText Example
8788 <p>
8789 <tscreen><verb>
8790 /* example-start text text.c */
8791
8792 /* text.c */
8793
8794 #include <stdio.h>
8795 #include <gtk/gtk.h>
8796
8797 void text_toggle_editable (GtkWidget *checkbutton,
8798                            GtkWidget *text)
8799 {
8800   gtk_text_set_editable(GTK_TEXT(text),
8801                         GTK_TOGGLE_BUTTON(checkbutton)->active);
8802 }
8803
8804 void text_toggle_word_wrap (GtkWidget *checkbutton,
8805                             GtkWidget *text)
8806 {
8807   gtk_text_set_word_wrap(GTK_TEXT(text),
8808                          GTK_TOGGLE_BUTTON(checkbutton)->active);
8809 }
8810
8811 void close_application( GtkWidget *widget, gpointer data )
8812 {
8813        gtk_main_quit();
8814 }
8815
8816 int main (int argc, char *argv[])
8817 {
8818   GtkWidget *window;
8819   GtkWidget *box1;
8820   GtkWidget *box2;
8821   GtkWidget *hbox;
8822   GtkWidget *button;
8823   GtkWidget *check;
8824   GtkWidget *separator;
8825   GtkWidget *table;
8826   GtkWidget *vscrollbar;
8827   GtkWidget *text;
8828   GdkColormap *cmap;
8829   GdkColor colour;
8830   GdkFont *fixed_font;
8831
8832   FILE *infile;
8833
8834   gtk_init (&amp;argc, &amp;argv);
8835  
8836   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
8837   gtk_widget_set_usize (window, 600, 500);
8838   gtk_window_set_policy (GTK_WINDOW(window), TRUE, TRUE, FALSE);  
8839   gtk_signal_connect (GTK_OBJECT (window), "destroy",
8840                       GTK_SIGNAL_FUNC(close_application),
8841                       NULL);
8842   gtk_window_set_title (GTK_WINDOW (window), "Text Widget Example");
8843   gtk_container_border_width (GTK_CONTAINER (window), 0);
8844   
8845   
8846   box1 = gtk_vbox_new (FALSE, 0);
8847   gtk_container_add (GTK_CONTAINER (window), box1);
8848   gtk_widget_show (box1);
8849   
8850   
8851   box2 = gtk_vbox_new (FALSE, 10);
8852   gtk_container_border_width (GTK_CONTAINER (box2), 10);
8853   gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
8854   gtk_widget_show (box2);
8855   
8856   
8857   table = gtk_table_new (2, 2, FALSE);
8858   gtk_table_set_row_spacing (GTK_TABLE (table), 0, 2);
8859   gtk_table_set_col_spacing (GTK_TABLE (table), 0, 2);
8860   gtk_box_pack_start (GTK_BOX (box2), table, TRUE, TRUE, 0);
8861   gtk_widget_show (table);
8862   
8863   /* Create the GtkText widget */
8864   text = gtk_text_new (NULL, NULL);
8865   gtk_text_set_editable (GTK_TEXT (text), TRUE);
8866   gtk_table_attach (GTK_TABLE (table), text, 0, 1, 0, 1,
8867                     GTK_EXPAND | GTK_SHRINK | GTK_FILL,
8868                     GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
8869   gtk_widget_show (text);
8870
8871   /* Add a vertical scrollbar to the GtkText widget */
8872   vscrollbar = gtk_vscrollbar_new (GTK_TEXT (text)->vadj);
8873   gtk_table_attach (GTK_TABLE (table), vscrollbar, 1, 2, 0, 1,
8874                     GTK_FILL, GTK_EXPAND | GTK_SHRINK | GTK_FILL, 0, 0);
8875   gtk_widget_show (vscrollbar);
8876
8877   /* Get the system colour map and allocate the colour red */
8878   cmap = gdk_colormap_get_system();
8879   colour.red = 0xffff;
8880   colour.green = 0;
8881   colour.blue = 0;
8882   if (!gdk_color_alloc(cmap, &amp;colour)) {
8883     g_error("couldn't allocate colour");
8884   }
8885
8886   /* Load a fixed font */
8887   fixed_font = gdk_font_load ("-misc-fixed-medium-r-*-*-*-140-*-*-*-*-*-*");
8888
8889   /* Realizing a widget creates a window for it, ready for us to insert some text */
8890   gtk_widget_realize (text);
8891
8892   /* Freeze the text widget, ready for multiple updates */
8893   gtk_text_freeze (GTK_TEXT (text));
8894   
8895   /* Insert some coloured text */
8896   gtk_text_insert (GTK_TEXT (text), NULL, &amp;text->style->black, NULL,
8897                    "Supports ", -1);
8898   gtk_text_insert (GTK_TEXT (text), NULL, &amp;colour, NULL,
8899                    "colored ", -1);
8900   gtk_text_insert (GTK_TEXT (text), NULL, &amp;text->style->black, NULL,
8901                    "text and different ", -1);
8902   gtk_text_insert (GTK_TEXT (text), fixed_font, &amp;text->style->black, NULL,
8903                    "fonts\n\n", -1);
8904   
8905   /* Load the file text.c into the text window */
8906
8907   infile = fopen("text.c", "r");
8908   
8909   if (infile) {
8910     char buffer[1024];
8911     int nchars;
8912     
8913     while (1)
8914       {
8915         nchars = fread(buffer, 1, 1024, infile);
8916         gtk_text_insert (GTK_TEXT (text), fixed_font, NULL,
8917                          NULL, buffer, nchars);
8918         
8919         if (nchars < 1024)
8920           break;
8921       }
8922     
8923     fclose (infile);
8924   }
8925
8926   /* Thaw the text widget, allowing the updates to become visible */  
8927   gtk_text_thaw (GTK_TEXT (text));
8928   
8929   hbox = gtk_hbutton_box_new ();
8930   gtk_box_pack_start (GTK_BOX (box2), hbox, FALSE, FALSE, 0);
8931   gtk_widget_show (hbox);
8932
8933   check = gtk_check_button_new_with_label("Editable");
8934   gtk_box_pack_start (GTK_BOX (hbox), check, FALSE, FALSE, 0);
8935   gtk_signal_connect (GTK_OBJECT(check), "toggled",
8936                       GTK_SIGNAL_FUNC(text_toggle_editable), text);
8937   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(check), TRUE);
8938   gtk_widget_show (check);
8939   check = gtk_check_button_new_with_label("Wrap Words");
8940   gtk_box_pack_start (GTK_BOX (hbox), check, FALSE, TRUE, 0);
8941   gtk_signal_connect (GTK_OBJECT(check), "toggled",
8942                       GTK_SIGNAL_FUNC(text_toggle_word_wrap), text);
8943   gtk_toggle_button_set_state(GTK_TOGGLE_BUTTON(check), FALSE);
8944   gtk_widget_show (check);
8945
8946   separator = gtk_hseparator_new ();
8947   gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
8948   gtk_widget_show (separator);
8949
8950   box2 = gtk_vbox_new (FALSE, 10);
8951   gtk_container_border_width (GTK_CONTAINER (box2), 10);
8952   gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
8953   gtk_widget_show (box2);
8954   
8955   button = gtk_button_new_with_label ("close");
8956   gtk_signal_connect (GTK_OBJECT (button), "clicked",
8957                       GTK_SIGNAL_FUNC(close_application),
8958                       NULL);
8959   gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
8960   GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
8961   gtk_widget_grab_default (button);
8962   gtk_widget_show (button);
8963
8964   gtk_widget_show (window);
8965
8966   gtk_main ();
8967   
8968   return 0;       
8969 }
8970 /* example-end */
8971 </verb></tscreen>
8972
8973
8974 <!-- ***************************************************************** -->
8975 <sect> Undocumented Widgets
8976 <!-- ***************************************************************** -->
8977 <p>
8978 These all require authors! :)  Please consider contributing to our tutorial.
8979
8980 If you must use one of these widgets that are undocumented, I strongly
8981 suggest you take a look at their respective header files in the GTK
8982 distribution. GTK's function names are very descriptive.  Once you have an
8983 understanding of how things work, it's not difficult to figure out how to
8984 use a widget simply by looking at its function declarations.  This, along
8985 with a few examples from others' code, and it should be no problem.
8986
8987 When you do come to understand all the functions of a new undocumented
8988 widget, please consider writing a tutorial on it so others may benefit
8989 from your time.
8990
8991 <!-- ----------------------------------------------------------------- -->
8992 <sect1> Fixed Container
8993 <p>
8994 <!-- ----------------------------------------------------------------- -->
8995 <sect1> Curves
8996 <p>
8997 <!-- ----------------------------------------------------------------- -->
8998 <sect1> Previews
8999 <p>
9000
9001 (This may need to be rewritten to follow the style of the rest of the tutorial)
9002
9003 <tscreen><verb>
9004
9005 Previews serve a number of purposes in GIMP/GTK. The most important one is
9006 this. High quality images may take up to tens of megabytes of memory - easy!
9007 Any operation on an image that big is bound to take a long time. If it takes
9008 you 5-10 trial-and-errors (i.e. 10-20 steps, since you have to revert after
9009 you make an error) to choose the desired modification, it make take you
9010 literally hours to make the right one - if you don't run out of memory
9011 first. People who have spent hours in color darkrooms know the feeling.
9012 Previews to the rescue!
9013
9014 But the annoyance of the delay is not the only issue. Oftentimes it is
9015 helpful to compare the Before and After versions side-by-side or at least
9016 back-to-back. If you're working with big images and 10 second delays,
9017 obtaining the Before and After impressions is, to say the least, difficult.
9018 For 30M images (4"x6", 600dpi, 24 bit) the side-by-side comparison is right
9019 out for most people, while back-to-back is more like back-to-1001, 1002,
9020 ..., 1010-back! Previews to the rescue!
9021
9022 But there's more. Previews allow for side-by-side pre-previews. In other
9023 words, you write a plug-in (e.g. the filterpack simulation) which would have
9024 a number of here's-what-it-would-look-like-if-you-were-to-do-this previews.
9025 An approach like this acts as a sort of a preview palette and is very
9026 effective for subtle changes. Let's go previews!
9027
9028 There's more. For certain plug-ins real-time image-specific human
9029 intervention maybe necessary. In the SuperNova plug-in, for example, the
9030 user is asked to enter the coordinates of the center of the future
9031 supernova. The easiest way to do this, really, is to present the user with a
9032 preview and ask him to interactively select the spot. Let's go previews!
9033
9034 Finally, a couple of misc uses. One can use previews even when not working
9035 with big images. For example, they are useful when rendering complicated
9036 patterns. (Just check out the venerable Diffraction plug-in + many other
9037 ones!) As another example, take a look at the colormap rotation plug-in
9038 (work in progress). You can also use previews for little logos inside you
9039 plug-ins and even for an image of yourself, The Author. Let's go previews!
9040
9041 When Not to Use Previews
9042
9043 Don't use previews for graphs, drawing etc. GDK is much faster for that. Use
9044 previews only for rendered images!
9045
9046 Let's go previews!
9047
9048 You can stick a preview into just about anything. In a vbox, an hbox, a
9049 table, a button, etc. But they look their best in tight frames around them.
9050 Previews by themselves do not have borders and look flat without them. (Of
9051 course, if the flat look is what you want...) Tight frames provide the
9052 necessary borders.
9053
9054                                [Image][Image]
9055
9056 Previews in many ways are like any other widgets in GTK (whatever that
9057 means) except they possess an additional feature: they need to be filled with
9058 some sort of an image! First, we will deal exclusively with the GTK aspect
9059 of previews and then we'll discuss how to fill them.
9060
9061 GtkWidget *preview!
9062
9063 Without any ado:
9064
9065                               /* Create a preview widget,
9066                               set its size, an show it */
9067 GtkWidget *preview;
9068 preview=gtk_preview_new(GTK_PREVIEW_COLOR)
9069                               /*Other option:
9070                               GTK_PREVIEW_GRAYSCALE);*/
9071 gtk_preview_size (GTK_PREVIEW (preview), WIDTH, HEIGHT);
9072 gtk_widget_show(preview);
9073 my_preview_rendering_function(preview);
9074
9075 Oh yeah, like I said, previews look good inside frames, so how about:
9076
9077 GtkWidget *create_a_preview(int        Width,
9078                             int        Height,
9079                             int        Colorfulness)
9080 {
9081   GtkWidget *preview;
9082   GtkWidget *frame;
9083   
9084   frame = gtk_frame_new(NULL);
9085   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN);
9086   gtk_container_border_width (GTK_CONTAINER(frame),0);
9087   gtk_widget_show(frame);
9088
9089   preview=gtk_preview_new (Colorfulness?GTK_PREVIEW_COLOR
9090                                        :GTK_PREVIEW_GRAYSCALE);
9091   gtk_preview_size (GTK_PREVIEW (preview), Width, Height);
9092   gtk_container_add(GTK_CONTAINER(frame),preview);
9093   gtk_widget_show(preview);
9094
9095   my_preview_rendering_function(preview);
9096   return frame;
9097 }
9098
9099 That's my basic preview. This routine returns the "parent" frame so you can
9100 place it somewhere else in your interface. Of course, you can pass the
9101 parent frame to this routine as a parameter. In many situations, however,
9102 the contents of the preview are changed continually by your application. In
9103 this case you may want to pass a pointer to the preview to a
9104 "create_a_preview()" and thus have control of it later.
9105
9106 One more important note that may one day save you a lot of time. Sometimes
9107 it is desirable to label you preview. For example, you may label the preview
9108 containing the original image as "Original" and the one containing the
9109 modified image as "Less Original". It might occur to you to pack the
9110 preview along with the appropriate label into a vbox. The unexpected caveat
9111 is that if the label is wider than the preview (which may happen for a
9112 variety of reasons unforseeable to you, from the dynamic decision on the
9113 size of the preview to the size of the font) the frame expands and no longer
9114 fits tightly over the preview. The same problem can probably arise in other
9115 situations as well.
9116
9117                                    [Image]
9118
9119 The solution is to place the preview and the label into a 2x1 table and by
9120 attaching them with the following parameters (this is one possible variations
9121 of course. The key is no GTK_FILL in the second attachment):
9122
9123 gtk_table_attach(GTK_TABLE(table),label,0,1,0,1,
9124                  0,
9125                  GTK_EXPAND|GTK_FILL,
9126                  0,0);
9127 gtk_table_attach(GTK_TABLE(table),frame,0,1,1,2,
9128                  GTK_EXPAND,
9129                  GTK_EXPAND,
9130                  0,0);
9131
9132
9133 And here's the result:
9134
9135                                    [Image]
9136
9137 Misc
9138
9139 Making a preview clickable is achieved most easily by placing it in a
9140 button. It also adds a nice border around the preview and you may not even
9141 need to place it in a frame. See the Filter Pack Simulation plug-in for an
9142 example.
9143
9144 This is pretty much it as far as GTK is concerned.
9145
9146 Filling In a Preview
9147
9148 In order to familiarize ourselves with the basics of filling in previews,
9149 let's create the following pattern (contrived by trial and error):
9150
9151                                    [Image]
9152
9153 void
9154 my_preview_rendering_function(GtkWidget     *preview)
9155 {
9156 #define SIZE 100
9157 #define HALF (SIZE/2)
9158
9159   guchar *row=(guchar *) malloc(3*SIZE); /* 3 bits per dot */
9160   gint i, j;                             /* Coordinates    */
9161   double r, alpha, x, y;
9162
9163   if (preview==NULL) return; /* I usually add this when I want */
9164                              /* to avoid silly crashes. You    */
9165                              /* should probably make sure that */
9166                              /* everything has been nicely     */
9167                              /* initialized!                   */
9168   for (j=0; j < ABS(cos(2*alpha)) ) { /* Are we inside the shape?  */
9169                                          /* glib.h contains ABS(x).   */
9170         row[i*3+0] = sqrt(1-r)*255;      /* Define Red                */
9171         row[i*3+1] = 128;                /* Define Green              */
9172         row[i*3+2] = 224;                /* Define Blue               */
9173       }                                  /* "+0" is for alignment!    */
9174       else {
9175         row[i*3+0] = r*255;
9176         row[i*3+1] = ABS(sin((float)i/SIZE*2*PI))*255;
9177         row[i*3+2] = ABS(sin((float)j/SIZE*2*PI))*255;
9178       }
9179     }
9180     gtk_preview_draw_row( GTK_PREVIEW(preview),row,0,j,SIZE);
9181     /* Insert "row" into "preview" starting at the point with  */
9182     /* coordinates (0,j) first column, j_th row extending SIZE */
9183     /* pixels to the right */
9184   }
9185
9186   free(row); /* save some space */
9187   gtk_widget_draw(preview,NULL); /* what does this do? */
9188   gdk_flush(); /* or this? */
9189 }
9190
9191 Non-GIMP users can have probably seen enough to do a lot of things already.
9192 For the GIMP users I have a few pointers to add.
9193
9194 Image Preview
9195
9196 It is probably wise to keep a reduced version of the image around with just
9197 enough pixels to fill the preview. This is done by selecting every n'th
9198 pixel where n is the ratio of the size of the image to the size of the
9199 preview. All further operations (including filling in the previews) are then
9200 performed on the reduced number of pixels only. The following is my
9201 implementation of reducing the image. (Keep in mind that I've had only basic
9202 C!)
9203
9204 (UNTESTED CODE ALERT!!!)
9205
9206 typedef struct {
9207   gint      width;
9208   gint      height;
9209   gint      bbp;
9210   guchar    *rgb;
9211   guchar    *mask;
9212 } ReducedImage;
9213
9214 enum {
9215   SELECTION_ONLY,
9216   SELECTION_IN_CONTEXT,
9217   ENTIRE_IMAGE
9218 };
9219
9220 ReducedImage *Reduce_The_Image(GDrawable *drawable,
9221                                GDrawable *mask,
9222                                gint LongerSize,
9223                                gint Selection)
9224 {
9225   /* This function reduced the image down to the the selected preview size */
9226   /* The preview size is determine by LongerSize, i.e. the greater of the  */
9227   /* two dimensions. Works for RGB images only!                            */
9228   gint RH, RW;          /* Reduced height and reduced width                */
9229   gint width, height;   /* Width and Height of the area being reduced      */
9230   gint bytes=drawable->bpp;
9231   ReducedImage *temp=(ReducedImage *)malloc(sizeof(ReducedImage));
9232
9233   guchar *tempRGB, *src_row, *tempmask, *src_mask_row,R,G,B;
9234   gint i, j, whichcol, whichrow, x1, x2, y1, y2;
9235   GPixelRgn srcPR, srcMask;
9236   gint NoSelectionMade=TRUE; /* Assume that we're dealing with the entire  */
9237                              /* image.                                     */
9238
9239   gimp_drawable_mask_bounds (drawable->id, &amp;x1, &amp;y1, &amp;x2, &amp;y2);
9240   width  = x2-x1;
9241   height = y2-y1;
9242   /* If there's a SELECTION, we got its bounds!)
9243
9244   if (width != drawable->width &amp;&amp; height != drawable->height)
9245     NoSelectionMade=FALSE;
9246   /* Become aware of whether the user has made an active selection   */
9247   /* This will become important later, when creating a reduced mask. */
9248
9249   /* If we want to preview the entire image, overrule the above!  */
9250   /* Of course, if no selection has been made, this does nothing! */
9251   if (Selection==ENTIRE_IMAGE) {
9252     x1=0;
9253     x2=drawable->width;
9254     y1=0;
9255     y2=drawable->height;
9256   }
9257
9258   /* If we want to preview a selection with some surrounding area we */
9259   /* have to expand it a little bit. Consider it a bit of a riddle. */
9260   if (Selection==SELECTION_IN_CONTEXT) {
9261     x1=MAX(0,                x1-width/2.0);
9262     x2=MIN(drawable->width,  x2+width/2.0);
9263     y1=MAX(0,                y1-height/2.0);
9264     y2=MIN(drawable->height, y2+height/2.0);
9265   }
9266
9267   /* How we can determine the width and the height of the area being */
9268   /* reduced.                                                        */
9269   width  = x2-x1;
9270   height = y2-y1;
9271
9272   /* The lines below determine which dimension is to be the longer   */
9273   /* side. The idea borrowed from the supernova plug-in. I suspect I */
9274   /* could've thought of it myself, but the truth must be told.      */
9275   /* Plagiarism stinks!                                               */
9276   if (width>height) {
9277     RW=LongerSize;
9278     RH=(float) height * (float) LongerSize/ (float) width;
9279   }
9280   else {
9281     RH=LongerSize;
9282     RW=(float)width * (float) LongerSize/ (float) height;
9283   }
9284
9285   /* The entire image is stretched into a string! */
9286   tempRGB   = (guchar *) malloc(RW*RH*bytes);
9287   tempmask  = (guchar *) malloc(RW*RH);
9288
9289   gimp_pixel_rgn_init (&amp;srcPR, drawable, x1, y1, width, height, FALSE, FALSE);
9290   gimp_pixel_rgn_init (&amp;srcMask, mask, x1, y1, width, height, FALSE, FALSE);
9291
9292   /* Grab enough to save a row of image and a row of mask. */
9293   src_row       = (guchar *) malloc (width*bytes);
9294   src_mask_row  = (guchar *) malloc (width);
9295
9296   for (i=0; i < RH; i++) {
9297     whichrow=(float)i*(float)height/(float)RH;
9298     gimp_pixel_rgn_get_row (&amp;srcPR, src_row, x1, y1+whichrow, width);
9299     gimp_pixel_rgn_get_row (&amp;srcMask, src_mask_row, x1, y1+whichrow, width);
9300
9301     for (j=0; j < RW; j++) {
9302       whichcol=(float)j*(float)width/(float)RW;
9303
9304       /* No selection made = each point is completely selected! */
9305       if (NoSelectionMade)
9306         tempmask[i*RW+j]=255;
9307       else
9308         tempmask[i*RW+j]=src_mask_row[whichcol];
9309
9310       /* Add the row to the one long string which now contains the image! */
9311       tempRGB[i*RW*bytes+j*bytes+0]=src_row[whichcol*bytes+0];
9312       tempRGB[i*RW*bytes+j*bytes+1]=src_row[whichcol*bytes+1];
9313       tempRGB[i*RW*bytes+j*bytes+2]=src_row[whichcol*bytes+2];
9314
9315       /* Hold on to the alpha as well */
9316       if (bytes==4)
9317         tempRGB[i*RW*bytes+j*bytes+3]=src_row[whichcol*bytes+3];
9318     }
9319   }
9320   temp->bpp=bytes;
9321   temp->width=RW;
9322   temp->height=RH;
9323   temp->rgb=tempRGB;
9324   temp->mask=tempmask;
9325   return temp;
9326 }
9327
9328 The following is a preview function which used the same ReducedImage type!
9329 Note that it uses fakes transparency (if one is present by means of
9330 fake_transparency which is defined as follows:
9331
9332 gint fake_transparency(gint i, gint j)
9333 {
9334   if ( ((i%20)- 10) * ((j%20)- 10)>0   )
9335     return 64;
9336   else
9337     return 196;
9338 }
9339
9340 Now here's the preview function:
9341
9342 void
9343 my_preview_render_function(GtkWidget     *preview,
9344                            gint          changewhat,
9345                            gint          changewhich)
9346 {
9347   gint Inten, bytes=drawable->bpp;
9348   gint i, j, k;
9349   float partial;
9350   gint RW=reduced->width;
9351   gint RH=reduced->height;
9352   guchar *row=malloc(bytes*RW);;
9353
9354
9355   for (i=0; i < RH; i++) {
9356     for (j=0; j < RW; j++) {
9357
9358       row[j*3+0] = reduced->rgb[i*RW*bytes + j*bytes + 0];
9359       row[j*3+1] = reduced->rgb[i*RW*bytes + j*bytes + 1];
9360       row[j*3+2] = reduced->rgb[i*RW*bytes + j*bytes + 2];
9361
9362       if (bytes==4)
9363         for (k=0; k<3; k++) {
9364           float transp=reduced->rgb[i*RW*bytes+j*bytes+3]/255.0;
9365           row[3*j+k]=transp*a[3*j+k]+(1-transp)*fake_transparency(i,j);
9366         }
9367     }
9368     gtk_preview_draw_row( GTK_PREVIEW(preview),row,0,i,RW);
9369   }
9370
9371   free(a);
9372   gtk_widget_draw(preview,NULL);
9373   gdk_flush();
9374 }
9375
9376 Applicable Routines
9377
9378 guint           gtk_preview_get_type           (void);
9379 /* No idea */
9380 void            gtk_preview_uninit             (void);
9381 /* No idea */
9382 GtkWidget*      gtk_preview_new                (GtkPreviewType   type);
9383 /* Described above */
9384 void            gtk_preview_size               (GtkPreview      *preview,
9385                                                 gint             width,
9386                                                 gint             height);
9387 /* Allows you to resize an existing preview.    */
9388 /* Apparently there's a bug in GTK which makes  */
9389 /* this process messy. A way to clean up a mess */
9390 /* is to manually resize the window containing  */
9391 /* the preview after resizing the preview.      */
9392
9393 void            gtk_preview_put                (GtkPreview      *preview,
9394                                                 GdkWindow       *window,
9395                                                 GdkGC           *gc,
9396                                                 gint             srcx,
9397                                                 gint             srcy,
9398                                                 gint             destx,
9399                                                 gint             desty,
9400                                                 gint             width,
9401                                                 gint             height);
9402 /* No idea */
9403
9404 void            gtk_preview_put_row            (GtkPreview      *preview,
9405                                                 guchar          *src,
9406                                                 guchar          *dest,
9407                                                 gint             x,
9408                                                 gint             y,
9409                                                 gint             w);
9410 /* No idea */
9411
9412 void            gtk_preview_draw_row           (GtkPreview      *preview,
9413                                                 guchar          *data,
9414                                                 gint             x,
9415                                                 gint             y,
9416                                                 gint             w);
9417 /* Described in the text */
9418
9419 void            gtk_preview_set_expand         (GtkPreview      *preview,
9420                                                 gint             expand);
9421 /* No idea */
9422
9423 /* No clue for any of the below but    */
9424 /* should be standard for most widgets */
9425 void            gtk_preview_set_gamma          (double           gamma);
9426 void            gtk_preview_set_color_cube     (guint            nred_shades,
9427                                                 guint            ngreen_shades,
9428                                                 guint            nblue_shades,
9429                                                 guint            ngray_shades);
9430 void            gtk_preview_set_install_cmap   (gint             install_cmap);
9431 void            gtk_preview_set_reserved       (gint             nreserved);
9432 GdkVisual*      gtk_preview_get_visual         (void);
9433 GdkColormap*    gtk_preview_get_cmap           (void);
9434 GtkPreviewInfo* gtk_preview_get_info           (void);
9435
9436 That's all, folks!
9437
9438 </verb></tscreen>
9439
9440 <!-- ***************************************************************** -->
9441 <sect>The EventBox Widget<label id="sec_EventBox">
9442 <!-- ***************************************************************** -->
9443 <p> 
9444 Some gtk widgets don't have associated X windows, so they just draw on 
9445 their parents. Because of this, they cannot receive events
9446 and if they are incorrectly sized, they don't clip so you can get
9447 messy overwriting etc. If you require more from these widgets, the
9448 EventBox is for you.
9449
9450 At first glance, the EventBox widget might appear to be totally
9451 useless. It draws nothing on the screen and responds to no
9452 events. However, it does serve a function - it provides an X window for
9453 its child widget. This is important as many GTK widgets do not
9454 have an associated X window. Not having an X window saves memory and
9455 improves performance, but also has some drawbacks. A widget without an
9456 X window cannot receive events, and does not perform any clipping on
9457 its contents. Although the name <em/EventBox/ emphasizes the
9458 event-handling function, the widget can also be used for clipping. 
9459 (And more ... see the example below.)
9460
9461 To create a new EventBox widget, use:
9462
9463 <tscreen><verb>
9464 GtkWidget *gtk_event_box_new( void );
9465 </verb></tscreen>
9466
9467 A child widget can then be added to this EventBox:
9468
9469 <tscreen><verb>
9470 gtk_container_add( GTK_CONTAINER(event_box), widget );
9471 </verb></tscreen>
9472
9473 The following example demonstrates both uses of an EventBox - a label
9474 is created that is clipped to a small box, and set up so that a
9475 mouse-click on the label causes the program to exit.
9476
9477 <tscreen><verb>
9478 /* example-start eventbox eventbox.c */
9479
9480 #include <gtk/gtk.h>
9481
9482 int 
9483 main (int argc, char *argv[])
9484 {
9485     GtkWidget *window;
9486     GtkWidget *event_box;
9487     GtkWidget *label;
9488     
9489     gtk_init (&amp;argc, &amp;argv);
9490     
9491     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
9492     
9493     gtk_window_set_title (GTK_WINDOW (window), "Event Box");
9494     
9495     gtk_signal_connect (GTK_OBJECT (window), "destroy",
9496                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
9497     
9498     gtk_container_border_width (GTK_CONTAINER (window), 10);
9499     
9500     /* Create an EventBox and add it to our toplevel window */
9501     
9502     event_box = gtk_event_box_new ();
9503     gtk_container_add (GTK_CONTAINER(window), event_box);
9504     gtk_widget_show (event_box);
9505     
9506     /* Create a long label */
9507     
9508     label = gtk_label_new ("Click here to quit, quit, quit, quit, quit");
9509     gtk_container_add (GTK_CONTAINER (event_box), label);
9510     gtk_widget_show (label);
9511     
9512     /* Clip it short. */
9513     gtk_widget_set_usize (label, 110, 20);
9514     
9515     /* And bind an action to it */
9516     gtk_widget_set_events (event_box, GDK_BUTTON_PRESS_MASK);
9517     gtk_signal_connect (GTK_OBJECT(event_box), "button_press_event",
9518                         GTK_SIGNAL_FUNC (gtk_exit), NULL);
9519     
9520     /* Yet one more thing you need an X window for ... */
9521     
9522     gtk_widget_realize (event_box);
9523     gdk_window_set_cursor (event_box->window, gdk_cursor_new (GDK_HAND1));
9524     
9525     gtk_widget_show (window);
9526     
9527     gtk_main ();
9528     
9529     return 0;
9530 }
9531 /* example-end */
9532 </verb></tscreen>
9533
9534 <!-- ***************************************************************** -->
9535 <sect>Setting Widget Attributes<label id="sec_setting_widget_attributes">
9536 <!-- ***************************************************************** -->
9537 <p>
9538 This describes the functions used to operate on widgets.  These can be used
9539 to set style, padding, size etc.
9540
9541 (Maybe I should make a whole section on accelerators.)
9542
9543 <tscreen><verb>
9544 void gtk_widget_install_accelerator( GtkWidget           *widget,
9545                                      GtkAcceleratorTable *table,
9546                                      gchar               *signal_name,
9547                                      gchar                key,
9548                                      guint8               modifiers );
9549
9550 void gtk_widget_remove_accelerator ( GtkWidget           *widget,
9551                                      GtkAcceleratorTable *table,
9552                                      gchar               *signal_name);
9553
9554 void gtk_widget_activate( GtkWidget *widget );
9555
9556 void gtk_widget_set_name( GtkWidget *widget,
9557                           gchar     *name );
9558
9559 gchar *gtk_widget_get_name( GtkWidget *widget );
9560
9561 void gtk_widget_set_sensitive( GtkWidget *widget,
9562                                gint       sensitive );
9563
9564 void gtk_widget_set_style( GtkWidget *widget,
9565                            GtkStyle  *style );
9566                                            
9567 GtkStyle *gtk_widget_get_style( GtkWidget *widget );
9568
9569 GtkStyle *gtk_widget_get_default_style( void );
9570
9571 void gtk_widget_set_uposition( GtkWidget *widget,
9572                                gint       x,
9573                                gint       y );
9574
9575 void gtk_widget_set_usize( GtkWidget *widget,
9576                            gint       width,
9577                            gint       height );
9578
9579 void gtk_widget_grab_focus( GtkWidget *widget );
9580
9581 void gtk_widget_show( GtkWidget *widget );
9582
9583 void gtk_widget_hide( GtkWidget *widget );
9584 </verb></tscreen>
9585
9586 <!-- ***************************************************************** -->
9587 <sect>Timeouts, IO and Idle Functions<label id="sec_timeouts">
9588 <!-- ***************************************************************** -->
9589
9590 <!-- ----------------------------------------------------------------- -->
9591 <sect1>Timeouts
9592 <p>
9593 You may be wondering how you make GTK do useful work when in gtk_main.
9594 Well, you have several options. Using the following functions you can
9595 create a timeout function that will be called every "interval"
9596 milliseconds.
9597
9598 <tscreen><verb>
9599 gint gtk_timeout_add( guint32     interval,
9600                       GtkFunction function,
9601                       gpointer    data );
9602 </verb></tscreen>
9603
9604 The first argument is the number of milliseconds between calls to your
9605 function.  The second argument is the function you wish to have called, and
9606 the third, the data passed to this callback function. The return value is
9607 an integer "tag" which may be used to stop the timeout by calling:
9608
9609 <tscreen><verb>
9610 void gtk_timeout_remove( gint tag );
9611 </verb></tscreen>
9612
9613 You may also stop the timeout function by returning zero or FALSE from
9614 your callback function. Obviously this means if you want your function to
9615 continue to be called, it should return a non-zero value, i.e. TRUE.
9616
9617 The declaration of your callback should look something like this:
9618
9619 <tscreen><verb>
9620 gint timeout_callback( gpointer data );
9621 </verb></tscreen>
9622
9623 <!-- ----------------------------------------------------------------- -->
9624 <sect1>Monitoring IO
9625 <p>
9626 Another nifty feature of GTK, is the ability to have it check for data on a
9627 file descriptor for you (as returned by open(2) or socket(2)).  This is
9628 especially useful for networking applications.  The function:
9629
9630 <tscreen><verb>
9631 gint gdk_input_add( gint              source,
9632                     GdkInputCondition condition,
9633                     GdkInputFunction  function,
9634                     gpointer          data );
9635 </verb></tscreen>
9636
9637 Where the first argument is the file descriptor you wish to have watched,
9638 and the second specifies what you want GDK to look for.  This may be one of:
9639
9640 <itemize>
9641 <item>GDK_INPUT_READ - Call your function when there is data ready for
9642 reading on your file descriptor.
9643
9644 <item>GDK_INPUT_WRITE - Call your function when the file descriptor is
9645 ready for writing.
9646 </itemize>
9647
9648 As I'm sure you've figured out already, the third argument is the function
9649 you wish to have called when the above conditions are satisfied, and the
9650 fourth is the data to pass to this function.
9651
9652 The return value is a tag that may be used to stop GDK from monitoring this
9653 file descriptor using the following function.
9654
9655 <tscreen><verb>
9656 void gdk_input_remove( gint tag );
9657 </verb></tscreen>
9658
9659 The callback function should be declared as:
9660
9661 <tscreen><verb>
9662 void input_callback( gpointer          data,
9663                      gint              source, 
9664                      GdkInputCondition condition );
9665 </verb></tscreen>
9666
9667 Where <tt/source/ and <tt/condition/ are as specified above.
9668
9669 <!-- ----------------------------------------------------------------- -->
9670 <sect1>Idle Functions
9671 <p>
9672 <!-- Need to check on idle priorities - TRG -->
9673 What if you have a function you want called when nothing else is
9674 happening ?
9675
9676 <tscreen><verb>
9677 gint gtk_idle_add( GtkFunction function,
9678                    gpointer    data );
9679 </verb></tscreen>
9680
9681 This causes GTK to call the specified function whenever nothing else is
9682 happening.
9683
9684 <tscreen><verb>
9685 void gtk_idle_remove( gint tag );
9686 </verb></tscreen>
9687
9688 I won't explain the meaning of the arguments as they follow very much like
9689 the ones above. The function pointed to by the first argument to
9690 gtk_idle_add will be called whenever the opportunity arises. As with the
9691 others, returning FALSE will stop the idle function from being called.
9692
9693 <!-- ***************************************************************** -->
9694 <sect>Advanced Event and Signal Handling<label id="sec_Adv_Events_and_Signals">
9695 <!-- ***************************************************************** -->
9696
9697 <!-- ----------------------------------------------------------------- -->
9698 <sect1>Signal Functions
9699
9700 <!-- ----------------------------------------------------------------- -->
9701 <sect2>Connecting and Disconnecting Signal Handlers
9702 <p>
9703
9704 <tscreen><verb>
9705 guint gtk_signal_connect( GtkObject     *object,
9706                           const gchar   *name,
9707                           GtkSignalFunc  func,
9708                           gpointer       func_data );
9709
9710 guint gtk_signal_connect_after( GtkObject     *object,
9711                                 const gchar   *name,
9712                                 GtkSignalFunc  func,
9713                                 gpointer       func_data );
9714
9715 guint gtk_signal_connect_object( GtkObject     *object,
9716                                  const gchar   *name,
9717                                  GtkSignalFunc  func,
9718                                  GtkObject     *slot_object );
9719
9720 guint gtk_signal_connect_object_after( GtkObject     *object,
9721                                        const gchar   *name,
9722                                        GtkSignalFunc  func,
9723                                        GtkObject     *slot_object );
9724
9725 guint gtk_signal_connect_full( GtkObject          *object,
9726                                const gchar        *name,
9727                                GtkSignalFunc       func,
9728                                GtkCallbackMarshal  marshal,
9729                                gpointer            data,
9730                                GtkDestroyNotify    destroy_func,
9731                                gint                object_signal,
9732                                gint                after );
9733
9734 guint gtk_signal_connect_interp( GtkObject          *object,
9735                                  const gchar        *name,
9736                                  GtkCallbackMarshal  func,
9737                                  gpointer            data,
9738                                  GtkDestroyNotify    destroy_func,
9739                                  gint                after );
9740
9741 void gtk_signal_connect_object_while_alive( GtkObject     *object,
9742                                             const gchar   *signal,
9743                                             GtkSignalFunc  func,
9744                                             GtkObject     *alive_object );
9745
9746 void gtk_signal_connect_while_alive( GtkObject     *object,
9747                                      const gchar   *signal,
9748                                      GtkSignalFunc  func,
9749                                      gpointer       func_data,
9750                                      GtkObject     *alive_object );
9751
9752 void gtk_signal_disconnect( GtkObject *object,
9753                             guint      handler_id );
9754
9755 void gtk_signal_disconnect_by_func( GtkObject     *object,
9756                                     GtkSignalFunc  func,
9757                                     gpointer       data );
9758 </verb></tscreen>
9759
9760 <!-- ----------------------------------------------------------------- -->
9761 <sect2>Blocking and Unblocking Signal Handlers
9762 <p>
9763 <tscreen><verb>
9764 void gtk_signal_handler_block( GtkObject *object,
9765                                guint      handler_id);
9766
9767 void gtk_signal_handler_block_by_func( GtkObject     *object,
9768                                        GtkSignalFunc  func,
9769                                        gpointer       data );
9770
9771 void gtk_signal_handler_block_by_data( GtkObject *object,
9772                                        gpointer   data );
9773
9774 void gtk_signal_handler_unblock( GtkObject *object,
9775                                  guint      handler_id );
9776
9777 void gtk_signal_handler_unblock_by_func( GtkObject     *object,
9778                                          GtkSignalFunc  func,
9779                                          gpointer       data );
9780
9781 void gtk_signal_handler_unblock_by_data( GtkObject *object,
9782                                          gpointer   data );
9783 </verb></tscreen>
9784
9785 <!-- ----------------------------------------------------------------- -->
9786 <sect2>Emitting and Stopping Signals
9787 <p>
9788 <tscreen><verb>
9789 void gtk_signal_emit( GtkObject *object,
9790                       guint      signal_id,
9791                       ... );
9792
9793 void gtk_signal_emit_by_name( GtkObject   *object,
9794                               const gchar *name,
9795                               ... );
9796
9797 void gtk_signal_emitv( GtkObject *object,
9798                        guint      signal_id,
9799                        GtkArg    *params );
9800
9801 void gtk_signal_emitv_by_name( GtkObject   *object,
9802                                const gchar *name,
9803                                GtkArg      *params );
9804
9805 guint gtk_signal_n_emissions( GtkObject *object,
9806                               guint      signal_id );
9807
9808 guint gtk_signal_n_emissions_by_name( GtkObject   *object,
9809                                       const gchar *name );
9810
9811 void gtk_signal_emit_stop( GtkObject *object,
9812                            guint      signal_id );
9813
9814 void gtk_signal_emit_stop_by_name( GtkObject   *object,
9815                                    const gchar *name );
9816 </verb></tscreen>
9817
9818 <!-- ----------------------------------------------------------------- -->
9819 <sect1>Signal Emission and Propagation
9820 <p>
9821 Signal emission is the process wherby GTK+ runs all handlers for a
9822 specific object and signal.
9823
9824 First, note that the return value from a signal emission is the
9825 return value of the <em>last</em> handler executed. Since event signals
9826 are all of type GTK_RUN_LAST, this will be the default (GTK+ supplied)
9827 default handler, unless you connect with gtk_signal_connect_after().
9828
9829 The way an event (say GTK_BUTTON_PRESS) is handled, is:
9830 <itemize>
9831 <item>Start with the widget where the event occured.
9832
9833 <item>Emit the generic "event" signal. If that signal handler returns
9834 a value of TRUE, stop all processing.
9835
9836 <item>Otherwise, emit a specific, "button_press_event" signal. If that
9837 returns TRUE, stop all processing.
9838
9839 <item>Otherwise, go to the widget's parent, and repeat the above steps.
9840
9841 <item>Contimue until some signal handler returns TRUE, or until the
9842 top-level widget is reached.
9843 </itemize>
9844
9845 Some consequences of the above are:
9846 <itemize>
9847 <item>Your handler's return value will have no effect if there is a
9848 default handler, unless you connect with gtk_signal_connect_after().
9849
9850 <item>To prevent the default handler from being run, you need to connect
9851 with gtk_signal_connect() and use gtk_signal_emit_stop_by_name() - the
9852 return value only affects whether the signal is propagated, not the
9853 current emission.
9854 </itemize>
9855
9856 <!-- ***************************************************************** -->
9857 <sect>Managing Selections
9858 <!-- ***************************************************************** -->
9859
9860 <!-- ----------------------------------------------------------------- -->
9861 <sect1> Overview
9862 <p>
9863 One type of interprocess communication supported by GTK is
9864 <em>selections</em>. A selection identifies a chunk of data, for
9865 instance, a portion of text, selected by the user in some fashion, for
9866 instance, by dragging with the mouse. Only one application on a
9867 display, (the <em>owner</em> can own a particular selection at one
9868 time, so when a selection is claimed by one application, the previous
9869 owner must indicate to the user that selection has been
9870 relinquished. Other applications can request the contents of a
9871 selection in different forms, called <em>targets</em>. There can be
9872 any number of selections, but most X applications only handle one, the
9873 <em>primary selection</em>.
9874
9875 In most cases, it isn't necessary for a GTK application to deal with
9876 selections itself. The standard widgets, such as the Entry widget,
9877 already have the capability to claim the selection when appropriate
9878 (e.g., when the user drags over text), and to retrieve the contents of
9879 the selection owned by another widget, or another application (e.g.,
9880 when the user clicks the second mouse button). However, there may be
9881 cases in which you want to give other widgets the ability to supply
9882 the selection, or you wish to retrieve targets not supported by
9883 default.
9884
9885 A fundamental concept needed to understand selection handling is that
9886 of the <em>atom</em>. An atom is an integer that uniquely identifies a
9887 string (on a certain display). Certain atoms are predefined by the X
9888 server, and in some cases there are constants in <tt>gtk.h</tt>
9889 corresponding to these atoms. For instance the constant
9890 <tt>GDK_PRIMARY_SELECTION</tt> corresponds to the string "PRIMARY".
9891 In other cases, you should use the functions
9892 <tt>gdk_atom_intern()</tt>, to get the atom corresponding to a string,
9893 and <tt>gdk_atom_name()</tt>, to get the name of an atom. Both
9894 selections and targets are identified by atoms.
9895
9896 <!-- ----------------------------------------------------------------- -->
9897 <sect1> Retrieving the selection
9898 <p>
9899 Retrieving the selection is an asynchronous process. To start the
9900 process, you call:
9901
9902 <tscreen><verb>
9903 gint gtk_selection_convert( GtkWidget *widget, 
9904                             GdkAtom    selection, 
9905                             GdkAtom    target,
9906                             guint32    time );
9907 </verb</tscreen>
9908
9909 This <em>converts</em> the selection into the form specified by
9910 <tt/target/. If at all possible, the time field should be the time
9911 from the event that triggered the selection. This helps make sure that
9912 events occur in the order that the user requested them. However, if it
9913 is not available (for instance, if the conversion was triggered by
9914 a "clicked" signal), then you can use the constant
9915 <tt>GDK_CURRENT_TIME</tt>.
9916
9917 When the selection owner responds to the request, a
9918 "selection_received" signal is sent to your application. The handler
9919 for this signal receives a pointer to a <tt>GtkSelectionData</tt>
9920 structure, which is defined as:
9921
9922 <tscreen><verb>
9923 struct _GtkSelectionData
9924 {
9925   GdkAtom selection;
9926   GdkAtom target;
9927   GdkAtom type;
9928   gint    format;
9929   guchar *data;
9930   gint    length;
9931 };
9932 </verb></tscreen>
9933
9934 <tt>selection</tt> and <tt>target</tt> are the values you gave in your
9935 <tt>gtk_selection_convert()</tt> call. <tt>type</tt> is an atom that
9936 identifies the type of data returned by the selection owner. Some
9937 possible values are "STRING", a string of latin-1 characters, "ATOM",
9938 a series of atoms, "INTEGER", an integer, etc. Most targets can only
9939 return one type. <tt/format/ gives the length of the units (for
9940 instance characters) in bits. Usually, you don't care about this when
9941 receiving data. <tt>data</tt> is a pointer to the returned data, and
9942 <tt>length</tt> gives the length of the returned data, in bytes. If
9943 <tt>length</tt> is negative, then an error occurred and the selection
9944 could not be retrieved. This might happen if no application owned the
9945 selection, or if you requested a target that the application didn't
9946 support. The buffer is actually guaranteed to be one byte longer than
9947 <tt>length</tt>; the extra byte will always be zero, so it isn't
9948 necessary to make a copy of strings just to null terminate them.
9949
9950 In the following example, we retrieve the special target "TARGETS",
9951 which is a list of all targets into which the selection can be
9952 converted.
9953
9954 <tscreen><verb>
9955 /* example-start selection gettargets.c */
9956
9957 #include <gtk/gtk.h>
9958
9959 void selection_received (GtkWidget *widget, 
9960                          GtkSelectionData *selection_data, 
9961                          gpointer data);
9962
9963 /* Signal handler invoked when user clicks on the "Get Targets" button */
9964 void
9965 get_targets (GtkWidget *widget, gpointer data)
9966 {
9967   static GdkAtom targets_atom = GDK_NONE;
9968
9969   /* Get the atom corresponding to the string "TARGETS" */
9970   if (targets_atom == GDK_NONE)
9971     targets_atom = gdk_atom_intern ("TARGETS", FALSE);
9972
9973   /* And request the "TARGETS" target for the primary selection */
9974   gtk_selection_convert (widget, GDK_SELECTION_PRIMARY, targets_atom,
9975                          GDK_CURRENT_TIME);
9976 }
9977
9978 /* Signal handler called when the selections owner returns the data */
9979 void
9980 selection_received (GtkWidget *widget, GtkSelectionData *selection_data, 
9981                     gpointer data)
9982 {
9983   GdkAtom *atoms;
9984   GList *item_list;
9985   int i;
9986
9987   /* **** IMPORTANT **** Check to see if retrieval succeeded  */
9988   if (selection_data->length < 0)
9989     {
9990       g_print ("Selection retrieval failed\n");
9991       return;
9992     }
9993   /* Make sure we got the data in the expected form */
9994   if (selection_data->type != GDK_SELECTION_TYPE_ATOM)
9995     {
9996       g_print ("Selection \"TARGETS\" was not returned as atoms!\n");
9997       return;
9998     }
9999   
10000   /* Print out the atoms we received */
10001   atoms = (GdkAtom *)selection_data->data;
10002
10003   item_list = NULL;
10004   for (i=0; i<selection_data->length/sizeof(GdkAtom); i++)
10005     {
10006       char *name;
10007       name = gdk_atom_name (atoms[i]);
10008       if (name != NULL)
10009         g_print ("%s\n",name);
10010       else
10011         g_print ("(bad atom)\n");
10012     }
10013
10014   return;
10015 }
10016
10017 int 
10018 main (int argc, char *argv[])
10019 {
10020   GtkWidget *window;
10021   GtkWidget *button;
10022   
10023   gtk_init (&amp;argc, &amp;argv);
10024
10025   /* Create the toplevel window */
10026
10027   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
10028   gtk_window_set_title (GTK_WINDOW (window), "Event Box");
10029   gtk_container_border_width (GTK_CONTAINER (window), 10);
10030
10031   gtk_signal_connect (GTK_OBJECT (window), "destroy",
10032                       GTK_SIGNAL_FUNC (gtk_exit), NULL);
10033
10034   /* Create a button the user can click to get targets */
10035
10036   button = gtk_button_new_with_label ("Get Targets");
10037   gtk_container_add (GTK_CONTAINER (window), button);
10038
10039   gtk_signal_connect (GTK_OBJECT(button), "clicked",
10040                       GTK_SIGNAL_FUNC (get_targets), NULL);
10041   gtk_signal_connect (GTK_OBJECT(button), "selection_received",
10042                       GTK_SIGNAL_FUNC (selection_received), NULL);
10043
10044   gtk_widget_show (button);
10045   gtk_widget_show (window);
10046   
10047   gtk_main ();
10048   
10049   return 0;
10050 }
10051 /* example-end */
10052 </verb></tscreen>
10053
10054 <!-- ----------------------------------------------------------------- -->
10055 <sect1> Supplying the selection 
10056 <p>
10057 Supplying the selection is a bit more complicated. You must register 
10058 handlers that will be called when your selection is requested. For
10059 each selection/target pair you will handle, you make a call to:
10060
10061 <tscreen><verb>
10062 void gtk_selection_add_handler( GtkWidget            *widget, 
10063                                 GdkAtom               selection,
10064                                 GdkAtom               target,
10065                                 GtkSelectionFunction  function,
10066                                 GtkRemoveFunction     remove_func,
10067                                 gpointer              data );
10068 </verb></tscreen>
10069
10070 <tt/widget/, <tt/selection/, and <tt/target/ identify the requests
10071 this handler will manage.  <tt/remove_func/, if not
10072 NULL, will be called when the signal handler is removed. This is
10073 useful, for instance, for interpreted languages which need to
10074 keep track of a reference count for <tt/data/.
10075
10076 The callback function has the signature:
10077
10078 <tscreen><verb>
10079 typedef void (*GtkSelectionFunction)( GtkWidget        *widget, 
10080                                       GtkSelectionData *selection_data,
10081                                       gpointer          data );
10082
10083 </verb></tscreen>
10084
10085 The GtkSelectionData is the same as above, but this time, we're
10086 responsible for filling in the fields <tt/type/, <tt/format/,
10087 <tt/data/, and <tt/length/. (The <tt/format/ field is actually
10088 important here - the X server uses it to figure out whether the data
10089 needs to be byte-swapped or not. Usually it will be 8 - <em/i.e./ a
10090 character - or 32 - <em/i.e./ a. integer.) This is done by calling the
10091 function:
10092
10093 <tscreen><verb>
10094 void gtk_selection_data_set( GtkSelectionData *selection_data,
10095                              GdkAtom           type,
10096                              gint              format,
10097                              guchar           *data,
10098                              gint              length );
10099 </verb></tscreen>
10100
10101 This function takes care of properly making a copy of the data so that
10102 you don't have to worry about keeping it around. (You should not fill
10103 in the fields of the GtkSelectionData structure by hand.)
10104
10105 When prompted by the user, you claim ownership of the selection by
10106 calling:
10107
10108 <tscreen><verb>
10109 gint gtk_selection_owner_set( GtkWidget *widget,
10110                               GdkAtom    selection,
10111                               guint32    time );
10112 </verb></tscreen>
10113
10114 If another application claims ownership of the selection, you will
10115 receive a "selection_clear_event".
10116
10117 As an example of supplying the selection, the following program adds
10118 selection functionality to a toggle button. When the toggle button is
10119 depressed, the program claims the primary selection. The only target
10120 supported (aside from certain targets like "TARGETS" supplied by GTK
10121 itself), is the "STRING" target. When this target is requested, a
10122 string representation of the time is returned.
10123
10124 <tscreen><verb>
10125 /* example-start selection setselection.c */
10126
10127 #include <gtk/gtk.h>
10128 #include <time.h>
10129
10130 /* Callback when the user toggles the selection */
10131 void
10132 selection_toggled (GtkWidget *widget, gint *have_selection)
10133 {
10134   if (GTK_TOGGLE_BUTTON(widget)->active)
10135     {
10136       *have_selection = gtk_selection_owner_set (widget,
10137                                                  GDK_SELECTION_PRIMARY,
10138                                                  GDK_CURRENT_TIME);
10139       /* if claiming the selection failed, we return the button to
10140          the out state */
10141       if (!*have_selection)
10142         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON(widget), FALSE);
10143     }
10144   else
10145     {
10146       if (*have_selection)
10147         {
10148           /* Before clearing the selection by setting the owner to NULL,
10149              we check if we are the actual owner */
10150           if (gdk_selection_owner_get (GDK_SELECTION_PRIMARY) == widget->window)
10151             gtk_selection_owner_set (NULL, GDK_SELECTION_PRIMARY,
10152                                      GDK_CURRENT_TIME);
10153           *have_selection = FALSE;
10154         }
10155     }
10156 }
10157
10158 /* Called when another application claims the selection */
10159 gint
10160 selection_clear (GtkWidget *widget, GdkEventSelection *event,
10161                  gint *have_selection)
10162 {
10163   *have_selection = FALSE;
10164   gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON(widget), FALSE);
10165
10166   return TRUE;
10167 }
10168
10169 /* Supplies the current time as the selection. */
10170 void
10171 selection_handle (GtkWidget *widget, 
10172                   GtkSelectionData *selection_data,
10173                   gpointer data)
10174 {
10175   gchar *timestr;
10176   time_t current_time;
10177
10178   current_time = time (NULL);
10179   timestr = asctime (localtime(&amp;current_time)); 
10180   /* When we return a single string, it should not be null terminated.
10181      That will be done for us */
10182
10183   gtk_selection_data_set (selection_data, GDK_SELECTION_TYPE_STRING,
10184                           8, timestr, strlen(timestr));
10185 }
10186
10187 int
10188 main (int argc, char *argv[])
10189 {
10190   GtkWidget *window;
10191
10192   GtkWidget *selection_button;
10193
10194   static int have_selection = FALSE;
10195   
10196   gtk_init (&amp;argc, &amp;argv);
10197
10198   /* Create the toplevel window */
10199
10200   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
10201   gtk_window_set_title (GTK_WINDOW (window), "Event Box");
10202   gtk_container_border_width (GTK_CONTAINER (window), 10);
10203
10204   gtk_signal_connect (GTK_OBJECT (window), "destroy",
10205                       GTK_SIGNAL_FUNC (gtk_exit), NULL);
10206
10207   /* Create a toggle button to act as the selection */
10208
10209   selection_button = gtk_toggle_button_new_with_label ("Claim Selection");
10210   gtk_container_add (GTK_CONTAINER (window), selection_button);
10211   gtk_widget_show (selection_button);
10212
10213   gtk_signal_connect (GTK_OBJECT(selection_button), "toggled",
10214                       GTK_SIGNAL_FUNC (selection_toggled), &amp;have_selection);
10215   gtk_signal_connect (GTK_OBJECT(selection_button), "selection_clear_event",
10216                       GTK_SIGNAL_FUNC (selection_clear), &amp;have_selection);
10217
10218   gtk_selection_add_handler (selection_button, GDK_SELECTION_PRIMARY,
10219                              GDK_SELECTION_TYPE_STRING,
10220                              selection_handle, NULL);
10221
10222   gtk_widget_show (selection_button);
10223   gtk_widget_show (window);
10224   
10225   gtk_main ();
10226   
10227   return 0;
10228 }
10229 /* example-end */
10230 </verb></tscreen>
10231
10232
10233 <!-- ***************************************************************** -->
10234 <sect>glib<label id="sec_glib">
10235 <!-- ***************************************************************** -->
10236 <p>
10237 glib provides many useful functions and definitions available for use
10238 when creating GDK and GTK applications. I will list them all here with
10239 a brief explanation. Many are duplicates of standard libc functions so
10240 I won't go into detail on those. This is mostly to be used as a reference,
10241 so you know what is available for use.
10242
10243 <!-- ----------------------------------------------------------------- -->
10244 <sect1>Definitions
10245 <p>
10246 Definitions for the extremes of many of the standard types are:
10247
10248 <tscreen><verb>
10249 G_MINFLOAT
10250 G_MAXFLOAT
10251 G_MINDOUBLE
10252 G_MAXDOUBLE
10253 G_MINSHORT
10254 G_MAXSHORT
10255 G_MININT
10256 G_MAXINT
10257 G_MINLONG
10258 G_MAXLONG
10259 </verb></tscreen>
10260
10261 Also, the following typedefs. The ones left unspecified are dynamically set
10262 depending on the architecture. Remember to avoid counting on the size of a
10263 pointer if you want to be portable! E.g., a pointer on an Alpha is 8 bytes, but 4
10264 on Intel.
10265
10266 <tscreen><verb>
10267 char   gchar;
10268 short  gshort;
10269 long   glong;
10270 int    gint;
10271 char   gboolean;
10272
10273 unsigned char   guchar;
10274 unsigned short  gushort;
10275 unsigned long   gulong;
10276 unsigned int    guint;
10277
10278 float   gfloat;
10279 double  gdouble;
10280 long double gldouble;
10281
10282 void* gpointer;
10283
10284 gint8
10285 guint8
10286 gint16
10287 guint16
10288 gint32
10289 guint32
10290 </verb></tscreen>
10291
10292 <!-- ----------------------------------------------------------------- -->
10293 <sect1>Doubly Linked Lists
10294 <p>
10295 The following functions are used to create, manage, and destroy doubly
10296 linked lists.  I assume you know what linked lists are, as it is beyond the scope
10297 of this document to explain them.  Of course, it's not required that you
10298 know these for general use of GTK, but they are nice to know.
10299
10300 <tscreen><verb>
10301 GList *g_list_alloc( void );
10302
10303 void g_list_free( GList *list );
10304
10305 void g_list_free_1( GList *list );
10306
10307 GList *g_list_append( GList     *list,
10308                       gpointer   data );
10309                            
10310 GList *g_list_prepend( GList    *list,
10311                        gpointer  data );
10312                         
10313 GList *g_list_insert( GList    *list,
10314                       gpointer  data,
10315                             gint      position );
10316
10317 GList *g_list_remove( GList    *list,
10318                       gpointer  data );
10319                            
10320 GList *g_list_remove_link( GList *list,
10321                            GList *link );
10322
10323 GList *g_list_reverse( GList *list );
10324
10325 GList *g_list_nth( GList *list,
10326                    gint   n );
10327                            
10328 GList *g_list_find( GList    *list,
10329                     gpointer  data );
10330
10331 GList *g_list_last( GList *list );
10332
10333 GList *g_list_first( GList *list );
10334
10335 gint g_list_length( GList *list );
10336
10337 void g_list_foreach( GList    *list,
10338                      GFunc     func,
10339                      gpointer  user_data );
10340 </verb></tscreen>                                             
10341
10342 <!-- ----------------------------------------------------------------- -->
10343 <sect1>Singly Linked Lists
10344 <p>
10345 Many of the above functions for singly linked lists are identical to the
10346 above. Here is a complete list:
10347 <tscreen><verb>
10348 GSList *g_slist_alloc( void );
10349
10350 void g_slist_free( GSList *list );
10351
10352 void g_slist_free_1( GSList *list );
10353
10354 GSList *g_slist_append( GSList   *list,
10355                         gpointer  data );
10356                 
10357 GSList *g_slist_prepend( GSList   *list,
10358                          gpointer  data );
10359                              
10360 GSList *g_slist_insert( GSList   *list,
10361                         gpointer  data,
10362                             gint      position );
10363                              
10364 GSList *g_slist_remove( GSList   *list,
10365                         gpointer  data );
10366                              
10367 GSList *g_slist_remove_link( GSList *list,
10368                              GSList *link );
10369                              
10370 GSList *g_slist_reverse( GSList *list );
10371
10372 GSList *g_slist_nth( GSList *list,
10373                      gint    n );
10374                              
10375 GSList *g_slist_find( GSList   *list,
10376                       gpointer  data );
10377                              
10378 GSList *g_slist_last( GSList *list );
10379
10380 gint g_slist_length( GSList *list );
10381
10382 void g_slist_foreach( GSList   *list,
10383                       GFunc     func,
10384                             gpointer  user_data );
10385         
10386 </verb></tscreen>
10387
10388 <!-- ----------------------------------------------------------------- -->
10389 <sect1>Memory Management
10390 <p>
10391 <tscreen><verb>
10392 gpointer g_malloc( gulong size );
10393 </verb></tscreen>
10394
10395 This is a replacement for malloc(). You do not need to check the return
10396 value as it is done for you in this function.
10397
10398 <tscreen><verb>
10399 gpointer g_malloc0( gulong size );
10400 </verb></tscreen>
10401
10402 Same as above, but zeroes the memory before returning a pointer to it.
10403
10404 <tscreen><verb>
10405 gpointer g_realloc( gpointer mem,
10406                     gulong   size );
10407 </verb></tscreen>
10408
10409 Relocates "size" bytes of memory starting at "mem".  Obviously, the
10410 memory should have been previously allocated.
10411
10412 <tscreen><verb>
10413 void g_free( gpointer mem );
10414 </verb></tscreen>
10415
10416 Frees memory. Easy one.
10417
10418 <tscreen><verb>
10419 void g_mem_profile( void );
10420 </verb></tscreen>
10421
10422 Dumps a profile of used memory, but requires that you add #define
10423 MEM_PROFILE to the top of glib/gmem.c and re-make and make install.
10424
10425 <tscreen><verb>
10426 void g_mem_check( gpointer mem );
10427 </verb></tscreen>
10428
10429 Checks that a memory location is valid.  Requires you add #define
10430 MEM_CHECK to the top of gmem.c and re-make and make install.
10431
10432 <!-- ----------------------------------------------------------------- -->
10433 <sect1>Timers
10434 <p>
10435 Timer functions..
10436
10437 <tscreen><verb>
10438 GTimer *g_timer_new( void );
10439
10440 void g_timer_destroy( GTimer *timer );
10441
10442 void g_timer_start( GTimer  *timer );
10443
10444 void g_timer_stop( GTimer  *timer );
10445
10446 void g_timer_reset( GTimer  *timer );
10447
10448 gdouble g_timer_elapsed( GTimer *timer,
10449                          gulong *microseconds );
10450 </verb></tscreen>                        
10451
10452 <!-- ----------------------------------------------------------------- -->
10453 <sect1>String Handling
10454 <p>
10455 A whole mess of string handling functions. They all look very interesting, and
10456 probably better for many purposes than the standard C string functions, but
10457 require documentation.
10458
10459 <tscreen><verb>
10460 GString *g_string_new( gchar *init );
10461
10462 void g_string_free( GString *string,
10463                     gint     free_segment );
10464                              
10465 GString *g_string_assign( GString *lval,
10466                           gchar   *rval );
10467                              
10468 GString *g_string_truncate( GString *string,
10469                             gint     len );
10470                              
10471 GString *g_string_append( GString *string,
10472                           gchar   *val );
10473                             
10474 GString *g_string_append_c( GString *string,
10475                             gchar    c );
10476         
10477 GString *g_string_prepend( GString *string,
10478                            gchar   *val );
10479                              
10480 GString *g_string_prepend_c( GString *string,
10481                              gchar    c );
10482         
10483 void g_string_sprintf( GString *string,
10484                        gchar   *fmt,
10485                        ...);
10486         
10487 void g_string_sprintfa ( GString *string,
10488                          gchar   *fmt,
10489                          ... );
10490 </verb></tscreen>                                                         
10491
10492 <!-- ----------------------------------------------------------------- -->
10493 <sect1>Utility and Error Functions
10494 <p>
10495 <tscreen><verb>
10496 gchar *g_strdup( const gchar *str );
10497 </verb></tscreen>
10498
10499 Replacement strdup function.  Copies the original strings contents to
10500 newly allocated memory, and returns a pointer to it.
10501
10502 <tscreen><verb>
10503 gchar *g_strerror( gint errnum );
10504 </verb></tscreen>
10505
10506 I recommend using this for all error messages.  It's much nicer, and more
10507 portable than perror() or others.  The output is usually of the form:
10508
10509 <tscreen><verb>
10510 program name:function that failed:file or further description:strerror
10511 </verb></tscreen>
10512
10513 Here's an example of one such call used in our hello_world program:
10514
10515 <tscreen><verb>
10516 g_print("hello_world:open:%s:%s\n", filename, g_strerror(errno));
10517 </verb></tscreen>
10518
10519 <tscreen><verb>
10520 void g_error( gchar *format, ... );
10521 </verb></tscreen>
10522
10523 Prints an error message. The format is just like printf, but it
10524 prepends "** ERROR **: " to your message, and exits the program.  
10525 Use only for fatal errors.
10526
10527 <tscreen><verb>
10528 void g_warning( gchar *format, ... );
10529 </verb></tscreen>
10530
10531 Same as above, but prepends "** WARNING **: ", and does not exit the
10532 program.
10533
10534 <tscreen><verb>
10535 void g_message( gchar *format, ... );
10536 </verb></tscreen>
10537
10538 Prints "message: " prepended to the string you pass in.
10539
10540 <tscreen><verb>
10541 void g_print( gchar *format, ... );
10542 </verb></tscreen>
10543
10544 Replacement for printf().
10545
10546 And our last function:
10547
10548 <tscreen><verb>
10549 gchar *g_strsignal( gint signum );
10550 </verb></tscreen>
10551
10552 Prints out the name of the Unix system signal given the signal number.
10553 Useful in generic signal handling functions.
10554
10555 All of the above are more or less just stolen from glib.h.  If anyone cares
10556 to document any function, just send me an email!
10557
10558 <!-- ***************************************************************** -->
10559 <sect>GTK's rc Files
10560 <!-- ***************************************************************** -->
10561 <p>
10562 GTK has its own way of dealing with application defaults, by using rc
10563 files. These can be used to set the colors of just about any widget, and
10564 can also be used to tile pixmaps onto the background of some widgets.  
10565
10566 <!-- ----------------------------------------------------------------- -->
10567 <sect1>Functions For rc Files 
10568 <p>
10569 When your application starts, you should include a call to:
10570
10571 <tscreen><verb>
10572 void gtk_rc_parse( char *filename );
10573 </verb></tscreen>
10574
10575 Passing in the filename of your rc file.  This will cause GTK to parse this
10576 file, and use the style settings for the widget types defined there.
10577
10578 If you wish to have a special set of widgets that can take on a different
10579 style from others, or any other logical division of widgets, use a call to:
10580
10581 <tscreen><verb>
10582 void gtk_widget_set_name( GtkWidget *widget,
10583                           gchar     *name );
10584 </verb></tscreen>
10585
10586 Passing your newly created widget as the first argument, and the name
10587 you wish to give it as the second. This will allow you to change the
10588 attributes of this widget by name through the rc file.
10589
10590 If we use a call something like this:
10591
10592 <tscreen><verb>
10593 button = gtk_button_new_with_label ("Special Button");
10594 gtk_widget_set_name (button, "special button");
10595 </verb></tscreen>
10596
10597 Then this button is given the name "special button" and may be addressed by
10598 name in the rc file as "special button.GtkButton".  [<--- Verify ME!]
10599
10600 The example rc file below, sets the properties of the main window, and lets
10601 all children of that main window inherit the style described by the "main
10602 button" style.  The code used in the application is:
10603
10604 <tscreen><verb>
10605 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
10606 gtk_widget_set_name (window, "main window");
10607 </verb></tscreen>
10608
10609 And then the style is defined in the rc file using:
10610
10611 <tscreen><verb>
10612 widget "main window.*GtkButton*" style "main_button"
10613 </verb></tscreen>
10614
10615 Which sets all the GtkButton widgets in the "main window" to the
10616 "main_buttons" style as defined in the rc file.
10617
10618 As you can see, this is a fairly powerful and flexible system.  Use your
10619 imagination as to how best to take advantage of this.
10620
10621 <!-- ----------------------------------------------------------------- -->
10622 <sect1>GTK's rc File Format
10623 <p>
10624 The format of the GTK file is illustrated in the example below. This is
10625 the testgtkrc file from the GTK distribution, but I've added a
10626 few comments and things. You may wish to include this explanation
10627 your application to allow the user to fine tune his application.
10628
10629 There are several directives to change the attributes of a widget.
10630
10631 <itemize>
10632 <item>fg - Sets the foreground color of a widget.
10633 <item>bg - Sets the background color of a widget.
10634 <item>bg_pixmap - Sets the background of a widget to a tiled pixmap.
10635 <item>font - Sets the font to be used with the given widget.
10636 </itemize>
10637
10638 In addition to this, there are several states a widget can be in, and you
10639 can set different colors, pixmaps and fonts for each state. These states are:
10640
10641 <itemize>
10642 <item>NORMAL - The normal state of a widget, without the mouse over top of
10643 it, and not being pressed etc.
10644 <item>PRELIGHT - When the mouse is over top of the widget, colors defined
10645 using this state will be in effect.
10646 <item>ACTIVE - When the widget is pressed or clicked it will be active, and
10647 the attributes assigned by this tag will be in effect.
10648 <item>INSENSITIVE - When a widget is set insensitive, and cannot be
10649 activated, it will take these attributes.
10650 <item>SELECTED - When an object is selected, it takes these attributes.
10651 </itemize>
10652
10653 When using the "fg" and "bg" keywords to set the colors of widgets, the
10654 format is:
10655
10656 <tscreen><verb>
10657 fg[<STATE>] = { Red, Green, Blue }
10658 </verb></tscreen>
10659
10660 Where STATE is one of the above states (PRELIGHT, ACTIVE etc), and the Red,
10661 Green and Blue are values in the range of 0 - 1.0,  { 1.0, 1.0, 1.0 } being
10662 white. They must be in float form, or they will register as 0, so a straight 
10663 "1" will not work, it must be "1.0".  A straight "0" is fine because it 
10664 doesn't matter if it's not recognized.  Unrecognized values are set to 0.
10665
10666 bg_pixmap is very similar to the above, except the colors are replaced by a
10667 filename.
10668
10669 pixmap_path is a list of paths separated by ":"'s.  These paths will be
10670 searched for any pixmap you specify.
10671
10672 The font directive is simply:
10673 <tscreen><verb>
10674 font = "<font name>"
10675 </verb></tscreen>
10676
10677 Where the only hard part is figuring out the font string. Using xfontsel or
10678 similar utility should help.
10679
10680 The "widget_class" sets the style of a class of widgets. These classes are
10681 listed in the widget overview on the class hierarchy.
10682
10683 The "widget" directive sets a specifically named set of widgets to a
10684 given style, overriding any style set for the given widget class.
10685 These widgets are registered inside the application using the
10686 gtk_widget_set_name() call. This allows you to specify the attributes of a
10687 widget on a per widget basis, rather than setting the attributes of an
10688 entire widget class. I urge you to document any of these special widgets so
10689 users may customize them.
10690
10691 When the keyword <tt>parent</> is used as an attribute, the widget will take on
10692 the attributes of its parent in the application.
10693
10694 When defining a style, you may assign the attributes of a previously defined
10695 style to this new one.
10696
10697 <tscreen><verb>
10698 style "main_button" = "button"
10699 {
10700   font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"
10701   bg[PRELIGHT] = { 0.75, 0, 0 }
10702 }
10703 </verb></tscreen>
10704
10705 This example takes the "button" style, and creates a new "main_button" style
10706 simply by changing the font and prelight background color of the "button"
10707 style.
10708
10709 Of course, many of these attributes don't apply to all widgets. It's a
10710 simple matter of common sense really. Anything that could apply, should.
10711
10712 <!-- ----------------------------------------------------------------- -->
10713 <sect1>Example rc file
10714 <p>
10715
10716 <tscreen><verb>
10717 # pixmap_path "<dir 1>:<dir 2>:<dir 3>:..."
10718 #
10719 pixmap_path "/usr/include/X11R6/pixmaps:/home/imain/pixmaps"
10720 #
10721 # style <name> [= <name>]
10722 # {
10723 #   <option>
10724 # }
10725 #
10726 # widget <widget_set> style <style_name>
10727 # widget_class <widget_class_set> style <style_name>
10728
10729
10730 # Here is a list of all the possible states.  Note that some do not apply to
10731 # certain widgets.
10732 #
10733 # NORMAL - The normal state of a widget, without the mouse over top of
10734 # it, and not being pressed etc.
10735 #
10736 # PRELIGHT - When the mouse is over top of the widget, colors defined
10737 # using this state will be in effect.
10738 #
10739 # ACTIVE - When the widget is pressed or clicked it will be active, and
10740 # the attributes assigned by this tag will be in effect.
10741 #
10742 # INSENSITIVE - When a widget is set insensitive, and cannot be
10743 # activated, it will take these attributes.
10744 #
10745 # SELECTED - When an object is selected, it takes these attributes.
10746 #
10747 # Given these states, we can set the attributes of the widgets in each of
10748 # these states using the following directives.
10749 #
10750 # fg - Sets the foreground color of a widget.
10751 # fg - Sets the background color of a widget.
10752 # bg_pixmap - Sets the background of a widget to a tiled pixmap.
10753 # font - Sets the font to be used with the given widget.
10754 #
10755
10756 # This sets a style called "button".  The name is not really important, as
10757 # it is assigned to the actual widgets at the bottom of the file.
10758
10759 style "window"
10760 {
10761   #This sets the padding around the window to the pixmap specified.
10762   #bg_pixmap[<STATE>] = "<pixmap filename>"
10763   bg_pixmap[NORMAL] = "warning.xpm"
10764 }
10765
10766 style "scale"
10767 {
10768   #Sets the foreground color (font color) to red when in the "NORMAL"
10769   #state.
10770   
10771   fg[NORMAL] = { 1.0, 0, 0 }
10772   
10773   #Sets the background pixmap of this widget to that of its parent.
10774   bg_pixmap[NORMAL] = "<parent>"
10775 }
10776
10777 style "button"
10778 {
10779   # This shows all the possible states for a button.  The only one that
10780   # doesn't apply is the SELECTED state.
10781   
10782   fg[PRELIGHT] = { 0, 1.0, 1.0 }
10783   bg[PRELIGHT] = { 0, 0, 1.0 }
10784   bg[ACTIVE] = { 1.0, 0, 0 }
10785   fg[ACTIVE] = { 0, 1.0, 0 }
10786   bg[NORMAL] = { 1.0, 1.0, 0 }
10787   fg[NORMAL] = { .99, 0, .99 }
10788   bg[INSENSITIVE] = { 1.0, 1.0, 1.0 }
10789   fg[INSENSITIVE] = { 1.0, 0, 1.0 }
10790 }
10791
10792 # In this example, we inherit the attributes of the "button" style and then
10793 # override the font and background color when prelit to create a new
10794 # "main_button" style.
10795
10796 style "main_button" = "button"
10797 {
10798   font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"
10799   bg[PRELIGHT] = { 0.75, 0, 0 }
10800 }
10801
10802 style "toggle_button" = "button"
10803 {
10804   fg[NORMAL] = { 1.0, 0, 0 }
10805   fg[ACTIVE] = { 1.0, 0, 0 }
10806   
10807   # This sets the background pixmap of the toggle_button to that of its
10808   # parent widget (as defined in the application).
10809   bg_pixmap[NORMAL] = "<parent>"
10810 }
10811
10812 style "text"
10813 {
10814   bg_pixmap[NORMAL] = "marble.xpm"
10815   fg[NORMAL] = { 1.0, 1.0, 1.0 }
10816 }
10817
10818 style "ruler"
10819 {
10820   font = "-adobe-helvetica-medium-r-normal--*-80-*-*-*-*-*-*"
10821 }
10822
10823 # pixmap_path "~/.pixmaps"
10824
10825 # These set the widget types to use the styles defined above.
10826 # The widget types are listed in the class hierarchy, but could probably be
10827 # just listed in this document for the users reference.
10828
10829 widget_class "GtkWindow" style "window"
10830 widget_class "GtkDialog" style "window"
10831 widget_class "GtkFileSelection" style "window"
10832 widget_class "*Gtk*Scale" style "scale"
10833 widget_class "*GtkCheckButton*" style "toggle_button"
10834 widget_class "*GtkRadioButton*" style "toggle_button"
10835 widget_class "*GtkButton*" style "button"
10836 widget_class "*Ruler" style "ruler"
10837 widget_class "*GtkText" style "text"
10838
10839 # This sets all the buttons that are children of the "main window" to
10840 # the main_button style.  These must be documented to be taken advantage of.
10841 widget "main window.*GtkButton*" style "main_button"
10842 </verb></tscreen>
10843
10844 <!-- ***************************************************************** -->
10845 <sect>Writing Your Own Widgets 
10846 <!-- ***************************************************************** -->
10847
10848 <!-- ----------------------------------------------------------------- -->
10849 <sect1> Overview
10850 <p>
10851 Although the GTK distribution comes with many types of widgets that
10852 should cover most basic needs, there may come a time when you need to
10853 create your own new widget type. Since GTK uses widget inheritance
10854 extensively, and there is already a widget that is close to what you want,
10855 it is often possible to make a useful new widget type in
10856 just a few lines of code. But before starting work on a new widget, check
10857 around first to make sure that someone has not already written
10858 it. This will prevent duplication of effort and keep the number of
10859 GTK widgets out there to a minimum, which will help keep both the code
10860 and the interface of different applications consistent. As a flip side
10861 to this, once you finish your widget, announce it to the world so
10862 other people can benefit. The best place to do this is probably the
10863 <tt>gtk-list</tt>.
10864
10865 Complete sources for the example widgets are available at the place you 
10866 got this tutorial, or from:
10867
10868 <htmlurl url="http://www.gtk.org/~otaylor/gtk/tutorial/"
10869 name="http://www.gtk.org/~otaylor/gtk/tutorial/">
10870
10871
10872 <!-- ----------------------------------------------------------------- -->
10873 <sect1> The Anatomy Of A Widget
10874 <p>
10875 In order to create a new widget, it is important to have an
10876 understanding of how GTK objects work. This section is just meant as a
10877 brief overview. See the reference documentation for the details. 
10878
10879 GTK widgets are implemented in an object oriented fashion. However,
10880 they are implemented in standard C. This greatly improves portability
10881 and stability over using current generation C++ compilers; however,
10882 it does mean that the widget writer has to pay attention to some of
10883 the implementation details. The information common to all instances of
10884 one class of widgets (e.g., to all Button widgets) is stored in the 
10885 <em>class structure</em>. There is only one copy of this in
10886 which is stored information about the class's signals
10887 (which act like virtual functions in C). To support inheritance, the
10888 first field in the class structure must be a copy of the parent's
10889 class structure. The declaration of the class structure of GtkButtton
10890 looks like:
10891
10892 <tscreen><verb>
10893 struct _GtkButtonClass
10894 {
10895   GtkContainerClass parent_class;
10896
10897   void (* pressed)  (GtkButton *button);
10898   void (* released) (GtkButton *button);
10899   void (* clicked)  (GtkButton *button);
10900   void (* enter)    (GtkButton *button);
10901   void (* leave)    (GtkButton *button);
10902 };
10903 </verb></tscreen>
10904
10905 When a button is treated as a container (for instance, when it is
10906 resized), its class structure can be cast to GtkContainerClass, and
10907 the relevant fields used to handle the signals.
10908
10909 There is also a structure for each widget that is created on a
10910 per-instance basis. This structure has fields to store information that
10911 is different for each instance of the widget. We'll call this
10912 structure the <em>object structure</em>. For the Button class, it looks
10913 like:
10914
10915 <tscreen><verb>
10916 struct _GtkButton
10917 {
10918   GtkContainer container;
10919
10920   GtkWidget *child;
10921
10922   guint in_button : 1;
10923   guint button_down : 1;
10924 };
10925 </verb></tscreen>
10926
10927 Note that, similar to the class structure, the first field is the
10928 object structure of the parent class, so that this structure can be
10929 cast to the parent class's object structure as needed.
10930
10931 <!-- ----------------------------------------------------------------- -->
10932 <sect1> Creating a Composite widget
10933
10934 <!-- ----------------------------------------------------------------- -->
10935 <sect2> Introduction
10936 <p>
10937 One type of widget that you may be interested in creating is a
10938 widget that is merely an aggregate of other GTK widgets. This type of
10939 widget does nothing that couldn't be done without creating new
10940 widgets, but provides a convenient way of packaging user interface
10941 elements for reuse. The FileSelection and ColorSelection widgets in
10942 the standard distribution are examples of this type of widget.
10943
10944 The example widget that we'll create in this section is the Tictactoe
10945 widget, a 3x3 array of toggle buttons which triggers a signal when all
10946 three buttons in a row, column, or on one of the diagonals are
10947 depressed. 
10948
10949 <!-- ----------------------------------------------------------------- -->
10950 <sect2> Choosing a parent class
10951 <p>
10952 The parent class for a composite widget is typically the container
10953 class that holds all of the elements of the composite widget. For
10954 example, the parent class of the FileSelection widget is the
10955 Dialog class. Since our buttons will be arranged in a table, it
10956 might seem natural to make our parent class the GtkTable
10957 class. Unfortunately, this turns out not to work. The creation of a
10958 widget is divided among two functions - a <tt/WIDGETNAME_new()/
10959 function that the user calls, and a <tt/WIDGETNAME_init()/ function
10960 which does the basic work of initializing the widget which is
10961 independent of the arguments passed to the <tt/_new()/
10962 function. Descendent widgets only call the <tt/_init/ function of
10963 their parent widget. But this division of labor doesn't work well for
10964 tables, which when created, need to know the number of rows and
10965 columns in the table. Unless we want to duplicate most of the
10966 functionality of <tt/gtk_table_new()/ in our Tictactoe widget, we had
10967 best avoid deriving it from GtkTable. For that reason, we derive it
10968 from GtkVBox instead, and stick our table inside the VBox.
10969
10970 <!-- ----------------------------------------------------------------- -->
10971 <sect2> The header file
10972 <p>
10973 Each widget class has a header file which declares the object and
10974 class structures for that widget, along with public functions. 
10975 A couple of features are worth pointing out. To prevent duplicate
10976 definitions, we wrap the entire header file in:
10977
10978 <tscreen><verb>
10979 #ifndef __TICTACTOE_H__
10980 #define __TICTACTOE_H__
10981 .
10982 .
10983 .
10984 #endif /* __TICTACTOE_H__ */
10985 </verb></tscreen>
10986
10987 And to keep C++ programs that include the header file happy, in:
10988
10989 <tscreen><verb>
10990 #ifdef __cplusplus
10991 extern "C" {
10992 #endif /* __cplusplus */
10993 .
10994 .
10995 .
10996 #ifdef __cplusplus
10997 }
10998 #endif /* __cplusplus */
10999 </verb></tscreen>
11000
11001 Along with the functions and structures, we declare three standard
11002 macros in our header file, <tt/TICTACTOE(obj)/,
11003 <tt/TICTACTOE_CLASS(klass)/, and <tt/IS_TICTACTOE(obj)/, which cast a
11004 pointer into a pointer to the object or class structure, and check
11005 if an object is a Tictactoe widget respectively.
11006
11007 Here is the complete header file:
11008
11009 <tscreen><verb>
11010 /* tictactoe.h */
11011
11012 #ifndef __TICTACTOE_H__
11013 #define __TICTACTOE_H__
11014
11015 #include <gdk/gdk.h>
11016 #include <gtk/gtkvbox.h>
11017
11018 #ifdef __cplusplus
11019 extern "C" {
11020 #endif /* __cplusplus */
11021
11022 #define TICTACTOE(obj)          GTK_CHECK_CAST (obj, tictactoe_get_type (), Tictactoe)
11023 #define TICTACTOE_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, tictactoe_get_type (), TictactoeClass)
11024 #define IS_TICTACTOE(obj)       GTK_CHECK_TYPE (obj, tictactoe_get_type ())
11025
11026
11027 typedef struct _Tictactoe       Tictactoe;
11028 typedef struct _TictactoeClass  TictactoeClass;
11029
11030 struct _Tictactoe
11031 {
11032   GtkVBox vbox;
11033   
11034   GtkWidget *buttons[3][3];
11035 };
11036
11037 struct _TictactoeClass
11038 {
11039   GtkVBoxClass parent_class;
11040
11041   void (* tictactoe) (Tictactoe *ttt);
11042 };
11043
11044 guint          tictactoe_get_type        (void);
11045 GtkWidget*     tictactoe_new             (void);
11046 void           tictactoe_clear           (Tictactoe *ttt);
11047
11048 #ifdef __cplusplus
11049 }
11050 #endif /* __cplusplus */
11051
11052 #endif /* __TICTACTOE_H__ */
11053
11054 </verb></tscreen>
11055
11056 <!-- ----------------------------------------------------------------- -->
11057 <sect2> The <tt/_get_type()/ function.
11058 <p>
11059 We now continue on to the implementation of our widget. A core
11060 function for every widget is the function
11061 <tt/WIDGETNAME_get_type()/. This function, when first called, tells
11062 GTK about the widget class, and gets an ID that uniquely identifies
11063 the widget class. Upon subsequent calls, it just returns the ID.
11064
11065 <tscreen><verb>
11066 guint
11067 tictactoe_get_type ()
11068 {
11069   static guint ttt_type = 0;
11070
11071   if (!ttt_type)
11072     {
11073       GtkTypeInfo ttt_info =
11074       {
11075         "Tictactoe",
11076         sizeof (Tictactoe),
11077         sizeof (TictactoeClass),
11078         (GtkClassInitFunc) tictactoe_class_init,
11079         (GtkObjectInitFunc) tictactoe_init,
11080         (GtkArgSetFunc) NULL,
11081         (GtkArgGetFunc) NULL
11082       };
11083
11084       ttt_type = gtk_type_unique (gtk_vbox_get_type (), &amp;ttt_info);
11085     }
11086
11087   return ttt_type;
11088 }
11089 </verb></tscreen>
11090
11091 The GtkTypeInfo structure has the following definition:
11092
11093 <tscreen><verb>
11094 struct _GtkTypeInfo
11095 {
11096   gchar *type_name;
11097   guint object_size;
11098   guint class_size;
11099   GtkClassInitFunc class_init_func;
11100   GtkObjectInitFunc object_init_func;
11101   GtkArgSetFunc arg_set_func;
11102   GtkArgGetFunc arg_get_func;
11103 };
11104 </verb></tscreen>
11105
11106 The fields of this structure are pretty self-explanatory. We'll ignore
11107 the <tt/arg_set_func/ and <tt/arg_get_func/ fields here: they have an important, 
11108 but as yet largely
11109 unimplemented, role in allowing widget options to be conveniently set
11110 from interpreted languages. Once GTK has a correctly filled in copy of
11111 this structure, it knows how to create objects of a particular widget
11112 type. 
11113
11114 <!-- ----------------------------------------------------------------- -->
11115 <sect2> The <tt/_class_init()/ function
11116 <p>
11117 The <tt/WIDGETNAME_class_init()/ function initializes the fields of
11118 the widget's class structure, and sets up any signals for the
11119 class. For our Tictactoe widget it looks like:
11120
11121 <tscreen><verb>
11122
11123 enum {
11124   TICTACTOE_SIGNAL,
11125   LAST_SIGNAL
11126 };
11127
11128 static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
11129
11130 static void
11131 tictactoe_class_init (TictactoeClass *class)
11132 {
11133   GtkObjectClass *object_class;
11134
11135   object_class = (GtkObjectClass*) class;
11136   
11137   tictactoe_signals[TICTACTOE_SIGNAL] = gtk_signal_new ("tictactoe",
11138                                          GTK_RUN_FIRST,
11139                                          object_class->type,
11140                                          GTK_SIGNAL_OFFSET (TictactoeClass, tictactoe),
11141                                          gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
11142
11143
11144   gtk_object_class_add_signals (object_class, tictactoe_signals, LAST_SIGNAL);
11145
11146   class->tictactoe = NULL;
11147 }
11148 </verb></tscreen>
11149
11150 Our widget has just one signal, the <tt/tictactoe/ signal that is
11151 invoked when a row, column, or diagonal is completely filled in. Not
11152 every composite widget needs signals, so if you are reading this for
11153 the first time, you may want to skip to the next section now, as
11154 things are going to get a bit complicated.
11155
11156 The function:
11157
11158 <tscreen><verb>
11159 gint gtk_signal_new( const gchar         *name,
11160                      GtkSignalRunType     run_type,
11161                      GtkType              object_type,
11162                      gint                 function_offset,
11163                      GtkSignalMarshaller  marshaller,
11164                      GtkType              return_val,
11165                      guint                nparams,
11166                      ...);
11167 </verb></tscreen>
11168
11169 Creates a new signal. The parameters are:
11170
11171 <itemize>
11172 <item> <tt/name/: The name of the signal.
11173 <item> <tt/run_type/: Whether the default handler runs before or after
11174 user handlers. Usually this will be <tt/GTK_RUN_FIRST/, or <tt/GTK_RUN_LAST/,
11175 although there are other possibilities.
11176 <item> <tt/object_type/: The ID of the object that this signal applies
11177 to. (It will also apply to that objects descendents)
11178 <item> <tt/function_offset/: The offset within the class structure of
11179 a pointer to the default handler.
11180 <item> <tt/marshaller/: A function that is used to invoke the signal
11181 handler. For signal handlers that have no arguments other than the
11182 object that emitted the signal and user data, we can use the
11183 pre-supplied marshaller function <tt/gtk_signal_default_marshaller/.
11184 <item> <tt/return_val/: The type of the return val.
11185 <item> <tt/nparams/: The number of parameters of the signal handler
11186 (other than the two default ones mentioned above)
11187 <item> <tt/.../: The types of the parameters.
11188 </itemize>
11189
11190 When specifying types, the <tt/GtkType/ enumeration is used:
11191
11192 <tscreen><verb>
11193 typedef enum
11194 {
11195   GTK_TYPE_INVALID,
11196   GTK_TYPE_NONE,
11197   GTK_TYPE_CHAR,
11198   GTK_TYPE_BOOL,
11199   GTK_TYPE_INT,
11200   GTK_TYPE_UINT,
11201   GTK_TYPE_LONG,
11202   GTK_TYPE_ULONG,
11203   GTK_TYPE_FLOAT,
11204   GTK_TYPE_DOUBLE,
11205   GTK_TYPE_STRING,
11206   GTK_TYPE_ENUM,
11207   GTK_TYPE_FLAGS,
11208   GTK_TYPE_BOXED,
11209   GTK_TYPE_FOREIGN,
11210   GTK_TYPE_CALLBACK,
11211   GTK_TYPE_ARGS,
11212
11213   GTK_TYPE_POINTER,
11214
11215   /* it'd be great if the next two could be removed eventually */
11216   GTK_TYPE_SIGNAL,
11217   GTK_TYPE_C_CALLBACK,
11218
11219   GTK_TYPE_OBJECT
11220
11221 } GtkFundamentalType;
11222 </verb></tscreen>
11223
11224 <tt/gtk_signal_new()/ returns a unique integer identifier for the
11225 signal, that we store in the <tt/tictactoe_signals/ array, which we
11226 index using an enumeration. (Conventionally, the enumeration elements
11227 are the signal name, uppercased, but here there would be a conflict
11228 with the <tt/TICTACTOE()/ macro, so we called it <tt/TICTACTOE_SIGNAL/
11229 instead.
11230
11231 After creating our signals, we need to tell GTK to associate our
11232 signals with the Tictactoe class. We do that by calling
11233 <tt/gtk_object_class_add_signals()/. We then set the pointer which
11234 points to the default handler for the `tictactoe' signal to NULL,
11235 indicating that there is no default action.
11236
11237 <!-- ----------------------------------------------------------------- -->
11238 <sect2> The <tt/_init()/ function.
11239 <p>
11240 Each widget class also needs a function to initialize the object
11241 structure. Usually, this function has the fairly limited role of
11242 setting the fields of the structure to default values. For composite
11243 widgets, however, this function also creates the component widgets.
11244
11245 <tscreen><verb>
11246 static void
11247 tictactoe_init (Tictactoe *ttt)
11248 {
11249   GtkWidget *table;
11250   gint i,j;
11251   
11252   table = gtk_table_new (3, 3, TRUE);
11253   gtk_container_add (GTK_CONTAINER(ttt), table);
11254   gtk_widget_show (table);
11255
11256   for (i=0;i<3; i++)
11257     for (j=0;j<3; j++)
11258       {
11259         ttt->buttons[i][j] = gtk_toggle_button_new ();
11260         gtk_table_attach_defaults (GTK_TABLE(table), ttt->buttons[i][j], 
11261                                    i, i+1, j, j+1);
11262         gtk_signal_connect (GTK_OBJECT (ttt->buttons[i][j]), "toggled",
11263                             GTK_SIGNAL_FUNC (tictactoe_toggle), ttt);
11264         gtk_widget_set_usize (ttt->buttons[i][j], 20, 20);
11265         gtk_widget_show (ttt->buttons[i][j]);
11266       }
11267 }
11268 </verb></tscreen>
11269
11270 <!-- ----------------------------------------------------------------- -->
11271 <sect2> And the rest...
11272 <p>
11273 There is one more function that every widget (except for base widget
11274 types like GtkBin that cannot be instantiated) needs to have - the
11275 function that the user calls to create an object of that type. This is
11276 conventionally called <tt/WIDGETNAME_new()/. In some
11277 widgets, though not for the Tictactoe widgets, this function takes
11278 arguments, and does some setup based on the arguments. The other two
11279 functions are specific to the Tictactoe widget. 
11280
11281 <tt/tictactoe_clear()/ is a public function that resets all the
11282 buttons in the widget to the up position. Note the use of
11283 <tt/gtk_signal_handler_block_by_data()/ to keep our signal handler for
11284 button toggles from being triggered unnecessarily.
11285
11286 <tt/tictactoe_toggle()/ is the signal handler that is invoked when the
11287 user clicks on a button. It checks to see if there are any winning
11288 combinations that involve the toggled button, and if so, emits
11289 the "tictactoe" signal.
11290
11291 <tscreen><verb>  
11292 GtkWidget*
11293 tictactoe_new ()
11294 {
11295   return GTK_WIDGET ( gtk_type_new (tictactoe_get_type ()));
11296 }
11297
11298 void           
11299 tictactoe_clear (Tictactoe *ttt)
11300 {
11301   int i,j;
11302
11303   for (i=0;i<3;i++)
11304     for (j=0;j<3;j++)
11305       {
11306         gtk_signal_handler_block_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
11307         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
11308                                      FALSE);
11309         gtk_signal_handler_unblock_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
11310       }
11311 }
11312
11313 static void
11314 tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
11315 {
11316   int i,k;
11317
11318   static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
11319                              { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
11320                              { 0, 1, 2 }, { 0, 1, 2 } };
11321   static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
11322                              { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
11323                              { 0, 1, 2 }, { 2, 1, 0 } };
11324
11325   int success, found;
11326
11327   for (k=0; k<8; k++)
11328     {
11329       success = TRUE;
11330       found = FALSE;
11331
11332       for (i=0;i<3;i++)
11333         {
11334           success = success &amp;&amp; 
11335             GTK_TOGGLE_BUTTON(ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
11336           found = found ||
11337             ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
11338         }
11339       
11340       if (success &amp;&amp; found)
11341         {
11342           gtk_signal_emit (GTK_OBJECT (ttt), 
11343                            tictactoe_signals[TICTACTOE_SIGNAL]);
11344           break;
11345         }
11346     }
11347 }
11348 </verb></tscreen>
11349
11350 And finally, an example program using our Tictactoe widget:
11351
11352 <tscreen><verb>
11353 #include <gtk/gtk.h>
11354 #include "tictactoe.h"
11355
11356 /* Invoked when a row, column or diagonal is completed */
11357 void
11358 win (GtkWidget *widget, gpointer data)
11359 {
11360   g_print ("Yay!\n");
11361   tictactoe_clear (TICTACTOE (widget));
11362 }
11363
11364 int 
11365 main (int argc, char *argv[])
11366 {
11367   GtkWidget *window;
11368   GtkWidget *ttt;
11369   
11370   gtk_init (&amp;argc, &amp;argv);
11371
11372   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
11373   
11374   gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
11375   
11376   gtk_signal_connect (GTK_OBJECT (window), "destroy",
11377                       GTK_SIGNAL_FUNC (gtk_exit), NULL);
11378   
11379   gtk_container_border_width (GTK_CONTAINER (window), 10);
11380
11381   /* Create a new Tictactoe widget */
11382   ttt = tictactoe_new ();
11383   gtk_container_add (GTK_CONTAINER (window), ttt);
11384   gtk_widget_show (ttt);
11385
11386   /* And attach to its "tictactoe" signal */
11387   gtk_signal_connect (GTK_OBJECT (ttt), "tictactoe",
11388                       GTK_SIGNAL_FUNC (win), NULL);
11389
11390   gtk_widget_show (window);
11391   
11392   gtk_main ();
11393   
11394   return 0;
11395 }
11396
11397 </verb></tscreen>
11398
11399 <!-- ----------------------------------------------------------------- -->
11400 <sect1> Creating a widget from scratch.
11401
11402 <!-- ----------------------------------------------------------------- -->
11403 <sect2> Introduction
11404 <p>
11405 In this section, we'll learn more about how widgets display themselves
11406 on the screen and interact with events. As an example of this, we'll
11407 create an analog dial widget with a pointer that the user can drag to
11408 set the value.
11409
11410 <!-- ----------------------------------------------------------------- -->
11411 <sect2> Displaying a widget on the screen
11412 <p>
11413 There are several steps that are involved in displaying on the screen.
11414 After the widget is created with a call to <tt/WIDGETNAME_new()/,
11415 several more functions are needed:
11416
11417 <itemize>
11418 <item> <tt/WIDGETNAME_realize()/ is responsible for creating an X
11419 window for the widget if it has one.
11420 <item> <tt/WIDGETNAME_map()/ is invoked after the user calls
11421 <tt/gtk_widget_show()/. It is responsible for making sure the widget
11422 is actually drawn on the screen (<em/mapped/). For a container class,
11423 it must also make calls to <tt/map()/> functions of any child widgets.
11424 <item> <tt/WIDGETNAME_draw()/ is invoked when <tt/gtk_widget_draw()/
11425 is called for the widget or one of its ancestors. It makes the actual
11426 calls to the drawing functions to draw the widget on the screen. For
11427 container widgets, this function must make calls to
11428 <tt/gtk_widget_draw()/ for its child widgets.
11429 <item> <tt/WIDGETNAME_expose()/ is a handler for expose events for the
11430 widget. It makes the necessary calls to the drawing functions to draw
11431 the exposed portion on the screen. For container widgets, this
11432 function must generate expose events for its child widgets which don't
11433 have their own windows. (If they have their own windows, then X will
11434 generate the necessary expose events)
11435 </itemize>
11436
11437 You might notice that the last two functions are quite similar - each
11438 is responsible for drawing the widget on the screen. In fact many
11439 types of widgets don't really care about the difference between the
11440 two. The default <tt/draw()/ function in the widget class simply
11441 generates a synthetic expose event for the redrawn area. However, some
11442 types of widgets can save work by distinguishing between the two
11443 functions. For instance, if a widget has multiple X windows, then
11444 since expose events identify the exposed window, it can redraw only
11445 the affected window, which is not possible for calls to <tt/draw()/.
11446
11447 Container widgets, even if they don't care about the difference for
11448 themselves, can't simply use the default <tt/draw()/ function because
11449 their child widgets might care about the difference. However,
11450 it would be wasteful to duplicate the drawing code between the two
11451 functions. The convention is that such widgets have a function called
11452 <tt/WIDGETNAME_paint()/ that does the actual work of drawing the
11453 widget, that is then called by the <tt/draw()/ and <tt/expose()/
11454 functions.
11455
11456 In our example approach, since the dial widget is not a container
11457 widget, and only has a single window, we can take the simplest
11458 approach and use the default <tt/draw()/ function and only implement
11459 an <tt/expose()/ function.
11460
11461 <!-- ----------------------------------------------------------------- -->
11462 <sect2> The origins of the Dial Widget
11463 <p>
11464 Just as all land animals are just variants on the first amphibian that
11465 crawled up out of the mud, Gtk widgets tend to start off as variants
11466 of some other, previously written widget.  Thus, although this section
11467 is entitled `Creating a Widget from Scratch', the Dial widget really
11468 began with the source code for the Range widget. This was picked as a
11469 starting point because it would be nice if our Dial had the same
11470 interface as the Scale widgets which are just specialized descendents
11471 of the Range widget. So, though the source code is presented below in
11472 finished form, it should not be implied that it was written, <em>deus
11473 ex machina</em> in this fashion. Also, if you aren't yet familiar with
11474 how scale widgets work from the application writer's point of view, it
11475 would be a good idea to look them over before continuing.
11476
11477 <!-- ----------------------------------------------------------------- -->
11478 <sect2> The Basics
11479 <p>
11480 Quite a bit of our widget should look pretty familiar from the
11481 Tictactoe widget. First, we have a header file:
11482
11483 <tscreen><verb>
11484 /* GTK - The GIMP Toolkit
11485  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
11486  *
11487  * This library is free software; you can redistribute it and/or
11488  * modify it under the terms of the GNU Library General Public
11489  * License as published by the Free Software Foundation; either
11490  * version 2 of the License, or (at your option) any later version.
11491  *
11492  * This library is distributed in the hope that it will be useful,
11493  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11494  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11495  * Library General Public License for more details.
11496  *
11497  * You should have received a copy of the GNU Library General Public
11498  * License along with this library; if not, write to the Free
11499  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
11500  */
11501
11502 #ifndef __GTK_DIAL_H__
11503 #define __GTK_DIAL_H__
11504
11505 #include <gdk/gdk.h>
11506 #include <gtk/gtkadjustment.h>
11507 #include <gtk/gtkwidget.h>
11508
11509
11510 #ifdef __cplusplus
11511 extern "C" {
11512 #endif /* __cplusplus */
11513
11514
11515 #define GTK_DIAL(obj)          GTK_CHECK_CAST (obj, gtk_dial_get_type (), GtkDial)
11516 #define GTK_DIAL_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, gtk_dial_get_type (), GtkDialClass)
11517 #define GTK_IS_DIAL(obj)       GTK_CHECK_TYPE (obj, gtk_dial_get_type ())
11518
11519
11520 typedef struct _GtkDial        GtkDial;
11521 typedef struct _GtkDialClass   GtkDialClass;
11522
11523 struct _GtkDial
11524 {
11525   GtkWidget widget;
11526
11527   /* update policy (GTK_UPDATE_[CONTINUOUS/DELAYED/DISCONTINUOUS]) */
11528   guint policy : 2;
11529
11530   /* Button currently pressed or 0 if none */
11531   guint8 button;
11532
11533   /* Dimensions of dial components */
11534   gint radius;
11535   gint pointer_width;
11536
11537   /* ID of update timer, or 0 if none */
11538   guint32 timer;
11539
11540   /* Current angle */
11541   gfloat angle;
11542
11543   /* Old values from adjustment stored so we know when something changes */
11544   gfloat old_value;
11545   gfloat old_lower;
11546   gfloat old_upper;
11547
11548   /* The adjustment object that stores the data for this dial */
11549   GtkAdjustment *adjustment;
11550 };
11551
11552 struct _GtkDialClass
11553 {
11554   GtkWidgetClass parent_class;
11555 };
11556
11557
11558 GtkWidget*     gtk_dial_new                    (GtkAdjustment *adjustment);
11559 guint          gtk_dial_get_type               (void);
11560 GtkAdjustment* gtk_dial_get_adjustment         (GtkDial      *dial);
11561 void           gtk_dial_set_update_policy      (GtkDial      *dial,
11562                                                 GtkUpdateType  policy);
11563
11564 void           gtk_dial_set_adjustment         (GtkDial      *dial,
11565                                                 GtkAdjustment *adjustment);
11566 #ifdef __cplusplus
11567 }
11568 #endif /* __cplusplus */
11569
11570
11571 #endif /* __GTK_DIAL_H__ */
11572 </verb></tscreen>
11573
11574 Since there is quite a bit more going on in this widget, than the last
11575 one, we have more fields in the data structure, but otherwise things
11576 are pretty similar.
11577
11578 Next, after including header files, and declaring a few constants,
11579 we have some functions to provide information about the widget
11580 and initialize it:
11581
11582 <tscreen><verb>
11583 #include <math.h>
11584 #include <stdio.h>
11585 #include <gtk/gtkmain.h>
11586 #include <gtk/gtksignal.h>
11587
11588 #include "gtkdial.h"
11589
11590 #define SCROLL_DELAY_LENGTH  300
11591 #define DIAL_DEFAULT_SIZE 100
11592
11593 /* Forward declarations */
11594
11595 [ omitted to save space ]
11596
11597 /* Local data */
11598
11599 static GtkWidgetClass *parent_class = NULL;
11600
11601 guint
11602 gtk_dial_get_type ()
11603 {
11604   static guint dial_type = 0;
11605
11606   if (!dial_type)
11607     {
11608       GtkTypeInfo dial_info =
11609       {
11610         "GtkDial",
11611         sizeof (GtkDial),
11612         sizeof (GtkDialClass),
11613         (GtkClassInitFunc) gtk_dial_class_init,
11614         (GtkObjectInitFunc) gtk_dial_init,
11615         (GtkArgSetFunc) NULL,
11616         (GtkArgGetFunc) NULL,
11617       };
11618
11619       dial_type = gtk_type_unique (gtk_widget_get_type (), &amp;dial_info);
11620     }
11621
11622   return dial_type;
11623 }
11624
11625 static void
11626 gtk_dial_class_init (GtkDialClass *class)
11627 {
11628   GtkObjectClass *object_class;
11629   GtkWidgetClass *widget_class;
11630
11631   object_class = (GtkObjectClass*) class;
11632   widget_class = (GtkWidgetClass*) class;
11633
11634   parent_class = gtk_type_class (gtk_widget_get_type ());
11635
11636   object_class->destroy = gtk_dial_destroy;
11637
11638   widget_class->realize = gtk_dial_realize;
11639   widget_class->expose_event = gtk_dial_expose;
11640   widget_class->size_request = gtk_dial_size_request;
11641   widget_class->size_allocate = gtk_dial_size_allocate;
11642   widget_class->button_press_event = gtk_dial_button_press;
11643   widget_class->button_release_event = gtk_dial_button_release;
11644   widget_class->motion_notify_event = gtk_dial_motion_notify;
11645 }
11646
11647 static void
11648 gtk_dial_init (GtkDial *dial)
11649 {
11650   dial->button = 0;
11651   dial->policy = GTK_UPDATE_CONTINUOUS;
11652   dial->timer = 0;
11653   dial->radius = 0;
11654   dial->pointer_width = 0;
11655   dial->angle = 0.0;
11656   dial->old_value = 0.0;
11657   dial->old_lower = 0.0;
11658   dial->old_upper = 0.0;
11659   dial->adjustment = NULL;
11660 }
11661
11662 GtkWidget*
11663 gtk_dial_new (GtkAdjustment *adjustment)
11664 {
11665   GtkDial *dial;
11666
11667   dial = gtk_type_new (gtk_dial_get_type ());
11668
11669   if (!adjustment)
11670     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
11671
11672   gtk_dial_set_adjustment (dial, adjustment);
11673
11674   return GTK_WIDGET (dial);
11675 }
11676
11677 static void
11678 gtk_dial_destroy (GtkObject *object)
11679 {
11680   GtkDial *dial;
11681
11682   g_return_if_fail (object != NULL);
11683   g_return_if_fail (GTK_IS_DIAL (object));
11684
11685   dial = GTK_DIAL (object);
11686
11687   if (dial->adjustment)
11688     gtk_object_unref (GTK_OBJECT (dial->adjustment));
11689
11690   if (GTK_OBJECT_CLASS (parent_class)->destroy)
11691     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
11692 }
11693 </verb></tscreen>
11694
11695 Note that this <tt/init()/ function does less than for the Tictactoe
11696 widget, since this is not a composite widget, and the <tt/new()/
11697 function does more, since it now has an argument. Also, note that when
11698 we store a pointer to the Adjustment object, we increment its
11699 reference count, (and correspondingly decrement when we no longer use
11700 it) so that GTK can keep track of when it can be safely destroyed.
11701
11702 <p>
11703 Also, there are a few function to manipulate the widget's options:
11704
11705 <tscreen><verb>
11706 GtkAdjustment*
11707 gtk_dial_get_adjustment (GtkDial *dial)
11708 {
11709   g_return_val_if_fail (dial != NULL, NULL);
11710   g_return_val_if_fail (GTK_IS_DIAL (dial), NULL);
11711
11712   return dial->adjustment;
11713 }
11714
11715 void
11716 gtk_dial_set_update_policy (GtkDial      *dial,
11717                              GtkUpdateType  policy)
11718 {
11719   g_return_if_fail (dial != NULL);
11720   g_return_if_fail (GTK_IS_DIAL (dial));
11721
11722   dial->policy = policy;
11723 }
11724
11725 void
11726 gtk_dial_set_adjustment (GtkDial      *dial,
11727                           GtkAdjustment *adjustment)
11728 {
11729   g_return_if_fail (dial != NULL);
11730   g_return_if_fail (GTK_IS_DIAL (dial));
11731
11732   if (dial->adjustment)
11733     {
11734       gtk_signal_disconnect_by_data (GTK_OBJECT (dial->adjustment), (gpointer) dial);
11735       gtk_object_unref (GTK_OBJECT (dial->adjustment));
11736     }
11737
11738   dial->adjustment = adjustment;
11739   gtk_object_ref (GTK_OBJECT (dial->adjustment));
11740
11741   gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
11742                       (GtkSignalFunc) gtk_dial_adjustment_changed,
11743                       (gpointer) dial);
11744   gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
11745                       (GtkSignalFunc) gtk_dial_adjustment_value_changed,
11746                       (gpointer) dial);
11747
11748   dial->old_value = adjustment->value;
11749   dial->old_lower = adjustment->lower;
11750   dial->old_upper = adjustment->upper;
11751
11752   gtk_dial_update (dial);
11753 }
11754 </verb></tscreen>
11755
11756 <sect2> <tt/gtk_dial_realize()/
11757
11758 <p>
11759 Now we come to some new types of functions. First, we have a function
11760 that does the work of creating the X window. Notice that a mask is
11761 passed to the function <tt/gdk_window_new()/ which specifies which fields of
11762 the GdkWindowAttr structure actually have data in them (the remaining
11763 fields will be given default values). Also worth noting is the way the
11764 event mask of the widget is created. We call
11765 <tt/gtk_widget_get_events()/ to retrieve the event mask that the user
11766 has specified for this widget (with <tt/gtk_widget_set_events()/, and
11767 add the events that we are interested in ourselves.
11768
11769 <p>
11770 After creating the window, we set its style and background, and put a
11771 pointer to the widget in the user data field of the GdkWindow. This
11772 last step allows GTK to dispatch events for this window to the correct
11773 widget.
11774
11775 <tscreen><verb>
11776 static void
11777 gtk_dial_realize (GtkWidget *widget)
11778 {
11779   GtkDial *dial;
11780   GdkWindowAttr attributes;
11781   gint attributes_mask;
11782
11783   g_return_if_fail (widget != NULL);
11784   g_return_if_fail (GTK_IS_DIAL (widget));
11785
11786   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
11787   dial = GTK_DIAL (widget);
11788
11789   attributes.x = widget->allocation.x;
11790   attributes.y = widget->allocation.y;
11791   attributes.width = widget->allocation.width;
11792   attributes.height = widget->allocation.height;
11793   attributes.wclass = GDK_INPUT_OUTPUT;
11794   attributes.window_type = GDK_WINDOW_CHILD;
11795   attributes.event_mask = gtk_widget_get_events (widget) | 
11796     GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | 
11797     GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
11798     GDK_POINTER_MOTION_HINT_MASK;
11799   attributes.visual = gtk_widget_get_visual (widget);
11800   attributes.colormap = gtk_widget_get_colormap (widget);
11801
11802   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
11803   widget->window = gdk_window_new (widget->parent->window, &amp;attributes, attributes_mask);
11804
11805   widget->style = gtk_style_attach (widget->style, widget->window);
11806
11807   gdk_window_set_user_data (widget->window, widget);
11808
11809   gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
11810 }
11811 </verb></tscreen>
11812
11813 <sect2> Size negotiation
11814
11815 <p>
11816 Before the first time that the window containing a widget is
11817 displayed, and whenever the layout of the window changes, GTK asks
11818 each child widget for its desired size. This request is handled by the
11819 function, <tt/gtk_dial_size_request()/. Since our widget isn't a
11820 container widget, and has no real constraints on its size, we just
11821 return a reasonable default value.
11822
11823 <tscreen><verb>
11824 static void 
11825 gtk_dial_size_request (GtkWidget      *widget,
11826                        GtkRequisition *requisition)
11827 {
11828   requisition->width = DIAL_DEFAULT_SIZE;
11829   requisition->height = DIAL_DEFAULT_SIZE;
11830 }
11831 </verb></tscreen>
11832
11833 <p>
11834 After all the widgets have requested an ideal size, the layout of the
11835 window is computed and each child widget is notified of its actual
11836 size. Usually, this will at least as large as the requested size, but
11837 if for instance, the user has resized the window, it may occasionally
11838 be smaller than the requested size. The size notification is handled
11839 by the function <tt/gtk_dial_size_allocate()/. Notice that as well as
11840 computing the sizes of some component pieces for future use, this
11841 routine also does the grunt work of moving the widgets X window into
11842 the new position and size.
11843
11844 <tscreen><verb>
11845 static void
11846 gtk_dial_size_allocate (GtkWidget     *widget,
11847                         GtkAllocation *allocation)
11848 {
11849   GtkDial *dial;
11850
11851   g_return_if_fail (widget != NULL);
11852   g_return_if_fail (GTK_IS_DIAL (widget));
11853   g_return_if_fail (allocation != NULL);
11854
11855   widget->allocation = *allocation;
11856   if (GTK_WIDGET_REALIZED (widget))
11857     {
11858       dial = GTK_DIAL (widget);
11859
11860       gdk_window_move_resize (widget->window,
11861                               allocation->x, allocation->y,
11862                               allocation->width, allocation->height);
11863
11864       dial->radius = MAX(allocation->width,allocation->height) * 0.45;
11865       dial->pointer_width = dial->radius / 5;
11866     }
11867 }
11868 </verb></tscreen>.
11869
11870 <!-- ----------------------------------------------------------------- -->
11871 <sect2> <tt/gtk_dial_expose()/
11872
11873 <p>
11874 As mentioned above, all the drawing of this widget is done in the
11875 handler for expose events. There's not much to remark on here except
11876 the use of the function <tt/gtk_draw_polygon/ to draw the pointer with
11877 three dimensional shading according to the colors stored in the
11878 widget's style.
11879
11880 <tscreen><verb>
11881 static gint
11882 gtk_dial_expose (GtkWidget      *widget,
11883                  GdkEventExpose *event)
11884 {
11885   GtkDial *dial;
11886   GdkPoint points[3];
11887   gdouble s,c;
11888   gdouble theta;
11889   gint xc, yc;
11890   gint tick_length;
11891   gint i;
11892
11893   g_return_val_if_fail (widget != NULL, FALSE);
11894   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
11895   g_return_val_if_fail (event != NULL, FALSE);
11896
11897   if (event->count > 0)
11898     return FALSE;
11899   
11900   dial = GTK_DIAL (widget);
11901
11902   gdk_window_clear_area (widget->window,
11903                          0, 0,
11904                          widget->allocation.width,
11905                          widget->allocation.height);
11906
11907   xc = widget->allocation.width/2;
11908   yc = widget->allocation.height/2;
11909
11910   /* Draw ticks */
11911
11912   for (i=0; i<25; i++)
11913     {
11914       theta = (i*M_PI/18. - M_PI/6.);
11915       s = sin(theta);
11916       c = cos(theta);
11917
11918       tick_length = (i%6 == 0) ? dial->pointer_width : dial->pointer_width/2;
11919       
11920       gdk_draw_line (widget->window,
11921                      widget->style->fg_gc[widget->state],
11922                      xc + c*(dial->radius - tick_length),
11923                      yc - s*(dial->radius - tick_length),
11924                      xc + c*dial->radius,
11925                      yc - s*dial->radius);
11926     }
11927
11928   /* Draw pointer */
11929
11930   s = sin(dial->angle);
11931   c = cos(dial->angle);
11932
11933
11934   points[0].x = xc + s*dial->pointer_width/2;
11935   points[0].y = yc + c*dial->pointer_width/2;
11936   points[1].x = xc + c*dial->radius;
11937   points[1].y = yc - s*dial->radius;
11938   points[2].x = xc - s*dial->pointer_width/2;
11939   points[2].y = yc - c*dial->pointer_width/2;
11940
11941   gtk_draw_polygon (widget->style,
11942                     widget->window,
11943                     GTK_STATE_NORMAL,
11944                     GTK_SHADOW_OUT,
11945                     points, 3,
11946                     TRUE);
11947   
11948   return FALSE;
11949 }
11950 </verb></tscreen>
11951
11952 <!-- ----------------------------------------------------------------- -->
11953 <sect2> Event handling
11954
11955 <p>
11956
11957 The rest of the widget's code handles various types of events, and
11958 isn't too different from what would be found in many GTK
11959 applications. Two types of events can occur - either the user can
11960 click on the widget with the mouse and drag to move the pointer, or
11961 the value of the Adjustment object can change due to some external
11962 circumstance. 
11963
11964 <p>
11965 When the user clicks on the widget, we check to see if the click was
11966 appropriately near the pointer, and if so, store then button that the
11967 user clicked with in the <tt/button/ field of the widget
11968 structure, and grab all mouse events with a call to
11969 <tt/gtk_grab_add()/. Subsequent motion of the mouse causes the
11970 value of the control to be recomputed (by the function
11971 <tt/gtk_dial_update_mouse/). Depending on the policy that has been
11972 set, "value_changed" events are either generated instantly
11973 (<tt/GTK_UPDATE_CONTINUOUS/), after a delay in a timer added with
11974 <tt/gtk_timeout_add()/ (<tt/GTK_UPDATE_DELAYED/), or only when the
11975 button is released (<tt/GTK_UPDATE_DISCONTINUOUS/).
11976
11977 <tscreen><verb>
11978 static gint
11979 gtk_dial_button_press (GtkWidget      *widget,
11980                        GdkEventButton *event)
11981 {
11982   GtkDial *dial;
11983   gint dx, dy;
11984   double s, c;
11985   double d_parallel;
11986   double d_perpendicular;
11987
11988   g_return_val_if_fail (widget != NULL, FALSE);
11989   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
11990   g_return_val_if_fail (event != NULL, FALSE);
11991
11992   dial = GTK_DIAL (widget);
11993
11994   /* Determine if button press was within pointer region - we 
11995      do this by computing the parallel and perpendicular distance of
11996      the point where the mouse was pressed from the line passing through
11997      the pointer */
11998   
11999   dx = event->x - widget->allocation.width / 2;
12000   dy = widget->allocation.height / 2 - event->y;
12001   
12002   s = sin(dial->angle);
12003   c = cos(dial->angle);
12004   
12005   d_parallel = s*dy + c*dx;
12006   d_perpendicular = fabs(s*dx - c*dy);
12007   
12008   if (!dial->button &&
12009       (d_perpendicular < dial->pointer_width/2) &&
12010       (d_parallel > - dial->pointer_width))
12011     {
12012       gtk_grab_add (widget);
12013
12014       dial->button = event->button;
12015
12016       gtk_dial_update_mouse (dial, event->x, event->y);
12017     }
12018
12019   return FALSE;
12020 }
12021
12022 static gint
12023 gtk_dial_button_release (GtkWidget      *widget,
12024                           GdkEventButton *event)
12025 {
12026   GtkDial *dial;
12027
12028   g_return_val_if_fail (widget != NULL, FALSE);
12029   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
12030   g_return_val_if_fail (event != NULL, FALSE);
12031
12032   dial = GTK_DIAL (widget);
12033
12034   if (dial->button == event->button)
12035     {
12036       gtk_grab_remove (widget);
12037
12038       dial->button = 0;
12039
12040       if (dial->policy == GTK_UPDATE_DELAYED)
12041         gtk_timeout_remove (dial->timer);
12042       
12043       if ((dial->policy != GTK_UPDATE_CONTINUOUS) &&
12044           (dial->old_value != dial->adjustment->value))
12045         gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
12046     }
12047
12048   return FALSE;
12049 }
12050
12051 static gint
12052 gtk_dial_motion_notify (GtkWidget      *widget,
12053                          GdkEventMotion *event)
12054 {
12055   GtkDial *dial;
12056   GdkModifierType mods;
12057   gint x, y, mask;
12058
12059   g_return_val_if_fail (widget != NULL, FALSE);
12060   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
12061   g_return_val_if_fail (event != NULL, FALSE);
12062
12063   dial = GTK_DIAL (widget);
12064
12065   if (dial->button != 0)
12066     {
12067       x = event->x;
12068       y = event->y;
12069
12070       if (event->is_hint || (event->window != widget->window))
12071         gdk_window_get_pointer (widget->window, &amp;x, &amp;y, &amp;mods);
12072
12073       switch (dial->button)
12074         {
12075         case 1:
12076           mask = GDK_BUTTON1_MASK;
12077           break;
12078         case 2:
12079           mask = GDK_BUTTON2_MASK;
12080           break;
12081         case 3:
12082           mask = GDK_BUTTON3_MASK;
12083           break;
12084         default:
12085           mask = 0;
12086           break;
12087         }
12088
12089       if (mods & mask)
12090         gtk_dial_update_mouse (dial, x,y);
12091     }
12092
12093   return FALSE;
12094 }
12095
12096 static gint
12097 gtk_dial_timer (GtkDial *dial)
12098 {
12099   g_return_val_if_fail (dial != NULL, FALSE);
12100   g_return_val_if_fail (GTK_IS_DIAL (dial), FALSE);
12101
12102   if (dial->policy == GTK_UPDATE_DELAYED)
12103     gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
12104
12105   return FALSE;
12106 }
12107
12108 static void
12109 gtk_dial_update_mouse (GtkDial *dial, gint x, gint y)
12110 {
12111   gint xc, yc;
12112   gfloat old_value;
12113
12114   g_return_if_fail (dial != NULL);
12115   g_return_if_fail (GTK_IS_DIAL (dial));
12116
12117   xc = GTK_WIDGET(dial)->allocation.width / 2;
12118   yc = GTK_WIDGET(dial)->allocation.height / 2;
12119
12120   old_value = dial->adjustment->value;
12121   dial->angle = atan2(yc-y, x-xc);
12122
12123   if (dial->angle < -M_PI/2.)
12124     dial->angle += 2*M_PI;
12125
12126   if (dial->angle < -M_PI/6)
12127     dial->angle = -M_PI/6;
12128
12129   if (dial->angle > 7.*M_PI/6.)
12130     dial->angle = 7.*M_PI/6.;
12131
12132   dial->adjustment->value = dial->adjustment->lower + (7.*M_PI/6 - dial->angle) *
12133     (dial->adjustment->upper - dial->adjustment->lower) / (4.*M_PI/3.);
12134
12135   if (dial->adjustment->value != old_value)
12136     {
12137       if (dial->policy == GTK_UPDATE_CONTINUOUS)
12138         {
12139           gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
12140         }
12141       else
12142         {
12143           gtk_widget_draw (GTK_WIDGET(dial), NULL);
12144
12145           if (dial->policy == GTK_UPDATE_DELAYED)
12146             {
12147               if (dial->timer)
12148                 gtk_timeout_remove (dial->timer);
12149
12150               dial->timer = gtk_timeout_add (SCROLL_DELAY_LENGTH,
12151                                              (GtkFunction) gtk_dial_timer,
12152                                              (gpointer) dial);
12153             }
12154         }
12155     }
12156 }
12157 </verb></tscreen>
12158
12159 <p>
12160 Changes to the Adjustment by external means are communicated to our
12161 widget by the `changed' and `value_changed' signals. The handlers
12162 for these functions call <tt/gtk_dial_update()/ to validate the
12163 arguments, compute the new pointer angle, and redraw the widget (by
12164 calling <tt/gtk_widget_draw()/).
12165
12166 <tscreen><verb>
12167 static void
12168 gtk_dial_update (GtkDial *dial)
12169 {
12170   gfloat new_value;
12171   
12172   g_return_if_fail (dial != NULL);
12173   g_return_if_fail (GTK_IS_DIAL (dial));
12174
12175   new_value = dial->adjustment->value;
12176   
12177   if (new_value < dial->adjustment->lower)
12178     new_value = dial->adjustment->lower;
12179
12180   if (new_value > dial->adjustment->upper)
12181     new_value = dial->adjustment->upper;
12182
12183   if (new_value != dial->adjustment->value)
12184     {
12185       dial->adjustment->value = new_value;
12186       gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
12187     }
12188
12189   dial->angle = 7.*M_PI/6. - (new_value - dial->adjustment->lower) * 4.*M_PI/3. /
12190     (dial->adjustment->upper - dial->adjustment->lower);
12191
12192   gtk_widget_draw (GTK_WIDGET(dial), NULL);
12193 }
12194
12195 static void
12196 gtk_dial_adjustment_changed (GtkAdjustment *adjustment,
12197                               gpointer       data)
12198 {
12199   GtkDial *dial;
12200
12201   g_return_if_fail (adjustment != NULL);
12202   g_return_if_fail (data != NULL);
12203
12204   dial = GTK_DIAL (data);
12205
12206   if ((dial->old_value != adjustment->value) ||
12207       (dial->old_lower != adjustment->lower) ||
12208       (dial->old_upper != adjustment->upper))
12209     {
12210       gtk_dial_update (dial);
12211
12212       dial->old_value = adjustment->value;
12213       dial->old_lower = adjustment->lower;
12214       dial->old_upper = adjustment->upper;
12215     }
12216 }
12217
12218 static void
12219 gtk_dial_adjustment_value_changed (GtkAdjustment *adjustment,
12220                                     gpointer       data)
12221 {
12222   GtkDial *dial;
12223
12224   g_return_if_fail (adjustment != NULL);
12225   g_return_if_fail (data != NULL);
12226
12227   dial = GTK_DIAL (data);
12228
12229   if (dial->old_value != adjustment->value)
12230     {
12231       gtk_dial_update (dial);
12232
12233       dial->old_value = adjustment->value;
12234     }
12235 }
12236 </verb></tscreen>
12237
12238 <!-- ----------------------------------------------------------------- -->
12239 <sect2> Possible Enhancements
12240 <p>
12241
12242 The Dial widget as we've described it so far runs about 670 lines of
12243 code. Although that might sound like a fair bit, we've really
12244 accomplished quite a bit with that much code, especially since much of
12245 that length is headers and boilerplate. However, there are quite a few
12246 more enhancements that could be made to this widget:
12247
12248 <itemize>
12249 <item> If you try this widget out, you'll find that there is some
12250 flashing as the pointer is dragged around. This is because the entire
12251 widget is erased every time the pointer is moved before being
12252 redrawn. Often, the best way to handle this problem is to draw to an
12253 offscreen pixmap, then copy the final results onto the screen in one
12254 step. (The ProgressBar widget draws itself in this fashion.)
12255
12256 <item> The user should be able to use the up and down arrow keys to
12257 increase and decrease the value.
12258
12259 <item> It would be nice if the widget had buttons to increase and
12260 decrease the value in small or large steps. Although it would be
12261 possible to use embedded Button widgets for this, we would also like
12262 the buttons to auto-repeat when held down, as the arrows on a
12263 scrollbar do. Most of the code to implement this type of behavior can
12264 be found in the GtkRange widget.
12265
12266 <item> The Dial widget could be made into a container widget with a
12267 single child widget positioned at the bottom between the buttons
12268 mentioned above. The user could then add their choice of a label or
12269 entry widget to display the current value of the dial.
12270
12271 </itemize>
12272
12273 <!-- ----------------------------------------------------------------- -->
12274 <sect1> Learning More
12275
12276 <p>
12277 Only a small part of the many details involved in creating widgets
12278 could be described above. If you want to write your own widgets, the
12279 best source of examples is the GTK source itself. Ask yourself some
12280 questions about the widget you want to write: is it a Container
12281 widget? does it have its own window? is it a modification of an
12282 existing widget? Then find a similar widget, and start making changes.
12283 Good luck!
12284
12285 <!-- ***************************************************************** -->
12286 <sect>Scribble, A Simple Example Drawing Program
12287 <!-- ***************************************************************** -->
12288
12289 <!-- ----------------------------------------------------------------- -->
12290 <sect1> Overview
12291
12292 <p>
12293 In this section, we will build a simple drawing program. In the
12294 process, we will examine how to handle mouse events, how to draw in a
12295 window, and how to do drawing better by using a backing pixmap. After
12296 creating the simple drawing program, we will extend it by adding
12297 support for XInput devices, such as drawing tablets. GTK provides
12298 support routines which makes getting extended information, such as
12299 pressure and tilt, from such devices quite easy.
12300
12301 <!-- ----------------------------------------------------------------- -->
12302 <sect1> Event Handling
12303
12304 <p>
12305 The GTK signals we have already discussed are for high-level actions,
12306 such as a menu item being selected. However, sometimes it is useful to
12307 learn about lower-level occurrences, such as the mouse being moved, or
12308 a key being pressed. There are also GTK signals corresponding to these
12309 low-level <em>events</em>. The handlers for these signals have an
12310 extra parameter which is a pointer to a structure containing
12311 information about the event. For instance, motion events handlers are
12312 passed a pointer to a GdkEventMotion structure which looks (in part)
12313 like:
12314
12315 <tscreen><verb>
12316 struct _GdkEventMotion
12317 {
12318   GdkEventType type;
12319   GdkWindow *window;
12320   guint32 time;
12321   gdouble x;
12322   gdouble y;
12323   ...
12324   guint state;
12325   ...
12326 };
12327 </verb></tscreen>
12328
12329 <tt/type/ will be set to the event type, in this case
12330 <tt/GDK_MOTION_NOTIFY/, window is the window in which the event
12331 occurred. <tt/x/ and <tt/y/ give the coordinates of the event,
12332 and <tt/state/ specifies the modifier state when the event
12333 occurred (that is, it specifies which modifier keys and mouse buttons
12334 were pressed.) It is the bitwise OR of some of the following:
12335
12336 <tscreen><verb>
12337 GDK_SHIFT_MASK  
12338 GDK_LOCK_MASK   
12339 GDK_CONTROL_MASK
12340 GDK_MOD1_MASK   
12341 GDK_MOD2_MASK   
12342 GDK_MOD3_MASK   
12343 GDK_MOD4_MASK   
12344 GDK_MOD5_MASK   
12345 GDK_BUTTON1_MASK
12346 GDK_BUTTON2_MASK
12347 GDK_BUTTON3_MASK
12348 GDK_BUTTON4_MASK
12349 GDK_BUTTON5_MASK
12350 </verb></tscreen>
12351
12352 <p>
12353 As for other signals, to determine what happens when an event occurs
12354 we call <tt>gtk_signal_connect()</tt>. But we also need let GTK
12355 know which events we want to be notified about. To do this, we call
12356 the function:
12357
12358 <tscreen><verb>
12359 void gtk_widget_set_events (GtkWidget *widget,
12360                             gint      events);
12361 </verb></tscreen>
12362
12363 The second field specifies the events we are interested in. It
12364 is the bitwise OR of constants that specify different types
12365 of events. For future reference the event types are:
12366
12367 <tscreen><verb>
12368 GDK_EXPOSURE_MASK
12369 GDK_POINTER_MOTION_MASK
12370 GDK_POINTER_MOTION_HINT_MASK
12371 GDK_BUTTON_MOTION_MASK     
12372 GDK_BUTTON1_MOTION_MASK    
12373 GDK_BUTTON2_MOTION_MASK    
12374 GDK_BUTTON3_MOTION_MASK    
12375 GDK_BUTTON_PRESS_MASK      
12376 GDK_BUTTON_RELEASE_MASK    
12377 GDK_KEY_PRESS_MASK         
12378 GDK_KEY_RELEASE_MASK       
12379 GDK_ENTER_NOTIFY_MASK      
12380 GDK_LEAVE_NOTIFY_MASK      
12381 GDK_FOCUS_CHANGE_MASK      
12382 GDK_STRUCTURE_MASK         
12383 GDK_PROPERTY_CHANGE_MASK   
12384 GDK_PROXIMITY_IN_MASK      
12385 GDK_PROXIMITY_OUT_MASK     
12386 </verb></tscreen>
12387
12388 There are a few subtle points that have to be observed when calling
12389 <tt/gtk_widget_set_events()/. First, it must be called before the X window
12390 for a GTK widget is created. In practical terms, this means you
12391 should call it immediately after creating the widget. Second, the
12392 widget must have an associated X window. For efficiency, many widget
12393 types do not have their own window, but draw in their parent's window.
12394 These widgets are:
12395
12396 <tscreen><verb>
12397 GtkAlignment
12398 GtkArrow
12399 GtkBin
12400 GtkBox
12401 GtkImage
12402 GtkItem
12403 GtkLabel
12404 GtkPixmap
12405 GtkScrolledWindow
12406 GtkSeparator
12407 GtkTable
12408 GtkAspectFrame
12409 GtkFrame
12410 GtkVBox
12411 GtkHBox
12412 GtkVSeparator
12413 GtkHSeparator
12414 </verb></tscreen>
12415
12416 To capture events for these widgets, you need to use an EventBox
12417 widget. See the section on the <ref id="sec_EventBox"
12418 name="EventBox"> widget for details.
12419
12420 <p>
12421 For our drawing program, we want to know when the mouse button is
12422 pressed and when the mouse is moved, so we specify
12423 <tt/GDK_POINTER_MOTION_MASK/ and <tt/GDK_BUTTON_PRESS_MASK/. We also
12424 want to know when we need to redraw our window, so we specify
12425 <tt/GDK_EXPOSURE_MASK/. Although we want to be notified via a
12426 Configure event when our window size changes, we don't have to specify
12427 the corresponding <tt/GDK_STRUCTURE_MASK/ flag, because it is
12428 automatically specified for all windows.
12429
12430 <p>
12431 It turns out, however, that there is a problem with just specifying
12432 <tt/GDK_POINTER_MOTION_MASK/. This will cause the server to add a new
12433 motion event to the event queue every time the user moves the mouse.
12434 Imagine that it takes us 0.1 seconds to handle a motion event, but the
12435 X server queues a new motion event every 0.05 seconds. We will soon
12436 get way behind the users drawing. If the user draws for 5 seconds,
12437 it will take us another 5 seconds to catch up after they release 
12438 the mouse button! What we would like is to only get one motion
12439 event for each event we process. The way to do this is to 
12440 specify <tt/GDK_POINTER_MOTION_HINT_MASK/. 
12441
12442 <p>
12443 When we specify <tt/GDK_POINTER_MOTION_HINT_MASK/, the server sends
12444 us a motion event the first time the pointer moves after entering
12445 our window, or after a button press or release event. Subsequent 
12446 motion events will be suppressed until we explicitly ask for
12447 the position of the pointer using the function:
12448
12449 <tscreen><verb>
12450 GdkWindow*    gdk_window_get_pointer     (GdkWindow       *window,
12451                                           gint            *x,
12452                                           gint            *y,
12453                                           GdkModifierType *mask);
12454 </verb></tscreen>
12455
12456 (There is another function, <tt>gtk_widget_get_pointer()</tt> which
12457 has a simpler interface, but turns out not to be very useful, since
12458 it only retrieves the position of the mouse, not whether the buttons
12459 are pressed.)
12460
12461 <p>
12462 The code to set the events for our window then looks like:
12463
12464 <tscreen><verb>
12465   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
12466                       (GtkSignalFunc) expose_event, NULL);
12467   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
12468                       (GtkSignalFunc) configure_event, NULL);
12469   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
12470                       (GtkSignalFunc) motion_notify_event, NULL);
12471   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
12472                       (GtkSignalFunc) button_press_event, NULL);
12473
12474   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
12475                          | GDK_LEAVE_NOTIFY_MASK
12476                          | GDK_BUTTON_PRESS_MASK
12477                          | GDK_POINTER_MOTION_MASK
12478                          | GDK_POINTER_MOTION_HINT_MASK);
12479 </verb></tscreen>
12480
12481 We'll save the "expose_event" and "configure_event" handlers for
12482 later. The "motion_notify_event" and "button_press_event" handlers
12483 pretty simple:
12484
12485 <tscreen><verb>
12486 static gint
12487 button_press_event (GtkWidget *widget, GdkEventButton *event)
12488 {
12489   if (event->button == 1 &amp;&amp; pixmap != NULL)
12490       draw_brush (widget, event->x, event->y);
12491
12492   return TRUE;
12493 }
12494
12495 static gint
12496 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
12497 {
12498   int x, y;
12499   GdkModifierType state;
12500
12501   if (event->is_hint)
12502     gdk_window_get_pointer (event->window, &amp;x, &amp;y, &amp;state);
12503   else
12504     {
12505       x = event->x;
12506       y = event->y;
12507       state = event->state;
12508     }
12509     
12510   if (state &amp; GDK_BUTTON1_MASK &amp;&amp; pixmap != NULL)
12511     draw_brush (widget, x, y);
12512   
12513   return TRUE;
12514 }
12515 </verb></tscreen>
12516
12517 <!-- ----------------------------------------------------------------- -->
12518 <sect1> The DrawingArea Widget, And Drawing
12519
12520 <p>
12521 We know turn to the process of drawing on the screen. The 
12522 widget we use for this is the DrawingArea widget. A drawing area
12523 widget is essentially an X window and nothing more. It is a blank
12524 canvas in which we can draw whatever we like. A drawing area
12525 is created using the call:
12526
12527 <tscreen><verb>
12528 GtkWidget* gtk_drawing_area_new        (void);
12529 </verb></tscreen>
12530
12531 A default size for the widget can be specified by calling:
12532
12533 <tscreen><verb>
12534 void       gtk_drawing_area_size       (GtkDrawingArea      *darea,
12535                                         gint                 width,
12536                                         gint                 height);
12537 </verb></tscreen>
12538
12539 This default size can be overridden, as is true for all widgets,
12540 by calling <tt>gtk_widget_set_usize()</tt>, and that, in turn, can
12541 be overridden if the user manually resizes the the window containing
12542 the drawing area.
12543
12544 <p>
12545 It should be noted that when we create a DrawingArea widget, we are,
12546 <em>completely</em> responsible for drawing the contents. If our
12547 window is obscured then uncovered, we get an exposure event and must
12548 redraw what was previously hidden.
12549
12550 <p>
12551 Having to remember everything that was drawn on the screen so we
12552 can properly redraw it can, to say the least, be a nuisance. In
12553 addition, it can be visually distracting if portions of the
12554 window are cleared, then redrawn step by step. The solution to
12555 this problem is to use an offscreen <em>backing pixmap</em>.
12556 Instead of drawing directly to the screen, we draw to an image
12557 stored in server memory but not displayed, then when the image
12558 changes or new portions of the image are displayed, we copy the
12559 relevant portions onto the screen.
12560
12561 <p>
12562 To create an offscreen pixmap, we call the function:
12563
12564 <tscreen><verb>
12565 GdkPixmap* gdk_pixmap_new               (GdkWindow  *window,
12566                                          gint        width,
12567                                          gint        height,
12568                                          gint        depth);
12569 </verb></tscreen>
12570
12571 The <tt>window</tt> parameter specifies a GDK window that this pixmap
12572 takes some of its properties from. <tt>width</tt> and <tt>height</tt>
12573 specify the size of the pixmap. <tt>depth</tt> specifies the <em>color
12574 depth</em>, that is the number of bits per pixel, for the new window.
12575 If the depth is specified as <tt>-1</tt>, it will match the depth
12576 of <tt>window</tt>.
12577
12578 <p>
12579 We create the pixmap in our "configure_event" handler. This event
12580 is generated whenever the window changes size, including when it
12581 is originally created.
12582
12583 <tscreen><verb>
12584 /* Backing pixmap for drawing area */
12585 static GdkPixmap *pixmap = NULL;
12586
12587 /* Create a new backing pixmap of the appropriate size */
12588 static gint
12589 configure_event (GtkWidget *widget, GdkEventConfigure *event)
12590 {
12591   if (pixmap)
12592     gdk_pixmap_unref(pixmap);
12593
12594   pixmap = gdk_pixmap_new(widget->window,
12595                           widget->allocation.width,
12596                           widget->allocation.height,
12597                           -1);
12598   gdk_draw_rectangle (pixmap,
12599                       widget->style->white_gc,
12600                       TRUE,
12601                       0, 0,
12602                       widget->allocation.width,
12603                       widget->allocation.height);
12604
12605   return TRUE;
12606 }
12607 </verb></tscreen>
12608
12609 The call to <tt>gdk_draw_rectangle()</tt> clears the pixmap
12610 initially to white. We'll say more about that in a moment.
12611
12612 <p>
12613 Our exposure event handler then simply copies the relevant portion
12614 of the pixmap onto the screen (we determine the area we need
12615 to redraw by using the event->area field of the exposure event):
12616
12617 <tscreen><verb>
12618 /* Redraw the screen from the backing pixmap */
12619 static gint
12620 expose_event (GtkWidget *widget, GdkEventExpose *event)
12621 {
12622   gdk_draw_pixmap(widget->window,
12623                   widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
12624                   pixmap,
12625                   event->area.x, event->area.y,
12626                   event->area.x, event->area.y,
12627                   event->area.width, event->area.height);
12628
12629   return FALSE;
12630 }
12631 </verb></tscreen>
12632
12633 We've now seen how to keep the screen up to date with our pixmap, but
12634 how do we actually draw interesting stuff on our pixmap?  There are a
12635 large number of calls in GTK's GDK library for drawing on
12636 <em>drawables</em>. A drawable is simply something that can be drawn
12637 upon. It can be a window, a pixmap, or a bitmap (a black and white
12638 image).  We've already seen two such calls above,
12639 <tt>gdk_draw_rectangle()</tt> and <tt>gdk_draw_pixmap()</tt>. The
12640 complete list is:
12641
12642 <tscreen><verb>
12643 gdk_draw_line ()
12644 gdk_draw_rectangle ()
12645 gdk_draw_arc ()
12646 gdk_draw_polygon ()
12647 gdk_draw_string ()
12648 gdk_draw_text ()
12649 gdk_draw_pixmap ()
12650 gdk_draw_bitmap ()
12651 gdk_draw_image ()
12652 gdk_draw_points ()
12653 gdk_draw_segments ()
12654 </verb></tscreen>
12655
12656 See the reference documentation or the header file
12657 <tt>&lt;gdk/gdk.h&gt;</tt> for further details on these functions.
12658 These functions all share the same first two arguments. The first
12659 argument is the drawable to draw upon, the second argument is a
12660 <em>graphics context</em> (GC). 
12661
12662 <p>
12663 A graphics context encapsulates information about things such as
12664 foreground and background color and line width. GDK has a full set of
12665 functions for creating and modifying graphics contexts, but to keep
12666 things simple we'll just use predefined graphics contexts. Each widget
12667 has an associated style. (Which can be modified in a gtkrc file, see
12668 the section GTK's rc file.) This, among other things, stores a number
12669 of graphics contexts. Some examples of accessing these graphics
12670 contexts are:
12671
12672 <tscreen><verb>
12673 widget->style->white_gc
12674 widget->style->black_gc
12675 widget->style->fg_gc[GTK_STATE_NORMAL]
12676 widget->style->bg_gc[GTK_WIDGET_STATE(widget)]
12677 </verb></tscreen>
12678
12679 The fields <tt>fg_gc</tt>, <tt>bg_gc</tt>, <tt>dark_gc</tt>, and
12680 <tt>light_gc</tt> are indexed by a parameter of type
12681 <tt>GtkStateType</tt> which can take on the values:
12682
12683 <tscreen><verb>
12684 GTK_STATE_NORMAL,
12685 GTK_STATE_ACTIVE,
12686 GTK_STATE_PRELIGHT,
12687 GTK_STATE_SELECTED,
12688 GTK_STATE_INSENSITIVE
12689 </verb></tscreen>
12690
12691 For instance, the for <tt/GTK_STATE_SELECTED/ the default foreground
12692 color is white and the default background color, dark blue.
12693
12694 <p>
12695 Our function <tt>draw_brush()</tt>, which does the actual drawing
12696 on the screen, is then:
12697
12698 <tscreen><verb>
12699 /* Draw a rectangle on the screen */
12700 static void
12701 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
12702 {
12703   GdkRectangle update_rect;
12704
12705   update_rect.x = x - 5;
12706   update_rect.y = y - 5;
12707   update_rect.width = 10;
12708   update_rect.height = 10;
12709   gdk_draw_rectangle (pixmap,
12710                       widget->style->black_gc,
12711                       TRUE,
12712                       update_rect.x, update_rect.y,
12713                       update_rect.width, update_rect.height);
12714   gtk_widget_draw (widget, &amp;update_rect);
12715 }
12716 </verb></tscreen>
12717
12718 After we draw the rectangle representing the brush onto the pixmap,
12719 we call the function:
12720
12721 <tscreen><verb>
12722 void       gtk_widget_draw                (GtkWidget           *widget,
12723                                            GdkRectangle        *area);
12724 </verb></tscreen>
12725
12726 which notifies X that the area given by the <tt>area</tt> parameter
12727 needs to be updated. X will eventually generate an expose event
12728 (possibly combining the areas passed in several calls to
12729 <tt>gtk_widget_draw()</tt>) which will cause our expose event handler
12730 to copy the relevant portions to the screen.
12731
12732 <p>
12733 We have now covered the entire drawing program except for a few
12734 mundane details like creating the main window. The complete
12735 source code is available from the location from which you got
12736 this tutorial, or from:
12737
12738 <htmlurl url="http://www.gtk.org/~otaylor/gtk/tutorial/"
12739 name="http://www.gtk.org/~otaylor/gtk/tutorial/">
12740
12741
12742 <!-- ----------------------------------------------------------------- -->
12743 <sect1> Adding XInput support
12744
12745 <p>
12746
12747 It is now possible to buy quite inexpensive input devices such 
12748 as drawing tablets, which allow drawing with a much greater
12749 ease of artistic expression than does a mouse. The simplest way
12750 to use such devices is simply as a replacement for the mouse,
12751 but that misses out many of the advantages of these devices,
12752 such as:
12753
12754 <itemize>
12755 <item> Pressure sensitivity
12756 <item> Tilt reporting
12757 <item> Sub-pixel positioning
12758 <item> Multiple inputs (for example, a stylus with a point and eraser)
12759 </itemize>
12760
12761 For information about the XInput extension, see the <htmlurl
12762 url="http://www.msc.cornell.edu/~otaylor/xinput/XInput-HOWTO.html"
12763 name="XInput-HOWTO">.
12764
12765 <p>
12766 If we examine the full definition of, for example, the GdkEventMotion
12767 structure, we see that it has fields to support extended device
12768 information.
12769
12770 <tscreen><verb>
12771 struct _GdkEventMotion
12772 {
12773   GdkEventType type;
12774   GdkWindow *window;
12775   guint32 time;
12776   gdouble x;
12777   gdouble y;
12778   gdouble pressure;
12779   gdouble xtilt;
12780   gdouble ytilt;
12781   guint state;
12782   gint16 is_hint;
12783   GdkInputSource source;
12784   guint32 deviceid;
12785 };
12786 </verb></tscreen>
12787
12788 <tt/pressure/ gives the pressure as a floating point number between
12789 0 and 1. <tt/xtilt/ and <tt/ytilt/ can take on values between 
12790 -1 and 1, corresponding to the degree of tilt in each direction.
12791 <tt/source/ and <tt/deviceid/ specify the device for which the
12792 event occurred in two different ways. <tt/source/ gives some simple
12793 information about the type of device. It can take the enumeration
12794 values.
12795
12796 <tscreen><verb>
12797 GDK_SOURCE_MOUSE
12798 GDK_SOURCE_PEN
12799 GDK_SOURCE_ERASER
12800 GDK_SOURCE_CURSOR
12801 </verb></tscreen>
12802
12803 <tt/deviceid/ specifies a unique numeric ID for the device. This can
12804 be used to find out further information about the device using the
12805 <tt/gdk_input_list_devices()/ call (see below). The special value
12806 <tt/GDK_CORE_POINTER/ is used for the core pointer device. (Usually
12807 the mouse.)
12808
12809 <sect2> Enabling extended device information
12810
12811 <p>
12812 To let GTK know about our interest in the extended device information,
12813 we merely have to add a single line to our program:
12814
12815 <tscreen><verb>
12816 gtk_widget_set_extension_events (drawing_area, GDK_EXTENSION_EVENTS_CURSOR);
12817 </verb></tscreen>
12818
12819 By giving the value <tt/GDK_EXTENSION_EVENTS_CURSOR/ we say that
12820 we are interested in extension events, but only if we don't have
12821 to draw our own cursor. See the section <ref
12822 id="sec_Further_Sophistications" name="Further Sophistications"> below
12823 for more information about drawing the cursor. We could also 
12824 give the values <tt/GDK_EXTENSION_EVENTS_ALL/ if we were willing 
12825 to draw our own cursor, or <tt/GDK_EXTENSION_EVENTS_NONE/ to revert
12826 back to the default condition.
12827
12828 <p>
12829 This is not completely the end of the story however. By default,
12830 no extension devices are enabled. We need a mechanism to allow
12831 users to enable and configure their extension devices. GTK provides
12832 the InputDialog widget to automate this process. The following
12833 procedure manages an InputDialog widget. It creates the dialog if
12834 it isn't present, and raises it to the top otherwise.
12835
12836 <tscreen><verb>
12837 void
12838 input_dialog_destroy (GtkWidget *w, gpointer data)
12839 {
12840   *((GtkWidget **)data) = NULL;
12841 }
12842
12843 void
12844 create_input_dialog ()
12845 {
12846   static GtkWidget *inputd = NULL;
12847
12848   if (!inputd)
12849     {
12850       inputd = gtk_input_dialog_new();
12851
12852       gtk_signal_connect (GTK_OBJECT(inputd), "destroy",
12853                           (GtkSignalFunc)input_dialog_destroy, &amp;inputd);
12854       gtk_signal_connect_object (GTK_OBJECT(GTK_INPUT_DIALOG(inputd)->close_button),
12855                                  "clicked",
12856                                  (GtkSignalFunc)gtk_widget_hide,
12857                                  GTK_OBJECT(inputd));
12858       gtk_widget_hide ( GTK_INPUT_DIALOG(inputd)->save_button);
12859
12860       gtk_widget_show (inputd);
12861     }
12862   else
12863     {
12864       if (!GTK_WIDGET_MAPPED(inputd))
12865         gtk_widget_show(inputd);
12866       else
12867         gdk_window_raise(inputd->window);
12868     }
12869 }
12870 </verb></tscreen>
12871
12872 (You might want to take note of the way we handle this dialog.  By
12873 connecting to the "destroy" signal, we make sure that we don't keep a
12874 pointer to dialog around after it is destroyed - that could lead to a
12875 segfault.)
12876
12877 <p>
12878 The InputDialog has two buttons "Close" and "Save", which by default
12879 have no actions assigned to them. In the above function we make
12880 "Close" hide the dialog, hide the "Save" button, since we don't
12881 implement saving of XInput options in this program.
12882
12883 <sect2> Using extended device information
12884
12885 <p>
12886 Once we've enabled the device, we can just use the extended 
12887 device information in the extra fields of the event structures.
12888 In fact, it is always safe to use this information since these
12889 fields will have reasonable default values even when extended
12890 events are not enabled.
12891
12892 <p>
12893 Once change we do have to make is to call
12894 <tt/gdk_input_window_get_pointer()/ instead of
12895 <tt/gdk_window_get_pointer/. This is necessary because
12896 <tt/gdk_window_get_pointer/ doesn't return the extended device
12897 information.
12898
12899 <tscreen><verb>
12900 void gdk_input_window_get_pointer     (GdkWindow       *window,
12901                                        guint32         deviceid,
12902                                        gdouble         *x,
12903                                        gdouble         *y,
12904                                        gdouble         *pressure,
12905                                        gdouble         *xtilt,
12906                                        gdouble         *ytilt,
12907                                        GdkModifierType *mask);
12908 </verb></tscreen>
12909
12910 When calling this function, we need to specify the device ID as
12911 well as the window. Usually, we'll get the device ID from the
12912 <tt/deviceid/ field of an event structure. Again, this function
12913 will return reasonable values when extension events are not
12914 enabled. (In this case, <tt/event->deviceid/ will have the value
12915 <tt/GDK_CORE_POINTER/).
12916
12917 So the basic structure of our button-press and motion event handlers,
12918 doesn't change much - we just need to add code to deal with the
12919 extended information.
12920
12921 <tscreen><verb>
12922 static gint
12923 button_press_event (GtkWidget *widget, GdkEventButton *event)
12924 {
12925   print_button_press (event->deviceid);
12926   
12927   if (event->button == 1 &amp;&amp; pixmap != NULL)
12928     draw_brush (widget, event->source, event->x, event->y, event->pressure);
12929
12930   return TRUE;
12931 }
12932
12933 static gint
12934 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
12935 {
12936   gdouble x, y;
12937   gdouble pressure;
12938   GdkModifierType state;
12939
12940   if (event->is_hint)
12941     gdk_input_window_get_pointer (event->window, event->deviceid,
12942                                   &amp;x, &amp;y, &amp;pressure, NULL, NULL, &amp;state);
12943   else
12944     {
12945       x = event->x;
12946       y = event->y;
12947       pressure = event->pressure;
12948       state = event->state;
12949     }
12950     
12951   if (state &amp; GDK_BUTTON1_MASK &amp;&amp; pixmap != NULL)
12952     draw_brush (widget, event->source, x, y, pressure);
12953   
12954   return TRUE;
12955 }
12956 </verb></tscreen>
12957
12958 We also need to do something with the new information. Our new
12959 <tt/draw_brush()/ function draws with a different color for
12960 each <tt/event->source/ and changes the brush size depending
12961 on the pressure.
12962
12963 <tscreen><verb>
12964 /* Draw a rectangle on the screen, size depending on pressure,
12965    and color on the type of device */
12966 static void
12967 draw_brush (GtkWidget *widget, GdkInputSource source,
12968             gdouble x, gdouble y, gdouble pressure)
12969 {
12970   GdkGC *gc;
12971   GdkRectangle update_rect;
12972
12973   switch (source)
12974     {
12975     case GDK_SOURCE_MOUSE:
12976       gc = widget->style->dark_gc[GTK_WIDGET_STATE (widget)];
12977       break;
12978     case GDK_SOURCE_PEN:
12979       gc = widget->style->black_gc;
12980       break;
12981     case GDK_SOURCE_ERASER:
12982       gc = widget->style->white_gc;
12983       break;
12984     default:
12985       gc = widget->style->light_gc[GTK_WIDGET_STATE (widget)];
12986     }
12987
12988   update_rect.x = x - 10 * pressure;
12989   update_rect.y = y - 10 * pressure;
12990   update_rect.width = 20 * pressure;
12991   update_rect.height = 20 * pressure;
12992   gdk_draw_rectangle (pixmap, gc, TRUE,
12993                       update_rect.x, update_rect.y,
12994                       update_rect.width, update_rect.height);
12995   gtk_widget_draw (widget, &amp;update_rect);
12996 }
12997 </verb></tscreen>
12998
12999 <sect2> Finding out more about a device
13000
13001 <p>
13002 As an example of how to find out more about a device, our program
13003 will print the name of the device that generates each button
13004 press. To find out the name of a device, we call the function:
13005
13006 <tscreen><verb>
13007 GList *gdk_input_list_devices               (void);
13008 </verb></tscreen>
13009
13010 which returns a GList (a linked list type from the glib library)
13011 of GdkDeviceInfo structures. The GdkDeviceInfo structure is defined
13012 as:
13013
13014 <tscreen><verb>
13015 struct _GdkDeviceInfo
13016 {
13017   guint32 deviceid;
13018   gchar *name;
13019   GdkInputSource source;
13020   GdkInputMode mode;
13021   gint has_cursor;
13022   gint num_axes;
13023   GdkAxisUse *axes;
13024   gint num_keys;
13025   GdkDeviceKey *keys;
13026 };
13027 </verb></tscreen>
13028
13029 Most of these fields are configuration information that you
13030 can ignore unless you are implemented XInput configuration
13031 saving. The we are interested in here is <tt/name/ which is
13032 simply the name that X assigns to the device. The other field
13033 that isn't configuration information is <tt/has_cursor/. If
13034 <tt/has_cursor/ is false, then we we need to draw our own
13035 cursor. But since we've specified <tt/GDK_EXTENSION_EVENTS_CURSOR/,
13036 we don't have to worry about this.
13037
13038 <p>
13039 Our <tt/print_button_press()/ function simply iterates through
13040 the returned list until it finds a match, then prints out
13041 the name of the device.
13042
13043 <tscreen><verb>
13044 static void
13045 print_button_press (guint32 deviceid)
13046 {
13047   GList *tmp_list;
13048
13049   /* gdk_input_list_devices returns an internal list, so we shouldn't
13050      free it afterwards */
13051   tmp_list = gdk_input_list_devices();
13052
13053   while (tmp_list)
13054     {
13055       GdkDeviceInfo *info = (GdkDeviceInfo *)tmp_list->data;
13056
13057       if (info->deviceid == deviceid)
13058         {
13059           printf("Button press on device '%s'\n", info->name);
13060           return;
13061         }
13062
13063       tmp_list = tmp_list->next;
13064     }
13065 }
13066 </verb></tscreen>
13067
13068 That completes the changes to `XInputize' our program. As with
13069 the first version, the complete source is available at the location
13070 from which you got this tutorial, or from:
13071
13072 <htmlurl url="http://www.gtk.org/~otaylor/gtk/tutorial/"
13073 name="http://www.gtk.org/~otaylor/gtk/tutorial/">
13074
13075
13076 <sect2> Further sophistications <label id="sec_Further_Sophistications">
13077
13078 <p>
13079 Although our program now supports XInput quite well, it lacks some
13080 features we would want in a full-featured application. First, the user
13081 probably doesn't want to have to configure their device each time they
13082 run the program, so we should allow them to save the device
13083 configuration. This is done by iterating through the return of
13084 <tt/gdk_input_list_devices()/ and writing out the configuration to a
13085 file.
13086
13087 <p>
13088 To restore the state next time the program is run, GDK provides
13089 functions to change device configuration:
13090
13091 <tscreen><verb>
13092 gdk_input_set_extension_events()
13093 gdk_input_set_source()
13094 gdk_input_set_mode()
13095 gdk_input_set_axes()
13096 gdk_input_set_key()
13097 </verb></tscreen>
13098
13099 (The list returned from <tt/gdk_input_list_devices()/ should not be
13100 modified directly.) An example of doing this can be found in the
13101 drawing program gsumi. (Available from <htmlurl
13102 url="http://www.msc.cornell.edu/~otaylor/gsumi/"
13103 name="http://www.msc.cornell.edu/~otaylor/gsumi/">) Eventually, it
13104 would be nice to have a standard way of doing this for all
13105 applications. This probably belongs at a slightly higher level than
13106 GTK, perhaps in the GNOME library.
13107
13108 <p>
13109 Another major omission that we have mentioned above is the lack of
13110 cursor drawing. Platforms other than XFree86 currently do not allow
13111 simultaneously using a device as both the core pointer and directly by
13112 an application. See the <url
13113 url="http://www.msc.cornell.edu/~otaylor/xinput/XInput-HOWTO.html"
13114 name="XInput-HOWTO"> for more information about this. This means that
13115 applications that want to support the widest audience need to draw
13116 their own cursor.
13117
13118 <p>
13119 An application that draws its own cursor needs to do two things:
13120 determine if the current device needs a cursor drawn or not, and
13121 determine if the current device is in proximity. (If the current
13122 device is a drawing tablet, it's a nice touch to make the cursor 
13123 disappear when the stylus is lifted from the tablet. When the
13124 device is touching the stylus, that is called "in proximity.")
13125 The first is done by searching the device list, as we did
13126 to find out the device name. The second is achieved by selecting
13127 "proximity_out" events. An example of drawing one's own cursor is
13128 found in the 'testinput' program found in the GTK distribution.
13129
13130 <!-- ***************************************************************** -->
13131 <sect>Tips For Writing GTK Applications
13132 <!-- ***************************************************************** -->
13133
13134 <p>
13135 This section is simply a gathering of wisdom, general style guidelines and hints to
13136 creating good GTK applications. It is totally useless right now cause its
13137 only a topic sentence :)
13138
13139 Use GNU autoconf and automake!  They are your friends :)  I am planning to
13140 make a quick intro on them here.
13141
13142 <!-- ***************************************************************** -->
13143 <sect>Contributing <label id="sec_Contributing">
13144 <!-- ***************************************************************** -->
13145
13146 <p>
13147 This document, like so much other great software out there, was created for
13148 free by volunteers.  If you are at all knowledgeable about any aspect of GTK
13149 that does not already have documentation, please consider contributing to
13150 this document.
13151 <p>
13152 If you do decide to contribute, please mail your text to Tony Gale, 
13153 <tt><htmlurl url="mailto:gale@gtk.org"
13154 name="gale@gtk.org"></tt>. Also, be aware that the entirety of this 
13155 document is free, and any addition by you provide must also be free. That is, 
13156 people may use any portion of your examples in their programs, and copies 
13157 of this document may be distributed at will etc.
13158 <p>
13159 Thank you.
13160
13161 <!-- ***************************************************************** -->
13162 <sect>Credits
13163 <!-- ***************************************************************** -->
13164 <p>
13165 I would like to thank the following for their contributions to this text.
13166
13167 <itemize>
13168 <item>Bawer Dagdeviren, <tt><htmlurl url="mailto:chamele0n@geocities.com"
13169 name="chamele0n@geocities.com"></tt> for the menus tutorial.                          
13170
13171 <item>Raph Levien, <tt><htmlurl url="mailto:raph@acm.org"
13172 name="raph@acm.org"></tt>
13173 for hello world ala GTK, widget packing, and general all around wisdom.
13174 He's also generously donated a home for this tutorial.
13175
13176 <item>Peter Mattis, <tt><htmlurl url="mailto:petm@xcf.berkeley.edu"
13177 name="petm@xcf.berkeley.edu"></tt> for the simplest GTK program.. 
13178 and the ability to make it :)
13179
13180 <item>Werner Koch <tt><htmlurl url="mailto:werner.koch@guug.de"
13181 name="werner.koch@guug.de"></tt> for converting the original plain text to
13182 SGML, and the widget class hierarchy.
13183
13184 <item>Mark Crichton <tt><htmlurl url="mailto:crichton@expert.cc.purdue.edu"
13185 name="crichton@expert.cc.purdue.edu"></tt> for the menu factory code, and
13186 the table packing tutorial.
13187
13188 <item>Owen Taylor <tt><htmlurl url="mailto:owt1@cornell.edu"
13189 name="owt1@cornell.edu"></tt> for the EventBox widget section (and
13190 the patch to the distro).  He's also responsible for the selections code and
13191 tutorial, as well as the sections on writing your own GTK widgets, and the
13192 example application. Thanks a lot Owen for all you help!
13193
13194 <item>Mark VanderBoom <tt><htmlurl url="mailto:mvboom42@calvin.edu"
13195 name="mvboom42@calvin.edu"></tt> for his wonderful work on the Notebook,
13196 Progress Bar, Dialogs, and File selection widgets.  Thanks a lot Mark!
13197 You've been a great help.
13198
13199 <item>Tim Janik <tt><htmlurl url="mailto:timj@gtk.org"
13200 name="timj@psynet.net"></tt> for his great job on the Lists
13201 Widget. His excellent work on automatically extracting the widget tree
13202 and signal information from GTK.
13203 Thanks Tim :)
13204
13205 <item>Rajat Datta <tt><htmlurl url="mailto:rajat@ix.netcom.com"
13206 name="rajat@ix.netcom.com"</tt> for the excellent job on the Pixmap tutorial.
13207
13208 <item>Michael K. Johnson <tt><htmlurl url="mailto:johnsonm@redhat.com"
13209 name="johnsonm@redhat.com"></tt> for info and code for popup menus. 
13210
13211 <item>David Huggins-Daines <tt><htmlurl url="mailto:bn711@freenet.carleton.ca"
13212 name="bn711@freenet.carleton.ca"></tt> for the Range Widgets and Tree Widget
13213 sections. 
13214
13215 <item>Stefan Mars <tt><htmlurl url="mailto:mars@lysator.liu.se"
13216 name="mars@lysator.liu.se"></tt> for the GtkCList section
13217 </itemize>
13218 <p>
13219 And to all of you who commented and helped refine this document.
13220 <p>
13221 Thanks.
13222
13223 <!-- ***************************************************************** -->
13224 <sect> Tutorial Copyright and Permissions Notice
13225 <!-- ***************************************************************** -->
13226
13227 <p>
13228 The GTK Tutorial is Copyright (C) 1997 Ian Main. 
13229
13230 Copyright (C) 1998 Tony Gale.
13231 <p>
13232 Permission is granted to make and distribute verbatim copies of this 
13233 manual provided the copyright notice and this permission notice are 
13234 preserved on all copies.
13235 <P>Permission is granted to copy and distribute modified versions of 
13236 this document under the conditions for verbatim copying, provided that 
13237 this copyright notice is included exactly as in the original,
13238 and that the entire resulting derived work is distributed under 
13239 the terms of a permission notice identical to this one.
13240 <P>Permission is granted to copy and distribute translations of this 
13241 document into another language, under the above conditions for modified 
13242 versions.
13243 <P>If you are intending to incorporate this document into a published 
13244 work, please contact the maintainer, and we will make an effort 
13245 to ensure that you have the most up to date information available.
13246 <P>There is no guarantee that this document lives up to its intended
13247 purpose.  This is simply provided as a free resource.  As such,
13248 the authors and maintainers of the information provided within can
13249 not make any guarantee that the information is even accurate.
13250
13251 <!-- ***************************************************************** -->
13252 <appendix>
13253 <!-- ***************************************************************** -->
13254
13255 <!-- ***************************************************************** -->
13256 <sect> GTK Signals <label id="sec_GTK_Signals">
13257 <!-- ***************************************************************** -->
13258 <p>
13259 As GTK is an object oriented widget set, it has a hierarchy of
13260 inheritance. This inheritance mechanism applies for
13261 signals. Therefore, you should refer to the widget hierarchy tree when
13262 using the signals listed in this section.
13263
13264 <!-- ----------------------------------------------------------------- -->
13265 <sect1>GtkObject
13266 <!-- ----------------------------------------------------------------- -->
13267 <p>
13268 <tscreen><verb>
13269 void GtkObject::destroy (GtkObject *,
13270                          gpointer);
13271 </verb></tscreen>
13272
13273 <!-- ----------------------------------------------------------------- -->
13274 <sect1>GtkWidget
13275 <!-- ----------------------------------------------------------------- -->
13276 <p>
13277 <tscreen><verb>
13278
13279 void GtkWidget::show    (GtkWidget *,
13280                          gpointer);
13281 void GtkWidget::hide    (GtkWidget *,
13282                          gpointer);
13283 void GtkWidget::map     (GtkWidget *,
13284                          gpointer);
13285 void GtkWidget::unmap   (GtkWidget *,
13286                          gpointer);
13287 void GtkWidget::realize (GtkWidget *,
13288                          gpointer);
13289 void GtkWidget::unrealize       (GtkWidget *,
13290                                  gpointer);
13291 void GtkWidget::draw    (GtkWidget *,
13292                          ggpointer,
13293                          gpointer);
13294 void GtkWidget::draw-focus      (GtkWidget *,
13295                                  gpointer);
13296 void GtkWidget::draw-default    (GtkWidget *,
13297                                  gpointer);
13298 void GtkWidget::size-request    (GtkWidget *,
13299                                  ggpointer,
13300                                  gpointer);
13301 void GtkWidget::size-allocate   (GtkWidget *,
13302                                  ggpointer,
13303                                  gpointer);
13304 void GtkWidget::state-changed   (GtkWidget *,
13305                                  GtkStateType,
13306                                  gpointer);
13307 void GtkWidget::parent-set      (GtkWidget *,
13308                                  GtkObject *,
13309                                  gpointer);
13310 void GtkWidget::style-set       (GtkWidget *,
13311                                  GtkStyle *,
13312                                  gpointer);
13313 void GtkWidget::add-accelerator (GtkWidget *,
13314                                  gguint,
13315                                  GtkAccelGroup *,
13316                                  gguint,
13317                                  GdkModifierType,
13318                                  GtkAccelFlags,
13319                                  gpointer);
13320 void GtkWidget::remove-accelerator      (GtkWidget *,
13321                                          GtkAccelGroup *,
13322                                          gguint,
13323                                          GdkModifierType,
13324                                          gpointer);
13325 gboolean GtkWidget::event       (GtkWidget *,
13326                                  GdkEvent *,
13327                                  gpointer);
13328 gboolean GtkWidget::button-press-event  (GtkWidget *,
13329                                          GdkEvent *,
13330                                          gpointer);
13331 gboolean GtkWidget::button-release-event        (GtkWidget *,
13332                                                  GdkEvent *,
13333                                                  gpointer);
13334 gboolean GtkWidget::motion-notify-event (GtkWidget *,
13335                                          GdkEvent *,
13336                                          gpointer);
13337 gboolean GtkWidget::delete-event        (GtkWidget *,
13338                                          GdkEvent *,
13339                                          gpointer);
13340 gboolean GtkWidget::destroy-event       (GtkWidget *,
13341                                          GdkEvent *,
13342                                          gpointer);
13343 gboolean GtkWidget::expose-event        (GtkWidget *,
13344                                          GdkEvent *,
13345                                          gpointer);
13346 gboolean GtkWidget::key-press-event     (GtkWidget *,
13347                                          GdkEvent *,
13348                                          gpointer);
13349 gboolean GtkWidget::key-release-event   (GtkWidget *,
13350                                          GdkEvent *,
13351                                          gpointer);
13352 gboolean GtkWidget::enter-notify-event  (GtkWidget *,
13353                                          GdkEvent *,
13354                                          gpointer);
13355 gboolean GtkWidget::leave-notify-event  (GtkWidget *,
13356                                          GdkEvent *,
13357                                          gpointer);
13358 gboolean GtkWidget::configure-event     (GtkWidget *,
13359                                          GdkEvent *,
13360                                          gpointer);
13361 gboolean GtkWidget::focus-in-event      (GtkWidget *,
13362                                          GdkEvent *,
13363                                          gpointer);
13364 gboolean GtkWidget::focus-out-event     (GtkWidget *,
13365                                          GdkEvent *,
13366                                          gpointer);
13367 gboolean GtkWidget::map-event   (GtkWidget *,
13368                                  GdkEvent *,
13369                                  gpointer);
13370 gboolean GtkWidget::unmap-event (GtkWidget *,
13371                                  GdkEvent *,
13372                                  gpointer);
13373 gboolean GtkWidget::property-notify-event       (GtkWidget *,
13374                                                  GdkEvent *,
13375                                                  gpointer);
13376 gboolean GtkWidget::selection-clear-event       (GtkWidget *,
13377                                                  GdkEvent *,
13378                                                  gpointer);
13379 gboolean GtkWidget::selection-request-event     (GtkWidget *,
13380                                                  GdkEvent *,
13381                                                  gpointer);
13382 gboolean GtkWidget::selection-notify-event      (GtkWidget *,
13383                                                  GdkEvent *,
13384                                                  gpointer);
13385 void GtkWidget::selection-get   (GtkWidget *,
13386                                  GtkSelectionData *,
13387                                  gguint,
13388                                  gpointer);
13389 void GtkWidget::selection-received      (GtkWidget *,
13390                                          GtkSelectionData *,
13391                                          gguint,
13392                                          gpointer);
13393 gboolean GtkWidget::proximity-in-event  (GtkWidget *,
13394                                          GdkEvent *,
13395                                          gpointer);
13396 gboolean GtkWidget::proximity-out-event (GtkWidget *,
13397                                          GdkEvent *,
13398                                          gpointer);
13399 void GtkWidget::drag-begin      (GtkWidget *,
13400                                  GdkDragContext *,
13401                                  gpointer);
13402 void GtkWidget::drag-end        (GtkWidget *,
13403                                  GdkDragContext *,
13404                                  gpointer);
13405 void GtkWidget::drag-data-delete        (GtkWidget *,
13406                                          GdkDragContext *,
13407                                          gpointer);
13408 void GtkWidget::drag-leave      (GtkWidget *,
13409                                  GdkDragContext *,
13410                                  gguint,
13411                                  gpointer);
13412 gboolean GtkWidget::drag-motion (GtkWidget *,
13413                                  GdkDragContext *,
13414                                  ggint,
13415                                  ggint,
13416                                  gguint,
13417                                  gpointer);
13418 gboolean GtkWidget::drag-drop   (GtkWidget *,
13419                                  GdkDragContext *,
13420                                  ggint,
13421                                  ggint,
13422                                  gguint,
13423                                  gpointer);
13424 void GtkWidget::drag-data-get   (GtkWidget *,
13425                                  GdkDragContext *,
13426                                  GtkSelectionData *,
13427                                  gguint,
13428                                  gguint,
13429                                  gpointer);
13430 void GtkWidget::drag-data-received      (GtkWidget *,
13431                                          GdkDragContext *,
13432                                          ggint,
13433                                          ggint,
13434                                          GtkSelectionData *,
13435                                          gguint,
13436                                          gguint,
13437                                          gpointer);
13438 gboolean GtkWidget::client-event        (GtkWidget *,
13439                                          GdkEvent *,
13440                                          gpointer);
13441 gboolean GtkWidget::no-expose-event     (GtkWidget *,
13442                                          GdkEvent *,
13443                                          gpointer);
13444 gboolean GtkWidget::visibility-notify-event     (GtkWidget *,
13445                                                  GdkEvent *,
13446                                                  gpointer);
13447 void GtkWidget::debug-msg       (GtkWidget *,
13448                                  GtkString *,
13449                                  gpointer);
13450 </verb></tscreen>
13451
13452 <!-- ----------------------------------------------------------------- -->
13453 <sect1>GtkData
13454 <!-- ----------------------------------------------------------------- -->
13455 <p>
13456 <tscreen><verb>
13457 void GtkData::disconnect        (GtkData *,
13458                                  gpointer);
13459 </verb></tscreen>
13460
13461 <!-- ----------------------------------------------------------------- -->
13462 <sect1>GtkContainer
13463 <!-- ----------------------------------------------------------------- -->
13464 <p>
13465 <tscreen><verb>
13466 void GtkContainer::add  (GtkContainer *,
13467                          GtkWidget *,
13468                          gpointer);
13469 void GtkContainer::remove       (GtkContainer *,
13470                                  GtkWidget *,
13471                                  gpointer);
13472 void GtkContainer::check-resize (GtkContainer *,
13473                                  gpointer);
13474 GtkDirectionType GtkContainer::focus    (GtkContainer *,
13475                                          GtkDirectionType,
13476                                          gpointer);
13477 void GtkContainer::set-focus-child      (GtkContainer *,
13478                                          GtkWidget *,
13479                                          gpointer);
13480 </verb></tscreen>
13481
13482 <!-- ----------------------------------------------------------------- -->
13483 <sect1>GtkCalendar
13484 <!-- ----------------------------------------------------------------- -->
13485 <p>
13486 <tscreen><verb>
13487 void GtkCalendar::month-changed (GtkCalendar *,
13488                                  gpointer);
13489 void GtkCalendar::day-selected  (GtkCalendar *,
13490                                  gpointer);
13491 void GtkCalendar::day-selected-double-click     (GtkCalendar *,
13492                                                  gpointer);
13493 void GtkCalendar::prev-month    (GtkCalendar *,
13494                                  gpointer);
13495 void GtkCalendar::next-month    (GtkCalendar *,
13496                                  gpointer);
13497 void GtkCalendar::prev-year     (GtkCalendar *,
13498                                  gpointer);
13499 void GtkCalendar::next-year     (GtkCalendar *,
13500                                  gpointer);
13501 </verb></tscreen>
13502
13503 <!-- ----------------------------------------------------------------- -->
13504 <sect1>GtkEditable
13505 <!-- ----------------------------------------------------------------- -->
13506 <p>
13507 <tscreen><verb>
13508 void GtkEditable::changed       (GtkEditable *,
13509                                  gpointer);
13510 void GtkEditable::insert-text   (GtkEditable *,
13511                                  GtkString *,
13512                                  ggint,
13513                                  ggpointer,
13514                                  gpointer);
13515 void GtkEditable::delete-text   (GtkEditable *,
13516                                  ggint,
13517                                  ggint,
13518                                  gpointer);
13519 void GtkEditable::activate      (GtkEditable *,
13520                                  gpointer);
13521 void GtkEditable::set-editable  (GtkEditable *,
13522                                  gboolean,
13523                                  gpointer);
13524 void GtkEditable::move-cursor   (GtkEditable *,
13525                                  ggint,
13526                                  ggint,
13527                                  gpointer);
13528 void GtkEditable::move-word     (GtkEditable *,
13529                                  ggint,
13530                                  gpointer);
13531 void GtkEditable::move-page     (GtkEditable *,
13532                                  ggint,
13533                                  ggint,
13534                                  gpointer);
13535 void GtkEditable::move-to-row   (GtkEditable *,
13536                                  ggint,
13537                                  gpointer);
13538 void GtkEditable::move-to-column        (GtkEditable *,
13539                                          ggint,
13540                                          gpointer);
13541 void GtkEditable::kill-char     (GtkEditable *,
13542                                  ggint,
13543                                  gpointer);
13544 void GtkEditable::kill-word     (GtkEditable *,
13545                                  ggint,
13546                                  gpointer);
13547 void GtkEditable::kill-line     (GtkEditable *,
13548                                  ggint,
13549                                  gpointer);
13550 void GtkEditable::cut-clipboard (GtkEditable *,
13551                                  gpointer);
13552 void GtkEditable::copy-clipboard        (GtkEditable *,
13553                                          gpointer);
13554 void GtkEditable::paste-clipboard       (GtkEditable *,
13555                                          gpointer);
13556 </verb></tscreen>
13557
13558 <!-- ----------------------------------------------------------------- -->
13559 <sect1>GtkTipsQuery
13560 <!-- ----------------------------------------------------------------- -->
13561 <p>
13562 <tscreen><verb>
13563 void GtkTipsQuery::start-query  (GtkTipsQuery *,
13564                                  gpointer);
13565 void GtkTipsQuery::stop-query   (GtkTipsQuery *,
13566                                  gpointer);
13567 void GtkTipsQuery::widget-entered       (GtkTipsQuery *,
13568                                          GtkWidget *,
13569                                          GtkString *,
13570                                          GtkString *,
13571                                          gpointer);
13572 gboolean GtkTipsQuery::widget-selected  (GtkTipsQuery *,
13573                                          GtkWidget *,
13574                                          GtkString *,
13575                                          GtkString *,
13576                                          GdkEvent *,
13577                                          gpointer);
13578 </verb></tscreen>
13579
13580 <!-- ----------------------------------------------------------------- -->
13581 <sect1>GtkCList
13582 <!-- ----------------------------------------------------------------- -->
13583 <p>
13584 <tscreen><verb>
13585 void GtkCList::select-row       (GtkCList *,
13586                                  ggint,
13587                                  ggint,
13588                                  GdkEvent *,
13589                                  gpointer);
13590 void GtkCList::unselect-row     (GtkCList *,
13591                                  ggint,
13592                                  ggint,
13593                                  GdkEvent *,
13594                                  gpointer);
13595 void GtkCList::row-move (GtkCList *,
13596                          ggint,
13597                          ggint,
13598                          gpointer);
13599 void GtkCList::click-column     (GtkCList *,
13600                                  ggint,
13601                                  gpointer);
13602 void GtkCList::resize-column    (GtkCList *,
13603                                  ggint,
13604                                  ggint,
13605                                  gpointer);
13606 void GtkCList::toggle-focus-row (GtkCList *,
13607                                  gpointer);
13608 void GtkCList::select-all       (GtkCList *,
13609                                  gpointer);
13610 void GtkCList::unselect-all     (GtkCList *,
13611                                  gpointer);
13612 void GtkCList::undo-selection   (GtkCList *,
13613                                  gpointer);
13614 void GtkCList::start-selection  (GtkCList *,
13615                                  gpointer);
13616 void GtkCList::end-selection    (GtkCList *,
13617                                  gpointer);
13618 void GtkCList::toggle-add-mode  (GtkCList *,
13619                                  gpointer);
13620 void GtkCList::extend-selection (GtkCList *,
13621                                  GtkScrollType,
13622                                  ggfloat,
13623                                  gboolean,
13624                                  gpointer);
13625 void GtkCList::scroll-vertical  (GtkCList *,
13626                                  GtkScrollType,
13627                                  ggfloat,
13628                                  gpointer);
13629 void GtkCList::scroll-horizontal        (GtkCList *,
13630                                          GtkScrollType,
13631                                          ggfloat,
13632                                          gpointer);
13633 void GtkCList::abort-column-resize      (GtkCList *,
13634                                          gpointer);
13635 </verb></tscreen>
13636
13637 <!-- ----------------------------------------------------------------- -->
13638 <sect1>GtkNotebook
13639 <!-- ----------------------------------------------------------------- -->
13640 <p>
13641 <tscreen><verb>
13642 void GtkNotebook::switch-page   (GtkNotebook *,
13643                                  ggpointer,
13644                                  gguint,
13645                                  gpointer);
13646
13647 </verb></tscreen>
13648
13649 <!-- ----------------------------------------------------------------- -->
13650 <sect1>GtkList
13651 <!-- ----------------------------------------------------------------- -->
13652 <p>
13653 <tscreen><verb>
13654 void GtkList::selection-changed (GtkList *,
13655                                  gpointer);
13656 void GtkList::select-child      (GtkList *,
13657                                  GtkWidget *,
13658                                  gpointer);
13659 void GtkList::unselect-child    (GtkList *,
13660                                  GtkWidget *,
13661                                  gpointer);
13662 </verb></tscreen>
13663
13664 <!-- ----------------------------------------------------------------- -->
13665 <sect1>GtkMenuShell
13666 <!-- ----------------------------------------------------------------- -->
13667 <p>
13668 <tscreen><verb>
13669 void GtkMenuShell::deactivate   (GtkMenuShell *,
13670                                  gpointer);
13671 void GtkMenuShell::selection-done       (GtkMenuShell *,
13672                                          gpointer);
13673 void GtkMenuShell::move-current (GtkMenuShell *,
13674                                  GtkMenuDirectionType,
13675                                  gpointer);
13676 void GtkMenuShell::activate-current     (GtkMenuShell *,
13677                                          gboolean,
13678                                          gpointer);
13679 void GtkMenuShell::cancel       (GtkMenuShell *,
13680                                  gpointer);
13681 </verb></tscreen>
13682
13683 <!-- ----------------------------------------------------------------- -->
13684 <sect1>GtkToolbar
13685 <!-- ----------------------------------------------------------------- -->
13686 <p>
13687 <tscreen><verb>
13688 void GtkToolbar::orientation-changed    (GtkToolbar *,
13689                                          ggint,
13690                                          gpointer);
13691 void GtkToolbar::style-changed  (GtkToolbar *,
13692                                  ggint,
13693                                  gpointer);
13694 </verb></tscreen>
13695
13696 <!-- ----------------------------------------------------------------- -->
13697 <sect1>GtkTree
13698 <!-- ----------------------------------------------------------------- -->
13699 <p>
13700 <tscreen><verb>
13701 void GtkTree::selection-changed (GtkTree *,
13702                                  gpointer);
13703 void GtkTree::select-child      (GtkTree *,
13704                                  GtkWidget *,
13705                                  gpointer);
13706 void GtkTree::unselect-child    (GtkTree *,
13707                                  GtkWidget *,
13708                                  gpointer);
13709 </verb></tscreen>
13710
13711 <!-- ----------------------------------------------------------------- -->
13712 <sect1>GtkButton
13713 <!-- ----------------------------------------------------------------- -->
13714 <p>
13715 <tscreen><verb>
13716 void GtkButton::pressed (GtkButton *,
13717                          gpointer);
13718 void GtkButton::released        (GtkButton *,
13719                                  gpointer);
13720 void GtkButton::clicked (GtkButton *,
13721                          gpointer);
13722 void GtkButton::enter   (GtkButton *,
13723                          gpointer);
13724 void GtkButton::leave   (GtkButton *,
13725                          gpointer);
13726 </verb></tscreen>
13727
13728 <!-- ----------------------------------------------------------------- -->
13729 <sect1>GtkItem
13730 <!-- ----------------------------------------------------------------- -->
13731 <p>
13732 <tscreen><verb>
13733 void GtkItem::select    (GtkItem *,
13734                          gpointer);
13735 void GtkItem::deselect  (GtkItem *,
13736                          gpointer);
13737 void GtkItem::toggle    (GtkItem *,
13738                          gpointer);
13739 </verb></tscreen>
13740
13741 <!-- ----------------------------------------------------------------- -->
13742 <sect1>GtkWindow
13743 <!-- ----------------------------------------------------------------- -->
13744 <p>
13745 <tscreen><verb>
13746 void GtkWindow::set-focus       (GtkWindow *,
13747                                  ggpointer,
13748                                  gpointer);
13749 </verb></tscreen>
13750
13751 <!-- ----------------------------------------------------------------- -->
13752 <sect1>GtkHandleBox
13753 <!-- ----------------------------------------------------------------- -->
13754 <p>
13755 <tscreen><verb>
13756 void GtkHandleBox::child-attached       (GtkHandleBox *,
13757                                          GtkWidget *,
13758                                          gpointer);
13759 void GtkHandleBox::child-detached       (GtkHandleBox *,
13760                                          GtkWidget *,
13761                                          gpointer);
13762 </verb></tscreen>
13763
13764 <!-- ----------------------------------------------------------------- -->
13765 <sect1>GtkToggleButton
13766 <!-- ----------------------------------------------------------------- -->
13767 <p>
13768 <tscreen><verb>
13769 void GtkToggleButton::toggled   (GtkToggleButton *,
13770                                  gpointer);
13771
13772 </verb></tscreen>
13773
13774 <!-- ----------------------------------------------------------------- -->
13775 <sect1>GtkMenuItem
13776 <!-- ----------------------------------------------------------------- -->
13777 <p>
13778 <tscreen><verb>
13779 void GtkMenuItem::activate      (GtkMenuItem *,
13780                                  gpointer);
13781 void GtkMenuItem::activate-item (GtkMenuItem *,
13782                                  gpointer);
13783 </verb></tscreen>
13784
13785 <!-- ----------------------------------------------------------------- -->
13786 <sect1>GtkListItem
13787 <!-- ----------------------------------------------------------------- -->
13788 <p>
13789 <tscreen><verb>
13790 void GtkListItem::toggle-focus-row      (GtkListItem *,
13791                                          gpointer);
13792 void GtkListItem::select-all    (GtkListItem *,
13793                                  gpointer);
13794 void GtkListItem::unselect-all  (GtkListItem *,
13795                                  gpointer);
13796 void GtkListItem::undo-selection        (GtkListItem *,
13797                                          gpointer);
13798 void GtkListItem::start-selection       (GtkListItem *,
13799                                          gpointer);
13800 void GtkListItem::end-selection (GtkListItem *,
13801                                  gpointer);
13802 void GtkListItem::toggle-add-mode       (GtkListItem *,
13803                                          gpointer);
13804 void GtkListItem::extend-selection      (GtkListItem *,
13805                                          GtkEnum,
13806                                          ggfloat,
13807                                          gboolean,
13808                                          gpointer);
13809 void GtkListItem::scroll-vertical       (GtkListItem *,
13810                                          GtkEnum,
13811                                          ggfloat,
13812                                          gpointer);
13813 void GtkListItem::scroll-horizontal     (GtkListItem *,
13814                                          GtkEnum,
13815                                          ggfloat,
13816                                          gpointer);
13817 </verb></tscreen>
13818
13819 <!-- ----------------------------------------------------------------- -->
13820 <sect1>GtkTreeItem
13821 <!-- ----------------------------------------------------------------- -->
13822 <p>
13823 <tscreen><verb>
13824 void GtkTreeItem::collapse      (GtkTreeItem *,
13825                                  gpointer);
13826 void GtkTreeItem::expand        (GtkTreeItem *,
13827                                  gpointer);
13828 </verb></tscreen>
13829
13830 <!-- ----------------------------------------------------------------- -->
13831 <sect1>GtkCheckMenuItem
13832 <!-- ----------------------------------------------------------------- -->
13833 <p>
13834 <tscreen><verb>
13835 void GtkCheckMenuItem::toggled  (GtkCheckMenuItem *,
13836                                  gpointer);
13837 </verb></tscreen>
13838
13839 <!-- ----------------------------------------------------------------- -->
13840 <sect1>GtkInputDialog
13841 <!-- ----------------------------------------------------------------- -->
13842 <p>
13843 <tscreen><verb>
13844 void GtkInputDialog::enable-device      (GtkInputDialog *,
13845                                          ggint,
13846                                          gpointer);
13847 void GtkInputDialog::disable-device     (GtkInputDialog *,
13848                                          ggint,
13849                                          gpointer);
13850 </verb></tscreen>
13851
13852 <!-- ----------------------------------------------------------------- -->
13853 <sect1>GtkColorSelection
13854 <!-- ----------------------------------------------------------------- -->
13855 <p>
13856 <tscreen><verb>
13857 void GtkColorSelection::color-changed   (GtkColorSelection *,
13858                                          gpointer);
13859 </verb></tscreen>
13860
13861 <!-- ----------------------------------------------------------------- -->
13862 <sect1>GtkStatusBar
13863 <!-- ----------------------------------------------------------------- -->
13864 <p>
13865 <tscreen><verb>
13866 void GtkStatusbar::text-pushed  (GtkStatusbar *,
13867                                  gguint,
13868                                  GtkString *,
13869                                  gpointer);
13870 void GtkStatusbar::text-popped  (GtkStatusbar *,
13871                                  gguint,
13872                                  GtkString *,
13873                                  gpointer);
13874 </verb></tscreen>
13875
13876 <!-- ----------------------------------------------------------------- -->
13877 <sect1>GtkCTree
13878 <!-- ----------------------------------------------------------------- -->
13879 <p>
13880 <tscreen><verb>
13881 void GtkCTree::tree-select-row  (GtkCTree *,
13882                                  GtkCTreeNode *,
13883                                  ggint,
13884                                  gpointer);
13885 void GtkCTree::tree-unselect-row        (GtkCTree *,
13886                                          GtkCTreeNode *,
13887                                          ggint,
13888                                          gpointer);
13889 void GtkCTree::tree-expand      (GtkCTree *,
13890                                  GtkCTreeNode *,
13891                                  gpointer);
13892 void GtkCTree::tree-collapse    (GtkCTree *,
13893                                  ggpointer,
13894                                  gpointer);
13895 void GtkCTree::tree-move        (GtkCTree *,
13896                                  GtkCTreeNode *,
13897                                  GtkCTreeNode *,
13898                                  GtkCTreeNode *,
13899                                  gpointer);
13900 void GtkCTree::change-focus-row-expansion       (GtkCTree *,
13901                                                  GtkCTreeExpansionType,
13902                                                  gpointer);
13903 </verb></tscreen>
13904
13905 <!-- ----------------------------------------------------------------- -->
13906 <sect1>GtkCurve
13907 <!-- ----------------------------------------------------------------- -->
13908 <p>
13909 <tscreen><verb>
13910 void GtkCurve::curve-type-changed       (GtkCurve *,
13911                                          gpointer);
13912 </verb></tscreen>
13913
13914 <!-- ----------------------------------------------------------------- -->
13915 <sect1>GtkAdjustment
13916 <!-- ----------------------------------------------------------------- -->
13917 <p>
13918 <tscreen><verb>
13919 void GtkAdjustment::changed     (GtkAdjustment *,
13920                                  gpointer);
13921 void GtkAdjustment::value-changed       (GtkAdjustment *,
13922                                          gpointer);
13923 </verb></tscreen>
13924
13925 <!-- ***************************************************************** -->
13926 <sect> GDK Event Types<label id="sec_GDK_Event_Types">
13927 <!-- ***************************************************************** -->
13928 <p>
13929 The follwing data types are passed into event handlers by GTK+. For
13930 each data type listed, the signals that use this data type are listed.
13931
13932 <itemize>
13933 <item>  GdkEvent
13934           <itemize>
13935           <item>drag_end_event
13936           </itemize>
13937
13938 <item>  GdkEventType
13939
13940 <item>  GdkEventAny
13941           <itemize>
13942           <item>delete_event
13943           <item>destroy_event
13944           <item>map_event
13945           <item>unmap_event
13946           <item>no_expose_event
13947           </itemize>
13948
13949 <item>  GdkEventExpose
13950           <itemize>
13951           <item>expose_event
13952           </itemize>
13953
13954 <item>  GdkEventNoExpose
13955
13956 <item>  GdkEventVisibility
13957
13958 <item>  GdkEventMotion
13959           <itemize>
13960           <item>motion_notify_event
13961           </itemize>
13962
13963 <item>  GdkEventButton
13964           <itemize>
13965           <item>button_press_event
13966           <item>button_release_event
13967           </itemize>
13968
13969 <item>  GdkEventKey
13970           <itemize>
13971           <item>key_press_event
13972           <item>key_release_event
13973           </itemize>
13974
13975 <item>  GdkEventCrossing
13976           <itemize>
13977           <item>enter_notify_event
13978           <item>leave_notify_event
13979           </itemize>
13980
13981 <item>  GdkEventFocus
13982           <itemize>
13983           <item>focus_in_event
13984           <item>focus_out_event
13985           </itemize>
13986
13987 <item>  GdkEventConfigure
13988           <itemize>
13989           <item>configure_event
13990           </itemize>
13991
13992 <item>  GdkEventProperty
13993           <itemize>
13994           <item>property_notify_event
13995           </itemize>
13996
13997 <item>  GdkEventSelection
13998           <itemize>
13999           <item>selection_clear_event
14000           <item>selection_request_event
14001           <item>selection_notify_event
14002           </itemize>
14003
14004 <item>  GdkEventProximity
14005           <itemize>
14006           <item>proximity_in_event
14007           <item>proximity_out_event
14008           </itemize>
14009
14010 <item>  GdkEventDragBegin
14011           <itemize>
14012           <item>drag_begin_event
14013           </itemize>
14014
14015 <item>  GdkEventDragRequest
14016           <itemize>
14017           <item>drag_request_event
14018           </itemize>
14019
14020 <item>  GdkEventDropEnter
14021           <itemize>
14022           <item>drop_enter_event
14023           </itemize>
14024
14025 <item>  GdkEventDropLeave
14026           <itemize>
14027           <item>drop_leave_event
14028           </itemize>
14029
14030 <item>  GdkEventDropDataAvailable
14031           <itemize>
14032           <item>drop_data_available_event
14033           </itemize>
14034
14035 <item>  GdkEventClient
14036           <itemize>
14037           <item>client_event
14038           </itemize>
14039
14040 <item>  GdkEventOther
14041           <itemize>
14042           <item>other_event
14043           </itemize>
14044 </itemize>
14045
14046 The data type <tt/GdkEventType/ is a special data type that is used by
14047 all the other data types as an indicator of the data type being passed
14048 to the signal handler. As you will see below, each of the event data
14049 structures has a member of this type. It is defined as an enumeration
14050 type as follows:
14051
14052 <tscreen><verb>
14053 typedef enum
14054 {
14055   GDK_NOTHING           = -1,
14056   GDK_DELETE            = 0,
14057   GDK_DESTROY           = 1,
14058   GDK_EXPOSE            = 2,
14059   GDK_MOTION_NOTIFY     = 3,
14060   GDK_BUTTON_PRESS      = 4,
14061   GDK_2BUTTON_PRESS     = 5,
14062   GDK_3BUTTON_PRESS     = 6,
14063   GDK_BUTTON_RELEASE    = 7,
14064   GDK_KEY_PRESS         = 8,
14065   GDK_KEY_RELEASE       = 9,
14066   GDK_ENTER_NOTIFY      = 10,
14067   GDK_LEAVE_NOTIFY      = 11,
14068   GDK_FOCUS_CHANGE      = 12,
14069   GDK_CONFIGURE         = 13,
14070   GDK_MAP               = 14,
14071   GDK_UNMAP             = 15,
14072   GDK_PROPERTY_NOTIFY   = 16,
14073   GDK_SELECTION_CLEAR   = 17,
14074   GDK_SELECTION_REQUEST = 18,
14075   GDK_SELECTION_NOTIFY  = 19,
14076   GDK_PROXIMITY_IN      = 20,
14077   GDK_PROXIMITY_OUT     = 21,
14078   GDK_DRAG_BEGIN        = 22,
14079   GDK_DRAG_REQUEST      = 23,
14080   GDK_DROP_ENTER        = 24,
14081   GDK_DROP_LEAVE        = 25,
14082   GDK_DROP_DATA_AVAIL   = 26,
14083   GDK_CLIENT_EVENT      = 27,
14084   GDK_VISIBILITY_NOTIFY = 28,
14085   GDK_NO_EXPOSE         = 29,
14086   GDK_OTHER_EVENT       = 9999  /* Deprecated, use filters instead */
14087 } GdkEventType;
14088 </verb></tscreen>
14089
14090 The other event type that is different from the others is
14091 <tt/GdkEvent/ itself. This is a union of all the other
14092 data types, which allows it to be cast to a specific
14093 event data type within a signal handler.
14094
14095 <!-- Just a big list for now, needs expanding upon - TRG -->
14096 So, the event data types are defined as follows:
14097
14098 <tscreen><verb>
14099 struct _GdkEventAny
14100 {
14101   GdkEventType type;
14102   GdkWindow *window;
14103   gint8 send_event;
14104 };
14105
14106 struct _GdkEventExpose
14107 {
14108   GdkEventType type;
14109   GdkWindow *window;
14110   gint8 send_event;
14111   GdkRectangle area;
14112   gint count; /* If non-zero, how many more events follow. */
14113 };
14114
14115 struct _GdkEventNoExpose
14116 {
14117   GdkEventType type;
14118   GdkWindow *window;
14119   gint8 send_event;
14120   /* XXX: does anyone need the X major_code or minor_code fields? */
14121 };
14122
14123 struct _GdkEventVisibility
14124 {
14125   GdkEventType type;
14126   GdkWindow *window;
14127   gint8 send_event;
14128   GdkVisibilityState state;
14129 };
14130
14131 struct _GdkEventMotion
14132 {
14133   GdkEventType type;
14134   GdkWindow *window;
14135   gint8 send_event;
14136   guint32 time;
14137   gdouble x;
14138   gdouble y;
14139   gdouble pressure;
14140   gdouble xtilt;
14141   gdouble ytilt;
14142   guint state;
14143   gint16 is_hint;
14144   GdkInputSource source;
14145   guint32 deviceid;
14146   gdouble x_root, y_root;
14147 };
14148
14149 struct _GdkEventButton
14150 {
14151   GdkEventType type;
14152   GdkWindow *window;
14153   gint8 send_event;
14154   guint32 time;
14155   gdouble x;
14156   gdouble y;
14157   gdouble pressure;
14158   gdouble xtilt;
14159   gdouble ytilt;
14160   guint state;
14161   guint button;
14162   GdkInputSource source;
14163   guint32 deviceid;
14164   gdouble x_root, y_root;
14165 };
14166
14167 struct _GdkEventKey
14168 {
14169   GdkEventType type;
14170   GdkWindow *window;
14171   gint8 send_event;
14172   guint32 time;
14173   guint state;
14174   guint keyval;
14175   gint length;
14176   gchar *string;
14177 };
14178
14179 struct _GdkEventCrossing
14180 {
14181   GdkEventType type;
14182   GdkWindow *window;
14183   gint8 send_event;
14184   GdkWindow *subwindow;
14185   GdkNotifyType detail;
14186 };
14187
14188 struct _GdkEventFocus
14189 {
14190   GdkEventType type;
14191   GdkWindow *window;
14192   gint8 send_event;
14193   gint16 in;
14194 };
14195
14196 struct _GdkEventConfigure
14197 {
14198   GdkEventType type;
14199   GdkWindow *window;
14200   gint8 send_event;
14201   gint16 x, y;
14202   gint16 width;
14203   gint16 height;
14204 };
14205
14206 struct _GdkEventProperty
14207 {
14208   GdkEventType type;
14209   GdkWindow *window;
14210   gint8 send_event;
14211   GdkAtom atom;
14212   guint32 time;
14213   guint state;
14214 };
14215
14216 struct _GdkEventSelection
14217 {
14218   GdkEventType type;
14219   GdkWindow *window;
14220   gint8 send_event;
14221   GdkAtom selection;
14222   GdkAtom target;
14223   GdkAtom property;
14224   guint32 requestor;
14225   guint32 time;
14226 };
14227
14228 /* This event type will be used pretty rarely. It only is important
14229    for XInput aware programs that are drawing their own cursor */
14230
14231 struct _GdkEventProximity
14232 {
14233   GdkEventType type;
14234   GdkWindow *window;
14235   gint8 send_event;
14236   guint32 time;
14237   GdkInputSource source;
14238   guint32 deviceid;
14239 };
14240
14241 struct _GdkEventDragRequest
14242 {
14243   GdkEventType type;
14244   GdkWindow *window;
14245   gint8 send_event;
14246   guint32 requestor;
14247   union {
14248     struct {
14249       guint protocol_version:4;
14250       guint sendreply:1;
14251       guint willaccept:1;
14252       guint delete_data:1; /* Do *not* delete if link is sent, only
14253                               if data is sent */
14254       guint senddata:1;
14255       guint reserved:22;
14256     } flags;
14257     glong allflags;
14258   } u;
14259   guint8 isdrop; /* This gdk event can be generated by a couple of
14260                     X events - this lets the app know whether the
14261                     drop really occurred or we just set the data */
14262
14263   GdkPoint drop_coords;
14264   gchar *data_type;
14265   guint32 timestamp;
14266 };
14267
14268 struct _GdkEventDragBegin
14269 {
14270   GdkEventType type;
14271   GdkWindow *window;
14272   gint8 send_event;
14273   union {
14274     struct {
14275       guint protocol_version:4;
14276       guint reserved:28;
14277     } flags;
14278     glong allflags;
14279   } u;
14280 };
14281
14282 struct _GdkEventDropEnter
14283 {
14284   GdkEventType type;
14285   GdkWindow *window;
14286   gint8 send_event;
14287   guint32 requestor;
14288   union {
14289     struct {
14290       guint protocol_version:4;
14291       guint sendreply:1;
14292       guint extended_typelist:1;
14293       guint reserved:26;
14294     } flags;
14295     glong allflags;
14296   } u;
14297 };
14298
14299 struct _GdkEventDropLeave
14300 {
14301   GdkEventType type;
14302   GdkWindow *window;
14303   gint8 send_event;
14304   guint32 requestor;
14305   union {
14306     struct {
14307       guint protocol_version:4;
14308       guint reserved:28;
14309     } flags;
14310     glong allflags;
14311   } u;
14312 };
14313
14314 struct _GdkEventDropDataAvailable
14315 {
14316   GdkEventType type;
14317   GdkWindow *window;
14318   gint8 send_event;
14319   guint32 requestor;
14320   union {
14321     struct {
14322       guint protocol_version:4;
14323       guint isdrop:1;
14324       guint reserved:25;
14325     } flags;
14326     glong allflags;
14327   } u;
14328   gchar *data_type; /* MIME type */
14329   gulong data_numbytes;
14330   gpointer data;
14331   guint32 timestamp;
14332   GdkPoint coords;
14333 };
14334
14335 struct _GdkEventClient
14336 {
14337   GdkEventType type;
14338   GdkWindow *window;
14339   gint8 send_event;
14340   GdkAtom message_type;
14341   gushort data_format;
14342   union {
14343     char b[20];
14344     short s[10];
14345     long l[5];
14346   } data;
14347 };
14348
14349 struct _GdkEventOther
14350 {
14351   GdkEventType type;
14352   GdkWindow *window;
14353   gint8 send_event;
14354   GdkXEvent *xevent;
14355 };
14356 </verb></tscreen>
14357
14358 <!-- ***************************************************************** -->
14359 <sect> Code Examples
14360 <!-- ***************************************************************** -->
14361 <p>
14362 Below are the code examples that are used in the above text
14363 which are not included in complete form elsewhere.
14364
14365 <!-- ----------------------------------------------------------------- -->
14366 <sect1>Tictactoe
14367 <!-- ----------------------------------------------------------------- -->
14368 <sect2>tictactoe.h
14369 <p>
14370 <tscreen><verb>
14371 /* example-start tictactoe tictactoe.h */
14372
14373 /* GTK - The GIMP Toolkit
14374  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
14375  *
14376  * This library is free software; you can redistribute it and/or
14377  * modify it under the terms of the GNU Library General Public
14378  * License as published by the Free Software Foundation; either
14379  * version 2 of the License, or (at your option) any later version.
14380  *
14381  * This library is distributed in the hope that it will be useful,
14382  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14383  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14384  * Library General Public License for more details.
14385  *
14386  * You should have received a copy of the GNU Library General Public
14387  * License along with this library; if not, write to the
14388  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14389  * Boston, MA 02111-1307, USA.
14390  */
14391 #ifndef __TICTACTOE_H__
14392 #define __TICTACTOE_H__
14393
14394
14395 #include <gdk/gdk.h>
14396 #include <gtk/gtkvbox.h>
14397
14398
14399 #ifdef __cplusplus
14400 extern "C" {
14401 #endif /* __cplusplus */
14402
14403 #define TICTACTOE(obj)          GTK_CHECK_CAST (obj, tictactoe_get_type (), Tictactoe)
14404 #define TICTACTOE_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, tictactoe_get_type (), TictactoeClass)
14405 #define IS_TICTACTOE(obj)       GTK_CHECK_TYPE (obj, tictactoe_get_type ())
14406
14407
14408 typedef struct _Tictactoe       Tictactoe;
14409 typedef struct _TictactoeClass  TictactoeClass;
14410
14411 struct _Tictactoe
14412 {
14413   GtkVBox vbox;
14414   
14415   GtkWidget *buttons[3][3];
14416 };
14417
14418 struct _TictactoeClass
14419 {
14420   GtkVBoxClass parent_class;
14421
14422   void (* tictactoe) (Tictactoe *ttt);
14423 };
14424
14425 guint          tictactoe_get_type        (void);
14426 GtkWidget*     tictactoe_new             (void);
14427 void           tictactoe_clear           (Tictactoe *ttt);
14428
14429 #ifdef __cplusplus
14430 }
14431 #endif /* __cplusplus */
14432
14433 #endif /* __TICTACTOE_H__ */
14434
14435 /* example-end */
14436 </verb></tscreen>
14437
14438 <!-- ----------------------------------------------------------------- -->
14439 <sect2>tictactoe.c
14440 <p>
14441 <tscreen><verb>
14442 /* example-start tictactoe tictactoe.c */
14443
14444 /* GTK - The GIMP Toolkit
14445  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
14446  *
14447  * This library is free software; you can redistribute it and/or
14448  * modify it under the terms of the GNU Library General Public
14449  * License as published by the Free Software Foundation; either
14450  * version 2 of the License, or (at your option) any later version.
14451  *
14452  * This library is distributed in the hope that it will be useful,
14453  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14454  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14455  * Library General Public License for more details.
14456  *
14457  * You should have received a copy of the GNU Library General Public
14458  * License along with this library; if not, write to the
14459  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14460  * Boston, MA 02111-1307, USA.
14461  */
14462 #include "gtk/gtksignal.h"
14463 #include "gtk/gtktable.h"
14464 #include "gtk/gtktogglebutton.h"
14465 #include "tictactoe.h"
14466
14467 enum {
14468   TICTACTOE_SIGNAL,
14469   LAST_SIGNAL
14470 };
14471
14472 static void tictactoe_class_init          (TictactoeClass *klass);
14473 static void tictactoe_init                (Tictactoe      *ttt);
14474 static void tictactoe_toggle              (GtkWidget *widget, Tictactoe *ttt);
14475
14476 static gint tictactoe_signals[LAST_SIGNAL] = { 0 };
14477
14478 guint
14479 tictactoe_get_type ()
14480 {
14481   static guint ttt_type = 0;
14482
14483   if (!ttt_type)
14484     {
14485       GtkTypeInfo ttt_info =
14486       {
14487         "Tictactoe",
14488         sizeof (Tictactoe),
14489         sizeof (TictactoeClass),
14490         (GtkClassInitFunc) tictactoe_class_init,
14491         (GtkObjectInitFunc) tictactoe_init,
14492         (GtkArgSetFunc) NULL,
14493         (GtkArgGetFunc) NULL
14494       };
14495
14496       ttt_type = gtk_type_unique (gtk_vbox_get_type (), &amp;ttt_info);
14497     }
14498
14499   return ttt_type;
14500 }
14501
14502 static void
14503 tictactoe_class_init (TictactoeClass *class)
14504 {
14505   GtkObjectClass *object_class;
14506
14507   object_class = (GtkObjectClass*) class;
14508   
14509   tictactoe_signals[TICTACTOE_SIGNAL] = gtk_signal_new ("tictactoe",
14510                                          GTK_RUN_FIRST,
14511                                          object_class->type,
14512                                          GTK_SIGNAL_OFFSET (TictactoeClass, tictactoe),
14513                                          gtk_signal_default_marshaller, GTK_TYPE_NONE, 0);
14514
14515
14516   gtk_object_class_add_signals (object_class, tictactoe_signals, LAST_SIGNAL);
14517
14518   class->tictactoe = NULL;
14519 }
14520
14521 static void
14522 tictactoe_init (Tictactoe *ttt)
14523 {
14524   GtkWidget *table;
14525   gint i,j;
14526   
14527   table = gtk_table_new (3, 3, TRUE);
14528   gtk_container_add (GTK_CONTAINER(ttt), table);
14529   gtk_widget_show (table);
14530
14531   for (i=0;i<3; i++)
14532     for (j=0;j<3; j++)
14533       {
14534         ttt->buttons[i][j] = gtk_toggle_button_new ();
14535         gtk_table_attach_defaults (GTK_TABLE(table), ttt->buttons[i][j], 
14536                                    i, i+1, j, j+1);
14537         gtk_signal_connect (GTK_OBJECT (ttt->buttons[i][j]), "toggled",
14538                             GTK_SIGNAL_FUNC (tictactoe_toggle), ttt);
14539         gtk_widget_set_usize (ttt->buttons[i][j], 20, 20);
14540         gtk_widget_show (ttt->buttons[i][j]);
14541       }
14542 }
14543
14544 GtkWidget*
14545 tictactoe_new ()
14546 {
14547   return GTK_WIDGET ( gtk_type_new (tictactoe_get_type ()));
14548 }
14549
14550 void           
14551 tictactoe_clear (Tictactoe *ttt)
14552 {
14553   int i,j;
14554
14555   for (i=0;i<3;i++)
14556     for (j=0;j<3;j++)
14557       {
14558         gtk_signal_handler_block_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
14559         gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (ttt->buttons[i][j]),
14560                                      FALSE);
14561         gtk_signal_handler_unblock_by_data (GTK_OBJECT(ttt->buttons[i][j]), ttt);
14562       }
14563 }
14564
14565 static void
14566 tictactoe_toggle (GtkWidget *widget, Tictactoe *ttt)
14567 {
14568   int i,k;
14569
14570   static int rwins[8][3] = { { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
14571                              { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
14572                              { 0, 1, 2 }, { 0, 1, 2 } };
14573   static int cwins[8][3] = { { 0, 1, 2 }, { 0, 1, 2 }, { 0, 1, 2 },
14574                              { 0, 0, 0 }, { 1, 1, 1 }, { 2, 2, 2 },
14575                              { 0, 1, 2 }, { 2, 1, 0 } };
14576
14577   int success, found;
14578
14579   for (k=0; k<8; k++)
14580     {
14581       success = TRUE;
14582       found = FALSE;
14583
14584       for (i=0;i<3;i++)
14585         {
14586           success = success &amp;&amp; 
14587             GTK_TOGGLE_BUTTON(ttt->buttons[rwins[k][i]][cwins[k][i]])->active;
14588           found = found ||
14589             ttt->buttons[rwins[k][i]][cwins[k][i]] == widget;
14590         }
14591       
14592       if (success &amp;&amp; found)
14593         {
14594           gtk_signal_emit (GTK_OBJECT (ttt), 
14595                            tictactoe_signals[TICTACTOE_SIGNAL]);
14596           break;
14597         }
14598     }
14599 }
14600
14601 /* example-end */
14602 </verb></tscreen>
14603
14604 <!-- ----------------------------------------------------------------- -->
14605 <sect2>ttt_test.c
14606 <p>
14607 <tscreen><verb>
14608 /* example-start tictactoe ttt_test.c */
14609
14610 #include <gtk/gtk.h>
14611 #include "tictactoe.h"
14612
14613 void
14614 win (GtkWidget *widget, gpointer data)
14615 {
14616   g_print ("Yay!\n");
14617   tictactoe_clear (TICTACTOE (widget));
14618 }
14619
14620 int 
14621 main (int argc, char *argv[])
14622 {
14623   GtkWidget *window;
14624   GtkWidget *ttt;
14625   
14626   gtk_init (&amp;argc, &amp;argv);
14627
14628   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
14629   
14630   gtk_window_set_title (GTK_WINDOW (window), "Aspect Frame");
14631   
14632   gtk_signal_connect (GTK_OBJECT (window), "destroy",
14633                       GTK_SIGNAL_FUNC (gtk_exit), NULL);
14634   
14635   gtk_container_border_width (GTK_CONTAINER (window), 10);
14636
14637   ttt = tictactoe_new ();
14638   
14639   gtk_container_add (GTK_CONTAINER (window), ttt);
14640   gtk_widget_show (ttt);
14641
14642   gtk_signal_connect (GTK_OBJECT (ttt), "tictactoe",
14643                       GTK_SIGNAL_FUNC (win), NULL);
14644
14645   gtk_widget_show (window);
14646   
14647   gtk_main ();
14648   
14649   return 0;
14650 }
14651
14652 /* example-end */
14653 </verb></tscreen>
14654
14655 <!-- ----------------------------------------------------------------- -->
14656 <sect1> GtkDial
14657
14658 <!-- ----------------------------------------------------------------- -->
14659 <sect2> gtkdial.h
14660 <p>
14661 <tscreen><verb>
14662 /* example-start gtkdial gtkdial.h */
14663
14664 /* GTK - The GIMP Toolkit
14665  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
14666  *
14667  * This library is free software; you can redistribute it and/or
14668  * modify it under the terms of the GNU Library General Public
14669  * License as published by the Free Software Foundation; either
14670  * version 2 of the License, or (at your option) any later version.
14671  *
14672  * This library is distributed in the hope that it will be useful,
14673  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14674  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14675  * Library General Public License for more details.
14676  *
14677  * You should have received a copy of the GNU Library General Public
14678  * License along with this library; if not, write to the
14679  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14680  * Boston, MA 02111-1307, USA.
14681  */
14682 #ifndef __GTK_DIAL_H__
14683 #define __GTK_DIAL_H__
14684
14685
14686 #include <gdk/gdk.h>
14687 #include <gtk/gtkadjustment.h>
14688 #include <gtk/gtkwidget.h>
14689
14690
14691 #ifdef __cplusplus
14692 extern "C" {
14693 #endif /* __cplusplus */
14694
14695
14696 #define GTK_DIAL(obj)          GTK_CHECK_CAST (obj, gtk_dial_get_type (), GtkDial)
14697 #define GTK_DIAL_CLASS(klass)  GTK_CHECK_CLASS_CAST (klass, gtk_dial_get_type (), GtkDialClass)
14698 #define GTK_IS_DIAL(obj)       GTK_CHECK_TYPE (obj, gtk_dial_get_type ())
14699
14700
14701 typedef struct _GtkDial        GtkDial;
14702 typedef struct _GtkDialClass   GtkDialClass;
14703
14704 struct _GtkDial
14705 {
14706   GtkWidget widget;
14707
14708   /* update policy (GTK_UPDATE_[CONTINUOUS/DELAYED/DISCONTINUOUS]) */
14709   guint policy : 2;
14710
14711   /* Button currently pressed or 0 if none */
14712   guint8 button;
14713
14714   /* Dimensions of dial components */
14715   gint radius;
14716   gint pointer_width;
14717
14718   /* ID of update timer, or 0 if none */
14719   guint32 timer;
14720
14721   /* Current angle */
14722   gfloat angle;
14723
14724   /* Old values from adjustment stored so we know when something changes */
14725   gfloat old_value;
14726   gfloat old_lower;
14727   gfloat old_upper;
14728
14729   /* The adjustment object that stores the data for this dial */
14730   GtkAdjustment *adjustment;
14731 };
14732
14733 struct _GtkDialClass
14734 {
14735   GtkWidgetClass parent_class;
14736 };
14737
14738
14739 GtkWidget*     gtk_dial_new                    (GtkAdjustment *adjustment);
14740 guint          gtk_dial_get_type               (void);
14741 GtkAdjustment* gtk_dial_get_adjustment         (GtkDial      *dial);
14742 void           gtk_dial_set_update_policy      (GtkDial      *dial,
14743                                                 GtkUpdateType  policy);
14744
14745 void           gtk_dial_set_adjustment         (GtkDial      *dial,
14746                                                 GtkAdjustment *adjustment);
14747 #ifdef __cplusplus
14748 }
14749 #endif /* __cplusplus */
14750
14751
14752 #endif /* __GTK_DIAL_H__ */
14753 /* example-end */
14754 </verb></tscreen>
14755
14756 <!-- ----------------------------------------------------------------- -->
14757 <sect2> gtkdial.c
14758 <p>
14759 <tscreen><verb>
14760 /* example-start gtkdial gtkdial.c */
14761
14762 /* GTK - The GIMP Toolkit
14763  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
14764  *
14765  * This library is free software; you can redistribute it and/or
14766  * modify it under the terms of the GNU Library General Public
14767  * License as published by the Free Software Foundation; either
14768  * version 2 of the License, or (at your option) any later version.
14769  *
14770  * This library is distributed in the hope that it will be useful,
14771  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14772  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14773  * Library General Public License for more details.
14774  *
14775  * You should have received a copy of the GNU Library General Public
14776  * License along with this library; if not, write to the
14777  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14778  * Boston, MA 02111-1307, USA.
14779  */
14780 #include <math.h>
14781 #include <stdio.h>
14782 #include <gtk/gtkmain.h>
14783 #include <gtk/gtksignal.h>
14784
14785 #include "gtkdial.h"
14786
14787 #define SCROLL_DELAY_LENGTH  300
14788 #define DIAL_DEFAULT_SIZE 100
14789
14790 /* Forward declarations */
14791
14792 static void gtk_dial_class_init               (GtkDialClass    *klass);
14793 static void gtk_dial_init                     (GtkDial         *dial);
14794 static void gtk_dial_destroy                  (GtkObject        *object);
14795 static void gtk_dial_realize                  (GtkWidget        *widget);
14796 static void gtk_dial_size_request             (GtkWidget      *widget,
14797                                                GtkRequisition *requisition);
14798 static void gtk_dial_size_allocate            (GtkWidget     *widget,
14799                                                GtkAllocation *allocation);
14800 static gint gtk_dial_expose                   (GtkWidget        *widget,
14801                                                 GdkEventExpose   *event);
14802 static gint gtk_dial_button_press             (GtkWidget        *widget,
14803                                                 GdkEventButton   *event);
14804 static gint gtk_dial_button_release           (GtkWidget        *widget,
14805                                                 GdkEventButton   *event);
14806 static gint gtk_dial_motion_notify            (GtkWidget        *widget,
14807                                                 GdkEventMotion   *event);
14808 static gint gtk_dial_timer                    (GtkDial         *dial);
14809
14810 static void gtk_dial_update_mouse             (GtkDial *dial, gint x, gint y);
14811 static void gtk_dial_update                   (GtkDial *dial);
14812 static void gtk_dial_adjustment_changed       (GtkAdjustment    *adjustment,
14813                                                 gpointer          data);
14814 static void gtk_dial_adjustment_value_changed (GtkAdjustment    *adjustment,
14815                                                 gpointer          data);
14816
14817 /* Local data */
14818
14819 static GtkWidgetClass *parent_class = NULL;
14820
14821 guint
14822 gtk_dial_get_type ()
14823 {
14824   static guint dial_type = 0;
14825
14826   if (!dial_type)
14827     {
14828       GtkTypeInfo dial_info =
14829       {
14830         "GtkDial",
14831         sizeof (GtkDial),
14832         sizeof (GtkDialClass),
14833         (GtkClassInitFunc) gtk_dial_class_init,
14834         (GtkObjectInitFunc) gtk_dial_init,
14835         (GtkArgSetFunc) NULL,
14836         (GtkArgGetFunc) NULL,
14837       };
14838
14839       dial_type = gtk_type_unique (gtk_widget_get_type (), &amp;dial_info);
14840     }
14841
14842   return dial_type;
14843 }
14844
14845 static void
14846 gtk_dial_class_init (GtkDialClass *class)
14847 {
14848   GtkObjectClass *object_class;
14849   GtkWidgetClass *widget_class;
14850
14851   object_class = (GtkObjectClass*) class;
14852   widget_class = (GtkWidgetClass*) class;
14853
14854   parent_class = gtk_type_class (gtk_widget_get_type ());
14855
14856   object_class->destroy = gtk_dial_destroy;
14857
14858   widget_class->realize = gtk_dial_realize;
14859   widget_class->expose_event = gtk_dial_expose;
14860   widget_class->size_request = gtk_dial_size_request;
14861   widget_class->size_allocate = gtk_dial_size_allocate;
14862   widget_class->button_press_event = gtk_dial_button_press;
14863   widget_class->button_release_event = gtk_dial_button_release;
14864   widget_class->motion_notify_event = gtk_dial_motion_notify;
14865 }
14866
14867 static void
14868 gtk_dial_init (GtkDial *dial)
14869 {
14870   dial->button = 0;
14871   dial->policy = GTK_UPDATE_CONTINUOUS;
14872   dial->timer = 0;
14873   dial->radius = 0;
14874   dial->pointer_width = 0;
14875   dial->angle = 0.0;
14876   dial->old_value = 0.0;
14877   dial->old_lower = 0.0;
14878   dial->old_upper = 0.0;
14879   dial->adjustment = NULL;
14880 }
14881
14882 GtkWidget*
14883 gtk_dial_new (GtkAdjustment *adjustment)
14884 {
14885   GtkDial *dial;
14886
14887   dial = gtk_type_new (gtk_dial_get_type ());
14888
14889   if (!adjustment)
14890     adjustment = (GtkAdjustment*) gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
14891
14892   gtk_dial_set_adjustment (dial, adjustment);
14893
14894   return GTK_WIDGET (dial);
14895 }
14896
14897 static void
14898 gtk_dial_destroy (GtkObject *object)
14899 {
14900   GtkDial *dial;
14901
14902   g_return_if_fail (object != NULL);
14903   g_return_if_fail (GTK_IS_DIAL (object));
14904
14905   dial = GTK_DIAL (object);
14906
14907   if (dial->adjustment)
14908     gtk_object_unref (GTK_OBJECT (dial->adjustment));
14909
14910   if (GTK_OBJECT_CLASS (parent_class)->destroy)
14911     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
14912 }
14913
14914 GtkAdjustment*
14915 gtk_dial_get_adjustment (GtkDial *dial)
14916 {
14917   g_return_val_if_fail (dial != NULL, NULL);
14918   g_return_val_if_fail (GTK_IS_DIAL (dial), NULL);
14919
14920   return dial->adjustment;
14921 }
14922
14923 void
14924 gtk_dial_set_update_policy (GtkDial      *dial,
14925                              GtkUpdateType  policy)
14926 {
14927   g_return_if_fail (dial != NULL);
14928   g_return_if_fail (GTK_IS_DIAL (dial));
14929
14930   dial->policy = policy;
14931 }
14932
14933 void
14934 gtk_dial_set_adjustment (GtkDial      *dial,
14935                           GtkAdjustment *adjustment)
14936 {
14937   g_return_if_fail (dial != NULL);
14938   g_return_if_fail (GTK_IS_DIAL (dial));
14939
14940   if (dial->adjustment)
14941     {
14942       gtk_signal_disconnect_by_data (GTK_OBJECT (dial->adjustment), (gpointer) dial);
14943       gtk_object_unref (GTK_OBJECT (dial->adjustment));
14944     }
14945
14946   dial->adjustment = adjustment;
14947   gtk_object_ref (GTK_OBJECT (dial->adjustment));
14948
14949   gtk_signal_connect (GTK_OBJECT (adjustment), "changed",
14950                       (GtkSignalFunc) gtk_dial_adjustment_changed,
14951                       (gpointer) dial);
14952   gtk_signal_connect (GTK_OBJECT (adjustment), "value_changed",
14953                       (GtkSignalFunc) gtk_dial_adjustment_value_changed,
14954                       (gpointer) dial);
14955
14956   dial->old_value = adjustment->value;
14957   dial->old_lower = adjustment->lower;
14958   dial->old_upper = adjustment->upper;
14959
14960   gtk_dial_update (dial);
14961 }
14962
14963 static void
14964 gtk_dial_realize (GtkWidget *widget)
14965 {
14966   GtkDial *dial;
14967   GdkWindowAttr attributes;
14968   gint attributes_mask;
14969
14970   g_return_if_fail (widget != NULL);
14971   g_return_if_fail (GTK_IS_DIAL (widget));
14972
14973   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
14974   dial = GTK_DIAL (widget);
14975
14976   attributes.x = widget->allocation.x;
14977   attributes.y = widget->allocation.y;
14978   attributes.width = widget->allocation.width;
14979   attributes.height = widget->allocation.height;
14980   attributes.wclass = GDK_INPUT_OUTPUT;
14981   attributes.window_type = GDK_WINDOW_CHILD;
14982   attributes.event_mask = gtk_widget_get_events (widget) | 
14983     GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK | 
14984     GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK |
14985     GDK_POINTER_MOTION_HINT_MASK;
14986   attributes.visual = gtk_widget_get_visual (widget);
14987   attributes.colormap = gtk_widget_get_colormap (widget);
14988
14989   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
14990   widget->window = gdk_window_new (widget->parent->window, &amp;attributes, attributes_mask);
14991
14992   widget->style = gtk_style_attach (widget->style, widget->window);
14993
14994   gdk_window_set_user_data (widget->window, widget);
14995
14996   gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
14997 }
14998
14999 static void 
15000 gtk_dial_size_request (GtkWidget      *widget,
15001                        GtkRequisition *requisition)
15002 {
15003   requisition->width = DIAL_DEFAULT_SIZE;
15004   requisition->height = DIAL_DEFAULT_SIZE;
15005 }
15006
15007 static void
15008 gtk_dial_size_allocate (GtkWidget     *widget,
15009                         GtkAllocation *allocation)
15010 {
15011   GtkDial *dial;
15012
15013   g_return_if_fail (widget != NULL);
15014   g_return_if_fail (GTK_IS_DIAL (widget));
15015   g_return_if_fail (allocation != NULL);
15016
15017   widget->allocation = *allocation;
15018   dial = GTK_DIAL (widget);
15019
15020   if (GTK_WIDGET_REALIZED (widget))
15021     {
15022
15023       gdk_window_move_resize (widget->window,
15024                               allocation->x, allocation->y,
15025                               allocation->width, allocation->height);
15026
15027     }
15028   dial->radius = MIN(allocation->width,allocation->height) * 0.45;
15029   dial->pointer_width = dial->radius / 5;
15030 }
15031
15032 static gint
15033 gtk_dial_expose (GtkWidget      *widget,
15034                  GdkEventExpose *event)
15035 {
15036   GtkDial *dial;
15037   GdkPoint points[3];
15038   gdouble s,c;
15039   gdouble theta;
15040   gint xc, yc;
15041   gint tick_length;
15042   gint i;
15043
15044   g_return_val_if_fail (widget != NULL, FALSE);
15045   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
15046   g_return_val_if_fail (event != NULL, FALSE);
15047
15048   if (event->count > 0)
15049     return FALSE;
15050   
15051   dial = GTK_DIAL (widget);
15052
15053   gdk_window_clear_area (widget->window,
15054                          0, 0,
15055                          widget->allocation.width,
15056                          widget->allocation.height);
15057
15058   xc = widget->allocation.width/2;
15059   yc = widget->allocation.height/2;
15060
15061   /* Draw ticks */
15062
15063   for (i=0; i<25; i++)
15064     {
15065       theta = (i*M_PI/18. - M_PI/6.);
15066       s = sin(theta);
15067       c = cos(theta);
15068
15069       tick_length = (i%6 == 0) ? dial->pointer_width : dial->pointer_width/2;
15070       
15071       gdk_draw_line (widget->window,
15072                      widget->style->fg_gc[widget->state],
15073                      xc + c*(dial->radius - tick_length),
15074                      yc - s*(dial->radius - tick_length),
15075                      xc + c*dial->radius,
15076                      yc - s*dial->radius);
15077     }
15078
15079   /* Draw pointer */
15080
15081   s = sin(dial->angle);
15082   c = cos(dial->angle);
15083
15084
15085   points[0].x = xc + s*dial->pointer_width/2;
15086   points[0].y = yc + c*dial->pointer_width/2;
15087   points[1].x = xc + c*dial->radius;
15088   points[1].y = yc - s*dial->radius;
15089   points[2].x = xc - s*dial->pointer_width/2;
15090   points[2].y = yc - c*dial->pointer_width/2;
15091
15092   gtk_draw_polygon (widget->style,
15093                     widget->window,
15094                     GTK_STATE_NORMAL,
15095                     GTK_SHADOW_OUT,
15096                     points, 3,
15097                     TRUE);
15098   
15099   return FALSE;
15100 }
15101
15102 static gint
15103 gtk_dial_button_press (GtkWidget      *widget,
15104                        GdkEventButton *event)
15105 {
15106   GtkDial *dial;
15107   gint dx, dy;
15108   double s, c;
15109   double d_parallel;
15110   double d_perpendicular;
15111
15112   g_return_val_if_fail (widget != NULL, FALSE);
15113   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
15114   g_return_val_if_fail (event != NULL, FALSE);
15115
15116   dial = GTK_DIAL (widget);
15117
15118   /* Determine if button press was within pointer region - we 
15119      do this by computing the parallel and perpendicular distance of
15120      the point where the mouse was pressed from the line passing through
15121      the pointer */
15122   
15123   dx = event->x - widget->allocation.width / 2;
15124   dy = widget->allocation.height / 2 - event->y;
15125   
15126   s = sin(dial->angle);
15127   c = cos(dial->angle);
15128   
15129   d_parallel = s*dy + c*dx;
15130   d_perpendicular = fabs(s*dx - c*dy);
15131   
15132   if (!dial->button &amp;&amp;
15133       (d_perpendicular < dial->pointer_width/2) &amp;&amp;
15134       (d_parallel > - dial->pointer_width))
15135     {
15136       gtk_grab_add (widget);
15137
15138       dial->button = event->button;
15139
15140       gtk_dial_update_mouse (dial, event->x, event->y);
15141     }
15142
15143   return FALSE;
15144 }
15145
15146 static gint
15147 gtk_dial_button_release (GtkWidget      *widget,
15148                           GdkEventButton *event)
15149 {
15150   GtkDial *dial;
15151
15152   g_return_val_if_fail (widget != NULL, FALSE);
15153   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
15154   g_return_val_if_fail (event != NULL, FALSE);
15155
15156   dial = GTK_DIAL (widget);
15157
15158   if (dial->button == event->button)
15159     {
15160       gtk_grab_remove (widget);
15161
15162       dial->button = 0;
15163
15164       if (dial->policy == GTK_UPDATE_DELAYED)
15165         gtk_timeout_remove (dial->timer);
15166       
15167       if ((dial->policy != GTK_UPDATE_CONTINUOUS) &amp;&amp;
15168           (dial->old_value != dial->adjustment->value))
15169         gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
15170     }
15171
15172   return FALSE;
15173 }
15174
15175 static gint
15176 gtk_dial_motion_notify (GtkWidget      *widget,
15177                          GdkEventMotion *event)
15178 {
15179   GtkDial *dial;
15180   GdkModifierType mods;
15181   gint x, y, mask;
15182
15183   g_return_val_if_fail (widget != NULL, FALSE);
15184   g_return_val_if_fail (GTK_IS_DIAL (widget), FALSE);
15185   g_return_val_if_fail (event != NULL, FALSE);
15186
15187   dial = GTK_DIAL (widget);
15188
15189   if (dial->button != 0)
15190     {
15191       x = event->x;
15192       y = event->y;
15193
15194       if (event->is_hint || (event->window != widget->window))
15195         gdk_window_get_pointer (widget->window, &amp;x, &amp;y, &amp;mods);
15196
15197       switch (dial->button)
15198         {
15199         case 1:
15200           mask = GDK_BUTTON1_MASK;
15201           break;
15202         case 2:
15203           mask = GDK_BUTTON2_MASK;
15204           break;
15205         case 3:
15206           mask = GDK_BUTTON3_MASK;
15207           break;
15208         default:
15209           mask = 0;
15210           break;
15211         }
15212
15213       if (mods &amp; mask)
15214         gtk_dial_update_mouse (dial, x,y);
15215     }
15216
15217   return FALSE;
15218 }
15219
15220 static gint
15221 gtk_dial_timer (GtkDial *dial)
15222 {
15223   g_return_val_if_fail (dial != NULL, FALSE);
15224   g_return_val_if_fail (GTK_IS_DIAL (dial), FALSE);
15225
15226   if (dial->policy == GTK_UPDATE_DELAYED)
15227     gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
15228
15229   return FALSE;
15230 }
15231
15232 static void
15233 gtk_dial_update_mouse (GtkDial *dial, gint x, gint y)
15234 {
15235   gint xc, yc;
15236   gfloat old_value;
15237
15238   g_return_if_fail (dial != NULL);
15239   g_return_if_fail (GTK_IS_DIAL (dial));
15240
15241   xc = GTK_WIDGET(dial)->allocation.width / 2;
15242   yc = GTK_WIDGET(dial)->allocation.height / 2;
15243
15244   old_value = dial->adjustment->value;
15245   dial->angle = atan2(yc-y, x-xc);
15246
15247   if (dial->angle < -M_PI/2.)
15248     dial->angle += 2*M_PI;
15249
15250   if (dial->angle < -M_PI/6)
15251     dial->angle = -M_PI/6;
15252
15253   if (dial->angle > 7.*M_PI/6.)
15254     dial->angle = 7.*M_PI/6.;
15255
15256   dial->adjustment->value = dial->adjustment->lower + (7.*M_PI/6 - dial->angle) *
15257     (dial->adjustment->upper - dial->adjustment->lower) / (4.*M_PI/3.);
15258
15259   if (dial->adjustment->value != old_value)
15260     {
15261       if (dial->policy == GTK_UPDATE_CONTINUOUS)
15262         {
15263           gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
15264         }
15265       else
15266         {
15267           gtk_widget_draw (GTK_WIDGET(dial), NULL);
15268
15269           if (dial->policy == GTK_UPDATE_DELAYED)
15270             {
15271               if (dial->timer)
15272                 gtk_timeout_remove (dial->timer);
15273
15274               dial->timer = gtk_timeout_add (SCROLL_DELAY_LENGTH,
15275                                              (GtkFunction) gtk_dial_timer,
15276                                              (gpointer) dial);
15277             }
15278         }
15279     }
15280 }
15281
15282 static void
15283 gtk_dial_update (GtkDial *dial)
15284 {
15285   gfloat new_value;
15286   
15287   g_return_if_fail (dial != NULL);
15288   g_return_if_fail (GTK_IS_DIAL (dial));
15289
15290   new_value = dial->adjustment->value;
15291   
15292   if (new_value < dial->adjustment->lower)
15293     new_value = dial->adjustment->lower;
15294
15295   if (new_value > dial->adjustment->upper)
15296     new_value = dial->adjustment->upper;
15297
15298   if (new_value != dial->adjustment->value)
15299     {
15300       dial->adjustment->value = new_value;
15301       gtk_signal_emit_by_name (GTK_OBJECT (dial->adjustment), "value_changed");
15302     }
15303
15304   dial->angle = 7.*M_PI/6. - (new_value - dial->adjustment->lower) * 4.*M_PI/3. /
15305     (dial->adjustment->upper - dial->adjustment->lower);
15306
15307   gtk_widget_draw (GTK_WIDGET(dial), NULL);
15308 }
15309
15310 static void
15311 gtk_dial_adjustment_changed (GtkAdjustment *adjustment,
15312                               gpointer       data)
15313 {
15314   GtkDial *dial;
15315
15316   g_return_if_fail (adjustment != NULL);
15317   g_return_if_fail (data != NULL);
15318
15319   dial = GTK_DIAL (data);
15320
15321   if ((dial->old_value != adjustment->value) ||
15322       (dial->old_lower != adjustment->lower) ||
15323       (dial->old_upper != adjustment->upper))
15324     {
15325       gtk_dial_update (dial);
15326
15327       dial->old_value = adjustment->value;
15328       dial->old_lower = adjustment->lower;
15329       dial->old_upper = adjustment->upper;
15330     }
15331 }
15332
15333 static void
15334 gtk_dial_adjustment_value_changed (GtkAdjustment *adjustment,
15335                                     gpointer       data)
15336 {
15337   GtkDial *dial;
15338
15339   g_return_if_fail (adjustment != NULL);
15340   g_return_if_fail (data != NULL);
15341
15342   dial = GTK_DIAL (data);
15343
15344   if (dial->old_value != adjustment->value)
15345     {
15346       gtk_dial_update (dial);
15347
15348       dial->old_value = adjustment->value;
15349     }
15350 }
15351 /* example-end */
15352 </verb></tscreen>
15353
15354 <!-- ----------------------------------------------------------------- -->
15355 <sect1> Scribble
15356 <p>
15357 <tscreen><verb>
15358 /* example-start scribble-simple scribble-simple.c */
15359
15360 /* GTK - The GIMP Toolkit
15361  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
15362  *
15363  * This library is free software; you can redistribute it and/or
15364  * modify it under the terms of the GNU Library General Public
15365  * License as published by the Free Software Foundation; either
15366  * version 2 of the License, or (at your option) any later version.
15367  *
15368  * This library is distributed in the hope that it will be useful,
15369  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15370  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15371  * Library General Public License for more details.
15372  *
15373  * You should have received a copy of the GNU Library General Public
15374  * License along with this library; if not, write to the
15375  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15376  * Boston, MA 02111-1307, USA.
15377  */
15378
15379 #include <gtk/gtk.h>
15380
15381 /* Backing pixmap for drawing area */
15382 static GdkPixmap *pixmap = NULL;
15383
15384 /* Create a new backing pixmap of the appropriate size */
15385 static gint
15386 configure_event (GtkWidget *widget, GdkEventConfigure *event)
15387 {
15388   if (pixmap)
15389     gdk_pixmap_unref(pixmap);
15390
15391   pixmap = gdk_pixmap_new(widget->window,
15392                           widget->allocation.width,
15393                           widget->allocation.height,
15394                           -1);
15395   gdk_draw_rectangle (pixmap,
15396                       widget->style->white_gc,
15397                       TRUE,
15398                       0, 0,
15399                       widget->allocation.width,
15400                       widget->allocation.height);
15401
15402   return TRUE;
15403 }
15404
15405 /* Redraw the screen from the backing pixmap */
15406 static gint
15407 expose_event (GtkWidget *widget, GdkEventExpose *event)
15408 {
15409   gdk_draw_pixmap(widget->window,
15410                   widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
15411                   pixmap,
15412                   event->area.x, event->area.y,
15413                   event->area.x, event->area.y,
15414                   event->area.width, event->area.height);
15415
15416   return FALSE;
15417 }
15418
15419 /* Draw a rectangle on the screen */
15420 static void
15421 draw_brush (GtkWidget *widget, gdouble x, gdouble y)
15422 {
15423   GdkRectangle update_rect;
15424
15425   update_rect.x = x - 5;
15426   update_rect.y = y - 5;
15427   update_rect.width = 10;
15428   update_rect.height = 10;
15429   gdk_draw_rectangle (pixmap,
15430                       widget->style->black_gc,
15431                       TRUE,
15432                       update_rect.x, update_rect.y,
15433                       update_rect.width, update_rect.height);
15434   gtk_widget_draw (widget, &amp;update_rect);
15435 }
15436
15437 static gint
15438 button_press_event (GtkWidget *widget, GdkEventButton *event)
15439 {
15440   if (event->button == 1 &amp;&amp; pixmap != NULL)
15441     draw_brush (widget, event->x, event->y);
15442
15443   return TRUE;
15444 }
15445
15446 static gint
15447 motion_notify_event (GtkWidget *widget, GdkEventMotion *event)
15448 {
15449   int x, y;
15450   GdkModifierType state;
15451
15452   if (event->is_hint)
15453     gdk_window_get_pointer (event->window, &amp;x, &amp;y, &amp;state);
15454   else
15455     {
15456       x = event->x;
15457       y = event->y;
15458       state = event->state;
15459     }
15460     
15461   if (state &amp; GDK_BUTTON1_MASK &amp;&amp; pixmap != NULL)
15462     draw_brush (widget, x, y);
15463   
15464   return TRUE;
15465 }
15466
15467 void
15468 quit ()
15469 {
15470   gtk_exit (0);
15471 }
15472
15473 int
15474 main (int argc, char *argv[])
15475 {
15476   GtkWidget *window;
15477   GtkWidget *drawing_area;
15478   GtkWidget *vbox;
15479
15480   GtkWidget *button;
15481
15482   gtk_init (&amp;argc, &amp;argv);
15483
15484   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
15485   gtk_widget_set_name (window, "Test Input");
15486
15487   vbox = gtk_vbox_new (FALSE, 0);
15488   gtk_container_add (GTK_CONTAINER (window), vbox);
15489   gtk_widget_show (vbox);
15490
15491   gtk_signal_connect (GTK_OBJECT (window), "destroy",
15492                       GTK_SIGNAL_FUNC (quit), NULL);
15493
15494   /* Create the drawing area */
15495
15496   drawing_area = gtk_drawing_area_new ();
15497   gtk_drawing_area_size (GTK_DRAWING_AREA (drawing_area), 200, 200);
15498   gtk_box_pack_start (GTK_BOX (vbox), drawing_area, TRUE, TRUE, 0);
15499
15500   gtk_widget_show (drawing_area);
15501
15502   /* Signals used to handle backing pixmap */
15503
15504   gtk_signal_connect (GTK_OBJECT (drawing_area), "expose_event",
15505                       (GtkSignalFunc) expose_event, NULL);
15506   gtk_signal_connect (GTK_OBJECT(drawing_area),"configure_event",
15507                       (GtkSignalFunc) configure_event, NULL);
15508
15509   /* Event signals */
15510
15511   gtk_signal_connect (GTK_OBJECT (drawing_area), "motion_notify_event",
15512                       (GtkSignalFunc) motion_notify_event, NULL);
15513   gtk_signal_connect (GTK_OBJECT (drawing_area), "button_press_event",
15514                       (GtkSignalFunc) button_press_event, NULL);
15515
15516   gtk_widget_set_events (drawing_area, GDK_EXPOSURE_MASK
15517                          | GDK_LEAVE_NOTIFY_MASK
15518                          | GDK_BUTTON_PRESS_MASK
15519                          | GDK_POINTER_MOTION_MASK
15520                          | GDK_POINTER_MOTION_HINT_MASK);
15521
15522   /* .. And a quit button */
15523   button = gtk_button_new_with_label ("Quit");
15524   gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0);
15525
15526   gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
15527                              GTK_SIGNAL_FUNC (gtk_widget_destroy),
15528                              GTK_OBJECT (window));
15529   gtk_widget_show (button);
15530
15531   gtk_widget_show (window);
15532
15533   gtk_main ();
15534
15535   return 0;
15536 }
15537 /* example-end */
15538 </verb></tscreen>
15539
15540 </article>