examples/Graphics/Embedding/Picture.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 "EmbeddingGraphicsControl.h"
00032 #include <coemain.h>
00033 
00034 // for the store
00035 #include <s32std.h>
00036 
00037 static const TUid KUidExampleSmileyPicture = { 0x100000E6 } ;
00038 
00039 /*
00040         CExamplePictureFactory
00041 
00042         Give picture header, checks UID and only if it's KUidExampleSmileyPicture will this restore
00043         a picture - changing the swizzle in aHeader from a stream id into a pointer.
00044         [incidentally, what errors should result if either the picture is already restored, or the UID
00045         isn't recognized?]
00046 */
00047 
00048 class TExamplePictureFactory : public MPictureFactory
00049         {
00050 public:
00051         void NewPictureL(TPictureHeader& aHeader,const CStreamStore& aDeferredPictureStore) const;
00052         };
00053 
00054 void TExamplePictureFactory::NewPictureL(TPictureHeader& aHeader,const CStreamStore& aDeferredPictureStore) const
00055         {
00056         if (aHeader.iPictureType == KUidExampleSmileyPicture)
00057                 {
00058                 // restore new picture from store into the TSwizzle
00059                 // (which changes from stream id to pointer)
00060                 // Construct a CSmileyPicture object and restore from the stream
00061             if (aHeader.iPicture.IsId())
00062                         aHeader.iPicture = CSmileyPicture::NewL(aDeferredPictureStore,aHeader.iPicture.AsId());
00063                         
00064                 }
00065         else
00066                 {
00067                         // output error and panic
00068                         User::Leave(KErrNoMemory);
00069                 }
00070         }
00071 
00072 // implementation of the CSmileyPicture class
00073 
00074 CSmileyPicture::CSmileyPicture()
00075         {
00076         // do nothing
00077         }
00078 
00079 CSmileyPicture* CSmileyPicture::NewL(TMood aMood, TSizeSpec aSizeSpec)
00080         {
00081         CSmileyPicture* self = new (ELeave) CSmileyPicture();
00082         self->iMood = aMood;
00083         self->iSizeSpec = aSizeSpec;
00084         return self;
00085         }
00086 
00087 CSmileyPicture* CSmileyPicture::NewL(const CStreamStore& aStore, TStreamId aStreamId) 
00088 // create from store
00089         {
00090         RStoreReadStream inStream;
00091         inStream.OpenLC(aStore,aStreamId);
00092         CSmileyPicture* self = new (ELeave) CSmileyPicture();
00093         self->InternalizeL(inStream);
00094         CleanupStack::PopAndDestroy();
00095         return self;
00096         }
00097 
00098 TStreamId CSmileyPicture::StoreL(CStreamStore& aStore) const
00099 // stores the CSmileyPicture in a new stream of the specified store (using ExternalizeL())
00100         {
00101         RStoreWriteStream stream;
00102         TStreamId id=stream.CreateLC(aStore);
00103         ExternalizeL(stream);  
00104         stream.CommitL();
00105         CleanupStack::PopAndDestroy();
00106         return id;
00107         }
00108 
00109 void CSmileyPicture::ExternalizeL(RWriteStream& aStream) const
00110         {
00111         aStream.WriteInt8L(iMood);
00112         aStream.WriteInt8L(iSizeSpec);
00113         }
00114 
00115 void CSmileyPicture::InternalizeL(RReadStream& aStream)
00116         {
00117         iMood = TMood(aStream.ReadInt8L());
00118         iSizeSpec = TSizeSpec(aStream.ReadInt8L());
00119         }
00120 
00121 TInt CSmileyPicture::SpecToFactor() const
00122         {
00123         switch (iSizeSpec)
00124                 {
00125                 case ESmall:
00126                         return 1;
00127                 case EMedium:
00128                         return 2;
00129                 case ELarge:
00130                         return 3;
00131                 default:
00132                         return 0;
00133                 }
00134         }
00135 
00136 void CSmileyPicture::Draw(CGraphicsContext& aGc,const TPoint& aTopLeft,const TRect& aClipRect,
00137                                                 MGraphicsDeviceMap* /*aMap*/) const
00138         {
00139         aGc.SetClippingRect(aClipRect);  
00140 
00141         TInt scaleFactor = SpecToFactor();
00142 
00143         TSize penSizeBold(3*scaleFactor,3*scaleFactor);
00144         TSize penSizeFat(5*scaleFactor,5*scaleFactor);
00145         aGc.SetPenSize(penSizeFat);
00146 
00147         TInt leftOffset = 13*scaleFactor;
00148         TInt rightOffset = 27*scaleFactor;
00149         TInt circleSize = 40*scaleFactor;
00150         TInt shrinkSize = 10*scaleFactor;
00151         TInt halfCircle = 20*scaleFactor;
00152         TInt neutralSize = 13*scaleFactor;
00153 
00154         TPoint leftEye(aTopLeft.iX+leftOffset,aTopLeft.iY+leftOffset);
00155         TPoint rightEye(aTopLeft.iX+rightOffset,aTopLeft.iY+leftOffset);
00156         aGc.Plot(leftEye);
00157         aGc.Plot(rightEye);
00158         aGc.SetPenSize(penSizeBold);
00159         TRect circle(aTopLeft,TPoint(aTopLeft.iX+circleSize,aTopLeft.iY+circleSize));
00160         aGc.DrawEllipse(circle);
00161 
00162         // draw the smile
00163         TRect smile = circle;
00164         switch (iMood)
00165                 {
00166                 case EHappy:
00167                         {
00168                         smile.Shrink(shrinkSize,shrinkSize);
00169                         aGc.DrawArc(smile,TPoint(aTopLeft.iX,aTopLeft.iY+circleSize-shrinkSize),TPoint(aTopLeft.iX+circleSize,aTopLeft.iY+circleSize-shrinkSize));
00170                         }
00171                         break;
00172                 case ENeutral:
00173                         {
00174                         aGc.DrawLine(TPoint(leftEye.iX,leftEye.iY+neutralSize),TPoint(rightEye.iX,rightEye.iY+neutralSize));
00175                         }
00176                         break;
00177                 case ESad:
00178                         {
00179                         smile.Shrink(shrinkSize,shrinkSize);
00180                         smile.Move(0,15*scaleFactor);
00181                         aGc.DrawArc(smile,TPoint(aTopLeft.iX+circleSize,aTopLeft.iY+halfCircle),TPoint(aTopLeft.iX,aTopLeft.iY+halfCircle));
00182                         }
00183                         break;
00184                 }
00185         }
00186 
00187 void CSmileyPicture::SetMood(TMood aMood)
00188         {
00189         iMood = aMood;  
00190         }
00191 
00192 CSmileyPicture::TMood CSmileyPicture::Mood()
00193         {
00194         return iMood;
00195         }
00196 
00197 void CSmileyPicture::SetSize(TSizeSpec aSizeSpec)
00198         {
00199         iSizeSpec = aSizeSpec;  
00200         }
00201 
00202 CSmileyPicture::TSizeSpec CSmileyPicture::Size()
00203         {
00204         return iSizeSpec;
00205         }
00206 
00207 void CSmileyPicture::GetOriginalSizeInTwips(TSize& /*aSize*/) const
00208         {
00209         // do nothing
00210         }
00211 
00212 
00213 void CSmileyPicture::SetScaleFactor(TInt /*aScaleFactorWidth*/,TInt /*aScaleFactorHeight*/)
00214         {
00215         // do nothing
00216         }
00217 
00218 
00219 TInt CSmileyPicture::ScaleFactorWidth()const
00220         {
00221         return 1;
00222         }
00223 
00224 
00225 TInt CSmileyPicture::ScaleFactorHeight()const
00226         {
00227         return 1;
00228         }
00229 
00230 
00231 void CSmileyPicture::SetCropInTwips(const TMargins& /*aMargins*/)
00232         {
00233         // do nothing
00234         }
00235 
00236 
00237 void CSmileyPicture::GetCropInTwips(TMargins& aMargins)const
00238         {
00239         CPicture::GetCropInTwips(aMargins); // no crop
00240         }
00241 
00242 TPictureCapability CSmileyPicture::Capability()const
00243         {
00244         return CPicture::Capability(); // no scale, no crop
00245         }
00246 
00247 // example really starts here
00248 CPictureControl::CPictureControl()
00249         { 
00250         SetMaxPhases(7);
00251         iValidDocument = CPictureControl::EPicture;
00252         iOffset = TPoint(50,50);
00253         }
00254 
00255 // The file name
00256 _LIT(KFileName,"\\grpict.dat");
00257 
00258 // Literal text
00259 _LIT(KTxtUpdateModelCase0,"draw happy face at (50,50)");
00260 _LIT(KTxtUpdateModelCase1,"draw sad face at (50,50)");
00261 _LIT(KTxtUpdateModelCase2,"draw neutral face at (50,50)");
00262 _LIT(KTxtUpdateModelCase3,"store picture in file");
00263 _LIT(KTxtUpdateModelCase4,"delete picture from memory");
00264 _LIT(KTxtUpdateModelCase5,"restore picture header from file");
00265 _LIT(KTxtUpdateModelCase6,"restore picture from file");
00266 
00267 void CPictureControl::UpdateModelL()
00268         {
00269         // set up zoom factor object
00270         testZf.SetGraphicsDeviceMap(iCoeEnv->ScreenDevice());
00271         // set the zoom factor of the object
00272         testZf.SetZoomFactor(TZoomFactor::EZoomOneToOne);
00273         // use graphics device maps for drawing and getting fonts
00274         testMap=&testZf;
00275 
00276         // the file session used in the example
00277         RFs fsSession;
00278         TParse filestorename;
00279         
00280         switch (Phase())
00281                 {
00282                 case 0:
00283                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase0);
00284                         iPicture = CSmileyPicture::NewL(CSmileyPicture::EHappy,CSmileyPicture::ESmall);
00285                         break;
00286                 case 1:
00287                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase1);
00288                         iPicture->SetMood(CSmileyPicture::ESad);
00289                         iPicture->SetSize(CSmileyPicture::EMedium);
00290                         break;
00291                 case 2:
00292                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase2);
00293                         iPicture->SetMood(CSmileyPicture::ENeutral);
00294                         iPicture->SetSize(CSmileyPicture::ELarge);
00295                         break;
00296                 case 3:
00297                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase3);
00298                         // set up the permament direct file store for the picture
00299                         fsSession.Connect();
00300                         
00301                         // Create (replace, if it exists) the direct file store
00302                         fsSession.Parse(KFileName,filestorename);
00303                         iStore = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00304                         // Must say what kind of file store.
00305                         iStore->SetTypeL(KDirectFileStoreLayoutUid);
00306 
00307                         // create picture header
00308                         iHeader.iPicture = iPicture;
00309                         iHeader.iSize = TSize(iPicture->SpecToFactor()*40,iPicture->SpecToFactor()*40);
00310                         iHeader.iPictureType = KUidExampleSmileyPicture;
00311 
00312                         // store picture header and picture
00313                         iHeaderId = StoreHeaderL(*iStore);
00314                         // make header stream the root stream
00315                         iStore->SetRootL(iHeaderId);
00316 
00317                         // close store
00318                         CleanupStack::PopAndDestroy();
00319                         fsSession.Close();
00320                         break;
00321                 case 4:
00322                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase4);
00323                         delete iPicture;
00324                         iPicture = NULL;
00325                         iValidDocument = CPictureControl::EFalse;
00326                         break;
00327                 case 5:
00328                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase5);
00329                         // set up the permament direct file store for the picture
00330                         fsSession.Connect();
00331                         
00332                         // Open the direct file store and read the header
00333                         fsSession.Parse(KFileName,filestorename);
00334                         iStore = CDirectFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00335 
00336                         RestoreHeaderL(*iStore,iStore->Root());
00337                         // close store
00338                         CleanupStack::PopAndDestroy();
00339                         fsSession.Close();
00340                         iValidDocument = CPictureControl::EHeader;
00341                         break;
00342                 case 6:
00343                         iGraphObserver->NotifyStatus(KTxtUpdateModelCase6);
00344 
00345                         fsSession.Connect();
00346                         
00347                         // Open the direct file store and read the header
00348                         fsSession.Parse(KFileName,filestorename);
00349                         iStore = CDirectFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00350                         
00351                         TExamplePictureFactory factory;
00352                         factory.NewPictureL(iHeader,*iStore);
00353                                                 
00354                         iPicture = (CSmileyPicture *) iHeader.iPicture.AsPtr();
00355 
00356                         // close store
00357                         CleanupStack::PopAndDestroy();
00358                         fsSession.Close();
00359                         iValidDocument = CPictureControl::EPicture;
00360                         break;
00361                 }
00362         }
00363 
00364 void CPictureControl::RestoreHeaderL(CStreamStore& aStore, TStreamId aId)
00365         {
00366         RStoreReadStream stream;
00367         stream.OpenLC(aStore,aId);
00368         iHeader.InternalizeL(stream);
00369         CleanupStack::PopAndDestroy();          // close and delete the stream
00370         }
00371 
00372 TStreamId CPictureControl::StoreHeaderL(CStreamStore& aStore) const
00373         {
00374         CStoreMap* map=CStoreMap::NewLC(aStore);
00375         StoreHeaderComponentsL(*map,aStore);
00376 
00377         RStoreWriteStream stream(*map);
00378         TStreamId id = stream.CreateLC(aStore);
00379         iHeader.ExternalizeL(stream);
00380         stream.CommitL();
00381         map->Reset();
00382         CleanupStack::PopAndDestroy(2);
00383         return id;
00384         }
00385 
00386 void CPictureControl::StoreHeaderComponentsL(CStoreMap& aMap,CStreamStore& aStore) const
00387         {
00388         TStreamId id;
00389 
00390         id = iPicture->StoreL(aStore);
00391         aMap.BindL(iPicture,id);
00392         }
00393 
00394 void CPictureControl::Draw(const TRect& /* aRect */) const
00395         {
00396         // draw surrounding rectangle
00397         CWindowGc& gc=SystemGc(); // graphics context we draw to
00398         gc.UseFont(iMessageFont); // use the system message font
00399         gc.Clear(); // clear the area to be drawn to
00400         SystemGc().DrawRect(Rect()); // surrounding rectangle to draw into
00401         TRect rect=Rect(); // a centered rectangle of the default size
00402         TRgb darkGray(85,85,85);
00403 
00404         // decide what to do, and do it
00405         switch (iValidDocument)
00406                 {
00407         case CPictureControl::EFalse:
00408                 // if document is not valid then Draw() draws gray screen
00409                 gc.SetBrushColor(darkGray);
00410                 gc.Clear(rect);
00411                 break;
00412         case CPictureControl::EPicture:
00413                 // if picture in is memory then draw it
00414                 iPicture->Draw(gc,iOffset,rect,testMap);
00415                 break;
00416         case CPictureControl::EHeader: 
00417                 // if no iPicture, draw picture outline to specified size at iOffset
00418                 TInt bottomRightX = iOffset.iX + iHeader.iSize.iWidth;
00419                 TInt bottomRightY = iOffset.iY + iHeader.iSize.iHeight;
00420                 TRect outlineBox(iOffset,TPoint(bottomRightX,bottomRightY));
00421         
00422                 gc.SetPenStyle(CGraphicsContext::EDottedPen);
00423                 gc.DrawRect(outlineBox);
00424                 break;
00425                 }
00426 
00427         //iConEnv->ScreenDevice()->ReleaseFont(iMessageFont);
00428         //gc.ReleaseFont(iMessageFont);
00429         //gc.DiscardFont(); // discard font
00430         }

Generated by  doxygen 1.6.2