Xlib programming
Introduction
X is a large system.
It contains a full set of windowing facilities, the ability to
handle colour, 2-D graphics drawing capacity, network access
mechanisms, and the ability to respond to user preferences.
X includes
-
Network connection between an application and a server.
-
Displaying windows on the server.
-
Drawing graphics and displaying text.
-
Receiving and responding to events.
-
Multiple window systems.
-
Handling user preferences.
-
Colour.
-
Widget libraries
Network connection
Compilation depends on the location of the libraries involved.
The library required is usually libX11.a (on Unix systems) giving
cc -o info info.c -lX11
But this may be different in your own environment.
Displaying windows (1)
-
Xlib allows the programmer to create applications with large numbers of
windows.
-
The windows form a tree structure: the `root' window is the full screen,
and all windows are descendants from this.
-
An application has as its toplevel window an immediate child of the
root window (some window managers will reparent them, but the application
need not worry about this).
-
Generally all other windows will be descendants of the toplevel window.
-
Windows are always `clipped' to their parent, so that no part of them will
show outside of the parent.
-
Some windows, such as those of popup dialogs, may need to appear outside
of their parent---such windows may also be parented off the root window.
Displaying windows (2)
In order to display windows with things inside them, a number
of steps have to be performed.
-
A window must be created with a number of characteristics such as
parent window, width,
height, x- and y-coordinates, background and foreground colours, width of
border, cursor appearance within the window, etc
-
A window will be merely part of the user's display, and so
an application should attempt to make sure that it co-operates
with any other applications running.
-
One way of doing this is to inform the rest of the system
of some of the properties of the window.
-
A window manager, for example, can then make informed
decisions about the geometry of the window and give it an actual position
and size on the screen.
-
Other properties must be set explicitly, and still others may be left to
`default' values.
-
A window always has a parent window within which it must
appear, and default values are usually picked up from the parent window.
-
Before any graphics or text can be drawn in the window,
various parameters such as the background and foreground
colours, the font, the width of lines, etc must be set.
-
As an efficiency measure
these properties are maintained in a graphics context
Events
A non-exhaustive listing of X events is
-
ButtonPress.
-
ButtonRelease.
-
CreateNotify (a window has been created).
-
DestroyNotify (a window is about to be destroyed).
-
EnterNotify (the boundary into a window has just been crossed by the mouse).
-
LeaveNotify (the mouse has just left the window).
-
Expose (a portion of the window has been uncovered).
-
KeyPress.
-
KeyRelease.
-
MotionNotify (the mouse has been moved inside the window).
-
ResizeRequest (the size of the window is being changed).
A standard schema (with some minor additions) is
in the program onewindow.c:
Multiple windows
Fonts
A program that uses a different font is font.c. Here are some extracts from
it---the bits that would need to be changed in any of catch.c, onewindow.c,
etc.
Colour
For portable applications, remember these rules:
-
Your application should be able to function even on monochrome
displays.
-
Some users may be colour blind and be unable to distinguish
shades of blue, or worse, green from red.
-
Colour only the `black' bits.
-
Use a small number of colours---say less than seven.
-
Use the X defaults where possible.
Widgets
-
There are toolkits built above X
-
A basic one is the Athena toolkit, used in e.g. xedit
-
The Motif toolkit is an early commercial one, still used in Java
-
There are many, many others, some in C, some in C++:
Simple Motif program