Add options to custom print settings in printToPDF API.
authorHaojian Wu <hokein.wu@gmail.com>
Sun, 31 May 2015 06:52:41 +0000 (14:52 +0800)
committerHaojian Wu <hokein.wu@gmail.com>
Tue, 16 Jun 2015 12:02:24 +0000 (20:02 +0800)
atom/browser/api/atom_api_window.cc
atom/browser/api/atom_api_window.h
atom/browser/native_window.cc
atom/browser/native_window.h
chromium_src/chrome/browser/printing/print_preview_message_handler.cc
chromium_src/chrome/browser/printing/print_preview_message_handler.h

index 00a0bb4..66f1702 100644 (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) {
index 606fda9..828ab2a 100644 (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);
index 9b922dc..f7616f4 100644 (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() {
index 5c99a2c..dd10dd1 100644 (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();
index c5b5954..bb960b2 100644 (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));
index 8f9f1b4..38943b0 100644 (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>;