# include "Ximage.h"
# define DrawLine(X,Y,DX,DY) (XDrawLine (graphic->display, graphic->window, graphic->gc, (int)(X), (int)(Y), (int)(X+DX), (int)(Y+DY)))
  
static Graphic *graphic;

void DrawFrame (KapaGraphWidget *graph) {
  
  int i, fx, fy, dfx, dfy, P, IsLabel;
  double range, major, minor, first, next;

  graphic = GetGraphic();

  // XXX why is this called for every redraw?
  DrawRotTextInit (graphic->display, graphic->window, graphic->gc, graphic->fore, graphic->back);

  /* each axis is drawn independently, but ticks and labels are placed according to
     perpendicular distance. */
  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);

    if (graph[0].axis[i].isaxis) {
      DrawLine (fx, fy, dfx, dfy);
    }
    
    if (graph[0].axis[i].areticks) {
      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));) {
	IsLabel = FALSE;
	if ((fabs((int)(next/major) - (next/major)) < 0.5*(minor/major)) || 
	    (fabs ((int)((next + 0.5*minor)/major) - (next/major)) < 0.5*(minor/major)) ||
	    (fabs ((int)((next - 0.5*minor)/major) - (next/major)) < 0.5*(minor/major)))
	  IsLabel = graph[0].axis[i].islabel;
	DrawTick (fx, fy, dfx, dfy, P, graph[0].axis[i].min, graph[0].axis[i].max, next, IsLabel, i);
	if (range > 0) 
	  next += minor;
	else 
	  next -= minor;
      }
    }
  }
}

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

  if (mode) { 
    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;
  
  DrawLine (x, y, dx, dy);

# ifdef IN_AND_OUT_TICKS
# define GAP 0.03
# else
# define GAP 0.01
# endif

  if (mode == 1) {
    fontname = GetRotFont (&fontsize);
    pos = 0;
    
    /* 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/(max-min)) < 0.001) { value = 0.0; }
    sprintf (string, "%4g", value);
    DrawRotText (x, y, string, pos, 0.0);
  }
  
}


void AxisTickScale (Axis *axis, double *major, double *minor) {

  double range, lrange, factor, mantis, fmantis, power;

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

  lrange = log10(MAX(fabs(range), 1e-30));
  mantis = modf (lrange, &factor);
  if (mantis < 0.0) {
    mantis += 1.0;
    factor -= 1.0;
  }
  
  power = pow(10.0, factor);
  fmantis = pow(10.0, mantis);
  *major = 0.5 * power;
  *minor = 0.1 * power;
  
  if ((fmantis >= 1.0) && (fmantis <  1.999)) {
    *major = 0.5 * power;
    *minor = 0.1 * power;
    if (axis[0].areticks == 1) {
      *major = 1.0 * power;
      *minor = 0.2 * power;
    }	  
  }
  if ((fmantis >= 1.999) && (fmantis <  3.999)) {
    *major = 1.0 * power;
    *minor = 0.2 * power;
    if (axis[0].areticks == 1) {
      *major = 2.0 * power;
      *minor = 0.5 * power;
    }	  
  }
  if ((fmantis >= 3.999) && (fmantis <  5.999)) {
    *major = 1.0 * power;
    *minor = 0.5 * power;
    if (axis[0].areticks == 1) {
      *major = 2.0 * power;
      *minor = 0.5 * power;
    }	  
  }
  if ((fmantis >= 5.999) && (fmantis <   7.999)) {
    *major = 2.0 * power;
    *minor = 0.5 * power;
    if (axis[0].areticks == 1) {
      *major = 2.5 * power;
      *minor = 0.5 * power;
    }	  
  }
  if ((fmantis >= 7.999) && (fmantis <  10.000)) {
    *major = 2.5 * power;
    *minor = 0.5 * power;
    if (axis[0].areticks == 1) {
      *major = 5.0 * power;
      *minor = 1.0 * power;
    }	  
  }
}
