diff options
Diffstat (limited to 'chrome')
11 files changed, 130 insertions, 29 deletions
diff --git a/chrome/browser/printing/print_preview_message_handler.cc b/chrome/browser/printing/print_preview_message_handler.cc index 86504f7..7dd0a96 100644 --- a/chrome/browser/printing/print_preview_message_handler.cc +++ b/chrome/browser/printing/print_preview_message_handler.cc @@ -191,11 +191,13 @@ void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout( const PageSizeMargins& page_layout_in_points, + const gfx::Rect& printable_area_in_points, bool has_custom_page_size_style) { PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); if (!print_preview_ui) return; print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points, + printable_area_in_points, has_custom_page_size_style); } diff --git a/chrome/browser/printing/print_preview_message_handler.h b/chrome/browser/printing/print_preview_message_handler.h index f97d49c..4e23e12 100644 --- a/chrome/browser/printing/print_preview_message_handler.h +++ b/chrome/browser/printing/print_preview_message_handler.h @@ -15,6 +15,10 @@ struct PrintHostMsg_DidGetPreviewPageCount_Params; struct PrintHostMsg_DidPreviewDocument_Params; struct PrintHostMsg_DidPreviewPage_Params; +namespace gfx { +class Rect; +} + namespace printing { struct PageSizeMargins; @@ -46,6 +50,7 @@ class PrintPreviewMessageHandler : public content::WebContentsObserver { void OnRequestPrintPreview(bool source_is_modifiable, bool webnode_only); void OnDidGetDefaultPageLayout( const printing::PageSizeMargins& page_layout_in_points, + const gfx::Rect& printable_area_in_points, bool has_custom_page_size_style); void OnDidGetPreviewPageCount( const PrintHostMsg_DidGetPreviewPageCount_Params& params); diff --git a/chrome/browser/resources/print_preview/header_footer_settings.js b/chrome/browser/resources/print_preview/header_footer_settings.js index 02776c5..882829b 100644 --- a/chrome/browser/resources/print_preview/header_footer_settings.js +++ b/chrome/browser/resources/print_preview/header_footer_settings.js @@ -13,7 +13,6 @@ cr.define('print_preview', function() { function HeaderFooterSettings() { this.headerFooterOption_ = $('header-footer-option'); this.headerFooterCheckbox_ = $('header-footer'); - this.headerFooterApplies_ = false; this.addEventListeners_(); } @@ -33,7 +32,7 @@ cr.define('print_preview', function() { * @return {boolean} true if Headers and Footers are checked. */ hasHeaderFooter: function() { - return this.headerFooterApplies_ && this.headerFooterCheckbox_.checked; + return previewModifiable && this.headerFooterCheckbox_.checked; }, /** @@ -46,6 +45,40 @@ cr.define('print_preview', function() { }, /** + * Checks the printable area and updates the visibility of header footer + * option based on the selected margins. + * @param {{contentWidth: number, contentHeight: number, marginLeft: number, + * marginRight: number, marginTop: number, marginBottom: number, + * printableAreaX: number, printableAreaY: number, + * printableAreaWidth: number, printableAreaHeight: number}} + * pageLayout Specifies default page layout details in points. + * @param {number} marginsType Specifies the selected margins type value. + */ + checkAndHideHeaderFooterOption: function(pageLayout, marginsType) { + var headerFooterApplies = true; + if (marginsType == + print_preview.MarginSettings.MARGINS_VALUE_NO_MARGINS || + !previewModifiable) { + headerFooterApplies = false; + } else if (marginsType != + print_preview.MarginSettings.MARGINS_VALUE_MINIMUM) { + if (cr.isLinux || cr.isChromeOS) { + headerFooterApplies = pageLayout.marginTop > 0 || + pageLayout.marginBottom > 0; + } else { + var pageHeight = pageLayout.marginTop + pageLayout.marginBottom + + pageLayout.contentHeight; + headerFooterApplies = + (pageLayout.marginTop > pageLayout.printableAreaY) || + (pageLayout.marginBottom > + (pageHeight - pageLayout.printableAreaY - + pageLayout.printableAreaHeight)); + } + } + this.setVisible_(headerFooterApplies); + }, + + /** * Adding listeners to header footer related controls. * @private */ @@ -54,14 +87,6 @@ cr.define('print_preview', function() { this.onHeaderFooterChanged_.bind(this); document.addEventListener(customEvents.PDF_LOADED, this.onPDFLoaded_.bind(this)); - document.addEventListener(customEvents.MARGINS_SELECTION_CHANGED, - this.onMarginsSelectionChanged_.bind(this)); - }, - - onMarginsSelectionChanged_: function(event) { - this.headerFooterApplies_ = event.selectedMargins != - print_preview.MarginSettings.MARGINS_VALUE_NO_MARGINS; - this.setVisible_(this.headerFooterApplies_); }, /** diff --git a/chrome/browser/resources/print_preview/margin_settings.js b/chrome/browser/resources/print_preview/margin_settings.js index 9faa640..0ffca9f 100644 --- a/chrome/browser/resources/print_preview/margin_settings.js +++ b/chrome/browser/resources/print_preview/margin_settings.js @@ -240,13 +240,6 @@ cr.define('print_preview', function() { this.customMargins_ = new Margins(-1, -1, -1 , -1); this.customMargins = lastUsedMarginsSettings; } - this.dispatchMarginsChangedEvent_(); - }, - - dispatchMarginsChangedEvent_: function() { - var customEvent = cr.Event(customEvents.MARGINS_SELECTION_CHANGED); - customEvent.selectedMargins = this.selectedMarginsValue; - document.dispatchEvent(customEvent); }, /** @@ -567,8 +560,6 @@ cr.define('print_preview', function() { * @private */ onMarginsChanged_: function() { - this.dispatchMarginsChangedEvent_(); - if (this.isDefaultMarginsSelected() || this.isMinimumMarginsSelected() || this.isNoMarginsSelected()) this.onDefaultMinimumNoMarginsSelected_(); diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js index d39d666..d5d239d 100644 --- a/chrome/browser/resources/print_preview/print_preview.js +++ b/chrome/browser/resources/print_preview/print_preview.js @@ -126,8 +126,6 @@ var customEvents = { MARGIN_TEXTBOX_FOCUSED: 'marginTextboxFocused', // Fired when a new preview might be needed because of margin changes. MARGINS_MAY_HAVE_CHANGED: 'marginsMayHaveChanged', - // Fired when the margins selection in the dropdown changes. - MARGINS_SELECTION_CHANGED: 'marginsSelectionChanged', // Fired when a pdf generation related error occurs. PDF_GENERATION_ERROR: 'PDFGenerationError', // Fired once the first page of the pdf document is loaded in the plugin. @@ -868,10 +866,13 @@ function onDidGetPreviewPageCount(pageCount, previewResponseId) { } /** - * @param {printing::PageSizeMargins} pageLayout The default layout of the page - * in points. + * @param {{contentWidth: number, contentHeight: number, marginLeft: number, + * marginRight: number, marginTop: number, marginBottom: number, + * printableAreaX: number, printableAreaY: number, + * printableAreaWidth: number, printableAreaHeight: number}} pageLayout + * Specifies default page layout details in points. * @param {boolean} hasCustomPageSizeStyle Indicates whether the previewed - * document has a custom page size style. + * document has a custom page size style. */ function onDidGetDefaultPageLayout(pageLayout, hasCustomPageSizeStyle) { hasPageSizeStyle = hasCustomPageSizeStyle; @@ -882,6 +883,8 @@ function onDidGetDefaultPageLayout(pageLayout, hasCustomPageSizeStyle) { pageLayout.marginTop, pageLayout.marginRight, pageLayout.marginBottom); + headerFooterSettings.checkAndHideHeaderFooterOption( + pageLayout, marginSettings.selectedMarginsValue); } /** diff --git a/chrome/browser/ui/webui/print_preview/print_preview_ui.cc b/chrome/browser/ui/webui/print_preview/print_preview_ui.cc index c1c2043..7c41091 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_ui.cc +++ b/chrome/browser/ui/webui/print_preview/print_preview_ui.cc @@ -24,6 +24,7 @@ #include "content/public/browser/web_contents.h" #include "printing/page_size_margins.h" #include "printing/print_job_constants.h" +#include "ui/gfx/rect.h" using content::WebContents; using printing::PageSizeMargins; @@ -206,10 +207,12 @@ void PrintPreviewUI::OnDidGetPreviewPageCount( } void PrintPreviewUI::OnDidGetDefaultPageLayout( - const PageSizeMargins& page_layout, bool has_custom_page_size_style) { + const PageSizeMargins& page_layout, const gfx::Rect& printable_area, + bool has_custom_page_size_style) { if (page_layout.margin_top < 0 || page_layout.margin_left < 0 || page_layout.margin_bottom < 0 || page_layout.margin_right < 0 || - page_layout.content_width < 0 || page_layout.content_height < 0) { + page_layout.content_width < 0 || page_layout.content_height < 0 || + printable_area.width() <= 0 || printable_area.height() <= 0) { NOTREACHED(); return; } @@ -221,6 +224,12 @@ void PrintPreviewUI::OnDidGetDefaultPageLayout( layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right); layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width); layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height); + layout.SetInteger(printing::kSettingPrintableAreaX, printable_area.x()); + layout.SetInteger(printing::kSettingPrintableAreaY, printable_area.y()); + layout.SetInteger(printing::kSettingPrintableAreaWidth, + printable_area.width()); + layout.SetInteger(printing::kSettingPrintableAreaHeight, + printable_area.height()); base::FundamentalValue has_page_size_style(has_custom_page_size_style); web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout, diff --git a/chrome/browser/ui/webui/print_preview/print_preview_ui.h b/chrome/browser/ui/webui/print_preview/print_preview_ui.h index ada0d5e..28e997d 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_ui.h +++ b/chrome/browser/ui/webui/print_preview/print_preview_ui.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -19,6 +19,10 @@ class PrintPreviewDataService; class PrintPreviewHandler; struct PrintHostMsg_DidGetPreviewPageCount_Params; +namespace gfx { +class Rect; +} + namespace printing { struct PageSizeMargins; } @@ -81,6 +85,7 @@ class PrintPreviewUI : public ConstrainedHtmlUI { // selected printer and page size. void OnDidGetDefaultPageLayout( const printing::PageSizeMargins& page_layout, + const gfx::Rect& printable_area, bool has_custom_page_size_style); // Notifies the Web UI that the 0-based page |page_number| has been rendered. diff --git a/chrome/common/print_messages.h b/chrome/common/print_messages.h index c0e1a8a..32dd0b9 100644 --- a/chrome/common/print_messages.h +++ b/chrome/common/print_messages.h @@ -365,10 +365,12 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetPreviewPageCount, // Notify the browser of the default page layout according to the currently // selected printer and page size. +// |printable_area_in_points| Specifies the printable area in points. // |has_custom_page_size_style| is true when the printing frame has a custom // page size css otherwise false. -IPC_MESSAGE_ROUTED2(PrintHostMsg_DidGetDefaultPageLayout, +IPC_MESSAGE_ROUTED3(PrintHostMsg_DidGetDefaultPageLayout, printing::PageSizeMargins /* page layout in points */, + gfx::Rect /* printable area in points */, bool /* has custom page size style */) // Notify the browser a print preview page has been rendered. diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index ec01361..55aafca 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -35,6 +35,7 @@ #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLResponse.h" #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" #include "ui/base/l10n/l10n_util.h" +#include "ui/gfx/rect.h" #include "webkit/glue/webpreferences.h" #if defined(OS_POSIX) @@ -872,14 +873,27 @@ bool PrintWebViewHelper::CreatePreviewDocument() { ComputePageLayoutInPointsForCss(print_preview_context_.frame(), 0, print_params, ignore_css_margins_, fit_to_page_, NULL, &default_page_layout); + if (!old_print_pages_params_.get() || !PageLayoutIsEqual(*old_print_pages_params_, *print_pages_params_)) { bool has_page_size_style = PrintingFrameHasPageSizeStyle( print_preview_context_.frame(), print_preview_context_.total_page_count()); + int dpi = GetDPI(&print_params); + gfx::Rect printable_area_in_points( + ConvertUnit(print_pages_params_->params.printable_area.x(), + dpi, printing::kPointsPerInch), + ConvertUnit(print_pages_params_->params.printable_area.y(), + dpi, printing::kPointsPerInch), + ConvertUnit(print_pages_params_->params.printable_area.width(), + dpi, printing::kPointsPerInch), + ConvertUnit(print_pages_params_->params.printable_area.height(), + dpi, printing::kPointsPerInch)); + // Margins: Send default page layout to browser process. Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(), default_page_layout, + printable_area_in_points, has_page_size_style)); } diff --git a/chrome/renderer/print_web_view_helper_browsertest.cc b/chrome/renderer/print_web_view_helper_browsertest.cc index bc4f8a3..ff4006f 100644 --- a/chrome/renderer/print_web_view_helper_browsertest.cc +++ b/chrome/renderer/print_web_view_helper_browsertest.cc @@ -431,7 +431,7 @@ class PrintWebViewHelperPreviewTest : public PrintWebViewHelperTestBase { EXPECT_EQ(margin_right, param.a.margin_right); EXPECT_EQ(margin_left, param.a.margin_left); EXPECT_EQ(margin_bottom, param.a.margin_bottom); - EXPECT_EQ(page_has_print_css, param.b); + EXPECT_EQ(page_has_print_css, param.c); } } diff --git a/chrome/test/data/webui/print_preview.js b/chrome/test/data/webui/print_preview.js index b427352..70a0f03 100644 --- a/chrome/test/data/webui/print_preview.js +++ b/chrome/test/data/webui/print_preview.js @@ -270,6 +270,51 @@ TEST_F('PrintPreviewWebUITest', 'TestSectionsDisabled', function() { checkSectionVisible(copiesSettings.copiesOption_, false); }); +// Page layout has zero margins. Hide header and footer option. +TEST_F('PrintPreviewWebUITest', 'PageLayoutHasNoMarginsHideHeaderFooter', + function() { + setInitialSettings({previewModifiable: true}); + onDidGetDefaultPageLayout({ + contentWidth: 100, contentHeight: 200, marginLeft: 0, marginRight: 0, + marginTop: 0, marginBottom: 0, printableAreaX: 0, printableAreaY: 0, + printableAreaWidth: 100, printableAreaHeight: 200}, true); + checkSectionVisible(headerFooterSettings.headerFooterOption_, false); +}); + +// Page layout has non-zero margins. Show header and footer option. +TEST_F('PrintPreviewWebUITest', 'PageLayoutHasMarginsShowHeaderFooter', + function() { + setInitialSettings({previewModifiable: true}); + onDidGetDefaultPageLayout({ + contentWidth: 100, contentHeight: 200, marginLeft: 3, marginRight: 2, + marginTop: 4, marginBottom: 1, printableAreaX: 1, printableAreaY: 1, + printableAreaWidth: 103, printableAreaHeight: 203}, true); + checkSectionVisible(headerFooterSettings.headerFooterOption_, true); +}); + +// Page layout has zero top and bottom margins. Hide header and footer option. +TEST_F('PrintPreviewWebUITest', 'ZeroTopAndBottomMarginsHideHeaderFooter', + function() { + setInitialSettings({previewModifiable: true}); + onDidGetDefaultPageLayout({ + contentWidth: 100, contentHeight: 200, marginLeft: 3, marginRight: 2, + marginTop: 0, marginBottom: 0, printableAreaX: 1, printableAreaY: 1, + printableAreaWidth: 98, printableAreaHeight: 198}, false); + checkSectionVisible(headerFooterSettings.headerFooterOption_, false); +}); + +// Page layout has zero top and non-zero bottom margin. Show header and footer +// option. +TEST_F('PrintPreviewWebUITest', 'ZeroTopAndNonZeroBottomMarginShowHeaderFooter', + function() { + setInitialSettings({previewModifiable: true}); + onDidGetDefaultPageLayout({ + contentWidth: 100, contentHeight: 200, marginLeft: 6, marginRight: 4, + marginTop: 0, marginBottom: 3, printableAreaX: 1, printableAreaY: 1, + printableAreaWidth: 103, printableAreaHeight: 208}, false); + checkSectionVisible(headerFooterSettings.headerFooterOption_, true); +}); + // Test that the color settings are set according to the printer capabilities. TEST_F('PrintPreviewWebUITest', 'TestColorSettings', function() { this.mockHandler.expects(once()).getPrinterCapabilities('FooDevice'). |