#include <X11/Intrinsic.h>
#include "Plotter.h"
#include "AxisP.h"
#include "XYPlot.h"
#include "XYErrorPlot.h"
#include "ContourPlot.h"
#include <stdio.h>

#define DATASIZE 21
#define CONTOURSIZE 25
double xdata[DATASIZE], ydata[DATASIZE], errabove[DATASIZE],
       errbelow[DATASIZE];

double zdata[] = {
  0.0, 0.0, 0.0, 0.0, 0.0,
  0.0, 0.0, 0.3, 0.0, 0.0,
  0.0, 0.3, 1.0, 0.3, 0.0,
  0.0, 0.0, 0.3, 0.0, 0.0,
  0.0, 0.0, 0.0, 0.0, 0.0,
};


void click(AtPlotterWidget w, caddr_t tag, caddr_t foo)

{
  AtPlotterGeneratePostscript("newplot.PS", w, "Title", 10, 10, 500, 500,
			      False);
  printf("print completed\n");
}

void Zoomin(AtPlotterWidget w, caddr_t tag, RectangleStruct rect)
{
    double xmin, xmax;
    double ymin, ymax;
    double y2min, y2max;
    double tmp;
#define swap(x,y) {tmp = x; x = y; y = tmp;}

    xmin = rect[0].x;
    xmax = rect[1].x;
    if (xmin > xmax) swap(xmin, xmax);
    ymin = rect[0].y;
    ymax = rect[1].y;
    if (ymin > ymax) swap(ymin, ymax);
    y2min = rect[0].y2;
    y2max = rect[1].y2;
    if (y2min > y2max) swap(y2min, y2max);

    AtPlotterSetAllAxisBounds(w, xmin, xmax, ymin, ymax, y2min, y2max);
#undef swap    
}


void main(Cardinal argc, char *argv[])

{
  Widget toplevelshell, plotter, plot, cplot;
  Arg args[8];
  int i, j;
  double x;
  double min = 0.0;
  double max = 4.0;

  toplevelshell = XtInitialize("plottest", "Plottest", NULL, 0, &argc, argv);

  plotter = XtCreateManagedWidget("plotter", atPlotterWidgetClass,
				      toplevelshell, NULL, 0);

  x = 2.0;
  for (i = 0; i < DATASIZE; i++, x += 0.5)  {
    xdata[i] = x;
    ydata[i] = x + 2.0;
    errabove[i] = (i % 3) * 2.0;
    errbelow[i] = (double) (i % 4);
  }

/*  for (i = 0; i < 5; i++)
    for (j = 0; j < 5; j++)
      zdata[i*5 + j] = i*5 + j;
*/
  i = 0;
  XtSetArg(args[i], XtNzPoints, zdata);  i++;
  XtSetArg(args[i], XtNxMin, &min);  i++;
  XtSetArg(args[i], XtNxMax, &max);  i++;
  XtSetArg(args[i], XtNyMin, &min);  i++;
  XtSetArg(args[i], XtNyMax, &max);  i++;
  cplot = XtCreateWidget("cplot", atContourPlotWidgetClass, plotter, 
			 args, i);
/*
  i = 0;
  XtSetArg(args[i], XtNnumPoints, DATASIZE);   i++;
  XtSetArg(args[i], XtNxPoints, xdata);  i++;
  XtSetArg(args[i], XtNyPoints, ydata);  i++;
  XtSetArg(args[i], XtNerrorAbove, errabove);  i++;
  XtSetArg(args[i], XtNerrorBelow, errbelow);  i++;
  plot = XtCreateWidget("plot", atXYErrorPlotWidgetClass, plotter, args, i);
*/
  XtAddCallback(plotter, XtNclickCallback, click, NULL);
  XtAddCallback(plotter, XtNdragCallback, Zoomin, NULL);

  XtRealizeWidget(toplevelshell);
  XtMainLoop();
}




