appletprinting.c

Go to the documentation of this file.
00001 
00002 /*  $Id: appletprinting.c 355 2005-01-11 22:48:55Z andreradke $    */
00003 
00004 /* copyright 1991-96 UserLand Software, Inc. All Rights Reserved.*/
00005 
00006 
00007 #include "appletinternal.h"
00008 #include "appletops.h"
00009 #include "appletquickdraw.h"
00010 #include "appletmain.h"
00011 
00012 
00013 static boolean flprintinit = false;
00014 
00015 
00016 static void printcomputewindowinfo (WindowPtr w, hdlappwindow appwindow) {
00017     
00018     Rect r;
00019     
00020     r = (*w).portRect;
00021     
00022     (**appwindow).windowrect = r;
00023     
00024     (**appwindow).contentrect = r;
00025     
00026     zerorect (&(**appwindow).statusrect);
00027     
00028     (**appwindow).windowvertpixels = r.bottom - r.top;
00029     
00030     (**appwindow).windowhorizpixels = r.right - r.left;
00031     
00032     (**appwindow).macwindow = w;
00033     } /*printcomputewindowinfo*/
00034     
00035 
00036 static void disposeprintinfo (void) {
00037     
00038     /*
00039     2/18/92 dmb: don't clear all fields; just nil the handle.  that way, a 
00040     hard-coded paper area can be maintained.
00041     */
00042     
00043     disposehandle ((Handle) app.printinfo.printhandle);
00044     
00045     app.printinfo.printhandle = nil;
00046     } /*disposeprintinfo*/
00047 
00048 
00049 static boolean checkprinterror (void) {
00050     
00051     /*
00052     returns false if there was a print error.
00053     */
00054     
00055     short x;
00056     
00057     x = PrError ();
00058     
00059     if (x == noErr) /*no error, keep going*/
00060         return (true);
00061     
00062     if (x != iPrAbort) 
00063         alertdialog ("\pPrinting error.");
00064     
00065     return (false); /*there was an error*/
00066     } /*checkprinterror*/
00067 
00068 
00069 static void copyprintinfo (void) {
00070     
00071     Rect r;
00072     
00073     r = app.printinfo.paperrect = (**(THPrint) app.printinfo.printhandle).prInfo.rPage;
00074     
00075     app.printinfo.vpagepixels = r.bottom - r.top; 
00076     } /*copyprintinfo*/
00077 
00078 
00079 boolean initprint (void) {
00080     
00081     /*
00082     2/18/92 dmb: set paperrect to a hard-coded default, in case the 
00083     print manager can't be opened.  also, manage flprintinit here, instead of 
00084     having every caller do so.
00085     */
00086     
00087     Handle h;
00088     
00089     if (flprintinit) /*already initted*/
00090         return (true);
00091     
00092     clearbytes (&app.printinfo, longsizeof (app.printinfo));
00093     
00094     SetRect (&app.printinfo.paperrect, 5, 6, 725, 546); /*defaults in case PrOpen fails*/
00095     
00096     if (!newclearhandle (longsizeof (TPrint), &h))
00097         return (false);
00098     
00099     app.printinfo.printhandle = h; /*copy into print record*/
00100     
00101     PrOpen (); /*initialize the Mac print manager*/
00102     
00103     if (!checkprinterror ())
00104         goto error;
00105     
00106     PrintDefault ((THPrint) app.printinfo.printhandle); /*set default print record*/
00107     
00108     PrValidate ((THPrint) app.printinfo.printhandle);
00109     
00110     PrClose (); /*shouldn't leave print resources open all the time*/
00111     
00112     if (!checkprinterror ()) 
00113         goto error;
00114     
00115     copyprintinfo (); /*copies fields from handle into record*/
00116     
00117     flprintinit = true;
00118     
00119     return (true);
00120     
00121     error:
00122     
00123     disposeprintinfo ();
00124     
00125     return (false);
00126     } /*initprint*/
00127     
00128     
00129 boolean getprintinfo (void) {
00130     
00131     /*
00132     after calling this you can safely refer to fields of the 
00133     app.printinfo record.
00134     */
00135     
00136     if (!initprint ()) 
00137         return (false);
00138     
00139     return (true);
00140     } /*getprintinfo*/
00141 
00142 
00143 static boolean pagesetupvisit (hdlappwindow appwindow) {
00144 
00145     setappwindow (appwindow);
00146     
00147     (*app.pagesetupcallback) ();
00148     
00149     return (true);
00150     } /*pagesetupvisit*/
00151 
00152 
00153 boolean pagesetup (void) {
00154 
00155     if (!initprint ()) 
00156         return (false);
00157     
00158     PrOpen ();
00159     
00160     if (!checkprinterror ()) 
00161         return (false);
00162     
00163     PrValidate ((THPrint) app.printinfo.printhandle);
00164     
00165     PrStlDialog ((THPrint) app.printinfo.printhandle);
00166     
00167     PrClose ();
00168     
00169     if (!checkprinterror ())
00170         return (false);
00171     
00172     copyprintinfo (); /*copies fields from handle into record*/
00173     
00174     visitappwindows (&pagesetupvisit);
00175     
00176     return (true);
00177     } /*pagesetup*/
00178 
00179 
00180 boolean printappwindow (hdlappwindow appwindow, boolean fldialog) {
00181     
00182     TPPrPort printport;
00183     TPrStatus printstatus;
00184     short i;
00185     WindowPtr origmacwindow;
00186     boolean fl;
00187     
00188     if (!initprint ()) 
00189         return (false);
00190     
00191     setappwindow (appwindow);
00192     
00193     PrOpen ();
00194     
00195     if (!checkprinterror ())
00196         return (false);
00197     
00198     fl = false; /*until sucessful print, this is return value*/
00199     
00200     if (fldialog) {
00201         
00202         if (!PrJobDialog ((THPrint) app.printinfo.printhandle)) {
00203             
00204             PrClose ();
00205             
00206             return (false);
00207             }
00208             
00209         appserviceeventqueue (); /*update all dirtied windows*/
00210         
00211         setappwindow (appwindow); /*9/3/92 DW: app.appwindow could have changed*/
00212         }
00213     else
00214         PrValidate ((THPrint) app.printinfo.printhandle);
00215     
00216     watchcursor ();
00217     
00218     copyprintinfo ();
00219     
00220     (**app.appwindow).flprinting = true;
00221     
00222     (*app.openprintcallback) (); /*fills in fields of printinfo record*/
00223     
00224     pushmacport (nil); /*save current port on stack*/
00225     
00226     printport = PrOpenDoc ((THPrint) app.printinfo.printhandle, nil, nil);
00227     
00228     for (i = 1; i <= app.printinfo.ctpages; i++) { /*print one page*/
00229         
00230         if (!appserviceeventqueue ()) /*user must have selected quit or something like that*/
00231             PrSetError (iPrAbort);
00232             
00233         if (PrError () != noErr)
00234             break;
00235         
00236         PrOpenPage (printport, nil);
00237         
00238         if (PrError () == noErr) {
00239             
00240             origmacwindow = (**app.appwindow).macwindow;
00241             
00242             printcomputewindowinfo ((WindowPtr) printport, app.appwindow);
00243             
00244             SetFractEnable (true);
00245             
00246             fl = (*app.printpagecallback) (i);
00247             
00248             SetFractEnable (false);
00249             
00250             computewindowinfo (origmacwindow, app.appwindow);
00251             }
00252         
00253         PrClosePage (printport);
00254         
00255         if (!fl)
00256             break;
00257         } /*for*/
00258     
00259     PrCloseDoc (printport);
00260     
00261     if (fl) {
00262         
00263         THPrint hp = (THPrint) app.printinfo.printhandle;
00264         
00265         if ((PrError () == noErr) && ((**hp).prJob.bJDocLoop == bSpoolLoop))
00266             PrPicFile (hp, nil, nil, nil, &printstatus);
00267         
00268         fl = checkprinterror ();
00269         }
00270     
00271     popmacport ();
00272     
00273     (*app.closeprintcallback) ();
00274     
00275     (**app.appwindow).flprinting = false;
00276     
00277     PrClose ();
00278     
00279     return (fl);
00280     } /*printappwindow*/
00281 
00282 

Generated on Wed May 31 18:19:43 2006 for frontierkernel 10.1.10a by  doxygen 1.4.6