/* DELP.C */

#include <demo.g>
#include <demo.rsg>
#include <hwim.h>

GLREF_D PR_DECOMMAN *DatApp2;

#define TIME_FMT_FLAGS (PR_TIME_MONTH_NAME|PR_TIME_SUFFIX_NAME)

LOCAL_C VOID InitTimeObject(PR_DELP *self)
    {
    P_DAYSEC ds;
    SE_TIME_FORMAT fmt;

    ds.day=DatApp2->decomman.dets.dayno;
    ds.sec=0;
    self->delp.time=f_new(CAT_DEMO_OLIB,C_TIME);
    p_send4(self->delp.time,O_TO_SET,SET_TIME_DAYSEC,&ds);
    fmt.flags=TIME_FMT_FLAGS;
    p_send4(self->delp.time,O_TO_SET_FORMAT,&fmt,TIME_FMT_FLAGS);
    }

LOCAL_C UINT SenseBufWidth(PR_DELP *self,TEXT *pb)
    {
    return(p_send5(self->lprinter.wdr,O_WDR_SENSE_WIDTH,
                                            self->delp.wtab,pb,p_slen(pb)));
    }

LOCAL_C VOID FindWidthFirstColumn(PR_DELP *self)
    {
    INT i;
    TEXT buf[E_MAX_DAY_NAME];
    UWORD wid;

    for (i=0; i<7; i++)
        {
        p_nmday(&buf[0],i);
        wid=SenseBufWidth(self,&buf[0]);
        if (wid>self->delp.colwid)
            self->delp.colwid=wid;
        }
    self->delp.colwid+=SenseBufWidth(self,"  ");    /* two spaces */
    if (self->delp.colwid>(3*self->lprinter.width/4))
        {
        hInfoPrint(DELP_PAPER_NARROW);
        p_leave(RUN_ACTIVE_CLEANUP_NONOTIFY);
        }
    self->lprinter.subsqind=self->delp.colwid;
    }

LOCAL_C VOID InitWidthTable(PR_DELP *self)
    {
    self->delp.wtab=(VOID *)p_send5(self->lprinter.wdr,O_WDR_GET_WIDTH_TABLE,
                self->lprinter.f.fid,self->lprinter.f.height,
                self->lprinter.f.style|WDR_STYLE_BOLD);
    }

#pragma METHOD_CALL

METHOD INT delp_lpr_sense_text(PR_DELP *self,WDR_PRINT *pr)
    {
    P_DATE dt;
    P_DAYSEC ds;

    if (!self->delp.time)
        {
        self->delp.ndays=DatApp2->decomman.dets.ndays;
        InitTimeObject(self);
        InitWidthTable(self);
        FindWidthFirstColumn(self);
        }
    else if (!self->delp.ndays)
        return(FALSE);
    switch (self->delp.state++)
        {
    case DELP_STATE_COL1:
        if (DatApp2->decomman.dets.gap)
            {
            p_send4(self->delp.time,O_TO_SENSE,SENSE_TIME_DATE,&dt);
            if (!dt.day)
                {
                pr->down=pr->height;
                if (DatApp2->decomman.dets.gap==1)
                    pr->down>>=1;
                }
            }
        p_send4(self->delp.time,O_TO_SENSE,SENSE_TIME_DAYSEC,&ds);
        p_nmday(&self->delp.dname[0],P_WEEK(ds.day));
        self->delp.right=self->delp.colwid
                                -SenseBufWidth(self,&self->delp.dname[0]);
        pr->buf=(&self->delp.dname[0]);
        pr->style|=WDR_STYLE_BOLD;
        break;
    case DELP_STATE_COL2:
        pr->flags&=(~(WDR_PRINT_LINE|WDR_PRINT_TEXT));
        pr->flags|=WDR_PRINT_RIGHT;
        pr->right=self->delp.right;
        return(TRUE);
    case DELP_STATE_COL3:
        p_send4(self->delp.time,O_TO_SENSE,SENSE_TIME_DATESTR,&self->delp.buf[0]);
        pr->buf=(&self->delp.buf[0]);
        pr->flags&=(~WDR_PRINT_LINE);
        pr->indent=self->delp.colwid;
        self->delp.ndays--;
        p_send4(self->delp.time,O_TO_ADD_DAYS,1,0);
        self->delp.state=DELP_STATE_COL1;
        }
    pr->blen=p_slen(pr->buf);
    return(TRUE);
    }
