/*
** GHWindows.c
** Copyright (C) 1996-97 Serge Emond
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation; either version 2
** of the License, or (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/// Protos
#include "GrabHTTP.h"
#include "GHWindows_protos.h"
///

/// Defines
#define TOPSKIP     3
#define LEFTSKIP    4
#define RIGHTSKIP   4
#define BOTTOMSKIP  3
#define HSKIP       8
#define VSKIP       1
#define BIGVSKIP    3
#define TSKIP       3
#define TXTHADD     4

#define FNTW        (w->winfo.fntw)
#define FNTH        (w->winfo.fnth)
#define TXTH        (FNTH + 4)

#define RECD_WIDTH  (17)
#define TIME_WIDTH  (8)

#define MAX(a,b)    (a>b ? (a) : (b))
///

struct TextAttr Topaz80 = { "topaz.font", 8, 0, 0, };

/// BOOL CalcWinInfo (struct Specific *)
BOOL CalcWinInfo(struct Specific *w)
{
    struct IntuiText itext = { 0, 0, 0, 0, 0, 0, 0, 0};

    itext.ITextFont = w->winfo.tattr;

    // URL: vs file:
    itext.IText = w->wtexts->url;
    w->winfo.urlt_tl = IntuiTextLength(&itext);
    itext.IText = w->wtexts->file;
    w->winfo.filet_tl = IntuiTextLength(&itext);
    w->winfo.urlt_l = MAX(w->winfo.urlt_tl, w->winfo.filet_tl)+1;

    // Received vs chars/sec
    itext.IText = w->wtexts->recd;
    w->winfo.recdt_tl = IntuiTextLength(&itext);
    itext.IText = w->wtexts->cps;
    w->winfo.cpst_tl = IntuiTextLength(&itext);
    w->winfo.col1t_l = MAX(w->winfo.recdt_tl, w->winfo.cpst_tl)+1;

    // Actual vs remaining
    itext.IText = w->wtexts->actual;
    w->winfo.actt_tl = IntuiTextLength(&itext);
    itext.IText = w->wtexts->rem;
    w->winfo.remt_tl = IntuiTextLength(&itext);
    w->winfo.col2t_l = MAX(w->winfo.actt_tl, w->winfo.remt_tl)+1;

    w->winfo.col1_l = RECD_WIDTH*w->winfo.fntw + TXTHADD;
    w->winfo.col2_l = TIME_WIDTH*w->winfo.fntw + TXTHADD;

    w->winfo.long_l = w->winfo.col1t_l + w->winfo.col2t_l + HSKIP + 2*TSKIP + w->winfo.col1_l + w->winfo.col2_l;

    w->winfo.win_w = w->winfo.long_l + LEFTSKIP + RIGHTSKIP + w->scr->WBorLeft + w->scr->WBorRight;
    if (w->winfo.win_w > w->scr->Width)
        return(FALSE);

    w->winfo.url_l = w->winfo.long_l - w->winfo.urlt_l - TSKIP;

    w->winfo.long_x = w->scr->WBorLeft + LEFTSKIP;
    w->winfo.url_x = w->winfo.long_x + w->winfo.urlt_l + TSKIP;
    w->winfo.url_y = w->scr->WBorTop + TOPSKIP + w->scr->RastPort.Font->tf_YSize + 1;
    w->winfo.file_y = w->winfo.url_y + VSKIP + TXTH;

    w->winfo.col1_x = w->winfo.long_x + w->winfo.col1t_l + TSKIP;
    w->winfo.col2t_x = w->winfo.col1_x + HSKIP + w->winfo.col1_l;
    w->winfo.col2_x = w->winfo.col2t_x + TSKIP + w->winfo.col2t_l;

    w->winfo.line1_y = w->winfo.file_y + TXTH + BIGVSKIP;
    w->winfo.line2_y = w->winfo.line1_y + TXTH + VSKIP;

    w->winfo.long1_y = w->winfo.line2_y + TXTH + BIGVSKIP;
    w->winfo.long2_y = w->winfo.long1_y + TXTH + VSKIP;

    w->winfo.but_y = w->winfo.long2_y + 2*(TXTH + BIGVSKIP);
    w->winfo.but_l = (w->winfo.long_l - HSKIP) / 2;
    w->winfo.but2_x = w->winfo.long_x + w->winfo.but_l + HSKIP;

    w->winfo.win_h = w->winfo.but_y + TXTH + BOTTOMSKIP + 1;
    if (w->winfo.win_h > w->scr->Height)
        return(FALSE);

    w->winfo.prog_x1 = w->winfo.long_x + 1;
    w->winfo.prog_l  = w->winfo.long_l - 3;
    w->winfo.prog_x2 = w->winfo.long_x + w->winfo.prog_l + 1;
    w->winfo.prog_y1 = w->winfo.long2_y + TXTH + BIGVSKIP + 1;
    w->winfo.prog_y2 = w->winfo.prog_y1 + TXTH - 3;

    w->winfo.win_x = ((LONG)w->scr->Width - w->winfo.win_w) / 2L;
    w->winfo.win_y = ((LONG)w->scr->Height - w->winfo.win_h) / 2L;

    // V39 fix of position of texts..
    w->winfo.urlt_x  = w->winfo.long_x  + w->winfo.urlt_l  - w->winfo.urlt_tl - 1;
    w->winfo.filet_x = w->winfo.long_x  + w->winfo.urlt_l  - w->winfo.filet_tl - 1;
    w->winfo.recdt_x = w->winfo.long_x  + w->winfo.col1t_l - w->winfo.recdt_tl - 1;
    w->winfo.cpst_x  = w->winfo.long_x  + w->winfo.col1t_l - w->winfo.cpst_tl - 1;
    w->winfo.actt_x  = w->winfo.col2t_x + w->winfo.col2t_l - w->winfo.actt_tl - 1;
    w->winfo.remt_x  = w->winfo.col2t_x + w->winfo.col2t_l - w->winfo.remt_tl - 1;

}
///

/// void SetProgressBar(struct Specific *w, long perc)
void SetProgressBar(struct Specific *w, LONG pct)
{
    struct IntuiText xit = {1, 0, JAM1, 0, 0, 0, 0, 0};
    TEXT txt[12];

    if (w->w) {
        w->BarValue = pct;
        SetAPen(w->w->RPort, 0);
        RectFill(w->w->RPort, w->winfo.prog_x1, w->winfo.prog_y1, w->winfo.prog_x2, w->winfo.prog_y2);
        if (pct >= 0) {
            SetAPen(w->w->RPort, -1);
            RectFill(w->w->RPort, w->winfo.prog_x1, w->winfo.prog_y1,
                    (w->winfo.prog_l * pct / 100L) + w->winfo.prog_x1, w->winfo.prog_y2);

            stcul_d(txt, pct);
            strcat(txt, "%");
            xit.IText = txt;
            xit.ITextFont = w->winfo.tattr;
            PrintIText(w->w->RPort, &xit, (w->winfo.prog_l-IntuiTextLength(&xit))/2 + w->winfo.prog_x1,
                                           w->winfo.prog_y1+1);
        }

    }
}
///

/// void SetProgressSize(struct Specific *w, long size, long recd)
void SetProgressSize(struct Specific *w, long size, long recd)
{
    char txt[25];

    if (w->w) {
        if (recd || size) {
            txt[0] = 0;
            stcul_d(txt, recd);
            if (size) {
                strcat(txt, "/");
                stcul_d(strlen(txt)+txt, size);
            }
            if (w->gtxt.recd.p)
                FreeMem(w->gtxt.recd.p, w->gtxt.recd.s);
            w->gtxt.recd.s = strlen(txt)+1;
            if (w->gtxt.recd.s && (w->gtxt.recd.p = (TEXT *)AllocMem(w->gtxt.recd.s, MEMF_REVERSE)))
                strcpy(w->gtxt.recd.p, txt);
            GT_SetGadgetAttrs(w->g.recd, w->w, NULL,
                GTTX_Text, w->gtxt.recd.p, TAG_END);
        }
        else {
            if (w->gtxt.recd.p)
                FreeMem(w->gtxt.recd.p, w->gtxt.recd.s);
            w->gtxt.recd.p=NULL;
            GT_SetGadgetAttrs(w->g.recd, w->w, NULL,
                GTTX_Text, NULL, TAG_END);
        }
    }
}
///

/// void SetProgressActual(struct Specific *w, long hour, long mins, long secs)
void SetProgressActual(struct Specific *w, long hour, long mins, long secs)
{
    char txt[12];

    if (w->w) {
        if (secs < 0 || mins < 0 || hour < 0) {
            strcpy(txt,"--:--:--");
        }
        else {
            sprintf(txt, "%02lu:%02lu:%02lu", hour, mins, secs);
        }
        if (w->gtxt.actual.p)
            FreeMem(w->gtxt.actual.p, w->gtxt.actual.s);
        w->gtxt.actual.s = strlen(txt)+1;
        if (w->gtxt.actual.s && (w->gtxt.actual.p = (TEXT *)AllocMem(w->gtxt.actual.s, MEMF_REVERSE)))
            strcpy(w->gtxt.actual.p, txt);
        GT_SetGadgetAttrs(w->g.actual, w->w, NULL,
            GTTX_Text, w->gtxt.actual.p, TAG_END);
    }

}
///

/// void SetProgressRemaining(struct Specific *w, long hour, long mins, long secs)
void SetProgressRemaining(struct Specific *w, long hour, long mins, long secs)
{
    char txt[12];

    if (w->w) {
        if (secs < 0 || mins < 0 || hour < 0) {
            strcpy(txt, "--:--:--");
        }
        else {
            sprintf(txt, "%02lu:%02lu:%02lu", hour, mins, secs);
        }
        if (w->gtxt.rem.p)
            FreeMem(w->gtxt.rem.p, w->gtxt.rem.s);
        w->gtxt.rem.s = strlen(txt)+1;
        if (w->gtxt.rem.s && (w->gtxt.rem.p = (TEXT *)AllocMem(w->gtxt.rem.s, MEMF_REVERSE)))
            strcpy(w->gtxt.rem.p, txt);
        GT_SetGadgetAttrs(w->g.rem, w->w, NULL,
            GTTX_Text, w->gtxt.rem.p, TAG_END);
    }

}
///

/// void SetProgresscps(struct Specific *w, long cps)
void SetProgresscps(struct Specific *w, long cps)
{
    char txt[12];

    if (w->w) {
        if (cps > 0) {
            stcul_d(txt, cps);
        }
        else {
            txt[0]=0;
        }
        if (w->gtxt.cps.p)
            FreeMem(w->gtxt.cps.p, w->gtxt.cps.s);
        w->gtxt.cps.s = strlen(txt)+1;
        if (w->gtxt.cps.s && (w->gtxt.cps.p = (TEXT *)AllocMem(w->gtxt.cps.s, MEMF_REVERSE)))
            strcpy(w->gtxt.cps.p, txt);
        GT_SetGadgetAttrs(w->g.cps, w->w, NULL,
            GTTX_Text, w->gtxt.cps.p, TAG_END);
    }
}
///

/// void SetProgressStatus(struct Specific *w, TEXT *status)
void SetProgressStatus(struct Specific *w, TEXT *status)
{
    if (w->w) {
        if (w->gtxt.stat.p)
            FreeMem(w->gtxt.stat.p, w->gtxt.stat.s);
        w->gtxt.stat.s = strlen(status)+1;
        if (w->gtxt.stat.s && (w->gtxt.stat.p = (TEXT *)AllocMem(w->gtxt.stat.s, MEMF_REVERSE)))
            strcpy(w->gtxt.stat.p, status);
        GT_SetGadgetAttrs(w->g.stat, w->w, NULL,
            GTTX_Text, w->gtxt.stat.p, TAG_END);
    }
}
///

/// void SetProgressUser(struct Specific *w, TEXT *status)
void SetProgressUser(struct Specific *w, TEXT *status)
{
    if (w->gtxt.user.p)
        FreeMem(w->gtxt.user.p, w->gtxt.user.s);
    w->gtxt.user.s = strlen(status)+1;
    if (w->gtxt.user.s && (w->gtxt.user.p = (TEXT *)AllocMem(w->gtxt.user.s, MEMF_REVERSE)))
        strcpy(w->gtxt.user.p, status);
    if (w->w) {
        GT_SetGadgetAttrs(w->g.user, w->w, NULL,
            GTTX_Text, (w->gtxt.user.p ? w->gtxt.user.p : NULL), TAG_END);
    }
}
///

/// void SetProgressURL(struct Specific *w, TEXT *url)
void SetProgressURL(struct Specific *w, TEXT *url)
{

/*    if (strlen(url) > 50) {
        fpos = strchr(url, '/');
        fpos = strchr(fpos+1, '/');
        fpos = strchr(fpos+1, '/') + 1;
        strncpy(tmpt, url, (int)fpos - (int)url);
        tmpt[(int)fpos - (int)url] = 0;
        strcat(tmpt, ".../");
        lpos = FilePart(fpos);
        strcat(tmpt, lpos);
        if (strlen(tmpt) > 50)
            tmpt[50]=0;
    }
    else {
        strcpy(tmpt, url);
    }

    if (w->w) {
        SetAPen(w->w->RPort, 0);
        RectFill(w->w->RPort, w->wi->url_x, w->wi->url_l,
                              w->wi->url_x + w->wi->url_w,
                              w->wi->url_l + w->wi->fnt.ta_YSize);

        if (url != 0) {
            xit.ITextFont = &w->wi->fnt;
            xit.IText = tmpt;
            PrintIText(w->w->RPort, &xit, w->wi->url_x, w->wi->url_l);
        }
    } */
    
    if (w->w) {
        if (w->gtxt.url.p)
            FreeMem(w->gtxt.url.p, w->gtxt.url.s);
        w->gtxt.url.s = strlen(url)+1;
        if (w->gtxt.url.s && (w->gtxt.url.p = (TEXT *)AllocMem(w->gtxt.url.s, MEMF_REVERSE)))
            strcpy(w->gtxt.url.p, url);
        GT_SetGadgetAttrs(w->g.url, w->w, NULL,
            GTTX_Text, (w->gtxt.url.p ? w->gtxt.url.p : NULL), TAG_END);
    }
}
///

