examples/ForumNokia/ContactsModel/src/ContactsModelContainer.cpp

00001 /*
00002  * Copyright (c) 2008-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 // INCLUDE FILES
00031 #include "ContactsModelDocument.h"
00032 #include "ContactsModelContainer.h"
00033 
00034 const TInt KGranularityOfArray = 10;
00035 
00036 const TInt KBufferSize = 256;
00037 // ================= MEMBER FUNCTIONS =======================
00038 
00039 // ---------------------------------------------------------
00040 // CContactsModelContainer::ConstructL(const TRect& aRect)
00041 // EPOC two phased constructor
00042 // ---------------------------------------------------------
00043 //
00044 void CContactsModelContainer::ConstructL(const TRect& aRect, CContactsModelDocument* aDocument)
00045     {
00046         iDocument = aDocument;
00047     CreateWindowL();
00048     SetRect(aRect);
00049 
00050         // create listbox control
00051     iListBox = new (ELeave) CAknSingleStyleListBox;
00052     iListBox->ConstructL(this, 0);
00053         iListBox->SetContainerWindowL(*this);
00054     iListBox->SetRect(aRect.Size());
00055 
00056         iListBoxRows = new (ELeave) CDesCArrayFlat( KGranularityOfArray );
00057         iListBox->Model()->SetItemTextArray( iListBoxRows );
00058         iListBox->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);
00059 
00060     iListBox->ActivateL();
00061     iListBox->CreateScrollBarFrameL(ETrue);
00062     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn,
00063         CEikScrollBarFrame::EAuto);
00064 
00065     ActivateL();
00066         MakeVisible(ETrue);
00067     }
00068 
00069 // Destructor
00070 CContactsModelContainer::~CContactsModelContainer()
00071     {
00072         delete iListBox;
00073         iListBox = NULL;
00074 
00075         if( iListBoxRows )
00076                 {
00077                 iListBoxRows->Reset();
00078                 }
00079         delete iListBoxRows;
00080         iListBoxRows= NULL;
00081     }
00082 
00083 // ---------------------------------------------------------
00084 // CContactsModelContainer::SizeChanged()
00085 // Called by framework when the view size is changed
00086 // ---------------------------------------------------------
00087 //
00088 void CContactsModelContainer::SizeChanged()
00089     {
00090     }
00091 
00092 // ---------------------------------------------------------
00093 // CContactsModelContainer::CountComponentControls() const
00094 // ---------------------------------------------------------
00095 //
00096 TInt CContactsModelContainer::CountComponentControls() const
00097     {
00098     return 1; // return nbr of controls inside this container
00099     }
00100 
00101 // ---------------------------------------------------------
00102 // CContactsModelContainer::ComponentControl(TInt aIndex) const
00103 // ---------------------------------------------------------
00104 //
00105 CCoeControl* CContactsModelContainer::ComponentControl(TInt aIndex) const
00106     {
00107         if( aIndex > 0 )
00108                 {
00109                 return NULL;
00110                 }
00111     return iListBox;
00112     }
00113 
00114 // ---------------------------------------------------------
00115 // CContactsModelContainer::Draw(const TRect& aRect) const
00116 // ---------------------------------------------------------
00117 //
00118 void CContactsModelContainer::Draw(const TRect& /*aRect*/) const
00119     {
00120     CWindowGc& gc = SystemGc();
00121     TRect rect = Rect();
00122     gc.Clear(rect);
00123     }
00124 
00125 // ---------------------------------------------------------
00126 // CContactsModelContainer::HandleControlEventL(
00127 //     CCoeControl* aControl,TCoeEvent aEventType)
00128 // ---------------------------------------------------------
00129 //
00130 void CContactsModelContainer::HandleControlEventL(
00131     CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/)
00132     {
00133     // TODO: Add your control event handler code here
00134     }
00135 
00136 void CContactsModelContainer::UpdateL()
00137     {
00138         iListBoxRows->Reset();
00139         if( iDocument->ItemCount() > 0 )
00140                 {
00141                 //add items to the list box
00142                 for( TInt i = 0; i < iDocument->ItemCount(); ++i )
00143                         {
00144 
00145                         HBufC* row = iDocument->GetItemL(i);
00146                         CleanupStack::PushL(row);
00147 
00148                         TBuf<KBufferSize> buf;
00149                         buf.Format(_L("\t%S"), row);
00150                         iListBoxRows->AppendL(buf);
00151 
00152                         CleanupStack::PopAndDestroy();
00153                         }
00154                 //update the control
00155                 iListBox->HandleItemAdditionL();
00156                 }
00157         }
00158 
00159 // return contacts items number
00160 TInt CContactsModelContainer::ItemCount()
00161 {
00162         return iDocument->ItemCount();
00163 }
00164 
00165 TKeyResponse CContactsModelContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
00166     {
00167     return iListBox->OfferKeyEventL(aKeyEvent, aType);
00168     }
00169 
00170 TInt CContactsModelContainer::GetSelectedItem()
00171         {
00172         return iListBox->CurrentItemIndex();
00173         }
00174 
00175 // End of File

Generated by  doxygen 1.6.2