# include "Ximage.h"

int LoadTickmarks (Graphic *graphic, Layout *layout) {
  
  char line[129], type[16];
  double x, y, dx, dy;
  int status, NOBJECTS, Nobjects, done;

  fcntl (layout[0].Ximage, F_SETFL, !O_NONBLOCK);  

  Nobjects = layout[0].tickmarks.Nobjects;
  NOBJECTS = Nobjects + 100;
  REALLOCATE (layout[0].tickmarks.objects, Object, NOBJECTS);
  
  done = FALSE;
  while (!done) {
    status = read (layout[0].Ximage, line, 128); 
    line[128] = 0; 

    if (!strncmp (line, "DONE", 4)) {
      done = TRUE;
      break;
    }
    
    sscanf (line, "%s %lf %lf %lf %lf\n", type, &x, &y, &dx, &dy);
    
    if (strcmp (type, "TEXT") && strcmp (type, "LINE") && strcmp (type, "BOX") && strcmp (type, "CIRCLE")) {  /* skip */
      fprintf (stderr, "don't know %s, skipping\n", type);
      continue;
    }
    
    strcpy (layout[0].tickmarks.objects[Nobjects].type, type);
    layout[0].tickmarks.objects[Nobjects].x = x;
    layout[0].tickmarks.objects[Nobjects].y = y;
    layout[0].tickmarks.objects[Nobjects].dx = dx;
    layout[0].tickmarks.objects[Nobjects].dy = dy;
    
    if (!strcmp (type, "TEXT")) { /* dx = Nchar, dy = angle (not yet used) */
      status = read (layout[0].Ximage, line, 128); 
      line[128] = 0; 
      ALLOCATE (layout[0].tickmarks.objects[Nobjects].text, char, (int) dx + 1);
      strncpy (layout[0].tickmarks.objects[Nobjects].text, line, (int) dx);
      layout[0].tickmarks.objects[Nobjects].text[(int)dx] = 0;
    }      
    
    Nobjects++;
    if (Nobjects >= NOBJECTS) {
      NOBJECTS = Nobjects + 100;
      REALLOCATE (layout[0].tickmarks.objects, Object, NOBJECTS);
    }

  }

  REALLOCATE (layout[0].tickmarks.objects, Object, MAX(Nobjects, 1));
  layout[0].tickmarks.Nobjects = Nobjects;

  Refresh (graphic, layout, 0);
  fcntl (layout[0].Ximage, F_SETFL, O_NONBLOCK);  
  return (TRUE);
  
}


/* this routine is independent of the number of overlays */
