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