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

Unified 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, 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « printing/printing_context_mac.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: printing/printing_context_mac.mm
diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm
index 9008eadea79ea91930fc83933259d7f1967e4a4e..5b11c1898fe3ae5c9db9f5c677673bc1aeb8dff7 100644
--- a/printing/printing_context_mac.mm
+++ b/printing/printing_context_mac.mm
@@ -143,6 +143,8 @@ PrintingContext::Result PrintingContextMac::UpdatePrinterSettings(
if (!SetOutputColor(color))
return OnError();
}
+ if (!UpdatePageFormatWithPaperInfo())
+ return OnError();
if (!SetOrientationIsLandscape(landscape))
return OnError();
@@ -197,6 +199,95 @@ bool PrintingContextMac::SetPrinter(const std::string& device_name) {
return status == noErr;
}
+bool PrintingContextMac::UpdatePageFormatWithPaperInfo() {
+ PMPrintSession print_session =
+ static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
+
+ PMPageFormat default_page_format =
+ static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
+
+ PMPaper default_paper;
+ if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr)
+ return false;
+
+ double default_page_width, default_page_height;
+ if (PMPaperGetWidth(default_paper, &default_page_width) != noErr)
+ return false;
+
+ if (PMPaperGetHeight(default_paper, &default_page_height) != noErr)
+ return false;
+
+ PMPrinter current_printer = NULL;
+ if (PMSessionGetCurrentPrinter(print_session, &current_printer) != noErr)
+ return false;
+
+ if (current_printer == nil)
+ return false;
+
+ CFArrayRef paper_list = NULL;
+ if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr)
+ return false;
+
+ PMPaper best_matching_paper = kPMNoData;
+ int num_papers = CFArrayGetCount(paper_list);
+ for (int i = 0; i < num_papers; ++i) {
+ PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i];
+ double paper_width, paper_height;
+ PMPaperGetWidth(paper, &paper_width);
+ PMPaperGetHeight(paper, &paper_height);
+ if (default_page_width == paper_width &&
+ default_page_height == paper_height) {
+ best_matching_paper = paper;
+ break;
+ }
+ // Trying to find the best matching paper.
+ if (fabs(default_page_width - paper_width) < 2 &&
+ fabs(default_page_height - paper_height) < 2) {
+ best_matching_paper = paper;
+ }
+ }
+
+ if (best_matching_paper == kPMNoData) {
+ PMPaper paper = kPMNoData;
+ // Create a custom paper for the specified default page size.
+ PMPaperMargins default_margins;
+ if (PMPaperGetMargins(default_paper, &default_margins) != noErr)
+ return false;
+
+ const PMPaperMargins margins =
+ {default_margins.top, default_margins.left, default_margins.bottom,
+ default_margins.right};
+ CFStringRef paper_id = CFSTR("Custom paper ID");
+ CFStringRef paper_name = CFSTR("Custom paper");
+ if (PMPaperCreateCustom(current_printer, paper_id, paper_name,
+ default_page_width, default_page_height, &margins, &paper) !=
+ noErr) {
+ return false;
+ }
+ [print_info_.get() updateFromPMPageFormat];
+ PMRelease(paper);
+ } else {
+ PMPageFormat chosen_page_format = NULL;
+ if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr)
+ return false;
+
+ // Create page format from that paper.
+ if (PMCreatePageFormatWithPMPaper(&chosen_page_format,
+ best_matching_paper) != noErr) {
+ PMRelease(chosen_page_format);
+ return false;
+ }
+ // Copy over the original format with the new page format.
+ if (PMCopyPageFormat(chosen_page_format, default_page_format) != noErr) {
+ PMRelease(chosen_page_format);
+ return false;
+ }
+ [print_info_.get() updateFromPMPageFormat];
+ PMRelease(chosen_page_format);
+ }
+ return true;
+}
+
bool PrintingContextMac::SetCopiesInPrintSettings(int copies) {
if (copies < 1)
return false;
@@ -221,6 +312,11 @@ bool PrintingContextMac::SetOrientationIsLandscape(bool landscape) {
if (PMSetOrientation(page_format, orientation, false) != noErr)
return false;
+ PMPrintSession print_session =
+ static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
+
+ PMSessionValidatePageFormat(print_session, page_format, kPMDontWantBoolean);
+
[print_info_.get() updateFromPMPageFormat];
return true;
}
« 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