Manual PDF
Manual PDF
Manual PDF
Version 3.4
1 Introduction 1
1.1 About GraphApp . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 The GraphApp User Interface . . . . . . . . . . . . . . . . . . . 2
2 General Concepts 7
2.1 Initialisation and Event Handling . . . . . . . . . . . . . . . . . . 7
3 Simple Objects 11
3.1 Bytes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.2 Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.3 Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.4 Regions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.5 Colours . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.6 Palettes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3.7 Fonts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.8 Bitmaps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
3.9 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3.10 Image Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
3.11 Image Readers . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
3.12 Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
iii
iv CONTENTS
5 Using Controls 73
5.1 Controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
5.2 Changing the Appearance of Controls . . . . . . . . . . . . . . . 76
5.3 Adding Data to Controls . . . . . . . . . . . . . . . . . . . . . . 77
5.4 Drawing Controls . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5.5 Resizing and Moving Controls . . . . . . . . . . . . . . . . . . . 79
CONTENTS v
6 Drawing Operations 93
6.1 Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . 93
6.2 Drawing Functions . . . . . . . . . . . . . . . . . . . . . . . . . 97
6.3 Copying Pixels . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
6.4 Drawing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
7 Miscellaneous 103
7.1 Clipboard Functions . . . . . . . . . . . . . . . . . . . . . . . . . 103
7.2 Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
7.3 Files and Folders . . . . . . . . . . . . . . . . . . . . . . . . . . 106
7.4 Internationalisation . . . . . . . . . . . . . . . . . . . . . . . . . 110
7.5 Memory Management . . . . . . . . . . . . . . . . . . . . . . . . 112
7.6 Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . 114
7.7 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7.8 Sound Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 116
7.9 Timer Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Chapter 1
Introduction
What Is GraphApp?
GraphApp is a cross-platform graphics library written in the C programming lan-
guage. It works with a variety of operating system and windowing system combi-
nations, including Microsoft Windows, X-Windows, Linux, Macintosh OSX, and
other Unixes. It gives the programmer access to buttons, text boxes, windows,
fonts, colours, and so on, in a platform independent and easy to learn manner,
so that graphical programs can be easily created and ported to different operating
systems.
About the Author The author of GraphApp is L. Patrick, who has been employed
by the University of Sydney, Australia, as a lecturer and tutor in the Basser De-
partment of Computer Science, in the areas of user interface design, computer
graphics and software engineering. GraphApp has been used in teaching courses
at the University.
Use and Distribution of GraphApp
GraphApp is distributed under the App Software Licence. A copy of this li-
cence can be found in the file LICENCE.TXT. GraphApp is free software and is
distributed in the hope it will be of use, but the software has no warranty.
1
2 CHAPTER 1. INTRODUCTION
EMULATED INTERFACE
GraphApp allows programs to have a graphical user interface. This interface is
designed to look very similar to many common graphical user interfaces, such as
the Microsoft Windows interface and the various X-Windows interfaces, such as
Motif and Gnome.
GraphApp does not use other GUI toolkits or native widget sets to implement its
graphical user interface. Instead, it uses its own code. This means that GraphApp
can provide its own features, such as support for Unicode fonts and text, which
other toolkits may be lacking. Conversely, GraphApp widgets may not work in
precisely the same manner as the native widgets on a given platform.
Because GraphApp emulates the widgets found on other platforms, it is a cross-
platform programming tool. That is, it is possible to create a GraphApp program
and compile it for two different operating systems (for example, Windows and
Linux) and it will look and function the same way on both platforms. This is
a different approach to portability than is advocated by those who assert that a
graphical program’s appearance on each platform should blend in with the appear-
ance of the other applications on that platform. Both approaches to portability are
valid and have their uses. GraphApp is designed with inter-platform portability in
mind, rather than the more common “local look and feel” approach.
UNICODE SUPPORT
GraphApp provides support for Unicode fonts and text encodings for internation-
alised text. Internally, GraphApp code assumes the use of the UTF-8 text encod-
ing. A bitmapped Unicode font is also provided with GraphApp to allow display
and editing of UTF-8 encoded Unicode strings.
Text can be cut, copied and pasted using Control-X, Control-C and Control-V re-
spectively. GraphApp checks to see if the text can be represented in one-byte-per-
character format, and if so, pastes text in that format. This gives interoperability
with many existing applications, which assume text on the clipboard is in that for-
mat. GraphApp will accept from the clipboard such text or else UTF-8 text. It
converts all text into UTF-8 for use internally.
There is a way to input Unicode characters into any GraphApp program. The ALT
key can be used to compose some characters. For example, ALT ’ e produces é
and ALT / O produces Ø. This composition technique allows the input of many
glyphs from European languages.
1.2. THE GRAPHAPP USER INTERFACE 3
The Alt key is held down to produce the accent, then released, followed by the
letter. The Shift key may also be needed to type the appropriate accent or the
following character.
For example:
Alt n produces the Spanish small letter n with tilde: ñ
Alt / o produces the Danish/Norwegian small letter o with a slash: ø
Alt ‘ E produces capital E with a grave accent: È
Alt A E produces capital AE ligature: Æ
Alt 1 2 produces the fraction one half: . Some other fractions work similarly.
Any GraphApp program which has a text entry field can thus be used to compose
European characters, for pasting into other applications.
PORTABILITY SUPPORT
GraphApp implements a core set of routines which behave the same way on each
platform. This core set include drawing routines, font selection, and navigation of
folders (directories).
Folder names are assumed to be separated by forward slashes, following the Web
and Unix convention. So, a program would open a file using open file("/My
Documents/stuff.txt", "r") rather than fopen(" My Documents stuff.txt,
"r") on a Windows platform (or any platform, for that matter). Internally,
GraphApp converts forward slashes to whatever the native directory separator is.
This allows GraphApp programs a great degree of portability. Folder and file
names can be represented in one format in source code and in data files, and the
library handles the conversion to whatever the operating system requires.
Text can be written to and read from files in either the UTF-8 or ISO-Latin-1
encodings. All text is converted to the UTF-8 format internally, so it is not valid
to assume one character is stored in one byte if you use the GraphApp file input
1.2. THE GRAPHAPP USER INTERFACE 5
idea, because it’s trivial to implement, but saves a lot of tedious mucking about
with installation programs.
Chapter 2
General Concepts
HEADERS
#include <stdio.h>
#include <graphapp.h>
OBJECTS
struct App {
int gui_available; /* is the GUI available? */
Rect screen_area; /* screen pixel dimensions */
Rect screen_mm; /* size in millimetres */
int num_windows; /* a list of windows */
Window ** windows;
int visible_windows; /* how many windows visible */
int fonts_loaded; /* a list of loaded fonts */
Font ** fonts;
char * program_name; /* absolute path to program */
int has_resources; /* is it a resource file? */
void * data; /* user-defined data */
void * extra; /* platform-specific data */
};
7
8 CHAPTER 2. GENERAL CONCEPTS
FUNCTIONS
NOTES
The two header files which must be included in any GraphApp program are named
<stdio.h> and <graphapp.h>, and they must be included in that order,
since the GraphApp functions make use of the stdio FILE type. Programs be-
gin, as usual, in the main function, which must be defined as above.
The function new app initialises the structures and variables necessary to use the
graphics library’s interface, storing the data into a structure called an App, and
returning a pointer to it. This pointer is required by other functions. If there is no
memory remaining, NULL will be returned. If the program is being run from a
non-graphical terminal, the structure will still be created, the gui available
field will be set to zero, and some functions will still work, such as drawing into
images, but functions which require access to the windowing system, such as
creating a window, or drawing to a bitmap, will not work.
If the graphical user interface could be initialised, the gui available field
will be set to non-zero. The screen area field will be initialised to the screen’s
dimensions in pixels. Typically the x and y fields of this rectangle will be zero,
while the width and height fields will contain the size of the screen in pixels.
The screen mm field will contain the dimensions of the screen in millimetres
(there are 25.4 millimetres in an inch).
The function takes parameters argc and argv from the main function and
searches them for interface-specific options (such as those found in X-Windows
environments). If it finds any such initialisation parameters, it removes them from
the argv list by moving the next parameters forwards in the list, and argc will
2.1. INITIALISATION AND EVENT HANDLING 9
be invalid after this process (a NULL value in the argv array now signals the end
of parameters).
The del app function is called at the end of the program. It closes all windows
and releases from the memory the App structure.
The main loop function is called after creation of graphical objects, such as win-
dows and controls. It polls the windowing system for events and dispatches them
to the appropriate callback functions. The function will terminate when there are
no windows visible. It is thus possible to call this function again if other windows
become visible.
The peek event is true when there are events to be processed in the event queue.
It is rarely used, since it polls the windowing system each time.
The do event function checks if there is an event to be processed, and if there
is, dispatches it. It returns zero only if there are no more events possible, for
instance, if there are no visible windows. It is generally not used, since it polls the
windowing system each time it is called.
The wait event function waits until an event is available, then handles that event
using do event. If there are no more events possible, it returns zero. It is called
repeatedly by the main loop function to handle events.
The exec function launches the specified application, returning 1 on success and
0 on failure. This function exists because the standard C function system is not
always implemented on all platforms (e.g. MS-Windows).
The error function displays an error message in a window and then stops the
application.
EXAMPLES
Here is an example program skeleton:
#include <stdio.h>
#include <graphapp.h>
Simple Objects
3.1 Bytes
OBJECTS
NOTES
A byte is simply another name for an unsigned char, which is defined by the C
language to be an 8-bit integer.
The byte type has been defined in this library as a convenient short-hand for an
unsigned 8-bit integer, and is used in a few functions. It is quite acceptable to use
unsigned char instead, if you prefer, since the two are equivalent.
The possible range of numbers which can be stored in a byte are 0 to 255 inclusive.
If your compiler does not allow the definition of this type, replace all instances
with unsigned char instead.
11
12 CHAPTER 3. SIMPLE OBJECTS
3.2 Points
OBJECTS
struct Point {
int x; /* horizontal co-ordinate */
int y; /* vertical co-ordinate */
};
FUNCTIONS
NOTES
A Point refers to a location in a drawing, which is a bitmap, window or control,
using x and y coordinates.
The coordinate system has x increasing to the right and y increasing down. The
top-left point of a drawing is always the point (0,0). The pixel corresponding to a
point is below and to the right of its coordinates.
Important note: Points, when passed as function parameters, are generally passed
by value on the stack. This means that modifying a point within a function will
not change its co-ordinates outside that function. Points can thus be treated as
numeric objects, like integers. (Were they to be passed by pointer, this would not
be the case.) This differs somewhat from the way Java implicitly passes all objects
by pointer, except for numbers.
To create a new point, call the function pt(x,y). This is a macro which actually
calls new point, but has a shorter name, for convenience.
The points equal function compares two points and returns non-zero if they are
equal, zero if they are not.
The point in rect function returns non-zero if the given point is within the given
rectangle, zero otherwise.
3.3. RECTANGLES 13
3.3 Rectangles
OBJECTS
struct Rect {
int x, y; /* top-left point inside rectangle */
int width, height; /* width and height in pixels */
};
FUNCTIONS
NOTES
A Rect defines a rectangular area. The x and y co-ordinates define the top-left
point within the rectangle, and the rectangle’s width and height are recorded in
pixels. The point (x+width,y+height) will thus be outside the rectangle.
function will not change its co-ordinates outside that function. Rectangles can
thus be treated as numeric objects, like integers. (Were they to be passed by
pointer, this would not be the case.) This differs somewhat from the way Java
implicitly passes all objects by pointer, except for numbers.
A new rectangle can be returned using rect(x,y,width,height). This is a macro
which just calls new rect, but which has a shorter name for convenience.
The inset rect function returns a rectangle which is inset from the given rectan-
gle by the specified number of pixels all the way around. So inset rect(r,i) is
equivalent to rect(r.x+i,r.y+i,r.width-i-i,r.height-i-i).
A rectangle can be centered within another rectangle using the center rect func-
tion: center rect(r1,r2) will return a rectangle with the same size as r1, but cen-
tered within r2, even if r2 is smaller than r1.
The rects equal function compares two rectangles, returning non-zero if they are
equal, zero otherwise.
Calling rect in rect(r1, r2) returns non-zero only if r1 is wholly contained within
r2. By contrast, rect intersects rect(r1, r2) returns non-zero if any part of r1
intersects with r2.
The clip rect function clips a rectangle so that it is within another: clip rect(r1,
r2) will clip r1 to be within r2 and return r1, unless r1 does not overlap r2 at all, in
which case it will return r1 unchanged.
The rect abs function converts the supplied rectangle to canonical form; the width
and height of the returned rectangle will be positive, and the area and location of
the rectangle will remain unchanged.
3.4. REGIONS 15
3.4 Regions
OBJECTS
struct Region {
Rect extents; /* enclosing rectangle */
int size; /* allocated size of rectangle list */
int num_rects; /* list of non-intersecting rects */
Rect * rects;
};
FUNCTIONS
Region * new_region(void);
void del_region(Region *rgn);
Region * new_rect_region(Rect r);
Region * copy_region(Region *rgn);
NOTES
A Region defines an arbitrary collection of points. It is implemented as an array
of non-intersecting rectangles.
Important note: Regions, unlike Rect objects, are usually passed by pointer, not
on the stack. Hence, changing a region within a function will usually also change
it outside that function.
16 CHAPTER 3. SIMPLE OBJECTS
A new empty region can be returned using new region. The function returns
NULL if it runs out of memory and cannot create the region.
The del region function should be used to delete regions when they are no longer
needed.
The new rect region function creates a new region which contains all points
within a given rectangle. It returns NULL if it runs out of memory.
To create a complete copy of an existing region, use copy region. This function
returns NULL if it runs out of memory.
To move a region to a new location, move region is used. It adds (dx,dy) to
all points within a region.
union region forms in dest a region which is the union of all points within the
regions rgn1 and rgn2. The dest parameter must be an existing region, which
may or may not be empty. The function returns one on success, zero if it runs out
of memory. The union of two regions is defined as all points which are within
either region.
union region with rect performs the same operation, but using a rectangle as one
of the items in the union.
intersect region forms the intersection of two regions, placing the result in dest,
which must be an existing, possibly empty, region. It returns one on success, zero
if it runs out of memory. The intersection of two regions is defined as all points
within both regions.
intersect region with rect performs the same operation, but using a rectangle as
one of the items to be intersected.
subtract region forms a region which is rgn1 with rgn2 removed, storing the
result into the already existing, possibly empty, region dest. Only dest is
changed. This will be the set of all points which are inside rgn1, but not in-
side rgn2. The function returns one on success, zero if it runs out of memory.
xor region forms the exclusive-or of two regions, placing the result in dest,
which must be an existing, possibly empty, region. It returns one on success, zero
if it runs out of memory. The exclusive-or of two regions is defined as all points
which are within either region rgn1 or rgn2, but not within both regions.
region is empty return one if the region is empty, zero if it is not. A region is
empty if it encloses no points.
regions equal returns one if the two regions enclose the same set of points, or
3.4. REGIONS 17
3.5 Colours
OBJECTS
struct Colour {
byte alpha; /* 0=opaque, 255=transparent */
byte red; /* intensity of red light, 0=none, 255=bright */
byte green; /* intensity of green light, 0=none, 255=bright */
byte blue; /* intensity of blue light, 0=none, 255=bright */
};
FUNCTIONS
CONSTANTS
NOTES
A Colour value is a structure used to represent a colour, using four integer com-
ponents: three to represent the intensity of red, green and blue light, and one to
represent the transparency of the colour against a background, otherwise known
as the alpha channel value. The names Colour and Color are interchangeable.
The rgb function constructs a fully-opaque Colour value, given the required inten-
sity of red, green and blue light. The values of red, green and blue can range from
zero (no intensity) to 255 (maximum intensity). The rgb function is implemented
as a macro.
Pre-defined colours are available, such as BLACK, WHITE, GREY, BLUE,
RED, GREEN, etc. These are defined using the C pre-processor as calls to
the rgb function, and as such, cannot be used outside of function blocks (for
instance, you cannot use the pre-defined colours in an initialising structure). The
bit-patterns these colours become when inside a bitmap are not generally defined;
bitmaps are platform-dependent data structures, and should be treated as output
devices only. (Note that both American and British English spellings are sup-
ported, both for colour-names and functions which employ the word ’colour’ or
’color’. Thus GREY and GRAY are quite legal, and interchangeable.)
The alpha byte of a Colour value is used to represent a transparency value. If this
byte is equal to 255, that Colour is fully transparent. If the alpha value is zero,
the Colour is fully opaque. In theory, an alpha value between zero and 255 can be
20 CHAPTER 3. SIMPLE OBJECTS
used to perform ’colour blending’, however currently only fully transparent and
fully opaque rgb values are supported. The special constant CLEAR can be used
to represent fully transparent Colour values in images, and so forth.
The argb function constructs a Colour value which has an explicit alpha value
between 0 and 255 inclusive.
The new color and new colour functions are macros which call rgb to create
fully-opaque colours.
Colours can be be compared using rgbs equal. The macro functions colors equal
and colours equal are synonyms for this comparison function.
Important note: Colours, when passed as function parameters, are generally passed
by value on the stack. This means that modifying a colour within a function will
not change its value outside that function. Colours can thus be treated as numeric
objects, like integers. (Were they to be passed by pointer, this would not be the
case.) This differs somewhat from the way Java implicitly passes all objects by
pointer, except for numbers.
3.6. PALETTES 21
3.6 Palettes
OBJECTS
struct Palette {
int size; /* number of colours */
Colour *element; /* array of colours */
};
FUNCTIONS
NOTES
A Palette defines an indexed array of up to 256 colours. The colours are usually
all different, but do not have to be.
The new palette function creates a palette by copying the supplied array of colours,
given the number of elements to copy and a pointer to the start of the array. Be-
cause the palette is a copy of the colours, the supplied array may be deleted after
this function completes.
A palette should be destroyed using del palette.
The set window palette functions sets the window’s private palette to a copy of
the given set of colours, if the window manager supports this operation. If the
depth of the screen is greater than 8 (for example, if the screen is in TrueColour or
DirectColour mode) this operation will do nothing, since there are already more
colours available than would be possible using a private window palette. If the
screen cannot display enough colours to display the entire requested palette, the
first N entries will be used, where N is the number of colours the screen can
display. Therefore, the palette should be sorted in order of most important colours,
22 CHAPTER 3. SIMPLE OBJECTS
for maximum portability. Passing a NULL palette pointer to this function removes
any private palette from the window.
If a window has a private palette, the window manager guarantees to display that
set of colours if the window has mouse focus. When another window has focus,
the colours may be remapped. Usually all windows share the common system
palette, so that no colour ’flashing’ occurs when focus changes between windows.
Such ’flashing’ is minimized in this library due to the way set window palette
matches the requested palette to the closest set of colours in the system palette
before the window manager is notified of the request.
The get window palette function returns a pointer to the actual palette used by a
window. This will be a copy of the requested palette, but might be smaller if not
all the colours could be allocated. It returns NULL if the window does not have
private palette, or if a private palette is not needed (for instance, if the screen is in
TrueColour mode).
The palette translation function writes into dest a series of integers which
show the best match between the colours in the array of colours named elem and
the target palette, such that elem[i] is closest to palette->element[dest[i]]
for 0<=i<size. It returns dest.
This function can be used to determine which entry in a palette has the closest
visual match to one given colour (if size is 1, and elem points to one colour)
or it can generate an entire list of matches at once, for efficiency. The matching
approximates the way the human eye matches colours. dest must be an array
large enough to hold all the answers; it must be at least size bytes long. Note
that although a palette can only hold 256 colours, the elem array might hold more
if, for example, it is a scanline of colours within an Image that is being matched
to a palette.
3.7. FONTS 23
3.7 Fonts
OBJECTS
struct Font {
int refcount; /* used when caching fonts */
int maximum_width; /* maximum character width */
int height; /* pixel height of chars */
char * name; /* UTF-8 encoded font name */
int style; /* style */
App * app; /* back pointer to cache */
int num_subfonts;
Subfont ** subfonts; /* list of subfonts */
void * extra; /* platform-specific data */
};
struct Subfont {
unsigned long base; /* Unicode value of first char */
int num_widths; /* list of font width records */
FontWidth ** widths;
void * extra; /* platform-specific data */
};
struct FontWidth {
int width; /* in pixels, -1=non-existent */
int num_ranges; /* number of (start,end) pairs */
byte * range_list; /* (start,end) pairs of bytes */
};
FUNCTIONS
CONSTANTS
enum {
PLAIN = 0,
BOLD = 1,
ITALIC = 2,
PORTABLE_FONT = 16,
NATIVE_FONT = 32
};
NOTES
A Font is a typeface of a certain size and style.
A font can be obtained using new font. The name parameter is a UTF-8 encoded
string like “serif” or “unifont”, while the style is a bit-field composed of any of the
styles PLAIN, BOLD, ITALIC, PORTABLE FONT or NATIVE FONT. Use
the bitwise-or operator to combine these styles. The height field specifies a pixel-
height for the font. The function may return NULL if it cannot find a suitable
font.
Hence new font(app, “serif”, BOLD, 12) returns a bold serif 12-pixel-high font.
If the PORTABLE FONT style flag is included, only portable fonts supplied
with the library will be searched for a matching font. If the NATIVE FONT
style flags is given, only native platform-specific fonts will be searched. Native
fonts are inherently non-portable and usually do not handle anything more than
ISO-Latin-1 text, but are generally rendered fast. If neither of these flags, or both
of them, are given, first portable fonts and then native fonts are searched for a
matching name, style and size; the first matching font is returned.
The del font function releases the memory used by a font. In general this function
does not need to be used unless memory is critical, as fonts will automatically be
released from memory when the program ends.
The font height functions reports the pixel height of the font. All characters
within the font will use this height as their inter-line spacing.
The font width function reports the pixel width of the given UTF-8 encoded
string in the supplied font. The nbytes parameter specifies the number of bytes
within the string, thus allowing ’ 0’ characters to be within the string.
3.7. FONTS 25
The find default font function returns the default font. The supplied App object’s
font list is searched for the font first, and loaded into that object if it hasn’t already
been loaded.
The current font used for drawing within a given Graphics context can be set using
the set font function.
The set default font function loads the default font and sets that font in the
Graphics context. It is the equivalent of set font(g, find default font(g-
>app));
Three portable fonts are currently supplied with GraphApp: “serif”, “plain” and
“unifont”, the last being the default Unicode font. Portable fonts are stored as
bitmaps. Each bitmap contains space for exactly 256 characters, and is called a
Subfont. Subfonts are loaded on demand by the font rendering engine, and cached
in a list on the Font structure. The details of this caching mechanism, and the other
data associated with a subfont, are not, in general, relevant to the programmer.
If a particular character glyph cannot be found on a font by the font rendering
engine, it will then search for it on the default font. If it still isn’t found, a rectan-
gular box shape will be drawn instead. Since the supplied Unicode font contains
some 35,000 characters, this event should be rare in normal usage.
26 CHAPTER 3. SIMPLE OBJECTS
3.8 Bitmaps
OBJECTS
struct Bitmap {
Window * win;
Rect area;
void * extra;
};
FUNCTIONS
NOTES
A Bitmap holds a rectangular pixel-image in offscreen memory. Bitmaps are very
fast to copy to a window or to another bitmap, and are used to store prepared
images for quick display. A bitmap can be drawn into, and also used as a source
of pixels. Bitmaps can have only one level of transparency, so that pixels are
either fully opaque or fully transparent within the bitmap. Drawing into a bitmap
renders modified pixels opaque.
The new bitmap function creates and returns an initially transparent bitmap with
the specified pixel width and height. The arrangement of data within the bitmap
is guaranteed to be the same as the supplied window, so that pixels may be copied
from the bitmap to that window and they will represent the same colours. A
bitmap always has a zero origin in its top-left corner. The function returns NULL
if it runs out of memory.
The del bitmap function releases the memory used by a bitmap.
The get bitmap area function returns a rectangle containing the dimensions of
the bitmap. The top and left co-ordinates of this rectangle will always both be
zero.
3.8. BITMAPS 27
The image to bitmap function creates a new bitmap which contains the data from
the image. It uses the window’s colour data arrangement when deciding how to
map the image’s colours onto pixel values in the bitmap. This function is efficient,
but does no dithering. Opaque pixels in the source image overwrite pixels in the
bitmap, and cause those pixels to become opaque. Transparent pixels in the source
image are not copied into the bitmap; hence, portions of the produced bitmap may
be transparent.
28 CHAPTER 3. SIMPLE OBJECTS
3.9 Images
OBJECTS
struct Image {
int depth; /* depth will be 8 or 32 */
int width; /* image width in pixels */
int height; /* image height in pixels */
int cmap_size; /* size of colour map, may be zero */
Colour * cmap; /* indexed colour map, may be null */
byte ** data8; /* array of scanlines, indexed */
Colour ** data32; /* array of scanlines, direct colour */
};
FUNCTIONS
NOTES
3.9. IMAGES 29
colours are placed at the top of the palette. The data8 pixel values will be modified
so they represent the same colours. This may also change the cmap size and cmap
fields in the image. This function has no effect on 32-bit images.
The scale image function produces a new image which is cropped and/or scaled.
The pixels from the source image which correspond to the source rectangle sr
are scaled to fit the destination rectangle dr. The resultant image will have the
same width and height as the destination rectangle. The x and y values from the
destination rectangle are ignored. If the source rectangle lies partially or wholly
outside the image’s rectangle, the corresponding pixels in the new image will be
CLEAR, unless the image has a palette and that palette has no transparent entries,
in which case those pixels will have the value zero.
Use draw image to draw an image into the given rectangle to the destination
specified by the graphics object. If the destination rectangle is smaller or larger
than the source rectangle, the source pixels will be scaled to fit.
The draw image monochrome function draws an image so that it appears black
and white.
The draw image greyscale function draws an image in five levels of grey (a syn-
onym for this function is draw image grayscale). This can be used to provide a
’disabled button’ effect.
By contrast, draw image darker draws an image so that it looks darkened, as if
seen through dark glass. This can be used to provided a ’clicked button’ effect.
The draw image brighter function draws a lighter image.
The image to bitmap function copies the pixels from an image into a new bitmap.
The bitmap will have the same arrangement of colour data as the supplied window,
so that copying the bitmap to that window will produce a picture with very similar
colours to the original image. Bitmaps may store one level of transparency, so
transparent pixels in the image will be transparent in the bitmap.
3.10. IMAGE LISTS 31
OBJECTS
struct ImageList {
int num_images; /* list of images */
Image ** images;
};
FUNCTIONS
ImageList * new_image_list(void);
void del_image_list(ImageList *imglist);
void append_to_image_list(ImageList *imglist, Image *img);
NOTES
An ImageList is simply a dynamically allocated array of image. It grows as Images
are appended to it.
The new image list function allocates memory for an empty image list. The func-
tion returns NULL if there is insufficient memory to create the structure.
The del image list function is used to deallocate the image list structure and all
the images which have been added to the list. Therefore, images should only be
added to the list if they will not be deleted elsewhere.
The append to image list function appends an image to the list, after first reallo-
cating the list to be large enough.
32 CHAPTER 3. SIMPLE OBJECTS
OBJECTS
struct ImageReader {
char * filename; /* user-given fields */
FILE * file; /* file to read from */
Palette * src_pal; /* dither to this palette */
int max_cmap_size; /* only use this many colours */
int required_depth; /* 8 or 32 */
FUNCTIONS
ImageReader * new_image_reader(void);
void del_image_reader(ImageReader *reader);
3.11. IMAGE READERS 33
CONSTANTS
enum ImageReaderState {
STOPPED = 0, /* at start or end of processing */
STARTING, /* creating data structures */
DITHERING, /* now dithering to a palette */
RENDERING, /* processing lines of pixels */
IMAGE_ERROR = -1 /* an error has happened */
};
enum ImageFormat {
PNG_FORMAT = 1, /* Portable Network Graphics */
JPEG_FORMAT = 2, /* Joint Photographic Experts Group */
GIF_FORMAT = 3, /* Graphic Interchange Format */
UNKNOWN_FORMAT = -1
};
NOTES
An ImageReader is an abstract object used to read an Image from a file. It can be
used to control how an image is read and to receive notifications as lines of the
image are completed.
An empty ImageReader structure is first obtained using new image reader, and
then fields are assigned to control the reading process. The fields filename,
file, src pal, max cmap size, and required depth should be as-
signed values. If dithering to a user-specified palette is not required, the src pal
and max cmap size can be left as their initial empty values.
Call-back functions can be assigned which will notify the program when the image
reading process has completed certain stages:
The error func call-back is used if a critical error occurs, such as the image
being damaged or truncated. Typically this will tidy up and place the pro-
gram in a state where it is no longer expecting a valid image pointer.
The startup func call-back is called before any image processing occurs.
It can be used to initialise program values needed to display the progress of
the image reader. The image width and height fields will be correct at
this point.
The after dither func call-back happens after the image reader has dithered
the image to the programmer-specified palette (if any). This happens before
reading any lines of pixels. The image palette will be correct at this point,
and will either be a copy of the programmer-specified palette (if one was
given), or the palette stored within the image (if any), or else NULL.
The progress func call-back occurs after each line of pixels has been read.
It is typically used to update some area on the screen showing how much of
the image has now been read.
The rendering func call-back occurs after each line of pixels has been read,
just after the progress function. It is typically used to copy the current line
of pixels to the screen. The row and row height fields will be set before
calling this function, and report which horizontal line in the image has just
been read.
The success func call-back is called after all image processing has finished
successfully.
If an error occurs part-way through processing, some of the call-backs may not
occur at all. For instance, the success function only happens if the image has been
completely processed. Any of the call-backs can be set to NULL (which they are
initialised to in any case), which prevents that function from being called.
A programmer-specified data pointer can be set in the user data field, for use
during the call-back functions. The pointer is never touched by the image reader
code.
The remaining fields of the ImageReader structure are modified automatically
during image processing.
The state field begins (and ends) at STOPPED and is set to different values
as image reading progresses: STARTING means that data structures are being
allocated, DITHERING means the reader is dithering the image to the required
palette, RENDERING means lines of pixels are being read from the image. The
3.11. IMAGE READERS 35
IMAGE ERROR state only happens if there is an error in the image, or if the con-
nection to the image’s file is somehow broken.
The stage field reports the current stage in processing, from 1 to max stages
inclusive. Different image formats will have a different maximum number of
stages. Interlaced images, for example, may have between 3 and 7 stages, de-
pending on the interlacing technique. Dithering may also be counted as a stage.
In general, it is not possible to determine in advance how many stages there are
for a given image, but it will either be 1, or a small integer usually less than 10.
The row gives the current pixel line which is being decoded. This will be a
number greater than or equal to zero, and less than the pixel height of the image.
The row height field reports the pixel height of this line of pixels when drawn.
Usually this will be equal to one pixel, but it could be larger in the case of in-
terlacing, where lines are read in a non-linear order. For example, a GIF image
might store lines in the file in the order 0,4,2,1. In that case, it might be desirable
to draw the line taller than 1 pixel, so that the image appears as a series of filled
rectangles. Ignoring row height and using a height of 1 for each line in an
interlaced image will instead produce a Venetian blind effect.
The rows done field increases from 0 to the image height, inclusive. It is a
cumulative total of the number of lines read so far.
The lines of image pixels are stored into either the data8 or data32 fields,
depending on whether the required depth field was set to 8 or 32.
The pal field will point to the image’s palette if the required depth was
8, NULL otherwise. This will either be a copy of the src pal supplied by the
programmer, or it will be the palette given in the image file, or a constructed
palette if the image file contains 24-bit or 32-bit data. The max cmap size
field given by the programmer can be zero, which means the palette can have
the maximum possible size (256 elements), or it can be set to a positive integer
between 1 and 256 inclusive, to specify the maximum number of colours that may
be present in the final image.
36 CHAPTER 3. SIMPLE OBJECTS
3.12 Cursors
OBJECTS
struct Cursor {
App * app; /* associated App */
void * extra; /* platform-specific data */
};
FUNCTIONS
CONSTANTS
enum StandardCursors {
BLANK_CURSOR,
ARROW_CURSOR,
WAIT_CURSOR,
CARET_CURSOR,
CROSS_CURSOR,
HAND_CURSOR,
GRAB_CURSOR,
POINTING_CURSOR,
PENCIL_CURSOR,
LASSO_CURSOR,
DROPPER_CURSOR,
MAGNIFY_CURSOR,
MAGPLUS_CURSOR,
MAGMINUS_CURSOR,
TEXT_CURSOR = CARET_CURSOR
};
3.12. CURSORS 37
NOTES
A Cursor represents a mouse pointer on the screen.
New cursors can be created using the new cursor function. The hotspot point is
where the ’tip’ of the mouse pointer should be, and the image defines the shape
of the cursor. The image can have transparent regions, and is allowed to use
BLACK, WHITE and CLEAR colours, and can be at least 16 by 16 pixels in size.
Other colours and larger cursors sizes may be possible, depending on the window
manager.
A number of standard cursors can be obtained using the get standard cursor
function and supplying one of the constants:
The memory used by a cursor can be reclaimed by calling del cursor. Normally
this is not necessary, since all used cursors are released when the App is deleted.
38 CHAPTER 3. SIMPLE OBJECTS
The find best cursor size function asks the window manager what other colours
and sizes can be used by a cursor. Pointers to integers are passed, and filled with
appropriate values. The width and height will be the maximum size of a cursor.
Often these will be 32 or 64, although a cursor might only look good at a width
and height of 16, so beware of using all the available space. The depth may be 1,
indicating BLACK and WHITE and CLEAR are the only colours to be used, or it
may be 32, indicating the cursor may use any colours. The window manager may
or may not allow alpha-blending effects to produce shadows around the cursor.
To change a window’s cursor, use set window cursor. This sets the cursor for
the entire window. It is valid to call this repeatedly within mouse event handlers.
The get cursor position returns the current location of the mouse cursor’s hotspot
in screen co-ordinates. To move the cursor to another location on the screen, use
set cursor position and pass the new position in screen co-ordinates.
Chapter 4
4.1 Windows
OBJECTS
struct Window {
App * app; /* system connection */
char * text; /* title bar string */
long flags; /* status flags */
Rect area; /* drawable area */
void * data; /* user-defined pointer */
Palette * pal; /* private colour palette */
int num_children; /* list of child controls */
Control ** children;
WindowFunc close; /* the user closed the window */
WindowFunc resize; /* the user resized the window */
WindowDrawFunc redraw; /* some part(s) exposed */
WindowMouseFunc mouse_down; /* mouse button clicked */
WindowMouseFunc mouse_up; /* mouse button released */
WindowMouseFunc mouse_drag; /* mouse moved, button down */
WindowMouseFunc mouse_move; /* mouse moved, no buttons down */
39
40 CHAPTER 4. WINDOWS AND CONTROLS
FUNCTIONS
CONSTANTS
NOTES
A Window is a rectangular area displayed on a screen. A window has a zero origin
in its own co-ordinate system, but may have various structures built around its
drawable area, such as title bars, borders and menu bars. A window can be drawn
to by obtaining an appropriate Graphics object.
The new window function creates an initially invisible window with the given
name. The app parameter specifies the connection to the windowing system. The
rectangle specifies where the window’s drawable area should appear on the screen,
with zero being the top-left point of the screen. The window manager is free
to ignore the x and y components of this rectangle, depending on its window
placement policies, but most window managers honour these values and try to
place the window in the correct place. If the width and height values are too large,
the window manager may reduce the size of the window. The flags parameter
is usually the constant STANDARD WINDOW. If an error occurs the function
returns NULL.
The flags field supplied to new window is a bit-field. Various constants can be
combined using the plus or bitwise-or operators to specify how the window should
look. Here is a list of those constants and their meanings:
The TITLEBAR flag gives the window a title bar which can be used for
moving it around the screen and also for displaying the window’s name.
42 CHAPTER 4. WINDOWS AND CONTROLS
The MENUBAR flag can be used to reserve space for a menu bar on the
window, if the platform allows this.
MAXIMIZE gives the user a way of increasing the size of the window to
its maximum, and
RESIZE gives the user a method of changing the size of the window.
Adding the MODAL flag means the window will be in front of all other
application windows when it is displayed, and no mouse or keyboard events
will be sent to the other windows until it is hidden.
The window manager might not be able to implement all of the above functional-
ity, and some platforms have different policies regarding placement of menu bars,
for instance. The flags should be treated as a request to the windowing system,
but that request may be partially or completely ignored. For instance, a window
manager might force all windows to have a title bar, whether one was requested
or not.
The del window function destroys the specified window, hiding it first if it is
currently visible. If a window can be re-used, it is more efficient to hide it and
then show it later on, rather than delete the window and recreate it every time the
user needs the window.
The show window function makes the specified window visible on the screen and
ensures it is the frontmost application window. The hide window function causes
the specified window to vanish from the screen. These functions do not destroy
4.1. WINDOWS 43
the window, so the window can be shown and hidden many times. This is faster
than deleting and recreating a window.
The set window title function changes the name of the window as shown in the
window’s title bar, and get window title returns the current title. Titles must
currently be zero-terminated C strings, not UTF-8 encoded Unicode strings, since
many window managers are not yet Unicode aware.
The set window icon function associates an icon image with a window. This icon
may be visible when the window is minimised, or in other circumstances. An icon
can use colour and transparent pixels, although some window managers, notably
under X11, will only display the icon as monochrome. Icons should be at least 32
pixels tall and wide, and might be automatically scaled, cropped or centered to fit
the window manager’s expectations.
Use move window to change the window’s top-left location without changing
the size of the window. The supplied rectangle’s width and height parameters are
ignore by this function.
Use size window to change the window’s size without changing the location of
the window. The supplied rectangle’s x and y parameters are ignore by this func-
tion.
The redraw rect function just forces a redrawing of the given rectangle (in window
co-ordinates), while the draw window function forces the entire window to be
drawn. The redraw window function is the same as draw window except that
the existing window contents are first erased using the window’s background
colour.
The get window area function returns the window’s drawable rectangle in window
co-ordinates; hence the top-left point will be zero.
The on window close function sets the call-back to be used when the user tries
to close the window using the window’s close-box. If this call-back is not set,
the window will simply be hidden. If the call-back is supplied, it will be called
instead, and the window will not be hidden. It is then up to the programmer to
achieve the desired effect.
The on window resize function sets the call-back to be used when a window
is resized by the user. The window is always resized before being redrawn, in
circumstances where both of these events must be occur.
The on window redraw function is used to attach a call-back function to a window,
which will be called every time that window needs to be redrawn. There is no need
for this call-back to clear the window since this will automatically be done by the
44 CHAPTER 4. WINDOWS AND CONTROLS
window manager.
Mouse events are handled busing the various on window mouse functions to set
call-back functions. A mouse down occurs when a mouse button is clicked within
the window; a mouse up occurs when a mouse button is released. Mouse ups
may occur outside the window since the event mechanism tracks the mouse even
if it leaves the window where a mouse down first occurred. A mouse drag oc-
curs when a mouse button is held down and then the mouse is moved, while a
mouse move occurs when no buttons are held down.
Keyboard events are handled using call-back functions. The on window key down
function sets the handler to be used for most keyboard events, except for the ar-
row keys, function keys, home, end, page up, page down, insert and delete keys,
or keys modified by holding down CONTROL, which are instead handled by
on window key action. Normal key events are mapped to Unicode values, where
possible, and passed as unsigned long integers to the call-back function. Not all
operating systems allow the input of Unicode values.
Note that mouse and keyboard events which are intercepted first by a Control may
not be seen by the underlying window. A control is a separate area placed on a
window’s surface, which can have its own mouse and keyboard handler functions.
When a window is redrawn, empty areas are first filled with the colour white, by
default. A different background colour can be used to fill empty areas, by using
the set window background function. Which colour is currently selected for use
as a background can be determined by using get window background.
A user-supplied pointer may be associated with a window using set window data.
This pointer is converted to a pointer to void and stored in the window’s data struc-
ture, for later use by the programmer. It can be retrieved using get window data.
The hide all windows function hides every window created using the given app
parameter, while del all windows hides and then deletes all such windows. This
can be used when terminating the application. The stop function automatically
calls these when stopping the application anyway, so they may not be needed in
many cases.
EXAMPLES
smiley.c
scribble.c
4.2. LABELS 45
4.2 Labels
FUNCTIONS
CONSTANTS
enum {
ALIGN_LEFT = 1,
ALIGN_RIGHT = 2,
ALIGN_JUSTIFY = 3,
ALIGN_CENTER = 4,
ALIGN_CENTRE = 4,
VALIGN_TOP = 8,
VALIGN_BOTTOM = 16,
VALIGN_JUSTIFY = 24,
VALIGN_CENTER = 32,
VALIGN_CENTRE = 32
};
NOTES
The new label function creates on the specified window a text label which can-
not be edited by the user. The text string is aligned within the rectangle accord-
ing to the value of the alignment parameter. Values such as ALIGN LEFT,
ALIGN RIGHT or ALIGN CENTER can be specified. A value of zero cor-
responds to ALIGN LEFT+VALIGN TOP.
The add label function works in the same way as new label, except that it at-
taches the label to a control rather than directly to a window.
A label does not respond to mouse or keyboard events; in fact, these events pass
straight through a label to the underlying parent control or window. The initial
background colour of a label is transparent, so that only the text of the label is
drawn against the background of the window.
It can be seen from the list of possible text alignments above, both American and
British spellings of the the word ’centre’ are legal.
The horizontal alignments are:
46 CHAPTER 4. WINDOWS AND CONTROLS
ALIGN LEFT (the default) makes the text start at the left hand edge of the
rectangle.
ALIGN RIGHT causes the text to appear at the rightmost edge of the rect-
angle. This is useful in fill-in forms.
VALIGN TOP (the default) means the label’s text will be drawn starting
from the top of the label’s rectangle.
VALIGN BOTTOM draws the text so that it fills the label from the bottom
of its rectangle.
scribble.c
4.3. IMAGE LABELS 47
FUNCTIONS
NOTES
The new image label function creates a control, which has the specified Image
drawn within it. The control will appear on the specified window, at the rectangle
given in window co-ordinates. The image is not copied by this function, so the
image should not be released from memory or modified while it is being used by
the control.
The image label control does not respond to user clicks or key events. It merely
draws the specified image within its boundaries. The alignment of the image
within the rectangle follows the alignment flags described in the entry for la-
bels, except that the image will be scaled to fit the rectangle if an alignment of
ALIGN JUSTIFY or VALIGN JUSTIFY is given.
The add image label function works in the same way as new image label, ex-
cept that it attaches the image label to a control rather than directly to a window.
To change the image used by a control, call set control image, and to retrieve a
pointer to the currently displayed image, use get control image.
48 CHAPTER 4. WINDOWS AND CONTROLS
4.4 Buttons
OBJECTS
FUNCTIONS
NOTES
The new button function creates a push-button control, with the given text string
being centered within the button. The button will appear on the specified window
in the rectangle, given in window co-ordinates.
When the user clicks on the button with the mouse, the specified call-back function
fn is called. The parameter to this function will be a pointer to the button which
called the function.
The add button function works in the same way as new button, except that it
attaches the button to a control rather than directly to a window.
4.5. IMAGE BUTTONS 49
OBJECTS
FUNCTIONS
NOTES
The new image button function creates a push-button control, which has the
specified image drawn within it. The button will appear on the specified window,
in the rectangle given in window co-ordinates. The image is not copied by this
function, so the image should not be released from memory or modified while it
is being used by the control.
When the user clicks on the button with the mouse, the specified call-back function
fn is called. The parameter to this function will be the button which called the
function.
If the button is disabled, an algorithm is used to generate a ’greyed out’ image
based on the original image.
The add image button function works in the same way as new image button,
except that it attaches the button to a control rather than directly to a window.
To change the image used by a button, call set control image, and to retrieve a
pointer to the image currently being used by the button, use get control image.
50 CHAPTER 4. WINDOWS AND CONTROLS
OBJECTS
FUNCTIONS
NOTES
The new check box function creates a check box (a square box with text dis-
played to its right). Clicking on the box causes an X to appear within the box, and
clicking on it again causes the X to vanish.
Each time the state changes between checked and unchecked, the call-back func-
tion fn is called after the event and the check box is passed to the function as its
parameter. This call-back function can be set to NULL, which means no function
should be called in response to checking or unchecking the check box.
The add check box function works in the same way as new check box, except
that it attaches the check box to a control rather than directly to a window.
The function is checked can be used to determine if the check box is currently
checked. The function check can be used to check a check box, and uncheck can
be used to remove a check-mark from the check box.
A check box can be freely switched on or off by the user, unlike radio buttons,
which are part of mutually exclusive sets. See the section on radio buttons for
more details.
EXAMPLES
tester.c
4.7. RADIO BUTTONS AND RADIO GROUPS 51
OBJECTS
FUNCTIONS
NOTES
The new radio button function creates a radio button control, which is similar
to a check box except that it has a circle which is filled in with a large dot when
the user clicks with the mouse.
The add radio button function works in the same way as new radio button,
except that it attaches the radio button to a control rather than directly to a window.
Whenever the user changes the checked-state of a radio button, the action function
fn is called after the change. Often this parameter can be left as NULL, which
signifies that no function need be called in response to a change of state; instead
the state can be determined at some later stage.
One difference between check boxes and radio buttons is that a check box can be
freely switched on and off by the user, while radio buttons automatically belong
to a mutually exclusive set of radio buttons, known as a ’radio group’. Activating
one radio button will therefore switch off the previously active radio button.
Initially, no radio buttons in a radio group will be checked. If a ’default’ option
needs to be specified, use the check function to check one radio button after cre-
ating it.
52 CHAPTER 4. WINDOWS AND CONTROLS
To begin a new radio group, the new radio group function can be used. Af-
ter calling this function, all radio buttons subsequently created attached to the
same window will belong to a new mutually exclusive set of buttons. Similarly
add radio group starts a new radio group within a control. Radio groups can
exist only within one window or control, so each new window has its own initial
radio group, as does each separate control.
The function is checked can be used to determine if a radio button is currently
checked. The function check can be used to activate a radio button while uncheck
can be used to remove the dot from a radio button.
4.8. IMAGE CHECK BOXES 53
OBJECTS
FUNCTIONS
NOTES
The new image check box function creates a check box which displays an image.
This is similar to an image button, except that clicking in the button with a mouse
will swap its state between unchecked (normal button appearance) and checked
(pressed in appearance). If the control is disabled, a greyed version of the image
will be displayed instead.
Each time the control changes between a checked and unchecked state, the call-
back function fn is called after the event and the image check box is passed to
the function as its parameter. This call-back function can be set to NULL, which
means no function will be called in response to checking or unchecking the image
check box.
The add image check box function works in the same way as new image check box,
except that it attaches the image check box to a control rather than directly to a
window.
The function is checked can be used to determine if the image check box is cur-
rently checked. The function check can be used to change the state to checked,
and uncheck can be used to restore an unchecked state.
To implement mutually exclusive sets of image check boxes (like radio button
groups), the call-back function must uncheck the other image check boxes in each
set whenever a new image check box becomes checked.
54 CHAPTER 4. WINDOWS AND CONTROLS
OBJECTS
FUNCTIONS
NOTES
The new scroll bar function creates a scroll bar which can be used to scroll
through text or to change some value within the program. The scroll bar will
fill the given rectangle, and will scroll vertically if the rectangle is taller than it
is wide, or horizontally otherwise. Manipulating the scrollbar with the mouse
changes the scroll position, which has a minimum value of zero (at the left or top
of the scroll bar), and a maximum value given by max.
The amount of some value which is displayed by the scrollbar can be set with
the size shown argument. When the user clicks in the scroll bar to make the
scroll ’thumb’ position jump, it will jump by this amount. For instance, in a text
window which scrolls vertically, if 24 lines out of an 84 line document is visible at
any one time, size shown would be set to 24 and max to 84-24, since the top-
most line of text on the screen at any time can only range from zero at the start of
the document, to 60 at the end. When the user clicks in the scroll bar (other than
on the ’thumb’) the scroll position will increase or decrease by 24.
Every time the scroll bar position changes, the call-back function fn is called.
This function is passed the scroll bar control as a parameter.
The add scroll bar function works in the same way as new scroll bar, except
that it attaches the scroll bar to a control rather than directly to a window.
The change scroll bar function changes the values used by the scroll bar. It will
reset the scroll bar’s position pos, its maximum value max and the size shown
value, and re-draw the control if it is currently visible.
4.9. SCROLL BARS 55
The get control value function returns the scroll bar’s current position value.
56 CHAPTER 4. WINDOWS AND CONTROLS
OBJECTS
FUNCTIONS
NOTES
The new list box function creates a list box, which displays lines of text from a
NULL-terminated array of strings in a scrollable area on screen. Only one line of
text can be selected at any one time.
Whenever the user clicks on one of the text strings with the mouse, that string
becomes selected (is drawn highlighted) and the call-back function fn is called.
The list box control is passed as a parameter, and its value field specifies which
string was selected. Any previous selection is deselected first.
The add list box function works in the same way as new list box, except that it
attaches the list box to a control rather than directly to a window.
The get list box item function returns which item is selected or -1 if none are
selected. Selected values range from zero to the number of strings minus one.
The value will be -1 if no string is currently selected.
Which string is currently selected in the list box can be changed using the set list box item
function. This will change the hilighting to reflect the new selected item. Passing
-1 to this function will remove all hilighting.
The change list box function sets a new array of strings to use in the list box, and
redraws the list. The list of strings is copied, so modification or deletion of the
4.10. LIST BOXES 57
passed-in list is possible without affecting the list box. The existing selection and
scroll bar positions are retained if possible.
The reset list box function removes the selection highlighting (if any) and posi-
tions the scroll bars of the list box so that the first element is visible.
58 CHAPTER 4. WINDOWS AND CONTROLS
OBJECTS
struct MenuBar {
Control * ctrl; /* associated control */
Font * font; /* for displaying all text */
int align; /* text alignment,direction */
int num_menus; /* list of menus */
Menu ** menus;
};
struct Menu {
MenuBar * parent; /* enclosing menubar */
char * text; /* name of the menu */
int num_items; /* list of menu items */
MenuItem ** items;
int lasthit; /* which item was chosen */
Font * font; /* for displaying items */
};
struct MenuItem {
Menu * parent; /* enclosing menu */
char * text; /* menu item name */
int shortcut; /* shortcut key */
int state; /* enabled? checked? */
Menu * submenu; /* pop-up menu */
MenuAction action; /* user-defined action */
void * data; /* user-defined data */
int value; /* user-defined value */
Font * font; /* for displaying this item */
Colour fg; /* for displaying text */
};
FUNCTIONS
4.11. MENUBARS, MENUS AND MENUITEMS 59
NOTES
A MenuBar is a horizontal bar in which the names of menus appear. A Menu
refers to a pull-down menu which contains MenuItems.
A menu bar can be associated with a window by calling new menu bar after
60 CHAPTER 4. WINDOWS AND CONTROLS
that window has been created. This function will return NULL if it fails for some
reason. Calling del menu bar will destroy the menu bar and all associated menus
and menu items, also removing the menu bar from the window. Destroying a
window will automatically call this function.
After creating a menubar, the new menu function is used to create menus, which
are attached to the menubar. Each menu has a name which is displayed in the
menubar, for example “File” might be the name of the menu which controls file
operations.
The new sub menu function can also be used to create menus. The parent
parameter specifies a menu to which the new sub-menu will be added. The name
of the sub-menu will appear in the parent menu, and selecting this name will
make the sub-menu appear. The del menu function can be used to delete a menu
from a menu bar, and destroy all of its menu items.
After the creation of a menu, menu items can be added to it using new menu item.
The name of the item is a string which is displayed in the menu. Next to that name
a short-cut key can be displayed. If key is non-zero it specifies an ASCII charac-
ter which can be typed in combination with some other keyboard key to activate
this menu item. For instance, if the key were given as ’Q’, on a Windows plat-
form the menu item would appear with “Ctrl+Q” next to its name, signifying that
pressing the control key and the letter ’Q’ together would trigger this menu item.
Other platforms would display their own normal menu key-combinations.
A menuitem created with a name which is a hyphen “-” will cause a ’separator’
line to appear in the pull-down menu. This can be used to logically group items
in a menu.
The function pointer fn given as a parameter to new menu item is called when
that menu item is selected by the user. When this call-back function is called, a
pointer to the menu item is passed as a parameter to fn.
Use del menu item to delete one menu item and remove it from its menu.
The check menu item function places a check-mark beside a menu item within
its menu, while the uncheck menu item function removes any such check-mark.
The menu item is checked function returns non-zero if the menu item has a
check-mark next to it, zero if it does not. Menu items initially have no check-
mark.
Menu items are enabled for mouse selection by default. They can be disabled
using disabled menu item, and re-enabled using enable menu item. Whether a
menu item is currently enabled can be determined using menu item is enabled.
4.11. MENUBARS, MENUS AND MENUITEMS 61
OBJECTS
FUNCTIONS
NOTES
A drop list is a push-button which, when clicked, displays a drop-down menu
list of selectable names. The control is created by new drop list on the specified
window, within the given rectangle. The lines parameter is a NULL-terminated
list of strings, to be displayed in the menu. The function pointer fn will be called
when the user chooses one of the lines from the menu.
The add drop list function works in the same way as new drop list, except that
it attaches the list to a control rather than directly to a window.
The program can determine which item was chosen by examining the control’s
value, using get control value. The first string in the list is numbered 0, the next
1, and so on.
EXAMPLES
scribble.c
4.13. DROP-FIELDS 63
4.13 Drop-Fields
OBJECTS
FUNCTIONS
NOTES
A drop field is a one-line text field with an associated push-button which, when
clicked, displays a drop-down menu list of selectable names. The control is cre-
ated using new drop field on the specified window, within the given rectangle.
The lines parameter is a NULL-terminated list of strings, to be displayed in the
menu.
Text can be typed as normal into the text field, or an item can be selected from the
drop-down menu, which will then set the text field’s text to be the selected item.
The add drop field function works in the same way as new drop field, except
that it attaches the drop field to a control rather than directly to a window.
The get control text function can be used to discover what text was typed or
selected.
EXAMPLES
alldemo.c
64 CHAPTER 4. WINDOWS AND CONTROLS
FUNCTIONS
NOTES
A pop-up list is a menu which appears at the press of a mouse button and contains
a list of selectable names. The menu will appear on the specified window, and
vanish when the user releases the mouse button which was used to invoke the
menu. The given font is used for menu text, unless it is NULL, in which case the
default font is used.
The lines parameter is a NULL-terminated list of strings, to be displayed in the
menu. The buttons parameter specifies which mouse button(s) were used to
invoke the menu. Constants such as LEFT BUTTON or RIGHT BUTTON can
be used here. The menu will appear near the given point.
The function returns -1 if no list item was chosen when the mouse button was
released, or else a number between 0 and the number of strings in the list minus
one, indicating which item was chosen.
EXAMPLES
imagine.c
4.15. TEXT FIELDS 65
OBJECTS
FUNCTIONS
NOTES
The new field function creates an editable text field, with the specified text string
displayed within it. The text field will be displayed on one line only and will scroll
horizontally if the user enters more text into it than will fit within the rectangle.
The add field function works in the same way as new field, except that it attaches
the field to a control rather than directly to a window.
The text inside a field can be changed using the set control text function. This
function makes a copy of the string and stores it into the text field.
Use get control text to find the current text inside a textbox or field. The string
returned from get control text is a read-only string, so care must be taken to avoid
modifying or deleting it.
Note that keyboard events will not be sent to a field unless it currently has key-
board focus. The set focus function can be used to set a window’s key focus
to a given control. Setting focus will remove the focus from whichever control
previously had it, if any.
The maximum number of characters which can be typed into a field can be con-
trolled with the set field allowed width function. The width parameter controls
66 CHAPTER 4. WINDOWS AND CONTROLS
the number of Unicode characters which can be typed. If the value is zero, the field
is unrestricted in width, which is the default.
A field can be restricted with set field allowed chars, so that only a set of al-
lowed characters can be typed into the field by the user. All other characters will
be passed to the parent control or window. Setting the allowed characters to
the UTF-8 encoded string “0123456789”, for example, restricts a field so it only
accepts digits, while setting the allowed string to “0123456789.$” additionally
allows the decimal point and dollar sign. Setting the allowed string to NULL
removes all restrictions, which is the default situation.
A field can also be instructed to ignore certain characters, and instead pass them
up to its parent control or window, using the set field disallowed chars function.
For example, if the disallowed string was set to “ n t”, the enter and tab keys
could not be typed into a field, and could thus propagate to the window, where a
key handler function could decide to shift focus, or perform some other action as
a result. By default, the tab, newline and escape keys are disallowed for fields, but
the tab and newline keys are allowed in text boxes, so this function is particularly
useful for controlling multi-line text boxes.
4.16. PASSWORD FIELDS 67
OBJECTS
FUNCTIONS
NOTES
The new password field function creates an editable password text field, with
the specified text string displayed in a secretive fashion to prevent observers from
discovering the password contained within. The text field will be displayed on
one line only and will scroll horizontally if the user enters more text into it than
will fit within the rectangle.
The add password field function works in the same way as new password field,
except that it attaches the field to a control rather than directly to a window.
The text inside a field can be changed using the set control text function. This
function makes a copy of the string and stores it into the text field.
Use get control text to find the actual text inside the field. The string returned
from get control text is a read-only string, so care must be taken to avoid modi-
fying or deleting it.
The cut and copy text operations will not reveal the password text. Instead the
clipboard will only contain what is visible in the password field. This prevents a
user from copying the password text to clear text elsewhere. The paste operation
works normally and can be used to paste a password into the field.
Note that keyboard events will not be sent to a field unless it currently has key-
board focus. The set focus function can be used to set a window’s key focus
to a given control. Setting focus will remove the focus from whichever control
previously had it, if any.
68 CHAPTER 4. WINDOWS AND CONTROLS
OBJECTS
FUNCTIONS
NOTES
To create a text field which can contain multiple lines of text use the new text box
function. A text box has a vertical scrollbar which allows the user to move the text
up and down. If a word will not fit on one line of text, that word will ’wrap’ onto
the next line of text.
The text inside a text box can be changed using the set control text function.
This function makes a copy of the string and stores it into the text box.
The add text box function works in the same way as new text box, except that
it attaches the text box to a control rather than directly to a window.
Use get control text to find the current text inside a text box. The string returned
is a read-only string, so care must be taken not to free or modify the string.
Text can be cut to the clipboard using Ctrl-X, copied using Ctrl-C, and pasted
using Ctrl-V. Arrow keys, mouse and shift-arrow selection, and the delete, home,
end, page up and page down keys will all work as expected.
Note that keyboard events will not be sent to a text box unless it currently has
keyboard focus. The set focus function can be used to set a window’s key focus
to a given control. Setting focus will remove the focus from whichever control
previously had it, if any.
4.18. TEXT EDITING FUNCTIONS 69
FUNCTIONS
NOTES
The following text editing functions will work with any textbox or field:
The cut text function cuts whatever text is currently selected from the specified
textbox, and places the text into the clipboard, while copy text copies the text
to the clipboard without deleting the text. To delete the currently selected text
without transferring it to the clipboard, clear text can be used.
The paste text function pastes the text in the clipboard into the specified textbox,
deleting any currently selected text in the process.
The insert text function inserts a specified text string after the insertion point in
a textbox. The insertion point is then moved to be after the newly inserted text.
Repeated use of this function will thus behave the same as if the user had typed
text into the textbox.
The current text selection can be changed by use of select text. The start and end
locations are measured from zero being before the first character in the textbox.
The number negative one has special meaning: it refers to the location after the
last character in the textbox. Hence select text(-1,-1) moves the insertion point to
the end of the textbox, select text(0,0) moves the insertion point to the start, and
select text(0,-1) selects all the text.
What text is currently selected can be found by use of the text selection function.
It returns the start and end-points of any selected text in the supplied long integer
pointers.
70 CHAPTER 4. WINDOWS AND CONTROLS
Many controls have associated with them a text string, (e.g. buttons, text fields,
labels, windows). This text string can be changed using set text and can be found
using get text. The string returned from get text is a read-only string! Do not
modify it or free it.
4.19. TAB BUTTONS 71
OBJECTS
FUNCTIONS
NOTES
The new tab button function creates a tab-pane button control, with the given
text string being centered within the button. The button will appear on the speci-
fied window in the rectangle, given in window co-ordinates.
When the user clicks on the tab button with the mouse, the specified call-back
function fn is called. The parameter to this function will be a pointer to the
button which called the function. Usually, this function will bring a control ’pane’
to the front within a window, to display that pane, as well as bringing the tab
button itself to the front.
The add tab button function works in the same way as new tab button, except
that it attaches the tab button to a control rather than directly to a window.
72 CHAPTER 4. WINDOWS AND CONTROLS
Chapter 5
Using Controls
5.1 Controls
OBJECTS
struct Control {
Rect area; /* rectangle on parent */
Point offset; /* to window’s co-ordinates */
Window * win; /* parent window, if any */
Control * parent; /* parent control, if any */
int num_children; /* list of child controls */
Control ** children;
Colour bg; /* background fill colour */
Colour fg; /* foreground drawing colour */
int state; /* VISIBLE, CHECKED, etc */
Region * visible; /* in window co-ords */
void * data; /* user-defined pointer */
char * text; /* text to display */
void * extra; /* internal pointer */
long value; /* internal integer value */
Image * img; /* internal image pointer */
Font * font; /* internal image pointer */
ControlFunc resize; /* event handlers */
DrawFunc redraw;
MouseFunc mouse_down;
MouseFunc mouse_up;
MouseFunc mouse_drag;
MouseFunc mouse_move;
73
74 CHAPTER 5. USING CONTROLS
KeyFunc key_down;
KeyFunc key_action;
ControlFunc action;
ControlFunc update;
ControlFunc refocus;
ControlFunc del;
};
FUNCTIONS
NOTES
A Control is a area on a window which responds to user’s actions, such as mouse
clicks and the keyboard, to perform some function. Functions which create con-
trols take as an argument a rectangle, which specifies where on the current window
the control should be placed. The rectangle is measured in pixels, relative to the
top-left point of the window which is the point (0,0).
Buttons, check boxes, text fields, etc are all kinds of Controls, so many of the
functions described in the following sections apply equally to those objects, as
they do to custom-made controls.
The new control function creates and returns a generic control of the requested
size and attaches it to the given window. The control can have call-backs added to
it using on control redraw, ap on control mouse down etc (see later sections),
to allow the control to respond to events.
The add control function behaves much the same, except the created control is
attached to the given parent control. Controls can have other controls attached
5.1. CONTROLS 75
to their surfaces. These child controls will appear inside the parent control, and
cannot move outside of the parent’s boundary.
The del control function destroys a control and removes it from any window to
which it is attached. Normally this function is not used, since destroying a window
will destroy all of its child controls.
The remove control function removes a control from its parent window or control.
A side-effect of this function is that the control will become invisible since the par-
ent window or control will be redrawn. Such a removed control can be re-attached
to a window or control’s surface using attach to window or attach to control.
A control’s position in the stacking hierarchy can be manipulated. The bring control to front
function moves a control in front of its siblings, while send control to back
places it behind its siblings.
The parent window function returns the window where the control resides. If a
control is a child of another control, this function looks up the parent window of
the parent controls until it finds the enclosing window. If the control or its parents
are not currently attached to any window, this function returns NULL.
76 CHAPTER 5. USING CONTROLS
FUNCTIONS
NOTES
When a control or window is drawn on the screen, its rectangle is first automat-
ically cleared by filling it with its background colour (unless it is set to be the
special value CLEAR).
The set control background function takes a Colour value as a parameter and
sets the background colour of the control to that value. The background colour is
returned by get control background.
After the background is drawn, the foreground colour is used in some places
to draw text and other features of a control. The set control foreground and
get control foreground functions access this foreground colour. It is not guaran-
teed that the foreground colour will be used by all controls (for example, image
labels draw images and don’t need to use a foreground colour, while buttons do
use the foreground colour when drawing text).
For controls which display an image, the set control image can be used to change
the image displayed, and get control image returns the current image used.
For controls which display text, the set control font can be used to change the
font used, and get control font returns the current font used. EXAMPLES
tester.c
5.3. ADDING DATA TO CONTROLS 77
OBJECTS
FUNCTIONS
NOTES
Every control can have a long integer value associated with it. Some controls,
such as scrollbars and listboxes use this integer value to represent the current state
of the control.
For programmer-defined controls, the set control value and get control value
functions exist to allow setting this value, or finding the value.
If a control’s state is more complex than an integer, more data must be stored with
a control. The set control data function can store a pointer with a control, which
could point to a data structure in memory. To find the value of this data pointer,
use get control data. Note, this function returns the pointer as a void pointer so
it is necessary to store this to the appropriate pointer type before use.
A control can have a call-back function associated with it for normal uses. This
call-back is usually called by controls (such as buttons) whenever the user clicks
on the control.
The function to be called can be set using the on control action function. This
can be used to set or change a button’s response to events.
The activate control function will call a control’s call-back (if the function pointer
is not NULL, which it is by default).
78 CHAPTER 5. USING CONTROLS
OBJECTS
FUNCTIONS
NOTES
The on control redraw function is used to attach a call-back function to a control
which will be called every time that control needs to be redrawn. There is no need
for this call-back to clear the control’s background since this will done automati-
cally. The call-back function should use drawing operations (see later sections) to
draw the entire contents of the control.
Built-in controls such as buttons, scrollbars, etc have their own redrawing func-
tions, so it is unwise to use the functions in this section on anything except custom-
defined controls.
The draw control function calls the control’s redraw call-back, as set using on control redraw
(see above). The redraw call-back, which is supplied by the programmer, is passed
two parameters: the control which needs to be redrawn, and a Graphics object
which can be used to draw to the control’s surface (see later sections).
The redraw control function behaves in a similar way, except it first clears the
control’s visible area using the control’s background colour, unless that colour is
transparent.
The get control area function can be used within the drawing call-back, to obtain
the rectangle of the control, in its own co-ordinate system. Hence, the top-left
point of this rectangle will be (0,0).
5.5. RESIZING AND MOVING CONTROLS 79
FUNCTIONS
NOTES
The get control area function can be used to discover the rectangle of a control
in its own co-ordinate system. Hence, the top-left point of this rectangle will be
(0,0).
A control can be moved using the move control function. This will change the
location (but not the size) of the control on its parent. The width and height fields
in the supplied rectangle will be ignored, while the x and y field must be expressed
relative the parent’s top-left point.
A control’s size can be changed without moving it, using size control. Here, the
given rectangle’s width and height field are used, while the x and y fields are
ignored. The control’s top-left point will remain in the same location.
80 CHAPTER 5. USING CONTROLS
FUNCTIONS
NOTES
The show control and hide control functions make a control visible or invisible
on its parent. The is visible function returns zero if the control’s state indicates it
is not visible, or one if it is visible.
By default, controls are visible when they are created, but windows are not. Vis-
ibility is the quality of whether the control should be drawn if it is not obscured,
not whether the control is currently unobscured.
A control might be visible, even if its parent window is not. Every object has its
own visibility state, which is separate to every other object’s state.
5.7. DISABLING CONTROLS 81
FUNCTIONS
NOTES
A control can be enabled so it can receive user input and mouse clicks, or disabled
so that it cannot.
To enable a control to receive user input and mouse clicks, call the enable func-
tion. By default, when a control is created, it is always enabled. However, this
function can re-enable a control which has been disabled.
To disable a control, and make it ignore user input, use disable. A disabled control
will usually have grey text, and will not respond to the user. An exception to this
is a disabled textbox or field, which will appear normal, but cannot be edited or
modified by the user.
The is enabled function will return zero if the control is disabled, and one if it is
enabled.
82 CHAPTER 5. USING CONTROLS
FUNCTIONS
NOTES
Push buttons, check boxes, radio buttons, etc are highlighted when the user holds
down a mouse button inside the control’s boundary, and become unhighlighted
when the mouse button is released. The highlight function makes a control appear
highlighted, and unhighlight returns a control to its normal appearance. Whether
or not a control is currently highlighted is reported by is highlighted.
The function flash control simulates a mouse button click in a button, check box
or radio button. The control will become highlighted and after a short period
of time will be unhighlighted. This function does nothing more than change the
appearance of the control for a brief period of time.
5.9. KEYBOARD FOCUS 83
FUNCTIONS
NOTES
A control can have the keyboard focus, which means that in its parent window, if
a user types a key on the keyboard, that key click will be sent to the control which
has focus rather than to the window itself.
To set focus to a particular control, use set focus. This will remove focus from
whatever control previously had the focus, if any, and it will redraw all affected
controls (since some controls appear differently if they have the focus).
Use has focus to determine if the given control has the keyboard focus.
Focus has no effect on mouse events.
The pass event function can be called within a call-back function to pass a key-
board event up the object hierarchy (to the enclosing parent control or window).
This might be used if a control handles some events, but not others. For instance,
a text field handles ordinary key strokes, but might not want to accept ’Enter’
or ’Tab’ key events; instead it might want to pass these events to the window’s
call-back functions.
84 CHAPTER 5. USING CONTROLS
OBJECTS
FUNCTIONS
CONSTANTS
enum {
NO_BUTTON = 0,
LEFT_BUTTON = 1,
MIDDLE_BUTTON = 2,
RIGHT_BUTTON = 4
};
NOTES
The functions on control mouse down, on control mouse up, on control mouse drag,
on control mouse move allow controls to respond to mouse events. They asso-
ciate call-back functions with a control, so that when a certain kind of mouse
events occurs, the relevant call-back function is called.
Each of the functions handles a different kind of mouse event:
A mouse down occurs when a mouse button is clicked while the mouse
pointer is inside a control.
A mouse move occurs when the mouse is moved within a control, but no
mouse buttons are held down at the time.
A mouse drag occurs when the mouse is moved while one or more mouse
buttons are held down at the same time.
5.10. RESPONDING TO MOUSE EVENTS 85
A mouse up occurs when any mouse button is released (some mouse but-
tons may still be held down).
OBJECTS
FUNCTIONS
CONSTANTS
enum {
BELL = 0x07, /* ASCII bell character */
BKSP = 0x08, /* ASCII backspace */
VTAB = 0x0B, /* ASCII vertical tab */
FF = 0x0C, /* ASCII form-feed */
ESC = 0x1B /* ASCII escape character */
};
enum {
INS = 0x2041, /* Insert key */
DEL = 0x2326, /* Delete key */
HOME = 0x21B8, /* Home key */
END = 0x2198, /* End key */
PGUP = 0x21DE, /* Page Up key */
PGDN = 0x21DF, /* Page Down key */
ENTER = 0x2324 /* Numerical keypad Enter key */
};
enum {
LEFT = 0x2190, /* Left arrow key */
UP = 0x2191, /* Up arrow key */
RIGHT = 0x2192, /* Right arrow key */
DOWN = 0x2193 /* Down arrow key */
};
enum {
F1 = 0x276C, /* Function keys */
5.11. RESPONDING TO KEYBOARD EVENTS 87
F2 = 0x276D,
F3 = 0x276E,
F4 = 0x276F,
F5 = 0x2770,
F6 = 0x2771,
F7 = 0x2772,
F8 = 0x2773,
F9 = 0x2774,
F10 = 0x2775
};
enum {
CONTROL = 0x20000000L, /* Modifier bit-fields */
SHIFT = 0x10000000L
};
NOTES
Keyboard events can be caught using the on control key down and on control key action
functions. The first function sets the call-back used to handle normal Unicode
keys, including the return and escape keys. Keys which do not produce Unicode
values are sent to the second function’s call-back, such as when the user presses
an arrow key or a function key.
Keyboard call-backs can be associated with controls. Typically you should not
use these functions with pre-defined controls such as buttons, fields etc, only with
custom-designed controls, since the pre-defined controls already make use of their
own keyboard call-backs.
The call-back specified by the on control key down function may be passed any
of the following keys:
Any normal ASCII key value which can generated by the keyboard.
To see other keyboard events, the on control key action function associates a
call-back with a control or window. That call-back can see the following key
values:
INS, DEL, HOME, END, PGUP, PGDN which correspond to the keys
Insert, Delete, Home, End, Page Up and Page Down.
ENTER which is the Enter key on a numeric keypad. Note that this value
is different from the normal ASCII return key, 0x0A or ’ n’ (newline).
F1, F2, F3, F4, F5, F6, F7, F8, F9, F10 which correspond to the Function
keys on a keyboard. Note that there is currently no provision for Function
keys higher than F10.
Keys modified by holding down the CONTROL key, or the other keys in
this list modified by CONTROL and/or SHIFT being held down.
These keys should not be handled by same call-back as ordinary key down events,
since a Unicode keyboard may be able to generate the same value explicitly, and
then confusion would exist about whether the user pressed an arrow key, or gen-
erated a Unicode value which happens to map to the same number.
5.12. SUMMARY OF CONTROL FUNCTIONS 89
FUNCTIONS
NOTES
The above is a list of the functions which will work on many different kinds of
controls and windows. See the individual sections for details on each function.
Most of the above functions cause a control’s update call-back to be called. If no
update call-back has been set, the control is redrawn by default.
When a control is deleted, its deletion call-back is called just prior to deletion, if
one exists. This function typically tidies up and released any memory stored in
the control’s extra pointer.
5.13. EVENT HANDLERS 91
OBJECTS
FUNCTIONS
NOTES
Above is a list of the event handling call-back functions which can be set for
programmer-defined controls. Some of these functions will do nothing for pre-
defined controls such as buttons, check boxes etc. See the individual sections for
more details.
The pass event function can be called within a call-back function to pass the event
up the object hierarchy. This might be used if a control handles some events, but
not others. For instance, a text field handles ordinary key strokes, but might not
want to accept ’Enter’ or ’Tab’ key events; instead it might want to pass these
events to the window’s call-back functions.
92 CHAPTER 5. USING CONTROLS
Chapter 6
Drawing Operations
OBJECTS
struct Graphics {
Colour colour; /* current drawing colour */
Font * font; /* current text drawing font */
int line_width; /* line width in pixels */
Window * win; /* target window, or */
Bitmap * bmap; /* target bitmap, or */
Control * ctrl; /* target control, or */
Image * img; /* target image */
Region * clip; /* clip all drawing to this region */
Point offset; /* where (0,0) really is */
CopyRect copy_rect; /* pointer to drawing func */
FillRect fill_rect; /* pointer to drawing func */
DrawUTF8 draw_utf8; /* pointer to drawing func */
void * extra; /* platform-specific data */
};
FUNCTIONS
93
94 CHAPTER 6. DRAWING OPERATIONS
NOTES
A Graphics object is required whenever a program needs to draw. A graphics
object is an abstract object obtained prior to drawing, and is released after draw-
ing has finished. Drawing functions (see later sections) use a graphics object as
a common parameter when drawing to windows, controls, bitmaps and images.
Hence, the drawing operations all behave the same on different objects, through
the use of the common graphics object interface.
Sometimes the system obtains and releases a graphics object automatically for
the program, for example, in certain window system call-back functions. When
a window or control is redrawn, its redraw call-back function is called by the
system. This will be defined as a WindowDrawFunc or a DrawFunc (see above
definitions for details). The first parameter passed to this function is the object
being drawn, the second parameter is a graphics object obtained by the system.
The programmer writes code in the call-back function to use the graphics object
for drawing, but does not need to release the graphics object; the system automat-
ically releases it after the call-back has returned.
If the programmer needs to draw to an object, but does not currently have a
valid graphics object to use, one can be obtained using get window graphics
(to draw to a window), get control graphics (to draw to a control on a window),
get bitmap graphics (to draw to a bitmap) or get image graphics (to draw to an
image in memory).
6.1. GRAPHICS OBJECTS 95
Graphics objects obtained through one of those functions should later be deleted
using del graphics. As stated earlier, this function should not be used on a
graphics object passed to a call-back by the system.
A graphics object keeps track of the target object (the object to which drawing is
directed) as well as some drawing state information, such as the current drawing
colour (which is initially black), the text drawing font (which is initially a Unicode
system font) and the pixel width to draw lines (which is initially set to one).
The set rgb function causes the drawing colour to change to the specified colour.
There are two synonyms for this function: set colour and set color both do the
same thing. Note that it is not correct to simply change the colour field in
the graphics object directly; the drawing colour must be changed via the set rgb
function or nothing will happen. The colour field is merely a way of reporting
what the system believes is the current drawing colour. If the alpha field in the
chosen colour is CLEAR (255), no drawing will occur.
The set rgbindex function changes the drawing colour to a particular integer
pixel value, by-passing the sometimes slow colour-matching algorithms used in
set rgb. This can only be used on indexed-colour displays; it will not work as
expected on TrueColour or DirectColour displays.
The set xor mode function changes the drawing mode so that the pixels being
drawn are combined with existing pixels using a bitwise exclusive-or operation.
The alt parameter gives the alternate colour with which drawing will occur.
When drawing shapes, pixels which are the alternate colour will become the cur-
rent drawing colour, and vice versa. Interactions with other colours are unpre-
dictable but reversible; if the same shape is drawn twice the original colours will
be restored.
The set paint mode function restores normal drawing operation after the exclusive-
or drawing mode has been used. This is the default mode whenever a Graphics
object is obtained, and signifies that the drawing colour overwrites existing pixels
wherever drawing occurs.
Use set line width to change the pixel width of lines drawn using the graphics
object. The default line width is 1 pixel.
Use set font to change the font used when drawing text using this graphics object.
The default font is a Unicode system font. See the section on fonts for details of
obtaining fonts.
The set clip rect function restricts drawing to within a certain rectangle. The
rectangle is given in co-ordinates relative to the object to which drawing is directed
96 CHAPTER 6. DRAWING OPERATIONS
(the target object). All drawing will thereafter be clipped to within that rectangle.
The set clip region functions restricts drawing to a given region (collection of
rectangles). The region is given in co-ordinates relative to the target object. All
drawing will be clipped to the region, so that no pixels outside that region will
be changed. The region is copied by this function, so the original region can be
safely modified or deleted after this function has been called, without affecting
the clipping region.
Initially, drawing is only clipped to the boundaries of the target object. When
setting a new clipping rectangle or region, drawing is also still clipped to the
rectangle of the target object. Hence, it is not possible to draw outside a window,
control, bitmap or image.
A note about windows: if a graphics object for a window has been obtained and
then the window is resized, the graphics object must be destroyed and obtained
again. Otherwise the clipping region in the graphics object will be incorrect, and
may restrict drawing to the wrong areas.
EXAMPLES
editdraw.c
6.2. DRAWING FUNCTIONS 97
FUNCTIONS
NOTES
All drawing operations require a valid Graphics object to allow drawing. This
graphics object is used to determine where and how pixels will be drawn. See the
section on graphics object for details on how to obtain a graphics object to draw
to a window, control, bitmap or image.
All of the drawing operations described here return 1 on success, or 0 if a memory
error prevents successful completion.
The draw point function sets the colour of the given point to be the current draw-
ing colour. It will change only one pixel, regardless of the current line width.
The draw rect function draws a rectangle within the given rectangle. The lines
will have a thickness defined by the current line width and will be wholly within
the rectangle.
The draw shadow rect function is similar, but draws the box in two colours. The
first colour c1 is used to draw the top and left portions of the box, and the second
colour c2 is used to draw the bottom and right portions. This creates a raised
border effect, as used by push-buttons and some other controls.
98 CHAPTER 6. DRAWING OPERATIONS
The fill rect function fills the specified rectangle with the current colour.
The draw line function draws a line starting at point p1 and extending up to but
not including point p2. The line will have a width equal to the current line width.
The draw round rect function draws a box with rounded edges. The fill round rect
function fills the rounded box with the current colour.
The draw ellipse function draws a complete ellipse within the supplied rectangle.
Remember that the right and bottom edges of a rectangle are not included within
the rectangle. To draw a circle, make the rectangle a square. The fill ellipse
function fills the corresponding ellipse with the current colour.
The draw arc function draws an ellipsoid arc centred in the middle of the rect-
angle r, extending anti-clockwise from the start angle to the end angle.
Angles are measured in degrees, with 0 degrees being in the 3 o’clock position
on the arc. The arc will fit within the rectangle. The fill arc function creates a
pie-shape with the end-points of the arc joined to the centre point.
To draw many lines at once, the draw polyline function is used. It is passed an
array of n points, and connects each point to the next in the array using draw line.
The draw polygon function is given an array of n points. It will draw lines from
the first point in the array to the next, and so on until it joins the last point back
to the first. The fill polygon function will create a polygon filled with the current
colour.
Graphics operations on some platforms (such as X-Windows) may be buffered,
and for those platforms calling draw all ensures all pending graphics requests are
processed immediately. On other platforms the function exists and does nothing.
This function is called during event handling anyway, and so is not generally
called explicitly.
EXAMPLES
arcs.c
ellipses.c
polygons.c
smiley.c
6.3. COPYING PIXELS 99
FUNCTIONS
NOTES
The copy rect operation copies pixels from the source rectangle sr in the source
graphics object src, to the destination point dp in the destination graphics object
g. There are some restrictions on this function:
Copying pixels from bitmaps into images will not work. Bitmaps are output
devices.
Copying pixels from windows into images will not work. Windows are
output devices.
Copying from a window to a bitmap shares the same restriction, but addi-
tionally might produce incorrect pixels if portions of the source window are
obscured by another window.
Copying from a window to another window shares the same restrictions, for
the same reasons.
For these reasons, it is best to think of bitmaps and windows as output devices
only, with copying going from images to bitmaps to windows, but never in the
other direction:
The texture rect function overlays the entire destination rectangle dr with copies
of the source rectangle sr from the pixel source src, starting in the top-left point
of dr and proceeding to the left and down. This produces a wall-paper effect.
The draw image function specialises in drawing a scaled version of an image. It
copies pixels from the source rectangle sr of the image img into the destination
rectangle dr, scaling between the two rectangles as required. When drawing an
image many times, it is sometimes more efficient to scale the image first then use
copy rect instead of draw image, since the scaling is only performed once.
Drawing an image to a bitmap or window is implemented by creating a temporary
bitmap, copying the image data into the bitmap, copying the bitmap to the window,
then destroying the temporary bitmap. For this reason, when drawing an image
many times, it is sometimes more efficient to create a bitmap from the image using
image to bitmap, and then draw from that bitmap instead.
EXAMPLES
imgtest.c
6.4. DRAWING TEXT 101
FUNCTIONS
CONSTANTS
enum {
ALIGN_LEFT = 1,
ALIGN_RIGHT = 2,
ALIGN_JUSTIFY = 3,
ALIGN_CENTER = 4,
ALIGN_CENTRE = 4,
VALIGN_TOP = 8,
VALIGN_BOTTOM = 16,
VALIGN_JUSTIFY = 24,
VALIGN_CENTER = 32,
VALIGN_CENTRE = 32,
LEFT_TO_RIGHT = 64,
RIGHT_TO_LEFT = 128
};
NOTES
The draw utf8 function draws the Unicode text characters from the UTF-8 en-
coded string, using the current font and the current colour. The number of bytes
in the string is given by nbytes, so the string may contain ’ 0’ characters if
required.
The upper left corner of the first character is placed at point p, and subsequent
characters are placed to the right of each previous character. It will not wrap
the text to the next line if the edge of a window is encountered; it will instead
simply stop drawing. The function returns one if it succeeded, or zero if a problem
happened, such as an inability to load parts of the font or a lack of memory.
102 CHAPTER 6. DRAWING OPERATIONS
The draw text function draws UTF-8 text within a bounding box, using a given
text-alignment. Words are wrapped to the next line as necessary. The possi-
ble text-alignments are: ALIGN LEFT, ALIGN RIGHT, ALIGN JUSTIFY,
ALIGN CENTRE, VALIGN TOP, VALIGN BOTTOM, VALIGN JUSTIFY or
VALIGN CENTRE (American spellings are also allowed). The first four of these
refer to the horizontal alignment of text within the bounding box, the other four
refer to the vertical placement of text. A horizontal and a vertical alignment may
be combined using the plus or bit-wise or operators. An alignment of zero is
equivalent to ALIGN LEFT+VALIGN TOP.
The draw text function draws only as much text as will fit in the bounding box.
It uses the current colour and the current font. The function returns a pointer to
the first character in the text string which could not be drawn within the bounding
box; hence this pointer can be used to continue drawing the text string elsewhere.
It will return NULL if the entire string was drawn.
The text height function returns the pixel height of the given string using the
given font and a supplied maximum pixel width for the UTF-8 text. The number
of lines over which the text will be displayed can thus be determined by dividing
the returned pixel height by the height of the font.
The text width function returns the pixel width of a line of UTF-8 text, if it was
drawn in the given font. The maximum pixel width parameter specifies how wide a
space the text is to be drawn within. Tab characters will align on boundaries which
are multiples of 8 spaces, and newline characters will end the line, otherwise the
line ends when a word wraps to the next line.
The text line length function returns the length in bytes of the largest line of text
which will fit in the given pixel width in the given font. If a newline occurs in the
text, this will end the line and return the length.
The set text direction function sets the direction in which text will be drawn. The
default direction is LEFT TO RIGHT, but this can be changed to RIGHT TO LEFT.
In that case, the point passed to draw utf8 should refer to the top-right point of
the text to be drawn, not the top-left, and the text will be drawn a character at a
time to the left of that point. Similarly, draw text will still draw all text within the
rectangle, but will draw the letters and words from the right to the left. Alignment
is separate to text direction, so it is possible to have text written right-to-left, but
left-aligned.
EXAMPLES
viewutf8.c
Chapter 7
Miscellaneous
FUNCTIONS
NOTES
The set clipboard text function causes the system clipboard to contain the spec-
ified zero-terminated text string. It returns zero if it fails for some reason, or
non-zero if it succeeds.
The get clipboard text function retrieves the text string from the system clip-
board (if it contains a text string). If the clipboard does not contain text, or it is
empty, this function will return NULL.
Text may be modified in the process of saving it to the clipboard. It is not guaran-
teed that non-ASCII characters, or control characters other than tab and newline,
can be transferred using this mechanism.
EXAMPLES
char.c
103
104 CHAPTER 7. MISCELLANEOUS
FUNCTIONS
CONSTANTS
enum {
CANCEL = -1,
NO = 0,
YES = 1
};
NOTES
The error function displays the message string in a modal message window
before terminating the application.
The rest of the functions which accept a title string parameter, display a modal
dialog box with the given title string showing in the title bar.
The ask ok function displays the message string in a modal window with an
“OK” button which the user can select to dismiss the window.
The ask ok cancel function displays a modal window with a question, and
returns YES if the user clicks the “OK” button, and CANCEL if the “Cancel”
button is used to dismiss the window.
The ask yes no function displays the question string in a modal window with
two buttons, “Yes” and “No”. The user may select either one to dismiss the
7.2. DIALOG BOXES 105
window. If the user selects “Yes”, the function returns YES; if the user selects
“No” the function returns NO.
The ask yes no cancel function operates the same way except it has another but-
ton “Cancel” which, if selected, will cause the function to return CANCEL.
The ask string function displays the question string in a modal window with
the specified title, which also has a text field into which the user can type a
string. The text field is initially loaded with the default answer. Space is
allocated for the string using alloc and then returned to the programmer by the
function (use free to free the returned string). If the user selects the “Cancel”
button in this window, the function will instead return NULL.
The ask file open function displays a modal window which can be used to select
a file from the file system. The path string is loaded into the filename text field
if this is available. The function returns the complete file pathname as a string, or
NULL if the user cancels the operation. The returned string should be freed using
free when it is no longer needed.
The ask file save function is similar, except that it is used when saving a file, and
the window may appear differently to signal that fact. It will warn the user if a
selected file already exists to confirm overwriting the file is allowed. The returned
file path string should be freed using free when it is no longer needed.
106 CHAPTER 7. MISCELLANEOUS
FUNCTIONS
CONSTANTS
enum FileInfo {
IS_APP = 1,
IS_WRITE = 2,
IS_READ = 4,
IS_FOLDER = 8,
IS_FILE = 16
};
NOTES
Operating systems often differ when it comes to accessing files and directories
(folders). There are many differences, but two of the main differences are the di-
rectory separator character (which is ’/’ in Unix), and the way to up one directory
level (which is ’..’ in Unix).
7.3. FILES AND FOLDERS 107
The functions in this section have been provided to allow all code to access files
and folders using the Unix-style notation.
Ordinarily, a standard C function such as fopen accepts a platform-specific C
string to use to locate a file. On a Linux platform this might be a string such as
“../manual/folders.htm” while on Windows this would be “.. manual folders.htm”
and on some Macintosh systems might be written as “::manual:folders.htm”. These
differences cause difficulties when porting C source code.
The open file function can be used as a cross-platform replacement for fopen. It
translates the given Unix-style file path string into whatever style the operating
system requires. On Windows, for instance, it exchanges ’/’ for ’ ’ in a copy
of the file path string, then calls fopen, then frees the temporary string. Using
open file instead of fopen allows code to assume Unix-style file and directory
names everywhere.
Correspondingly, close file replaces fclose, except that it returns 1 for success,
and 0 for failure. Passing a NULL file pointer to this function is valid and returns
1.
The remove file function attempts to delete the named file, returning 1 for success,
0 for failure. The name is given in Unix-style notation.
The rename file function renames a file on the file system from the old path name
to the new, both given in Unix-style notation. This can be used to rename a file,
and/or move a file to a new folder. It returns 1 for success, 0 for failure.
The open folder function opens a directory for reading, using a Unix-style direc-
tory specifier. The “..” path component causes a textual removal of the previous
path component, if any, and “.” components are removed, since they represent the
same directory. Again, ’/’ represents the directory path separator on all platforms,
but is internally substituted with the platform-specific separator.
The read folder function reads one file or directory name from the open folder,
returning a pointer to a static zero-terminated string which contains the name, or
NULL if there are no more names listed in that directory. The names “.” and “..”
will usually be present as the first items on the list. The list of names will not be
sorted into alphabetic order.
The close folder function closes an open folder, returning 1 for success, 0 if there
is an error. Passing a NULL folder pointer to this function is valid and returns 1.
Use make folder to create a new folder with the given name and mode, if possible.
The name is given in Unix-style notation. The mode is a bit-field, consisting of
three groups of three permission bits, in the order rwxrwxrwx where ’r’ means
108 CHAPTER 7. MISCELLANEOUS
read access, ’w’ means write access, and ’x’ means execute or search access. The
first (highest) group of bits refers to user-level access, the next to group access,
and the last to world access. The mode is often written as an octal constant, such
as 0755 (which is rwxr-xr-x mode), or 0700 (which is rwx------ mode,
or user access only). The mode may be ignored by some implementations if the
operating system cannot support such access modes. The function returns 1 for
success, 0 for failure.
The remove folder function attempts to delete the named folder from the file
system, returning 1 for success, 0 for failure. The name is given in Unix-style
notation. The operation will fail if there are any files or folders contained within
the folder which is being deleted.
The rename folder function renames a folder on the file system from the old path
name to the new, both given in Unix-style notation. This can be used to rename
a folder, and/or move a folder into a new folder. It returns 1 for success, 0 for
failure.
Use current folder to discover the current working directory, given using Unix-
style notation.
The set current folder function changes the current working directory to the
specified path, given in Unix-style notation. It returns 1 for success, 0 for fail-
ure.
The file info function is a cross-platform version of the standard C function fstat,
which returns some flags yielding information about the given file or directory.
The returned integer is a bit-field with the following possible values:
The file size function reports the size in bytes of the specified file. The path uses
Unix-style notation.
The file time function returns the last modification time of the specified file, rep-
resented in seconds since the start of the epoch (the current epoch is from Jan 1,
1970). The path uses Unix-style notation.
7.3. FILES AND FOLDERS 109
The form file path function joins a Unix-style directory path name and a file
name, to produce a new correct Unix-style path name. The string is created using
alloc, and so should later be freed using free. The function inserts a ’/’ between
the path and the file name if necessary.
The get file name function returns a pointer to the start of just the file name from
a full path name, discarding the folder information. Because this is just a pointer
to the first character in the file name, the returned string should not be freed.
110 CHAPTER 7. MISCELLANEOUS
7.4 Internationalisation
FUNCTIONS
NOTES
The set string function associates a string value with a string key, for use in inter-
nationalising programs. Keys and values are case-sensitive and UTF-8 encoded.
Once a value is associated with a key, that stored value can be returned by passing
the key to get string, otherwise NULL is returned.
The dialog functions use certain keys to determine what message strings to dis-
play. Changing the values associated with these keys allows a program to interna-
tionalise the dialog functions.
The current set of dialog keys and values are given in the table below.
To create a program which uses dialogs in a language other than English, each
of the above values should be translated into the new language, using set string
after the App structure has been created but before using any dialog functions. For
example, a French program might begin:
Some of the defined strings appear on dialog buttons, some in the title bar of
dialog windows, some as messages. Those strings that appear in title bars (the last
4 in the given table) may be impossible to properly internationalise, since some
window managers incorrectly display Unicode text, and might only allow ISO
Latin 1 or even only ASCII text to be used in title bars.
7.4. INTERNATIONALISATION 111
FUNCTIONS
NOTES
All memory allocation in the library uses the functions described in this section.
The alloc function allocates a block of memory size bytes in length. This is
usually just a safe wrapper around the standard C function malloc, but may per-
form additional integrity checks. It aborts the program if it runs out of memory.
Allocating a zero-sized block is valid and returns NULL.
Use free to release from memory any blocks allocated by alloc. Never use the
standard C function free on a block. Passing NULL to free is allowed, and does
nothing.
The zero alloc function uses alloc to create a block of memory, and then uses the
standard C function memset to set every byte of that block to the value zero. It
returns NULL only if alloc returned NULL.
The realloc function accepts a pointer to a memory block which was previously
allocated using alloc, and resizes it to the newsize (in bytes). It keeps the old
contents, or as much as will fit in the new block if making it smaller. The block
may be moved around in memory, so a new pointer is returned by this function,
and the old pointer becomes invalid. If the function runs out of memory, it aborts
the program. The block may be resized to zero bytes, in which case the function
frees the block and returns NULL. The pointer passed in may be NULL, in which
case it just calls alloc. This function is essentially a safe wrapper around the
standard C function realloc, except that it operates only on blocks produced from
alloc.
The debug memory function activates or deactivates a memory debugger, which
replaces internal calls to malloc, realloc and free with calls to custom, portable,
debugging functions. Once activated by passing a non-zero integer to this func-
tion, debugging should not be deactivated, since this might cause blocks allocated
7.5. MEMORY MANAGEMENT 113
using a custom allocator to be deallocated using the standard free function, which
would cause a crash. The option to switch off memory debugging (by passing zero
to debug memory) exists because there are certain controlled conditions where it
might be possible to do this.
The memory used function attempts to report how many bytes are currently allo-
cated by these memory functions. This will only be correct if the debugging mode
was activated before any other library functions were called. If the debugging
mode was never activated, the function reports how many bytes have been allo-
cated through alloc only, ignoring changes made through calls to free or realloc.
114 CHAPTER 7. MISCELLANEOUS
FUNCTIONS
NOTES
The regex match function performs a regular expression match on the given ex-
pression regexp and text. It returns non-zero (true) if there is a match, or zero
(false) if there is no match. Both strings are assumed to be UTF-8 strings.
The regular expression language is very simple:
“a?bc” matches “a” followed by any one UTF-8 char, then “bc”
The only special characters are ’*’ and ’?’. There is currently no way to escape
these characters. The ’.’, ’’, ’$’, and ’ ’ characters all have no special meaning.
Strings must match at the start and end of the string unless the ’*’ character is
used at the start or end. This scheme is similar to a shell match, rather than grep.
7.7. RESOURCES 115
7.7 Resources
FUNCTIONS
NOTES
The open resource function attempts to locate a named resource within a given
file. A resource is a sub-file, associated with a file using the AddRes tool available
from the tools folder. This function returns an open file pointer, pointing to the
located resource.
The resource descriptor string can be a regular expression as described in the
section on regular expressions. The first matching resource will be used if there
are several matching names.
If the resource is found, the return value will be non-NULL, and the length pa-
rameter will point to the length in bytes of the resource (which will be zero or
greater). If the resource is not found, the return value will be NULL, and the
length parameter will point to zero.
The file pointer returned by this function will not signal EOF when the resource
has ended. Rather, the length parameter must be used, or the resource must have
a way of telling the program that it has ended. For example, an embedded image
file may contain a marker meaning “end of image”. For embedded text files, the
zero byte which separates one resource from the next may be the only other way
to determine this end point.
The file has resources function returns non-zero if the named file ends with the
byte sequence that specifies a resource file, otherwise it returns zero. The byte
sequence is " nApp Resource File Type 1 n".
116 CHAPTER 7. MISCELLANEOUS
FUNCTIONS
NOTES
The beep function beeps the speaker.
Currently there are no other functions for using sounds.
7.9. TIMER FUNCTIONS 117
OBJECTS
struct Timer {
App * app; /* associated App */
int milliseconds; /* interval between actions */
int remaining; /* time until action */
TimerAction action; /* user-defined action */
void * data; /* user-defined data */
int value; /* user-defined value */
};
FUNCTIONS
NOTES
The delay function suspends program operation for the required number of mil-
liseconds, returning the number of milliseconds elapsed. The return value
may be less than the required milliseconds if the delay was interrupted by an
external event. The timing of this function is likely to be inaccurate for very
small delays, since operating system activity and hardware interrupts are likely to
produce small variations in the actual delay time. This function should be used
sparingly.
The current time function returns the approximate time in milliseconds since the
program began. This number will overflow if the program is used continuously
for more than a month.
A Timer is an object which allows a program to have a function repeatedly called
at certain intervals. It is created by new timer, which is passed the number of
milliseconds which should elapse between calls to the given TimerFunction.
118 CHAPTER 7. MISCELLANEOUS
A TimerFunction is a user defined function which is called when the timer acti-
vates, and is passed a pointer to the Timer.
A timer can be stopped using del timer, which also deletes the timer’s data struc-
ture from memory.
Timers are inexact and not likely to occur more often than at one tenth of a second
intervals.
EXAMPLES
polygons.c
clock.c
moire.c