#include "gerber.h"

static tool_t *tools[MAXTOOLS];
extern GC create_default_gc();
extern int verbose;

read_toolfile(f)
     char *f;
{
  FILE *t = NULL;
  int default_tools = FALSE;
  Pixmap p;



  p = XCreatePixmap(dpy, RootWindowOfScreen(XtScreen(toplevel)),
		    10, 10, 1);

  if (f) {
    t = fopen(f,"r");
    if (!t) {
      fprintf(stderr,"Can't open tool file %s\n",f);
      fprintf(stderr,"All lines will have width 10 mil.\n");
      default_tools = TRUE;
    }
  } else {
    default_tools = TRUE;
  }

  if (default_tools) {
    int i;
    tool_t *tool;
    GC gc;
    XGCValues gc_values;
    unsigned long gc_mask;

    gc = create_default_gc(p);

    for (i=0; i<MAXTOOLS; i++) {
      tool = (tool_t *)xmalloc(sizeof(tool_t));
      tool->size = 10;
      tool->shape = Round;
      tool->flash = Draw;

      gc_values.cap_style = CapRound;
      gc_mask = GCCapStyle;
      XChangeGC(dpy,gc,gc_mask,&gc_values);

      tool->gc = gc;
      tools[i] = tool;
    }
  } else {

    int lineno = 0;
    int i;
    char line[1024];

    for (i=0; i<MAXTOOLS; i++) {
      tools[i] = NULL;
    }

    while (!feof(t)) {
      int station, dcode, size;
      char d, ac;
      char shape[10], flash_draw[10];
      tool_t *tool;

      (void) fgets(&line[0],1023,t);
      lineno++;
      (void) sscanf(line,"%d %c%d %c%d %10s %10s\n",
	     &station,&d,&dcode,&ac,&size,shape,flash_draw);

      if ((dcode < 0) || (dcode > MAXTOOLS)) {
	fprintf(stderr,"Invalid draft code %d at line number %d\n",
		dcode,lineno);
	continue;
      }

      tool = (tool_t *)xmalloc(sizeof(tool_t));
      tool->size = size;
      tool->shape = (shape[0]=='R'?Round:Square);
      tool->flash = (flash_draw[0]=='D'?Draw:Flash);
      tool->gc = create_default_gc(p);

      if (tool->shape == Round) {
	XGCValues gc_values;
	unsigned long gc_mask;

	gc_values.cap_style = CapRound;
	gc_mask = GCCapStyle;
	XChangeGC(dpy,tool->gc,gc_mask,&gc_values);
      } else {
	XGCValues gc_values;
	unsigned long gc_mask;

	gc_values.cap_style = CapProjecting;
	gc_mask = GCCapStyle;
	XChangeGC(dpy,tool->gc,gc_mask,&gc_values);
      }

      tools[dcode] = tool;
    }
  }
  if (t) (void) fclose(t);
}
	

rescale_tools()
{
  int i;
  XGCValues gc_values;
  unsigned long gc_mask;

  for (i=0; i<MAXTOOLS; i++) {
    if (tools[i]) {
      gc_values.line_width = MIL2PIXEL(tools[i]->size);
      gc_mask = GCLineWidth;
      XChangeGC(dpy,tools[i]->gc,gc_mask,&gc_values);
    }
  }
}

tool_t *gettool(n)
     int n;
{
  if ((n>=0) && (n<MAXTOOLS)) {
    return tools[n];
  } else {
    return NULL;
  }
}



GC create_default_gc(p)
     Pixmap p;
{
  XGCValues gc_values;
  unsigned long gc_mask;

  gc_values.foreground = BlackPixelOfScreen(XtScreen(toplevel));
  gc_values.background = WhitePixelOfScreen(XtScreen(toplevel));
  gc_values.function = GXcopy;
  gc_values.line_width = 1;
  gc_values.fill_style = FillSolid;
  gc_mask = GCForeground|GCBackground|GCFunction;
  gc_mask = gc_mask|GCFillStyle|GCLineWidth;
    
  return XCreateGC(XtDisplay(toplevel),
		       /*RootWindowOfScreen(XtScreen(toplevel)),*/
		       p,
		       gc_mask, &gc_values);

}
