$Id: glademm-guide.html,v 1.10 1999/08/12 09:06:39
christof Exp $
Glade-- User's Guide (revised for 1.1.x)
This document attempts to give you a basic understanding on how to use
glade-- and to indicate where it's strengths and weeknesses are.
Glade-- is a backend to glade and glade-2. Glade is a GUI builder
for the Gimp Toolkit (gtk). Glade is entirely written in C. It operates
on an XML file and can output C sourcecode. Glade-- is written in C++
and makes use of recent C++ features such as the STL.
As you might already know there are different versions of gtk+ around:
- 1.2: the old stable release, standard between 1999 and 2002. This
corresponds to gnome 1.2 and 1.4 and gtk-- 1.2. The matching glade
version is 0.6.5. It has a lot of drawbacks compared to 2.2 [no
pkgconfig, no gdk-pixbuf, no unicode, no model/view/controller concept
...]
- 2.2: the new stable release, and upcoming standard. This is the
base of gnome2 and gtkmm2 (2.2 silently replaced 2.0 in 2002.). The
corresponding glade-2 version is 1.1.3. Sadly 1.2 and 2.2 are not
compatible.
You can use glade-- to read a XML file generated with any of the glade
versions to generate code for any target gtk--/gtkmm2 version. But it's
easier if you use the correspondences mentioned above.
As a side note I recommend using a version control system for your
projects, e.g. CVS, subversion or bitkeeper. It makes source code
management really easy and if you by chance delete a file or overwrite
it you can easily get it back. You might even get your old files back if
your design decisions misleaded you. Besides that you can
light-heartedly delete no longer necessary files, you can get their
content back if you need to.
Table of contents
Abstract
Table of contents
Create your glade file
Create
your initial program files (glade--)
Special Widgets, special glade-- features
Optionmenu (offers deactivate signal)
You changed the glade file
The widget map
User types
Using
glade-- generated widgets within another context (components, user
defined widgets)
Strengths of glade--
Weaknesses of glade--
Future development
How to get it
Further reading (more documentation, tutorials, contact)
Create your glade file
Design your GUI up to your favor. I recommend tables, they look great.
For numeric input I use spinbuttons, it doesn't cost you more and the
user might use the extra functionality.
For textual input you might prefer option menus or combo boxes.
You should also take a look at the fancy SearchCombo widget of the
gtk--addons I put to cvs.gnome.org.
! You will not
see the C++ code options in glade unless you select C++ as your
language. (Project options)
Think about partitioning complex windows into substructures.
Toplevel windows always come in their own file (and their own class).
Signal callback functions are part of the topmost widget's class.
Feel free to break this via the 'separate class' or 'separate_file'
switch. I recommend up to ten to twenty callbacks in one class. But on
the other hand you lose the ability to easily access widgets outside
your class if you partition too much.
Then think whether you need to access a widget in your program. If
you need to access it (e.g. read or alter values) inside your scope
(class) declare it as protected. (Attaching signals to widget does not
count as a need to access it)
If you need to access it
outside your scope, mark it as public. Then include the appropriate
header file, glade-- has generated a GMM_* macro for easy access:
GMM_WINDOW1->show();
But remember this breaks all OO design rules! I do not recommend it. A
better way is to pass the widget (or it's containing class) to other
classes' ctors.
Create
your initial program files (glade--)
Look into glade's project options (the sheet) and adjust the pathnames.
I recommend to put the glade file near to your program and let glade
create fresh files in a subdirectory (source directory option in glade).
Then copy the files you need into your current directory.
Simply invoke glade-- by pressing the gear's button in glade or by
hand:
glade-- <project file>
Note that some glade-- options are only available from the command
line.
Glade-- will generate the following files:
- myprog.cc: This file
contains a sample main function. Simple. Extend it up to your choice.
- autogen.sh: Invoking this
shell script configures and compiles the program (standard for gnome
projects)
- Makefile.am:
automake/autoconf really are your friends! Get used to them. I admit
that m4 is horrible though.
- glademm_support.hh,
glademm_support.cc: these files contain helper functions
- window1_glade.hh: This is
the GUI classes declaration file. For the GUI-class name '_glade' is
appended to the widget name. Each class contains its child-widgets'
declarations as members.
Do not modify this file! -
window1.hh: These are your program's classes, no GUI parts
beside the derivation. Signal handlers are declared as member functions
(private by default), this is the place to add your program's variables
and functions (besides including your own classes of course). Glade-- will not add new callbacks to this
file, it will not change it. Look into window1.hh_new for the
signature.
- window1_glade.cc: This
file contains the constructors and destructors for the GUI part of your
program. Do not edit! You might take a look here to see what the code
looks like.
- window1.cc: This file
contains your constructors, destructors, signal handlers, custom
functions etc. Take them out to other files, it would not matter (as
long as you link them into the program, remember to add new files to the
Makefile.am). Good stuff for a constructor is reading of data (e.g.
database interaction) or anything else which has to be done before the
user starts interacting with your program.
Signal handlers react to user's actions. Writing a program in this
manner is not very difficult. Try yourself!
You changed the glade file
Since you should not edit the *_glade.cc and *_glade.hh files glade
will overwrite them. Similar for the glademm_support.?? files. But what
about the rest? Glade-- creates new files:
- myprog.cc_new: contains the new main function, most likely new
toplevel widgets have been added, insert the variable declaration into
your main file.
- Makefile.am_new: New toplevel widgets? Add the corresponding .o
files to your Makefile.am
- window1.hh_new: New classes? Add the corresponding class
declaration to your header file.
New signal handlers? Add the handler declaration to the corresponding
class.
Other changes should not affect this file. - window1.cc_new: You
divided a class (separate class/separate file)? Move the corresponding
Signal callbacks to the right scope!
New signal handlers? Add the handler stub and fill it.
Never rename a widget which form a class! (Unless you want to do a lot
of search/replace work)
I usually use a
diff window1.hh window1.hh_new | less
whenever I did huge changes. Did I mention that CVS is a good idea? Use
it! Accidental overwrites will not cause you any harm. And glade has no
undo function ...
The widget map (not
recommended, see here)
Glade-- creates a map of public widgets. Access them via
the GMM_FOO1 macros. But never access them before they are created or
after they have been destroyed.
User types
glade supports custom widgets. Glade-- takes the creation function name
as include file name (.hh added) and class type.
If neccessary you can include and wrap any other class inside the
expected header file. Glade-- uses some tricks so you can even use
template widgets and uncommon parameter types. Simply abuse the creation
function, e.g. Foo<int>(Class(1,2)).
Using
glade-- generated widgets within another context (components, user
defined widgets)
If you want to use components I propose the following route:
- Create another toplevel window (the name determines the source
file name), I usually set visible to off and title to '' to remember
that this is a dummy window (you don't have to compile these four
files). Name the windows child widget to represent your type! Mark it as
'separate class' and 'separate file'
- Pass it through glade--.
You might as well delete the empty window's files. - Use it like any
other custom widget (put your widget's name into the 'creation function'
part) (see above)
Strengths of glade--
- Used well, glade-- will not overwrite any of your code. Designing
the GUI and programming the application is seperated. You might also
name this a weakness ;-)
- Glade-- is pure C++. You can customize the indentation style of
its output via the 'SourceWriter' class.
You can change the naming of files/widgets via the 'Naming' class.
Glade-- now inherits widget code generation functions (in the
src/writers dir).
Weaknesses of glade--
- Using different file naming schemes is not yet supported. If you
really need it, tell me. You can use a different header extension (e.g.
.h with --header-suffix h)
- SourceWriter is not yet runtime customizable (indentation of
generated programs).
- gnomemm support is still under development (especially 2.x)
- widgets with duplicated names really confuse glade-- (or your
compiler)! And it will choke on widget names with spaces!
Future development
- more and more gnome support
- automatized source code updates (see 'You changed the glade file')
How to get it
http://home.wtal.de/petig/Gtk/
for released versions
anoncvs.gnome.org for development
versions (recommended)
Further reading
Automake + Autoconf tutorial by a gtkmm maintainer http://www.murrayc.com/learning/linux/automake/automake.shtml
Gtk+ online documentation
Gtk-- online documentation
Glade tutorial
Splitting a glademm project into subdirectories
(by Mark Jones)
Glade-- mailing list and it's archives
Glade-- bug
tracking system and project page