blob: 3be4917109f5658275bb28b3fd377d88aca2df17 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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"
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000017#include "ui/shell_dialogs/print_settings_dialog_win.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000018
19namespace printing {
20
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000021class PRINTING_EXPORT PrintingContextWin
22 : public PrintingContext,
23 public ui::PrintSettingsDialogWin::Observer {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024 public:
25 explicit PrintingContextWin(const std::string& app_locale);
26 ~PrintingContextWin();
27
28 // PrintingContext implementation.
29 virtual void AskUserForSettings(
30 gfx::NativeView parent_view,
31 int max_pages,
32 bool has_selection,
33 const PrintSettingsCallback& callback) OVERRIDE;
34 virtual Result UseDefaultSettings() OVERRIDE;
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000035 virtual gfx::Size GetPdfPaperSizeDeviceUnits() OVERRIDE;
36 virtual Result UpdatePrinterSettings(bool external_preview) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000037 virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE;
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010038 virtual Result NewDocument(const base::string16& document_name) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000039 virtual Result NewPage() OVERRIDE;
40 virtual Result PageDone() OVERRIDE;
41 virtual Result DocumentDone() OVERRIDE;
42 virtual void Cancel() OVERRIDE;
43 virtual void ReleaseContext() OVERRIDE;
44 virtual gfx::NativeDrawingContext context() const OVERRIDE;
45
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000046 // PrintSettingsDialogWin::Observer implementation:
47 virtual void PrintSettingsConfirmed(PRINTDLGEX* dialog_options) OVERRIDE;
48 virtual void PrintSettingsCancelled(PRINTDLGEX* dialog_options) OVERRIDE;
49
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050#if defined(UNIT_TEST) || defined(PRINTING_IMPLEMENTATION)
51 // Sets a fake PrintDlgEx function pointer in tests.
52 void SetPrintDialog(HRESULT (__stdcall *print_dialog_func)(LPPRINTDLGEX)) {
53 print_dialog_func_ = print_dialog_func;
54 }
55#endif // defined(UNIT_TEST)
56
57 // Allocates the HDC for a specific DEVMODE.
58 static bool AllocateContext(const std::wstring& printer_name,
59 const DEVMODE* dev_mode,
60 gfx::NativeDrawingContext* context);
61
Torne (Richard Coles)58218062012-11-14 11:43:16 +000062 private:
63 // Class that manages the PrintDlgEx() callbacks. This is meant to be a
64 // temporary object used during the Print... dialog display.
65 class CallbackHandler;
66
67 // Used in response to the user canceling the printing.
68 static BOOL CALLBACK AbortProc(HDC hdc, int nCode);
69
70 // Reads the settings from the selected device context. Updates settings_ and
71 // its margins.
72 bool InitializeSettings(const DEVMODE& dev_mode,
73 const std::wstring& new_device_name,
74 const PRINTPAGERANGE* ranges,
75 int number_ranges,
76 bool selection_only);
77
78 // Retrieves the printer's default low-level settings. On Windows, context_ is
79 // allocated with this call.
80 bool GetPrinterSettings(HANDLE printer,
81 const std::wstring& device_name);
82
83 // Parses the result of a PRINTDLGEX result.
84 Result ParseDialogResultEx(const PRINTDLGEX& dialog_options);
85 Result ParseDialogResult(const PRINTDLG& dialog_options);
86
87 // The selected printer context.
88 HDC context_;
89
90 // The dialog box for the time it is shown.
91 volatile HWND dialog_box_;
92
93 // Function pointer that defaults to PrintDlgEx. It can be changed using
94 // SetPrintDialog() in tests.
95 HRESULT (__stdcall *print_dialog_func_)(LPPRINTDLGEX);
96
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000097 // Where to notify when the dialog is closed.
98 PrintSettingsCallback callback_;
99
100 // Wrapper around native print dialog that runs it on a background thread.
101 scoped_refptr<ui::PrintSettingsDialogWin> print_settings_dialog_;
102
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103 DISALLOW_COPY_AND_ASSIGN(PrintingContextWin);
104};
105
106} // namespace printing
107
108#endif // PRINTING_PRINTING_CONTEXT_WIN_H_