examples/ForumNokia/DBMS/src/DBMSListboxView.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 #include <eikenv.h>
00006 #include <avkon.hrh>
00007 #include <gdi.h>
00008 #include <eiktxlbx.h>  // CEikTextListBox
00009 #include <eiklabel.h>  // CEikLabel
00010 #include <eiktxlbm.h>  // CTextListBoxModel
00011 #include <aknconsts.h>
00012 #include <AknUtils.h>
00013 #include "DBMSListboxView.h"
00014 
00015 
00016 // ---------------------------------------------------------------------------
00017 // CDBMSListboxView::NewL()
00018 //
00019 // Create instance of this view.
00020 // ---------------------------------------------------------------------------
00021 //
00022 CDBMSListboxView* CDBMSListboxView::NewL(const TRect& aRect)
00023     {
00024     CDBMSListboxView* self = new (ELeave) CDBMSListboxView;
00025     CleanupStack::PushL(self);
00026     self->ConstructL(aRect);
00027     CleanupStack::Pop(self);
00028     return self;
00029     }
00030 
00031 
00032 // ---------------------------------------------------------------------------
00033 // CDBMSListboxView::ConstructL()
00034 //
00035 // Perform the second phase construction. Construct the contained controls.
00036 // ---------------------------------------------------------------------------
00037 //
00038 void CDBMSListboxView::ConstructL(const TRect& aRect)
00039     {
00040     // Create a window for this application view
00041     CreateWindowL();
00042         _LIT(KListboxView, "Listbox view");
00043 
00044     iLabel = new (ELeave) CEikLabel();
00045     iLabel->SetTextL(KListboxView);
00046     iLabel->SetContainerWindowL( *this );
00047 
00048     iListBox = new(ELeave)CEikTextListBox();
00049     iListBox->ConstructL( this);
00050     iListBox->SetContainerWindowL( *this );
00051 
00052     // Set the windows size
00053     SetRect(aRect);
00054     ActivateL();
00055     }
00056 
00057 
00058 // ---------------------------------------------------------------------------
00059 // CDBMSListboxView::SetCaptionL()
00060 //
00061 // Set caption for this view. Updates the label.
00062 // ---------------------------------------------------------------------------
00063 //
00064 void CDBMSListboxView::SetCaptionL(const TDesC& aNewCaption)
00065     {
00066     iLabel->SetTextL(aNewCaption);
00067     SizeChanged();
00068     }
00069 
00070 
00071 // ---------------------------------------------------------------------------
00072 // CDBMSListboxView::SetListItemsL()
00073 //
00074 // Sets the listbox array and updates the view to reflect new array items.
00075 // The listbox takes ownership of the given aNewItems array.
00076 // ---------------------------------------------------------------------------
00077 //
00078 void CDBMSListboxView::SetListItemsL(CDesCArrayFlat* aNewItems)
00079     {
00080     CTextListBoxModel* model = iListBox->Model();
00081     model->SetItemTextArray(aNewItems);
00082     // Set ListBox model responsible for deleting the listItems array
00083     model->SetOwnershipType( ELbmOwnsItemArray );
00084     iListBox->HandleItemAdditionL();
00085     if(aNewItems->Count()>0)
00086         {
00087         // Select the first item, if there is one.
00088         iListBox->SetCurrentItemIndexAndDraw(0);
00089         }
00090     }
00091 
00092 
00093 // ---------------------------------------------------------------------------
00094 // CDBMSListboxView::CDBMSListboxView()
00095 //
00096 // Constructor
00097 // ---------------------------------------------------------------------------
00098 //
00099 CDBMSListboxView::CDBMSListboxView()
00100     {
00101     // No implementation required
00102     }
00103 
00104 
00105 // ---------------------------------------------------------------------------
00106 // CDBMSListboxView::~CDBMSListboxView()
00107 //
00108 // Desctructor. Delete contained controls
00109 // ---------------------------------------------------------------------------
00110 //
00111 CDBMSListboxView::~CDBMSListboxView()
00112     {
00113     delete iLabel;
00114     delete iListBox;
00115     }
00116 
00117 
00118 // ---------------------------------------------------------------------------
00119 // CDBMSAppUi::Draw()
00120 //
00121 // Draw the view background. This is called by the framework, when necessary.
00122 //
00123 // Note: The framework will draw the contained controls (label and the listbox)
00124 // by using CountComponentControls() and ComponentControl() methods.
00125 // ---------------------------------------------------------------------------
00126 //
00127 void CDBMSListboxView::Draw(const TRect& /*aRect*/) const
00128     {
00129     //Clear the screen
00130     CWindowGc& gc = SystemGc();
00131     gc.Clear(Rect());
00132     }
00133 
00134 
00135 // ---------------------------------------------------------------------------
00136 // CDBMSListboxView::SizeChanged()
00137 //
00138 // Specify locations and sizes of the label and listbox. Called by
00139 // the framework when the view size is changed
00140 // ---------------------------------------------------------------------------
00141 //
00142 void CDBMSListboxView::SizeChanged()
00143     {
00144 
00145         TRect containerRect = Rect(); // Rect of the container
00146 
00147         TSize labelSize = iLabel->MinimumSize();
00148 
00149         // Show the label in the upper part of the screen and in whole
00150         // screen width
00151         iLabel->SetExtent( TPoint(5,2),
00152                            TSize(containerRect.Width(), labelSize.iHeight) );
00153 
00154         // Fill the rest (bottom) of the screen with listbox
00155         iListBox->SetExtent( TPoint(5,labelSize.iHeight + 5 ),
00156             TSize( containerRect.Width(),
00157                    containerRect.Height()-labelSize.iHeight ) );
00158                    
00159     }
00160 
00161 
00162 // ---------------------------------------------------------------------------
00163 // CDBMSListboxView::CountComponentControls()
00164 //
00165 // Return number of contained controls.
00166 // ---------------------------------------------------------------------------
00167 //
00168 TInt CDBMSListboxView::CountComponentControls() const
00169     {
00170         return 2; // Label and listbox
00171     }
00172 
00173 
00174 // ---------------------------------------------------------
00175 // CDBMSListboxView::ComponentControl()
00176 //
00177 // Return owned controls to framework so it will draw them.
00178 // ---------------------------------------------------------
00179 //
00180 CCoeControl* CDBMSListboxView::ComponentControl(TInt aIndex) const
00181     {
00182     switch ( aIndex )
00183         {
00184         case 0:
00185             return iLabel;
00186         case 1:
00187             return iListBox;
00188         default:
00189             return NULL;
00190         }
00191     }
00192 
00193 // ---------------------------------------------------------------------------
00194 // CDBMSListboxView::OfferKeyEventL()
00195 //
00196 // Handle key events. Let the listbox handle the key events.
00197 //
00198 // AppUi must add this control to control stack to ensure key events are
00199 // received and this method is called.
00200 // ---------------------------------------------------------------------------
00201 //
00202 TKeyResponse CDBMSListboxView::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00203     TEventCode aType)
00204     {
00205         return iListBox->OfferKeyEventL( aKeyEvent, aType );
00206     }
00207 
00208 
00209 // ---------------------------------------------------------------------------
00210 // CDBMSListboxView::GetSelectedItem()
00211 //
00212 // Get name of the selected item in the listbox.
00213 // ---------------------------------------------------------------------------
00214 //
00215 TInt CDBMSListboxView::GetSelectedItem(TDes& aResult) const
00216     {
00217     CTextListBoxModel* model = iListBox->Model();
00218     if(model->NumberOfItems()==0)
00219         {
00220         return KErrNotFound;
00221         }
00222 
00223     aResult.Copy(model->ItemText(iListBox->CurrentItemIndex()));
00224     return KErrNone;
00225     }
00226 
00227 //EOF

Generated by  doxygen 1.6.2