javauis/eswt_akn/org.eclipse.ercp.swt.s60/native/src/swtdisplaybase.cpp
branchRCL_3
changeset 83 26b2b12093af
parent 66 2455ef1f5bbc
equal deleted inserted replaced
77:7cee158cb8cd 83:26b2b12093af
    19 #include <avkon.mbg>
    19 #include <avkon.mbg>
    20 #include <AknsUtils.h>
    20 #include <AknsUtils.h>
    21 #include <AknsConstants.h>
    21 #include <AknsConstants.h>
    22 #include <aknconsts.h>
    22 #include <aknconsts.h>
    23 #include <AknIconUtils.h>
    23 #include <AknIconUtils.h>
       
    24 #include <e32math.h>
    24 #include <swtlaffacade.h>
    25 #include <swtlaffacade.h>
    25 #include "s60commonutils.h"
    26 #include "s60commonutils.h"
    26 #include "swtfactory.h"
    27 #include "swtfactory.h"
    27 #include "swtdisplaybase.h"
    28 #include "swtdisplaybase.h"
    28 #include "eswtwidgetsexpanded.h"
    29 #include "eswtwidgetsexpanded.h"
    34 #include "swtmidremconobserver.h"
    35 #include "swtmidremconobserver.h"
    35 #include "swtjavabufferdrawer.h"
    36 #include "swtjavabufferdrawer.h"
    36 #include "swtfont.h"
    37 #include "swtfont.h"
    37 #include "swtuiutils.h"
    38 #include "swtuiutils.h"
    38 #include "imagescaler.h"
    39 #include "imagescaler.h"
       
    40 #include "mifconverter.h"
    39 
    41 
    40 #define ASSERT_JAVAUITHREAD() ASSERT(IsCurrentThreadJavaUi())
    42 #define ASSERT_JAVAUITHREAD() ASSERT(IsCurrentThreadJavaUi())
    41 #define ASSERT_NATIVEUITHREAD() ASSERT(IsCurrentThreadNativeUi())
    43 #define ASSERT_NATIVEUITHREAD() ASSERT(IsCurrentThreadNativeUi())
    42 
    44 
    43 
    45 
    44 // Assuming ResourceLanguageFileNameL inserts DRIVE:/KDC_RESOURCE_FILES_DIR/java/
    46 // Assuming ResourceLanguageFileNameL inserts DRIVE:/KDC_RESOURCE_FILES_DIR/java/
    45 // Assuming KDC_RESOURCE_FILES_DIR = /resource/java/
    47 // Assuming KDC_RESOURCE_FILES_DIR = /resource/java/
    46 _LIT(KSwtResFile, "eswtcore.rsc");
    48 _LIT(KSwtResFile, "eswtcore.rsc");
       
    49 
       
    50 const TInt KMifFileHeaderUid = 0x34232342;
       
    51 const TInt KMifFileHeaderVersion = 2;
       
    52 const TInt KMifFileHeaderLength = 2;
       
    53 const TInt KMifIconHeaderUid = 0x34232343;
       
    54 const TInt KMifIconHeaderVersion = 1;
       
    55 const TInt KMifIconHeaderType = 1;
       
    56 const TInt KMifIconHeaderAnimated = 0;
       
    57 
       
    58 _LIT(KTempMifFileDrive, "D:\\");
       
    59 _LIT(KTempMifFileExt, ".mif");
    47 
    60 
    48 
    61 
    49 // ======== MEMBER FUNCTIONS ========
    62 // ======== MEMBER FUNCTIONS ========
    50 
    63 
    51 
    64 
   423     }
   436     }
   424     else
   437     else
   425     {
   438     {
   426         if (size.iWidth > 0)
   439         if (size.iWidth > 0)
   427         {
   440         {
   428             AknIconUtils::SetSize(bmp, size);
   441             AknIconUtils::SetSize(bmp, size); // will resize the mask as well
   429         }
   442         }
   430         // bmp & mask ownership transferred to returned object
   443         // bmp & mask ownership transferred to returned object
   431         return CSwtImage::NewL(*bmp, mask);
   444         return CSwtImage::NewL(*bmp, mask);
   432     }
   445     }
       
   446 }
       
   447 
       
   448 MSwtImage* ASwtDisplayBase::NewImageFromSvgBufL(const TDesC8& aBuf, const TSize& aSize)
       
   449 {
       
   450     CFbsBitmap* mifBmp = NULL;
       
   451     CFbsBitmap* mifMask = NULL;
       
   452     CFbsBitmap* bmp = NULL;
       
   453     CFbsBitmap* mask = NULL;
       
   454     TInt bmpId(0);
       
   455     TInt maskId(1);
       
   456     
       
   457     TFileName tmpFile;
       
   458     StoreSvgAsMifL(aBuf, tmpFile);
       
   459     AknIconUtils::ValidateLogicalAppIconId(tmpFile, bmpId, maskId);
       
   460     AknIconUtils::CreateIconL(mifBmp, mifMask, tmpFile, bmpId, maskId);
       
   461     
       
   462     if (!mifBmp)
       
   463     {
       
   464         return NULL;
       
   465     }
       
   466     else
       
   467     {
       
   468         AknIconUtils::ExcludeFromCache(mifBmp);
       
   469         if (mifMask)
       
   470         {
       
   471             AknIconUtils::ExcludeFromCache(mifMask);
       
   472         }
       
   473         
       
   474         // Will resize the mask as well
       
   475         AknIconUtils::SetSize(mifBmp, aSize, EAspectRatioPreservedAndUnusedSpaceRemoved);
       
   476 
       
   477         // Rasterize
       
   478         CleanupStack::PushL(mifBmp);
       
   479         if (mifMask)
       
   480         {
       
   481             CleanupStack::PushL(mifMask);
       
   482         }
       
   483         bmp = RasterizeL(*mifBmp);
       
   484         CleanupStack::PushL(bmp);
       
   485         if (mifMask)
       
   486         {
       
   487             mask = RasterizeL(*mifMask);
       
   488         }
       
   489         CleanupStack::Pop(bmp);
       
   490         
       
   491         // Delete the mif bitmaps
       
   492         if (mifMask)
       
   493         {
       
   494             AknIconUtils::DestroyIconData(mifMask);
       
   495             CleanupStack::PopAndDestroy(mifMask);
       
   496         }
       
   497         
       
   498         AknIconUtils::DestroyIconData(mifBmp);
       
   499         CleanupStack::PopAndDestroy(mifBmp);
       
   500         
       
   501         // Delete the temp file
       
   502         iCoeEnv->FsSession().Delete(tmpFile);
       
   503         
       
   504         // bmp & mask ownership transferred to returned object
       
   505         return CSwtImage::NewL(*bmp, mask);
       
   506     }
       
   507 }
       
   508 
       
   509 MSwtImage* ASwtDisplayBase::NewImageFromSvgFileL(const TDesC& aFile, const TSize& aSize)
       
   510 {
       
   511     HBufC8* buf = LoadFileL(aFile);
       
   512     CleanupStack::PushL(buf);
       
   513     MSwtImage* res = NewImageFromSvgBufL(*buf, aSize);
       
   514     CleanupStack::PopAndDestroy(buf);
       
   515     return res;
   433 }
   516 }
   434 
   517 
   435 /**
   518 /**
   436  * Constructs an Image from another Image.
   519  * Constructs an Image from another Image.
   437  * @param aData  The data to construct the image from
   520  * @param aData  The data to construct the image from
  1298             TRAP_IGNORE(activeShell->Control()->CoeControl().OfferKeyEventL(aKeyEvent, (TEventCode)aEventType));
  1381             TRAP_IGNORE(activeShell->Control()->CoeControl().OfferKeyEventL(aKeyEvent, (TEventCode)aEventType));
  1299         }
  1382         }
  1300     }
  1383     }
  1301 }
  1384 }
  1302 
  1385 
  1303 // ---------------------------------------------------------------------------
       
  1304 // CSwtUiUtils::LoadResourceFileL
       
  1305 // ---------------------------------------------------------------------------
       
  1306 //
       
  1307 TInt ASwtDisplayBase::LoadResourceFileL()
  1386 TInt ASwtDisplayBase::LoadResourceFileL()
  1308 {
  1387 {
  1309     TFileName langFile = java::util::S60CommonUtils::ResourceLanguageFileNameL(KSwtResFile);
  1388     TFileName langFile = java::util::S60CommonUtils::ResourceLanguageFileNameL(KSwtResFile);
  1310     return iCoeEnv->AddResourceFileL(langFile);
  1389     return iCoeEnv->AddResourceFileL(langFile);
  1311 }
  1390 }
       
  1391 
       
  1392 void ASwtDisplayBase::StoreSvgAsMifL(const TDesC8& aSvgBuf, TFileName& aGeneratedFile)
       
  1393 {
       
  1394     TInt iconDataSize = aSvgBuf.Length();
       
  1395 
       
  1396     // File header
       
  1397     TMifFileHeader fileHeader;
       
  1398     fileHeader.iUid = KMifFileHeaderUid;
       
  1399     fileHeader.iVersion = KMifFileHeaderVersion;
       
  1400     fileHeader.iOffset = sizeof(fileHeader);
       
  1401     fileHeader.iLength = KMifFileHeaderLength;  // number of indexes
       
  1402 
       
  1403     // Icon offset element
       
  1404     TMifIconOffset iconOffset;
       
  1405     iconOffset.iIconOffset = sizeof(fileHeader) + sizeof(iconOffset) * KMifFileHeaderLength; // mif header + icon offset
       
  1406     iconOffset.iIconLength = sizeof(TMifIconHeader) + iconDataSize; // icon header + icon data
       
  1407 
       
  1408     // Icon header
       
  1409     TMifIconHeader iconHeader;
       
  1410     iconHeader.iUid = KMifIconHeaderUid;
       
  1411     iconHeader.iVersion = KMifIconHeaderVersion;
       
  1412     iconHeader.iOffset = sizeof(iconHeader);  // dataOffset
       
  1413     iconHeader.iLength = iconDataSize;  // dataLength
       
  1414     iconHeader.iType = KMifIconHeaderType;  // svg
       
  1415     iconHeader.iDepth = EColor16M;
       
  1416     iconHeader.iAnimated = KMifIconHeaderAnimated;
       
  1417     iconHeader.iMaskDepth = EColor16M;
       
  1418 
       
  1419     // Generate a unique filename for the temporary mif file.
       
  1420     // During the execution of the app, there cannot be 2 svg 
       
  1421     // images loaded from the same mif file due to caching.
       
  1422     aGeneratedFile.Append(KTempMifFileDrive);
       
  1423     TTime now;
       
  1424     if (now.UniversalTimeSecure() != KErrNone)
       
  1425     {
       
  1426         now.UniversalTime();
       
  1427     }
       
  1428     TInt64 seed = now.Int64();
       
  1429     aGeneratedFile.AppendNum(iApplicationUid, EHex); // the app uid
       
  1430     aGeneratedFile.AppendNum(seed, EHex);            // universal time
       
  1431     aGeneratedFile.AppendNum(Math::Rand(seed), EHex);// random number
       
  1432     aGeneratedFile.Append(KTempMifFileExt);
       
  1433 
       
  1434     // Create MIFConverter class
       
  1435     CMifConverter* mifConverter = CMifConverter::NewL(iCoeEnv->FsSession(), aGeneratedFile);
       
  1436     CleanupStack::PushL(mifConverter);
       
  1437 
       
  1438     // Write mif file header
       
  1439     mifConverter->WriteMifFileHeaderL(fileHeader);
       
  1440     
       
  1441     // Insert 2 iconOffset elements: first for the image, the other for the mask
       
  1442     mifConverter->WriteMifIconOffsetL(iconOffset);
       
  1443     mifConverter->WriteMifIconOffsetL(iconOffset);
       
  1444     mifConverter->WriteMifIconHeaderL(iconHeader);
       
  1445 
       
  1446     // Write mif file body
       
  1447     mifConverter->WriteMifBodyL(aSvgBuf);
       
  1448 
       
  1449     // Cleanup
       
  1450     CleanupStack::PopAndDestroy(mifConverter);
       
  1451 }
       
  1452 
       
  1453 HBufC8* ASwtDisplayBase::LoadFileL(const TDesC& aFileName)
       
  1454 {
       
  1455     RFile file;
       
  1456     User::LeaveIfError(file.Open(iCoeEnv->FsSession(), aFileName, EFileRead));
       
  1457     TInt size;
       
  1458     User::LeaveIfError(file.Size(size));
       
  1459     HBufC8* buf = HBufC8::NewLC(size);
       
  1460     TPtr8 ptr(buf->Des());
       
  1461     User::LeaveIfError(file.Read(ptr, size));
       
  1462     CleanupStack::Pop(buf);
       
  1463     file.Close();
       
  1464     return buf;
       
  1465 }
       
  1466 
       
  1467 CFbsBitmap* ASwtDisplayBase::RasterizeL(const CFbsBitmap& aMifBmp)
       
  1468 {
       
  1469     CFbsBitmap* bmp = new(ELeave) CFbsBitmap;
       
  1470     CleanupStack::PushL(bmp);
       
  1471     User::LeaveIfError(bmp->Create(aMifBmp.SizeInPixels(), aMifBmp.DisplayMode()));
       
  1472     CFbsBitmapDevice* dev = CFbsBitmapDevice::NewL(bmp);
       
  1473     CleanupStack::PushL(dev);
       
  1474     CFbsBitGc* gc = NULL;
       
  1475     User::LeaveIfError(dev->CreateContext(gc));
       
  1476     gc->BitBlt(TPoint(), &aMifBmp);
       
  1477     delete gc;
       
  1478     CleanupStack::PopAndDestroy(dev);
       
  1479     CleanupStack::Pop(bmp);
       
  1480     return bmp;
       
  1481 }
       
  1482