Sophie

Sophie

distrib > Fedora > 15 > x86_64 > by-pkgid > 0d2c760c00774fa1d350bb3c66520f68 > files > 444

GtkAda-doc-2.14.1-4.fc15.noarch.rpm

<HTML>
<HEAD>
<!-- Created by texi2html 1.56k from gtkada_ug.texi on February, 8  2011 -->

<TITLE>GtkAda User's Guide</TITLE>
</HEAD>
<BODY >
<H1>GtkAda User's Guide</H1>
<H2>Version 2.14.1</H2>
<H2>Document revision level 143240</H2>
<H2>Date: 2009/04/23</H2>
<ADDRESS>E. Briot, J. Brobecker, A. Charlet</ADDRESS>
<P>
<P><HR><P>
<H1>Table of Contents</H1>
<UL>
<LI><A NAME="TOC1" HREF="gtkada_ug.html#SEC1">Introduction: What is GtkAda ?</A>
</LI><LI><A NAME="TOC2" HREF="gtkada_ug.html#SEC2">Getting started with GtkAda</A>
</LI><UL>
<LI><A NAME="TOC3" HREF="gtkada_ug.html#SEC3">How to build and install GtkAda</A>
</LI><LI><A NAME="TOC4" HREF="gtkada_ug.html#SEC4">How to distribute a GtkAda application</A>
</LI><LI><A NAME="TOC5" HREF="gtkada_ug.html#SEC5">Organization of the GtkAda package</A>
</LI><LI><A NAME="TOC6" HREF="gtkada_ug.html#SEC6">How to compile an application with GtkAda</A>
</LI><UL>
<LI><A NAME="TOC7" HREF="gtkada_ug.html#SEC7">Using project files</A>
</LI><LI><A NAME="TOC8" HREF="gtkada_ug.html#SEC8">Using the command line</A>
</LI><UL>
<LI><A NAME="TOC9" HREF="gtkada_ug.html#SEC9">Unix systems</A>
</LI><LI><A NAME="TOC10" HREF="gtkada_ug.html#SEC10">Windows systems</A>
</LI></UL>
</UL>
<LI><A NAME="TOC11" HREF="gtkada_ug.html#SEC11">Architecture of the toolkit</A>
</LI><LI><A NAME="TOC12" HREF="gtkada_ug.html#SEC12">Widgets Hierarchy</A>
</LI></UL>
<LI><A NAME="TOC13" HREF="gtkada_ug.html#SEC13">Hierarchical composition of a window</A>
</LI><LI><A NAME="TOC14" HREF="gtkada_ug.html#SEC14">Signal handling</A>
</LI><UL>
<LI><A NAME="TOC15" HREF="gtkada_ug.html#SEC15">Predefined signals</A>
</LI><LI><A NAME="TOC16" HREF="gtkada_ug.html#SEC16">Connecting signals</A>
</LI><LI><A NAME="TOC17" HREF="gtkada_ug.html#SEC17">Handling user data</A>
</LI><UL>
<LI><A NAME="TOC18" HREF="gtkada_ug.html#SEC18">First case: simple user data</A>
</LI><LI><A NAME="TOC19" HREF="gtkada_ug.html#SEC19">Second case: using Object_Connect instead</A>
</LI><LI><A NAME="TOC20" HREF="gtkada_ug.html#SEC20">Third case: manually disconnecting the callback</A>
</LI><LI><A NAME="TOC21" HREF="gtkada_ug.html#SEC21">Fourth case: setting a watch on a specific widget</A>
</LI></UL>
</UL>
<LI><A NAME="TOC22" HREF="gtkada_ug.html#SEC22">Starting an application with GtkAda</A>
</LI><LI><A NAME="TOC23" HREF="gtkada_ug.html#SEC23">Resource files</A>
</LI><LI><A NAME="TOC24" HREF="gtkada_ug.html#SEC24">Memory management</A>
</LI><LI><A NAME="TOC25" HREF="gtkada_ug.html#SEC25">Tasking with GtkAda</A>
</LI><LI><A NAME="TOC26" HREF="gtkada_ug.html#SEC26">Processing external events</A>
</LI><LI><A NAME="TOC27" HREF="gtkada_ug.html#SEC27">Object-oriented features</A>
</LI><UL>
<LI><A NAME="TOC28" HREF="gtkada_ug.html#SEC28">General description of the tagged types</A>
</LI><UL>
<LI><A NAME="TOC29" HREF="gtkada_ug.html#SEC29">Why should I use object-oriented programing ?</A>
</LI><LI><A NAME="TOC30" HREF="gtkada_ug.html#SEC30">Type conversions from C to Ada widgets</A>
</LI></UL>
<LI><A NAME="TOC31" HREF="gtkada_ug.html#SEC31">Using tagged types to extend Gtk widgets</A>
</LI><LI><A NAME="TOC32" HREF="gtkada_ug.html#SEC32">Creating new widgets in Ada</A>
</LI><UL>
<LI><A NAME="TOC33" HREF="gtkada_ug.html#SEC33">Creating composite widgets</A>
</LI><LI><A NAME="TOC34" HREF="gtkada_ug.html#SEC34">Creating widgets from scratch</A>
</LI></UL>
</UL>
<LI><A NAME="TOC35" HREF="gtkada_ug.html#SEC35">Support for Glade, the Gtk GUI builder</A>
</LI><UL>
<LI><A NAME="TOC36" HREF="gtkada_ug.html#SEC36">Introduction</A>
</LI><LI><A NAME="TOC37" HREF="gtkada_ug.html#SEC37">Using Glade</A>
</LI><UL>
<LI><A NAME="TOC38" HREF="gtkada_ug.html#SEC38">Taking advantage of Glade's options</A>
</LI><LI><A NAME="TOC39" HREF="gtkada_ug.html#SEC39">Associating icons with buttons</A>
</LI><LI><A NAME="TOC40" HREF="gtkada_ug.html#SEC40">Note for GNU/Linux users</A>
</LI></UL>
<LI><A NAME="TOC41" HREF="gtkada_ug.html#SEC41">Gate</A>
</LI><UL>
<LI><A NAME="TOC42" HREF="gtkada_ug.html#SEC42">Invoking Gate</A>
</LI><LI><A NAME="TOC43" HREF="gtkada_ug.html#SEC43">Structure of the generated files</A>
</LI><UL>
<LI><A NAME="TOC44" HREF="gtkada_ug.html#SEC44">The main file</A>
</LI><LI><A NAME="TOC45" HREF="gtkada_ug.html#SEC45">Top level widget files</A>
</LI><LI><A NAME="TOC46" HREF="gtkada_ug.html#SEC46">Main handler file</A>
</LI><LI><A NAME="TOC47" HREF="gtkada_ug.html#SEC47">Top level widget signal files</A>
</LI></UL>
<LI><A NAME="TOC48" HREF="gtkada_ug.html#SEC48">Modifying generated files</A>
</LI><LI><A NAME="TOC49" HREF="gtkada_ug.html#SEC49">Adding Gate support for new widgets</A>
</LI><UL>
<LI><A NAME="TOC50" HREF="gtkada_ug.html#SEC50">Implementing the <CODE>Generate</CODE> procedure</A>
</LI></UL>
</UL>
<LI><A NAME="TOC51" HREF="gtkada_ug.html#SEC51">Dynamic loading of XML files</A>
</LI><UL>
<LI><A NAME="TOC52" HREF="gtkada_ug.html#SEC52">Introduction to Libglade</A>
</LI></UL>
<LI><A NAME="TOC53" HREF="gtkada_ug.html#SEC53">Limitations</A>
</LI></UL>
<LI><A NAME="TOC54" HREF="gtkada_ug.html#SEC54">Binding new widgets</A>
</LI><LI><A NAME="TOC55" HREF="gtkada_ug.html#SEC55">Debugging GtkAda applications</A>
</LI><LI><A NAME="TOC56" HREF="gtkada_ug.html#SEC56">How to report bugs</A>
</LI><LI><A NAME="TOC57" HREF="gtkada_ug.html#SEC57">Bibliography</A>
</LI><LI><A NAME="TOC58" HREF="gtkada_ug.html#SEC58">GNU Free Documentation License</A>
</LI></UL>
<P><HR><P>

