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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "printing/printing_context_mac.h"
#import <ApplicationServices/ApplicationServices.h>
#import <AppKit/AppKit.h>
#include "base/logging.h"
#include "base/mac/scoped_cftyperef.h"
#include "base/sys_string_conversions.h"
#include "base/values.h"
#include "printing/print_settings_initializer_mac.h"
static const CFStringRef kColorModel = CFSTR("ColorModel");
static const CFStringRef kGrayColor = CFSTR("Gray");
namespace printing {
// static
PrintingContext* PrintingContext::Create(const std::string& app_locale) {
return static_cast<PrintingContext*>(new PrintingContextMac(app_locale));
}
PrintingContextMac::PrintingContextMac(const std::string& app_locale)
: PrintingContext(app_locale),
context_(NULL) {
}
PrintingContextMac::~PrintingContextMac() {
ReleaseContext();
}
void PrintingContextMac::AskUserForSettings(gfx::NativeView parent_view,
int max_pages,
bool has_selection,
PrintSettingsCallback* callback) {
DCHECK([NSThread isMainThread]);
// We deliberately don't feed max_pages into the dialog, because setting
// NSPrintLastPage makes the print dialog pre-select the option to only print
// a range.
// TODO(stuartmorgan): implement 'print selection only' (probably requires
// adding a new custom view to the panel on 10.5; 10.6 has
// NSPrintPanelShowsPrintSelection).
NSPrintPanel* panel = [NSPrintPanel printPanel];
NSPrintInfo* printInfo = [NSPrintInfo sharedPrintInfo];
NSPrintPanelOptions options = [panel options];
options |= NSPrintPanelShowsPaperSize;
options |= NSPrintPanelShowsOrientation;
options |= NSPrintPanelShowsScaling;
[panel setOptions:options];
// Set the print job title text.
if (parent_view) {
NSString* job_title = [[parent_view window] title];
if (job_title) {
PMPrintSettings printSettings =
(PMPrintSettings)[printInfo PMPrintSettings];
PMPrintSettingsSetJobName(printSettings, (CFStringRef)job_title);
[printInfo updateFromPMPrintSettings];
}
}
// TODO(stuartmorgan): We really want a tab sheet here, not a modal window.
// Will require restructuring the PrintingContext API to use a callback.
NSInteger selection = [panel runModalWithPrintInfo:printInfo];
if (selection == NSOKButton) {
ParsePrintInfo([panel printInfo]);
callback->Run(OK);
} else {
callback->Run(CANCEL);
}
}
PrintingContext::Result PrintingContextMac::UseDefaultSettings() {
DCHECK(!in_print_job_);
ParsePrintInfo([NSPrintInfo sharedPrintInfo]);
return OK;
}
PrintingContext::Result PrintingContextMac::UpdatePrintSettings(
const DictionaryValue& job_settings, const PageRanges& ranges) {
DCHECK(!in_print_job_);
ResetSettings();
print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]);
bool landscape;
std::string printer_name;
int copies;
bool collate;
int duplex_mode;
bool color;
if (!job_settings.GetBoolean(kSettingLandscape, &landscape) ||
!job_settings.GetString(kSettingPrinterName, &printer_name) ||
!job_settings.GetInteger(kSettingCopies, &copies) ||
!job_settings.GetBoolean(kSettingCollate, &collate) ||
!job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) ||
!job_settings.GetBoolean(kSettingColor, &color)) {
return OnError();
}
if (!SetPrinter(printer_name))
return OnError();
if (!SetCopiesInPrintSettings(copies))
return OnError();
if (!SetCollateInPrintSettings(collate))
return OnError();
if (!SetOrientationIsLandscape(landscape))
return OnError();
if (!SetDuplexModeInPrintSettings(static_cast<DuplexMode>(duplex_mode)))
return OnError();
if (!SetOutputIsColor(color))
return OnError();
[print_info_.get() updateFromPMPrintSettings];
InitPrintSettingsFromPrintInfo(ranges);
return OK;
}
void PrintingContextMac::InitPrintSettingsFromPrintInfo(
const PageRanges& ranges) {
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
PMPageFormat page_format =
static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
PMPrinter printer;
PMSessionGetCurrentPrinter(print_session, &printer);
PrintSettingsInitializerMac::InitPrintSettings(
printer, page_format, ranges, false, &settings_);
}
bool PrintingContextMac::SetPrinter(const std::string& printer_name) {
NSString* new_printer_name = base::SysUTF8ToNSString(printer_name);
if (!new_printer_name)
return false;
if (![[[print_info_.get() printer] name] isEqualToString:new_printer_name]) {
NSPrinter* new_printer = [NSPrinter printerWithName:new_printer_name];
if (new_printer == nil)
return false;
[print_info_.get() setPrinter:new_printer];
}
return true;
}
bool PrintingContextMac::SetCopiesInPrintSettings(int copies) {
if (copies < 1)
return false;
PMPrintSettings pmPrintSettings =
static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
return PMSetCopies(pmPrintSettings, copies, false) == noErr;
}
bool PrintingContextMac::SetCollateInPrintSettings(bool collate) {
PMPrintSettings pmPrintSettings =
static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
return PMSetCollate(pmPrintSettings, collate) == noErr;
}
bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) {
PMPageFormat page_format =
static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
PMOrientation orientation = landscape ? kPMLandscape : kPMPortrait;
if (PMSetOrientation(page_format, orientation, false) != noErr)
return false;
[print_info_.get() updateFromPMPageFormat];
return true;
}
bool PrintingContextMac::SetDuplexModeInPrintSettings(DuplexMode mode) {
PMDuplexMode duplexSetting;
switch (mode) {
case LONG_EDGE:
duplexSetting = kPMDuplexNoTumble;
break;
case SHORT_EDGE:
duplexSetting = kPMDuplexTumble;
break;
default:
duplexSetting = kPMDuplexNone;
break;
}
PMPrintSettings pmPrintSettings =
static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
return PMSetDuplex(pmPrintSettings, duplexSetting) == noErr;
}
bool PrintingContextMac::SetOutputIsColor(bool color) {
PMPrintSettings pmPrintSettings =
static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
CFStringRef output_color = color ? NULL : kGrayColor;
return PMPrintSettingsSetValue(pmPrintSettings,
kColorModel,
output_color,
false) == noErr;
}
void PrintingContextMac::ParsePrintInfo(NSPrintInfo* print_info) {
ResetSettings();
print_info_.reset([print_info retain]);
PageRanges page_ranges;
NSDictionary* print_info_dict = [print_info_.get() dictionary];
if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) {
PageRange range;
range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1;
range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1;
page_ranges.push_back(range);
}
InitPrintSettingsFromPrintInfo(page_ranges);
}
PrintingContext::Result PrintingContextMac::InitWithSettings(
const PrintSettings& settings) {
DCHECK(!in_print_job_);
settings_ = settings;
NOTIMPLEMENTED();
return FAILED;
}
PrintingContext::Result PrintingContextMac::NewDocument(
const string16& document_name) {
DCHECK(!in_print_job_);
in_print_job_ = true;
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
PMPrintSettings print_settings =
static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
PMPageFormat page_format =
static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
base::mac::ScopedCFTypeRef<CFStringRef> job_title(
base::SysUTF16ToCFStringRef(document_name));
PMPrintSettingsSetJobName(print_settings, job_title.get());
OSStatus status = PMSessionBeginCGDocumentNoDialog(print_session,
print_settings,
page_format);
if (status != noErr)
return OnError();
return OK;
}
PrintingContext::Result PrintingContextMac::NewPage() {
if (abort_printing_)
return CANCEL;
DCHECK(in_print_job_);
DCHECK(!context_);
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
PMPageFormat page_format =
static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
OSStatus status;
status = PMSessionBeginPageNoDialog(print_session, page_format, NULL);
if (status != noErr)
return OnError();
status = PMSessionGetCGGraphicsContext(print_session, &context_);
if (status != noErr)
return OnError();
return OK;
}
PrintingContext::Result PrintingContextMac::PageDone() {
if (abort_printing_)
return CANCEL;
DCHECK(in_print_job_);
DCHECK(context_);
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
OSStatus status = PMSessionEndPageNoDialog(print_session);
if (status != noErr)
OnError();
context_ = NULL;
return OK;
}
PrintingContext::Result PrintingContextMac::DocumentDone() {
if (abort_printing_)
return CANCEL;
DCHECK(in_print_job_);
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
OSStatus status = PMSessionEndDocumentNoDialog(print_session);
if (status != noErr)
OnError();
ResetSettings();
return OK;
}
void PrintingContextMac::Cancel() {
abort_printing_ = true;
in_print_job_ = false;
context_ = NULL;
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
PMSessionEndPageNoDialog(print_session);
}
void PrintingContextMac::ReleaseContext() {
print_info_.reset();
context_ = NULL;
}
gfx::NativeDrawingContext PrintingContextMac::context() const {
return context_;
}
} // namespace printing
|