0

PrintPreview: [MAC] Set the system default page size in NSPrintInfo PMPageFormat.

BUG=none
TEST=Preview a webpage. Make sure the preview page size is same as default page size in print&fax preference pane.


Review URL: http://codereview.chromium.org/8426002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@108235 0039d316-1c4b-4281-b951-d872f2087c98
This commit is contained in:
kmadhusu@chromium.org
2011-11-02 02:32:57 +00:00
parent 67cf2386da
commit 4ed1df613b
2 changed files with 100 additions and 0 deletions

@ -57,6 +57,10 @@ class PRINTING_EXPORT PrintingContextMac : public PrintingContext {
// Returns true if the printer was set else returns false.
bool SetPrinter(const std::string& device_name);
// Updates |print_info_| page format with user default paper information.
// Returns true if the paper was set else returns false.
bool UpdatePageFormatWithPaperInfo();
// Sets |copies| in PMPrintSettings.
// Returns true if the number of copies is set.
bool SetCopiesInPrintSettings(int copies);

@ -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;
}