# include "Ximage.h"

/* initialize graph data */
KapaGraphWidget *InitGraph () {

  int i;
  KapaGraphWidget *graph;

  ALLOCATE (graph, KapaGraphWidget, 1);

  /* set up axis positions */
  for (i = 0; i < 4; i++) {
    graph[0].axis[i].min = 0.0;
    graph[0].axis[i].max = 1.0;
    graph[0].axis[i].isaxis = FALSE;
    graph[0].axis[i].areticks = FALSE;
    graph[0].axis[i].islabel = FALSE;
    graph[0].axis[i].lweight = 0.0;
    graph[0].axis[i].color = 0;
  }    
  for (i = 0; i < 8; i++) {
    strcpy (graph[0].label[i].text, "");
  }

  graph[0].data.xmin = 0.0;
  graph[0].data.xmax = 1.0;
  graph[0].data.ymin = 0.0;
  graph[0].data.ymax = 1.0;

  graph[0].data.style 	= 2; 		// points
  graph[0].data.ptype 	= 2;		// + for points
  graph[0].data.ltype 	= 0;		// solid line
  graph[0].data.etype 	= 0;		// no error bars
  graph[0].data.ebar  	= 0;		// no cross bar
  graph[0].data.color 	= 0;		// black
  graph[0].data.lweight = 0.5;		// line weight of 0.5
  graph[0].data.size    = 1.0;		// point size of 1.0

  graph[0].Nobjects = 0;
  graph[0].Ntextline = 0;

  ALLOCATE (graph[0].objects, Gobjects, 1);  /* allocate so later free will not crash! */
  ALLOCATE (graph[0].textline, Label, 1);    /* allocate so later free will not crash! */

  graph[0].objects[0].x   = graph[0].objects[0].y   = graph[0].objects[0].z = NULL;
  graph[0].objects[0].dxm = graph[0].objects[0].dxp = NULL;
  graph[0].objects[0].dym = graph[0].objects[0].dyp = NULL;

  return (graph);
}

void DrawGraph (KapaGraphWidget *graph) {
  if (graph == NULL) return;
  DrawFrame    (graph);
  DrawObjects  (graph);
  DrawLabels   (graph);
  DrawTextlines(graph);
}

/* remove objects */
void EraseGraph (KapaGraphWidget *graph) {

  int i;

  if (graph == NULL) return;

  /* free data objects, then re-alloc those needed */
  for (i = 0; i < graph[0].Nobjects; i++) {
    FREE (graph[0].objects[i].x);
    FREE (graph[0].objects[i].y);
    FREE (graph[0].objects[i].z);
    FREE (graph[0].objects[i].dxm);
    FREE (graph[0].objects[i].dxp);
    FREE (graph[0].objects[i].dym);
    FREE (graph[0].objects[i].dyp);
  }
    
  /* reset axes and labels */
  for (i = 0; i < 4; i++) {
    graph[0].axis[i].isaxis = FALSE;
    graph[0].axis[i].islabel = FALSE;
    graph[0].axis[i].areticks = FALSE;
  }
  for (i = 0; i < 8; i++) {
    strcpy (graph[0].label[i].text, "");
  }
    
  graph[0].Nobjects = 0;
  graph[0].Ntextline = 0;
  REALLOCATE (graph[0].objects, Gobjects, 1);
  REALLOCATE (graph[0].textline, Label, 1);

  graph[0].objects[0].x   = graph[0].objects[0].y   = graph[0].objects[0].z = NULL;
  graph[0].objects[0].dxm = graph[0].objects[0].dxp = NULL;
  graph[0].objects[0].dym = graph[0].objects[0].dyp = NULL;
}

/* remove objects */
void FreeGraph (KapaGraphWidget *graph) {

  int i;

  if (graph == NULL) return;

  /* free data objects, then re-alloc those needed */
  for (i = 0; i < graph[0].Nobjects; i++) {
    FREE (graph[0].objects[i].x);
    FREE (graph[0].objects[i].y);
    FREE (graph[0].objects[i].z);
    FREE (graph[0].objects[i].dxm);
    FREE (graph[0].objects[i].dxp);
    FREE (graph[0].objects[i].dym);
    FREE (graph[0].objects[i].dyp);
  }
    
  FREE (graph[0].objects);
  FREE (graph[0].textline);
  free (graph);
  return;
}
