emailuis/emailui/src/OverlayControl.cpp
changeset 2 5253a20d2a1e
child 3 a4d6f1ea0416
equal deleted inserted replaced
1:12c456ceeff2 2:5253a20d2a1e
       
     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 the License "Symbian Foundation License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.symbianfoundation.org/legal/sfl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: This file implements overlay controls
       
    15 *
       
    16 */
       
    17 
       
    18 #include "OverlayControl.h"
       
    19 
       
    20 // CONSTS
       
    21 const TReal KSolidTransparencyFactor = 2;
       
    22 
       
    23 
       
    24 COverlayControl::COverlayControl( MOverlayControlObserver* aObserver )
       
    25 : iObserver( aObserver )
       
    26 	{
       
    27 	}
       
    28 
       
    29 COverlayControl::~COverlayControl()
       
    30 	{
       
    31     delete iBitmap;
       
    32     delete iMask;
       
    33     delete iSolidMask;
       
    34 	}
       
    35 
       
    36 COverlayControl* COverlayControl::NewL( CCoeControl* aParent, MOverlayControlObserver* aObserver, 
       
    37         const TRect& aRect, TInt aBitmapId, TInt aMaskId )
       
    38 	{
       
    39     COverlayControl* self = new (ELeave) COverlayControl( aObserver );
       
    40 	CleanupStack::PushL( self );
       
    41 	self->ConstructL( aParent, aRect, aBitmapId, aMaskId );
       
    42     CleanupStack::Pop( self );
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 void COverlayControl::ConstructL( CCoeControl* aParent, const TRect& aRect, TInt aBitmapId, TInt aMaskId )
       
    47 	{
       
    48 	iSolidMask = new(ELeave) CFbsBitmap();
       
    49 
       
    50     CreateWindowL( aParent );
       
    51 	
       
    52 	TFileName iconFileName;
       
    53 	TFsEmailUiUtility::GetFullIconFileNameL( iconFileName );
       
    54 	AknIconUtils::CreateIconL( iBitmap,
       
    55 							   iMask,
       
    56 							   iconFileName,
       
    57 							   aBitmapId,
       
    58 							   aMaskId );
       
    59 
       
    60 	SetRect( aRect );
       
    61     EnableDragEvents();
       
    62 	
       
    63 	ActivateL();
       
    64 	}
       
    65 
       
    66 void COverlayControl::SetRect( const TRect& aRect )
       
    67     {
       
    68     if( iBitmap )
       
    69         {
       
    70         AknIconUtils::SetSize( iBitmap, aRect.Size() );
       
    71         }
       
    72     if( iMask )
       
    73         {
       
    74         AknIconUtils::SetSize( iMask, aRect.Size() );
       
    75         
       
    76         // Create a solid version of the mask, too
       
    77         TRAP_IGNORE(
       
    78             CopyBitmapL( *iMask, *iSolidMask );
       
    79             AdjustAlpha( *iSolidMask, KSolidTransparencyFactor );
       
    80             );
       
    81         
       
    82         UpdateMask();
       
    83         }
       
    84     CCoeControl::SetRect( aRect );
       
    85     }
       
    86 
       
    87 void COverlayControl::CopyBitmapL( const CFbsBitmap& aSource, CFbsBitmap& aDest )
       
    88     {
       
    89     TSize size = aSource.SizeInPixels();
       
    90     TInt dataSize = aSource.DataStride() * size.iHeight;
       
    91     User::LeaveIfError( aDest.Create( size, aSource.DisplayMode() ) );
       
    92     
       
    93     CFbsBitGc* bitCtx;
       
    94     CFbsBitmapDevice* bitDev = CFbsBitmapDevice::NewL( &aDest );
       
    95     CleanupStack::PushL( bitDev );
       
    96     User::LeaveIfError( bitDev->CreateContext( bitCtx ) );
       
    97     CleanupStack::PushL( bitCtx );
       
    98     bitCtx->BitBlt( TRect( size ).iTl, &aSource );
       
    99     CleanupStack::PopAndDestroy( 2 ); // bitCtx, bitDev
       
   100     }
       
   101 
       
   102 void COverlayControl::AdjustAlpha( CFbsBitmap& aBitmap, TReal aFactor )
       
   103     {
       
   104     aBitmap.LockHeap();
       
   105     TUint8* data = (TUint8*)aBitmap.DataAddress();
       
   106     TSize size = aBitmap.SizeInPixels();
       
   107     TInt dataSize = aBitmap.DataStride() * size.iHeight;
       
   108     for( TInt i = 0; i<dataSize; i++ )
       
   109         {
       
   110         // multiply each pixel by aFactor
       
   111         if( data[i] ) 
       
   112             {
       
   113             TInt value = aFactor * data[ i ];
       
   114             if( value > KMaxTUint8 ) 
       
   115                 {
       
   116                 value = KMaxTUint8;
       
   117                 }
       
   118             data[ i ] = value;
       
   119             }
       
   120         }
       
   121     aBitmap.UnlockHeap();   
       
   122     }
       
   123 
       
   124 void COverlayControl::HandlePointerEventL( const TPointerEvent& aPointerEvent )
       
   125     {
       
   126     CCoeControl::HandlePointerEventL( aPointerEvent );
       
   127 
       
   128     // Do hit test for the pointer event
       
   129     TBool hit = EFalse;
       
   130     
       
   131     TSize size = Size();
       
   132     if( aPointerEvent.iPosition.iX >= 0 && 
       
   133          aPointerEvent.iPosition.iY >= 0 &&
       
   134          aPointerEvent.iPosition.iX < size.iWidth && 
       
   135          aPointerEvent.iPosition.iY < size.iHeight )
       
   136         {
       
   137         hit = ETrue;
       
   138         }
       
   139     
       
   140     if( aPointerEvent.iType == TPointerEvent::EButton1Up )
       
   141         {
       
   142         SetPointerCapture( EFalse );
       
   143         UpdateButtonState( EFalse );
       
   144         
       
   145         // Notify the events only if released on top of the control 
       
   146         if( iObserver && hit )
       
   147             {
       
   148             // Do not let leaves disturb the system
       
   149             TRAP_IGNORE(
       
   150                 iObserver->HandleOverlayPointerEventL( this, aPointerEvent );
       
   151                 );
       
   152             }
       
   153         }
       
   154     else if( aPointerEvent.iType == TPointerEvent::EButton1Down )
       
   155         {
       
   156         UpdateButtonState( ETrue );
       
   157         SetGloballyCapturing( ETrue );
       
   158         SetPointerCapture( ETrue );
       
   159         }
       
   160     else if( aPointerEvent.iType == TPointerEvent::EDrag )
       
   161         {
       
   162         // Update the button UI according to hit result
       
   163         UpdateButtonState( hit );
       
   164         }
       
   165     }
       
   166 
       
   167 void COverlayControl::UpdateButtonState( TBool aButtonDown )
       
   168     {
       
   169     if( iDownState != aButtonDown )
       
   170         {
       
   171         iDownState = aButtonDown;
       
   172         UpdateMask();
       
   173         }
       
   174     }
       
   175 
       
   176 void COverlayControl::MakeVisible( TBool aVisible )
       
   177     {
       
   178     if( !aVisible )
       
   179         {
       
   180         UpdateButtonState( EFalse );
       
   181         SetPointerCapture( EFalse );
       
   182         }
       
   183     CCoeControl::MakeVisible( aVisible );
       
   184     }
       
   185 
       
   186 void COverlayControl::UpdateMask()
       
   187     {
       
   188     CFbsBitmap* mask = iMask;
       
   189     if( iDownState )
       
   190         {
       
   191         mask = iSolidMask;
       
   192         }
       
   193     if( mask )
       
   194         {
       
   195         Window().SetTransparencyBitmap( *mask );
       
   196         }
       
   197     }
       
   198 
       
   199 void COverlayControl::Draw( const TRect& ) const 
       
   200 	{
       
   201 	CWindowGc& gc = SystemGc();
       
   202 
       
   203 	if( iBitmap )
       
   204 	    {
       
   205         TSize size = iBitmap->SizeInPixels();
       
   206         gc.BitBlt( TPoint( 0, 0 ), iBitmap );
       
   207 	    }
       
   208 	}