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