#include #include #include #define FontName\ "-Adobe-New Century Schoolbook-Medium-R-Normal--*-140-*-*-P-*-ISO8859-1" #define WindowName "Sample Xlib Program" Display *dpy; Window window1,window2; GC gc; Screen *screen; int state = 0; char *message[]= { "Click here to exit", "Click HERE to exit!" }; static void doInitialize( ); static int doDefineColor( ); static void doCreateWindows( ); static void doCreateGraphicsContext( ); static void doLoadFont( ); static void doExpose( ); static void doMapWindows( ); static void doHandleEvents( ); static void doButtonPress( ); /********************** The main program *******************************/ static int main() { doInitialize( ); doHandleEvents( ); } /***************** doInitialize **************************/ static void doInitialize( ) { dpy = XOpenDisplay(0); if (!dpy){ printf("Display not opened!\n"); exit(-1); } screen = XDefaultScreenOfDisplay(dpy); XSynchronize(dpy, 1); doCreateWindows( ); doCreateGraphicsContext( ); doLoadFont( ); doMapWindows( ); } /******* doCreateWindows *********/ static void doCreateWindows( ) { int window1W = 400; int window1H = 300; int window1X = (XWidthOfScreen(screen)-window1W)>>1; int window1Y = (XHeightOfScreen(screen)-window1H)>>1; int window2X = 50; int window2Y = 75; int window2W = 300; int window2H = 150; XSetWindowAttributes xswa; /* Create the window1 window */ xswa.event_mask = ExposureMask | ButtonPressMask; xswa.background_pixel = doDefineColor(1); window1 = XCreateWindow(dpy, XRootWindowOfScreen(screen), window1X, window1Y, window1W, window1H, 0, XDefaultDepthOfScreen(screen), InputOutput, XDefaultVisualOfScreen(screen), CWEventMask | CWBackPixel, &xswa); XStoreName(dpy, window1, WindowName); /* Create the window2 window */ xswa.event_mask = ExposureMask | ButtonPressMask; xswa.background_pixel = doDefineColor(2); window2 = XCreateWindow(dpy, window1, window2X, window2Y, window2W, window2H, 4, XDefaultDepthOfScreen(screen), InputOutput, XDefaultVisualOfScreen(screen), CWEventMask | CWBackPixel, &xswa); } /******** Create the graphics context *********/ static void doCreateGraphicsContext( ) { XGCValues xgcv; xgcv.foreground = doDefineColor(3); xgcv.background = doDefineColor(2); gc = XCreateGC(dpy, window2, GCForeground | GCBackground, &xgcv); } /******* Load the font for text writing ******/ static void doLoadFont( ) { Font font; font = XLoadFont(dpy, FontName); XSetFont(dpy, gc, font); } /******* Create color ************************/ static int doDefineColor(n) int n; { int pixel; XColor exact_color,screen_color; char *colors[] = { "dark slate blue", "light grey", "firebrick" }; if ((XDefaultVisualOfScreen(screen))->class == TrueColor || (XDefaultVisualOfScreen(screen))->class == PseudoColor || (XDefaultVisualOfScreen(screen))->class == DirectColor || (XDefaultVisualOfScreen(screen))->class == StaticColor) if (XAllocNamedColor(dpy, XDefaultColormapOfScreen(screen), colors[n-1], &screen_color, &exact_color)) return screen_color.pixel; else printf("Color not allocated!"); else switch (n) { case 1: return XBlackPixelOfScreen(screen); break; case 2: return XWhitePixelOfScreen(screen); break; case 3: return XBlackPixelOfScreen(screen); break; } } /******** doMapWindows ***********/ static void doMapWindows( ) { XMapWindow(dpy, window1); XMapWindow(dpy, window2); } /****************** doHandleEvents ***********************/ static void doHandleEvents( ) { XEvent event; for ( ; ; ) { XNextEvent(dpy, &event); switch (event.type) { case Expose: doExpose(&event); break; case ButtonPress: doButtonPress(&event); break; } } } /***** Write the message in the window *****/ static void doExpose(eventP) XEvent *eventP; { /* If this is an expose event on our child window, then write the text. */ if (eventP->xexpose.window != window2) return; XClearWindow(dpy, window2); XDrawImageString(dpy, window2, gc, 75, 75, message[state], strlen(message[state])); } /***************** doShutdown ***************************/ static void doButtonPress(eventP) XEvent *eventP; { if (eventP->xexpose.window != window2) { state = 1; XDrawImageString(dpy, window2, gc, 75, 75, message[state], strlen(message[state])); return; } /* Unmap and destroy windows */ XUnmapWindow(dpy, window1); XDestroyWindow(dpy, window1); XCloseDisplay(dpy); sys$exit (1); }