/* This file is part of `mogul'
 * Copyright (c) 1995 David Deaven
 *
 * In the following, "you" and "You" refer to any entity other than 
 * the Microsoft corporation and its subsidiaries.  All rights are
 * reserved by the author in connection with Microsoft and its
 * subsidiaries.  The Microsoft Network is explicity denied the right
 * to copy, reproduce, or transmit this software.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * $Author: deaven $
 * $Id: sample.c,v 2.0 1996/07/09 20:59:55 deaven Exp deaven $
 */

#undef MOGUL_SOURCE

#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Shell.h>
#include <X11/Core.h>
#include <X11/Xaw/Box.h>
#include "Mogul.h"

#include <stdio.h>
#include <math.h>
#include "dmd/geometry.h"
#include "dmd/dmdtimer.h"
#include "mogul.h"

#define APPNAME "mogul"
#define APPCLASS "Mogul"

char *myname = "mogul";

static Widget mogul_widget, frame_widget;
static MogulPlot plot = NULL;
static Camera cref = NULL;
static double xref, yref, rref;

static void button(Widget w, XtPointer nil, XEvent *e)
{
	double s = (1.0 / 300.0);
	static int n3 = 0;
	switch (e->type) {
	  case ButtonPress:
	  { XButtonEvent *event = (XButtonEvent *) e;
	    double x = s * event->x, y = s * event->y;
	    if(event->button == 3) {
	      switch(n3++) {
		case 0:
		  XtDestroyWidget(mogul_widget);
		  break;

		default:
		  LEAVE();
		  REPORT(stdout);
		  exit(0);
		  break;
	      }
	    }
	    cref = MogulGetCamera(plot);
	    xref = x;
	    yref = y;
	  }
	  break;

	  case MotionNotify:
	  { XMotionEvent *event = (XMotionEvent *) e;
	    double x = s * event->x, y = s * event->y;
	    if(cref) {
	      CameraMoveOnGlassSphere(MogulGetCamera(plot),
		x, y, xref, yref, rref, cref);
	      MogulRender(plot, MWGetDevice(w));
	      xref = x;
	      yref = y;
	      cref = CameraCopy(MogulGetCamera(plot), cref);
	    }
	  }
	  break;

	  default:
	    break;
	}
}

void main(int argc, char **argv)
{
	Widget toplevel;
	XtAppContext app;
	Display *theDisplay;
	Arg args[10];
	Cardinal n;
	Geometry *g;
	MogulRules r;
	struct plotAttributes *attr;

	myname = argv[0];

	{
	  FILE *f = fopen("sample.geo", "r");
	  if(f) {
	    g = Gread(f);
	    fclose(f);
	  }
	  else {
	    printf("%s: can't open geometry file, exiting...", APPNAME);
	    exit(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);

	n = 0;
	frame_widget = XtCreateManagedWidget("Frame", boxWidgetClass,
	  toplevel, args, n);

	mogul_widget = XtVaCreateManagedWidget("MogulWidget", mogulWidgetClass,
	  frame_widget, 
	  XtNwidth, 256,
	  XtNheight, 256,
	  NULL);

	plot = NewMogulPlot(myname);
	r = NewMogulRules();

	{ NeighborList *nl = nlalloc(10.0);
	  newnl(g, nl);
	  MogulAddGeometry(plot, r, g, nl);
	  nlfree(nl);
	}
	
	attr = MogulGetPlotAttributes(plot);
	attr->width = 10.0;
	attr->height = 10.0;
	MogulSetPlotAttributes(plot, attr);
	rref = 1.0;

	XtRealizeWidget(toplevel);

	MogulRender(plot, (MogulDevice) MWGetDevice(mogul_widget));

	XtAddEventHandler(frame_widget, ButtonPressMask | ButtonMotionMask,
	  FALSE, button, NULL);

	printf("Use mouse button 3 to exit.\n");

	ENTER("Xt loop");
	XtAppMainLoop(app);
	LEAVE();
	REPORT(stdout);
	exit(0);
}
