/*
 * Copyright 1994 Loganville Technical Resources, Incorporated
 *
 *  standard GNU copyleft legalese mumbo-jumbo applies. IN SHORT WE (LTR) 
 *  ARE NOT RESPONSIBLE FOR ANYTHING IN REGARDS TO THIS PROGRAM. USE
 *  IT AT YOUR OWN PERIL.  DISTRIBUTE IT AROUND FREELY.
 *
 * 			Zmans Tachometer Widget Test Program
 *
 *
 */
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Core.h>
#include <X11/Xaw/Form.h>

#include "Ztach.h"

#include <stdio.h>
#include <math.h>

#define APPNAME "ZtachTest"
#define APPCLASS "ZtachTest"

Widget	ztach2;

static void QuitIt(_w, _nil, _event)
Widget _w;
caddr_t _nil;
XEvent *_event;
{
  	if (_event->type == ButtonPress)
    		if (((XButtonEvent *)_event)->button == 3)
      			exit(1);
}



static void DestroyIt(_w, _nil, _event)
Widget _w;
caddr_t _nil;
XEvent *_event;
{
  	if (_event->type == ButtonPress)
    		if (((XButtonEvent *)_event)->button == 3)
      			XtDestroyWidget(_w);
}



void ZTMonitor(w, closure, call_data)
Widget  w;
caddr_t closure;        /* unused */
ZtachCallbackPtr call_data;      /* returned callback struct */
{
        Arg     args[3];   /* maybe add Bool forceColumns XtNforceColumns? */
        int     n;
        int	value;
        
        value = call_data->value;
	n = 0;
        XtSetArg(args[n], XtNvalue, value); n++;
        XtSetValues(ztach2, args, n);
}                                                 



main(argc, argv)
int argc;
char *argv[];
{
  	Widget ztach1, ztach3, ztach_form, toplevel;
  	XtAppContext app;
  	Display *theDisplay;
	Arg args[10];
	register int n;
	char	*master = "Master";
	char	*slave = "Slave";
	int	yes = 1;
	int	no = 0;     


  	XtToolkitInitialize();
  	app = XtCreateApplicationContext();
	
  	theDisplay = XtOpenDisplay (app, NULL, APPNAME, APPCLASS, 
    							NULL, 0, &argc, argv);

  	if (!theDisplay)
  	       {
    		printf("%s: can't open display, exiting...", APPNAME);
    		exit (0);
               }

  	toplevel = XtAppCreateShell (APPNAME, APPCLASS,
			applicationShellWidgetClass, theDisplay, NULL, 0);

  	ztach_form = XtCreateManagedWidget ("ZtachForm", formWidgetClass,
                                                      toplevel, NULL, 0);
	
  	ztach1 = XtCreateManagedWidget("ZtachWidget1", ztachWidgetClass,
    							ztach_form, NULL, 0);
    	n = 0;
 	XtSetArg (args[n], XtNulabel, master); n++;
 	XtSetArg (args[n], XtNdrawMarkers, no); n++; 	
        XtSetValues (ztach1, args, n); 						

	XtAddCallback(ztach1, XtNvalueChangeCallback, ZTMonitor, NULL);
	XtAddCallback(ztach1, XtNincrementCallback, ZTMonitor, NULL);
	XtAddCallback(ztach1, XtNdecrementCallback, ZTMonitor, NULL);
  	ztach2 = XtCreateManagedWidget("ZtachWidget2", ztachWidgetClass,
    							ztach_form, NULL, 0);
	n = 0;
 	XtSetArg (args[n], XtNfromHoriz, ztach1); n++;
 	XtSetArg (args[n], XtNulabel, slave); n++;
 	XtSetArg (args[n], XtNrotateCW, no); n++;
 	XtSetArg (args[n], XtNthickBits, no); n++; 	
        XtSetValues (ztach2, args, n);         

	XtUninstallTranslations(ztach2);
	
  	ztach3 = XtCreateManagedWidget("ZtachWidget3", ztachWidgetClass,
    							ztach_form, NULL, 0);
	n = 0;
 	XtSetArg (args[n], XtNfromHoriz, ztach2); n++;
 	XtSetArg (args[n], XtNthickBits, yes); n++;
 	XtSetArg (args[n], XtNdrawTachArm, no); n++;
 	XtSetArg (args[n], XtNrotateCW, no); n++;
 	XtSetArg (args[n], XtNdrawArc, no); n++;
 	XtSetArg (args[n], XtNdrawMarkers, no); n++;
 	XtSetArg (args[n], XtNdrawAhead, yes); n++; 	 	 	 	
 	XtSetArg (args[n], XtNdrawHole, no); n++; 	 	 	 	
 	XtSetArg (args[n], XtNdrawNumstring, no); n++; 	 	 	 	
        XtSetValues (ztach3, args, n);
  	
  	XtRealizeWidget(toplevel);

  	XtAddEventHandler(ztach2 , ButtonPressMask, FALSE, QuitIt, NULL);
  	XtAddEventHandler(ztach3 , ButtonPressMask, FALSE, DestroyIt, NULL);  	

  	printf("Press mouse button 3 on the slave tach to exit.\n");

  	XtAppMainLoop(app);
}
