Introducing Zinzala - page 4
The changes from the previous code are located on lines 2,3 and 14. Line 3 specifies flags that set the behavior of the window. The first flag kFlgResizeToFit instructs the window to resize to fit its contents. We no longer need to specify a given size for the window on line 2. The addition of SetPadding on line 14 is just for cosmetic purposes. This line adds a 5 pixel padding around the cGroupView widget.
We now have something which is a bit nicer. You may consult the source code here:
Next, we will add items to the two cComboBoxView widgets which will be used to select the weight and distance units. This is done by calling the method SetItems using the following array of chars:
01 // Defines units labels
02
03 const tChar *kDistanceLabels[2] = {
04 "Miles",
05 "Kilometers"
06 };
07
08 const tChar *kWeightLabels[2] = {
09 "Pounds",
10 "Kilograms"
11 };
The cCalcWindow constructor will be modified as follows:
32 // Create and set widget to select distance unit
33 ldistance = new cComboBoxView(cRect(60,20),NULL,NULL,0,kFlgNotEditable);
34 ldistance->SetItems(kDistanceLabels,2);
35 ldistance->SetSelectedItem(2);
36
37 // Create and set widget to select weight unit
38 lweight = new cComboBoxView(cRect(60,20),NULL,NULL,0,kFlgNotEditable);
39 lweight->SetItems(kWeightLabels,2);
40 lweight->SetSelectedItem(2);
Now, the user can select between the units (see the source code here):
We are now going to specify which message will be sent when the user interacts with the interface. Each message that will be sent by a widget needs to be identified. For this, we are going to define several tUint32 constants, one for each message:
01 const tUint32 kUidDistanceChanged = 'MdCh';
02 const tUint32 kUidWeightChanged = 'MwCh';
03 const tUint32 kUidDistanceUnitChanged = 'MdCT';
04 const tUint32 kUidWeightUnitChanged = 'MwCT';
05 const tUint32 kUidCompute = 'MCmp';
We then modify the cCalcWindow constructor as follows:
01 cCalcWindow::cCalcWindow()
02 : cWindow(cRect(0,0),"calc",eWinNormal,
03 kFlgResizeToFit | kFlgNotResizable | cFlgNotZoomable,
04 kCurrentWorkspace)
05 {
06 cGroupView *lFrame;
07 cFloatEntryView *lDistance;
08 cFloatEntryView *lWeight;
09 cComboBoxView *lDistanceUnit;
10 cComboBoxView *lWeightUnit;
11 cStringView *lResult;
12
13 // Set some settings of the window
14 SetPadding(5,5);
15 SetTitle("Calorie burner Calculator");
16 MoveTo(10,10);
17
18 // Create widget to group all the widgets to put on the window
19 lFrame = new cGroupView(cRect(0,0),NULL,eHorizontal,0,
20 kFlgForceEqualSize | kFlgResizeToFit);
21 lFrame->SetSlicing(3);
22 lFrame->SetSpacing(5,5);
23
24 // Create and set widget to enter the distance
25 lDistance = new cFloatEntryView(cRect(60,20),NULL,0,
26 new cMessage(kUidDistanceChanged),0,kFlgUpDown);
27 lDistance->SetPrecision(1);
28
29 // Create and set widget to enter weight
30 lWeight = new cFloatEntryView(cRect(60,20),NULL,0,
31 new cMessage(kUidWeightChanged),0,kFlgUpDown);
32 lWeight->SetPrecision(1);
33
34 // Create and set widget to select distance unit
35 lDistanceUnit = new cComboBoxView(cRect(60,20),NULL,
36 new cMessage(kUidDistanceUnitChanged),0,kFlgNotEditable);
37 lDistanceUnit->SetItems(kDistanceLabels,2);
38 lDistanceUnit->SetSelectedItem(2);
39
40 // Create and set widget to select weight uni
41 lWeightUnit = new cComboBoxView(cRect(60,20),NULL,
42 new cMessage(kUidWeightUnitChanged),0,kFlgNotEditable);
43 lWeightUnit->SetItems(kWeightLabels,2);
44 lWeightUnit->SetSelectedItem(2);
45
46 // Create and set widget to display the computation result
47 lResult = new cStringView(cRect(0,0),NULL,"a string",0,0);
48 lResult->SetFGColor(z_COLOR_BLUE);
49 lResult->SetAlignment(z_ALIGN_CENTER);
50
51 // Add all the widget to the group
52 lFrame->AddChild(new cStringView(cRect(0,0),NULL,"Distance ran",0,0));
53 lFrame->AddChild(lDistance);
54 lFrame->AddChild(lDistanceUnit);
55 lFrame->AddChild(new cStringView(cRect(0,0),NULL,"Weight",0,0));
56 lFrame->AddChild(lWeight);
57 lFrame->AddChild(lWeightUnit);
58 lFrame->AddChild(new cButtonView(cRect(0,0),NULL,"Compute",
59 new cMessage(kUidCompute),0,0));
60 lFrame->AddChild(lResult);
61
62 // Add the group to the window
63 AddChild(lFrame);
64 }
Previous
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | Next
Print version