TipCalc.pc (plain text)
// TipCalc

include "Ccontrols.c"

Chandle  h1, h2, h3, h4, h5, cmdCompute, txtMealAmt, lstTipPct, lblTipAmt, lblTotal;

StringToFloat(string s)
{
    string buffer;
    int i;
    string x;

    for (i=0; i < strlen(s); i++) 
    {
        x = substr(s, i, 1);
        if (x == "0")
            buffer = buffer + x;
        else if (x == "1")
            buffer = buffer + x;
        else if (x == "2")
            buffer = buffer + x;
        else if (x == "3")
            buffer = buffer + x;
        else if (x == "4")
            buffer = buffer + x;
        else if (x == "5")
            buffer = buffer + x;
        else if (x == "6")
            buffer = buffer + x;
        else if (x == "7")
            buffer = buffer + x;
        else if (x == "8")
            buffer = buffer + x;
        else if (x == "9")
            buffer = buffer + x;
        else if (x == "-")
        {
            if (strstr(buffer, "-", 0) == -1)
                buffer = buffer + x;
        }
        else if (x == ".")
        {
            if (strstr(buffer, "-", 0) == -1)
                buffer = buffer + x;
        }
    }

    return (float)buffer;
}
                    
on_cmdCompute()
{
    string sMealAmt, sTipAmt;
    float fMealAmt, fTipPct, fTipAmt, fTotal;
    
    sMealAmt = Cgetcontent(txtMealAmt);
    sTipAmt = Cgetcontent(lstTipPct);

    fMealAmt = StringToFloat(sMealAmt);
    fTipPct = StringToFloat(sTipAmt) / 100;
    fTipAmt = fMealAmt * fTipPct;
    fTotal = fMealAmt + fTipAmt;
    
    Csetcontent(lblTipAmt, "$" + format(fTipAmt, 2));
    Csetcontent(lblTotal, "$" + format(fTotal, 2));
    Cdraw(lblTipAmt);
    Cdraw(lblTotal);
}

null_event()
{
    // Do nothing
}

messageloop()
{
    int e;
    while(1)
    {
        e=event(1);
        if      (Cevent(cmdCompute,e)) on_cmdCompute();
        else if (Cevent(txtMealAmt,e)) null_event();
        else if (Cevent(lstTipPct, e)) null_event();
        else if (Cevent(lblTipAmt, e)) null_event();
        else if (Cevent(lblTotal, e))  null_event();
        else if (Cevent(h1,e))         null_event();
        else if (Cevent(h2,e))         null_event();
        else if (Cevent(h3,e))         null_event();
        else if (Cevent(h4,e))         null_event();
        else if (Cevent(h5,e))         null_event();
    }
}

initcontents()
{
    Csetcontent(h1,"TipCalc");
    Csetcontent(h2,"Meal Amount:");
    Csetcontent(h3,"Tip %:");
    Csetcontent(h4,"Tip Amount:");
    Csetcontent(h5,"Total:");
    Csetcontent(cmdCompute,"Compute");
    Csetcontent(txtMealAmt,"$");
    Csettopic(txtMealAmt,"Enter the amount of the meal:");
    Csetcontent(lblTipAmt,"$");
    Csetcontent(lblTotal,"$");
}

inititems()
{
    int i;
    string s;

    for (i=10; i<20; i++)
    {
        s = "" + i + "%";
        Cadditem(lstTipPct, s);
    }
    Csetcursel(lstTipPct, 5);
}

drawcontrols()
{
    Cdraw(h1);
    Cdraw(h2);
    Cdraw(h3);
    Cdraw(h4);
    Cdraw(h5);
    Cdraw(cmdCompute);
    Cdraw(txtMealAmt);
    Cdraw(lstTipPct);
    Cdraw(lblTipAmt);
    Cdraw(lblTotal);
}

initcontrols()
{
    h1=Cframe(4,20,152,120);
    h2=Clabel(11,45,58,0,2,2);
    h3=Clabel(39,62,30,0,2,2);
    h4=Clabel(20,78,49,0,2,2);
    h5=Clabel(38,95,30,0,2,2);
    cmdCompute=Cbutton(55,118,46,12,1,6);
    txtMealAmt=Cedit(76,45,59,2,2,2);
    lstTipPct=Cdropdown(76,62,33,3);
    lblTipAmt=Clabel(75,78,59,1,2,2);
    lblTotal=Clabel(75,95,59,1,2,2);
}

main()
{
    graph_on();
    title("TipCalc");
    initcontrols();
    initcontents();
    inititems();
    drawcontrols();
    messageloop();
}