static char RCSid[] = "$Id: Plot.c,v 1.0 91/08/22 15:33:41 gnb Exp $"; 
/*
 * $Source: /export/data/sources/x/At/Plotter/RCS/Plot.c,v $
 * 
 * $Log:	Plot.c,v $
 * Revision 1.0  91/08/22  15:33:41  gnb
 * Initial revision
 * 
 * 
 */

/*

Copyright 1990,1991 by the Massachusetts Institute of Technology

All rights reserved.

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 the Massachusetts
Institute of Technology (M.I.T.) not be used in advertising or publicity
pertaining to distribution of the software without specific, written
prior permission.

M.I.T. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
M.I.T. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
SOFTWARE.

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

#ifdef _AtDevelopment_
#include "PlotP.h"
#include "AtConverters.h"
#else
#include <At/PlotP.h>
#include <At/AtConverters.h>
#endif

/* Make the default background the same as the parent background */
static void defBG(w, off, val)
Widget w;
int off;
XrmValue *val;
{
     Widget p = XtParent(w);
     val->addr = (XtPointer)&p->core.background_pixel;
}

#define offset(field) XtOffset(AtPlotWidget, field)
static XtResource resources[] = {
  {XtNforeground, XtCForeground, XtRPixel,
     sizeof(Pixel), offset(plot.foreground),
	XtRString, XtDefaultForeground},
  {XtNbackground, XtCBackground, XtRPixel,
     sizeof(Pixel), offset(plot.background),
	XtRCallProc, (XtPointer)defBG},
	/* XtRString, XtDefaultBackground}, */
  {XtNlineWidth, XtCLineWidth, XtRInt,
     sizeof(int), offset(plot.line_width),
     XtRImmediate, (caddr_t)0},
  {XtNlineStyle, XtCLineStyle, XtRLinestyle,
     sizeof(int), offset(plot.line_style),
     XtRImmediate, (caddr_t)LineSolid},
  {XtNdashLength, XtCDashLength, XtRInt,
     sizeof(int), offset(plot.dash_length),
     XtRImmediate, (caddr_t)4},
  {XtNfastUpdate, XtCFastUpdate, XtRBoolean,
     sizeof(Boolean), offset(plot.fast_update),
     XtRImmediate, (caddr_t)False},
};
#undef offset

static void Initialize P((AtPlotWidget, AtPlotWidget));
static void Destroy P((AtPlotWidget));
static Boolean SetValues P((AtPlotWidget, AtPlotWidget, AtPlotWidget));
static void ClassPartInitialize P((WidgetClass));

AtPlotClassRec atPlotClassRec = {
{ /* core part */
    /* superclass         */    (WidgetClass) &objectClassRec,
    /* class_name         */    "AtPlot",
    /* widget_size        */    sizeof(AtPlotRec),
    /* class_initialize   */    NULL,
    /* class_part_initialize*/  ClassPartInitialize,
    /* class_inited       */    FALSE,
    /* initialize         */    (XtInitProc) Initialize,
    /* initialize_hook    */    NULL,
    /* pad                */    NULL,
    /* pad                */    NULL,
    /* pad                */    0,
    /* resources          */    resources,
    /* num_resources      */    XtNumber(resources),
    /* xrm_class          */    NULLQUARK,
    /* pad                */    FALSE,
    /* pad                */    FALSE,
    /* pad                */    FALSE,
    /* pad                */    FALSE,
    /* destroy            */    (XtWidgetProc)Destroy,
    /* pad                */    NULL,
    /* pad                */    NULL,
    /* set_values         */    (XtSetValuesFunc) SetValues,
    /* set_values_hook    */    NULL,
    /* pad                */    NULL,
    /* get_values_hook    */    NULL,
    /* pad                */    NULL,
    /* version            */    XtVersion,
    /* callback_offsets   */    NULL,
    /* pad                */    NULL,
    /* pad                */    NULL,
    /* pad                */    NULL,
    /* extension            */  NULL
},
  /* AtPlotClassPart initialization */
  {
    /* draw()		*/	NULL,
    /* draw_icon()	*/	NULL,
    /* draw_ps()	*/	NULL,
    /* draw_icon_ps()	*/	NULL,
    /* recalc()		*/	NULL
  }
};

WidgetClass atPlotWidgetClass = (WidgetClass)&atPlotClassRec;

static void GetGC P((AtPlotWidget w)); 
static void GetGC(w)
AtPlotWidget w;
{
     XGCValues gcv;
     XtGCMask gcmask;
     
     if (w->plot.fast_update)  {
	  gcv.foreground = w->plot.background ^ w->plot.foreground;
	  gcv.function = GXxor;
	  gcmask = GCForeground | GCFunction;
     } else {
	  gcv.foreground = w->plot.foreground;
	  gcv.background = w->plot.background;
	  gcmask = GCForeground | GCBackground;
     }
     gcv.line_width = w->plot.line_width;
     gcv.line_style = w->plot.line_style;
     gcv.dashes = (char) w->plot.dash_length;
     
     gcmask |= GCLineWidth | GCLineStyle | GCDashList;
     w->plot.gc = XtGetGC(XtParent((Widget)w), gcmask, &gcv);
}

