examples/ForumNokia/SMSExample/GUI/src/SMSExampleMarkableListContainer.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 // INCLUDE FILES
00006 #include "SMSExampleMarkableListContainer.h"
00007 #include "SmsExamplePanics.pan"
00008 
00009 #include <akniconarray.h> // CAknIcon
00010 #include <aknlists.h> // CAknDoubleGraphicStyleListBox
00011 #include <barsread.h> // TResource Reader
00012 #include <e32def.h> // STATIC_CAST
00013 #include <SMSExample.mbg> // icons
00014 #include <smsexample.rsg> // R_SMSEXAMPLE_MARKABLE_LISTBOX
00015 #include <stringloader.h> // StringLoader
00016 #include <uikon.hrh> // TKeyCode #defines
00017 
00018 #ifdef __UI_FRAMEWORKS_V2__
00019     #include <GULICON.H>
00020     #include <AknIconUtils.h>
00021     #include <FBS.H> //fbscli.lib
00022 #endif
00023 
00024 #define KListPosition TPoint(0,0)
00025 
00026 const TInt KAknExListAddItemBufLength(512);
00027 const TInt KNumberOfIcons(2);
00028 const TInt KBeginIdx=0;
00029 const TInt KEndIdx=2;
00030 
00031 _LIT(KIconSelectedText,"1\t");
00032 _LIT(KIconNotSelectedText,"0\t");
00033 
00034 // ----------------------------------------------------------------------------
00035 // CMarkableListContainer::ConstructL(const TRect& aRect)
00036 //
00037 // Symbian OS 2 phase constructor. Creates a Window for the controls, which it
00038 // contains.
00039 // ----------------------------------------------------------------------------
00040 void CMarkableListContainer::ConstructL(const TRect& aRect)
00041     {
00042     CreateWindowL();
00043 
00044     // Create the list
00045     CreateListL();
00046 
00047     iAddress = 0;
00048     iMessage = 0;
00049 
00050     // Set the icons in the list's drawer
00051     SetupListIconsL();
00052 
00053     // Set up scroll bars
00054     SetupScrollBarsL();
00055 
00056     iSelectedIds = new (ELeave) RArray<TMsvId>;
00057 
00058     SetRect(aRect);
00059     ActivateL();
00060     }
00061 
00062 // ----------------------------------------------------------------------------
00063 // CMarkableListContainer::CreateListL()
00064 //
00065 // 2nd phase constructor constructs the iSmsListBox and sets its window.
00066 // ----------------------------------------------------------------------------
00067 void CMarkableListContainer::CreateListL()
00068     {
00069     // First phase construction
00070     iSmsListBox = new (ELeave) CAknDoubleGraphicStyleListBox();
00071     iSmsListBox->SetContainerWindowL(*this);
00072     iSmsListBox->SetListBoxObserver(this);
00073 
00074     // Second Phase Construction
00075     TResourceReader reader;
00076     CEikonEnv::Static()->CreateResourceReaderLC(reader,
00077                                                 R_SMSEXAMPLE_MARKABLE_LISTBOX);
00078     iSmsListBox->ConstructFromResourceL(reader);
00079     CleanupStack::PopAndDestroy(); // reader
00080     }
00081 
00082 // ----------------------------------------------------------------------------
00083 // CMarkableListContainer::SetupScrollBarsL()
00084 //
00085 // Create scrollbars. Scrollbars appear automatically.
00086 // ----------------------------------------------------------------------------
00087 void CMarkableListContainer::SetupScrollBarsL()
00088     {
00089     iSmsListBox->CreateScrollBarFrameL(ETrue);
00090     iSmsListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
00091         CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
00092     }
00093 
00094 // ----------------------------------------------------------------------------
00095 // CMarkableListContainer::SetArraysL(CDesCArrayFlat* aAddress,
00096 //                                    CDesCArrayFlat* aMessage )
00097 //
00098 // Insert both lists into listbox.
00099 // ----------------------------------------------------------------------------
00100 void CMarkableListContainer::SetArraysL(CDesCArrayFlat* aAddress,
00101                                         CDesCArrayFlat* aMessage )
00102     {
00103     iAddress = aAddress;
00104     iMessage = aMessage;
00105 
00106     for (TInt i = 0; i < aAddress->Count(); i++)
00107         {
00108         AddItemL( (*aAddress)[i], (*aMessage)[i] );
00109         }
00110     }
00111 
00112 // ----------------------------------------------------------------------------
00113 // CMarkableListContainer::MarkAllL(TBool aSelectionMode)
00114 //
00115 // Select or deselect all messages that ListBox displayes.
00116 // ----------------------------------------------------------------------------
00117 void CMarkableListContainer::MarkAllL(TBool aSelectionMode)
00118     {
00119     if  (iSmsListBox)
00120         {
00121         // Not taking ownership
00122         CTextListBoxModel* model = iSmsListBox->Model();
00123         model->SetOwnershipType (ELbmOwnsItemArray);
00124         CDesCArray* smsArray = STATIC_CAST(CDesCArray*,model->ItemTextArray());
00125 
00126         // Change all icon values.
00127         for (TInt i = 0; i < smsArray->Count(); i++)
00128             {
00129             TBuf <KAknExListAddItemBufLength> changeItem( (*smsArray)[i] );
00130 
00131 
00132             if ( aSelectionMode ) // Select one item
00133                 {
00134                 // Change the icon to selected
00135                 changeItem.Replace(KBeginIdx, KEndIdx, KIconSelectedText);
00136                 }
00137             else  // Deselect one item
00138                 {
00139                 // Change the icon to not selected
00140                 changeItem.Replace(KBeginIdx, KEndIdx, KIconNotSelectedText);
00141                 }
00142 
00143                 // Delete one item
00144                 smsArray->Delete(i);
00145                 // Insert changed item back.
00146                 smsArray->InsertL(i, changeItem);
00147             }
00148 
00149             // Change selected ids array to match the case.
00150             if ( aSelectionMode )
00151                 {
00152                 // Select all.
00153                 SelectItem( 0, ETrue );
00154                 }
00155             else
00156                 {
00157                 // Deselect all.
00158                 DeSelectItem( 0, ETrue );
00159                 }
00160 
00161             iSmsListBox->HandleItemAdditionL();
00162         }
00163     }
00164 
00165 // ----------------------------------------------------------------------------
00166 // CMarkableListContainer::MarkOrUnMarkL()
00167 //
00168 // Change icon and modify iIdArray to correspond situation.
00169 // The icon is used to display if the item has been selected or not.
00170 // ----------------------------------------------------------------------------
00171 void CMarkableListContainer::MarkOrUnMarkL()
00172     {
00173     if  (iSmsListBox)
00174         {
00175         // Cursor's current position
00176         TInt itemIndex = iSmsListBox->CurrentItemIndex();
00177 
00178         if (itemIndex >= 0)
00179             {
00180             // not taking ownership
00181             CTextListBoxModel* model = iSmsListBox->Model();
00182             model->SetOwnershipType (ELbmOwnsItemArray);
00183             CDesCArray* smsArray = STATIC_CAST(CDesCArray*,
00184                                                model->ItemTextArray());
00185 
00186             if( smsArray->Count() <= itemIndex )
00187                 {
00188                 User::Leave(KErrTooBig);
00189                 }
00190 
00191             TBuf <KAknExListAddItemBufLength> changeItem(
00192                                                         (*smsArray)[itemIndex]
00193                                                         );
00194 
00195             // Compare index to see which is the case. Deselected -> Selected
00196             if ( changeItem.Left(KEndIdx) == KIconNotSelectedText )
00197                 {
00198                 changeItem.Replace(KBeginIdx, KEndIdx, KIconSelectedText);
00199                 // Modify iIdArray
00200                 SelectItem( itemIndex, EFalse );
00201                 } // Selected -> Deselected
00202             else if ( changeItem.Left(KEndIdx) == KIconSelectedText )
00203                 {
00204                 changeItem.Replace(KBeginIdx, KEndIdx, KIconNotSelectedText);
00205                 // Modify iIdArray
00206                 DeSelectItem( itemIndex, EFalse );
00207                 }
00208 
00209             smsArray->Delete(itemIndex);
00210             smsArray->InsertL(itemIndex, changeItem);
00211 
00212             iSmsListBox->HandleItemAdditionL();
00213             }
00214         }
00215     }
00216 
00217 // ----------------------------------------------------------------------------
00218 // CMarkableListContainer::SelectItem(TInt aIndex, TBool aSelectAll)
00219 //
00220 // Select item.
00221 // ----------------------------------------------------------------------------
00222 void CMarkableListContainer::SelectItem(TInt aIndex, TBool aSelectAll)
00223     {
00224     // Copy
00225     if ( aSelectAll )
00226         {
00227         iSelectedIds->Reset();
00228         for (TInt i = 0; i < iIdArray->Count(); i++)
00229             {
00230             iSelectedIds->Append( (*iIdArray)[i] );
00231             }
00232         }
00233     else if( iIdArray->Count() > aIndex )
00234         {
00235         TMsvId id = (*iIdArray)[aIndex];
00236         // Try to find the id, if not present then append id onto list.
00237         if ( KErrNotFound == iSelectedIds->Find( id ) )
00238             {
00239             iSelectedIds->Append( id );
00240             }
00241         }
00242     else
00243         {
00244         Panic(EGuiGeneral);
00245         }
00246 
00247     }
00248 
00249 // ----------------------------------------------------------------------------
00250 // CMarkableListContainer::DeSelectItem(TInt aIndex, TBool aDeselectAll)
00251 //
00252 // Deselect item.
00253 // ----------------------------------------------------------------------------
00254 void CMarkableListContainer::DeSelectItem(TInt aIndex, TBool aDeselectAll)
00255     {
00256     if ( aDeselectAll )
00257         {
00258         iSelectedIds->Reset();
00259         }
00260     else if( iIdArray->Count() > aIndex )
00261         {
00262         TInt index = iSelectedIds->Find( (*iIdArray)[aIndex] );
00263 
00264         if ( index != KErrNotFound  )
00265             {
00266             iSelectedIds->Remove( index );
00267             }
00268         }
00269     else
00270         {
00271         Panic(EGuiGeneral);
00272         }
00273 
00274     }
00275 
00276 // ----------------------------------------------------------------------------
00277 // CMarkableListContainer::SetupListIconsL()
00278 //
00279 // Create icon array.
00280 // ----------------------------------------------------------------------------
00281 void CMarkableListContainer::SetupListIconsL()
00282     {
00283     // Get the name of the file containing the icons
00284     HBufC* iconFilename = NULL;
00285     iconFilename = StringLoader::LoadLC(R_ICON_FILE_NAME); //#ifdef in loc file
00286     // Create an array of icons, reading them from the file
00287     CArrayPtr<CGulIcon>* icons = new(ELeave) CAknIconArray(KNumberOfIcons);
00288     CleanupStack::PushL(icons);
00289 
00290     #ifdef __UI_FRAMEWORKS_V2__ // NOT IN FP3, should executed also there
00291                                 // because scalable ui is supported
00292         CFbsBitmap* icon = NULL;
00293         CFbsBitmap* iconMask = NULL;
00294         CFbsBitmap* icon2 = NULL;
00295         CFbsBitmap* iconMask2 = NULL;
00296 
00297         AknIconUtils::CreateIconLC(icon, iconMask, *iconFilename,
00298                                    EMbmSmsexampleTock,EMbmSmsexampleTock_mask);
00299 
00300 
00301         CGulIcon* guiIcon = CGulIcon::NewL( icon, iconMask );
00302         CleanupStack::PushL(guiIcon);
00303         icons->AppendL( guiIcon );
00304         CleanupStack::Pop(guiIcon);
00305         CleanupStack::Pop(2); //icon, iconMask
00306 
00307         AknIconUtils::CreateIconLC(icon2, iconMask2, *iconFilename,
00308                                    EMbmSmsexampleTick,EMbmSmsexampleTick_mask);
00309 
00310         CGulIcon* guiIcon2 = CGulIcon::NewL( icon2, iconMask2 );
00311         CleanupStack::PushL(guiIcon2);
00312         icons->AppendL( guiIcon2 );
00313         CleanupStack::Pop(guiIcon2);
00314         CleanupStack::Pop(2); //icon2, iconMask2
00315     #else
00316         CGulIcon* icon = iEikonEnv->CreateIconL(*iconFilename,
00317                                                 EMbmSmsexampleTock,
00318                                                 EMbmSmsexampleTock_mask);
00319         CleanupStack::PushL(icon);
00320         icons->AppendL(icon);
00321         CleanupStack::Pop(icon);
00322         icon =iEikonEnv->CreateIconL(*iconFilename, EMbmSmsexampleTick,
00323                                                     EMbmSmsexampleTick_mask);
00324         CleanupStack::PushL(icon);
00325         icons->AppendL(icon);
00326         CleanupStack::Pop(icon);
00327     #endif
00328 
00329     CleanupStack::Pop(icons);
00330 
00331     CleanupStack::PopAndDestroy(iconFilename);
00332 
00333     // passing ownership of icons
00334     iSmsListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
00335     }
00336 
00337 // ----------------------------------------------------------------------------
00338 // CMarkableListContainer::NewL(const TRect& aRect)
00339 //
00340 // Symbian OS 2 phase constructor.
00341 // ----------------------------------------------------------------------------
00342 CMarkableListContainer* CMarkableListContainer::NewL(const TRect& aRect)
00343     {
00344     CMarkableListContainer* self = CMarkableListContainer::NewLC(aRect);
00345     CleanupStack::Pop(self);
00346     return self;
00347     }
00348 
00349 // ----------------------------------------------------------------------------
00350 // CMarkableListContainer::NewLC(const TRect& aRect)
00351 //
00352 // Symbian OS 2 phase constructor.
00353 // ----------------------------------------------------------------------------
00354 CMarkableListContainer* CMarkableListContainer::NewLC(const TRect& aRect)
00355     {
00356     CMarkableListContainer* self = new (ELeave) CMarkableListContainer;
00357     CleanupStack::PushL(self);
00358     self->ConstructL(aRect);
00359     return self;
00360     }
00361 
00362 // ----------------------------------------------------------------------------
00363 // CMarkableListContainer::~CMarkableListContainer()
00364 //
00365 // Destructor.
00366 // ----------------------------------------------------------------------------
00367 CMarkableListContainer::~CMarkableListContainer()
00368     {
00369     Cleanup();
00370     }
00371 
00372 void CMarkableListContainer::Cleanup()
00373     {
00374     iIdArray->Reset();
00375     delete iIdArray;
00376     iIdArray = 0;
00377 
00378     iAddress->Reset();
00379     delete iAddress;
00380     iAddress = 0;
00381 
00382     iMessage->Reset();
00383     delete iMessage;
00384     iMessage = 0;
00385 
00386     delete iSmsListBox;
00387     iSmsListBox = 0;
00388     }
00389 // ----------------------------------------------------------------------------
00390 // CMarkableListContainer::SizeChanged()
00391 //
00392 // Called by framework when the view size is changed
00393 // ----------------------------------------------------------------------------
00394 void CMarkableListContainer::SizeChanged()
00395     {
00396     iSmsListBox->SetExtent (KListPosition, iSmsListBox->MinimumSize());
00397     }
00398 
00399 // ----------------------------------------------------------------------------
00400 // CMarkableListContainer::CountComponentControls() const
00401 //
00402 // Called by the framework in compound controls
00403 // ----------------------------------------------------------------------------
00404 TInt CMarkableListContainer::CountComponentControls() const
00405     {
00406     return 1; // return number of controls inside this container
00407     }
00408 
00409 // ----------------------------------------------------------------------------
00410 // CMarkableListContainer::ComponentControl(TInt aIndex) const
00411 //
00412 // Called by the framework in compound controls
00413 // ----------------------------------------------------------------------------
00414 CCoeControl* CMarkableListContainer::ComponentControl(TInt aIndex) const
00415     {
00416     switch (aIndex)
00417         {
00418         case 0:
00419             return iSmsListBox;
00420         default:
00421             return NULL;
00422         }
00423     }
00424 
00425 // ----------------------------------------------------------------------------
00426 // CMarkableListContainer::Draw(const TRect& aRect) const
00427 //
00428 // Called by the framework to draw this control.
00429 // ----------------------------------------------------------------------------
00430 void CMarkableListContainer::Draw(const TRect& aRect) const
00431     {
00432     CWindowGc& gc = SystemGc();
00433     gc.Clear(aRect);
00434     }
00435 
00436 // ----------------------------------------------------------------------------
00437 // CMarkableListContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00438 //                                              TEventCode aType)
00439 //
00440 // Called by the framework whenever a key event occurs.
00441 // ----------------------------------------------------------------------------
00442 TKeyResponse CMarkableListContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00443                                                     TEventCode aType)
00444     {
00445     if (iSmsListBox)
00446         return iSmsListBox->OfferKeyEventL (aKeyEvent, aType);
00447     else
00448         return EKeyWasNotConsumed;
00449     }
00450 
00451 // ----------------------------------------------------------------------------
00452 // CMarkableListContainer::SetIdArray(RArray<TMsvId>* aArray)
00453 // ----------------------------------------------------------------------------
00454 void CMarkableListContainer::SetIdArray(RArray<TMsvId>* aArray)
00455     {
00456     iIdArray = aArray;
00457     }
00458 
00459 // ----------------------------------------------------------------------------
00460 // CMarkableListContainer::GetSelectedItems()
00461 //
00462 // Return an array of items that have been selected.
00463 // ----------------------------------------------------------------------------
00464 RArray<TMsvId>* CMarkableListContainer::GetSelectedItems()
00465     {
00466     return iSelectedIds;
00467     }
00468 
00469 // ----------------------------------------------------------------------------
00470 // CMarkableListContainer::HandleListBoxEventL(CEikListBox* /*aListBox*/,
00471 //                                             TListBoxEvent aListBoxEvent)
00472 //
00473 // Called by the framework whenever a list event occurs for which this container
00474 // is an observer.
00475 // ----------------------------------------------------------------------------
00476 void CMarkableListContainer::HandleListBoxEventL(CEikListBox* /*aListBox*/,
00477                                                  TListBoxEvent aListBoxEvent)
00478     {
00479     // if the Select Key has been pressed
00480     if ((aListBoxEvent == MEikListBoxObserver::EEventEnterKeyPressed) ||
00481         (aListBoxEvent == MEikListBoxObserver::EEventItemDoubleClicked))
00482         {
00483         MarkOrUnMarkL();
00484         }
00485     }
00486 
00487 // ----------------------------------------------------------------------------
00488 // CMarkableListContainer::AddItemL(const TDesC& aNewItem)
00489 //
00490 // Add an item into the listbox.
00491 // ----------------------------------------------------------------------------
00492 void CMarkableListContainer::AddItemL(const TDesC& aFirstLabel,
00493                                       const TDesC& aSecondLabel)
00494     {
00495     CTextListBoxModel* model = iSmsListBox->Model();
00496     model->SetOwnershipType (ELbmOwnsItemArray);
00497     CDesCArray* smsArray = static_cast <CDesCArray*> (model->ItemTextArray());
00498 
00499     TBuf <KAknExListAddItemBufLength> addedItem( KBeginIdx );
00500 
00501     // List item string format: "0\tFirstLabel\tSecondLabel\t0"
00502     // where 0 is index to icon array
00503     // Listbox icon is required at the beginning of a descriptor,
00504     //" \t" if there is no icon.
00505     _LIT( KBeginning, "0\t");
00506     addedItem.Append( KBeginning );
00507     addedItem.Append( aFirstLabel );
00508 
00509     // Second icon is always empty.
00510     _LIT( KEnd, "\t");
00511     addedItem.Append( KEnd );
00512     addedItem.Append( aSecondLabel );
00513     addedItem.Append( KEnd );
00514 
00515     // Insert a new item into the array
00516     smsArray->AppendL( addedItem );
00517 
00518     iSmsListBox->HandleItemAdditionL();
00519     }
00520 
00521 // End of file

Generated by  doxygen 1.6.2