/*
 example.c - simple X Window System application. Uses Display PostScript
 to draw an ochre box and uses X primitives to wait for a mouse click before
 terminating.
*/
#include <stdio.h>								/* Standard C library I/O routines */
#include <string.h>								/* Standard C library string routines */
#include <X11/X.h>								/* X definitions */
#include <X11/Xlib.h>								/* Interface to X library */
#include <X11/Intrinsic.h>								/* X toolkit definitions */
#include <X11/DPS/psops.h>								/* Interface to PostScript single-op wraps */
#include <X11/DPS/dpsXclient.h>								/* Interface to the DPS Client Library */
#include "examplewraps.h"								/* Interface to user-defined 
							"wrap" procedures created by the pswrap utility*/
/* Window geometry definitions */
#define XWINDOW_X_ORIGIN							100
#define XWINDOW_Y_ORIGIN							100
#define XWINDOW_WIDTH							500
#define XWINDOW_HEIGHT							500
void main(argc, argv)
 int argc;
 char *argv[];
{
 Display *dpy;						/* X display structure */
 int screen;						/* screen on display */
 DPSContext ctxt;						/* DPS drawing context */
 DPSContext txtCtxt;						/* DPS text context for debugging */
 Window xWindow;						/* window where drawing occurs */
 int blackPixel, whitePixel;
 int debug = { FALSE };
 GC gc;
 XSetWindowAttributes attributes;
 unsigned long mask;
 DPSSpace space;
 float x, y, width, height;
 /* Connect to the window server by opening the display. Most of command
 line is parsed by XtOpenDisplay, leaving any options not recognized by
 the X toolkit; look for local -debug switch */
 XtToolkitInitialize();
 dpy = XtOpenDisplay(NULL, (String) NULL, "example", "example",
 		(XrmOptionDescRec *) NULL, 0, &argc, argv);
 screen = DefaultScreen(dpy);
 if (argc == 2)
 	if (strcmp(argv[1], "-debug") == 0)
 		debug = TRUE;
 	else {
 		printf("Usage: example [-display xx:0] [-sync] [-debug]\n");
 exit(1);
 }
 /* Create a window to draw in; register interest in mouse button events. */
 blackPixel = BlackPixel(dpy, screen);
 whitePixel = WhitePixel(dpy, screen);
 attributes.background_pixel = whitePixel;
 attributes.border_pixel = blackPixel;
 attributes.bit_gravity = SouthWestGravity;
 attributes.event_mask = ButtonPressMask | ButtonReleaseMask;
 mask = CWBackPixel | CWBorderPixel | CWBitGravity | CWEventMask;
 xWindow = XCreateWindow(dpy, DefaultRootWindow(dpy),
 XWINDOW_X_ORIGIN, XWINDOW_Y_ORIGIN, XWINDOW_WIDTH, XWINDOW_HEIGHT,
 	1, CopyFromParent, InputOutput, CopyFromParent, mask, &attributes);
 XMapWindow(dpy, xWindow);
 gc = XCreateGC(dpy, RootWindow(dpy, screen), 0, NULL);
 XSetForeground(dpy, gc, blackPixel);
 XSetBackground(dpy, gc, whitePixel);
 /* Create a DPS context to draw in the window we just created. If the
 user has asked for debugging, create a text context chained to the
 `drawing' context. */
 ctxt = XDPSCreateSimpleContext(dpy, xWindow, gc, 0, XWINDOW_HEIGHT,
 DPSDefaultTextBackstop, DPSDefaultErrorProc, NULL);
 if (ctxt == NULL) {
 	fprintf(stderr, "Error attempting to create DPS context\n");
 	exit(1);
 }
 DPSSetContext(ctxt);
 if (debug) {
 	txtCtxt = 
		DPSCreateTextContext(DPSDefaultTextBackstop,DPSDefaultErrorProc);
 	DPSChainContext(ctxt, txtCtxt);
 }
 /* Convert the X Window System coordinates at the lower right corner
 of the window to get the width and height in user space. */
 PSitransform(
 	(float) XWINDOW_WIDTH,
 	(float) -XWINDOW_HEIGHT,
 	&width,
 	&height);
 /* Locate the box in the middle of the window. */
 x = width / 4.0;
 y = height / 4.0;
 /* Paint an ochre box. */
 PSWDrawBox(0.77, 058, 0.02, x, y, width / 2.0, height / 2.0);
 /* Wait for a mouse click on any button then terminate */
 while (NextEvent() != ButtonPress);
 while (NextEvent() != ButtonRelease);
 space = DPSSpaceFromContext(ctxt);
 DPSDestroySpace(space);
 exit(0);
} /* main */
int NextEvent()
{
 XEvent			event;
 XtNextEvent(&event);
 return(event.type);
}
