5.2. Creating the Window   

The X library provides two window creation functions, XCreateSimpleWindow and XCreateWindow. Rather than using two different functions depending on what degree of control is desired, this course (and most references) will just use the more general XCreateWindow function. The syntax is given below.

     #include <X11/Xlib.h>

Display *display;
Window parent_window;
int x, y;
unsigned int width, height;
unsigned int border_width;
int depth;
unsigned int class;
Visual *visual;
unsigned long valuemask;
XSetWindowAttributes *attributes;
window = XCreateWindow (display, parent_window, x, y, width,
height, border_width, depth, class,
valuemask, attributes );

The function call will return the window identifier (or the constant None, if the call failed). The first parameter is the target display, as obtained from XOpenDisplay. The second parameter is the parent's id. Note that all windows are always clipped by their parent's boundaries. If this is the first window that we are creating then the parent window is the root window, and there is a macro, RootWindow, that will give us this window id.

The x and y parameters give the desired pixel location of the top left corner of this new window. However, the window manager has the option of arbitrarily changing where the window will go so view this as a request rather than an absolute value. Width and height are the size of the window in pixels. The border_width is the number of pixels wide that the window's border should be. If you are creating a child of the root window, the window manager may override the border width. In particular, on the Personal Iris the border has been known to disappear.

Depth is the number of bit planes available. This value may have been obtained from the DefaultDepth macro, or you can use the constant CopyFromParent. Class will take the form of a constant, either InputOutput, InputOnly or CopyFromParent. Most windows of course will be of the InputOutput class. The visual describes an abstract model for graphics hardware capabilities. Normally we will play safe and use CopyFromParent.

Valuemask and attributes are related. The attributes structure contains 15 fields related to the window. The valuemask variable is a bit mask, indicating which of the 15 fields you have actually specified. This allows us to specify only those attributes that we are interested in. The actual attributes structure is defined below.

     typedef struct {

Pixmap background_pixmap;
unsigned long background_pixel;
Pixmap border_pixmap;
unsigned long border_pixel;
int bit_gravity;
int win_gravity;
int backing_store;
unsigned long backing_planes;
unsigned long backing_pixel;
Bool save_under;
long event_mask;
long do_not_propagate_mask;
Bool override_redirect;
Colormap colormap;
Cursor cursor;
} XSetWindowAttributes;

We will only look at four of these attributes.

background_pixel - the colour of the window background
border_pixel - the colour of the window border
event_mask - ask for window expose events.
This gets round a potential problem.
override_redirect - allows you to override the window manager.
To do so is normally considered unfriendly, and should
only be used for short pop up screens or menus.

In practise we are likely to set these attributes using code such as that below.

     #include <X11/Xlib.h>

#include <X11/Xutil.h>
Display *display;
int screen;
XSetWindowAttributes attributes;
unsigned long attr_mask;
attributes.background_pixel = WhitePixel(display, screen);
attributes.border_pixel = BlackPixel(display, screen);
attributes.event_mask = ExposureMask;
attributes.override_redirect = False;
attr_mask = CWBackPixel | CWBorderPixel | CWEventMask | CWOverrideRedirect;