

#include <stdio.h>
#include <X11/Intrinsic.h>
#include "VisDrawA.h"


static void
da_cb( w, data, cbs )
    Widget w;
    XtPointer data;
    XmDrawingAreaCallbackStruct *cbs;
{
    static Position x, y;
    static GC gc = NULL;
    static Pixel fg, top, bot, hl;

    XEvent *e = cbs->event;

    if (gc == NULL && XtWindow(w) != (Window)0) {
       XGCValues gcv;
       XtVaGetValues (w,
           XmNforeground, &fg,
           XmNtopShadowColor, &top,
           XmNbottomShadowColor, &bot,
           XmNhighlightColor, &hl,
           NULL);
       gcv.foreground = fg;
       gc = XCreateGC (XtDisplay(w), XtWindow(w), GCForeground, &gcv);
    }

    switch (cbs->reason) {
       case XmCR_INPUT:
           printf ("da_cb input\n");
           if (e->xany.type == ButtonPress) {
               x = e->xbutton.x;
               y = e->xbutton.y;
           } else if (e->xany.type == ButtonRelease) {
               XSetForeground(XtDisplay(w), gc, fg);
               XDrawLine (XtDisplay(w), XtWindow(w), gc,
                          x, y, e->xbutton.x, e->xbutton.y);
           }
           break;
       case XmCR_EXPOSE:
           printf ("da_cb expose\n");
               XSetForeground(XtDisplay(w), gc, top);
               XFillRectangle(XtDisplay(w), XtWindow(w), gc, 10,10,20,20);
               XSetForeground(XtDisplay(w), gc, bot);
               XFillRectangle(XtDisplay(w), XtWindow(w), gc, 30,10,20,20);
               XSetForeground(XtDisplay(w), gc, hl);
               XFillRectangle(XtDisplay(w), XtWindow(w), gc, 50,10,20,20);
           break;
       case XmCR_RESIZE:
           printf ("da_cb resize\n");
           break;
    }
}


static void
oda_cb (w, data, cbs)
    Widget w;
    XtPointer data;
    XmDrawingAreaCallbackStruct *cbs;
{
    static Position x, y;
    static GC gc = NULL;
    static Pixel fg, top, bot, hl;

    XEvent *e = cbs->event;

    if (gc == NULL && XtWindow(w) != (Window)0) {
       XGCValues gcv;
       XtVaGetValues (w,
           XmNforeground, &fg,
           XmNtopShadowColor, &top,
           XmNbottomShadowColor, &bot,
           XmNhighlightColor, &hl,
           NULL);
       gcv.foreground = fg;
       gc = XCreateGC (XtDisplay(w), XtWindow(w), GCForeground, &gcv);
    }

    switch (cbs->reason) {
       case XmCR_INPUT:
           printf ("oda_cb input\n");
           if (e->xany.type == ButtonPress) {
               x = e->xbutton.x;
               y = e->xbutton.y;
           } else if (e->xany.type == ButtonRelease) {
               XSetForeground(XtDisplay(w), gc, fg);
               XDrawLine (XtDisplay(w), XtWindow(w), gc,
                          x, y, e->xbutton.x, e->xbutton.y);
           }
           break;
       case XmCR_EXPOSE:
           printf ("oda_cb expose\n");
               XSetForeground(XtDisplay(w), gc, top);
               XFillRectangle(XtDisplay(w), XtWindow(w), gc, 0,0,20,20);
               XSetForeground(XtDisplay(w), gc, bot);
               XFillRectangle(XtDisplay(w), XtWindow(w), gc, 20,0,20,20);
               XSetForeground(XtDisplay(w), gc, hl);
               XFillRectangle(XtDisplay(w), XtWindow(w), gc, 40,0,20,20);
           break;
       case XmCR_RESIZE:
           printf ("oda_cb resize\n");
           break;
    }
}


main( argc, argv )
    int argc;
    char *argv[];
{
    Widget cmap_list[3];
    Widget top, area, overlay;
    XtAppContext app;

    top = XtVaAppInitialize( &app, "Foo", NULL, 0, &argc, argv, NULL,
       XtNwidth, 200,
       XtNheight, 200,
       NULL );

    area = XtVaCreateManagedWidget( "area",
       xkVisualDrawingAreaWidgetClass, top,
       XtVaTypedArg, XtNvisual, XtRString, "PseudoColor Depth 8", 20,
       XtVaTypedArg, XtNbackground, XtRString, "Blue", 5,
       NULL );
    XtAddCallback( area, XmNinputCallback,  (XtCallbackProc) da_cb, NULL );
    XtAddCallback( area, XmNresizeCallback, (XtCallbackProc) da_cb, NULL );
    XtAddCallback( area, XmNexposeCallback, (XtCallbackProc) da_cb, NULL );

    overlay = XtVaCreateManagedWidget( "overlay",
       xkOverlayDrawingAreaWidgetClass, area,
       XtVaTypedArg, XtNbackground, XtRString, "Green", 6,
       NULL );
    XtAddCallback( overlay, XmNinputCallback,  (XtCallbackProc) oda_cb, NULL );
    XtAddCallback( overlay, XmNresizeCallback, (XtCallbackProc) oda_cb, NULL );
    XtAddCallback( overlay, XmNexposeCallback, (XtCallbackProc) oda_cb, NULL );

    XtRealizeWidget( top );

    /* Must set colormap windows after top is realized */
    cmap_list[0] = overlay;
    cmap_list[1] = area;
    cmap_list[2] = top;
    XtSetWMColormapWindows( top, cmap_list, 3 );

#ifndef NO_MWM_COLORMAP_HACK
    XFlush( XtDisplay(top) );
    sleep(5);

    cmap_list[0] = area;
    cmap_list[1] = overlay;
    cmap_list[2] = top;
    XtSetWMColormapWindows( top, cmap_list, 3 );
#endif

    XtAppMainLoop( app );
}

