00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "EmbeddingGraphicsControl.h"
00032 #include <coemain.h>
00033
00034
00035 #include <s32std.h>
00036
00037 static const TUid KUidExampleSmileyPicture = { 0x100000E6 } ;
00038
00039
00040
00041
00042
00043
00044
00045
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
00059
00060
00061 if (aHeader.iPicture.IsId())
00062 aHeader.iPicture = CSmileyPicture::NewL(aDeferredPictureStore,aHeader.iPicture.AsId());
00063
00064 }
00065 else
00066 {
00067
00068 User::Leave(KErrNoMemory);
00069 }
00070 }
00071
00072
00073
00074 CSmileyPicture::CSmileyPicture()
00075 {
00076
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
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
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* ) 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
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& ) const
00208 {
00209
00210 }
00211
00212
00213 void CSmileyPicture::SetScaleFactor(TInt ,TInt )
00214 {
00215
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& )
00232 {
00233
00234 }
00235
00236
00237 void CSmileyPicture::GetCropInTwips(TMargins& aMargins)const
00238 {
00239 CPicture::GetCropInTwips(aMargins);
00240 }
00241
00242 TPictureCapability CSmileyPicture::Capability()const
00243 {
00244 return CPicture::Capability();
00245 }
00246
00247
00248 CPictureControl::CPictureControl()
00249 {
00250 SetMaxPhases(7);
00251 iValidDocument = CPictureControl::EPicture;
00252 iOffset = TPoint(50,50);
00253 }
00254
00255
00256 _LIT(KFileName,"\\grpict.dat");
00257
00258
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
00270 testZf.SetGraphicsDeviceMap(iCoeEnv->ScreenDevice());
00271
00272 testZf.SetZoomFactor(TZoomFactor::EZoomOneToOne);
00273
00274 testMap=&testZf;
00275
00276
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
00299 fsSession.Connect();
00300
00301
00302 fsSession.Parse(KFileName,filestorename);
00303 iStore = CDirectFileStore::ReplaceLC(fsSession,filestorename.FullName(),EFileWrite);
00304
00305 iStore->SetTypeL(KDirectFileStoreLayoutUid);
00306
00307
00308 iHeader.iPicture = iPicture;
00309 iHeader.iSize = TSize(iPicture->SpecToFactor()*40,iPicture->SpecToFactor()*40);
00310 iHeader.iPictureType = KUidExampleSmileyPicture;
00311
00312
00313 iHeaderId = StoreHeaderL(*iStore);
00314
00315 iStore->SetRootL(iHeaderId);
00316
00317
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
00330 fsSession.Connect();
00331
00332
00333 fsSession.Parse(KFileName,filestorename);
00334 iStore = CDirectFileStore::OpenLC(fsSession,filestorename.FullName(),EFileRead);
00335
00336 RestoreHeaderL(*iStore,iStore->Root());
00337
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
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
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();
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& ) const
00395 {
00396
00397 CWindowGc& gc=SystemGc();
00398 gc.UseFont(iMessageFont);
00399 gc.Clear();
00400 SystemGc().DrawRect(Rect());
00401 TRect rect=Rect();
00402 TRgb darkGray(85,85,85);
00403
00404
00405 switch (iValidDocument)
00406 {
00407 case CPictureControl::EFalse:
00408
00409 gc.SetBrushColor(darkGray);
00410 gc.Clear(rect);
00411 break;
00412 case CPictureControl::EPicture:
00413
00414 iPicture->Draw(gc,iOffset,rect,testMap);
00415 break;
00416 case CPictureControl::EHeader:
00417
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
00428
00429
00430 }