/// void SetProgressFile(struct Specific *w, TEXT *file)
void SetProgressFile(struct Specific *w, TEXT *file)
{

/*    if (strlen(file) > 50) {
        strcpy(tmpt, ".../");
        strcat(tmpt, FilePart(file));
        tmpt[50]=0;
    }
    else {
        strcpy(tmpt, file);
    }

    if (w->w) {
        SetAPen(w->w->RPort, 0);
        RectFill(w->w->RPort, w->wi->url_x, w->wi->file_l,
                              w->wi->url_x + w->wi->url_w,
                              w->wi->file_l + w->wi->fnt.ta_YSize);

        if (file != 0) {
            xit.ITextFont = &w->wi->fnt;
            xit.IText = tmpt;
            PrintIText(w->w->RPort, &xit, w->wi->url_x, w->wi->file_l);
        }
    } */
    
    if (w->w) {
        if (w->gtxt.file.p)
            FreeMem(w->gtxt.file.p, w->gtxt.file.s);
        w->gtxt.file.s = strlen(file)+1;
        if (w->gtxt.file.s && (w->gtxt.file.p = (TEXT *)AllocMem(w->gtxt.file.s, MEMF_REVERSE)))
            strcpy(w->gtxt.file.p, file);
        GT_SetGadgetAttrs(w->g.file, w->w, NULL,
            GTTX_Text, w->gtxt.file.p, TAG_END);
    }
    
    
}
///

