00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <barsread.h>
00032 #include <eiklabel.h>
00033 #include <eikedwin.h>
00034
00035 #include "EComCalculatorAppView.h"
00036 #include <EComCalculator.rsg>
00037
00038
00039 const TInt KXPos = 10;
00040
00041
00042 const TInt KNumberOfControls = 5;
00043 const TInt KBottomMargin = 10;
00044 const TInt KResultSize = 128;
00045
00046
00047 _LIT(KLabelTextA,"Number A:");
00048 _LIT(KLabelTextB,"Number B:");
00049 _LIT(KTextEmpty, "");
00050
00051
00052 enum TControls
00053 {
00054 ELabelA,
00055 EEdwinA,
00056 ELabelB,
00057 EEdwinB,
00058 ELabelResult
00059 };
00060
00061
00062 CEComCalculatorAppView* CEComCalculatorAppView::NewL(const TRect& aRect)
00063 {
00064 CEComCalculatorAppView* self = new (ELeave) CEComCalculatorAppView;
00065 CleanupStack::PushL(self);
00066 self->ConstructL(aRect);
00067 CleanupStack::Pop(self);
00068 return self;
00069 }
00070
00071
00072 void CEComCalculatorAppView::ConstructL(const TRect& aRect)
00073 {
00074
00075
00076 CreateWindowL();
00077
00078 iLabelA = new (ELeave) CEikLabel;
00079 iLabelA->SetContainerWindowL(*this);
00080 iLabelA->SetTextL(KLabelTextA);
00081
00082 iLabelB = new (ELeave) CEikLabel;
00083 iLabelB->SetContainerWindowL(*this);
00084 iLabelB->SetTextL(KLabelTextB);
00085
00086 iLabelResult = new (ELeave) CEikLabel;
00087 iLabelResult->SetContainerWindowL(*this);
00088 iLabelResult->SetTextL(KTextEmpty);
00089
00090 TResourceReader reader;
00091 iCoeEnv->CreateResourceReaderLC(reader, R_ECOMCALCULATOR_EDWIN);
00092 iEdwinA = new (ELeave) CEikEdwin;
00093 iEdwinA->SetContainerWindowL(*this);
00094 iEdwinA->ConstructFromResourceL(reader);
00095 CleanupStack::PopAndDestroy();
00096
00097 iCoeEnv->CreateResourceReaderLC(reader, R_ECOMCALCULATOR_EDWIN);
00098 iEdwinB = new (ELeave) CEikEdwin;
00099 iEdwinB->SetContainerWindowL(*this);
00100 iEdwinB->ConstructFromResourceL(reader);
00101 CleanupStack::PopAndDestroy();
00102
00103 SizeChanged();
00104 iEdwinA->SetFocus(ETrue);
00105
00106
00107 SetRect(aRect);
00108
00109
00110 ActivateL();
00111 }
00112
00113
00114 CEComCalculatorAppView::CEComCalculatorAppView()
00115 :iLabelA(NULL), iLabelB(NULL),
00116 iEdwinA(NULL), iEdwinB(NULL),
00117 iLabelResult(NULL)
00118 {
00119
00120 }
00121
00122
00123 CEComCalculatorAppView::~CEComCalculatorAppView()
00124 {
00125 delete iLabelA;
00126 delete iLabelB;
00127 delete iEdwinA;
00128 delete iEdwinB;
00129 delete iLabelResult;
00130 }
00131
00132
00133 void CEComCalculatorAppView::Draw(const TRect& aRect) const
00134 {
00135
00136 CWindowGc& gc = SystemGc();
00137 gc.Clear(Rect());
00138 gc.SetBrushColor(KRgbGray);
00139 gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00140 gc.DrawRect(aRect);
00141 }
00142
00143
00144 void CEComCalculatorAppView::SizeChanged()
00145 {
00146 TRect rect = Rect();
00147
00148 TInt bottom = rect.iBr.iY - KBottomMargin;
00149 TInt comp_height = bottom/KNumberOfControls;
00150
00151 TPoint label_a_pos = TPoint(KXPos, bottom- comp_height*5);
00152 TPoint edwin_a_pos = TPoint(KXPos, bottom- comp_height*4);
00153 TPoint label_b_pos = TPoint(KXPos, bottom- comp_height*3);
00154 TPoint edwin_b_pos = TPoint(KXPos, bottom- comp_height*2);
00155 TPoint label_result_pos = TPoint(KXPos, bottom - comp_height);
00156
00157 iLabelA->SetExtent(label_a_pos, iLabelA->MinimumSize());
00158 iLabelB->SetExtent(label_b_pos, iLabelB->MinimumSize());
00159 iEdwinA->SetExtent(edwin_a_pos, iEdwinA->MinimumSize());
00160 iEdwinB->SetExtent(edwin_b_pos, iEdwinB->MinimumSize());
00161
00162
00163
00164 TSize extent = iLabelResult->MinimumSize();
00165 extent.iHeight += KBottomMargin;
00166
00167 iLabelResult->SetExtent(label_result_pos,
00168
00169
00170 extent );
00171 }
00172
00173
00174 TInt CEComCalculatorAppView::CountComponentControls() const
00175 {
00176 return KNumberOfControls;
00177 }
00178
00179
00180 CCoeControl* CEComCalculatorAppView::ComponentControl(TInt aIndex) const
00181 {
00182 switch (aIndex)
00183 {
00184 case ELabelA:
00185 return iLabelA;
00186 case EEdwinA:
00187 return iEdwinA;
00188 case ELabelB:
00189 return iLabelB;
00190 case EEdwinB:
00191 return iEdwinB;
00192 case ELabelResult:
00193 return iLabelResult;
00194 default:
00195 return NULL;
00196 }
00197 }
00198
00199
00200
00201
00202 TKeyResponse CEComCalculatorAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00203 TEventCode aType)
00204 {
00205
00206
00207 switch (aKeyEvent.iScanCode)
00208 {
00209 case EStdKeyUpArrow:
00210 if(iEdwinB && iEdwinB->IsFocused())
00211 {
00212 iEdwinB->SetFocus(EFalse);
00213 iEdwinA->SetFocus(ETrue);
00214 return EKeyWasConsumed;
00215 }
00216 break;
00217
00218 case EStdKeyDownArrow:
00219 if(iEdwinA && iEdwinA->IsFocused())
00220 {
00221 iEdwinA->SetFocus(EFalse);
00222 iEdwinB->SetFocus(ETrue);
00223 return EKeyWasConsumed;
00224 }
00225 break;
00226 }
00227
00228
00229 if (iEdwinA && iEdwinA->IsFocused())
00230 {
00231 return iEdwinA->OfferKeyEventL(aKeyEvent, aType);
00232 }
00233 if (iEdwinB && iEdwinB->IsFocused())
00234 {
00235 return iEdwinB->OfferKeyEventL(aKeyEvent, aType);
00236 }
00237
00238 return EKeyWasNotConsumed;
00239 }
00240
00241
00242
00243 TInt CEComCalculatorAppView::GetA(TReal &aResult) const
00244 {
00245 TInt err = KErrGeneral;
00246 if(iEdwinA)
00247 {
00248 TLex16 lex(iEdwinA->Text()->Read(0));
00249 err = lex.Val(aResult);
00250 }
00251 return err;
00252 }
00253
00254
00255 TInt CEComCalculatorAppView::GetB(TReal &aResult) const
00256 {
00257 TInt err = KErrGeneral;
00258 if(iEdwinB)
00259 {
00260 TLex16 lex(iEdwinB->Text()->Read(0));
00261 err = lex.Val(aResult);
00262 }
00263 return err;
00264 }
00265
00266
00267 void CEComCalculatorAppView::ShowResultL(const TDesC8& aOperationName,
00268 TReal aValue)
00269 {
00270 TRealFormat format;
00271 TBuf<KResultSize> resultStr;
00272 _LIT(KSeparator,": ");
00273 resultStr.Copy(aOperationName);
00274 resultStr.Append(KSeparator);
00275 resultStr.AppendNum(aValue,format);
00276 iLabelResult->SetTextL(resultStr);
00277 SizeChanged();
00278 DrawDeferred();
00279 }