Nokia N97 SDK
Example Applications Guide

ListboxNumberContainer.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : ListboxNumberContainer.cpp
00004 *  Part of     : Listbox example
00005 *  Interface   :
00006 *  Description :
00007 *  Version     :
00008 *
00009 *  Copyright (c) 2007 Nokia Corporation.
00010 *  This material, including documentation and any related
00011 *  computer programs, is protected by copyright controlled by
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 #include <aknlists.h> 
00017 #include <barsread.h> 
00018 #include <listbox.rsg>
00019 #include <eikclb.h> 
00020 #include <StringLoader.h>
00021 #include <Listbox.rsg>
00022 
00023 #include "ListboxNumberContainer.h"
00024 #include "NaviScrollTimer.h"
00025 
00026 
00027 // Listbox class name is shown on navigation pane
00028 _LIT( KNaviPaneText, "CAknSingleNumberStyleListBox" );
00029 _LIT( KNumberSeparator, "\t");
00030 
00031 // ============================ MEMBER FUNCTIONS ===============================
00032 
00033 // -----------------------------------------------------------------------------
00034 // CListboxNumberContainer::CListboxNumberContainer()
00035 // C++ default constructor
00036 // -----------------------------------------------------------------------------
00037 //
00038 CListboxNumberContainer::CListboxNumberContainer()
00039     {        
00040     }
00041 
00042 // -----------------------------------------------------------------------------
00043 // CListboxNumberContainer::~CListboxNumberContainer()
00044 // Destructor
00045 // -----------------------------------------------------------------------------
00046 //
00047 CListboxNumberContainer::~CListboxNumberContainer()
00048     {
00049     if( iListBox ) delete iListBox;    
00050     if( iTimer ) delete iTimer;
00051     }
00052 
00053 // -----------------------------------------------------------------------------
00054 // CListboxNumberContainer::ConstructL()
00055 // 2nd phase constructor
00056 // -----------------------------------------------------------------------------
00057 //    
00058 void CListboxNumberContainer::ConstructL( const TRect& aRect )
00059     {
00060     CreateWindowL(); // Creates window.
00061         
00062     SetRect( aRect ); // Sets rectangle of frame.
00063         
00064     CreateListboxL();
00065     iListBox->SetRect(Rect());
00066     
00067     ActivateL(); // Activates window. ( Ready to draw )       
00068     
00069     // Show listbox class name in navigation pane
00070     iTimer = CNaviScrollTimer::NewL( KNaviPaneText );
00071     iTimer->StartScroll();         
00072     }
00073 
00074 // -----------------------------------------------------------------------------
00075 // CListboxNumberContainer::CreateListboxL()
00076 // Constructs listbox from resource, creates scroll bar and sets empty
00077 // list background text. 
00078 // -----------------------------------------------------------------------------
00079 //
00080 void CListboxNumberContainer::CreateListboxL()
00081     {
00082     iListBox = new ( ELeave ) CAknSingleNumberStyleListBox();  
00083     
00084     iListBox->SetContainerWindowL( *this );
00085 
00086     TResourceReader reader;
00087     iEikonEnv->CreateResourceReaderLC( reader, 
00088                     R_LISTBOX_SINGLE_NUMBER_STYLE );
00089     
00090     // Construct listbox from resource    
00091     iListBox->ConstructFromResourceL( reader );
00092     CleanupStack::PopAndDestroy(); 
00093     
00094     // Creates scrollbar.
00095     iListBox->CreateScrollBarFrameL( ETrue );
00096     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( 
00097         CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto ); 
00098     
00099     // Set the listbox empty text
00100     HBufC* emptyText = StringLoader::LoadLC( R_LISTBOX_EMPTYTEXT );   
00101     iListBox->View()->SetListEmptyTextL( emptyText->Des() );  
00102     CleanupStack::PopAndDestroy( emptyText );        
00103     }
00104  
00105 
00106 // -----------------------------------------------------------------------------
00107 // CListboxNumberContainer::CountComponentControls()
00108 // Gets the number of controls contained in a compound control.
00109 // -----------------------------------------------------------------------------
00110 //
00111 TInt CListboxNumberContainer::CountComponentControls() const
00112     {
00113     return 1; 
00114     }
00115     
00116 // -----------------------------------------------------------------------------
00117 // CListboxNumberContainer::ComponentControl()
00118 // Gets an indexed component of a compound control.
00119 // -----------------------------------------------------------------------------
00120 //
00121 CCoeControl* CListboxNumberContainer::ComponentControl(TInt aIndex) const
00122     {
00123     switch( aIndex )
00124         {
00125         case 0:
00126             return iListBox;
00127         default:
00128             return NULL;                
00129         }
00130     }
00131     
00132 // -----------------------------------------------------------------------------
00133 // CListboxNumberContainer::SizeChanged()
00134 // Responds to changes to the size and position of the contents of this control.
00135 // -----------------------------------------------------------------------------
00136 //
00137 void CListboxNumberContainer::SizeChanged()
00138     {
00139     if ( iListBox )
00140         {
00141         iListBox->SetRect( Rect() );
00142         }
00143     }
00144 
00145 // -----------------------------------------------------------------------------
00146 // CListboxNumberContainer::AddItemL()
00147 // Adds item to the end of the list.
00148 // -----------------------------------------------------------------------------
00149 //   
00150 void CListboxNumberContainer::AddItemL()
00151     {
00152     // Number of items
00153     TInt numOfItems = iListBox->Model()->NumberOfItems();
00154     // Item array. By default iListBox owns the item array, so no deletion 
00155     // is needed.
00156     CDesCArray* itemArray = 
00157         static_cast<CDesC16ArrayFlat*>(iListBox->Model()->ItemTextArray());
00158         
00159     // if listbox does not have items, load first item from resource.
00160     if( numOfItems < 1 )
00161         {
00162         HBufC* itemText = StringLoader::LoadLC( R_LISTBOX_ITEMTEXT ); 
00163             itemArray->AppendL( itemText->Des() );
00164             CleanupStack::PopAndDestroy( itemText );
00165         }
00166         else // Add new item to the end of the listbox's item array
00167                 {
00168                 // Item text of last item
00169         TPtrC itemPtr = iListBox->Model()->ItemText( numOfItems - 1 );
00170                     
00171             TInt numSep = itemPtr.FindC( KNumberSeparator );
00172             
00173         // Item number in TDes format
00174         TBuf<5> numBuf = itemPtr.Mid( 0, numSep );
00175         // Convert to TInt and add 1
00176         TInt itemIndex(0);
00177         TLex conv( numBuf );
00178         conv.Val( itemIndex );        
00179         ++itemIndex;
00180         
00181         // Create new item text 
00182         _LIT( KItemFormat, "%d\t%S" );
00183         TBuf<10> itemEnd = itemPtr.Mid( numSep + KNumberSeparator().Length() );
00184         TBuf<60> itemText;        
00185         itemText.Format( KItemFormat, itemIndex, &itemEnd );
00186                 
00187         itemArray->AppendL( itemText );       
00188                 }   
00189     
00190     // Update the listbox
00191     iListBox->HandleItemAdditionL();
00192     }
00193 
00194 // -----------------------------------------------------------------------------
00195 // CListboxNumberContainer::RemoveItemL()
00196 // Removes item from the end of the list.
00197 // -----------------------------------------------------------------------------
00198 //          
00199 void CListboxNumberContainer::RemoveItemL()
00200     {
00201     // Number of items
00202     TInt numOfItems = iListBox->Model()->NumberOfItems();
00203     
00204     if( numOfItems > 0 )
00205         {
00206         TInt currentItemIndex = iListBox->CurrentItemIndex();
00207         // Item array. By default iListBox owns the item array, so no deletion 
00208         // is needed.
00209         CDesCArray* itemArray = 
00210             static_cast<CDesC16ArrayFlat*>(iListBox->Model()->ItemTextArray());
00211         // Delete last item
00212         itemArray->Delete( numOfItems - 1 );
00213         
00214         if( currentItemIndex >= ( numOfItems - 1 ) &&
00215             currentItemIndex > 0 )
00216             iListBox->SetCurrentItemIndex( numOfItems - 2 );
00217         }
00218     else
00219         return;           
00220     
00221     // Update the listbox
00222     iListBox->HandleItemAdditionL();
00223     }
00224 
00225 // -----------------------------------------------------------------------------
00226 // CListboxNumberContainer::ItemCount()
00227 // Getter for listbox item count
00228 // -----------------------------------------------------------------------------
00229 //   
00230 TInt CListboxNumberContainer::ItemCount()
00231     {
00232     return iListBox->Model()->NumberOfItems();
00233     }
00234 
00235 // -----------------------------------------------------------------------------
00236 // CListboxNumberContainer::OfferKeyEventL
00237 // Handles key events
00238 // -----------------------------------------------------------------------------
00239 //
00240 TKeyResponse CListboxNumberContainer::OfferKeyEventL(const TKeyEvent &aKeyEvent, 
00241                                                    TEventCode aType)
00242     {
00243     if ( aKeyEvent.iCode == EKeyDownArrow || aKeyEvent.iCode == EKeyUpArrow )
00244         // Forward key events to listbox                
00245         return iListBox->OfferKeyEventL( aKeyEvent, aType );
00246     
00247     return EKeyWasNotConsumed;    
00248     }
00249 
00250 // End of File
00251 

© Nokia 2009

Back to top