# include "Ximage.h"

/* Set the dimensions of the specific graph based on the current window size.  The graph
   is placed within the window at the fractional position defined by the section */

void SetGraphSize (Section *section) {

  int fontsize, bump, Nc;
  int textpad, textdY, WdY; 
  double PADx, PADy, Dx, Dy;
  double PXm, PXp, PYm, PYp;
  double X0, Y0, dX, dY;
  char string[64], *fontname;
  KapaGraphWidget *graph;
  Graphic *graphic;

  if (section == NULL) return;
  graph = section->graph;
  if (graph == NULL) return;

  graphic = GetGraphic ();
  fontname = GetRotFont (&fontsize);

  PADx = MAX (graphic->dx / 20.0, fontsize);
  PADy = MAX (graphic->dy / 20.0, fontsize);
  Dx = graphic->dx - 2*PADx;
  Dy = graphic->dy - 2*PADy;

  /* each graph has a padding PXm, PXp, PYm, PYp */
  PXm = (graph[0].axis[1].islabel) ? 4*fontsize : 0;
  PXp = (graph[0].axis[3].islabel) ? 4*fontsize : 0;
  PYm = (graph[0].axis[0].islabel) ? 4*fontsize : 0;
  PYp = (graph[0].axis[2].islabel) ? 4*fontsize : 0;
 
  /* basic size of the graph in Xwindow coordinates */
  X0 = PADx + PXm + (Dx * section[0].x);
  Y0 = PADy + PYm + (Dy * section[0].y);
  dX = (Dx * section[0].dx) - PXp - PXm;
  dY = (Dy * section[0].dy) - PYp - PYm;

  // if we are tied to an image, make mods as needed
  if (section->image) {
    textpad = graphic[0].font[0].ascent;
    textdY = 6*textpad + 7*PAD1;
    WdY = MAX (ZOOM_Y, textdY + 2*BUTTON_HEIGHT + PAD1);

    switch (section->image->location) {
      case 1:
	Y0 = graphic[0].dy * section[0].y + 2*PAD1 + WdY + 2; // tied to image in Y
	dY = graphic[0].dy * section[0].dy - 5*PAD1 - WdY - COLORPAD + 1;
	break;
      case 3:
	dY = graphic[0].dy * section[0].dy - 5*PAD1 - WdY - COLORPAD - PADy - PYm;
	break;
      case 2:
	X0 = graphic[0].dx * section[0].x  + 2*PAD1 + ZOOM_X;
	dX = graphic[0].dx * section[0].dx - 3*PAD1 - ZOOM_X;
	break;
      case 4:
	dX = graphic[0].dx * section[0].dx - 3*PAD1 - ZOOM_X - PADx - PXm;
	break;
    }
  }

  /* define locations of coordinate axes */
  graph[0].axis[0].fx  = X0;
  graph[0].axis[0].fy  = graphic->dy - Y0;
  graph[0].axis[0].dfx = dX;
  graph[0].axis[0].dfy = 0;
  
  graph[0].axis[1].fx  = X0;
  graph[0].axis[1].fy  = graphic->dy - Y0;
  graph[0].axis[1].dfx = 0;
  graph[0].axis[1].dfy = -dY;

  graph[0].axis[2].fx  = X0;
  graph[0].axis[2].fy  = graphic->dy - Y0 - dY;
  graph[0].axis[2].dfx = dX;
  graph[0].axis[2].dfy = 0;

  graph[0].axis[3].fx  = X0 + dX;
  graph[0].axis[3].fy  = graphic->dy - Y0;
  graph[0].axis[3].dfx = 0;
  graph[0].axis[3].dfy = -dY;

  PADx = 0.8*fontsize + 2;
  PADy = 3.0*fontsize + 4;

  /* define locations of axis labels */
  graph[0].label[LABELX0].x = graph[0].axis[0].fx + 0.5*graph[0].axis[0].dfx;
  bump = (graph[0].axis[0].islabel) ? PADy : PADx;
  graph[0].label[LABELX0].y = graph[0].axis[0].fy + bump;

  graph[0].label[LABELX1].x = graph[0].axis[2].fx + 0.5*graph[0].axis[2].dfx;
  bump = (graph[0].axis[2].islabel) ? PADy : PADx;
  graph[0].label[LABELX1].y = graph[0].axis[2].fy - bump;

  /* check for the max size of the axis label */
  sprintf (string, "%4g", graph[0].axis[1].min);
  Nc = strlen (string);
  sprintf (string, "%4g", graph[0].axis[1].max);
  Nc = MAX (Nc, strlen (string));

  graph[0].label[LABELY0].y = graph[0].axis[1].fy + 0.5*graph[0].axis[1].dfy;
  bump = (graph[0].axis[1].islabel) ? (0.8*Nc*fontsize + 1) : PADx;
  graph[0].label[LABELY0].x = graph[0].axis[1].fx - bump;

  sprintf (string, "%4g", graph[0].axis[3].min);
  Nc = strlen (string);
  sprintf (string, "%4g", graph[0].axis[3].max);
  Nc = MAX (Nc, strlen (string));

  graph[0].label[LABELY1].y = graph[0].axis[3].fy + 0.5*graph[0].axis[3].dfy;
  bump = (graph[0].axis[3].islabel) ? (0.8*Nc*fontsize + 1) : PADx;
  graph[0].label[LABELY1].x = graph[0].axis[3].fx + bump;
  
  /* these are wrong and have to be adjusted to sit in the corners */

  graph[0].label[LABELUL].x = graph[0].axis[2].fx - PADx;
  graph[0].label[LABELUL].y = graph[0].axis[2].fy - PADx;
  graph[0].label[LABELUR].x = graph[0].axis[2].fx + graph[0].axis[2].dfx + PADx;
  graph[0].label[LABELUR].y = graph[0].axis[2].fy - PADx;
  graph[0].label[LABELLL].x = graph[0].axis[0].fx - PADx;
  graph[0].label[LABELLL].y = graph[0].axis[0].fy + PADx;
  graph[0].label[LABELLR].x = graph[0].axis[0].fx + graph[0].axis[0].dfx + PADx;
  graph[0].label[LABELLR].y = graph[0].axis[0].fy + PADx;

}
