OLD | NEW |
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 Loading... |
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 Loading... |
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, ¤t_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(current_printer, paper_id, paper_name, |
| 263 default_page_width, default_page_height, &margins, &paper) != |
| 264 noErr) { |
| 265 return false; |
| 266 } |
| 267 [print_info_.get() updateFromPMPageFormat]; |
| 268 PMRelease(paper); |
| 269 } else { |
| 270 PMPageFormat chosen_page_format = NULL; |
| 271 if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr) |
| 272 return false; |
| 273 |
| 274 // Create page format from that paper. |
| 275 if (PMCreatePageFormatWithPMPaper(&chosen_page_format, |
| 276 best_matching_paper) != noErr) { |
| 277 PMRelease(chosen_page_format); |
| 278 return false; |
| 279 } |
| 280 // Copy over the original format with the new page format. |
| 281 if (PMCopyPageFormat(chosen_page_format, default_page_format) != noErr) { |
| 282 PMRelease(chosen_page_format); |
| 283 return false; |
| 284 } |
| 285 [print_info_.get() updateFromPMPageFormat]; |
| 286 PMRelease(chosen_page_format); |
| 287 } |
| 288 return true; |
| 289 } |
| 290 |
200 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { | 291 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { |
201 if (copies < 1) | 292 if (copies < 1) |
202 return false; | 293 return false; |
203 | 294 |
204 PMPrintSettings pmPrintSettings = | 295 PMPrintSettings pmPrintSettings = |
205 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); | 296 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
206 return PMSetCopies(pmPrintSettings, copies, false) == noErr; | 297 return PMSetCopies(pmPrintSettings, copies, false) == noErr; |
207 } | 298 } |
208 | 299 |
209 bool PrintingContextMac::SetCollateInPrintSettings(bool collate) { | 300 bool PrintingContextMac::SetCollateInPrintSettings(bool collate) { |
210 PMPrintSettings pmPrintSettings = | 301 PMPrintSettings pmPrintSettings = |
211 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); | 302 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
212 return PMSetCollate(pmPrintSettings, collate) == noErr; | 303 return PMSetCollate(pmPrintSettings, collate) == noErr; |
213 } | 304 } |
214 | 305 |
215 bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) { | 306 bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) { |
216 PMPageFormat page_format = | 307 PMPageFormat page_format = |
217 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); | 308 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); |
218 | 309 |
219 PMOrientation orientation = landscape ? kPMLandscape : kPMPortrait; | 310 PMOrientation orientation = landscape ? kPMLandscape : kPMPortrait; |
220 | 311 |
221 if (PMSetOrientation(page_format, orientation, false) != noErr) | 312 if (PMSetOrientation(page_format, orientation, false) != noErr) |
222 return false; | 313 return false; |
223 | 314 |
| 315 PMPrintSession print_session = |
| 316 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
| 317 |
| 318 PMSessionValidatePageFormat(print_session, page_format, kPMDontWantBoolean); |
| 319 |
224 [print_info_.get() updateFromPMPageFormat]; | 320 [print_info_.get() updateFromPMPageFormat]; |
225 return true; | 321 return true; |
226 } | 322 } |
227 | 323 |
228 bool PrintingContextMac::SetDuplexModeInPrintSettings(DuplexMode mode) { | 324 bool PrintingContextMac::SetDuplexModeInPrintSettings(DuplexMode mode) { |
229 PMDuplexMode duplexSetting; | 325 PMDuplexMode duplexSetting; |
230 switch (mode) { | 326 switch (mode) { |
231 case LONG_EDGE: | 327 case LONG_EDGE: |
232 duplexSetting = kPMDuplexNoTumble; | 328 duplexSetting = kPMDuplexNoTumble; |
233 break; | 329 break; |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
377 void PrintingContextMac::ReleaseContext() { | 473 void PrintingContextMac::ReleaseContext() { |
378 print_info_.reset(); | 474 print_info_.reset(); |
379 context_ = NULL; | 475 context_ = NULL; |
380 } | 476 } |
381 | 477 |
382 gfx::NativeDrawingContext PrintingContextMac::context() const { | 478 gfx::NativeDrawingContext PrintingContextMac::context() const { |
383 return context_; | 479 return context_; |
384 } | 480 } |
385 | 481 |
386 } // namespace printing | 482 } // namespace printing |
OLD | NEW |