# include "Ximage.h"

int SetGraphData (int sock) {
  
  int i;
  double xmin, xmax, ymin, ymax;
  Graphic *graphic;
  Section *section;
  KapaGraphWidget *graph;

  graphic = GetGraphic();

  section = GetActiveSection();
  if (section->graph == NULL) {
    section->graph = InitGraph ();
    SetSectionSizes (section);
  }
  graph = section->graph;
  
  // get graph data from client 
  KiiScanMessage (sock, "%d %d %d %d %d %d %lf %lf", 
		  &graph[0].data.style, 
		  &graph[0].data.ptype, 
		  &graph[0].data.ltype, 
		  &graph[0].data.etype, 
		  &graph[0].data.ebar, 
		  &graph[0].data.color, 
		  &graph[0].data.lweight, 
		  &graph[0].data.size);

  KiiScanMessage (sock, "%lf %lf %lf %lf", 
		  &graph[0].data.xmin, 
		  &graph[0].data.xmax, 
		  &graph[0].data.ymin, 
		  &graph[0].data.ymax);

  KiiScanMessage (sock, "%f %f %f %f", 
		  &graph[0].data.coords.pc1_1, &graph[0].data.coords.pc2_2,
		  &graph[0].data.coords.pc1_2, &graph[0].data.coords.pc2_1);

  KiiScanMessage (sock, "%d %d %s", 
		  &graph[0].data.flipeast, &graph[0].data.flipnorth,
		  graph[0].data.coords.ctype);

  KiiScanMessage (sock, "%lf %lf %f %f %f %f", 
		  &graph[0].data.coords.crval1,
		  &graph[0].data.coords.crval2,
		  &graph[0].data.coords.crpix1,
		  &graph[0].data.coords.crpix2,
		  &graph[0].data.coords.cdelt1,
		  &graph[0].data.coords.cdelt2);



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

  // XXX there are now two things which track the graph limits
  // make sure these are kept in sync (or drop one!)
  graph[0].axis[2].min = graph[0].axis[0].min = xmin;
  graph[0].axis[2].max = graph[0].axis[0].max = xmax;
  graph[0].axis[3].min = graph[0].axis[1].min = ymin; 
  graph[0].axis[3].max = graph[0].axis[1].max = ymax;
  
  for (i = 0; i < graph[0].Nobjects; i++) {
    graph[0].objects[i].x0 = xmin;
    graph[0].objects[i].x1 = xmax;
    graph[0].objects[i].y0 = ymin;
    graph[0].objects[i].y1 = ymax;
  }

  if (USE_XWINDOW) XClearWindow (graphic->display, graphic->window);
  Refresh ();

  return (TRUE);  
}

int GetGraphData (int sock) {
  
  Section *section;
  KapaGraphWidget *graph;

  section = GetActiveSection();
  if (section->graph == NULL) {
    section->graph = InitGraph ();
    SetSectionSizes (section);
  }
  graph = section->graph;

  KiiSendMessage (sock, "%8d %d %d %d %d %d %f %f", 
		  graph[0].data.style, 
		  graph[0].data.ptype, graph[0].data.ltype, 
		  graph[0].data.etype, graph[0].data.ebar, graph[0].data.color, 
		  graph[0].data.lweight, graph[0].data.size);

  KiiSendMessage (sock, "%g %g %g %g", 
		  graph[0].data.xmin, graph[0].data.xmax, 
		  graph[0].data.ymin, graph[0].data.ymax);

  KiiSendMessage (sock, "%g %g %g %g", 
		  graph[0].data.coords.pc1_1, graph[0].data.coords.pc2_2,
		  graph[0].data.coords.pc1_2, graph[0].data.coords.pc2_1);

  KiiSendMessage (sock, "%d %d %s", 
		  graph[0].data.flipeast, graph[0].data.flipnorth,
		  graph[0].data.coords.ctype);

  KiiSendMessage (sock, "%g %g %g %g %g %g", 
		  graph[0].data.coords.crval1,
		  graph[0].data.coords.crval2,
		  graph[0].data.coords.crpix1,
		  graph[0].data.coords.crpix2,
		  graph[0].data.coords.cdelt1,
		  graph[0].data.coords.cdelt2);

  return (TRUE);
}
