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

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 (!SetPaperName())
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::SetPaperName() {
csilv 2011/11/01 17:34:14 'SetPaperName' seems like an odd name for this met
kmadhusu 2011/11/01 20:00:41 SetPaperName => UpdatePageFormatWithPaperInfo
csilv 2011/11/01 20:11:59 Fantastic, thanks. On 2011/11/01 20:00:41, kmadhu
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 int num_papers = CFArrayGetCount(paper_list);
232 PMPaper best_matching_paper = kPMNoData;
233 for (int i = 0; i < num_papers; ++i) {
234 PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i];
csilv 2011/11/01 17:34:14 This can be simplified by using NSArray fast enume
kmadhusu 2011/11/01 20:00:41 As we discussed, I am ignoring this comment.
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 std::string paper_id_str("Custom paper ID");
261 std::string paper_name_str("Custom paper");
262 CFStringRef paper_id = base::SysUTF8ToCFStringRef(paper_id_str);
263 CFStringRef paper_name = base::SysUTF8ToCFStringRef(paper_name_str);
csilv 2011/11/01 17:34:14 paper_id and paper_name will leak memory because S
kmadhusu 2011/11/01 20:00:41 Done.
264 #if (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5)
265 if (PMPaperCreateCustom != NULL) {
266 if (PMPaperCreateCustom(current_printer, paper_id, paper_name,
267 default_page_width, default_page_height,
268 &margins, &paper) != noErr) {
269 return false;
270 }
271 }
272 #else
273 if (paper == kPMNoData) {
274 if (PMPaperCreate(current_printer, paper_id, paper_name,
kmadhusu 2011/11/01 20:00:41 This code will run on machines whose version is MA
csilv 2011/11/01 20:11:59 Oh, thanks for asking about this. You can remove
275 default_page_width, default_page_height,
276 &margins, &paper) != noErr) {
277 return false;
278 }
279 }
280 #endif
281 [print_info_.get() updateFromPMPageFormat];
282 PMRelease(paper);
283 } else {
284 PMPageFormat chosen_page_format = NULL;
285 if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr)
286 return false;
287
288 // Create page format from that paper.
289 if (PMCreatePageFormatWithPMPaper(&chosen_page_format,
290 best_matching_paper) != noErr) {
291 PMRelease(chosen_page_format);
292 return false;
293 }
294 // Copy over the original format with the new page format.
295 if (PMCopyPageFormat(chosen_page_format, default_page_format) != noErr) {
296 PMRelease(chosen_page_format);
297 return false;
298 }
299 [print_info_.get() updateFromPMPageFormat];
300 PMRelease(chosen_page_format);
301 }
302 return true;
303 }
304
200 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { 305 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) {
201 if (copies < 1) 306 if (copies < 1)
202 return false; 307 return false;
203 308
204 PMPrintSettings pmPrintSettings = 309 PMPrintSettings pmPrintSettings =
205 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); 310 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
206 return PMSetCopies(pmPrintSettings, copies, false) == noErr; 311 return PMSetCopies(pmPrintSettings, copies, false) == noErr;
207 } 312 }
208 313
209 bool PrintingContextMac::SetCollateInPrintSettings(bool collate) { 314 bool PrintingContextMac::SetCollateInPrintSettings(bool collate) {
210 PMPrintSettings pmPrintSettings = 315 PMPrintSettings pmPrintSettings =
211 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); 316 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
212 return PMSetCollate(pmPrintSettings, collate) == noErr; 317 return PMSetCollate(pmPrintSettings, collate) == noErr;
213 } 318 }
214 319
215 bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) { 320 bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) {
216 PMPageFormat page_format = 321 PMPageFormat page_format =
217 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); 322 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
218 323
219 PMOrientation orientation = landscape ? kPMLandscape : kPMPortrait; 324 PMOrientation orientation = landscape ? kPMLandscape : kPMPortrait;
220 325
221 if (PMSetOrientation(page_format, orientation, false) != noErr) 326 if (PMSetOrientation(page_format, orientation, false) != noErr)
222 return false; 327 return false;
223 328
329 PMPrintSession print_session =
330 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
331
332 PMSessionValidatePageFormat(print_session, page_format, kPMDontWantBoolean);
333
224 [print_info_.get() updateFromPMPageFormat]; 334 [print_info_.get() updateFromPMPageFormat];
225 return true; 335 return true;
226 } 336 }
227 337
228 bool PrintingContextMac::SetDuplexModeInPrintSettings(DuplexMode mode) { 338 bool PrintingContextMac::SetDuplexModeInPrintSettings(DuplexMode mode) {
229 PMDuplexMode duplexSetting; 339 PMDuplexMode duplexSetting;
230 switch (mode) { 340 switch (mode) {
231 case LONG_EDGE: 341 case LONG_EDGE:
232 duplexSetting = kPMDuplexNoTumble; 342 duplexSetting = kPMDuplexNoTumble;
233 break; 343 break;
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
377 void PrintingContextMac::ReleaseContext() { 487 void PrintingContextMac::ReleaseContext() {
378 print_info_.reset(); 488 print_info_.reset();
379 context_ = NULL; 489 context_ = NULL;
380 } 490 }
381 491
382 gfx::NativeDrawingContext PrintingContextMac::context() const { 492 gfx::NativeDrawingContext PrintingContextMac::context() const {
383 return context_; 493 return context_;
384 } 494 }
385 495
386 } // namespace printing 496 } // 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