Next: Two Dimensional Graphics Up: Xlib and Motif Previous: Xlib Basics

Graphics Contexts

As just mentioned the GC is responsible for setting the properties of lines and basic (2D) shapes.

The draw.c program illustrates the setting of a variety of GC elements.

To create a GC use the (Xlib) function XCreateGC(). It takes 4 parameters:

The XGCValues structure contains elements like foreground, background, line_width, line_style etc. that we can set.

The mask has predefined values such as GCForeground, GCBackground, GCLineStyle .....

In draw.c we create a GC structure, gc, and set the foreground.

Xlib provides two macros BlackPixel() and WhitePixel() which will find the default black and white pixel values for a given Display and Screen if the default colourmaps are installed.

So to set foreground:

gcv.foreground = BlackPixel(display, screen); gc = XCreateGC(display, screen, GCForeground, &gcv);

where gcv is a XCGValues structure and gc a GC structure.

To set both back and foreground do:

gcv.foreground = BlackPixel(display, screen); gcv.background = WhitePixel(display, screen); gc = XCreateGC(display, screen, GCForeground | GCBackground, &gcv);

Note the use of the | (OR) in the mask parameter that allows both the values to be set in the XGCValues structure.

There is an alternative way to change GC elements.

Xlib provides convenience functions to set appropriate GC values.

For example: XSetForeground(), XSetBackground(), XSetLineAttributes()

These set GC values for a given display and gc.

Examples of their use are in the draw.c program.
Tue May 24 16:52:56 BST 1994