/* $Header: /afs/athena.mit.edu/astaff/project/atdev/src/plotter/plotterdemo/RCS/callbacks.c,v 1.2 91/01/14 16:30:39 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/Command.h>
#include <X11/Xaw/Form.h>
#include <X11/Xaw/Box.h>
#include <X11/Xaw/Toggle.h>
#include <X11/Xaw/AsciiText.h>
#include <X11/Xaw/SmeBSB.h>
#include <X11/Shell.h>
#include <X11/Xaw/Cardinals.h>

#include <At/Plotter.h>
#include <At/Axis.h>

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

extern AtPlotterWidget plotter;

#define LANDSCAPE  1
#define PORTRAIT   2

typedef struct {
  Widget shell;
  Widget form;
  Widget title;
  Widget orientbox;
  Widget landtoggle;
  Widget portoggle;
  Widget filelabel, filetext;
  Widget widthlabel, widthtext;
  Widget heightlabel, heighttext;
  Widget okB;
  Widget cancelB;
  double width, height;
} PrintDialog;

static PrintDialog pdinfo;
static Boolean print_dialog_created;

static void CreatePrintDialog(AtPlotterWidget, PrintDialog *);
static void PrintOk(Widget, PrintDialog *, XtPointer);
static void PrintCancel(Widget, PrintDialog *, XtPointer);

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

void QuitApp(Widget w, XtPointer client_data, XtPointer call_data)
{
  XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
  exit(0);
}

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

void Print(Widget w, XtPointer client_data, XtPointer call_data)
{
  if (!print_dialog_created)  {
    pdinfo.width = 6.0;
    pdinfo.height = 8.0;
    CreatePrintDialog(plotter, &pdinfo);
    print_dialog_created = True;
  }

  XtPopup(pdinfo.shell, XtGrabNone);
}

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

static void CreatePrintDialog(AtPlotterWidget pw, PrintDialog *pd)
{
  Arg args[5];
  int n;
  char width_buf[20], height_buf[20];

  pd->shell = XtCreatePopupShell("printshell", transientShellWidgetClass,
				 pw, NULL, ZERO);
  pd->form = XtCreateManagedWidget("printform", formWidgetClass, pd->shell,
				   NULL, ZERO);

  pd->title = XtCreateManagedWidget("title", labelWidgetClass,
				    pd->form, NULL, ZERO);
  pd->filelabel = XtCreateManagedWidget("filelabel", labelWidgetClass,
					pd->form, NULL, ZERO);
  pd->filetext = XtCreateManagedWidget("filetext", asciiTextWidgetClass,
				       pd->form, NULL, ZERO);
  pd->orientbox = XtCreateManagedWidget("orientbox", boxWidgetClass,
					pd->form, NULL, ZERO);
  n = 0;
  XtSetArg(args[n], XtNradioData, LANDSCAPE);  n++;
  pd->landtoggle = XtCreateManagedWidget("ltoggle", toggleWidgetClass,
					 pd->orientbox, args, n);
  n = 0; 
  XtSetArg(args[n], XtNradioGroup, pd->landtoggle); n++;
  XtSetArg(args[n], XtNstate, True); n++;
  XtSetArg(args[n], XtNradioData, PORTRAIT);  n++;
  pd->portoggle = XtCreateManagedWidget("ptoggle", toggleWidgetClass,
					pd->orientbox, args, n);
  
  pd->widthlabel = XtCreateManagedWidget("widthlabel", labelWidgetClass,
				       pd->form, NULL, ZERO);
  sprintf(width_buf, "%g", pd->width);
  n = 0;
  XtSetArg(args[n], XtNstring, width_buf); n++;
  pd->widthtext = XtCreateManagedWidget("widthtext", asciiTextWidgetClass, 
				      pd->form, args, n);

  pd->heightlabel = XtCreateManagedWidget("heightlabel", labelWidgetClass,
				       pd->form, NULL, ZERO);
  sprintf(height_buf, "%g", pd->height);
  n = 0;
  XtSetArg(args[n], XtNstring, height_buf); n++;
  pd->heighttext = XtCreateManagedWidget("heighttext", asciiTextWidgetClass, 
				      pd->form, args, n);

  pd->okB = XtCreateManagedWidget("okB", commandWidgetClass,
				    pd->form, NULL, ZERO);
  XtAddCallback(pd->okB, XtNcallback, PrintOk, pd);
  pd->cancelB = XtCreateManagedWidget("cancelB", commandWidgetClass,
				    pd->form, NULL, ZERO);
  XtAddCallback(pd->cancelB, XtNcallback, PrintCancel, pd);
}

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

static void PrintOk(Widget w, PrintDialog *pd, XtPointer call_data)
{
  String width, height, fname;
  Arg arg;
  int orientation, pwidth, pheight, x1, y1, x2, y2;

  XtSetArg(arg, XtNstring, &fname);
  XtGetValues(pd->filetext, &arg, 1);

  XtSetArg(arg, XtNstring, &width);
  XtGetValues(pd->widthtext, &arg, 1);
  XtSetArg(arg, XtNstring, &height);
  XtGetValues(pd->heighttext, &arg, 1);
  sscanf(width, " %lf ", &pd->width);
  sscanf(height, " %lf ", &pd->height);

  pwidth = 72 * pd->width;
  pheight = 72 * pd->height;

  orientation = (int) XawToggleGetCurrent(pd->landtoggle);

  if (orientation == PORTRAIT) { /* portrait mode */
    x1 = (612 - pwidth) / 2;
    x2 = x1 + pwidth - 1;
    y1 = (792 - pheight) / 2;
    y2 = y1 + pheight;
  }
  else {  /* landscape mode */
    x1 = (792 - pwidth) / 2;
    x2 = x1 + pwidth - 1;
    y1 = (612 - pheight)/2;
    y2 = y1 + pheight;
  }

  XtPopdown(pd->shell);
  
  AtPlotterGeneratePostscript(fname, plotter, "Demo Plot",
			      x1, y1, x2, y2,
			      (orientation == LANDSCAPE ? True : False));
}

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

