# include "Ximage.h"

int bDrawFrame (KapaGraphWidget *graph) {
  
  int i, P, IsLabel, IsMajor, fontsize;
  double fx, fy, dfx, dfy, range, major, minor, first, next;
  char *fontname;
  int weight;
  bDrawColor color;

  /* each axis is drawn independently */
  fontname = GetRotFont (&fontsize);
  for (i = 0; i < 4; i++) {
    fx  = graph[0].axis[i].fx;
    fy  = graph[0].axis[i].fy;
    dfx = graph[0].axis[i].dfx;
    dfy = graph[0].axis[i].dfy;
    P = hypot ((double)graph[0].axis[(i+1)%2].dfx, (double)graph[0].axis[(i+1)%2].dfy);

    weight = MAX (0, MIN (10, graph[0].axis[i].lweight));
    color = MAX (0, MIN (15, graph[0].axis[i].color));
    bDrawSetStyle (color, weight, 0);

    if (graph[0].axis[i].isaxis) { 
      bDrawLine (fx, fy, fx+dfx, fy+dfy); 
    }
    
    if (graph[0].axis[i].areticks) {
      if (isnan(graph[0].axis[i].min) || isinf(graph[0].axis[i].min)) continue;
      if (isnan(graph[0].axis[i].max) || isinf(graph[0].axis[i].max)) continue;

      range = graph[0].axis[i].max - graph[0].axis[i].min;

      AxisTickScale (&graph[0].axis[i], &major, &minor);

      first = minor*((int)(graph[0].axis[i].min/minor));
      if ((range > 0) && (first < graph[0].axis[i].min)) {
	first += minor;
      }
      if ((range < 0) && (first > graph[0].axis[i].min)) {
	first -= minor;
      }
      for (next = first; ((range > 0) && (next <= graph[0].axis[i].max)) || ((range < 0) && (next >= graph[0].axis[i].max));) {
	IsMajor = FALSE;
	IsMajor |= (fabs((int)(next/major) - (next/major)) < 0.5*(minor/major));
	IsMajor |= (fabs ((int)((next + 0.5*minor)/major) - (next/major)) < 0.5*(minor/major));
	IsMajor |= (fabs ((int)((next - 0.5*minor)/major) - (next/major)) < 0.5*(minor/major));
	IsLabel = (IsMajor && graph[0].axis[i].islabel);
	bDrawTick (fx, fy, dfx, dfy, P, graph[0].axis[i].min, graph[0].axis[i].max, next, IsLabel, IsMajor, i);
	if (range > 0) 
	  next += minor;
	else 
	  next -= minor;
      }
    }
  }
  return (TRUE);
}

void bDrawTick (double fx, double fy, double dfx, double dfy, int P, double min, double max, double value, int IsLabel, int IsMajor, int naxis) {
  
  int pos, dir, fontsize;
  double size, n, x, y, dx, dy;
  char string[64], *fontname;

  pos = size = 0;

  if (IsMajor) { 
    size = MAX (0.02, 7.0 / P); 
  } else {
    size = MAX (0.01, 4.0 / P); 
  }
  
  n = P / sqrt ((double)(dfx*dfx + dfy*dfy));
  x = fx + (value-min)*dfx/(max - min);
  y = fy + (value-min)*dfy/(max - min);

  dir = +1;
  if ((naxis == 0) || (naxis == 1)) dir *= -1;
  dx = dir*size*dfy*n;	
  dy = dir*size*dfx*n;
  
  bDrawLine (x, y, x+dx, y+dy);

  if (IsLabel) {
    fontname = GetRotFont (&fontsize);

    /* temporarily assume rectilinear axes */
    if (naxis == 0) { dx = 0; dy = -dir*(0.8*fontsize + 1); pos = 1; }
    if (naxis == 2) { dx = 0; dy = -dir*(0.8*fontsize + 1); pos = 7; }

    if (naxis == 1) { dy = 0; dx = -(0.8*fontsize + 1); pos = 3; }
    if (naxis == 3) { dy = 0; dx = +(0.8*fontsize + 1); pos = 5; }

    x = fx + (value-min)*dfx/(max - min) + dx;
    y = fy + (value-min)*dfy/(max - min) + dy;
    if (fabs(value) < 1e-14) { value = 0.0; }
    sprintf (string, "%g", value);
    bDrawRotText ((int)x, (int)y, string, pos, 0.0);
  }
}