static void FreeGC P((AtPlotWidget w)); 
static void FreeGC(w)
AtPlotWidget w;
{
     XtReleaseGC(XtParent((Widget)w), w->plot.gc);
}

static void Initialize(request, new)
AtPlotWidget request, new;
{
     GetGC(new);
}

static void Destroy(w)
AtPlotWidget w;
{
     FreeGC(w);
}

static Boolean SetValues(current, request, new)
AtPlotWidget current, request, new;
{
#define Changed(field) (new->plot.field != current->plot.field)
     Boolean redraw = False;
     
     if (Changed(background) ||
	 Changed(line_width) || Changed(line_style) || Changed(dash_length) ||
	 Changed(fast_update)) {
	  redraw = True;
     }
     
     if (redraw || Changed(foreground)) {
	  FreeGC(new);
	  GetGC(new);
	  if (redraw)
	       AtPlotterRedrawRequired(new);
	  else
	       AtPlotterRefreshRequired(new);
     }
     
     /* nothing to redisplay */
     return False;
#undef Changed    
}

static void ClassPartInitialize(class)
WidgetClass class;
{
     AtPlotWidgetClass plot, super;
     
     AtRegisterLinestyleConverter();
     /* AtRegisterMarkerConverter(); */
     
     plot = (AtPlotWidgetClass) class;
     super = (AtPlotWidgetClass) plot->object_class.superclass;
     
#define CheckInherit(proc, inherit) \
     if (plot->plot_class.proc == inherit)\
	  plot->plot_class.proc = super->plot_class.proc
	       
	       CheckInherit(draw, XtInheritDraw);
     CheckInherit(draw_icon, XtInheritDrawIcon);
     CheckInherit(draw_ps, XtInheritDrawPS);
     CheckInherit(draw_icon_ps, XtInheritDrawIconPS);
     CheckInherit(recalc, XtInheritRecalc);
     
#undef CheckInherit
     *RCSid = *RCSid;		/* Keep gcc quiet */
}

/*
 * Write a line to the specified file to set up the PostScript line
 * drawing GC to match this PlotWidget.
 */
void AtPlotPSLineStyle(fp, w)
FILE *fp;
AtPlotWidget w;
{
     int dl = w->plot.dash_length;
     int dl2;
     
     if (dl <= 0) dl = 1;
     dl2 = (dl + 1) >> 1;
     
     if (w->plot.line_width > 0)
	  fprintf(fp, "%d setlinewidth ", w->plot.line_width);
     
     switch (w->plot.line_style) {
     case LineOnOffDash:
	  fprintf(fp, "[ %d ] 0 setdash\n", dl); 
	  break;
     case LineDoubleDash:
	  fprintf(fp, "[ %d %d %d %d ] 0 setdash\n", dl, dl2, dl2, dl2);
	  break; 
     default:
	  /* Line solid */
	  break;
     }
}

/*
 * Wrappers to call the member routines
 */

void AtPlotDraw(pw, dpy, win, region, refresh)
AtPlotWidget pw;
Display *dpy;
Window win;
Region region;
int refresh;
{
     AtPlotDrawProc fn = ((AtPlotWidgetClass)
			  XtClass((Widget)pw))->plot_class.draw;
     if (fn)
	  fn(pw, dpy, win, region, refresh);
}

void AtPlotDrawIcon(pw, dpy, win, x1, y1, x2, y2, region)
AtPlotWidget pw;
Display *dpy;
Window win;
int x1, y1, x2, y2;
Region region;
{
     AtPlotDrawIconProc fn = ((AtPlotWidgetClass)
			      XtClass((Widget)pw))->plot_class.draw_icon;
     if (fn)
	  fn(pw, dpy, win, x1, y1, x2, y2, region);
}

void AtPlotDrawPS(pw, f, xs, ys)
AtPlotWidget pw;
FILE *f;
AtScale *xs, *ys;
{
     AtPlotDrawPSProc fn = ((AtPlotWidgetClass)
			    XtClass((Widget)pw))->plot_class.draw_ps;
     if (fn)
	  fn(pw, f, xs, ys);
}

void AtPlotDrawIconPS(pw, f, x1, y1, x2, y2)
AtPlotWidget pw;
FILE *f;
int x1, y1, x2, y2;
{
     AtPlotDrawIconPSProc fn = ((AtPlotWidgetClass)
				XtClass((Widget)pw))->plot_class.draw_icon_ps;
     if (fn)
	  fn(pw, f, x1, y1, x2, y2);
     
}

void AtPlotRecalc(pw, xs, ys, from, to)
AtPlotWidget pw;
AtScale *xs, *ys;
int from, to;
{
     AtPlotRecalcProc fn = ((AtPlotWidgetClass)
				XtClass((Widget)pw))->plot_class.recalc;
     if (fn)
	  fn(pw, xs, ys, from, to);
}
