# include "Ximage.h"

int SetSection (int SwitchSection) {
  
  int i, NewSection, ThisSection, MoveSection;
  char name[128];
  double x, y, dx, dy;

  if (SwitchSection) {
    KiiScanMessage (sock, "%s", name);
  } else {
    KiiScanMessage (sock, "%s %lf %lf %lf %lf", name, &x, &y, &dx, &dy);
  }    

  NewSection = FALSE;
  MoveSection = FALSE;
  ThisSection = -1;
  for (i = 0; i < Nsection; i++) {
    if (!strcmp (name, section[i].name)) {
      ThisSection = i;
      break;
    }
  }

  if (ThisSection == -1) {
    if (SwitchSection) {
      fprintf (stderr, "section %s not found\n", name);
      return (TRUE);
    }
    NewSection = TRUE;
    Nsection ++;
    REALLOCATE (section, Layout, Nsection);
    ThisSection = Nsection - 1;
    strcpy (section[ThisSection].name, name);
    section[ThisSection].Nobjects = 0;
    section[ThisSection].Nptext = 0;
    ALLOCATE (section[ThisSection].objects, Gobjects, 1);  /* allocate so later free will not crash! */
    section[ThisSection].objects[0].x   = section[ThisSection].objects[0].y   = section[ThisSection].objects[0].z = (float *) NULL;
    section[ThisSection].objects[0].dxm = section[ThisSection].objects[0].dxp = (float *) NULL;
    section[ThisSection].objects[0].dym = section[ThisSection].objects[0].dyp = (float *) NULL;
    ALLOCATE (section[ThisSection].ptext, Label, 1);       /* allocate so later free will not crash! */
    for (i = 0; i < 4; i++) {
      section[ThisSection].axis[i].min = 0;
      section[ThisSection].axis[i].max = 1;
      section[ThisSection].axis[i].isaxis = FALSE;
      section[ThisSection].axis[i].areticks = FALSE;
      section[ThisSection].axis[i].islabel = FALSE;
    }    
    for (i = 0; i < 8; i++) {
      strcpy (section[ThisSection].label[i].text, "");
    }
  }

  if (!SwitchSection) {
    if (!NewSection && (section[ThisSection].x != x)) MoveSection = TRUE;
    section[ThisSection].x = x;
    if (!NewSection && (section[ThisSection].y != y)) MoveSection = TRUE;
    section[ThisSection].y = y;
    if (!NewSection && (section[ThisSection].dx != dx)) MoveSection = TRUE;
    section[ThisSection].dx = dx;
    if (!NewSection && (section[ThisSection].dy != dy)) MoveSection = TRUE;
    section[ThisSection].dy = dy;

    if (NewSection || MoveSection) {
      PositionPicture (&section[ThisSection]);
    }

    if (MoveSection) {
      XClearWindow (graphic.display, graphic.window);
      Refresh (1);
    } 
    if (NewSection) {
    }      
  }

  TheSection = ThisSection;

  return (TRUE);
  
}

int ListSection () {
  
  int i, ThisSection;
  char name[128];

  KiiScanMessage (sock, "%s", name);

  if (!strcmp (name, "*")) {
    for (i = 0; i < Nsection; i++) {
      fprintf (stderr, "%s: %6.3f %6.3f %6.3f %6.3f\n", 
	       section[i].name, section[i].x, section[i].y, section[i].dx, section[i].dy);
    }
    return (TRUE);
  }

  ThisSection = -1;
  for (i = 0; i < Nsection; i++) {
    if (!strcmp (name, section[i].name)) {
      ThisSection = i;
      break;
    }
  }
  if (ThisSection == -1) {
    fprintf (stderr, "section %s not found\n", name);
    return (TRUE);
  }

  fprintf (stderr, "%s: %6.3f %6.3f %6.3f %6.3f\n", 
	   section[ThisSection].name, 
	   section[ThisSection].x, section[ThisSection].y, 
	   section[ThisSection].dx, section[ThisSection].dy);
  return (TRUE);
}
