imageeditor/plugins/ClipartPlugin/src/ClipartSelectionDialog.cpp
changeset 1 edfc90759b9f
equal deleted inserted replaced
0:57d4cdd99204 1:edfc90759b9f
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - Initial contribution
       
    11 *
       
    12 * Contributors:
       
    13 * Ixonos Plc
       
    14 *
       
    15 * Description: 
       
    16 * Dialog for clipart plugin.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #include <AknsDrawUtils.h>
       
    22 #include <eikmover.h> 
       
    23 #include <aknview.h>
       
    24 #include <aknlists.h> 
       
    25 #include <barsread.h>
       
    26 #include <akniconarray.h> 
       
    27 #include <gulicon.h>
       
    28 #include <bautils.h>
       
    29 #include <coecobs.h>
       
    30 #include <uikon.hrh>
       
    31 #include <ConeResLoader.h> 
       
    32 #include <BitmapTransforms.h>
       
    33 #include <eikprogi.h>
       
    34 #include <s32file.h>
       
    35 #include <AknLayoutScalable_Apps.cdl.h>
       
    36 #include <layoutmetadata.cdl.h>
       
    37 #include <aknslistboxbackgroundcontrolcontext.h>
       
    38 
       
    39 #include "ClipartSelectionGrid.h"
       
    40 #include "ClipartScaler.h"
       
    41 #include "Callback.h"
       
    42 #include "Clipart.hrh"
       
    43 #include "ClipartSelectionDialog.h"
       
    44 #include "ResolutionUtil.h"
       
    45 #include "ImageEditorUI.hrh"
       
    46 #include "iepb.h"
       
    47 #include "ImageEditorError.h"
       
    48 #include <Clipart.rsg>
       
    49 
       
    50 // debug log
       
    51 //#include "ImageEditorClipartLogFile.h"
       
    52 #include "imageeditordebugutils.h"
       
    53 
       
    54 _LIT(KClipartPluginLogFile,"ClipartPlugin.log");
       
    55 
       
    56 // Constants 
       
    57 const TInt KDefaultNumberOfCliparts = 51;
       
    58 
       
    59 const TInt KStandardClipartGridCellWidth = 40;
       
    60 const TInt KStandardClipartGridCellHeight = 30;
       
    61 
       
    62 const TInt KQVGAClipartGridCellWidth = 55;
       
    63 const TInt KQVGAClipartGridCellHeight = 46;
       
    64 
       
    65 const TInt KDoubleClipartGridCellWidth = 80;
       
    66 const TInt KDoubleClipartGridCellHeight = 60;
       
    67 
       
    68 const TInt KVGAClipartGridCellWidth = 95;
       
    69 const TInt KVGAClipartGridCellHeight = 60;
       
    70 
       
    71 const TInt KQHDClipartGridCellWidth = 75;
       
    72 const TInt KQHDClipartGridCellHeight = 65;
       
    73 
       
    74 const TInt KClipartGridCellGap = 0;
       
    75 
       
    76 //const TInt KScrollBarWidth = 3;
       
    77 
       
    78 const TInt KProgressInterval = 5;
       
    79 
       
    80 _LIT (KClipartResourceFile, "clipart.rsc");
       
    81 _LIT (KClipartFileWild, "*.mbm");
       
    82 _LIT( KClipartExternalizeFile, "c:\\private\\101FFA91\\ClipartStorage.ini");
       
    83 
       
    84 
       
    85 //=============================================================================
       
    86 CClipartSelectionDialog::CClipartSelectionDialog(TFileName& aClipartFileName, TInt& aClipartFileNameIndex)
       
    87  : CEikDialog()
       
    88  , iGrid(NULL)
       
    89  , iClipartFileName(aClipartFileName)
       
    90  , iClipartFileNameIndex(aClipartFileNameIndex)
       
    91  , iIconArray(NULL)
       
    92  , iTextArray(NULL)
       
    93  , iProgressDialog(NULL)
       
    94  , iClipartScaler(NULL)
       
    95  , iCallback(NULL)
       
    96  , iCurrentBitmap(NULL)
       
    97  , iCurrentMask(NULL)
       
    98  , iCurrentIcon(0)
       
    99  , iState(EIdle)
       
   100  , iBusy(ETrue)
       
   101  , iDoubleClick(EFalse)
       
   102  , iClick(EFalse)
       
   103     {
       
   104 
       
   105     }
       
   106 //=============================================================================
       
   107 CClipartSelectionDialog::~CClipartSelectionDialog()
       
   108     {
       
   109     if (iCallback)
       
   110     	{
       
   111         iCallback->Cancel();
       
   112     	}
       
   113     delete iCallback;
       
   114 
       
   115     if (iClipartScaler)
       
   116     	{
       
   117         iClipartScaler->Cancel();
       
   118     	}
       
   119     delete iClipartScaler;
       
   120     
       
   121     if (iIconArray)
       
   122     	{
       
   123     	delete iIconArray;
       
   124     	iIconArray = NULL;
       
   125     	}
       
   126     if (iTextArray)
       
   127     	{
       
   128     	delete iTextArray;	
       
   129     	iTextArray = NULL;
       
   130 	    }
       
   131     
       
   132     iClipartList.Close();    
       
   133     delete iCurrentBitmap;
       
   134     delete iCurrentMask;
       
   135     delete iProgressDialog;
       
   136     
       
   137     delete iGrid;
       
   138     }
       
   139 
       
   140 
       
   141 //=============================================================================
       
   142 void CClipartSelectionDialog::ConstructL(MCoeControlObserver* aParent)
       
   143 {
       
   144 	
       
   145 	SetObserver(aParent);
       
   146 	
       
   147     iClipartScaler = new (ELeave) CClipartScaler(this);
       
   148     iClipartScaler->ConstructL();
       
   149 
       
   150     // Callback
       
   151     iCallback = CObCallback::NewL(this);
       
   152 
       
   153     // Create icon and text arrays
       
   154     iIconArray = new(ELeave) CAknIconArray(KDefaultNumberOfCliparts);
       
   155 	iTextArray = new(ELeave) CDesC16ArrayFlat(KDefaultNumberOfCliparts);
       
   156       
       
   157     TInt screenMode = CResolutionUtil::Self()->GetScreenMode();
       
   158 
       
   159     switch (screenMode)
       
   160     {
       
   161 
       
   162     case CResolutionUtil::EDouble:
       
   163     case CResolutionUtil::EDoubleFullScreen:
       
   164     case CResolutionUtil::EDoubleLandscape:
       
   165     case CResolutionUtil::EDoubleLandscapeFullScreen:
       
   166         {
       
   167         iClipartGridCellWidth = KDoubleClipartGridCellWidth;
       
   168         iClipartGridCellHeight = KDoubleClipartGridCellHeight;
       
   169         break;
       
   170         }
       
   171 
       
   172     case CResolutionUtil::EQVGA:
       
   173     case CResolutionUtil::EQVGAFullScreen:
       
   174     case CResolutionUtil::EQVGALandscape:
       
   175     case CResolutionUtil::EQVGALandscapeFullScreen:
       
   176         {
       
   177         iClipartGridCellWidth = KQVGAClipartGridCellWidth;
       
   178         iClipartGridCellHeight = KQVGAClipartGridCellHeight;
       
   179         break;
       
   180         }
       
   181     
       
   182     case CResolutionUtil::EVGA:
       
   183     case CResolutionUtil::EVGAFullScreen:
       
   184     case CResolutionUtil::EVGALandscape:
       
   185     case CResolutionUtil::EVGALandscapeFullScreen:
       
   186         {
       
   187         iClipartGridCellWidth = KVGAClipartGridCellWidth;
       
   188         iClipartGridCellHeight = KVGAClipartGridCellHeight;
       
   189         break;
       
   190         }
       
   191         
       
   192     case CResolutionUtil::EQHD:
       
   193     case CResolutionUtil::EQHDFullScreen:
       
   194     case CResolutionUtil::EQHDLandscape:
       
   195     case CResolutionUtil::EQHDLandscapeFullScreen:
       
   196         {
       
   197         iClipartGridCellWidth = KQHDClipartGridCellWidth;
       
   198         iClipartGridCellHeight = KQHDClipartGridCellHeight;
       
   199         break;
       
   200         }
       
   201     default:
       
   202         {
       
   203         iClipartGridCellWidth = KStandardClipartGridCellWidth;
       
   204         iClipartGridCellHeight = KStandardClipartGridCellHeight;
       
   205         break;
       
   206         }
       
   207     }
       
   208 
       
   209     // Check that if externalize file exists
       
   210     RFs& fs = ControlEnv()->FsSession();
       
   211     BaflUtils::EnsurePathExistsL( fs, KClipartExternalizeFile() );
       
   212    
       
   213     if( BaflUtils::FileExists(fs, KClipartExternalizeFile()) )
       
   214     {
       
   215         FindClipartsL();
       
   216     
       
   217         // Internalize icons if externalize file exists
       
   218         TRAPD(err,
       
   219         InternalizeIconArrayL();
       
   220         );
       
   221 
       
   222         if (err)
       
   223             {
       
   224             // if error delete externalization file
       
   225             BaflUtils::DeleteFile(
       
   226                 ControlEnv()->FsSession(),
       
   227                 KClipartExternalizeFile()
       
   228                 );
       
   229 
       
   230             User::Leave(err);
       
   231             }
       
   232  
       
   233         iCallback->DoCallback(0);
       
   234     }
       
   235     else
       
   236     {
       
   237     
       
   238         // Find Cliparts
       
   239         FindClipartsL();
       
   240 
       
   241         // Create progress note dialog
       
   242         iProgressDialog = new ( ELeave ) CAknProgressDialog( reinterpret_cast<CEikDialog**>(&iProgressDialog) );
       
   243         iProgressDialog->PrepareLC(R_CLIPART_PROGRESS_NOTE);
       
   244         iProgressDialog->GetProgressInfoL()->SetFinalValue( iClipartList.Count() );
       
   245         iProgressDialog->SetCallback ( (MProgressDialogCallback *) this);
       
   246         iProgressDialog->RunLD();
       
   247 
       
   248         // Load clipart bitmaps for the grid - asynchronous method
       
   249         LoadBitmapsL();
       
   250 
       
   251     }
       
   252     
       
   253     
       
   254 }
       
   255 
       
   256 //============================================================================= 
       
   257 void  CClipartSelectionDialog::PreLayoutDynInitL () 
       
   258 {
       
   259    
       
   260 	CAknGrid* grid = (CAknGrid*)ControlOrNull(EClipartSelectionGrid);
       
   261 
       
   262     // Set icon array to grid
       
   263     grid->ItemDrawer()->FormattedCellData()->SetIconArray(iIconArray);
       
   264 	iIconArray = NULL; // ownership moved
       
   265 
       
   266     grid->Model()->SetItemTextArray(iTextArray);
       
   267 	iTextArray = NULL;  // ownership moved
       
   268 
       
   269 	TRect clientRect;
       
   270 	CResolutionUtil::Self()->GetClientRect(clientRect);
       
   271 	
       
   272 	grid->SetRect(clientRect);
       
   273     
       
   274     TInt xOffset( 0 );  
       
   275     TAknWindowLineLayout lineLayout;    
       
   276     TAknLayoutRect layoutRect;    
       
   277     TInt variety( 0 );
       
   278     TRect scrollPaneRect;
       
   279 	if( Layout_Meta_Data::IsLandscapeOrientation() )
       
   280     	{
       
   281      	variety = 1;
       
   282     	}
       
   283     	
       
   284     if( AknLayoutUtils::LayoutMirrored() )
       
   285         {
       
   286 	    // scroll_pane_cp029
       
   287 	    lineLayout =
       
   288             AknLayoutScalable_Apps::scroll_pane_cp029( variety ).LayoutLine();
       
   289         layoutRect.LayoutRect( Rect(), lineLayout );
       
   290         scrollPaneRect = layoutRect.Rect();
       
   291         xOffset = scrollPaneRect.Width() + 5;
       
   292         }
       
   293     
       
   294     TInt rows = clientRect.Height() / iClipartGridCellHeight;
       
   295     TInt columns = (clientRect.Width() - scrollPaneRect.Width()) / iClipartGridCellWidth;
       
   296     
       
   297     AknListBoxLayouts::SetupGridPos( *grid, 
       
   298                                      xOffset, 
       
   299                                      0, 
       
   300                                      -1, 
       
   301                                      -1, 
       
   302                                      iClipartGridCellWidth, 
       
   303                                      iClipartGridCellHeight );
       
   304                 
       
   305     //  Set grid layout
       
   306     grid->SetLayoutL (
       
   307         EFalse, 
       
   308         ETrue, 
       
   309         ETrue, 
       
   310         columns, 
       
   311         rows, 
       
   312         TSize(iClipartGridCellWidth, iClipartGridCellHeight)
       
   313         );
       
   314     
       
   315     grid->SetPrimaryScrollingType (CAknGridView::EScrollIncrementLineAndLoops);
       
   316     grid->SetSecondaryScrollingType (CAknGridView::EScrollIncrementLineAndLoops);
       
   317 
       
   318     // Set gaps between items
       
   319     ((CAknGridView*)grid->View())->
       
   320         SetSpacesBetweenItems(TSize(KClipartGridCellGap,KClipartGridCellGap));
       
   321     
       
   322     grid->SetCurrentDataIndex (0);		
       
   323 
       
   324 	SetupGrid();
       
   325 	
       
   326     // Set scroll bar
       
   327     grid->CreateScrollBarFrameL(ETrue, EFalse);
       
   328     grid->ScrollBarFrame()->
       
   329        SetScrollBarVisibilityL(CEikScrollBarFrame::EAuto, CEikScrollBarFrame::EAuto);
       
   330 
       
   331     // Inform list box about new icons
       
   332     grid->HandleItemAdditionL();
       
   333 	
       
   334     grid->SetListBoxObserver(this);
       
   335     TBool iLandscape = CResolutionUtil::Self()->GetLandscape();
       
   336     if (iLandscape)
       
   337     {
       
   338     grid->ItemDrawer()->ColumnData()->SetSkinEnabledL(ETrue);
       
   339     CAknsListBoxBackgroundControlContext* context = static_cast<CAknsListBoxBackgroundControlContext*>(grid->ItemDrawer()->ColumnData()->SkinBackgroundContext());
       
   340     TRect bgRect = clientRect;
       
   341            
       
   342     TRect spRect; 
       
   343     AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EBatteryPane, spRect);
       
   344           
       
   345     bgRect.iTl.iY-=spRect.Height();
       
   346     context->SetRect(bgRect);
       
   347     context->SetBitmap( KAknsIIDQsnBgAreaMainFlat );
       
   348     
       
   349     } 
       
   350     // Reset busy flag
       
   351     SetBusy(EFalse);
       
   352 	
       
   353 }
       
   354 
       
   355 //============================================================================= 
       
   356 void CClipartSelectionDialog::SetSizeAndPosition( const TSize& aSize)
       
   357 {
       
   358 	CEikDialog::SetSizeAndPosition(aSize);
       
   359 
       
   360 }
       
   361 
       
   362 //=============================================================================
       
   363 TBool CClipartSelectionDialog::OkToExitL( TInt aButtonId )
       
   364     {
       
   365     if ( aButtonId == EAknSoftkeyOk || aButtonId == EAknSoftkeySelect)
       
   366         {
       
   367         ClipartFilenameL(iClipartFileName, iClipartFileNameIndex);
       
   368         return ETrue;
       
   369         }
       
   370     else
       
   371         {
       
   372         return EFalse;
       
   373         }
       
   374     }
       
   375 
       
   376 //=============================================================================
       
   377 
       
   378 TBool CClipartSelectionDialog::CallbackMethodL ( TInt /*aParam*/ )
       
   379 {
       
   380 	LOG(KClipartPluginLogFile, "CClipartSelectionDialog::CallbackMethodL()");
       
   381 
       
   382     // Report that the grid is ready
       
   383     ReportEventL(MCoeControlObserver::EEventStateChanged);
       
   384 
       
   385     return EFalse;
       
   386 }
       
   387 
       
   388 //=============================================================================
       
   389 TKeyResponse CClipartSelectionDialog::OfferKeyEventL (
       
   390     const TKeyEvent &   aKeyEvent,
       
   391     TEventCode          aType
       
   392     )
       
   393 {
       
   394 	TKeyResponse result = EKeyWasNotConsumed;
       
   395     if (
       
   396         ( aKeyEvent.iCode == EKeyEnter )
       
   397        )
       
   398         {
       
   399         TryExitL( EAknSoftkeyOk );
       
   400         result = EKeyWasConsumed;
       
   401         }
       
   402     else
       
   403         {
       
   404         result = CEikDialog::OfferKeyEventL( aKeyEvent, aType );
       
   405         }
       
   406     return result;
       
   407 
       
   408 }
       
   409 
       
   410 //=============================================================================
       
   411 void CClipartSelectionDialog::FindClipartsL()
       
   412 {
       
   413 	LOG(KClipartPluginLogFile, "CClipartSelectionGrid::FindClipartsL()");
       
   414 
       
   415     //	Read resource
       
   416 	TFileName resourcefile;
       
   417 	resourcefile.Append(KPgnResourcePath);
       
   418 	resourcefile.Append(KClipartResourceFile);
       
   419     User::LeaveIfError( CompleteWithAppPath( resourcefile ) );
       
   420 
       
   421     //  Get reference to application file session
       
   422     RFs & fs = ControlEnv()->FsSession();
       
   423     
       
   424     // Implementation of RConeResourceLoader uses BaflUtils::NearestLanguageFile 
       
   425     // to search for a localised resource in proper search order
       
   426     RConeResourceLoader resLoader( *ControlEnv() );
       
   427     resLoader.OpenL ( resourcefile );
       
   428     CleanupClosePushL(resLoader);
       
   429 
       
   430     CDesCArrayFlat* array = 
       
   431         ControlEnv()->ReadDesCArrayResourceL(R_CLIPART_DIRS);
       
   432     CleanupStack::PushL(array);
       
   433 
       
   434     TBuf<256> pathList;
       
   435     _LIT(KPathSeparator, ";");
       
   436 
       
   437     for (TInt i = 0; i < array->Count(); ++i)
       
   438         {
       
   439         pathList.Append((*array)[i]);
       
   440         pathList.Append(KPathSeparator);
       
   441         }
       
   442     CleanupStack::PopAndDestroy(2); // resLoader, array
       
   443 
       
   444 	//  Create a file finder
       
   445     TFindFile fileFinder (fs); 
       
   446     CDir * fileList = NULL; 
       
   447 
       
   448     //  Find files by wild card and directory
       
   449     TInt err = fileFinder.FindWildByPath (
       
   450 		KClipartFileWild, 
       
   451 		&pathList, 
       
   452 		fileList
       
   453 		);
       
   454 
       
   455 	LOGFMT(KClipartPluginLogFile, "CClipartSelectionGrid::FindClipartsL: found %d cliparts", fileList->Count());
       
   456 
       
   457     // Trace leave
       
   458     if (err)
       
   459         {
       
   460 		LOGFMT(KClipartPluginLogFile, "CClipartSelectionGrid::FindClipartsL: FindWildByPath returned %d. Leaving...", err);
       
   461         User::LeaveIfError(KSIEEInternal);
       
   462         }
       
   463 
       
   464 	//	Go through files
       
   465     while (err == KErrNone)
       
   466     {
       
   467         CleanupStack::PushL (fileList);
       
   468 
       
   469 		//	Check all plug-in candidates
       
   470         for (TInt i = 0; i < fileList->Count(); ++i) 
       
   471         {
       
   472 
       
   473             //	Create a full file name for a MBM file
       
   474             TParse fullentry;
       
   475             fullentry.Set (
       
   476 				(*fileList)[i].iName,&
       
   477                 fileFinder.File(), 
       
   478 				0
       
   479 				); 
       
   480             TPtrC fullname (fullentry.FullName());
       
   481 
       
   482             //  Check the amount of frames 
       
   483             TInt err_bmp = KErrNone;
       
   484             TInt j = 0;
       
   485             
       
   486             while (err_bmp == KErrNone)
       
   487             {
       
   488                 // Try to load next bitmap. 
       
   489                 // KErrNotFound if no more bitmaps
       
   490                 CFbsBitmap* bitmap = new (ELeave) CFbsBitmap;
       
   491                 CleanupStack::PushL (bitmap);
       
   492                 err_bmp = bitmap->Load (fullname, j);
       
   493 
       
   494                 CFbsBitmap* bitmapMask = new (ELeave) CFbsBitmap;
       
   495                 CleanupStack::PushL (bitmapMask);
       
   496                 TInt err_mask = bitmapMask->Load (fullname, j + 1);
       
   497                 
       
   498                 if (err_bmp == KErrNone && err_mask != KErrNone)
       
   499                     {
       
   500                     User::Leave(KSIEEInternal);
       
   501                     }
       
   502 
       
   503                 if (err_bmp == KErrNone)
       
   504                 {
       
   505  
       
   506                     // Add filename-index pair to the clipart list
       
   507                     TClipartListItem item;
       
   508                     item.iFilename = fullname;
       
   509                     item.iIndex = j;
       
   510                     iClipartList.Append(item);
       
   511 
       
   512                 }
       
   513                 CleanupStack::PopAndDestroy(2); // bitmap, mask
       
   514 
       
   515                 j += 2;
       
   516             }
       
   517         }	
       
   518     
       
   519 		CleanupStack::PopAndDestroy(); /// fileList
       
   520 
       
   521 		//	Try once again
       
   522         err = fileFinder.FindWild (fileList); 
       
   523 
       
   524     }
       
   525 
       
   526 }
       
   527 
       
   528 
       
   529 //=============================================================================
       
   530 void CClipartSelectionDialog::LoadBitmapsL()
       
   531     {
       
   532     LOG(KClipartPluginLogFile, "CClipartSelectionGrid::LoadBitmapsL()");
       
   533 
       
   534     // Create and load new one
       
   535     iCurrentBitmap = new (ELeave) CFbsBitmap;
       
   536     User::LeaveIfError(iCurrentBitmap->Load(
       
   537         iClipartList[iCurrentIcon].iFilename, 
       
   538         iClipartList[iCurrentIcon].iIndex));
       
   539 
       
   540     // Create and load new one
       
   541     iCurrentMask = new (ELeave) CFbsBitmap;
       
   542     User::LeaveIfError(iCurrentMask->Load(
       
   543         iClipartList[iCurrentIcon].iFilename, 
       
   544         iClipartList[iCurrentIcon].iIndex + 1));
       
   545 
       
   546     // Scale bitmap to grid size
       
   547     iState = EScalingBitmap;
       
   548     iClipartScaler->ScaleBitmapL(
       
   549         iCurrentBitmap,
       
   550         TSize(iClipartGridCellWidth, iClipartGridCellHeight));
       
   551     }
       
   552 
       
   553 //=============================================================================
       
   554 void CClipartSelectionDialog::AddIconL()
       
   555     {
       
   556 	LOG(KClipartPluginLogFile, "CClipartSelectionGrid::AddIconL()");
       
   557     // Create icon
       
   558     CGulIcon* icon = NULL;
       
   559     icon = CGulIcon::NewLC();
       
   560     icon->SetBitmapsOwnedExternally (EFalse);
       
   561 
       
   562     // Set bitmap to icon and append icon to icon array
       
   563     icon->SetBitmap (iCurrentBitmap);
       
   564     iCurrentBitmap = NULL;
       
   565     icon->SetMask(iCurrentMask);
       
   566     iCurrentMask = NULL;
       
   567     iIconArray->AppendL(icon);
       
   568 
       
   569     CleanupStack::Pop(); // icon
       
   570 
       
   571     //  Create grid item text
       
   572     TBuf<8> buf;
       
   573     buf.AppendNum (iCurrentIcon);
       
   574     buf.Append(_L("\t1"));
       
   575     iTextArray->AppendL (buf);
       
   576     
       
   577     // Increase current icon index
       
   578     iCurrentIcon++;
       
   579 
       
   580     // Load next icon
       
   581     // one step begins.
       
   582     if (iProgressDialog && (iCurrentIcon % KProgressInterval) == 0)
       
   583         {
       
   584         iProgressDialog->GetProgressInfoL()->IncrementAndDraw (KProgressInterval);
       
   585         }
       
   586     if (iCurrentIcon < iClipartList.Count())
       
   587         {
       
   588    
       
   589         LoadBitmapsL();
       
   590         }
       
   591     else
       
   592         {
       
   593 
       
   594         // Externalize loaded icons
       
   595         TRAPD(err,
       
   596             ExternalizeIconArrayL();
       
   597         );
       
   598         if (err)
       
   599             {
       
   600             // if error delete externalization file
       
   601             BaflUtils::DeleteFile(
       
   602                 ControlEnv()->FsSession(),
       
   603                 KClipartExternalizeFile()
       
   604                 );
       
   605             }
       
   606         // dismiss progress dialog
       
   607         if (iProgressDialog)
       
   608             {
       
   609             iProgressDialog->ProcessFinishedL();
       
   610             }
       
   611 
       
   612         SetBusy(EFalse);
       
   613 
       
   614         // Report that the grid is ready
       
   615         ReportEventL(MCoeControlObserver::EEventStateChanged);
       
   616         }
       
   617     }
       
   618 
       
   619 
       
   620 //=============================================================================
       
   621 void CClipartSelectionDialog::ClipartScalerOperationReadyL(TInt aError)
       
   622     {
       
   623     if (aError == KErrNone)
       
   624         {
       
   625         
       
   626         switch (iState)
       
   627             {
       
   628             case EScalingBitmap:
       
   629                 {
       
   630                 // Scale the mask bitmap
       
   631                 iState = EScalingMask;
       
   632                 iClipartScaler->ScaleBitmapL(
       
   633                     iCurrentMask,
       
   634                     TSize(iClipartGridCellWidth, iClipartGridCellHeight));
       
   635                 break;
       
   636                 }
       
   637             case EScalingMask:
       
   638                 {
       
   639                 iState = EIdle;
       
   640                 // Add icon to grid
       
   641                 AddIconL();
       
   642                 break;
       
   643                 }
       
   644             default:
       
   645                 {
       
   646 				break;
       
   647                 }
       
   648             }
       
   649         }
       
   650     else
       
   651         {
       
   652         User::Leave(KSIEEInternal);
       
   653         }
       
   654     }
       
   655 
       
   656 //=============================================================================
       
   657 void CClipartSelectionDialog::DialogDismissedL( TInt aButtonId )
       
   658 {
       
   659 	LOGFMT(KClipartPluginLogFile, "CClipartSelectionGrid::DialogDismissedL(%d)", aButtonId);
       
   660 
       
   661 	CAknGrid* grid = (CAknGrid*)ControlOrNull(EClipartSelectionGrid);
       
   662 
       
   663     if( aButtonId == EAknSoftkeyCancel )
       
   664     {
       
   665         // Cancel the on-going image decoding
       
   666         iClipartScaler->Cancel();
       
   667 
       
   668 //		grid->HandleItemAdditionL();
       
   669 
       
   670         // Report cancel request
       
   671         ReportEventL(MCoeControlObserver::EEventRequestCancel);
       
   672     }
       
   673 
       
   674 }
       
   675 
       
   676 //=============================================================================
       
   677 void CClipartSelectionDialog::ClipartFilenameL(TDes& aFilename, TInt& aIndex)
       
   678     {
       
   679 	CAknGrid* grid = (CAknGrid*)ControlOrNull(EClipartSelectionGrid);
       
   680 
       
   681     // Get MBM file and icon index of the selected clip-art
       
   682     aFilename = iClipartList[grid->CurrentItemIndex()].iFilename;
       
   683     aIndex = iClipartList[grid->CurrentItemIndex()].iIndex;
       
   684     }
       
   685 
       
   686 //=============================================================================
       
   687 void CClipartSelectionDialog::SetupGrid ()
       
   688 {
       
   689 
       
   690 	CAknGrid* grid = (CAknGrid*)ControlOrNull(EClipartSelectionGrid);
       
   691 
       
   692 	LOG(KClipartPluginLogFile, "CClipartSelectionGrid::SetupGrid()");
       
   693 
       
   694 	// Setup text foreground and background colors to default
       
   695 	AknListBoxLayouts::SetupStandardGrid (*grid);
       
   696     
       
   697 	// Get local copies of data we will need	
       
   698 	CFormattedCellListBoxItemDrawer * itemDrawer = grid->ItemDrawer();
       
   699 	    
       
   700     TInt cell_w = grid->ColumnWidth(); 
       
   701 	TInt cell_h = grid->ItemHeight();
       
   702 	
       
   703 	// Set up graphics sub-cells
       
   704 	AknListBoxLayouts::SetupGridFormGfxCell (
       
   705 		*grid,                  // the grid
       
   706 		itemDrawer,				// the grid's drawer
       
   707 		0,						// index of the graphic within item strings
       
   708 		0,						// left position
       
   709 		0,						// top position
       
   710 		0,						// right - unused
       
   711 		0,						// bottom - unused
       
   712 		cell_w, 				// width of graphic
       
   713 		cell_h,					// height of graphic
       
   714 		TPoint (0, 0),			// start position
       
   715 		TPoint (cell_w, cell_h)	// end position
       
   716 		);
       
   717 
       
   718 }
       
   719 
       
   720 //=============================================================================
       
   721 void CClipartSelectionDialog::InternalizeIconArrayL()
       
   722 {
       
   723 	LOG(KClipartPluginLogFile, "CClipartSelectionGrid::InternalizeIconArrayL()");
       
   724 
       
   725     //	Internalize clipart icons
       
   726     RFileReadStream stream;
       
   727 
       
   728     User::LeaveIfError (
       
   729         stream.Open (
       
   730         ControlEnv()->FsSession(),
       
   731         KClipartExternalizeFile(),
       
   732         EFileRead | EFileShareReadersOnly
       
   733         ));
       
   734     stream.PushL();
       
   735 
       
   736     CGulIcon* icon = NULL;
       
   737     CFbsBitmap* bitmap = NULL;
       
   738 
       
   739     TInt count = stream.ReadInt32L();
       
   740     for ( TInt i=0; i < count; i++ )
       
   741     {                
       
   742         icon = CGulIcon::NewLC();
       
   743         icon->SetBitmapsOwnedExternally (EFalse);
       
   744     
       
   745         // Text index
       
   746         TInt len = stream.ReadInt32L();
       
   747         HBufC* iconText = HBufC::NewLC(len);
       
   748         TPtr iconTextPtr = iconText->Des();
       
   749         stream >> iconTextPtr;
       
   750 
       
   751         iTextArray->AppendL(iconTextPtr);
       
   752 
       
   753         CleanupStack::PopAndDestroy(); // iconText
       
   754 
       
   755         // Bitmap Data
       
   756 
       
   757         bitmap = new(ELeave) CFbsBitmap;
       
   758         CleanupStack::PushL(bitmap);
       
   759 
       
   760         stream >> *bitmap;
       
   761 
       
   762         // Save data
       
   763         //TBuf<20> name;
       
   764         //name.Format(_L("bitmap%d.mbm"), i);
       
   765         //bitmap->Save(name);
       
   766 
       
   767         // Set bitmap to icon - Ownership is transfered
       
   768         icon->SetBitmap (bitmap);
       
   769 
       
   770         CleanupStack::Pop();
       
   771 
       
   772         bitmap = new(ELeave) CFbsBitmap;
       
   773         CleanupStack::PushL(bitmap);
       
   774 
       
   775         stream >> *bitmap;
       
   776 
       
   777         //TBuf<20> name2;
       
   778         //name2.Format(_L("mask%d.mbm"), i);
       
   779         //bitmap->Save(name2);
       
   780 
       
   781         // Set bitmap to icon - Ownership transfers
       
   782         icon->SetMask(bitmap);
       
   783 
       
   784         CleanupStack::Pop();
       
   785         bitmap = NULL;
       
   786 
       
   787         // Set icon to icon array
       
   788         iIconArray->AppendL(icon);
       
   789     
       
   790         CleanupStack::Pop(); // icon
       
   791         icon = NULL;
       
   792 
       
   793     } 
       
   794 
       
   795     CleanupStack::PopAndDestroy(); // stream
       
   796 
       
   797 }
       
   798 
       
   799 
       
   800 //=============================================================================
       
   801 void CClipartSelectionDialog::ExternalizeIconArrayL()
       
   802 {
       
   803 	LOG(KClipartPluginLogFile, "CClipartSelectionGrid::ExternalizeIconArrayL()");
       
   804 
       
   805     RFileWriteStream stream;
       
   806 
       
   807     User::LeaveIfError (
       
   808         stream.Replace (
       
   809         ControlEnv()->FsSession(),
       
   810         KClipartExternalizeFile(),
       
   811         EFileWrite
       
   812         ));
       
   813     stream.PushL();
       
   814 
       
   815     // Externalize the contents of clipart
       
   816     stream.WriteInt32L( iTextArray->Count() );
       
   817 
       
   818     for ( TInt i=0; i< iTextArray->Count(); i++ )
       
   819     {
       
   820         // Text index 
       
   821         TPtrC text = (*iTextArray)[i];
       
   822         stream.WriteInt32L(text.Length());
       
   823         stream << text;
       
   824 
       
   825         // Bitmap 
       
   826         CFbsBitmap* bitmap = (*iIconArray)[i]->Bitmap();
       
   827         stream << *bitmap;
       
   828         bitmap = NULL;
       
   829 
       
   830         // Mask
       
   831         bitmap = (*iIconArray)[i]->Mask();
       
   832         stream << *bitmap;
       
   833         bitmap = NULL;
       
   834         
       
   835     }
       
   836 
       
   837     // Write extra before commiting 
       
   838     stream.WriteInt32L( 0 );
       
   839 
       
   840     stream.CommitL();
       
   841 
       
   842     CleanupStack::PopAndDestroy(); // stream
       
   843 
       
   844 }
       
   845 
       
   846 //=============================================================================
       
   847 void CClipartSelectionDialog::SetBusy(TBool aBusy)
       
   848 {
       
   849     iBusy = aBusy;
       
   850 }
       
   851 
       
   852 //=============================================================================
       
   853 SEikControlInfo CClipartSelectionDialog::CreateCustomControlL(TInt aControlType)
       
   854 {
       
   855 
       
   856 	SEikControlInfo controlInfo;
       
   857 	controlInfo.iControl = NULL;
       
   858 	controlInfo.iTrailerTextId = 0;
       
   859 	controlInfo.iFlags = 0;
       
   860 
       
   861 	switch (aControlType)
       
   862     {
       
   863  	
       
   864  		case EClipartCustomControlType:
       
   865 		{
       
   866 			controlInfo.iControl = new(ELeave) CClipartSelectionGrid;
       
   867 			break;
       
   868 		}
       
   869         
       
   870         default:
       
   871 		{
       
   872 			break;
       
   873 		}
       
   874         
       
   875     }
       
   876 
       
   877 	return controlInfo;
       
   878 } 
       
   879 
       
   880 void CClipartSelectionDialog::HandleListBoxEventL(CEikListBox* /*aListBox*/, TListBoxEvent aEventType)
       
   881 {
       
   882 	if ( aEventType == EEventItemDoubleClicked )
       
   883 	{
       
   884 		iDoubleClick = ETrue;
       
   885 	}
       
   886 	// also single click is enough for selecting item
       
   887 	else if ( aEventType == EEventItemClicked )
       
   888     {
       
   889         iClick = ETrue;
       
   890     }
       
   891 }
       
   892 
       
   893 void CClipartSelectionDialog::HandleDialogPageEventL(TInt /*aEventId*/)
       
   894 {
       
   895     if( iDoubleClick || iClick )
       
   896     {
       
   897         TryExitL( EAknSoftkeyOk );
       
   898     }
       
   899 }
       
   900 
       
   901 void CClipartSelectionDialog::HandleResourceChange(TInt aType)
       
   902 {
       
   903     CEikDialog::HandleResourceChange(aType);
       
   904     if( aType == KEikDynamicLayoutVariantSwitch )
       
   905     {
       
   906         CAknGrid* grid = (CAknGrid*)ControlOrNull(EClipartSelectionGrid);
       
   907         if ( grid )
       
   908         {
       
   909         	TRect clientRect = Rect(); 
       
   910 	        grid->SetRect(clientRect);
       
   911             TInt xOffset( 0 );  
       
   912             TAknWindowLineLayout lineLayout;    
       
   913             TAknLayoutRect layoutRect; 
       
   914             TInt variety( 0 );
       
   915             TRect scrollPaneRect;
       
   916 	        if( Layout_Meta_Data::IsLandscapeOrientation() )
       
   917     	    {
       
   918      	    variety = 1;
       
   919     	    }
       
   920     	
       
   921             if( AknLayoutUtils::LayoutMirrored() )
       
   922             {
       
   923 	        // scroll_pane_cp029
       
   924 	        lineLayout =
       
   925             AknLayoutScalable_Apps::scroll_pane_cp029( variety ).LayoutLine();
       
   926             layoutRect.LayoutRect( Rect(), lineLayout );
       
   927             scrollPaneRect = layoutRect.Rect();
       
   928             xOffset = scrollPaneRect.Width() + 5;
       
   929             }
       
   930             TInt rows = clientRect.Height() / iClipartGridCellHeight;
       
   931             TInt columns = (clientRect.Width() - scrollPaneRect.Width()) / iClipartGridCellWidth;
       
   932           
       
   933             //  Set grid layout
       
   934            
       
   935             grid->SetLayoutL (
       
   936             EFalse, 
       
   937             ETrue, 
       
   938             ETrue, 
       
   939             columns, 
       
   940             rows, 
       
   941             TSize(iClipartGridCellWidth, iClipartGridCellHeight)
       
   942             );
       
   943                           
       
   944         }
       
   945     }
       
   946 }
       
   947 
       
   948 // End of File