/*
 * A wcl shell for prototyping the plot widget.
 */

/*
 * $Id: test.c,v 1.1 91/05/20 15:17:46 gnb Exp $
 * $Source: /export/sources/x/At/Plotter/tmp/RCS/test.c,v $
 *
 * $Log:	test.c,v $
 * Revision 1.1  91/05/20  15:17:46  gnb
 * Initial revision
 * 
 * 
 */

#include <stdio.h>
#ifdef __STDC__
#include <stdlib.h>
#endif

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xaw/Toggle.h>

#include <X11/Wc/WcCreate.h>
#include <X11/Xp/Table.h>

#ifdef _AtDevelopment_
#include "Plotter.h"
#include "LinePlot.h"
#else
#include <At/Plotter.h>
#include <At/LinePlot.h>
#endif

extern void AriRegisterAthena P((XtAppContext));
extern void WcRegisterAtPlotter P((XtAppContext));

extern char *weekday_to_str();

char *progname;

/*
 * Some macros for playing with callbacks....
 */
#ifdef __STDC__
#define DEFINE_CB(name) void name (Widget w, caddr_t client_data, \
				  caddr_t widget_data)
#define REGISTER_CB(name) WcRegisterCallback(app, #name, name)
#else
#define DEFINE_CB(name) void name P((Widget, caddr_t, caddr_t)); \
     void name (w, client_data, widget_data) \
     Widget w; \
     caddr_t client_data, widget_data; 
#define REGISTER_CB(name) WcRegisterCallback(app, "name", name)
#endif

/*
 * These are the main graph widgets we attatch
 */
static Widget frameWidget;
static TableWidget tableWidget;
static AtPlotterWidget plotWidget;
/* Are actually subclasses of AtPlotWidget */
static LinePlotWidget aWidget, bWidget;

/*
 * Make and install a date label array....
 */
/*
 * Some test data
 */
#define NUM 40

struct data {
     double a;
     float b;
     char lbl[12];
     char *ptr;
};

static struct data array[NUM];

static void make_data()
{
     int i;
     double a = 1000, b = 1020;
     int day = 8, month = 4;	/* Start on monday 8/4 */
     
     for (i = 0; i < NUM; i++) {
	  array[i].a = a *= (double)(rand() & 0xffffff) / 0xffffff * .1 + .95;
	  array[i].b = b *= (double)(rand() & 0xffffff) / 0xffffff * .1 + .95;

	  sprintf(array[i].lbl, "%02d/%02d/91", day, month);

	  if (i % 10 == 0) /* once per fortnight */
	       array[i].ptr = array[i].lbl;
	  else
	       array[i].ptr = NULL;
	  
	  day++; 
	  if (i % 5 == 4)
	       /* skip weekend */
	       day += 2;
	  if (day > 30) {
	       day -= 30;
	       month++;
	  }
	  
     }
     AtAxisDateLabels(AtPlotterGetXAxis(plotWidget), &array[0].ptr, 
		      sizeof (struct data), 0, NUM);
     LinePlotAttatchData(aWidget, (XtPointer)&array[0].a, AtDouble, 
			  sizeof (struct data), 0, NUM);
     LinePlotAttatchData(bWidget, (XtPointer)&array[0].b, AtFloat, 
			  sizeof (struct data), 0, NUM);
     
}

/*
 * These callbacks are called at the graph element creation time. They
 * store the actual widget in the var described above, and in the case
 * of graph wigets attatch data to them.
 */
extern int strcasecmp P((const char *, const char *));

DEFINE_CB(AttatchDataCB)
{
#ifdef DEBUG
     fprintf(stderr, "%s: AttatchDataCB called for %s\n", progname,
	     client_data);
#endif 
     if (strcasecmp(client_data, "a") == 0) {
	  aWidget = (LinePlotWidget)w;
     } else if (strcasecmp(client_data, "b") == 0) {
	  bWidget = (LinePlotWidget)w;
	  make_data(); 
     } else if (strcasecmp(client_data, "plot") == 0) {
	  plotWidget = (AtPlotterWidget)w;
     } else if (strcasecmp(client_data, "table") == 0) {
	  tableWidget = (TableWidget)w;
	  frameWidget = XtParent(w); 
     } else {
	  fprintf(stderr, "%s: AttatchDataCB has no data for %s\n",
		  progname, client_data);
	  return;
     }
}



DEFINE_CB(PrintGraphCB)
{
     AtPlotterGeneratePostscript("ps.out", (AtPlotterWidget)plotWidget, 
				 "All Ords Plot", 90, 100, 700, 539, True);
     fprintf(stderr, "Plot dumped in ps.out\n");
}

static void RegisterAppCBs P((XtAppContext)); 
static void RegisterAppCBs(app)
XtAppContext app; 
{
     REGISTER_CB(AttatchDataCB);
     REGISTER_CB(PrintGraphCB);
}


int main(ac, av)
Cardinal ac;
char **av;
{
     XtAppContext app;
     Widget appShell;

     if (progname = rindex(av[0], '/')) av[0] = ++progname;
     else progname = av[0];

     appShell = XtAppInitialize(&app, "Test", NULL, 0, &ac, av,
				NULL, NULL, 0);

     AriRegisterAthena(app);

     WcRegisterAtPlotter(app);

     RegisterAppCBs(app);
     
     WcWidgetCreation(appShell);
     XtRealizeWidget(appShell);
     XtAppMainLoop(app);
     /*NOTREACHED*/
     exit(0); 
}
