Software and eBooks for your Palm-powered PDA.

Google

The X Windows Application Programming Interface

For most applications, the X Windows subroutines are separated into at least three layers.

The lowest layer of the X Windows package contains those functions that are part of the X11 library (libX11.a or libX11.sh on most UNIX machines). The functions in this library are the implementation of the X protocols for communication between the X client and an X server. Rarely are the functions at this level directly called by application programs. All application programs must be linked to this library to support subroutines from the higher level libraries.

The next higher layer of an X Windows application is the X Toolkit. On most UNIX machines, this library of functions will be called "libXt.a" or "libXt.sh". The X Toolkit, (also called Xt or toolkit for short) contains subroutines that initialize an application and that allow the organization of screen objects into classes of widgets.

Many applications can, and are, written using only the X11 and Xt libraries, but in most cases developers will choose to use a standard set of widgets to incorporate a standardized "look and feel" to the user interface. These routines that embody the look and feel of an application can be grouped into a separate group of functions for creation of individual widgets and modification of their properties.

There are several standard widget sets available to application developers. The three most widely known are Athena, Open Look, and Motif.

An Example Program

Here is an example of an X Windows version of the "Hello World" program written for the Motif widget set:


01:    #include < Xm/Xm.h >
02:    #include < Xm/Label.h >
03:
04:    XtAppContext context;
05:    XmStringCharSet char_set=XmSTRING_DEFAULT_CHARSET;
06:
07:    main(argc, argv)
08:    int argc;
09:    char *argv[];
10:    {
11:        Widget toplevel, label;
12:        XmString S;
14:
15:        /* Create an application shell. */
16:        toplevel = XtAppInitialize(&context,"",NULL,0,&argc,argv,NULL,
17:            NULL,0);
18:
19:        /* Create the Label widget. */
20:        label = XmCreateLabel(toplevel,"label",NULL,0);
21:        S = XmStringCreateLtoR("Hello World!", char_set);
22:        XtVaSetValues(label,
23:            XmNlabelString, S,
24:            NULL);
25:
26:        XtRealizeWidget(toplevel);
27:        XtAppMainLoop(context);
28:    }


The line numbers that appear here are not part of the original program, but have been inserted only for reference.

LINES 1-2

These lines retrieve the definitions of data structures and macros that will be used in the rest of the program. Both Xm.h and Label.h are part of the Motif package. Xm.h contains include statements that will pull in the lower level definitions that are necessary for the Xt and X11 libraries (including stdio.h). On most systems it is not really necessary to include Xm.h, since each of the widget definition headers (such as Label.h) contains a statement to include Xm.h.

LINE 4

context is declared to be a variable of type XtAppContext, or an application context. The application context may be thought of as the parent of all widgets in the application, since it establishes a base for inheritance for some properties of all widgets. This structure will be initialized later in the program.

LINE 5

This line declares that the character set for this program is the default character set for Motif programs.

LINE 12

The variable S is declared to be a Motif string. Motif strings are different from C strings (or character arrays) in that they have additional properties such as a character set and a text direction.

LINE 16

This line initializes communications between the application program and the X server, and also creates the base window for the application. The size of the base window will automatically be sized to fit the contents.

LINES 20-24

Line 20 creates a Label widget. In line 21, a static array of characters is converted to a Motif string, and in lines 22-24, the Motif string is assigned to the XmNlabelString property of the label struct.

LINE 26

The function XtRealizeWidget forces construction of the graphic image of the widget that is specified in the call and all widgets that inherit from the specified widget. This causes the application to appear on the display screen. It is at this point that a window appears on the screen containing the words "Hello World!".

LINE 27

The function XtAppMainLoop is the main event handling of the X toolkit. After this function is called, the program waits for an event, such as a keypress or button click, to occur. Since no widgets were created for user interaction in this program, the only thing that the user can do at this point is to close the application window. The precise method of closing an application window depends on which window manager is in use.



Rique's Tutorials X Windows Desktop X Windows
Rique's Navbar