/*
    GROWBAR.C

Written by DavidW
Started on 19/8/94

*/

#include <p_std.h>
#include <p_object.h>
#include <hwif.h>
#include <hwim.h>

#include <gbar.g>
#include <donewn.g>
#include "gbar.rsg"

GLREF_D VOID *w_am;
GLREF_D PR_WSERV *w_ws;
GLREF_D VOID *DatDialogPtr;


LOCAL_C VOID doDial(INT classno,INT resid,VOID *rbuf)
    {
    hOODialog(p_getlibh(CAT_GBAR_GBAR),classno,resid,rbuf);
    }

LOCAL_C VOID InitGbarData(RB_GBAR *pgb)
    {
    pgb->totloops=10;
    pgb->update=1;
    pgb->esc=TRUE;
    }

LOCAL_C VOID DisallowDialling(VOID)
    {
    w_ws->wserv.flags|=PR_WSERV_FREEFORM_DIALLING;
    }

LOCAL_C VOID MainLoop(VOID)
    {
    VOID *ao;
    RB_GBAR gb_data;

    InitGbarData(&gb_data);
    DisallowDialling();
    ao=f_newsend(CAT_GBAR_GBAR,C_BAR_AO,O_AO_INIT,&gb_data);
    FOREVER
        {
        doDial(C_DATA_DL,DL_DATA,&gb_data);
        if (gb_data.exit_code==W_KEY_ESCAPE)
            continue;
        p_send2(ao,O_AO_QUEUE);
        doDial(C_GBAR_DL,DL_GBAR,&gb_data);
        p_send2(ao,O_AO_CANCEL);
        hInfoPrint(gb_data.exit_code==W_KEY_ESCAPE?
                                                STR_CANCELLED: STR_FINISHED);
        }
    }

LOCAL_C VOID SpecificInit(VOID)
    {
    hInitAppRcb();
    hCrackCommandLine();
    }

GLDEF_C VOID main(VOID)
    {
    uCommonInit();
    SpecificInit();
    MainLoop();
    }

#pragma METHOD_CALL

#define DL_DATA_TOT     1
#define DL_DATA_UPD     2
#define DL_DATA_ESC     3

METHOD VOID data_dl_dl_dyn_init(PR_DLGBOX *self)
    {
    RB_GBAR *prbuf;

    prbuf=self->dlgbox.rbuf;
    hDlgSetNcedit(DL_DATA_TOT,prbuf->totloops);
    hDlgSetNcedit(DL_DATA_UPD,prbuf->update);
    hDlgSetChlist(DL_DATA_ESC,prbuf->esc);
    }

METHOD INT data_dl_dl_key(PR_DLGBOX *self)
    {
    RB_GBAR *prbuf;

    prbuf=self->dlgbox.rbuf;
    prbuf->totloops=hDlgSenseNcedit(DL_DATA_TOT);
    prbuf->update=hDlgSenseNcedit(DL_DATA_UPD);
    prbuf->esc=hDlgSenseChlist(DL_DATA_ESC);
    return(WN_KEY_CHANGED);
    }

METHOD VOID gbar_dl_dl_dyn_init(PR_DLGBOX *self)
    {
    RB_GBAR *prbuf;
    SE_DONEWN set;

    prbuf=self->dlgbox.rbuf;
    if (prbuf->esc)
        p_send3(self,O_DL_ITEM_APPEND,ITEM_ABANDON);
    set.flags=SE_DONEWN_RANGE;
    set.range=prbuf->totloops;
    hDlgSet(1,&set);
    prbuf->toupdate=prbuf->update;
    prbuf->loopsleft=prbuf->totloops;
    }

METHOD VOID gbar_dl_gbar_update(PR_DLGBOX *self)
    {
    RB_GBAR *prbuf;
    SE_DONEWN set;

    prbuf=self->dlgbox.rbuf;
    set.flags=SE_DONEWN_VALUE;
    set.val=prbuf->totloops-prbuf->loopsleft;
    hDlgSet(1,&set);
    }

METHOD INT gbar_dl_dl_key(PR_DLGBOX *self)
    {
    RB_GBAR *prbuf;

    prbuf=self->dlgbox.rbuf;
    return(prbuf->esc? WN_KEY_CHANGED: WN_KEY_NO_CHANGE);
    }

METHOD VOID gbar_dl_wn_sense_help(VOID *self)
    {
    p_leave(RUN_ACTIVE_USED);
    }

METHOD VOID bar_ao_ao_init(PR_BAR_AO *self,RB_GBAR *prb)
    {
    p_send3(w_am,O_AM_ADD_TASK,self);
    self->bar_ao.prb=prb;
    }

METHOD INT bar_ao_ao_run(PR_BAR_AO *self)
    {
    RB_GBAR *prb;

    p_sleep(5);       /* the next stage of computation or drawing etc */
    p_send2(self,O_AO_QUEUE);
    prb=self->bar_ao.prb;
    if (!--(prb->loopsleft))
        {
        p_send2(DatDialogPtr,O_DESTROY);
        prb->exit_code=0;
        }
    else if (!--(prb->toupdate))
        {
        p_send2(DatDialogPtr,O_GBAR_UPDATE);
        prb->toupdate=prb->update;
        }
    return(RUN_ACTIVE_USED);
    }
