# include "Ximage.h"

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

// XXX currently only set to fill the window; does not adjust for section range
void SetImageSize (Section *section) {

  int Xs, Ys, Xe, Ye, dX, dY;
  int textpad, textdY, WdY; 
  KapaImageWidget *image;
  KapaGraphWidget *graph;
  Graphic *graphic;

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

  graphic = GetGraphic ();

  /* the image is placed within the graphic window in region specified by section */
  Xs = graphic[0].dx * section[0].x;
  Ys = graphic[0].dy * (1 - section[0].y - section[0].dy);
  dX = graphic[0].dx * section[0].dx;
  dY = graphic[0].dy * section[0].dy;

  switch (image[0].location) {
    case 0:
      if (section->graph) {
	  image[0].picture.x  = graph[0].axis[0].fx;
	  image[0].picture.y  = graph[0].axis[1].fy + graph[0].axis[1].dfy;
	  image[0].picture.dx = MAX(graph[0].axis[0].dfx + 1, 1);
	  image[0].picture.dy = MAX(fabs(graph[0].axis[1].dfy) - 1, 1);
      } else {
	  image[0].picture.x  = Xs + PAD1;
	  image[0].picture.y  = Ys + PAD1;
	  image[0].picture.dx = dX - 2*PAD1; 
	  image[0].picture.dy = dY - 2*PAD1;
      }
      if (USE_XWINDOW) CreatePicture (image, graphic);
      Remap (graphic, image, &image[0].matrix);
      return;

    case 1:
      textpad = graphic[0].font[0].ascent;
      textdY = 6*textpad + 7*PAD1;
      WdY = MAX (ZOOM_Y, textdY + 2*BUTTON_HEIGHT + PAD1);

      if (section->graph) {
	  image[0].picture.x = graph[0].axis[0].fx;
	  image[0].picture.y = Ys + 2*PAD1 + COLORPAD;
	  image[0].picture.dx = graph[0].axis[0].dfx;
	  image[0].picture.dy = dY - 5*PAD1 - WdY - COLORPAD;
      } else {
	  image[0].picture.x  = Xs + PAD1;
	  image[0].picture.y  = Ys + 2*PAD1 + COLORPAD;
	  image[0].picture.dx = dX - 2*PAD1; 
	  image[0].picture.dy = dY - 5*PAD1 - WdY - COLORPAD;
      }

      image[0].cmapbar.dx = dX - 2*PAD1; 
      image[0].cmapbar.dy = COLORPAD;
      image[0].cmapbar.x = Xs + PAD1;
      image[0].cmapbar.y = Ys + PAD1;

      // XXX zoom should scale somewhat with the image? (with a min and a max)
      // XXX actually, it is limited by the buttons and status region
      image[0].zoom.dx = ZOOM_X; 
      image[0].zoom.dy = ZOOM_Y;
      image[0].zoom.x = Xs + PAD1;
      image[0].zoom.y = image[0].picture.y + image[0].picture.dy + PAD1;

      /** everything below is tied in x-dir to the zoom box **/
      image[0].text_x = image[0].zoom.x + image[0].zoom.dx + PAD1;
      image[0].text_y = image[0].zoom.y;
      image[0].text_dx = ZOOM_X;
      image[0].text_dy = 6*textpad + 7*PAD1;

      image[0].overlay_button[0].x = image[0].text_x;
      image[0].overlay_button[0].y = image[0].text_y + image[0].text_dy + PAD1;
   
      image[0].overlay_button[1].x = image[0].overlay_button[0].x + image[0].overlay_button[0].dx + PAD1;
      image[0].overlay_button[1].y = image[0].overlay_button[0].y;

      image[0].overlay_button[2].x = image[0].overlay_button[1].x + image[0].overlay_button[1].dx + PAD1;
      image[0].overlay_button[2].y = image[0].overlay_button[0].y;

      image[0].overlay_button[3].x = image[0].overlay_button[2].x + image[0].overlay_button[2].dx + PAD1;
      image[0].overlay_button[3].y = image[0].overlay_button[0].y;

      image[0].hms_button.x = image[0].overlay_button[3].x + image[0].overlay_button[3].dx + PAD1;
      image[0].hms_button.y = image[0].overlay_button[0].y;

      image[0].PS_button.x = image[0].text_x;
      image[0].PS_button.y = image[0].overlay_button[0].y + BUTTON_HEIGHT + PAD1;

      /** everything below is tied to the PS_button in y-dir + the neighbor in x-dir **/
      image[0].grey_button.x = image[0].PS_button.x + image[0].PS_button.dx + PAD1;
      image[0].grey_button.y = image[0].PS_button.y;

      image[0].rainbow_button.x = image[0].grey_button.x + image[0].grey_button.dx + PAD1;
      image[0].rainbow_button.y = image[0].PS_button.y;

      image[0].puns_button.x = image[0].rainbow_button.x + image[0].rainbow_button.dx + PAD1;
      image[0].puns_button.y = image[0].PS_button.y;

      image[0].recenter_button.x = image[0].puns_button.x + image[0].puns_button.dx + PAD1;
      image[0].recenter_button.y = image[0].PS_button.y;
      break;

    case 3:
      textpad = graphic[0].font[0].ascent;
      textdY = 6*textpad + 7*PAD1;
      WdY = MAX (ZOOM_Y, textdY + 2*BUTTON_HEIGHT + PAD1);

      if (section->graph) {
	image[0].picture.x = graph[0].axis[0].fx;
	image[0].picture.y = graph[0].axis[1].fy + graph[0].axis[1].dfy;
	image[0].picture.dx = MAX(graph[0].axis[0].dfx, 1);
	image[0].picture.dy = MAX(fabs(graph[0].axis[1].dfy) - 1, 1);
      } else {
	image[0].picture.dx = dX - 2*PAD1; 
	image[0].picture.dy = dY - 5*PAD1 - WdY - COLORPAD;
	image[0].picture.x = Xs + PAD1;
	image[0].picture.y = Ys + 4*PAD1 + COLORPAD + WdY;
      }

      image[0].cmapbar.dx = dX - 2*PAD1; 
      image[0].cmapbar.dy = COLORPAD;
      image[0].cmapbar.x = Xs + PAD1;
      image[0].cmapbar.y = Ys + PAD1;

      // XXX zoom should scale somewhat with the image? (with a min and a max)
      // XXX actually, it is limited by the buttons and status region
      image[0].zoom.dx = ZOOM_X; 
      image[0].zoom.dy = ZOOM_Y;
      image[0].zoom.x = Xs + PAD1;
      image[0].zoom.y = Ys + 2*PAD1 + COLORPAD;

      /** everything below is tied in x-dir to the zoom box **/
      image[0].text_x = image[0].zoom.x + image[0].zoom.dx + PAD1;
      image[0].text_y = image[0].zoom.y;
      image[0].text_dx = ZOOM_X;
      image[0].text_dy = 6*textpad + 7*PAD1;

      image[0].overlay_button[0].x = image[0].text_x;
      image[0].overlay_button[0].y = image[0].text_y + image[0].text_dy + PAD1;
   
      image[0].overlay_button[1].x = image[0].overlay_button[0].x + image[0].overlay_button[0].dx + PAD1;
      image[0].overlay_button[1].y = image[0].overlay_button[0].y;

      image[0].overlay_button[2].x = image[0].overlay_button[1].x + image[0].overlay_button[1].dx + PAD1;
      image[0].overlay_button[2].y = image[0].overlay_button[0].y;

      image[0].overlay_button[3].x = image[0].overlay_button[2].x + image[0].overlay_button[2].dx + PAD1;
      image[0].overlay_button[3].y = image[0].overlay_button[0].y;

      image[0].hms_button.x = image[0].overlay_button[3].x + image[0].overlay_button[3].dx + PAD1;
      image[0].hms_button.y = image[0].overlay_button[0].y;

      image[0].PS_button.x = image[0].text_x;
      image[0].PS_button.y = image[0].overlay_button[0].y + BUTTON_HEIGHT + PAD1;

      /** everything below is tied to the PS_button in y-dir + the neighbor in x-dir **/
      image[0].grey_button.x = image[0].PS_button.x + image[0].PS_button.dx + PAD1;
      image[0].grey_button.y = image[0].PS_button.y;

      image[0].rainbow_button.x = image[0].grey_button.x + image[0].grey_button.dx + PAD1;
      image[0].rainbow_button.y = image[0].PS_button.y;

      image[0].puns_button.x = image[0].rainbow_button.x + image[0].rainbow_button.dx + PAD1;
      image[0].puns_button.y = image[0].PS_button.y;

      image[0].recenter_button.x = image[0].puns_button.x + image[0].puns_button.dx + PAD1;
      image[0].recenter_button.y = image[0].PS_button.y;
      break;

    case 2:

      if (section->graph) {
	image[0].picture.x = Xs + 2*PAD1 + ZOOM_X;
	image[0].picture.y = graph[0].axis[1].fy + graph[0].axis[1].dfy;
	image[0].picture.dx = dX - 3*PAD1 - ZOOM_X; 
	image[0].picture.dy = MAX(fabs(graph[0].axis[1].dfy) - 1, 1);
      } else {
	image[0].picture.dx = dX - 3*PAD1 - ZOOM_X; 
	image[0].picture.dy = dY - 3*PAD1 - COLORPAD;
	image[0].picture.x = Xs + 2*PAD1 + ZOOM_X;
	image[0].picture.y = Ys + 2*PAD1 + COLORPAD;
      }

      image[0].cmapbar.dx = dX - 2*PAD1; 
      image[0].cmapbar.dy = COLORPAD;
      image[0].cmapbar.x = Xs + PAD1;
      image[0].cmapbar.y = Ys + PAD1;

      // XXX zoom should scale somewhat with the image? (with a min and a max)
      // XXX actually, it is limited by the buttons and status region
      image[0].zoom.dx = ZOOM_X; 
      image[0].zoom.dy = ZOOM_Y;
      image[0].zoom.x = Xs + PAD1;
      image[0].zoom.y = image[0].picture.y;

      /** everything below is tied in x-dir to the zoom box **/
      textpad = graphic[0].font[0].ascent;
      image[0].text_x = image[0].zoom.x;
      image[0].text_y = image[0].zoom.y + image[0].zoom.dy + PAD1;
      image[0].text_dx = ZOOM_X;
      image[0].text_dy = 6*textpad + 7*PAD1;

      image[0].overlay_button[0].x = image[0].text_x;
      image[0].overlay_button[0].y = image[0].text_y + image[0].text_dy + PAD1;
   
      image[0].overlay_button[1].x = image[0].overlay_button[0].x + image[0].overlay_button[0].dx + PAD1;
      image[0].overlay_button[1].y = image[0].overlay_button[0].y;

      image[0].overlay_button[2].x = image[0].overlay_button[1].x + image[0].overlay_button[1].dx + PAD1;
      image[0].overlay_button[2].y = image[0].overlay_button[0].y;

      image[0].overlay_button[3].x = image[0].overlay_button[2].x + image[0].overlay_button[2].dx + PAD1;
      image[0].overlay_button[3].y = image[0].overlay_button[0].y;

      image[0].hms_button.x = image[0].overlay_button[3].x + image[0].overlay_button[3].dx + PAD1;
      image[0].hms_button.y = image[0].overlay_button[0].y;

      image[0].PS_button.x = image[0].zoom.x;
      image[0].PS_button.y = image[0].overlay_button[0].y + BUTTON_HEIGHT + PAD1;

      /** everything below is tied to the PS_button in y-dir + the neighbor in x-dir **/
      image[0].grey_button.x = image[0].PS_button.x + image[0].PS_button.dx + PAD1;
      image[0].grey_button.y = image[0].PS_button.y;

      image[0].rainbow_button.x = image[0].grey_button.x + image[0].grey_button.dx + PAD1;
      image[0].rainbow_button.y = image[0].PS_button.y;

      image[0].puns_button.x = image[0].rainbow_button.x + image[0].rainbow_button.dx + PAD1;
      image[0].puns_button.y = image[0].PS_button.y;

      image[0].recenter_button.x = image[0].puns_button.x + image[0].puns_button.dx + PAD1;
      image[0].recenter_button.y = image[0].PS_button.y;
      break;

    case 4:

      if (section->graph) {
	image[0].picture.x = graph[0].axis[0].fx;
	image[0].picture.y = graph[0].axis[1].fy + graph[0].axis[1].dfy;
	image[0].picture.dx = dX - 3*PAD1 - ZOOM_X - graph[0].axis[0].fx; 
	image[0].picture.dy = MAX(fabs(graph[0].axis[1].dfy) - 1, 1);
      } else {
	image[0].picture.dx = dX - 3*PAD1 - ZOOM_X; 
	image[0].picture.dy = dY - 3*PAD1 - COLORPAD;
	image[0].picture.x = Xs + PAD1;
	image[0].picture.y = Ys + 2*PAD1 + COLORPAD;
      }

      image[0].cmapbar.dx = dX - 2*PAD1; 
      image[0].cmapbar.dy = COLORPAD;
      image[0].cmapbar.x = Xs + PAD1;
      image[0].cmapbar.y = Ys + PAD1;

      // XXX zoom should scale somewhat with the image? (with a min and a max)
      // XXX actually, it is limited by the buttons and status region
      image[0].zoom.dx = ZOOM_X; 
      image[0].zoom.dy = ZOOM_Y;
      image[0].zoom.x = image[0].picture.x + image[0].picture.dx + PAD1;
      image[0].zoom.y = image[0].picture.y;

      /** everything below is tied in x-dir to the zoom box **/
      textpad = graphic[0].font[0].ascent;
      image[0].text_x = image[0].zoom.x;
      image[0].text_y = image[0].zoom.y + image[0].zoom.dy + PAD1;
      image[0].text_dx = ZOOM_X;
      image[0].text_dy = 6*textpad + 7*PAD1;

      image[0].overlay_button[0].x = image[0].text_x;
      image[0].overlay_button[0].y = image[0].text_y + image[0].text_dy + PAD1;
   
      image[0].overlay_button[1].x = image[0].overlay_button[0].x + image[0].overlay_button[0].dx + PAD1;
      image[0].overlay_button[1].y = image[0].overlay_button[0].y;

      image[0].overlay_button[2].x = image[0].overlay_button[1].x + image[0].overlay_button[1].dx + PAD1;
      image[0].overlay_button[2].y = image[0].overlay_button[0].y;

      image[0].overlay_button[3].x = image[0].overlay_button[2].x + image[0].overlay_button[2].dx + PAD1;
      image[0].overlay_button[3].y = image[0].overlay_button[0].y;

      image[0].hms_button.x = image[0].overlay_button[3].x + image[0].overlay_button[3].dx + PAD1;
      image[0].hms_button.y = image[0].overlay_button[0].y;

      image[0].PS_button.x = image[0].zoom.x;
      image[0].PS_button.y = image[0].overlay_button[0].y + BUTTON_HEIGHT + PAD1;

      /** everything below is tied to the PS_button in y-dir + the neighbor in x-dir **/
      image[0].grey_button.x = image[0].PS_button.x + image[0].PS_button.dx + PAD1;
      image[0].grey_button.y = image[0].PS_button.y;

      image[0].rainbow_button.x = image[0].grey_button.x + image[0].grey_button.dx + PAD1;
      image[0].rainbow_button.y = image[0].PS_button.y;

      image[0].puns_button.x = image[0].rainbow_button.x + image[0].rainbow_button.dx + PAD1;
      image[0].puns_button.y = image[0].PS_button.y;

      image[0].recenter_button.x = image[0].puns_button.x + image[0].puns_button.dx + PAD1;
      image[0].recenter_button.y = image[0].PS_button.y;
      break;

    default:
      abort ();
      break;
  }

  if (USE_XWINDOW) {
    CreatePicture (image, graphic);
    CreateColorbar (image, graphic);
    CreateZoom (image, graphic, 0, 0); 
  }
  Remap (graphic, image, &image[0].matrix);

  return;
}
