5.2 Switching Between Graphics Contexts

One purpose of the GC is to store information about how to interpret graphics requests so that the same information does not have to be sent with every request. Another useful feature of the GC concept is that you can create several GCs with the different characteristics you need and then switch between them. Example 5-4 demonstrates how this is done. It creates two slightly different GCs with swapped foreground and background pixel values.

Example 5-4. Example of switching graphics contexts

GC gc1, gc2;
XGCValues values;
unsigned long valuemask;
    .
    .
    .
/* Open display, create window, etc. */
values.foreground = BlackPixel(display,screen_num);
values.background = WhitePixel(display,screen_num);

gc1 = XCreateGC(display, RootWindow(display, screen_num),
    (GCForeground | GCBackground), &values);

values.foreground = WhitePixel(display,screen_num);
values.background = BlackPixel(display,screen_num);

gc2 = XCreateGC(display, RootWindow(display, screen_num),
    (GCForeground | GCBackground), &values);

/* Now you can use either gc in drawing routines, thereby
 * quickly swapping the foreground and background colors */

Whether it is faster to switch between GCs or to modify a few values of a single GC depends on the particular server implementation. On some types of display hardware, several or many GCs can be cached. On these servers, it is faster to switch between GCs than to change members of them. On servers that do not cache or that cache only one GC, ...

Get XLIB Programming Manual, Rel. 5, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.