idlehomescreen/widgetmanager/src/wmportalbutton.cpp
changeset 1 5315654608de
child 2 08c6ee43b396
equal deleted inserted replaced
0:f72a12da539e 1:5315654608de
       
     1 /*
       
     2 * Copyright (c) 2009 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:
       
    15 * Implementation of the portal button for WidgetManager
       
    16 *
       
    17 */
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <aknlayoutscalable_apps.cdl.h>
       
    21 #include <aknlayoutscalable_avkon.cdl.h>
       
    22 #include <layoutmetadata.cdl.h>
       
    23 #include <aknbutton.h>
       
    24 #include <AknsDrawUtils.h>
       
    25 #include <AknBidiTextUtils.h>
       
    26 #include <AknsFrameBackgroundControlContext.h>
       
    27 #include <gulicon.h>
       
    28 #include <avkon.mbg>
       
    29 #include <widgetmanager.mbg>
       
    30 
       
    31 #include "wmportalbutton.h"
       
    32 #include "wmcommon.h"
       
    33 #include "wmplugin.h"
       
    34 #include "wmresourceloader.h"
       
    35 #include "wmmaincontainer.h"
       
    36 
       
    37 // MEMBER FUNCTIONS
       
    38 
       
    39 // ---------------------------------------------------------
       
    40 // CWmPortalButton::NewL
       
    41 // ---------------------------------------------------------
       
    42 //
       
    43 CWmPortalButton* CWmPortalButton::NewL( 
       
    44         const CCoeControl* aParent,
       
    45         const TDesC& aText,
       
    46         const TDesC& aUrl,
       
    47         TWmUiControlIds aButtonCtrlId )
       
    48 	{
       
    49  	CWmPortalButton* self = new (ELeave) CWmPortalButton( 
       
    50                 KAknButtonTextInsideFrame, aButtonCtrlId );
       
    51     CleanupStack::PushL( self );
       
    52     self->ConstructL( aParent,aText, aUrl );
       
    53     CleanupStack::Pop( self );
       
    54     return self;
       
    55 	}
       
    56 
       
    57 // ---------------------------------------------------------
       
    58 // CWmPortalButton::~CWmPortalButton
       
    59 // ---------------------------------------------------------
       
    60 //
       
    61 CWmPortalButton::~CWmPortalButton()
       
    62     {
       
    63     delete iText;
       
    64     delete iUrl;
       
    65     delete iImageConverter;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------
       
    69 // CWmPortalButton::CWmPortalButton
       
    70 // ---------------------------------------------------------
       
    71 //
       
    72 CWmPortalButton::CWmPortalButton( 
       
    73                         const TInt aFlags,
       
    74                         TWmUiControlIds aButtonCtrlId )
       
    75     :CAknButton( aFlags ),
       
    76     iButtonCtrlId( aButtonCtrlId )
       
    77     {
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------
       
    81 // CWmPortalButton::ConstructL
       
    82 // ---------------------------------------------------------
       
    83 //
       
    84 void CWmPortalButton::ConstructL( 
       
    85         const CCoeControl* aParent,
       
    86         const TDesC& aText, const TDesC& aUrl )
       
    87     {
       
    88     if (  !aParent )
       
    89         {
       
    90         User::Leave( KErrArgument );
       
    91         }
       
    92     else if ( iButtonCtrlId != EOviPortal )
       
    93         {
       
    94         // operator button not supported until layout available.
       
    95         User::Leave( KErrNotSupported );
       
    96         }
       
    97 
       
    98     SetContainerWindowL( *aParent );
       
    99     
       
   100     // Obtain pointer to main container.
       
   101     iWmMainContainer = 
       
   102             static_cast <CWmMainContainer*>( 
       
   103                 const_cast <CCoeControl*>( aParent ) );
       
   104     
       
   105     CGulIcon* icon = AknsUtils::CreateGulIconL(
       
   106             AknsUtils::SkinInstance(), KAknsIIDQgnMenuOviStore,
       
   107             iWmMainContainer->WmPlugin().ResourceLoader().IconFilePath(),
       
   108             EMbmWidgetmanagerQgn_menu_ovistore,
       
   109             EMbmWidgetmanagerQgn_menu_ovistore_mask );
       
   110     
       
   111     CleanupStack::PushL( icon );
       
   112     CAknButton::ConstructL( icon, NULL, NULL, NULL,
       
   113                             aText, KNullDesC, 0 );
       
   114     CleanupStack::Pop( icon ); // ownership taken
       
   115     
       
   116     TAknsItemID frameId = ((iButtonCtrlId == EOviPortal) ? 
       
   117         KAknsIIDQgnHomeWmButton : KAknsIIDQsnFrButtonNormal);
       
   118     TAknsItemID frameCenterId = ((iButtonCtrlId == EOviPortal) ? 
       
   119         KAknsIIDQgnHomeWmButtonCenter : KAknsIIDQsnFrButtonCenterNormal );
       
   120     TAknsItemID framePressedId = ((iButtonCtrlId == EOviPortal) ? 
       
   121         KAknsIIDQgnHomeWmButtonPressed : KAknsIIDQsnFrButtonPressed );
       
   122     TAknsItemID framePressedCenterId = ((iButtonCtrlId == EOviPortal) ? 
       
   123         KAknsIIDQgnHomeWmButtonPressedCenter : KAknsIIDQsnFrButtonCenterPressed );
       
   124 
       
   125     iText = aText.AllocL();
       
   126     iUrl = aUrl.AllocL();
       
   127 
       
   128     SetFocusing( ETrue );
       
   129     SetBackgroundIds( frameId,
       
   130                       framePressedId,
       
   131                       KAknsIIDQsnFrButtonInactive,
       
   132                       framePressedId,
       
   133                       KAknsIIDQsnFrButtonInactive );
       
   134     SetFrameAndCenterIds( 
       
   135                         frameId,
       
   136 						frameCenterId,
       
   137 						KAknsIIDDefault,
       
   138 						KAknsIIDDefault,
       
   139 						KAknsIIDDefault,
       
   140 						KAknsIIDDefault,
       
   141 						framePressedId,
       
   142 						framePressedCenterId,
       
   143 						KAknsIIDDefault,
       
   144 						KAknsIIDDefault );
       
   145     
       
   146     // ready to be drawn
       
   147     ActivateL();
       
   148     }
       
   149 
       
   150 // ---------------------------------------------------------
       
   151 // CWmPortalButton::HandlePointerEventL
       
   152 // ---------------------------------------------------------
       
   153 //
       
   154 void CWmPortalButton::HandlePointerEventL( 
       
   155         const TPointerEvent& aPointerEvent )
       
   156 	{
       
   157 	CAknButton::HandlePointerEventL( aPointerEvent);
       
   158 	
       
   159 	if ( AknLayoutUtils::PenEnabled() )
       
   160 		{
       
   161         switch ( aPointerEvent.iType )
       
   162             {
       
   163             case TPointerEvent::EButton1Down:
       
   164                 {
       
   165                 break;	
       
   166                 }
       
   167             case TPointerEvent::EButton1Up:
       
   168                 {
       
   169                 if ( iWmMainContainer && 
       
   170                     iButtonCtrlId == EOviPortal )
       
   171                     {
       
   172                     iWmMainContainer->OpenOviPortalL();
       
   173                     }
       
   174                 break;
       
   175                 }
       
   176             default:
       
   177                 break;
       
   178             }
       
   179        }	
       
   180 	}
       
   181 	
       
   182 // ---------------------------------------------------------
       
   183 // CWmPortalButton::OfferKeyEventL
       
   184 // ---------------------------------------------------------
       
   185 //
       
   186 TKeyResponse CWmPortalButton::OfferKeyEventL( 
       
   187 		const TKeyEvent& aKeyEvent, 
       
   188 		TEventCode aType )
       
   189     {
       
   190     TKeyResponse keyResponse( EKeyWasNotConsumed );    
       
   191     keyResponse = CAknButton::OfferKeyEventL( aKeyEvent, aType );
       
   192     
       
   193     if ( ( aType == EEventKey ) &&
       
   194        ( aKeyEvent.iScanCode == EStdKeyDevice3 ||
       
   195         aKeyEvent.iScanCode == EStdKeyEnter ) )
       
   196         {
       
   197         if ( iWmMainContainer && 
       
   198             iButtonCtrlId == EOviPortal )
       
   199             {
       
   200             iWmMainContainer->OpenOviPortalL();
       
   201             }
       
   202 
       
   203         keyResponse = EKeyWasConsumed;
       
   204         }
       
   205 
       
   206     return keyResponse;
       
   207     }
       
   208 
       
   209 // ---------------------------------------------------------
       
   210 // CWmPortalButton::SizeChanged
       
   211 // ---------------------------------------------------------
       
   212 //
       
   213 void CWmPortalButton::SizeChanged()
       
   214     {    
       
   215     CAknButton::SizeChanged();
       
   216 
       
   217     TBool landscape = Layout_Meta_Data::IsLandscapeOrientation();
       
   218     TRect rect = Rect();
       
   219     TAknLayoutRect imageLayout;
       
   220     if ( iButtonCtrlId == EOviPortal )
       
   221         {
       
   222         imageLayout.LayoutRect( rect,
       
   223                 AknLayoutScalable_Apps::wgtman_btn_pane_g1(
       
   224                                     landscape ? 1 : 0).LayoutLine() );
       
   225         }
       
   226     SetTextVerticalAlignment( CAknButton::ECenter );
       
   227     SetIconSize( imageLayout.Rect().Size() );
       
   228     SetTextAndIconAlignment( 
       
   229             landscape ? CAknButton::EIconOverText : CAknButton::EIconBeforeText );
       
   230     }
       
   231 
       
   232 // ---------------------------------------------------------
       
   233 // CWmPortalButton::NotifyCompletion
       
   234 // ---------------------------------------------------------
       
   235 //
       
   236 void CWmPortalButton::NotifyCompletion( TInt aError )
       
   237     {
       
   238     if ( KErrNone != aError )
       
   239         {
       
   240         // no image available. Do nothing.
       
   241         }
       
   242     else
       
   243         {        
       
   244         CGulIcon* icon = NULL;
       
   245         TRAPD( err, icon = CGulIcon::NewL( 
       
   246                 iImageConverter->Bitmap(), iImageConverter->Mask() ) );
       
   247         if ( KErrNone == err && icon )
       
   248             {
       
   249             // Ownership transfered
       
   250             State()->SetIcon( icon );
       
   251             DrawDeferred();            
       
   252             }
       
   253         }
       
   254     }
       
   255 
       
   256 // ---------------------------------------------------------
       
   257 // CWmPortalButton::Draw
       
   258 // ---------------------------------------------------------
       
   259 //
       
   260 void CWmPortalButton::Draw( const TRect& /*aRect*/ ) const
       
   261     {
       
   262     TRect rect = Rect();
       
   263 
       
   264     TAknLayoutRect centerLayout;
       
   265     centerLayout.LayoutRect( rect,
       
   266         AknLayoutScalable_Avkon::toolbar_button_pane_g1().LayoutLine() );
       
   267     TRect innerRect = centerLayout.Rect();
       
   268 
       
   269     CWindowGc& gc = SystemGc();
       
   270     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   271 
       
   272  	TAknsItemID frameId = ( ( iButtonCtrlId == EOviPortal) ? 
       
   273  	    KAknsIIDQgnHomeWmButton : KAknsIIDQsnFrButtonNormal );
       
   274     TAknsItemID frameCenterId = ( ( iButtonCtrlId == EOviPortal) ? 
       
   275         KAknsIIDQgnHomeWmButtonCenter : KAknsIIDQsnFrButtonCenterNormal );
       
   276  	
       
   277     if ( iButtonPressed )
       
   278     	{
       
   279         frameId = ( ( iButtonCtrlId == EOviPortal) ? 
       
   280             KAknsIIDQgnHomeWmButtonPressed : KAknsIIDQsnFrButtonPressed );
       
   281         frameCenterId = ( ( iButtonCtrlId == EOviPortal) ? 
       
   282             KAknsIIDQgnHomeWmButtonPressedCenter : KAknsIIDQsnFrButtonCenterPressed );
       
   283     	}
       
   284     else if ( IsDimmed() )
       
   285         {
       
   286     	frameId = KAknsIIDQsnFrButtonInactive;
       
   287     	frameCenterId = KAknsIIDQsnFrButtonCenterInactive;
       
   288         }
       
   289     else if ( IsFocused() )
       
   290         {
       
   291         frameId = KAknsIIDQsnFrList;
       
   292         frameCenterId = KAknsIIDQsnFrListCenter;
       
   293         }
       
   294     
       
   295     iBgContext->SetFrame( frameId );
       
   296     iBgContext->SetCenter( frameCenterId );
       
   297     iBgContext->SetFrameRects( rect, innerRect );
       
   298 
       
   299     if ( !AknsDrawUtils::Background( skin, iBgContext, NULL, 
       
   300                     gc, rect, KAknsDrawParamNoClearUnderImage ) )
       
   301         {
       
   302         gc.SetBrushColor( KRgbRed );
       
   303         gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   304         gc.DrawRect( rect );
       
   305         }
       
   306     gc.SetBrushStyle( CGraphicsContext::ENullBrush );
       
   307 	
       
   308     CAknButtonState* state = State();
       
   309     if ( state )
       
   310         {
       
   311         TBool landscape = Layout_Meta_Data::IsLandscapeOrientation();
       
   312         
       
   313         const CGulIcon* icon = state->Icon();
       
   314         CFbsBitmap* bitmap = const_cast<CFbsBitmap*>(icon->Bitmap());
       
   315         CFbsBitmap* mask = const_cast<CFbsBitmap*>(icon->Mask());
       
   316         // draw image
       
   317         if ( state->Icon() && bitmap && mask )
       
   318             {
       
   319             TAknLayoutRect imageLayout;
       
   320             if ( iButtonCtrlId == EOviPortal )
       
   321                 {
       
   322                 imageLayout.LayoutRect( rect,
       
   323                         AknLayoutScalable_Apps::wgtman_btn_pane_g1(
       
   324                                 landscape ? 1 : 0).LayoutLine() );
       
   325                 }
       
   326             imageLayout.DrawImage( gc, bitmap, mask );
       
   327             }
       
   328         
       
   329         // draw text if portrait        
       
   330         if ( !landscape )
       
   331             {
       
   332             TAknTextComponentLayout leftLayout;
       
   333             if ( iButtonCtrlId == EOviPortal )
       
   334                 {
       
   335                 leftLayout = AknLayoutScalable_Apps::wgtman_btn_pane_t1( 
       
   336                                                     landscape ? 1 : 0  );
       
   337                 }
       
   338             DrawText( gc, state->Text(), leftLayout, 1 );
       
   339             }
       
   340         }
       
   341     }
       
   342 
       
   343 // ---------------------------------------------------------
       
   344 // CWmPortalButton::DrawText
       
   345 // ---------------------------------------------------------
       
   346 //
       
   347 void CWmPortalButton::DrawText( 
       
   348                         CWindowGc& aGc,
       
   349                         const TDesC& aText,
       
   350                         TAknTextComponentLayout& aLayout,
       
   351                         TInt aMargin ) const
       
   352     {
       
   353     TAknLayoutText layoutText;
       
   354     layoutText.LayoutText(Rect(), aLayout.LayoutLine() );
       
   355     
       
   356     const CFont* font = layoutText.Font();
       
   357     TRect textRect = layoutText.TextRect();
       
   358     
       
   359     aGc.UseFont( font );
       
   360     
       
   361     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   362     
       
   363     TRgb textColor( layoutText.Color() );
       
   364 
       
   365     if ( IsDimmed() )
       
   366         {
       
   367         const TInt KAlpha = 77;
       
   368         textColor = iEikonEnv->ControlColor( EColorButtonText, *this );        
       
   369         // try over-writing color from theme, ignore error.
       
   370         AknsUtils::GetCachedColor( 
       
   371                 skin, textColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG6 );
       
   372         textColor.SetAlpha( KAlpha );
       
   373         }
       
   374     else if ( IsFocused() || iButtonPressed )
       
   375         {
       
   376         textColor = iEikonEnv->ControlColor( EColorButtonTextPressed, *this );
       
   377         // try over-writing color from theme, ignore error.
       
   378         AknsUtils::GetCachedColor( 
       
   379                 skin, textColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG10 );
       
   380         }
       
   381     else if ( textColor.Internal() <= 0 )
       
   382         {
       
   383         textColor = iEikonEnv->ControlColor( EColorButtonText, *this );
       
   384         // try over-writing color from theme, ignore error.
       
   385         AknsUtils::GetCachedColor( 
       
   386                 skin, textColor, KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG6 );        
       
   387         }
       
   388     aGc.SetPenColor( textColor);
       
   389     
       
   390     // Layout data not correct, add some space for text
       
   391     textRect.iBr.iX += aMargin;
       
   392     textRect.iTl.iX -= aMargin;
       
   393     
       
   394     // buffer for visually ordered text
       
   395     TBuf<255 + KAknBidiExtraSpacePerLine> visualText; 
       
   396     TInt clipWidth = textRect.Width();
       
   397 
       
   398     // bidi processing - using AknBidiTextUtils.
       
   399     AknBidiTextUtils::ConvertToVisualAndClip(
       
   400         aText,
       
   401         visualText,
       
   402         *font,
       
   403         clipWidth,
       
   404         clipWidth );
       
   405 
       
   406     TInt baselineOffset = 0;
       
   407     switch ( iVerticalAlignment )
       
   408         {
       
   409         case ETop:
       
   410             baselineOffset = font->AscentInPixels();
       
   411             break;
       
   412 
       
   413         case EBottom:
       
   414             baselineOffset = textRect.Height();
       
   415             break;
       
   416 
       
   417         default:  // centered
       
   418             baselineOffset = font->AscentInPixels() +
       
   419                            ( textRect.Height() - font->AscentInPixels() ) / 2;
       
   420         }
       
   421         
       
   422     aGc.DrawText( visualText, textRect, 
       
   423             baselineOffset, layoutText.Align() );    
       
   424     }
       
   425 
       
   426 // End of file