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
18namespace printing {
19
20class 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 Result UpdatePrinterSettings(
33      const base::DictionaryValue& job_settings,
34      const PageRanges& ranges) OVERRIDE;
35  virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE;
36  virtual Result NewDocument(const string16& document_name) OVERRIDE;
37  virtual Result NewPage() OVERRIDE;
38  virtual Result PageDone() OVERRIDE;
39  virtual Result DocumentDone() OVERRIDE;
40  virtual void Cancel() OVERRIDE;
41  virtual void ReleaseContext() OVERRIDE;
42  virtual gfx::NativeDrawingContext context() const OVERRIDE;
43
44#if defined(UNIT_TEST) || defined(PRINTING_IMPLEMENTATION)
45  // Sets a fake PrintDlgEx function pointer in tests.
46  void SetPrintDialog(HRESULT (__stdcall *print_dialog_func)(LPPRINTDLGEX)) {
47    print_dialog_func_ = print_dialog_func;
48  }
49#endif  // defined(UNIT_TEST)
50
51  // Allocates the HDC for a specific DEVMODE.
52  static bool AllocateContext(const std::wstring& printer_name,
53                              const DEVMODE* dev_mode,
54                              gfx::NativeDrawingContext* context);
55
56 private:
57  // Class that manages the PrintDlgEx() callbacks. This is meant to be a
58  // temporary object used during the Print... dialog display.
59  class CallbackHandler;
60
61  // Used in response to the user canceling the printing.
62  static BOOL CALLBACK AbortProc(HDC hdc, int nCode);
63
64  // Reads the settings from the selected device context. Updates settings_ and
65  // its margins.
66  bool InitializeSettings(const DEVMODE& dev_mode,
67                          const std::wstring& new_device_name,
68                          const PRINTPAGERANGE* ranges,
69                          int number_ranges,
70                          bool selection_only);
71
72  // Retrieves the printer's default low-level settings. On Windows, context_ is
73  // allocated with this call.
74  bool GetPrinterSettings(HANDLE printer,
75                          const std::wstring& device_name);
76
77  // Parses the result of a PRINTDLGEX result.
78  Result ParseDialogResultEx(const PRINTDLGEX& dialog_options);
79  Result ParseDialogResult(const PRINTDLG& dialog_options);
80
81  // The selected printer context.
82  HDC context_;
83
84  // The dialog box for the time it is shown.
85  volatile HWND dialog_box_;
86
87  // Function pointer that defaults to PrintDlgEx. It can be changed using
88  // SetPrintDialog() in tests.
89  HRESULT (__stdcall *print_dialog_func_)(LPPRINTDLGEX);
90
91  DISALLOW_COPY_AND_ASSIGN(PrintingContextWin);
92};
93
94}  // namespace printing
95
96#endif  // PRINTING_PRINTING_CONTEXT_WIN_H_
97