#include <Xm/DrawingA.h>

XtAppContext app;

struct _res_value {
    String vclass;
    int depth;
} res_value;

XtResource resources[] = {
    { "visualClass", "VisualClass", XtRString, sizeof(String),
      XtOffsetOf(struct _res_value, vclass), XtRImmediate, NULL },
    { "visualDepth", "VisualDepth", XtRInt, sizeof(int),
      XtOffsetOf(struct _res_value, depth), XtRImmediate, NULL },
};

XrmOptionDescRec options[] =
{
    { "-visual", "*visualClass", XrmoptionSepArg, NULL },
    { "-depth", "*visualDepth", XrmoptionSepArg, NULL },
};

void
main(int ac, char *av[])
{
    Widget top, draw_area, dialog;
    Arg args[] = {
	{ XmNwidth, 200 },
	{ XmNheight, 200 },
    };
    Visual *visual;
    Colormap cmap;

    top = XtAppInitialize(&app, "Sample", options, XtNumber(options),
			  &ac, av, NULL, NULL, 0);

    XtGetApplicationResources(top, &res_value,
			  resources, XtNumber(resources), NULL, 0);
    if (res_value.depth)
	XtVaSetValues(top, XmNdepth, res_value.depth, NULL);
    if (res_value.vclass)
	XtVaSetValues(top,
		      XtVaTypedArg,
		      	  XmNvisual, XtRString,
		      	  res_value.vclass, strlen(res_value.vclass) + 1,
		      NULL);

    XtVaGetValues(top, XmNvisual, &visual, NULL);
    if (visual != CopyFromParent &&
	    visual != DefaultVisualOfScreen(XtScreen(top))) {
	cmap = XCreateColormap(XtDisplay(top),
			       RootWindowOfScreen(XtScreen(top)),
			       visual, AllocNone);
	XtVaSetValues(top,
		      XmNcolormap, cmap,
		      NULL);
    }

    draw_area = XmCreateDrawingArea(top, "draw_area", args, XtNumber(args));
    XtManageChild(draw_area);

    XtRealizeWidget(top);
    XtAppMainLoop(app);
}