/// struct Gadget *CreateMyGadgets(struct Specific *w)
struct Gadget *CreateMyGadgets(struct Specific *w)
{
    struct Gadget *gad;
    struct NewGadget ng;

    memset(&ng, NULL, sizeof(struct NewGadget));

    gad = CreateContext(&w->glist);

    ng.ng_TextAttr      = w->winfo.tattr;
    ng.ng_VisualInfo    = w->vi;
    ng.ng_LeftEdge      = w->winfo.url_x;
    ng.ng_TopEdge       = w->winfo.url_y;
    ng.ng_Width         = w->winfo.url_l;
    ng.ng_Height        = TXTH;
    w->g.url = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      NULL,
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);

    ng.ng_LeftEdge      = w->winfo.urlt_x;
    ng.ng_Width         = w->winfo.urlt_l;
    gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      w->wtexts->url,
        TAG_END,        NULL);

    ng.ng_LeftEdge      = w->winfo.url_x;
    ng.ng_TopEdge       = w->winfo.file_y;
    ng.ng_Width         = w->winfo.url_l;
    w->g.file = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      NULL,
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);

    ng.ng_LeftEdge      = w->winfo.filet_x;
    ng.ng_Width         = w->winfo.urlt_l;
    gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      w->wtexts->file,
        TAG_END,        NULL);

    // Second serie
    ng.ng_LeftEdge      = w->winfo.recdt_x;
    ng.ng_TopEdge       = w->winfo.line1_y;
    ng.ng_Width         = w->winfo.col1t_l;
    gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      w->wtexts->recd,
        TAG_END,        NULL);
    ng.ng_TopEdge       = w->winfo.line2_y;
    ng.ng_LeftEdge      = w->winfo.cpst_x;
    gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      w->wtexts->cps,
        TAG_END,        NULL);
    ng.ng_LeftEdge      = w->winfo.actt_x;
    ng.ng_TopEdge       = w->winfo.line1_y;
    ng.ng_Width         = w->winfo.col2t_l;
    gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      w->wtexts->actual,
        TAG_END,        NULL);
    ng.ng_TopEdge       = w->winfo.line2_y;
    ng.ng_LeftEdge      = w->winfo.remt_x;
    gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      w->wtexts->rem,
        TAG_END,        NULL);
    ng.ng_LeftEdge      = w->winfo.col1_x;
    ng.ng_TopEdge       = w->winfo.line1_y;
    ng.ng_Width         = w->winfo.col1_l;
    w->g.recd = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      NULL,
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);
    ng.ng_TopEdge       = w->winfo.line2_y;
    w->g.cps = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      NULL,
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);
    ng.ng_LeftEdge      = w->winfo.col2_x;
    ng.ng_TopEdge       = w->winfo.line1_y;
    ng.ng_Width         = w->winfo.col2_l;
    w->g.actual = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      NULL,
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);
    ng.ng_TopEdge       = w->winfo.line2_y;
    w->g.rem = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      NULL,
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);

    // 3 boxes
    ng.ng_LeftEdge      = w->winfo.long_x;
    ng.ng_Width         = w->winfo.long_l;
    ng.ng_TopEdge       = w->winfo.long1_y;
    w->g.stat = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      NULL,
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);
    ng.ng_TopEdge       = w->winfo.long2_y;
    w->g.user = gad = CreateGadget(TEXT_KIND, gad, &ng,
        GTTX_Text,      (w->gtxt.user.p ? w->gtxt.user.p : NULL),
        GTTX_Border,    TRUE,
        GTTX_Justification, GTJ_LEFT,
        GTTX_Clipped,   TRUE,
        TAG_END,        NULL);

    // 2 buttons...
    ng.ng_Width         = w->winfo.but_l;
    ng.ng_TopEdge       = w->winfo.but_y;
    ng.ng_GadgetText    = w->wtexts->skip;
    ng.ng_GadgetID      = GADGET_SKIP;
    w->g.skip = gad = CreateGadget(BUTTON_KIND, gad, &ng,
        GT_Underscore,  '_',
        TAG_END,        NULL);
    ng.ng_LeftEdge      = w->winfo.but2_x;
    ng.ng_GadgetText    = w->wtexts->abort;
    ng.ng_GadgetID      = GADGET_ABORT;
    w->g.abort = gad = CreateGadget(BUTTON_KIND, gad, &ng,
        GT_Underscore,  '_',
        TAG_END,        NULL);

    return(gad);
}
///

