/*
    QU4.C
*/

#include <p_std.h>
#include <p_date.h>
#include <p_file.h>
#include <wlib.h>
#include <hwif.h>


LOCAL_D TEXT *cmds[]=
    {
    "iInches/Centimetres",
    "mMiles/Kilometres",
    "lPounds/Kilogrammes",
    "pPints/Litres",
    "fFahrenheit/Centigrade",
    "dDay of week",
    "cCombinations",
    "tTime difference",
    "hHoroscope",
    "nNow",
    "wPassword",
    "eEncrypt",
    "uDecrypt",
    "sSignificance",
    "zFile size",
    "xExit",
    NULL
    };

LOCAL_D H_MENU_DATA mdata[]=
    {
    "Conversions",5,
    "Calendar",5,
    "Secret",3,
    "Special",3,
    NULL
    };

GLDEF_D TEXT ** _cmds=(&cmds[0]);
GLDEF_D H_MENU_DATA * _mdata=(&mdata[0]);


LOCAL_D UINT MainWid;

LOCAL_D VOID *ttH;
LOCAL_D UWORD ttMonth=1;
LOCAL_D UWORD ttSuffix=1;
LOCAL_D UWORD ttDay=1;
LOCAL_D UWORD ttCentury=2;
LOCAL_D UWORD ttSeconds=2;


LOCAL_C INT AddNoYesChoiceList(TEXT *pmt,UWORD *pval)
    {
    return(uAddChoiceList(pmt,pval,"No","Yes",NULL));
    }

LOCAL_C VOID TimeNow(VOID)
    {
    INT values;
    H_DI_TEXT txt;
    TEXT buf[48];

    txt.str=(&buf[0]);
    do
        {
        if (uOpenDialog(NULL))
            return;
        uZTStoBCS(&buf[0],"Time is now");
        txt.type=H_DTEXT_ALIGN_CENTRE;
        if (uAddDialogItem(H_DIALOG_TEXT,NULL,&txt))
            return;
        values=0;
        if (ttMonth==2)
            values=H_TIME_FORMAT_MONTH_NAME;
        if (ttDay==2)
            values|=H_TIME_FORMAT_DAY_NAME;
        if (ttSuffix==2)
            values|=H_TIME_FORMAT_SUFFIX;
        if (ttCentury==1)
            values|=H_TIME_FORMAT_NO_CENTURY;
        if (ttSeconds==1)
            values|=H_TIME_FORMAT_NO_SECONDS;
        hTTSetFormat(ttH,values);
        hTTSetTime(ttH,H_TIME_SET_NOW,NULL);
        buf[0]=hTTSenseString(ttH,H_TIME_SENSE_BOTH,&buf[1]);
        txt.type=H_DTEXT_ALIGN_CENTRE|H_DTEXT_UNDERLINE;
        if (uAddDialogItem(H_DIALOG_TEXT,NULL,&txt))
            return;
        if (AddNoYesChoiceList("Give month name",&ttMonth))
            return;
        if (AddNoYesChoiceList("Give day name",&ttDay))
            return;
        if (AddNoYesChoiceList("Use date suffix",&ttSuffix))
            return;
        if (AddNoYesChoiceList("Show century",&ttCentury))
            return;
        if (AddNoYesChoiceList("Show seconds",&ttSeconds))
            return;
        } while (uRunDialog()>0);
    }

LOCAL_C VOID SetUpDiDate(H_DI_DATE *pdate,P_DAYSEC *pds)
    {
    ULONG sdate;

    sdate=p_date();
    p_sttods(&sdate,pds);
    pdate->value=(&pds->day);
    pdate->low=0;
    pdate->high=H_LAST_DAY;
    }

LOCAL_C VOID DayOfWeek(VOID)
    {
    P_DAYSEC ds;
    H_DI_DATE date;
    TEXT buf[40];
    H_DI_TEXT txt;

    SetUpDiDate(&date,&ds);
    do
        {
        p_nmday(&buf[1],p_wkday(ds.day));
        buf[0]=p_slen(&buf[1]);
        txt.str=(&buf[0]);
        txt.type=H_DTEXT_ALIGN_LEFT;
        if (uOpenDialog("Find day of week"))
            return;
        if (uAddDialogItem(H_DIALOG_DATE,"Date",&date))
            return;
        if (uAddDialogItem(H_DIALOG_TEXT,"Day of week",&txt))
            return;
        } while (uRunDialog()>0);
    }

LOCAL_C VOID FileSize(VOID)
    {
    TEXT fname[P_FNAMESIZE+2];
    H_DI_FSEL fsel;
    P_INFO info;
    TEXT buf[30];

    fname[0]=0;
    fsel.fname=(&fname[0]);
    fsel.flags=H_FILE_PICK_SELECTOR;
    FOREVER
        {
        if (uOpenDialog("Find file size"))
            return;
        if (uAddDialogItem(H_DIALOG_FSEL,"File:",&fsel))
            return;
        if (uRunDialog()<=0)
            return;
        fname[1+fname[0]]=0;
        if (!uErrorValue(p_finfo(&fname[1],&info)))
            {
            p_atos(&buf[0],"File size is %lu bytes",info.size);
            wInfoMsg(&buf[0]);
            }
        }
    }

LOCAL_C VOID ManageCommand(INT keycode)
    {
    INT index;
    TEXT buf[40];

    switch (keycode)
        {
    case 'd':
        DayOfWeek();
        break;
    case 'n':
        TimeNow();
        break;
    case 'z':
        FileSize();
        break;
    case 'x':
        p_exit(0);
    default:
        index=uLocateCommand(keycode);
        if (index>=0)
            {
            p_atos(&buf[0],"You chose `%s'",cmds[index]+1);
            wInfoMsg(&buf[0]);
            }
        }
    }

LOCAL_C VOID MainLoop(VOID)
    {
    INT ret;
    WMSG_KEY key;

    FOREVER
        {
        uGetKey(&key);
        if (key.keycode&W_SPECIAL_KEY)
            ManageCommand(key.keycode&(~W_SPECIAL_KEY));
        else if (key.keycode==W_KEY_MENU && !(key.modifiers&W_CTRL_MODIFIER))
            {
            ret=uPresentMenus();
            if (ret>0)
                ManageCommand(ret);
            }
        }
    }

LOCAL_C VOID ReduceScreenSize(VOID)
    {
    W_WINDATA wd;

    wd.extent.tl.x=wd.extent.tl.y=0;
    wd.extent.width=189;
    wd.extent.height=80;
    wSetWindow(MainWid,W_WIN_EXTENT,&wd);
    if (uErrorValue(wCheckPoint()))
        p_exit(0);
    }

LOCAL_C VOID CreateGC(VOID)
    {
    INT hgc;

    hgc=gCreateGC0(MainWid);
    if (hgc<0)
        p_exit(hgc);
    }

LOCAL_C VOID SpecificInit(VOID)
    {
    MainWid=uFindMainWid();
    ReduceScreenSize();
    CreateGC();
    gBorder(W_BORD_CORNER_4);
    wsEnable();
    ttH=hTTOpen();
    }

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