summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoraayushkumar@chromium.org <aayushkumar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-23 19:56:18 +0000
committeraayushkumar@chromium.org <aayushkumar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-08-23 19:56:18 +0000
commit7a9b03e386c6ca34575c1819ba0ad413a15e2d76 (patch)
tree84b7a18a28042a7d4b6d0f14e4126a666fd68c54
parent12dd463c5497498e49214ae186c681adcbcd445c (diff)
downloadchromium_src-7a9b03e386c6ca34575c1819ba0ad413a15e2d76.zip
chromium_src-7a9b03e386c6ca34575c1819ba0ad413a15e2d76.tar.gz
chromium_src-7a9b03e386c6ca34575c1819ba0ad413a15e2d76.tar.bz2
Backend changes for custom margins.
The UI for margin options are hidden by default. BUG=69337 TEST= There is currently minimal UI for testing this. The custom margins option does nothing. You can however click the no margins option to see the page print without margins. Review URL: http://codereview.chromium.org/7552033 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97901 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--chrome/app/generated_resources.grd18
-rw-r--r--chrome/browser/printing/print_preview_message_handler.cc14
-rw-r--r--chrome/browser/printing/print_preview_message_handler.h4
-rw-r--r--chrome/browser/resources/print_preview/layout_settings.js2
-rw-r--r--chrome/browser/resources/print_preview/margin_settings.html8
-rw-r--r--chrome/browser/resources/print_preview/margin_settings.js121
-rw-r--r--chrome/browser/resources/print_preview/print_preview.html4
-rw-r--r--chrome/browser/resources/print_preview/print_preview.js21
-rw-r--r--chrome/browser/ui/webui/print_preview_data_source.cc4
-rw-r--r--chrome/browser/ui/webui/print_preview_ui.cc23
-rw-r--r--chrome/browser/ui/webui/print_preview_ui.h9
-rw-r--r--chrome/common/print_messages.h15
-rw-r--r--chrome/renderer/print_web_view_helper.cc102
-rw-r--r--chrome/renderer/print_web_view_helper.h17
-rw-r--r--chrome/renderer/print_web_view_helper_browsertest.cc1
-rw-r--r--chrome/renderer/print_web_view_helper_linux.cc3
-rw-r--r--chrome/renderer/print_web_view_helper_mac.mm3
-rw-r--r--chrome/renderer/print_web_view_helper_win.cc3
-rw-r--r--printing/page_size_margins.h24
-rw-r--r--printing/print_job_constants.cc25
-rw-r--r--printing/print_job_constants.h8
-rw-r--r--printing/printing.gyp1
22 files changed, 402 insertions, 28 deletions
diff --git a/chrome/app/generated_resources.grd b/chrome/app/generated_resources.grd
index 1dbfb45..864ed27 100644
--- a/chrome/app/generated_resources.grd
+++ b/chrome/app/generated_resources.grd
@@ -4239,13 +4239,13 @@ Keep your key file in a safe place. You will need it to create new versions of y
</message>
<message name="IDS_FLAGS_ENABLE_SHORTCUTS_PROVIDER_DESCRIPTION" desc=" Description of the 'Enable better ranking of previously selected shortcuts in omnibox' lab.">
Remembers commonly selected autocomplete results based on the omnibox search term and offers those URLs the next time that same search term is typed.
- </message>
+ </message>
<message name="IDS_FLAGS_MEMORY_WIDGET_NAME" desc="Name of the 'Show memory status' lab.">
Show memory status
</message>
<message name="IDS_FLAGS_MEMORY_WIDGET_DESCRIPTION" desc="Description of the 'Show memory status' lab.">
Shows the amount of free memory in the status area. Click on the menu for more information about system memory.
- </message>
+ </message>
<message name="IDS_FLAGS_DOWNLOADS_NEW_UI_NAME" desc="Name of the 'New Downloads UI' lab.">
New Downloads UI
</message>
@@ -6028,6 +6028,18 @@ Keep your key file in a safe place. You will need it to create new versions of y
<message name="IDS_PRINT_PREVIEW_OPTION_HEADER_FOOTER" desc="Checkbox label that provides a choice to print the headers and footers.">
Headers and footers
</message>
+ <message name="IDS_PRINT_PREVIEW_MARGINS_LABEL" desc="Margins option label. Provides user the option to change the margins of the printed page.">
+ Margins
+ </message>
+ <message name="IDS_PRINT_PREVIEW_DEFAULT_MARGINS" desc="Option that specifies the page be printed with default margins.">
+ Default
+ </message>
+ <message name="IDS_PRINT_PREVIEW_NO_MARGINS" desc="Option that specifies the page be printed with no margins.">
+ None
+ </message>
+ <message name="IDS_PRINT_PREVIEW_CUSTOM_MARGINS" desc="Option that specifies the page be printed with user-specified custom margins.">
+ Custom
+ </message>
<!-- Load State -->
<message name="IDS_LOAD_STATE_WAITING_FOR_DELEGATE">
@@ -9221,7 +9233,7 @@ Keep your key file in a safe place. You will need it to create new versions of y
Album
</message>
<message name="IDS_FILE_BROWSER_ID3_BPM" desc="BPM ">
- BPM
+ BPM
</message>
<message name="IDS_FILE_BROWSER_ID3_COMPOSER" desc="Composer">
Composer
diff --git a/chrome/browser/printing/print_preview_message_handler.cc b/chrome/browser/printing/print_preview_message_handler.cc
index 810f0ab..9bbc96f 100644
--- a/chrome/browser/printing/print_preview_message_handler.cc
+++ b/chrome/browser/printing/print_preview_message_handler.cc
@@ -23,6 +23,7 @@
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/common/content_restriction.h"
+#include "printing/page_size_margins.h"
#include "printing/print_job_constants.h"
namespace {
@@ -195,6 +196,17 @@ void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) {
}
}
+void PrintPreviewMessageHandler::OnDidGetDefaultPageLayout(
+ const PageSizeMargins& page_layout_in_points) {
+ TabContents* print_preview_tab = GetPrintPreviewTab();
+ if (!print_preview_tab)
+ return;
+
+ PrintPreviewUI* print_preview_ui =
+ static_cast<PrintPreviewUI*>(print_preview_tab->web_ui());
+ print_preview_ui->OnDidGetDefaultPageLayout(page_layout_in_points);
+}
+
void PrintPreviewMessageHandler::OnPrintPreviewCancelled(int document_cookie) {
// Always need to stop the worker.
StopWorker(document_cookie);
@@ -214,6 +226,8 @@ bool PrintPreviewMessageHandler::OnMessageReceived(
OnPagesReadyForPreview)
IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed,
OnPrintPreviewFailed)
+ IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout,
+ OnDidGetDefaultPageLayout)
IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled,
OnPrintPreviewCancelled)
IPC_MESSAGE_UNHANDLED(handled = false)
diff --git a/chrome/browser/printing/print_preview_message_handler.h b/chrome/browser/printing/print_preview_message_handler.h
index 647e5b5..4b6fe89 100644
--- a/chrome/browser/printing/print_preview_message_handler.h
+++ b/chrome/browser/printing/print_preview_message_handler.h
@@ -14,6 +14,8 @@ struct PrintHostMsg_DidPreviewPage_Params;
namespace printing {
+struct PageSizeMargins;
+
// TabContents offloads print preview message handling to
// PrintPreviewMessageHandler. This object has the same life time as the
// TabContents that owns it.
@@ -32,6 +34,8 @@ class PrintPreviewMessageHandler : public TabContentsObserver {
// Message handlers.
void OnRequestPrintPreview();
+ void OnDidGetDefaultPageLayout(
+ const printing::PageSizeMargins& page_layout_in_points);
void OnDidGetPreviewPageCount(
const PrintHostMsg_DidGetPreviewPageCount_Params& params);
void OnDidPreviewPage(const PrintHostMsg_DidPreviewPage_Params& params);
diff --git a/chrome/browser/resources/print_preview/layout_settings.js b/chrome/browser/resources/print_preview/layout_settings.js
index d6894e6..43d0752 100644
--- a/chrome/browser/resources/print_preview/layout_settings.js
+++ b/chrome/browser/resources/print_preview/layout_settings.js
@@ -89,7 +89,7 @@ cr.define('print_preview', function() {
onLayoutButtonClick_: function() {
// If the chosen layout is same as before, nothing needs to be done.
if (this.hasChanged_())
- setDefaultValuesAndRegeneratePreview();
+ setDefaultValuesAndRegeneratePreview(true);
},
/**
diff --git a/chrome/browser/resources/print_preview/margin_settings.html b/chrome/browser/resources/print_preview/margin_settings.html
new file mode 100644
index 0000000..afd92d2
--- /dev/null
+++ b/chrome/browser/resources/print_preview/margin_settings.html
@@ -0,0 +1,8 @@
+<div id="margins-option" class="two-column option visible">
+ <h1 i18n-content="marginsLabel"></h1>
+ <select id="margin-list">
+ <option i18n-content="defaultMargins" value="0" selected></option>
+ <option i18n-content="noMargins" value="1"></option>
+ <option i18n-content="customMargins" value="2"></option>
+ </select>
+</div>
diff --git a/chrome/browser/resources/print_preview/margin_settings.js b/chrome/browser/resources/print_preview/margin_settings.js
new file mode 100644
index 0000000..f9d81a3
--- /dev/null
+++ b/chrome/browser/resources/print_preview/margin_settings.js
@@ -0,0 +1,121 @@
+// Copyright (c) 2011 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.
+
+cr.define('print_preview', function() {
+ 'use strict';
+
+ /**
+ * Creates a MarginSettings object. This object encapsulates all settings and
+ * logic related to the margins mode.
+ * @constructor
+ */
+ function MarginSettings() {
+ this.marginsOption_ = $('margins-option');
+ this.marginList_ = $('margin-list');
+ // Holds the custom left margin value (if set).
+ this.customMarginLeft_ = -1;
+ // Holds the custom right margin value (if set).
+ this.customMarginRight_ = -1;
+ // Holds the custom top margin value (if set).
+ this.customMarginTop_ = -1;
+ // Holds the custom bottom margin value (if set).
+ this.customMarginBottom_ = -1;
+ // Margin list values.
+ this.customMarginsValue_ = 2;
+ this.defaultMarginsValue_ = 0;
+ this.noMarginsValue_ = 1;
+ // Default Margins option index.
+ this.defaultMarginsIndex_ = 0;
+ }
+
+ cr.addSingletonGetter(MarginSettings);
+
+ MarginSettings.prototype = {
+ /**
+ * The selection list corresponding to the margins option.
+ * @return {HTMLInputElement}
+ */
+ get marginList() {
+ return this.marginList_;
+ },
+
+ /**
+ * Returns a dictionary of the four custom margin values.
+ * @return {object}
+ */
+ get customMargins() {
+ return {'marginLeft': this.customMarginLeft_,
+ 'marginTop': this.customMarginTop_,
+ 'marginRight': this.customMarginRight_,
+ 'marginBottom': this.customMarginBottom_};
+ },
+
+ /**
+ * Gets the value of the selected margin option.
+ * @private
+ * @return {number}
+ */
+ get selectedMarginsValue_() {
+ return this.marginList_.options[this.marginList_.selectedIndex].value;
+ },
+
+ /**
+ * Checks whether user has selected the Default Margins option or not.
+ *
+ * @return {boolean} true if default margins are selected.
+ */
+ isDefaultMarginsSelected: function() {
+ return this.selectedMarginsValue_ == this.defaultMarginsValue_;
+ },
+
+ /**
+ * Adds listeners to all margin related controls. The listeners take care
+ * of altering their behavior depending on |hasPendingPreviewRequest|.
+ */
+ addEventListeners: function() {
+ this.marginList_.onchange = this.onMarginsChanged_.bind(this);
+ document.addEventListener('PDFLoaded', this.onPDFLoaded_.bind(this));
+ },
+
+ /**
+ * Listener executing when user selects a different margin option, ie,
+ * |this.marginList_| is changed.
+ * @private
+ */
+ onMarginsChanged_: function() {
+ if (this.selectedMarginsValue_ == this.defaultMarginsValue_) {
+ setDefaultValuesAndRegeneratePreview(false);
+ } else if (this.selectedMarginsValue_ == this.noMarginsValue_) {
+ this.customMarginLeft_ = 0;
+ this.customMarginTop_ = 0;
+ this.customMarginRight_ = 0;
+ this.customMarginBottom_ = 0;
+ setDefaultValuesAndRegeneratePreview(false);
+ }
+ // TODO(aayushkumar): Add handler for custom margins
+ },
+
+ /**
+ * If custom margins is the currently selected option then change to the
+ * default margins option.
+ */
+ resetMarginsIfNeeded: function() {
+ if (this.selectedMarginsValue_ == this.customMarginsValue_)
+ this.marginList_.options[this.defaultMarginsIndex_].selected = true;
+ },
+
+ /**
+ * Listener executing when a PDFLoaded event occurs.
+ * @private
+ */
+ onPDFLoaded_: function() {
+ if (!previewModifiable)
+ fadeOutElement(this.marginsOption_);
+ }
+ };
+
+ return {
+ MarginSettings: MarginSettings,
+ };
+});
diff --git a/chrome/browser/resources/print_preview/print_preview.html b/chrome/browser/resources/print_preview/print_preview.html
index eeb0d89..c98f0b7 100644
--- a/chrome/browser/resources/print_preview/print_preview.html
+++ b/chrome/browser/resources/print_preview/print_preview.html
@@ -39,6 +39,10 @@
<hr>
<include src="color_settings.html"></include>
<hr>
+ <div id="display-margin-options" hidden>
+ <include src="margin_settings.html"></include>
+ <hr>
+ </div>
<include src="header_footer_settings.html"></include>
<hr>
<div id="system-dialog-div">
diff --git a/chrome/browser/resources/print_preview/print_preview.js b/chrome/browser/resources/print_preview/print_preview.js
index 124a567..0231ace 100644
--- a/chrome/browser/resources/print_preview/print_preview.js
+++ b/chrome/browser/resources/print_preview/print_preview.js
@@ -51,6 +51,9 @@ var copiesSettings;
// Object holding all the layout related settings.
var layoutSettings;
+// Object holding all the margin related settings.
+var marginSettings;
+
// Object holding all the header footer related settings.
var headerFooterSettings;
@@ -107,6 +110,7 @@ function onLoad() {
pageSettings = print_preview.PageSettings.getInstance();
copiesSettings = print_preview.CopiesSettings.getInstance();
layoutSettings = print_preview.LayoutSettings.getInstance();
+ marginSettings = print_preview.MarginSettings.getInstance();
headerFooterSettings = print_preview.HeaderFooterSettings.getInstance();
colorSettings = print_preview.ColorSettings.getInstance();
printHeader.addEventListeners();
@@ -114,6 +118,7 @@ function onLoad() {
copiesSettings.addEventListeners();
headerFooterSettings.addEventListeners();
layoutSettings.addEventListeners();
+ marginSettings.addEventListeners();
colorSettings.addEventListeners();
$('printer-list').onchange = updateControlsWithSelectedPrinterCapabilities;
@@ -224,7 +229,7 @@ function updateControlsWithSelectedPrinterCapabilities() {
lastSelectedPrinterIndex = selectedIndex;
// Regenerate the preview data based on selected printer settings.
- setDefaultValuesAndRegeneratePreview();
+ setDefaultValuesAndRegeneratePreview(true);
}
}
@@ -255,7 +260,7 @@ function doUpdateCloudPrinterCapabilities(printer) {
lastSelectedPrinterIndex = selectedIndex;
// Regenerate the preview data based on selected printer settings.
- setDefaultValuesAndRegeneratePreview();
+ setDefaultValuesAndRegeneratePreview(true);
}
/**
@@ -325,6 +330,8 @@ function getSettings() {
'printToPDF': printToPDF,
'isFirstRequest' : false,
'headerFooterEnabled': headerFooterSettings.hasHeaderFooter(),
+ 'defaultMarginsSelected': marginSettings.isDefaultMarginsSelected(),
+ 'margins': marginSettings.customMargins,
'requestID': -1};
var printerList = $('printer-list');
@@ -837,6 +844,10 @@ function onDidGetPreviewPageCount(pageCount, isModifiable, previewResponseId,
cr.dispatchSimpleEvent(document, 'updateSummary');
}
+function onDidGetDefaultPageLayout(pageLayout) {
+ // TODO(aayushkumar): Do something here!
+}
+
/**
* Called when no pipelining previewed pages.
* @param {string} previewUid Preview unique identifier.
@@ -956,8 +967,11 @@ window.addEventListener('DOMContentLoaded', onLoad);
/**
* Sets the default values and sends a request to regenerate preview data.
+ * Resets the margin options only if |resetMargins| is true.
*/
-function setDefaultValuesAndRegeneratePreview() {
+function setDefaultValuesAndRegeneratePreview(resetMargins) {
+ if (resetMargins)
+ marginSettings.resetMarginsIfNeeded();
pageSettings.resetState();
requestPrintPreview();
}
@@ -972,3 +986,4 @@ function setDefaultValuesAndRegeneratePreview() {
<include src="header_footer_settings.js"/>
<include src="layout_settings.js"/>
<include src="color_settings.js"/>
+<include src="margin_settings.js"/>
diff --git a/chrome/browser/ui/webui/print_preview_data_source.cc b/chrome/browser/ui/webui/print_preview_data_source.cc
index 084f904..ed9bf95 100644
--- a/chrome/browser/ui/webui/print_preview_data_source.cc
+++ b/chrome/browser/ui/webui/print_preview_data_source.cc
@@ -98,6 +98,10 @@ PrintPreviewDataSource::PrintPreviewDataSource()
AddLocalizedString("optionsLabel", IDS_PRINT_PREVIEW_OPTIONS_LABEL);
AddLocalizedString("optionHeaderFooter",
IDS_PRINT_PREVIEW_OPTION_HEADER_FOOTER);
+ AddLocalizedString("marginsLabel", IDS_PRINT_PREVIEW_MARGINS_LABEL);
+ AddLocalizedString("defaultMargins", IDS_PRINT_PREVIEW_DEFAULT_MARGINS);
+ AddLocalizedString("noMargins", IDS_PRINT_PREVIEW_NO_MARGINS);
+ AddLocalizedString("customMargins", IDS_PRINT_PREVIEW_CUSTOM_MARGINS);
set_json_path("strings.js");
add_resource_path("print_preview.js", IDR_PRINT_PREVIEW_JS);
diff --git a/chrome/browser/ui/webui/print_preview_ui.cc b/chrome/browser/ui/webui/print_preview_ui.cc
index 64a7d58..7e087e67 100644
--- a/chrome/browser/ui/webui/print_preview_ui.cc
+++ b/chrome/browser/ui/webui/print_preview_ui.cc
@@ -17,8 +17,11 @@
#include "chrome/browser/ui/webui/print_preview_handler.h"
#include "chrome/common/print_messages.h"
#include "content/browser/tab_contents/tab_contents.h"
+#include "printing/page_size_margins.h"
#include "printing/print_job_constants.h"
+using printing::PageSizeMargins;
+
namespace {
// Thread-safe wrapper around a std::map to keep track of mappings from
@@ -154,6 +157,26 @@ void PrintPreviewUI::OnDidGetPreviewPageCount(
request_id, title);
}
+void PrintPreviewUI::OnDidGetDefaultPageLayout(
+ const PageSizeMargins& page_layout) {
+ 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) {
+ NOTREACHED();
+ return;
+ }
+
+ base::DictionaryValue layout;
+ layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top);
+ layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left);
+ layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom);
+ layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right);
+ layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width);
+ layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height);
+
+ CallJavascriptFunction("onDidGetDefaultPageLayout", layout);
+}
+
void PrintPreviewUI::OnDidPreviewPage(int page_number,
int preview_request_id) {
DCHECK_GE(page_number, 0);
diff --git a/chrome/browser/ui/webui/print_preview_ui.h b/chrome/browser/ui/webui/print_preview_ui.h
index d90eddd..8778c9b 100644
--- a/chrome/browser/ui/webui/print_preview_ui.h
+++ b/chrome/browser/ui/webui/print_preview_ui.h
@@ -18,6 +18,10 @@ class PrintPreviewDataService;
class PrintPreviewHandler;
struct PrintHostMsg_DidGetPreviewPageCount_Params;
+namespace printing {
+struct PageSizeMargins;
+}
+
class PrintPreviewUI : public ChromeWebUI {
public:
explicit PrintPreviewUI(TabContents* contents);
@@ -58,6 +62,11 @@ class PrintPreviewUI : public ChromeWebUI {
void OnDidGetPreviewPageCount(
const PrintHostMsg_DidGetPreviewPageCount_Params& params);
+ // Notifies the Web UI of the default page layout according to the currently
+ // selected printer and page size.
+ void OnDidGetDefaultPageLayout(
+ const printing::PageSizeMargins& page_layout);
+
// Notifies the Web UI that the 0-based page |page_number| has been rendered.
// |preview_request_id| indicates wich request resulted in this response.
void OnDidPreviewPage(int page_number, int preview_request_id);
diff --git a/chrome/common/print_messages.h b/chrome/common/print_messages.h
index a3714ea..cfb9719 100644
--- a/chrome/common/print_messages.h
+++ b/chrome/common/print_messages.h
@@ -11,6 +11,7 @@
#include "base/values.h"
#include "base/shared_memory.h"
#include "ipc/ipc_message_macros.h"
+#include "printing/page_size_margins.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/gfx/rect.h"
@@ -129,6 +130,15 @@ IPC_STRUCT_BEGIN(PrintMsg_PrintPage_Params)
IPC_STRUCT_MEMBER(int, page_number)
IPC_STRUCT_END()
+IPC_STRUCT_TRAITS_BEGIN(printing::PageSizeMargins)
+ IPC_STRUCT_TRAITS_MEMBER(content_width)
+ IPC_STRUCT_TRAITS_MEMBER(content_height)
+ IPC_STRUCT_TRAITS_MEMBER(margin_left)
+ IPC_STRUCT_TRAITS_MEMBER(margin_right)
+ IPC_STRUCT_TRAITS_MEMBER(margin_top)
+ IPC_STRUCT_TRAITS_MEMBER(margin_bottom)
+IPC_STRUCT_TRAITS_END()
+
IPC_STRUCT_TRAITS_BEGIN(PrintMsg_PrintPages_Params)
// Parameters to render the page as a printed page. It must always be the same
// value for all the document.
@@ -337,6 +347,11 @@ IPC_MESSAGE_ROUTED0(PrintHostMsg_RequestPrintPreview)
IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetPreviewPageCount,
PrintHostMsg_DidGetPreviewPageCount_Params /* params */)
+// Notify the browser of the default page layout according to the currently
+// selected printer and page size.
+IPC_MESSAGE_ROUTED1(PrintHostMsg_DidGetDefaultPageLayout,
+ printing::PageSizeMargins /* page layout in points */)
+
// Notify the browser a print preview page has been rendered.
IPC_MESSAGE_ROUTED1(PrintHostMsg_DidPreviewPage,
PrintHostMsg_DidPreviewPage_Params /* params */)
diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc
index 67250e9..c1833fa 100644
--- a/chrome/renderer/print_web_view_helper.cc
+++ b/chrome/renderer/print_web_view_helper.cc
@@ -23,6 +23,7 @@
#include "content/renderer/render_view.h"
#include "grit/generated_resources.h"
#include "printing/metafile_impl.h"
+#include "printing/page_size_margins.h"
#include "printing/print_job_constants.h"
#include "printing/units.h"
#include "third_party/skia/include/core/SkRect.h"
@@ -58,9 +59,11 @@ using base::mac::ScopedCFTypeRef;
#endif
using printing::ConvertPixelsToPoint;
using printing::ConvertPixelsToPointDouble;
+using printing::ConvertPointsToPixelDouble;
using printing::ConvertUnit;
using printing::ConvertUnitDouble;
using printing::GetHeaderFooterSegmentWidth;
+using printing::PageSizeMargins;
using WebKit::WebConsoleMessage;
using WebKit::WebDocument;
using WebKit::WebElement;
@@ -104,18 +107,23 @@ bool PrintMsg_Print_Params_IsEmpty(const PrintMsg_Print_Params& params) {
!params.supports_alpha_blend;
}
+bool PageLayoutIsEqual(const PrintMsg_PrintPages_Params& oldParams,
+ const PrintMsg_PrintPages_Params& newParams) {
+ return oldParams.params.printable_size == newParams.params.printable_size &&
+ oldParams.params.page_size == newParams.params.page_size &&
+ oldParams.params.margin_top == newParams.params.margin_top &&
+ oldParams.params.margin_left == newParams.params.margin_left &&
+ oldParams.params.desired_dpi == newParams.params.desired_dpi &&
+ oldParams.params.dpi == newParams.params.dpi;
+}
+
bool PrintMsg_Print_Params_IsEqual(
const PrintMsg_PrintPages_Params& oldParams,
const PrintMsg_PrintPages_Params& newParams) {
- return oldParams.params.desired_dpi == newParams.params.desired_dpi &&
+ return PageLayoutIsEqual(oldParams, newParams) &&
oldParams.params.max_shrink == newParams.params.max_shrink &&
oldParams.params.min_shrink == newParams.params.min_shrink &&
- oldParams.params.dpi == newParams.params.dpi &&
- oldParams.params.printable_size == newParams.params.printable_size &&
oldParams.params.selection_only == newParams.params.selection_only &&
- oldParams.params.page_size == newParams.params.page_size &&
- oldParams.params.margin_top == newParams.params.margin_top &&
- oldParams.params.margin_left == newParams.params.margin_left &&
oldParams.params.supports_alpha_blend ==
newParams.params.supports_alpha_blend &&
oldParams.pages.size() == newParams.pages.size() &&
@@ -138,6 +146,77 @@ void CalculatePrintCanvasSize(const PrintMsg_Print_Params& print_params,
print_params.desired_dpi));
}
+// Get the margins option selected and set custom margins appropriately.
+void SetCustomMarginsIfSelected(const DictionaryValue& job_settings,
+ PrintMsg_PrintPages_Params* settings) {
+ bool default_margins_selected;
+ if (!job_settings.GetBoolean(printing::kSettingDefaultMarginsSelected,
+ &default_margins_selected)) {
+ NOTREACHED();
+ default_margins_selected = true;
+ }
+
+ if (default_margins_selected)
+ return;
+
+ DictionaryValue* custom_margins;
+ if (!job_settings.GetDictionary(printing::kSettingMargins,
+ &custom_margins)) {
+ NOTREACHED();
+ return;
+ }
+
+ double custom_margin_top_in_points = 0;
+ double custom_margin_left_in_points = 0;
+ double custom_margin_right_in_points = 0;
+ double custom_margin_bottom_in_points = 0;
+ if (!custom_margins->GetDouble(printing::kSettingMarginTop,
+ &custom_margin_top_in_points) ||
+ !custom_margins->GetDouble(printing::kSettingMarginLeft,
+ &custom_margin_left_in_points) ||
+ !custom_margins->GetDouble(printing::kSettingMarginRight,
+ &custom_margin_right_in_points) ||
+ !custom_margins->GetDouble(printing::kSettingMarginBottom,
+ &custom_margin_bottom_in_points)) {
+ NOTREACHED();
+ return;
+ }
+
+ int dpi = GetDPI(&settings->params);
+ double custom_margin_top_in_dots = ConvertUnitDouble(
+ custom_margin_top_in_points, printing::kPointsPerInch, dpi);
+ double custom_margin_left_in_dots = ConvertUnitDouble(
+ custom_margin_left_in_points, printing::kPointsPerInch, dpi);
+ double custom_margin_right_in_dots = ConvertUnitDouble(
+ custom_margin_right_in_points, printing::kPointsPerInch, dpi);
+ double custom_margin_bottom_in_dots = ConvertUnitDouble(
+ custom_margin_bottom_in_points, printing::kPointsPerInch, dpi);
+
+
+ if (custom_margin_left_in_dots < 0 || custom_margin_right_in_dots < 0 ||
+ custom_margin_top_in_dots < 0 || custom_margin_bottom_in_dots < 0) {
+ NOTREACHED();
+ return;
+ }
+
+ if (settings->params.page_size.width() < custom_margin_left_in_dots +
+ custom_margin_right_in_dots ||
+ settings->params.page_size.height() < custom_margin_top_in_dots +
+ custom_margin_bottom_in_dots) {
+ NOTREACHED();
+ return;
+ }
+
+ settings->params.margin_top = custom_margin_top_in_dots;
+ settings->params.margin_left = custom_margin_left_in_dots;
+ settings->params.printable_size.set_width(
+ settings->params.page_size.width() - custom_margin_right_in_dots -
+ custom_margin_left_in_dots);
+ settings->params.printable_size.set_height(
+ settings->params.page_size.height() - custom_margin_bottom_in_dots -
+ custom_margin_top_in_dots);
+}
+
// Get the (x, y) coordinate from where printing of the current text should
// start depending on the horizontal alignment (LEFT, RIGHT, CENTER) and
// vertical alignment (TOP, BOTTOM).
@@ -982,6 +1061,17 @@ bool PrintWebViewHelper::UpdatePrintSettings(
if (settings.params.dpi < kMinDpi || !settings.params.document_cookie)
return false;
+ // Send default page layout to browser process.
+ PageSizeMargins default_page_layout;
+ GetPageSizeAndMarginsInPoints(NULL, -1, settings.params,
+ &default_page_layout);
+ if (!old_print_pages_params_.get() ||
+ !PageLayoutIsEqual(*old_print_pages_params_, settings)) {
+ Send(new PrintHostMsg_DidGetDefaultPageLayout(routing_id(),
+ default_page_layout));
+ }
+ SetCustomMarginsIfSelected(job_settings, &settings);
+
if (!job_settings.GetString(printing::kPreviewUIAddr,
&(settings.params.preview_ui_addr)) ||
!job_settings.GetInteger(printing::kPreviewRequestID,
diff --git a/chrome/renderer/print_web_view_helper.h b/chrome/renderer/print_web_view_helper.h
index 2dc20fb..1f5e948 100644
--- a/chrome/renderer/print_web_view_helper.h
+++ b/chrome/renderer/print_web_view_helper.h
@@ -29,6 +29,9 @@ struct PrintMsg_PrintPages_Params;
namespace base {
class DictionaryValue;
}
+namespace printing {
+struct PageSizeMargins;
+}
#if defined(USE_SKIA)
namespace skia {
class VectorCanvas;
@@ -81,16 +84,6 @@ class PrepareFrameAndViewForPrint {
DISALLOW_COPY_AND_ASSIGN(PrepareFrameAndViewForPrint);
};
-// Struct that holds margin and content area sizes of a page in points.
-struct PageSizeMargins {
- double content_width;
- double content_height;
- double margin_top;
- double margin_right;
- double margin_bottom;
- double margin_left;
-};
-
// PrintWebViewHelper handles most of the printing grunt work for RenderView.
// We plan on making print asynchronous and that will require copying the DOM
// of the document and creating a new WebView with the contents.
@@ -252,7 +245,7 @@ class PrintWebViewHelper : public RenderViewObserver,
WebKit::WebFrame* frame,
int page_index,
const PrintMsg_Print_Params& default_params,
- PageSizeMargins* page_layout_in_points);
+ printing::PageSizeMargins* page_layout_in_points);
static void UpdatePrintableSizeInPrintParameters(
WebKit::WebFrame* frame,
@@ -267,7 +260,7 @@ class PrintWebViewHelper : public RenderViewObserver,
int page_number,
int total_pages,
float webkit_scale_factor,
- const PageSizeMargins& page_layout_in_points,
+ const printing::PageSizeMargins& page_layout_in_points,
const base::DictionaryValue& header_footer_info);
bool GetPrintFrame(WebKit::WebFrame** frame);
diff --git a/chrome/renderer/print_web_view_helper_browsertest.cc b/chrome/renderer/print_web_view_helper_browsertest.cc
index 6df060c..372123f 100644
--- a/chrome/renderer/print_web_view_helper_browsertest.cc
+++ b/chrome/renderer/print_web_view_helper_browsertest.cc
@@ -48,6 +48,7 @@ void CreatePrintSettingsDictionary(DictionaryValue* dict) {
dict->SetString(printing::kPreviewUIAddr, "0xb33fbeef");
dict->SetInteger(printing::kPreviewRequestID, 12345);
dict->SetBoolean(printing::kIsFirstRequest, true);
+ dict->SetBoolean(printing::kSettingDefaultMarginsSelected, true);
dict->SetBoolean(printing::kSettingHeaderFooterEnabled, false);
}
diff --git a/chrome/renderer/print_web_view_helper_linux.cc b/chrome/renderer/print_web_view_helper_linux.cc
index 3a72edd..6ece1a0 100644
--- a/chrome/renderer/print_web_view_helper_linux.cc
+++ b/chrome/renderer/print_web_view_helper_linux.cc
@@ -13,6 +13,7 @@
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
#include "printing/metafile_skia_wrapper.h"
+#include "printing/page_size_margins.h"
#include "skia/ext/vector_canvas.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
@@ -180,7 +181,7 @@ void PrintWebViewHelper::PrintPageInternal(
const gfx::Size& canvas_size,
WebFrame* frame,
printing::Metafile* metafile) {
- PageSizeMargins page_layout_in_points;
+ printing::PageSizeMargins page_layout_in_points;
GetPageSizeAndMarginsInPoints(frame, params.page_number, params.params,
&page_layout_in_points);
diff --git a/chrome/renderer/print_web_view_helper_mac.mm b/chrome/renderer/print_web_view_helper_mac.mm
index 73c7bbf..93509d7 100644
--- a/chrome/renderer/print_web_view_helper_mac.mm
+++ b/chrome/renderer/print_web_view_helper_mac.mm
@@ -12,6 +12,7 @@
#include "chrome/common/print_messages.h"
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
+#include "printing/page_size_margins.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
#if defined(USE_SKIA)
@@ -147,7 +148,7 @@ void PrintWebViewHelper::RenderPage(
CGContextRef canvas_ptr = cgContext;
#endif
- PageSizeMargins page_layout_in_points;
+ printing::PageSizeMargins page_layout_in_points;
GetPageSizeAndMarginsInPoints(frame, page_number,
print_pages_params_->params,
&page_layout_in_points);
diff --git a/chrome/renderer/print_web_view_helper_win.cc b/chrome/renderer/print_web_view_helper_win.cc
index dec1ddd..7d18e3d 100644
--- a/chrome/renderer/print_web_view_helper_win.cc
+++ b/chrome/renderer/print_web_view_helper_win.cc
@@ -12,6 +12,7 @@
#include "printing/metafile.h"
#include "printing/metafile_impl.h"
#include "printing/metafile_skia_wrapper.h"
+#include "printing/page_size_margins.h"
#include "printing/units.h"
#include "skia/ext/vector_canvas.h"
#include "skia/ext/platform_device.h"
@@ -149,7 +150,7 @@ bool PrintWebViewHelper::RenderPreviewPage(int page_number) {
Metafile* PrintWebViewHelper::RenderPage(
const PrintMsg_Print_Params& params, float* scale_factor, int page_number,
bool is_preview, WebFrame* frame, Metafile* metafile) {
- PageSizeMargins page_layout_in_points;
+ printing::PageSizeMargins page_layout_in_points;
GetPageSizeAndMarginsInPoints(frame, page_number, params,
&page_layout_in_points);
diff --git a/printing/page_size_margins.h b/printing/page_size_margins.h
new file mode 100644
index 0000000..4f74c297
--- /dev/null
+++ b/printing/page_size_margins.h
@@ -0,0 +1,24 @@
+// Copyright (c) 2011 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.
+
+#ifndef PRINTING_PAGE_SIZE_MARGINS_H_
+#define PRINTING_PAGE_SIZE_MARGINS_H_
+
+namespace printing {
+
+// Struct that holds margin and content area sizes of a page. Units are
+// arbitrary and can be chosen by the programmer.
+struct PageSizeMargins {
+ double content_width;
+ double content_height;
+ double margin_top;
+ double margin_right;
+ double margin_bottom;
+ double margin_left;
+};
+
+} // namespace printing
+
+#endif // PRINTING_PAGE_SIZE_MARGINS_H_
+
diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc
index b1928e5..5e7863e 100644
--- a/printing/print_job_constants.cc
+++ b/printing/print_job_constants.cc
@@ -24,9 +24,19 @@ const char kSettingCollate[] = "collate";
// Print out color: true for color, false for grayscale.
const char kSettingColor[] = "color";
+// Key that specifies the height of the content area of the page.
+const char kSettingContentHeight[] = "contentHeight";
+
+// Key that specifies the width of the content area of the page.
+const char kSettingContentWidth[] = "contentWidth";
+
// Number of copies.
const char kSettingCopies[] = "copies";
+// Key that specifies if the default margins have been selected or not.
+// True if selected, false if not.
+const char kSettingDefaultMarginsSelected[] = "defaultMarginsSelected";
+
// Device name: Unique printer identifier.
const char kSettingDeviceName[] = "deviceName";
@@ -71,6 +81,21 @@ const char kSettingHeaderFooterURL[] = "url";
// Page orientation: true for landscape, false for portrait.
const char kSettingLandscape[] = "landscape";
+// Key that specifies the bottom margin of the page.
+const char kSettingMarginBottom[] = "marginBottom";
+
+// Key that specifies the left margin of the page.
+const char kSettingMarginLeft[] = "marginLeft";
+
+// Key that specifies the right margin of the page.
+const char kSettingMarginRight[] = "marginRight";
+
+// Key that specifies the top margin of the page.
+const char kSettingMarginTop[] = "marginTop";
+
+// Key that specifies the array of custom margins as set by the user.
+const char kSettingMargins[] = "margins";
+
// A page range.
const char kSettingPageRange[] = "pageRange";
diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h
index e451189..5739ab9 100644
--- a/printing/print_job_constants.h
+++ b/printing/print_job_constants.h
@@ -13,7 +13,10 @@ extern const char kPreviewUIAddr[];
extern const char kSettingCloudPrintId[];
extern const char kSettingCollate[];
extern const char kSettingColor[];
+extern const char kSettingContentHeight[];
+extern const char kSettingContentWidth[];
extern const char kSettingCopies[];
+extern const char kSettingDefaultMarginsSelected[];
extern const char kSettingDeviceName[];
extern const char kSettingDuplexMode[];
extern const char kSettingHeaderFooterEnabled[];
@@ -27,6 +30,11 @@ extern const char kSettingHeaderFooterDate[];
extern const char kSettingHeaderFooterTitle[];
extern const char kSettingHeaderFooterURL[];
extern const char kSettingLandscape[];
+extern const char kSettingMarginBottom[];
+extern const char kSettingMarginLeft[];
+extern const char kSettingMarginRight[];
+extern const char kSettingMarginTop[];
+extern const char kSettingMargins[];
extern const char kSettingPageRange[];
extern const char kSettingPageRangeFrom[];
extern const char kSettingPageRangeTo[];
diff --git a/printing/printing.gyp b/printing/printing.gyp
index 4f5483f..0b13a00 100644
--- a/printing/printing.gyp
+++ b/printing/printing.gyp
@@ -46,6 +46,7 @@
'page_range.h',
'page_setup.cc',
'page_setup.h',
+ 'page_size_margins.h',
'pdf_metafile_cairo_linux.cc',
'pdf_metafile_cairo_linux.h',
'pdf_metafile_cg_mac.cc',