|
| 1 | +/* |
| 2 | +* File: DoPrinting.c |
| 3 | +* |
| 4 | +* Copyright: Copyright © 2005 Apple Computer, Inc., All Rights Reserved |
| 5 | +* |
| 6 | +* Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in |
| 7 | +* consideration of your agreement to the following terms, and your use, installation, modification |
| 8 | +* or redistribution of this Apple software constitutes acceptance of these terms. If you do |
| 9 | +* not agree with these terms, please do not use, install, modify or redistribute this Apple |
| 10 | +* software. |
| 11 | +* |
| 12 | +* In consideration of your agreement to abide by the following terms, and subject to these terms, |
| 13 | +* Apple grants you a personal, non-exclusive license, under Apple's copyrights in this |
| 14 | +* original Apple software (the "Apple Software"), to use, reproduce, modify and redistribute the |
| 15 | +* Apple Software, with or without modifications, in source and/or binary forms; provided that if you |
| 16 | +* redistribute the Apple Software in its entirety and without modifications, you must retain this |
| 17 | +* notice and the following text and disclaimers in all such redistributions of the Apple Software. |
| 18 | +* Neither the name, trademarks, service marks or logos of Apple Computer, Inc. may be used to |
| 19 | +* endorse or promote products derived from the Apple Software without specific prior written |
| 20 | +* permission from Apple. Except as expressly stated in this notice, no other rights or |
| 21 | +* licenses, express or implied, are granted by Apple herein, including but not limited to any |
| 22 | +* patent rights that may be infringed by your derivative works or by other works in which the |
| 23 | +* Apple Software may be incorporated. |
| 24 | +* |
| 25 | +* The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR |
| 26 | +* IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY |
| 27 | +* AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE |
| 28 | +* OR IN COMBINATION WITH YOUR PRODUCTS. |
| 29 | +* |
| 30 | +* IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL |
| 31 | +* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 32 | +* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, |
| 33 | +* REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER |
| 34 | +* UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN |
| 35 | +* IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 36 | +* |
| 37 | +*/ |
| 38 | + |
| 39 | +#include "DoPrinting.h" |
| 40 | +#include "AppDrawing.h" |
| 41 | +#include "UIHandling.h" |
| 42 | + |
| 43 | +// This code only prints one page. |
| 44 | +#define NUMDOCPAGES 1 |
| 45 | + |
| 46 | +static OSStatus MyDoPrintLoop(PMPrintSession printSession, PMPageFormat pageFormat, |
| 47 | + PMPrintSettings printSettings, OSType command); |
| 48 | + |
| 49 | +// ----------------------------------------------------------------------- |
| 50 | +PMPageFormat CreateDefaultPageFormat(void) |
| 51 | +{ |
| 52 | + OSStatus err = noErr, tempErr; |
| 53 | + PMPageFormat pageFormat = NULL; |
| 54 | + PMPrintSession printSession; |
| 55 | + err = PMCreateSession(&printSession); |
| 56 | + if(!err){ |
| 57 | + err = PMCreatePageFormat(&pageFormat); |
| 58 | + if(err == noErr) |
| 59 | + err = PMSessionDefaultPageFormat(printSession, pageFormat); |
| 60 | + |
| 61 | + tempErr = PMRelease(printSession); |
| 62 | + if(!err)err = tempErr; |
| 63 | + } |
| 64 | + if(err){ |
| 65 | + fprintf(stderr, "Got an error = %d creating the default page format\n", err); |
| 66 | + } |
| 67 | + return pageFormat; |
| 68 | +} |
| 69 | + |
| 70 | +// ----------------------------------------------------------------- |
| 71 | +OSStatus DoPageSetup(PMPageFormat pageFormat) |
| 72 | +{ |
| 73 | + OSStatus err = noErr; |
| 74 | + PMPrintSession printSession; |
| 75 | + err = PMCreateSession(&printSession); |
| 76 | + if(!err){ |
| 77 | + Boolean accepted; |
| 78 | + if(!err) // Validate the page format we're going to pass to the dialog code. |
| 79 | + err = PMSessionValidatePageFormat(printSession, pageFormat, kPMDontWantBoolean); |
| 80 | + |
| 81 | + if(!err){ |
| 82 | + err = PMSessionPageSetupDialog(printSession, pageFormat, &accepted); |
| 83 | + } |
| 84 | + |
| 85 | + (void)PMRelease(printSession); |
| 86 | + } |
| 87 | + |
| 88 | + if(err && err != kPMCancel) |
| 89 | + fprintf(stderr, "Got an error %d in Page Setup\n", err); |
| 90 | + |
| 91 | + return err; |
| 92 | +} // DoPageSetup |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | +// ------------------------------------------------------------------------------- |
| 97 | +OSStatus DoPrint(PMPageFormat pageFormat, OSType drawingCommand) |
| 98 | +{ |
| 99 | + OSStatus err = noErr; |
| 100 | + UInt32 minPage = 1, maxPage = NUMDOCPAGES; // One page document printing. |
| 101 | + PMPrintSession printSession; |
| 102 | + |
| 103 | + err = PMCreateSession(&printSession); |
| 104 | + if(err == noErr){ |
| 105 | + err = PMSessionValidatePageFormat(printSession, pageFormat, kPMDontWantBoolean); |
| 106 | + if (err == noErr) |
| 107 | + { |
| 108 | + PMPrintSettings printSettings = NULL; |
| 109 | + err = PMCreatePrintSettings(&printSettings); |
| 110 | + if(err == noErr) |
| 111 | + err = PMSessionDefaultPrintSettings(printSession, printSettings); |
| 112 | + |
| 113 | + if (err == noErr) |
| 114 | + err = PMSetPageRange(printSettings, minPage, maxPage); |
| 115 | + |
| 116 | + if (err == noErr) |
| 117 | + { |
| 118 | + Boolean accepted; |
| 119 | + err = PMSessionPrintDialog(printSession, printSettings, pageFormat, &accepted); |
| 120 | + |
| 121 | + if(!err && accepted){ |
| 122 | + err = MyDoPrintLoop(printSession, pageFormat, printSettings, drawingCommand); |
| 123 | + } |
| 124 | + } |
| 125 | + if(printSettings) |
| 126 | + (void)PMRelease(printSettings); |
| 127 | + } |
| 128 | + |
| 129 | + (void)PMRelease(printSession); |
| 130 | + } |
| 131 | + |
| 132 | + if(err && err != kPMCancel) |
| 133 | + fprintf(stderr, "Got an error %d in DoPrint!\n", err); |
| 134 | + |
| 135 | + return err; |
| 136 | +} |
| 137 | + |
| 138 | +static OSStatus MyPMSessionBeginCGDocument(PMPrintSession printSession, |
| 139 | + PMPrintSettings printSettings, PMPageFormat pageFormat) |
| 140 | +{ |
| 141 | + OSStatus err = noErr; |
| 142 | + |
| 143 | + // Tell the printing system we want to print by drawing to a CGContext, not a QD port. |
| 144 | + |
| 145 | + // Use the simpler call if it is present. |
| 146 | + if(&PMSessionBeginCGDocument != NULL){ |
| 147 | + err = PMSessionBeginCGDocument(printSession, printSettings, pageFormat); |
| 148 | + }else{ |
| 149 | + CFStringRef s[1] = { kPMGraphicsContextCoreGraphics }; |
| 150 | + CFArrayRef graphicsContextsArray = CFArrayCreate(NULL, (const void**)s, 1, &kCFTypeArrayCallBacks); |
| 151 | + if(graphicsContextsArray){ |
| 152 | + err = PMSessionSetDocumentFormatGeneration(printSession, kPMDocumentFormatPDF, graphicsContextsArray, NULL); |
| 153 | + CFRelease(graphicsContextsArray); |
| 154 | + if(!err) |
| 155 | + err = PMSessionBeginDocument(printSession, printSettings, pageFormat); |
| 156 | + }else{ |
| 157 | + err = memFullErr; |
| 158 | + } |
| 159 | + |
| 160 | + } |
| 161 | + return err; |
| 162 | +} |
| 163 | + |
| 164 | +static OSStatus MyPMSessionGetCGGraphicsContext(PMPrintSession printSession, CGContextRef *printingContextP) |
| 165 | +{ |
| 166 | + OSStatus err = noErr; |
| 167 | + // Use the simpler call if it is present. |
| 168 | + if(&PMSessionGetCGGraphicsContext != NULL){ |
| 169 | + err = PMSessionGetCGGraphicsContext(printSession, printingContextP); |
| 170 | + }else{ |
| 171 | + err = PMSessionGetGraphicsContext(printSession, kPMGraphicsContextCoreGraphics, (void**)printingContextP); |
| 172 | + } |
| 173 | + return err; |
| 174 | +} |
| 175 | + |
| 176 | +// -------------------------------------------------------------------------------------- |
| 177 | +static OSStatus MyDoPrintLoop(PMPrintSession printSession, PMPageFormat pageFormat, |
| 178 | + PMPrintSettings printSettings, OSType drawingType) |
| 179 | +{ |
| 180 | + OSStatus err = noErr; |
| 181 | + OSStatus tempErr = noErr; |
| 182 | + UInt32 firstPage, lastPage, totalDocPages = NUMDOCPAGES; // Only print 1 page. |
| 183 | + |
| 184 | + if(!err) |
| 185 | + err = PMGetFirstPage(printSettings, &firstPage); |
| 186 | + |
| 187 | + if (!err) |
| 188 | + err = PMGetLastPage(printSettings, &lastPage); |
| 189 | + |
| 190 | + if(!err && lastPage > totalDocPages){ |
| 191 | + // Don't draw more than the number of pages in our document. |
| 192 | + lastPage = totalDocPages; |
| 193 | + } |
| 194 | + |
| 195 | + // Tell the printing system the number of pages we are going to print. |
| 196 | + if (!err) |
| 197 | + err = PMSetLastPage(printSettings, lastPage, false); |
| 198 | + |
| 199 | + if (!err) |
| 200 | + { |
| 201 | + err = MyPMSessionBeginCGDocument(printSession, printSettings, pageFormat); |
| 202 | + if (!err){ |
| 203 | + UInt32 pageNumber = firstPage; |
| 204 | + // Need to check errors from our print loop and errors from the session each |
| 205 | + // time around our print loop before calling our BeginPageProc. |
| 206 | + while(pageNumber <= lastPage && err == noErr && PMSessionError(printSession) == noErr) |
| 207 | + { |
| 208 | + err = PMSessionBeginPage(printSession, pageFormat, NULL); |
| 209 | + if (!err){ |
| 210 | + CGContextRef printingContext = NULL; |
| 211 | + // Get the printing CGContext to draw to. |
| 212 | + err = MyPMSessionGetCGGraphicsContext(printSession, &printingContext); |
| 213 | + if(!err){ |
| 214 | + DispatchDrawing(printingContext, drawingType); |
| 215 | + } |
| 216 | + // We must call EndPage if BeginPage returned noErr. |
| 217 | + tempErr = PMSessionEndPage(printSession); |
| 218 | + |
| 219 | + if(!err)err = tempErr; |
| 220 | + } |
| 221 | + pageNumber++; |
| 222 | + } // End of while loop. |
| 223 | + |
| 224 | + // We must call EndDocument if BeginDocument returned noErr. |
| 225 | + tempErr = PMSessionEndDocument(printSession); |
| 226 | + |
| 227 | + if(!err)err = tempErr; |
| 228 | + |
| 229 | + if(!err) |
| 230 | + err = PMSessionError(printSession); |
| 231 | + } |
| 232 | + } |
| 233 | + return err; |
| 234 | +} |
| 235 | + |
| 236 | + |
0 commit comments