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 <eikenv.h> 00031 #include <avkon.hrh> 00032 #include <gdi.h> 00033 #include <eiktxlbx.h> // CEikTextListBox 00034 #include <eiklabel.h> // CEikLabel 00035 #include <eiktxlbm.h> // CTextListBoxModel 00036 #include <aknconsts.h> 00037 #include <AknUtils.h> 00038 #include "DBMSListboxView.h" 00039 00040 00041 // --------------------------------------------------------------------------- 00042 // CDBMSListboxView::NewL() 00043 // 00044 // Create instance of this view. 00045 // --------------------------------------------------------------------------- 00046 // 00047 CDBMSListboxView* CDBMSListboxView::NewL(const TRect& aRect) 00048 { 00049 CDBMSListboxView* self = new (ELeave) CDBMSListboxView; 00050 CleanupStack::PushL(self); 00051 self->ConstructL(aRect); 00052 CleanupStack::Pop(self); 00053 return self; 00054 } 00055 00056 00057 // --------------------------------------------------------------------------- 00058 // CDBMSListboxView::ConstructL() 00059 // 00060 // Perform the second phase construction. Construct the contained controls. 00061 // --------------------------------------------------------------------------- 00062 // 00063 void CDBMSListboxView::ConstructL(const TRect& aRect) 00064 { 00065 // Create a window for this application view 00066 CreateWindowL(); 00067 _LIT(KListboxView, "Listbox view"); 00068 00069 iLabel = new (ELeave) CEikLabel(); 00070 iLabel->SetTextL(KListboxView); 00071 iLabel->SetContainerWindowL( *this ); 00072 00073 iListBox = new(ELeave)CEikTextListBox(); 00074 iListBox->ConstructL( this); 00075 iListBox->SetContainerWindowL( *this ); 00076 00077 // Set the windows size 00078 SetRect(aRect); 00079 ActivateL(); 00080 } 00081 00082 00083 // --------------------------------------------------------------------------- 00084 // CDBMSListboxView::SetCaptionL() 00085 // 00086 // Set caption for this view. Updates the label. 00087 // --------------------------------------------------------------------------- 00088 // 00089 void CDBMSListboxView::SetCaptionL(const TDesC& aNewCaption) 00090 { 00091 iLabel->SetTextL(aNewCaption); 00092 SizeChanged(); 00093 } 00094 00095 00096 // --------------------------------------------------------------------------- 00097 // CDBMSListboxView::SetListItemsL() 00098 // 00099 // Sets the listbox array and updates the view to reflect new array items. 00100 // The listbox takes ownership of the given aNewItems array. 00101 // --------------------------------------------------------------------------- 00102 // 00103 void CDBMSListboxView::SetListItemsL(CDesCArrayFlat* aNewItems) 00104 { 00105 CTextListBoxModel* model = iListBox->Model(); 00106 model->SetItemTextArray(aNewItems); 00107 // Set ListBox model responsible for deleting the listItems array 00108 model->SetOwnershipType( ELbmOwnsItemArray ); 00109 iListBox->HandleItemAdditionL(); 00110 if(aNewItems->Count()>0) 00111 { 00112 // Select the first item, if there is one. 00113 iListBox->SetCurrentItemIndexAndDraw(0); 00114 } 00115 } 00116 00117 00118 // --------------------------------------------------------------------------- 00119 // CDBMSListboxView::CDBMSListboxView() 00120 // 00121 // Constructor 00122 // --------------------------------------------------------------------------- 00123 // 00124 CDBMSListboxView::CDBMSListboxView() 00125 { 00126 // No implementation required 00127 } 00128 00129 00130 // --------------------------------------------------------------------------- 00131 // CDBMSListboxView::~CDBMSListboxView() 00132 // 00133 // Desctructor. Delete contained controls 00134 // --------------------------------------------------------------------------- 00135 // 00136 CDBMSListboxView::~CDBMSListboxView() 00137 { 00138 delete iLabel; 00139 delete iListBox; 00140 } 00141 00142 00143 // --------------------------------------------------------------------------- 00144 // CDBMSAppUi::Draw() 00145 // 00146 // Draw the view background. This is called by the framework, when necessary. 00147 // 00148 // Note: The framework will draw the contained controls (label and the listbox) 00149 // by using CountComponentControls() and ComponentControl() methods. 00150 // --------------------------------------------------------------------------- 00151 // 00152 void CDBMSListboxView::Draw(const TRect& /*aRect*/) const 00153 { 00154 //Clear the screen 00155 CWindowGc& gc = SystemGc(); 00156 gc.Clear(Rect()); 00157 } 00158 00159 00160 // --------------------------------------------------------------------------- 00161 // CDBMSListboxView::SizeChanged() 00162 // 00163 // Specify locations and sizes of the label and listbox. Called by 00164 // the framework when the view size is changed 00165 // --------------------------------------------------------------------------- 00166 // 00167 void CDBMSListboxView::SizeChanged() 00168 { 00169 00170 TRect containerRect = Rect(); // Rect of the container 00171 00172 TSize labelSize = iLabel->MinimumSize(); 00173 00174 // Show the label in the upper part of the screen and in whole 00175 // screen width 00176 iLabel->SetExtent( TPoint(5,2), 00177 TSize(containerRect.Width(), labelSize.iHeight) ); 00178 00179 // Fill the rest (bottom) of the screen with listbox 00180 iListBox->SetExtent( TPoint(5,labelSize.iHeight + 5 ), 00181 TSize( containerRect.Width(), 00182 containerRect.Height()-labelSize.iHeight ) ); 00183 00184 } 00185 00186 00187 // --------------------------------------------------------------------------- 00188 // CDBMSListboxView::CountComponentControls() 00189 // 00190 // Return number of contained controls. 00191 // --------------------------------------------------------------------------- 00192 // 00193 TInt CDBMSListboxView::CountComponentControls() const 00194 { 00195 return 2; // Label and listbox 00196 } 00197 00198 00199 // --------------------------------------------------------- 00200 // CDBMSListboxView::ComponentControl() 00201 // 00202 // Return owned controls to framework so it will draw them. 00203 // --------------------------------------------------------- 00204 // 00205 CCoeControl* CDBMSListboxView::ComponentControl(TInt aIndex) const 00206 { 00207 switch ( aIndex ) 00208 { 00209 case 0: 00210 return iLabel; 00211 case 1: 00212 return iListBox; 00213 default: 00214 return NULL; 00215 } 00216 } 00217 00218 // --------------------------------------------------------------------------- 00219 // CDBMSListboxView::OfferKeyEventL() 00220 // 00221 // Handle key events. Let the listbox handle the key events. 00222 // 00223 // AppUi must add this control to control stack to ensure key events are 00224 // received and this method is called. 00225 // --------------------------------------------------------------------------- 00226 // 00227 TKeyResponse CDBMSListboxView::OfferKeyEventL(const TKeyEvent& aKeyEvent, 00228 TEventCode aType) 00229 { 00230 return iListBox->OfferKeyEventL( aKeyEvent, aType ); 00231 } 00232 00233 00234 // --------------------------------------------------------------------------- 00235 // CDBMSListboxView::GetSelectedItem() 00236 // 00237 // Get name of the selected item in the listbox. 00238 // --------------------------------------------------------------------------- 00239 // 00240 TInt CDBMSListboxView::GetSelectedItem(TDes& aResult) const 00241 { 00242 CTextListBoxModel* model = iListBox->Model(); 00243 if(model->NumberOfItems()==0) 00244 { 00245 return KErrNotFound; 00246 } 00247 00248 aResult.Copy(model->ItemText(iListBox->CurrentItemIndex())); 00249 return KErrNone; 00250 } 00251 00252 //EOF