phoneuis/BubbleManager/Src/BMTouchPaneButton.cpp
branchRCL_3
changeset 62 5266b1f337bd
equal deleted inserted replaced
61:41a7f70b3818 62:5266b1f337bd
       
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  Button for touch pane.
       
    15 *
       
    16 */
       
    17 
       
    18 #include <AknBidiTextUtils.h>
       
    19 #include <AknsDrawUtils.h>
       
    20 #include <aknlayoutscalable_avkon.cdl.h>
       
    21 #include <gulicon.h>
       
    22 #include <barsread.h> 
       
    23 #include <AknsFrameBackgroundControlContext.h>
       
    24 #include <AknsConstants.h>
       
    25 #include <phoneappcommands.hrh>
       
    26 
       
    27 #include "BMTouchPaneButton.h"
       
    28 #include "BMTouchPaneInterface.h"
       
    29 #include "BMPanic.h"
       
    30 
       
    31 const TInt KDimmedTransparency = 77;
       
    32 const TInt KBubbleButtonText = 255;
       
    33 
       
    34 // ======== MEMBER FUNCTIONS ========
       
    35 
       
    36 // ---------------------------------------------------------------------------
       
    37 // C++ default constructor
       
    38 // ---------------------------------------------------------------------------
       
    39 //
       
    40 CBubbleTouchPaneButton::CBubbleTouchPaneButton(
       
    41     MBubbleTouchPaneIconProvider* aIconProvider,
       
    42     const TInt aFlags ) :
       
    43     CAknButton( aFlags ),
       
    44     iIconProvider( aIconProvider )
       
    45     {
       
    46     }
       
    47     
       
    48 // -----------------------------------------------------------------------------
       
    49 // Two-phased constructor.
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 CBubbleTouchPaneButton* CBubbleTouchPaneButton::NewL( 
       
    53         MBubbleTouchPaneIconProvider* aIconProvider, 
       
    54         const TInt aFlags )
       
    55     {
       
    56     CBubbleTouchPaneButton* self = 
       
    57     new(ELeave) CBubbleTouchPaneButton( aIconProvider, aFlags );
       
    58     CleanupStack::PushL( self );
       
    59     self->ConstructL();
       
    60     
       
    61     CleanupStack::Pop( self );
       
    62     return self;
       
    63     }
       
    64 
       
    65 // -----------------------------------------------------------------------------
       
    66 // Symbian 2nd phase constructor 
       
    67 // -----------------------------------------------------------------------------
       
    68 //
       
    69 void CBubbleTouchPaneButton::ConstructL()
       
    70     {
       
    71     CAknButton::ConstructL();
       
    72     }
       
    73 
       
    74 // ---------------------------------------------------------------------------
       
    75 // Destructor
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 CBubbleTouchPaneButton::~CBubbleTouchPaneButton()
       
    79     {
       
    80     iCommands.Close();   
       
    81     }
       
    82         
       
    83 // ---------------------------------------------------------------------------
       
    84 // CBubbleTouchPaneButton::ConstructFromResouceL
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CBubbleTouchPaneButton::ConstructFromResouceL( TResourceReader& aReader )
       
    88     {
       
    89     
       
    90     TInt commandCount = aReader.ReadInt16();
       
    91     for ( TInt i=0; i < commandCount; i++ )    
       
    92         {
       
    93         TInt command = aReader.ReadInt16();
       
    94         iCommands.AppendL( command );
       
    95         }
       
    96     
       
    97     aReader.ReadInt16(); // this is mystery?
       
    98     
       
    99     CAknButton::ConstructFromResourceL( aReader );
       
   100     
       
   101     SelectTextColor();
       
   102     
       
   103     // Customize icons
       
   104     if ( iIconProvider )
       
   105         {
       
   106         LoadCustomIcons( *iIconProvider );
       
   107         }
       
   108     
       
   109     if ( State()->Icon() )
       
   110         {
       
   111         SetIconScaleMode( EAspectRatioPreserved );
       
   112         }
       
   113     }
       
   114 
       
   115 // ---------------------------------------------------------------------------
       
   116 // CBubbleTouchPaneButton::LoadCustomIconsL
       
   117 // ---------------------------------------------------------------------------
       
   118 //    
       
   119 void CBubbleTouchPaneButton::LoadCustomIcons(
       
   120     MBubbleTouchPaneIconProvider& aIconProvider )
       
   121     {
       
   122     TInt commandCount = iCommands.Count();
       
   123     
       
   124     for ( TInt i=0; i < commandCount; i++ )    
       
   125         {
       
   126         CGulIcon* icon = NULL;
       
   127            
       
   128         TRAPD( err, icon = aIconProvider.GetIconForCommandL( 
       
   129                                iCommands[i] ) );
       
   130      
       
   131         if ( !err && icon )       
       
   132             {
       
   133             CAknButtonState* state = State( i );
       
   134             // State takes ownership of icon
       
   135             state->SetIcon( icon );                
       
   136             }
       
   137         }    
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // CBubbleTouchPaneButton::CommandId
       
   142 // ---------------------------------------------------------------------------
       
   143 //
       
   144 TInt CBubbleTouchPaneButton::CommandId( TBool aPreviousState ) const
       
   145     {
       
   146     __ASSERT_DEBUG( iCommands.Count(), Panic( EBMPanicTouchPane ) );
       
   147     
       
   148     TInt command;
       
   149     if ( iCommands.Count() > 1 )
       
   150         {
       
   151         TInt index =  StateIndex();
       
   152         if ( aPreviousState )
       
   153             {
       
   154             // return command from previous state
       
   155             index -= 1;
       
   156             if ( index < 0 )
       
   157                 {
       
   158                 index = iCommands.Count() - 1;    
       
   159                 }
       
   160             }
       
   161         
       
   162         command = iCommands[index];            
       
   163         }
       
   164     else
       
   165         {
       
   166         command = iCommands[0];    
       
   167         }        
       
   168     
       
   169     return command;    
       
   170     }    
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // CBubbleTouchPaneButton::SetState
       
   174 // ---------------------------------------------------------------------------
       
   175 //    
       
   176 void CBubbleTouchPaneButton::SetState( TInt aCommand,
       
   177                                        TBool aDrawNow )
       
   178     {
       
   179     for ( TInt i=0; i < iCommands.Count(); i++ )
       
   180         {
       
   181         if ( iCommands[i] == aCommand )
       
   182             {
       
   183             if ( i != StateIndex() )
       
   184                 {
       
   185                 SetCurrentState( i, aDrawNow );                
       
   186                 }
       
   187             }
       
   188         }
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 // CBubbleTouchPaneButton::SetDimmed
       
   193 // ---------------------------------------------------------------------------
       
   194 //
       
   195 void CBubbleTouchPaneButton::SetDimmed( TBool aDimmed )
       
   196     {
       
   197     CAknButton::SetDimmed( aDimmed );
       
   198     DrawDeferred();                
       
   199     }
       
   200 
       
   201 // ---------------------------------------------------------------------------
       
   202 // CBubbleTouchPaneButton::HasState
       
   203 // ---------------------------------------------------------------------------
       
   204 //   
       
   205 TBool CBubbleTouchPaneButton::HasState( TInt aCommand ) const
       
   206     {
       
   207     TBool hasState = EFalse;
       
   208     
       
   209     for ( TInt i=0; i < iCommands.Count(); i++ )
       
   210         {
       
   211         if ( iCommands[i] == aCommand )
       
   212             {
       
   213             hasState = ETrue;
       
   214             break;    
       
   215             }
       
   216         }
       
   217     
       
   218     return hasState;
       
   219     }
       
   220    
       
   221 
       
   222 // ---------------------------------------------------------------------------
       
   223 // Draw button, text and icon.
       
   224 // ---------------------------------------------------------------------------
       
   225 // 
       
   226 void CBubbleTouchPaneButton::Draw( const TRect& aRect ) const
       
   227     {
       
   228     TAknsItemID frameId( KAknsIIDQsnFrButtonNormal );
       
   229     TAknLayoutRect centerLayout;
       
   230     centerLayout.LayoutRect( aRect,
       
   231         AknLayoutScalable_Avkon::toolbar_button_pane_g1().LayoutLine() );
       
   232     TRect innerRect = centerLayout.Rect();
       
   233 
       
   234     CWindowGc& gc = SystemGc();
       
   235     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
       
   236 
       
   237     TInt commandCount = iCommands.Count();
       
   238     
       
   239     for ( TInt i=0; i < commandCount; i++ )    
       
   240         {
       
   241         TRAP_IGNORE( frameId = iIconProvider->GetButtonForCommandL( 
       
   242                                    iCommands[i]));
       
   243 
       
   244         if ( iButtonPressed )
       
   245             {
       
   246             frameId = SelectPressedButton( frameId );
       
   247             }
       
   248         else if ( IsDimmed() )
       
   249             {
       
   250             frameId = SelectDimmedButton( frameId );
       
   251             }
       
   252     
       
   253         iBgContext->SetFrame( frameId );
       
   254         iBgContext->SetCenter( KAknsIIDQsnFrButtonCenterNormal );
       
   255         iBgContext->SetFrameRects( aRect, innerRect );
       
   256     
       
   257         if ( !AknsDrawUtils::Background( skin, 
       
   258                                         iBgContext, 
       
   259                                         NULL, 
       
   260                                         gc, 
       
   261                                         aRect, 
       
   262                                         KAknsDrawParamNoClearUnderImage ) )
       
   263             {
       
   264             gc.SetBrushColor( KRgbRed );
       
   265             gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
       
   266             gc.DrawRect( aRect );
       
   267             }
       
   268         LayoutIconAndText( gc );
       
   269         }
       
   270     }
       
   271 
       
   272 // --------------------------------------------------------------------------
       
   273 // Draw text and icon according to the layout.
       
   274 // --------------------------------------------------------------------------
       
   275 
       
   276 void CBubbleTouchPaneButton::LayoutIconAndText( CWindowGc& aGc ) const
       
   277     {
       
   278     TRect parentRect( Rect() );
       
   279     CAknButtonState* state = State();
       
   280     if ( !state  ||       
       
   281          !state->Icon() ||
       
   282          !state->Text().Length() )
       
   283         {
       
   284         return;
       
   285         }    
       
   286 
       
   287     TRgb penColor;
       
   288     TRgb brushColor;
       
   289     GetTextColors( penColor ); 
       
   290     
       
   291     // buffer for visually ordered text
       
   292     TBuf< KBubbleButtonText + KAknBidiExtraSpacePerLine> visualText; 
       
   293     TInt clipWidth = iLayoutText.TextRect().Width();
       
   294     
       
   295     // bidi processing - using AknBidiTextUtils.
       
   296     AknBidiTextUtils::ConvertToVisualAndClip(
       
   297         state->Text(),
       
   298         visualText,
       
   299         *iLayoutText.Font(),
       
   300         clipWidth,
       
   301         clipWidth );
       
   302     
       
   303     iLayoutText.DrawText( aGc, visualText, EFalse, penColor );
       
   304     
       
   305     const CGulIcon* icon = GetCurrentIcon();
       
   306     if ( icon )
       
   307         {
       
   308         CFbsBitmap* buttonBmp = icon->Bitmap();
       
   309         CFbsBitmap* buttonMask = icon->Mask();
       
   310    
       
   311         if( buttonBmp && buttonMask )
       
   312             {
       
   313             aGc.BitBltMasked( iIconRect.iTl, buttonBmp,
       
   314                     iIconRect.Size(), buttonMask, ETrue );
       
   315             }
       
   316         }
       
   317     }
       
   318 
       
   319 // ---------------------------------------------------------------------------
       
   320 // CBubbleTouchPaneButton::SetLayout
       
   321 // ---------------------------------------------------------------------------
       
   322 //
       
   323 void CBubbleTouchPaneButton::SetLayout( const TAknLayoutText aLayoutText,
       
   324                                         const TRect aIconRect ) 
       
   325     {
       
   326     iLayoutText = aLayoutText;
       
   327     iIconRect = aIconRect;
       
   328     }    
       
   329 
       
   330 // ---------------------------------------------------------------------------
       
   331 // CBubbleTouchPaneButton::HandleResourceChange
       
   332 // ---------------------------------------------------------------------------
       
   333 //
       
   334 void CBubbleTouchPaneButton::HandleResourceChange( TInt aType )
       
   335     {
       
   336     CAknButton::HandleResourceChange( aType );
       
   337     
       
   338     if ( aType == KAknsMessageSkinChange &&
       
   339          iIconProvider )
       
   340         {
       
   341         LoadCustomIcons( *iIconProvider );
       
   342         }    
       
   343     }
       
   344 
       
   345 // -----------------------------------------------------------------------------
       
   346 // Gets the correct text color.
       
   347 // -----------------------------------------------------------------------------
       
   348 //
       
   349 void CBubbleTouchPaneButton::GetTextColors( TRgb& aPenColor ) const 
       
   350    { 
       
   351    if ( IsDimmed() ) 
       
   352        {
       
   353        // Using parameter EColorButtonTextDimmed does not return valid 
       
   354        // color for the text that is used in dimmed button. We have 
       
   355        // to use default text and set it transparent.
       
   356        aPenColor = iEikonEnv->ControlColor( EColorButtonText, *this );
       
   357        aPenColor.SetAlpha( KDimmedTransparency );
       
   358        }
       
   359    else if ( iButtonPressed )
       
   360        {
       
   361        aPenColor = iEikonEnv->ControlColor( EColorButtonTextPressed, *this );
       
   362        }
       
   363    else
       
   364        {
       
   365        aPenColor = iEikonEnv->ControlColor( EColorButtonText, *this );
       
   366        }
       
   367    }
       
   368 
       
   369 // -----------------------------------------------------------------------------
       
   370 // Selects the correct pressed button.
       
   371 // -----------------------------------------------------------------------------
       
   372 //
       
   373 TAknsItemID CBubbleTouchPaneButton::SelectPressedButton( TAknsItemID frameId ) const
       
   374    { 
       
   375    if( frameId == KAknsIIDQsnFrButtonNormalAnswer )
       
   376        {
       
   377        return KAknsIIDQsnFrButtonPressedAnswer;
       
   378        }
       
   379    else if( frameId == KAknsIIDQsnFrButtonNormalReject )
       
   380        {
       
   381        return KAknsIIDQsnFrButtonPressedReject;
       
   382        }
       
   383    else
       
   384        {
       
   385        return KAknsIIDQsnFrButtonPressed;
       
   386        }
       
   387    }
       
   388 
       
   389 // -----------------------------------------------------------------------------
       
   390 // Selects the correct dimmed button.
       
   391 // -----------------------------------------------------------------------------
       
   392 //
       
   393  TAknsItemID CBubbleTouchPaneButton::SelectDimmedButton( TAknsItemID frameId ) const
       
   394    {
       
   395    if( frameId == KAknsIIDQsnFrButtonNormalAnswer )
       
   396        {
       
   397        return KAknsIIDQsnFrButtonInactiveAnswer;
       
   398        }
       
   399    else if( frameId == KAknsIIDQsnFrButtonNormalReject )
       
   400        {
       
   401        return KAknsIIDQsnFrButtonInactiveReject;
       
   402        }
       
   403    else
       
   404        {
       
   405        return KAknsIIDQsnFrButtonInactive;
       
   406        }
       
   407    }
       
   408 
       
   409 // -----------------------------------------------------------------------------
       
   410 // Selects the correct text color.
       
   411 // -----------------------------------------------------------------------------
       
   412 //
       
   413 void CBubbleTouchPaneButton::SelectTextColor() 
       
   414     { 
       
   415     TAknsQsnTextColorsIndex textColorIndex( EAknsCIQsnTextColorsCG65 );
       
   416     TInt command = CommandId( EFalse );
       
   417 
       
   418     if ( command == EPhoneCallComingCmdAnswer )
       
   419         {
       
   420         textColorIndex = EAknsCIQsnTextColorsCG82;
       
   421         }
       
   422     else if ( command == EPhoneCallComingCmdReject )
       
   423         {
       
   424         textColorIndex = EAknsCIQsnTextColorsCG83;
       
   425         }
       
   426 
       
   427     // function button text color
       
   428     SetTextColorIds( KAknsIIDQsnTextColors, textColorIndex );
       
   429     }