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
|
// Copyright (c) 2009 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.h"
#import <ApplicationServices/ApplicationServices.h>
#import <AppKit/AppKit.h>
#include "base/logging.h"
namespace printing {
PrintingContext::PrintingContext()
: context_(NULL),
print_info_(nil),
#ifndef NDEBUG
page_number_(-1),
#endif
dialog_box_dismissed_(false),
in_print_job_(false),
abort_printing_(false) {
}
PrintingContext::~PrintingContext() {
ResetSettings();
}
PrintingContext::Result PrintingContext::AskUserForSettings(
gfx::NativeWindow window, int max_pages, bool has_selection) {
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];
// 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:[NSPrintInfo sharedPrintInfo]];
if (selection != NSOKButton) {
return CANCEL;
}
ParsePrintInfo([panel printInfo]);
return OK;
}
PrintingContext::Result PrintingContext::UseDefaultSettings() {
DCHECK(!in_print_job_);
ParsePrintInfo([NSPrintInfo sharedPrintInfo]);
return OK;
}
void PrintingContext::ParsePrintInfo(NSPrintInfo* print_info) {
ResetSettings();
print_info_ = [print_info retain];
PageRanges page_ranges;
NSDictionary* print_info_dict = [print_info_ 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);
}
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_ PMPrintSession]);
PMPageFormat page_format =
static_cast<PMPageFormat>([print_info_ PMPageFormat]);
PMPrinter printer;
PMSessionGetCurrentPrinter(print_session, &printer);
settings_.Init(printer, page_format, page_ranges, false);
}
PrintingContext::Result PrintingContext::InitWithSettings(
const PrintSettings& settings) {
DCHECK(!in_print_job_);
settings_ = settings;
NOTIMPLEMENTED();
return FAILED;
}
void PrintingContext::ResetSettings() {
[print_info_ autorelease];
print_info_ = nil;
settings_.Clear();
#ifndef NDEBUG
page_number_ = -1;
#endif
dialog_box_dismissed_ = false;
abort_printing_ = false;
in_print_job_ = false;
context_ = NULL;
}
PrintingContext::Result PrintingContext::NewDocument(
const std::wstring& document_name) {
DCHECK(!in_print_job_);
in_print_job_ = true;
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_ PMPrintSession]);
PMPrintSettings print_settings =
static_cast<PMPrintSettings>([print_info_ PMPrintSettings]);
PMPageFormat page_format =
static_cast<PMPageFormat>([print_info_ PMPageFormat]);
OSStatus status = PMSessionBeginCGDocumentNoDialog(print_session,
print_settings,
page_format);
if (status != noErr)
return OnError();
#ifndef NDEBUG
page_number_ = 0;
#endif
return OK;
}
PrintingContext::Result PrintingContext::NewPage() {
if (abort_printing_)
return CANCEL;
DCHECK(in_print_job_);
DCHECK(!context_);
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_ PMPrintSession]);
PMPageFormat page_format =
static_cast<PMPageFormat>([print_info_ 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();
#ifndef NDEBUG
++page_number_;
#endif
return OK;
}
PrintingContext::Result PrintingContext::PageDone() {
if (abort_printing_)
return CANCEL;
DCHECK(in_print_job_);
DCHECK(context_);
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_ PMPrintSession]);
OSStatus status = PMSessionEndPageNoDialog(print_session);
if (status != noErr)
OnError();
context_ = NULL;
return OK;
}
PrintingContext::Result PrintingContext::DocumentDone() {
if (abort_printing_)
return CANCEL;
DCHECK(in_print_job_);
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_ PMPrintSession]);
OSStatus status = PMSessionEndDocumentNoDialog(print_session);
if (status != noErr)
OnError();
ResetSettings();
return OK;
}
void PrintingContext::Cancel() {
abort_printing_ = true;
in_print_job_ = false;
context_ = NULL;
PMPrintSession print_session =
static_cast<PMPrintSession>([print_info_ PMPrintSession]);
PMSessionEndPageNoDialog(print_session);
}
void PrintingContext::DismissDialog() {
NOTIMPLEMENTED();
}
PrintingContext::Result PrintingContext::OnError() {
ResetSettings();
return abort_printing_ ? CANCEL : FAILED;
}
} // namespace printing
|