source: Sophya/trunk/SophyaPI/PI/Quartz/Test/Quartz2DBasics/Quartz2DBasics.carbon/DoPrinting.c

Last change on this file was 4084, checked in by garnier, 13 years ago

projet Quartz qui marche, origine : https://developer.apple.com/library/mac/#samplecode/Quartz2DBasics/Listings/CommonCode_AppDrawing_c.html#//apple_ref/doc/uid/DTS10003975-CommonCode_AppDrawing_c-DontLinkElementID_3

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