1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
//
// "$Id$"
//
// Mac OS X-specific printing support (objective-c++) for the Fast Light Tool Kit (FLTK).
//
// Copyright 2010 by Bill Spitzak and others.
//
// This library is free software. Distribution and use rights are outlined in
// the file "COPYING" which should have been included with this file. If this
// file is missing or damaged, see the license at:
//
// http://www.fltk.org/COPYING.php
//
// Please report all bugs and problems to:
//
// http://www.fltk.org/str.php
//
#ifdef __APPLE__
#include <FL/Fl_Printer.H>
#include <FL/Fl.H>
#include <FL/fl_ask.H>
#include <FL/fl_draw.H>
#import <Cocoa/Cocoa.h>
extern void fl_quartz_restore_line_style_();
Fl_System_Printer::Fl_System_Printer(void)
{
x_offset = 0;
y_offset = 0;
scale_x = scale_y = 1.;
gc = 0;
driver(Fl_Display_Device::display_device()->driver());
}
Fl_System_Printer::~Fl_System_Printer(void) {}
int Fl_System_Printer::start_job (int pagecount, int *frompage, int *topage)
//printing using a Quartz graphics context
//returns 0 iff OK
{
OSStatus status = 0;
Fl_X::q_release_context();
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
if( [NSPrintPanel instancesRespondToSelector:@selector(runModalWithPrintInfo:)] &&
[NSPrintInfo instancesRespondToSelector:@selector(PMPrintSession)] ) {
NSAutoreleasePool *localPool;
localPool = [[NSAutoreleasePool alloc] init];
NSPrintInfo *info = [NSPrintInfo sharedPrintInfo];
NSPageLayout *layout = [NSPageLayout pageLayout];
NSInteger retval = [layout runModal];
if(retval == NSOKButton) {
NSPrintPanel *panel = [NSPrintPanel printPanel];
retval = (NSInteger)[panel runModalWithPrintInfo:info];//from 10.5 only
}
if(retval != NSOKButton) {
Fl::first_window()->show();
[localPool release];
return 1;
}
printSession = (PMPrintSession)[info PMPrintSession];
pageFormat = (PMPageFormat)[info PMPageFormat];
printSettings = (PMPrintSettings)[info PMPrintSettings];
UInt32 from32, to32;
PMGetFirstPage(printSettings, &from32);
if (frompage) *frompage = (int)from32;
PMGetLastPage(printSettings, &to32);
if (topage) *topage = (int)to32;
if(topage && *topage > pagecount) *topage = pagecount;
status = PMSessionBeginCGDocumentNoDialog(printSession, printSettings, pageFormat);
[localPool release];
}
else {
#endif
#if !__LP64__
Boolean accepted;
status = PMCreateSession(&printSession);
if (status != noErr) return 1;
status = PMCreatePageFormat(&pageFormat);
status = PMSessionDefaultPageFormat(printSession, pageFormat);
if (status != noErr) return 1;
// get pointer to the PMSessionPageSetupDialog Carbon function
typedef OSStatus (*dialog_f)(PMPrintSession, PMPageFormat, Boolean *);
static dialog_f f = NULL;
if (!f) f = (dialog_f)Fl_X::get_carbon_function("PMSessionPageSetupDialog");
status = (*f)(printSession, pageFormat, &accepted);
if (status != noErr || !accepted) {
Fl::first_window()->show();
return 1;
}
status = PMCreatePrintSettings(&printSettings);
if (status != noErr || printSettings == kPMNoPrintSettings) return 1;
status = PMSessionDefaultPrintSettings (printSession, printSettings);
if (status != noErr) return 1;
PMSetPageRange(printSettings, 1, (UInt32)kPMPrintAllPages);
// get pointer to the PMSessionPrintDialog Carbon function
typedef OSStatus (*dialog_f2)(PMPrintSession, PMPrintSettings, PMPageFormat, Boolean *);
static dialog_f2 f2 = NULL;
if (!f2) f2 = (dialog_f2)Fl_X::get_carbon_function("PMSessionPrintDialog");
status = (*f2)(printSession, printSettings, pageFormat, &accepted);
if (!accepted) status = kPMCancel;
if (status != noErr) {
Fl::first_window()->show();
return 1;
}
UInt32 from32, to32;
PMGetFirstPage(printSettings, &from32);
if (frompage) *frompage = (int)from32;
PMGetLastPage(printSettings, &to32);
if (topage) *topage = (int)to32;
if(topage && *topage > pagecount) *topage = pagecount;
CFStringRef mystring[1];
mystring[0] = kPMGraphicsContextCoreGraphics;
CFArrayRef array = CFArrayCreate(NULL, (const void **)mystring, 1, &kCFTypeArrayCallBacks);
status = PMSessionSetDocumentFormatGeneration(printSession, kPMDocumentFormatDefault, array, NULL);
CFRelease(array);
status = PMSessionBeginDocumentNoDialog(printSession, printSettings, pageFormat);
#endif //__LP64__
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
}
#endif
if (status != noErr) return 1;
y_offset = x_offset = 0;
this->set_current();
return 0;
}
void Fl_System_Printer::margins(int *left, int *top, int *right, int *bottom)
{
PMPaper paper;
PMGetPageFormatPaper(pageFormat, &paper);
PMOrientation orientation;
PMGetOrientation(pageFormat, &orientation);
PMPaperMargins margins;
PMPaperGetMargins(paper, &margins);
if(orientation == kPMPortrait) {
if (left) *left = (int)(margins.left / scale_x + 0.5);
if (top) *top = (int)(margins.top / scale_y + 0.5);
if (right) *right = (int)(margins.right / scale_x + 0.5);
if (bottom) *bottom = (int)(margins.bottom / scale_y + 0.5);
}
else {
if (left) *left = (int)(margins.top / scale_x + 0.5);
if (top) *top = (int)(margins.left / scale_y + 0.5);
if (right) *right = (int)(margins.bottom / scale_x + 0.5);
if (bottom) *bottom = (int)(margins.right / scale_y + 0.5);
}
}
int Fl_System_Printer::printable_rect(int *w, int *h)
//returns 0 iff OK
{
OSStatus status;
PMRect pmRect;
int x, y;
status = PMGetAdjustedPageRect(pageFormat, &pmRect);
if (status != noErr) return 1;
x = (int)pmRect.left;
y = (int)pmRect.top;
*w = int((int)(pmRect.right - x) / scale_x + 1);
*h = int((int)(pmRect.bottom - y) / scale_y + 1);
return 0;
}
void Fl_System_Printer::origin(int x, int y)
{
x_offset = x;
y_offset = y;
CGContextRestoreGState(fl_gc);
CGContextRestoreGState(fl_gc);
CGContextSaveGState(fl_gc);
CGContextScaleCTM(fl_gc, scale_x, scale_y);
CGContextTranslateCTM(fl_gc, x, y);
CGContextRotateCTM(fl_gc, angle);
CGContextSaveGState(fl_gc);
}
void Fl_System_Printer::scale (float s_x, float s_y)
{
if (s_y == 0.) s_y = s_x;
scale_x = s_x;
scale_y = s_y;
CGContextRestoreGState(fl_gc);
CGContextRestoreGState(fl_gc);
CGContextSaveGState(fl_gc);
CGContextScaleCTM(fl_gc, scale_x, scale_y);
CGContextRotateCTM(fl_gc, angle);
x_offset = y_offset = 0;
CGContextSaveGState(fl_gc);
}
void Fl_System_Printer::rotate (float rot_angle)
{
angle = - rot_angle * M_PI / 180.;
CGContextRestoreGState(fl_gc);
CGContextRestoreGState(fl_gc);
CGContextSaveGState(fl_gc);
CGContextScaleCTM(fl_gc, scale_x, scale_y);
CGContextTranslateCTM(fl_gc, x_offset, y_offset);
CGContextRotateCTM(fl_gc, angle);
CGContextSaveGState(fl_gc);
}
void Fl_System_Printer::translate(int x, int y)
{
CGContextSaveGState(fl_gc);
CGContextTranslateCTM(fl_gc, x, y );
CGContextSaveGState(fl_gc);
}
void Fl_System_Printer::untranslate(void)
{
CGContextRestoreGState(fl_gc);
CGContextRestoreGState(fl_gc);
}
int Fl_System_Printer::start_page (void)
{
OSStatus status = PMSessionBeginPageNoDialog(printSession, pageFormat, NULL);
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
if ( PMSessionGetCGGraphicsContext != NULL ) {
status = PMSessionGetCGGraphicsContext(printSession, &fl_gc);
}
else {
#endif
#if ! __LP64__
status = PMSessionGetGraphicsContext(printSession,NULL,(void **)&fl_gc);
#endif
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
}
#endif
PMRect pmRect;
float win_scale_x, win_scale_y;
PMPaper paper;
PMGetPageFormatPaper(pageFormat, &paper);
PMPaperMargins margins;
PMPaperGetMargins(paper, &margins);
PMOrientation orientation;
PMGetOrientation(pageFormat, &orientation);
status = PMGetAdjustedPageRect(pageFormat, &pmRect);
double h = pmRect.bottom - pmRect.top;
x_offset = 0;
y_offset = 0;
angle = 0;
scale_x = scale_y = 1;
win_scale_x = win_scale_y = 1;
if(orientation == kPMPortrait)
CGContextTranslateCTM(fl_gc, margins.left, margins.bottom + h);
else
CGContextTranslateCTM(fl_gc, margins.top, margins.right + h);
CGContextScaleCTM(fl_gc, win_scale_x, - win_scale_y);
fl_quartz_restore_line_style_();
CGContextSetShouldAntialias(fl_gc, false);
CGContextSaveGState(fl_gc);
CGContextSaveGState(fl_gc);
fl_line_style(FL_SOLID);
fl_window = (void *)1; // TODO: something better
fl_clip_region(0);
if( status == noErr) gc = fl_gc;
return status != noErr;
}
int Fl_System_Printer::end_page (void)
{
CGContextFlush(fl_gc);
CGContextRestoreGState(fl_gc);
CGContextRestoreGState(fl_gc);
OSStatus status = PMSessionEndPageNoDialog(printSession);
gc = NULL;
return status != noErr;
}
void Fl_System_Printer::end_job (void)
{
OSStatus status;
status = PMSessionError(printSession);
if (status != noErr) {
fl_alert ("PM Session error %d", (int)status);
}
PMSessionEndDocumentNoDialog(printSession);
Fl_Display_Device::display_device()->set_current();
fl_gc = 0;
Fl::first_window()->show();
}
#endif // __APPLE__
//
// End of "$Id$".
//
|