examples/ForumNokia/Symbian_OS_Basics_Lab_Exercises_v3_1/Lab_04304.cb1/solution/src/S60MemoryLabContainer.cpp

00001 // Copyright: (c) 2006 Nokia Ltd.  All rights reserved.
00002 
00003 // INCLUDE FILES
00004 #include "S60MemoryLabContainer.h"
00005 #include <eiklabel.h>  // for example label control
00006 #include <eikappui.h>
00007 
00008 // ================= MEMBER FUNCTIONS =======================
00009 
00010 // ---------------------------------------------------------
00011 // CS60MemoryLabContainer::ConstructL(const TRect& aRect)
00012 // Second phase constructor
00013 // ---------------------------------------------------------
00014 //
00015 _LIT(KTextExtremelyLongLabel,    "Extremely Long Label");
00016 _LIT(KTextVeryLongLabel,         "Very Long Label");
00017 _LIT(KTextLongLabel,             "Long Label");
00018 _LIT(KTextLabel,                 "Label");
00019 _LIT(KTextCleanupStackTest,      "Cleanup Stack Test");
00020 
00021 
00022 void CS60MemoryLabContainer::ConstructL(const TRect& aRect)
00023     {
00024     CreateWindowL();
00025 
00026     // Edit 1: uncomment the line below 
00027     // User::Leave(KErrCancel);
00028 
00029     iTopLabel = new (ELeave) CEikLabel;
00030     iTopLabel->SetContainerWindowL(*this);
00031     iTopLabel->SetTextL(KTextLabel);
00032 
00033     iBottomLabel = new (ELeave) CEikLabel;
00034     iBottomLabel->SetContainerWindowL( *this );
00035     iBottomLabel->SetTextL(KTextLongLabel);
00036 
00037     SetRect(aRect);
00038     ActivateL();
00039     }
00040 
00041 // Destructor
00042 CS60MemoryLabContainer::~CS60MemoryLabContainer()
00043     {
00044     delete iTopLabel;
00045     delete iBottomLabel;
00046     }
00047 
00048 void CS60MemoryLabContainer::ToggleLabelsL()
00049     {
00050     if (iToggleFlag)
00051         {
00052         iTopLabel->SetTextL(KTextLabel);
00053         iBottomLabel->SetTextL(KTextLongLabel);
00054         }
00055     else
00056         {
00057         // Edit 3: 
00058         //  a) Add a Trap harness (TRAPD macro) around the following 3 lines of code
00059         //  b) If an error occurs:
00060         //      i) Set the text of iTopLabel to be KTextLabel
00061         //      ii) Raise the error again using User::Leave(error)
00062         TRAPD(
00063             error,
00064             iTopLabel->SetTextL(KTextVeryLongLabel);
00065             // Edit 2: Uncomment the line below
00066             User::Leave(KErrNoMemory); // simulates the line below leaving
00067             iBottomLabel->SetTextL(KTextExtremelyLongLabel);
00068             );
00069             
00070         if (error != KErrNone)
00071             {
00072             // Attempt to reset the label to the previous value 
00073             // This may be unneccessary as setting the text of  
00074             // iTopLabel may have caused the Leave
00075             iTopLabel->SetTextL(KTextLabel);
00076             // Propagate the leave up the call stack
00077             User::Leave(error);
00078             }
00079         }
00080     iToggleFlag = !iToggleFlag;
00081     }
00082                 
00083 void CS60MemoryLabContainer::CleanupStackTestL()
00084     {
00085     // Create string on the heap to store a copy of a literal string
00086     HBufC* labelText = HBufC::NewL(KTextCleanupStackTest().Length());   
00087     // Edit 5: Put labelText on the Cleanup Stack using 
00088     //         CleanupStack::PushL(labelText);
00089     //         Alternatively HBufC::NewLC(KTextCleanupStackTest().Length()) could 
00090     //         be called instead of HBufC::NewL(KTextCleanupStackTest().Length())
00091     CleanupStack::PushL(labelText);
00092     *labelText = KTextCleanupStackTest; // Initialise the heap string
00093     // Edit 4: Uncomment the line below
00094     User::Leave(KErrNoMemory); // mimic the effect of SetTextL() below leaving
00095     iBottomLabel->SetTextL(*labelText);
00096     // Edit 6: Replace the following line with CleanupStack::PopAndDestroy();
00097     CleanupStack::PopAndDestroy();
00098     }
00099 
00100 
00101 // ---------------------------------------------------------
00102 // CS60MemoryLabContainer::SizeChanged()
00103 // Called by framework when the view size is changed
00104 // ---------------------------------------------------------
00105 //
00106 void CS60MemoryLabContainer::SizeChanged()
00107     {
00108     // Set the position and size of the labels so they can 
00109     // hold all the text
00110     TRect rect = Rect();
00111     
00112     iTopLabel->SetExtent( TPoint(0,0), TSize(Rect().Width(), Rect().Height()/2));
00113     iBottomLabel->SetExtent( TPoint(0,Rect().Height()/2), TSize(Rect().Width(),Rect().Height()/2));
00114     }
00115 
00116 // ---------------------------------------------------------
00117 // CS60MemoryLabContainer::CountComponentControls() const
00118 // ---------------------------------------------------------
00119 //
00120 TInt CS60MemoryLabContainer::CountComponentControls() const
00121     {
00122     return 2; // return nbr of controls inside this container
00123     }
00124 
00125 // ---------------------------------------------------------
00126 // CS60MemoryLabContainer::ComponentControl(TInt aIndex) const
00127 // ---------------------------------------------------------
00128 //
00129 CCoeControl* CS60MemoryLabContainer::ComponentControl(TInt aIndex) const
00130     {
00131     switch ( aIndex )
00132         {
00133         case 0:
00134             return iTopLabel;
00135         case 1:
00136             return iBottomLabel;
00137         default:
00138             return NULL;
00139         }
00140     }
00141 
00142 // ---------------------------------------------------------
00143 // CS60MemoryLabContainer::Draw(const TRect& aRect) const
00144 // ---------------------------------------------------------
00145 //
00146 void CS60MemoryLabContainer::Draw(const TRect& aRect) const
00147     {
00148     CWindowGc& gc = SystemGc();
00149 
00150     gc.SetPenStyle(CGraphicsContext::ENullPen);
00151     gc.SetBrushColor(KRgbGray);
00152     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00153     gc.DrawRect(aRect);
00154     }
00155 
00156 // ---------------------------------------------------------
00157 // CS60MemoryLabContainer::HandleControlEventL(
00158 //     CCoeControl* aControl,TCoeEvent aEventType)
00159 // ---------------------------------------------------------
00160 //
00161 void CS60MemoryLabContainer::HandleControlEventL(
00162     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00163     {
00164     // Do nothing
00165     }
00166     
00167 // End of File  

Generated by  doxygen 1.6.2