|
Nokia N97 SDK Example Applications Guide |
00001 /** 00002 * Copyright (c) 2007 Nokia Corporation. 00003 * This material, including documentation and any related 00004 * computer programs, is protected by 00005 * copyright controlled by Nokia Corporation. 00006 */ 00007 00008 #include <coemain.h> 00009 #include <badesca.h> 00010 #include <eiklabel.h> 00011 #include <aknbutton.h> 00012 #include <aknchoicelist.h> 00013 #include <barsread.h> 00014 #include <choicelistex.rsg> 00015 #include <aknsdrawutils.h> 00016 #include <aknsbasicbackgroundcontrolcontext.h> 00017 00018 #include "ChoiceListExContainer.h" 00019 00020 00021 // ============================== CONSTANTS ================================== 00022 00023 const TInt KArrayGranularity ( 1 ); 00024 const TRect KListRect( TPoint( 120, 200 ), TPoint( 400, 250 ) ); 00025 const TRect KLabelRect( TPoint( 20, 50 ), TPoint( 450, 100 ) ); 00026 00027 // Texts for choice list items 00028 _LIT( KListItem, "Item index %d" ); 00029 _LIT( KListItemNew, "New Item" ); 00030 00031 // Message texts 00032 _LIT( KAppName," Choicelist example" ); 00033 _LIT( KNoList," No choice list created" ); 00034 _LIT( KNoItems, "No items on list"); 00035 _LIT( KCreate, " Choice list created" ); 00036 _LIT( KSelect, "Selected item index %d" ); 00037 _LIT( KAdd, "Added item index %d" ); 00038 _LIT( KInsert, "Inserted item to index %d" ); 00039 _LIT( KShow, "Choice list opened" ); 00040 _LIT( KRemove, "Removed item index %d" ); 00041 _LIT( KButtonTxt, "Choice list" ); 00042 _LIT( KButtonHelpTxt, "Button help text" ); 00043 _LIT( KFlag," List position changed" ); 00044 00045 // =========================== MEMBER FUNCTIONS ============================== 00046 00047 00048 // --------------------------------------------------------------------------- 00049 // C++ default Constructor 00050 // --------------------------------------------------------------------------- 00051 // 00052 CChoiceListExContainer::CChoiceListExContainer() 00053 { 00054 } 00055 00056 // --------------------------------------------------------------------------- 00057 // Destructor 00058 // --------------------------------------------------------------------------- 00059 // 00060 CChoiceListExContainer::~CChoiceListExContainer() 00061 { 00062 00063 delete iLabel; 00064 delete iBgContext; 00065 00066 if ( iChoiceList ) 00067 { 00068 delete iChoiceList; 00069 } 00070 } 00071 00072 // --------------------------------------------------------------------------- 00073 // Symbian 2nd Constructor 00074 // --------------------------------------------------------------------------- 00075 // 00076 void CChoiceListExContainer::ConstructL( const TRect& aRect ) 00077 { 00078 CreateWindowL(); 00079 SetRect( aRect ); 00080 00081 // Label for showing messages 00082 iLabel = new ( ELeave ) CEikLabel; 00083 iLabel->SetLabelAlignment( ELayoutAlignCenter ); 00084 iLabel->SetContainerWindowL( *this ); 00085 iLabel->SetTextL( KAppName ); 00086 iLabel->SetRect( KLabelRect ); 00087 00088 // Background context for skinned background 00089 iBgContext = CAknsBasicBackgroundControlContext::NewL( 00090 KAknsIIDSkinBmpMainPaneUsual, TRect( 0, 0, 0, 0 ), ETrue ); 00091 iBgContext->SetRect( Rect() ); 00092 iBgContext->SetParentPos( PositionRelativeToScreen() ); 00093 00094 SizeChanged(); 00095 ActivateL(); 00096 } 00097 00098 // --------------------------------------------------------------------------- 00099 // CChoiceListExContainer::Draw 00100 // --------------------------------------------------------------------------- 00101 // 00102 void CChoiceListExContainer::Draw( const TRect& aRect ) const 00103 { 00104 // Draw background 00105 CWindowGc& gc = SystemGc(); 00106 MAknsSkinInstance* skin = AknsUtils::SkinInstance(); 00107 if ( !AknsDrawUtils::Background( skin, iBgContext, gc, aRect ) ) 00108 { 00109 SystemGc().Clear( aRect ); 00110 } 00111 } 00112 00113 // --------------------------------------------------------------------------- 00114 // CChoiceListExContainer::CountComponentControls 00115 // --------------------------------------------------------------------------- 00116 // 00117 TInt CChoiceListExContainer::CountComponentControls() const 00118 { 00119 TInt count ( 1 ); // iLabel 00120 00121 if ( iChoiceList ) 00122 { 00123 ++count; 00124 } 00125 00126 return count; 00127 } 00128 00129 // --------------------------------------------------------------------------- 00130 // CChoiceListExContainer::ComponentControl 00131 // --------------------------------------------------------------------------- 00132 // 00133 CCoeControl* CChoiceListExContainer::ComponentControl( TInt aIndex ) const 00134 { 00135 if ( aIndex == 0 ) 00136 { 00137 return iLabel; 00138 } 00139 else if ( aIndex == 1 ) 00140 { 00141 return iChoiceList; 00142 } 00143 00144 return NULL; 00145 } 00146 00147 // --------------------------------------------------------------------------- 00148 // CChoiceListExContainer::OfferKeyEventL 00149 // Handles key events by passing them to choice list 00150 // --------------------------------------------------------------------------- 00151 // 00152 TKeyResponse CChoiceListExContainer::OfferKeyEventL( 00153 const TKeyEvent& aKeyEvent,TEventCode aType ) 00154 { 00155 if ( iChoiceList ) 00156 { 00157 return iChoiceList->OfferKeyEventL( aKeyEvent, aType ); 00158 } 00159 return EKeyWasNotConsumed; 00160 } 00161 00162 // --------------------------------------------------------------------------- 00163 // CChoiceListExContainer::HandleControlEventL 00164 // Handles events from the Choice List 00165 // --------------------------------------------------------------------------- 00166 // 00167 void CChoiceListExContainer::HandleControlEventL( CCoeControl* aControl, 00168 TCoeEvent aEventType ) 00169 { 00170 if ( aControl == iChoiceList ) 00171 { 00172 TInt selection; 00173 switch ( aEventType ) 00174 { 00175 case EEventStateChanged: 00176 selection = iChoiceList->SelectedIndex(); 00177 if ( iSelection != selection ) 00178 { 00179 // selected item changed 00180 iSelection = selection; 00181 TBuf<32> buf; 00182 buf.Format( KSelect, selection ); 00183 iLabel->SetTextL( buf ); 00184 iLabel->DrawDeferred(); 00185 } 00186 break; 00187 00188 default: 00189 break; 00190 } 00191 } 00192 } 00193 00194 // --------------------------------------------------------------------------- 00195 // CChoiceListExContainer::CreateDefaultChoiceListL 00196 // Deletes previous Choice list if exists and constructs a new one with an 00197 // array of items. 00198 // --------------------------------------------------------------------------- 00199 // 00200 void CChoiceListExContainer::CreateDefaultChoiceListL() 00201 { 00202 if ( iChoiceList ) 00203 { 00204 delete iChoiceList; 00205 iChoiceList = NULL; 00206 } 00207 00208 // Create array of choice list items 00209 CDesCArrayFlat* array = new ( ELeave ) CDesCArrayFlat ( KArrayGranularity ); 00210 CleanupStack::PushL( array ); 00211 00212 for ( TInt k = 0; k < 10; k++ ) 00213 { 00214 TBuf<32> buf; 00215 buf.Format( KListItem, k ); 00216 array->AppendL( buf ); 00217 } 00218 00219 iArraySize = array->Count(); 00220 iSelection = 0; 00221 00222 // Create choice list with array 00223 iChoiceList = CAknChoiceList::NewL( this, array ); 00224 00225 CleanupStack::Pop( array ); 00226 00227 iChoiceList->SetObserver( this ); 00228 iChoiceList->SetRect( KListRect ); 00229 00230 iLabel->SetTextL( KCreate ); 00231 } 00232 00233 // --------------------------------------------------------------------------- 00234 // CChoiceListExContainer::CreateButtonChoiceListL 00235 // Deletes previous Choice list if exists and constructs a new one with an 00236 // button and item array. 00237 // --------------------------------------------------------------------------- 00238 // 00239 void CChoiceListExContainer::CreateButtonChoiceListL() 00240 { 00241 if ( iChoiceList ) 00242 { 00243 delete iChoiceList; 00244 iChoiceList = NULL; 00245 } 00246 00247 // Create array of choice list items 00248 CDesCArrayFlat* array = new ( ELeave ) CDesCArrayFlat ( KArrayGranularity ); 00249 00250 CleanupStack::PushL( array ); 00251 00252 for ( TInt k = 0; k < 10; k++ ) 00253 { 00254 TBuf<32> buf; 00255 buf.Format( KListItem, k ); 00256 array->AppendL( buf ); 00257 } 00258 00259 iArraySize = array->Count(); 00260 iSelection = 0; 00261 00262 // Create a button for the choice list 00263 CAknButton* button = CAknButton::NewLC( NULL, 00264 NULL, 00265 NULL, 00266 NULL, 00267 KButtonTxt, 00268 KButtonHelpTxt, 00269 KAknButtonSizeFitText, 00270 0 ); 00271 00272 // Create a choice list 00273 iChoiceList = CAknChoiceList::NewL( this, array, 00274 CAknChoiceList::EAknChoiceListWithoutCurrentSelection, 00275 button ); 00276 CleanupStack::Pop( button ); 00277 CleanupStack::Pop( array ); 00278 00279 iChoiceList->SetObserver( this ); 00280 iChoiceList->SetRect( KListRect ); 00281 00282 iLabel->SetTextL( KCreate ); 00283 } 00284 00285 // --------------------------------------------------------------------------- 00286 // CChoiceListExContainer::CreateResourceChoiceListL 00287 // Deletes previous Choice list if exists and constructs a new one from 00288 // resources defined in resource file ChoiceListEx.rss 00289 // --------------------------------------------------------------------------- 00290 // 00291 void CChoiceListExContainer::CreateResourceChoiceListL() 00292 { 00293 if ( iChoiceList ) 00294 { 00295 delete iChoiceList; 00296 iChoiceList = NULL; 00297 } 00298 00299 // Create empty Choice list 00300 iChoiceList = CAknChoiceList::NewL( this, NULL ); 00301 00302 // Add items from resource 00303 iChoiceList->SetItemsL( R_CHOICELISTEX_ITEM_ARRAY ); 00304 iArraySize = ArraySizeL( R_CHOICELISTEX_ITEM_ARRAY ); 00305 iSelection = 0; 00306 00307 iChoiceList->SetObserver( this ); 00308 iChoiceList->SetRect( KListRect ); 00309 00310 iLabel->SetTextL( KCreate ); 00311 } 00312 00313 // --------------------------------------------------------------------------- 00314 // CChoiceListExContainer::ShowListL 00315 // Opens the choice list 00316 // --------------------------------------------------------------------------- 00317 // 00318 void CChoiceListExContainer::ShowListL() 00319 { 00320 if ( !iChoiceList ) 00321 { 00322 iLabel->SetTextL( KNoList ); 00323 return; 00324 } 00325 iChoiceList->ShowChoiceListL(); 00326 00327 iLabel->SetTextL( KShow ); 00328 } 00329 00330 // --------------------------------------------------------------------------- 00331 // CChoiceListExContainer::AddItemL 00332 // Adds a new item to the choice list 00333 // --------------------------------------------------------------------------- 00334 // 00335 void CChoiceListExContainer::AddItemL() 00336 { 00337 if ( !iChoiceList ) 00338 { 00339 iLabel->SetTextL( KNoList ); 00340 return; 00341 } 00342 00343 TInt index = iChoiceList->AddItemL( &KListItemNew ); 00344 ++iArraySize; 00345 00346 TBuf<32> buf; 00347 buf.Format( KAdd, index ); 00348 iLabel->SetTextL( buf ); 00349 } 00350 00351 // --------------------------------------------------------------------------- 00352 // CChoiceListExContainer::InsertItemL 00353 // Inserts a new item in the middle of the list. 00354 // --------------------------------------------------------------------------- 00355 // 00356 void CChoiceListExContainer::InsertItemL() 00357 { 00358 if ( !iChoiceList ) 00359 { 00360 iLabel->SetTextL( KNoList ); 00361 return; 00362 } 00363 TInt point = TInt ( iArraySize / 2 ); 00364 00365 TInt index = iChoiceList->InsertItemL( point, KListItemNew ); 00366 ++iArraySize; 00367 00368 TBuf<32> buf; 00369 buf.Format( KInsert, index ); 00370 iLabel->SetTextL( buf ); 00371 } 00372 00373 // --------------------------------------------------------------------------- 00374 // CChoiceListExContainer::RemoveItemL 00375 // Removes the last item from the choice list 00376 // --------------------------------------------------------------------------- 00377 // 00378 void CChoiceListExContainer::RemoveItemL() 00379 { 00380 if ( !iChoiceList ) 00381 { 00382 iLabel->SetTextL( KNoList ); 00383 return; 00384 } 00385 00386 if ( iArraySize < 1 ) 00387 { 00388 iLabel->SetTextL( KNoItems ); 00389 return; 00390 } 00391 00392 iArraySize--; 00393 iChoiceList->RemoveItem( iArraySize ); 00394 00395 TBuf<32> buf; 00396 buf.Format( KRemove, iArraySize ); 00397 iLabel->SetTextL( buf ); 00398 } 00399 00400 // --------------------------------------------------------------------------- 00401 // CChoiceListExContainer::SelectItemL 00402 // Sets the last item as selected 00403 // --------------------------------------------------------------------------- 00404 // 00405 void CChoiceListExContainer::SelectItemL() 00406 { 00407 if ( !iChoiceList ) 00408 { 00409 iLabel->SetTextL( KNoList ); 00410 return; 00411 } 00412 00413 if ( iArraySize < 1 ) 00414 { 00415 iLabel->SetTextL( KNoItems ); 00416 return; 00417 } 00418 00419 iChoiceList->SetSelectedIndex( iArraySize - 1 ); 00420 00421 TBuf<32> buf; 00422 buf.Format( KSelect, iArraySize-1 ); 00423 iLabel->SetTextL( buf ); 00424 } 00425 00426 // --------------------------------------------------------------------------- 00427 // CChoiceListExContainer::ChangeListFlagsL 00428 // Changes the choice list flags ( Position ) 00429 // --------------------------------------------------------------------------- 00430 // 00431 void CChoiceListExContainer::ChangeListFlagsL( 00432 CAknChoiceList::TAknChoiceListFlags aFlag ) 00433 { 00434 if ( !iChoiceList ) 00435 { 00436 iLabel->SetTextL( KNoList ); 00437 return; 00438 } 00439 iChoiceList->SetFlags( aFlag ); 00440 iLabel->SetTextL( KFlag ); 00441 } 00442 00443 // --------------------------------------------------------------------------- 00444 // CChoiceListExContainer::ArraySize 00445 // Returns the size of the given array 00446 // --------------------------------------------------------------------------- 00447 // 00448 TInt CChoiceListExContainer::ArraySizeL( TInt aResourceId ) 00449 { 00450 TResourceReader reader; 00451 CCoeEnv::Static()->CreateResourceReaderLC( reader, aResourceId ); 00452 CDesCArray* array = NULL; 00453 array = reader.ReadDesC16ArrayL(); 00454 CleanupStack::PopAndDestroy(); // reader 00455 00456 TInt size = 0; 00457 size = array->Count(); 00458 delete array; 00459 00460 return size; 00461 } 00462 00463 // --------------------------------------------------------------------------- 00464 // CChoiceListExContainer::HandleResourceChange 00465 // 00466 // --------------------------------------------------------------------------- 00467 // 00468 void CChoiceListExContainer::HandleResourceChange( TInt aType ) 00469 { 00470 CCoeControl::HandleResourceChange( aType ); 00471 if ( aType == KEikDynamicLayoutVariantSwitch ) 00472 { 00473 TRect rect; 00474 AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect); 00475 SetRect(rect); 00476 00477 iBgContext->SetRect( Rect() ); 00478 iBgContext->SetParentPos( PositionRelativeToScreen() ); 00479 } 00480 } 00481 00482 // --------------------------------------------------------------------------- 00483 // CChoiceListExContainer::SizeChanged 00484 // 00485 // --------------------------------------------------------------------------- 00486 // 00487 void CChoiceListExContainer::SizeChanged() 00488 { 00489 if( iLabel) 00490 { 00491 iLabel->SetRect( TRect( TPoint(Rect().iTl.iX, Rect().iTl.iY + 35), TPoint(Rect().iBr.iX, Rect().iTl.iY + 75 ) ) ); 00492 } 00493 if( iChoiceList ) 00494 { 00495 iChoiceList->SetRect( TRect( TPoint(Rect().iTl.iX, Rect().iTl.iY + 75), TPoint(Rect().iBr.iX, Rect().iTl.iY + 105 ) ) ); 00496 } 00497 00498 }
|
© Nokia 2009 |