#include <exec/types.h>
#include <string.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <proto/gadtools.h>
#include <proto/intuition.h>
#include <proto/icon.h>
#include <proto/graphics.h>
#include <proto/wb.h>
#include <proto/commodities.h>
#include <proto/diskfont.h>
#include <proto/graphics.h>
#include <proto/utility.h>

#include "CopyWindow.h"
#include "WBStartupPlusPrefs_Cat.h"

#define GADTEXTBORDERSUM 6


int CopyWindow(UWORD x, UWORD y, int flags)
{
  /* WORD x         The current X mouse position */
  /* WORD y         The current Y mouse position */
  /* RETURNS the new flags */

  ULONG signals;
  ULONG winsigflag;
  BOOL LOOP;
  struct IntuiMessage *winmsg;
  struct Window *win;
  struct Screen *scr;
  struct VisualInfo *vi;
  struct Gadget *gad1,*gad2,*gad3;
  struct Gadget *glist=NULL;
  struct NewGadget ng;
  ULONG winheight,winwidth;
  UBYTE *labels[3];
  UBYTE *labels2[3];
  STRPTR WinTitleString  = GetString(STRAddPrograms);
  STRPTR GadTitleString  = GetString(STRStatusOfPrograms);
  STRPTR GadTitleString2 = GetString(STROperation);
  long mxgadheight, buttongadheight;
  long active;

  int newflags=0;

  labels[0]=(UBYTE *)GetString(STREnabled);
  labels[1]=(UBYTE *)GetString(STRDisabled);
  labels[2]=NULL;

  labels2[0]=(UBYTE *)GetString(STRCopyFile);
  labels2[1]=(UBYTE *)GetString(STRMoveFile);
  labels2[2]=NULL;


  if (scr=LockPubScreen(NULL))
  {
    if (vi=GetVisualInfo(scr,TAG_END))
    {
      mxgadheight = (3 * scr->Font->ta_YSize)+6;
      buttongadheight = scr->Font->ta_YSize + 6;

      winheight = scr->WBorTop + scr->Font->ta_YSize + 1 + 4 + mxgadheight + 4 + 2 + 4 + mxgadheight + 4 + 2 + buttongadheight + scr->WBorBottom-2;
      winwidth  = (6/5*TextLength(&scr->RastPort,WinTitleString,strlen(WinTitleString))) + 63;
      if (win = OpenWindowTags(NULL,
          WA_Left,   x - (winwidth/2),
          WA_Top,    y - (winheight/2),
          WA_Width, winwidth,
          WA_Height, winheight,
          WA_AutoAdjust, TRUE,
          WA_Gadgets, glist,
          WA_IDCMP,  MXIDCMP | IDCMP_REFRESHWINDOW | IDCMP_CLOSEWINDOW | IDCMP_GADGETUP,
          WA_Flags,  WFLG_CLOSEGADGET | WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_SMART_REFRESH | WFLG_ACTIVATE,
          WA_Title,  WinTitleString,
          WA_PubScreen, scr,
          TAG_END))
      {

        gad1 = CreateContext(&glist);

        ng.ng_TopEdge = win->BorderTop + 4 + scr->Font->ta_YSize + 4;  /* The last 4 here is for the space between the gadget title and the actual gadget */
        ng.ng_Height = scr->Font->ta_YSize + 1;
        ng.ng_Width = ng.ng_Height*4/3;
        ng.ng_LeftEdge = (win->Width-(ng.ng_Width+7+TextLength(&scr->RastPort,labels[1],strlen(labels[1]))))/2;
        ng.ng_GadgetText = GadTitleString;
        ng.ng_TextAttr = NULL;//win->WScreen->Font;
        ng.ng_VisualInfo = vi;
        ng.ng_Flags = PLACETEXT_RIGHT | NG_HIGHLABEL;
        ng.ng_GadgetID = COPYGAD_ENABLED;

        gad1 = CreateGadget(MX_KIND,gad1,&ng,
          GTMX_Labels, (ULONG)&labels[0],
          GTMX_Active, (flags & WBSP_ENABLED) ? 0: 1,
          GTMX_Scaled, TRUE,
          GTMX_TitlePlace, PLACETEXT_ABOVE,
          TAG_DONE);

        ng.ng_LeftEdge = (win->Width-(ng.ng_Width+7+TextLength(&scr->RastPort,labels2[0],strlen(labels2[0]))))/2;
        ng.ng_TopEdge = win->BorderTop + 4 + mxgadheight + 4 + 4 + scr->Font->ta_YSize + 4 + 2 - 1; /* The 2 is is to make room for the 2 lines of the bevel box */
        ng.ng_GadgetText = GadTitleString2;
        ng.ng_GadgetID = COPYGAD_COPY;

        gad2 = CreateGadget(MX_KIND,gad1,&ng,
          GTMX_Labels, (ULONG)&labels2[0],
          GTMX_Active, (flags & WBSP_MOVE) ? 1 : 0,
          GTMX_Scaled, TRUE,
          GTMX_TitlePlace, PLACETEXT_ABOVE,
          TAG_DONE);

        ng.ng_LeftEdge   = win->BorderRight;
        ng.ng_Width      = (win->Width-win->BorderRight-win->BorderLeft)/2;
        ng.ng_TopEdge    = win->BorderTop + 4 + mxgadheight + 4 + 2 + 4 + mxgadheight + 4;
        ng.ng_Height     = buttongadheight;
        ng.ng_GadgetText = GetString(STROk);
        ng.ng_Flags      = 0;
        ng.ng_GadgetID   = COPYGAD_OK;

        gad3 = CreateGadget(BUTTON_KIND,gad2,&ng,
          TAG_DONE);

        ng.ng_LeftEdge   = win->BorderRight + ng.ng_Width;
        ng.ng_Width      = win->Width-win->BorderRight-win->BorderLeft-ng.ng_Width;
        ng.ng_GadgetText = GetString(STRCancel);
        ng.ng_GadgetID   = COPYGAD_CANCEL;

        CreateGadget(BUTTON_KIND,gad3,&ng,
          TAG_DONE);

        if (gad2)
        {
          winsigflag = 1L<<win->UserPort->mp_SigBit;
          AddGList(win, glist, (UWORD)~0, -1, NULL);
          RefreshGList(glist,win,NULL,-1);
          GT_RefreshWindow(win,NULL);

          DrawBevelBox(win->RPort, win->BorderLeft, win->BorderTop, win->Width-win->BorderLeft-win->BorderRight, 4+mxgadheight+4+1, GT_VisualInfo, vi, TAG_DONE);
          DrawBevelBox(win->RPort, win->BorderLeft, win->BorderTop+4+mxgadheight+4+1, win->Width-win->BorderLeft-win->BorderRight, 4+mxgadheight+4+1, GT_VisualInfo, vi, TAG_DONE);

          LOOP=TRUE;
          while (LOOP)
          {
            signals=Wait(SIGBREAKF_CTRL_C | winsigflag);

            if (signals & SIGBREAKF_CTRL_C)
              LOOP=FALSE;

            if (signals & winsigflag)
            {
              while (winmsg = (struct IntuiMessage *)GT_GetIMsg((struct MsgPort *)win->UserPort))
              {
                switch(winmsg->Class)
                {
                  case IDCMP_GADGETUP:
                    switch(((struct Gadget *)winmsg->IAddress)->GadgetID)
                    {
                      case COPYGAD_OK:
                        LOOP=FALSE;
                        break;
                      case COPYGAD_CANCEL:
                        newflags |= WBSP_CANCEL;
                        LOOP=FALSE;
                        break;
                    }
                    break;
                  case IDCMP_REFRESHWINDOW:
                    BeginRefresh(win);
                    EndRefresh(win, TRUE);
                    break;
                  case IDCMP_CLOSEWINDOW:
                    LOOP=FALSE;
                    break;
                }
                GT_ReplyIMsg(winmsg);
              }
            }  /* End of process window events */
          }   /* End of while (LOOP) */

          /* get status gadgets */
          GT_GetGadgetAttrs(gad1, win, NULL, GTMX_Active, &active);
          if (!active)
            newflags |= WBSP_ENABLED;
          GT_GetGadgetAttrs(gad2, win, NULL, GTMX_Active, &active);
          if (active)
            newflags |= WBSP_MOVE;

          RemoveGList(win,glist,-1);
        }
        FreeGadgets(glist);
        CloseWindow(win);
      }  /* End of if (OpenWindowTags()) */
      FreeVisualInfo(vi);
    }
    UnlockPubScreen(NULL,scr);
  }
  return(newflags);
}
