examples/Graphics/Bitmaps/FontBitmapServer.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 // header for multi-bitmap file grbitmap.mbm containing 2 bitmaps to use
00019 #include <grbmap.mbg>
00020 
00021 
00022         // The name of the multi-bitmap file containing the bitmap
00023         // and bitmap mask files.
00024 _LIT(KTxtMBMname,"\\resource\\apps\\grbmap.mbm");
00025 
00026 CFbsControl::~CFbsControl()
00027 {
00028         // as the user can exit the example at any phase
00029         // delete any member data that may cause a memory leak
00030         if (Phase()==1 || Phase()==7 || Phase()==8) delete(iBitmap1);
00031         if (Phase()==3 || Phase()==4) delete(iBitmap2);
00032         if (Phase()==4 || Phase()==5) delete(iBitmap3);
00033         if (Phase()==8) delete(iBitmap4);
00034 }
00035 
00036 
00037         // Text printed to the console
00038 _LIT(KTxtUpdateModelCase0,"Load, draw and delete bitmap");
00039 _LIT(KTxtUpdateModelCase1,"Load and draw bitmap");
00040 _LIT(KTxtUpdateModelCase2,"Delete bitmap");
00041 _LIT(KTxtUpdateModelCase3,"Create bitmap2 and draw");
00042 _LIT(KTxtUpdateModelCase4,"Duplicate bitmap 2 as bitmap 3 and draw both");
00043 _LIT(KTxtUpdateModelCase5,"Delete bitmap 2, draw bitmap 3");
00044 _LIT(KTxtUpdateModelCase6,"Delete bitmap 3");
00045 _LIT(KTxtUpdateModelCase7,"Reload and draw bitmap1");
00046 _LIT(KTxtUpdateModelCase8,"Make bitmap 4 as transposed bitmap 1 and draw both");
00047 _LIT(KTxtUpdateModelCase9,"Delete bitmaps 1 and 4");
00048 
00049 void CFbsControl::UpdateModelL()
00050         {
00051         // set up name for bitmap sharing
00052         TBool shareIfLoaded(ETrue);
00053 
00054         // device and context for drawing to the in-memory bitmap
00055         CFbsBitmapDevice* iBitmapDevice2;
00056         CGraphicsContext* iBitmapContext2;
00057 
00058                 switch (Phase())
00059                 {
00060                 case 0:
00061                         // NB. in this phase, loading of the bitmap occurs in Draw()
00062                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase0);
00063                         break;
00064                 case 1:
00065                         // load bitmap1 from .mbm file
00066                         iBitmap1 = new (ELeave) CFbsBitmap();
00067             LoadBitmapL(iBitmap1,KTxtMBMname,EMbmGrbmapSmiley,shareIfLoaded);
00068                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase1);
00069                         break;
00070                 case 2:
00071                         // delete bitmap1
00072                         delete iBitmap1;
00073                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase2);
00074                         break;
00075                 case 3:
00076                         // create bitmap 2
00077                         iBitmap2 = new (ELeave) CFbsBitmap();
00078                         iBitmap2->Create(TSize(100,100),EGray4);
00079                         // create a device and gc for it
00080                         iBitmapDevice2 = CFbsBitmapDevice::NewL(iBitmap2);
00081                         iBitmapDevice2->CreateContext(iBitmapContext2);
00082                         // draw to it
00083                         DrawSmiley(*iBitmapContext2);
00084                         // delete the context and device for bitmap 2
00085                         delete iBitmapContext2;
00086                         delete iBitmapDevice2;
00087                         // and blit it (in the Draw() function)
00088                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase3);
00089                         break;
00090                 case 4:
00091                         // create bitmap 3 as a duplicate of bitmap 2
00092                         iBitmap3 = new (ELeave) CFbsBitmap();
00093                         iBitmap3->Duplicate(iBitmap2->Handle());
00094                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase4);
00095                         break;
00096                 case 5:
00097                         // delete bitmap 2
00098                         delete iBitmap2;
00099                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase5);
00100                         break;
00101                 case 6:
00102                         // delete bitmap 3
00103                         delete iBitmap3;
00104                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase6);
00105                         break;
00106                 case 7:
00107                         // reload bitmap 1 from .mbm file
00108                         iBitmap1 = new (ELeave) CFbsBitmap();
00109             LoadBitmapL(iBitmap1,KTxtMBMname,EMbmGrbmapSmiley,shareIfLoaded);
00110                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase7);
00111                         break;
00112                 case 8:
00113                         // create bitmap 4 as rotated bitmap 1
00114                         CreateRotatedBitmapL();
00115                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase8);
00116                         break;
00117                 case 9:
00118                         // delete remaing bitmaps (1 and 4)
00119                         delete iBitmap1;
00120                         delete iBitmap4;
00121                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase9);
00122                 }
00123         }
00124 
00125 void CFbsControl::Draw(const TRect& /* aRect */) const
00126         {
00127         // draw surrounding rectangle
00128         CWindowGc& gc=SystemGc(); // graphics context we draw to
00129         gc.UseFont(iMessageFont); // use the system message font
00130         gc.Clear(); // clear the area to be drawn to
00131         TRect rect=Rect(); // a centered rectangle of the default size
00132         gc.DrawRect(rect); // surrounding rectangle to draw into
00133         TRgb darkGray(85,85,85);
00134 
00135         // decide what to do, and do it
00136         switch (Phase())
00137                 {
00138                 case 0:
00139                         {
00140                         // perform (leavable) draw function
00141                     TRAP_IGNORE(MUTABLE_CAST(CFbsControl*,this)->DrawL(gc));
00142                         }
00143                         break;
00144                 case 1:
00145                         // draw bitmap, centered on the screen,
00146                         // using bitmap block transfer
00147                         {
00148                         DrawSingleBitmap(iBitmap1,gc);
00149                         }
00150                         break;
00151                 case 2:
00152                         {
00153                         // no bitmaps are loaded so draw gray screen
00154                         gc.SetBrushColor(darkGray);
00155                         gc.Clear(rect);
00156                         }
00157                         break;
00158                 case 3:
00159                         // draw bitmap2
00160                         {
00161                         DrawSingleBitmap(iBitmap2,gc);
00162                         }
00163                         break;
00164                 case 4:
00165                         // draw bitmap 2 and bitmap3
00166                         {
00167                         DrawTwoBitmaps(iBitmap2,iBitmap3,gc);
00168                         }
00169                         break;
00170                 case 5:
00171                         // draw bitmap3
00172                         {
00173                         DrawSingleBitmap(iBitmap3,gc);
00174                         }
00175                         break;
00176                 case 6:
00177                         {
00178                         // no bitmaps are loaded so draw gray screen
00179                         gc.SetBrushColor(darkGray);
00180                         gc.Clear(rect);
00181                         }
00182                         break;
00183                 case 7:
00184                         {
00185                         DrawSingleBitmap(iBitmap1,gc);
00186                         }
00187                         break;
00188                 case 8:
00189                         // draw bitmap and bitmap4
00190                         {
00191                         DrawTwoBitmaps(iBitmap1,iBitmap4,gc);
00192                         }
00193                         break;
00194                 case 9:
00195                         {
00196                         // no bitmaps are loaded so draw gray screen
00197                         gc.SetBrushColor(darkGray);
00198                         gc.Clear(rect);
00199                         }
00200                         break;
00201                 default:
00202                         break;
00203                 }
00204         }
00205 
00206 
00207 _LIT(KTxtZDrive,"Z:");
00208 
00209 void CFbsControl::LoadBitmapL(CFbsBitmap* aBitMap,const TDesC& aPathAndFile,TInt aId,TBool aShareIfLoaded)
00210         {
00211         TParse mbfn;
00212         
00213         TDriveUnit sysDrive (RFs::GetSystemDrive());
00214         TDriveName sysDriveName (sysDrive.Name());      
00215         TFileName fileName(sysDriveName);
00216         fileName.Append(aPathAndFile);
00217 
00218         mbfn.Set(aPathAndFile,&fileName,NULL);
00219         if (!aBitMap->Load(mbfn.FullName(),aId,aShareIfLoaded))
00220                 return;
00221 
00222         mbfn.Set(aPathAndFile,&KTxtZDrive,NULL);
00223         User::LeaveIfError(aBitMap->Load(mbfn.FullName(),aId,aShareIfLoaded));
00224         return;
00225         }
00226 
00227 
00228 void CFbsControl::DrawL(CWindowGc& aGc)
00229         // draw a whole bitmap centered on the screen,
00230         // using bitmap block transfer
00231         {
00232         // set up name for bitmap sharing
00233         TBool shareIfLoaded(ETrue);
00234 
00235         // load the bitmap and mask bitmap
00236         iBitmap1 = new (ELeave) CFbsBitmap();
00237 
00238     // Load the bitmap
00239     LoadBitmapL(iBitmap1,KTxtMBMname,EMbmGrbmapSmiley,shareIfLoaded);
00240         
00241         // calculate position for top left of bitmap so it is centered
00242         DrawSingleBitmap(iBitmap1,aGc);
00243 
00244         // delete bitmap from memory
00245         delete(iBitmap1);
00246         }
00247 
00248 void CFbsControl::DrawSmiley(CGraphicsContext& aGc)
00249         {
00250         // setup pens for drawing smiley
00251         TSize penSizeBold(3,3);
00252         TSize penSizeFat(5,5);
00253         aGc.SetPenSize(penSizeFat);
00254 
00255         // setup sizes/offsets for the face
00256         TInt leftOffset = 22;
00257         TInt rightOffset = 38;
00258         TInt circleSize = 50;
00259         TInt shrinkSize = 10;
00260 
00261         // draw eyes and outline of face (circle)
00262         TPoint leftEye(leftOffset,21);
00263         TPoint rightEye(rightOffset,21);
00264         aGc.Plot(leftEye);
00265         aGc.Plot(rightEye);
00266         aGc.SetPenSize(penSizeBold);
00267         TRect circle(TPoint(10,10),TPoint(circleSize,circleSize));
00268         aGc.DrawEllipse(circle);
00269 
00270         // draw the smile
00271         TRect smile = circle;
00272         smile.Shrink(shrinkSize,shrinkSize);
00273         aGc.DrawArc(smile,TPoint(10,circleSize-shrinkSize),TPoint(circleSize,circleSize-shrinkSize));
00274         }
00275 
00276 void CFbsControl::CreateRotatedBitmapL()
00277         {
00278         // create iBitmap4 as the same size as iBitmap, but with the height and width swapped
00279         iBitmap4 = new (ELeave) CFbsBitmap();
00280         TSize inSize = iBitmap1->SizeInPixels();
00281         iBitmap4->Create(TSize(inSize.iHeight,inSize.iWidth),iBitmap1->DisplayMode());
00282 
00283         // create the bitmap utils
00284         TBitmapUtil* iBitmap1Util = new (ELeave) TBitmapUtil(iBitmap1);
00285         TBitmapUtil* iBitmap4Util = new (ELeave) TBitmapUtil(iBitmap4);
00286         
00287         iBitmap1Util->Begin(TPoint(0,0));       // lock the stack and perform a SetPos(TPoint(0,0)) on iBitmap1Util
00288         iBitmap4Util->Begin(TPoint(0,0),*iBitmap1Util); // perform a SetPos(TPoint(0,0)) on iBitmap4Util, but does not lock the stack again
00289 
00290         // set the bits of iBitmap4 as iBitmap1, rotated through 90 degrees
00291         TInt xPos;
00292         for (TInt yPos=0;yPos<inSize.iHeight;yPos++)
00293                 {
00294                 iBitmap1Util->SetPos(TPoint(0,yPos));
00295                 iBitmap4Util->SetPos(TPoint(yPos,0));
00296                 for (xPos=0;xPos<inSize.iWidth;xPos++) 
00297                         {
00298                         iBitmap4Util->SetPixel(*iBitmap1Util);
00299                         iBitmap1Util->IncXPos();
00300                         iBitmap4Util->IncYPos();
00301                         }
00302                 }
00303 
00304         // each Begin() must have a corresponding End()
00305         iBitmap1Util->End();
00306         iBitmap4Util->End();
00307         
00308         // delete the bitmap utils
00309         delete iBitmap1Util;
00310         delete iBitmap4Util;
00311   }
00312 
00313 void CFbsControl::DrawSingleBitmap(CFbsBitmap* aDrawBitmap,CWindowGc& aGc) const
00314         {
00315         // calculate position for top left of bitmap so it is centered
00316         TSize bmpSizeInPixels=aDrawBitmap->SizeInPixels();
00317         TRect rect=Rect(); // a centered rectangle of the default size
00318         TInt xDelta=(rect.Width()-bmpSizeInPixels.iWidth)/2;
00319         TInt yDelta=(rect.Height()-bmpSizeInPixels.iHeight)/2;
00320         TPoint pos=TPoint(xDelta,yDelta); // displacement vector
00321         pos+=rect.iTl; // bitmap top left corner position
00322         aGc.BitBlt(pos, aDrawBitmap); // CWindowGc member function
00323         }
00324 
00325 void CFbsControl::DrawTwoBitmaps(CFbsBitmap* aBitmap1,CFbsBitmap* aBitmap2,CWindowGc& aGc) const
00326         {
00327         // calculate position for top left of bitmap so it is centered
00328         TSize bmpSizeInPixels=aBitmap1->SizeInPixels();
00329         TRect rect=Rect(); // a centered rectangle of the default size
00330         TInt xDelta=(rect.Width()-bmpSizeInPixels.iWidth)/4;
00331         TInt yDelta=(rect.Height()-bmpSizeInPixels.iHeight)/4;
00332         TPoint pos=TPoint(xDelta,yDelta); // displacement vector
00333         TPoint pos2=TPoint(xDelta*3,yDelta);
00334         pos+=rect.iTl; // bitmap top left corner position
00335         pos2+=rect.iTl;
00336         aGc.BitBlt(pos, aBitmap1); // CWindowGc member function
00337         aGc.BitBlt(pos2, aBitmap2);
00338         }

Generated by  doxygen 1.6.2