/*
 * Progress.c - for showing a progress window on Imagemaster
 */
#include "system.h"

struct Library *IntuitionBase;
struct Library *GfxBase;
struct Library *ExecBase;

int openlibraries()
  {
    if (!(ExecBase = OpenLibrary("exec.library",37)))
      {
        printf("Could not open exec.library\n");
        return(1);
      }
    if (!(IntuitionBase = OpenLibrary("intuition.library",0)))
      {
        printf("Could not open intuition.library\n");
        return(2);
      }
    if (!(GfxBase = OpenLibrary("graphics.library",0)))
      {
        printf("Could not open graphics.library\n");
        return(3);
      }
    return(0);
  }

int closelibraries()
  {
    if (GfxBase)       CloseLibrary(GfxBase);
    if (IntuitionBase) CloseLibrary(IntuitionBase);
    if (ExecBase)      CloseLibrary(ExecBase);
    return(0);
  }

struct NewWindow newwindow = {
  20,10,  /* window XY origin relative to TopLeft of screen */
  600,38,  /* window width and height */
  0,1,  /* detail and block pens */
  MOUSEBUTTONS|RAWKEY|MOUSEMOVE,  /* IDCMP flags */
  RMBTRAP|BORDERLESS|NOCAREREFRESH|REPORTMOUSE,  /* other window flags */
  NULL,  /* first gadget in gadget list */
  NULL,  /* custom CHECKMARK imagery */
  NULL,  /* window title */
  NULL,  /* custom screen pointer */
  NULL,  /* custom bitmap */
  5,5,  /* minimum width and height */
  640,200,  /* maximum width and height */
  CUSTOMSCREEN  /* destination screen type */
};

static struct Window *pwindow;      /* The progress window         */
static int barx1,barx2,bary1,bary2; /* The progress bar location   */
static int lastpos,maxpos;          /* The current progress extent */

#define BLACK   0 /* Imagemaster's panel colors */
#define WHITE   1
#define GRAY1   2
#define GRAY3   3
#define RED     4
#define BLUE    5
#define GREEN   6
#define GRAY2   7

int progressbegin(struct Screen *imscr,int max,char *title)
  {
  int xw,yw,p;
  struct RastPort *rp;
  
    /* First: Open the window */
    if (!imscr)
      {
        printf("The Imagemaster screen was not located\n");
        return(1);
      }
    newwindow.Screen = imscr;
    if ((pwindow=(struct Window *)OpenWindow(&newwindow)) == NULL)
      {
        printf("Open window failed\n");
        return(2);
      }
    
    /* Next: draw the background */
    /* We do not use black because it is transparent when overlayed */
    xw = pwindow->Width;
    yw = pwindow->Height;
    rp = pwindow->RPort;
    SetAPen(rp,GRAY2);
    RectFill(rp,0,0,xw-1,yw-1);
    SetAPen(rp,GRAY3);
    Move(rp,0,yw-1);
    Draw(rp,0,0);
    Draw(rp,xw-1,0);
    Move(rp,1,yw-2);
    Draw(rp,1,1);
    Draw(rp,xw-2,1);
    SetAPen(rp,GRAY1);
    Move(rp,1,yw-1);
    Draw(rp,xw-1,yw-1);
    Draw(rp,xw-1,1);
    Move(rp,2,yw-2);
    Draw(rp,xw-2,yw-2);
    Draw(rp,xw-2,2);
    
    /* Next: draw the bar */
    barx1 = 20;
    barx2 = xw-20;
    bary1 = 10;
    bary2 = 16;
    SetAPen(rp,GRAY3);
    Move(rp,barx1-2,bary2+2);
    Draw(rp,barx1-2,bary1-2);
    Draw(rp,barx2+2,bary1-2);
    SetAPen(rp,GRAY1);
    Move(rp,barx2+2,bary1-1);
    Draw(rp,barx2+2,bary2+2);
    Draw(rp,barx1-1,bary2+2);
    maxpos = max;
    if (maxpos == 0) maxpos = 1;
    lastpos = 0;
    
    /* Next Draw the text */
    p = (barx1+barx2)/2 - strlen(title)*4;
    SetAPen(rp,GRAY1);
    SetDrMd(rp,JAM1);
    Move(rp,p,pwindow->Height-7);
    Text(rp,title,strlen(title));
    Move(rp,barx1-8,bary2+10);
    Text(rp,"0 %",3);
    Move(rp,barx2-24,bary2+10);
    Text(rp,"100 %",5);
    
    return(0);
  }

int progressupdate(int y)
  {
  int x1,x2;
  struct RastPort *rp;
  
    /* Some safety checks */
    if (!pwindow) return(0);
    if (y < 0) y = 0;
    if (y > maxpos) y = maxpos;
    rp = pwindow->RPort;
    
    /* Determine how much to redraw */
    x1 = barx1 + lastpos * (barx2-barx1) / maxpos;
    x2 = barx1 + y       * (barx2-barx1) / maxpos;
    
    /* Now draw in the achieved area */
    
    if (x2 < x1)            /* We went backwards */
      {
        SetAPen(rp,GRAY1);
        RectFill(rp,x2,bary1,x1,bary2);
      }
    else if (x1 < x2)       /* This is forewards */
      {
        SetAPen(rp,BLUE);
        RectFill(rp,x1,bary1,x2,bary2);
      }
    
    lastpos = y;
    return(0);
  }

int progressend()
  {
    if (!pwindow) return(1);
    CloseWindow(pwindow);
    pwindow = NULL;
    return(0);
  }