home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 720 / PDF090B4-SorceCode / eikon / pdfappui.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  52.9 KB  |  1,913 lines

  1. // PdfAppUi.cpp
  2. //
  3. // Copyright (c) 1999-2000 Sander van der Wal.  All rights reserved.
  4. //
  5. // $Id: pdfappui.cpp 1.4 2000-09-27 09:39:05+02 svdwal Exp svdwal $
  6. //
  7. // $Log: pdfappui.cpp $
  8. // Revision 1.4  2000-09-27 09:39:05+02  svdwal
  9. // OOM and Trace in separate submenu of Tools
  10. //
  11. // Revision 1.3  2000-09-24 21:11:27+02  svdwal
  12. // Different OOM tests can be turned on and of in a debugger
  13. //
  14. // Revision 1.2  2000-09-21 14:02:00+02  svdwal
  15. // More OOM testing
  16. //
  17. // Revision 1.1  2000-09-17 13:18:10+02  svdwal
  18. // Initial checkin
  19. //
  20.  
  21. #include "PdfAppUi.h"
  22.  
  23. // --o AppArc
  24. #ifndef __APGTASK_H__
  25. #include <apgtask.h>
  26. #endif
  27. #ifndef __APGWGNAM_H__
  28. #include <apgwgnam.h>
  29. #endif
  30.  
  31. // --o CONE
  32. #ifndef __COEUTILS_H__
  33. #include <coeutils.h>
  34. #endif
  35.  
  36. // --o EIKON
  37. #include <eikapp.h>
  38. #include <eikcfdlg.h>
  39. #include <eikchkbx.h>
  40. #include <eikcmbox.h>
  41. #include <eikcmbut.h>
  42. #include <eikdll.h>
  43. #include <eikenv.h>
  44. #include <eikfindd.h>
  45. #include <eikfnlab.h>
  46. #include <eikmenub.h>
  47. #include <eiklabel.h>
  48. #include <eikprtpv.h>
  49. #include <eikproc.h>
  50. #include <eiktbar.h>
  51. #include <eiktxlbm.h>
  52. #include <eiktxlbx.h>
  53.  
  54. #include <eikcmds.hrh>
  55. #include <eikmenu.hrh>
  56. #include <eikon.rsg> // needed for file browse dialog
  57.  
  58. // User interface
  59. #include "PdfPageView.h"
  60. #include "Pdf.hrh"
  61. #include "Pdf.rsg"
  62.  
  63. // Framework
  64. #include "PdfDocument.h"
  65.  
  66. // Engine
  67. #include "SearchState.h"
  68.  
  69. // --o PDFLib
  70. #include "PDFDoc.h"
  71. #include "TextOutputDev.h"
  72.  
  73. // Profiling
  74. #include "PROFILE.H"
  75.  
  76. //
  77. // Global const
  78. //
  79. static const TInt KBusyInitialDelay=1000000; // in microseconds
  80.  
  81. static inline TInt round(double x) { return TInt(x+0.5); };
  82.  
  83. // index to zoom factor percentage
  84. static const TInt KZoom[11] = { 33, 50, 66, 75, 87, 100, 125, 150, 200, 300, 400};
  85.  
  86. static TInt ZoomToIndex(TInt aZoom)
  87. {
  88.   TInt index;
  89.   if (aZoom == CPdfPageView::KZoomPage)
  90.     index = 11;
  91.   else if (aZoom == CPdfPageView::KZoomWidth)
  92.     index = 12;
  93.   else if (aZoom <=  33)
  94.     index =  0;
  95.   else if (aZoom <=  50)
  96.     index =  1;
  97.   else if (aZoom <=  66)
  98.     index =  2;
  99.   else if (aZoom <=  75)
  100.     index =  3;
  101.   else if (aZoom <=  87)
  102.     index =  4;
  103.   else if (aZoom <= 100)
  104.     index =  5;
  105.   else if (aZoom <= 125)
  106.     index =  6;
  107.   else if (aZoom <= 150)
  108.     index =  7;
  109.   else if (aZoom <= 200)
  110.     index =  8;
  111.   else if (aZoom <= 300)
  112.     index =  9;
  113.   else if (aZoom <= 400)
  114.     index = 10;
  115.   else // > 400
  116.     index = 10;
  117.   return index;
  118. }
  119.  
  120.  
  121. //////////////////////////////////////////////////////////////////////////////
  122. //
  123. // CPdfTextListBox
  124. //
  125. //////////////////////////////////////////////////////////////////////////////
  126. class CPdfTextListBox: public CEikTextListBox
  127. {
  128. public:    
  129.   TInt MaxWidth();
  130. };
  131.  
  132.  
  133. TInt CPdfTextListBox::MaxWidth()
  134. {
  135.   iView->CalcDataWidth();
  136.   TInt width=iView->DataWidth();
  137.   width+=(HorizontalMargin()<<1)+iBorder.SizeDelta().iWidth;
  138.   return width;
  139. }
  140.  
  141.  
  142. //////////////////////////////////////////////////////////////////////////////
  143. //
  144. // CPdfDocInfoDialog
  145. //
  146. //////////////////////////////////////////////////////////////////////////////
  147. class CPdfDocInfoDialog : public CEikDialog
  148. {
  149. public:
  150.   CPdfDocInfoDialog(PDFDoc* aDoc):iDoc(aDoc) {};
  151.   
  152. // -- CEikDialog
  153. private:
  154.   void PreLayoutDynInitL();
  155.   TBool OkToExitL(TInt /* aKeycode */) { return(ETrue); };
  156.  
  157. // -- CPdfDocInfoDialog
  158. private:
  159.   CEikEdwin* Edwin(TInt aControlId) { return STATIC_CAST(CEikEdwin*,Control(aControlId)); };
  160.   void SetDateTimeValueL(TInt aControlId, const TText8* aValue);
  161.   void SetCheckBoxState(TInt aControlId, TBool aState);
  162.  
  163. private:
  164.   PDFDoc* iDoc;
  165. };
  166.  
  167.  
  168. void CPdfDocInfoDialog::PreLayoutDynInitL()
  169. {
  170.   RAutoObject info;
  171.  
  172.   // print doc info
  173.   iDoc->getDocInfoL(&info);
  174.   if (info.isDict()) {
  175.     RAutoObject obj;
  176.     
  177.     if (info.dictLookupL("Title", &obj)->isString())
  178.       SetEdwinTextL(EPdfDocInfoDialogTitle, &obj.getString()->getTPtr());
  179.     obj.free();
  180.     
  181.     if (info.dictLookupL("Subject", &obj)->isString())
  182.       SetEdwinTextL(EPdfDocInfoDialogSubject, &obj.getString()->getTPtr());
  183.     obj.free();
  184.     
  185.     if (info.dictLookupL("Keywords", &obj)->isString())
  186.       SetEdwinTextL(EPdfDocInfoDialogKeywords, &obj.getString()->getTPtr());
  187.     obj.free();
  188.     
  189.     if (info.dictLookupL("Author", &obj)->isString())
  190.       SetEdwinTextL(EPdfDocInfoDialogAuthor, &obj.getString()->getTPtr());
  191.     obj.free();
  192.     
  193.     if (info.dictLookupL("Creator", &obj)->isString())
  194.       SetEdwinTextL(EPdfDocInfoDialogCreator, &obj.getString()->getTPtr());
  195.     obj.free();
  196.     
  197.     if (info.dictLookupL("Producer", &obj)->isString())
  198.       SetEdwinTextL(EPdfDocInfoDialogProducer,  &obj.getString()->getTPtr());
  199.     obj.free();
  200.  
  201.     if (info.dictLookupL("CreationDate", &obj)->isString())
  202.       SetDateTimeValueL(EPdfDocInfoDialogCreated, (const TText8*)obj.getString()->getCString());
  203.     obj.free();
  204.     
  205.     if (info.dictLookupL("ModDate", &obj)->isString())
  206.       SetDateTimeValueL(EPdfDocInfoDialogModified, (const TText8*)obj.getString()->getCString());
  207.     obj.free();
  208.   }
  209.   info.free();
  210.  
  211.   // file size in bytes
  212.   TEntry entry;
  213.   if (KErrNone == iCoeEnv->FsSession().Entry(*iDoc->getFileName(), entry)) {
  214.     SetNumberEditorMinAndMax(EPdfDocInfoDialogFileSize, entry.iSize, entry.iSize);
  215.     SetNumberEditorValue(EPdfDocInfoDialogFileSize, entry.iSize);
  216.   }
  217.  
  218.   SetCheckBoxState(EPdfDocInfoDialogEncrypted, iDoc->isEncrypted());
  219.   
  220.   SetCheckBoxState(EPdfDocInfoDialogPrinting,  iDoc->okToPrint());
  221.   SetCheckBoxState(EPdfDocInfoDialogChanging,  iDoc->okToChange());
  222.   SetCheckBoxState(EPdfDocInfoDialogSelecting, iDoc->okToCopy());
  223.   SetCheckBoxState(EPdfDocInfoDialogAdding,    iDoc->okToAddNotes());
  224. }
  225.  
  226.  
  227. void CPdfDocInfoDialog::SetDateTimeValueL(TInt aControlId, const TText8* aValue)
  228. {
  229.   //
  230.   // the syntax of the time is D:YYYYMMDDHHmmSSOHH'mm'
  231.   // with: D: optinal: indicates this is a date
  232.   //       YYYY year, MM month (1-12), DD day (1-31)
  233.   //       HH hour (0-23), mm minutes (0-59), SS seconds (0-59)
  234.   //       O: + later than GMT, - earlier thhan GMT, % GMT
  235.   //       HH'mm': HH hours and mm minutes before/after GMT
  236.   
  237.   TBuf<23> buf;
  238.   buf.Copy(aValue, 23);
  239.  
  240.   if (buf[0] == 'D' && buf[1] == ':')
  241.     buf.Delete(0,2);
  242.  
  243.   // There must be at leas the year 
  244.   if (buf.Length() >=  4) {
  245.   
  246.     TDateTime datetime;
  247.  
  248.     // Unfortunately the datetime parsing functions expect months and days
  249.     // to be ofsetted from zero instead of 1. Now we have to do all the work.
  250.     TInt   year =(((buf[0]-'0')* 10 + (buf[1]-'0'))*10+(buf[2]-'0'))*10+(buf[3]-'0');
  251.     TMonth month=(buf.Length()< 6) ? EJanuary:TMonth((buf[4]-'0')*10+(buf[5]-'0')-1);
  252.     TInt   day  =(buf.Length()< 8) ?        0:(buf[ 6]-'0')*10+(buf[ 7]-'0')-1;
  253.     
  254.     TInt   hour =(buf.Length()<10) ?        0:(buf[ 8]-'0')*10+(buf[ 9]-'0');
  255.     TInt   mins =(buf.Length()<12) ?        0:(buf[10]-'0')*10+(buf[11]-'0');
  256.     TInt   secs =(buf.Length()<14) ?        0:(buf[12]-'0')*10+(buf[13]-'0');
  257.     
  258.     if (KErrNone == datetime.Set(year, month, day, hour, mins, secs, 0)) {
  259.  
  260.       TTime time(datetime);
  261.       // turn it into a formatted string according to the locale
  262.       TBuf<64> string;
  263.       TRAPD(ignore, 
  264.             time.FormatL(string, _L("%D%M%Y%/0%1%/1%2%/2%3%/3 %-B%:0%J%:1%T%:2%S%:3%+B")));
  265.  
  266.       //SetTTimeEditorMinAndMax(aControlId, val, val);
  267.       SetEdwinTextL(aControlId, &string);
  268.     }
  269.   }
  270. }
  271.  
  272.  
  273. // This way the state stays as it has been set.
  274. void CPdfDocInfoDialog::SetCheckBoxState(TInt aControlId, TBool aState)
  275. {
  276.   CEikDialog::SetCheckBoxState(aControlId, 
  277.      aState ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  278.   ((CEikCheckBox*)Control(aControlId))->SetBehavior(
  279.      aState ? EEikButtonStaysSet: EEikButtonStaysClear);
  280. }
  281.  
  282.  
  283. //////////////////////////////////////////////////////////////////////////////
  284. //
  285. // TPdfUiState
  286. //
  287. //////////////////////////////////////////////////////////////////////////////
  288. TPdfUiState::TPdfUiState()
  289. {
  290.   SetDefaults();
  291. }
  292.  
  293.  
  294. void TPdfUiState::SetDefaults()
  295. {
  296.   iZoom           = 100; // percent
  297.   iRotate         =   0;
  298.   iDoLinks        = ETrue;
  299.   iDrawAccurate   = EFalse;
  300.   iHBarVisibility =   2; // auto
  301.   iVBarVisibility =   2; // auto
  302.   iShowToolBar    = ETrue;
  303.   iShowToolBand   = ETrue;
  304.   iMaximizeView   = EFalse;
  305.   iRenderText     = ETrue;
  306.   iRenderLineDrawings = EImageVisOn;
  307.   iRenderImages   = ETrue;
  308.   iDitherImages   = ETrue;
  309.   iClip           = ETrue;
  310. }
  311.  
  312.  
  313. void TPdfUiState::InternalizeL(RReadStream& aStream)
  314. {
  315.   iZoom   = aStream.ReadInt32L();
  316.   iRotate = aStream.ReadInt16L();
  317.   iDoLinks = TBool(aStream.ReadInt8L());
  318.   iDrawAccurate = TBool(aStream.ReadInt8L());
  319.   
  320.   iHBarVisibility = aStream.ReadInt8L();
  321.   iVBarVisibility = aStream.ReadInt8L();
  322.  
  323.   iShowToolBar  = TBool(aStream.ReadInt8L());
  324.   iShowToolBand = TBool(aStream.ReadInt8L());
  325.   iMaximizeView = TBool(aStream.ReadInt8L());
  326.  
  327.   iRenderText         = TBool(aStream.ReadInt8L());
  328.   iRenderLineDrawings = TBool(aStream.ReadInt8L());
  329.   iRenderImages       = aStream.ReadInt8L();
  330.   iDitherImages       = TBool(aStream.ReadInt8L());
  331.   iClip               = TBool(aStream.ReadInt8L());
  332. }
  333.  
  334.  
  335. void TPdfUiState::ExternalizeL(RWriteStream& aStream) const
  336. {
  337.   aStream.WriteInt32L(iZoom);
  338.   aStream.WriteInt16L(iRotate);
  339.   aStream.WriteInt8L(TInt8(iDoLinks));
  340.   aStream.WriteInt8L(TInt8(iDrawAccurate));
  341.   
  342.   aStream.WriteInt8L(iHBarVisibility);
  343.   aStream.WriteInt8L(iVBarVisibility);
  344.   
  345.   aStream.WriteInt8L(TInt8(iShowToolBar));
  346.   aStream.WriteInt8L(TInt8(iShowToolBand));
  347.   aStream.WriteInt8L(TInt8(iMaximizeView));
  348.  
  349.   aStream.WriteInt8L(TInt8(iRenderText));
  350.   aStream.WriteInt8L(TInt8(iRenderLineDrawings));
  351.   aStream.WriteInt8L(iRenderImages);
  352.   aStream.WriteInt8L(TInt8(iDitherImages));
  353.   aStream.WriteInt8L(TInt8(iClip));
  354. }
  355.  
  356.  
  357. void TPdfUiState::StoreL(CDictionaryStore* aIniFile, TUid aStreamUid) const
  358. {
  359.   RDictionaryWriteStream writeStream;
  360.   writeStream.AssignLC(*aIniFile,aStreamUid);
  361.   ExternalizeL(writeStream);
  362.   writeStream.CommitL();
  363.   CleanupStack::PopAndDestroy(); // writeStream
  364. }
  365.  
  366.  
  367. void TPdfUiState::RestoreL(CDictionaryStore* aIniFile, TUid aStreamUid)
  368. {
  369.   RDictionaryReadStream readStream;
  370.   readStream.OpenLC(*aIniFile, aStreamUid);
  371.   InternalizeL(readStream);
  372.   CleanupStack::PopAndDestroy(); // readStream
  373. }
  374.  
  375.  
  376. TBool TPdfUiState::operator!=(const TPdfUiState& y)
  377. {
  378.   if (iZoom != y.iZoom)
  379.     return ETrue;
  380.   else if (iHBarVisibility != y.iHBarVisibility)
  381.     return ETrue;
  382.   else if (iVBarVisibility != y.iVBarVisibility)
  383.     return ETrue;
  384.   else if (iDrawAccurate != y.iDrawAccurate)
  385.     return ETrue;
  386.   else if (iRotate != y.iRotate)
  387.     return ETrue;
  388.   else if (iDoLinks != y.iDoLinks)
  389.     return ETrue;
  390.   else if (iShowToolBar != y.iShowToolBar)
  391.     return ETrue;
  392.   else if (iShowToolBand != y.iShowToolBand)
  393.     return ETrue;
  394.   else if (iMaximizeView != y.iMaximizeView)
  395.     return ETrue;
  396.   else if (iRenderText != y.iRenderText)
  397.     return ETrue;
  398.   else if (iRenderLineDrawings != y.iRenderLineDrawings)
  399.     return ETrue;
  400.   else if (iRenderImages != y.iRenderImages)
  401.     return ETrue;
  402.   else if (iDitherImages  != y.iDitherImages)
  403.     return ETrue;
  404.   else if (iClip != y.iClip)
  405.     return ETrue;
  406.   else
  407.     return EFalse;
  408. }
  409.  
  410.  
  411. //////////////////////////////////////////////////////////////////////////////
  412. //
  413. // CSetViewDialog
  414. //
  415. //////////////////////////////////////////////////////////////////////////////
  416. class CSetViewDialog : public CEikDialog
  417. {
  418. public:
  419.   CSetViewDialog(TPdfUiState& aState): iState(aState) {};
  420.   
  421. // -- CEikDialog
  422. protected:
  423.   void PreLayoutDynInitL();
  424.   TBool OkToExitL(TInt aKeycode);
  425.   void HandleControlStateChangeL(TInt aControlId);
  426.  
  427. //--o CSetViewDialog
  428. protected:
  429.   virtual void ShowControls();
  430.  
  431. protected:
  432.   TPdfUiState& iState;
  433. };
  434.  
  435.  
  436. void CSetViewDialog::PreLayoutDynInitL()
  437. {
  438.   TInt index = ZoomToIndex(iState.iZoom);
  439.   SetChoiceListCurrentItem(ESetViewDialogZoomFactor, index);
  440.  
  441.   SetCheckBoxState(ESetViewDialogAccurate, iState.iDrawAccurate ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  442.   
  443.   TInt list;
  444.   switch (iState.iRotate) {
  445.   case  90: list = 1; break;
  446.   case 180: list = 2; break;
  447.   case 270: list = 3; break;
  448.   default : list = 0; break;
  449.   }
  450.   SetChoiceListCurrentItem(ESetViewDialogRotate, list);
  451.  
  452.   SetCheckBoxState(ESetViewDialogDoLinks, iState.iDoLinks ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  453.  
  454.   SetHorOptionButListCurrentSet(ESetViewDialogHBar, iState.iHBarVisibility);
  455.   SetHorOptionButListCurrentSet(ESetViewDialogVBar, iState.iVBarVisibility);
  456.  
  457.   SetCheckBoxState(ESetViewDialogRenderText, iState.iRenderText ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  458.   SetCheckBoxState(ESetViewDialogRenderLineDrawings, iState.iRenderLineDrawings ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  459.   SetHorOptionButListCurrentSet(ESetViewDialogRenderImages, iState.iRenderImages);
  460.   SetCheckBoxState(ESetViewDialogDitherImages, iState.iDitherImages ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  461.   SetCheckBoxState(ESetViewDialogClip, iState.iClip ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  462.   ShowControls();
  463. }
  464.  
  465.  
  466. TBool CSetViewDialog::OkToExitL(TInt /*aKeycode*/) 
  467.   TInt index = ChoiceListCurrentItem(ESetViewDialogZoomFactor);
  468.   switch (index) {
  469.   case 11: iState.iZoom = CPdfPageView::KZoomPage; break;
  470.   case 12: iState.iZoom = CPdfPageView::KZoomWidth; break;
  471.   default: iState.iZoom = KZoom[index]; break;
  472.   }
  473.  
  474.   iState.iDrawAccurate = CheckBoxState(ESetViewDialogAccurate)== CEikButtonBase::ESet;
  475.   
  476.   switch (ChoiceListCurrentItem(ESetViewDialogRotate)) {
  477.   case  1: iState.iRotate =  90; break;
  478.   case  2: iState.iRotate = 180; break;
  479.   case  3: iState.iRotate = 270; break;
  480.   default: iState.iRotate =   0; break;
  481.   }
  482.   
  483.   iState.iDoLinks = CheckBoxState(ESetViewDialogDoLinks) == CEikButtonBase::ESet;
  484.   
  485.   iState.iHBarVisibility = HorOptionButListCurrentSet(ESetViewDialogHBar);
  486.   iState.iVBarVisibility = HorOptionButListCurrentSet(ESetViewDialogVBar);
  487.   
  488.   iState.iRenderText         = CheckBoxState(ESetViewDialogRenderText) == CEikButtonBase::ESet;
  489.   iState.iRenderLineDrawings = CheckBoxState(ESetViewDialogRenderLineDrawings) == CEikButtonBase::ESet;
  490.   iState.iRenderImages       = HorOptionButListCurrentSet(ESetViewDialogRenderImages);
  491.   iState.iDitherImages       = CheckBoxState(ESetViewDialogDitherImages) == CEikButtonBase::ESet;
  492.   iState.iClip               = CheckBoxState(ESetViewDialogClip) == CEikButtonBase::ESet;
  493.   
  494.   return ETrue;
  495. }
  496.  
  497.  
  498. void CSetViewDialog::HandleControlStateChangeL(TInt aControlId)
  499. {
  500.   switch (aControlId) {
  501.   case ESetViewDialogZoomFactor:
  502.     break;
  503.   case ESetViewDialogRenderImages:
  504.     // dim the dither checkbox if images aren't drawn
  505.     ShowControls();
  506.     break;
  507.   }
  508. }
  509.  
  510.  
  511. void CSetViewDialog::ShowControls()
  512. {
  513.   TBool visible = (EImageVisOn == HorOptionButListCurrentSet(ESetViewDialogRenderImages));
  514.   MakeLineVisible(ESetViewDialogDitherImages , visible);
  515. }
  516.  
  517.  
  518. //////////////////////////////////////////////////////////////////////////////
  519. //
  520. // CPreferencesDialog
  521. //
  522. //////////////////////////////////////////////////////////////////////////////
  523. class CPreferencesDialog: public CSetViewDialog
  524. {
  525. public:
  526.   CPreferencesDialog(TPdfUiState& aState): CSetViewDialog(aState) {};
  527.   
  528. // --o CEikDialog
  529. private:
  530.   void PreLayoutDynInitL();
  531.   TBool OkToExitL(TInt aKeycode);
  532.   void HandleControlStateChangeL(TInt aControlId);
  533.  
  534. //--o CPreferencesDialog
  535. private:
  536.   void ShowControls();
  537. };
  538.  
  539.  
  540. void CPreferencesDialog::PreLayoutDynInitL()
  541.   SetCheckBoxState(EPreferencesDialogToolBar     , iState.iShowToolBar  ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  542.   SetCheckBoxState(EPreferencesDialogToolBand    , iState.iShowToolBand ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  543.   SetCheckBoxState(EPreferencesDialogMaximizeView, iState.iMaximizeView ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  544.   CSetViewDialog::PreLayoutDynInitL();
  545. }
  546.  
  547.  
  548. void CPreferencesDialog::HandleControlStateChangeL(TInt aControlId)
  549. {
  550.   // dim the toolbar controls if miximise view is on
  551.   if (aControlId == EPreferencesDialogMaximizeView)
  552.     ShowControls();
  553.   else
  554.     CSetViewDialog::HandleControlStateChangeL(aControlId);
  555. }
  556.  
  557.  
  558. TBool CPreferencesDialog::OkToExitL(TInt aKeycode) 
  559. {
  560.   if (CSetViewDialog::OkToExitL(aKeycode)) {
  561.     
  562.     iState.iShowToolBar  = CheckBoxState(EPreferencesDialogToolBar) == CEikButtonBase::ESet;
  563.     iState.iShowToolBand = CheckBoxState(EPreferencesDialogToolBand)== CEikButtonBase::ESet;
  564.     
  565.     iState.iMaximizeView = CheckBoxState(EPreferencesDialogMaximizeView) == CEikButtonBase::ESet;
  566.     return(ETrue);
  567.   }
  568.   else
  569.     return EFalse;
  570. }
  571.  
  572.  
  573. void CPreferencesDialog::ShowControls()
  574. {
  575.   CSetViewDialog::ShowControls();
  576.  
  577.   int visible = CheckBoxState(EPreferencesDialogMaximizeView) == CEikButtonBase::EClear;
  578.   
  579.   MakeLineVisible(EPreferencesDialogToolBar , visible);
  580.   MakeLineVisible(EPreferencesDialogToolBand, visible);
  581. }
  582.  
  583.  
  584. //////////////////////////////////////////////////////////////////////////////
  585. //
  586. // CPdfAppUi
  587. //
  588. //////////////////////////////////////////////////////////////////////////////
  589. const TUid CPdfAppUi::KPreferencesStreamUid = { 1 };
  590.  
  591. void CPdfAppUi::ConstructL()
  592. {
  593.   // Allow base class (CEikAppUi) to perform necessary construction
  594.   BaseConstructL();
  595.  
  596.   // Construct the container control which forms the main view
  597.   // for this application. Construction requires the rectangle
  598.   // available.
  599.   iPageView = new(ELeave) CPdfPageView();
  600.   iPageView->ConstructL(ClientRect());
  601.   iPageView->SetCmdHandler(this);
  602.  
  603.   iAppView = iPageView;
  604.  
  605.   // The container is added to the control stack (for key event handling).
  606.   AddToStackL(iAppView);
  607.  
  608.   // hack toolband: set the font of the page movement buttons to the eikon symbolic font
  609.   for (TInt i=EPdfCmdFirstPage; i<=EPdfCmdLastPage; i++) {
  610.     if (i == EPdfLblCurrentPage)
  611.       i = EPdfCmdNextPage;
  612.     CEikCommandButton* button = STATIC_CAST(CEikCommandButton*, iToolBand->ControlById(i));
  613.     CEikLabel* label = button->Label();
  614.     label->SetFont(iEikonEnv->SymbolFont());
  615.   }
  616.  
  617.   // draw toolbars
  618.   iToolBar->DrawNow();
  619.   iToolBand->DrawNow();
  620.  
  621.   // int ui state
  622.   iUiState = TPdfUiState();
  623.   ReflectUiStateL(); 
  624.  
  625.   // create the find model and -list
  626.   iFindModel=new(ELeave) SEdwinFindModel;
  627.   iFindModel->iFlags=0;
  628.   iFindList=new(ELeave) CDesCArrayFlat(5);
  629.   iSearchState = 0;
  630.  
  631.   // clear the fields
  632.   CEikLabel* maxPage=STATIC_CAST(CEikLabel*, iToolBand->ControlById(EPdfLblMaxPage));
  633.   maxPage->SetTextL(_L(" "));
  634.     
  635.   CEikLabel* page=STATIC_CAST(CEikLabel*, iToolBand->ControlById(EPdfLblCurrentPage));
  636.   page->SetTextL(_L(" "));
  637. }
  638.     
  639.  
  640. CPdfAppUi::~CPdfAppUi()
  641. {
  642.   delete iSearchState;
  643.   delete iFindModel;
  644.   iFindList->Reset();
  645.   delete iFindList;
  646.  
  647.   delete iPopoutListBox;
  648.   
  649.   delete iPageView;
  650. }
  651.  
  652.  
  653. TBool CPdfAppUi::ProcessCommandParametersL(TApaCommand aCommand,TFileName& aDocumentName,const TDesC& /* aTail */)
  654. {
  655.   if (aCommand==EApaCommandCreate)
  656.     return EFalse; // assume aDocumentName is valid
  657.  
  658.   if (aCommand==EApaCommandRun || aCommand==EApaCommandBackground) {
  659.     TFileName fileName=iEikonEnv->Process()->LastUsedFileL(CEikProcess::ELastOpenFile);
  660.     if (ConeUtils::FileExists(fileName)) {
  661.       aDocumentName=fileName;
  662.       return ETrue;
  663.     }
  664.   }
  665.   return ConeUtils::FileExists(aDocumentName);
  666. }
  667.  
  668.  
  669. TFileName CPdfAppUi::CurrentFilePath()const
  670. {
  671.   TParsePtrC parser(Document()->Process()->MainDocFileName());
  672.   return parser.DriveAndPath();
  673. }
  674.  
  675.  
  676. // checks that the initial path exists, creating it if not
  677. // if the path was read-only a default is used instead
  678. void CPdfAppUi::SetInitialPathL(TFileName& aFileName)const
  679. {
  680.   aFileName=CurrentFilePath();
  681.   TRAPD(ret,ConeUtils::EnsurePathExistsL(aFileName) );
  682.   if (ret!=KErrNone) {
  683.     // if the path is eg read-only default to C:
  684.     aFileName=R_DEFAULT_PATH;
  685.     ConeUtils::EnsurePathExistsL(aFileName);
  686.   }
  687. }
  688.  
  689.  
  690. void CPdfAppUi::SetModel()
  691. {
  692.   // Get the model via an ugly cast
  693.   iDoc=((CPdfDocument*)iDocument)->Model();
  694.   
  695.   CEikLabel* maxPage=STATIC_CAST(CEikLabel*, iToolBand->ControlById(EPdfLblMaxPage));
  696.   if (iDoc) {
  697.     
  698.     TBuf<10> num(10);
  699.     num.Num(iDoc->getNumPages());
  700.     maxPage->SetTextL(num);
  701.    
  702.     // Set the UI vars
  703.     CEikLabel* page=STATIC_CAST(CEikLabel*, iToolBand->ControlById(EPdfLblCurrentPage));
  704.     num.Num(1);
  705.     page->SetTextL(num);
  706.     page->DrawNow();
  707.   }
  708.   else {
  709.     maxPage->SetTextL(_L(" "));
  710.   }
  711.   maxPage->DrawNow();
  712. }
  713.  
  714.  
  715. void CPdfAppUi::SetPage(TInt aPage)
  716. {
  717.   CEikLabel* pageControl=STATIC_CAST(CEikLabel*, iToolBand->ControlById(EPdfLblCurrentPage));
  718.  
  719.   TBuf<10> num(10);
  720.   num.Num(aPage);
  721.   pageControl->SetTextL(num);
  722.   pageControl->DrawNow();
  723.  
  724.   iPage = aPage;
  725. }
  726.  
  727.  
  728. void CPdfAppUi::UpdateFileNameLabelL()
  729. {
  730.   // draw the label that is part of the toolbar
  731.   CEikFileNameLabel* label=STATIC_CAST(CEikFileNameLabel*,iToolBar->ControlById(EPdfLblFileName));
  732.   label->UpdateL();
  733.   label->DrawNow();
  734.  
  735.   // draw the same label that is part of the toolband
  736.   label=STATIC_CAST(CEikFileNameLabel*,iToolBand->ControlById(EPdfLblFileName));
  737.   label->UpdateL();
  738.   label->DrawNow();
  739. }
  740.  
  741.  
  742. void CPdfAppUi::HandleModelChangeL()
  743. {
  744.   SetModel();
  745.   iPageView->SetModelL(iDoc);
  746.  
  747.   // update visible signs of the ui state 
  748.   iSelection = TRect();
  749.   iPageView->SetSelection(iSelection);
  750.   SetUiStateForModelL();
  751.   ReflectUiStateL();
  752.  
  753.   // set the filename
  754.   iEikonEnv->UpdateTaskNameL();
  755.   UpdateFileNameLabelL();  
  756.  
  757.   // start at the first page
  758.   SetPage(iDoc ? 1 : 0);
  759.  
  760.   if (iDoc && iUiState.iDoLinks)
  761.     iDoc->getLinksL(1);
  762.  
  763.   // draw the first page
  764.   StartRenderingL();
  765. }
  766.  
  767.  
  768. void CPdfAppUi::SetUiStateForModelL()
  769. {
  770.   LoadUiStateL(CPdfAppUi::KPreferencesStreamUid, iUiState);
  771. }
  772.  
  773.  
  774. void CPdfAppUi::StartRenderingL()
  775. {
  776.   if (iDoc)
  777.     iPageView->StartRenderingL(iPage, iUiState);
  778. }
  779.  
  780.  
  781. void CPdfAppUi::DynInitMenuPaneL(int aMenuId, CEikMenuPane *aMenuPane)
  782. {
  783.   switch (aMenuId) {
  784.   case R_FILE_MENU:
  785.     if (!iDoc)
  786.       aMenuPane->SetItemDimmed(EPdfCmdDocumentInfo, ETrue);
  787.     break;
  788.   
  789.   case R_MORE_MENU:
  790.     if (!iDoc || !(iDoc->okToCopy()))
  791.       aMenuPane->SetItemDimmed(EPdfCmdExportAsText, ETrue);
  792.     break;
  793.  
  794.   case R_FIND_MENU:
  795.     if (!iDoc) {
  796.       aMenuPane->SetItemDimmed(EEikCmdSearchFind, ETrue);
  797.       aMenuPane->SetItemDimmed(EEikCmdSearchFindAgain, ETrue);
  798.       aMenuPane->SetItemDimmed(EPdfCmdGoTo, ETrue);
  799.     }
  800.     else if (!iSearchState)
  801.       aMenuPane->SetItemDimmed(EEikCmdSearchFindAgain, ETrue);
  802.     break;
  803.  
  804.   case R_VIEW_MENU:
  805.     if (iToolBar->IsVisible())
  806.       aMenuPane->SetItemButtonState(EPdfCmdShowToolBar, EEikMenuItemSymbolOn);
  807.     if (iToolBand->IsVisible())
  808.       aMenuPane->SetItemButtonState(EPdfCmdShowToolBand, EEikMenuItemSymbolOn);
  809.  
  810.     aMenuPane->SetItemDimmed(EPdfCmdShowToolBar, iUiState.iMaximizeView);
  811.     aMenuPane->SetItemDimmed(EPdfCmdShowToolBand, iUiState.iMaximizeView);
  812.     if (iUiState.iMaximizeView)
  813.       aMenuPane->SetItemButtonState(EPdfCmdMaximizeView, EEikMenuItemSymbolOn);
  814.     break;
  815.  
  816. #if defined(OOM) || defined(TRACE)
  817.   case R_TOOLS_MENU: {
  818.       CEikMenuPane::TItem::SData extraItem;
  819.       extraItem.iCommandId=0;
  820.       extraItem.iCascadeId=R_DEBUG_MENU;
  821.       extraItem.iFlags=0;
  822.       extraItem.iText=_L("Debug");
  823.       aMenuPane->AddMenuItemL(extraItem);
  824.     }
  825.     break;
  826.   
  827.   case R_DEBUG_MENU:
  828. #ifndef TRACE
  829.     aMenuPane->SetItemDimmed(EPdfTstShowProfile, ETrue);
  830. #endif
  831. #ifndef OOM
  832.     aMenuPane->SetItemDimmed(EPdfTstOOM, ETrue);
  833. #endif
  834.     break;
  835.  
  836. #endif
  837.  
  838.   // default silk screen menu's
  839.   case R_EIK_SIDEBAR_EDIT_MENU:
  840.     aMenuPane->SetItemDimmed(EEikCmdEditCut, ETrue);
  841.     aMenuPane->SetItemDimmed(EEikCmdEditCopy, ETrue);
  842.     aMenuPane->SetItemDimmed(EEikCmdEditPaste, ETrue);
  843.     break;
  844.  
  845.   case R_EIK_SIDEBAR_IRDA_MENU:
  846.     aMenuPane->SetItemDimmed(EEikCmdIrdaSend, ETrue);
  847.     aMenuPane->SetItemDimmed(EEikCmdIrdaReceive, ETrue);
  848.     break;
  849.   
  850.   default:
  851.     break;
  852.   }
  853. }
  854.  
  855.  
  856. void CPdfAppUi::OpenFileL(const TDesC& aFileName)
  857. {   
  858.   // pdf does not write files: no need to save anything
  859.  
  860.   STATIC_CAST(CPdfDocument*,iDocument)->DoOpenFileL(aFileName);
  861.   HandleModelChangeL();
  862. }
  863.  
  864.  
  865. TErrorHandlerResponse CPdfAppUi::HandleError(TInt aError, const SExtendedError& aExtErr, TDes& aErrorText, TDes& aContextText)
  866. {
  867.   iAppView->DrawDeferred();
  868.   return CEikAppUi::HandleError(aError, aExtErr, aErrorText, aContextText);
  869. }
  870.  
  871.  
  872. void CPdfAppUi::SetDrawAccurate(TBool aDrawAccurate)
  873. {
  874.   iUiState.iDrawAccurate = aDrawAccurate;
  875.   
  876.   // redraw the button
  877.   CEikButtonBase* b = (CEikButtonBase*)iToolBand->ControlById(EPdfCmdDrawAccurate);
  878.   b->SetState(iUiState.iDrawAccurate ? CEikButtonBase::ESet : CEikButtonBase::EClear);
  879.   b->DrawNow();
  880. }
  881.  
  882.  
  883. void CPdfAppUi::SetZoom(TInt aZoom)
  884. {
  885.   // turn all buttons off
  886.   CEikButtonBase* b = (CEikButtonBase*)iToolBand->ControlById(EPdfCmdFitActualSize);
  887.   b->SetState(CEikButtonBase::EClear);
  888.   b->DrawNow();
  889.  
  890.   b = (CEikButtonBase*)iToolBand->ControlById(EPdfCmdFitPage);
  891.   b->SetState(CEikButtonBase::EClear);
  892.   b->DrawNow();
  893.   
  894.   b = (CEikButtonBase*)iToolBand->ControlById(EPdfCmdFitWidth);
  895.   b->SetState(CEikButtonBase::EClear);
  896.   b->DrawNow();
  897.   
  898.   // set new state
  899.   iUiState.iZoom = aZoom;
  900.   TInt id;
  901.   if (iUiState.iZoom == 100)
  902.     id = EPdfCmdFitActualSize;
  903.   else if (CPdfPageView::KZoomPage == iUiState.iZoom)
  904.     id = EPdfCmdFitPage;
  905.   else if (CPdfPageView::KZoomWidth == iUiState.iZoom)
  906.     id = EPdfCmdFitWidth;
  907.   else
  908.     return;
  909.   
  910.   b = (CEikButtonBase*)iToolBand->ControlById(id);
  911.   b->SetState(CEikButtonBase::ESet);
  912.   b->DrawNow();
  913. }
  914.  
  915.  
  916. void CPdfAppUi::DoZoomL(TInt aZoom)
  917. {
  918.   TPdfPageViewMemento zoomPars = iUiState;
  919.   zoomPars.iZoom = aZoom;
  920.  
  921.   TPoint center = iPageView->Center();
  922.  
  923.   iPageView->StartRenderingL(iPage, zoomPars);
  924.   SetZoom(zoomPars.iZoom);
  925.   if (!iSelection.IsEmpty())
  926.     iPageView->SetSelection(iSelection);
  927.   else
  928.     iPageView->SetCenter(center);
  929. }
  930.  
  931.  
  932. void CPdfAppUi::SetToolBarsL(TBool aShowToolBar, TBool aShowToolBand)
  933. {
  934.   // Note: don't use Ui state vars iShowToolBar and -Band here
  935.   if (aShowToolBar != iToolBar->IsVisible())
  936.     iToolBar->MakeVisible(aShowToolBar);
  937.   
  938.   if (aShowToolBand != iToolBand->IsVisible())
  939.     iToolBand->MakeVisible(aShowToolBand);
  940.  
  941.   if (aShowToolBar)
  942.     iToolBand->ControlById(EPdfLblFileName)->DrawNow();  // redraw the filename label only
  943.   else if (aShowToolBand) {
  944.     TRect bottomBorder=iAppView->Rect();
  945.     bottomBorder.iTl.iY=bottomBorder.iBr.iY-1;
  946.     STATIC_CAST(RWindow*, iAppView->DrawableWindow())->Invalidate(bottomBorder);
  947.   }
  948.     
  949.   TInt adjacent=ECoeAdjNone;
  950.   if (aShowToolBar)
  951.     adjacent|=ECoeAdjRight;
  952.   if (aShowToolBand)
  953.     adjacent|=ECoeAdjTop;
  954.     
  955.   iAppView->SetAdjacent(adjacent);
  956.   iAppView->SetRectL(ClientRect());
  957. }
  958.  
  959.  
  960. void CPdfAppUi::SetMaximizeViewL(TBool aMaximizeView)
  961. {
  962.   iUiState.iMaximizeView = aMaximizeView;
  963.   if (iUiState.iMaximizeView) {
  964.     SetToolBarsL(EFalse, EFalse);
  965.     iPageView->SetScrollBarVisibility(0, 0);
  966.   }
  967.   else {
  968.     SetToolBarsL(iUiState.iShowToolBar, iUiState.iShowToolBand);
  969.     iPageView->SetScrollBarVisibility(iUiState.iHBarVisibility, 
  970.                                      iUiState.iVBarVisibility);
  971.   }
  972. }
  973.  
  974.  
  975. void CPdfAppUi::ReflectUiStateL()
  976. {
  977.   SetMaximizeViewL(iUiState.iMaximizeView);
  978.   SetDrawAccurate(iUiState.iDrawAccurate);
  979.   SetZoom(iUiState.iZoom);
  980. }
  981.  
  982.  
  983. void CPdfAppUi::UpdateUiStateL(const TPdfUiState& aNewState)
  984. {
  985.   TBool doRedraw = EFalse;
  986.   TBool doDisplay= EFalse;
  987.   TBool doLinks  = iUiState.iDoLinks != aNewState.iDoLinks;
  988.  
  989.   if (doLinks ||
  990.       iUiState.iZoom         != aNewState.iZoom         ||
  991.       iUiState.iDrawAccurate != aNewState.iDrawAccurate ||
  992.       iUiState.iRotate       != aNewState.iRotate       ||
  993.       iUiState.iRenderText   != aNewState.iRenderText   ||
  994.       iUiState.iRenderLineDrawings != aNewState.iRenderLineDrawings ||
  995.       iUiState.iRenderImages != aNewState.iRenderImages ||
  996.       iUiState.iDitherImages != aNewState.iDitherImages ||
  997.       iUiState.iClip         != aNewState.iClip)
  998.     doDisplay = ETrue;
  999.  
  1000.   if (iUiState.iHBarVisibility != aNewState.iHBarVisibility ||
  1001.       iUiState.iVBarVisibility != aNewState.iVBarVisibility ||
  1002.       iUiState.iShowToolBar  != aNewState.iShowToolBar  ||
  1003.       iUiState.iShowToolBand != aNewState.iShowToolBand ||
  1004.       iUiState.iMaximizeView != aNewState.iMaximizeView ||
  1005.       (iUiState.iZoom==CPdfPageView::KZoomPage && iUiState.iZoom!=aNewState.iZoom) ||
  1006.       (iUiState.iZoom==CPdfPageView::KZoomWidth&& iUiState.iZoom!=aNewState.iZoom) 
  1007.      )
  1008.     doRedraw = ETrue;
  1009.  
  1010.   iUiState.iHBarVisibility = aNewState.iHBarVisibility;
  1011.   iUiState.iVBarVisibility = aNewState.iVBarVisibility;
  1012.   iUiState.iShowToolBar  = aNewState.iShowToolBar;
  1013.   iUiState.iShowToolBand = aNewState.iShowToolBand;
  1014.   SetMaximizeViewL(aNewState.iMaximizeView);
  1015.   
  1016.   if (doLinks)
  1017.     iDoc->getLinksL(iPage);
  1018.  
  1019.   if (doDisplay && iDoc) {
  1020.    
  1021.     TPoint center = iPageView->Center();
  1022.     iPageView->StartRenderingL(iPage, aNewState);
  1023.     if (!iSelection.IsEmpty())
  1024.       iPageView->SetSelection(iSelection);
  1025.     else if (iUiState.iZoom != aNewState.iZoom)
  1026.       iPageView->SetCenter(center);
  1027.   
  1028.   }
  1029.   else if (doRedraw) {
  1030.     iPageView->DrawNow();
  1031.   }
  1032.  
  1033.   SetDrawAccurate(aNewState.iDrawAccurate);
  1034.   SetZoom(aNewState.iZoom);
  1035.   iUiState.iRenderText   = aNewState.iRenderText;
  1036.   iUiState.iRenderLineDrawings = aNewState.iRenderLineDrawings;
  1037.   iUiState.iRenderImages = aNewState.iRenderImages;
  1038.   iUiState.iDitherImages = aNewState.iDitherImages;
  1039.   iUiState.iClip         = aNewState.iClip;
  1040.   iUiState.iRotate       = aNewState.iRotate;
  1041.   iUiState.iDoLinks      = aNewState.iDoLinks;  
  1042. }
  1043.  
  1044.  
  1045. void CPdfAppUi::LoadUiStateL(const TUid& aStreamUid, TPdfUiState& aUiState)
  1046. {
  1047.   CDictionaryStore* iniFile = 
  1048.     iDocument->Application()->OpenIniFileLC(iDocument->Process()->FsSession());
  1049.  
  1050.   if (iniFile->IsPresentL(aStreamUid)) {
  1051.     TRAPD(ignore, aUiState.RestoreL(iniFile, aStreamUid));
  1052.     if (ignore)
  1053.       aUiState.SetDefaults();
  1054.   }
  1055.   else
  1056.     aUiState.SetDefaults();
  1057.   CleanupStack::PopAndDestroy(); //iniFile
  1058. }
  1059.  
  1060.  
  1061. void CPdfAppUi::StoreUiStateL(const TUid& aStreamUid, const TPdfUiState& aUiState) const
  1062. {
  1063.   CDictionaryStore* iniFile = 
  1064.     iDocument->Application()->OpenIniFileLC(iDocument->Process()->FsSession());
  1065.  
  1066.   aUiState.StoreL(iniFile, aStreamUid);
  1067.   
  1068.   iniFile->CommitL();
  1069.   CleanupStack::PopAndDestroy(); // iniFile
  1070. }
  1071.  
  1072.  
  1073. void CPdfAppUi::DoSearch()
  1074. {
  1075.   if (iSearchState->FindNextL()) {
  1076.     if (iSearchState->Page() != iPage)
  1077.       ChangePageL(iSearchState->Page());
  1078.     iSelection = iSearchState->Selection();
  1079.     iPageView->SetSelection(iSelection);
  1080.   }
  1081.   else {
  1082.     delete iSearchState;
  1083.     iSearchState = 0;
  1084.     iSelection = TRect(0,0,0,0);
  1085.     iPageView->SetSelection(iSelection);
  1086.     iEikonEnv->InfoMsg(R_NOTHING_FOUND);
  1087.   }
  1088. }
  1089.  
  1090.  
  1091. void CPdfAppUi::HandleCommandL(TInt aCommand)
  1092. {
  1093.  
  1094.   switch (aCommand) {
  1095.   case EEikCmdFileOpen:        CmdFileOpenL();     break;
  1096.   case EPdfCmdDocumentInfo:    CmdDocumentInfoL(); break;
  1097.   case EPdfCmdExportAsText:    CmdExportAsTextL(); break;
  1098.   case EEikCmdExit:            CmdExit();          break;
  1099.  
  1100.   case EEikCmdSearchFind:      CmdSearchFindL();      break;
  1101.   case EEikCmdSearchFindAgain: CmdSearchFindAgainL(); break;
  1102.   case EPdfCmdGoTo:            CmdGoToL();            break;
  1103.   
  1104.   case EEikCmdZoomIn:          CmdZoomInL();               break;
  1105.   case EEikCmdZoomOut:         CmdZoomOutL();              break;
  1106.   case EPdfCmdSetView:         CmdSetViewL();              break;
  1107.   case EPdfCmdShowToolBar:
  1108.   case EPdfCmdShowToolBand:    CmdShowToolBarsL(aCommand); break;
  1109.   case EPdfCmdMaximizeView:    CmdMaximizeViewL();         break;
  1110.  
  1111.   case EPdfCmdPreferences:     CmdPreferencesL();      break;
  1112.   case EEikCmdHelpContents:    DoHelpContentsL(ETrue); break; // show Help    
  1113.   case EEikCmdHelpAbout:       CmdHelpAboutL();        break;
  1114.     
  1115. #ifdef TRACE
  1116.   case EPdfTstShowProfile:     CmdShowProfile();  break;
  1117. #endif
  1118. #ifdef OOM
  1119.   case EPdfTstOOM:             CmdOOML(); break;
  1120. #endif
  1121.  
  1122.   case EPdfCmdZoomPopup:       CmdZoomPopupL();     break;
  1123.   case EPdfCmdFitActualSize:   CmdFitActualSizeL(); break;
  1124.   case EPdfCmdFitPage:         CmdFitPageL();       break;
  1125.   case EPdfCmdFitWidth:        CmdFitWidthL();      break;
  1126.   
  1127.   case EPdfCmdFirstPage:       CmdFirstPageL();     break;
  1128.   case EPdfCmdPrevPage:        CmdPrevPageL();      break;
  1129.   case EPdfCmdNextPage:        CmdNextPageL();      break;
  1130.   case EPdfCmdLastPage:        CmdLastPageL();      break;
  1131.   
  1132.   case EPdfCmdDrawAccurate:  CmdDrawAccurateL();    break;
  1133.  
  1134.   default: break;
  1135.   }
  1136. }
  1137.  
  1138.  
  1139. void CPdfAppUi::CmdFileOpenL()
  1140. {
  1141.   _LIT(KPdfExtension, "pdf");
  1142.  
  1143.   // Set an initial value for the folder to contain the new file
  1144.   // Pass this to the dialog box to display in the "Folder" field.
  1145.   TFileName filename(CurrentFilePath());
  1146.   SetInitialPathL(filename);
  1147.     
  1148.   // Launch the standard EIKON dialog for the user to enter a filename
  1149.   CEikFileOpenDialog* dialog=new(ELeave) CEikFileOpenDialog(&filename);
  1150.   dialog->SetRequiredExtension(&KPdfExtension);
  1151.  
  1152.   // If the dialog returns a filename, open the file.
  1153.   if (dialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN)) {
  1154.     
  1155.     iPageView->CancelRendering();
  1156.     delete iSearchState;
  1157.     iSearchState = 0;
  1158.  
  1159.     OpenFileL(filename);
  1160.   }
  1161. }
  1162.  
  1163.  
  1164. void CPdfAppUi::CmdDocumentInfoL()
  1165. {
  1166.   if (!iDoc) {
  1167.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1168.     return;
  1169.   }
  1170.  
  1171.   CEikDialog* dialog = new(ELeave) CPdfDocInfoDialog(iDoc);
  1172.   dialog->ExecuteLD(R_DOC_INFO_DIALOG);
  1173. }
  1174.  
  1175.  
  1176. void CPdfAppUi::CmdExportAsTextL()
  1177. {
  1178.   if (!iDoc) {
  1179.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1180.     return;
  1181.   }
  1182.  
  1183.   // check for copy permission
  1184.   if (!iDoc->okToCopy()) {
  1185.     iEikonEnv->InfoMsg(R_COPYING_OF_TEXT_FROM_THIS_DOCUMENT_IS_NOT_ALLOWED);
  1186.     return;
  1187.   }
  1188.  
  1189.   TFileName textFileName=*iDoc->getFileName();
  1190.   textFileName.Replace(textFileName.Length()-4, 4, _L(".txt"));
  1191.   //filename.Append(_L(".txt"));
  1192.   ConeUtils::EnsurePathExistsL(textFileName);
  1193.   CEikDialog* dialog=new(ELeave) CEikFileSaveAsDialog(&textFileName,NULL,R_EXPORT_AS_TEXT_DIALOG_TITLE);
  1194.   if (!dialog->ExecuteLD(R_EIK_DIALOG_FILE_SAVEAS))
  1195.     return;
  1196.   
  1197.   // get page range
  1198.   TInt firstPage = 1;
  1199.   TInt lastPage  = iDoc->getNumPages();
  1200.   
  1201.   GBool useASCII7 = gFalse;
  1202.   GBool rawOrder = gFalse;
  1203.  
  1204.   // exporting as text takes a while
  1205.   iAppView->DrawNow();
  1206.   iEikonEnv->BusyMsgL(R_EIK_TBUF_EXPORTING, KBusyInitialDelay);
  1207.   
  1208.   TextOutputDev* textOut = new(ELeave) TextOutputDev();
  1209.   CleanupStack::PushL(textOut);
  1210.   textOut->ConstructL(&textFileName, useASCII7, rawOrder);
  1211.   if (textOut->isOk())
  1212.     iDoc->displayPagesL(textOut, firstPage, lastPage, 72, 0);
  1213.   CleanupStack::PopAndDestroy(); // delete textOut;
  1214.   
  1215.   iEikonEnv->BusyMsgCancel();
  1216. }
  1217.  
  1218.  
  1219. void CPdfAppUi::CmdExit()
  1220. {
  1221.   TRAPD(ignore, DoHelpContentsL(EFalse)); // stop our 'Help' app if it is running
  1222.   iPageView->CancelRendering();
  1223.   Exit();
  1224. }
  1225.  
  1226.  
  1227. void CPdfAppUi::CmdSearchFindL()
  1228. {
  1229.   if (!iDoc) {
  1230.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1231.     return;
  1232.   }
  1233.   
  1234.   iFindModel->iFlags&=(~CSearchState::EFindAgain);
  1235.   
  1236.   CEikEdwinFindDialog* dialog=new(ELeave) CEikEdwinFindDialog(iFindModel, iFindList);
  1237.   if (dialog->ExecuteLD(R_PDF_FIND_DIALOG)) {
  1238.   
  1239.     iSelection = TRect(0,0,0,0);
  1240.     iPageView->SetSelection(iSelection);
  1241.     
  1242.     delete iSearchState;
  1243.     iSearchState = 0;
  1244.     iSearchState = new(ELeave) CSearchState(iDoc, iPage, iFindModel);
  1245.     iSearchState->ConstructL();
  1246.     
  1247.     DoSearch();
  1248.   }
  1249. }
  1250.  
  1251.  
  1252. void CPdfAppUi::CmdSearchFindAgainL()
  1253. {
  1254.   if (!iDoc) {
  1255.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1256.     return;
  1257.   }
  1258.   iFindModel->iFlags|=CSearchState::EFindAgain;
  1259.   if (iSearchState)
  1260.     DoSearch();
  1261. }
  1262.  
  1263. void CPdfAppUi::ChangePageL(TInt aPage, TBool aTop)
  1264. {
  1265.   iPageView->CancelRendering();
  1266.   iSelection = TRect(0,0,0,0);
  1267.   iPageView->SetSelection(iSelection);
  1268.   if (aTop)
  1269.     iPageView->ScrollToTop();
  1270.   else
  1271.     iPageView->ScrollToBottom();
  1272.   if (iUiState.iDoLinks)
  1273.     iDoc->getLinksL(aPage);
  1274.   iPageView->StartRenderingL(aPage, iUiState);
  1275.   SetPage(aPage);
  1276. }
  1277.  
  1278. void CPdfAppUi::CmdFirstPageL()
  1279. {
  1280.   if (!iDoc) {
  1281.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1282.     return;
  1283.   }
  1284.  
  1285.   if (iPage != 1)
  1286.     ChangePageL(1);
  1287. }
  1288.  
  1289. void CPdfAppUi::CmdPrevPageL(TBool aTop)
  1290.   if (!iDoc) {
  1291.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1292.     return;
  1293.   }
  1294.  
  1295.   if (iPage > 1)
  1296.     ChangePageL(iPage-1, aTop);
  1297. }
  1298.  
  1299. void CPdfAppUi::CmdNextPageL()
  1300. {
  1301.   if (!iDoc) {
  1302.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1303.     return;
  1304.   }
  1305.  
  1306.   if (iPage < iDoc->getNumPages())
  1307.     ChangePageL(iPage+1);
  1308. }
  1309.  
  1310.  
  1311. void CPdfAppUi::CmdLastPageL(TBool aTop)
  1312. {
  1313.   if (!iDoc) {
  1314.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1315.     return;
  1316.   }
  1317.  
  1318.   if (iPage != iDoc->getNumPages())
  1319.     ChangePageL(iDoc->getNumPages(), aTop);
  1320. }
  1321.  
  1322.  
  1323. void CPdfAppUi::CmdCancelL()
  1324. {
  1325.   iSelection = TRect(0,0,0,0);
  1326.   iPageView->SetSelection(iSelection);
  1327.   iPageView->DrawNow();
  1328. }
  1329.  
  1330.  
  1331. void CPdfAppUi::CmdGoToL()
  1332. {
  1333.   if (!iDoc) {
  1334.     iEikonEnv->InfoMsg(R_OPEN_A_DOCUMENT);
  1335.     return;
  1336.   }
  1337.  
  1338.   TInt newPage = iPage;
  1339.   CEikPreviewGotoDialog* dialog=new(ELeave) CEikPreviewGotoDialog(newPage, iDoc->getNumPages());
  1340.   if (dialog->ExecuteLD(R_EIK_DIALOG_PREVIEW_GOTO)) {
  1341.     if (newPage != iPage)
  1342.       ChangePageL(newPage);
  1343.   }
  1344. }
  1345.  
  1346.  
  1347. void CPdfAppUi::CmdShowToolBarsL(TInt aCommand)
  1348. {
  1349.   if (aCommand==EPdfCmdShowToolBar)
  1350.     iUiState.iShowToolBar  = !(iUiState.iShowToolBar);
  1351.   else
  1352.     iUiState.iShowToolBand = !(iUiState.iShowToolBand);
  1353.   
  1354.   // toggle state but do not show it
  1355.   if (!iUiState.iMaximizeView)
  1356.     SetToolBarsL(iUiState.iShowToolBar, iUiState.iShowToolBand);
  1357.   
  1358.   if (iDoc && (iUiState.iZoom == CPdfPageView::KZoomPage || iUiState.iZoom == CPdfPageView::KZoomWidth)) 
  1359.     StartRenderingL();
  1360.   else
  1361.     iAppView->DrawNow();
  1362. }
  1363.  
  1364.  
  1365. void CPdfAppUi::CmdMaximizeViewL()
  1366. {
  1367.   SetMaximizeViewL(!iUiState.iMaximizeView);
  1368.   if (iDoc && (iUiState.iZoom == CPdfPageView::KZoomPage || iUiState.iZoom == CPdfPageView::KZoomWidth))
  1369.     StartRenderingL();
  1370.   else
  1371.     iAppView->DrawNow();
  1372. }
  1373.  
  1374.  
  1375. void CPdfAppUi::CmdZoomInL()
  1376. {
  1377.   if (iDoc) {
  1378.    
  1379.     TInt oldZoom;
  1380.     TInt zoom;
  1381.     
  1382.     if (iUiState.iZoom==CPdfPageView::KZoomPage || iUiState.iZoom==CPdfPageView::KZoomWidth)
  1383.       oldZoom = iPageView->RealZoom();
  1384.     else
  1385.       oldZoom = iUiState.iZoom;
  1386.  
  1387.     if (oldZoom <  33)
  1388.       zoom =  33;
  1389.     else if (oldZoom <  50)
  1390.       zoom =  50;
  1391.     else if (oldZoom <  66)
  1392.       zoom =  66;
  1393.     else if (oldZoom <  75)
  1394.       zoom =  75;
  1395.     else if (oldZoom <  87)
  1396.       zoom =  87;
  1397.     else if (oldZoom < 100)
  1398.       zoom = 100;
  1399.     else if (oldZoom < 125)
  1400.       zoom = 125;
  1401.     else if (oldZoom < 150)
  1402.       zoom = 150;
  1403.     else if (oldZoom < 200)
  1404.       zoom = 200;
  1405.     else if (oldZoom < 300)
  1406.       zoom = 300;
  1407.     else if (oldZoom < 400)
  1408.       zoom = 400;
  1409.     else
  1410.       zoom =  33;
  1411.     
  1412.     DoZoomL(zoom);
  1413.   }
  1414. }
  1415.  
  1416.  
  1417. void CPdfAppUi::CmdZoomOutL()
  1418. {
  1419.   if (iDoc) {
  1420.  
  1421.     TInt oldZoom;
  1422.     TInt zoom;
  1423.     
  1424.     if (iUiState.iZoom==CPdfPageView::KZoomPage || iUiState.iZoom==CPdfPageView::KZoomWidth)
  1425.       oldZoom = iPageView->RealZoom();
  1426.     else
  1427.       oldZoom = iUiState.iZoom;
  1428.     
  1429.     if (oldZoom > 400)
  1430.       zoom = 400;
  1431.     else if (oldZoom > 300)
  1432.       zoom = 300;
  1433.     else if (oldZoom > 200)
  1434.       zoom = 200;
  1435.     else if (oldZoom > 125)
  1436.       zoom = 125;
  1437.     else if (oldZoom > 100)
  1438.       zoom = 100;
  1439.     else if (oldZoom >  87)
  1440.       zoom =  87;
  1441.     else if (oldZoom >  75)
  1442.       zoom =  75;
  1443.     else if (oldZoom >  66)
  1444.       zoom =  66;
  1445.     else if (oldZoom >  50)
  1446.       zoom =  50;
  1447.     else if (oldZoom >  33)
  1448.       zoom =  33;
  1449.     else
  1450.       zoom = 400;
  1451.  
  1452.     DoZoomL(zoom);
  1453.   }
  1454. }
  1455.  
  1456.  
  1457. void CPdfAppUi::CmdSetViewL()
  1458. {
  1459.   TPdfUiState newState = iUiState;
  1460.   CSetViewDialog* dialog=new(ELeave) CSetViewDialog(newState);
  1461.   if (dialog->ExecuteLD(R_SET_VIEW_DIALOG)) {
  1462.     if (newState != iUiState)
  1463.       UpdateUiStateL(newState);
  1464.   }
  1465. }
  1466.  
  1467.  
  1468. void CPdfAppUi::CmdPreferencesL()
  1469. {
  1470.   TPdfUiState oldPrefs;
  1471.   LoadUiStateL(KPreferencesStreamUid, oldPrefs);
  1472.  
  1473.   TPdfUiState newPrefs = oldPrefs;
  1474.   CPreferencesDialog* dialog=new(ELeave) CPreferencesDialog(newPrefs);
  1475.   if (dialog->ExecuteLD(R_PREFERENCES_DIALOG)) {
  1476.    
  1477.     if (oldPrefs != newPrefs) {
  1478.       
  1479.       // store all new preferences
  1480.       StoreUiStateL(KPreferencesStreamUid, newPrefs);
  1481.    
  1482.       // only set new preferences that differs from old preferences
  1483.       // do this by setting the current state in preferences if not changed
  1484.       if (oldPrefs.iZoom == newPrefs.iZoom)
  1485.         newPrefs.iZoom = iUiState.iZoom;
  1486.       
  1487.       if (oldPrefs.iDrawAccurate == newPrefs.iDrawAccurate)
  1488.         newPrefs.iDrawAccurate = iUiState.iDrawAccurate;
  1489.       
  1490.       if (oldPrefs.iRenderText == newPrefs.iRenderText)
  1491.         newPrefs.iRenderText = iUiState.iRenderText;
  1492.       
  1493.       if (oldPrefs.iRenderLineDrawings == newPrefs.iRenderLineDrawings)
  1494.         newPrefs.iRenderLineDrawings = iUiState.iRenderLineDrawings;
  1495.       
  1496.       if (oldPrefs.iRenderImages == newPrefs.iRenderImages)
  1497.         newPrefs.iRenderImages = iUiState.iRenderImages;
  1498.       
  1499.       if (oldPrefs.iDitherImages == newPrefs.iDitherImages)
  1500.         newPrefs.iDitherImages = iUiState.iDitherImages;
  1501.       
  1502.       if (oldPrefs.iClip == newPrefs.iClip)
  1503.         newPrefs.iClip = iUiState.iClip;
  1504.       
  1505.       if (oldPrefs.iRotate == newPrefs.iRotate)
  1506.         newPrefs.iRotate = iUiState.iRotate;
  1507.       
  1508.       if (oldPrefs.iDoLinks == newPrefs.iDoLinks)
  1509.         newPrefs.iDoLinks = iUiState.iDoLinks;
  1510.       
  1511.       if (oldPrefs.iHBarVisibility == newPrefs.iHBarVisibility)
  1512.         newPrefs.iHBarVisibility = iUiState.iHBarVisibility;
  1513.       
  1514.       if (oldPrefs.iVBarVisibility == newPrefs.iVBarVisibility)
  1515.         newPrefs.iVBarVisibility = iUiState.iVBarVisibility;
  1516.       
  1517.       if (oldPrefs.iShowToolBar == newPrefs.iShowToolBar)
  1518.         newPrefs.iShowToolBar = iUiState.iShowToolBar;
  1519.       
  1520.       if (oldPrefs.iShowToolBand == newPrefs.iShowToolBand)
  1521.         newPrefs.iShowToolBand = iUiState.iShowToolBand;
  1522.  
  1523.       if (oldPrefs.iMaximizeView == newPrefs.iMaximizeView)
  1524.         newPrefs.iMaximizeView = iUiState.iMaximizeView;
  1525.       
  1526.       // set new ui state
  1527.       if (newPrefs != iUiState)
  1528.         UpdateUiStateL(newPrefs);
  1529.     }
  1530.   }
  1531. }
  1532.  
  1533.  
  1534. void CPdfAppUi::CmdDrawAccurateL()
  1535. {
  1536.   TPdfPageViewMemento drawAccuratePars = iUiState;
  1537.   drawAccuratePars.iDrawAccurate = !iUiState.iDrawAccurate;
  1538.   if (iDoc)
  1539.     iPageView->StartRenderingL(iPage, drawAccuratePars);
  1540.   SetDrawAccurate(drawAccuratePars.iDrawAccurate);
  1541. }
  1542.  
  1543.  
  1544. //
  1545. // see 'Professional Symbian Programming', page 424
  1546. //
  1547. void CPdfAppUi::DoHelpContentsL(TBool aStartup)
  1548. {
  1549.   // get the help filename
  1550.   TFileName appName(Application()->AppFullName());
  1551.   TParse parser;
  1552.   parser.SetNoWild(_L(".hlp"),&appName, 0);
  1553.   TPtrC helpFile(parser.FullName());
  1554.   if (!ConeUtils::FileExists(helpFile)) 
  1555.     User::Leave(KErrNotFound);
  1556.  
  1557.   // schan through open programs
  1558.   TPtrC helpFileName(parser.Name());
  1559.   HBufC *caption = HBufC::NewLC(helpFileName.Length()+2);
  1560.   caption->Des().Format(_L("- %S"), &helpFileName);
  1561.   TApaTaskList taskList(iEikonEnv->WsSession());
  1562.   TInt numGroups = iEikonEnv->WsSession().NumWindowGroups(0);
  1563.  
  1564.   // look through applist to see what we can find
  1565.   for (TInt index=0; index < numGroups; index++) {
  1566.     // Get task information
  1567.     TApaTask *task= new(ELeave) TApaTask(taskList.FindByPos(index));
  1568.     CleanupStack::PushL(task);
  1569.     CApaWindowGroupName* wgName=CApaWindowGroupName::NewLC(iEikonEnv->WsSession(), task->WgId());
  1570.     TUid uid = wgName->AppUid();
  1571.     TPtrC taskCaption = wgName->Caption();
  1572.     
  1573.     // check it's a 'Help' app with the desired caption
  1574.     TBool found = (uid.iUid == 0x10000171 && taskCaption.FindF(*caption)>=0); 
  1575.     if (found) {
  1576.       // either start or stop our 'Help' app
  1577.       if (aStartup)
  1578.         task->BringToForeground();
  1579.       else
  1580.         task->EndTask();
  1581.       CleanupStack::PopAndDestroy(3); // wgName, task and caption
  1582.       return; // exit procedure
  1583.     }
  1584.     CleanupStack::PopAndDestroy(2); // wgName and task
  1585.   }
  1586.   CleanupStack::PopAndDestroy(); // caption
  1587.  
  1588.   if (aStartup) {
  1589.     // if not found, then open using EikDll
  1590.     EikDll::StartDocL(helpFile);
  1591.     User::After(1000000); // pause 1s 
  1592.   }
  1593. }
  1594.  
  1595.  
  1596. void CPdfAppUi::CmdHelpAboutL()
  1597. {
  1598.   CEikDialog* dialog = new(ELeave) CEikDialog();
  1599.   dialog->ExecuteLD(R_HELP_ABOUT_DIALOG);
  1600. }
  1601.  
  1602.  
  1603. void CPdfAppUi::HandleControlEventL(CCoeControl* /*aControl*/, TCoeEvent aEventType)    
  1604. {
  1605.   if (iZoomBoxPopped && (aEventType==EEventRequestCancel || aEventType==EEventRequestExit)) {
  1606.     
  1607.     TInt zoom = iUiState.iZoom;
  1608.  
  1609.     if (aEventType==EEventRequestExit)  {  
  1610.       TInt index = iPopoutListBox->CurrentItemIndex();  
  1611.       switch (index) {
  1612.       case 11: zoom = CPdfPageView::KZoomPage;  break;
  1613.       case 12: zoom = CPdfPageView::KZoomWidth; break;
  1614.       default: zoom = KZoom[index]; break;
  1615.       }
  1616.     }
  1617.     
  1618.     iEikonEnv->RemoveFromStack(iPopoutListBox);
  1619.     delete iPopoutListBox;
  1620.     iPopoutListBox = 0;
  1621.     iZoomBoxPopped = EFalse;
  1622.   
  1623.     if (iUiState.iZoom != zoom)
  1624.       DoZoomL(zoom);
  1625.   }
  1626.   return;
  1627. }
  1628.  
  1629.  
  1630. void CPdfAppUi::CmdZoomPopupL()
  1631. {
  1632.   if (iZoomBoxPopped)
  1633.     return;
  1634.  
  1635.   CDesCArray* zoomList = iCoeEnv->ReadDesCArrayResourceL(R_ZOOM_LIST);
  1636.   CleanupStack::PushL(zoomList);
  1637.   
  1638.   TInt index = ZoomToIndex(iUiState.iZoom);
  1639.   
  1640.   CEikCommandButtonBase* zoomButton = STATIC_CAST(CEikCommandButtonBase*, iToolBand->ControlById(EPdfCmdZoomPopup));
  1641.   
  1642.   DoCreatePopoutL(zoomList, index, zoomButton);
  1643.   iZoomBoxPopped = ETrue;
  1644. }
  1645.  
  1646.  
  1647. void CPdfAppUi::DoCreatePopoutL(CDesCArray* aText, TInt aIndex, CEikCommandButtonBase* aLaunchingButton)
  1648. {
  1649.   CPdfTextListBox* popout=new(ELeave) CPdfTextListBox;
  1650.   CleanupStack::PushL(popout);
  1651.   popout->ConstructL(0, CEikListBox::EPopout|CEikListBox::ELeftDownInViewRect);
  1652.   popout->CreateScrollBarFrameL();
  1653.   popout->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
  1654.   popout->SetObserver(this);
  1655.   iEikonEnv->AddWindowShadow(popout);
  1656.   popout->Model()->SetItemTextArray(aText);
  1657.   CleanupStack::Pop(); // aText
  1658.   const TSize screenSize=iEikonEnv->ScreenDevice()->SizeInPixels();
  1659.   TSize butSize=aLaunchingButton->Size();
  1660.   TInt width=Max(butSize.iWidth,popout->MaxWidth())+CEikScrollBar::EScrollbarWidth;
  1661.   if (width>screenSize.iWidth)
  1662.     width = screenSize.iWidth;
  1663.   TPoint pos=aLaunchingButton->PositionRelativeToScreen();
  1664.   pos.iY+=butSize.iHeight;
  1665.   TRect listBoxRect(pos,TSize(width,0));
  1666.   popout->CalculatePopoutRect(0,pos.iY,listBoxRect);
  1667.   if (listBoxRect.iTl.iY<pos.iY) {
  1668.     const TInt itemHeight=popout->ItemHeight();
  1669.     while (listBoxRect.iTl.iY<pos.iY)
  1670.       listBoxRect.iTl.iY+=itemHeight;
  1671.     listBoxRect.iTl.iY=pos.iY;
  1672.   }
  1673.   if (listBoxRect.iBr.iX>screenSize.iWidth)
  1674.     listBoxRect.Move(listBoxRect.iBr.iX-screenSize.iWidth,0);
  1675.   popout->SetExtentL(TPoint(pos.iX,listBoxRect.iTl.iY),listBoxRect.Size());
  1676.   popout->SetCurrentItemIndex(aIndex);
  1677.   iEikonEnv->AddDialogLikeControlToStackL(popout);
  1678.   popout->SetLaunchingButton(aLaunchingButton);
  1679.   popout->ActivateL();
  1680.   CleanupStack::Pop(); // popout
  1681.   iPopoutListBox=popout;
  1682.   aLaunchingButton->SetIgnoreNextPointerUp();
  1683.   iPopoutListBox->ClaimPointerGrab();
  1684. }
  1685.  
  1686.  
  1687. void CPdfAppUi::CmdFitActualSizeL()
  1688. {
  1689.   if (iDoc && 100 != iUiState.iZoom)
  1690.     DoZoomL(100);
  1691. }
  1692.  
  1693.  
  1694. void CPdfAppUi::CmdFitPageL()
  1695. {
  1696.   if (iDoc && CPdfPageView::KZoomPage != iUiState.iZoom)
  1697.     DoZoomL(CPdfPageView::KZoomPage);
  1698. }
  1699.  
  1700.  
  1701. void CPdfAppUi::CmdFitWidthL()
  1702. {
  1703.   if (iDoc && CPdfPageView::KZoomWidth != iUiState.iZoom)     
  1704.     DoZoomL(CPdfPageView::KZoomWidth);
  1705. }
  1706.  
  1707.  
  1708. #ifdef TRACE
  1709. void CPdfAppUi::CmdShowProfile()
  1710. {
  1711.   PROFILE_DUMP();
  1712. }
  1713. #endif
  1714.  
  1715.  
  1716. #ifdef OOM
  1717.  
  1718. #include "FbsBitmapOutputDev.h"
  1719.  
  1720. void CPdfAppUi::CmdOOML()
  1721. {
  1722.   _LIT(KPdfExtension, "pdf");
  1723.  
  1724.   // As in CmdFileOpenL()
  1725.   TFileName filename(CurrentFilePath());
  1726.   SetInitialPathL(filename);
  1727.   CEikFileOpenDialog* dialog=new(ELeave) CEikFileOpenDialog(&filename);
  1728.   dialog->SetRequiredExtension(&KPdfExtension);
  1729.   if (!dialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN))
  1730.     return;
  1731.  
  1732.   iPageView->CancelRendering();
  1733.  
  1734.   // no displaying of links, reallocates memory
  1735.   TBool oldLinks = iUiState.iDoLinks;
  1736.   iUiState.iDoLinks = EFalse;
  1737.  
  1738.   // drawing of rotated pages use extra memory
  1739.   TInt oldRotate = iUiState.iRotate;
  1740.  
  1741.   // drawing of zoomed page use extra memory
  1742.   TInt oldZoom   = iUiState.iZoom;
  1743.  
  1744.   TInt err;
  1745.   TInt i;
  1746.   
  1747.   // set these vars to zero if you need to skip a test
  1748.   TInt run1 =1, run2=1, run3=1, run4=1;
  1749.  
  1750.   if (run1) {
  1751.     // check reading and destroying a doc
  1752.     __UHEAP_MARK;
  1753.     PDFDoc* newDoc = 0;
  1754.     for (i= 1; ; i++) {
  1755.       __UHEAP_FAILNEXT(i);
  1756.       __UHEAP_MARK;
  1757.       TRAP(err, newDoc = DoOpenFileOOML(filename));
  1758.       delete newDoc;
  1759.       __UHEAP_MARKEND;
  1760.       newDoc = 0;
  1761.       if (err!=KErrNoMemory)
  1762.         break;
  1763.     }
  1764.     __UHEAP_MARKEND;
  1765.     __UHEAP_RESET;
  1766.  
  1767.     // other errors are still bad
  1768.     User::LeaveIfError(err);
  1769.  
  1770.     User::Beep(440, 1000000);
  1771.   }
  1772.  
  1773.   
  1774.   // now it is safe to replace the old doc with the new doc
  1775.   STATIC_CAST(CPdfDocument*,iDocument)->DoOpenFileL(filename);
  1776.   if (!iDoc->isOk()) {
  1777.     iEikonEnv->InfoMsg(_L("Bad document"));
  1778.   }
  1779.   else {
  1780.     // cannot be OOM tested in a trap harness, because it deletes and creates 
  1781.     // views. however, errors here are trapped by the overall CONE OOM check
  1782.     HandleModelChangeL();
  1783.     iPageView->CancelRendering();
  1784.     
  1785.     // no displaying of links, reallocates memory
  1786.     iUiState.iDoLinks = EFalse;
  1787.  
  1788.     // display all the pages
  1789.     __UHEAP_MARK;
  1790.     for (TInt p=1; p<= iDoc->getNumPages(); p++) {
  1791.       // start rendering
  1792.       iSelection = TRect(0,0,0,0);
  1793.       iPageView->SetSelection(iSelection);
  1794.       iPageView->ScrollToTop();
  1795.  
  1796.       if (run2) {
  1797.         // start rendering, then zoom out
  1798.         iUiState.iRotate = 0;
  1799.         for (TInt zoom = 100; zoom <= 125; zoom += 25) {
  1800.           iUiState.iZoom = zoom;
  1801.           for (i = 0; ; i++) {
  1802.             __UHEAP_FAILNEXT(i);
  1803.             __UHEAP_MARK;
  1804.             TRAP(err, iPageView->StartRenderingL(p, iUiState));
  1805.             iPageView->CancelRendering();
  1806.             __UHEAP_MARKEND;
  1807.             if (err!=KErrNoMemory)
  1808.               break;
  1809.           }
  1810.         }
  1811.         __UHEAP_RESET;
  1812.         iUiState.iZoom = oldZoom;
  1813.       } // if (run2)
  1814.  
  1815.       if (run3) {
  1816.         // start rendering a rotated page
  1817.         __UHEAP_MARK;
  1818.         iUiState.iRotate = 90;
  1819.         for (i= 0; ; i++) {
  1820.           __UHEAP_FAILNEXT(i);
  1821.           __UHEAP_MARK;
  1822.           TRAP(err, iPageView->StartRenderingL(p, iUiState));
  1823.           iPageView->CancelRendering();
  1824.           __UHEAP_MARKEND;
  1825.           if (err!=KErrNoMemory)
  1826.            break;
  1827.         }
  1828.         iUiState.iRotate = oldRotate;
  1829.         __UHEAP_MARKEND;
  1830.         __UHEAP_RESET;
  1831.       
  1832.         User::Beep(660, 1000000);
  1833.       } // if (run3)
  1834.       
  1835.       // it is too hard to do OOM testing on a running active object
  1836.       // this is the next best thing
  1837.       // zooming is less important here, because that does not generate extra
  1838.       // allocations during rendering, only at the start and that is tested above
  1839.       if (run4) {
  1840.         __UHEAP_MARK;
  1841.         for (TInt rotate = 0; rotate <= 90; rotate +=90) {
  1842.           iUiState.iRotate = rotate;
  1843.           for (i= 0; ; i++) {
  1844.             __UHEAP_FAILNEXT(i);
  1845.             __UHEAP_MARK;
  1846.             TRAP(err, DisplayPageOOML(p));
  1847.             __UHEAP_MARKEND;
  1848.             if (err!=KErrNoMemory)
  1849.               break;
  1850.           }
  1851.         }
  1852.         __UHEAP_MARKEND;
  1853.         __UHEAP_RESET;
  1854.         User::Beep(660, 1000000);
  1855.       } // if (run4)
  1856.     
  1857.     } // for (p=1; ...
  1858.     
  1859.     __UHEAP_MARKEND;
  1860.     
  1861.     // done
  1862.     iEikonEnv->InfoMsg(_L("OOM Test passed"));
  1863.     User::Beep(880, 1000000);
  1864.   }
  1865.  
  1866.   // restore setting 
  1867.   iUiState.iDoLinks = oldLinks;
  1868.   iUiState.iRotate  = oldRotate;
  1869.   iUiState.iZoom    = oldZoom;
  1870.  
  1871.   iPageView->SetObserver(0);
  1872.   OpenFileL(filename);
  1873. }
  1874.  
  1875.  
  1876. PDFDoc* CPdfAppUi::DoOpenFileOOML(const TFileName& aFileName)
  1877. {
  1878.   PDFDoc* newDoc = new(ELeave) PDFDoc();
  1879.   CleanupStack::PushL(newDoc);
  1880.   newDoc->ConstructL(Document()->Process()->FsSession(), aFileName);
  1881.   CleanupStack::Pop(); // newDoc
  1882.   return newDoc;
  1883. }
  1884.  
  1885.  
  1886. void CPdfAppUi::DisplayPageOOML(TInt aPage)
  1887. {
  1888.   // set bitmapsize in twips
  1889.   TSize sizeInTwips(round(iDoc->getPageWidth(aPage) *KTwipsPerPoint),
  1890.                     round(iDoc->getPageHeight(aPage)*KTwipsPerPoint));
  1891.   
  1892.   // create the bitmap
  1893.   CWsBitmap* bitmap = new(ELeave) CWsBitmap(iCoeEnv->WsSession());
  1894.   CleanupStack::PushL(bitmap);
  1895.   User::LeaveIfError(bitmap->Create(TSize(0,0), iCoeEnv->ScreenDevice()->DisplayMode()));
  1896.   
  1897.   // create the output device
  1898.   CFbsBitmapOutputDev* out = new(ELeave) CFbsBitmapOutputDev(0);
  1899.   CleanupStack::PushL(out);
  1900.   out->ConstructL(bitmap, iCoeEnv->ScreenDevice()->DisplayMode());
  1901.   out->InitPageRendering(sizeInTwips, iUiState);
  1902.  
  1903.   // draw the page
  1904.   iDoc->displayPageL(out, aPage, iUiState.iZoom, iUiState.iRotate, iUiState.iDoLinks);
  1905.  
  1906.   CleanupStack::PopAndDestroy(2); // out, bitmap
  1907. }
  1908.  
  1909. #endif
  1910.