examples/Graphics/Bitmaps/Bitmap.cpp

00001 /*
00002 Copyright (c) 2000-2010 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 
00030 
00031 #include "BitmapsGraphicsControl.h"
00032 
00033 #include <grbmap.mbg>
00034 
00035 _LIT(KTxtZDrive,"Z:");
00036 
00037 void CBitmapControl::LoadBitmapL(CFbsBitmap* aBitMap,const TDesC& aPathAndFile,TInt aId,TBool aShareIfLoaded)
00038         {
00039         TParse mbfn;
00040         
00041         TDriveUnit sysDrive (RFs::GetSystemDrive());
00042         TDriveName sysDriveName (sysDrive.Name());      
00043         TFileName fileName(sysDriveName);
00044         fileName.Append(aPathAndFile);
00045 
00046         mbfn.Set(aPathAndFile,&fileName,NULL);
00047         if (!aBitMap->Load(mbfn.FullName(),aId,aShareIfLoaded))
00048                 return;
00049 
00050         mbfn.Set(aPathAndFile,&KTxtZDrive,NULL);
00051         User::LeaveIfError(aBitMap->Load(mbfn.FullName(),aId,aShareIfLoaded));
00052         return;
00053         }
00054 
00055         
00056         // Text printed to the console
00057 _LIT(KTxtCase0,"draw bitmap, centered on screen using block transfer");
00058 _LIT(KTxtCase1,"draw piece of bitmap using block transfer");
00059 _LIT(KTxtCase2,"draw bitmap described in twips using DrawBitmap()");
00060 _LIT(KTxtCase3,"draw stretched bitmap");
00061 _LIT(KTxtCase4,"tile rectangle, using bitmap as the brush pattern");
00062 _LIT(KTxtCase5,"tile rectangle, tiling around center of screen");
00063 _LIT(KTxtCase6,"masks: the problem of drawing a bitmap on different backgrounds");
00064 _LIT(KTxtCase7,"masks: using a mask to give a bitmap a transparent background");
00065 
00066         // The name of the multi-bitmap file containing the bitmap
00067         // and bitmap mask files.
00068 _LIT(KTxtMBMname,"\\resource\\apps\\grbmap.mbm");
00069 
00070 void CBitmapControl::UpdateModelL()
00071         {
00072                 // set up name for bitmap sharing
00073         TBool shareIfLoaded(ETrue);
00074         
00075                 switch (Phase())
00076                 {
00077                 case 0:
00078                         // load the bitmap and mask bitmap
00079                         iBitmap = new (ELeave) CFbsBitmap();
00080                         LoadBitmapL(iBitmap,KTxtMBMname,EMbmGrbmapSmiley,shareIfLoaded);
00081                         iMaskBitmap = new (ELeave) CFbsBitmap();
00082                         LoadBitmapL(iMaskBitmap,KTxtMBMname,EMbmGrbmapSmilmask,shareIfLoaded);
00083                         iGraphObserver->NotifyStatus(KTxtCase0);
00084                         break;
00085                 case 1:
00086                         iGraphObserver->NotifyStatus(KTxtCase1);
00087                         break;
00088                 case 2:
00089                         iGraphObserver->NotifyStatus(KTxtCase2);
00090                         break;
00091                 case 3:
00092                         iGraphObserver->NotifyStatus(KTxtCase3);
00093                         break;
00094                 case 4:
00095                         iGraphObserver->NotifyStatus(KTxtCase4);
00096                         break;
00097                 case 5:
00098                         iGraphObserver->NotifyStatus(KTxtCase5);
00099                         break;
00100                 case 6:
00101                         iGraphObserver->NotifyStatus(KTxtCase6);
00102                         break;
00103                 case 7:
00104                         iGraphObserver->NotifyStatus(KTxtCase7);
00105                         break;
00106                 }
00107         }
00108 
00109 void CBitmapControl::Draw(const TRect& /* aRect */) const
00110         {
00111         // draw surrounding rectangle
00112         CWindowGc& gc=SystemGc(); // graphics context we draw to
00113         gc.UseFont(iMessageFont); // use the system message font
00114         gc.Clear(); // clear the area to be drawn to
00115         SystemGc().DrawRect(Rect()); // surrounding rectangle to draw into
00116         TRect rect=Rect(); // a centered rectangle of the default size
00117         TRect bmpPieceRect=Rect(); // a rectangle to define a piece of bitmap
00118         TInt xDelta=0; // for x coordinates
00119         TInt yDelta=0; // for y coordinates
00120         TPoint screenCenterPoint=rect.Center(); // the center of the screen
00121         
00122         // decide what to do, and do it
00123         switch (Phase())
00124                 {
00125                 case 0:
00126                         // draw a whole bitmap centered on the screen,
00127                         // using bitmap block transfer
00128                         {
00129                         // calculate position for top left of bitmap so it is centered
00130                         TSize bmpSizeInPixels=iBitmap->SizeInPixels();
00131                         xDelta=(rect.Width()-bmpSizeInPixels.iWidth)/2;
00132                         yDelta=(rect.Height()-bmpSizeInPixels.iHeight)/2;
00133                         TPoint pos=TPoint(xDelta,yDelta); // displacement vector
00134                         pos+=rect.iTl; // bitmap top left corner position
00135                         gc.BitBlt(pos, iBitmap); // CWindowGc member function
00136                         }
00137                         break;
00138                 case 1:
00139                         // draw a rectangular piece of a bitmap, centered on the screen,
00140                         // using bitmap block transfer
00141                         {
00142                         // calculate bitmap piece, half size from center of source bitmap
00143                         TSize bmpSizeInPixels=iBitmap->SizeInPixels();
00144                         TSize bmpPieceSize(bmpSizeInPixels.iWidth*2/3,bmpSizeInPixels.iHeight*2/3);
00145                         TPoint bmpPieceTopLeft(0,0); 
00146                         bmpPieceRect.SetRect(bmpPieceTopLeft,bmpPieceSize); 
00147                         // calculate position for top left of bitmap piece so it is centered
00148                         xDelta=(rect.Width()-bmpPieceRect.Width())/2;
00149                         yDelta=(rect.Height()-bmpPieceRect.Height())/2;
00150                         TPoint pos=TPoint(xDelta,yDelta); // displacement vector
00151                         pos+=rect.iTl; // bitmap piece top left corner position
00152                         gc.BitBlt(pos, iBitmap, bmpPieceRect); // using bitmap piece
00153                         }
00154                         break;
00155                 case 2:
00156                         // draw a bitmap to a defined size in twips
00157                         // in the top left corner the rectangle,
00158                         // using the GDI DrawBitmap() function
00159                         {
00160                         TSize bmpSizeInTwips(600,600); // must set twips size, default (0,0)
00161                         iBitmap->SetSizeInTwips(bmpSizeInTwips);
00162                         gc.DrawBitmap(rect.iTl, iBitmap);
00163                         }
00164                         break;
00165                 case 3:
00166                         // draw a stretched bitmap inside the rectangle,
00167                         // using the GDI DrawBitmap() function
00168                         {
00169                         gc.DrawBitmap(rect, iBitmap);
00170                         }
00171                         break;
00172                 case 4:
00173                         {
00174                         // use bitmap as brush pattern, tiling from top left of rectangle
00175                         // set brush pattern and style to use the bitmap
00176                         gc.UseBrushPattern(iBitmap);
00177                         gc.SetBrushStyle(CGraphicsContext::EPatternedBrush);
00178                         gc.DrawRect(rect);
00179                         gc.DiscardBrushPattern();
00180                         }
00181                         break;
00182                 case 5:
00183                         {
00184                         // use bitmap as brush pattern, tiling around center of screen
00185                         // set brush pattern and style to use the bitmap
00186                         gc.SetBrushOrigin(screenCenterPoint);
00187                         gc.UseBrushPattern(iBitmap);
00188                         gc.SetBrushStyle(CGraphicsContext::EPatternedBrush);
00189                         gc.DrawRect(rect);
00190                         gc.DiscardBrushPattern();
00191                         }
00192                         break;
00193                 case 6:
00194                         // bisect screen into two different coloured rects
00195                         {
00196                         TRect screenRect=Rect();
00197                         TInt bisect = (screenRect.iBr.iX-screenRect.iTl.iX)/2 + screenRect.iTl.iX;
00198                         TRect leftRect(screenRect.iTl,TPoint(bisect,screenRect.iBr.iY));
00199                         TRect rightRect(TPoint(bisect,screenRect.iTl.iY),screenRect.iBr);
00200                         TRgb darkGray(85,85,85);
00201                         gc.SetBrushColor(darkGray);
00202                         gc.Clear(leftRect);
00203                         TRgb black(0,0,0);
00204                         gc.SetBrushColor(black);
00205                         gc.Clear(rightRect);
00206 
00207                         TSize bmpSizeInPixels=iBitmap->SizeInPixels();
00208                         TSize bmpPieceSize(bmpSizeInPixels.iWidth,bmpSizeInPixels.iHeight);
00209                         TPoint bmpPieceTopLeft(0,0); 
00210                         bmpPieceRect.SetRect(bmpPieceTopLeft,bmpPieceSize); 
00211                         
00212                         // center bitmap on left
00213                         xDelta=(leftRect.Width()-bmpPieceRect.Width())/2;
00214                         yDelta=(leftRect.Height()-bmpPieceRect.Height())/2;
00215                         TPoint pos=TPoint(xDelta,yDelta); // displacement vector
00216                         pos += leftRect.iTl; // bitmap piece top left corner position
00217                         gc.BitBlt(pos,iBitmap);
00218 
00219                         // center bitmap on right
00220                         xDelta=(rightRect.Width()-bmpPieceRect.Width())/2;
00221                         yDelta=(rightRect.Height()-bmpPieceRect.Height())/2;
00222                         TPoint pos2=TPoint(xDelta,yDelta); // displacement vector
00223                         pos2 += rightRect.iTl; // bitmap piece top left corner position
00224                         gc.BitBlt(pos2,iBitmap);
00225                         }
00226                         break;
00227                 case 7:
00228                         // bisect screen into two different coloured rects
00229                         {
00230                         TRect screenRect=Rect();
00231                         TInt bisect = (screenRect.iBr.iX-screenRect.iTl.iX)/2 + screenRect.iTl.iX;
00232                         TRect leftRect(TPoint(screenRect.iTl.iX,screenRect.iTl.iY+50),TPoint(bisect,screenRect.iBr.iY));
00233                         TRect rightRect(TPoint(bisect,screenRect.iTl.iY+50),screenRect.iBr);
00234                         TRgb darkGray(85,85,85);
00235                         gc.SetBrushColor(darkGray);
00236                         gc.Clear(leftRect);
00237                         TRgb black(0,0,0);
00238                         gc.SetBrushColor(black);
00239                         gc.Clear(rightRect);
00240 
00241                         TSize bmpSizeInPixels=iBitmap->SizeInPixels();
00242                         TSize bmpPieceSize(bmpSizeInPixels.iWidth,bmpSizeInPixels.iHeight);
00243                         TPoint bmpPieceTopLeft(0,0); 
00244                         bmpPieceRect.SetRect(bmpPieceTopLeft,bmpPieceSize); 
00245                         
00246                         // center bitmap on left
00247                         xDelta=(leftRect.Width()-bmpPieceRect.Width())/2;
00248                         yDelta=(leftRect.Height()-bmpPieceRect.Height())/2;
00249                         TPoint pos=TPoint(xDelta,yDelta); // displacement vector
00250                         pos += leftRect.iTl; // bitmap piece top left corner position
00251                         gc.BitBltMasked(pos,iBitmap,bmpPieceRect,iMaskBitmap,EFalse); // CWindowGc member function
00252 
00253                         // center bitmap on right
00254                         xDelta=(rightRect.Width()-bmpPieceRect.Width())/2;
00255                         yDelta=(rightRect.Height()-bmpPieceRect.Height())/2;
00256                         TPoint pos2=TPoint(xDelta,yDelta); // displacement vector
00257                         pos2 += rightRect.iTl; // bitmap piece top left corner position
00258                         gc.BitBltMasked(pos2,iBitmap,bmpPieceRect,iMaskBitmap,EFalse); // CWindowGc member function
00259                         
00260                         _LIT(KTxtTheBitmap,"The bitmap:");
00261                         _LIT(KTxtBitmapMask,"The bitmap's mask:");
00262 
00263                         gc.DrawText(KTxtTheBitmap,TPoint(5,20));
00264                         gc.BitBlt(TPoint(130,0),iBitmap);
00265                         gc.DrawText(KTxtBitmapMask,TPoint(197,20));
00266                         gc.BitBlt(TPoint(400,0),iMaskBitmap);
00267                         }
00268                         break;
00269                 default:
00270                         break;
00271                 }
00272         }

Generated by  doxygen 1.6.2