Introducing Zinzala - page 5

In the call to the widget constructors, we have added the creation of a cMessage object (lines 26 31 36 42 and 59), with the message identifier as its argument. When the user uses the interface, these messages will be sent to the window.

The MessageReceived method of the cCalcWindow class will now be modified to handle the messages that will be received by the window:

01 void cCalcWindow::MessageReceived(cMessage *aMessage) 02 { 03 switch(aMessage->iWhat) 04 { 05 case kUidDistanceChanged: 06 { 07 break; 08 } 09 case kUidWeightChanged: 10 { 11 break; 12 } 13 case kUidDistanceUnitChanged: 14 { 15 break; 16 } 17 case kUidWeightUnitChanged: 18 { 19 break; 20 } 21 case kUidCompute: 22 { 23 break; 24 } 25 default: 26 cWindow::MessageReceived(message); 27 } 28 }

The intended application can now be easily completed, mainly by modifying the MessageReceived as follows:

01 void cCalcWindow::MessageReceived(cMessage *aMessage) 02 { 03 switch(aMessage->iWhat) 04 { 05 case kUidDistanceChanged: 06 { 07 aMessage->Getfloat64("value",&iDistance); 08 break; 09 } 10 case kUidWeightChanged: 11 { 12 aMessage->Getfloat64("value",&iWeight); 13 break; 14 } 15 case kUidDistanceUnitChanged: 16 { 17 aMessage->Getuint16("pos",&iDistanceUnit); 18 break; 19 } 20 case kUidWeightUnitChanged: 21 { 22 aMessage->Getuint16("pos",&iWeightUnit); 23 break; 24 } 25 case kUidCompute: 26 { 27 tChar lString[128]; 28 29 tFloat64 lDistance; 30 tFloat64 lWeight; 31 tFloat64 lCalorie; 32 33 // convert the distance and weight if needed 34 if(iDistanceUnit == kUnitMiles) 35 lDistance = iDistance * kMilesToKm; 36 else 37 lDistance = iDistance; 38 if(iWeightUnit == kUnitPounds) 39 lWeight = iWeight * lPoundsToKg; 40 else 41 lWeight = iWeight; 42 43 lCalorie = iDistance * iWeight * kFormula; 44 45 sprintf(lString,"%0.2f calories",lCalorie); 46 47 // Display the result in the stringview widget 48 iResult->SetText(lString); 49 50 break; 51 } 52 default: 53 cWindow::MessageReceived(aMessage); 54 } 55 }

Line 7 retreives the value from the received message which is entered by the user in the cFloatEntryView widget. We simply store this value in a private member of the cCalcWindow class, and then reuse it when we have received the message telling us that the button Compute has been pressed. On line 48, we modify the text displayed by the cStringView widget. This widget is now a private member of the window class.

the completed version - calories burned calculator

Since the cWindow handles the destruction of its widgets, we don't have to worry about any possible memory leakage. When the window is closed by the user and the application terminates, all the widgets are destroyed.

The complete source code of the application can be seen here.

Print version

All content © 2004-2007, hexaZen - Vancouver BC, Canada