<P>
@dircategory User Interface Toolkit
* GtkAda_Ug: (gtkada_ug).           Ada95 graphical tookit based on GTK+ (User's Guide)


<P>
Copyright (C) 1998-2000, Emmanuel Briot, Joel Brobecker, Arnaud Charlet


<P>
Copyright (C) 2000-2009, AdaCore


<P>
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1
or any later version published by the Free Software Foundation;
with the Invariant Sections being "GNU Free Documentation License", with the
Front-Cover Texts being
"GtkAda User's Guide",
and with no Back-Cover Texts.
A copy of the license is included in the section entitled
"GNU Free Documentation License".




<H1><A NAME="SEC1">Introduction: What is GtkAda ?</A></H1>

<P>
GtkAda is a high-level portable graphical toolkit, based on the gtk+
toolkit, one of the official GNU toolkits. It makes it easy to create
portable user interfaces for multiple platforms, including most
platforms that have a X11 server and Win32 platforms.


<P>
Although it is based on a C library, GtkAda uses some advanced Ada95
features like tagged types, generic packages, access to subprograms,
exceptions, etc... to make it easier to use and design interfaces.
For efficiency reasons, it does not use controlled types, but takes care
of all the memory management for you in other ways.


<P>
As a result, this library provides a <I>secure</I>, <I>easy to use</I> and
<I>extensible</I> toolkit.


<P>
Compared to the C library, GtkAda provides type safety (especially in the
callbacks area), and object-oriented programming. As opposed to common
knowledge, it requires <I>less</I> type casting than with in C.
Its efficiency is about the same as the C library through the use of inline
subprograms.


<P>
GtkAda comes with a complete integration to the graphical interface
builder <CODE>Glade</CODE><A NAME="DOCF1" HREF="gtkada_ug.html#FOOT1">(1)</A>. This
makes it even easier to develop interfaces, since you just have to click
to create a window and all the dialogs. Ada code can then be generated
with a single click.


<P>
Under some platforms, GtkAda also provides a bridge to use OpenGL, with
which you can create graphical applications that display 3D graphics, and
display them in a GtkAda window, as with any other 2D graphics.
This manual does not
document OpenGL at all, see any book on OpenGL, or the specification
that came with your OpenGL library, for more information.


<P>
The following Internet sites will always contain the latest public
packages for <CODE>GtkAda</CODE>, <CODE>gtk+</CODE> and <CODE>Glade</CODE>.


<P>
     <A HREF="http://libre.act-europe.fr/GtkAda/">http://libre.act-europe.fr/GtkAda/</A>


<P>
     <A HREF="http://www.gtk.org/">http://www.gtk.org/</A>


<P>
     <A HREF="http://glade.gnome.org/">http://glade.gnome.org/</A>
     


<P>
The scheme used for GtkAda's version numbers is the following: the major
and minor version number is the same as for the underlying gtk+ library
(e.g 2.14).
The micro version number depends on GtkAda's release number.


<P>
This toolkit was tested on the following systems:

<UL>
<LI>GNU Linux/x86

<LI>GNU Linux/x86-64

<LI>GNU Linux/ia64

<LI>Solaris/sparc

<LI>Windows XP/Vista/2003

</UL>

<P>
with the latest version of the <CODE>GNAT</CODE> compiler, developed and supported by
Ada Core Technologies (see <A HREF="http://www.adacore.com">http://www.adacore.com</A>).


<P>
This version of GtkAda is known to be compatible with <CODE>gtk+</CODE> <B>2.14.x</B>.
This release may or may not be compatible with older versions of gtk+.


<P>
This version of GtkAda is compatible with <CODE>Glade</CODE> <B>version
2.0.0</B>.  Due to some modification in the output format of Glade, this
release will not work with older versions. It is also not guaranteed to
work with more recent versions.


<P>
This document does not describe all the widgets available in GtkAda, nor
does it try to explain all the subprograms. The GtkAda Reference Manual
provides this documentation instead, as well as
the GtkAda sources spec files themselves, whose extension is <TT>`.ads'</TT>.


<P>
No complete example is provided in this documentation. Instead, please
refer to the examples that you can find in the <TT>`testgtk/'</TT> and
<TT>`examples/'</TT> directory in the GtkAda distribution, since these are
more up-to-date (and more extensive).
They are heavily commented, and are likely to contain a lot of
information that you might find interesting.


<P>
If you are interested in getting support for GtkAda (including priority
bug fixes, early releases, help in using the toolkit, help in designing
your interface, on site consulting...), please contact AdaCore
(<A HREF="mailto:sales@adacore.com">mailto:sales@adacore.com</A>).




<H1><A NAME="SEC2">Getting started with GtkAda</A></H1>

<P>
This chapter describes how to start a new GtkAda application. It explains
the basic features of the toolkit, and shows how to compile and run your
application.


<P>
It also gives a brief overview of the extensive widget hierarchy available
in GtkAda.



<UL>
<LI><A HREF="gtkada_ug.html#SEC3">How to build and install GtkAda</A>
<LI><A HREF="gtkada_ug.html#SEC4">How to distribute a GtkAda application</A>
<LI><A HREF="gtkada_ug.html#SEC5">Organization of the GtkAda package</A>
<LI><A HREF="gtkada_ug.html#SEC6">How to compile an application with GtkAda</A>
<LI><A HREF="gtkada_ug.html#SEC11">Architecture of the toolkit</A>
<LI><A HREF="gtkada_ug.html#SEC12">Widgets Hierarchy</A>
</UL>



<H2><A NAME="SEC3">How to build and install GtkAda</A></H2>

<P>
This section explains how to build and install GtkAda on your machine.
It is Unix-oriented, since GtkAda is distributed in binary format on
Windows machines, and comes with all the dependent packages, including
the gtk+ libraries and <CODE>Glade</CODE>. If you are a Windows-user, you should
skip this section.


<P>
On Unix systems, you first need to install the glib and gtk+
libraries. Download the compatible packages from the gtk+ web site
(<A HREF="http://www.gtk.org">http://www.gtk.org</A>, compile and install it.


<P>
Change your PATH environment variable so that the script <CODE>pkg-config</CODE>,
which indicates where gtk+ was installed and what libraries it needs is
automatically found by GtkAda. You will no longer need this script once
GtkAda is installed, unless you develop part of your application in C.


<P>
OpenGL support will not be activated in GtkAda unless you already have
the OpenGL libraries on your systems. You can for instance look at
Mesa, which is free implementation.


<P>
Optionally, you can also install the <CODE>Glade</CODE> interface builder. Get
the compatible package from the Glade web site, compile and install it.
The official version already knows about Ada (at least enough to call
GtkAda's own programs), so no patch is needed.


<P>
You can finally download the latest version of GtkAda from the web site.
Untar and uncompress the package, then simply do the following
steps:

<PRE>
$ ./configure
$ make
$ make tests     (this step is optional)
$ make install
</PRE>

<P>
As usual with the <CODE>configure</CODE> script, you can specify where you want
to install the GtkAda libraries by using the <CODE>--prefix</CODE> switch.


<P>
You can specify the switch <CODE>--disable-shared</CODE> to prevent building
shared libraries, even if your system supports them (by default, both
shared and static libraries are installed). By default, your application
will be linked statically with the GtkAda libraries. You can override this
default by specifying <CODE>--enable-shared</CODE> as a switch to <CODE>configure</CODE>,
although you can override it later through the LIBRARY_TYPE scenario
variable.


<P>
If you have some OpenGL libraries installed on your system, you can make
sure that <CODE>configure</CODE> finds them by specifying the
<CODE>--with-GL-prefix</CODE> switch on the command line. <CODE>configure</CODE>
should be able to automatically detect the libraries however.


<P>
You must then make sure that the system will be able to find the dynamic
libraries at run time if your application uses them. Typically, you
would do one of the following:

<UL>
<LI>run <CODE>ldconfig</CODE> if you installed GtkAda in one of the standard

  location and you are super-user on your machine
<LI>edit <CODE>/etc/ld.conf</CODE> if you are super-user but did not install

  GtkAda in one of the standard location. Add the path that contains
  libgtkada.so (by default <TT>`/usr/local/lib'</TT> or <TT>`$prefix/lib'</TT>.
<LI>modify your <CODE>LD_LIBRARY_PATH</CODE> environment variable if you are

  not super-user. You should simply add the path to libgtkada.
</UL>

<P>
In addition, if you are using precompiled Gtk+ binary packages, you will
also need to set the <CODE>FONTCONFIG_FILE</CODE> environment variable to point to
the <TT>`prefix/etc/fonts/fonts.conf'</TT> file of your binary installation.


<P>
For example, assuming you have installed Gtk+ under <TT>`/opt/gtk'</TT> and
using bash:



<PRE>
$ export FONTCONFIG_FILE=/opt/gtk/etc/fonts/fonts.conf
</PRE>



<H2><A NAME="SEC4">How to distribute a GtkAda application</A></H2>

<P>
Since GtkAda depends on Gtk+, you usually need to distribute some Gtk+
libraries along with your application.


<P>
Under some OSes such as Linux, Gtk+ comes preinstalled, so in this case, 
a simple solution is to rely on the preinstalled Gtk+ libraries. See
below for more information on the gtkada library itself.


<P>
Under other unix systems, GtkAda usually comes with a precompiled set of Gtk+
libraries that have been specifically designed to be easily redistributed.


<P>
In order to use the precompiled Gtk+ binaries that we distribute
with GtkAda, you need to distribute all the Gtk+ .so libraries along
with your application, and use the LD_LIBRARY_PATH environment variable to
point to these libraries.


<P>
The list of libraries needed is <TT>`&#60;gtkada-prefix&#62;/lib/lib*.so.?'</TT>
along with your executable, and set LD_LIBRARY_PATH.


<P>
You may also need the <TT>`libgtkada-xxx.so'</TT> file. This dependency
is optional since gtkada supports both static and dynamic linking, so
by e.g. using <CODE>gtkada-config --static</CODE> or by using
<TT>`gtkada_static.gpr'</TT>, you will end up linking with <TT>`libgtkada.a'</TT>.


<P>
Under Windows, you need to distribute the following files and directories
along with your application, and respect the original directory set up:



<UL>
<LI><TT>`bin/*.dll'</TT>

<LI><TT>`etc/'</TT>

<LI><TT>`lib/gtk-2.0'</TT>

</UL>



<H2><A NAME="SEC5">Organization of the GtkAda package</A></H2>

<P>
In addition to the full sources, the GtkAda package contains a lot of
heavily commented examples. If you haven't been through those examples, we
really recommend that you look at them and try to understand them, since
they contain some examples of code that you might find interesting for
your own application.



<UL>

<LI><TT>`testgtk/'</TT> directory:

This directory contains an application that tests all the widgets in GtkAda.
It gives you a quick overview of what can be found in the toolkit, as well
as some detailed information on the widgets and their parameters.

Each demo is associated with contextual help pointing to aspects worth
studying.

It also contains an OpenGL demo, if GtkAda was compiled with support for
OpenGL.

This program is far more extensive that its C counterpart, and the GtkAda
team has added a lot of new examples.

<LI><TT>`examples/'</TT> directory:

This directory contains some small examples, unrelated to testgtk.  For
instance, this is where you will find some sample XML files for
<CODE>Gate</CODE> and <CODE>Dgate</CODE>, as well as some new widgets created
directly in Ada, as examples of how to create your own callback
marshallers.

On the whole these examples are a little more complex than testgtk but,
since they focus on demonstrating a precise concept, they are still quite
easy to understand.

<LI><TT>`docs/'</TT> directory:

It contains the html, info, text and TeX versions of the documentation you
are currently reading. Note that the documentation is divided into two
subdirectories, one containing the user guide, which you are currently
reading, the other containing the reference manual, which gives detailed
information on all the widgets found in GtkAda. The docs directory also
contains a subdirectory with some slides that were used to present GtkAda
at various shows.

</UL>



<H2><A NAME="SEC6">How to compile an application with GtkAda</A></H2>

<P>
This section explains how you can compile your own applications.


<P>
There are several ways to use GtkAda in your applications




<H3><A NAME="SEC7">Using project files</A></H3>

<P>
A set of project files is installed along with GtkAda. If you have installed
GtkAda in the same location as GNAT itself, nothing else needs to be done.


<P>
Otherwise, you need to make the directory that contains these project files
visible to the compiler. This is done by adding the directory to the
<CODE>ADA_PROJECT_PATH</CODE> environment variable. Assuming you have installed the
library in <CODE>prefix</CODE>, the directory you need to add is
<CODE>prefix/lib/gnat</CODE>.


<P>
On Unix, this is done with



<PRE>
csh:
   setenv ADA_PROJECT_PATH $prefix/lib/gnat:$ADA_PROJECT_PATH
sh:
   ADA_PROJECT_PATH=$prefix/lib/gnat:$ADA_PROJECT_PATH
   export ADA_PROJECT_PATH
</PRE>

<P>
To build your own application, you should then setup a project file (see
the GNAT documentation for more details on project files), which simply
contains the statement



<PRE>
with "gtkada";
</PRE>

<P>
This will automatically set the right compiler and linker options, so that
your application is linked with GtkAda.


<P>
By default, the linker will use GtkAda's shared library, if it was built.
If you would prefer to link with the static library, you can set the
environment variable
    LIBRARY_TYPE=static
    export LIBRARY_TYPE
before launching the compiler or linker, which will force it to use the
static library instead.




<H3><A NAME="SEC8">Using the command line</A></H3>

<P>
The procedure is system-dependent, and thus is divided into two
subsections.




<H4><A NAME="SEC9">Unix systems</A></H4>

<P>
On Unix systems, a script called <CODE>gtkada-config</CODE> is automatically
created when you build GtkAda. This script is copied in a subdirectory
<TT>`bin/'</TT> in the installation directory.


<P>
The easiest and recommended way to build a GtkAda application is to
use the <CODE>gnatmake</CODE> program distributed with GNAT, that takes care of
all the dependencies for you. Use the <CODE>gtkada-config</CODE> to specify
where GtkAda and gtk+ libraries have been installed.



<PRE>
&#62; gnatmake &#60;main-file&#62; `gtkada-config`
</PRE>

<P>
Note the use of back-ticks around gtkada-config, which force the shell to
evaluate the script and put the output on the command line.


<P>
However, on complex systems, gnatmake might not be enough. Users frequently
like to create <CODE>Makefile</CODE>s. The script <CODE>gtkada-config</CODE> remains
useful in that case, since you can call it from your Makefile (same
syntax as above with the back-ticks) to create variables like FLAGS and
LIBS. See the switches of <CODE>gtkada-config</CODE> below for more information.


<P>
The script <CODE>gtkada-config</CODE> understands the following command line
switches (chosen to be compatible with the ones set by <CODE>gtk-config</CODE>):



<UL>
<LI><CODE>--cflags</CODE>: Output only the compiler flags, i.e the  include

  directories where the GtkAda spec files are found. This should be used
  if you only want to compile your files, but do not want to bind or link
  them.
<LI><CODE>--libs</CODE>: Output only the switches for the linker. This lists

  the directories where all the GtkAda, gtk+, and dependant libraries are
  found. For instance, if GtkAda was compiled with support for OpenGL,
  the OpenGL libraries will automatically be present.
<LI><CODE>--static</CODE>: Forces linking with the static gtkada library. This

  option will still use the dynamic gtk+ libraries.
</UL>



<H4><A NAME="SEC10">Windows systems</A></H4>

<P>
Things are somewhat easier on Windows systems. You don't have access to the
<CODE>gtkada-config</CODE> script. On the other hand you also don't
have to specify which libraries to use or where to find them.


<P>
The only thing you should specify on the <CODE>gnatmake</CODE> command line is
where the GtkAda spec files are found, as in:



<PRE>
&#62; gnatmake &#60;main-file&#62; -Ic:\gtkada\include\gtkada
</PRE>

<P>
if GtkAda was installed under <TT>`c:\gtkada'</TT>.




<H2><A NAME="SEC11">Architecture of the toolkit</A></H2>

<P>
The gtk+ toolkit has been designed from the beginning to be portable.
It is made of three libraries, <CODE>gtk</CODE>, <CODE>gdk</CODE> and <CODE>glib</CODE>.


<P>
<CODE>Glib</CODE> is a non-graphical library, that includes support for lists,
h-tables, threads, etc... It is a highly optimized,
platform-independent library. Since most of its contents are already
available in Ada (or in the <TT>`GNAT.*'</TT> hierarchy in the GNAT
distribution), GtkAda does not include a binding to it, except for a few
required packages. These are the <TT>`Glib.*'</TT> packages in the GtkAda
distribution.


<P>
<CODE>Gdk</CODE> is the platform-dependent part of gtk+. Its implementation is
completely different on win32 systems and X11 systems, although the
interface is of course the same. It provides a set of functions to draw
lines, rectangles and pixmaps on the screen, manipulate
colors, etc... It has a complete equivalent in GtkAda, through the
<TT>`Gdk.*'</TT> packages.


<P>
<CODE>Gtk</CODE> is the top level library. It is platform independent, and
does all its drawing through calls to Gdk. This is where the high-level
widgets are defined. It also includes support for callbacks. Its
equivalent in the GtkAda libraries are the <TT>`Gtk.*'</TT> packages. It is
made of a fully object-oriented hierarchy of widgets (see section <A HREF="gtkada_ug.html#SEC12">Widgets Hierarchy</A>).


<P>
Since your application only calls GtkAda, it is fully portable, and can
be recompiled as-is on other platforms.



<PRE>
+---------------------------------------------+
|             Your Application                |
+---------------------------------------------+
|                 GtkAda                      |
|               +-----------------------------+
|               |            GTK              |
|       +-------+-----------------------------+
|       |           GDK                       |
+-------+--------------+--+-------------------+
|          GLIB        |  | X-Window / Win32  |
+----------------------+  +-------------------+
</PRE>

<P>
Although the packages have been evolving a lot since the first versions
of GtkAda, the specs are stabilizing now. We will try as much as possible
to provide backward compatibility whenever possible.


<P>
Since GtkAda is based on gtk+ we have tried to stay as close to it as possible
while using high-level features of the Ada95 language. It is
thus relatively easy to convert external examples from C to Ada.


<P>
We have tried to adopt a consistent naming scheme for Ada identifiers:

<UL>

<LI>The widget names are the same as in C, except that an underscore

sign (_) is used to separate words, e.g

<PRE>
Gtk_Button   Gtk_Color_Selection_Dialog
</PRE>

<LI>Because of a clash between Ada keywords and widget names, there

are two exceptions to the above general rule:

<PRE>
Gtk.GEntry.Gtk_Entry   Gtk.GRange.Gtk_Range
</PRE>

<LI>The function names are the same as in  C, ignoring the leading

<CODE>gtk_</CODE> and the widget name, e.g

<PRE>
gtk_misc_set_padding        =>  Gtk.Misc.Set_Padding
gtk_toggle_button_set_state =>  Gtk.Toggle_Button.Set_State
</PRE>

<LI>Most enum types have been grouped in the <TT>`gtk-enums.ads'</TT> file

<LI>Some features have been implemented as generic packages. These

are the timeout functions (see Gtk.Main.Timeout), the idle functions
(see Gtk.Main.Idle), and the data that can be attached to any object
(see Gtk.Object.User_Data). Type safety is ensured through these
generic packages.

<LI>Callbacks were the most difficult thing to interface with. These

are extremely powerful and versatile, since the callbacks can have any
number of arguments, can return values, or not,etc... These are once
again implemented as generic packages, that require more explanation
(see section <A HREF="gtkada_ug.html#SEC14">Signal handling</A>).

</UL>

<P>
<B>WARNING:</B> all the generic packages allocate some memory for internal
structures, and call internal functions. This memory is freed by gtk
itself, by calling some Ada functions. Therefore the generic packages
have to be instanciated at library level, not inside a subprogram, so
that the functions are still defined when gtk needs to free the memory.


<P>
<B>WARNING</B> Before any other call to the GtkAda library is performed,
<CODE>Gtk.Main.Init</CODE> must be invoked first. Most of the time, this
procedure is invoked from the main procedure of the application, in
which case no use of GtkAda can be done during the application
elaboration.




<H2><A NAME="SEC12">Widgets Hierarchy</A></H2>

<P>
All widgets in <CODE>GtkAda</CODE> are implemented as tagged types. They all have
a common ancestor, called <CODE>Gtk.Object.Gtk_Object</CODE>. All visual objects
have a common ancestor called <CODE>Gtk.Widget.Gtk_Widget</CODE>.


<P>
The following table describes the list of objects and their inheritance
tree. As usual with tagged types, all the primitive subprograms defined
for a type are also known for all of its children. This is a very
powerful way to create new widgets, as will be explained in
section <A HREF="gtkada_ug.html#SEC32">Creating new widgets in Ada</A>.


<P>
Although gtk+ was written in C its design is object-oriented, and thus
GtkAda has the same structure. The following rules have been applied to
convert from C names to Ada names: a widget <CODE>Gtk_XXX</CODE> is defined in
the Ada package <CODE>Gtk.XXX</CODE>, in the file <TT>`gtk-xxx.ads'</TT>. This
follows the GNAT convention for file names.  For instance, the
<CODE>Gtk_Text</CODE> widget is defined in the package <CODE>Gtk.Text</CODE>, in the
file <TT>`gtk-text.ads'</TT>.


<P>
Note also that most of the documentation for GtkAda is found in the spec
files themselves.


<P>
It is important to be familiar with this hierarchy. It is then easier to
know how to build and organize your windows. Most widgets are demonstrated
in the <TT>`testgtk/'</TT> directory in the GtkAda distribution.


<P>
<IMG SRC="hierarchy.jpg" ALT="hierarchy">
<PRE>
            <STRONG>Hierarchy of widgets in GtkAda</STRONG>
</PRE>



<H1><A NAME="SEC13">Hierarchical composition of a window</A></H1>

<P>
Interfaces in GtkAda are built in layers, as in Motif.
For instance, a typical dialog is basically a Gtk_Window, that in turn
contains a Gtk_Box, itself divided into two boxes and a Gtk_Separator,
and so on.


<P>
<IMG SRC="boxes.gif" ALT="boxes">

<P>
Altough this may seem more complicated than setting absolute positions
for children, this is the simplest way to automatically handle the
resizing of windows. Each container that creates a layer knows how it
should behave when it is resized, and how it should move its children.
Thus almost everything is handled automatically, and you don't have to do
anything to support resizing.


<P>
If you really insist on moving the children to a specific position, look
at the <CODE>Gtk_Fixed</CODE> widget and its demo in <TT>`testgtk/'</TT>. But you
really should not use this container, since you will then have to do
everything by hand.


<P>
All the containers are demonstrated in <TT>`testgtk/'</TT>, in the GtkAda
distribution. This should help you understand all the parameters
associated with the containers. It is very important to master these
containers, since using the appropriate containers will make building
interfaces a lot easier.


<P>
If you look at the widget hierarchy (see section <A HREF="gtkada_ug.html#SEC12">Widgets Hierarchy</A>),
you can see that a Gtk_Window inherits from Gtk_Bin, and thus can have
only one child. In most cases, the child of a Gtk_Window will thus
be a Gtk_Box, which can have any number of children.


<P>
Some widgets in GtkAda itself are built using this strategy, from the
very basic <CODE>Gtk_Button</CODE> to the more advanced
<CODE>Gtk_File_Selection</CODE>.


<P>
For example, by default a Gtk_Button contains a Gtk_Label, which
displays the text of the button (like "OK" or "Cancel").


<P>
However, it is easy to put a pixmap in a button instead. When you
create the button, do not specify any label. Thus, no child will be
added, and you can give it your own. See <TT>`testgtk/create_pixmap.adb'</TT>
for an example on how to do that.




<H1><A NAME="SEC14">Signal handling</A></H1>

<P>
In GtkAda, the interaction between the interface and the core
application is done via signals. Most user actions on the graphical
application trigger some signals to be <SAMP>`emitted'</SAMP>. 


<P>
A signal is a message that an object wants to broadcast. It is
identified by its name, and each one is associated with certain events
which happen during the widget's lifetime. For instance, when the user
clicks on a Gtk_Button, a "clicked" signal is emitted by that
button. More examples of signals can be found in the GtkAda reference
manual.


<P>
It is possible to cause the application to react to such events by
<SAMP>`connecting'</SAMP> to a signal a special procedure called a
<SAMP>`handler'</SAMP> or <SAMP>`callback'</SAMP>.  This handler will be called every
time that signal is emitted, giving the application a chance to do any
processing it needs. More than one handler can be connected to the
same signal on the same object; the handlers are invoked in the order
they were connected.



<UL>
<LI><A HREF="gtkada_ug.html#SEC15">Predefined signals</A>
<LI><A HREF="gtkada_ug.html#SEC16">Connecting signals</A>
<LI><A HREF="gtkada_ug.html#SEC17">Handling user data</A>
</UL>



<H2><A NAME="SEC15">Predefined signals</A></H2>

<P>
Widgets, depending on their type, may define zero or more different signals.
The signals defined for the parent widget are also automatically inherited;
thus every widget answers many signals.


<P>
The easiest way to find out which signals can be emitted by a widget is to
look at the GtkAda reference manual. Every widget will
be documented there. The GtkAda RM explains when particular signals are
emitted, and the general form that their handlers should have (although
you can always add a <CODE>User_Data</CODE> if you wish, see below).


<P>
You can also look directly at the C header files
distributed with the gtk+ library. Each widget is described in its own C
file and has two C structures associated with it. One of them is the
"class" structure, which contains a series of pointers to
functions. Each of these functions has the same name as the signal name.


<P>
For instance, consider the following extract from gtkbutton.h:

<PRE>
<B>struct</B> _GtkButtonClass
{
  GtkBinClass        parent_class;
  
  <B>void</B> (* pressed)  (GtkButton *button);
  <B>void</B> (* released) (GtkButton *button);
  <B>void</B> (* clicked)  (GtkButton *button);
  <B>void</B> (* enter)    (GtkButton *button);
  <B>void</B> (* leave)    (GtkButton *button);
};
</PRE>

<P>
This means that the Gtk_Button widget redefines five new signals,
respectively called <CODE>"pressed"</CODE>, <CODE>"released"</CODE>, ...


<P>
The profile of the handler can also be deduced from those pointers:
The handler has the same arguments, plus an optional <CODE>User_Data</CODE> parameter
that can be used to pass any kind of data to the handler.  When the
<CODE>User_Data</CODE> parameter is used, the value of this data is specified when
connecting the handler to the signal. It is then given back to the
handler when the signal is raised.


<P>
Therefore, the profile of a handler should look like:

<PRE>
procedure Pressed_Handler
  (Button    : access Gtk_Button_Record'Class;
   User_Data : ...);
</PRE>

<P>
The callback does not need to use all the arguments. It is legal to use
a procedure that "drops" some of the last arguments.
There is one special case, however: if, at connection time, you decided to
use <CODE>User_Data</CODE>, your callback must handle it.  This is checked by
the compiler.


<P>
Any number of arguments can be dropped as long as those arguments are
the last ones in the list and you keep the first one. For instance,
the signal "button_press_event" normally can be connected to a
handler with any of the following profiles:



<PRE>
--  with a user_data argument
procedure Handler
  (Widget    : access Gtk_Widget_Record'Class;
   Event     : Gdk.Event.Gdk_Event;
   User_Data : ...);
procedure Handler
  (Widget    : access Gtk_Widget_Record'Class;
   User_Data : ...);

--  without a user_data argument
procedure Handler
  (Widget : access Gtk_Widget_Record'Class;
   Event  : Gdk.Event.Gdk_Event);
procedure Handler (Widget : access Gtk_Widget_Record'Class);
</PRE>

<P>
Beware that adding new arguments is not possible, since no value would
be provided for them. When connecting a handler, GtkAda will not always
verify that your handler does not have more arguments than expected, so
caution is recommended (it only does so if you use the <CODE>Gtk.Marshallers</CODE>
package, see below).




<H2><A NAME="SEC16">Connecting signals</A></H2>

<P>
All the signal handling work is performed by using the services provided
by the <CODE>Gtk.Handlers</CODE> package. This package is self-documented,
so please read the documentation for this package either in the GtkAda
Reference Manual or in the specs themselves. The rest of this section assumes
that you have this documentation handy.


<P>
A short, annotated example of connecting signals follows; a complete
example can be found in create_file_selection.adb (inside the testgtk/
directory). In our example, an application opens a file selector to
allow the user to select a file.  GtkAda provides a high-level widget
called Gtk_File_Selection which can be used in this case:



<PRE>
declare
   Window : Gtk_File_Selection;
begin
   Gtk.File_Selection.Gtk_New (Window, Title =&#62; "Select a file");
end;
</PRE>

<P>
When the "OK" button is pressed, the application needs to retrieve the
selected file and then close the dialog. The only information that the
handler for the button press needs is which widget to operate upon.
This can be achieved by the following handler:



<PRE>
procedure OK (Files : access Gtk_File_Selection_Record'Class) is
begin
   Ada.Text_IO.Put_Line ("Selected " &#38; Get_Filename (Files));
   --  Prints the name of the selected file.
   Destroy (Files);
   --  Destroys the file selector dialog
end Ok;
</PRE>

<P>
We now need to connect the object we created in the first part with the
new callback we just defined. <CODE>Gtk.Handlers</CODE> defines four types of
generic packages, depending on the arguments one expects in the callback and
whether the callback returns a value or not. Note that you can not
use an arbitrary list of arguments; this depends on the signal, as
explained in the previous section.


<P>
In our example, since the callback does not return any value and does not
handle any <CODE>User_Data</CODE> (that is, we don't pass it extra data,
which will be specified at connection time), the appropriate package
to use is <CODE>Gtk.Handlers.Callback</CODE>. We thus instantiate that package.


<P>
Remember that generic package instantiations in GtkAda must be present
in memory at all times, since they take care of freeing allocated memory
when finished. GtkAda generic package instantiations must therefore
always be performed at the library level, and not inside any inner block.



<PRE>
package Files_Cb is new 
  Handlers.Callback (Gtk_File_Selection_Record);
</PRE>

<P>
The <CODE>Files_Cb</CODE> package now provides a set of Connect subprograms
that can be used to establish a tie between a widget and a handler.
It also provides a set of other subprograms which you can use to emit
the signals manually, although most of the time, the signals are simply
emitted internally by GtkAda. We will not discuss the Emit_By_Name
subprograms here.


<P>
The general form of handler, as used in <CODE>Gtk.Handlers</CODE>, expects some
handlers that take two or three arguments: the widget on which the
signal was applied, an array of all the extra arguments sent internally
by GtkAda, and possibly some user data given when the connection was
made.


<P>
This is the most general form of handler and it covers all the possible
cases. However, it also expects the user to manually extract the needed
values from the array of arguments. This is not always the most convenient
solution. This is why GtkAda provides a second package related to signals,
<CODE>Gtk.Marshallers</CODE>.


<P>
The <CODE>Gtk.Marshallers</CODE> package provides a set of functions that can
be used as callbacks directly for GtkAda, and that will call your
application's handlers after extracting the required values from the array of
arguments. Although this might sound somewhat complicated, in practice
it simplifies the task of connecting signals. In fact, the techniques
employed are similar to what is done internally by gtk+ in C. Because of
the similarity of techniques, there is no overhead involved in using
<CODE>Gtk.Marshallers</CODE> with Ada over the C code in gtk+.


<P>
A set of functions <CODE>To_Marshaller</CODE> is found in every generic package
in <CODE>Gtk.Handlers</CODE>. They each take a single argument, the name of the
function you want to call, and return a handler that can be used directly in
<CODE>Connect</CODE>.


<P>
The connection is then done with the following piece of code. Note that this
can be done just after creating the widget, in the same block. As soon as it
is created, a widget is ready to accept connections (although no signals will
be emitted before the widget is shown on the screen).


<P>
Note that we use <CODE>To_Marshaller</CODE> since our handler does not accept
the array of arguments as a parameter, and we use the special
<CODE>Object_Connect</CODE> procedure. This means that the parameter to
our callback (Files) will be the Slot_Object given in Object_Connect, instead
of being the button itself.



<PRE>
Files_Cb.Object_Connect
  (Get_Ok_Button (Window),  --  The object to connect to the handler
   "clicked",               --  The name of the signal
   Files_Cb.To_Marshaller (Ok'Access),  --  The signal handler
   Slot_Object =&#62; Window);
</PRE>



<H2><A NAME="SEC17">Handling user data</A></H2>

<P>
As described above, it is possible to define some data that is that
passed to the callback when it is called. This data is called
user_data, and is passed to the <CODE>Connect</CODE> or
<CODE>Object_Connect</CODE> subprograms.


<P>
GtkAda will automatically free any memory it has allocated internally
to store these user data. For instance, if you instanciated the
generic package <CODE>User_Callback</CODE> with a String, it means that you
want to be able to have a callback of the form:



<PRE>
   procedure My_Callback (Widget : access Gtk_Widget_Record'Class;
                           User_Data : String);
</PRE>

<P>
and connect it with a call similar to:



<PRE>
   Connect (Button, "Clicked", To_Marshaller (My_Callback'Access),
            User_Data =&#62; "any string");
</PRE>

<P>
GtkAda needs to allocate some memory to store the string (an
unconstrained type). However, this memory is automatically freed when
the callback is destroyed.


<P>
There are a few subtleties in the use of user_data, most importantly
when the user data is itself a widget.


<P>
The following four examples do exactly the same thing, ie create two
buttons, and clicking on the first one will destroy the second
one. They all work fine the first time, while the two buttons
exist. However, some of them will fail if you press on the first
button a second time.


<P>
The code for this example can be found in the distribution, in the
<TT>`examples/user_data'</TT> directory. The examples below do not include
the creation of the main window, or of the buttons themselves, to
emphasize the important part.




<H3><A NAME="SEC18">First case: simple user data</A></H3>

<P>
This code will fail: when <CODE>Button2</CODE> has been destroyed, the Ada
type still points to some random memory, and the second call to
<CODE>Destroy</CODE> will fail with a Storage_Error.



<PRE>
   package User_Callback is new Gtk.Handlers.User_Callback
     (Gtk_Widget_Record, Gtk_Widget);

   procedure My_Destroy2
     (Button : access Gtk_Widget_Record'Class; Data : Gtk_Widget) is
   begin
      Destroy (Data);
   end My_Destroy2;

   begin
      User_Callback.Connect
        (Button1, "clicked",
         User_Callback.To_Marshaller (My_Destroy2'Access),
         Gtk_Widget (Button2));
   end;
</PRE>



<H3><A NAME="SEC19">Second case: using Object_Connect instead</A></H3>

<P>
One of the solutions to fix the above problem is to use
<CODE>Object_Connect</CODE> instead of <CODE>Connect</CODE>. In that case, GtkAda
automatically takes care of disconnecting the callback when either of
the two widgets is destroyed.



<PRE>
   procedure My_Destroy (Button : access Gtk_Widget_Record'Class) is
   begin
      Destroy (Button);
   end My_Destroy;

   begin
      Widget_Callback.Object_Connect
        (Button1, "clicked",
         Widget_Callback.To_Marshaller (My_Destroy'Access),
         Button2);
   end;
</PRE>



<H3><A NAME="SEC20">Third case: manually disconnecting the callback</A></H3>

<P>
Using <CODE>Object_Connect</CODE> is not always possible. In that case, one
of the possibilities is to store the <CODE>Id</CODE> of the callback, and
properly disconnect it when appropriate. This is the most complex
method, and very often is not applicable, since you cannot know for
sure when the callback is no longer needed.



<PRE>
   type My_Data3 is record
      Button, Object : Gtk_Widget;
      Id             : Handler_Id;
   end record;
   type My_Data3_Access is access My_Data3;

   package User_Callback3 is new Gtk.Handlers.User_Callback
     (Gtk_Widget_Record, My_Data3_Access);

   procedure My_Destroy3
     (Button : access Gtk_Widget_Record'Class;
      Data   : My_Data3_Access) is
   begin
      Destroy (Data.Button);
      Disconnect (Data.Object, Data.Id);
   end My_Destroy3;

      Id : Handler_Id;
   begin
      Data3 := new My_Data3' (Object =&#62; Gtk_Widget (Button1),
                              Button =&#62; Gtk_Widget (Button2),
                              Id     =&#62; (Null_Signal_Id, null));
      Id := User_Callback3.Connect
        (Button1, "clicked",
         User_Callback3.To_Marshaller (My_Destroy3'Access),
         Data3);
      Data3.Id := Id;
   end;
</PRE>



<H3><A NAME="SEC21">Fourth case: setting a watch on a specific widget</A></H3>

<P>
GtkAda provides a function <CODE>Add_Watch</CODE>, that will automatically
disconnect a callback when a given widget is destroyed. This is the
function used internally by <CODE>Object_Connect</CODE>. In the example
below, the callback is automatically disconnected whenever
<CODE>Button2</CODE> is destroyed.



<PRE>
   procedure My_Destroy2
     (Button : access Gtk_Widget_Record'Class; Data : Gtk_Widget) is
   begin
      Destroy (Data);
   end My_Destroy2;

      Id : Handler_Id;
   begin
      Id := User_Callback.Connect
        (Button1, "clicked",
         User_Callback.To_Marshaller (My_Destroy2'Access),
         Gtk_Widget (Button2));
      Add_Watch (Id, Button2);
   end;
</PRE>



<H1><A NAME="SEC22">Starting an application with GtkAda</A></H1>

<P>
You need to perform some initializations to start a GtkAda application:

<PRE>
--  predefined units of the library
<B>with</B> Gtk.Rc;
<B>with</B> Gtk.Main;
<B>with</B> Gtk.Enums;
<B>with</B> Gtk.Window;
...
--  My units
<B>with</B> Callbacks;
...
<B>procedure</B> Application is
   <B>procedure</B> Create_Window <B>is</B> ...

<B>begin</B>
   --  Set the locale specific datas (e.g time and date format)
   Gtk.Main.Set_Locale;

   --  Initializes GtkAda
   Gtk.Main.Init;

   --  Load the resources. Note that this part is optional.
   Gtk.Rc.Parse ("application.rc");

   --  Create the main window
   Create_Window;

   --  Signal handling loop
   Gtk.Main.Main;
<B>end</B> Application;
</PRE>

<P>
the <CODE>Create_Window</CODE> procedure looks like



<PRE>
   <B>procedure</B> Create_Window <B>is</B>
      Main_Window : Gtk.Window.Gtk_Window;
      ...
   <B>begin</B>
      Gtk.Window.Gtk_New
        (Window   =&#62; Main_Window,
         The_Type =&#62; Gtk.Enums.Window_Toplevel);

      --  From Gtk.Widget:
      Gtk.Window.Set_Title (Window =&#62; Main_Window, Title  =&#62; "Editor");

      --  Construct the window and connect various callbacks

      ...
      Gtk.Window.Show_All (Main_Window);
   <B>end</B> Create_Window;
</PRE>



<H1><A NAME="SEC23">Resource files</A></H1>

<P>
Resource files let you parametrize aspects of the widgets in a GtkAda
application without having to recompile it.


<P>
A resource file needs to be loaded (<CODE>Gtk.Rc.Parse</CODE>) <VAR>before</VAR> setting
the corresponding window.


<P>
In this file, it is possible to specify the visual characteristics of the
widgets (colors, fonts, ...).
Under X, the <CODE>xfontsel</CODE> command allows you to easily select a font.
The FontSelection widget is also a simple way to select fonts.


<P>
Here is an example of a resource file:

<PRE>
# application.rc
#
# resource file for "Application"

# Buttons style
style "button"
{
# BackGround Colors
#                  Red  Green  Blue
  bg[PRELIGHT] = { 0.0,  0.75, 0.0 } # Green when the mouse is on
                                     # the button
  bg[ACTIVE]   = { 0.75, 0.0,  0.0 } # Red on click
# ForeGround Colors
#                  Red  Green  Blue
  fg[PRELIGHT] = { 1.0,  1.0,  1.0 } # White when the mouse is on
                                     # the button
  fg[ACTIVE]   = { 1.0,  1.0,  1.0 } # White on click
}

# All the buttons will have the style "button"
widget_class "*GtkButton*" style "button"

# Text style
style "text"
{
  font = "-adobe-courier-medium-r-normal-*-15-*-*-*-*-*-*-*"
  text[NORMAL] = { 0.0, 0.0, 0.0 } # black
  fg[NORMAL]   = { 0.0, 0.0, 0.0 } # black
  base[NORMAL] = { 1.0, 1.0, 1.0 } # white : background color
}

# All Gtk_Text will have the "text" style
widget_class "*GtkText" style "text"
</PRE>



<H1><A NAME="SEC24">Memory management</A></H1>

<P>
GtkAda takes care of almost all the memory management for you.
Here is a brief overview of how this works, you'll have to check the sources
if you want more detailed information.
Gtk+ (the C library) does its own memory management through reference
counting, i.e. any widget is destroyed when it is no longer referenced anywhere
in the application.


<P>
In GtkAda itself, a "user_data" is associated with each object
allocated by a <CODE>Gtk_New</CODE> procedure. A "destroy" callback is also
associated, to be called when the object to which the 
user_data belongs is destroyed.
Thus, every time a C object is destroyed, the equivalent Ada structure is also
destroyed (see Gtk.Free_User_Data).
 
Concerning widgets containing children, 
every container holds a reference to its children, whose reference counting is
thus different from 0 (and generally 1). When the container is destroyed, the 
reference of all its children and grand-children is decremented, and they are 
destroyed in turn if needed. So the deallocation of a widget hierarchy is also
performed automatically.




<H1><A NAME="SEC25">Tasking with GtkAda</A></H1>

<P>
Note that Gtk+ under Windows does not interact properly with threads,
so the only safe approach under this operating system is to perform all your
Gtk+ calls in the same task.


<P>
Under other platforms,
the Glib library can be used in a task-safe mode by calling Gdk.Threads.G_Init
and Gdk.Threads.Init before making any other Glib/Gdk calls.
In this mode Gdk automatically locks all
internal data structures as needed. This does not mean that two tasks can
simultaneously access, for example, a single hash table, but they can access
two different hash tables simultaneously. If two different tasks need to
access the same hash table, the application is responsible for locking itself
(e.g by using protected objects).


<P>
When Gdk is initialized to be task-safe, GtkAda is task aware. There is
a single global lock that you must acquire with Gdk.Threads.Enter before
making any Gdk/Gtk call, and which you must release with Gdk.Threads.Leave
afterwards.


<P>
Thus, Gtk.Main.Main should be called with the lock acquired (see example
below), ensuring that all the functions executed in the task that started
the main loop do not need to protect themselves again.


<P>
Beware that the GtkAda main loop (Gtk.Main.Main) can only be be run inside
one specific task. In other words, you cannot call Gtk.Main.Main from
any task other than the one that started the outer level main loop.


<P>
Note that Gdk.Threads assumes that you are using a tasking run time that maps
Ada tasks to native threads.


<P>
A minimal main program for a tasking GtkAda application looks like: 



<PRE>
<B>with</B> Gdk.Threads;
<B>with</B> Gtk.Main;
<B>with</B> Gtk.Enums; <B>use</B> Gtk.Enums;
<B>with</B> Gtk.Window; <B>use</B> Gtk.Window;

<B>procedure</B> GtkAda_With_Tasks <B>is</B>
   Window : Gtk_Window;
<B>begin</B>
   Gdk.Threads.G_Init;
   Gdk.Threads.Init;
   Gtk.Main.Init;

   Gtk_New (Window, Window_Toplevel);
   Show (Window);

   Gdk.Threads.Enter;
   Gtk.Main.Main;
   Gdk.Threads.Leave;
<B>end</B> GtkAda_With_Tasks;
</PRE>

<P>
Callbacks require a bit of attention. Callbacks from GtkAda (signals) are made
within the GtkAda lock. However, callbacks from Glib (timeouts, IO callbacks,
and idle functions) are made outside of the GtkAda lock. So, within a signal
handler you do not need to call Gdk.Threads.Enter, but within the other types
of callbacks, you do.




<H1><A NAME="SEC26">Processing external events</A></H1>

<P>
It often happens that your application, in addition to processing graphical
events through the GtkAda main loop, also needs to monitor external events.
This is the case for instance if you are running external processes and need
to display their output, or if you are listening to incoming data on a socket,
or various other situations. You cannot have your own infinite loop which
keeps checking for those external events, and spawns the GUI as appropriate,
because then the GUI itself starts an infinite loop and never goes back to
your own.


<P>
There are several ways to handle that:



<UL>
<LI>The most correct one, especially if you intend to make the GUI a

 major part of your application (as opposed to just popping up a
 few dialogs here and there), would be to use gtk+ mainloop as the
 infinite loop, instead of yours.

 You can the use what gtk+ calls idle callbacks (which are called
 every time the gtk+ loop is not busy processing graphical events)
 or timeout callbacks (which are called every n millisecons), and
 in those callbacks do the work you were doing before in your own
 infinite loop (that assumes the check is relatively fast,
 otherwise the GUI will be frozen during that time). Such callbacks
 are created through packages in glib-main.ads

<LI>Another approach is not to start the gtk+ mainloop, but check

 yourself periodically whether there are some events to be handled.
 See the subprogram Gtk.Main.Main_Iteration.

 This second approach is not necessarily recommended, since you would
 basically duplicate code that's already in gtk+ to manage the main
 loop, and you also get finer control using idle and timeout callbacks

</UL>



<H1><A NAME="SEC27">Object-oriented features</A></H1>

<P>
GtkAda has been designed from the beginning to provide a full Object
oriented layer over gtk+. This means that features such as type
extension, dynamic dispatching, ... are made available through the
standard Ada language.


<P>
This section will describe both how things work and how you can extend
existing widgets or even create your own.



<UL>
<LI><A HREF="gtkada_ug.html#SEC28">General description of the tagged types</A>
<LI><A HREF="gtkada_ug.html#SEC31">Using tagged types to extend Gtk widgets</A>
<LI><A HREF="gtkada_ug.html#SEC32">Creating new widgets in Ada</A>
</UL>



<H2><A NAME="SEC28">General description of the tagged types</A></H2>



<H3><A NAME="SEC29">Why should I use object-oriented programing ?</A></H3>
<P>
Every widget in GtkAda is a tagged type and has a number of
primitive subprograms which all its children inherit.


<P>
This means that most of the time, as opposed to what you see in C
code, you don't have to explicitly cast types and, even when you have
to, Ada always makes sure that the conversion is valid.


<P>
Thus your programs are much safer and most errors are found at compile
time, as usual with Ada.


<P>
For instance, if you create a table, put some widgets in it, and then,
later in your program, try to access those widgets, then you do not need
to know beforehand what their type is, when and by whom they were
created, ... You simply ask for the children of the table, and you get
in return a tagged type that contains all the information you need. You
can even use dynamic dispatching without ever having to cast to a known
type.


<P>
This makes GtkAda a very powerful tool for designing graphical interfaces.


<P>
If you think one of the standard widgets is nice, but would be even
better if it was drawing itself in a slighlty different way, or if it could
contain some other data that you need in your application, there is a very
simple way to do it: just create a new type that extends the
current one (see the section section <A HREF="gtkada_ug.html#SEC31">Using tagged types to extend Gtk widgets</A>
below.


<P>
Maybe you want to create your own brand new widget, that knows how to draw
itself, how to react to events, ... and you want to be able to reuse it
anytime you need ? Once again, using the standard Ada features, you can
simply create a new tagged type and teach it how to interact with the user.
See the section section <A HREF="gtkada_ug.html#SEC32">Creating new widgets in Ada</A> below.




<H3><A NAME="SEC30">Type conversions from C to Ada widgets</A></H3>

<P>
There are three kinds of widgets that you can use with GtkAda:

<UL>
<LI><I>Ada widgets</I>:

These are widgets that are written directly in Ada, using the object
oriented features of GtkAda
<LI><I>Standard widgets</I>:

These are the widgets that are part of the standard gtk+ and GtkAda
distributions. This include all the basic widgets you need to build
advanced interfaces.
<LI><I>third party C widgets</I>

These are widgets that were created in C, and for which you (or someone else)
created an Ada binding. This is most probably the kind of widgets you will
have if you want to use third party widgets.
</UL>

<P>
GtkAda will always be able to find and/or create a valid tagged type in
the first two cases, no matter if you explicitly created the widget or if it
was created automatically by gtk+. For instance, if you created a widget
in Ada, put it in a table, and later on extracted it from the table, then you
will still have the same widget.


<P>
In the third case (third party C widgets), GtkAda is not, by default, able to
create the corresponding Ada type.


<P>
The case of third party C widgets is a little bit trickier. Since GtkAda does
not know anything about them when it is built, it can't magically convert the C
widgets to Ada widgets. This is your job to teach GtkAda how to do the
conversion.


<P>
We thus provide a 'hook' function which you need to modify. This function is
defined in the package <B>Glib.Type_Conversion</B>. This function takes
a string with the name of the C widget (ex/ "GtkButton"), and should return a
newly allocated pointer. If you don't know this type either, simply
return <B>null</B>.




<H2><A NAME="SEC31">Using tagged types to extend Gtk widgets</A></H2>

<P>
With this toolkit, it's possible to associate your
own data with existing widgets simply by creating new types. This
section will show you a simple example, but you should rather read the
source code in testgtk/ where we used this feature instead of using
<CODE>user_data</CODE> as is used in the C version.



<PRE>
<B>type</B> My_Button_Record <B>is new</B> Gtk_Button_Record <B>with record</B>
    --  whatever data you want to associate with your button
<B>end record</B>;
<B>type</B> My_Button <B>is access all</B> My_Button_Record'<B>Class</B>;
</PRE>

<P>
With the above statements, your new type is defined. Every function
available for <CODE>Gtk_Button</CODE> is also available for <CODE>My_Button</CODE>.
Of course, as with every tagged type in Ada, you can create your own
primitive functions with the following prototype:



<PRE>
<B>procedure</B> My_Primitive_Func (Myb : <B>access</B> My_Button_Record);
</PRE>

<P>
To instanciate an object of type <CODE>My_Button</CODE> in your application, do
the following:



<PRE>
<B>declare</B>
   Myb : My_Button;
<B>begin</B>
   Myb := <B>new</B> My_Button_Record;
   Initialize (Myb);   --  from Gtk.Button
<B>end</B>;
</PRE>

<P>
The first line creates the Ada type, whereas the <CODE>Initialize</CODE> call
actually creates the C widget and associates it with the Ada type.




<H2><A NAME="SEC32">Creating new widgets in Ada</A></H2>

<P>
With GtkAda, you can now create widgets directly in Ada. These new
widgets can be used directly, as if they were part of gtk itself.


<P>
Creating new widgets is a way to create reuseable components. You can
apply to them the same functions as would for any other widget, such as
Show, Hide, ...


<P>
This section will explain how to create two types of widgets: composite
widgets and widgets created from scratch. Two examples are provided with
GtkAda, in the directories <TT>`examples/composite_widget'</TT> and
<TT>`examples/base_widget'</TT>. Please also refer to the gtk+ tutorial,
which describes the basic mechanisms that you need to know to create
a widget (even if the Ada code is really different from the C code...)



<UL>
<LI><A HREF="gtkada_ug.html#SEC33">Creating composite widgets</A>
<LI><A HREF="gtkada_ug.html#SEC34">Creating widgets from scratch</A>
</UL>



<H3><A NAME="SEC33">Creating composite widgets</A></H3>

<P>
A composite widget is a widget that does not do much by itself. Rather,
this is a collection of subwidgets grouped into a more general entity.
For instance, among the standard widgets, <CODE>Gtk_File_Selection</CODE> and
<CODE>Gtk_Font_Selection</CODE> belong to this category.


<P>
The good news is that there is nothing special to know. Just create a
new tagged type, extending one of the standard widgets (or even another
of your own widgets), provide a <CODE>Gtk_New</CODE> function that allocates
memory for this widget, and call the <CODE>Initialize</CODE> function that does
the actual creation of the widget and the subwidgets.
There is only one thing to do: <CODE>Initialize</CODE> should call the
parent class's <CODE>Initialize</CODE> function, to create the underlying C
widget.


<P>
The example directory <TT>`examples/composite_widget'</TT> reimplements the
<CODE>Gtk_Dialog</CODE> widget as written in C by the creators of gtk+.




<H3><A NAME="SEC34">Creating widgets from scratch</A></H3>

<P>
First, an important note: please do not read this if this is your first
time using GtkAda or if you don't really understand the signal mechanism.
Creating a nice and working widget really takes a lot of messing with the
low level signals.


<P>
Creating a widget from scratch is what you want to do if your widget should
be drawn in a special way, should create and emit new signals, ...
The example we give in <TT>`examples/base_widget'</TT> is a small target on
which the user can click, and that sends one of two signals "bullseye" or
"missed", depending on where the user has clicked.


<P>
See also the example in <TT>`examples/tutorial/gtkdial'</TT> for a more complex
widget, that implements a gauge where the user can move the arrow to select
a new value.


<P>
Once again, the only two functions that you must create are <CODE>Gtk_New</CODE>
and <CODE>Initialize</CODE>.
This time, <CODE>Initialize</CODE> has to do two things:



<PRE>
Parent_Package.Initialize (Widget);

--  The above line calls the Initialize function from the parent.
--  This creates the underlying C widget, which we are going to
--  modify with the following call:

Gtk.Object.Initialize_Class_Record
  (Widget, Signals, Class_Record);
--  This initializes the "class record" for the widget and
--  creates the signals.
</PRE>

<P>
In the above example, the new part is the second call. It takes three or four
arguments:

<UL>
<LI><CODE>Widget</CODE>

This is the widget that you want to initialize

<LI><CODE>Signals</CODE>

This is an array of string access containing the name of the signals
you want to create. For instance, you could create Signals with

<PRE>
Signals : Gtkada.Types.Chars_Ptr_Array := "bullseye" + "missed";
</PRE>

This will create two signals, named "bullseye" and "missed", whose callbacks'
arguments can be specified with the fourth parameter.

<LI><CODE>Class_Record</CODE>

Every widget in C is associated with two records. The first one, which exists
only once per widget type, is the "class record". It contains the list of
signals that are known by this widget type, the list of default callbacks for
the signals, ...; the second record is an "instance record", which contains
data specific to a particular instance.

In GtkAda, the "instance record" is simply your tagged type and its fields.
The call to <CODE>Initialize_Class_Record</CODE> is provided to initialize the
"class record". As we said, there should be only one such record per widget
type. This parameter "Class_Record" will point to this records, once it is
created, and will be reused for every instanciation of the widget.

<LI><CODE>Parameters</CODE>

This fourth argument is in fact optional, and is used to specify which
kind of parameters each new signal is expecting.
By default (ie if you don't give any value for this parameter), all the
signals won't expect any argument, except of course a possible user_data.
However, you can decide for instance that the first signal ("bullseye") should
in fact take a second argument (say a Gint), and that "missed" will take
two parameters (two Gints).

<CODE>Parameters</CODE> should thus contain a value of

<PRE>
(1 =&#62; (1 =&#62; Gtk_Type_Int, 2 =&#62; Gtk_Type_None),
 2 =&#62; (1 =&#62; Gtk_Type_Int, 2 =&#62; Gtk_Type_Int));
</PRE>

Due to the way arrays are handled in Ada, each component must have the same
number of signals. However, if you specify a type of <CODE>Gtk_Type_None</CODE>, this
will in fact be considered as no argument. Thus, the first signal above has
only one parameter.

Note also that to be able to emit a signal such a the second one, ie with
multiple arguments, you will have to extend the packages defined in
Gtk.Handlers. By default, the provided packages can only emit up to one
argument (and only for a few specific types). Creating your own
<CODE>Emit_By_Name</CODE> subprograms should not be hard if you look at what is done
in <TT>`gtk-marshallers.adb'</TT>. Basically, something like:


<PRE>
<B>procedure</B> Emit_With_Two_Ints
  (Object : <B>access</B> Widget_Type'Class;
   Name   : String;
   Arg1   : Gint;
   Arg2   : Gint);
<B>pragma</B> Import (C, Emit_With_Two_Ints,
    "gtk_signal_emit_by_name");

Emit_With_Two_Ints (Gtk.Get_Object (Your_Widget),
    "missed" &#38; ASCII.NUL, 1, 2);
</PRE>

will emit the "missed" signal with the two parameters 1 and 2.

</UL>

<P>
Then of course <CODE>Initialize</CODE> should set up some signal handlers for
the functions you want to redefine.
Three signals are especially useful:



<UL>

<LI>"size_request"

This callback is passed one parameter, as in :

<PRE>
<B>procedure</B> Size_Request
   (Widget      : <B>access</B> My_Widget_Record;
    Requisition : <B>in out</B> Gtk.Widget.Gtk_Requisition);
</PRE>

This function should modify Requisition to specify the widget's ideal
size. This might not be the exact size that will be set, since some
containers might decide to enlarge or to shrink it.

<LI>"size_allocate"

This callback is called every time the widget is moved in its parent
window, or it is resized. It is passed one paramater, as in :

<PRE>
<B>procedure</B> Size_Allocate
   (Widget     : <B>access</B> My_Widget_Record;
    Allocation : <B>in out</B> Gtk.Widget.Gtk_Allocation)
</PRE>

This function should take the responsability to move the widget, using
for instance <CODE>Gdk.Window.Move_Resize</CODE>.

<LI>"expose_event"

This callback is called every time the widget needs to be redrawn. It
is passed one parameter, the area to be redrawn (to speed things up, you
don't need to redraw the whole widget, just this area).

</UL>



<H1><A NAME="SEC35">Support for Glade, the Gtk GUI builder</A></H1>



<H2><A NAME="SEC36">Introduction</A></H2>
<P>
GtkAda now comes with support for the GUI builder Glade (this is not
the glade released with Gnat for distributed systems).
Using Glade itself is straightforward: it is an intuitive point and click
GUI builder. The main difference from other builders is that, since GtkAda
builds a UI with blocks by default, you will not be asked to set the size and
position of your windows by default. If you are looking for this kind of
interaction you should consider using the Gtk_Fixed container. However, please
read the GtkAda reference manual before considering using Gtk_Fixed.




<H2><A NAME="SEC37">Using Glade</A></H2>
<P>
Note that we only recommend using version 2.0.0 of Glade, as previous
versions are not compatible. Using this version you can directly create Ada
files from Glade by selecting Ada95 as the language under project options.
Glade saves your interface in a project file whose syntax is XML
based. In the following sections, we will refer to this file as either
<CODE>the XML file</CODE> or <CODE>the project file</CODE> interchangeably.




<H3><A NAME="SEC38">Taking advantage of Glade's options</A></H3>
<P>
In this XML file, Glade will save various options that Gate can take
advantage of. In particular, going to the project options window, some General
Options in the <CODE>C Options</CODE> section will also be used by Gate: Gettext
support, if enabled, will generate an additional package called &#60;project&#62;_intl
that will use Gtkada.Intl;
The <CODE>Set Widget Names</CODE> option is also recognized and will generate
additional calls to Set_Name for each widget.




<H3><A NAME="SEC39">Associating icons with buttons</A></H3>
<P>
Up to now, the only way to specify an icon for a button was to associate a
pixmap file to it. As an alternative, if the name specified in the <CODE>Icon</CODE>
field does not contain any dots, Gate will consider the name as a variable
rather than a file name. It is up to you to provide the necessary definitions
in order to compile properly the generated code.




<H3><A NAME="SEC40">Note for GNU/Linux users</A></H3>
<P>
Some GNU/Linux systems come with a precompiled version of Glade that will
usually have support for Gnome. Gate currently does not provide support
for Gnome, which means that you should not enable Gnome support in Glade.




<H2><A NAME="SEC41">Gate</A></H2>


<H3><A NAME="SEC42">Invoking Gate</A></H3>
<P>
Gate is a program that is delivered with GtkAda. Gate
takes the Glade XML file as an argument and generates a set of Ada files.
When compiled, these files will recreate the interface you just designed with
Glade.


<P>
Gate is generally invoked through the <CODE>Write source code</CODE> button in
Glade's menu bar.


<P>
In addition, you can also invoke it from the command line by providing a
Glade project file. This will generate Ada files in the directory set
as the <CODE>Source Directory</CODE> project option, or otherwise in the
current working directory. Note that under Windows, the <CODE>Source Directory</CODE>
option is ignored, and the current directory is always used instead.




<H3><A NAME="SEC43">Structure of the generated files</A></H3>


<H4><A NAME="SEC44">The main file</A></H4>
<P>
The main file is the name of the program name specified in the XML Glade file:
<CODE>&#60;program name&#62;.adb</CODE>. It contains initialization and creation code for
each top level widget contained in the XML file. This is intended as a
convenient default to visualize your GUI, but you will usually want to modify
this initialization.




<H4><A NAME="SEC45">Top level widget files</A></H4>
<P>
For each top level widget, Gate will generate a package
<CODE>&#60;widget&#62;_Pkg</CODE> in the files <CODE>&#60;widget&#62;_pkg.ads</CODE> and
<CODE>.adb</CODE>. These packages contain all the GtkAda calls needed to create the
widgets you designed within Glade. You will usually not need to modify these
files yourself.




<H4><A NAME="SEC46">Main handler file</A></H4>
<P>
This file, called <CODE>callbacks_&#60;program name&#62;.ads</CODE> contains all the Handler
package instantiations needed by your application. You will usually not need
to modify it.




<H4><A NAME="SEC47">Top level widget signal files</A></H4>
<P>
These are the most important files created by Gate. Each is called
<CODE>&#60;widget_name&#62;_pkg-callbacks.adb</CODE>.
They contain stubs for all the callbacks related to a top level widget
you declared in Glade. With the main file, this the only files you should modify
yourself. Note that it is recommended that you structure your application such that
the real code is put in separate packages (i.e not generated files) as much
as possible, to avoid potential merging problems when your interface is
significantly changed within Glade. See next section for more details.


<P>
Currently, Gate will generate callback stub procedures that can
handle the expected number of arguments for each signal. This is done using
a relatively low level mechanism explained in the reference manual
(package Gtk.Handlers). Basically, callbacks expecting arguments will take
a Gtk_Argument as their only parameter, and Gate will generate the appropriate
variable declaration and conversion calls to provide the expected GtkAda
arguments. You should not modify this code by hand unless you know what
you are doing.


<P>
For example, given a signal <CODE>delete_event</CODE>, Gate will generate the
following procedure body, giving access to the arguments of this callback:
a <CODE>Gtk_Window_Record</CODE> (Object) and a <CODE>Gdk_Event</CODE> (Arg1)



<PRE>
<B>function</B> On_Main_Window_Delete_Event
  (Object : <B>access</B> Gtk_Window_Record'Class;
   Params : Gtk.Arguments.Gtk_Args) return Boolean
<B>is</B>
   Arg1 : Gdk_Event := To_Event (Params, 1);
<B>begin</B>
   return False;
<B>end</B> On_Main_Window_Delete_Event;
</PRE>



<H3><A NAME="SEC48">Modifying generated files</A></H3>
<P>
Note that you can easily go back to Glade any time, modify your interface,
and have Gate re-generate a set of files. All your modifications will be
kept in the new files. For that, Gate creates a directory <CODE>.gate</CODE> in the
current directory. Please do not delete it if you want Gate to be able to
keep your changes from one version to the next.


<P>
Also note that to keep track of your modifications, gate relies on either
<CODE>merge</CODE>, or <CODE>patch</CODE> and <CODE>diff</CODE> being available on your system.
If you don't have a working set of <CODE>diff/patch</CODE>, <CODE>configure</CODE> will
simply replace them by null operations, which means that regenerated
files will override the previous ones.


<P>
Under Windows, Gate will not attempt to merge changes. Instead,
it will always regenerate and overwrite every file.


<P>
In some cases, due to major changes in the project file, Gate may not be
able to merge all the changes. In this case, it will notify you so, and
will either keep the old and new sections in the files (if you are using
<CODE>merge</CODE>), or files with a <CODE>.rej</CODE> extension will be generated to help
you do the merge manually.




<H3><A NAME="SEC49">Adding Gate support for new widgets</A></H3>

<P>
This section is intended for developpers that wish to add support for new
GtkAda widgets in Gate. Note that this section is not complete yet.




<H4><A NAME="SEC50">Implementing the <CODE>Generate</CODE> procedure</A></H4>
<P>
To provide support for a new widget, you first need to write a <CODE>Generate</CODE>
procedure that will take care of generating the piece of code related to
the widget's properties. Note that the parents properties do not
have to be handled by this function.



<UL>
<LI>Glib.Glade API

The Glib.Glade package contains a self documented API that will help you
automate most of the work for common widgets.
<LI>Most common mapping


<UL>
<LI>Gen_New

<LI>Gen_Set

<LI>Gen_Call_Child

<LI>example

</UL>

<LI>Registering <CODE>Generate</CODE> functions

  Gtk.Glade.Register_Generate (Widget, Func)
This procedure associates a given Generate procedure with a given widget C name.
</UL>



<H2><A NAME="SEC51">Dynamic loading of XML files</A></H2>


<H3><A NAME="SEC52">Introduction to Libglade</A></H3>
<P>
Libglade is an external library provided by default on some systems (e.g
GNU/Linux, Gnome) that supports dynamic loading of XML files and creation of
widgets at run time. The advantage of using libglade is that you do not need
to recompile your application to change your user interface. On the other
hand, everything is done at run time, meaning less compile checks, and also the
inability to take advantage of the object oriented features of GtkAda and Ada
such as deriving from a widget creating using the GUI builder, to add fields
and properties.
See packages Glade and Glade.XML for more information.




<H2><A NAME="SEC53">Limitations</A></H2>
<P>
Gate currently support all Gtk+ widgets and properties available
under Glade. But, to help you identify widgets that may not be supported (e.g
Gnome widgets), Gate will generate a warning on the standard error:



<PRE>
$ gate warning.glade
Generating Ada files...

GtkAda-WARNING **: Unsupported widget GnomeCanvas (canvas1)
The following files have been created/updated in src:
[...]
</PRE>

<P>
and add a comment in the <CODE>&#60;widget&#62;_pkg.adb</CODE> file that looks like:



<PRE>
--  WARNING: Unsupported widget GnomeCanvas (canvas1)
</PRE>

<P>
This means that while generating the file Gate detected an unsupported
widget (in this case GnomeCanvas) whose name is canvas1.
If you get such a warning your file may or may not compile properly, but
you won't get the complete widget hierarchy at run time.


<P>
Feel free to send us (see section <A HREF="gtkada_ug.html#SEC56">How to report bugs</A>) the XML file that
causes this problem. We don't guarantee a rapid fix for each particular
problem but receiving real examples of missing functionnalities will
certainly help implementing them faster.




<H1><A NAME="SEC54">Binding new widgets</A></H1>

<P>
GtkAda comes with a Perl script to help you create a binding to a C widget
(this is the script we have used ourselves).  This will not fully
automate the process, although it should really speed things up. You
will probably need less than 15 min to create a new binding once you
will get used to the way GtkAda works. Note that your C file should
have the same format as is used by Gtk+ itself.


<P>
To get started on a new binding, launch the script <TT>`contrib/binding.pl'</TT>
as follows:



<PRE>
$ touch gtk-button.ads
$ binding.pl ../include/gtk/gtkbutton.h &#62; temporary
</PRE>

<P>
This dumps several kind of information on the standard output:



<UL>
<LI>List of subprograms defined in the <TT>`.h'</TT> file. Their

  documentation is also added, since binding.pl will parse the <TT>`.c'</TT> file
  as appropriate.
<LI>List of properties and signals for the widget

<LI>Tentative bodies for the subprograms

  These will often need adjustements, but provide a good start
</UL>

<P>
You can also use this script to update existing bindings:



<PRE>
$ binding.pl ../include/gtk/*.h
</PRE>



<H1><A NAME="SEC55">Debugging GtkAda applications</A></H1>

<P>


<P>
This chapter presents a number of technics that can be used when
debugging GtkAda applications. First, the standard tools to debug
Ada applications can be used:


<DL COMPACT>

<DT>Compile with -g
<DD>
You should almost always include debugging information when compiling
and linking your code. This gives you the possibility to use the
debugger. See below the variable GDK_DEBUG for how to disable grabs.

<DT>bind with -E
<DD>
Using this argument on the <CODE>gnatbind</CODE> or <CODE>gnatmake</CODE> command
line will force the compiler to include backtraces when an exception is
raised. These backtraces can be converted to symbolic backtraces by
using the <CODE>addr2line</CODE> tool.

<DT>Link with -lgmem
<DD>
Using this switch gives access to the <CODE>gnatmem</CODE> tool, that helps
you to detect memory leaks or doubly-deallocated memory. The latter
often results in hard-to-fix Storage_Error exceptions. See the GNAT
User's guide for more information.

</DL>

<P>
There are also a number of technics specific to GtkAda or gtk+
applications. For most of them, you might need to recompile these
libraries with the appropriate switches to get access to the extended
debugging features.


<DL COMPACT>

<DT>Use the <CODE>--sync</CODE> switch
<DD>
Under unix systems, all applications compiled with gtk+ automatically
support this switch, which forces events to be processed synchronously,
thus making it easier to detect problems as soon as they happen.
This switch is not relevant to Windows systems.

<DT>break on g_log
<DD>
In the debugger, it is often useful to put a breakpoint on the glib
function <CODE>g_log</CODE>. When gtk+ is linked dynamically, you will need
to first start your application with <CODE>begin</CODE>, then put the
breakpoint and continue the application with <CODE>cont</CODE>. This helps
understand internal errors or warnings reported by gtk+ and glib

<DT>compile glib with <CODE>--disable-mem-pools</CODE>
<DD>
Glib, the underlying layer that provides system-independent services
to gtk+, has an extensive and optimized system for memory
allocation. Bigger chunks of Memory are allocated initially, and then
subdivided by glib itself. Although this is extremely performant, this
also make the debugging of memory-related problems (storage_error)
more difficult. Compiling with the above switch forces glib to use the
standard malloc() and free() system calls. On GNU/Linux systems, it might
be useful to set the variable <CODE>MALLOC_CHECK_</CODE> to 1 to use
error-detecting algorithms (see the man page for malloc()).

<DT>compile glib and gtk+ with <CODE>--enable-debug=yes</CODE>
<DD>
It is recommended that you always compile these two libraries by
specifying this switch on the <CODE>configure</CODE> command line. In
addition to giving access to the debugger, this also provides a number
of environment variables that can be set to visualize events, object
creation,...

For these three variables, the possible values are given below. These
are lists of colon-separated keywords. You can choose to remove any of
these value from the variable

<DL COMPACT>

<DT><SAMP>`GOBJECT_DEBUG=objects:signals'</SAMP>
<DD>
This sets up the debugging output for glib. The value <SAMP>`objects'</SAMP>
is probably the most useful, and displays, on exit of the application,
the list of unfreed objects. This helps detect memory leaks. The
second value <SAMP>`signals'</SAMP> will display all the signals emitted by
the objects. Note that this results in a significant amount of output.

<DT><SAMP>`GDK_DEBUG=updates:nograbs:events:dnd:misc:<BR>xim:colormap:gdkrgb:gc:pixmap:image:input:cursor'</SAMP>
<DD>
This sets up the debugging output for gdk. The most useful value is
<SAMP>`nograbs'</SAMP>, which prevents the application from ever grabbing the
mouse or keyboards. If you don't set this, it might happen that the
debugger becomes unusable, since you don't have access to the mouse
when the debugger stops on a breakpoint. Another simpler solution is
to debug remotely from another machine, in which case the grabs
won't affect the terminal on which the debugger is running.

<DT><SAMP>`GTK_DEBUG=misc:plugsocket:text:tree:updates:keybindings'</SAMP>
<DD>
This sets up the debugging output for gtk. Almost all of these values
are mostly for internal use by gtk+ developpers, although
<SAMP>`keybindings'</SAMP> might prove useful sometimes.

</DL>

<DT>Import the C function ada_gtk_debug_get_ref_count
<DD>
This function has the following Ada profile:

<PRE>
<B>function</B> Ref_Count (Add : System.Address) <B>return</B> Guint;
<B>pragma Import</B> (C, Ref_Count, "ada_gtk_debug_get_ref_count");
</PRE>

and should be called in a manner similar to


<PRE>
<B>declare</B>
   Widget : Gtk_Widget;
   Count  : Guint;
<B>begin</B>
   Count := Ref_Count (Get_Object (Widget));
<B>end</B>;
</PRE>

and returns the internal reference counter for the widget. When this
counter reaches 0, the memory allocated for the widget is
automatically freed.

This is mostly a debugging aid for people writting their own
containers, and shouldn't generally be needed. You shouldn't rely on
the internal reference counter in your actual code, which is why it
isn't exported by default in GtkAda.

</DL>



<H1><A NAME="SEC56">How to report bugs</A></H1>

<P>


<P>
GtkAda is becoming more and more stable due to its increasing use, but
you may still find bugs while using it. We have tried to test it as much
as possible, essentially by converting the testgtk.c file found in the gtk
distribution, as well as with generating a significant enumber of interfaces
using the GUI builder and Gate.
We strongly suggest that you have a look at testgtk, which gives
a lot of examples of how to use this toolkit.


<P>
There are two kinds of problems you can encounter:

<UL>
<LI>If the gtk library itself was compiled with warnings turned on, you

may get some  warnings messages,  mainly because of types problems.
These warnings should not appear, as we have tried to be as type safe
as possible  in this package. To know exactly where the problem is,
compile your program with debug information, run gdb, and set a
breakpoint on the function <CODE>g_log</CODE>. Then run your program as usual,
using the <CODE>run</CODE> command. Then send us the result of the <CODE>where</CODE>
command. Here is a summary:


<PRE>
$ gnatmake -f -g &#60;your_program_name&#62; `gtkada-config`
$ gdb &#60;your_program_name&#62;
(gdb) break main
(gdb) run
(gdb) break g_log
(gdb) continue
....
(gdb) where
</PRE>

<LI>In  some  (hopefully) rare cases,   you can even get a  segmentation

fault within gtk.  That means there is definitly something wrong either
in your program or in the toolkit.  Please check your program carefully
and, if you think this is a problem in GtkAda itself, send us an e-mail.
</UL>

<P>
If you are a supported user of GNAT, send mail to
<A HREF="mailto:report@gnat.com">mailto:report@gnat.com</A> to report errors, otherwise send mail
to the GtkAda list (<A HREF="mailto:gtkada@lists.adacore.com">mailto:gtkada@lists.adacore.com</A>) explaining exactly
what your are  doing,  what  is  the  expected  result  and  what  you
actually get. Please include the required sources to reproduce the
problem, in a  format usable  by <CODE>gnatchop</CODE>  (basically, insert
all  the required sources at  the end of  the mail). Please  try to
provide as small as possible a  subset of your sources.


<P>
Of course, we will  welcome any patch   you can provide, so  that this
toolkit may be as useful as possible.




<H1><A NAME="SEC57">Bibliography</A></H1>

<P>
We recommand the following documents. Most of them were written with C
in mind, but should be easily adapted after you've read the rest of
this document.



<UL>
<LI>[1] "Gtk+/Gome Application Development" -- Havoc Pennington

This book, by one of the main authors of the the GNOME environment,
describes in detail some of the inner mechanisms of gtk+, including
signal handling, and a complete description of all the widgets and all
the events found in <CODE>Gdk.Event</CODE>.

It is worth noting that this book has been published under the Open
Publication License. You can get an electronic copy of it at
<A HREF="http://www.opencontent.org/">http://www.opencontent.org/</A>.
</UL>



<H1><A NAME="SEC58">GNU Free Documentation License</A></H1>
<P>
<A NAME="IDX1"></A>
<A NAME="IDX2"></A>
<A NAME="IDX3"></A>


<P>
Version 1.1, March 2000
Copyright (C) 2000 Free Software Foundation, Inc.
<BR>
59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.



<H2>0. PREAMBLE</H2>

<P>
The purpose of this License is to make a manual, textbook, or other
written document "free" in the sense of freedom: to assure everyone
the effective freedom to copy and redistribute it, with or without
modifying it, either commercially or noncommercially.  Secondarily,
this License preserves for the author and publisher a way to get
credit for their work, while not being considered responsible for
modifications made by others.


<P>
This License is a kind of "copyleft", which means that derivative
works of the document must themselves be free in the same sense.  It
complements the GNU General Public License, which is a copyleft
license designed for free software.


<P>
We have designed this License in order to use it for manuals for free
software, because free software needs free documentation: a free
program should come with manuals providing the same freedoms that the
software does.  But this License is not limited to software manuals;
it can be used for any textual work, regardless of subject matter or
whether it is published as a printed book.  We recommend this License
principally for works whose purpose is instruction or reference.



<H2>1. APPLICABILITY AND DEFINITIONS</H2>

<P>
This License applies to any manual or other work that contains a
notice placed by the copyright holder saying it can be distributed
under the terms of this License.  The "Document", below, refers to any
such manual or work.  Any member of the public is a licensee, and is
addressed as "you".


<P>
A "Modified Version" of the Document means any work containing the
Document or a portion of it, either copied verbatim, or with
modifications and/or translated into another language.


<P>
A "Secondary Section" is a named appendix or a front-matter section of
the Document that deals exclusively with the relationship of the
publishers or authors of the Document to the Document's overall subject
(or to related matters) and contains nothing that could fall directly
within that overall subject.  (For example, if the Document is in part a
textbook of mathematics, a Secondary Section may not explain any
mathematics.)  The relationship could be a matter of historical
connection with the subject or with related matters, or of legal,
commercial, philosophical, ethical or political position regarding
them.


<P>
The "Invariant Sections" are certain Secondary Sections whose titles
are designated, as being those of Invariant Sections, in the notice
that says that the Document is released under this License.


<P>
The "Cover Texts" are certain short passages of text that are listed,
as Front-Cover Texts or Back-Cover Texts, in the notice that says that
the Document is released under this License.


<P>
A "Transparent" copy of the Document means a machine-readable copy,
represented in a format whose specification is available to the
general public, whose contents can be viewed and edited directly and
straightforwardly with generic text editors or (for images composed of
pixels) generic paint programs or (for drawings) some widely available
drawing editor, and that is suitable for input to text formatters or
for automatic translation to a variety of formats suitable for input
to text formatters.  A copy made in an otherwise Transparent file
format whose markup has been designed to thwart or discourage
subsequent modification by readers is not Transparent.  A copy that is
not "Transparent" is called "Opaque".


<P>
Examples of suitable formats for Transparent copies include plain
ASCII without markup, Texinfo input format, LaTeX input format, SGML
or XML using a publicly available DTD, and standard-conforming simple
HTML designed for human modification.  Opaque formats include
PostScript, PDF, proprietary formats that can be read and edited only
by proprietary word processors, SGML or XML for which the DTD and/or
processing tools are not generally available, and the
machine-generated HTML produced by some word processors for output
purposes only.


<P>
The "Title Page" means, for a printed book, the title page itself,
plus such following pages as are needed to hold, legibly, the material
this License requires to appear in the title page.  For works in
formats which do not have any title page as such, "Title Page" means
the text near the most prominent appearance of the work's title,
preceding the beginning of the body of the text.



<H2>2. VERBATIM COPYING</H2>

<P>
You may copy and distribute the Document in any medium, either
commercially or noncommercially, provided that this License, the
copyright notices, and the license notice saying this License applies
to the Document are reproduced in all copies, and that you add no other
conditions whatsoever to those of this License.  You may not use
technical measures to obstruct or control the reading or further
copying of the copies you make or distribute.  However, you may accept
compensation in exchange for copies.  If you distribute a large enough
number of copies you must also follow the conditions in section 3.


<P>
You may also lend copies, under the same conditions stated above, and
you may publicly display copies.



<H2>3. COPYING IN QUANTITY</H2>

<P>
If you publish printed copies of the Document numbering more than 100,
and the Document's license notice requires Cover Texts, you must enclose
the copies in covers that carry, clearly and legibly, all these Cover
Texts: Front-Cover Texts on the front cover, and Back-Cover Texts on
the back cover.  Both covers must also clearly and legibly identify
you as the publisher of these copies.  The front cover must present
the full title with all words of the title equally prominent and
visible.  You may add other material on the covers in addition.
Copying with changes limited to the covers, as long as they preserve
the title of the Document and satisfy these conditions, can be treated
as verbatim copying in other respects.


<P>
If the required texts for either cover are too voluminous to fit
legibly, you should put the first ones listed (as many as fit
reasonably) on the actual cover, and continue the rest onto adjacent
pages.


<P>
If you publish or distribute Opaque copies of the Document numbering
more than 100, you must either include a machine-readable Transparent
copy along with each Opaque copy, or state in or with each Opaque copy
a publicly-accessible computer-network location containing a complete
Transparent copy of the Document, free of added material, which the
general network-using public has access to download anonymously at no
charge using public-standard network protocols.  If you use the latter
option, you must take reasonably prudent steps, when you begin
distribution of Opaque copies in quantity, to ensure that this
Transparent copy will remain thus accessible at the stated location
until at least one year after the last time you distribute an Opaque
copy (directly or through your agents or retailers) of that edition to
the public.


<P>
It is requested, but not required, that you contact the authors of the
Document well before redistributing any large number of copies, to give
them a chance to provide you with an updated version of the Document.



<H2>4. MODIFICATIONS</H2>

<P>
You may copy and distribute a Modified Version of the Document under
the conditions of sections 2 and 3 above, provided that you release
the Modified Version under precisely this License, with the Modified
Version filling the role of the Document, thus licensing distribution
and modification of the Modified Version to whoever possesses a copy
of it.  In addition, you must do these things in the Modified Version:

<OL>
<LI>

Use in the Title Page (and on the covers, if any) a title distinct
   from that of the Document, and from those of previous versions
   (which should, if there were any, be listed in the History section
   of the Document).  You may use the same title as a previous version
   if the original publisher of that version gives permission.
<LI>

List on the Title Page, as authors, one or more persons or entities
   responsible for authorship of the modifications in the Modified
   Version, together with at least five of the principal authors of the
   Document (all of its principal authors, if it has less than five).
<LI>

State on the Title page the name of the publisher of the
   Modified Version, as the publisher.
<LI>

Preserve all the copyright notices of the Document.
<LI>

Add an appropriate copyright notice for your modifications
   adjacent to the other copyright notices.
<LI>

Include, immediately after the copyright notices, a license notice
   giving the public permission to use the Modified Version under the
   terms of this License, in the form shown in the Addendum below.
<LI>

Preserve in that license notice the full lists of Invariant Sections
   and required Cover Texts given in the Document's license notice.
<LI>

Include an unaltered copy of this License.
<LI>

Preserve the section entitled "History", and its title, and add to
   it an item stating at least the title, year, new authors, and
   publisher of the Modified Version as given on the Title Page.  If
   there is no section entitled "History" in the Document, create one
   stating the title, year, authors, and publisher of the Document as
   given on its Title Page, then add an item describing the Modified
   Version as stated in the previous sentence.
<LI>

Preserve the network location, if any, given in the Document for
   public access to a Transparent copy of the Document, and likewise
   the network locations given in the Document for previous versions
   it was based on.  These may be placed in the "History" section.
   You may omit a network location for a work that was published at
   least four years before the Document itself, or if the original
   publisher of the version it refers to gives permission.
<LI>

In any section entitled "Acknowledgements" or "Dedications",
   preserve the section's title, and preserve in the section all the
   substance and tone of each of the contributor acknowledgements
   and/or dedications given therein.
<LI>

Preserve all the Invariant Sections of the Document,
   unaltered in their text and in their titles.  Section numbers
   or the equivalent are not considered part of the section titles.
<LI>

Delete any section entitled "Endorsements".  Such a section
   may not be included in the Modified Version.
<LI>

Do not retitle any existing section as "Endorsements"
   or to conflict in title with any Invariant Section.
</OL>

<P>
If the Modified Version includes new front-matter sections or
appendices that qualify as Secondary Sections and contain no material
copied from the Document, you may at your option designate some or all
of these sections as invariant.  To do this, add their titles to the
list of Invariant Sections in the Modified Version's license notice.
These titles must be distinct from any other section titles.


<P>
You may add a section entitled "Endorsements", provided it contains
nothing but endorsements of your Modified Version by various
parties -- for example, statements of peer review or that the text has
been approved by an organization as the authoritative definition of a
standard.


<P>
You may add a passage of up to five words as a Front-Cover Text, and a
passage of up to 25 words as a Back-Cover Text, to the end of the list
of Cover Texts in the Modified Version.  Only one passage of
Front-Cover Text and one of Back-Cover Text may be added by (or
through arrangements made by) any one entity.  If the Document already
includes a cover text for the same cover, previously added by you or
by arrangement made by the same entity you are acting on behalf of,
you may not add another; but you may replace the old one, on explicit
permission from the previous publisher that added the old one.


<P>
The author(s) and publisher(s) of the Document do not by this License
give permission to use their names for publicity for or to assert or
imply endorsement of any Modified Version.



<H2>5. COMBINING DOCUMENTS</H2>

<P>
You may combine the Document with other documents released under this
License, under the terms defined in section 4 above for modified
versions, provided that you include in the combination all of the
Invariant Sections of all of the original documents, unmodified, and
list them all as Invariant Sections of your combined work in its
license notice.


<P>
The combined work need only contain one copy of this License, and
multiple identical Invariant Sections may be replaced with a single
copy.  If there are multiple Invariant Sections with the same name but
different contents, make the title of each such section unique by
adding at the end of it, in parentheses, the name of the original
author or publisher of that section if known, or else a unique number.
Make the same adjustment to the section titles in the list of
Invariant Sections in the license notice of the combined work.


<P>
In the combination, you must combine any sections entitled "History"
in the various original documents, forming one section entitled
"History"; likewise combine any sections entitled "Acknowledgements",
and any sections entitled "Dedications".  You must delete all sections
entitled "Endorsements."


<P>
Heading 6. COLLECTIONS OF DOCUMENTS


<P>
You may make a collection consisting of the Document and other documents
released under this License, and replace the individual copies of this
License in the various documents with a single copy that is included in
the collection, provided that you follow the rules of this License for
verbatim copying of each of the documents in all other respects.


<P>
You may extract a single document from such a collection, and distribute
it individually under this License, provided you insert a copy of this
License into the extracted document, and follow this License in all
other respects regarding verbatim copying of that document.



<H2>7. AGGREGATION WITH INDEPENDENT WORKS</H2>

<P>
A compilation of the Document or its derivatives with other separate
and independent documents or works, in or on a volume of a storage or
distribution medium, does not as a whole count as a Modified Version
of the Document, provided no compilation copyright is claimed for the
compilation.  Such a compilation is called an "aggregate", and this
License does not apply to the other self-contained works thus compiled
with the Document, on account of their being thus compiled, if they
are not themselves derivative works of the Document.


<P>
If the Cover Text requirement of section 3 is applicable to these
copies of the Document, then if the Document is less than one quarter
of the entire aggregate, the Document's Cover Texts may be placed on
covers that surround only the Document within the aggregate.
Otherwise they must appear on covers around the whole aggregate.



<H2>8. TRANSLATION</H2>

<P>
Translation is considered a kind of modification, so you may
distribute translations of the Document under the terms of section 4.
Replacing Invariant Sections with translations requires special
permission from their copyright holders, but you may include
translations of some or all Invariant Sections in addition to the
original versions of these Invariant Sections.  You may include a
translation of this License provided that you also include the
original English version of this License.  In case of a disagreement
between the translation and the original English version of this
License, the original English version will prevail.



<H2>9. TERMINATION</H2>

<P>
You may not copy, modify, sublicense, or distribute the Document except
as expressly provided for under this License.  Any other attempt to
copy, modify, sublicense or distribute the Document is void, and will
automatically terminate your rights under this License.  However,
parties who have received copies, or rights, from you under this
License will not have their licenses terminated so long as such
parties remain in full compliance.



<H2>10. FUTURE REVISIONS OF THIS LICENSE</H2>

<P>
The Free Software Foundation may publish new, revised versions
of the GNU Free Documentation License from time to time.  Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.  See
http://www.gnu.org/copyleft/.


<P>
Each version of the License is given a distinguishing version number.
If the Document specifies that a particular numbered version of this
License "or any later version" applies to it, you have the option of
following the terms and conditions either of that specified version or
of any later version that has been published (not as a draft) by the
Free Software Foundation.  If the Document does not specify a version
number of this License, you may choose any version ever published (not
as a draft) by the Free Software Foundation.



<H2>ADDENDUM: How to use this License for your documents</H2>

<P>
To use this License in a document you have written, include a copy of
the License in the document and put the following copyright and
license notices just after the title page:

<BLOCKQUOTE>
<P>
Copyright (c)  YEAR  YOUR NAME.
<BR>
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.1
or any later version published by the Free Software Foundation;
with the Invariant Sections being LIST THEIR TITLES, with the
Front-Cover Texts being LIST, and with the Back-Cover Texts being LIST.
A copy of the license is included in the section entitled "GNU
Free Documentation License".
</BLOCKQUOTE>

<P>
If you have no Invariant Sections, write "with no Invariant Sections"
instead of saying which ones are invariant.  If you have no
Front-Cover Texts, write "no Front-Cover Texts" instead of
"Front-Cover Texts being LIST"; likewise for Back-Cover Texts.


<P>
If your document contains nontrivial examples of program code, we
recommend releasing these examples in parallel under your choice of
free software license, such as the GNU General Public License,
to permit their use in free software.


<P><HR><P>
<H1>Footnotes</H1>
<H3><A NAME="FOOT1" HREF="gtkada_ug.html#DOCF1">(1)</A></H3>
<P>Glade was written by Damon Chaplin
<P><HR><P>
This document was generated on February, 8  2011 using
<A HREF="http://wwwinfo.cern.ch/dis/texi2html/">texi2html</A>&nbsp;1.56k.

</BODY>
</HTML>