/*
O_GAUGE: demonstrates a gauge used to monitor the progress
          of a lengthy operation simulated using a timer.
*/


#include <hwimman.g>
#include <p_file.h>
#include <timer.g>
#include <donewn.g>
#include <ncedit.g>
#include <gaugedem.g>
#include <gaugedem.rsg>

GLREF_D WSERV_SPEC *wserv_channel;
GLREF_D PR_HWIMMAN *w_am;
GLREF_D PR_WSERV *w_ws;

GLDEF_C VOID main(VOID)
    {
    IN_HWIMMAN app;
    IN_WSERV ws;

    p_linklib(0);
    app.flags=FLG_APPMAN_FULLSCREEN|FLG_APPMAN_RSCFILE|FLG_APPMAN_CLEAN|
        FLG_APPMAN_SRSCFILE;
    app.wserv_cat=p_getlibh(CAT_GAUGEDEM_GAUGEDEM);
    ws.com_cat=p_getlibh(CAT_GAUGEDEM_GAUGEDEM);
    app.wserv_class=C_GAUGEWSV;
    ws.com_class=C_GAUGECOM;
    p_send4(p_new(CAT_GAUGEDEM_HWIM,C_HWIMMAN),O_AM_INIT,&app,&ws);
    }

LOCAL_C INT LaunchDial(VOID *buf,INT id,INT class)
    {
    DL_DATA dl_data;

    dl_data.id=id;
    dl_data.rbuf=buf;
    dl_data.pdlg=NULL;
    return(p_send5(w_ws,O_WS_DO_DIAL,p_getlibh(CAT_GAUGEDEM_GAUGEDEM),class,&dl_data));
    }

#pragma METHOD_CALL

METHOD VOID gaugewsv_ws_dyn_init(PR_GAUGEWSV *self)
    {
    self->wserv.cli=f_new(CAT_GAUGEDEM_GAUGEDEM,C_GAUGEWIN);
    p_send2(self->wserv.cli,O_WN_INIT);
    }

METHOD VOID gaugewin_wn_init(PR_GAUGEWIN *self)
    {
    W_WINDATA wd;

    wd.extent.tl.x=0;
    wd.extent.tl.y=0;
    wd.extent.width=wserv_channel->conn.info.pixels.x;
    wd.extent.height=wserv_channel->conn.info.pixels.y;
    p_send5(self,O_WN_CONNECT,NULL,W_WIN_EXTENT,&wd);
    p_send3(self,O_WN_VISIBLE,WV_INITVIS);
    p_send3(self,O_WN_EMPHASISE,TRUE);
    }

METHOD VOID gaugewin_wn_draw(PR_GAUGEWIN *self)
    {
    G_FONT_INFO fontinfo;
    W_WINDATA windata;
    TEXT buf[40];
    P_RECT r;
    INT w,len;

    p_supersend2(self,O_WN_DRAW);
    len=p_send(w_am,O_AM_LOAD_RES_BUF,TITLE_RES,&buf[0]);
    w=gTextWidth(WS_FONT_SYSTEM,0,&buf[0],--len);
    wInquireWindow(self->win.id,&windata);
    r.tl.x=(windata.extent.width-w)/2;
    r.br.x=r.tl.x+w;
    gFontInfo(WS_FONT_SYSTEM,0,&fontinfo);
    r.tl.y=(windata.extent.height-fontinfo.height)/2;
    r.br.y=r.tl.y+fontinfo.height;
    gPrintBoxText(&r,fontinfo.ascent,G_TEXT_ALIGN_CENTRE,0,&buf[0],len);
    }

METHOD VOID gaugecom_com_run(PR_GAUGECOM *self)
    {
    ULONG duration;

    duration=100;
    f_newsend(CAT_GAUGEDEM_GAUGEDEM,C_GAUGETIM,O_AO_INIT,&duration);
    LaunchDial(&duration,GAUGE_DL_RES,C_GAUGEDLG);
    }

METHOD VOID gaugedlg_dl_dyn_init(PR_GAUGEDLG *self)
    {
    SE_DONEWN se_donewn;

    se_donewn.val=0L;
    se_donewn.range=*(ULONG *)self->dlgbox.rbuf;
    se_donewn.flags=SE_DONEWN_RANGE|SE_DONEWN_VALUE;
    p_send4(self,O_WN_SET,1,&se_donewn);
    }

METHOD VOID gaugetim_ao_queue(PR_GAUGETIM *self,UINT lsw,UINT msw)
    {
    self->active.stat=E_FILE_PENDING;
    self->active.isactive=TRUE;
    p_supersend4(self,O_AO_QUEUE,lsw,msw);
    }    

METHOD VOID gaugetim_ao_init(PR_GAUGETIM *self,ULONG *ptotal)
    {
    p_send3(w_am,O_AM_ADD_TASK,self);
    self->active.priority=PRIORITY_ACTIVE_WSERV-1;
    self->gaugetim.count=0L;
    self->gaugetim.total=*ptotal;
    p_supersend2(self,O_AO_INIT);
    p_send4(self,O_AO_QUEUE,1,0);
    }

METHOD INT gaugetim_ao_run(PR_GAUGETIM *self)
    {
    SE_DONEWN se_donewn;

    self->gaugetim.count++;
    if (w_ws->wserv.dial)
        {
        se_donewn.flags=SE_DONEWN_INCREMENT;
        p_send4(w_ws->wserv.dial,O_WN_SET,1,&se_donewn);
        if (self->gaugetim.count<self->gaugetim.total)
            {
            p_send4(self,O_AO_QUEUE,1,0);
            return(RUN_ACTIVE_USED);
            }
        else
            p_send2(w_ws->wserv.dial,O_DESTROY);
        }
    p_send2(self,O_DESTROY);
    return(RUN_ACTIVE_USED);
    }
