examples/ForumNokia/ThreadAndActiveObjectsEx/src/devicelistcontainer.cpp

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

Generated by  doxygen 1.6.2