home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Framewrk / FWPart / Sources / FWPrHdlr.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  25.5 KB  |  1,010 lines  |  [TEXT/MPS ]

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