|
1 /* |
|
2 * Copyright (c) 2007 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: Toolbar extension component |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <barsread.h> |
|
20 #include <akntoolbarextension.h> |
|
21 #include <avkon.mbg> |
|
22 #include <aknconsts.h> |
|
23 #include <akntoolbar.h> |
|
24 |
|
25 #include "akntoolbarextensionview.h" |
|
26 |
|
27 |
|
28 // ======== MEMBER FUNCTIONS ======== |
|
29 |
|
30 // --------------------------------------------------------------------------- |
|
31 // Two-phased constructor. |
|
32 // Constructs an extension with graphics |
|
33 // --------------------------------------------------------------------------- |
|
34 // |
|
35 CAknToolbarExtension* CAknToolbarExtension::NewL() |
|
36 { |
|
37 CAknToolbarExtension* self = new (ELeave) CAknToolbarExtension(); |
|
38 CleanupStack::PushL( self ); |
|
39 self->BaseConstructL(); |
|
40 CleanupStack::Pop( self ); |
|
41 return self; |
|
42 } |
|
43 |
|
44 // --------------------------------------------------------------------------- |
|
45 // Two-phased constructor. |
|
46 // Constructs an extension with graphics |
|
47 // --------------------------------------------------------------------------- |
|
48 // |
|
49 EXPORT_C CAknToolbarExtension* CAknToolbarExtension::NewL( const TInt aResourceId ) |
|
50 { |
|
51 CAknToolbarExtension* self = NewLC( aResourceId ); |
|
52 CleanupStack::Pop( self ); |
|
53 return self; |
|
54 } |
|
55 |
|
56 // --------------------------------------------------------------------------- |
|
57 // Two-phased constructor. |
|
58 // Constructs an extension with graphics |
|
59 // --------------------------------------------------------------------------- |
|
60 // |
|
61 EXPORT_C CAknToolbarExtension* CAknToolbarExtension::NewLC( const TInt aResourceId ) |
|
62 { |
|
63 CAknToolbarExtension* self = new (ELeave) CAknToolbarExtension(); |
|
64 CleanupStack::PushL( self ); |
|
65 self->BaseConstructL(); |
|
66 self->ConstructFromResourceL( aResourceId ); |
|
67 return self; |
|
68 } |
|
69 |
|
70 |
|
71 // --------------------------------------------------------------------------- |
|
72 // Destructor |
|
73 // --------------------------------------------------------------------------- |
|
74 // |
|
75 CAknToolbarExtension::~CAknToolbarExtension() |
|
76 { |
|
77 delete iView; |
|
78 if ( iIdle && iIdle->IsActive() ) |
|
79 { |
|
80 iIdle->Cancel(); |
|
81 } |
|
82 delete iIdle; |
|
83 } |
|
84 |
|
85 // --------------------------------------------------------------------------- |
|
86 // From class CAknButton. |
|
87 // Constructs the toolbar extension and creates the extension view |
|
88 // --------------------------------------------------------------------------- |
|
89 // |
|
90 void CAknToolbarExtension::ConstructFromResourceL( TResourceReader& aReader ) |
|
91 { |
|
92 TInt version = aReader.ReadInt8(); // version |
|
93 iFlags = aReader.ReadInt32(); // flags |
|
94 |
|
95 if ( iFlags & KAknTbExtensionButtonNoFrame ) |
|
96 { |
|
97 SetButtonFlags( KAknButtonNoFrame ); |
|
98 } |
|
99 |
|
100 if ( version > 0 ) |
|
101 { |
|
102 TPtrC txt = aReader.ReadTPtrC(); |
|
103 CAknButtonState* state = State( 0 ); |
|
104 |
|
105 if ( state ) |
|
106 { |
|
107 state->SetHelpTextL( txt ); |
|
108 } |
|
109 state = State( 1 ); |
|
110 if ( state ) |
|
111 { |
|
112 state->SetHelpTextL( txt ); |
|
113 } |
|
114 |
|
115 if ( version >= 2 ) |
|
116 { |
|
117 // bmpfile |
|
118 TPtrC bmpFile = aReader.ReadTPtrC(); |
|
119 |
|
120 // bmpid & bmpmask |
|
121 TInt bmpId = aReader.ReadInt16(); |
|
122 TInt maskId = aReader.ReadInt16(); |
|
123 |
|
124 TAknsItemID id; |
|
125 TInt major = aReader.ReadInt32(); |
|
126 TInt minor = aReader.ReadInt32(); |
|
127 if ( major != 0 && minor != 0 ) |
|
128 { |
|
129 id.Set( major, minor ); |
|
130 } |
|
131 else if ( bmpFile == KNullDesC || ( bmpId == -1 && maskId == -1 ) ) |
|
132 { |
|
133 // To be sure that there will be at least some icon |
|
134 id.Set( KAknsIIDQgnIndiTbExtension ); |
|
135 } |
|
136 |
|
137 // dimmed bitmaps |
|
138 TInt dimmedBmpId = aReader.ReadInt16(); |
|
139 TInt dimmedMaskId = aReader.ReadInt16(); |
|
140 |
|
141 major = aReader.ReadInt32(); |
|
142 minor = aReader.ReadInt32(); |
|
143 |
|
144 TAknsItemID dimmedId; |
|
145 if ( major != 0 && minor != 0 ) |
|
146 { |
|
147 dimmedId.Set( major, minor ); |
|
148 } |
|
149 |
|
150 // pressed bitmaps |
|
151 TInt pressedBmpId = aReader.ReadInt16(); |
|
152 TInt pressedMaskId = aReader.ReadInt16(); |
|
153 |
|
154 TAknsItemID pressedId; |
|
155 major = aReader.ReadInt32(); |
|
156 minor = aReader.ReadInt32(); |
|
157 |
|
158 if ( major != 0 && minor != 0 ) |
|
159 { |
|
160 pressedId.Set( major, minor ); |
|
161 } |
|
162 |
|
163 TBool useNormalIcon = ETrue; // Use normal icon with state 1, if pressed icon does not exist. |
|
164 if ( ( bmpFile != KNullDesC && ( pressedBmpId != -1 && pressedMaskId != -1 ) ) |
|
165 || ( major != 0 && minor != 0 ) ) |
|
166 { |
|
167 useNormalIcon = EFalse; |
|
168 } |
|
169 |
|
170 // hover bitmaps |
|
171 TInt hoverBmpId = aReader.ReadInt16(); |
|
172 TInt hoverMaskId = aReader.ReadInt16(); |
|
173 |
|
174 TAknsItemID hoverId; |
|
175 major = aReader.ReadInt32(); |
|
176 minor = aReader.ReadInt32(); |
|
177 |
|
178 if ( major != 0 && minor != 0 ) |
|
179 { |
|
180 hoverId.Set( major, minor ); |
|
181 } |
|
182 |
|
183 if ( state ) |
|
184 { |
|
185 state->UpdateIconL( bmpFile, useNormalIcon ? bmpId : pressedBmpId, |
|
186 useNormalIcon ? maskId : pressedMaskId, dimmedBmpId, dimmedMaskId, |
|
187 pressedBmpId, pressedMaskId, hoverBmpId, hoverMaskId, |
|
188 useNormalIcon ? id : pressedId, dimmedId, pressedId, hoverId ); |
|
189 } |
|
190 state = State( 0 ); |
|
191 if ( state ) |
|
192 { |
|
193 state->UpdateIconL( bmpFile, bmpId, maskId, dimmedBmpId, dimmedMaskId, |
|
194 pressedBmpId, pressedMaskId, hoverBmpId, hoverMaskId, id, dimmedId, |
|
195 pressedId, hoverId ); |
|
196 } |
|
197 } |
|
198 } |
|
199 |
|
200 |
|
201 // construct view from resource, items are standard TBAR_CTRL structures |
|
202 iView = CAknToolbarExtensionView::NewL( aReader, this ); |
|
203 |
|
204 aReader.ReadInt32(); // extension |
|
205 } |
|
206 |
|
207 // --------------------------------------------------------------------------- |
|
208 // From class CCoeControl. |
|
209 // Handles key events |
|
210 // --------------------------------------------------------------------------- |
|
211 // |
|
212 TKeyResponse CAknToolbarExtension::OfferKeyEventL( const TKeyEvent& aKeyEvent, |
|
213 TEventCode aType ) |
|
214 { |
|
215 if ( CAknButton::OfferKeyEventL( aKeyEvent, aType ) == EKeyWasNotConsumed ) |
|
216 { |
|
217 return iView->OfferKeyEventL( aKeyEvent, aType ); |
|
218 } |
|
219 |
|
220 return EKeyWasConsumed; |
|
221 } |
|
222 |
|
223 // --------------------------------------------------------------------------- |
|
224 // From class CCoeControl. |
|
225 // Handles resource changes |
|
226 // --------------------------------------------------------------------------- |
|
227 // |
|
228 void CAknToolbarExtension::HandleResourceChange( TInt aType ) |
|
229 { |
|
230 CAknButton::HandleResourceChange( aType ); |
|
231 iView->HandleResourceChange( aType ); |
|
232 } |
|
233 |
|
234 // --------------------------------------------------------------------------- |
|
235 // From class CCoeControl. |
|
236 // Makes extension visible |
|
237 // --------------------------------------------------------------------------- |
|
238 // |
|
239 void CAknToolbarExtension::MakeVisible( TBool aVisible ) |
|
240 { |
|
241 CAknButton::MakeVisible( aVisible ); |
|
242 // When the view is changing the extension is hidden and then shown again |
|
243 // when coming back to view. If extension button stays pressed down, the |
|
244 // extension view should also be shown again. Showing the extension view |
|
245 // via idle so that it is shown on top. Otherwise applications container |
|
246 // is drawn after extension view, and because they have the same ordinal |
|
247 // position and priority, only container would be shown. |
|
248 if ( StateIndex() == 1 && aVisible ) |
|
249 { |
|
250 ShowViewViaIdle(); |
|
251 } |
|
252 else if ( !aVisible ) |
|
253 { |
|
254 if ( iIdle && iIdle->IsActive() ) |
|
255 { |
|
256 iIdle->Cancel(); |
|
257 } |
|
258 iView->MakeVisible( aVisible ); |
|
259 } |
|
260 } |
|
261 |
|
262 |
|
263 // --------------------------------------------------------------------------- |
|
264 // From class MCoeControlObserver. |
|
265 // Handles events from itself and from extension view. |
|
266 // --------------------------------------------------------------------------- |
|
267 // |
|
268 void CAknToolbarExtension::HandleControlEventL( CCoeControl* aControl, |
|
269 TCoeEvent aEventType ) |
|
270 { |
|
271 switch ( aEventType ) |
|
272 { |
|
273 case MCoeControlObserver::EEventRequestExit: |
|
274 { |
|
275 SetShown( EFalse ); // close view |
|
276 break; |
|
277 } |
|
278 case MCoeControlObserver::EEventStateChanged: |
|
279 { |
|
280 if ( !iToolbar ) |
|
281 { |
|
282 iToolbar = static_cast<CAknToolbar*>( Parent() ); |
|
283 } |
|
284 if ( aControl == this ) |
|
285 { |
|
286 if ( StateIndex() == 0 ) |
|
287 { |
|
288 iView->MakeVisible( EFalse ); // close view |
|
289 } |
|
290 else |
|
291 { |
|
292 TBool nonFocusing = iToolbar->IsNonFocusing(); |
|
293 iView->SetFocusing( !nonFocusing ); |
|
294 if ( IsVisible() ) |
|
295 { |
|
296 iToolbar->DynInitExtensionL( this ); |
|
297 iView->MakeVisible( ETrue ); // show view |
|
298 } |
|
299 } |
|
300 } |
|
301 else |
|
302 { |
|
303 TInt commandId = iView->CommandIdByControl( aControl ); |
|
304 iToolbar->ExtensionEventL( commandId ); |
|
305 } |
|
306 break; |
|
307 } |
|
308 default: |
|
309 { |
|
310 break; |
|
311 } |
|
312 } |
|
313 } |
|
314 |
|
315 |
|
316 // --------------------------------------------------------------------------- |
|
317 // Gets extension position so that extension view knows where to draw |
|
318 // ifself |
|
319 // --------------------------------------------------------------------------- |
|
320 // |
|
321 TInt CAknToolbarExtension::ExtensionPosition( ) |
|
322 { |
|
323 if ( !iToolbar ) |
|
324 { |
|
325 iToolbar = static_cast<CAknToolbar*>(Parent()); |
|
326 } |
|
327 return iToolbar->ToolbarVisibleIndexByControl( this ); |
|
328 } |
|
329 |
|
330 |
|
331 // --------------------------------------------------------------------------- |
|
332 // Shows view via idle. This is needed when the extension is in pressed down |
|
333 // state when coming back to view. |
|
334 // --------------------------------------------------------------------------- |
|
335 // |
|
336 void CAknToolbarExtension::ShowViewViaIdle() |
|
337 { |
|
338 if ( !iIdle ) |
|
339 { |
|
340 TRAPD( err, iIdle = CIdle::NewL( CActive::EPriorityIdle ) ); |
|
341 if ( err != KErrNone) |
|
342 { |
|
343 return; |
|
344 } |
|
345 } |
|
346 if ( iIdle->IsActive() ) |
|
347 { |
|
348 iIdle->Cancel(); |
|
349 } |
|
350 iIdle->Start( TCallBack ( ShowView, this ) ); |
|
351 } |
|
352 |
|
353 // --------------------------------------------------------------------------- |
|
354 // Shows view when called by idle. |
|
355 // --------------------------------------------------------------------------- |
|
356 // |
|
357 TInt CAknToolbarExtension::ShowView( TAny* aThis ) |
|
358 { |
|
359 CAknToolbarExtension* extension = static_cast<CAknToolbarExtension*>(aThis); |
|
360 extension->iView->MakeVisible( ETrue ); |
|
361 return KErrNone; |
|
362 } |
|
363 |
|
364 // --------------------------------------------------------------------------- |
|
365 // View uses this method to inform that it has closed ( a tap outside the |
|
366 // view ) and then extension should change state, so that it does not look |
|
367 // pressed down. |
|
368 // --------------------------------------------------------------------------- |
|
369 // |
|
370 void CAknToolbarExtension::ViewClosed( ) |
|
371 { |
|
372 ChangeState( ETrue ); |
|
373 } |
|
374 |
|
375 // --------------------------------------------------------------------------- |
|
376 // Returns a pointer to the specified control. |
|
377 // --------------------------------------------------------------------------- |
|
378 // |
|
379 EXPORT_C CCoeControl* CAknToolbarExtension::ControlOrNull( TInt aCommandId ) const |
|
380 { |
|
381 return iView->ControlOrNull( aCommandId ); |
|
382 } |
|
383 |
|
384 |
|
385 // --------------------------------------------------------------------------- |
|
386 // Adds new item to extension view |
|
387 // --------------------------------------------------------------------------- |
|
388 // |
|
389 EXPORT_C void CAknToolbarExtension::AddItemL( CCoeControl* aItem, |
|
390 TInt aType, |
|
391 TInt aCommandId, |
|
392 TInt aFlags, |
|
393 TInt aIndex ) |
|
394 { |
|
395 if ( iView->IsVisible() ) |
|
396 { |
|
397 User::Leave( KErrNotSupported ); |
|
398 } |
|
399 iView->AddItemL( aItem, aType, aCommandId, aFlags, aIndex ); |
|
400 } |
|
401 |
|
402 |
|
403 // --------------------------------------------------------------------------- |
|
404 // Removes an item from extension view |
|
405 // --------------------------------------------------------------------------- |
|
406 // |
|
407 EXPORT_C void CAknToolbarExtension::RemoveItemL( TInt aCommandId ) |
|
408 { |
|
409 if ( iView->IsVisible() ) |
|
410 { |
|
411 User::Leave( KErrNotSupported ); |
|
412 } |
|
413 iView->RemoveItemL( aCommandId ); |
|
414 } |
|
415 |
|
416 |
|
417 // --------------------------------------------------------------------------- |
|
418 // Return boolean indicating if extension view is shown. |
|
419 // --------------------------------------------------------------------------- |
|
420 // |
|
421 EXPORT_C TBool CAknToolbarExtension::IsShown() const |
|
422 { |
|
423 if ( iView->IsVisible() ) |
|
424 { |
|
425 return ETrue; |
|
426 } |
|
427 return EFalse; |
|
428 } |
|
429 |
|
430 // --------------------------------------------------------------------------- |
|
431 // Sets extension view shown or hides it. |
|
432 // --------------------------------------------------------------------------- |
|
433 // |
|
434 EXPORT_C void CAknToolbarExtension::SetShown( TBool aShown ) |
|
435 { |
|
436 if ( iView->IsVisible() != aShown ) |
|
437 { |
|
438 iView->MakeVisible( aShown ); |
|
439 ChangeState( ETrue ); |
|
440 } |
|
441 } |
|
442 |
|
443 |
|
444 // --------------------------------------------------------------------------- |
|
445 // Getter for extension flags |
|
446 // --------------------------------------------------------------------------- |
|
447 // |
|
448 TInt CAknToolbarExtension::ExtensionFlags() |
|
449 { |
|
450 return iFlags; |
|
451 } |
|
452 |
|
453 // --------------------------------------------------------------------------- |
|
454 // Default constructor |
|
455 // --------------------------------------------------------------------------- |
|
456 // |
|
457 CAknToolbarExtension::CAknToolbarExtension() : CAknButton( 0 ), iFlags(0) |
|
458 { |
|
459 } |
|
460 |
|
461 // --------------------------------------------------------------------------- |
|
462 // Contructs the button |
|
463 // --------------------------------------------------------------------------- |
|
464 // |
|
465 void CAknToolbarExtension::BaseConstructL() |
|
466 { |
|
467 SetObserver( this ); |
|
468 |
|
469 CAknButton::ConstructL( KAvkonBitmapFile, |
|
470 -1, |
|
471 -1, |
|
472 -1, |
|
473 -1, |
|
474 -1, |
|
475 -1, |
|
476 -1, |
|
477 -1, |
|
478 KNullDesC, |
|
479 KNullDesC, |
|
480 0, |
|
481 KAknsIIDQgnIndiTbExtension |
|
482 ); // replace with parametrized version to load graphics etc |
|
483 |
|
484 |
|
485 AddStateL( KAvkonBitmapFile, |
|
486 -1, |
|
487 -1, |
|
488 -1, |
|
489 -1, |
|
490 -1, |
|
491 -1, |
|
492 -1, |
|
493 -1, |
|
494 KNullDesC, |
|
495 KNullDesC, |
|
496 KAknButtonStateHasLatchedFrame, |
|
497 KAknsIIDQgnIndiTbExtension, |
|
498 KAknsIIDDefault, |
|
499 KAknsIIDDefault, |
|
500 KAknsIIDDefault ); // add a second state with latched down graphics |
|
501 |
|
502 } |
|
503 |
|
504 // ----------------------------------------------------------------------------- |
|
505 // Constructs controls from a resource file. |
|
506 // ----------------------------------------------------------------------------- |
|
507 // |
|
508 void CAknToolbarExtension::ConstructFromResourceL( const TInt aResourceId ) |
|
509 { |
|
510 TResourceReader reader; |
|
511 iCoeEnv->CreateResourceReaderLC( reader, aResourceId ); |
|
512 ConstructFromResourceL( reader ); |
|
513 CleanupStack::PopAndDestroy(); |
|
514 } |
|
515 |
|
516 |
|
517 // ----------------------------------------------------------------------------- |
|
518 // Returns extension view. Needed by toolbar when dimming the background. |
|
519 // We don't want toolbar to be dimmed too. |
|
520 // ----------------------------------------------------------------------------- |
|
521 // |
|
522 CCoeControl* CAknToolbarExtension::ExtensionView() |
|
523 { |
|
524 return iView; |
|
525 } |
|
526 |
|
527 // ----------------------------------------------------------------------------- |
|
528 // Dims or undims a toolbar item in extension. |
|
529 // ----------------------------------------------------------------------------- |
|
530 // |
|
531 EXPORT_C void CAknToolbarExtension::SetItemDimmed( TInt aCommandId, TBool aDimmed ) |
|
532 { |
|
533 iView->SetItemDimmed( aCommandId, aDimmed ); |
|
534 } |
|
535 |
|
536 // ----------------------------------------------------------------------------- |
|
537 // Hides or unhides a toolbar item in extension. |
|
538 // ----------------------------------------------------------------------------- |
|
539 // |
|
540 EXPORT_C void CAknToolbarExtension::HideItemL( TInt aCommandId, TBool aHide ) |
|
541 { |
|
542 if ( iView->IsVisible() ) |
|
543 { |
|
544 User::Leave( KErrNotSupported ); |
|
545 } |
|
546 iView->HideItemL( aCommandId, aHide ); |
|
547 } |