Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(99)

Side by Side Diff: printing/printing_context_mac.mm

Issue 268036: Implement the basic OS-level printing mechanics on Mac (Closed)
Patch Set: Address review comments Created 11 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "printing/printing_context.h"
6
7 #import <ApplicationServices/ApplicationServices.h>
8 #import <AppKit/AppKit.h>
9
10 #include "base/logging.h"
11
12 namespace printing {
13
14 PrintingContext::PrintingContext()
15 : context_(NULL),
16 print_info_(nil),
17 #ifndef NDEBUG
18 page_number_(-1),
19 #endif
20 dialog_box_dismissed_(false),
21 in_print_job_(false),
22 abort_printing_(false) {
23 }
24
25 PrintingContext::~PrintingContext() {
26 ResetSettings();
27 }
28
29
30 PrintingContext::Result PrintingContext::AskUserForSettings(
31 gfx::NativeWindow window, int max_pages, bool has_selection) {
32 DCHECK([NSThread isMainThread]);
33
34 // We deliberately don't feed max_pages into the dialog, because setting
35 // NSPrintLastPage makes the print dialog pre-select the option to only print
36 // a range.
37
38 // TODO(stuartmorgan): implement 'print selection only' (probably requires
39 // adding a new custom view to the panel on 10.5; 10.6 has
40 // NSPrintPanelShowsPrintSelection).
41 NSPrintPanel* panel = [NSPrintPanel printPanel];
42 // TODO(stuartmorgan): We really want a tab sheet here, not a modal window.
43 // Will require restructuring the PrintingContext API to use a callback.
44 NSInteger selection =
45 [panel runModalWithPrintInfo:[NSPrintInfo sharedPrintInfo]];
46 if (selection != NSOKButton) {
47 return CANCEL;
48 }
49
50 ParsePrintInfo([panel printInfo]);
51 return OK;
52 }
53
54 PrintingContext::Result PrintingContext::UseDefaultSettings() {
55 DCHECK(!in_print_job_);
56
57 ParsePrintInfo([NSPrintInfo sharedPrintInfo]);
58
59 return OK;
60 }
61
62 void PrintingContext::ParsePrintInfo(NSPrintInfo* print_info) {
63 ResetSettings();
64 print_info_ = [print_info retain];
65 PageRanges page_ranges;
66 NSDictionary* print_info_dict = [print_info_ dictionary];
67 if (![[print_info_dict objectForKey:NSPrintAllPages] boolValue]) {
68 PageRange range;
69 range.from = [[print_info_dict objectForKey:NSPrintFirstPage] intValue] - 1;
70 range.to = [[print_info_dict objectForKey:NSPrintLastPage] intValue] - 1;
71 page_ranges.push_back(range);
72 }
73 PMPrintSession print_session =
74 static_cast<PMPrintSession>([print_info_ PMPrintSession]);
75 PMPageFormat page_format =
76 static_cast<PMPageFormat>([print_info_ PMPageFormat]);
77 PMPrinter printer;
78 PMSessionGetCurrentPrinter(print_session, &printer);
79
80 settings_.Init(printer, page_format, page_ranges, false);
81 }
82
83 PrintingContext::Result PrintingContext::InitWithSettings(
84 const PrintSettings& settings) {
85 DCHECK(!in_print_job_);
86 settings_ = settings;
87
88 NOTIMPLEMENTED();
89
90 return FAILED;
91 }
92
93 void PrintingContext::ResetSettings() {
94 [print_info_ autorelease];
95 print_info_ = nil;
96 settings_.Clear();
97 #ifndef NDEBUG
98 page_number_ = -1;
99 #endif
100 dialog_box_dismissed_ = false;
101 abort_printing_ = false;
102 in_print_job_ = false;
103 context_ = NULL;
104 }
105
106 PrintingContext::Result PrintingContext::NewDocument(
107 const std::wstring& document_name) {
108 DCHECK(!in_print_job_);
109
110 in_print_job_ = true;
111
112 PMPrintSession print_session =
113 static_cast<PMPrintSession>([print_info_ PMPrintSession]);
114 PMPrintSettings print_settings =
115 static_cast<PMPrintSettings>([print_info_ PMPrintSettings]);
116 PMPageFormat page_format =
117 static_cast<PMPageFormat>([print_info_ PMPageFormat]);
118 OSStatus status = PMSessionBeginCGDocumentNoDialog(print_session,
119 print_settings,
120 page_format);
121 if (status != noErr)
122 return OnError();
123
124 #ifndef NDEBUG
125 page_number_ = 0;
126 #endif
127
128 return OK;
129 }
130
131 PrintingContext::Result PrintingContext::NewPage() {
132 if (abort_printing_)
133 return CANCEL;
134 DCHECK(in_print_job_);
135 DCHECK(!context_);
136
137 PMPrintSession print_session =
138 static_cast<PMPrintSession>([print_info_ PMPrintSession]);
139 PMPageFormat page_format =
140 static_cast<PMPageFormat>([print_info_ PMPageFormat]);
141 OSStatus status;
142 status = PMSessionBeginPageNoDialog(print_session, page_format, NULL);
143 if (status != noErr)
144 return OnError();
145 status = PMSessionGetCGGraphicsContext(print_session, &context_);
146 if (status != noErr)
147 return OnError();
148
149 #ifndef NDEBUG
150 ++page_number_;
151 #endif
152
153 return OK;
154 }
155
156 PrintingContext::Result PrintingContext::PageDone() {
157 if (abort_printing_)
158 return CANCEL;
159 DCHECK(in_print_job_);
160 DCHECK(context_);
161
162 PMPrintSession print_session =
163 static_cast<PMPrintSession>([print_info_ PMPrintSession]);
164 OSStatus status = PMSessionEndPageNoDialog(print_session);
165 if (status != noErr)
166 OnError();
167 context_ = NULL;
168
169 return OK;
170 }
171
172 PrintingContext::Result PrintingContext::DocumentDone() {
173 if (abort_printing_)
174 return CANCEL;
175 DCHECK(in_print_job_);
176
177 PMPrintSession print_session =
178 static_cast<PMPrintSession>([print_info_ PMPrintSession]);
179 OSStatus status = PMSessionEndDocumentNoDialog(print_session);
180 if (status != noErr)
181 OnError();
182
183 ResetSettings();
184 return OK;
185 }
186
187 void PrintingContext::Cancel() {
188 abort_printing_ = true;
189 in_print_job_ = false;
190 context_ = NULL;
191
192 PMPrintSession print_session =
193 static_cast<PMPrintSession>([print_info_ PMPrintSession]);
194 PMSessionEndPageNoDialog(print_session);
195 }
196
197 void PrintingContext::DismissDialog() {
198 NOTIMPLEMENTED();
199 }
200
201 PrintingContext::Result PrintingContext::OnError() {
202 ResetSettings();
203 return abort_printing_ ? CANCEL : FAILED;
204 }
205
206 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698