|
Nokia N97 SDK Example Applications Guide |
00001 /* 00002 * ============================================================================== 00003 * Name : ListboxGraphicContainer.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 <Listbox_bitmaps.mbg> 00018 #include <gulicon.h> 00019 #include <AknIconUtils.h> 00020 #include <akniconarray.h> 00021 #include <eikclbd.h> 00022 #include <eikapp.h> 00023 #include <aknquerydialog.h> 00024 #include <StringLoader.h> 00025 #include <listbox.rsg> 00026 00027 #include "ListboxGraphicContainer.h" 00028 #include "IconFileProvider.h" 00029 #include "NaviScrollTimer.h" 00030 00031 00032 // Format string for listbox items 00033 _LIT( KListItemFormat, "0\tItem %d" ); 00034 // Listbox class name is shown on navigation pane 00035 _LIT( KNaviPaneText, "CAknSingleGraphicStyleListBox" ); 00036 // Listbox icon file name 00037 _LIT(KMifFileName, "listbox_bitmaps.mif"); 00038 00039 00040 // ============================ MEMBER FUNCTIONS =============================== 00041 00042 // ----------------------------------------------------------------------------- 00043 // CListboxGraphicContainer::CListboxGraphicContainer() 00044 // C++ default constructor 00045 // ----------------------------------------------------------------------------- 00046 // 00047 CListboxGraphicContainer::CListboxGraphicContainer() 00048 { 00049 } 00050 00051 // ----------------------------------------------------------------------------- 00052 // CListboxGraphicContainer::~CListboxGraphicContainer() 00053 // Destructor 00054 // ----------------------------------------------------------------------------- 00055 // 00056 CListboxGraphicContainer::~CListboxGraphicContainer() 00057 { 00058 if( iListBox ) delete iListBox; 00059 iFsSession.Close(); 00060 if( iFileProvider ) delete iFileProvider; 00061 if( iTimer ) delete iTimer; 00062 } 00063 00064 // ----------------------------------------------------------------------------- 00065 // CListboxGraphicContainer::ConstructL() 00066 // 2nd phase constructor 00067 // ----------------------------------------------------------------------------- 00068 // 00069 void CListboxGraphicContainer::ConstructL( const TRect& aRect ) 00070 { 00071 // Creates window. 00072 CreateWindowL(); 00073 // Sets rectangle of frame. 00074 SetRect( aRect ); 00075 00076 CreateListboxL(); 00077 00078 iListBox->SetRect(Rect()); 00079 00080 // Set lisbox icons 00081 SetIconsL(); 00082 // Set listbox item texts 00083 SetItemsL(); 00084 // Activates window. ( Ready to draw ) 00085 ActivateL(); 00086 // Show listbox class name in navigation pane 00087 iTimer = CNaviScrollTimer::NewL( KNaviPaneText ); 00088 iTimer->StartScroll(); 00089 00090 CEikMenuBar* menuBar = CEikonEnv::Static()->AppUiFactory()->MenuBar(); 00091 menuBar->SetContextMenuTitleResourceId( 00092 R_LISTBOX_GRAPHICVIEW_MSK_MENUBAR ); 00093 } 00094 00095 00096 // ----------------------------------------------------------------------------- 00097 // CListboxGraphicContainer::CreateListboxL() 00098 // Constructs listbox, creates scroll bar, sets empty list background text, and 00099 // enables marquee. 00100 // ----------------------------------------------------------------------------- 00101 // 00102 void CListboxGraphicContainer::CreateListboxL() 00103 { 00104 iListBox = new ( ELeave ) CAknSingleGraphicStyleListBox(); 00105 iListBox->ConstructL(this); 00106 iListBox->SetContainerWindowL( *this ); 00107 00108 // Creates scrollbar. 00109 iListBox->CreateScrollBarFrameL( ETrue ); 00110 iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( 00111 CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto ); 00112 00113 // Enable marquee 00114 iListBox->ItemDrawer()->ColumnData()->EnableMarqueeL( ETrue ); 00115 00116 // Set empty text for the listbox. 00117 HBufC* emptyText = StringLoader::LoadLC( R_LISTBOX_EMPTYTEXT ); 00118 iListBox->View()->SetListEmptyTextL( emptyText->Des() ); 00119 CleanupStack::PopAndDestroy( emptyText ); 00120 } 00121 00122 // ----------------------------------------------------------------------------- 00123 // CListboxGraphicContainer::SetIconsL() 00124 // Sets icons for the listbox. 00125 // ----------------------------------------------------------------------------- 00126 // 00127 void CListboxGraphicContainer::SetIconsL() 00128 { 00129 CArrayPtr<CGulIcon>* iconList = new (ELeave) CAknIconArray(1); 00130 // push iconarray to stack 00131 CleanupStack::PushL(iconList); 00132 00133 User::LeaveIfError( iFsSession.Connect() ); 00134 // Let other sessions to use this. ShareProtected() must be called for 00135 // enabling other other threads in this process to use this handle. 00136 iFsSession.ShareProtected(); 00137 TBuf<256> path; 00138 iFsSession.PrivatePath( path ); 00139 00140 path.Append( KMifFileName() ); 00141 // insert the drive to the private path 00142 TParsePtrC parse( 00143 (CEikonEnv::Static()->EikAppUi()->Application())->AppFullName() ); 00144 path.Insert( 0, parse.Drive() ); 00145 00146 CFbsBitmap* bitmap = NULL; 00147 CFbsBitmap* mask = NULL; 00148 00149 iFileProvider = CIconFileProvider::NewL( iFsSession, path ); 00150 00151 // Create icons. This function uses CIconFileProvider to pass handle to 00152 // icon file (which lies in application private directory) to AknIcon 00153 // server. This server does not have access to private directories. 00154 AknIconUtils::CreateIconL(bitmap, 00155 mask, 00156 *iFileProvider, 00157 EMbmListbox_bitmapsListbox_icon, 00158 EMbmListbox_bitmapsListbox_icon_mask); 00159 00160 CGulIcon* newIcon = CGulIcon::NewL( bitmap, mask ); 00161 iconList->AppendL( newIcon ); 00162 00163 iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconList ); 00164 CleanupStack::Pop(iconList); 00165 } 00166 00167 // ----------------------------------------------------------------------------- 00168 // CListboxGraphicContainer::SetItemsL() 00169 // Sets items to the listbox. 00170 // ----------------------------------------------------------------------------- 00171 // 00172 void CListboxGraphicContainer::SetItemsL() 00173 { 00174 TBuf<20> itemText; 00175 00176 CDesCArray* itemArray = new ( ELeave ) CDesCArrayFlat(4); 00177 00178 for( TInt i = 1; i < 5; ++i ) 00179 { 00180 itemText.Format( KListItemFormat(), i ); 00181 itemArray->AppendL( itemText ); 00182 } 00183 00184 iListBox->Model()->SetItemTextArray( itemArray ); 00185 iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); 00186 iListBox->HandleItemAdditionL(); 00187 } 00188 00189 // ----------------------------------------------------------------------------- 00190 // CListboxGraphicContainer::AddItemL() 00191 // Adds item to the listbox. 00192 // ----------------------------------------------------------------------------- 00193 // 00194 void CListboxGraphicContainer::AddItemL() 00195 { 00196 // Icon number for all items. 00197 _LIT( KItemStart, "0\t" ); 00198 // New item text 00199 TBuf<50> itemText; 00200 CAknTextQueryDialog *dialog = CAknTextQueryDialog::NewL( itemText ); 00201 // Maximum length for input text. Can also be defined in 00202 // R_LISTBOX_DATA_QUERY_DIALOG resourse. 00203 dialog->SetMaxLength( 45 ); 00204 // Execute dialog 00205 if (dialog->ExecuteLD( R_LISTBOX_DATA_QUERY_DIALOG ) ) 00206 { 00207 // Insert icon number at the beginning of itemText. Note that max 00208 // length for dialog input text is 45 and itemText size is 50. So 00209 // there is space for item number and column separator. 00210 itemText.Insert( 0, KItemStart() ); 00211 // Item array. By default iListBox owns the item array, so no 00212 // deletion is needed. 00213 CDesCArray* itemArray = 00214 static_cast<CDesC16ArrayFlat*>(iListBox->Model()->ItemTextArray()); 00215 00216 // Insert item to the item array 00217 if( itemArray->Count() > 0 ) 00218 itemArray->InsertL( iListBox->View()->CurrentItemIndex(),itemText ); 00219 else 00220 itemArray->InsertL( 0, itemText ); 00221 00222 // Update the listbox 00223 iListBox->HandleItemAdditionL(); 00224 } 00225 } 00226 00227 // ----------------------------------------------------------------------------- 00228 // CListboxGraphicContainer::RemoveItemL() 00229 // Removes focused item from the listbox. 00230 // ----------------------------------------------------------------------------- 00231 // 00232 void CListboxGraphicContainer::RemoveItemL() 00233 { 00234 // If we don't have items, do nothing and return 00235 if( iListBox->Model()->NumberOfItems() < 1 ) 00236 return; 00237 00238 TInt oldIndex = iListBox->View()->CurrentItemIndex(); 00239 // Item array. By default iListBox owns the item array, so no deletion is 00240 // needed. 00241 CDesCArray* itemArray = 00242 static_cast<CDesC16ArrayFlat*>(iListBox->Model()->ItemTextArray()); 00243 00244 // Remove highlighted item from the listbox. 00245 itemArray->Delete( iListBox->View()->CurrentItemIndex() ); 00246 00247 // If focused item is out of range, bring it forth 00248 if( (oldIndex > ( itemArray->Count()-1 )) && 00249 ( (itemArray->Count()-1) >= 0 ) ) 00250 { 00251 iListBox->View()->SetCurrentItemIndex( itemArray->Count()-1 ); 00252 } 00253 00254 // Update the listbox 00255 iListBox->HandleItemAdditionL(); 00256 } 00257 00258 // ----------------------------------------------------------------------------- 00259 // CListboxGraphicContainer::CountComponentControls() 00260 // Gets the number of controls contained in a compound control. 00261 // ----------------------------------------------------------------------------- 00262 // 00263 TInt CListboxGraphicContainer::CountComponentControls() const 00264 { 00265 return 1; 00266 } 00267 00268 // ----------------------------------------------------------------------------- 00269 // CListboxGraphicContainer::ComponentControl() 00270 // Gets an indexed component of a compound control. 00271 // ----------------------------------------------------------------------------- 00272 // 00273 CCoeControl* CListboxGraphicContainer::ComponentControl(TInt aIndex) const 00274 { 00275 switch( aIndex ) 00276 { 00277 case 0: 00278 return iListBox; 00279 default: 00280 return NULL; 00281 } 00282 } 00283 00284 // ----------------------------------------------------------------------------- 00285 // CListboxGraphicContainer::SizeChanged() 00286 // Responds to changes to the size and position of the contents of this control. 00287 // ----------------------------------------------------------------------------- 00288 // 00289 void CListboxGraphicContainer::SizeChanged() 00290 { 00291 if ( iListBox ) 00292 { 00293 iListBox->SetRect( Rect() ); 00294 } 00295 } 00296 00297 // ----------------------------------------------------------------------------- 00298 // CListboxGraphicContainer::OfferKeyEventL 00299 // Handles key events 00300 // ----------------------------------------------------------------------------- 00301 // 00302 TKeyResponse CListboxGraphicContainer::OfferKeyEventL(const TKeyEvent &aKeyEvent, 00303 TEventCode aType) 00304 { 00305 if ( aKeyEvent.iCode == EKeyDownArrow || aKeyEvent.iCode == EKeyUpArrow ) 00306 // Forward key events to listbox 00307 return iListBox->OfferKeyEventL( aKeyEvent, aType ); 00308 00309 return EKeyWasNotConsumed; 00310 } 00311 00312 // ----------------------------------------------------------------------------- 00313 // CListboxGraphicContainer::ItemCount() 00314 // Getter for listbox item count. 00315 // ----------------------------------------------------------------------------- 00316 // 00317 TInt CListboxGraphicContainer::ItemCount() 00318 { 00319 return iListBox->Model()->NumberOfItems(); 00320 } 00321 00322 // End of File 00323
|
© Nokia 2009 |