home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWPart / FWPrHdlr.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  27.9 KB  |  1,092 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPrHdlr.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWFrameW.hpp"
  11.  
  12. #ifndef FWPRHDLR_H
  13. #include "FWPrHdlr.h"
  14. #endif
  15.  
  16. #ifndef FWPART_H
  17. #include "FWPart.h"
  18. #endif
  19.  
  20. #ifndef FWFRAME_H
  21. #include "FWFrame.h"
  22. #endif
  23.  
  24. #ifndef FWPRINFO_H
  25. #include "FWPrInfo.h"
  26. #endif
  27.  
  28. #ifndef FWCFMRES_H
  29. #include "FWCFMRes.h"
  30. #endif
  31.  
  32. #ifndef FWPRINT_K
  33. #include "FWPrint.k"
  34. #endif
  35.  
  36. #ifndef FWALERT_H
  37. #include "FWAlert.h"
  38. #endif
  39.  
  40. #ifndef FWGRUTIL_H
  41. #include "FWGrUtil.h"
  42. #endif
  43.  
  44. #ifndef FWMNUBAR_H
  45. #include "FWMnuBar.h"
  46. #endif
  47.  
  48. #ifndef FWEVENT_H
  49. #include "FWEvent.h"
  50. #endif
  51.  
  52. #ifndef FWRESACC_H
  53. #include "FWResAcc.h"
  54. #endif
  55.  
  56. #ifndef FWRESTYP_H
  57. #include "FWResTyp.h"
  58. #endif
  59.  
  60. #ifndef FWODGEOM_H
  61. #include "FWODGeom.h"
  62. #endif
  63.  
  64. #ifndef FWITERS_H
  65. #include "FWIters.h"
  66. #endif
  67.  
  68. #ifndef SLODFSTR_H
  69. #include "SLODFStr.h"
  70. #endif
  71.  
  72. #ifndef SLODFSTR_K
  73. #include "SLODFStr.k"
  74. #endif
  75.  
  76. #ifndef FWSESION_H
  77. #include "FWSesion.h"
  78. #endif
  79.  
  80. // ----- OpenDoc includes
  81.  
  82. #ifndef SOM_ODFacet_xh
  83. #include <Facet.xh>
  84. #endif
  85.  
  86. #ifndef SOM_ODArbitrator_xh
  87. #include <Arbitrat.xh>
  88. #endif
  89.  
  90. #ifndef SOM_ODFoci_xh
  91. #include <Foci.xh>
  92. #endif
  93.  
  94. #ifndef SOM_ODFrame_xh
  95. #include <Frame.xh>
  96. #endif
  97.  
  98. #ifndef SOM_ODWindowState_xh
  99. #include <WinStat.xh>
  100. #endif
  101.  
  102. #ifndef SOM_Module_OpenDoc_Commands_defined
  103. #include <CmdDefs.xh>
  104. #endif
  105.  
  106. //========================================================================================
  107. // RunTime Info
  108. //========================================================================================
  109.  
  110. #ifdef FW_BUILD_MAC
  111. #pragma segment odfprinting
  112. #endif
  113.  
  114. //========================================================================================
  115. // Local helpers
  116. //========================================================================================
  117.  
  118. #ifdef FW_BUILD_WIN
  119.  
  120. static FW_Boolean    gWinAbortPrinting = FALSE;
  121. static HWND         gWinProgressDlg = NULL;
  122.  
  123. static HWND    WinGetFrameWindow(
  124.             Environment*    ev,
  125.             ODFrame*        frame);
  126.         
  127.  
  128. static void    WinShowControl(
  129.             HWND        hWnd,
  130.             int            nID,
  131.             BOOL        bShow);
  132.  
  133. static int CALLBACK WinPrintProgessDlgProc(
  134.             HWND        hWnd,
  135.             UINT        message,
  136.             WPARAM        wParam,
  137.             LPARAM        lParam);
  138.  
  139. static BOOL CALLBACK WinAbortProc(
  140.             HDC        hDC,
  141.             int        nCode);
  142.  
  143. #endif
  144.  
  145. #ifdef FW_BUILD_MAC
  146.  
  147. static DialogPtr    gMacProgressDlg = NULL;
  148. static PrIdleUPP    gMacIdleProcUPP = NULL;
  149. static FW_Boolean    gMacPrintAborted = FALSE;
  150.  
  151. static pascal void MacIdleProc();
  152.  
  153. #endif
  154.  
  155. //----------------------------------------------------------------------------------------
  156. //    FW_CPrintHandler::FW_CPrintHandler
  157. //----------------------------------------------------------------------------------------
  158.  
  159. FW_CPrintHandler::FW_CPrintHandler(FW_CPart* part, FW_CFrame* frame, FW_Boolean updateEmbeddedFacets) :
  160.     fPart(part),
  161.     fFrame(frame),
  162.     fPrintView(NULL),
  163.     fUpdateEmbeddedFacets(updateEmbeddedFacets)
  164. {
  165. }
  166.  
  167. //----------------------------------------------------------------------------------------
  168. //    FW_CPrintHandler::~FW_CPrintHandler
  169. //----------------------------------------------------------------------------------------
  170.  
  171. FW_CPrintHandler::~FW_CPrintHandler()
  172. {
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    FW_CPrintHandler::GetPart
  177. //----------------------------------------------------------------------------------------
  178.  
  179. FW_CPart* FW_CPrintHandler::GetPart() const
  180. {
  181.     return fPart;
  182. }
  183.  
  184. //----------------------------------------------------------------------------------------
  185. //    FW_CPrintHandler::GetFrame
  186. //----------------------------------------------------------------------------------------
  187.  
  188. FW_CFrame* FW_CPrintHandler::GetFrame() const
  189. {
  190.     return fFrame;
  191. }
  192.  
  193. //----------------------------------------------------------------------------------------
  194. //    FW_CPrintHandler::GetPrintView
  195. //----------------------------------------------------------------------------------------
  196.  
  197. FW_CView* FW_CPrintHandler::GetPrintView() const
  198. {
  199.     return fPrintView;
  200. }
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    FW_CPrintHandler::IsCurrentlyPrintable
  204. //----------------------------------------------------------------------------------------
  205.  
  206. FW_Boolean FW_CPrintHandler::IsCurrentlyPrintable(Environment* /* ev */) const
  207. {
  208.     return TRUE;
  209. }
  210.  
  211. //----------------------------------------------------------------------------------------
  212. //    FW_CPrintHandler::DoAdjustMenus
  213. //----------------------------------------------------------------------------------------
  214.  
  215. FW_Handled FW_CPrintHandler::DoAdjustMenus(Environment *ev, FW_CMenuBar* menuBar, FW_Boolean hasMenuFocus, FW_Boolean isRoot)
  216. {
  217. FW_UNUSED(hasMenuFocus);
  218.     if (isRoot)
  219.     {
  220.         menuBar->EnableCommand(ev, kODCommandPageSetup, TRUE);
  221.         menuBar->EnableCommand(ev, kODCommandPrint, IsCurrentlyPrintable(ev));
  222.     }
  223.  
  224.     return FW_kNotHandled;
  225. }
  226.  
  227. //----------------------------------------------------------------------------------------
  228. //    FW_CPrintHandler::GetPrintContentExtent
  229. //----------------------------------------------------------------------------------------
  230.  
  231. void FW_CPrintHandler::GetPrintContentExtent(Environment* ev, FW_CPoint& extent) const
  232. {
  233.     fFrame->GetContentExtent(ev, extent);
  234.     FW_ASSERT(extent.x != FW_kFixed0);
  235.     FW_ASSERT(extent.y != FW_kFixed0);
  236. }
  237.  
  238. //----------------------------------------------------------------------------------------
  239. //    FW_CPrintHandler::GetPrintJobTitle
  240. //----------------------------------------------------------------------------------------
  241.  
  242. void FW_CPrintHandler::GetPrintJobTitle(Environment* ev, FW_CString& jobTitle) const
  243. {
  244.     fPart->GetPartName(ev, jobTitle);
  245. }
  246.  
  247. //----------------------------------------------------------------------------------------
  248. //    FW_CPrintHandler::DoMenu
  249. //----------------------------------------------------------------------------------------
  250.  
  251. FW_Handled FW_CPrintHandler::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent)
  252. {
  253.     FW_Handled result = FW_kNotHandled;
  254.  
  255.     if (fFrame->IsRoot(ev))
  256.     {
  257.         switch (theMenuEvent.GetCommandID(ev))
  258.         {
  259.             case kODCommandPageSetup:
  260.                 HandlePageSetup(ev);
  261.                 result = FW_kHandled;
  262.                 break;
  263.  
  264.             case kODCommandPrint:
  265.                 HandlePrint(ev);
  266.                 result = FW_kHandled;
  267.                 break;
  268.         }
  269.     }
  270.     
  271.     return result;
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. //    FW_CPrintHandler::DoPageSetupDialog
  276. //----------------------------------------------------------------------------------------
  277.  
  278. FW_Boolean FW_CPrintHandler::DoPageSetupDialog(Environment* ev)
  279. {
  280.     FW_Boolean result = FALSE;
  281.  
  282.     if (PreModalDialog(ev))
  283.     {
  284.         FW_CPrintInfo* printInfo = GetPrintInfo();
  285.  
  286. #ifdef FW_SUPPORT_GX
  287.         gxJob job = printInfo->GetGXJob();
  288.         if (job != NULL)
  289.         {
  290.             result = ::GXJobDefaultFormatDialog(job, NULL);
  291.         }
  292.         else
  293.         {
  294. #endif
  295. #ifdef FW_BUILD_WIN
  296.             LPPRINTDLG lppd = printInfo->WinGetPrintDlg()
  297.             lppd->Flags    = PD_PRINTSETUP;
  298.             lppd->hwndOwner = WinGetFrameWindow(ev, fFrame->GetODFrame(ev));
  299.             result = ::PrintDlg(lppd);
  300. #endif
  301. #ifdef FW_BUILD_MAC
  302.             THPrint thPrint = printInfo->MacGetTHPrint();
  303.             result = ::PrStlDialog(thPrint);
  304. #endif
  305. #ifdef FW_SUPPORT_GX
  306.         }
  307. #endif
  308.         PostModalDialog(ev);
  309.     }
  310.     
  311.     return result;
  312. }
  313.  
  314. //----------------------------------------------------------------------------------------
  315. //    FW_CPrintHandler::DoJobDialog
  316. //----------------------------------------------------------------------------------------
  317.  
  318. FW_Boolean FW_CPrintHandler::DoJobDialog(Environment* ev)
  319. {
  320.     FW_Boolean result = FALSE;
  321.  
  322.     if (PreModalDialog(ev))
  323.     {
  324.         FW_CPrintInfo* printInfo = GetPrintInfo();
  325.  
  326. #ifdef FW_SUPPORT_GX
  327.         gxJob job = printInfo->GetGXJob();
  328.         if (job != NULL)
  329.         {
  330.             result = ::GXJobPrintDialog(job, NULL);
  331.         }
  332.         else
  333.         {
  334. #endif
  335. #ifdef FW_BUILD_WIN
  336.             LPPRINTDLG lppd = printInfo->WinGetPrintDlg()
  337.             lppd->Flags    = PD_RETURNDC;
  338.             lppd->hwndOwner = WinGetFrameWindow(ev, fFrame->GetODFrame(ev));
  339.             if (::PrintDlg(lppd))
  340.             {
  341.                 printInfo->WinUpdatePageRect();
  342.                 result = TRUE;
  343.             }
  344. #endif
  345. #ifdef FW_BUILD_MAC
  346.             THPrint thPrint = printInfo->MacGetTHPrint();
  347.             (*thPrint)->prJob.iFstPage = 1;
  348.             (*thPrint)->prJob.iLstPage = 9999;
  349.     
  350.             result = ::PrJobDialog(thPrint);
  351. #endif
  352. #ifdef FW_SUPPORT_GX
  353.         }
  354. #endif
  355.         PostModalDialog(ev);
  356.     }
  357.  
  358.     return result;
  359. }
  360.  
  361. //----------------------------------------------------------------------------------------
  362. //    FW_CPrintHandler::NotifyNoDefaultPrinter
  363. //----------------------------------------------------------------------------------------
  364.  
  365. void FW_CPrintHandler::NotifyNoDefaultPrinter(Environment* ev)
  366. {
  367.     // fix #1345077  we don't want the OpenDoc error dialog after ODF alert
  368.     FW_SetEvError(ev, FW_xNoError);
  369.  
  370.     FW_CString partName;
  371.     fPart->GetPartName(ev, partName);
  372.     
  373.     FW_CString message;
  374.     ::FW_PrivLoadODFString(ev, FW_kNoDefaultPrinterMsg, message);
  375.         
  376.     FW_NoteAlert(partName, message);
  377. }
  378.  
  379. //----------------------------------------------------------------------------------------
  380. //    FW_CPrintHandler::NotifyPrintingCanceled
  381. //----------------------------------------------------------------------------------------
  382.  
  383. void FW_CPrintHandler::NotifyPrintingCanceled(Environment* ev)
  384. {
  385.     // fix #1345077  we don't want the OpenDoc error dialog after ODF alert
  386.     FW_SetEvError(ev, FW_xNoError);
  387.  
  388.     FW_CString partName;
  389.     fPart->GetPartName(ev, partName);
  390.     
  391.     FW_CString message;
  392.     ::FW_PrivLoadODFString(ev, FW_kPrintingCanceledMsg, message);
  393.  
  394.     FW_NoteAlert(partName, message);
  395. }
  396.  
  397. //----------------------------------------------------------------------------------------
  398. //    FW_CPrintHandler::PreModalDialog
  399. //----------------------------------------------------------------------------------------
  400.  
  401. FW_Boolean FW_CPrintHandler::PreModalDialog(Environment* ev)
  402. {
  403.     ODFrame* odFrame = fFrame->GetODFrame(ev);
  404.     ODToken modalFocus = FW_CSession::Tokenize(ev, kODModalFocus);
  405.  
  406.     return FW_CSession::GetArbitrator(ev)->RequestFocus(ev, modalFocus, odFrame);
  407. }
  408.  
  409. //----------------------------------------------------------------------------------------
  410. //    FW_CPrintHandler::PostModalDialog
  411. //----------------------------------------------------------------------------------------
  412.  
  413. void FW_CPrintHandler::PostModalDialog(Environment* ev)
  414. {
  415.     ODFrame* odFrame = fFrame->GetODFrame(ev);
  416.     ODToken modalFocus = FW_CSession::Tokenize(ev, kODModalFocus);
  417.  
  418.     FW_CSession::GetArbitrator(ev)->RelinquishFocus(ev, modalFocus, odFrame);
  419. }
  420.  
  421. //----------------------------------------------------------------------------------------
  422. // FW_CPrintHandler::GetPrintInfo
  423. //----------------------------------------------------------------------------------------
  424.  
  425. FW_CPrintInfo* FW_CPrintHandler::GetPrintInfo() const
  426. {
  427.     FW_CPrintInfo* printInfo = fPart->GetPrintInfo();
  428.  
  429. #if 0
  430.     if (printInfo == NULL)
  431.     {
  432.         printInfo = fPart->NewPrintInfo();
  433.         fPart->SetPrintInfo(printInfo);
  434.     }
  435. #endif
  436.     
  437.     return printInfo;
  438. }
  439.  
  440. //----------------------------------------------------------------------------------------
  441. // FW_CPrintHandler::CreateProgressDialog
  442. //----------------------------------------------------------------------------------------
  443.  
  444. void FW_CPrintHandler::CreateProgressDialog(
  445.         Environment*        ev,
  446.         ODFrame*            frame,
  447.         long                numPages,
  448.         const FW_CString&    title)
  449. {
  450. FW_UNUSED(frame);
  451.     FW_CPrintInfo* printInfo = GetPrintInfo();
  452.  
  453. #ifdef FW_SUPPORT_GX
  454.     gxJob job = printInfo->GetGXJob();
  455.     if (job != NULL)
  456.         return;
  457. #endif
  458. #ifdef FW_BUILD_WIN
  459.     FW_ASSERT(gWinProgressDlg == NULL);
  460.  
  461.     HWND hWndParent = WinGetFrameWindow(ev, frame);
  462.     gWinProgressDlg = ::CreateDialog(FW_gInstance,
  463.         FW_kWinPrintDialog, hWndParent, WinPrintProgessDlgProc);
  464.     FW_ASSERT(gWinProgressDlg);
  465.  
  466.     ::EnableWindow(hWndParent, FALSE);
  467.  
  468.     // Initialize some fields
  469.     ::SetDlgItemText(gWinProgressDlg, FW_kFileNameID, title);
  470.     ::SetDlgItemInt(gWinProgressDlg, FW_kTotalPagesNumID, numPages, TRUE);
  471.     ::ShowWindow(gWinProgressDlg, SW_NORMAL);
  472.     ::UpdateWindow(gWinProgressDlg);
  473.  
  474.     // Set up the abort proc
  475.     gWinAbortPrinting = FALSE;
  476.     ::SetAbortProc(lppd->hDC, WinAbortProc);
  477. #endif
  478. #ifdef FW_BUILD_MAC
  479.     FW_ASSERT(gMacProgressDlg == NULL);
  480.     
  481.     gMacPrintAborted = FALSE;
  482.  
  483.     FW_PSharedLibraryResourceFile resourceFile(ev);
  484.     gMacProgressDlg = ::GetNewDialog(FW_kMacPrintDialog, NULL, (WindowPtr) -1L);
  485.     
  486.     // Return if no Progress dialog resource available
  487.     if (!gMacProgressDlg) return;
  488.     
  489.     THPrint thPrint = printInfo->MacGetTHPrint();
  490.     gMacIdleProcUPP = NewPrIdleProc(MacIdleProc);
  491.     (*thPrint)->prJob.pIdleProc = gMacIdleProcUPP;
  492.     
  493.     ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageStrID);
  494.     ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageNumID);
  495.  
  496.     ::HideDialogItem(gMacProgressDlg, FW_kCancelingJobID);
  497.  
  498.     short iType;
  499.     Handle iHndl;
  500.     Rect iRect;
  501.     Str255 pasString;
  502.     
  503.     ::GetDialogItem(gMacProgressDlg, FW_kFileNameID, &iType, &iHndl, &iRect);
  504.     title.ExportPascal(pasString);
  505.     ::SetDialogItemText(iHndl, pasString);
  506.     ::SetWTitle(gMacProgressDlg, pasString);
  507.  
  508.     ::GetDialogItem(gMacProgressDlg, FW_kTotalPagesNumID, &iType, &iHndl, &iRect);
  509.     ::NumToString(numPages, pasString);
  510.     ::SetDialogItemText(iHndl, pasString);
  511.  
  512.     ::ShowWindow(gMacProgressDlg);
  513.     ::SelectWindow(gMacProgressDlg);
  514.     ::UpdateControls(gMacProgressDlg, gMacProgressDlg->visRgn);
  515.     ::UpdateDialog(gMacProgressDlg, gMacProgressDlg->visRgn);
  516. #endif
  517. }
  518.  
  519. //----------------------------------------------------------------------------------------
  520. // FW_CPrintHandler::DestroyProgressDialog
  521. //----------------------------------------------------------------------------------------
  522.  
  523. void FW_CPrintHandler::DestroyProgressDialog(
  524.         Environment*        ev,
  525.         ODFrame*            frame)
  526. {
  527. FW_UNUSED(ev);
  528. FW_UNUSED(frame);
  529.     FW_CPrintInfo* printInfo = GetPrintInfo();
  530.  
  531. #ifdef FW_SUPPORT_GX
  532.     gxJob job = printInfo->GetGXJob();
  533.     if (job != NULL)
  534.         return;
  535. #endif
  536. #ifdef FW_BUILD_WIN
  537.     FW_ASSERT(gWinProgressDlg != NULL);
  538.  
  539.     ::SetAbortProc(lppd->hDC, NULL);
  540.  
  541.     HWND hWndParent = WinGetFrameWindow(ev, frame);
  542.     ::EnableWindow(hWndParent, TRUE);
  543.     ::EndDialog(gWinProgressDlg, 0);
  544.     gWinProgressDlg = NULL;
  545. #endif
  546. #ifdef FW_BUILD_MAC
  547.     // Return if Progress dialog was not created
  548.     if (gMacProgressDlg == NULL) return;
  549.     
  550.     ::DisposeDialog(gMacProgressDlg);
  551.     gMacProgressDlg = NULL;
  552.     
  553.     DisposeRoutineDescriptor(gMacIdleProcUPP);
  554.     gMacIdleProcUPP = NULL;
  555. #endif
  556. }
  557.  
  558. //----------------------------------------------------------------------------------------
  559. // FW_CPrintHandler::UpdateProgressDialog
  560. //----------------------------------------------------------------------------------------
  561.  
  562. void FW_CPrintHandler::UpdateProgressDialog(
  563.         long                pageNumber)
  564. {
  565.     FW_CPrintInfo* printInfo = GetPrintInfo();
  566.  
  567. #ifdef FW_SUPPORT_GX
  568.     gxJob job = printInfo->GetGXJob();
  569.     if (job != NULL)
  570.         return;
  571. #endif
  572. #ifdef FW_BUILD_WIN
  573.     ::WinShowControl(gWinProgressDlg, FW_kStartingJobID,    FALSE);
  574.     
  575.     ::WinShowControl(gWinProgressDlg, FW_kTotalPagesStrID,    TRUE);
  576.     ::WinShowControl(gWinProgressDlg, FW_kTotalPagesNumID,    TRUE);
  577.     
  578.     ::WinShowControl(gWinProgressDlg, FW_kCurrentPageStrID,    TRUE);
  579.     ::WinShowControl(gWinProgressDlg, FW_kCurrentPageNumID,    TRUE);
  580.     
  581.     ::SetDlgItemInt(gWinProgressDlg, FW_kCurrentPageNumID, pageNumber, FALSE);
  582.     
  583.     ::UpdateWindow(gWinProgressDlg);
  584. #endif
  585. #ifdef FW_BUILD_MAC
  586.     // Return if Progress dialog was not created
  587.     if (!gMacProgressDlg) return;
  588.     
  589.     ::HideDialogItem(gMacProgressDlg, FW_kStartingJobID);
  590.     
  591.     ::ShowDialogItem(gMacProgressDlg, FW_kTotalPagesStrID);
  592.     ::ShowDialogItem(gMacProgressDlg, FW_kTotalPagesNumID);
  593.     
  594.     ::ShowDialogItem(gMacProgressDlg, FW_kCurrentPageStrID);
  595.     ::ShowDialogItem(gMacProgressDlg, FW_kCurrentPageNumID);
  596.  
  597.     short iType;
  598.     Handle iHndl;
  599.     Rect iRect;
  600.     Str255 pasString;
  601.     
  602.     ::GetDialogItem(gMacProgressDlg, FW_kCurrentPageNumID, &iType, &iHndl, &iRect);
  603.     ::NumToString(pageNumber, pasString);
  604.     ::SetDialogItemText(iHndl, pasString);
  605.  
  606.     ::UpdateControls(gMacProgressDlg, gMacProgressDlg->visRgn);
  607.     ::UpdateDialog(gMacProgressDlg, gMacProgressDlg->visRgn);
  608. #endif
  609. }
  610.  
  611. //----------------------------------------------------------------------------------------
  612. //    FW_CPrintHandler::HandlePageSetup
  613. //----------------------------------------------------------------------------------------
  614.  
  615. void FW_CPrintHandler::HandlePageSetup(Environment* ev)
  616. {
  617.     FW_TRY
  618.     {
  619.         FW_CPrintEnvironment printEnv;
  620.     
  621.         FW_CPrintInfo* printInfo = GetPrintInfo();
  622.         if (printInfo == NULL)
  623.         {
  624.             ODStorageUnit*    storageUnit = fPart->GetStorageUnit(ev);
  625.             printInfo = FW_NEW(FW_CPrintInfo, (ev, storageUnit, &printEnv));
  626.             fPart->SetPrintInfo(printInfo);
  627.             fPart->PrintInfoChanged(ev, printInfo);
  628.         }
  629.     
  630.         if(DoPageSetupDialog(ev))
  631.         {
  632.             fPart->PrintInfoChanged(ev, printInfo);
  633.             ODStorageUnit* storageUnit = fPart->GetStorageUnit(ev);
  634.             printInfo->SaveToStorageUnit(ev, storageUnit);
  635.         }
  636.     }
  637.     FW_CATCH_BEGIN
  638.     FW_CATCH_REFERENCE(FW_XException, pr)
  639.     {
  640.         if (pr.GetPlatformError() == FW_xNoDefaultPrinter)
  641.             NotifyNoDefaultPrinter(ev);
  642.         else
  643.             FW_THROW_SAME();
  644.     }
  645.     FW_CATCH_EVERYTHING()
  646.     {
  647.         FW_THROW_SAME();
  648.     }
  649.     FW_CATCH_END
  650. }
  651.  
  652. //----------------------------------------------------------------------------------------
  653. //    FW_CPrintHandler::HandlePrint
  654. //----------------------------------------------------------------------------------------
  655.  
  656. void FW_CPrintHandler::HandlePrint(Environment* ev)
  657. {
  658.     FW_TRY
  659.     {
  660.         FW_CPrintEnvironment printEnv;
  661.  
  662.         FW_CPrintInfo* printInfo = GetPrintInfo();
  663.         if (printInfo == NULL)
  664.         {
  665.             ODStorageUnit*    storageUnit = fPart->GetStorageUnit(ev);
  666.             printInfo = FW_NEW(FW_CPrintInfo, (ev, storageUnit, &printEnv));
  667.             fPart->SetPrintInfo(printInfo);
  668.             fPart->PrintInfoChanged(ev, printInfo);
  669.         }
  670.  
  671.         if (DoJobDialog(ev))
  672.         {
  673.             fPart->PrintInfoChanged(ev, printInfo);
  674.             fPrintView = CreatePrintView(ev);
  675.             PrintDocument(ev, &printEnv);
  676.         }
  677.     }
  678.     FW_CATCH_BEGIN
  679.     FW_CATCH_REFERENCE(FW_XException, pr)
  680.     {
  681.         switch(pr.GetPlatformError())
  682.         {
  683.             case FW_xNoDefaultPrinter:
  684.             {
  685.                 NotifyNoDefaultPrinter(ev);
  686.                 break;
  687.             }
  688.             case FW_xPrintingCanceled:
  689.             {
  690.                 NotifyPrintingCanceled(ev);
  691.                 break;
  692.             }
  693.             default:
  694.             {
  695.                 if (fPrintView != NULL)
  696.                 {
  697.                     ReleasePrintView(ev);
  698.                     fPrintView = NULL;
  699.                 }
  700.                 FW_THROW_SAME();
  701.             }
  702.         }
  703.     }
  704.     FW_CATCH_END
  705.     
  706.     if (fPrintView != NULL)
  707.     {
  708.         ReleasePrintView(ev);
  709.         fPrintView = NULL;
  710.     }
  711. }
  712.  
  713. //----------------------------------------------------------------------------------------
  714. //    FW_CPrintHandler::CreatePrintView
  715. //----------------------------------------------------------------------------------------
  716. //    return by default the content view
  717.  
  718. FW_CView* FW_CPrintHandler::CreatePrintView(Environment* ev)
  719. {
  720.     return fFrame->GetContentView(ev);
  721. }
  722.  
  723. //----------------------------------------------------------------------------------------
  724. //    FW_CPrintHandler::ReleasePrintView
  725. //----------------------------------------------------------------------------------------
  726.  
  727. void FW_CPrintHandler::ReleasePrintView(Environment* ev)
  728. {
  729. FW_UNUSED(ev);
  730.     // Does nothing
  731. }
  732.  
  733. //----------------------------------------------------------------------------------------
  734. //    FW_CPrintHandler::PrintDocument
  735. //----------------------------------------------------------------------------------------
  736.  
  737. void FW_CPrintHandler::PrintDocument(Environment* ev, FW_CPrintEnvironment* printEnv)
  738. {
  739.     long xPageCount, yPageCount;
  740.     CountPrintPages(ev, xPageCount, yPageCount);
  741.  
  742.     FW_ASSERT(xPageCount > 0);
  743.     FW_ASSERT(yPageCount > 0);
  744.  
  745.     FW_CPrintInfo* printInfo = GetPrintInfo();
  746.     FW_ASSERT(printInfo != NULL);
  747.  
  748.     short numCopies;
  749.     FW_Boolean collate;
  750.     printInfo->GetCopyCount(numCopies, collate);
  751.  
  752.     long firstPage, lastPage;
  753.     printInfo->GetPages(firstPage, lastPage);
  754.  
  755.     FW_ASSERT(firstPage > 0);
  756.     FW_ASSERT(lastPage > 0);
  757.  
  758.     long numPages = xPageCount * yPageCount;
  759.  
  760.     if (lastPage > numPages)
  761.     {
  762.         // prevent printing past last page
  763.         lastPage = numPages;
  764.     }
  765.     
  766.     // Get some OD objects that we will need
  767.     ODFrame* odFrame = fPrintView->GetFrame(ev)->GetODFrame(ev);
  768.  
  769.     FW_CPrintJob printJob(printInfo, this, printEnv);
  770.  
  771.     // Print the requested number of copies
  772.     short numCopiesPerPrintJob    = collate ? 1 : numCopies;
  773.     short numCopiesPerPage        = collate ? numCopies : 1;
  774.  
  775.     for (short copiesOfJob = 1; copiesOfJob <= numCopiesPerPrintJob; ++ copiesOfJob)
  776.     {
  777.         FW_CString jobTitle;
  778.         GetPrintJobTitle(ev, jobTitle);
  779.  
  780.         ODFacet* facet = printJob.OpenDocument(ev, odFrame, numPages, jobTitle);
  781.  
  782.         FW_TRY
  783.         {
  784.             for (long pageNumber = firstPage; pageNumber <= lastPage; ++ pageNumber)
  785.             {
  786.                 for (short copiesOfPage = 1; copiesOfPage <= numCopiesPerPage; ++ copiesOfPage)
  787.                 {
  788.                     printJob.OpenPage(pageNumber);
  789.  
  790.                     FW_TRY
  791.                     {
  792.                         long xPage = (pageNumber - 1) % xPageCount;
  793.                         long yPage = (pageNumber - 1) / xPageCount;
  794.  
  795.                         FW_CPrintInfo*    printInfo = GetPrintInfo();
  796.                     
  797.                         FW_CRect         pageBounds  = printInfo->GetPageBounds();
  798.                         FW_Fixed        pageWidth    = pageBounds.Width();
  799.                         FW_Fixed        pageHeight    = pageBounds.Height();
  800.                     
  801.                         pageBounds.Offset(FW_MultipliedByInt(pageWidth,  xPage), FW_MultipliedByInt(pageHeight, yPage));
  802.  
  803.                         PrintPage(ev, facet, pageBounds, xPage, yPage);
  804.                     }
  805.                     FW_CATCH_BEGIN
  806.                     FW_CATCH_EVERYTHING()
  807.                     {
  808.                         printJob.ClosePageOnError();
  809.                         FW_THROW_SAME();
  810.                     }
  811.                     FW_CATCH_END
  812.  
  813.                     printJob.ClosePage();
  814.                 }
  815.             }
  816.         }
  817.         FW_CATCH_BEGIN
  818.         FW_CATCH_EVERYTHING()
  819.         {
  820.             printJob.CloseDocumentOnError(ev, facet);
  821.             FW_THROW_SAME();
  822.         }
  823.         FW_CATCH_END
  824.  
  825.         printJob.CloseDocument(ev, facet);
  826.     }
  827.  
  828.     printJob.FinishPrinting();
  829. }
  830.  
  831. //----------------------------------------------------------------------------------------
  832. //    FW_CPrintHandler::CountPrintPages
  833. //----------------------------------------------------------------------------------------
  834.  
  835. void FW_CPrintHandler::CountPrintPages(Environment* ev, long& xPageCount, long& yPageCount)
  836. {
  837.     FW_CPrintInfo* printInfo = GetPrintInfo();
  838.     FW_ASSERT(printInfo != NULL);
  839.  
  840.     FW_CRect         pageBounds  = printInfo->GetPageBounds();
  841.     FW_Fixed        pageWidth    = pageBounds.Width();
  842.     FW_Fixed        pageHeight    = pageBounds.Height();
  843.  
  844.     FW_CPoint        content;
  845.     GetPrintContentExtent(ev, content);
  846.     
  847.     FW_ASSERT(content.x != FW_kFixed0);
  848.     FW_ASSERT(content.y != FW_kFixed0);
  849.  
  850.     xPageCount = FW_FixedToInt((content.x + FW_Half(pageWidth)  - FW_kFixedPos1) / pageWidth );
  851.     yPageCount = FW_FixedToInt((content.y + FW_Half(pageHeight) - FW_kFixedPos1) / pageHeight);
  852. }
  853.  
  854. //----------------------------------------------------------------------------------------
  855. //    FW_CPrintHandler::PrintPage
  856. //----------------------------------------------------------------------------------------
  857.  
  858. void FW_CPrintHandler::PrintPage(Environment* ev, ODFacet* facet, const FW_CRect& pageBounds, long xPage, long yPage)
  859. {
  860. FW_UNUSED(xPage);
  861. FW_UNUSED(yPage);
  862.     
  863.     // ----- Construct an appropriate transform and shape for this page
  864.     // Note: aqPageShape is in content coordinate 
  865.     FW_CAcquiredODTransform aqTransform = ::FW_NewODTransform(ev, pageBounds.TopLeft());
  866.  
  867.     FW_CAcquiredODShape    aqPageShape = ::FW_NewODShape(ev, pageBounds);
  868.  
  869.     // ----- Intersect the page shape with the print frame's extent
  870.     FW_CPoint content;
  871.     GetPrintContentExtent(ev, content);
  872.  
  873.     {
  874.         FW_CRect contentRect(FW_kFixed0, FW_kFixed0, content.x, content.y);
  875.         FW_CAcquiredODShape aqExtentShape = ::FW_NewODShape(ev, contentRect);
  876.         aqPageShape->Intersect(ev, aqExtentShape);
  877.     }
  878.     
  879.     // Transform from content to frame coordinate
  880.     fPrintView->ViewContentToFrame(ev, aqPageShape);
  881.  
  882.     // Take into account the position and scrolling of the print view
  883.     FW_CAcquiredODTransform aqContentToFrame = fPrintView->AcquireViewContentToFrameTransform(ev);
  884.     aqTransform->PostCompose(ev, aqContentToFrame);
  885.     
  886.     // Set the facet to use this transform and shape
  887.     aqTransform->Invert(ev);
  888.     facet->ChangeGeometry(ev, aqPageShape, aqTransform, NULL);
  889.     
  890.     // Print 
  891.     fPrintView->PrivSetTopPrintedView(ev, TRUE);
  892.     FW_TRY
  893.     {
  894.         // ----- First draw the printing view    
  895.         fPrintView->HandleDraw(ev, facet, aqPageShape);
  896.  
  897.         // ----- OpenDoc is not going to draw embedded facets. I need to force them to draw -----
  898.         if (fUpdateEmbeddedFacets)
  899.         {
  900.             FW_CFacetIterator ite(ev, facet);
  901.             for (ODFacet* embeddedFacet = ite.First(ev); ite.IsNotComplete(ev); embeddedFacet = ite.Next(ev))
  902.                 embeddedFacet->Update(ev, NULL, NULL);
  903.         }
  904.     }
  905.     FW_CATCH_BEGIN
  906.     FW_CATCH_EVERYTHING()
  907.     {
  908.         fPrintView->PrivSetTopPrintedView(ev, FALSE);
  909.         FW_THROW_SAME();
  910.     }
  911.     FW_CATCH_END
  912.     
  913.     fPrintView->PrivSetTopPrintedView(ev, FALSE);
  914. }
  915.  
  916. #ifdef FW_BUILD_WIN
  917. //----------------------------------------------------------------------------------------
  918. // WinGetFrameWindow
  919. //----------------------------------------------------------------------------------------
  920.  
  921. HWND WinGetFrameWindow(
  922.         Environment*    ev,
  923.         ODFrame*        frame)
  924. {
  925.     ODFrameFacetIterator* ite = frame->->CreateFacetIterator(ev);
  926.     ODFacet* facet = ite->First(ev);
  927.     delete ite;
  928.     
  929.     return facet->GetWindow(ev)->GetPlatformWindow(ev);
  930. }
  931.  
  932. //----------------------------------------------------------------------------------------
  933. // WinShowControl
  934. //----------------------------------------------------------------------------------------
  935.  
  936. void WinShowControl(
  937.         HWND        hWnd,
  938.         int            nID,
  939.         BOOL        bShow)
  940. {
  941.     HWND hWndControl = ::GetDlgItem(hWnd, nID);
  942.     if (hWndControl != NULL)
  943.         ::ShowWindow(hWndControl, bShow ? SW_SHOW : SW_HIDE);
  944. }
  945.  
  946. //----------------------------------------------------------------------------------------
  947. // WinPrintProgessDlgProc
  948. //----------------------------------------------------------------------------------------
  949.  
  950. int CALLBACK WinPrintProgessDlgProc(
  951.         HWND        hWnd,
  952.         UINT        message,
  953.         WPARAM        wParam,
  954.         LPARAM        lParam)
  955. {
  956.     switch (message)
  957.     {
  958.     case WM_INITDIALOG:
  959.         return TRUE;
  960.  
  961.     case WM_COMMAND:
  962.         gWinAbortPrinting = TRUE;
  963.         
  964.         ::WinShowControl(gWinProgressDlg, FW_kStartingJobID,        FALSE);
  965.  
  966.         ::WinShowControl(gWinProgressDlg, FW_kTotalPagesStrID,    FALSE);
  967.         ::WinShowControl(gWinProgressDlg, FW_kTotalPagesNumID,    FALSE);
  968.  
  969.         ::WinShowControl(gWinProgressDlg, FW_kCurrentPageStrID,    FALSE);
  970.         ::WinShowControl(gWinProgressDlg, FW_kCurrentPageNumID,    FALSE);
  971.         
  972.         ::WinShowControl(gWinProgressDlg, FW_kCancelingJobID,    TRUE);
  973.         
  974.         ::UpdateWindow(gWinProgressDlg);
  975.         
  976.         return TRUE;
  977.     
  978.     default:
  979.         return FALSE;
  980.     }
  981. }
  982.  
  983. //----------------------------------------------------------------------------------------
  984. // WinAbortProc
  985. //----------------------------------------------------------------------------------------
  986.  
  987. BOOL CALLBACK WinAbortProc(
  988.             HDC        hDC,
  989.             int        nCode)
  990. {
  991.     MSG msg;
  992.     
  993.     while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  994.     {
  995.         if (gWinProgressDlg == NULL || !::IsDialogMessage(gWinProgressDlg, &msg))
  996.         {
  997.             ::TranslateMessage(&msg);
  998.             ::DispatchMessage(&msg);
  999.         }
  1000.     }
  1001.     
  1002.     return !gWinAbortPrinting;
  1003. }
  1004. #endif
  1005.  
  1006. #ifdef FW_BUILD_MAC
  1007.  
  1008. //----------------------------------------------------------------------------------------
  1009. // MacIdleProc
  1010. //----------------------------------------------------------------------------------------
  1011.  
  1012. pascal void MacIdleProc()
  1013. {
  1014. #if 0
  1015.     short resFile = ::CurResFile();
  1016.  
  1017.     if (!gMacPrintAborted)
  1018.     {
  1019.         FW_Boolean bCancel = FALSE;
  1020.  
  1021.         FW_CMacTempPort tempPort = gMacProgressDlg;
  1022.  
  1023.         EventRecord event;
  1024.         RgnHandle rgnCursor = NULL;
  1025.         if (::WaitNextEvent(everyEvent, &event, 0, rgnCursor))
  1026.         {
  1027.             if (event.what == keyDown || event.what == autoKey)
  1028.             {
  1029.                 if ((event.message & charCodeMask) == '.')
  1030.                 {
  1031.                     if ((event.modifiers & cmdKey) != 0)
  1032.                     {
  1033.                         short             itemType;
  1034.                         ControlHandle     hItem = NULL;
  1035.                         Rect            box;
  1036.  
  1037.                         ::GetDialogItem(gMacProgressDlg, FW_kCancelButtonID, &itemType, (Handle*) &hItem, &box);
  1038.                         if (hItem != NULL)
  1039.                         {
  1040.                             long theTick;
  1041.                             ::HiliteControl(hItem, 1);
  1042.                             ::Delay(6,&theTick);
  1043.                             ::HiliteControl(hItem, 0);
  1044.                         }
  1045.  
  1046.                         bCancel = TRUE;
  1047.                     }
  1048.                 }
  1049.             }
  1050.             else if (::IsDialogEvent(&event))
  1051.             {
  1052.                 short nItemHit = 0;
  1053.                 DialogPtr dialog = NULL;
  1054.                 if (::DialogSelect(&event, &dialog, &nItemHit) &&
  1055.                     dialog == gMacProgressDlg &&
  1056.                     nItemHit == FW_kCancelButtonID)
  1057.                 {
  1058.                     bCancel = TRUE;
  1059.                 }
  1060.             }
  1061.         }
  1062.  
  1063.         if (bCancel)
  1064.         {
  1065.             // Update the dialog
  1066.             ::HideDialogItem(gMacProgressDlg, FW_kStartingJobID);
  1067.     
  1068.             ::HideDialogItem(gMacProgressDlg, FW_kTotalPagesStrID);
  1069.             ::HideDialogItem(gMacProgressDlg, FW_kTotalPagesNumID);
  1070.     
  1071.             ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageStrID);
  1072.             ::HideDialogItem(gMacProgressDlg, FW_kCurrentPageNumID);
  1073.     
  1074.             ::ShowDialogItem(gMacProgressDlg, FW_kCancelingJobID);
  1075.     
  1076.             ::UpdateControls(gMacProgressDlg, gMacProgressDlg->visRgn);
  1077.             ::UpdateDialog(gMacProgressDlg, gMacProgressDlg->visRgn);
  1078.             
  1079.             // Tell the print manager that the job should be aborted
  1080.             ::PrSetError(iPrAbort);
  1081.             
  1082.             gMacPrintAborted = TRUE;
  1083.         }
  1084.     }
  1085.  
  1086.     ::UseResFile(resFile);
  1087. #endif
  1088. }
  1089.  
  1090. #endif
  1091.  
  1092.