static void PrintCancel(Widget w, PrintDialog *pd, XtPointer call_data)
{
  XtPopdown(pd->shell);
}

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

#define toggle_width 8
#define toggle_height 8
static char toggle_bits[] = {
   0x00, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x00};

static Pixmap set_pixmap;
static Boolean created = False;

void SetEntry(Widget w, Boolean set)
{
  Arg a;
  if (!created)  {
    Display *dpy = XtDisplayOfObject(w);
    set_pixmap = XCreateBitmapFromData(dpy, DefaultRootWindow(dpy),
				       toggle_bits, toggle_width, 
				       toggle_height);
    created = True;
  }
  XtSetArg(a, XtNleftBitmap, (set ? set_pixmap : None));
  XtSetValues(w, &a, 1);
}

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

void SetFloatingX(Widget w, XtPointer client_data, XtPointer call_data)
{
  static Boolean set = False;
  Arg a;
  Pixmap bm;

  XtSetArg(a, XtNleftBitmap, &bm);
  XtGetValues(w, &a, 1);

  set = (bm == None ? True : False);
  SetEntry(w, set);
  XtSetArg(a, XtNfloatingX, set);
  XtSetValues(plotter, &a, 1);
}

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

void SetFloatingY(Widget w, XtPointer client_data, XtPointer call_data)
{
  static Boolean set = False;
  Arg a;
  Pixmap bm;

  XtSetArg(a, XtNleftBitmap, &bm);
  XtGetValues(w, &a, 1);

  set = (bm == None ? True : False);
  SetEntry(w, set);
  XtSetArg(a, XtNfloatingY, set);
  XtSetValues(plotter, &a, 1);
}

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

void SetFramed(Widget w, XtPointer client_data, XtPointer call_data)
{
  static Boolean set = False;
  Arg a;
  Pixmap bm;

  XtSetArg(a, XtNleftBitmap, &bm);
  XtGetValues(w, &a, 1);

  set = (bm == None ? True : False);
  SetEntry(w, set);
  XtSetArg(a, XtNframedAxes, set);
  XtSetValues(plotter, &a, 1);
}

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

void SetLegend(Widget w, XtPointer client_data, XtPointer call_data)
{
  static Boolean set = False;
  Arg a;
  Pixmap bm;

  XtSetArg(a, XtNleftBitmap, &bm);
  XtGetValues(w, &a, 1);

  set = (bm == None ? True : False);
  SetEntry(w, set);
  XtSetArg(a, XtNshowLegend, set);
  XtSetValues(plotter, &a, 1);
}

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

void SetAutoscale(Widget w, XtPointer client_data, XtPointer call_data)
{
  static Boolean set = False;
  Arg a;
  Pixmap bm;

  XtSetArg(a, XtNleftBitmap, &bm);
  XtGetValues(w, &a, 1);

  set = (bm == None ? True : False);
  SetEntry(w, set);
  XtSetArg(a, XtNautoScale, set);
  XtSetValues(plotter, &a, 1);
}






