examples/ForumNokia/Symbian_OS_Basics_Lab_Exercises_v3_1/Lab_04304.cb1/starter/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         iTopLabel->SetTextL(KTextVeryLongLabel);
00063         // Edit 2: Uncomment the line below
00064         // User::Leave(KErrNoMemory); // simulates the line below leaving
00065         iBottomLabel->SetTextL(KTextExtremelyLongLabel);
00066         }
00067     iToggleFlag = !iToggleFlag;
00068     }
00069 
00070 void CS60MemoryLabContainer::CleanupStackTestL()
00071     {
00072     // Create string on the heap to store a copy of a literal string
00073     HBufC* labelText = HBufC::NewL(KTextCleanupStackTest().Length());   
00074     // Edit 5: Put labelText on the Cleanup Stack using 
00075     //         CleanupStack::PushL(labelText);
00076     //         Alternatively HBufC::NewLC(KTextCleanupStackTest().Length()) could 
00077     //         be called instead of HBufC::NewL(KTextCleanupStackTest().Length())
00078     *labelText = KTextCleanupStackTest; // Initialise the heap string
00079     // Edit 4: Uncomment the line below
00080     // User::Leave(KErrNoMemory); // mimic the effect of SetTextL() below leaving
00081     iBottomLabel->SetTextL(*labelText);
00082     // Edit 6: Replace the following line with CleanupStack::PopAndDestroy();
00083     delete labelText;
00084     }
00085 
00086 
00087 // ---------------------------------------------------------
00088 // CS60MemoryLabContainer::SizeChanged()
00089 // Called by framework when the view size is changed
00090 // ---------------------------------------------------------
00091 //
00092 void CS60MemoryLabContainer::SizeChanged()
00093     {
00094     // Set the position and size of the labels so they can 
00095     // hold all the text
00096     TRect rect = Rect();
00097     
00098     iTopLabel->SetExtent( TPoint(0,0), TSize(Rect().Width(), Rect().Height()/2));
00099     iBottomLabel->SetExtent( TPoint(0,Rect().Height()/2), TSize(Rect().Width(),Rect().Height()/2));
00100     }
00101 
00102 // ---------------------------------------------------------
00103 // CS60MemoryLabContainer::CountComponentControls() const
00104 // ---------------------------------------------------------
00105 //
00106 TInt CS60MemoryLabContainer::CountComponentControls() const
00107     {
00108     return 2; // return nbr of controls inside this container
00109     }
00110 
00111 // ---------------------------------------------------------
00112 // CS60MemoryLabContainer::ComponentControl(TInt aIndex) const
00113 // ---------------------------------------------------------
00114 //
00115 CCoeControl* CS60MemoryLabContainer::ComponentControl(TInt aIndex) const
00116     {
00117     switch ( aIndex )
00118         {
00119         case 0:
00120             return iTopLabel;
00121         case 1:
00122             return iBottomLabel;
00123         default:
00124             return NULL;
00125         }
00126     }
00127 
00128 // ---------------------------------------------------------
00129 // CS60MemoryLabContainer::Draw(const TRect& aRect) const
00130 // ---------------------------------------------------------
00131 //
00132 void CS60MemoryLabContainer::Draw(const TRect& aRect) const
00133     {
00134     CWindowGc& gc = SystemGc();
00135 
00136     gc.SetPenStyle(CGraphicsContext::ENullPen);
00137     gc.SetBrushColor(KRgbGray);
00138     gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
00139     gc.DrawRect(aRect);
00140     }
00141 
00142 // ---------------------------------------------------------
00143 // CS60MemoryLabContainer::HandleControlEventL(
00144 //     CCoeControl* aControl,TCoeEvent aEventType)
00145 // ---------------------------------------------------------
00146 //
00147 void CS60MemoryLabContainer::HandleControlEventL(
00148     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00149     {
00150     // Do nothing
00151     }
00152     
00153 // End of File  

Generated by  doxygen 1.6.2