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

Side by Side Diff: printing/printing_context_mac.mm

Issue 8426002: PrintPreview: [MAC] Set the system default page size in NSPrintInfo PMPageFormat. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: '' Created 9 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « printing/printing_context_mac.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "printing/printing_context_mac.h" 5 #include "printing/printing_context_mac.h"
6 6
7 #import <ApplicationServices/ApplicationServices.h> 7 #import <ApplicationServices/ApplicationServices.h>
8 #import <AppKit/AppKit.h> 8 #import <AppKit/AppKit.h>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 return OnError(); 136 return OnError();
137 137
138 if (!SetDuplexModeInPrintSettings( 138 if (!SetDuplexModeInPrintSettings(
139 static_cast<DuplexMode>(duplex_mode))) { 139 static_cast<DuplexMode>(duplex_mode))) {
140 return OnError(); 140 return OnError();
141 } 141 }
142 142
143 if (!SetOutputColor(color)) 143 if (!SetOutputColor(color))
144 return OnError(); 144 return OnError();
145 } 145 }
146 if (!UpdatePageFormatWithPaperInfo())
147 return OnError();
146 148
147 if (!SetOrientationIsLandscape(landscape)) 149 if (!SetOrientationIsLandscape(landscape))
148 return OnError(); 150 return OnError();
149 151
150 [print_info_.get() updateFromPMPrintSettings]; 152 [print_info_.get() updateFromPMPrintSettings];
151 153
152 InitPrintSettingsFromPrintInfo(ranges); 154 InitPrintSettingsFromPrintInfo(ranges);
153 return OK; 155 return OK;
154 } 156 }
155 157
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 192
191 PMPrinter new_printer = PMPrinterCreateFromPrinterID(new_printer_id.get()); 193 PMPrinter new_printer = PMPrinterCreateFromPrinterID(new_printer_id.get());
192 if (new_printer == NULL) 194 if (new_printer == NULL)
193 return false; 195 return false;
194 196
195 OSStatus status = PMSessionSetCurrentPMPrinter(print_session, new_printer); 197 OSStatus status = PMSessionSetCurrentPMPrinter(print_session, new_printer);
196 PMRelease(new_printer); 198 PMRelease(new_printer);
197 return status == noErr; 199 return status == noErr;
198 } 200 }
199 201
202 bool PrintingContextMac::UpdatePageFormatWithPaperInfo() {
203 PMPrintSession print_session =
204 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
205
206 PMPageFormat default_page_format =
207 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
208
209 PMPaper default_paper;
210 if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr)
211 return false;
212
213 double default_page_width, default_page_height;
214 if (PMPaperGetWidth(default_paper, &default_page_width) != noErr)
215 return false;
216
217 if (PMPaperGetHeight(default_paper, &default_page_height) != noErr)
218 return false;
219
220 PMPrinter current_printer = NULL;
221 if (PMSessionGetCurrentPrinter(print_session, &current_printer) != noErr)
222 return false;
223
224 if (current_printer == nil)
225 return false;
226
227 CFArrayRef paper_list = NULL;
228 if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr)
229 return false;
230
231 PMPaper best_matching_paper = kPMNoData;
232 int num_papers = CFArrayGetCount(paper_list);
233 for (int i = 0; i < num_papers; ++i) {
234 PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i];
235 double paper_width, paper_height;
236 PMPaperGetWidth(paper, &paper_width);
237 PMPaperGetHeight(paper, &paper_height);
238 if (default_page_width == paper_width &&
239 default_page_height == paper_height) {
240 best_matching_paper = paper;
241 break;
242 }
243 // Trying to find the best matching paper.
244 if (fabs(default_page_width - paper_width) < 2 &&
245 fabs(default_page_height - paper_height) < 2) {
246 best_matching_paper = paper;
247 }
248 }
249
250 if (best_matching_paper == kPMNoData) {
251 PMPaper paper = kPMNoData;
252 // Create a custom paper for the specified default page size.
253 PMPaperMargins default_margins;
254 if (PMPaperGetMargins(default_paper, &default_margins) != noErr)
255 return false;
256
257 const PMPaperMargins margins =
258 {default_margins.top, default_margins.left, default_margins.bottom,
259 default_margins.right};
260 CFStringRef paper_id = CFSTR("Custom paper ID");
261 CFStringRef paper_name = CFSTR("Custom paper");
262 if (PMPaperCreateCustom != NULL) {
csilv 2011/11/01 20:55:58 Remove this check. This would only be needed pre-
kmadhusu 2011/11/01 22:06:18 Done.
263 if (PMPaperCreateCustom(current_printer, paper_id, paper_name,
264 default_page_width, default_page_height,
265 &margins, &paper) != noErr) {
266 return false;
267 }
268 }
269 [print_info_.get() updateFromPMPageFormat];
270 PMRelease(paper);
271 } else {
272 PMPageFormat chosen_page_format = NULL;
273 if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr)
274 return false;
275
276 // Create page format from that paper.
277 if (PMCreatePageFormatWithPMPaper(&chosen_page_format,
278 best_matching_paper) != noErr) {
279 PMRelease(chosen_page_format);
280 return false;
281 }
282 // Copy over the original format with the new page format.
283 if (PMCopyPageFormat(chosen_page_format, default_page_format) != noErr) {
284 PMRelease(chosen_page_format);
285 return false;
286 }
287 [print_info_.get() updateFromPMPageFormat];
288 PMRelease(chosen_page_format);
289 }
290 return true;
291 }
292
200 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { 293 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) {
201 if (copies < 1) 294 if (copies < 1)
202 return false; 295 return false;
203 296
204 PMPrintSettings pmPrintSettings = 297 PMPrintSettings pmPrintSettings =
205 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); 298 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
206 return PMSetCopies(pmPrintSettings, copies, false) == noErr; 299 return PMSetCopies(pmPrintSettings, copies, false) == noErr;
207 } 300 }
208 301
209 bool PrintingContextMac::SetCollateInPrintSettings(bool collate) { 302 bool PrintingContextMac::SetCollateInPrintSettings(bool collate) {
210 PMPrintSettings pmPrintSettings = 303 PMPrintSettings pmPrintSettings =
211 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); 304 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
212 return PMSetCollate(pmPrintSettings, collate) == noErr; 305 return PMSetCollate(pmPrintSettings, collate) == noErr;
213 } 306 }
214 307
215 bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) { 308 bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) {
216 PMPageFormat page_format = 309 PMPageFormat page_format =
217 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); 310 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
218 311
219 PMOrientation orientation = landscape ? kPMLandscape : kPMPortrait; 312 PMOrientation orientation = landscape ? kPMLandscape : kPMPortrait;
220 313
221 if (PMSetOrientation(page_format, orientation, false) != noErr) 314 if (PMSetOrientation(page_format, orientation, false) != noErr)
222 return false; 315 return false;
223 316
317 PMPrintSession print_session =
318 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
319
320 PMSessionValidatePageFormat(print_session, page_format, kPMDontWantBoolean);
321
224 [print_info_.get() updateFromPMPageFormat]; 322 [print_info_.get() updateFromPMPageFormat];
225 return true; 323 return true;
226 } 324 }
227 325
228 bool PrintingContextMac::SetDuplexModeInPrintSettings(DuplexMode mode) { 326 bool PrintingContextMac::SetDuplexModeInPrintSettings(DuplexMode mode) {
229 PMDuplexMode duplexSetting; 327 PMDuplexMode duplexSetting;
230 switch (mode) { 328 switch (mode) {
231 case LONG_EDGE: 329 case LONG_EDGE:
232 duplexSetting = kPMDuplexNoTumble; 330 duplexSetting = kPMDuplexNoTumble;
233 break; 331 break;
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 void PrintingContextMac::ReleaseContext() { 475 void PrintingContextMac::ReleaseContext() {
378 print_info_.reset(); 476 print_info_.reset();
379 context_ = NULL; 477 context_ = NULL;
380 } 478 }
381 479
382 gfx::NativeDrawingContext PrintingContextMac::context() const { 480 gfx::NativeDrawingContext PrintingContextMac::context() const {
383 return context_; 481 return context_;
384 } 482 }
385 483
386 } // namespace printing 484 } // namespace printing
OLDNEW
« no previous file with comments | « printing/printing_context_mac.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698