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

00001 /*
00002  * Copyright (c) 2003-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *    
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *    
00007  *  * Redistributions of source code must retain the above copyright notice, this
00008  *    list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright notice,
00010  *    this list of conditions and the following disclaimer in the documentation
00011  *    and/or other materials provided with the distribution.
00012  *  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *    may be used to endorse or promote products derived from this software
00014  *    without specific prior written permission.
00015  *    
00016  *    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  *    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  *    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  *    DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  *    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  *    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  *    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  *    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  *    OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  *    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *    
00027  *    Description:  
00028  */
00029 
00030 
00031 #include <barsread.h>  // for resource reader
00032 #include <eiklabel.h>  // for label controls
00033 #include <eikedwin.h>  // for CEikEdwin
00034 
00035 #include "EComCalculatorAppView.h"
00036 #include <EComCalculator.rsg> // Generated from EComCalculator.rss
00037 
00038 // Constants used in controls' position calculations
00039 const TInt KXPos = 10; //x position
00040 
00041 // Constants
00042 const TInt KNumberOfControls = 5; //Number of UI controls within this container
00043 const TInt KBottomMargin = 10; //The amount of space to leave to bottom
00044 const TInt KResultSize = 128;
00045 
00046 // Constant texts
00047 _LIT(KLabelTextA,"Number A:");
00048 _LIT(KLabelTextB,"Number B:");
00049 _LIT(KTextEmpty, "");
00050 
00051 // Enumarations
00052 enum TControls
00053     {
00054     ELabelA,
00055     EEdwinA,
00056     ELabelB,
00057     EEdwinB,
00058     ELabelResult
00059     };
00060 
00061 // Create instance of this compound control.
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 // Perform the second phase construction. Instantiate the owned controls.
00072 void CEComCalculatorAppView::ConstructL(const TRect& aRect)
00073     {
00074 
00075     // Create a window for this application view
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();  // Resource reader
00096 
00097     iCoeEnv->CreateResourceReaderLC(reader, R_ECOMCALCULATOR_EDWIN);
00098     iEdwinB = new (ELeave) CEikEdwin;
00099     iEdwinB->SetContainerWindowL(*this);
00100     iEdwinB->ConstructFromResourceL(reader);
00101     CleanupStack::PopAndDestroy();  // Resource reader
00102 
00103     SizeChanged();
00104     iEdwinA->SetFocus(ETrue);
00105 
00106     // Set the windows size
00107     SetRect(aRect);
00108 
00109     // Activate the window, which marks it ready to be drawn
00110     ActivateL();
00111     }
00112 
00113 // Constructor.
00114 CEComCalculatorAppView::CEComCalculatorAppView()
00115                         :iLabelA(NULL), iLabelB(NULL),
00116                          iEdwinA(NULL), iEdwinB(NULL),
00117                          iLabelResult(NULL)
00118     {
00119     // No implementation required
00120     }
00121 
00122 // Destructor. Release the owned controls
00123 CEComCalculatorAppView::~CEComCalculatorAppView()
00124     {
00125         delete iLabelA;
00126         delete iLabelB;
00127         delete iEdwinA;
00128         delete iEdwinB;
00129         delete iLabelResult;
00130     }
00131 
00132 // Draw the view. Called by framework, when necessary.
00133 void CEComCalculatorAppView::Draw(const TRect& aRect) const
00134     {
00135     //Clear the screen
00136     CWindowGc& gc = SystemGc();
00137     gc.Clear(Rect());
00138     gc.SetBrushColor(KRgbGray);
00139     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00140     gc.DrawRect(aRect);
00141     }
00142 
00143 // Scale the owned controls to reflect current (new) state
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     //Adjust the extent so it doesn't cut off y and p 
00163     //in "multiply", test with out this to see it
00164     TSize extent = iLabelResult->MinimumSize();
00165     extent.iHeight += KBottomMargin;
00166     
00167     iLabelResult->SetExtent(label_result_pos,
00168         //this cuts off the bottom of y and p in "multiply"
00169         //iLabelResult->MinimumSize()); 
00170         extent );
00171     }
00172 
00173 // Tell the framework the number of owned controls
00174 TInt CEComCalculatorAppView::CountComponentControls() const
00175     {
00176     return KNumberOfControls;
00177     }
00178 
00179 // Return owned control to framework so it will draw it.
00180 CCoeControl* CEComCalculatorAppView::ComponentControl(TInt aIndex) const
00181     {
00182     switch (aIndex)
00183         {
00184         case ELabelA:       // 0
00185             return iLabelA;
00186         case EEdwinA:       // 1
00187             return iEdwinA;
00188         case ELabelB:       // 2
00189             return iLabelB;
00190         case EEdwinB:       // 3
00191             return iEdwinB;
00192         case ELabelResult:  // 4
00193             return iLabelResult;
00194         default:
00195             return NULL;
00196         }
00197     }
00198 
00199 // Handle key events. User of this view control must add this to control
00200 // stack to ensure this component is able to get key events. In practise
00201 // see how it is done in AppUI: CEComCalculatorAppUi::ConstructL()
00202 TKeyResponse CEComCalculatorAppView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00203                             TEventCode aType)
00204     {
00205 
00206     // Handle key up & down. Switch edit boxes, if appropriate
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     // Let the focused edit box handle the key event
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 // Convert text in the edit box to a number.
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 // Convert text in the edit box to a number.
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 // Update text in iLabelResult. Show result of calculation
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     }

Generated by  doxygen 1.6.2