# include "Ximage.h"

int LoadFrame (int sock) {
  
  int i;
  Section *section;
  KapaGraphWidget *graph;

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

  KapaScanGraphData (sock, &graph[0].data);

  graph[0].axis[3].min = graph[0].axis[1].min;
  graph[0].axis[3].max = graph[0].axis[1].max;
  graph[0].axis[2].min = graph[0].axis[0].min;
  graph[0].axis[2].max = graph[0].axis[0].max;
  
  // XXX this is fragile
  graph[0].data.color = MAX (0, MIN (15, graph[0].data.color));

  graph[0].axis[0].pad = graph[0].data.padXm;
  graph[0].axis[1].pad = graph[0].data.padYm;
  graph[0].axis[2].pad = graph[0].data.padXp;
  graph[0].axis[3].pad = graph[0].data.padYp;

  graph[0].axis[0].labelPad = graph[0].data.labelPadXm;
  graph[0].axis[1].labelPad = graph[0].data.labelPadYm;
  graph[0].axis[2].labelPad = graph[0].data.labelPadXp;
  graph[0].axis[3].labelPad = graph[0].data.labelPadYp;

  for (i = 0; i < 4; i++) {
    graph[0].axis[i].lweight = graph[0].data.lweight;
    graph[0].axis[i].color = graph[0].data.color;

    graph[0].axis[i].ticktextPad = graph[0].data.ticktextPad;

    switch (graph[0].data.axis[i]) {
    case '0':
      graph[0].axis[i].isaxis = FALSE;
      break;
    case '1':
      graph[0].axis[i].isaxis = TRUE;
      break;
    case '2':
      graph[0].axis[i].isaxis = TRUE;
      break;
    }
    switch (graph[0].data.ticks[i]) {
    case '0':
      graph[0].axis[i].areticks = FALSE;
      break;
    case '1':
      graph[0].axis[i].areticks = TRUE;
      break;
    case '2':
      graph[0].axis[i].areticks = 2;
      break;
    }
    switch (graph[0].data.labels[i]) {
    case '0':
      graph[0].axis[i].islabel = FALSE;
      break;
    case '1':
      graph[0].axis[i].islabel = TRUE;
      break;
    case '2':
      graph[0].axis[i].islabel = (i < 2);
      break;
    }
  }

  SetSectionSizes (section);
  Refresh();
  // if (USE_XWINDOW) DrawFrame (graph);
  // FlushDisplay ();

  /* XXX why did I do this??? */
# if (0)
  status = TRUE;
  if (status) {
    for (i = 0; i < Nsection; i++) {
      PositionPicture (&section[i]);
    }
    if (USE_XWINDOW) XClearWindow (graphic.display, graphic.window);
    Refresh ();
  } else {
    FlushDisplay ();
  } 
# endif

  return (TRUE);
  
}
