LINUX - XWindows PROGRAM EXAMPLE 1

9/5/97

This is a very simple minded program that demonstrates that you can write, compile, and link an XWindows C program in LINUX.

Create a file named "demo2.cpp" with the contents shown in the box:

#include < X11/Xlib.h >
#include < X11/Xutil.h >

main(int argc, char *argv[])
{
XSizeHints sizehints;
Display *display;
Window *window;
int screen;
Colormap cmap, orig_cmap;
XVisualInfo vTemplate, *visualList;
Visual *visual;
XSetWindowAttributes atts;
XEvent event;
int visualsMatched;
int done = 0;
display = XOpenDisplay("");
screen = DefaultScreen(display);
window=XDefaultRootWindow(display);
vTemplate.screen = screen;
vTemplate.depth = 8;
// vTemplate.class = PseudoColor;
visualList = XGetVisualInfo(display,
VisualScreenMask | VisualClassMask | VisualDepthMask,
&vTemplate, &visualsMatched);
if ( visualsMatched == 0)
{
puts("visual not found, using default");
visual = DefaultVisual(display, screen);
}
else
{
printf("found %d visuals\n", visualsMatched);
visual = visualList[0].visual;
}
sizehints.x = 200;
sizehints.y = 0;
sizehints.width = sizehints.height = 150;
sizehints.flags = USSize | USPosition;
cmap = XCreateColormap(display, window, visual, AllocAll);
orig_cmap = DefaultColormap(display, DefaultScreen(display));
atts.colormap=cmap;
window = XCreateWindow(display, DefaultRootWindow(display),
sizehints.x, sizehints.y, sizehints.width, sizehints.height,
// 200, 0, 150, 150,
0, 8, InputOutput, visual, CWColormap, &atts);
XSetStandardProperties(display, window, argv[0],
argv[0], None, argv, 1, &sizehints);
XFree((char *)visualList);
XSelectInput(display, window, ButtonPressMask);
XMapWindow(display, window);
do
{
XNextEvent(display, &event);
switch(event.type)
{
case ButtonPress:
printf ("****** Found ButtonPress event *****\n");
done = -1;
break;
}
} while (!done);
XCloseDisplay(display);
}

Now type
g++ -o demo2 demo2.cpp -L/usr/X11R6/lib -lX11
<-- -lxm -lXt --> This will compile and link demo2.cpp into the executable file demo2
Now type
demo1
and a window should now appear until you press a key
******* NOT QUITE !!! ******** - still being fixed