5.1. Using Windows   

Windows are what X is all about. Each window is a data structure stored in the X server. The server handles clipping to window boundaries and manages a display made up of multiple overlapping windows. In your applications, each window is identified by an ID of Window type (usually an unsigned 32 bit integer). This ID is then used when you wish to use the window to draw in, to display data, etc.

To create a single window application you would need to :


Connect to the server,
Check the environment,
Create the window,
Display the window,
at the end of the program you will need to destroy the window
(& close the display).

While we are talking about windows it should be highlighted that under X the screen and window origins are always the top left hand corner. The width and height of windows and their attributes (eg the border) is normally measured in pixels.

5.1.1. Connecting to the Server   

To make the initial connection to the server we use the function XOpenDisplay. We normally pass this function a NULL parameter to indicate that we wish to use the display specified in the DISPLAY environment variable, as in the example below.


Display *display;
display = XOpenDisplay ( (char *) NULL);

If all goes well it should return a pointer to a Display data structure. If a NULL is returned then the connection failed, and your program would normally quit with an error message to the user.

Once you have obtained a successful connection you will need to determine which screen you are connected to. The function used is the DefaultScreen function.


int screen;
screen = DefaultScreen(display);

5.1.2. Checking the Environment   

If we are writing our programs to be device independent then we can't make assumptions about what sort of screen hardware we will have to work with. In particular we could be using a mono or a colour screen and we could be drawing on a 27 inch InterPro workstation (1664 x 1248 pixels) or on an IBM PS/2 with a 12 inch VGA monitor (640 x 480 pixels).

How do we get round this problem? We ask the X server what the characteristics of the screen are. In particular there are three functions that our device independent program will always call.


DisplayWidth (display,screen)
DisplayHeight (display,screen)
DefaultDepth (display,screen)

In fact these are not really functions but are macros, but this doesn't make any difference to us.

There are two more functions (macros) that we may also call :
DisplayWidthMM (display, screen) DisplayHeightMM (display, screen)

These functions are provided to allow WYSIWYG applications to be written, but they should be used with extreme care. The problem is that most graphics cards can't tell what size screen is attached, eg. a PC won't know whether you have a 9 inch or a 14 inch VGA screen attached. Having mentioned these last two functions, we will ignore them for the rest of the course.