LINUX - XT PROGRAM EXAMPLE 3

This example adds to XT Example 2 by adding a background color to the window.

New concepts include:

XtWindow - Retrieves the window ID of a widget. (Used by X Library functions)
XtDisplay - Retrieves the X server connection ID for a widget.
DefaultColormap -Macro that retrieves the Default Colormap ID.
Colormap - Color map where cells are allocated.
XColor - Structure used to define a color. Each color value ranges from 0 - 65,535. (NOT 0-255)
XParseColor - Locates the closest colormap cell to the named color.
XAllocColor - Allocates a read-only colormap cell containing the closest RGB color supported by the hardware.
XSetWindowBackground - Changes the background color.
XClearWindow - Clears an entire window with the background color (Note for later - this does not generate an exposure event - i.e. does not cause a redraw of the window contents)

Go here to get some simplified X Window and X Toolkit Intrinsics Documentation on calls used in this example


Create a file named "demo_xt3.cpp" with the contents shown in the box (the changes from demo_xt2.cpp are highlighted):
#include <X11/IntrinsicP.h> // IntrinsicP.h has some faster macros than Intrinsic.h
#include <X11/ShellP.h> // Needed for Widget class definitions
#include <stdio.h> //fprintf, stderr
XtAppContext app;
Widget main_widget; //Main Window
Display *main_disp; //The Display pointer for the parent widget is stored here.
Window main_wind; //The Window for the parent widget is stored here.
//------------------------------------------------
// This function is called by a ButtonPress event in the main widget
// Button 1 is the loft mouse button, Button 3 is the right button,
// Button 2 is the middle button (or the left & right button pressed together)
// Button 3 quits the program
void EvProc(Widget w, XtPointer client_data, XEvent *event, char* CTmp)
{
if (event->xbutton.button == Button1) fprintf(stderr, "Button 1 Pressed\n");
if (event->xbutton.button == Button2) fprintf(stderr, "Button 2 Pressed\n");
if (event->xbutton.button == Button3)
{
fprintf(stderr, "Button 3 Pressed\n");
exit(0);
}
}
//------------------------------------------------
void main (int argc, char **argv)
{
short unsigned int xx,yy;
Colormap cmap;
XColor color;
//--- Create and initialize the top-level widget
main_widget = XtOpenApplication(&app, "My Application", NULL, 0, &argc, argv, NULL, applicationShellWidgetClass, NULL, 0);
//--- Make it the desired size
XtMakeResizeRequest(main_widget, 300, 100, &xx, &yy);
//--- Realize the main widget
XtRealizeWidget(main_widget);

//Save main widget Window & Display values
main_wind = XtWindow(main_widget);
main_disp = XtDisplay(main_widget);
//--- Load default colormap and allocate a color close to the desired background color
cmap = DefaultColormap(main_disp, DefaultScreen(main_disp));
if (XParseColor(main_disp, cmap, "dark_blue", &color) == 0) fprintf(stderr, "Bad Parse\n");
if (XAllocColor(main_disp, cmap, &color) == 0) fprintf(stderr, "Bad Color\n");
XSetWindowBackground(main_disp, main_wind, color.pixel);
//--- Add Mouse Button Event Handler
XtAddEventHandler(main_widget, ButtonPressMask, FALSE, EvProc, NULL);
//-----
XClearWindow(main_disp, main_wind);
XtAppMainLoop(app);

}

Change the Makefile file from Example 2 to use demo_xt3
To compile and link this program, type
make
This will compile and link demo_xt3.cpp into the executable file demo_xt3
Now type
demo_xt3 (you may have to enter the complete path)
and a window with a background color should now appear.