# include "Ximage.h"
# include "buttons.h"

/* initialization for things not specific to X */
KapaImageWidget *InitImage () {

  int i;
  KapaImageWidget *image;
  Graphic *graphic;

  graphic = GetGraphic ();

  ALLOCATE (image, KapaImageWidget, 1);
  memset (image, 0, sizeof(KapaImageWidget));

  /** set up a bunch of default things **/
  image[0].X = image[0].Y = 0;
  image[0].expand = 1;
  image[0].zero = 0;
  image[0].range = 1;
  image[0].start = 0;
  image[0].slope = 1;
  image[0].location = 4;

  image[0].coords.Npolyterms = 0;
  for (i = 0; i < NOVERLAYS; i++) {
    image[0].overlay[i].Nobjects = 0;
    ALLOCATE (image[0].overlay[i].objects, KiiOverlay, 1);  /* allocate so later free will not crash! */
    image[0].overlay[i].active = FALSE;
    image[0].overlay[i].color = graphic[0].overlay_color[i];
  }
  image[0].matrix.size = 0; /* a flag to show there is no data in the matrix */

  image[0].MovePointer = TRUE;
  image[0].DecimalDegrees  = TRUE;
  ALLOCATE (image[0].matrix.buffer, char, 1);  /* allocate so later free will not crash! */
  ALLOCATE (image[0].picture.data, char, 1);   /* allocate so later free will not crash! */
  ALLOCATE (image[0].cmapbar.data, char, 1);   /* allocate so later free will not crash! */
  ALLOCATE (image[0].zoom.data, char, 1);      /* allocate so later free will not crash! */

  InitButtonSize (&image[0].PS_button, PS_width, PS_height, PS_bits);
  InitButtonFunc (&image[0].PS_button, PSfunction);

  InitButtonSize (&image[0].grey_button, grey_width, grey_height, grey_bits);
  InitButtonFunc (&image[0].grey_button, greycolors);

  InitButtonSize (&image[0].rainbow_button, rainbow_width, rainbow_height, rainbow_bits);
  InitButtonFunc (&image[0].rainbow_button, rainbow);

  InitButtonSize (&image[0].puns_button, puns_width, puns_height, puns_bits);
  InitButtonFunc (&image[0].puns_button, puns);

  InitButtonSize (&image[0].recenter_button, recenter_width, recenter_height, recenter_bits);
  image[0].recenter_button.function_1 = Recenter;
  image[0].recenter_button.function_2 = RecenterRescale;
  image[0].recenter_button.function_3 = Rescale;

  InitButtonSize (&image[0].overlay_button[0], red_width, red_height, red_bits);
  InitButtonFunc (&image[0].overlay_button[0], Overlay0);

  InitButtonSize (&image[0].overlay_button[1], green_width, green_height, green_bits);
  InitButtonFunc (&image[0].overlay_button[1], Overlay1);

  InitButtonSize (&image[0].overlay_button[2], blue_width, blue_height, blue_bits);
  InitButtonFunc (&image[0].overlay_button[2], Overlay2);

  InitButtonSize (&image[0].overlay_button[3], yellow_width, yellow_height, yellow_bits);
  InitButtonFunc (&image[0].overlay_button[3], Overlay3);

  InitButtonSize (&image[0].hms_button, hms_width, hms_height, hms_bits);
  InitButtonFunc (&image[0].hms_button, ToggleDEG);

  return (image);
}

void InitButtonSize (Button *button, int width, int height, char *bitmap) {
  button->dx = BUTTON_WIDTH;
  button->dy = BUTTON_HEIGHT;
  button->width = width;
  button->height = height;
  button->bitmap = bitmap;
}

void InitButtonFunc (Button *button, int (*function)()) {
  button->function_1 = function;
  button->function_2 = function;
  button->function_3 = function;
}

void DrawImage (KapaImageWidget *image) {

  int i;
  Graphic *graphic;

  if (image == NULL) return;

  graphic = GetGraphic ();

  XSetForeground (graphic[0].display,  graphic[0].gc, graphic[0].fore);
  XDrawRectangle (graphic[0].display,  graphic[0].window, graphic[0].gc, 
		  image[0].picture.x,  image[0].picture.y, 
		  image[0].picture.dx+1, image[0].picture.dy+1);
  
  XPutImage (graphic[0].display, graphic[0].window, graphic[0].gc,
	     image[0].picture.pix, 0, 0, 
	     image[0].picture.x + 1, image[0].picture.y + 1, 
	     image[0].picture.dx, image[0].picture.dy);

  for (i = 0; i < NOVERLAYS; i++) {
    if (image[0].overlay[i].active) {
      PaintOverlay (graphic, image, i);
    }
  }

  if (image[0].location) {
    XPutImage (graphic[0].display, graphic[0].window, graphic[0].gc,
	       image[0].cmapbar.pix, 0, 0, 
	       image[0].cmapbar.x, image[0].cmapbar.y, 
	       image[0].cmapbar.dx, image[0].cmapbar.dy);

    CrossHairs (graphic, image);
  
    /* erase everything below zoom box, then draw */
    /*
    XSetForeground (graphic[0].display, graphic[0].gc, graphic[0].back);
    XFillRectangle (graphic[0].display, graphic[0].window, graphic[0].gc, 
		    image[0].text_x, image[0].text_x, image[0].text_dx, image[0].text_dy); 
    */    
    DrawButton (graphic, &image[0].PS_button);
    DrawButton (graphic, &image[0].recenter_button);
    DrawButton (graphic, &image[0].grey_button);
    DrawButton (graphic, &image[0].rainbow_button);
    DrawButton (graphic, &image[0].puns_button);
    DrawButton (graphic, &image[0].hms_button);

    for (i = 0; i < NOVERLAYS; i++) {
      DrawButton (graphic, &image[0].overlay_button[i]);
    }
    StatusBox (graphic, image);
  }

  FlushDisplay (graphic[0].display);
}

void FreeImage (KapaImageWidget *image) {

  int i;

  if (image == NULL) return;

  for (i = 0; i < NOVERLAYS; i++) {
    free (image[0].overlay[i].objects);
  }
  free (image[0].matrix.buffer);
  free (image[0].picture.data);
  free (image[0].cmapbar.data);
  free (image[0].zoom.data);

  free (image);
}
