# examples/Graphics/Bitmaps/Bitmap.cpp

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