/* * * Zinzala SDK Example * * File : phalorieburn2.cpp * Purpose : Calorie burner calculator * Author : JLV * Creation : 04/07/03 * Last Update : 08/17/04 * * (C) hexaZen 2003-2004 * * This file is part of an example program for Zinzala. It can be * used, distributed and modified without limitation. * */ #include #include #include #include #include #include #include #include #include #include using namespace zSDK::Cincinella; using namespace zSDK::Farfalla; // Defines units labels const tChar *kDistanceLabels[2] = { "Miles", "Kilometers" }; const tChar *kWeightLabels[2] = { "Pounds", "Kilograms" }; // Defines units constants const tUint16 kUnitMiles = 1; const tUint16 kUnitKilometers = 2; const tUint16 kUnitPounds = 1; const tUint16 kUnitKilogranms = 2; // Conversion constants const tFloat64 kMilesToKm = 1.6; const tFloat64 kPoundsToKg = 0.454545; // Formula constant(s) const tFloat64 kFormula = 1.036; // Messages UID const tUint32 kUidDistanceChanged = 'MdCh'; const tUint32 kUidWeightChanged = 'MwCh'; const tUint32 kUidDistanceUnitChanged = 'MdCT'; const tUint32 kUidWeightUnitChanged = 'MwCT'; const tUint32 kUidCompute = 'MCmp'; // Define the Application's window class class cCalcWindow : public cWindow { public: cCalcWindow(); protected: void MessageReceived(cMessage *aMessage); private: tFloat64 iDistance; tFloat64 iWeight; tUint16 iDistanceUnit; tUint16 iWeightUnit; private: cStringView *iResult; }; /* * Method : cCalcWindow * Purpose : Constructor * Inputs : none * Output : none * Side Effects : none * */ cCalcWindow::cCalcWindow() : cWindow(uRect(0,0),"calc",eWinNormal, kFlgResizeToFit | kFlgNotResizable | kFlgNotZoomable, kCurrentWorkspace) { cGroupView *lFrame; cFloatEntryView *lDistance; cFloatEntryView *lWeight; cComboBoxView *lDistanceUnit; cComboBoxView *lWeightUnit; cButtonView *lCompute; // Set the inital values iDistance = 0; iWeight = 0; iDistanceUnit = 2; iWeightUnit = 2; // Set some settings of the window SetPadding(5,5); SetTitle("Calories-burned Calculator"); MoveTo(10,10); AllowScripting(eLocal); // Create widget to group all the widgets to put on the window lFrame = new cGroupView(uRect(0,0),NULL,eHorizontal,0,kFlgForceEqualSize | kFlgResizeToFit); lFrame->SetSlicing(3); lFrame->SetSpacing(5,5); // Create and set widget to enter the distance lDistance = new cFloatEntryView(uRect(60,20),"distance",iDistance,new cMessage(kUidDistanceChanged),0,kFlgUpDown); lDistance->SetPrecision(1); lDistance->SetRange(0,200); lDistance->AllowScripting(eLocal); // Create and set widget to enter weight lWeight = new cFloatEntryView(uRect(60,20),"weight",iWeight,new cMessage(kUidWeightChanged),0,kFlgUpDown); lWeight->SetPrecision(1); lWeight->SetRange(0,400); lWeight->AllowScripting(eLocal); // Create and set widget to select lDistance unit lDistanceUnit = new cComboBoxView(uRect(60,20),"dunit",new cMessage(kUidDistanceUnitChanged),0,kFlgNotEditable); lDistanceUnit->SetItems(kDistanceLabels,2); lDistanceUnit->SetSelectedItem(iDistanceUnit); lDistanceUnit->AllowScripting(eLocal); // Create and set widget to select weight unit lWeightUnit = new cComboBoxView(uRect(60,20),"wunit",new cMessage(kUidWeightUnitChanged),0,kFlgNotEditable); lWeightUnit->SetItems(kWeightLabels,2); lWeightUnit->SetSelectedItem(iWeightUnit); lWeightUnit->AllowScripting(eLocal); // Create and set widget to display the computation result iResult = new cStringView(uRect(0,0),"result","",0,0); iResult->SetFGColor(kColorBlue); iResult->SetAlignment(eCenter); iResult->AllowScripting(eLocal); // Create the button to compute lCompute = new cButtonView(uRect(0,0),"compute","Compute",new cMessage(kUidCompute),0,0); lCompute->AllowScripting(eLocal); // Add all the widget to the group lFrame->AddChild(new cStringView(uRect(0,0),NULL,"Distance ran",0,0)); lFrame->AddChild(lDistance); lFrame->AddChild(lDistanceUnit); lFrame->AddChild(new cStringView(uRect(0,0),NULL,"Weight",0,0)); lFrame->AddChild(lWeight); lFrame->AddChild(lWeightUnit); lFrame->AddChild(lCompute); lFrame->AddChild(iResult); // Add the group to the window AddChild(lFrame); } /* * Method : MessageReceived * Purpose : A message has been received by the Window * Inputs : * * cMessage *message, message received * * Output : none * Side Effects : none * */ void cCalcWindow::MessageReceived(cMessage *aMessage) { switch(aMessage->iWhat) { case kUidDistanceChanged: { aMessage->GetFloat64("value",&iDistance); break; } case kUidWeightChanged: { aMessage->GetFloat64("value",&iWeight); break; } case kUidDistanceUnitChanged: { aMessage->GetUint16("pos",&iDistanceUnit); break; } case kUidWeightUnitChanged: { aMessage->GetUint16("pos",&iWeightUnit); break; } case kUidCompute: { tChar lString[128]; tFloat64 lDistance; tFloat64 lWeight; tFloat64 lCalorie; if(iDistanceUnit == kUnitMiles) lDistance = iDistance * kMilesToKm; else lDistance = iDistance; if(iWeightUnit == kUnitPounds) lWeight = iWeight * kPoundsToKg; else lWeight = iWeight; lCalorie = lDistance * lWeight * kFormula; sprintf(lString,"%0.2f calories",lCalorie); // Display the result in the stringview widget iResult->SetText(lString); break; } default: cWindow::MessageReceived(aMessage); } } int main() { // Create the application cApplication lMyApp("hexaZen/phalorieburn",0); if(lMyApp.IsValid()) { // Start the app (and the Photon connection) lMyApp.Run(); // Allow scripting of the application lMyApp.AllowScripting(eLocal); // Create window cCalcWindow win; // show the window now win.Show(); // Wait for the end of the app lMyApp.WaitEnd(); } else fprintf(stderr,"Failed the init the application"); }