Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Xlib tutorial 1 - painting colors, graphics contexts and fonts




Preparing for painting


Concept Graphics context (GC): A ghraphics context is a structure that defines the attributes of a painting operation. It is kept on the server (to minimize traffic) and controls attributes like pen width, font type, filling style and the painting colors.

    XFontStruct *font = XLoadQueryFont( dpy, "fixed" );
    if( !font )
    {
        printf("Frekko: Error, couldn't load font\n" );
        exit( 1 );
    }

    XGCValues gv;
    gv.function = GXcopy;  // paint with src color only.
    gv.line_width = 3;
    gv.foreground = red.pixel; // foreground is the color reds pixel value.
    gv.background = red.pixel; // background is the color reds pixel value.
    gv.font = font->fid;

    GC gc = XCreateGC( dpy, root, GCFunction | GCLineWidth | GCForeground | GCBackground | GCFont, &gv );


Before we can start and paint on our newly created window we need to set up graphics context (hereafter called GC) as all painting operations take a GC as parameter. First off we start by loading a font. Fonts are a mess under X and is a possible target for another tutorial. We'll go easy and just load the "fixed" font which is ugly, but available on all systems. (We still check though. In case something is wrong crash with an error and not with a segfault when painting). The call to XLoadQueryFont returns a pointer to a XFontStruct which holds information about the font we just loaded.
Initializing a new GC is done through the XGCValues struct. The first attribute we set is the function attribute. This controls how the "paint" is applied to already existing "paint". GXcopy simply takes no account to what's already there and paints right over it. Another usefull function is GXinvert which inverts the existing paint (nice for the resize rubberband feature used in many window managers).
gv.line_width controls the width of any lines drawn, foreground and background control the colors drawn and are set to the pixel values of the colors we created and finaly the font is set to the freshly loaded font.
Lots of X calls take an attribute structure as parameter. Common for most of them is that you don't need to specify all attributes. This also holds for XCreateGC and we specify which elements to use by ORing the component mask bits together.

Painting galore


    // get the geometry of our window, the windowmanager might have changed it.
    XWindowAttributes attr;
    XGetWindowAttributes( dpy, win, &attr );
    int w = attr.width, h = attr.height;

    // Draw a big fat cross that covers the whole window.
    XDrawLine( dpy, win, gc, 0, 0, w, h );
    XDrawLine( dpy, win, gc, 0, h, w, 0 );

    const char *text = "Hello world";

    // fetch the height of the font, and the width of the text.
    int fontHeight = font->ascent + font->descent; // max height above and below baseline
    int textWidth = XTextWidth( font, text, strlen( text ) );
    
    // center the text at the top of the window
    XDrawString( dpy, win, gc, (w - textWidth)/2, fontHeight, text, strlen( text ) );
    
    XFlush( dpy );

One little detail remains before we start painting. We have to fetch the window size again. Why do we need to do this you may ask, we set the window size ourselves. This is easy to answer: the windowmanager may have (for any reason) decided to resize our window before showing it. We use the simplest method of obtaining window attributes, namely with the XWindowAttributes function which returns all the attributes in a nice structure.

Once everything is set up painting is actually pretty easy since only really primitive painting functions are available in Xlib. Note that all the painting functions take a GC as parameter and that the coordinates given are in window space. (0,0 is the topleft corner of the window that is beeing painted).

Finally we send an XFlush command. This is to ensure that the paint commands are actually sent to the server. This has to do with the client/server modell of X which will be explained further in tutorial2.

Attached files:


<< Previous page | 1 | 2 | < 3 > | 4 | Next page >> | Printer-friendly page |

Comment List


There are no comments.


Forgot your password?

Register a new user

Results

Polls