Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / printing / printing_context_win.h
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef PRINTING_PRINTING_CONTEXT_WIN_H_
6 #define PRINTING_PRINTING_CONTEXT_WIN_H_
7
8 #include <ocidl.h>
9 #include <commdlg.h>
10
11 #include <string>
12
13 #include "base/memory/scoped_ptr.h"
14 #include "build/build_config.h"
15 #include "printing/printing_context.h"
16 #include "ui/gfx/native_widget_types.h"
17
18 namespace printing {
19
20 class PRINTING_EXPORT PrintingContextWin : public PrintingContext {
21  public:
22   explicit PrintingContextWin(const std::string& app_locale);
23   ~PrintingContextWin();
24
25   // PrintingContext implementation.
26   virtual void AskUserForSettings(
27       gfx::NativeView parent_view,
28       int max_pages,
29       bool has_selection,
30       const PrintSettingsCallback& callback) OVERRIDE;
31   virtual Result UseDefaultSettings() OVERRIDE;
32   virtual gfx::Size GetPdfPaperSizeDeviceUnits() OVERRIDE;
33   virtual Result UpdatePrinterSettings(bool external_preview) OVERRIDE;
34   virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE;
35   virtual Result NewDocument(const base::string16& document_name) OVERRIDE;
36   virtual Result NewPage() OVERRIDE;
37   virtual Result PageDone() OVERRIDE;
38   virtual Result DocumentDone() OVERRIDE;
39   virtual void Cancel() OVERRIDE;
40   virtual void ReleaseContext() OVERRIDE;
41   virtual gfx::NativeDrawingContext context() const OVERRIDE;
42
43 #if defined(UNIT_TEST) || defined(PRINTING_IMPLEMENTATION)
44   // Sets a fake PrintDlgEx function pointer in tests.
45   void SetPrintDialog(HRESULT (__stdcall *print_dialog_func)(LPPRINTDLGEX)) {
46     print_dialog_func_ = print_dialog_func;
47   }
48 #endif  // defined(UNIT_TEST)
49
50   // Allocates the HDC for a specific DEVMODE.
51   static bool AllocateContext(const std::wstring& printer_name,
52                               const DEVMODE* dev_mode,
53                               gfx::NativeDrawingContext* context);
54
55  private:
56   // Class that manages the PrintDlgEx() callbacks. This is meant to be a
57   // temporary object used during the Print... dialog display.
58   class CallbackHandler;
59
60   // Used in response to the user canceling the printing.
61   static BOOL CALLBACK AbortProc(HDC hdc, int nCode);
62
63   // Reads the settings from the selected device context. Updates settings_ and
64   // its margins.
65   bool InitializeSettings(const DEVMODE& dev_mode,
66                           const std::wstring& new_device_name,
67                           const PRINTPAGERANGE* ranges,
68                           int number_ranges,
69                           bool selection_only);
70
71   // Retrieves the printer's default low-level settings. On Windows, context_ is
72   // allocated with this call.
73   bool GetPrinterSettings(HANDLE printer,
74                           const std::wstring& device_name);
75
76   // Parses the result of a PRINTDLGEX result.
77   Result ParseDialogResultEx(const PRINTDLGEX& dialog_options);
78   Result ParseDialogResult(const PRINTDLG& dialog_options);
79
80   // The selected printer context.
81   HDC context_;
82
83   // The dialog box for the time it is shown.
84   volatile HWND dialog_box_;
85
86   // Function pointer that defaults to PrintDlgEx. It can be changed using
87   // SetPrintDialog() in tests.
88   HRESULT (__stdcall *print_dialog_func_)(LPPRINTDLGEX);
89
90   DISALLOW_COPY_AND_ASSIGN(PrintingContextWin);
91 };
92
93 }  // namespace printing
94
95 #endif  // PRINTING_PRINTING_CONTEXT_WIN_H_