/// struct Window *OpenProgressWindow(struct Specific *w)
struct Window *OpenProgressWindow(struct Specific *w)
{
    BOOL ret;
    struct Gadget *gad;
    
    if (w->w) return (w->w);
    
    if (w->scr = LockPubScreen(NULL)) {
        if (w->vi = GetVisualInfo(w->scr, TAG_END)) {
            
            w->winfo.tattr = w->scr->Font;
            w->winfo.fnth = w->scr->RastPort.Font->tf_YSize;
            w->winfo.fntw = w->scr->RastPort.Font->tf_XSize;
            
            if (!(ret = CalcWinInfo(w))) {
                w->winfo.fnth = 8;
                w->winfo.fntw = 8;
                w->winfo.tattr = &Topaz80;
                ret = CalcWinInfo(w);
            }
            
            if (ret) {
                if (gad = CreateMyGadgets(w)) {
                    if (w->w = OpenWindowTags(NULL,
                        WA_Top,         w->winfo.win_y,
                        WA_Left,        w->winfo.win_x,
                        WA_Width,       w->winfo.win_w,
                        WA_Height,      w->winfo.win_h,
                        WA_IDCMP,       IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | BUTTONIDCMP | IDCMP_VANILLAKEY,
                        WA_AutoAdjust,  0,
                        WA_PubScreen,   w->scr,
                        WA_RMBTrap,     1,
                        WA_DragBar,     1,
                        WA_DepthGadget, 1,
                        WA_CloseGadget, 1,
                        WA_Gadgets,     w->glist,
                        WA_Title,       "GrabHTTP v"SVER" (c) Serge Emond, 1996-97",
                        NULL, NULL)) {
                        
                        GT_RefreshWindow(w->w, NULL);
                        DrawBevelBox(w->w->RPort, w->winfo.long_x, w->winfo.prog_y1-1,
                                     w->winfo.long_l, TXTH,
                                     GT_VisualInfo,     w->vi,
                                     GTBB_Recessed,     TRUE,
                                     TAG_END,           NULL);
                        SetProgressBar(w, -1L);
                        
                        return(w->w);
                    }
                    FreeGadgets(w->glist);
                }
            }
            FreeVisualInfo(w->vi);
        }
        UnlockPubScreen(NULL, w->scr);
    }
    return(NULL);
}
///

