examples/ForumNokia/ThreadAndActiveObjectsEx/src/devicelistcontainer.cpp

00001 /*
00002  * Copyright © 2008 Nokia Corporation.
00003  */
00004 
00005 
00006 // INCLUDE FILES
00007 
00008 // Class include
00009 #include "DeviceListContainer.h"
00010 #include "SharedIntermediator.h"
00011 #include <aknnotewrappers.h>
00012 #include "ThreadAOAppUi.h"
00013 #include "BTDiscoverer.h" // KBTDeviceAddress
00014 
00015 // System includes
00016 #include <aknlists.h>           // CAknSingleStyleListBox
00017 #include <barsread.h>           // TResource Reader
00018 #include <eikclbd.h>            // CColumnListBoxData
00019 #include <eikmenub.h>           // CEikMenuBar
00020 #include <ThreadAO.rsg>         // R_DEVICE_LIST_LISTBOX
00021 #include <stringloader.h>       // StringLoader
00022 #include <uikon.hrh>            // TKeyCode #defines
00023 
00024 
00025 const TInt KAknExListAddItemBufLength(256);
00026 #define KListBoxPosition TPoint(0,0) 
00027 
00028 _LIT( KListBoxHeader, "Bluetooth devices:");
00029 
00030 // ----------------------------------------------------------------------------
00031 // CDeviceListContainer::CDeviceListContainer()
00032 //
00033 // Default C++ constructor.
00034 // ----------------------------------------------------------------------------
00035 CDeviceListContainer::CDeviceListContainer() : iSMediator(NULL)
00036         {       
00037         }
00038 
00039 // ----------------------------------------------------------------------------
00040 // CDeviceListContainer::ConstructL(const TRect& aRect)
00041 //
00042 // Symbian OS 2nd phase constructor. Creates a Window for the 
00043 // controls, which it contains. Constructs a label and adds it to the window, 
00044 // which it then activates.
00045 // param aRect The rectangle for this window
00046 // ---------------------------------------------------------------------------- 
00047 void CDeviceListContainer::ConstructL(const TRect& aRect)
00048         {
00049          CreateWindowL();
00050         
00051         // Create the listbox
00052         iDeviceListBox = new (ELeave) CAknSingleStyleListBox;
00053         iDeviceListBox->SetContainerWindowL(*this);
00054         
00055         // Create from resource
00056         TResourceReader reader;
00057         CEikonEnv::Static()->CreateResourceReaderLC(reader, R_DEVICE_LIST_LISTBOX);
00058         iDeviceListBox->ConstructFromResourceL(reader);
00059 
00060         CleanupStack::PopAndDestroy(); // reader
00061 
00062         // Observe the list
00063         iDeviceListBox->SetListBoxObserver(this);
00064 
00065         // Set scroll bars
00066         CreateScrollBarsL();
00067         SetRect(aRect);
00068         ActivateL();
00069 
00070         // Use listbox first cell as a header
00071         AddItemL(KListBoxHeader);
00072         }
00073 
00074 
00075 // ----------------------------------------------------------------------------
00076 // CDeviceListContainer::CreateScrollBarsL()
00077 //
00078 // Creates vertical scrollbars for the list. Scrollbars are always visible.
00079 // ----------------------------------------------------------------------------
00080 void CDeviceListContainer::CreateScrollBarsL()
00081         {
00082         iDeviceListBox->CreateScrollBarFrameL();
00083         iDeviceListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
00084                 CEikScrollBarFrame::EOn, CEikScrollBarFrame::EOn);
00085         }
00086 
00087 
00088 // ----------------------------------------------------------------------------
00089 // Symbian OS 2 phase constructor.
00090 //
00091 // Constructs the CDeviceListContainer using the NewLC method, popping
00092 // the constructed object from the CleanupStack before returning it.
00093 // 
00094 // ----------------------------------------------------------------------------
00095 CDeviceListContainer* CDeviceListContainer::NewL(const TRect& aRect)
00096         {
00097         CDeviceListContainer* self = CDeviceListContainer::NewLC(aRect);
00098         CleanupStack::Pop(self);
00099         return self;
00100         }
00101 
00102 // ----------------------------------------------------------------------------
00103 // Symbian OS 2 phase constructor.
00104 //
00105 // discussion Constructs the CDeviceListContainer using the constructor and
00106 // ConstructL method, leaving the constructed object on the CleanupStack before
00107 // returning it.
00108 // 
00109 // ----------------------------------------------------------------------------
00110 CDeviceListContainer* CDeviceListContainer::NewLC(const TRect& aRect)
00111         {
00112         CDeviceListContainer* self = new (ELeave) CDeviceListContainer;
00113         CleanupStack::PushL(self);
00114         self->ConstructL(aRect);
00115         return self;
00116         }
00117 
00118 // ----------------------------------------------------------------------------
00119 // Destructor.  Frees up memory.
00120 // ----------------------------------------------------------------------------
00121 CDeviceListContainer::~CDeviceListContainer()
00122         {
00123         delete iDeviceListBox;
00124         }
00125 
00126 // ----------------------------------------------------------------------------
00127 // CDeviceListContainer::SizeChanged()
00128 //
00129 // Called by framework when the view size is changed.
00130 // ----------------------------------------------------------------------------
00131 //
00132 void CDeviceListContainer::SizeChanged()
00133         {
00134         iDeviceListBox->SetExtent( KListBoxPosition, 
00135                                      iDeviceListBox->MinimumSize() );
00136         }
00137 
00138 
00139 // ----------------------------------------------------------------------------
00140 // CDeviceListContainer::CountComponentControls() const
00141 //
00142 // Returns number of components.
00143 // ----------------------------------------------------------------------------
00144 //
00145 TInt CDeviceListContainer::CountComponentControls() const
00146         {
00147         return 1;
00148         }
00149 
00150 // ----------------------------------------------------------------------------
00151 // CDeviceListContainer::ComponentControl(TInt aIndex) const
00152 //
00153 // Returns pointer to particular component.
00154 // ----------------------------------------------------------------------------
00155 //
00156 CCoeControl* CDeviceListContainer::ComponentControl(TInt aIndex) const
00157         {
00158         switch (aIndex)
00159                 {
00160                 case 0:
00161                         return iDeviceListBox;
00162                 default:
00163                         return NULL;
00164                 }
00165         }
00166 // ----------------------------------------------------------------------------
00167 // CDeviceListContainer::Draw(const TRect& aRect) const
00168 //
00169 // Fills the window's rectangle.
00170 // ----------------------------------------------------------------------------
00171 //
00172 void CDeviceListContainer::Draw(const TRect& aRect) const
00173         {
00174         CWindowGc& gc = SystemGc();
00175         gc.Clear(aRect);
00176         }
00177 
00178 
00179 // ----------------------------------------------------------------------------
00180 // CDeviceListContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,
00181 // TEventCode aType)
00182 //
00183 // Handles the key events.
00184 // ----------------------------------------------------------------------------
00185 //
00186 TKeyResponse CDeviceListContainer::OfferKeyEventL(
00187                                   const TKeyEvent& aKeyEvent,TEventCode aType )
00188         {
00189         if (iDeviceListBox)
00190                 return iDeviceListBox->OfferKeyEventL (aKeyEvent, aType);
00191         else
00192                 return EKeyWasNotConsumed;
00193         }
00194 
00195 
00196 // ----------------------------------------------------------------------------
00197 // CDeviceListContainer::HandleListBoxEventL(CEikListBox* aListBox, 
00198 //                               TListBoxEvent aEvent)
00199 //
00200 // Handles listbox event.
00201 // ----------------------------------------------------------------------------
00202 void CDeviceListContainer::HandleListBoxEventL( CEikListBox* /*aListBox*/,
00203                                                          TListBoxEvent aEvent )
00204         {       
00205         // Select Key has been pressed
00206         if ((aEvent == MEikListBoxObserver::EEventEnterKeyPressed) ||
00207                 (aEvent == MEikListBoxObserver::EEventItemClicked))
00208                 {
00209                 ShowSelectedAddressL();
00210                 }
00211         }
00212 
00213 
00214 // ----------------------------------------------------------------------------
00215 // CDeviceListContainer::AddItemL(const TDesC& aNewItem)
00216 //
00217 // Add an item to the listbox.
00218 // ----------------------------------------------------------------------------
00219 void CDeviceListContainer::AddItemL(const TDesC& aNewItem)
00220         {
00221                 
00222         CTextListBoxModel* model = iDeviceListBox->Model();  
00223         CDesCArray* deviceArray = static_cast<CDesCArray*>(model->ItemTextArray());
00224         
00225         TBuf <KAknExListAddItemBufLength> addedItem( 0 );
00226         
00227         // Listbox icon is required at the beginning of a descriptor, " \t" if
00228         // there is no icon.
00229         _LIT( beginning, " \t");
00230         addedItem.Append( beginning );
00231         addedItem.Append( aNewItem );
00232 
00233         // Insert a new item into the array
00234         deviceArray->InsertL(deviceArray->Count(), addedItem);
00235         }
00236 
00237 // ----------------------------------------------------------------------------
00238 // CDeviceListContainer::HandleChangedL()
00239 //
00240 // Notify that the listbox should be redrawn. 
00241 // ----------------------------------------------------------------------------
00242 void CDeviceListContainer::HandleChangedL()
00243         {
00244         iDeviceListBox->HandleItemAdditionL();
00245         }
00246 
00247 // ----------------------------------------------------------------------------
00248 // CDeviceListContainer::ShowSelectedAddressL()
00249 //
00250 // Shows bluetooth address. 
00251 // ----------------------------------------------------------------------------
00252 void CDeviceListContainer::ShowSelectedAddressL()
00253         {
00254         if (iDeviceListBox)
00255                 {
00256                 CTextListBoxModel* model = iDeviceListBox->Model(); // Not taking
00257                                                                     // ownership
00258                 
00259                 if (model->NumberOfItems() > 0)
00260                         {
00261                         
00262             // Selected item if you want to show
00263                         TInt itemIndex = iDeviceListBox->CurrentItemIndex();                    
00264         
00265                         // First item in the listbox is header not tbluetoothinfo
00266                         if (itemIndex > 0) 
00267                                 {
00268                                 TBuf <KBTDeviceAddress> address;
00269                                 iSMediator->GetAddress(address, itemIndex -1);
00270         
00271                                 // Display address using an information note
00272                                 CAknInformationNote* addressNote = 
00273                                                     new (ELeave) CAknInformationNote;
00274                                 addressNote->ExecuteLD(address);
00275                                 }                               
00276                         }
00277                 }
00278         
00279         }
00280 
00281 // ----------------------------------------------------------------------------
00282 // CDeviceListContainer::SetSMediator(CSharedIntermediator* aSMediator)
00283 //
00284 // Set a shared intermediator for this class.
00285 // ----------------------------------------------------------------------------
00286 void CDeviceListContainer::SetSMediator(CSharedIntermediator* aSMediator)
00287         {
00288         iSMediator = aSMediator;
00289         }
00290 
00291 
00292 // ----------------------------------------------------------------------------
00293 // CDeviceListContainer::ClearListBox()
00294 //
00295 // Clear listbox.
00296 // ----------------------------------------------------------------------------
00297 void CDeviceListContainer::ClearListBox()
00298         {
00299         CTextListBoxModel* model = iDeviceListBox->Model();
00300         CDesCArray* deviceArray = static_cast <CDesCArray*>(model->ItemTextArray());
00301         deviceArray->Reset();
00302         }
00303 
00304 void CDeviceListContainer::HandleResourceChange(TInt aType)
00305     {
00306     CCoeControl::HandleResourceChange(aType);
00307     if ( aType==KEikDynamicLayoutVariantSwitch )
00308         {
00309         TRect rect;
00310         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00311         SetRect(rect);
00312         }
00313     }
00314 

Generated by  doxygen 1.6.2