/* $Header: /afs/athena.mit.edu/astaff/project/atdev/src/plotter/plotterdemo/RCS/demo.c,v 1.1 91/01/02 18:56:48 crcraig Exp $ */

/*******************************************************************
  Copyright (C) 1990 by the Massachusetts Institute of Technology

   Export of this software from the United States of America is assumed
   to require a specific license from the United States Government.
   It is the responsibility of any person or organization contemplating
   export to obtain such a license before exporting.

WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
distribute this software and its documentation for any purpose and
without fee is hereby granted, provided that the above copyright
notice appear in all copies and that both that copyright notice and
this permission notice appear in supporting documentation, and that
the name of M.I.T. not be used in advertising or publicity pertaining
to distribution of the software without specific, written prior
permission.  M.I.T. makes no representations about the suitability of
this software for any purpose.  It is provided "as is" without express
or implied warranty.

***************************************************************** */

#include <stdio.h>

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

#include <X11/Xaw/Form.h>
#include <X11/Xaw/Paned.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/MenuButton.h>
#include <X11/Xaw/SimpleMenu.h>
#include <X11/Xaw/Sme.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Xaw/Cardinals.h>

#include <At/Plotter.h>
#include <At/XYPlot.h>
#include <At/Barchart.h>

#include "callbacks.h"

/**********************************************************************/

AtPlotterWidget plotter;

static Widget CreateMenu(Widget);
static Widget CreatePlotter(Widget);

/**********************************************************************/

void main(int argc, char **argv)
{
  Widget top, form, menubox;
  XtAppContext app_con;
  int i;
    
  top = XtAppInitialize(&app_con, "Plotterdemo", NULL, ZERO, &argc, argv,
			NULL, NULL, ZERO);
  form = XtCreateManagedWidget("topform", panedWidgetClass, top,
			       NULL, ZERO);
  menubox = CreateMenu(form);
  plotter = (AtPlotterWidget) CreatePlotter(form);

  XtRealizeWidget(top);
  XtAppMainLoop(app_con);
}

/**********************************************************************/

double xdata[] = {
  0.0, 1.0, 2.0, 2.5, 3.3, 8.4, 10.5, 20.3, 22.1, 25.0 };
double ydata[] = {
  2.0, -3.5, -1.0, 0.5, 3.9, 2.5, 8.5, 10.5, 11.1, 15.0 };
double y2data[] = {
  1.5, 4.2, 2.5, 9.4, 3.0 };

static Widget CreatePlotter(Widget parent)
{
  Widget pw, plot1, plot2;
  Arg args[5];
  int n;

  pw = XtCreateManagedWidget("plotter", atPlotterWidgetClass, parent,
			     NULL, ZERO);
  n = 0;
  XtSetArg(args[n], XtNnumPoints, XtNumber(xdata));  n++;
  XtSetArg(args[n], XtNxPoints, xdata);  n++;
  XtSetArg(args[n], XtNyPoints, ydata);  n++;
  plot1 = XtCreateWidget("plot1", atXYPlotWidgetClass, pw,
			args, n);

  n = 0;
  XtSetArg(args[n], XtNnumPoints, XtNumber(y2data));  n++;
  XtSetArg(args[n], XtNyPoints, y2data);  n++;
  plot2 = XtCreateWidget("plot2", atBarchartWidgetClass, pw, args, n);

  return pw;
}

/**********************************************************************/
/**********************************************************************/

#define NONE 0
#define YES  1
#define NO   2

typedef struct {
  String name;
  XtCallbackProc callback;
  int set;
} MenuNamesAndCallbacks;

typedef struct {
  MenuNamesAndCallbacks  *menu_item_names;
  int num_items;
} MenuItemInfo;


static String mbutton_names[] = {
  "filebutton", 
  "optionsbutton",
};

static MenuNamesAndCallbacks filemenu_names[] = {
  { "printB", Print, NONE },
  { "quitB", QuitApp, NONE },
};

static MenuNamesAndCallbacks optionsmenu_names[] = {
  { "floatXB", SetFloatingX, NO },
  { "floatYB", SetFloatingY, NO },
  { "framedB", SetFramed, YES },
  { "legendB", SetLegend, YES },
  { "autoscaleB", SetAutoscale, YES },
};


static MenuItemInfo item_info[] = {
  { filemenu_names, XtNumber(filemenu_names) },
  { optionsmenu_names, XtNumber(optionsmenu_names) },
};

/**********************************************************************/


static Widget CreateMenu(Widget parent)
{
  Widget box, menubuttons[XtNumber(mbutton_names)], 
         menupanes[XtNumber(mbutton_names)], menuitem;
  Arg args[5];
  int i, j, n;

  box = XtCreateManagedWidget("menubox", boxWidgetClass, parent, 
			      NULL, 0);

  for (i = 0; i < XtNumber(mbutton_names); i++)  {
    menubuttons[i] = XtCreateManagedWidget(mbutton_names[i], menuButtonWidgetClass, 
				    box, NULL, ZERO);
    menupanes[i] = XtCreatePopupShell("menu", simpleMenuWidgetClass,
				      menubuttons[i], NULL, ZERO);
    for (j = 0; j < item_info[i].num_items; j++)  {
      menuitem = XtCreateManagedWidget(item_info[i].menu_item_names[j].name, 
				       smeBSBObjectClass, menupanes[i],
				       NULL, ZERO);
      if (item_info[i].menu_item_names[j].set != NONE)
	SetEntry(menuitem, (item_info[i].menu_item_names[j].set == YES ? 
			    True : False));
      if (item_info[i].menu_item_names[j].callback != NULL)
	XtAddCallback(menuitem, XtNcallback, 
		      item_info[i].menu_item_names[j].callback, NULL);
    }
  }
  return box;
}
