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