/* Demonstrate Locator Class Input with Pointer device
 *
 * Copyright 1989 Chromatics. Inc. Release 1.3 SCCS 1.5 (89/05/08)
 *
 * CR0495 89/04/12 (rfc) Release 1.3D, check transform
 * CR0495 89/01/15 (rfc) Release 1.3
 */

#include <gks/gks.h>

#define WS 1
#define WHITE 7
#define BLACK 0
#define RED 4
#define YELLOW 6
#define GREEN 2
#define XYDEV 2 /* must be 2 so that defaults can be used */
#define LOCPET 4 /* rubber band */
#define BREAK 1

static char copyid[] = "@(#)Copyright 1989 Chromatics. Inc.";
static char sccsid[] = "@(#)Release 1.3 SCCS 1.5 (89/05/08)";

extern char *cltos();

main()
{
	Gloc    init;
	Glimit  area;
	Glocrec locrec;
	Gqloc   resp;
	Glimit  window;
	Gloc    loc;
	Gevent  event;
	Gchoice choice;

	Gpoint pt[1];
	Gchar str[30];
	Gtxfp	txfp;
	int i;

	gopengks(NULL, 0);
	openws(GCXHD1,"Demonstrate Locator Class Input");

	init.transform = 0;
	init.position.x = 0.5;
	init.position.y = 0.5;
	area.xmin = 0.0;
	area.xmax = 1.0;
	area.ymin = 0.0;
	area.ymax = 1.0;
	locrec.locpet4_datarec.acf = GCURRENT;
	locrec.locpet4_datarec.ln.type = GINDIVIDUAL;
	locrec.locpet4_datarec.ln.width = GINDIVIDUAL;
	locrec.locpet4_datarec.ln.colour = GINDIVIDUAL;
	locrec.locpet4_datarec.ln.line = 1;
	locrec.locpet4_datarec.ln.bundl.type = GLN_SOLID;
	locrec.locpet4_datarec.ln.bundl.width = 1.0;
	locrec.locpet4_datarec.ln.bundl.colour = YELLOW;

	printf("This demo requires a pointer device defined as xydev1\n");

	ginitloc(WS, XYDEV, &init, LOCPET, &area, &locrec);

	if(gsetlocmode(WS, XYDEV, GSAMPLE, GECHO))
		{
		printf("No pointer device defined - abort demo\n");
		printf("Use menu or tstdpd program to define device\n");
		exit(1);
		}

	gsetlocmode(WS, XYDEV, GREQUEST, GECHO);

	printf("Using Rubberband cursor\n");
	printf("Locator class input using Request mode.\n");
	printf("Enter input locations with TRIGGER.\n");
	printf("Use BREAK to end demo\n");

	/* Use targets in WC with 0,0 as viewport center */
	gactivatews(WS);
	txfp.font = 3;
	txfp.prec = GP_STRING;
	gsettextfontprec(&txfp);
	window.xmin = -0.5;
	window.xmax = 0.5;
	window.ymin = -0.5;
	window.ymax = 0.5;
	gsetwindow(1, &window);
	gselntran(1);
	gsetviewportinputpri(1, 0, GHIGHER);

	/* draw targets */
	gsetmarkersize(3.0);
	gcreateseg(1);
	pt[0].x = pt[0].y = 0.0;
	gpolymarker(1,pt);
	sprintf(str," (%.3f,%.3f)", pt[0].x, pt[0].y);
	gtext(pt,str);
	for(i = 0; i < 4; i++ )
		{
		pt[0].x = 0.25 - 0.5 * (i/2);
		pt[0].y = 0.25 - 0.5 * (i%2);
		sprintf(str," (%.3f,%.3f)", pt[0].x, pt[0].y);
		gpolymarker(1,pt);
		gtext(pt,str);
		}
	gcloseseg();

	gsetmarkercolourind(GREEN);
	gsetmarkertype(GMK_X);

	for (;;)
		{
		greqloc(WS, XYDEV, &resp);
		if (resp.status == GNONE)
			{
			printf("*** BREAK ENTERED, REQUEST SECTION FINISHED ***\n");
			break;
			}
		else
			printf("Transform=%d, x=%f, y=%f\n", resp.loc.transform,
			       resp.loc.position.x, resp.loc.position.y);

			if(resp.loc.transform != 1)
				{
				printf("Out of Viewport!\n");
				continue;
				}

			gpolymarker(1,&resp.loc.position);
			init.transform = resp.loc.transform;
			init.position.x = resp.loc.position.x;
			init.position.y = resp.loc.position.y;
			ginitloc(WS, XYDEV, &init, LOCPET, &area, &locrec);
		}

	printf("Locator class input using Sample mode.\n");
	printf("Markers (WHITE or RED) will be drawn at sample\n");
	printf(" locations when Middle(WHITE) or Left(RED) is pressed\n");
	printf("Break button will end demo\n");

	init.transform = 0;
	init.position.x = 0.5;
	init.position.y = 0.5;

	ginitloc(WS, XYDEV, &init, LOCPET, &area, &locrec);
	gsetlocmode(WS, XYDEV, GSAMPLE, GECHO);
	gsetchoicemode(WS, XYDEV, GSAMPLE, GNOECHO);

	/* Wait for key ending previous section to be released */
		gsamplechoice(WS, XYDEV, &choice);
		while(choice.status != GC_NOCHOICE)
			gsamplechoice(WS, XYDEV, &choice);

	gsetmarkertype(GMK_X);
	for (;;)
		{
		gsamplechoice(WS, XYDEV, &choice);
		if (choice.status == GC_NOCHOICE)
			continue;

		if (choice.choice == BREAK)
			{
			printf("*** Sample mode finished ***\n");
			break;
			}

		gsampleloc(WS, XYDEV, &loc);

		if(choice.choice == 2)
			gsetmarkercolourind(WHITE);
		else
			gsetmarkercolourind(RED);

		if(loc.transform == 1)
			gpolymarker(1,&loc.position);

		}

	printf("Locator class input using Event mode.\n");
	printf("Enter 5 input locations with TRIGGER Button.\n");

	gsetlocmode(WS, XYDEV, GEVENT, GECHO);

	gsetmarkercolourind(GREEN);
	gsetmarkertype(GMK_X);
	for (i = 0; i < 5;)
		{
		gawaitevent(0.0, &event);
		if (event.class == GNCLASS)
			continue;

		printf("wkid=%d, class=%s, devno=%d\n", event.ws,
		       cltos(event.class), event.dev);
		if (event.class == GLOCATOR)
			{
			i++;
			ggetloc(&loc);
			printf("Transform=%d, x=%f, y=%f\n", loc.transform,
			       loc.position.x, loc.position.y);
			if(loc.transform == 1)
				gpolymarker(1,&loc.position);
			}
		else
			printf("Illegal event in this context\n");
		}

	gsetlocmode(WS, XYDEV, GREQUEST, GECHO);
	gsetchoicemode(WS, XYDEV, GREQUEST, GNOECHO);
	gemergencyclosegks();
}
