shellprint.c

Go to the documentation of this file.
00001 
00002 /*  $Id: shellprint.c 1260 2006-04-13 06:13:10Z sethdill $    */
00003 
00004 /******************************************************************************
00005 
00006     UserLand Frontier(tm) -- High performance Web content management,
00007     object database, system-level and Internet scripting environment,
00008     including source code editing and debugging.
00009 
00010     Copyright (C) 1992-2004 UserLand Software, Inc.
00011 
00012     This program is free software; you can redistribute it and/or modify
00013     it under the terms of the GNU General Public License as published by
00014     the Free Software Foundation; either version 2 of the License, or
00015     (at your option) any later version.
00016 
00017     This program is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020     GNU General Public License for more details.
00021 
00022     You should have received a copy of the GNU General Public License
00023     along with this program; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 
00026 ******************************************************************************/
00027 
00028 #include "frontier.h"
00029 #include "standard.h"
00030 
00031 #include "memory.h"
00032 #include "cursor.h"
00033 #include "error.h"
00034 #include "kb.h"
00035 #include "quickdraw.h"
00036 #include "strings.h"
00037 #include "shell.h"
00038 #include "shellprivate.h"
00039 #include "shell.rsrc.h"
00040 #include "shellprint.h"
00041 
00042 #ifdef MACVERSION
00043 
00044 CGrafPtr currentprintport = NULL;
00045 
00046 #   if TARGET_API_MAC_CARBON
00047 
00048 #       define iPrAbort kPMCancel
00049 
00050 #   endif
00051 
00052 #endif
00053 
00054 #ifdef WIN95VERSION
00055 
00056 HWND currentprintport = NULL;
00057 
00058 #endif
00059 
00060 typrintinfo shellprintinfo;
00061 
00062 /*
00063 static shelldisposeprintinfo (void) {
00064 
00065     disposehandle ((Handle) shellprintinfo.printhandle);
00066     
00067     clearbytes (&shellprintinfo, longsizeof (shellprintinfo));
00068     } /%shelldisposeprintinfo%/
00069 */
00070 
00071 #if defined(WIN95VERSION) || (defined(MACVERSION) && !TARGET_API_MAC_CARBON)
00072 
00073 static boolean shellcheckprinterror (boolean flopening) {
00074     
00075     /*
00076      returns false if there was a print error.
00077      
00078      1/18/93 dmb: take flopening parameter, & special case this error
00079      */
00080     
00081     OSErr x;
00082     bigstring bserror;
00083     
00084 #   ifdef MACVERSION
00085     
00086     //Code change by Timothy Paustian Friday, June 16, 2000 3:50:52 PM
00087     //Changed to Opaque call for Carbon
00088     // this is pointless --- explicitly excluded
00089 #       if TARGET_API_MAC_CARBON == 1
00090     
00091     x = PMSessionError(shellprintinfo.printhandle);
00092     
00093 #       else    
00094     
00095     x = PrError ();
00096     
00097 #       endif
00098     
00099     if (x == noErr) /*no error, keep going*/
00100         return (true);
00101     
00102     if (x != iPrAbort) {
00103         
00104         if (flopening) 
00105         {
00106             GetIndString (bserror, interfacelistnumber, trychooserstring);
00107         }
00108         else {
00109             
00110             getsystemerrorstring (x, bserror);
00111             
00112             parsedialogstring (bserror, "\x06" ".Print", nil, nil, nil, bserror);
00113         }
00114         
00115         shellerrormessage (bserror);
00116     }
00117     
00118 #   endif
00119     
00120 #ifdef WIN95VERSION
00121     x = GetLastError();
00122 
00123     if (x == 0)
00124         return (true);
00125 
00126     getsystemerrorstring (x, bserror);
00127 
00128     shellerrormessage (bserror);
00129 #endif
00130     
00131     return (false); /*there was an error*/
00132     } /*shellcheckprinterror*/
00133 
00134 #endif
00135 
00136 
00137 static boolean
00138 carbonKillPrintVars(void)
00139 {
00140     
00141 #if TARGET_API_MAC_CARBON == 1
00142     
00143     // empty out carbon print vars
00144     // setting to nil is important since it is checked for
00145     if (nil != shellprintinfo.printsettings)
00146     {
00147         PMRelease(shellprintinfo.printsettings);
00148         shellprintinfo.printsettings = nil;
00149     }
00150     
00151     if (nil != shellprintinfo.pageformat)
00152     {
00153         PMRelease(shellprintinfo.pageformat);
00154         shellprintinfo.pageformat = nil;
00155     }
00156     
00157     if (nil != shellprintinfo.printhandle)
00158     {
00159         PMRelease(shellprintinfo.printhandle);
00160         shellprintinfo.printhandle = nil;
00161     }
00162     
00163 #endif
00164     
00165     return(true);
00166 }   
00167 
00168 
00169 static boolean
00170 carbonValidSession(void)
00171 {
00172 #if TARGET_API_MAC_CARBON == 1
00173     return (nil != shellprintinfo.printhandle);
00174 #else
00175     return (false);
00176 #endif
00177 }
00178 
00179 
00180 static boolean
00181 carbonSessionPrintSession()
00182 {
00183 #if TARGET_API_MAC_CARBON == 1
00184     OSStatus    theErr;
00185 
00186     if (nil == shellprintinfo.printhandle)
00187     {
00188         theErr = PMCreateSession(&shellprintinfo.printhandle);
00189         
00190         if(theErr != noErr)
00191         {
00192             shellprintinfo.printhandle = nil;
00193             return (false);
00194         }
00195     }
00196 #endif  
00197     return(true);
00198 }
00199 
00200 
00201 static boolean
00202 carbonSessionDefaultPageAndSettingValidate(void)
00203 {
00204 #if TARGET_API_MAC_CARBON == 1
00205     OSStatus    theErr;
00206     Boolean     f;
00207     
00208     
00209     theErr = PMSessionDefaultPageFormat(
00210                                         shellprintinfo.printhandle,
00211                                         shellprintinfo.pageformat);
00212     if(theErr != noErr)
00213         goto error;
00214     
00215     theErr = PMSessionValidatePageFormat(
00216                                          shellprintinfo.printhandle,
00217                                          shellprintinfo.pageformat, &f);
00218     if(theErr != noErr)
00219         goto error;
00220     
00221     theErr = PMSessionDefaultPrintSettings(
00222                                            shellprintinfo.printhandle,
00223                                            shellprintinfo.printsettings);
00224     if(theErr != noErr)
00225         goto error;
00226     
00227     theErr = PMSessionValidatePrintSettings(
00228                                             shellprintinfo.printhandle,
00229                                             shellprintinfo.printsettings, &f);
00230     if(theErr != noErr)
00231         goto error;
00232 
00233     
00234     return (true);
00235     
00236 error:
00237         carbonKillPrintVars();
00238 
00239 #endif
00240     
00241     return false;
00242     
00243 }
00244 
00245 
00246 static boolean
00247 carbonCreateFormatAndSetting(void)
00248 {
00249 #if TARGET_API_MAC_CARBON == 1
00250     OSStatus    theErr;
00251 
00252     theErr = PMCreatePageFormat(&shellprintinfo.pageformat);
00253     if(theErr != noErr || (shellprintinfo.pageformat == kPMNoPageFormat))
00254         goto error;
00255     
00256     theErr = PMCreatePrintSettings(&shellprintinfo.printsettings);
00257     if(theErr != noErr)
00258         goto error;
00259 
00260     return (true);
00261     
00262 error:
00263         carbonKillPrintVars();
00264     
00265 #endif
00266     return false;
00267 }
00268 
00269 
00270 static boolean
00271 carbonStdSetup(void)
00272 {
00273     boolean fl;
00274     
00275     fl = carbonKillPrintVars();
00276     
00277     fl &= !carbonCreateFormatAndSetting();
00278     
00279     fl &= !carbonSessionPrintSession();
00280     
00281     fl &= !carbonSessionDefaultPageAndSettingValidate();
00282     
00283     return(fl);
00284 }
00285 
00286 
00287 #define rect2rect(dstr, srcr) \
00288 {\
00289     srcr.top    = (short)lround(dstr.top); \
00290     srcr.bottom = (short)lround(dstr.bottom); \
00291     srcr.left   = (short)lround(dstr.left); \
00292     srcr.right  = (short)lround(dstr.right); \
00293 }
00294 
00295 
00296 static void shellcopyprintinfo (void) {
00297     
00298     /*
00299      set up our printing rect according to the print manager's rects.
00300      
00301      we'll use the smaller of the printable area (rPage) and the physical 
00302      paper (rPaper) with 1/2 inch margins
00303      
00304      5.0.1 dmb: measure in pixels (1/72 inch) for Win
00305      */
00306     
00307     Rect
00308         rpage,
00309         rpaper;
00310     
00311     //typrintinfo *dbgprintinfo = &shellprintinfo;
00312     
00313 #ifdef MACVERSION
00314 #   if TARGET_API_MAC_CARBON == 1
00315     
00316     static int firstTime = 1;
00317     
00318     OSErr           theErr;
00319     boolean         fl;
00320     PMRect          dRect;
00321     
00322     
00323     if (1 == firstTime)
00324     {
00325         carbonSessionPrintSession();
00326         carbonCreateFormatAndSetting();
00327         carbonSessionDefaultPageAndSettingValidate();
00328     }
00329     
00330     
00331     theErr = PMSessionValidatePageFormat(
00332                                          shellprintinfo.printhandle,
00333                                          shellprintinfo.pageformat, &fl);
00334     
00335     if(theErr != noErr) // || !fl) // why should i kill this if nothing has changed???
00336         oserror(theErr);
00337     
00338     theErr = PMGetAdjustedPageRect(shellprintinfo.pageformat, &dRect);
00339     
00340     shellprintinfo.pagerect = dRect;
00341     
00342     theErr = PMGetAdjustedPaperRect(shellprintinfo.pageformat, &dRect);
00343     
00344     rect2rect(dRect, shellprintinfo.paperrect);
00345     
00346     rect2rect(shellprintinfo.pagerect, rpage);
00347     
00348     rpaper = shellprintinfo.paperrect;
00349     
00350 #   else
00351     
00352     rpage = (**shellprintinfo.printhandle).prInfo.rPage;
00353     
00354     rpaper = (**shellprintinfo.printhandle).rPaper;
00355     
00356 #   endif
00357     
00358 #endif
00359     
00360 #ifdef WIN95VERSION
00361     
00362     long res = 72L;
00363     
00364     pushport (hwndMDIClient);
00365     res = GetDeviceCaps(getcurrentDC (), LOGPIXELSX);
00366     popport ();
00367     
00368     rpaper.left = 0;
00369     rpaper.top = 0;
00370     rpaper.right = shellprintinfo.pagesetupinfo.ptPaperSize.x * res / 1000L;
00371     rpaper.bottom = shellprintinfo.pagesetupinfo.ptPaperSize.y * res / 1000L;
00372     
00373     rpage.left = rpaper.left + (shellprintinfo.pagesetupinfo.rtMinMargin.left * res / 1000L);
00374     rpage.top = rpaper.top + (shellprintinfo.pagesetupinfo.rtMinMargin.top * res / 1000L);
00375     rpage.right = rpaper.right - (shellprintinfo.pagesetupinfo.rtMinMargin.right * res / 1000L);
00376     rpage.bottom = rpaper.bottom - (shellprintinfo.pagesetupinfo.rtMinMargin.bottom * res / 1000L);
00377     
00378     shellprintinfo.margins.left = shellprintinfo.pagesetupinfo.rtMargin.left * res / 1000L;
00379     shellprintinfo.margins.top = shellprintinfo.pagesetupinfo.rtMargin.top * res / 1000L;
00380     shellprintinfo.margins.right = shellprintinfo.pagesetupinfo.rtMargin.right * res / 1000L;
00381     shellprintinfo.margins.bottom = shellprintinfo.pagesetupinfo.rtMargin.bottom * res / 1000L;
00382     
00383 #endif
00384     
00385     shellprintinfo.paperrect.top = max (rpage.top, rpaper.top + shellprintinfo.margins.top);
00386     
00387     shellprintinfo.paperrect.left = max (rpage.left, rpaper.left + shellprintinfo.margins.left);
00388     
00389     shellprintinfo.paperrect.bottom = min (rpage.bottom, rpaper.bottom - shellprintinfo.margins.bottom);
00390     
00391     shellprintinfo.paperrect.right = min (rpage.right, rpaper.right - shellprintinfo.margins.right);
00392     } /*shellcopyprintinfo*/
00393 
00394 
00395 boolean shellinitprint (void) {
00396     
00397     /*
00398      9/5/90 dmb: close print resources after initializing stuff
00399      
00400      10/21/91 dmb: added margins field to print info; structure is now here to 
00401      have user-settable margins.
00402      
00403      12/31/91 dmb: initialize shellprintinfo.paperrect to standard 72dpi values in 
00404      case no printer is chosen and shellcopyprintinfo never gets called
00405      
00406      1/18/93 dmb: don't call shellcheckprinterror the first time; if PrOpen fails 
00407      here, we don't want to raise an alert.
00408      */
00409     
00410 #if MACVERSION && !TARGET_API_MAC_CARBON
00411     
00412     Handle h;
00413     
00414 #endif
00415     
00416     currentprintport = NULL;
00417     
00418     clearbytes (&shellprintinfo, longsizeof (shellprintinfo));
00419     
00420     setrect (&shellprintinfo.margins, 36, 36, 36, 36);
00421     
00422     setrect (&shellprintinfo.paperrect, 5, 6, 725, 546); /*defaults in case PrOpen fails*/
00423     
00424     shellprintinfo.scaleMult = 1;
00425     shellprintinfo.scaleDiv = 1;
00426     
00427 #ifdef MACVERSION
00428     
00429 #   if TARGET_API_MAC_CARBON == 1
00430     
00431     //I realized this is only called once during the startup of the app.
00432     //Carbon printing really doesn't need any global structures. Better to 
00433     //allocate them as we use them.
00434     
00435     // Nope.
00436     // We need a global var for calling page setup
00437     
00438     shellprintinfo.printport = nil;
00439     shellprintinfo.printhandle = nil;
00440     shellprintinfo.pageformat = nil;
00441     shellprintinfo.printsettings = nil;
00442     // shellprintinfo.pagerect = {0,0,0,0};
00443     
00444 #   else        
00445     
00446     if (!newclearhandle (longsizeof (TPrint), &h))
00447         return (false);
00448     
00449     shellprintinfo.printhandle = (THPrint) h; /*copy into print record*/
00450     
00451     PrOpen (); /*initialize the Mac print manager*/
00452     
00453     if (PrError () != noErr)
00454         goto error;
00455     
00456     PrintDefault (shellprintinfo.printhandle); /*set default print record*/
00457     
00458     PrClose (); /*shouldn't leave print resources open all the time*/
00459     
00460     if (!shellcheckprinterror (false)) 
00461         goto error;
00462 #   endif
00463     
00464 #endif
00465     
00466 #ifdef WIN95VERSION
00467     
00468     ZeroMemory (&shellprintinfo.pagesetupinfo, sizeof (PAGESETUPDLG));
00469     
00470     shellprintinfo.pagesetupinfo.lStructSize = sizeof (PAGESETUPDLG);
00471     
00472     shellprintinfo.pagesetupinfo.hwndOwner = NULL;
00473     
00474     shellprintinfo.pagesetupinfo.Flags = PSD_RETURNDEFAULT | PSD_NOWARNING;
00475     
00476     if (! PageSetupDlg (&shellprintinfo.pagesetupinfo)) {
00477         
00478         if (CommDlgExtendedError() != 0)
00479             goto error;
00480     }
00481     
00482 #endif
00483     
00484     //#if !TARGET_API_MAC_CARBON
00485     
00486     shellcopyprintinfo (); /*copies fields from handle into record*/
00487     
00488     //#endif
00489     
00490     return (true);
00491     
00492 #if !defined(MACVERSION) || !TARGET_API_MAC_CARBON
00493     
00494 error:
00495         
00496 #endif
00497         
00498         /*
00499          shelldisposeprintinfo ();
00500          */
00501         
00502         return (false);
00503 } /*shellinitprint*/
00504 
00505 
00506 static boolean shellpagesetupvisit (WindowPtr w, ptrvoid refcon) {
00507 #pragma unused (refcon)
00508     
00509     shellpushglobals (w);
00510     
00511     (*shellglobals.pagesetuproutine) ();
00512     
00513     shellpopglobals ();
00514     
00515     return (true);
00516 } /*shellpagesetupvisit*/
00517 
00518 
00519 boolean shellpagesetup (void) {
00520     
00521     /*
00522      9/5/90 dmb:  open and close print resources each time we're called
00523      
00524      9/27/91 dmb: don't do anything when the user cancels the dialog
00525      */
00526     
00527     boolean fl;
00528     
00529 #ifdef MACVERSION
00530     
00531     //Code change by Timothy Paustian Friday, June 16, 2000 10:08:37 PM
00532     //new code for the new print manager.
00533     
00534 #   if TARGET_API_MAC_CARBON == 1
00535     
00536     if (!carbonValidSession())
00537     {
00538         carbonStdSetup();
00539     }
00540     
00541     {
00542         OSStatus status;
00543         
00544         status = PMSessionPageSetupDialog(
00545                                           shellprintinfo.printhandle, 
00546                                           shellprintinfo.pageformat,
00547                                           &fl);
00548         
00549         if (!fl)
00550             status = kPMCancel; // user clicked Cancel button
00551         
00552         if(status != noErr)
00553             return false;
00554         
00555     }
00556     
00557 #   else
00558     
00559     PrOpen ();
00560     
00561     if (!shellcheckprinterror (true)) 
00562         return (false);
00563     
00564     PrValidate (shellprintinfo.printhandle);
00565     
00566     fl = PrStlDialog (shellprintinfo.printhandle);
00567     
00568     PrClose ();
00569     
00570     if (!shellcheckprinterror (false))
00571         return (false);
00572     
00573 #   endif
00574     
00575 #endif
00576     
00577 #ifdef WIN95VERSION
00578     
00579     shellprintinfo.pagesetupinfo.Flags = PSD_MARGINS | PSD_INTHOUSANDTHSOFINCHES;
00580     
00581     shellprintinfo.pagesetupinfo.hwndOwner = hwndMDIClient;
00582     
00583     fl = PageSetupDlg (&shellprintinfo.pagesetupinfo);
00584     
00585 #endif
00586     
00587     if (!fl)
00588         return (false);
00589     
00590     shellcopyprintinfo (); /*copies fields from handle into record*/
00591     
00592     shellvisittypedwindows (-1, &shellpagesetupvisit, nil); /*visit all windows*/
00593     
00594     return (true);
00595 } /*shellpagesetup*/
00596 
00597 
00598 /*
00599 boolean shellsetprintmargins (Rect newmargins) {
00600     
00601     /%
00602     10/21/91 dmb: we may need this some day [never tested]
00603     %/
00604     
00605     PrOpen ();
00606     
00607     if (!shellcheckprinterror (true))
00608         return (false);
00609     
00610     PrValidate (shellprintinfo.printhandle);
00611     
00612     PrClose ();
00613     
00614     shellprintinfo.margins = newmargins;
00615     
00616     shellcopyprintinfo (); /%copies fields from handle into record%/
00617     
00618     shellvisittypedwindows (-1, &shellpagesetupvisit, nil); /%visit all windows%/
00619     
00620     return (true);
00621     } /%shellsetprintmargins%/
00622 
00623 
00624 void printresetrects (hdlwindowinfo hinfo, Rect * r) {
00625     
00626     /%
00627     (re)initialize all of the rectangles stored in the window's information 
00628     record.  start by clearing the buttons rect and setting the content rect 
00629     to be the owning window's portrect.  the resetwindowrects callback routine 
00630     is responsible for:
00631         1.  adjust the contentrect, if necessary
00632         2.  setting the buttons rect, if used
00633         3.  positioning any scrollbars
00634         4.  setting up the grow box rect
00635     %/
00636     
00637     register hdlwindowinfo h = hinfo;
00638     
00639     // (**h).contentrect = (*(**h).macwindow).portRect;
00640     
00641     (**h).contentrect = *r;
00642     
00643     zerorect (&(**h).buttonsrect);
00644     
00645     (*shellglobals.resetrectsroutine) (h);
00646     
00647     (*shellglobals.resizeroutine) ();
00648     } /%printresetrects%/
00649 */
00650 
00651 
00652 boolean iscurrentportprintport (void) {
00653     
00654     return (isprintingactive () && (currentprintport == getport()));
00655     } /*iscurrentportprintport*/
00656 
00657 
00658 boolean isprintingactive (void) {
00659     
00660     return (currentprintport != NULL);
00661     } /*isprintingactive*/
00662 
00663 
00664 boolean getprintscale (long * scaleMult, long * scaleDiv) {
00665     
00666     *scaleMult = shellprintinfo.scaleMult;
00667     
00668     *scaleDiv = shellprintinfo.scaleDiv;
00669     
00670     return (true);
00671     } /*getprintscale*/
00672 
00673 
00674 #ifdef WIN95VERSION
00675 
00676 // kw - 2006-06 - splitted for each platform
00677 #pragma mark shellprint windows
00678 boolean shellprint (WindowPtr w, boolean fldialog) {
00679     
00680     /*
00681      9/5/90 dmb:  open and close print resources each time we're called.  also, 
00682      make sure we close each page opened, even when errors occur
00683      
00684      9/28/91 dmb: for exit of edit mode before printing
00685      
00686      4/24/92 dmb: make sure bJDocLoop is bSpoolLoop before calling PrPicFile, as 
00687      per IM II-155.  otherwise, we can get bogus PrErrors
00688      */
00689 
00690     
00691     PRINTDLG pd;
00692     DOCINFO di;
00693     HDC printport;
00694     hdlwindowinfo hinfo;
00695     
00696     short i;
00697     boolean fl = false;
00698     
00699     if (w == nil) /*defensive driving*/
00700         return (false);
00701     
00702     fl = false; /*until sucessfull print, this is return value*/
00703 
00704     ZeroMemory (&pd, sizeof (PRINTDLG));
00705     pd.lStructSize = sizeof (PRINTDLG);
00706     pd.hwndOwner = hwndMDIClient;
00707     pd.Flags = PD_RETURNDC;
00708     
00709     if (!fldialog)
00710         pd.Flags = pd.Flags | PD_RETURNDEFAULT;
00711     
00712     if (! PrintDlg (&pd)) {
00713         goto exit;
00714     }
00715     
00716     setcursortype (cursoriswatch);
00717     
00718     shellupdateallnow (); /*update all windows that were dirtied by the print dialog*/
00719     
00720     shellpushglobals (w);
00721     
00722     (*shellglobals.settextmoderoutine) (false); /*make sure editing changes are accepted*/
00723         
00724     di.cbSize = sizeof(DOCINFO);
00725     di.lpszDocName = "Frontier Document";
00726     di.lpszOutput = NULL;
00727     di.lpszDatatype = NULL;
00728     di.fwType = 0;
00729     
00730     shellprintinfo.printport = printport = pd.hDC;
00731     shellprintinfo.scaleMult = GetDeviceCaps(pd.hDC, LOGPIXELSX);
00732     shellprintinfo.scaleDiv = GetDeviceCaps(getcurrentDC (), LOGPIXELSX);
00733     
00734     StartDoc (printport, &di);
00735     winpushport (w, printport);
00736     currentprintport = w;
00737     
00738     getwindowinfo (w, &hinfo);
00739     
00740     
00741     /*prepares for printing*/
00742     (*shellglobals.beginprintroutine) ();
00743     
00744     /*fills in fields of printinfo record*/
00745     (*shellglobals.setprintinfoproutine) ();
00746     //this only counts the number of pages.
00747     
00748     
00749     for (i = 1; i <= shellprintinfo.ctpages; i++) { /*print one page*/
00750         
00751         if (StartPage(shellprintinfo.printport) > 0) {
00752             fl = (*shellglobals.printroutine) (i);
00753         }
00754         
00755         EndPage (shellprintinfo.printport);
00756         
00757         if (!fl)
00758             break;
00759     } /*for*/
00760     
00761     
00762     EndDoc (printport);
00763     
00764     winpopport();
00765     
00766     currentprintport = NULL;
00767     
00768     DeleteDC (printport);
00769     
00770     
00771     (*shellglobals.endprintroutine) ();
00772     
00773     shellpopglobals ();
00774     
00775 exit:
00776     currentprintport = NULL;
00777 
00778     return (fl);
00779 
00780 } /*shellprint*/
00781 
00782         
00783 #endif
00784 
00785 #ifdef MACVERSION
00786 #   if TARGET_API_MAC_CARBON == 1
00787 #       pragma mark shellprint carbon
00788 
00789 boolean shellprint (WindowPtr w, boolean fldialog) {
00790 
00791     /*
00792     9/5/90 dmb:  open and close print resources each time we're called.  also, 
00793     make sure we close each page opened, even when errors occur
00794     
00795     9/28/91 dmb: for exit of edit mode before printing
00796     
00797     4/24/92 dmb: make sure bJDocLoop is bSpoolLoop before calling PrPicFile, as 
00798     per IM II-155.  otherwise, we can get bogus PrErrors
00799     */
00800 
00801     OSStatus        theErr;
00802     boolean         accepted;
00803 
00804     //remember that this is a grafport, TPPrPort isn't
00805     //but this should work because all the code that
00806     //messes with printing uses a grafport. 
00807 
00808     // CGrafPtr         printport;
00809     UInt32
00810         minPage = 1,
00811         maxPage = 9999;
00812 
00813     SInt32  firstPage = 1;
00814     SInt32  lastPage = 9999;
00815 
00816     short i;
00817     boolean fl = false;
00818     
00819     if (w == nil) /*defensive driving*/
00820         return (false);
00821 
00822     if (!carbonValidSession())
00823     {
00824         carbonStdSetup();
00825     }
00826     
00827     fl = false; /*until sucessfull print, this is return value*/
00828     
00829     /*fills in fields of printinfo record*/
00830     (*shellglobals.setprintinfoproutine) ();
00831 
00832     //this only counts the number of pages.
00833     maxPage = shellprintinfo.ctpages;
00834     
00835     //set the page range
00836     theErr = PMSetPageRange(shellprintinfo.printsettings, minPage, maxPage);
00837     if(theErr != noErr){
00838         goto exit;
00839     }
00840 
00841     //finally display the dialog
00842     if (fldialog)
00843     {
00844         theErr = PMSessionPrintDialog(
00845                              shellprintinfo.printhandle,
00846                              shellprintinfo.printsettings,
00847                              shellprintinfo.pageformat,
00848                             &accepted);
00849 
00850         //either the user canceled or their was some other error.
00851         //don't print
00852         if (!accepted || (theErr != noErr)){
00853             goto exit;
00854         }
00855     }
00856 
00857     setcursortype (cursoriswatch);
00858 
00859     shellupdateallnow (); /*update all windows that were dirtied by the print dialog*/
00860 
00861     shellpushglobals (w);
00862 
00863     (*shellglobals.settextmoderoutine) (false); /*make sure editing changes are accepted*/
00864 
00865     pushport (nil); /*save current port on stack*/
00866 
00867     //Code change by Timothy Paustian Friday, June 16, 2000 4:19:04 PM
00868     //Changed to Opaque call for Carbon
00869     //I will have to watch out for this,
00870     //to save the port I need to use..
00871     //PMSessionGetGraphicsContext (
00872     //This does not return the printport, but TPrPort gPort field
00873     //I think I can get away with this because the only function that uses
00874     //it is pgInitDevice and that assumes you are passing a port.
00875     //It is OK in the old code because the first item in the struct is a port
00876 
00877     //we have to call this first before we can get the graphics context
00878     theErr = PMSessionBeginDocument(
00879                             shellprintinfo.printhandle,
00880                             shellprintinfo.printsettings,
00881                             shellprintinfo.pageformat);
00882 
00883     if(theErr != noErr)
00884         goto exit;
00885 
00886     /*prepares for printing*/
00887     (*shellglobals.beginprintroutine) ();
00888 
00889     /*fills in fields of printinfo record*/
00890     // (*shellglobals.setprintinfoproutine) ();
00891     //this only counts the number of pages.
00892     
00893     //limit this to the number of pages the user asked for. 
00894 
00895     theErr = PMGetFirstPage(shellprintinfo.printsettings, (UInt32 *) (&firstPage));
00896     if (theErr == noErr)
00897     {
00898         theErr = PMGetLastPage(shellprintinfo.printsettings, (UInt32 *) (&lastPage));
00899     }
00900 
00901     //sanity checks
00902     if(theErr != noErr)
00903         goto exit;
00904 
00905     if(lastPage > shellprintinfo.ctpages)
00906         lastPage = shellprintinfo.ctpages;
00907 
00908     for (i = 1; i <= lastPage; i++) { /*print one page*/
00909 
00910         theErr = PMSessionBeginPage(
00911                              shellprintinfo.printhandle,
00912                              shellprintinfo.pageformat,
00913                             &shellprintinfo.pagerect);
00914 
00915         if (theErr != noErr)
00916             break;
00917         
00918         //now set the graphics context.
00919         //in carbon we have to set up the port each time
00920         //to make sure it is in the printer port. Since other things can be going on
00921         //during the printing, the port may have changed.
00922         {
00923             GrafPtr thePort, oldPort;
00924 
00925             GetPort(&oldPort);
00926 
00927             //the second parameter is currently ignored.
00928             theErr = PMSessionGetGraphicsContext(
00929                                     shellprintinfo.printhandle,
00930                                     kPMGraphicsContextQuickdraw,
00931                                     (void **) &thePort);
00932 
00933             shellprintinfo.printport = thePort;
00934 
00935             SetPort(thePort);
00936 
00937             // Draw the page
00938             SetFractEnable (true);
00939 
00940             fl = (*shellglobals.printroutine) (i);
00941 
00942             //reset back to the old port.
00943             SetPort(oldPort);
00944         }
00945 
00946         theErr = PMSessionEndPage(shellprintinfo.printhandle);
00947 
00948         if (!fl)
00949             break;
00950 
00951         if (keyboardescape ())
00952         {
00953             theErr = PMSessionSetError(shellprintinfo.printhandle, iPrAbort);
00954         }
00955     } /*for*/
00956 
00957     theErr = PMSessionEndDocument(shellprintinfo.printhandle);
00958 
00959     if(theErr !=  noErr)
00960         goto exit;
00961 
00962     //everything worked and printing is done. so set fl to true.
00963     fl = true;
00964 
00965     //the PrPicFile is not supported in carbon.
00966 
00967     popport ();
00968 
00969     (*shellglobals.endprintroutine) ();
00970 
00971     shellpopglobals ();
00972 
00973 exit:
00974 
00975     carbonKillPrintVars();
00976 //  PMRelease(shellprintinfo.printhandle);
00977 //  PMRelease(pageformat);
00978 //  PMRelease(printsettings);
00979 
00980     currentprintport = NULL;
00981 
00982     return (fl);
00983 
00984     } /*shellprint*/
00985 
00986 #   else
00987 #       pragma mark shellprint classic
00988 boolean shellprint (WindowPtr w, boolean fldialog) {
00989 
00990     /*
00991     9/5/90 dmb:  open and close print resources each time we're called.  also, 
00992     make sure we close each page opened, even when errors occur
00993     
00994     9/28/91 dmb: for exit of edit mode before printing
00995     
00996     4/24/92 dmb: make sure bJDocLoop is bSpoolLoop before calling PrPicFile, as 
00997     per IM II-155.  otherwise, we can get bogus PrErrors
00998     */
00999 
01000     // classic mac
01001     TPPrPort printport;
01002     TPrStatus printstatus;
01003     THPrint hp = shellprintinfo.printhandle;
01004 
01005     SInt32  firstPage = 1;
01006     SInt32  lastPage = 9999;
01007 
01008     short i;
01009     boolean fl = false;
01010     
01011     if (w == nil) /*defensive driving*/
01012         return (false);
01013 
01014     PrOpen ();
01015     if (!shellcheckprinterror (true))
01016         return (false);
01017 
01018     fl = false; /*until sucessfull print, this is return value*/
01019 
01020     if (fldialog) {
01021         if (!PrJobDialog (hp))
01022             goto exit;
01023     }
01024     else
01025         PrValidate (hp);
01026 
01027     setcursortype (cursoriswatch);
01028 
01029     shellupdateallnow (); /*update all windows that were dirtied by the print dialog*/
01030 
01031     shellpushglobals (w);
01032 
01033     (*shellglobals.settextmoderoutine) (false); /*make sure editing changes are accepted*/
01034 
01035     pushport (nil); /*save current port on stack*/
01036 
01037     shellprintinfo.printport = printport = PrOpenDoc (hp, nil, nil);
01038     currentprintport = w;
01039 
01040     /*prepares for printing*/
01041     (*shellglobals.beginprintroutine) ();
01042 
01043     /*fills in fields of printinfo record*/
01044     (*shellglobals.setprintinfoproutine) ();
01045     //this only counts the number of pages.
01046 
01047     // wird in classic lastPage gesetzt?
01048     if(lastPage > shellprintinfo.ctpages)
01049         lastPage = shellprintinfo.ctpages;
01050 
01051     for (i = 1; i <= lastPage; i++) { /*print one page*/
01052 
01053         if (PrError () != noErr)
01054             break;
01055 
01056         PrOpenPage (printport, nil);
01057 
01058         if (PrError () == noErr) {
01059 
01060             SetFractEnable (true);
01061 
01062             fl = (*shellglobals.printroutine) (i);
01063 
01064             SetFractEnable (false);
01065         }
01066 
01067         PrClosePage (printport);
01068 
01069         if (!fl)
01070             break;
01071 
01072         if (keyboardescape ()) {
01073 
01074             PrSetError (iPrAbort);
01075 
01076         }
01077     } /*for*/
01078 
01079     PrCloseDoc (printport);
01080 
01081     if (fl) {
01082 
01083         if (    (PrError () == noErr)
01084             && ((**hp).prJob.bJDocLoop == bSpoolLoop))
01085         {
01086             PrPicFile (hp, nil, nil, nil, &printstatus);
01087         }
01088 
01089         fl = shellcheckprinterror (false);
01090     }
01091 
01092     popport ();
01093     
01094     (*shellglobals.endprintroutine) ();
01095 
01096     shellpopglobals ();
01097 
01098 exit:
01099 
01100     PrClose ();
01101 
01102     currentprintport = NULL;
01103 
01104     return (fl);
01105 
01106     } /*shellprint*/
01107 
01108 #   endif
01109 #endif
01110 

Generated on Wed May 31 18:20:01 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6