]> Pileus Git - ~andy/gtk/blob - docs/faq/gtk-faq.sgml
Correct compile line for threads example.
[~andy/gtk] / docs / faq / gtk-faq.sgml
1 <!doctype book PUBLIC "-//OASIS//DTD DocBook V3.1//EN" []>
2 <book>
3
4 <bookinfo>
5   <date>June 25th 2003</date>
6   <title>GTK+ FAQ</title>
7   <authorgroup>
8     <author>
9 <firstname>Tony</firstname>
10 <surname>Gale</surname>
11     </author>
12     <author>
13 <firstname>Shawn</firstname>
14 <surname>Amundson</surname>
15     </author>
16     <author>
17 <firstname>Emmanuel</firstname>
18 <surname>Deloget</surname>
19     </author>
20   </authorgroup>
21   <abstract>
22     <para> This document is intended to answer questions that are
23     likely to be frequently asked by programmers using GTK+ or
24     people who are just looking at using GTK+. </para>
25     <para><emphasis>Note: </emphasis> This FAQ mainly covers GTK+ 1.2. 
26         Where the text covers GTK+ 2.x this will be indicated</para>
27   </abstract>
28 </bookinfo>
29
30 <toc></toc>
31
32 <!-- ***************************************************************** -->
33 <chapter>
34 <title>General Information</title>
35
36 <!-- ----------------------------------------------------------------- -->
37
38 <sect1>
39 <title>Note: This FAQ mainly covers GTK+ 1.2</title>
40 <para>This FAQ mainly covers GTK+ 1.2.
41 Where the text covers GTK+ 2.x this will be indicated by prefixing the
42 text with: <emphasis>[GTK+ 2.x]</emphasis>
43 </para>
44 </sect1>
45
46 <!-- ----------------------------------------------------------------- -->
47
48 <sect1>
49 <title>Before anything else: the greetings</title>
50 <para>The FAQ authors want to thank:</para>
51 <itemizedlist spacing=Compact>
52 <listitem>
53 <simpara>Havoc Pennington</simpara>
54 </listitem>
55 <listitem>
56 <simpara>Erik Mouw</simpara>
57 </listitem>
58 <listitem>
59 <simpara>Owen Taylor</simpara>
60 </listitem>
61 <listitem>
62 <simpara>Tim Janik</simpara>
63 </listitem>
64 <listitem>
65 <simpara>Thomas Mailund Jensen</simpara>
66 </listitem>
67 <listitem>
68 <simpara>Joe Pfeiffer</simpara>
69 </listitem>
70 <listitem>
71 <simpara>Andy Kahn</simpara>
72 </listitem>
73 <listitem>
74 <simpara>Federico Mena Quntero</simpara>
75 </listitem>
76 <listitem>
77 <simpara>Damon Chaplin</simpara>
78 </listitem>
79 <listitem>
80 <simpara>and all the members of the GTK+ lists</simpara>
81 </listitem></itemizedlist>
82 <para> If we forgot you, please email us! Thanks again (I know,
83 it's really short :) </para>
84 </sect1>
85
86 <!-- ----------------------------------------------------------------- -->
87
88 <sect1>
89 <title>Authors</title>
90
91 <para>The original authors of GTK+ were:</para>
92
93 <itemizedlist spacing=Compact>
94 <listitem>
95 <simpara>Peter Mattis</simpara>
96 </listitem>
97 <listitem>
98 <simpara>Spencer Kimball</simpara>
99 </listitem>
100 <listitem>
101 <simpara>Josh MacDonald</simpara>
102 </listitem>
103 </itemizedlist>
104
105 <para>Since then, much has been added by others. Please see the
106 AUTHORS file in the distribution for the GTK+ Team.</para>
107
108 </sect1>
109
110 <!-- ----------------------------------------------------------------- -->
111
112 <sect1>
113 <title>What is GTK+?</title>
114
115 <para>GTK+ is a small and efficient widget set designed with
116 the general look and feel of Motif.  In reality, it looks much
117 better than Motif.  It contains common widgets and some more
118 complex widgets such as a file selection, and color selection
119 widgets.</para>
120
121 <para>GTK+ provides some unique features. (At least, I know of
122 no other widget library which provides them). For example, a
123 button does not contain a label, it contains a child widget,
124 which in most instances will be a label.  However, the child
125 widget can also be a pixmap, image or any combination possible
126 the programmer desires. This flexibility is adhered to
127 throughout the library.</para>
128
129 </sect1>
130
131 <!-- ----------------------------------------------------------------- -->
132
133 <sect1>
134 <title>What is the + in GTK+?</title>
135
136 <para>Peter Mattis informed the gtk mailing list that:</para>
137
138 <para><quote>I originally wrote gtk which included the three
139 libraries, libglib, libgdk and libgtk. It featured a flat
140 widget hierarchy. That is, you couldn't derive a new widget
141 from an existing one. And it contained a more standard
142 callback mechanism instead of the signal mechanism now present
143 in gtk+. The + was added to distinguish between the original
144 version of gtk and the new version. You can think of it as
145 being an enhancement to the original gtk that adds object
146 oriented features.</quote></para>
147
148 </sect1>
149
150 <!-- ----------------------------------------------------------------- -->
151
152 <sect1>
153 <title>Does the G in GTK+, GDK and GLib stand for?</title>
154
155 <para>GTK+ == Gimp Toolkit</para>
156 <para>GDK == GTK+ Drawing Kit</para>
157 <para>GLib == G Library</para>
158
159 </sect1>
160
161 <!-- ----------------------------------------------------------------- -->
162
163 <sect1>
164 <title>Where is the documentation for GTK+?</title>
165
166 <para>In the GTK+ distribution's doc/ directory you will find
167 the reference material for both GTK and GDK, this FAQ and the
168 GTK Tutorial.</para>
169
170 <para>In addition, you can find links to HTML versions of
171 these documents  by going to <ulink url="http://www.gtk.org/">
172 http://www.gtk.org/</ulink>. A
173 packaged version of the GTK Tutorial, with SGML, HTML,
174 Postscript, DVI and text versions can be found in <ulink
175 url="ftp://ftp.gtk.org/pub/gtk/tutorial">
176 ftp://ftp.gtk.org/pub/gtk/tutorial
177 </ulink></para>
178
179 <para>There are now a couple of books available that deal with
180 programming GTK+, GDK and GNOME:</para>
181
182 <itemizedlist>
183 <listitem><simpara>Eric Harlows book entitled "Developing
184 Linux Applications with GTK+ and GDK". The ISBN is
185 0-7357-0021-4</simpara>
186 </listitem>
187 <listitem><simpara>The example code from Eric's book is
188 available on-line at <ulink
189 url="http://www.bcpl.net/~eharlow/book">
190 http://www.bcpl.net/~eharlow/book</ulink></simpara>
191 </listitem>
192 <listitem><simpara>Havoc Pennington has released a book called
193 "GTK+/GNOME Application Development". The ISBN is
194 0-7357-0078-8</simpara>
195 <simpara>The free version of the book lives here: <ulink
196 url="http://developer.gnome.org/doc/GGAD/">
197 http://developer.gnome.org/doc/GGAD/
198 </ulink></simpara>
199 <simpara>And Havoc maintains information about it and
200 errata here: <ulink
201 url="http://pobox.com/~hp/gnome-app-devel.html">
202 http://pobox.com/~hp/gnome-app-devel.html
203 </ulink></simpara>
204 </listitem>
205 </itemizedlist>
206
207 </sect1>
208
209 <!-- ----------------------------------------------------------------- -->
210
211 <sect1>
212 <title>Is there a mailing list (or mailing list archive) for
213 GTK+?</title>
214
215 <para>Information on mailing lists relating to GTK+ can be
216 found at: <ulink
217 url="http://www.gtk.org/mailinglists.html">
218 http://www.gtk.org/mailinglists.html
219 </ulink></para>
220
221 </sect1>
222
223 <!-- ----------------------------------------------------------------- -->
224
225 <sect1>
226 <title>How to get help with GTK+</title>
227
228 <para>First, make sure your question isn't answered in the
229 documentation, this FAQ or the tutorial. Done that? You're
230 sure you've done that, right? In that case, the best place to
231 post questions is to the GTK+ mailing list.</para>
232
233 </sect1>
234
235 <!-- ----------------------------------------------------------------- -->
236
237 <sect1>
238 <title>How to report bugs in GTK+</title>
239
240 <para>Bugs should be reported to the GNOME bug tracking system
241 (<ulink url="http://bugzilla.gnome.org">http://bugzilla.gnome.org</ulink>).
242 You will need to enter your email address and receive a password before
243 you can use the system to register a new bug report.</para>
244
245 <para>There are a number of options to select and boxes to fill in when
246 submitting a bug report. Please remember that the more information you
247 give, the easier it will be to track the problem down. Extra information
248 that may prove useful includes:</para>
249
250 <itemizedlist>
251 <listitem><simpara> How to reproduce the bug.</simpara>
252 <simpara>If you can reproduce it with the testgtk program
253 that is built in the gtk/ subdirectory, that will be most
254 convenient. Otherwise, please include a short test program
255 that exhibits the behavior. As a last resort, you can also
256 provide a pointer to a larger piece of software that can
257 be downloaded.</simpara>
258 <simpara>(Bugs that can be reproduced within the GIMP are
259 almost as good as bugs that can be reproduced in
260 testgtk. If you are reporting a bug found with the GIMP,
261 please include the version number of the GIMP you are
262 using)</simpara>
263 </listitem>
264
265 <listitem><simpara> If the bug was a crash, the exact text that was
266 printed out when the crash occurred.</simpara>
267 </listitem>
268
269 <listitem><simpara> Further information such as stack traces
270 may be useful, but are not necessary. If you do send a stack trace,
271 and the error is an X error, it will be more useful if the stacktrace is
272 produced running the test program with the <literal>--sync</literal>
273 command line option.</simpara>
274 </listitem>
275 </itemizedlist>
276
277 </sect1>
278
279 <!-- ----------------------------------------------------------------- -->
280
281 <sect1>
282 <title>Is there a Windows version of GTK+?</title>
283
284 <para>There is an on going port of GTK+ to the Windows
285 platform which is making impressive progress.</para>
286
287 <para>See <ulink
288 url="http://www.iki.fi/tml/gimp/win32">
289 http://www.iki.fi/tml/gimp/win32</ulink>
290 for more information.</para>
291
292 </sect1>
293
294 <!-- ----------------------------------------------------------------- -->
295
296 <sect1>
297 <title>What applications have been written with GTK+?</title>
298
299 <para>A list of some GTK+ based application can be found on
300 the GTK+ web server at <ulink
301 url="http://www.gtk.org/apps/">http://www.gtk.org/apps/</ulink>
302 and contains more than 350 applications.</para>
303
304 <para>Failing that, look for a project to work on for the
305 GNOME project, <ulink
306 url="http://www.gnome.org/">http://www.gnome.org/</ulink>
307 Write a game. Write something that is useful.</para>
308 <para>Some of these are:</para>
309
310 <itemizedlist>
311 <listitem><simpara> GIMP (<ulink
312 url="http://www.gimp.org/">http://www.gimp.org/</ulink>), an
313 image manipulation program</simpara>
314 </listitem>
315 <listitem><simpara> AbiWord (<ulink
316 url="http://www.abisource.com/">http://www.abisource.com/</ulink>),
317 a professional word processor</simpara>
318 </listitem>
319 <listitem><simpara> Gzilla (<ulink
320 url="http://www.levien.com/gzilla/">http://www.levien.com/gzilla/</ulink>),
321 a web browser</simpara>
322 </listitem>
323 <listitem><simpara> XQF (<ulink
324 url="http://www.botik.ru/~roma/quake/">
325 http://www.botik.ru/~roma/quake/</ulink>),
326 a QuakeWorld/Quake2 server browser and launcher</simpara>
327 </listitem>
328 <listitem><simpara> GDK Imlib (<ulink
329 url="http://www.rasterman.com/imlib.html">
330 http://www.rasterman.com/imlib.html</ulink>),
331 a fast image loading and manipulation library for GDK</simpara>
332 </listitem>
333 <listitem><simpara> Glade (<ulink
334 url="http://glade.pn.org/">http://glade.pn.org/</ulink>), a
335 GTK+ based RAD tool which produces GTK+ applications</simpara>
336 </listitem>
337 </itemizedlist>
338 </sect1>
339
340 <!-- ----------------------------------------------------------------- -->
341
342 <sect1>
343 <title>I'm looking for an application to write in GTK+. How
344 about an IRC client?</title>
345
346 <para>Ask on gtk-list for suggestions. There are at least
347 three IRC clients already under development (probably more in fact. The
348 server at <ulink url="http://www.forcix.cx/irc-clients.html">
349 http://www.forcix.cx/irc-clients.html</ulink> list a bunch of
350 them).</para>
351
352 <itemizedlist spacing=compact>
353 <listitem><simpara> X-Chat.</simpara>
354 </listitem>
355 <listitem><simpara> girc. (Included with GNOME)</simpara>
356 </listitem>
357 <listitem><simpara> gsirc. (In the gnome CVS tree)</simpara>
358 </listitem>
359 </itemizedlist>
360
361 </sect1>
362
363 </chapter>
364
365 <!-- ***************************************************************** -->
366 <chapter>
367 <title>How to find, configure, install, and troubleshoot GTK+</title>
368
369 <!-- ----------------------------------------------------------------- -->
370
371 <sect1>
372 <title>What do I need to run GTK+?</title>
373
374 <para>To compile GTK+, all you need is a C compiler (gcc) and
375 the X Window System and associated libraries on your system.</para>
376
377 </sect1>
378
379 <!-- ----------------------------------------------------------------- -->
380
381 <sect1>
382 <title>Where can I get GTK+?</title>
383
384 <para>The canonical site is <ulink
385 url="ftp://ftp.gtk.org/pub/gtk">ftp://ftp.gtk.org/pub/gtk</ulink>.</para>
386
387 <para>This site tends to get busy around the time of a new
388 GTK+ release so try and use one of the mirror sites that are
389 listed in <ulink
390 url="ftp://ftp.gtk.org/etc/mirrors">ftp://ftp.gtk.org/etc/mirrors
391 </ulink></para>
392
393 <para>Here's a few mirror sites to get you started:</para>
394
395 <itemizedlist spacing=compact>
396 <listitem><simpara> Africa -
397 <ulink url="ftp://ftp.is.co.za/applications/gimp">
398 ftp://ftp.is.co.za/applications/gimp</ulink></simpara> 
399 </listitem>
400 <listitem><simpara> Australia -
401 <ulink
402 url="ftp://ftp.au.gimp.org/pub/gimp">
403 ftp://ftp.au.gimp.org/pub/gimp</ulink></simpara> 
404 </listitem>
405 <listitem><simpara> Finland -
406 <ulink url="ftp://ftp.funet.fi/pub/sci/graphics/packages/gimp">
407 ftp://ftp.funet.fi/pub/sci/graphics/packages/gimp</ulink></simpara> 
408 </listitem>
409 <listitem><simpara> Germany -
410 <ulink url="ftp://infosoc.uni-koeln.de/pub/ftp.gimp.org">
411 ftp://infosoc.uni-koeln.de/pub/ftp.gimp.org"</ulink></simpara> 
412 </listitem>
413 <listitem><simpara> Japan -
414 <ulink url="ftp://SunSITE.sut.ac.jp/pub/archives/packages/gimp">
415 ftp://SunSITE.sut.ac.jp/pub/archives/packages/gimp</ulink></simpara> 
416 </listitem>
417 <listitem><simpara> UK - 
418 <ulink url="ftp://ftp.flirble.org/pub/X/gimp">
419 ftp://ftp.flirble.org/pub/X/gimp</ulink></simpara>
420 </listitem>
421 <listitem><simpara> US -
422 <ulink url="ftp://ftp.insync.net/pub/mirrors/ftp.gimp.org">
423 ftp://ftp.insync.net/pub/mirrors/ftp.gimp.org</ulink></simpara> 
424 </listitem>
425 </itemizedlist>
426 </sect1>
427
428 <!-- ----------------------------------------------------------------- -->
429
430 <sect1>
431 <title>How do I configure/compile GTK+?</title>
432
433 <para>Generally, all you will need to do is issue the commands:</para>
434
435 <para><literallayout><literal>./configure</literal>
436 <literal>make</literal></literallayout></para>
437
438 <para>in the gtk+-version/ directory.</para>
439 </sect1>
440
441 <!-- ----------------------------------------------------------------- -->
442
443 <sect1>
444 <title>When compiling GTK+ I get an error like: <literal>make:
445 file `Makefile' line 456: Syntax error</literal></title>
446
447 <para>Make sure that you are using GNU make
448 (use <literal>make -v</literal>
449 to check). There are many weird and wonderful versions of make
450 out there, and not all of them handle the automatically
451 generated Makefiles.</para>
452
453 </sect1>
454
455 <!-- ----------------------------------------------------------------- -->
456
457 <sect1>
458 <title>I've compiled and installed GTK+, but I can't get any
459 programs to link with it! <emphasis>[GTK 2.x]</emphasis></title>
460
461 <para>This problem is most often encountered when the GTK+
462 libraries can't be  found or are the wrong version. Generally,
463 the compiler will complain about an 'unresolved symbol'.</para>
464
465 <para>Make sure that the libraries can be
466 found. You want to edit <filename>/etc/ld.so.conf</filename> to
467 include the directories which contain the GTK libraries,
468 so it looks something like:</para>
469
470 <para><literallayout><literal>/usr/X11R6/lib</literal>
471 <literal>/usr/local/lib</literal></literallayout></para>
472
473 <para>Then you need to run /sbin/ldconfig as root. You can
474 find what libraries GTK requires using</para>
475
476 <para><literallayout><literal>pkg-config gtk+-2.0 --libs</literal>
477 </literallayout></para>
478
479 <para>If your system doesn't use ld.so to find libraries
480 (such as Solaris), then you will have to use the LD_LIBRARY_PATH
481 environment variable (or compile the path into your program, which I'm
482 not going to cover here). So, with a Bourne type shell you can do (if
483 your GTK libraries are in /usr/local/lib):</para>
484
485 <para><literallayout>
486 <literal>export LD_LIBRARY_PATH=/usr/local/lib</literal>
487 </literallayout></para>
488
489 <para>and in a csh, you can do:</para>
490
491 <para><literallayout>
492 <literal>setenv LD_LIBRARY_PATH /usr/local/lib</literal>
493 </literallayout></para>
494
495 </sect1>
496
497 <!-- ----------------------------------------------------------------- -->
498
499 <sect1>
500 <title>When installing a GTK+ application, configure reports
501 that it can't find GTK. <emphasis>[GTK 2.x]</emphasis></title>
502
503 <para>There are several common reasons for this:</para>
504
505 <itemizedlist>
506 <listitem><simpara>You have an old version of GTK installed
507 somewhere. You should remove this old copy, but note that
508 this may break applications that have been compiled against
509 the old version.</simpara>
510 </listitem>
511
512 <listitem><simpara><literal>pkg-config</literal> (or another
513 component of GTK) isn't in your path, or there is an old
514 version on your system. Type:</simpara>
515
516 <para><literallayout>
517 <literal>pkg-config gtk+-2.0 --version</literal>
518 </literallayout></para>
519
520 <para>to check for both of these. If it returns a value
521 different from what you expect, then you have an old
522 version of GTK on your system.</para>
523 </listitem>
524
525 <listitem><simpara>The ./configure script can't find the GTK
526 libraries. As ./configure compiles various test programs, it needs to be
527 able to find the GTK libraries. See the question above
528 for help on this. </simpara></listitem>
529 </itemizedlist>
530
531 <para>If none of the above help, then have a look in
532 config.log, which is generated by ./configure as it runs. At the
533 bottom will be the last action it took before failing. If it is a
534 section of source code, copy the source code to a file and compile it
535 with the line just above it in config.log. If the compilation is
536 successful, try executing it.</para>
537
538 </sect1>
539
540 </chapter>
541
542 <!-- ***************************************************************** -->
543 <chapter>
544 <title>Development of GTK+</title>
545
546 <!-- ----------------------------------------------------------------- -->
547
548 <sect1>
549 <title>Whats this CVS thing that everyone keeps talking about,
550 and how do I access it?</title>
551
552 <para>CVS is the Concurrent Version System and is a very
553 popular means of version control for software projects. It is
554 designed to allow multiple  authors to be able to
555 simultanously operate on the same source tree.  This source
556 tree is centrally maintained, but each developer has a local
557 mirror of this repository that they make their changes to.</para>
558
559 <para>The GTK+ developers use a CVS repository to store the
560 master copy of the current development version of GTK+. As
561 such, people wishing to contribute patches to GTK+ should
562 generate them against the CVS version. Normal people should
563 use the packaged releases.</para>
564
565 <para>The CVS toolset is available as RPM packages from the
566 usual RedHat sites. The latest version is available at <ulink
567 url="http://download.cyclic.com/pub/">http://download.cyclic.com/pub/
568 </ulink></para>
569
570 <para>Anyone can download the latest CVS version of GTK+ by
571 using anonymous access using the following steps:</para>
572
573 <itemizedlist>
574 <listitem><simpara> In a bourne shell descendant (e.g. bash) type:</simpara>
575 <para><literallayout>
576 <literal>CVSROOT=':pserver:anonymous@anoncvs.gnome.org:/cvs/gnome'</literal>
577 <literal>export CVSROOT</literal>
578 </literallayout></para>
579 </listitem>
580
581 <listitem><simpara>Next, the first time the source tree is
582 checked out, a cvs login is needed. </simpara>
583 <para><literallayout>
584 <literal>cvs login</literal>
585 </literallayout></para>
586 <para>This will ask you for a password. There is no
587 password for cvs.gimp.org, so just enter a carriage return.</para>
588 </listitem>
589
590 <listitem><simpara>To get the tree and place it in a subdir of your
591 current working directory, issue the command:</simpara>
592 <para><literallayout>
593 <literal>cvs -z3 get gtk+</literal>
594 </literallayout></para>
595 <para>Note that with the GTK+ 1.1 tree, glib has been moved to
596 a separate CVS module, so if you don't have glib installed you will
597 need to get that as well:</para>
598 <para><literallayout>
599 <literal>cvs -z3 get glib</literal>
600 </literallayout></para>
601 </listitem>
602 </itemizedlist>
603
604 </sect1>
605
606 <!-- ----------------------------------------------------------------- -->
607
608 <sect1>
609 <title>How can I contribute to GTK+?</title>
610
611 <para>It's simple. If something doesn't work like you think it
612 should in a program, check the documentation to make sure
613 you're not missing something. If it is a true bug or missing
614 feature, track it down in the GTK+ source, change it,  and
615 then generate a patch in the form of a 'context diff'. This
616 can be done using a command such as <literal>diff -ru
617 &lt;oldfile&gt; &lt;newfile&gt;</literal>. Then upload the patchfile
618 to:</para>
619
620 <para><literallayout>
621 <literal>ftp://ftp.gtk.org/incoming</literal>
622 </literallayout></para>
623
624 <para>along with a README file. Make sure you follow the
625 naming conventions or your patch will just be deleted! The
626 filenames should be of this form:</para>
627
628 <para><literallayout>
629 <literal>gtk&lt;username&gt;-&lt;date yymmdd-n&gt;.patch.gz</literal>
630 <literal>gtk-&lt;username&gt;-&lt;date yymmdd-n&gt;.patch.README</literal>
631 </literallayout></para>
632
633 <para>The "n" in the date indicates a unique number (starting
634 from 0) of patches you uploaded that day.  It should be 0,
635 unless you upload more than one patch in the same day.</para>
636
637 <para>Example:</para>
638
639 <para><literallayout>
640 <literal>gtk-gale-982701-0.patch.gz</literal>
641 <literal>gtk-gale-982701-0.patch.README</literal>
642 </literallayout></para>
643
644 <para>Once you upload <emphasis>anything</emphasis>, send the README to
645 ftp-admin@gtk.org</para>
646
647 </sect1>
648
649 <!-- ----------------------------------------------------------------- -->
650
651 <sect1>
652 <title>How do I know if my patch got applied, and if not, why
653 not?</title>
654
655 <para>Uploaded patches will be moved to
656 <filename>ftp://ftp.gtk.org/pub/gtk/patches</filename> where one of the
657 GTK+ development team will pick them up. If applied, they will be moved
658 to <filename>/pub/gtk/patches/old</filename>.</para>
659
660 <para>Patches that aren't applied, for whatever reason, are
661 moved to <filename>/pub/gtk/patches/unapplied</filename> or
662 <filename>/pub/gtk/patches/outdated</filename>. At this point you can ask
663 on the <literal>gtk-list</literal> mailing list why your patch wasn't
664 applied. There are many possible reasons why patches may not
665 be applied, ranging from it doesn't apply cleanly, to it isn't
666 right. Don't be put off if your patch didn't make it first
667 time round.</para>
668
669 </sect1>
670
671 <!-- ----------------------------------------------------------------- -->
672
673 <sect1>
674 <title>What is the policy on incorporating new widgets into
675 the library?</title>
676
677 <para>This is up to the authors, so you will have to ask them
678 once you are done with your widget. As a general guideline,
679 widgets that are  generally useful, work, and are not a
680 disgrace to the widget set will gladly be included.</para>
681
682 </sect1>
683
684 <!-- ----------------------------------------------------------------- -->
685
686 <sect1>
687 <title>Is anyone working on bindings for languages other than
688 C?</title>
689
690 <para>The GTK+ home page (<ulink
691 url="http://www.gtk.org/">http://www.gtk.org/</ulink>)
692 presents a list of GTK+ bindings.</para>
693
694 <itemizedlist>
695 <listitem><simpara>There are several C++ wrappers for GTK+.</simpara>
696
697   <itemizedlist> 
698   <listitem><simpara>the gtkmm package, which is a wrapper for
699   GTK+. You can find the home page at <ulink
700   url="http://www.gtkmm.org">
701   http://www.gtkmm.org/</ulink>.</simpara>
702   </listitem>
703
704   <listitem><simpara>the VDK package, which was built as
705   the base package of a GTK+ application Borland-like
706   builder. The home page can be found at <ulink
707   url="http://vdkbuilder.sourceforge.net/">
708   http://vdkbuilder.sourceforge.net/</ulink>.</simpara>
709   </listitem>
710
711   <listitem><simpara>The wxWindows/Gtk package, a free C++ library for
712   cross-platform GUI development. The home page of this package is
713   <ulink url="http://www.wxwindows.org/">
714   http://www.wxwindows.org/</ulink>.</simpara>
715   </listitem>
716
717   </itemizedlist>
718 </listitem>
719
720 <listitem><simpara>There are three known Objective-c
721 bindings currently in development:</simpara>
722
723   <itemizedlist> 
724   <listitem><simpara>The <ulink
725   url="http://www.gnome.org/">http://www.gnome.org/</ulink>
726   package of choice is objgtk. Objgtk is based on the Object class and
727   is maintained by <ulink url="mailto:sopwith@cuc.edu">Elliot
728   Lee</ulink>. Apparently, objgtk is being accepted as the `standard'
729   Objective-C binding for GTK+.</simpara>
730   </listitem>
731
732   <listitem><simpara>If you are more inclined towards the 
733   <ulink url="http://www.gnustep.org/">GNUstep project</ulink>,
734   you may want to check out GTKKit by 
735   <ulink url="mailto:helge@mdlink.de">Helge He&szlig;</ulink>.
736   The intention is to setup a GTK+ binding using the FoundationKit. 
737   GTKKit includes nicities like writing a XML-type template file to 
738   construct a GTK+ interface.</simpara>
739   </listitem>
740
741   <listitem><simpara>The GToolKit package, which can be found at
742   <ulink url="ftp://ftp.gtk.org/pub/gtk/objc-gtoolkit/">
743   ftp://ftp.gtk.org/pub/gtk/objc-gtoolkit/</ulink>.</simpara>
744   </listitem>
745   
746   </itemizedlist>
747 </listitem>
748
749 <listitem><simpara>Perl bindings <ulink
750 url="ftp://ftp.gtk.org/pub/gtk/perl">
751 ftp://ftp.gtk.org/pub/gtk/perl</ulink></simpara>
752 </listitem>
753
754 <listitem><simpara>Guile bindings. The home page is at
755 <ulink url="http://www.ping.de/sites/zagadka/guile-gtk">
756 http://www.ping.de/sites/zagadka/guile-gtk</ulink>.
757 By the way, Guile is the GNU Project's implemention of R4RS Scheme (the
758 standard). If you like Scheme, you may want to take a look at
759 this.</simpara>
760 </listitem>
761
762 <listitem><simpara>David Monniaux reports:
763 <quote>I've started a gtk-O'Caml binding system.
764 The basics of the system, including callbacks, work fine.
765
766 The current development is in
767 <ulink url="http://www.ens-lyon.fr/~dmonniau/arcs">
768 http://www.ens-lyon.fr/~dmonniau/arcs</ulink>
769 </quote></simpara>
770 </listitem>
771
772 <listitem><simpara>Several python bindings have been done:</simpara>
773
774   <itemizedlist>
775   <listitem><simpara>pygtk is at 
776   <ulink url="http://www.daa.com.au/~james/pygtk">
777   http://www.daa.com.au/~james/pygtk</ulink> and 
778   <ulink url="ftp://ftp.gtk.org/pub/gtk/python">
779   ftp://ftp.gtk.org/pub/gtk/python</ulink></simpara>
780   </listitem>
781
782   <listitem><simpara>python-gtk is at
783   <ulink url="http://www.ucalgary.ca/~nascheme/python-gtk">
784   http://www.ucalgary.ca/~nascheme/python-gtk</ulink></simpara>
785   </listitem>
786   
787   </itemizedlist>
788 </listitem>
789
790 <listitem><simpara>There's are a couple of OpenGL/Mesa
791 widgets available for GTK+. I suggest you start at
792 <ulink url="http://www.student.oulu.fi/~jlof/gtkglarea/index.html">
793 http://www.student.oulu.fi/~jlof/gtkglarea/index.html</ulink></simpara>
794 </listitem>
795 <listitem><simpara>Last, there are a lot of other language
796 bindings for languages such as Eiffel, TOM, Pascal, Pike, etc.</simpara>
797 </listitem>
798
799 </itemizedlist>
800 </sect1>
801
802 </chapter>
803
804 <!-- ***************************************************************** -->
805 <chapter>
806 <title>Development with GTK+: the begining</title>
807
808 <!-- ----------------------------------------------------------------- -->
809
810 <sect1>
811 <title>How do I get started?</title>
812
813 <para>So, after you have installed GTK+ there are a couple of
814 things that can ease you into developing applications with
815 it. There is the GTK+ Tutorial <ulink
816 url="http://www.gtk.org/tutorial/">
817 http://www.gtk.org/tutorial/</ulink>, which is undergoing
818 development. This will introduce you to writing applications
819 using C.</para>
820
821 <para>The Tutorial doesn't (yet) contain information on all of
822 the widgets that are in GTK+. For example code on how to use
823 the basics of all the GTK+ widgets you should look at the file
824 gtk/testgtk.c (and associated source files) within the GTK+
825 distribution. Looking at these examples will give you a good
826 grounding on what the widgets can do.</para>
827
828 </sect1>
829
830 <!-- ----------------------------------------------------------------- -->
831
832 <sect1>
833 <title>How do I use the Glade GUI builder with GTK+? <emphasis>[GTK 2.x]</emphasis></title>
834
835 <para>There are two ways to use Glade. The first way is to use
836 Glade's facilities for generating code; the second
837 way is to use the libglade library which directly loads
838 the XML user interface description files that Glade
839 generates into a running program.</para>
840
841 <para>Experienced GTK+ programmers generally strongly recommend
842 using libglade; you don't have to worry about the interaction
843 between Glade generating the source and you editing it,
844 and its been shown to be a method that works better
845 for large projects, so there is a lot of example code
846 out there you can look at.</para>
847
848 <para>An introduction to using libglade can be found in the 
849 libglade API docs
850 (<ulink url="http://developer.gnome.org/doc/API/2.0/libglade/libglade-notes.html#libglade-basics">
851 http://developer.gnome.org/doc/API/2.0/libglade/libglade-notes.html#libglade-basics</ulink>)
852 .</para>
853
854 </sect1>
855
856 <!-- ----------------------------------------------------------------- -->
857
858 <sect1>
859 <title>How do I write security sensitive/SUID/SGID programs with GTK+?
860 Is GTK+ secure? What's this GTK_MODULES security hole I heard about?</title>
861
862 <para>The short answer to this question is: Don't write SUID/SGID
863 programs with GTK+</para>
864
865 <para>For a more thorough explanation of the GTK+ Developers position on
866 this issue see <ulink
867 url="http://www.gtk.org/setuid.html">http://www.gtk.org/setuid.html</ulink>.</para>
868
869 </sect1>
870
871 <!-- ----------------------------------------------------------------- -->
872
873 <sect1>
874 <title>I tried to compile a small <command>Hello World</command> of mine,
875 but it failed. Any clue? <emphasis>[GTK 2.x]</emphasis></title>
876
877 <para>Since you are good at coding, we will not deal with
878 compile time error here :)</para>
879
880 <para>The classic command line to compile a GTK+ based program is</para>
881 <para><literallayout>
882 <literal>gcc -o myprog [c files] `pkg-config gtk+-2.0 --cflags --libs`</literal>
883 </literallayout></para>
884
885 <para>You should notice the backquote character which is used
886 in this command line. A common mistake when you start a GTK+
887 based development is to use quote instead of backquotes. If
888 you do so, the compiler will complain about an unknown file
889 called <filename>pkg-config gtk+-2.0 --cflags --libs</filename>. The
890 text in backquotes is an instruction to your shell to
891 substitute the output of executing this command into the
892 commandline.</para>
893
894 <para>The command line above ensures that:</para>
895
896 <itemizedlist>
897 <listitem><simpara>the correct C compiler flags will be used
898 to compile the program (including the complete C header
899 directory list)</simpara>
900 </listitem>
901
902 <listitem><simpara>your program will be linked with the
903 needed libraries.</simpara>
904 </listitem>
905 </itemizedlist>
906
907 </sect1>
908
909 <!-- ----------------------------------------------------------------- -->
910
911 <sect1>
912 <title>What about using the <command>make</command>
913 utility? <emphasis>[GTK 2.x]</emphasis></title>
914
915 <para>This is a sample makefile which compile a GTK+ based
916 program:</para>
917
918 <programlisting role="C">
919 # basic GTK+ app makefile
920 SOURCES = myprg.c foo.c bar.c
921 OBJS    = ${SOURCES:.c=.o}
922 CFLAGS  = `pkg-config gtk+-2.0 --cflags`
923 LDADD   = `pkg-config gtk+-2.0 --libs`
924 CC      = gcc
925 PACKAGE = myprg
926
927 all : ${OBJS}
928   ${CC} -o ${PACKAGE} ${OBJS} ${LDADD}
929
930 .c.o:
931   ${CC} ${CFLAGS} -c $<
932
933 # end of file
934 </programlisting>
935
936 <para>For more information about the <command>make</command> utility, you
937 should read either the related man page or the relevant info file.</para>
938
939 </sect1>
940
941 <!-- ----------------------------------------------------------------- -->
942
943 <sect1>
944 <title>I use the backquote stuff in my makefiles, but my make
945 process failed.</title>
946
947 <para>The backquote construction seems to not be accepted by
948 some old <command>make</command> utilities. If you use one of these, the
949 make process will probably fail. In order to have the
950 backquote syntax working again, you should use the GNU make
951 utility (get it on the GNU ftp server at <ulink
952 url="ftp://ftp.gnu.org/">ftp://ftp.gnu.org/</ulink>).</para>
953
954 </sect1>
955
956 <!-- ----------------------------------------------------------------- -->
957
958 <sect1>
959 <title>I want to add some configure stuff, how could I do
960 this?</title>
961
962 <para>To use autoconf/automake, you must first install the
963 relevant packages. These are:</para>
964
965 <itemizedlist spacing=Compact>
966 <listitem><simpara>the m4 preprocessor v1.4 or better</simpara>
967 </listitem>
968
969 <listitem><simpara>autoconf v2.13 or better</simpara>
970 </listitem>
971
972 <listitem><simpara>automake v1.4 or better</simpara>
973 </listitem>
974 </itemizedlist>
975
976 <para>You'll find these packages on the GNU main ftp server
977 (<ulink url="ftp://ftp.gnu.org/">ftp://ftp.gnu.org/</ulink>)
978 or on any GNU mirror.</para>
979
980 <para>In order to use the powerful autoconf/automake scheme,
981 you must create a configure.in which may look like:</para>
982
983 <programlisting role="C">
984 dnl Process this file with autoconf to produce a configure script.
985 dnl configure.in for a GTK+ based program
986
987 AC_INIT(myprg.c)dnl
988 AM_INIT_AUTOMAKE(mypkgname,0.0.1)dnl
989 AM_CONFIG_HEADER(config.h)dnl
990
991 dnl Checks for programs.
992 AC_PROG_CC dnl check for the c compiler
993 dnl you should add CFLAGS="" here, 'cos it is set to -g by PROG_CC
994
995 dnl Checks for libraries.
996 AM_PATH_GTK(1.2.0,,AC_MSG_ERROR(mypkgname 0.1 needs GTK))dnl
997
998 AC_OUTPUT(
999         Makefile
1000 )dnl
1001 </programlisting>
1002
1003 <para>You must add a Makefile.am file:</para>
1004
1005 <programlisting role="C">
1006 bin_PROGRAMS    = myprg
1007 myprg_SOURCES   = myprg.c foo.c bar.c
1008 INCLUDES        = @GTK_CFLAGS@
1009 LDADD           = @GTK_LIBS@
1010 CLEANFILES      = *~
1011 DISTCLEANFILES  = .deps/*.P
1012 </programlisting>
1013
1014 <para>If your project contains more than one subdirectory,
1015 you'll have to create one Makefile.am in each directory plus a
1016 master Makefile.am which will look like:</para>
1017
1018 <programlisting role="C">
1019 SUBDIRS         = mydir1 mydir2 mydir3
1020 </programlisting>
1021
1022 <para>then, to use these, simply type the following
1023 commands:</para>
1024
1025 <programlisting role="C">
1026 aclocal
1027 autoheader
1028 autoconf
1029 automake --add-missing --include-deps --foreign 
1030 </programlisting>
1031
1032 <para>For further information, you should look at the autoconf
1033 and the automake documentation (the shipped info files are
1034 really easy to understand, and there are plenty of web
1035 resources that deal with autoconf and automake).</para>
1036
1037 </sect1>
1038
1039 <!-- ----------------------------------------------------------------- -->
1040
1041 <sect1>
1042 <title>I try to debug my GTK+ application with gdb, but it
1043 hangs my X server when I hit some breakpoint. Any
1044 Idea?</title>
1045
1046 <para>From Federico Mena Quintero:</para>
1047
1048 <para><quote>X is not locked up.  It is likely that you are hitting a
1049 breakpoint inside a callback that is called from a place in Gtk that has
1050 a mouse grab.</quote></para>
1051
1052 <para><quote>Run your program with the <literal>--sync</literal>
1053 option; it will make it easier to debug. Also, you may want to
1054 use the console for running the debugger, and just let the
1055 program run in another console with the X server.</quote></para>
1056
1057 <para>Eric Mouw had another solution:</para>
1058
1059 <para><quote>An old terminal connected to an otherwise unused serial
1060 port is also great for debugging X programs. Old vt100/vt220
1061 terminals are dirt cheap but a bit hard to get (here in The
1062 Netherlands, YMMV).</quote></para>
1063
1064 <para>
1065 Another option is to run your application on Xnest. Xnest is an X server
1066 which displays its root window in a regular window of another X server.  
1067 A pointer grab on the Xnest display will not affect the GUI of your debugger
1068 running on your regular X server.
1069 <programlisting>
1070 Xnest :1
1071 twm -display :1
1072 myapp --display=:1
1073 </programlisting>
1074 </para>
1075 </sect1>
1076 </chapter>
1077
1078 <!-- ***************************************************************** -->
1079 <chapter>
1080 <title>Development with GTK+: general questions</title>
1081
1082 <!-- ----------------------------------------------------------------- -->
1083
1084 <sect1>
1085 <title>What widgets are in GTK?</title>
1086
1087 <para>The GTK+ Tutorial lists the following widgets:</para>
1088
1089 <programlisting role="C">
1090   GtkObject
1091    +GtkData
1092    | +GtkAdjustment
1093    | `GtkTooltips
1094    `GtkWidget
1095      +GtkContainer
1096      | +GtkBin
1097      | | +GtkAlignment
1098      | | +GtkEventBox
1099      | | +GtkFrame
1100      | | | `GtkAspectFrame
1101      | | +GtkHandleBox
1102      | | +GtkItem
1103      | | | +GtkListItem
1104      | | | +GtkMenuItem
1105      | | | | `GtkCheckMenuItem
1106      | | | |   `GtkRadioMenuItem
1107      | | | `GtkTreeItem
1108      | | +GtkViewport
1109      | | `GtkWindow
1110      | |   +GtkColorSelectionDialog
1111      | |   +GtkDialog
1112      | |   | `GtkInputDialog
1113      | |   `GtkFileSelection
1114      | +GtkBox
1115      | | +GtkButtonBox
1116      | | | +GtkHButtonBox
1117      | | | `GtkVButtonBox
1118      | | +GtkHBox
1119      | | | +GtkCombo
1120      | | | `GtkStatusbar
1121      | | `GtkVBox
1122      | |   +GtkColorSelection
1123      | |   `GtkGammaCurve
1124      | +GtkButton
1125      | | +GtkOptionMenu
1126      | | `GtkToggleButton
1127      | |   `GtkCheckButton
1128      | |     `GtkRadioButton
1129      | +GtkCList
1130      |   `GtkCTree
1131      | +GtkFixed
1132      | +GtkList
1133      | +GtkMenuShell
1134      | | +GtkMenuBar
1135      | | `GtkMenu
1136      | +GtkNotebook
1137      | +GtkPaned
1138      | | +GtkHPaned
1139      | | `GtkVPaned
1140      | +GtkScrolledWindow
1141      | +GtkTable
1142      | +GtkToolbar
1143      | `GtkTree
1144      +GtkDrawingArea
1145      | `GtkCurve
1146      +GtkEditable
1147      | +GtkEntry
1148      | | `GtkSpinButton
1149      | `GtkText
1150      +GtkMisc
1151      | +GtkArrow
1152      | +GtkImage
1153      | +GtkLabel
1154      | | `GtkTipsQuery
1155      | `GtkPixmap
1156      +GtkPreview
1157      +GtkProgressBar
1158      +GtkRange
1159      | +GtkScale
1160      | | +GtkHScale
1161      | | `GtkVScale
1162      | `GtkScrollbar
1163      |   +GtkHScrollbar
1164      |   `GtkVScrollbar
1165      +GtkRuler
1166      | +GtkHRuler
1167      | `GtkVRuler
1168      `GtkSeparator
1169        +GtkHSeparator
1170        `GtkVSeparator
1171 </programlisting>
1172 </sect1>
1173
1174 <!-- ----------------------------------------------------------------- -->
1175
1176 <sect1>
1177 <title>Is GTK+ thread safe? How do I write multi-threaded GTK+
1178 applications? <emphasis>[GTK 2.x]</emphasis></title>
1179
1180 <para>The GLib library can be used in a thread-safe mode by
1181 calling g_thread_init() before making any other GLib
1182 calls. In this mode GLib automatically locks all internal
1183 data structures as needed.  This does not mean that two
1184 threads can simultaneously access, for example, a single hash
1185 table, but they can access two different hash tables
1186 simultaneously. If two different threads need to access the
1187 same hash table, the application is responsible for locking
1188 itself.</para>
1189
1190 <para>In order to make GDK thread aware, you also need to
1191 call gdk_threads_init() in conjunction with the above call.
1192 There is a single global
1193 lock that you must acquire with gdk_threads_enter() before
1194 making any GDK calls, and release with gdk_threads_leave()
1195 afterwards throughout your code.</para>
1196
1197 <para>A minimal main program for a threaded GTK+ application
1198 looks like:</para>
1199
1200 <programlisting role="C">
1201 int
1202 main (int argc, char *argv[])
1203 {
1204   GtkWidget *window;
1205
1206   /* init threads */    
1207   g_thread_init(NULL);
1208   gdk_threads_init();
1209   
1210   /* init gtk */
1211   gtk_init(&amp;argc, &amp;argv);
1212
1213   window = create_window();
1214   gtk_widget_show(window);
1215
1216   gdk_threads_enter();
1217   gtk_main();
1218   gdk_threads_leave();
1219
1220   return 0;
1221 }
1222 </programlisting>
1223
1224 <para>Callbacks require a bit of attention. Callbacks from
1225 GTK+ (signals) are made within the GTK+ lock. However
1226 callbacks from GLib (timeouts, IO callbacks, and idle
1227 functions) are made outside of the GTK+ lock. So, within a
1228 signal handler you do not need to call gdk_threads_enter(),
1229 but within the other types of callbacks, you do.</para>
1230
1231 <para>Erik Mouw contributed the following code example to
1232 illustrate how to use threads within GTK+ programs.</para>
1233
1234 <programlisting role="C">
1235 /*-------------------------------------------------------------------------
1236  * Filename:      gtk-thread.c
1237  * Version:       1.99.1
1238  * Copyright:     Copyright (C) 1999, Erik Mouw
1239  * Author:        Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt;
1240  * Description:   GTK threads example.
1241  * Created at:    Sun Oct 17 21:27:09 1999
1242  * Modified by:   Owen Taylor &lt;otaylor@gtk.org&gt;
1243  * Modified at:   Wed May 28 10:43:00 2003
1244  *-----------------------------------------------------------------------*/
1245 /*
1246  * Compile with:
1247  *
1248  * cc -o gtk-thread gtk-thread.c `pkg-config --cflags --libs gtk+-2.0 gthread-2.0`
1249  *
1250  * Thanks to Sebastian Wilhelmi for pointing out some bugs in earlier versions.
1251  *
1252  */
1253
1254 #include &lt;unistd.h&gt;
1255 #include &lt;gtk/gtk.h&gt;
1256
1257 #define YES_IT_IS    (1)
1258 #define NO_IT_IS_NOT (0)
1259
1260 typedef struct
1261 {
1262   GtkWidget *label;
1263   int what;
1264 } yes_or_no_args;
1265
1266 G_LOCK_DEFINE_STATIC (yes_or_no);
1267 static volatile int yes_or_no = YES_IT_IS;
1268
1269 void destroy(GtkWidget *widget, gpointer data)
1270 {
1271   gtk_main_quit();
1272 }
1273
1274 void *argument_thread(void *args)
1275 {
1276   yes_or_no_args *data = (yes_or_no_args *)args;
1277   gboolean say_something;
1278
1279   for(;;)
1280     {
1281       /* sleep a while */
1282       sleep(g_random_int_range (1, 4));
1283
1284       /* lock the yes_or_no_variable */
1285       G_LOCK(yes_or_no);
1286
1287       /* do we have to say something? */
1288       say_something = (yes_or_no != data->what);
1289
1290       if(say_something)
1291         {
1292           /* set the variable */
1293           yes_or_no = data->what;
1294         }
1295
1296       /* Unlock the yes_or_no variable */
1297       G_UNLOCK(yes_or_no);
1298
1299       if(say_something)
1300         {
1301           /* get GTK thread lock */
1302           gdk_threads_enter();
1303
1304           /* set label text */
1305           if(data->what == YES_IT_IS)
1306             gtk_label_set_text(GTK_LABEL(data->label), "O yes, it is!");
1307           else
1308             gtk_label_set_text(GTK_LABEL(data->label), "O no, it isn't!");
1309
1310           /* Make sure all X commands are sent to the X server; not strictly
1311            * necessary here, but always a good idea when you do anything
1312            * from a thread other than the one where the main loop is running.
1313            */
1314           gdk_flush ();
1315
1316           /* release GTK thread lock */
1317           gdk_threads_leave();
1318         }
1319     }
1320
1321   return NULL;
1322 }
1323
1324 int main(int argc, char *argv[])
1325 {
1326   GtkWidget *window;
1327   GtkWidget *label;
1328   GError *error = NULL;
1329   yes_or_no_args yes_args, no_args;
1330
1331   /* init threads */
1332   g_thread_init(NULL);
1333   gdk_threads_init();
1334
1335   /* init gtk */
1336   gtk_init(&amp;argc, &amp;argv);
1337
1338   /* create a window */
1339   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1340
1341   g_signal_connect(window, "destroy",
1342                    G_CALLBACK(destroy), NULL);
1343
1344   gtk_container_set_border_width(GTK_CONTAINER (window), 10);
1345
1346   /* create a label */
1347   label = gtk_label_new("And now for something completely different ...");
1348   gtk_container_add(GTK_CONTAINER(window), label);
1349
1350   /* show everything */
1351   gtk_widget_show(label);
1352   gtk_widget_show (window);
1353
1354   /* create the threads */
1355   yes_args.label = label;
1356   yes_args.what = YES_IT_IS;
1357   if (!g_thread_create(argument_thread, &amp;yes_args, FALSE, &amp;error))
1358     {
1359       g_printerr ("Failed to create YES thread: %s\n", error->message);
1360       return 1;
1361     }
1362
1363   no_args.label = label;
1364   no_args.what = NO_IT_IS_NOT;
1365   if (!g_thread_create(argument_thread, &amp;no_args, FALSE, &amp;error))
1366     {
1367       g_printerr ("Failed to create NO thread: %s\n", error->message);
1368       return 1;
1369     }
1370
1371   /* enter the GTK main loop */
1372   gdk_threads_enter();
1373   gtk_main();
1374   gdk_threads_leave();
1375
1376   return 0;
1377 }
1378 </programlisting>
1379 </sect1>
1380
1381 <!-- ----------------------------------------------------------------- -->
1382
1383 <sect1>
1384 <title>I'm doing some stuff with GTK+ in a separate thread, and
1385 properly locking with gdk_threads_enter/gdk_threads_leave()
1386 but the display doesn't update properly. <emphasis>[GTK 2.x]</emphasis>
1387 </title>
1388
1389 <para>For efficiency, the X window system batches up commands
1390 and sends them to the X server in batches instead of sending
1391 out immediately.</para>
1392
1393 <para>In a non-multithreaded program, you don't have to worry about
1394 this, since the first thing that happens when control returns
1395 to the main loop is that any outstanding X requests are 
1396 sent to the X server.</para>
1397
1398 <para>However, if you are making GTK+ calls from a thread other
1399 than the main loop, then GTK+ doesn't know when to send batched
1400 commands out. For that reason, after making GTK+ calls 
1401 in a separate thread, it is usually a good idea to call
1402 gdk_flush() before gdk_thread_leave().</para>
1403
1404 <para>Actually, gdk_flush() is more expensive than is necessary here,
1405 since it waits for the X server to finish outstanding commands
1406 as well; if performance is an issue, you may want to call
1407 XFlush() directly:</para>
1408
1409 <programlisting role="C">
1410
1411 #include &lt;gdk/gdkx.h&gt;
1412
1413 void my_flush_commands (void)
1414 {
1415   GdkDisplay *display = gdk_display_get_default ();
1416   XFlush (GDK_DISPLAY_XDISPLAY (display);
1417 }
1418 </programlisting>
1419
1420 </sect1>
1421
1422 <!-- ----------------------------------------------------------------- -->
1423
1424 <sect1>
1425 <title>What's an easy way to run a function in the thread with
1426 the main loop? <emphasis>[GTK 2.x]</emphasis></title>
1427
1428 <para>Sometimes the simplest way to set up a threaded program
1429 is to make all the GTK+ calls in a single thread. In such
1430 a program, you should still call g_threads_init(), but
1431 don't need to call gdk_threads_init(), gkd_threads_enter(),
1432 and gdk_threads_leave().</para>
1433
1434 <para>If you set your program up this way, how then do you get
1435 the thread making GTK+ calls and running the main loop
1436 to do something in response to another thread?</para>
1437
1438 <para>An easy way to do it is to take advantage of the fact that
1439 the GLib main loop functions are all thread safe, and can
1440 be called from any thread by adding an idle function
1441 with g_idle_add(). The function provided will be called
1442 at the next opportunity by the main thread. If you want
1443 your function to take priority over event handling and
1444 drawing, you can instead use g_idle_add_full() and pass
1445 in a priority of G_PRIORITY_HIGH.</para>
1446
1447 </sect1>
1448
1449 <!-- ----------------------------------------------------------------- -->
1450
1451 <sect1>
1452 <title>Why does this strange 'x io error' occur when I
1453 <literal>fork()</literal> in my GTK+ app?</title>
1454
1455 <para>This is not really a GTK+ problem, and the problem is
1456 not related to <literal>fork()</literal> either. If the 'x io
1457 error' occurs then you probably use the <literal>exit()</literal> function
1458 in order to exit from the child process.</para>
1459
1460 <para>When GDK opens an X display, it creates a socket file
1461 descriptor. When you use the <literal>exit()</literal>
1462 function, you implicitly close all the open file descriptors,
1463 and the underlying X library really doesn't like this.</para>
1464
1465 <para>The right function to use here is
1466 <literal>_exit()</literal>.</para> 
1467
1468 <para>Erik Mouw contributed the following code example to
1469 illustrate handling fork() and exit().</para>
1470
1471 <programlisting role="C">
1472 /*-------------------------------------------------------------------------
1473  * Filename:      gtk-fork.c
1474  * Version:       0.99.1
1475  * Copyright:     Copyright (C) 1999, Erik Mouw
1476  * Author:        Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt;
1477  * Description:   GTK+ fork example
1478  * Created at:    Thu Sep 23 21:37:55 1999
1479  * Modified by:   Erik Mouw &lt;J.A.K.Mouw@its.tudelft.nl&gt;
1480  * Modified at:   Thu Sep 23 22:39:39 1999
1481  *-----------------------------------------------------------------------*/
1482 /*
1483  * Compile with:
1484  *
1485  * cc -o gtk-fork gtk-fork.c `pkg-config gtk+-2.0 --cflags --libs`
1486  *
1487  */
1488
1489 #include &lt;stdio.h&gt;
1490 #include &lt;stdlib.h&gt;
1491 #include &lt;signal.h&gt;
1492 #include &lt;sys/types.h&gt;
1493 #include &lt;sys/wait.h&gt;
1494 #include &lt;unistd.h&gt;
1495 #include &lt;gtk/gtk.h&gt;
1496
1497 void sigchld_handler(int num)
1498 {
1499   sigset_t set, oldset;
1500   pid_t pid;
1501   int status, exitstatus;
1502
1503   /* block other incoming SIGCHLD signals */
1504   sigemptyset(&amp;set);
1505   sigaddset(&amp;set, SIGCHLD);
1506   sigprocmask(SIG_BLOCK, &amp;set, &amp;oldset);
1507
1508   /* wait for child */
1509   while((pid = waitpid((pid_t)-1, &amp;status, WNOHANG)) > 0)
1510     {
1511       if(WIFEXITED(status))
1512         {
1513           exitstatus = WEXITSTATUS(status);
1514
1515           fprintf(stderr, 
1516                   "Parent: child exited, pid = %d, exit status = %d\n", 
1517                   (int)pid, exitstatus);
1518         }
1519       else if(WIFSIGNALED(status))
1520         {
1521           exitstatus = WTERMSIG(status);
1522
1523           fprintf(stderr,
1524                   "Parent: child terminated by signal %d, pid = %d\n",
1525                   exitstatus, (int)pid);
1526         }
1527       else if(WIFSTOPPED(status))
1528         {
1529           exitstatus = WSTOPSIG(status);
1530
1531           fprintf(stderr,
1532                   "Parent: child stopped by signal %d, pid = %d\n",
1533                   exitstatus, (int)pid);
1534         }
1535       else
1536         {
1537           fprintf(stderr,
1538                   "Parent: child exited magically, pid = %d\n",
1539                   (int)pid);
1540         }
1541     }
1542
1543   /* re-install the signal handler (some systems need this) */
1544   signal(SIGCHLD, sigchld_handler);
1545   
1546   /* and unblock it */
1547   sigemptyset(&amp;set);
1548   sigaddset(&amp;set, SIGCHLD);
1549   sigprocmask(SIG_UNBLOCK, &amp;set, &amp;oldset);
1550 }
1551
1552 gint delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
1553 {
1554   return(FALSE);
1555 }
1556
1557 void destroy(GtkWidget *widget, gpointer data)
1558 {
1559   gtk_main_quit();
1560 }
1561
1562 void fork_me(GtkWidget *widget, gpointer data)
1563 {
1564   pid_t pid;
1565
1566   pid = fork();
1567
1568   if(pid == -1)
1569     {
1570       /* ouch, fork() failed */
1571       perror("fork");
1572       exit(-1);
1573     }
1574   else if(pid == 0)
1575     {
1576       /* child */
1577       fprintf(stderr, "Child: pid = %d\n", (int)getpid());
1578
1579       execlp("ls", "ls", "-CF", "/", NULL);
1580       
1581       /* if exec() returns, there is something wrong */
1582       perror("execlp");
1583
1584       /* exit child. note the use of _exit() instead of exit() */
1585       _exit(-1);
1586     }
1587   else
1588     {
1589       /* parent */
1590       fprintf(stderr, "Parent: forked a child with pid = %d\n", (int)pid);
1591     }
1592 }
1593
1594 int main(int argc, char *argv[])
1595 {
1596   GtkWidget *window;
1597   GtkWidget *button;
1598
1599   gtk_init(&amp;argc, &amp;argv);
1600
1601   /* the basic stuff: make a window and set callbacks for destroy and
1602    * delete events 
1603    */
1604   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1605
1606   gtk_signal_connect(GTK_OBJECT (window), "delete_event",
1607                      GTK_SIGNAL_FUNC(delete_event), NULL);
1608           
1609   gtk_signal_connect(GTK_OBJECT (window), "destroy",
1610                      GTK_SIGNAL_FUNC(destroy), NULL);
1611
1612 #if (GTK_MAJOR_VERSION == 1) && (GTK_MINOR_VERSION == 0)
1613   gtk_container_border_width(GTK_CONTAINER (window), 10);
1614 #else  
1615   gtk_container_set_border_width(GTK_CONTAINER (window), 10);
1616 #endif
1617
1618   /* add a button to do something usefull */
1619   button = gtk_button_new_with_label("Fork me!");
1620           
1621   gtk_signal_connect(GTK_OBJECT (button), "clicked",
1622                      GTK_SIGNAL_FUNC(fork_me), NULL);
1623
1624   gtk_container_add(GTK_CONTAINER(window), button);
1625           
1626   /* show everything */
1627   gtk_widget_show (button);
1628   gtk_widget_show (window);
1629
1630
1631   /* install a signal handler for SIGCHLD signals */
1632   signal(SIGCHLD, sigchld_handler);
1633
1634   
1635   /* main loop */
1636   gtk_main ();
1637
1638   exit(0);         
1639 }
1640 </programlisting>
1641 </sect1>
1642
1643 <!-- ----------------------------------------------------------------- -->
1644
1645 <sect1>
1646 <title>Why don't the contents of a button move when the button
1647 is pressed? Here's a patch to make it work that way...</title>
1648
1649 <para>From: Peter Mattis</para>
1650
1651 <para><quote>The reason buttons don't move their child down and to
1652 the right when they are depressed is because I don't think
1653 that's what is happening visually. My view of buttons is
1654 that you are looking at them straight on. That is, the user
1655 interface lies in a plane and you're above it looking
1656 straight at it. When a button gets pressed it moves directly
1657 away from you. To be absolutely correct I guess the child
1658 should actually shrink a tiny amount. But I don't see why
1659 the child should shift down and to the left. Remember, the
1660 child is supposed to be attached to the buttons surface. Its
1661 not good for it to appear like the child is slipping on the
1662 surface of the button.</quote></para>
1663
1664 <para><quote>On a more practical note, I did implement this at one point
1665 and determined it didn't look good and removed it.</quote></para>
1666
1667 </sect1>
1668
1669 <!-- ----------------------------------------------------------------- -->
1670
1671 <sect1>
1672 <title>How do I identifiy a widgets top level window or other
1673 ancestor?</title>
1674
1675 <para>There are a couple of ways to find the top level parent
1676 of a widget. The easier way is to call the
1677 <literal>gtk_widget_get_toplevel()</literal> function that
1678 returns pointer to a GtkWidget that is the top level
1679 window.</para>
1680
1681 <para>A more complicated way to do this (but less limited, as
1682 it allows the user to get the closest ancestor of a known type) is to use
1683 <literal>gtk_widget_get_ancestor()</literal> as in:</para>
1684
1685 <programlisting role="C">
1686       GtkWidget       *widget;
1687       widget = gtk_widget_get_ancestor(w, GTK_TYPE_WINDOW);
1688 </programlisting>
1689
1690 <para>Since virtually all the GTK_TYPEs can be used as the
1691 second parameter of this function, you can get any parent
1692 widget of a particular widget. Suppose you have an hbox which
1693 contains a vbox, which in turn contains some other atomic
1694 widget (entry, label, etc. To find the master hbox using the
1695 <literal>entry</literal> widget simply use:</para>
1696
1697 <programlisting role="C">
1698       GtkWidget       *hbox;
1699       hbox = gtk_widget_get_ancestor(w, GTK_TYPE_HBOX);
1700 </programlisting>
1701
1702 </sect1>
1703
1704 <!-- ----------------------------------------------------------------- -->
1705
1706 <sect1>
1707 <title>How do I get the Window ID of a GtkWindow?</title>
1708
1709 <para>The actual Gdk/X window will be created when the widget
1710 gets realized. You can get the Window ID with:</para>
1711
1712 <programlisting role="C">
1713 #include &lt;gdk/gdkx.h&gt;
1714
1715 Window xwin = GDK_WINDOW_XWINDOW (GTK_WIDGET (my_window)->window);
1716 </programlisting>
1717 </sect1>
1718
1719 <!-- ----------------------------------------------------------------- -->
1720
1721 <sect1>
1722 <title>How do I catch a double click event (in a list widget,
1723 for example)?</title>
1724
1725 <para>Tim Janik wrote to gtk-list (slightly modified):</para>
1726
1727 <para>Define a signal handler:</para>
1728
1729 <programlisting role="C">
1730 gint
1731 signal_handler_event(GtkWidget *widget, GdkEventButton *event, gpointer func_data)
1732 {
1733   if (GTK_IS_LIST_ITEM(widget) &&
1734        (event->type==GDK_2BUTTON_PRESS ||
1735         event->type==GDK_3BUTTON_PRESS) ) {
1736     printf("I feel %s clicked on button %d\n",
1737            event->type==GDK_2BUTTON_PRESS ? "double" : "triple",
1738            event->button);
1739   }
1740
1741   return FALSE;
1742 }</programlisting>
1743
1744 <para>And connect the handler to your object:</para>
1745
1746 <programlisting role="C">
1747 {
1748   /* list, list item init stuff */     
1749
1750   gtk_signal_connect(GTK_OBJECT(list_item),
1751                      "button_press_event",
1752                      GTK_SIGNAL_FUNC(signal_handler_event),
1753                      NULL);
1754
1755   /* and/or */
1756
1757   gtk_signal_connect(GTK_OBJECT(list_item),
1758                      "button_release_event",
1759                      GTK_SIGNAL_FUNC(signal_handler_event),
1760                      NULL);
1761
1762   /* something else */
1763 }
1764 </programlisting>
1765
1766 <para>and, Owen Taylor wrote:</para>
1767
1768 <para><quote>Note that a single button press will be received
1769 beforehand, and if you are doing this for a button, you will
1770 therefore also get a "clicked" signal for the button. (This
1771 is going to be true for any toolkit, since computers aren't
1772 good at reading one's mind.)</quote></para>
1773
1774 </sect1>
1775
1776 <!-- ----------------------------------------------------------------- -->
1777
1778 <sect1>
1779 <title>By the way, what are the differences between signals
1780 and events?</title>
1781
1782 <para>First of all, Havoc Pennington gives a rather complete
1783 description of the differences between events and signals in
1784 his free book (two chapters can be found at <ulink
1785 url="http://www106.pair.com/rhp/sample_chapters.html">
1786 http://www106.pair.com/rhp/sample_chapters.html</ulink>).</para>
1787
1788 <para>Moreover, Havoc posted this to the <literal>gtk-list</literal>
1789 <quote>Events are a stream of messages received from the X
1790 server. They drive the Gtk main loop; which more or less
1791 amounts to "wait for events, process them" (not exactly, it
1792 is really more general than that and can wait on many
1793 different input streams at once). Events are a Gdk/Xlib
1794 concept.</quote></para>
1795
1796 <para><quote>Signals are a feature of GtkObject and its subclasses. They
1797 have nothing to do with any input stream; really a signal is just a way
1798 to keep a list of callbacks around and invoke them ("emit" the
1799 signal). There are lots of details and extra features of
1800 course. Signals are emitted by object instances, and are entirely
1801 unrelated to the Gtk main loop.  Conventionally, signals are emitted
1802 "when something changes" about the object emitting the
1803 signal.</quote></para>
1804
1805 <para><quote>Signals and events only come together because GtkWidget
1806 happens to emit signals when it gets events. This is purely a
1807 convenience, so you can connect callbacks to be invoked when a
1808 particular widget receives a particular event. There is nothing about
1809 this that makes signals and events inherently related concepts, any more
1810 than emitting a signal when you click a button makes button clicking and
1811 signals related concepts.</quote></para>
1812
1813 </sect1>
1814
1815 <!-- ----------------------------------------------------------------- -->
1816
1817 <sect1>
1818 <title>Data I pass to the <literal>delete_event</literal> (or other event)
1819 handler gets corrupted.</title>
1820
1821 <para>All event handlers take an additional argument which
1822 contains information about the event that triggered the
1823 handler. So, a <literal>delete_event</literal> handler must
1824 be declared as:</para>
1825
1826
1827 <programlisting role="C">
1828 gint delete_event_handler (GtkWidget   *widget,
1829                            GdkEventAny *event,
1830                            gpointer     data);
1831 </programlisting>
1832
1833 </sect1>
1834
1835 <!-- ----------------------------------------------------------------- -->
1836
1837 <sect1>
1838 <title>I have my signal connected to the the (whatever) event,
1839 but it seems I don't catch it. What's wrong?</title>
1840
1841 <para>There is some special initialisation to do in order to
1842 catch some particular events. In fact, you must set the
1843 correct event mask bit of your widget before getting some
1844 particular events.</para>
1845
1846 <para>For example,</para>
1847
1848 <programlisting role="C">
1849   gtk_widget_add_events(window, GDK_KEY_RELEASE_MASK);
1850 </programlisting>
1851
1852 <para>lets you catch the key release events. If you want to
1853 catch every events, simply us the GDK_ALL_EVENTS_MASK event
1854 mask.</para>
1855
1856 <para>All the event masks are defined in the
1857 <filename>gdktypes.h</filename> file.</para>
1858
1859 </sect1>
1860
1861 <!-- ----------------------------------------------------------------- -->
1862
1863 <sect1>
1864 <title>I need to add a new signal to a GTK+ widget. Any
1865 idea?</title>
1866
1867 <para>If the signal you want to add may be beneficial for
1868 other GTK+ users, you may want to submit a patch that
1869 presents your changes. Check the tutorial for more
1870 information about adding signals to a widget class.</para>
1871
1872 <para>If you don't think it is the case or if your patch is
1873 not applied you'll have to use the
1874 <literal>gtk_object_class_user_signal_new</literal>
1875 function. <literal>gtk_object_class_user_signal_new</literal> allows you
1876 to add a new signal to a predefined GTK+ widget without any
1877 modification of the GTK+ source code. The new signal can be
1878 emited with <literal>gtk_signal_emit</literal> and can be
1879 handled in the same way as other signals.</para>
1880
1881 <para>Tim Janik posted this code snippet:</para>
1882
1883 <programlisting role="C">
1884 static guint signal_user_action = 0;
1885
1886 signal_user_action =
1887   gtk_object_class_user_signal_new (gtk_type_class (GTK_TYPE_WIDGET),
1888                     "user_action",
1889                     GTK_RUN_LAST | GTK_RUN_ACTION,
1890                     gtk_marshal_NONE__POINTER,
1891                     GTK_TYPE_NONE, 1,
1892                     GTK_TYPE_POINTER);
1893
1894 void
1895 gtk_widget_user_action (GtkWidget *widget,
1896                         gpointer   act_data)
1897 {
1898   g_return_if_fail (GTK_IS_WIDGET (widget));
1899
1900   gtk_signal_emit (GTK_OBJECT (widget), signal_user_action, act_data);
1901 }
1902 </programlisting>
1903
1904 <para>If you want your new signal to have more than the
1905 classical gpointer parameter, you'll have to play with GTK+
1906 marshallers.</para>
1907
1908 </sect1>
1909
1910 <!-- ----------------------------------------------------------------- -->
1911
1912 <sect1>
1913 <title>Is it possible to get some text displayed which is
1914 truncated to fit inside its allocation?</title>
1915
1916 <para>GTK's behavior (no clipping) is a consequence of its
1917 attempts to conserve X resources. Label widgets (among
1918 others) don't get their own X window - they just draw their
1919 contents on their parent's window. While it might be possible
1920 to have clipping occur by setting the clip mask before
1921 drawing the text, this would probably cause a substantial
1922 performance penalty.</para>
1923
1924 <para>Its possible that, in the long term, the best solution
1925 to such problems might be just to change gtk to give labels X
1926 windows. A short term workaround is to put the label widget
1927 inside another widget that does get its own window - one
1928 possible candidate would be the viewport widget.</para>
1929
1930 <programlisting role="C">
1931 viewport = gtk_viewport (NULL, NULL);
1932 gtk_widget_set_usize (viewport, 50, 25);
1933 gtk_viewport_set_shadow_type (GTK_VIEWPORT(viewport), GTK_SHADOW_NONE);
1934 gtk_widget_show(viewport);
1935
1936 label = gtk_label ("a really long label that won't fit");
1937 gtk_container_add (GTK_CONTAINER(viewport), label);
1938 gtk_widget_show (label);
1939 </programlisting>
1940
1941 <para>If you were doing this for a bunch of widgets, you might
1942 want to copy gtkviewport.c and strip out the adjustment and
1943 shadow functionality (perhaps you could call it
1944 GtkClipper).</para>
1945 </sect1>
1946
1947 <!-- ----------------------------------------------------------------- -->
1948
1949 <sect1>
1950 <title>How do I make my window modal? / How do I make a single
1951 window active?</title>
1952
1953 <para>After you create your window, do
1954 <literal>gtk_grab_add(my_window)</literal>. And after closing the window
1955 do <literal>gtk_grab_remove(my_window)</literal>.</para>
1956
1957 </sect1>
1958
1959 <!-- ----------------------------------------------------------------- -->
1960
1961 <sect1>
1962 <title>Why doesn't my widget (e.g. progressbar)
1963 update?</title>
1964
1965 <para>You are probably doing all the changes within a function without
1966 returning control to <literal>gtk_main()</literal>. This may
1967 be the case if you do some lengthy calculation in your
1968 code. Most drawing updates are only placed on a queue, which
1969 is processed within <literal>gtk_main()</literal>. You can force the
1970 drawing queue to be processed using something like:</para>
1971
1972
1973 <programlisting role="C">
1974 while (g_main_iteration(FALSE));
1975 </programlisting>
1976
1977 <para>inside you're function that changes the widget.</para>
1978
1979 <para>What the above snippet does is run all pending events
1980 and high priority idle functions, then return immediately
1981 (the drawing is done in a high priority idle function).</para>
1982
1983 </sect1>
1984
1985 <!-- ----------------------------------------------------------------- -->
1986
1987 <sect1>
1988 <title>How do I attach data to some GTK+ object/widget?
1989 <emphasis>[GTK 2.x]</emphasis></title>
1990
1991 <para>First of all, the attached data is stored in the
1992 object_data field of a GtkObject. The type of this field is
1993 GData, which is defined in glib.h.  So you should read the
1994 gdataset.c file in your glib source directory very
1995 carefully.</para>
1996
1997 <para>There are two (easy) ways to attach some data to a gtk
1998 object.  Using <literal>g_object_set_data()</literal> and
1999 <literal>g_object_get_data()</literal> seems to be the most
2000 common way to do this, as it provides a powerful interface to
2001 connect objects and data.</para>
2002
2003 <programlisting role="C">
2004 void g_object_set_data(GObject *object, const gchar *key, gpointer data);
2005
2006 gpointer g_object_get_data(GObject *object, const gchar *key);
2007 </programlisting>
2008
2009 <para>Since a short example is better than any lengthy speech:</para>
2010
2011 <programlisting role="C">
2012 struct my_struct        p1,p2,*result;
2013 GtkWidget               *w;
2014
2015 g_object_set_data(G_OBJECT(w),"p1 data",(gpointer)&amp;p1);
2016 g_object_set_data(G_OBJECT(w),"p2 data",(gpointer)&amp;p2);
2017
2018 result = g_object_get_data(G_OBJECT(w),"p1 data");
2019 </programlisting>
2020
2021 <para>The <literal>gtk_object_set_user_data()</literal> and
2022 <literal>gtk_object_get_user_data()</literal> functions does
2023 exactly the same thing as the functions above, but does not
2024 let you specify the "key" parameter.Instead, it uses a
2025 standard "user_data" key. Note that the use of these functions
2026 is deprecated in 1.2. They only provide a compatibility mode
2027 with some old gtk packages.</para>
2028
2029 </sect1>
2030
2031 <!-- ----------------------------------------------------------------- -->
2032
2033 <sect1>
2034 <title>How do I remove the data I have attached to an
2035 object?</title>
2036
2037 <para>When attaching the data to the object, you can use the
2038 <literal>gtk_object_set_data_full()</literal> function. The three
2039 first arguments of the function are the same as in
2040 <literal>gtk_object_set_data()</literal>. The fourth one is a
2041 pointer to a callback function which is called when the data
2042 is destroyed. The data is destroyed when you:</para>
2043
2044 <itemizedlist>
2045 <listitem><simpara> destroy the object</simpara>
2046 </listitem>
2047
2048 <listitem><simpara> replace the data with a new one (with
2049 the same key)</simpara>
2050 </listitem>
2051
2052 <listitem><simpara> replace the data with NULL (with the
2053 same key)</simpara>
2054 </listitem>
2055
2056 </itemizedlist>
2057
2058 </sect1>
2059
2060 <!-- ----------------------------------------------------------------- -->
2061
2062 <sect1>
2063 <title>How do I reparent a widget?</title>
2064
2065 <para>The normal way to reparent (ie change the owner) of a
2066 widget should be to use the function:</para>
2067
2068 <programlisting role="C">
2069 void gtk_widget_reparent (GtkWidget *widget, 
2070                           GtkWidget *new_parent)
2071 </programlisting>
2072
2073 <para>But this is only a "should be" since this function does
2074 not correctly do its job on some specific widgets. The main
2075 goal of gtk_widget_reparent() is to avoid unrealizing widget
2076 if both widget and new_parent are realized (in this case,
2077 widget->window is successfully reparented). The problem here
2078 is that some widgets in the GTK+ hierarchy have multiple
2079 attached X subwindows and this is notably the case for the
2080 GtkSpinButton widget. For those, gtk_widget_reparent() will
2081 fail by leaving an unrealized child window where it should
2082 not.</para>
2083
2084 <para>To avoid this problem, simply use the following code
2085 snippet:</para>
2086
2087 <programlisting role="C">
2088      gtk_widget_ref(widget);
2089      gtk_container_remove(GTK_CONTAINER(old_parent), widget);
2090      gtk_container_add(GTK_CONTAINER(new_parent), widget);
2091      gtk_widget_unref(widget);
2092 </programlisting>
2093
2094 </sect1>
2095
2096 <!-- ----------------------------------------------------------------- -->
2097
2098 <sect1>
2099 <title>How could I get any widgets position?</title>
2100
2101 <para>As Tim Janik pointed out, there are different cases, and
2102 each case requires a different solution.</para>
2103
2104 <itemizedlist>
2105 <listitem><simpara> If you want the position of a widget
2106 relative to its parent, you should use
2107 <literal>widget->allocation.x</literal> and
2108 <literal>widget->allocation.y</literal>.</simpara>
2109 </listitem>
2110
2111 <listitem><simpara> If you want the position of a window
2112 relative to the X root window, you should use
2113 <literal>gdk_window_get_geometry()</literal>
2114 <literal>gdk_window_get_position()</literal> or
2115 <literal>gdk_window_get_origin()</literal>.</simpara>
2116 </listitem>
2117
2118 <listitem><simpara> If you want to get the position of the
2119 window (including the WM decorations), you should use
2120 <literal>gdk_window_get_root_origin()</literal>.</simpara>
2121 </listitem>
2122
2123 <listitem><simpara> Last but not least, if you want to get a Window
2124 Manager frame position, you should use
2125 <literal>gdk_window_get_deskrelative_origin()</literal>.</simpara>
2126 </listitem>
2127 </itemizedlist>
2128
2129 <para>Your choice of Window Manager will have an effect of the
2130 results of the above functions. You should keep this in mind
2131 when writing your application. This is dependant upon how the
2132 Window Managers manage the decorations that they add around
2133 windows.</para>
2134
2135 </sect1>
2136
2137 <!-- ----------------------------------------------------------------- -->
2138
2139 <sect1>
2140 <title>How do I set the size of a widget/window? How do I
2141 prevent the user resizing my window? <emphasis>[GTK 2.x]</emphasis></title>
2142
2143 <para>The <literal>gtk_widget_set_size_request()</literal> function
2144 is used to set the size of a widget to a specific size.
2145
2146 The function
2147 <literal>gtk_window_set_resizable()</literal> function sets whether
2148 the user can resize a window, which they can by default. The
2149 definition of these functions are:</para>
2150
2151 <programlisting role="C">
2152 void gtk_widget_set_size_request (GtkWidget *widget,
2153                                   gint       width,
2154                                   gint       height);
2155
2156 void gtk_window_set_resizable (GtkWindow *window,
2157                                gboolean   resizable);
2158
2159 </programlisting>
2160
2161 </sect1>
2162
2163 <!-- ----------------------------------------------------------------- -->
2164
2165 <sect1>
2166 <title>How do I add a popup menu to my GTK+ application?</title>
2167
2168 <para>The <literal>menu</literal> example in the examples/menu
2169 directory of the GTK+ distribution implements a popup menu
2170 with this technique:</para>
2171
2172 <programlisting role="C">
2173 static gint button_press (GtkWidget *widget, GdkEvent *event)
2174 {
2175
2176     if (event->type == GDK_BUTTON_PRESS) {
2177         GdkEventButton *bevent = (GdkEventButton *) event; 
2178         gtk_menu_popup (GTK_MENU(widget), NULL, NULL, NULL, NULL,
2179                         bevent->button, bevent->time);
2180         /* Tell calling code that we have handled this event; the buck
2181          * stops here. */
2182         return TRUE;
2183     }
2184
2185     /* Tell calling code that we have not handled this event; pass it on. */
2186     return FALSE;
2187 }
2188 </programlisting>
2189
2190 </sect1>
2191
2192 <!-- ----------------------------------------------------------------- -->
2193
2194 <sect1>
2195 <title>How do I disable or enable a widget, such as a
2196 button?</title>
2197
2198 <para>To disable (or to enable) a widget, use the
2199 <literal>gtk_widget_set_sensitive()</literal> function. The
2200 first parameter is you widget pointer. The second parameter is
2201 a boolean value: when this value is TRUE, the widget is
2202 enabled.</para>
2203 </sect1>
2204
2205 <!-- ----------------------------------------------------------------- -->
2206
2207 <sect1>
2208 <title>Shouldn't the text argument in the gtk_clist_*
2209 functions be declared const?</title>
2210
2211 <para>For example:</para>
2212
2213 <programlisting role="C">
2214 gint gtk_clist_prepend (GtkCList *clist,
2215                         gchar    *text[]);
2216 </programlisting>
2217
2218 <para>Answer: No, while a type "gchar*" (pointer to char) can
2219 automatically be cast into "const gchar*" (pointer to const
2220 char), this does not apply for "gchar *[]" (array of an
2221 unspecified number of pointers to char) into "const gchar *[]"
2222 (array of an unspecified number of pointers to const char).</para>
2223
2224 <para>The type qualifier "const" may be subject to automatic
2225 casting, but in the array case, it is not the array itself
2226 that needs the (const) qualified cast, but its members, thus
2227 changing the whole type.</para>
2228 </sect1>
2229
2230 <!-- ----------------------------------------------------------------- -->
2231
2232 <sect1>
2233 <title>How do I render pixels (image data) to the
2234 screen?</title>
2235
2236 <para>There are several ways to approach this. The simplest
2237 way is to use GdkRGB, see gdk/gdkrgb.h. You create an RGB
2238 buffer, render to your RGB buffer, then use GdkRGB routines to
2239 copy your RGB buffer to a drawing area or custom widget. The
2240 book "GTK+/Gnome Application Development" gives some details;
2241 GdkRGB is also documented in the GTK+ reference
2242 documentation.</para>
2243
2244 <para>If you're writing a game or other graphics-intensive
2245 application, you might consider a more elaborate
2246 solution. OpenGL is the graphics standard that will let you
2247 access hardware accelaration in future versions of XFree86; so
2248 for maximum speed, you probably want to use OpenGL. A
2249 GtkGLArea widget is available for using OpenGL with GTK+ (but
2250 GtkGLArea does not come with GTK+ itself). There are also
2251 several open source game libraries, such as ClanLib and Loki's
2252 Simple DirectMedia Layer library (SDL).</para>
2253
2254 <para>You do NOT want to use
2255 <literal>gdk_draw_point()</literal>, that will be extremely
2256 slow.</para>
2257 </sect1>
2258
2259 <!-- ----------------------------------------------------------------- -->
2260
2261 <sect1>
2262 <title>How do I create a pixmap without having my window being
2263 realized/shown?</title>
2264
2265 <para>Functions such as
2266 <literal>gdk_pixmap_create_from_xpm()</literal> require a
2267 valid window as a parameter. During the initialisation phase
2268 of an application, a valid window may not be available without
2269 showing a window, which may be inappropriate. In order to
2270 avoid this, a function such as
2271 <literal>gdk_pixmap_colormap_create_from_xpm</literal> can be
2272 used, as in:</para>
2273
2274 <programlisting role="C">
2275   char *pixfile = "foo.xpm";
2276   GtkWidget *top, *box, *pixw;
2277   GdkPixmap *pixmap, *pixmap_mask;
2278
2279   top = gtk_window_new (GKT_WINDOW_TOPLEVEL);
2280   box = gtk_hbox_new (FALSE, 4);
2281   gtk_conainer_add (GTK_CONTAINER(top), box);
2282  
2283   pixmap = gdk_pixmap_colormap_create_from_xpm (
2284                NULL, gtk_widget_get_colormap(top),
2285                &amp;pixmap_mask, NULL, pixfile);
2286   pixw = gtk_pixmap_new (pixmap, pixmap_mask);
2287   gdk_pixmap_unref (pixmap);
2288   gdk_pixmap_unref (pixmap_mask);
2289 </programlisting>
2290
2291 </sect1>
2292
2293 <!-- ----------------------------------------------------------------- -->
2294
2295 <sect1>
2296 <title>How do I do drag-and-drop?</title>
2297
2298 <para>GTK+ has a high level set of functions for doing inter-process
2299 communication via the drag-and-drop system. GTK+ can perform
2300 drag-and-drop on top of the low level Xdnd and Motif drag-and-drop
2301 protocols.</para>
2302
2303 <para>The documentation on GTK+ drag-and-drop isn't complete, but there
2304 is some information in the <ulink
2305 url="http://www.gtk.org/tutorial/">Tutorial</ulink>. You should also
2306 look at the drag-and-drop example code that is part of the GTK+ source
2307 distribution, in the file <filename>gtk/testdnd.c</filename>.</para>
2308
2309 </sect1>
2310
2311 <!-- ----------------------------------------------------------------- -->
2312
2313 <sect1>
2314 <title>Why does GTK+/GLib leak memory?</title>
2315
2316 <para>It doesn't. Both GLib and the C library (malloc implementation)
2317 will cache allocated memory on occasion, even if you free it with
2318 free().</para>
2319
2320 <para>So you can't generally use tools such as top to see if you are
2321 using free() properly (aside from the very roughest of estimations, i.e.
2322 if you are really, really screwing up top will show that, but you can't
2323 distinguish small mistakes from the GLib/malloc caches).</para>
2324
2325 <para>In order to find memory leaks, use proper memory profiling
2326 tools.</para>
2327
2328 </sect1>
2329
2330 </chapter>
2331
2332 <!-- ***************************************************************** -->
2333 <chapter>
2334 <title>Development with GTK+: widget specific questions</title>
2335
2336 <!-- ----------------------------------------------------------------- -->
2337
2338 <sect1>
2339 <title>How do I find out about the selection of a GtkList?</title>
2340
2341 <para>Get the selection something like this:</para>
2342
2343 <programlisting role="C">
2344 GList *sel;
2345 sel = GTK_LIST(list)->selection;
2346 </programlisting>
2347
2348 <para>This is how GList is defined (quoting glist.h):</para>
2349
2350 <programlisting role="C">
2351 typedef struct _GList GList;
2352
2353 struct _GList
2354 {
2355   gpointer data;
2356   GList *next;
2357   GList *prev;
2358 };
2359 </programlisting>
2360
2361 <para>A GList structure is just a simple structure for doubly
2362 linked lists. There exist several g_list_*() functions to
2363 modify a linked list in glib.h.  However the
2364 GTK_LIST(MyGtkList)->selection is maintained by the
2365 gtk_list_*() functions and should not be modified.</para>
2366
2367
2368 <para>The selection_mode of the GtkList determines the
2369 selection facilities of a GtkList and therefore the contents
2370 of GTK_LIST(AnyGtkList)->selection:</para>
2371
2372 <informaltable frame="all">
2373 <tgroup cols="2">
2374 <thead>
2375 <row>
2376 <entry><literal>selection_mode</literal></entry>
2377 <entry><literal> GTK_LIST()->selection</literal>
2378 contents</entry>
2379 </row>
2380 </thead>
2381
2382 <tbody>
2383 <row>
2384 <entry><literal>GTK_SELECTION_SINGLE</literal></entry>
2385 <entry>selection is either NULL or contains a GList*
2386 pointer for a single selected item.</entry>
2387 </row>
2388
2389 <row>
2390 <entry><literal>GTK_SELECTION_BROWSE</literal></entry>
2391 <entry>selection is NULL if the list contains no
2392 widgets, otherwise it contains a GList*
2393 pointer for one GList structure.</entry>
2394 </row>
2395
2396 <row>
2397 <entry><literal>GTK_SELECTION_MULTIPLE</literal></entry>
2398 <entry>selection is NULL if no listitems are selected
2399 or a a GList* pointer for the first selected
2400 item. that in turn points to a GList structure
2401 for the second selected item and so
2402 on.</entry>
2403 </row>
2404
2405 <row>
2406 <entry><literal>GTK_SELECTION_EXTENDED</literal></entry>
2407 <entry>selection is NULL.</entry>
2408 </row>
2409
2410 </tbody>
2411 </tgroup>
2412 </informaltable>
2413
2414 <para>The data field of the GList structure
2415 GTK_LIST(MyGtkList)->selection points to the first
2416 GtkListItem that is selected.  So if you would like to
2417 determine which listitems are selected you should go like
2418 this:</para>
2419
2420 <programlisting role="C">
2421 {
2422         gchar           *list_items[]={
2423                                 "Item0",
2424                                 "Item1",
2425                                 "foo",
2426                                 "last Item",
2427                         };
2428         guint           nlist_items=sizeof(list_items)/sizeof(list_items[0]);
2429         GtkWidget       *list_item;
2430         guint           i;
2431
2432         list=gtk_list_new();
2433         gtk_list_set_selection_mode(GTK_LIST(list), GTK_SELECTION_MULTIPLE);
2434         gtk_container_add(GTK_CONTAINER(AnyGtkContainer), list);
2435         gtk_widget_show (list);
2436
2437         for (i = 0; i < nlist_items; i++)
2438         {
2439                 list_item=gtk_list_item_new_with_label(list_items[i]);
2440                 gtk_object_set_user_data(GTK_OBJECT(list_item), (gpointer)i);
2441                 gtk_container_add(GTK_CONTAINER(list), list_item);
2442                 gtk_widget_show(list_item);
2443         }
2444 }
2445 </programlisting>
2446
2447 <para>To get known about the selection:</para>
2448
2449 <programlisting role="C">
2450 {
2451         GList   *items;
2452
2453         items=GTK_LIST(list)->selection;
2454
2455         printf("Selected Items: ");
2456         while (items) {
2457                 if (GTK_IS_LIST_ITEM(items->data))
2458                         printf("%d ", (guint) 
2459                 gtk_object_get_user_data(items->data));
2460                 items=items->next;
2461         }
2462         printf("\n");
2463 }
2464 </programlisting>
2465
2466 </sect1>
2467
2468 <!-- ----------------------------------------------------------------- -->
2469
2470 <sect1>
2471 <title>How do I stop the column headings of a GtkCList
2472 disappearing when the list is scrolled?</title>
2473
2474 <para>This happens when a GtkCList is packed into a
2475 GtkScrolledWindow using the function
2476 <literal>gtk_scroll_window_add_with_viewport()</literal>. The prefered
2477 method of adding a CList to a scrolled window is to use the
2478 function <literal>gtk_container_add</literal>, as in:</para>
2479
2480 <programlisting role="C">
2481     GtkWidget *scrolled, *clist;
2482     char *titles[] = { "Title1" , "Title2" };
2483
2484     scrolled = gtk_scrolled_window_new(NULL, NULL);
2485
2486     clist = gtk_clist_new_with_titles(2, titles);
2487     gtk_container_add(GTK_CONTAINER(scrolled), clist);
2488 </programlisting>
2489
2490 </sect1>
2491
2492 <!-- ----------------------------------------------------------------- -->
2493
2494 <sect1>
2495 <title>I don't want the user of my applications to enter text
2496 into a GtkCombo. Any idea?</title>
2497
2498 <para>A GtkCombo has an associated entry which can be accessed
2499 using the following expression:</para>
2500
2501 <programlisting role="C">
2502       GTK_COMBO(combo_widget)->entry
2503 </programlisting>
2504
2505 <para>If you don't want the user to be able to modify the
2506 content of this entry, you can use the
2507 gtk_entry_set_editable() function:</para>
2508
2509
2510 <programlisting role="C">
2511       void gtk_entry_set_editable(GtkEntry *entry, 
2512                                   gboolean editable);
2513 </programlisting>
2514
2515 <para>Set the editable parameter to FALSE to disable typing
2516 into the entry.</para>
2517 </sect1>
2518
2519 <!-- ----------------------------------------------------------------- -->
2520
2521 <sect1>
2522 <title>How do I catch a combo box change?</title>
2523
2524 <para>The entry which is associated to your GtkCombo send a
2525 "changed" signal when:</para>
2526
2527 <itemizedlist>
2528 <listitem><simpara>some text is typed in</simpara>
2529 </listitem>
2530
2531 <listitem><simpara>the selection of the combo box is changed</simpara>
2532 </listitem>
2533 </itemizedlist>
2534
2535 <para>To catch any combo box change, simply connect your
2536 signal handler with</para>
2537
2538 <programlisting role="C">
2539       gtk_signal_connect(GTK_COMBO(cb)->entry,
2540                          "changed",
2541                          GTK_SIGNAL_FUNC(my_cb_change_handler),
2542                          NULL);
2543 </programlisting>
2544
2545 </sect1>
2546
2547 <!-- ----------------------------------------------------------------- -->
2548
2549 <sect1>
2550 <title>How can I define a separation line in a menu?</title>
2551
2552 <para>See the <ulink
2553 url="http://www.gtk.org/tutorial/">Tutorial</ulink> for
2554 information on how to create menus. However, to create a
2555 separation line in a menu, just insert an empty menu item:</para>
2556
2557 <programlisting role="C">
2558 menuitem = gtk_menu_item_new();
2559 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
2560 gtk_widget_show(menuitem);
2561 </programlisting>
2562
2563 </sect1>
2564
2565 <!-- ----------------------------------------------------------------- -->
2566
2567 <sect1>
2568 <title>How can I right justify a menu, such as Help?</title>
2569
2570 <para>Depending on if you use the MenuFactory or not, there
2571 are two ways to proceed. With the MenuFactory, use something
2572 like the following:</para>
2573
2574 <programlisting role="C">
2575 menu_path = gtk_menu_factory_find (factory,  "&lt;MyApp&gt;/Help");
2576 gtk_menu_item_right_justify(menu_path->widget);
2577 </programlisting>
2578
2579 <para>If you do not use the MenuFactory, you should simply
2580 use:</para>
2581
2582
2583 <programlisting role="C">
2584 gtk_menu_item_right_justify(my_menu_item);
2585 </programlisting>
2586
2587 </sect1>
2588
2589 <!-- ----------------------------------------------------------------- -->
2590
2591 <sect1>
2592 <title>How do I add some underlined accelerators to menu
2593 items?</title>
2594
2595 <para>Damon Chaplin, the technical force behind the Glade
2596 project, provided the following code sample (this code is an
2597 output from Glade). It creates a small <GUIMenu>File</guimenu> menu item
2598 with only one child (<guimenu>New</guimenu>). The F in
2599 <guimenu>File</guimenu> and the N in <guimenu>New</guimenu> are
2600 underlined, and the relevant accelerators are created.</para>
2601
2602 <programlisting role="C">
2603   menubar1 = gtk_menu_bar_new ();
2604   gtk_object_set_data (GTK_OBJECT (window1), "menubar1", menubar1);
2605   gtk_widget_show (menubar1);
2606   gtk_box_pack_start (GTK_BOX (vbox1), menubar1, FALSE, FALSE, 0);
2607
2608   file1 = gtk_menu_item_new_with_label ("");
2609   tmp_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (file1)->child),
2610                                    _("_File"));
2611   gtk_widget_add_accelerator (file1, "activate_item", accel_group,
2612                               tmp_key, GDK_MOD1_MASK, 0);
2613   gtk_object_set_data (GTK_OBJECT (window1), "file1", file1);
2614   gtk_widget_show (file1);
2615   gtk_container_add (GTK_CONTAINER (menubar1), file1);
2616
2617   file1_menu = gtk_menu_new ();
2618   file1_menu_accels = gtk_menu_ensure_uline_accel_group (GTK_MENU (file1_menu));
2619   gtk_object_set_data (GTK_OBJECT (window1), "file1_menu", file1_menu);
2620   gtk_menu_item_set_submenu (GTK_MENU_ITEM (file1), file1_menu);
2621
2622   new1 = gtk_menu_item_new_with_label ("");
2623   tmp_key = gtk_label_parse_uline (GTK_LABEL (GTK_BIN (new1)->child),
2624                                    _("_New"));
2625   gtk_widget_add_accelerator (new1, "activate_item", file1_menu_accels,
2626                               tmp_key, 0, 0);
2627   gtk_object_set_data (GTK_OBJECT (window1), "new1", new1);
2628   gtk_widget_show (new1);
2629   gtk_container_add (GTK_CONTAINER (file1_menu), new1);
2630 </programlisting>
2631
2632 </sect1>
2633
2634 <!-- ----------------------------------------------------------------- -->
2635
2636 <sect1>
2637 <title>How can I retrieve the text from a GtkMenuItem?</title>
2638
2639 <para>You can usually retrieve the label of a specific
2640 GtkMenuItem with:</para>
2641
2642 <programlisting role="C">
2643     if (GTK_BIN (menu_item)->child)
2644     {
2645       GtkWidget *child = GTK_BIN (menu_item)->child;
2646   
2647       /* do stuff with child */
2648       if (GTK_IS_LABEL (child))
2649       {
2650         gchar *text;
2651     
2652         gtk_label_get (GTK_LABEL (child), &amp;text);
2653         g_print ("menu item text: %s\n", text);
2654       }
2655     }
2656 </programlisting>
2657
2658 <para>To get the active menu item from a GtkOptionMenu you can
2659 do:</para>
2660
2661 <programlisting role="C">
2662 if (GTK_OPTION_MENU (option_menu)->menu_item)
2663 {
2664   GtkWidget *menu_item = GTK_OPTION_MENU (option_menu)->menu_item;
2665 }
2666 </programlisting>
2667
2668 <para>But, there's a catch. For this specific case, you can
2669 <emphasis>not</emphasis> get the label widget from
2670 <literal>menu_item</literal> with the above code, because the
2671 option menu reparents the menu_item's child temporarily to
2672 display the currently active contents. So to retrive the child
2673 of the currently active menu_item of an option menu, you'll
2674 have to do:</para>
2675
2676 <programlisting role="C">
2677     if (GTK_BIN (option_menu)->child)
2678     {
2679       GtkWidget *child = GTK_BIN (option_menu)->child;
2680
2681       /* do stuff with child */
2682     }
2683 </programlisting>
2684
2685 </sect1>
2686
2687 <!-- ----------------------------------------------------------------- -->
2688
2689 <sect1>
2690 <title>How do I right (or otherwise) justify a
2691 GtkLabel?</title>
2692
2693 <para>Are you sure you want to <emphasis>justify</emphasis>
2694 the labels? The label class contains the
2695 <literal>gtk_label_set_justify()</literal> function that is
2696 used to control the justification of a multi-line
2697 label.</para>
2698
2699 <para>What you probably want is to set the <emphasis>alignment</emphasis>
2700 of the label, ie right align it, center it or left align
2701 it. If you want to do this, you should use:</para>
2702
2703 <programlisting role="C">
2704 void gtk_misc_set_alignment (GtkMisc *misc,
2705                              gfloat xalign,
2706                              gfloat yalign);
2707 </programlisting>
2708
2709 <para>where the <literal>xalign</literal> and
2710 <literal>yalign</literal> values are floats in
2711 [0.00;1.00].</para>
2712
2713 <programlisting role="C">
2714 GtkWidget       *label;
2715
2716 /* horizontal : left align, vertical : top */
2717 gtk_misc_set_alignment(GTK_MISC(label), 0.0f, 0.0f);
2718
2719 /* horizontal : centered, vertical : centered */
2720 gtk_misc_set_alignment(GTK_MISC(label), 0.5f, 0.5f);
2721
2722 /* horizontal : right align, vertical : bottom */
2723 gtk_misc_set_alignment(GTK_MISC(label), 1.0f, 1.0f);
2724 </programlisting>
2725
2726 </sect1>
2727
2728 <!-- ----------------------------------------------------------------- -->
2729
2730 <sect1>
2731 <title>How do I set the background color of a GtkLabel
2732 widget?</title>
2733
2734 <para>The GtkLabel widget is one of a few GTK+ widgets that
2735 don't create their own window to render themselves
2736 into. Instead, they draw themselves directly onto their
2737 parents window.</para>
2738
2739 <para>This means that in order to set the background color for
2740 a GtkLabel widget, you need to change the background color of
2741 its parent, i.e. the object that you pack it into.</para>
2742
2743 </sect1> 
2744
2745 <!-- ----------------------------------------------------------------- -->
2746
2747 <sect1>
2748 <title>How do I set the color and font of a GtkLabel using a
2749 Resource File?</title>
2750
2751 <para>The widget name path constructed for a Label consists of
2752 the widget names of its object hierarchy as well, e.g.</para>
2753
2754 <para><literallayout>
2755 <literal>window (name: humphrey)</literal>
2756 <literal>  hbox</literal>
2757 <literal>     label (name: mylabel)</literal>
2758 </literallayout></para>
2759
2760 <para>The widget path your pattern needs to match would be:
2761 <literal>humphrey.GtkHBox.mylabel</literal></para>
2762
2763 <para>The resource file may look something like:</para>
2764
2765 <programlisting role="C">
2766 style "title"
2767 {
2768       fg[NORMAL] = {1.0, 0.0, 0.0}
2769       font = "-adobe-helvetica-bold-r-normal--*-140-*-*-*-*-*-*"
2770 }
2771 widget "*mylabel" style "title"
2772 </programlisting>
2773
2774 <para>In your program, you would also need to give a name to
2775 the Label widget, which can be done using:</para>
2776
2777 <programlisting role="C">
2778   label = gtk_label_new("Some Label Text");
2779   gtk_widget_set_name(label, "mylabel");
2780   gtk_widget_show(label);
2781 </programlisting>
2782
2783 </sect1>
2784
2785 <!-- ----------------------------------------------------------------- -->
2786
2787 <sect1>
2788 <title>How do I configure Tooltips in a Resource File?</title>
2789
2790 <para>The tooltip's window is named "gtk-tooltips",
2791 GtkTooltips in itself is not a GtkWidget (though a GtkObject)
2792 and as such is not attempted to match any widget styles.</para>
2793
2794 <para>So, you resource file should look something like:</para>
2795
2796 <programlisting role="C">
2797 style "postie"
2798 {
2799       bg[NORMAL] = {1.0, 1.0, 0.0}
2800 }
2801 widget "gtk-tooltips*" style "postie"
2802 </programlisting>
2803
2804 </sect1>
2805
2806 <!-- ----------------------------------------------------------------- -->
2807
2808 <sect1>
2809 <title>I can't add more than (something like) 2000 chars in a
2810 GtkEntry. What's wrong?</title>
2811
2812 <para>There is now a known problem in the GtkEntry widget. In
2813 the <literal>gtk_entry_insert_text()</literal> function, the
2814 following lines limit the number of chars in the entry to
2815 2047.</para>
2816
2817 <programlisting role="C">
2818   /* The algorithms here will work as long as, the text size (a
2819    * multiple of 2), fits into a guint16 but we specify a shorter
2820    * maximum length so that if the user pastes a very long text, there
2821    * is not a long hang from the slow X_LOCALE functions.  */
2822
2823   if (entry->text_max_length == 0)
2824     max_length = 2047;
2825   else
2826     max_length = MIN (2047, entry->text_max_length);
2827 </programlisting>
2828
2829 </sect1>
2830
2831 <!-- ----------------------------------------------------------------- -->
2832
2833 <sect1>
2834 <title>How do I make a GtkEntry widget activate on pressing
2835 the Return key?</title>
2836
2837 <para>The Entry widget emits an 'activate' signal when you
2838 press return in it. Just attach to the activate signal on the
2839 entry and do whatever you want to do. Typical code would
2840 be:</para>
2841
2842 <programlisting role="C">
2843   entry = gtk_entry_new();
2844   gtk_signal_connect (GTK_OBJECT(entry), "activate",
2845                       GTK_SIGNAL_FUNC(entry_callback),
2846                       NULL);
2847 </programlisting>
2848
2849 </sect1>
2850
2851 <!-- ----------------------------------------------------------------- -->
2852
2853 <sect1>
2854 <title>How do I validate/limit/filter the input to a GtkEntry?</title>
2855
2856 <para>If you want to validate the text that a user enters into
2857 a GtkEntry widget you can attach to the "insert_text" signal
2858 of the entry, and modify the text within the callback
2859 function. The example below forces all characters to
2860 uppercase, and limits the range of characters to A-Z. Note
2861 that the entry is cast to an object of type GtkEditable, from
2862 which GtkEntry is derived.</para>
2863
2864 <programlisting role="C">
2865 #include &lt;ctype.h&gt;
2866 #include &lt;gtk/gtk.h&gt;
2867
2868 void insert_text_handler (GtkEntry    *entry,
2869                           const gchar *text,
2870                           gint         length,
2871                           gint        *position,
2872                           gpointer     data)
2873 {
2874   GtkEditable *editable = GTK_EDITABLE(entry);
2875   int i, count=0;
2876   gchar *result = g_new (gchar, length);
2877
2878   for (i=0; i < length; i++) {
2879     if (!isalpha(text[i]))
2880       continue;
2881     result[count++] = islower(text[i]) ? toupper(text[i]) : text[i];
2882   }
2883   
2884   if (count > 0) {
2885     gtk_signal_handler_block_by_func (GTK_OBJECT (editable),
2886                                       GTK_SIGNAL_FUNC (insert_text_handler),
2887                                       data);
2888     gtk_editable_insert_text (editable, result, count, position);
2889     gtk_signal_handler_unblock_by_func (GTK_OBJECT (editable),
2890                                         GTK_SIGNAL_FUNC (insert_text_handler),
2891                                         data);
2892   }
2893   gtk_signal_emit_stop_by_name (GTK_OBJECT (editable), "insert_text");
2894   
2895   g_free (result);
2896 }
2897
2898 int main (int   argc,
2899           char *argv[])
2900 {
2901   GtkWidget *window;
2902   GtkWidget *entry;
2903   
2904   gtk_init (&amp;argc, &amp;argv);
2905   
2906   /* create a new window */
2907   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
2908   gtk_window_set_title(GTK_WINDOW (window), "GTK Entry");
2909   gtk_signal_connect(GTK_OBJECT (window), "delete_event",
2910                      (GtkSignalFunc) gtk_exit, NULL);
2911   
2912   entry = gtk_entry_new();
2913   gtk_signal_connect(GTK_OBJECT(entry), "insert_text",
2914                      GTK_SIGNAL_FUNC(insert_text_handler),
2915                      NULL);
2916   gtk_container_add(GTK_CONTAINER (window), entry);
2917   gtk_widget_show(entry);
2918   
2919   gtk_widget_show(window);
2920   
2921   gtk_main();
2922   return(0);
2923 }
2924 </programlisting>
2925
2926 </sect1>
2927
2928 <!-- ----------------------------------------------------------------- -->
2929
2930 <sect1>
2931 <title>How do I use horizontal scrollbars with a GtkText widget?</title>
2932
2933 <para>The short answer is that you can't. The current version
2934 of the GtkText widget does not support horizontal
2935 scrolling. There is an intention to completely rewrite the
2936 GtkText widget, at which time this limitation will be
2937 removed.</para>
2938
2939 </sect1>
2940
2941 <!-- ----------------------------------------------------------------- -->
2942
2943 <sect1>
2944 <title>How do I change the font of a GtkText widget?</title>
2945
2946 <para>There are a couple of ways of doing this. As GTK+ allows
2947 the appearance of applications to be changed at run time using
2948 resources you can use something like the following in the
2949 appropriate file:</para>
2950
2951 <programlisting role="C">
2952 style "text"
2953 {
2954   font = "-adobe-helvetica-medium-r-normal--*-100-*-*-*-*-*-*"
2955 }
2956 </programlisting>
2957
2958 <para>Another way to do this is to load a font within your
2959 program, and then use this in the functions for adding text to
2960 the text widget. You can load a font using, for example:</para>
2961
2962 <programlisting role="C">
2963   GdkFont *font;
2964   font = gdk_font_load("-adobe-helvetica-medium-r-normal--*-140-*-*-*-*-*-*");
2965 </programlisting>
2966
2967 </sect1>
2968
2969 <!-- ----------------------------------------------------------------- -->
2970
2971 <sect1>
2972 <title>How do I set the cursor position in a GtkText
2973 object?</title>
2974
2975 <para>Notice that the response is valid for any object that
2976 inherits from the GtkEditable class.</para>
2977
2978 <para>Are you sure that you want to move the cursor position?
2979 Most of the time, while the cursor position is good, the
2980 insertion point does not match the cursor position. If this
2981 apply to what you really want, then you should use the
2982 <literal>gtk_text_set_point()</literal> function. If you want
2983 to set the insertion point at the current cursor position, use
2984 the following:</para>
2985
2986 <programlisting role="C">
2987   gtk_text_set_point(GTK_TEXT(text),
2988   gtk_editable_get_position(GTK_EDITABLE(text)));
2989 </programlisting>
2990
2991 <para>If you want the insertion point to follow the cursor at
2992 all time, you should probably catch the button press event,
2993 and then move the insertion point. Be careful : you'll have to
2994 catch it after the widget has changed the cursor position
2995 though. Thomas Mailund Jensen proposed the following
2996 code:</para>
2997
2998 <programlisting role="C">
2999 static void
3000 insert_bar (GtkWidget *text)
3001 {
3002   /* jump to cursor mark */
3003   gtk_text_set_point (GTK_TEXT (text),
3004   gtk_editable_get_position (GTK_EDITABLE  (text)));
3005
3006   gtk_text_insert (GTK_TEXT (text), NULL, NULL, NULL,
3007      "bar", strlen ("bar"));
3008 }
3009
3010 int
3011 main (int argc, char *argv[])
3012 {
3013   GtkWidget *window, *text;
3014
3015   gtk_init (&amp;argc, &amp;argv);
3016
3017   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
3018   text = gtk_text_new (NULL, NULL);
3019   gtk_text_set_editable (GTK_TEXT (text), TRUE);
3020   gtk_container_add (GTK_CONTAINER (window), text);
3021
3022   /* connect after everything else */
3023   gtk_signal_connect_after (GTK_OBJECT(text), "button_press_event",
3024     GTK_SIGNAL_FUNC (insert_bar), NULL);
3025
3026   gtk_widget_show_all(window);
3027   gtk_main();
3028
3029   return 0;
3030 }
3031 </programlisting>
3032
3033 <para>Now, if you really want to change the cursor position,
3034 you should use the
3035 <literal>gtk_editable_set_position()</literal>
3036 function.</para>
3037
3038 </sect1>
3039
3040 </chapter>
3041
3042 <!-- ***************************************************************** -->
3043 <chapter>
3044 <title>About GDK</title>
3045
3046 <!-- ----------------------------------------------------------------- -->
3047
3048 <sect1>
3049 <title>What is GDK?</title>
3050
3051 <para>GDK is basically a wrapper around the standard Xlib
3052 function calls. If you are at all familiar with Xlib, a lot of
3053 the functions in GDK will require little or no getting used
3054 to. All functions are written to provide an way to access Xlib
3055 functions in an easier and slightly more intuitive manner. In
3056 addition, since GDK uses GLib (see below), it will be more
3057 portable and safer to use on multiple platforms.</para>
3058
3059 <!-- Examples, anybody? I've been mulling some over. NF -->
3060
3061 </sect1>
3062
3063 <!-- ----------------------------------------------------------------- -->
3064
3065 <sect1>
3066 <title>How do I use color allocation?</title>
3067
3068 <para>One of the nice things about GDK is that it's based on
3069 top of Xlib; this is also a problem, especially in the area of
3070 color management. If you want  to use color in your program
3071 (drawing a rectangle or such, your code  should look something
3072 like this:</para>
3073
3074 <programlisting role="C">
3075 {
3076   GdkColor *color;
3077   int width, height;
3078   GtkWidget *widget;
3079   GdkGC *gc;
3080
3081   ...
3082   
3083   /* first, create a GC to draw on */
3084   gc = gdk_gc_new(widget->window);
3085
3086   /* find proper dimensions for rectangle */
3087   gdk_window_get_size(widget->window, &amp;width, &amp;height);
3088
3089   /* the color we want to use */
3090   color = (GdkColor *)malloc(sizeof(GdkColor));
3091   
3092   /* red, green, and blue are passed values, indicating the RGB triple
3093    * of the color we want to draw. Note that the values of the RGB components
3094    * within the GdkColor are taken from 0 to 65535, not 0 to 255.
3095    */
3096   color->red = red * (65535/255);
3097   color->green = green * (65535/255);
3098   color->blue = blue * (65535/255);
3099   
3100   /* the pixel value indicates the index in the colormap of the color.
3101    * it is simply a combination of the RGB values we set earlier
3102    */
3103   color->pixel = (gulong)(red*65536 + green*256 + blue);
3104
3105   /* However, the pixel valule is only truly valid on 24-bit (TrueColor)
3106    * displays. Therefore, this call is required so that GDK and X can
3107    * give us the closest color available in the colormap
3108    */
3109   gdk_color_alloc(gtk_widget_get_colormap(widget), color);
3110
3111   /* set the foreground to our color */
3112   gdk_gc_set_foreground(gc, color);
3113   
3114   /* draw the rectangle */
3115   gdk_draw_rectangle(widget->window, gc, 1, 0, 0, width, height);
3116
3117   ...
3118 }
3119 </programlisting>
3120
3121 </sect1>
3122
3123 </chapter>
3124
3125 <!-- ***************************************************************** -->
3126 <chapter>
3127 <title>About GLib</title>
3128
3129 <!-- ----------------------------------------------------------------- -->
3130
3131 <sect1>
3132 <title>What is GLib?</title>
3133
3134 <para>GLib is a library of useful functions and definitions
3135 available for use  when creating GDK and GTK applications. It
3136 provides replacements for some standard libc functions, such
3137 as malloc, which are buggy on some systems.</para>
3138
3139 <para>It also provides routines for handling:</para>
3140
3141 <itemizedlist spacing=compact>
3142 <listitem><simpara>Doubly Linked Lists</simpara>
3143 </listitem>
3144
3145 <listitem><simpara>Singly Linked Lists</simpara>
3146 </listitem>
3147
3148 <listitem><simpara>Timers</simpara>
3149 </listitem>
3150
3151 <listitem><simpara>String Handling</simpara>
3152 </listitem>
3153
3154 <listitem><simpara>A Lexical Scanner</simpara>
3155 </listitem>
3156
3157 <listitem><simpara>Error Functions</simpara>
3158 </listitem>
3159 </itemizedlist>
3160
3161 </sect1>
3162
3163 <!-- ----------------------------------------------------------------- -->
3164
3165 <sect1>
3166 <title>How can I use the doubly linked lists?</title>
3167
3168 <para>The GList object is defined as:</para>
3169
3170 <programlisting role="C">
3171 typedef struct _GList GList;
3172
3173 struct _GList
3174 {
3175   gpointer data;
3176   GList *next;
3177   GList *prev;
3178 };
3179 </programlisting>
3180
3181 <para>To use the GList objects, simply:</para>
3182
3183 <programlisting role="C">
3184 GList   *list = NULL;
3185 GList   *listrunner;
3186 gint    array[] = { 1, 2, 3, 4, 5, 6 };
3187 gint    pos;
3188 gint    *value;
3189
3190 /* add data to the list */
3191 for (pos=0;pos < sizeof array; pos++) {
3192   list = g_list_append(list, (gpointer)&amp;array[pos]);
3193 }
3194
3195 /* run through the list */
3196 listrunner = g_list_first(list);
3197 while (listrunner) {
3198   value = (gint *)listrunner->data;
3199   printf("%d\n", *value);
3200   listrunner = g_list_next(listrunner);
3201 }
3202
3203 /* removing datas from the list */
3204 listrunner = g_list_first(list);
3205 list = g_list_remove_link(list, listrunner);
3206 list = g_list_remove(list, &amp;array[4]);
3207 </programlisting>
3208
3209 <para>The same code is usable with singly linked lists (GSList
3210 objects) by replacing g_list_* functions with the relevant
3211 g_slist_* ones (g_slist_append,  g_slist_remove, ...). Just
3212 remember that since you can't go backward in a singly linked
3213 list, there is no g_slist_first function - you'll need to keep
3214 a  reference on the first node of the list.</para>
3215
3216 <!-- Some Examples might be useful here! NF -->
3217 <!-- I believe it should be better :) ED -->
3218 <!-- Linked lists are pretty standard data structures - don't want to
3219 over do it - TRG -->
3220
3221 </sect1>
3222
3223 <!-- ----------------------------------------------------------------- -->
3224
3225 <sect1>
3226 <title>Memory does not seem to be released when I free the
3227 list nodes I've allocated</title>
3228
3229 <para>GLib tries to be "intelligent" on this special issue: it
3230 assumes that you are likely to reuse the objects, so caches
3231 the allocated memory. If you do not want to use this behavior,
3232 you'll probably want to set up a special allocator.</para>
3233
3234 <para>To quote Tim Janik:</para>
3235 <para><quote>If you have a certain portion of code that uses *lots*
3236 of GLists or GNodes, and you know you'd better want to release
3237 all of them after a short while, you'd want to use a
3238 GAllocator. Pushing an allocator into g_list will make all
3239 subsequent glist operations private to that allocator's memory
3240 pool (and thus you have to take care to pop the allocator
3241 again, before making any external calls): </quote></para>
3242
3243 <programlisting role="C">
3244 GAllocator *allocator;
3245 GList *list = NULL;
3246 guint i;
3247
3248 /* set a new allocation pool for GList nodes */
3249 allocator = g_allocator_new ("list heap", 1024);
3250 g_list_push_allocator (allocator);
3251
3252 /* do some list operations */
3253 for (i = 0; i < 4096; i++)
3254   list = g_list_prepend (list, NULL);
3255 list = g_list_reverse (list);
3256
3257 /* beware to pop allocator befor calling external functions */
3258 g_list_pop_allocator ();
3259 gtk_label_set_text (GTK_LABEL (some_label), "some text");
3260
3261 /* and set our private glist pool again */
3262 g_list_push_allocator (allocator);
3263
3264 /* do some list operations */
3265 g_list_free (list);
3266 list = NULL;
3267 for (i = 0; i < 4096; i++)
3268   list = g_list_prepend (list, NULL);
3269   
3270 /* and back out (while freeing all of the list nodes in our pool) */
3271 g_list_pop_allocator ();
3272 g_allocator_free (allocator);
3273 </programlisting>
3274
3275 </sect1>
3276
3277 <!-- ----------------------------------------------------------------- -->
3278
3279 <sect1>
3280 <title>Why use g_print, g_malloc, g_strdup and fellow glib
3281 functions?</title>
3282
3283 <para>Thanks to Tim Janik who wrote to gtk-list: (slightly
3284 modified)</para>
3285
3286 <para><quote>Regarding g_malloc(), g_free() and siblings, these
3287 functions are much safer than their libc equivalents. For
3288 example, g_free() just returns if called with NULL. Also, if
3289 USE_DMALLOC is defined, the definition for these functions
3290 changes (in glib.h) to use MALLOC(), FREE() etc... If
3291 MEM_PROFILE or MEM_CHECK are defined, there are even small
3292 statistics made counting the used block sizes (shown by
3293 g_mem_profile() / g_mem_check()).</quote></para>
3294
3295 <para><quote>Considering the fact that glib provides an interface for
3296 memory chunks to save space if you have lots of blocks that
3297 are always the same size and to mark them ALLOC_ONLY if
3298 needed, it is just straight forward to create a small saver
3299 (debug able) wrapper around the normal malloc/free stuff as
3300 well - just like gdk covers Xlib. ;)</quote></para>
3301
3302 <para><quote>Using g_error() and g_warning() inside of applications
3303 like the GIMP that fully rely on gtk even gives the
3304 opportunity to pop up a window showing the messages inside of
3305 a gtk window with your own handler (by using
3306 g_set_error_handler()) along the lines of
3307 <literal>gtk_print()</literal> (inside of
3308 gtkmain.c).</quote></para>
3309
3310 </sect1>
3311
3312 <!-- ----------------------------------------------------------------- -->
3313
3314 <sect1>
3315 <title>What's a GScanner and how do I use one?</title>
3316
3317 <para>A GScanner will tokenize your text, that is, it'll return
3318 an integer for every word or number that appears in its input
3319 stream, following certain (customizable) rules to perform this
3320 translation. You still need to write the parsing functions on
3321 your own though.</para>
3322
3323 <para>Here's a little test program supplied by Tim Janik that
3324 will parse</para>
3325
3326 <para><literallayout>
3327 <literal>&lt;SYMBOL&gt; = &lt;OPTIONAL-MINUS&gt; &lt;NUMBER&gt; ;</literal>
3328 </literallayout></para>
3329
3330 <para>constructs, while skipping "#\n" and "/**/" style
3331 comments.</para>
3332
3333 <programlisting role="C">
3334 #include &lt;glib.h&gt;
3335
3336 /* some test text to be fed into the scanner */
3337 static const gchar *test_text =
3338 ( "ping = 5;\n"
3339   "/* slide in some \n"
3340   " * comments, just for the\n"
3341   " * fun of it \n"
3342   " */\n"
3343   "pong = -6; \n"
3344   "\n"
3345   "# the next value is a float\n"
3346   "zonk = 0.7;\n"
3347   "# redefine ping\n"
3348   "ping = - 0.5;\n" );
3349
3350 /* define enumeration values to be returned for specific symbols */
3351 enum {
3352   SYMBOL_PING = G_TOKEN_LAST + 1,
3353   SYMBOL_PONG = G_TOKEN_LAST + 2,
3354   SYMBOL_ZONK = G_TOKEN_LAST + 3
3355 };
3356
3357 /* symbol array */
3358 static const struct {
3359   gchar *symbol_name;
3360   guint  symbol_token;
3361 } symbols[] = {
3362   { "ping", SYMBOL_PING, },
3363   { "pong", SYMBOL_PONG, },
3364   { "zonk", SYMBOL_ZONK, },
3365   { NULL, 0, },
3366 }, *symbol_p = symbols;
3367
3368 static gfloat ping = 0;
3369 static gfloat pong = 0;
3370 static gfloat zonk = 0;
3371
3372 static guint
3373 parse_symbol (GScanner *scanner)
3374 {
3375   guint symbol;
3376   gboolean negate = FALSE;
3377
3378   /* expect a valid symbol */
3379   g_scanner_get_next_token (scanner);
3380   symbol = scanner->token;
3381   if (symbol < SYMBOL_PING ||
3382       symbol > SYMBOL_ZONK)
3383     return G_TOKEN_SYMBOL;
3384
3385   /* expect '=' */
3386   g_scanner_get_next_token (scanner);
3387   if (scanner->token != '=')
3388     return '=';
3389
3390   /* feature optional '-' */
3391   g_scanner_peek_next_token (scanner);
3392   if (scanner->next_token == '-')
3393     {
3394       g_scanner_get_next_token (scanner);
3395       negate = !negate;
3396     }
3397
3398   /* expect a float (ints are converted to floats on the fly) */
3399   g_scanner_get_next_token (scanner);
3400   if (scanner->token != G_TOKEN_FLOAT)
3401     return G_TOKEN_FLOAT;
3402
3403   /* make sure the next token is a ';' */
3404   if (g_scanner_peek_next_token (scanner) != ';')
3405     {
3406       /* not so, eat up the non-semicolon and error out */
3407       g_scanner_get_next_token (scanner);
3408       return ';';
3409     }
3410
3411   /* assign value, eat the semicolon and exit successfully */
3412   switch (symbol)
3413     {
3414     case SYMBOL_PING:
3415       ping = negate ? - scanner->value.v_float : scanner->value.v_float;
3416       break;
3417     case SYMBOL_PONG:
3418       pong = negate ? - scanner->value.v_float : scanner->value.v_float;
3419       break;
3420     case SYMBOL_ZONK:
3421       zonk = negate ? - scanner->value.v_float : scanner->value.v_float;
3422       break;
3423     }
3424   g_scanner_get_next_token (scanner);
3425
3426   return G_TOKEN_NONE;
3427 }
3428
3429 int
3430 main (int argc, char *argv[])
3431 {
3432   GScanner *scanner;
3433   guint expected_token;
3434
3435   scanner = g_scanner_new (NULL);
3436
3437   /* adjust lexing behaviour to suit our needs
3438    */
3439   /* convert non-floats (octal values, hex values...) to G_TOKEN_INT */
3440   scanner->config->numbers_2_int = TRUE;
3441   /* convert G_TOKEN_INT to G_TOKEN_FLOAT */
3442   scanner->config->int_2_float = TRUE;
3443   /* don't return G_TOKEN_SYMBOL, but the symbol's value */
3444   scanner->config->symbol_2_token = TRUE;
3445
3446   /* load symbols into the scanner */
3447   while (symbol_p->symbol_name)
3448     {
3449       g_scanner_add_symbol (scanner,
3450                             symbol_p->symbol_name,
3451                             GINT_TO_POINTER (symbol_p->symbol_token));
3452       symbol_p++;
3453     }
3454
3455   /* feed in the text */
3456   g_scanner_input_text (scanner, test_text, strlen (test_text));
3457
3458   /* give the error handler an idea on how the input is named */
3459   scanner->input_name = "test text";
3460
3461   /* scanning loop, we parse the input until its end is reached,
3462    * the scanner encountered a lexing error, or our sub routine came
3463    * across invalid syntax
3464    */
3465   do
3466     {
3467       expected_token = parse_symbol (scanner);
3468       
3469       g_scanner_peek_next_token (scanner);
3470     }
3471   while (expected_token == G_TOKEN_NONE &&
3472          scanner->next_token != G_TOKEN_EOF &&
3473          scanner->next_token != G_TOKEN_ERROR);
3474
3475   /* give an error message upon syntax errors */
3476   if (expected_token != G_TOKEN_NONE)
3477     g_scanner_unexp_token (scanner, expected_token, NULL, "symbol", NULL, NULL, TRUE);
3478
3479   /* finsish parsing */
3480   g_scanner_destroy (scanner);
3481
3482   /* print results */
3483   g_print ("ping: %f\n", ping);
3484   g_print ("pong: %f\n", pong);
3485   g_print ("zonk: %f\n", zonk);
3486   
3487   return 0;
3488 }
3489 </programlisting>
3490
3491 <para>You need to understand that the scanner will parse its
3492 input and tokenize it, it is up to you to interpret these
3493 tokens, not define their types before they get parsed,
3494 e.g. watch gscanner parse a string:</para>
3495
3496 <para><literallayout>
3497 <literal>"hi i am 17"</literal>
3498 <literal> |  | |  |</literal>
3499 <literal> |  | |  v</literal>
3500 <literal> |  | v  TOKEN_INT, value: 17</literal>
3501 <literal> |  v TOKEN_IDENTIFIER, value: "am"</literal>
3502 <literal> v  TOKEN_CHAR, value: 'i'</literal>
3503 <literal>TOKEN_IDENTIFIER, value: "hi"</literal>
3504 </literallayout></para>
3505
3506 <para>If you configure the scanner with:</para>
3507
3508 <programlisting role="C">
3509 scanner->config->int_2_float = TRUE;
3510 scanner->config->char_2_token = TRUE;
3511 scanner->config->scan_symbols = TRUE;
3512 </programlisting>
3513
3514 <para>and add "am" as a symbol with</para>
3515
3516 <programlisting role="C">
3517 g_scanner_add_symbol (scanner, "am", "symbol value");
3518 </programlisting>
3519
3520 <para>GScanner will parse it as</para>
3521
3522 <para><literallayout>
3523 <literal>"hi i am 17"</literal>
3524 <literal> |  | |  |</literal>
3525 <literal> |  | |  v</literal>
3526 <literal> |  | v  TOKEN_FLOAT, value: 17.0  (automatic int->float conversion)</literal>
3527 <literal> |  | TOKEN_SYMBOL, value: "symbol value"  (a successfull hash table lookup</literal>
3528 <literal> |  |                                       turned a TOKEN_IDENTIFIER into a</literal>
3529 <literal> |  |                                       TOKEN_SYMBOL and took over the</literal>
3530 <literal> |  v                                       symbol's value)</literal>
3531 <literal> v  'i'  ('i' can be a valid token as well, as all chars >0 and <256)</literal>
3532 <literal>TOKEN_IDENTIFIER, value: "hi"</literal>
3533 </literallayout></para>
3534
3535 <para>You need to match the token sequence with your code, and
3536 if you encounter something that you don't want, you error
3537 out:</para>
3538
3539 <programlisting role="C">
3540 /* expect an identifier ("hi") */
3541 g_scanner_get_next_token (scanner);
3542 if (scanner->token != G_TOKEN_IDENTIFIER)
3543   return G_TOKEN_IDENTIFIER;
3544 /* expect a token 'i' */
3545 g_scanner_get_next_token (scanner);
3546 if (scanner->token != 'i')
3547   return 'i';
3548 /* expect a symbol ("am") */
3549 g_scanner_get_next_token (scanner);
3550 if (scanner->token != G_TOKEN_SYMBOL)
3551   return G_TOKEN_SYMBOL;
3552 /* expect a float (17.0) */
3553 g_scanner_get_next_token (scanner);
3554 if (scanner->token != G_TOKEN_FLOAT)
3555   return G_TOKEN_FLOAT;
3556 </programlisting>
3557
3558 <para>If you got past here, you have parsed "hi i am 17" and
3559 would have accepted "dooh i am 42" and  "bah i am 0.75" as
3560 well, but you would have not accepted "hi 7 am 17" or "hi i hi
3561 17".</para>
3562
3563 </sect1>
3564
3565 </chapter>
3566
3567 <!-- ***************************************************************** -->
3568
3569 <chapter>
3570 <title>GTK+ FAQ Contributions, Maintainers and Copyright</title>
3571
3572 <para>If you would like to make a contribution to the FAQ, send either one
3573 of us an e-mail message with the exact text you think should be
3574 included (question and answer). With your help, this document can grow
3575 and become more useful!</para>
3576
3577 <para>This document is maintained by
3578 Tony Gale
3579 <ulink url="mailto:gale@gtk.org">&lt;gale@gtk.org&gt;</ulink>
3580
3581 Nathan Froyd
3582 <ulink url="mailto:maestrox@geocities.com">
3583 &lt;maestrox@geocities.com&gt;</ulink>,
3584 and 
3585 Emmanuel Deloget
3586 <ulink url="mailto:logout@free.fr">&lt;logout@free.fr&gt;</ulink>.
3587
3588 This FAQ was created by Shawn T. Amundson 
3589 <ulink url="mailto:amundson@gimp.org">
3590 &lt;amundson@gimp.org&gt;</ulink> who continues to provide support.
3591
3592 Contributions should be sent to Tony Gale <ulink
3593 url="mailto:gale@gtk.org">&lt;gale@gtk.org&gt;</ulink></para>
3594
3595 <para>The GTK+ FAQ is Copyright (C) 1997-2000 by Shawn T. Amundson, 
3596 Tony Gale, Emmanuel Deloget and Nathan Froyd.</para>
3597
3598 <para>Permission is granted to make and distribute verbatim copies of this
3599 manual provided the copyright notice and this permission notice are
3600 preserved on all copies.</para>
3601
3602 <para>Permission is granted to copy and distribute modified versions of this
3603 document under the conditions for verbatim copying, provided that this
3604 copyright notice is included exactly as in the original, and that the
3605 entire resulting derived work is distributed under the terms of a
3606 permission notice identical to this one.</para>
3607
3608 <para>Permission is granted to copy and distribute translations of this
3609 document into another language, under the above conditions for
3610 modified versions.</para>
3611
3612 <para>If you are intending to incorporate this document into a published
3613 work, please contact one of the maintainers, and we will make an
3614 effort to ensure that you have the most up to date information
3615 available.</para>
3616
3617 <para>There is no guarentee that this document lives up to its intended
3618 purpose.  This is simply provided as a free resource.  As such, the
3619 authors and maintainers of the information provided within can not
3620 make any guarentee that the information is even accurate.</para>
3621
3622 </chapter>
3623
3624 <!-- ----------------------------------------------------------------- -->
3625
3626 </book>