BMICalc.pc (plain text)
// BMICalc

////////////////////////////////////////////////////////////////////////
// Body Mass Index Calculator
// By Tammy Cravit, tammy@warmfuzzy.com
// Written using PocketC and the CControls library
//
// This program is copyright (c) 2000, Tammy Cravit. All rights
// reserved. Unauthorized distribution, duplication, or use is
// prohibited.
////////////////////////////////////////////////////////////////////////

include "Ccontrols.c"

Chandle fraMain;
Chandle capHeight, capWeight, capBMI, capRange;
Chandle txtHeight, txtWeight;
Chandle rdoHeightUnits, rdoWeightUnits;
Chandle lblBMIValue, lblBMIRange;
Chandle cmdCompute;

//+--------------------------------------------------------------------+
//| These functions compute the BMI and the scale range.               |
//+--------------------------------------------------------------------+

GetBMIScale(float bmi)
{
    if (bmi < 14.0)      return "Starvation";
    else if (bmi < 20.0) return "Mild Starvation";
    else if (bmi < 25.0) return "Acceptable";
    else if (bmi < 30.0) return "Marginal";
    else if (bmi < 40.0) return "Mild Obesity";
    else                 return "Gross Obesity";
}

ComputeBMI(int metricweight, int metricheight, float weight, float height)
{
    float w;
    float h;
    float bmi;
    
    if (metricweight == 1)
        w = weight;
    else
        w = weight / 2.2;
    
    if (metricheight == 1)
        h = height;
    else
        h = (height * 2.54) / 100;
    
    bmi = format((w / (h*h)), 0);
    
    return bmi;
}

//+--------------------------------------------------------------------+
//| CControls event handler methods.                                   |
//+--------------------------------------------------------------------+

null_handler()
{
    // Do nothing
}

on_cmdCompute()
{
    float weight;
    float height;
    int mh, mw;
    float bmi;
    
    mh = Cgetcursel(rdoHeightUnits);
    mw = Cgetcursel(rdoWeightUnits);
    weight = (float) Cgetcontent(txtWeight);
    height = (float) Cgetcontent(txtHeight);
    
    bmi = ComputeBMI(mw, mh, weight, height);
    
    Csetcontent(lblBMIValue, (int)bmi);
    Csetcontent(lblBMIRange, GetBMIScale(bmi));
    
    Cdraw(lblBMIValue);
    Cdraw(lblBMIRange);
}

//+--------------------------------------------------------------------+
//| CControls GUI initialization functions.                            |
//+--------------------------------------------------------------------+

initcontrols()
{
    fraMain        = Cframe  (6,22,146,120);
    capHeight      = Clabel  (17,43,30,0,2,2);
    capWeight      = Clabel  (17,60,30,0,2,2);
    txtHeight      = Cedit   (55,43,30,2,2,2);
    txtWeight      = Cedit   (55,60,30,2,2,2);
    rdoHeightUnits = Cradio  (91,42,30,12);
    rdoWeightUnits = Cradio  (91,59,30,12);
    cmdCompute     = Cbutton (51,83,55,12,1,4);
    capBMI         = Clabel  (15,107,57,0,2,2);
    capRange       = Clabel  (17,122,55,0,2,2);
    lblBMIValue    = Clabel  (78,108,30,1,2,0);
    lblBMIRange    = Clabel  (78,122,68,1,2,0);
}

initcontents()
{
    Csetcontent (fraMain,     "BMI Calculator");
    Csetcontent (capHeight,   "Height:");
    Csetcontent (capWeight,   "Weight:");
    Csetcontent (txtHeight,   "0");
    Csetcontent (txtWeight,   "0");
    Csetcontent (cmdCompute,  "Compute!");
    Csetcontent (capBMI,      "BMI Number:");
    Csetcontent (capRange,    "BMI Range:");
    Csetcontent (lblBMIValue, "0");
    Csetcontent (lblBMIRange, "---");
    
    Csettopic   (txtHeight,   "Enter height in inches or cm:");
    Csettopic   (txtWeight,   "Enter weight in lbs or kg:");
}

inititems()
{
    Cadditem (rdoHeightUnits, "in");
    Cadditem (rdoHeightUnits, "cm");
    Cadditem (rdoWeightUnits, "lb");
    Cadditem (rdoWeightUnits, "kg");
    
    Csetcursel (rdoHeightUnits, 0);
    Csetcursel (rdoWeightUnits, 0);
}

drawcontrols()
{
    Cdraw (fraMain);
    Cdraw (capHeight);
    Cdraw (capWeight);
    Cdraw (txtHeight);
    Cdraw (txtWeight);
    Cdraw (rdoHeightUnits);
    Cdraw (rdoWeightUnits);
    Cdraw (cmdCompute);
    Cdraw (capBMI);
    Cdraw (capRange);
    Cdraw (lblBMIValue);
    Cdraw (lblBMIRange);
}
    
//+--------------------------------------------------------------------+
//| CControls Message-Dispatching Loop                                 |
//+--------------------------------------------------------------------+

messageloop() 
{
    int e;
    
    while (1)
    {
        e = event(1);
        if (Cevent(txtHeight,      e))      null_handler();
        else if (Cevent(txtWeight,      e)) null_handler();
        else if (Cevent(rdoHeightUnits, e)) null_handler();
        else if (Cevent(rdoWeightUnits, e)) null_handler();
        else if (Cevent(cmdCompute,     e)) on_cmdCompute();
    }
}
        
//+--------------------------------------------------------------------+
//| main() - Program Entry Point                                       |
//+--------------------------------------------------------------------+

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