/* coordinate window routines */

#include "PlotMap.h"

/********** externals **********/

extern struct Screen *screen;
extern struct TextAttr myfont;
extern struct RastPort *rp;
extern struct Menu *menu;
extern struct config config;

extern WORD calc_lon(WORD), calc_lat(WORD);

/********** Variablen, Daten **********/

struct Window *coord_wd = NULL;
static struct RastPort *coord_rp;

/********** Routinen **********/

void refresh_coord(WORD mx, WORD my)
{
   char str[20];
   struct TextFont *font;
   WORD left, top;

   if (coord_wd)                          /* window may be closed! */
   {
      font = coord_rp->Font;
      left = coord_wd->BorderLeft + 2;
      top = coord_wd->BorderTop + 1 + font->tf_Baseline;
      Move(coord_rp, left, top);          /* go to initial pos. */

      if (config.coord_mousexy)           /* print mouse-coords */
      {
         sprintf(str, "X: %4d  Y: %3d", mx,my);
         Text(coord_rp, str, 15);

         Move(coord_rp, left, top + font->tf_YSize+2);   /* next line */
      }

      if (config.coord_degree)         /* print longitude and lattitude */
      {
         WORD lon = calc_lon(mx), lat = calc_lat(my);
         char lon_c = ' ', lat_c = ' ';

         if (lon < 0)            /* longitude negativ means */
         {
            lon_c = 'W';         /* west */
            lon = -lon;
         }
         else if (lon > 0)
            lon_c = 'O';
         if (lat < 0)            /* lattitude negativ means */
         {
            lat_c = 'S';         /* south hemisphere */
            lat = -lat;
         }
         else if (lat > 0)
            lat_c = 'N';

         sprintf(str, "Lon: %3d° %2d' %c", lon/60, lon%60, lon_c);
         Text(coord_rp, str, 15);
         Move(coord_rp, left, coord_rp->cp_y + font->tf_YSize+2);
         sprintf(str, "Lat: %3d° %2d' %c", lat/60, lat%60, lat_c);
         Text(coord_rp, str, 15);
      } /* if (config.coord_degree) */
   } /* if (coord_wd) */
}


void set_coord_window_size(void)
{
   WORD wd_y, wd_h;

   if (coord_wd)        /* if user changes display while window closed */
   {
      wd_h = screen->WBorTop + myfont.ta_YSize + screen->WBorBottom +
               (config.coord_mousexy + 2*config.coord_degree) *
               (myfont.ta_YSize+2);

      wd_y = coord_wd->TopEdge;              /* get top edge */
      if (wd_y + wd_h > screen->Height)      /* move window up a bit */
         wd_y = screen->Height - wd_h - 1;   /* if too far down */

      ChangeWindowBox(coord_wd, coord_wd->LeftEdge, wd_y,
            coord_wd->Width, wd_h);

      refresh_coord(main_wd->MouseX, main_wd->MouseY);
   } /* if (coord_wd) */
}


void open_coord_window(void)
{
   WORD wd_w, wd_h;

   /* calculate window width and height depending on used font and
    * displayed coordinates (mouse/degree) */
   wd_w = screen->WBorLeft + screen->WBorRight + 5 +
         max(TextLength(rp, "X: 0000  Y: 000", 15),
               TextLength(rp, "Lon: 000° 00` W", 15));
   wd_h = screen->WBorTop + myfont.ta_YSize + screen->WBorBottom +
      (config.coord_mousexy + 2*config.coord_degree) * (myfont.ta_YSize+2);

   /* open window at old position */
   if (coord_wd = OpenWindowTags(NULL,
            WA_Left,config.coord_wdx,  WA_Top,config.coord_wdy,
            WA_Width,wd_w,  WA_Height,wd_h,
            WA_CustomScreen,screen,
            WA_Title,"Coords",  WA_AutoAdjust,TRUE,
            WA_Flags, WFLG_NOCAREREFRESH | WFLG_SIMPLE_REFRESH |
               WFLG_DRAGBAR | WFLG_CLOSEGADGET | WFLG_RMBTRAP,
            WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_ACTIVEWINDOW,
            TAG_DONE))
   {
      coord_rp = coord_wd->RPort;            /* init. rastport */
      SetAPen(coord_rp, 1);
      SetBPen(coord_rp, 0);
      SetDrMd(coord_rp, JAM2);

      refresh_coord(main_wd->MouseX, main_wd->MouseY);
   }
   else
      TITLE_ERROR("Can`t open window!");
}


void close_coord_window(void)
{
   if (coord_wd)                       /* why close if it is closed? */
   {
      config.coord_wdx = coord_wd->LeftEdge;    /* remember old position */
      config.coord_wdy = coord_wd->TopEdge;

      CloseWindow(coord_wd);
      coord_wd = NULL;
   }
}


void handlecoordIDCMP(void)
{
   struct IntuiMessage *msg;
   ULONG class;

   while (coord_wd && (msg = (struct IntuiMessage *)GetMsg(coord_wd->UserPort)))
   {
      class = msg->Class;
      ReplyMsg((struct Message *)msg);

      switch (class)
      {
         case IDCMP_ACTIVEWINDOW:      /* do not allow activation of me */
            ActivateWindow(main_wd);
            break;
         case IDCMP_CLOSEWINDOW:
            config.coord_window = FALSE;
            ClearMenuStrip(main_wd);         /* clear menu`s checkmark */
            ItemAddress(menu, FULLMENUNUM(1,4,NOSUB))->Flags &= ~CHECKED;
            ResetMenuStrip(main_wd, menu);
            close_coord_window();            /* sets coord_wd to NULL */
            break;
      } /* switch (class) */
   } /* while (GetMsg()) */
}

