examples/SFExamples/Graphics_Images/src/BitmapContainer.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */ 
00029 #include <eiklabel.h>
00030 #include <eikenv.h>
00031 
00032 #include "BitmapContainer.h"
00033 
00034 
00035 #include <aknutils.h> 
00036 
00037 /*
00038 -----------------------------------------------------------------------------
00039 -----------------------------------------------------------------------------
00040 */ 
00041 CBitmapContainer* CBitmapContainer::NewL( const TRect& aRect)
00042         {
00043         CBitmapContainer* self = CBitmapContainer::NewLC( aRect);
00044         CleanupStack::Pop( self );
00045         return self;
00046         }
00047 /*
00048 -----------------------------------------------------------------------------
00049 -----------------------------------------------------------------------------
00050 */ 
00051 CBitmapContainer* CBitmapContainer::NewLC( const TRect& aRect)
00052         {
00053         CBitmapContainer* self = new ( ELeave ) CBitmapContainer;
00054         CleanupStack::PushL( self );
00055         self->ConstructL( aRect);
00056         return self;
00057         }
00058 /*
00059 -----------------------------------------------------------------------------
00060 -----------------------------------------------------------------------------
00061 */ 
00062 CBitmapContainer::~CBitmapContainer()
00063         {
00064         delete iBitmap;
00065         delete iBall;
00066         delete iMask;
00067         }
00068 /*
00069 -----------------------------------------------------------------------------
00070 -----------------------------------------------------------------------------
00071 */      
00072 void CBitmapContainer::ConstructL( const TRect& aRect)
00073         {
00074         CreateWindowL();
00075         
00076         // find the image file to show
00077         TFindFile bitmapFile(CCoeEnv::Static()->FsSession());
00078         if(KErrNone == bitmapFile.FindByDir(KTxBitmapFile, KNullDesC))
00079                 {       
00080                 // construct the bitmap
00081                 // which after we have valid bitmap instance
00082                 // i.e. the pointer is valid
00083                 iBitmap = new(ELeave)CFbsBitmap();
00084                 
00085                 // then load the actual image data to the bitmap
00086                 TInt err = iBitmap->Load(bitmapFile.File(),0);
00087                 // if Err == KErrNone, then it is ok
00088                 // othervise it is most likely invalidand Handle will show it
00089                 // we could also use User::LeaveIfError()
00090                 // then instead of getting blank screen without image
00091                 // we would have a leave during costruction
00092                 // which would cause application to crash
00093                 
00094                 iBall = new(ELeave)CFbsBitmap();
00095                 iBall->Load(bitmapFile.File(),1);
00096                 
00097                 iMask = new(ELeave)CFbsBitmap();
00098                 iMask->Load(bitmapFile.File(),2);
00099                 }
00100         
00101         SetRect( aRect );
00102         ActivateL();
00103         DrawNow();
00104         }
00105 
00106 /*
00107 -----------------------------------------------------------------------------
00108 normal Draw funtion for CCoeControl, which is used to draw to the screen
00109 -----------------------------------------------------------------------------
00110 */ 
00111 void CBitmapContainer::Draw( const TRect& /*aRect*/ ) const
00112         {
00113         CWindowGc& gc = SystemGc();
00114     
00115     TBool hasImage(EFalse);
00116     
00117     if(iBitmap)// check that we have  constructed the bitmap
00118                 {
00119                 // and that the bitmap has valid data for drawing
00120                 if(iBitmap->Handle())
00121                         {
00122                         // just draw the image to the whole screen
00123                         // without caring about the scaling issues
00124                         gc.DrawBitmap(Rect(),iBitmap);
00125                         hasImage = ETrue;       
00126                         }
00127                 }
00128         
00129         // if we didn't get the image drawn, then we clear the screen
00130         if(!hasImage)
00131                 {
00132                 // clear the screen with default brush (solid white)
00133         gc.Clear(Rect());
00134                 }
00135         
00136         if(iBall && iMask)// check that we have  constructed the bitmap
00137                 {
00138                 // and that the bitmap has valid data for drawing
00139                 if(iBall->Handle() && iMask->Handle())
00140                         {
00141                         TSize imageSize(iBall->SizeInPixels());
00142                         
00143                         TInt xGap = ((Rect().Width() - imageSize.iWidth) / 2);
00144                         TInt yGap = ((Rect().Height()- imageSize.iHeight)/ 2);
00145                         
00146                         TRect destRect(xGap,yGap,(xGap + imageSize.iWidth),(yGap + imageSize.iHeight));
00147                         
00148                         gc.DrawBitmapMasked(destRect,iBall,TRect(0,0,imageSize.iWidth,imageSize.iHeight),iMask,ETrue);  
00149                         }
00150                 }
00151         }
00152 
00153 /*
00154 -----------------------------------------------------------------------------
00155 used for informinging about resource changes, 
00156 in S60 one usage is to know when the layout of the screen has been changed
00157 -----------------------------------------------------------------------------
00158 */
00159 void CBitmapContainer::HandleResourceChange(TInt aType)
00160         {
00161         TRect rect;
00162 
00163     if ( aType==KEikDynamicLayoutVariantSwitch )// layout change event
00164         {    // get new main panel rect and set it
00165         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00166         SetRect(rect);
00167         }
00168 
00169         // forward events for CCoeControl's base class handler
00170         CCoeControl::HandleResourceChange(aType);
00171         }
00172 
00173 
00174         
00175 // End of File

Generated by  doxygen 1.6.2