examples/ForumNokia/EComCalculator/client/src/EComCalculatorAppview.cpp

00001 /*
00002  * ============================================================================
00003  *  Name     : CEComCalculatorAppView from EComCalculatorAppView.cpp
00004  *  Part of  : EComCalculator
00005  *  Created  : 17/11/2003 by Forum Nokia
00006  *  Version  : 1.0
00007  *  Copyright: Nokia Corporation
00008  * ============================================================================
00009  */
00010 
00011 
00012 #include <barsread.h>  // for resource reader
00013 #include <eiklabel.h>  // for label controls
00014 #include <eikedwin.h>  // for CEikEdwin
00015 
00016 #include "EComCalculatorAppView.h"
00017 #include <EComCalculator.rsg> // Generated from EComCalculator.rss
00018 
00019 // Constants used in controls' position calculations
00020 const TInt KXPos = 10; //x position
00021 
00022 // Constants
00023 const TInt KNumberOfControls = 5; //Number of UI controls within this container
00024 const TInt KBottomMargin = 10; //The amount of space to leave to bottom
00025 const TInt KResultSize = 128;
00026 
00027 // Constant texts
00028 _LIT(KLabelTextA,"Number A:");
00029 _LIT(KLabelTextB,"Number B:");
00030 _LIT(KTextEmpty, "");
00031 
00032 // Enumarations
00033 enum TControls
00034     {
00035     ELabelA,
00036     EEdwinA,
00037     ELabelB,
00038     EEdwinB,
00039     ELabelResult
00040     };
00041 
00042 // Create instance of this compound control.
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 // Perform the second phase construction. Instantiate the owned controls.
00053 void CEComCalculatorAppView::ConstructL(const TRect& aRect)
00054     {
00055 
00056     // Create a window for this application view
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();  // Resource reader
00077 
00078     iCoeEnv->CreateResourceReaderLC(reader, R_ECOMCALCULATOR_EDWIN);
00079     iEdwinB = new (ELeave) CEikEdwin;
00080     iEdwinB->SetContainerWindowL(*this);
00081     iEdwinB->ConstructFromResourceL(reader);
00082     CleanupStack::PopAndDestroy();  // Resource reader
00083 
00084     SizeChanged();
00085     iEdwinA->SetFocus(ETrue);
00086 
00087     // Set the windows size
00088     SetRect(aRect);
00089 
00090     // Activate the window, which marks it ready to be drawn
00091     ActivateL();
00092     }
00093 
00094 // Constructor.
00095 CEComCalculatorAppView::CEComCalculatorAppView()
00096                         :iLabelA(NULL), iLabelB(NULL),
00097                          iEdwinA(NULL), iEdwinB(NULL),
00098                          iLabelResult(NULL)
00099     {
00100     // No implementation required
00101     }
00102 
00103 // Destructor. Release the owned controls
00104 CEComCalculatorAppView::~CEComCalculatorAppView()
00105     {
00106         delete iLabelA;
00107         delete iLabelB;
00108         delete iEdwinA;
00109         delete iEdwinB;
00110         delete iLabelResult;
00111     }
00112 
00113 // Draw the view. Called by framework, when necessary.
00114 void CEComCalculatorAppView::Draw(const TRect& aRect) const
00115     {
00116     //Clear the screen
00117     CWindowGc& gc = SystemGc();
00118     gc.Clear(Rect());
00119     gc.SetBrushColor(KRgbGray);
00120     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00121     gc.DrawRect(aRect);
00122     }
00123 
00124 // Scale the owned controls to reflect current (new) state
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     //Adjust the extent so it doesn't cut off y and p 
00144     //in "multiply", test with out this to see it
00145     TSize extent = iLabelResult->MinimumSize();
00146     extent.iHeight += KBottomMargin;
00147     
00148     iLabelResult->SetExtent(label_result_pos,
00149         //this cuts off the bottom of y and p in "multiply"
00150         //iLabelResult->MinimumSize()); 
00151         extent );
00152     }
00153 
00154 // Tell the framework the number of owned controls
00155 TInt CEComCalculatorAppView::CountComponentControls() const
00156     {
00157     return KNumberOfControls;
00158     }
00159 
00160 // Return owned control to framework so it will draw it.
00161 CCoeControl* CEComCalculatorAppView::ComponentControl(TInt aIndex) const
00162     {
00163     switch (aIndex)
00164         {
00165         case ELabelA:       // 0
00166             return iLabelA;
00167         case EEdwinA:       // 1
00168             return iEdwinA;
00169         case ELabelB:       // 2
00170             return iLabelB;
00171         case EEdwinB:       // 3
00172             return iEdwinB;
00173         case ELabelResult:  // 4
00174             return iLabelResult;
00175         default:
00176             return NULL;
00177         }
00178     }
00179 
00180 // Handle key events. User of this view control must add this to control
00181 // stack to ensure this component is able to get key events. In practise
00182 // see how it is done in AppUI: CEComCalculatorAppUi::ConstructL()
00183 TKeyResponse CEComCalculatorAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00184                             TEventCode aType)
00185     {
00186 
00187     // Handle key up & down. Switch edit boxes, if appropriate
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     // Let the focused edit box handle the key event
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 // Convert text in the edit box to a number.
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 // Convert text in the edit box to a number.
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 // Update text in iLabelResult. Show result of calculation
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     }

Generated by  doxygen 1.6.2