printing_context_win.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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  // Retrieves the content of a GetPrinter call.
57  static void GetPrinterHelper(HANDLE printer, int level,
58                               scoped_array<uint8>* buffer);
59
60 private:
61  // Class that manages the PrintDlgEx() callbacks. This is meant to be a
62  // temporary object used during the Print... dialog display.
63  class CallbackHandler;
64
65  // Used in response to the user canceling the printing.
66  static BOOL CALLBACK AbortProc(HDC hdc, int nCode);
67
68  // Reads the settings from the selected device context. Updates settings_ and
69  // its margins.
70  bool InitializeSettings(const DEVMODE& dev_mode,
71                          const std::wstring& new_device_name,
72                          const PRINTPAGERANGE* ranges,
73                          int number_ranges,
74                          bool selection_only);
75
76  // Retrieves the printer's default low-level settings. On Windows, context_ is
77  // allocated with this call.
78  bool GetPrinterSettings(HANDLE printer,
79                          const std::wstring& device_name);
80
81  // Parses the result of a PRINTDLGEX result.
82  Result ParseDialogResultEx(const PRINTDLGEX& dialog_options);
83  Result ParseDialogResult(const PRINTDLG& dialog_options);
84
85  // The selected printer context.
86  HDC context_;
87
88  // The dialog box for the time it is shown.
89  volatile HWND dialog_box_;
90
91  // Function pointer that defaults to PrintDlgEx. It can be changed using
92  // SetPrintDialog() in tests.
93  HRESULT (__stdcall *print_dialog_func_)(LPPRINTDLGEX);
94
95  DISALLOW_COPY_AND_ASSIGN(PrintingContextWin);
96};
97
98}  // namespace printing
99
100#endif  // PRINTING_PRINTING_CONTEXT_WIN_H_
101