filebrowser/src/FBFileEditor.cpp
branchRCL_3
changeset 22 fad26422216a
parent 0 d6fe6244b863
equal deleted inserted replaced
21:b3cee849fa46 22:fad26422216a
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "FBFileEditor.h"
       
    21 #include "FBStd.h"
       
    22 #include "FB.hrh"
       
    23 #include <filebrowser.rsg>
       
    24 
       
    25 #include <utf.h>
       
    26 #include <eikedwin.h>
       
    27 #include <eikrted.h>
       
    28 #include <txtrich.h>
       
    29 #include <eikspane.h>
       
    30 #include <avkon.hrh> 
       
    31 #include <akntitle.h>
       
    32 #include <AknUtils.h> 
       
    33 #include <AknBidiTextUtils.h>
       
    34 #include <aknnotewrappers.h>
       
    35 #include <eiksbfrm.h>
       
    36 #include <AknDef.h>
       
    37 #include <layoutmetadata.cdl.h>
       
    38 
       
    39 const TUint KLeftMargin = 2;
       
    40 const TUint KRightMargin = 2;
       
    41 const TUint KHexSeparatorMargin = 4;
       
    42 
       
    43 const TInt KEditorFontHeight = 117;
       
    44 const TInt KViewerFontHeight = 117;
       
    45 const TInt KEditorFontHeightQHD = 100;
       
    46 const TInt KViewerFontHeightQHD = 100;
       
    47 
       
    48 
       
    49 
       
    50 // ================= MEMBER FUNCTIONS =======================
       
    51 
       
    52 CFileBrowserFileEditorDlg* CFileBrowserFileEditorDlg::NewL(const TDesC& aFileName, TInt aMode)
       
    53     {
       
    54     CFileBrowserFileEditorDlg* self = new(ELeave) CFileBrowserFileEditorDlg(aFileName, aMode);
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop(self);
       
    58     return self;    
       
    59     }
       
    60 
       
    61 // --------------------------------------------------------------------------------------------
       
    62 
       
    63 CFileBrowserFileEditorDlg::~CFileBrowserFileEditorDlg()
       
    64     {
       
    65     if (iEditorPF)
       
    66         delete iEditorPF;
       
    67     }
       
    68 
       
    69 // --------------------------------------------------------------------------------------------
       
    70 
       
    71 CFileBrowserFileEditorDlg::CFileBrowserFileEditorDlg(const TDesC& aFileName, TInt aMode) :
       
    72     iFileName(aFileName), iActiveMode(aMode)
       
    73     {
       
    74     }
       
    75 
       
    76 // --------------------------------------------------------------------------------------------
       
    77 
       
    78 void CFileBrowserFileEditorDlg::ConstructL()
       
    79     {
       
    80     // construct a menu bar
       
    81     if (iActiveMode == EFileEditorEditAsText || iActiveMode == EFileEditorEditAsHex)
       
    82         CAknDialog::ConstructL(R_FILEBROWSER_FILEEDITOR_MENUBAR);
       
    83     else
       
    84         CAknDialog::ConstructL(R_FILEBROWSER_FILEVIEWER_MENUBAR);
       
    85     
       
    86     iTextFormat = EFileBrowserCmdFileEditorSaveFormatANSIASCII;
       
    87     }
       
    88 
       
    89 // --------------------------------------------------------------------------------------------
       
    90 
       
    91 void CFileBrowserFileEditorDlg::PreLayoutDynInitL()
       
    92     {
       
    93     CAknDialog::PreLayoutDynInitL();
       
    94     
       
    95     // set title text
       
    96     CEikStatusPane* sp = iEikonEnv->AppUiFactory()->StatusPane();
       
    97     CAknTitlePane* tp = static_cast<CAknTitlePane*>( sp->ControlL( TUid::Uid( EEikStatusPaneUidTitle ) ) );
       
    98     tp->SetTextL( _L("File editor") );
       
    99     
       
   100     
       
   101     // init edwin editor component
       
   102     if (iActiveMode == EFileEditorEditAsText || iActiveMode == EFileEditorEditAsHex)
       
   103         {
       
   104         CCoeControl* control = CreateLineByTypeL(KNullDesC, EFileEditorDialogId, EEikCtEdwin, NULL);
       
   105         iTextEditor = static_cast<CEikEdwin*>(control);
       
   106         iTextEditor->SetMopParent(this);
       
   107         iTextEditor->ConstructL(EEikEdwinOwnsWindow|EEikEdwinNoHorizScrolling|EEikEdwinInclusiveSizeFixed|EEikEdwinNoAutoSelection, 0, 0, 0 );
       
   108         iTextEditor->CreateTextViewL();
       
   109         iTextEditor->SetAknEditorFlags(EAknEditorFlagEnableScrollBars);
       
   110         iTextEditor->CreateScrollBarFrameL();
       
   111         iTextEditor->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
   112         iTextEditor->ForceScrollBarUpdateL(); 
       
   113         iTextEditor->SetBackgroundColorL(KRgbWhite);
       
   114         iTextEditor->SetFocus(ETrue);
       
   115         }
       
   116     }
       
   117 
       
   118 // --------------------------------------------------------------------------------------------
       
   119 
       
   120 void CFileBrowserFileEditorDlg::PostLayoutDynInitL()
       
   121     {
       
   122     CAknDialog::PostLayoutDynInitL();
       
   123     
       
   124     // modify layout of the texteditor
       
   125     if (iActiveMode == EFileEditorEditAsText || iActiveMode == EFileEditorEditAsHex)
       
   126         {
       
   127         CGraphicsDevice* dev = iCoeEnv->ScreenDevice();
       
   128         const TSize screenSize = dev->SizeInPixels();
       
   129         
       
   130         // Set the font of the editor
       
   131         TFontSpec fontSpec = AknLayoutUtils::FontFromId(EAknLogicalFontSecondaryFont)->FontSpecInTwips();
       
   132         fontSpec.iHeight = IsQHD(screenSize) ? KEditorFontHeightQHD : KEditorFontHeight; // set height
       
   133         
       
   134         TCharFormat cf(fontSpec.iTypeface.iName, fontSpec.iHeight);
       
   135         cf.iFontPresentation.iTextColor = KRgbBlack; 
       
   136 
       
   137         TCharFormatMask cfm;
       
   138         cfm.SetAll();
       
   139 
       
   140         CCharFormatLayer *charFL = CCharFormatLayer::NewL(cf, cfm);
       
   141         iTextEditor->SetCharFormatLayer(charFL);
       
   142 
       
   143         if (iEditorPF)
       
   144             {
       
   145             delete iEditorPF;
       
   146             iEditorPF = NULL;
       
   147             }
       
   148 
       
   149         iEditorPF = CParaFormat::NewL();
       
   150         iEditorPF->iLineSpacingInTwips = IsQHD(screenSize) ? KEditorFontHeightQHD : KEditorFontHeight;
       
   151         iEditorPF->iFillColor = KRgbWhite;
       
   152 
       
   153         TParaFormatMask pfm;
       
   154         pfm.SetAll();
       
   155 
       
   156         CParaFormatLayer *paraFL = CParaFormatLayer::NewL(iEditorPF, pfm);
       
   157         iTextEditor->SetParaFormatLayer(paraFL);
       
   158         
       
   159 
       
   160         // set size of the editor
       
   161         TRect rect = Rect();
       
   162         iTextEditor->SetRect(rect);
       
   163         //iTextEditor->Size().iWidth - (CEikScrollBar::DefaultScrollBarBreadth());
       
   164         }
       
   165 
       
   166     // load the file to editor or viewer
       
   167     LoadFileL();
       
   168     }
       
   169 
       
   170 // --------------------------------------------------------------------------------------------
       
   171 
       
   172 void CFileBrowserFileEditorDlg::OpenFileLC(RFile& aFile, const TDesC& aFileName)
       
   173     {
       
   174     TInt err = aFile.Open(CEikonEnv::Static()->FsSession(), aFileName, EFileRead|EFileShareReadersOnly);
       
   175     
       
   176     if (err == KErrInUse)
       
   177         {
       
   178         CAknErrorNote* note = new(ELeave) CAknErrorNote;
       
   179         note->ExecuteLD(_L("The file is in use, cannot open"));
       
   180         User::Leave(KErrNone);
       
   181         }
       
   182     else if (err != KErrNone)
       
   183         {
       
   184         CAknErrorNote* note = new(ELeave) CAknErrorNote;
       
   185         note->ExecuteLD(_L("Cannot open the file"));
       
   186         User::Leave(KErrNone);
       
   187         }
       
   188     else
       
   189         {
       
   190         // opened succesfully, add to cleanup stack
       
   191         CleanupClosePushL(aFile);
       
   192         }     
       
   193     }
       
   194 
       
   195 // --------------------------------------------------------------------------------------------
       
   196 
       
   197 void CFileBrowserFileEditorDlg::GetTextFileMode(RFile& aFile, TInt& aFileSize)
       
   198     {
       
   199     iTextFormat = EFileBrowserCmdFileEditorSaveFormatANSIASCII;
       
   200 
       
   201     // if we are working with text files, check the type first   
       
   202     if (iActiveMode == EFileEditorEditAsText || iActiveMode == EFileEditorViewAsText)
       
   203         {
       
   204         TBuf8<4> BOM;
       
   205 
       
   206         // first check for UTF-16
       
   207         if (aFileSize >= 2 && aFile.Read(0, BOM, 2) == KErrNone)
       
   208             {
       
   209             if (BOM.Length()==2 && BOM[0]==0xFF && BOM[1]==0xFE)
       
   210                 {
       
   211                 iTextFormat = EFileBrowserCmdFileEditorSaveFormatUTF16LE;
       
   212                 aFileSize -= 2;
       
   213                 return;
       
   214                 }
       
   215             else if (BOM.Length()==2 && BOM[0]==0xFE && BOM[1]==0xFF)
       
   216                 {
       
   217                 iTextFormat = EFileBrowserCmdFileEditorSaveFormatUTF16BE;
       
   218                 aFileSize -= 2;
       
   219                 return;
       
   220                 }
       
   221             }       
       
   222 
       
   223         // then check for UTF-8
       
   224         if (aFileSize >= 3 && aFile.Read(0, BOM, 3) == KErrNone)
       
   225             {
       
   226             if (BOM.Length()==3 && BOM[0]==0xEF && BOM[1]==0xBB && BOM[2]==0xBF)
       
   227                 {
       
   228                 iTextFormat = EFileBrowserCmdFileEditorSaveFormatUTF8;
       
   229                 aFileSize -= 3;
       
   230                 return;
       
   231                 }
       
   232             }
       
   233             
       
   234         
       
   235         // none of those, seek back to beginning
       
   236         TInt pos(0);
       
   237         aFile.Seek(ESeekStart, pos);
       
   238         }    
       
   239     }
       
   240 
       
   241 
       
   242 // --------------------------------------------------------------------------------------------
       
   243 
       
   244 HBufC16* CFileBrowserFileEditorDlg::ConvertBuf8ToBuf16L(HBufC8* aBuf8)
       
   245     {
       
   246     __ASSERT_ALWAYS(aBuf8 != NULL, User::Panic(_L("Emp.Buf."),942));
       
   247 
       
   248     HBufC16* buf16 = NULL;
       
   249     TPtr8 buf8Ptr(aBuf8->Des()); 
       
   250 
       
   251     if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatANSIASCII)
       
   252         {
       
   253         // simple conversion
       
   254         buf16 = HBufC16::NewMaxL(aBuf8->Length());
       
   255         buf16->Des().Copy(aBuf8->Des());
       
   256         }
       
   257     
       
   258     else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF8)
       
   259         {
       
   260         // use the conversion service
       
   261         buf16 = CnvUtfConverter::ConvertToUnicodeFromUtf8L(aBuf8->Des());
       
   262         }            
       
   263 
       
   264     else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16LE || iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16BE)
       
   265         {
       
   266         buf16 = HBufC16::NewL(aBuf8->Length());
       
   267         buf16->Des().Copy(KNullDesC);
       
   268         
       
   269         HBufC16* oneUniCodeChar = HBufC16::NewMaxLC(1);
       
   270         TUint16* oneUniCodeCharPtr = &oneUniCodeChar->Des()[0];
       
   271        
       
   272         for (TInt i=0; i<buf8Ptr.Length(); i+=2)
       
   273             {
       
   274             // get two 8-bit values and pack them to one 16-bit character depending of endianess
       
   275 
       
   276             if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16LE)
       
   277                 {
       
   278                 oneUniCodeCharPtr[0] = TUint16((buf8Ptr[i+1] << 8) + buf8Ptr[i]);
       
   279                 }
       
   280             else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16BE)
       
   281                 {
       
   282                 oneUniCodeCharPtr[0] = TUint16((buf8Ptr[i] << 8) + buf8Ptr[i+1]);
       
   283                 }
       
   284             
       
   285             // append to buffer
       
   286             buf16->Des().Append( oneUniCodeChar->Des() );
       
   287             }
       
   288         
       
   289         CleanupStack::PopAndDestroy(); // oneUniCodeChar
       
   290         } 
       
   291     
       
   292     return buf16; //ownership of buf16 will transferred to the caller           
       
   293     }
       
   294 
       
   295 // --------------------------------------------------------------------------------------------
       
   296 
       
   297 HBufC8* CFileBrowserFileEditorDlg::ConvertBuf16ToBuf8L(HBufC16* aBuf16)
       
   298     {
       
   299     __ASSERT_ALWAYS(aBuf16 != NULL, User::Panic(_L("Emp.Buf."),943));
       
   300 
       
   301     HBufC8* buf8 = NULL;
       
   302     TPtr16 buf16Ptr(aBuf16->Des()); 
       
   303 
       
   304     if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatANSIASCII)
       
   305         {
       
   306         // simple conversion
       
   307         buf8 = HBufC8::NewMaxL(aBuf16->Length());
       
   308         buf8->Des().Copy(aBuf16->Des());
       
   309         }
       
   310     
       
   311     else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF8)
       
   312         {
       
   313         // use the conversion service
       
   314         buf8 = CnvUtfConverter::ConvertFromUnicodeToUtf8L(aBuf16->Des());
       
   315         }            
       
   316 
       
   317     else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16LE || iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16BE)
       
   318         {
       
   319         buf8 = HBufC8::NewL(aBuf16->Length()*2);
       
   320         buf8->Des().Copy(KNullDesC);
       
   321 
       
   322         TUint16* charPtr = NULL;
       
   323         TBuf8<2> twoChars;
       
   324         twoChars.Copy(_L8("XX"));        
       
   325        
       
   326         for (TInt i=0; i<aBuf16->Length(); i++)
       
   327             {
       
   328             charPtr = &aBuf16->Des()[i];
       
   329             
       
   330             // get 16-bit character to two variables
       
   331             if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16LE)
       
   332                 {
       
   333                 twoChars[0] = TUint8(charPtr[0]);
       
   334                 twoChars[1] = TUint8(charPtr[0] << 8);
       
   335                 }
       
   336             else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16BE)
       
   337                 {
       
   338                 twoChars[0] = TUint8(charPtr[0] << 8);
       
   339                 twoChars[1] = TUint8(charPtr[0]);
       
   340                 }
       
   341 
       
   342             // append to the buffer
       
   343             buf8->Des().Append(twoChars);
       
   344 
       
   345             } // for
       
   346             
       
   347         } // else if
       
   348     
       
   349     return buf8; //ownership of buf8 will transferred to the caller           
       
   350     }                
       
   351 // --------------------------------------------------------------------------------------------
       
   352 
       
   353 void CFileBrowserFileEditorDlg::LoadFileL()
       
   354     {
       
   355     // open the file for reading
       
   356     RFile fileP;
       
   357     OpenFileLC(fileP, iFileName);
       
   358     
       
   359     // get file size
       
   360     TInt fileSize(0);
       
   361     User::LeaveIfError(fileP.Size(fileSize));
       
   362 
       
   363     // read BOM from file
       
   364     GetTextFileMode(fileP, fileSize);
       
   365 
       
   366     
       
   367     // editor related file loading
       
   368     if (iActiveMode == EFileEditorEditAsText || iActiveMode == EFileEditorEditAsHex)
       
   369         {
       
   370         // make sure that there is currently no text in the editor 
       
   371         iTextEditor->Text()->Reset();
       
   372 
       
   373         // set character handling related cases for the editor
       
   374         if (iActiveMode == EFileEditorEditAsText)
       
   375             {
       
   376             iTextEditor->SetOnlyASCIIChars(EFalse);
       
   377             iTextEditor->SetAknEditorCase(EAknEditorTextCase);
       
   378             }
       
   379         else if (iActiveMode == EFileEditorEditAsHex)
       
   380             {
       
   381             iTextEditor->SetOnlyASCIIChars(ETrue);
       
   382             iTextEditor->SetAknEditorCase(EAknEditorUpperCase);
       
   383             }  
       
   384             
       
   385         const TInt KBufSize(1024);
       
   386         TInt fileOffset(0);
       
   387         TInt printOffset(0);
       
   388 
       
   389         HBufC8* buf = HBufC8::NewLC(KBufSize);
       
   390         TPtr8 bufPtr(buf->Des());
       
   391         TChar ch;
       
   392         _LIT(KHex, "%02x");
       
   393         TBuf<3> hexBuf;
       
   394 
       
   395         // read to buffer and supply the editor
       
   396         while (fileOffset < fileSize)
       
   397             {
       
   398             // read to buffer
       
   399             User::LeaveIfError( fileP.Read(bufPtr, KBufSize) );
       
   400             
       
   401             // handle in text format
       
   402             if (iActiveMode == EFileEditorEditAsText)
       
   403                 {
       
   404                 HBufC16* buf16 = ConvertBuf8ToBuf16L(buf);
       
   405                 CleanupStack::PushL(buf16);
       
   406                 TPtr16 buf16Ptr(buf16->Des());
       
   407 
       
   408                 // replace any line breaks and append to the editor
       
   409                 for (TInt i=0; i<buf16Ptr.Length(); i++)
       
   410                     {
       
   411                     ch = buf16Ptr[i] != '\n' ? (TChar)buf16Ptr[i] : (TChar)CEditableText::ELineBreak;
       
   412                     iTextEditor->Text()->InsertL(printOffset, ch);
       
   413                     printOffset++;
       
   414                     }
       
   415                 
       
   416                 CleanupStack::PopAndDestroy(); // buf16
       
   417                 }
       
   418             
       
   419             // or handle in hex format
       
   420             else if (iActiveMode == EFileEditorEditAsHex)
       
   421                 {
       
   422                 for (TInt i=0; i<bufPtr.Length(); i++)
       
   423                     {
       
   424                     hexBuf.Format(KHex, bufPtr[i]);
       
   425                     hexBuf.UpperCase();
       
   426                     hexBuf.Append(_L(" "));                    
       
   427                     iTextEditor->Text()->InsertL(printOffset, hexBuf);
       
   428                     printOffset += 3;
       
   429                     }
       
   430                 }
       
   431             
       
   432             fileOffset += bufPtr.Length();
       
   433             }
       
   434                 
       
   435         CleanupStack::PopAndDestroy(); // buf
       
   436 
       
   437         // make sure that the cursor position is in the beginning 
       
   438         iTextEditor->SetCursorPosL(0, EFalse);
       
   439         iTextEditor->NotifyNewDocumentL();
       
   440         iTextEditor->UpdateScrollBarsL();
       
   441         }
       
   442 
       
   443 
       
   444     // viewer related file loading
       
   445     else if (iActiveMode == EFileEditorViewAsText || iActiveMode == EFileEditorViewAsHex)
       
   446         {
       
   447         // load the entire file to buffer
       
   448         HBufC8* buf = HBufC8::NewLC(fileSize);
       
   449         TPtr8 bufPtr(buf->Des());
       
   450         
       
   451         User::LeaveIfError( fileP.Read(bufPtr, fileSize) );
       
   452         
       
   453         // send the buffer to viewer
       
   454         if (iActiveMode == EFileEditorViewAsText)
       
   455             {
       
   456             HBufC16* buf16 = ConvertBuf8ToBuf16L(buf);
       
   457             CleanupStack::PopAndDestroy(); // buf
       
   458             iViewer->FormatAsTextL(buf16);
       
   459             }
       
   460 
       
   461         else if (iActiveMode == EFileEditorViewAsHex)
       
   462             {
       
   463             iViewer->FormatAsHexL(buf);
       
   464             CleanupStack::Pop(); // buf
       
   465             }
       
   466         }
       
   467 
       
   468     CleanupStack::PopAndDestroy(); // fileP
       
   469     }
       
   470     
       
   471 // --------------------------------------------------------------------------------------------
       
   472 
       
   473 void CFileBrowserFileEditorDlg::SaveEditorDataL(TBool aQueryNewName)
       
   474     {
       
   475     TBool doSave(ETrue);
       
   476     CPlainText* docText = iTextEditor->Text();
       
   477     TInt docLength = docText->DocumentLength();
       
   478     
       
   479     TFileName fileName;
       
   480     fileName.Copy(iFileName);
       
   481     
       
   482     // check that format of the file is valid
       
   483     if (iActiveMode == EFileEditorEditAsHex)
       
   484         {
       
   485         // check every 3rd char is a space
       
   486         for (TInt i=2; i<docLength; i+=3)
       
   487             {
       
   488             TPtrC docPtr = docText->Read(i, 1);
       
   489             if (docPtr.Length() < 1 || docPtr[0] != ' ')
       
   490                 {
       
   491                 CAknErrorNote* note = new(ELeave) CAknErrorNote;
       
   492                 note->ExecuteLD(_L("Separate hex blocks with space!"));
       
   493                 return;                
       
   494                 }
       
   495             }
       
   496         
       
   497         // check for valid chars
       
   498         for (TInt i=0; i<docLength; i+=3)
       
   499             {
       
   500             TPtrC docPtr = docText->Read(i, 2);
       
   501             if (docPtr.Length() < 2 || !IsHexChar(docPtr[0]) || !IsHexChar(docPtr[1]))
       
   502                 {
       
   503                 CAknErrorNote* note = new(ELeave) CAknErrorNote;
       
   504                 note->ExecuteLD(_L("Use only hex characters!"));
       
   505                 return;                
       
   506                 }
       
   507             }
       
   508         }
       
   509     
       
   510     
       
   511     // query new file name
       
   512     if (aQueryNewName)
       
   513         {
       
   514         CAknTextQueryDialog* textQuery = CAknTextQueryDialog::NewL(fileName);
       
   515         textQuery->SetPromptL(_L("Full path:"));
       
   516         
       
   517         if (textQuery->ExecuteLD(R_GENERAL_TEXT_QUERY))
       
   518             {
       
   519             doSave = ETrue;
       
   520             }
       
   521         else
       
   522             {
       
   523             doSave = EFalse;
       
   524             }
       
   525         }
       
   526 
       
   527 
       
   528     // continue with writing
       
   529     if (doSave)        
       
   530         {
       
   531         RFile fileP;
       
   532         TInt err = fileP.Replace(CEikonEnv::Static()->FsSession(), fileName, EFileWrite);
       
   533             
       
   534         if (err == KErrNone)    
       
   535             {
       
   536             CleanupClosePushL(fileP);
       
   537             
       
   538             const TInt KBufSize(1002);
       
   539             TInt fileOffset(0);        
       
   540             TInt docOffset(0);
       
   541             TLex converter;
       
   542             TUint hexValue(0);
       
   543             
       
   544             // write BOM for text files
       
   545             if (iActiveMode == EFileEditorEditAsText)
       
   546                 {
       
   547                 if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16LE)
       
   548                     {
       
   549                     TBuf8<2> BOM;
       
   550                     BOM.Append(0xFF);
       
   551                     BOM.Append(0xFE);
       
   552                     
       
   553                     if (fileP.Write(0, BOM, 2) == KErrNone)
       
   554                         fileOffset += 2;
       
   555                     }
       
   556                 
       
   557                 else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16BE)
       
   558                     {
       
   559                     TBuf8<2> BOM;
       
   560                     BOM.Append(0xFE);
       
   561                     BOM.Append(0xFF);
       
   562                     
       
   563                     if (fileP.Write(0, BOM, 2) == KErrNone)
       
   564                         fileOffset += 2;
       
   565                     }
       
   566                                   
       
   567                 else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF8)
       
   568                     {
       
   569                     TBuf8<3> BOM;
       
   570                     BOM.Append(0xEF);
       
   571                     BOM.Append(0xBB);
       
   572                     BOM.Append(0xBF);
       
   573                     
       
   574                     if (fileP.Write(0, BOM, 3) == KErrNone)
       
   575                         fileOffset += 3;
       
   576                     }                
       
   577                 }
       
   578             
       
   579             
       
   580             HBufC8* buf = HBufC8::NewLC(KBufSize * 2);
       
   581             TPtr8 bufPtr(buf->Des());      
       
   582           
       
   583             while (docOffset < docLength)
       
   584                 {
       
   585                 // get text from storage
       
   586                 TPtrC docPtr = docText->Read(docOffset, KBufSize);
       
   587                 bufPtr.Copy(KNullDesC);
       
   588 
       
   589                 if (iActiveMode == EFileEditorEditAsText)
       
   590                     {
       
   591                     HBufC16* convBuf = HBufC16::NewLC(docPtr.Length());
       
   592                     convBuf->Des().Copy(KNullDesC);
       
   593                     
       
   594                      // convert to line breaks first
       
   595                     for (TInt i=0; i<docPtr.Length(); i++)
       
   596                         {
       
   597                         if (docPtr[i] == CEditableText::EParagraphDelimiter) // skip extra char from the editor
       
   598                             continue;    
       
   599                         else if (docPtr[i] == CEditableText::ELineBreak)
       
   600                             convBuf->Des().Append('\n');
       
   601                         else
       
   602                             convBuf->Des().Append(docPtr[i]);
       
   603                         }
       
   604  
       
   605                     HBufC8* buf8 = ConvertBuf16ToBuf8L(convBuf);
       
   606                     
       
   607                     // make sure the sizes are correct
       
   608                     if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatANSIASCII || iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF8)
       
   609                         {
       
   610                         bufPtr.Copy(buf8->Des().Left(convBuf->Length())); 
       
   611                         }
       
   612                     else if (iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16LE || iTextFormat == EFileBrowserCmdFileEditorSaveFormatUTF16BE)
       
   613                         {
       
   614                         bufPtr.Copy(buf8->Des().Left(convBuf->Length()*2)); 
       
   615                         }
       
   616                     
       
   617                     delete buf8;
       
   618                     buf8 = NULL;
       
   619 
       
   620                     CleanupStack::PopAndDestroy(); // convBuf
       
   621                     }
       
   622                     
       
   623                 else if (iActiveMode == EFileEditorEditAsHex)
       
   624                     {
       
   625                     for (TInt i=0; i<docPtr.Length(); i+=3)
       
   626                         {
       
   627                         if (i+1 >= docPtr.Length() - 1)
       
   628                             break;
       
   629                         
       
   630                         TBuf<3> buf2;
       
   631                         buf2.Append(docPtr[i]);
       
   632                         buf2.Append(docPtr[i+1]);
       
   633 
       
   634                         // convert from hex to decimal
       
   635                         converter.Assign(buf2);
       
   636                         if (converter.Val(hexValue, EHex) == KErrNone)
       
   637                             {
       
   638                             bufPtr.Append(hexValue);
       
   639                             }
       
   640                         }                    
       
   641                     }
       
   642                 
       
   643                 // write to a file
       
   644                 err = fileP.Write(fileOffset, bufPtr, bufPtr.Length());
       
   645                 if (err != KErrNone)
       
   646                     break;
       
   647                 
       
   648                 docOffset += docPtr.Length();
       
   649                 fileOffset += bufPtr.Length();
       
   650                 }
       
   651             
       
   652            CleanupStack::PopAndDestroy(2); // buf, fileP
       
   653             }
       
   654         
       
   655         if (err == KErrNone)
       
   656             {
       
   657             // remember the new file name if save was succesfull
       
   658             iFileName.Copy(fileName);
       
   659             
       
   660             CAknConfirmationNote* note = new(ELeave) CAknConfirmationNote;
       
   661             note->ExecuteLD(_L("Saved succesfully"));
       
   662             } 
       
   663         else if (err == KErrInUse)
       
   664             {
       
   665             CAknErrorNote* note = new(ELeave) CAknErrorNote;
       
   666             note->ExecuteLD(_L("The file is in use, cannot write"));
       
   667             User::Leave(KErrNone);
       
   668             }
       
   669         else
       
   670             {
       
   671             CAknErrorNote* note = new(ELeave) CAknErrorNote;
       
   672             note->ExecuteLD(_L("Cannot write to the file"));
       
   673             User::Leave(KErrNone);
       
   674             }
       
   675         }
       
   676     }
       
   677     
       
   678 // --------------------------------------------------------------------------------------------
       
   679 
       
   680 TBool CFileBrowserFileEditorDlg::IsHexChar(TInt aCh)
       
   681     {
       
   682     if (aCh >= '0' && aCh <= '9')
       
   683         return ETrue;
       
   684     else if (aCh >= 'a' && aCh <= 'f')
       
   685         return ETrue;
       
   686     else if (aCh >= 'A' && aCh <= 'F')
       
   687         return ETrue;
       
   688     else
       
   689         return EFalse;
       
   690     }
       
   691       
       
   692 // --------------------------------------------------------------------------------------------
       
   693 
       
   694 void CFileBrowserFileEditorDlg::ProcessCommandL(TInt aCommandId)
       
   695     {
       
   696     CAknDialog::ProcessCommandL(aCommandId);
       
   697 
       
   698     switch (aCommandId)
       
   699         {
       
   700         case EFileBrowserCmdFileEditorViewAsText:
       
   701             {
       
   702             iActiveMode = EFileEditorViewAsText;
       
   703             LoadFileL();
       
   704             }
       
   705             break;
       
   706 
       
   707         case EFileBrowserCmdFileEditorViewAsHex:
       
   708             {
       
   709             iActiveMode = EFileEditorViewAsHex;
       
   710             LoadFileL();
       
   711             }
       
   712             break;
       
   713 
       
   714 //
       
   715         case EFileBrowserCmdFileEditorSave:
       
   716             {
       
   717             SaveEditorDataL();
       
   718             }
       
   719             break;
       
   720             
       
   721         case EFileBrowserCmdFileEditorSaveAs:
       
   722             {
       
   723             SaveEditorDataL(ETrue);
       
   724             }
       
   725             break;
       
   726 
       
   727         case EFileBrowserCmdFileEditorSaveFormatANSIASCII:
       
   728         case EFileBrowserCmdFileEditorSaveFormatUTF8:
       
   729         case EFileBrowserCmdFileEditorSaveFormatUTF16LE:
       
   730         case EFileBrowserCmdFileEditorSaveFormatUTF16BE:
       
   731             {
       
   732             iTextFormat = aCommandId;
       
   733             }
       
   734             break;
       
   735             
       
   736         case EFileBrowserCmdFileEditorEditAsText:
       
   737             {
       
   738             iActiveMode = EFileEditorEditAsText;
       
   739             LoadFileL();
       
   740             }
       
   741             break;
       
   742 
       
   743         case EFileBrowserCmdFileEditorEditAsHex:
       
   744             {
       
   745             iActiveMode = EFileEditorEditAsHex;
       
   746             LoadFileL();
       
   747             }
       
   748             break;
       
   749 
       
   750 //                        
       
   751         case EFileBrowserCmdFileEditorExit:
       
   752             TryExitL(EAknCmdExit);
       
   753             break;
       
   754 
       
   755         default:
       
   756             break;
       
   757         }
       
   758     }
       
   759 
       
   760 // --------------------------------------------------------------------------------------------
       
   761 
       
   762 void CFileBrowserFileEditorDlg::DynInitMenuPaneL(TInt aResourceId, CEikMenuPane* aMenuPane)
       
   763     {
       
   764     if (aResourceId == R_FILEBROWSER_FILEVIEWER_MENUPANE)
       
   765         {
       
   766         aMenuPane->SetItemDimmed(EFileBrowserCmdFileEditorViewAsText, iActiveMode == EFileEditorViewAsText);
       
   767         aMenuPane->SetItemDimmed(EFileBrowserCmdFileEditorViewAsHex, iActiveMode == EFileEditorViewAsHex);
       
   768         }
       
   769     else if (aResourceId == R_FILEBROWSER_FILEEDITOR_MENUPANE)
       
   770         {
       
   771         aMenuPane->SetItemDimmed(EFileBrowserCmdFileEditorEditAsText, iActiveMode == EFileEditorEditAsText);
       
   772         aMenuPane->SetItemDimmed(EFileBrowserCmdFileEditorEditAsHex, iActiveMode == EFileEditorEditAsHex);
       
   773         aMenuPane->SetItemDimmed(EFileBrowserCmdFileEditorSaveFormat, iActiveMode == EFileEditorEditAsHex);
       
   774         }
       
   775 	else if (aResourceId == R_FILEBROWSER_FILEEDITOR_SAVEFORMAT_SUBMENU)
       
   776 	    {
       
   777         aMenuPane->SetItemButtonState(iTextFormat, EEikMenuItemSymbolOn);
       
   778 	    }
       
   779     }
       
   780 
       
   781 // --------------------------------------------------------------------------------------------
       
   782 
       
   783 SEikControlInfo CFileBrowserFileEditorDlg::CreateCustomControlL(TInt aControlType)
       
   784     {
       
   785     SEikControlInfo controlInfo;
       
   786     controlInfo.iControl = NULL;
       
   787     controlInfo.iTrailerTextId = 0;
       
   788     controlInfo.iFlags = 0;
       
   789 
       
   790     if (aControlType == EFileViewerDialogControl)
       
   791         {
       
   792         // create a control
       
   793         iViewer = new(ELeave) CFileBrowserFileEditorViewControl();
       
   794         iViewer->SetMopParent(this);
       
   795         iViewer->ConstructL();
       
   796 
       
   797         controlInfo.iControl = iViewer;
       
   798         }
       
   799         
       
   800     return controlInfo;
       
   801     }
       
   802 
       
   803 // --------------------------------------------------------------------------------------------
       
   804 
       
   805 void CFileBrowserFileEditorDlg::SetSizeAndPosition(const TSize& /*aSize*/)
       
   806     {
       
   807     // fill whole window
       
   808     TRect mainPaneRect;
       
   809     TRect naviPaneRect;
       
   810 
       
   811     if (Layout_Meta_Data::IsLandscapeOrientation() ||
       
   812         iEikonEnv->AppUiFactory()->StatusPane()->CurrentLayoutResId() != R_AVKON_STATUS_PANE_LAYOUT_SMALL)
       
   813         {
       
   814         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
       
   815         }
       
   816     else
       
   817         {
       
   818         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
       
   819         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::ENaviPane, naviPaneRect);
       
   820         mainPaneRect.iTl.iY = naviPaneRect.Height();   
       
   821         }
       
   822             
       
   823     SetRect(mainPaneRect);
       
   824     }
       
   825 
       
   826 // --------------------------------------------------------------------------------------------
       
   827 
       
   828 TBool CFileBrowserFileEditorDlg::RunDlgLD()
       
   829     {
       
   830     if (iActiveMode == EFileEditorViewAsText || iActiveMode == EFileEditorViewAsHex)
       
   831         return ExecuteLD(R_FILEBROWSER_FILEVIEWER_DIALOG);
       
   832     else
       
   833         return ExecuteLD(R_FILEBROWSER_FILEEDITOR_DIALOG);
       
   834     }
       
   835        
       
   836 // --------------------------------------------------------------------------------------------
       
   837 // --------------------------------------------------------------------------------------------
       
   838 
       
   839 CFileBrowserFileEditorViewControl::~CFileBrowserFileEditorViewControl()
       
   840     {
       
   841     ResetVariables();
       
   842     }
       
   843 
       
   844 // --------------------------------------------------------------------------------------------
       
   845 
       
   846 CFileBrowserFileEditorViewControl::CFileBrowserFileEditorViewControl()
       
   847     {
       
   848     }
       
   849 
       
   850 // --------------------------------------------------------------------------------------------
       
   851 
       
   852 void CFileBrowserFileEditorViewControl::ConstructL()
       
   853     {
       
   854     CreateWindowL();
       
   855     SetBlank();
       
   856     SetRect(Rect());
       
   857     ActivateL();
       
   858     
       
   859     iTotalNumberOfLines = 0;
       
   860     }
       
   861 
       
   862 // --------------------------------------------------------------------------------------------
       
   863 
       
   864 TSize CFileBrowserFileEditorViewControl::MinimumSize()
       
   865     {
       
   866     TRect mainPaneRect;
       
   867     TRect naviPaneRect;
       
   868     
       
   869     if (Layout_Meta_Data::IsLandscapeOrientation() ||
       
   870         iEikonEnv->AppUiFactory()->StatusPane()->CurrentLayoutResId() != R_AVKON_STATUS_PANE_LAYOUT_SMALL)
       
   871         {
       
   872         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
       
   873         }
       
   874     else
       
   875         {
       
   876         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
       
   877         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::ENaviPane, naviPaneRect);
       
   878         mainPaneRect.iTl.iY = naviPaneRect.Height();   
       
   879         }
       
   880 
       
   881     return mainPaneRect.Size();
       
   882     }
       
   883         
       
   884 // --------------------------------------------------------------------------------------------
       
   885 
       
   886 TKeyResponse CFileBrowserFileEditorViewControl::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
       
   887     {
       
   888     if(aType != EEventKey)
       
   889         return EKeyWasNotConsumed;
       
   890     
       
   891     // normally scroll only one line
       
   892     TInt scrollLines(1); 
       
   893     
       
   894     // with special keys scroll iNumberOfLinesFitsScreen-1 lines instead
       
   895     if (aKeyEvent.iCode == EKeyIncVolume || aKeyEvent.iCode=='#' || aKeyEvent.iCode == EKeyDecVolume || aKeyEvent.iCode=='*')
       
   896         scrollLines = iNumberOfLinesFitsScreen-1;
       
   897     
       
   898     
       
   899     if (aKeyEvent.iCode == EKeyUpArrow || aKeyEvent.iCode == EKeyIncVolume || aKeyEvent.iCode=='#')
       
   900         {
       
   901         if (iCurrentLine > 0)
       
   902             {
       
   903             iCurrentLine -= scrollLines;
       
   904             
       
   905             if (iCurrentLine < 0)
       
   906                 iCurrentLine = 0;
       
   907             
       
   908             UpdateScrollBarL();
       
   909             DrawNow();
       
   910             }
       
   911 
       
   912         return EKeyWasConsumed;
       
   913         }
       
   914     
       
   915     else if (aKeyEvent.iCode == EKeyDownArrow || aKeyEvent.iCode == EKeyDecVolume || aKeyEvent.iCode=='*')
       
   916         {
       
   917         if (iActiveMode == EFileEditorViewAsText)
       
   918             {
       
   919             // no need to scroll text if it fits to the screen
       
   920             if (iTotalNumberOfLines <= iNumberOfLinesFitsScreen)
       
   921                 return EKeyWasConsumed;
       
   922 
       
   923             iCurrentLine += scrollLines;
       
   924 
       
   925             // adjust the last line to end of the screen            
       
   926             if (iCurrentLine > iTotalNumberOfLines - iNumberOfLinesFitsScreen)
       
   927                 iCurrentLine = iTotalNumberOfLines - iNumberOfLinesFitsScreen;            
       
   928             }
       
   929         else if (iActiveMode == EFileEditorViewAsHex)
       
   930             {
       
   931             iCurrentLine += scrollLines;
       
   932 
       
   933             // last line can be first line on the screen            
       
   934             if (iCurrentLine > iTotalNumberOfLines - 1)
       
   935                 iCurrentLine = iTotalNumberOfLines - 1;              
       
   936             }
       
   937             
       
   938         UpdateScrollBarL();
       
   939         DrawNow();
       
   940         
       
   941         return EKeyWasConsumed;
       
   942         }
       
   943     
       
   944     return EKeyWasNotConsumed;
       
   945     }
       
   946 
       
   947 // --------------------------------------------------------------------------------------------
       
   948 
       
   949 void CFileBrowserFileEditorViewControl::FormatAsTextL(HBufC16* aBuf)
       
   950     {
       
   951     if (aBuf == NULL)    
       
   952         ResetVariables(EFalse);
       
   953     else
       
   954         ResetVariables();
       
   955     
       
   956     InitVariables();
       
   957     iActiveMode = EFileEditorViewAsText;
       
   958     
       
   959     // store the bugger
       
   960     if (aBuf != NULL)
       
   961         iTextBuf = aBuf;
       
   962     
       
   963     iWrappedTextArray = new(ELeave) CArrayFixFlat<TPtrC>(32);
       
   964     
       
   965     // wrap to array
       
   966     AknTextUtils::WrapToArrayL(iTextBuf->Des(), iDrawingWidth, *iFont, *iWrappedTextArray);
       
   967     iTotalNumberOfLines = iWrappedTextArray->Count();
       
   968 
       
   969     // count amount of lines fits to screen
       
   970     iNumberOfLinesFitsScreen = TInt(iViewerRect.Height() / (iFont->HeightInPixels()));
       
   971 
       
   972     // update scroll bars
       
   973     UpdateScrollBarL();
       
   974 
       
   975     // update the screen
       
   976     DrawNow();    
       
   977     }
       
   978 
       
   979 // --------------------------------------------------------------------------------------------
       
   980 
       
   981 void CFileBrowserFileEditorViewControl::FormatAsHexL(HBufC8* aBuf)
       
   982     {
       
   983     if (aBuf == NULL)    
       
   984         ResetVariables(EFalse);
       
   985     else
       
   986         ResetVariables();
       
   987     
       
   988     InitVariables();
       
   989     iActiveMode = EFileEditorViewAsHex;
       
   990     
       
   991     // store the buffer
       
   992     if (aBuf != NULL)
       
   993         iHexesBuffer = aBuf;
       
   994     
       
   995     // calculate amount of hex values fits to the screen
       
   996     TUint charWidth = iFont->CharWidthInPixels('X');
       
   997     iHexGrougWidth = charWidth*2 + TInt (KHexSeparatorMargin*iX_factor);
       
   998     iAmountOfHexesFitsHorizontally = TUint( iDrawingWidth / iHexGrougWidth );
       
   999 
       
  1000     // calculate total number of files
       
  1001     iTotalNumberOfLines = iHexesBuffer->Length() / iAmountOfHexesFitsHorizontally + 1;
       
  1002 
       
  1003     // count amount of lines fits to screen
       
  1004     iNumberOfLinesFitsScreen = TInt(iViewerRect.Height() / iFont->HeightInPixels()) - 1;
       
  1005 
       
  1006     // update scroll bars
       
  1007     UpdateScrollBarL();
       
  1008 
       
  1009     // update the screen
       
  1010     DrawNow();      
       
  1011     }
       
  1012 
       
  1013 // --------------------------------------------------------------------------------------------
       
  1014 
       
  1015 void CFileBrowserFileEditorViewControl::InitVariables()
       
  1016     {
       
  1017     iCurrentLine = 0;
       
  1018 
       
  1019     // calculate layout data
       
  1020     TRect mainPaneRect;
       
  1021     TRect naviPaneRect;
       
  1022     
       
  1023     if (Layout_Meta_Data::IsLandscapeOrientation() ||
       
  1024         iEikonEnv->AppUiFactory()->StatusPane()->CurrentLayoutResId() != R_AVKON_STATUS_PANE_LAYOUT_SMALL)
       
  1025         {
       
  1026         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
       
  1027         }
       
  1028     else
       
  1029         {
       
  1030         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, mainPaneRect);
       
  1031         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::ENaviPane, naviPaneRect);
       
  1032         mainPaneRect.iTl.iY = naviPaneRect.Height();   
       
  1033         }
       
  1034 
       
  1035     iViewerRect = TRect(mainPaneRect.Size());
       
  1036 
       
  1037     iX_factor = TReal(iViewerRect.Width()) / 176;
       
  1038     iY_factor = TReal(iViewerRect.Height()) / 144;
       
  1039 
       
  1040 
       
  1041     // define drawing width, some extra space needed for double span scrolling bar
       
  1042     if (AknLayoutUtils::LayoutMirrored())  // scroll bar on 'left'
       
  1043         {
       
  1044         iLeftDrawingPosition = KLeftMargin + 12;
       
  1045         iDrawingWidth = TUint( iViewerRect.Width() - iLeftDrawingPosition*iX_factor - (KRightMargin*iX_factor)); 
       
  1046         }
       
  1047     else // scroll bar on 'right'
       
  1048         {
       
  1049         iLeftDrawingPosition = KLeftMargin;  
       
  1050         iDrawingWidth = TUint( iViewerRect.Width() - iLeftDrawingPosition*iX_factor - (KRightMargin*iX_factor + 7*iX_factor)); 
       
  1051         } 
       
  1052 
       
  1053 
       
  1054     // set font
       
  1055     CGraphicsDevice* dev = iCoeEnv->ScreenDevice();
       
  1056     const TSize screenSize = dev->SizeInPixels();
       
  1057     
       
  1058     TFontSpec fontSpec = AknLayoutUtils::FontFromId(EAknLogicalFontSecondaryFont)->FontSpecInTwips();
       
  1059     fontSpec.iHeight = IsQHD(screenSize) ? KViewerFontHeightQHD : KViewerFontHeight; // set height
       
  1060     dev->GetNearestFontInTwips(iFont, fontSpec);
       
  1061     }
       
  1062 
       
  1063 // --------------------------------------------------------------------------------------------
       
  1064 
       
  1065 void CFileBrowserFileEditorViewControl::ResetVariables(TBool aResetData)
       
  1066     {
       
  1067     if (iScrollBarFrame)
       
  1068         {
       
  1069         delete iScrollBarFrame;
       
  1070         iScrollBarFrame = NULL;
       
  1071         }
       
  1072         
       
  1073     if (iHexesBuffer && aResetData)
       
  1074         {
       
  1075         delete iHexesBuffer;
       
  1076         iHexesBuffer = NULL;
       
  1077         }    
       
  1078 
       
  1079     if (iWrappedTextArray)
       
  1080         {
       
  1081         delete iWrappedTextArray;
       
  1082         iWrappedTextArray = NULL;
       
  1083         }
       
  1084 
       
  1085     if (iTextBuf && aResetData)
       
  1086         {
       
  1087         delete iTextBuf;
       
  1088         iTextBuf = NULL;
       
  1089         }
       
  1090 
       
  1091     if (iFont)
       
  1092         {
       
  1093         CGraphicsDevice* dev = iCoeEnv->ScreenDevice();
       
  1094         dev->ReleaseFont(iFont);
       
  1095         iFont = NULL;
       
  1096         }
       
  1097     }
       
  1098     
       
  1099 // --------------------------------------------------------------------------------------------
       
  1100 
       
  1101 void CFileBrowserFileEditorViewControl::UpdateScrollBarL()
       
  1102     {
       
  1103     TRect rect(iViewerRect);
       
  1104     
       
  1105     // init the scroll bar    
       
  1106     if (!iScrollBarFrame)
       
  1107         {
       
  1108         iScrollBarFrame = new(ELeave) CEikScrollBarFrame(this, this, ETrue);
       
  1109         iScrollBarFrame->CreateDoubleSpanScrollBarsL(ETrue, EFalse);            
       
  1110         iScrollBarFrame->SetTypeOfVScrollBar(CEikScrollBarFrame::EDoubleSpan);
       
  1111         iScrollBarFrame->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
       
  1112         }       
       
  1113 
       
  1114 
       
  1115     // update values
       
  1116 	TEikScrollBarModel horizontalBar;
       
  1117     TEikScrollBarModel verticalBar;
       
  1118 
       
  1119     if (iActiveMode == EFileEditorViewAsText)
       
  1120         {    
       
  1121         verticalBar.iThumbPosition = iCurrentLine;
       
  1122         verticalBar.iScrollSpan = iTotalNumberOfLines - iNumberOfLinesFitsScreen + 1;
       
  1123         verticalBar.iThumbSpan = 1;
       
  1124         }
       
  1125     
       
  1126     else if (iActiveMode == EFileEditorViewAsHex)
       
  1127         {    
       
  1128         verticalBar.iThumbPosition = iCurrentLine;
       
  1129         verticalBar.iScrollSpan = iTotalNumberOfLines;
       
  1130         verticalBar.iThumbSpan = 1;
       
  1131         }
       
  1132         
       
  1133     TEikScrollBarFrameLayout layout;
       
  1134 	layout.iTilingMode = TEikScrollBarFrameLayout::EInclusiveRectConstant;
       
  1135 
       
  1136     // do not let scrollbar values overflow
       
  1137     if (verticalBar.iThumbPosition + verticalBar.iThumbSpan > verticalBar.iScrollSpan)
       
  1138         verticalBar.iThumbPosition = verticalBar.iScrollSpan - verticalBar.iThumbSpan;
       
  1139 	
       
  1140     TAknDoubleSpanScrollBarModel horizontalDSBar(horizontalBar);
       
  1141     TAknDoubleSpanScrollBarModel verticalDSBar(verticalBar);
       
  1142    
       
  1143     iScrollBarFrame->TileL(&horizontalDSBar, &verticalDSBar, rect, rect, layout);        
       
  1144     iScrollBarFrame->SetVFocusPosToThumbPos(verticalDSBar.FocusPosition());
       
  1145     }
       
  1146 
       
  1147 // --------------------------------------------------------------------------------------------
       
  1148 
       
  1149 void CFileBrowserFileEditorViewControl::Draw(const TRect& aRect) const
       
  1150     {
       
  1151     CWindowGc& gc = SystemGc();
       
  1152     gc.Clear(aRect);
       
  1153     gc.SetPenColor(KRgbBlack);
       
  1154     gc.UseFont(iFont);
       
  1155     
       
  1156     TInt fontHeight = iFont->HeightInPixels();
       
  1157 
       
  1158     if (iActiveMode == EFileEditorViewAsText)
       
  1159         {
       
  1160         // draw the text
       
  1161         for (TInt i=0; i<iNumberOfLinesFitsScreen; i++)
       
  1162             {
       
  1163             // check for bounds and draw
       
  1164             if (iCurrentLine >= 0 && iTotalNumberOfLines > i+iCurrentLine)
       
  1165                 gc.DrawText(iWrappedTextArray->At(i+iCurrentLine), TPoint(TInt(iLeftDrawingPosition*iX_factor), TInt(fontHeight*(i+1))));
       
  1166             }        
       
  1167         }
       
  1168 
       
  1169     else if (iActiveMode == EFileEditorViewAsHex)
       
  1170         {
       
  1171         TInt hexesOffSet = iCurrentLine * iAmountOfHexesFitsHorizontally; 
       
  1172         TInt topLineHeight = 2*iY_factor + fontHeight;
       
  1173         
       
  1174         // draw the top line
       
  1175         gc.SetPenColor(KRgbWhite);
       
  1176         
       
  1177         gc.SetBrushColor(KRgbBlue);
       
  1178         gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
       
  1179         if (AknLayoutUtils::LayoutMirrored())  // scroll bar on 'left'
       
  1180             {
       
  1181             gc.DrawRect(TRect((KLeftMargin + 12)*iX_factor, 0, aRect.iBr.iX, topLineHeight));
       
  1182             }
       
  1183         else // scroll bar on 'right'
       
  1184             {
       
  1185             gc.DrawRect(TRect(0, 0, aRect.iBr.iX-(KRightMargin*iX_factor + 7*iX_factor), topLineHeight));
       
  1186             } 
       
  1187 
       
  1188         TBuf<128> topLineBuf;
       
  1189         topLineBuf.AppendNum(hexesOffSet);
       
  1190         topLineBuf.Append(_L(": "));
       
  1191         
       
  1192         for (TInt i=0; i<iAmountOfHexesFitsHorizontally; i++)
       
  1193             {
       
  1194             TInt pos = hexesOffSet + i;
       
  1195             
       
  1196             if (pos < iHexesBuffer->Des().Length())
       
  1197                 {
       
  1198                 topLineBuf.Append(iHexesBuffer->Des()[pos]);
       
  1199                 }
       
  1200             }
       
  1201  
       
  1202         gc.DrawText(topLineBuf, TPoint(TInt(iLeftDrawingPosition*iX_factor), TInt(fontHeight)));
       
  1203 
       
  1204  
       
  1205         // draw hex values
       
  1206         gc.SetPenColor(KRgbBlack);
       
  1207         for (TInt i=0; i<iNumberOfLinesFitsScreen; i++)
       
  1208             {
       
  1209             for (TInt j=0; j<iAmountOfHexesFitsHorizontally; j++)
       
  1210                 {
       
  1211                 TInt pos = hexesOffSet + i*iAmountOfHexesFitsHorizontally + j;
       
  1212                 
       
  1213                 if (pos >= iHexesBuffer->Des().Length())
       
  1214                     return;
       
  1215                 
       
  1216                 TUint8* ptr = &iHexesBuffer->Des()[pos];
       
  1217                 
       
  1218                 _LIT(KHex, "%02x");
       
  1219                 TBuf<5> hexBuf;
       
  1220                 hexBuf.Format(KHex, ptr[0]);
       
  1221                 hexBuf.UpperCase();
       
  1222 
       
  1223                 gc.DrawText(hexBuf, TPoint(TInt(iLeftDrawingPosition*iX_factor + j*iHexGrougWidth), TInt(topLineHeight + fontHeight*(i+1))));
       
  1224                 }
       
  1225             }         
       
  1226         }
       
  1227 
       
  1228     gc.DiscardFont();
       
  1229     }
       
  1230   
       
  1231 // --------------------------------------------------------------------------------------------
       
  1232 
       
  1233 CCoeControl* CFileBrowserFileEditorViewControl::ComponentControl(TInt aIndex) const
       
  1234 	{
       
  1235 	if (aIndex == 0 && iScrollBarFrame)
       
  1236 	    return iScrollBarFrame->VerticalScrollBar();
       
  1237 	else
       
  1238 	    return NULL;
       
  1239 	}
       
  1240 
       
  1241 // --------------------------------------------------------------------------------------------
       
  1242 
       
  1243 TInt CFileBrowserFileEditorViewControl::CountComponentControls() const
       
  1244 	{
       
  1245 	if (iScrollBarFrame)
       
  1246 	    return 1;
       
  1247 	else
       
  1248 	    return 0;
       
  1249 	}
       
  1250 
       
  1251 // --------------------------------------------------------------------------------------------
       
  1252 
       
  1253 void CFileBrowserFileEditorViewControl::HandleScrollEventL(CEikScrollBar* aScrollBar, TEikScrollEvent aEventType)
       
  1254     {
       
  1255     if ((aEventType == EEikScrollPageDown) || (aEventType == EEikScrollPageUp) || 
       
  1256        (aEventType == EEikScrollThumbDragVert) || (aEventType == EEikScrollUp) ||
       
  1257        (aEventType == EEikScrollDown))
       
  1258         {
       
  1259         iCurrentLine = aScrollBar->ThumbPosition();
       
  1260         UpdateScrollBarL();
       
  1261         DrawNow();
       
  1262         }
       
  1263     }
       
  1264 
       
  1265 // --------------------------------------------------------------------------------------------
       
  1266  
       
  1267 TTypeUid::Ptr CFileBrowserFileEditorViewControl::MopSupplyObject(TTypeUid aId)
       
  1268     {
       
  1269     return CCoeControl::MopSupplyObject(aId);
       
  1270     }
       
  1271 	   
       
  1272 // --------------------------------------------------------------------------------------------
       
  1273 
       
  1274 void CFileBrowserFileEditorViewControl::HandleResourceChange(TInt aType)
       
  1275     {
       
  1276     if (aType == KEikDynamicLayoutVariantSwitch)
       
  1277         {
       
  1278         SetRect(Rect());
       
  1279         ActivateL();
       
  1280         
       
  1281         // reformat the text
       
  1282         if (iActiveMode == EFileEditorViewAsText)
       
  1283             {
       
  1284             FormatAsTextL();
       
  1285             }
       
  1286         else if (iActiveMode == EFileEditorViewAsHex)
       
  1287             {
       
  1288             FormatAsHexL();
       
  1289             }
       
  1290         }
       
  1291     else
       
  1292         {
       
  1293         CCoeControl::HandleResourceChange(aType);
       
  1294         }
       
  1295     }
       
  1296 
       
  1297 // --------------------------------------------------------------------------------------------
       
  1298 
       
  1299 // End of File
       
  1300