/// LONG CheckWindow(struct Specific *, long)
LONG CheckWindow(struct Specific *w, long oret)
{
    struct IntuiMessage *imsg;
    LONG ret;
    
    ret=0;
    if (w->w) {
        if (CheckSignal(1L << w->w->UserPort->mp_SigBit)) {
            // Messages pending

            while (imsg = GT_GetIMsg(w->w->UserPort)) {

                switch (imsg->Class) {
                    case IDCMP_VANILLAKEY:
                        if (imsg->Code == 's')
                            ret = 201;
                        else if (imsg->Code == 'a')
                            ret = 202;
                        break;
                    case IDCMP_GADGETUP:
                        if (((struct Gadget *)(imsg->IAddress))->GadgetID == GADGET_SKIP)
                            ret = 201;
                        else if (((struct Gadget *)(imsg->IAddress))->GadgetID == GADGET_ABORT)
                            ret = 202;
                        break;
                    case IDCMP_CLOSEWINDOW:
                        ret = 202;
                        break;
                    case IDCMP_REFRESHWINDOW:
                        GT_BeginRefresh(w->w);
                        DrawBevelBox(w->w->RPort, w->winfo.long_x, w->winfo.prog_y1-1,
                            w->winfo.long_l, TXTH,
                            GT_VisualInfo,     w->vi,
                            GTBB_Recessed,     TRUE,
                            TAG_END,           NULL);
                        SetProgressBar(w, w->BarValue);
                        GT_EndRefresh(w->w, TRUE);
                        break;
                }
                GT_ReplyIMsg(imsg);
            }
        }
    }
    if (ret) return(ret);
    return(oret);
}
///
