/* $Id: Plot.c,v 1.1 91/05/20 15:14:09 gnb Exp $ */
/* $Source: /export/sources/x/At/Plotter/tmp/RCS/Plot.c,v $ */

/*

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.

*/

/*
 * $Log:	Plot.c,v $
 * Revision 1.1  91/05/20  15:14:09  gnb
 * Initial revision
 * 
 * 
 */

#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

#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),
     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,
    /* drawPS()     */	 NULL,
    /* draw_iconPS()*/   NULL,
    /* recalc()    */   NULL,
    /* checkhit()   */   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 P((AtPlotWidget request, AtPlotWidget new)); 
static void Initialize(request, new)
AtPlotWidget request, new;
{
  GetGC(new);
}

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

static Boolean SetValues P((AtPlotWidget current,
			    AtPlotWidget request,
			    AtPlotWidget new));
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 P((WidgetClass class)); 
static void ClassPartInitialize(class)
WidgetClass class;
{
  AtPlotWidgetClass plot, super;

  AtRegisterLinestyleConverter();
  AtRegisterMarkerConverter();

  plot = (AtPlotWidgetClass) class;
  super = (AtPlotWidgetClass) plot->object_class.superclass;

#define CheckInherit(proc) \
  if ((void (*)())plot->plot_class.proc == _XtInherit)\
    plot->plot_class.proc = super->plot_class.proc

  CheckInherit(draw);
  CheckInherit(draw_icon);
  CheckInherit(recalc);
  CheckInherit(drawPS);
  CheckInherit(draw_iconPS);

#undef CheckInherit
}

/*
 * 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);
  else 
    fprintf(fp, ".5 setlinewidth ");
    
  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 */
    fprintf(fp, "[] 0 setdash\n");
    break;
  }
}

/*
 * Need to use eval rather than just c-style as hack-local-variables
 * is called after the c-mode-hook. 
 *
 * Local Variables:
 * eval: (set-c-style 'GNU)
 * End:
 */
