|
1 /* |
|
2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Content that is shown inside a stylus popup menu. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <aknbutton.h> |
|
20 #include <eikon.hrh> |
|
21 #include <aknlayoutscalable_avkon.cdl.h> |
|
22 |
|
23 #include "aistyluspopupmenucontent.h" |
|
24 |
|
25 const TInt KItemArrayGranularity = 10; |
|
26 |
|
27 // Item shown or dimmed |
|
28 const TInt KShown = 0; |
|
29 const TInt KHidden = 0x02; |
|
30 |
|
31 |
|
32 // ======== MEMBER FUNCTIONS ======== |
|
33 |
|
34 // --------------------------------------------------------------------------- |
|
35 // Constructor |
|
36 // --------------------------------------------------------------------------- |
|
37 // |
|
38 CAiStylusPopUpMenuContent::CAiStylusPopUpMenuContent() : |
|
39 iItems( KItemArrayGranularity ), |
|
40 iCommands( KItemArrayGranularity ) |
|
41 { |
|
42 |
|
43 } |
|
44 |
|
45 |
|
46 // --------------------------------------------------------------------------- |
|
47 // CAiStylusPopUpMenuContent::ConstructL |
|
48 // --------------------------------------------------------------------------- |
|
49 // |
|
50 void CAiStylusPopUpMenuContent::ConstructL() |
|
51 { |
|
52 for ( TInt i=0; i<KAiStylusMenuMaxItems; i++ ) |
|
53 { |
|
54 iVisibility[i] = KShown; |
|
55 } |
|
56 } |
|
57 |
|
58 |
|
59 // --------------------------------------------------------------------------- |
|
60 // CAiStylusPopUpMenuContent::NewL |
|
61 // --------------------------------------------------------------------------- |
|
62 // |
|
63 CAiStylusPopUpMenuContent* CAiStylusPopUpMenuContent::NewL() |
|
64 { |
|
65 CAiStylusPopUpMenuContent* self = CAiStylusPopUpMenuContent::NewLC(); |
|
66 CleanupStack::Pop( self ); |
|
67 return self; |
|
68 } |
|
69 |
|
70 |
|
71 // --------------------------------------------------------------------------- |
|
72 // CAiStylusPopUpMenuContent::NewLC |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 CAiStylusPopUpMenuContent* CAiStylusPopUpMenuContent::NewLC() |
|
76 { |
|
77 CAiStylusPopUpMenuContent* self = |
|
78 new ( ELeave ) CAiStylusPopUpMenuContent; |
|
79 CleanupStack::PushL( self ); |
|
80 self->ConstructL(); |
|
81 return self; |
|
82 } |
|
83 |
|
84 |
|
85 // --------------------------------------------------------------------------- |
|
86 // Destructor |
|
87 // --------------------------------------------------------------------------- |
|
88 // |
|
89 CAiStylusPopUpMenuContent::~CAiStylusPopUpMenuContent() |
|
90 { |
|
91 iItems.ResetAndDestroy(); |
|
92 iCommands.Reset(); |
|
93 } |
|
94 |
|
95 |
|
96 // --------------------------------------------------------------------------- |
|
97 // Adds a new menu item to the array of items. |
|
98 // --------------------------------------------------------------------------- |
|
99 // |
|
100 void CAiStylusPopUpMenuContent::AddMenuItemL( const TDesC& aItem, |
|
101 const TInt aCommandId, MCoeControlObserver& aObserver ) |
|
102 { |
|
103 if ( iItems.Count() >= KAiStylusMenuMaxItems ) |
|
104 { |
|
105 return; |
|
106 } |
|
107 TInt flags = 0; |
|
108 flags = KAknButtonTextLeft | KAknButtonSizeFitText | KAknButtonNoFrame; |
|
109 CAknButton* item = CAknButton::NewL( NULL, NULL, NULL, NULL, aItem, |
|
110 KNullDesC, flags, 0 ); |
|
111 |
|
112 item->SetTextFont( AknLayoutUtils::FontFromId( |
|
113 AknLayoutScalable_Avkon:: |
|
114 list_single_popup_submenu_pane_t1( 0 ).LayoutLine().FontId() ) ); |
|
115 item->SetBackground( Background() ); |
|
116 item->SetObserver( &aObserver ); |
|
117 iItems.Append( item ); |
|
118 iCommands.Append( aCommandId ); |
|
119 SizeChanged(); |
|
120 } |
|
121 |
|
122 |
|
123 // --------------------------------------------------------------------------- |
|
124 // Removes the menu item based on the given command id and frees the |
|
125 // memory occupied by it. |
|
126 // --------------------------------------------------------------------------- |
|
127 // |
|
128 void CAiStylusPopUpMenuContent::RemoveMenuItem( const TInt aCommandId ) |
|
129 { |
|
130 TInt itemIndex; |
|
131 for ( itemIndex = 0; itemIndex < iItems.Count(); itemIndex++ ) |
|
132 { |
|
133 if ( iCommands[itemIndex] == aCommandId ) |
|
134 { |
|
135 delete iItems[itemIndex]; |
|
136 iItems.Remove( itemIndex ); |
|
137 iCommands.Remove( itemIndex ); |
|
138 iVisibility[itemIndex] = KShown; |
|
139 break; |
|
140 } |
|
141 } |
|
142 } |
|
143 |
|
144 // --------------------------------------------------------------------------- |
|
145 // Hides / unhides the menu item based on the given command id |
|
146 // --------------------------------------------------------------------------- |
|
147 // |
|
148 void CAiStylusPopUpMenuContent::SetItemDimmed( const TInt aCommandId, const TBool aDimmed ) |
|
149 { |
|
150 TInt itemIndex; |
|
151 for ( itemIndex = 0; itemIndex < iItems.Count(); itemIndex++ ) |
|
152 { |
|
153 if ( iCommands[itemIndex] == aCommandId ) |
|
154 { |
|
155 if ( aDimmed ) |
|
156 { |
|
157 iVisibility[itemIndex] = KHidden; |
|
158 } |
|
159 else |
|
160 { |
|
161 iVisibility[itemIndex] = KShown; |
|
162 } |
|
163 SizeChanged(); |
|
164 } |
|
165 } |
|
166 } |
|
167 |
|
168 // --------------------------------------------------------------------------- |
|
169 // Returns the command id of the specified menu item. |
|
170 // --------------------------------------------------------------------------- |
|
171 // |
|
172 TInt CAiStylusPopUpMenuContent::CommandId( CCoeControl* aControl ) const |
|
173 { |
|
174 TInt retVal = 0; |
|
175 for ( TInt i = 0; i < iItems.Count(); i++ ) |
|
176 { |
|
177 if ( iItems[i] == aControl ) |
|
178 { |
|
179 retVal = iCommands[i]; |
|
180 } |
|
181 } |
|
182 return retVal; |
|
183 } |
|
184 |
|
185 |
|
186 // --------------------------------------------------------------------------- |
|
187 // From class CCoeControl |
|
188 // Calculates the total size needed to display the items. |
|
189 // --------------------------------------------------------------------------- |
|
190 // |
|
191 TSize CAiStylusPopUpMenuContent::MinimumSize() |
|
192 { |
|
193 TInt totalHeight = 0; // height of all menu items combined. |
|
194 TInt width = 0; // Width of the widest menu item. |
|
195 TSize itemSize; |
|
196 |
|
197 for ( TInt i = 0; i < iItems.Count(); i++ ) |
|
198 { |
|
199 if ( iVisibility[i] == KShown ) |
|
200 { |
|
201 itemSize = iItems[i]->MinimumSize(); |
|
202 if ( itemSize.iWidth > width ) |
|
203 { |
|
204 width = itemSize.iWidth; |
|
205 } |
|
206 totalHeight += itemSize.iHeight; |
|
207 } |
|
208 } |
|
209 return TSize( width, totalHeight ); |
|
210 } |
|
211 |
|
212 |
|
213 // --------------------------------------------------------------------------- |
|
214 // From class CCoeControl |
|
215 // CAiStylusPopUpMenuContent::CountComponentControls |
|
216 // --------------------------------------------------------------------------- |
|
217 // |
|
218 TInt CAiStylusPopUpMenuContent::CountComponentControls() const |
|
219 { |
|
220 TInt count ( 0 ); |
|
221 |
|
222 for ( TInt i = 0; i < iItems.Count(); i++ ) |
|
223 { |
|
224 if ( iVisibility[i] == KShown ) |
|
225 { |
|
226 count++; |
|
227 } |
|
228 } |
|
229 return count; |
|
230 } |
|
231 |
|
232 |
|
233 // --------------------------------------------------------------------------- |
|
234 // From class CCoeControl |
|
235 // CAiStylusPopUpMenuContent::ComponentControl |
|
236 // --------------------------------------------------------------------------- |
|
237 // |
|
238 CCoeControl* CAiStylusPopUpMenuContent::ComponentControl( |
|
239 TInt aIndex ) const |
|
240 { |
|
241 TInt count ( 0 ); |
|
242 |
|
243 for ( TInt i = 0; i< iItems.Count(); i++ ) |
|
244 { |
|
245 // Skip dimmed item(s) |
|
246 if ( iVisibility[i] == KShown ) |
|
247 { |
|
248 if ( count == aIndex ) |
|
249 { |
|
250 return iItems[i]; |
|
251 } |
|
252 count++; |
|
253 } |
|
254 } |
|
255 |
|
256 // Should never come here |
|
257 return NULL; |
|
258 } |
|
259 |
|
260 |
|
261 // --------------------------------------------------------------------------- |
|
262 // From class CCoeControl |
|
263 // CAiStylusPopUpMenuContent::HandleResourceChange |
|
264 // --------------------------------------------------------------------------- |
|
265 // |
|
266 void CAiStylusPopUpMenuContent::HandleResourceChange( TInt aType ) |
|
267 { |
|
268 CCoeControl::HandleResourceChange( aType ); |
|
269 |
|
270 if ( aType == KAknsMessageSkinChange ) |
|
271 { |
|
272 // Implementation when graphics are ready. |
|
273 } |
|
274 else if ( aType == KEikDynamicLayoutVariantSwitch ) |
|
275 { |
|
276 SizeChanged(); |
|
277 } |
|
278 } |
|
279 |
|
280 |
|
281 // --------------------------------------------------------------------------- |
|
282 // From class CCoeControl |
|
283 // CAiStylusPopUpMenuContent::SizeChanged |
|
284 // --------------------------------------------------------------------------- |
|
285 // |
|
286 void CAiStylusPopUpMenuContent::SizeChanged() |
|
287 { |
|
288 if ( iItems.Count() == 0 ) |
|
289 { |
|
290 return; |
|
291 } |
|
292 // Note: LAF data missing at the moment |
|
293 TRect rect = Rect(); |
|
294 TInt itemHeight = iItems[0]->Size().iHeight; |
|
295 TPoint topLeft = rect.iTl; |
|
296 TSize itemSize( 0, 0 ); |
|
297 |
|
298 // Position items starting from the topmost item |
|
299 for ( TInt i = 0; i < iItems.Count(); i++ ) |
|
300 { |
|
301 // Skip hidden item(s) |
|
302 if ( iVisibility[i] == KShown ) |
|
303 { |
|
304 // CAknButton::MinimumSize returns the size needed by the item text. |
|
305 itemSize = iItems[i]->MinimumSize(); |
|
306 iItems[i]->SetRect( TRect( topLeft, itemSize ) ); |
|
307 topLeft.iY += itemHeight; // Move next item down. |
|
308 } |
|
309 } |
|
310 } |