Add options to custom print settings in printToPDF API.

This commit is contained in:
Haojian Wu 2015-05-31 14:52:41 +08:00
parent 9cf9229308
commit ce8bbb689c
6 changed files with 40 additions and 11 deletions

View file

@ -16,6 +16,7 @@
#include "native_mate/callback.h"
#include "native_mate/constructor.h"
#include "native_mate/dictionary.h"
#include "printing/print_job_constants.h"
#include "ui/gfx/geometry/rect.h"
#include "atom/common/node_includes.h"
@ -430,8 +431,13 @@ void Window::Print(mate::Arguments* args) {
window_->Print(settings.silent, settings.print_background);
}
void Window::PrintToPDF() {
window_->PrintToPDF();
void Window::PrintToPDF(mate::Arguments* args) {
mate::Dictionary options;
if (args->Length() == 1 && !args->GetNext(&options)) {
args->ThrowError();
return;
}
window_->PrintToPDF(options);
}
void Window::SetProgressBar(double progress) {

View file

@ -132,7 +132,7 @@ class Window : public mate::EventEmitter,
bool IsDocumentEdited();
void CapturePage(mate::Arguments* args);
void Print(mate::Arguments* args);
void PrintToPDF();
void PrintToPDF(mate::Arguments* args);
void SetProgressBar(double progress);
void SetOverlayIcon(const gfx::Image& overlay,
const std::string& description);

View file

@ -265,9 +265,9 @@ void NativeWindow::Print(bool silent, bool print_background) {
PrintNow(silent, print_background);
}
void NativeWindow::PrintToPDF() {
void NativeWindow::PrintToPDF(const mate::Dictionary& options) {
printing::PrintPreviewMessageHandler::FromWebContents(GetWebContents())->
HandleGetPreview(NULL);
HandleGetPreview(options);
}
void NativeWindow::ShowDefinitionForSelection() {

View file

@ -158,7 +158,7 @@ class NativeWindow : public CommonWebContentsDelegate,
virtual void Print(bool silent, bool print_background);
// Print current page as PDF.
virtual void PrintToPDF();
virtual void PrintToPDF(const mate::Dictionary& options);
// Show popup dictionary.
virtual void ShowDefinitionForSelection();

View file

@ -199,7 +199,8 @@ bool PrintPreviewMessageHandler::OnMessageReceived(
return handled;
}
void PrintPreviewMessageHandler::HandleGetPreview(const base::ListValue* args) {
void PrintPreviewMessageHandler::HandleGetPreview(
const mate::Dictionary& options) {
static int request_id = 0;
request_id++;
// A simulated Chromium print preivew setting.
@ -212,7 +213,7 @@ void PrintPreviewMessageHandler::HandleGetPreview(const base::ListValue* args) {
\"width_microns\":210000, \
\"custom_display_name\":\"A4\" \
}, \
\"landscape\":true, \
\"landscape\":false, \
\"color\":2, \
\"headerFooterEnabled\":false, \
\"marginsType\":0, \
@ -229,7 +230,7 @@ void PrintPreviewMessageHandler::HandleGetPreview(const base::ListValue* args) {
\"duplex\":0, \
\"copies\":1, \
\"collate\":true, \
\"shouldPrintBackgrounds\":true, \
\"shouldPrintBackgrounds\":false, \
\"shouldPrintSelectionOnly\":false \
}";
@ -237,7 +238,26 @@ void PrintPreviewMessageHandler::HandleGetPreview(const base::ListValue* args) {
static_cast<base::DictionaryValue*>(
base::JSONReader::Read(setting_json_str)));
settings->SetInteger(printing::kPreviewRequestID, request_id);
// Default Print PDF settings:
int margins_type = 0; // DEFAULT_MARGINS
bool print_background = false;
bool print_selection_only = false;
bool is_landscape = false; // layout: true for portrait, false for landscape
if (!options.IsEmpty()) {
options.Get(printing::kSettingMarginsType, &margins_type);
options.Get(printing::kSettingShouldPrintBackgrounds, &print_background);
options.Get(printing::kSettingShouldPrintSelectionOnly,
&print_selection_only);
std::string layout;
options.Get("layout", &is_landscape);
}
settings->SetInteger(printing::kSettingMarginsType, margins_type);
settings->SetBoolean(printing::kSettingShouldPrintBackgrounds,
print_background);
settings->SetBoolean(printing::kSettingShouldPrintSelectionOnly,
print_selection_only);
settings->SetBoolean(printing::kSettingLandscape, is_landscape);
LOG(ERROR) << "Print preview request start";
content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
rvh->Send(new PrintMsg_PrintPreview(rvh->GetRoutingID(), *settings));

View file

@ -17,6 +17,10 @@ namespace content {
class WebContents;
}
namespace mate {
class Dictionary;
}
namespace gfx {
class Rect;
}
@ -37,8 +41,7 @@ class PrintPreviewMessageHandler
// Asks the initiator renderer to generate a preview. First element of |args|
// is a job settings JSON string.
void HandleGetPreview(const base::ListValue* args);
void HandleGetPreview(const mate::Dictionary& options);
private:
explicit PrintPreviewMessageHandler(content::WebContents* web_contents);
friend class content::WebContentsUserData<PrintPreviewMessageHandler>;