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

Generated by  doxygen 1.6.2