OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PRINTING_PRINTING_CONTEXT_H_ | 5 #ifndef PRINTING_PRINTING_CONTEXT_H_ |
6 #define PRINTING_PRINTING_CONTEXT_H_ | 6 #define PRINTING_PRINTING_CONTEXT_H_ |
7 | 7 |
8 #include "build/build_config.h" | |
9 | |
10 #if defined(OS_WIN) | |
11 #include <ocidl.h> | |
12 #include <commdlg.h> | |
13 #endif | |
14 | |
15 #include <string> | 8 #include <string> |
16 | 9 |
17 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
18 #include "base/callback.h" | 11 #include "base/callback.h" |
19 #if !(defined(OS_WIN) || defined(OS_MACOSX)) | |
20 // TODO(port) Remove after implementing PrintingContext::context() | |
21 #include "base/logging.h" | |
22 #endif | |
23 #include "base/scoped_ptr.h" | 12 #include "base/scoped_ptr.h" |
24 #include "base/string16.h" | 13 #include "base/string16.h" |
25 #include "gfx/native_widget_types.h" | 14 #include "gfx/native_widget_types.h" |
26 #include "printing/print_settings.h" | 15 #include "printing/print_settings.h" |
27 | 16 |
28 #if defined(OS_MACOSX) | |
29 #include "base/scoped_cftyperef.h" | |
30 #ifdef __OBJC__ | |
31 @class NSPrintInfo; | |
32 #else | |
33 class NSPrintInfo; | |
34 #endif // __OBJC__ | |
35 #endif // OS_MACOSX | |
36 | |
37 namespace printing { | 17 namespace printing { |
38 | 18 |
39 // Describe the user selected printing context for Windows. This includes the | 19 // An abstraction of a printer context, implemented by objects that describe the |
40 // OS-dependent UI to ask the user about the print settings. This class directly | 20 // user selected printing context. This includes the OS-dependent UI to ask the |
41 // talk to the printer and manages the document and pages breaks. | 21 // user about the print settings. Concrete implementations directly talk to the |
| 22 // printer and manage the document and page breaks. |
42 class PrintingContext { | 23 class PrintingContext { |
43 public: | 24 public: |
44 // Tri-state result for user behavior-dependent functions. | 25 // Tri-state result for user behavior-dependent functions. |
45 enum Result { | 26 enum Result { |
46 OK, | 27 OK, |
47 CANCEL, | 28 CANCEL, |
48 FAILED, | 29 FAILED, |
49 }; | 30 }; |
50 | 31 |
51 PrintingContext(); | 32 virtual ~PrintingContext(); |
52 ~PrintingContext(); | |
53 | 33 |
54 // Callback of AskUserForSettings, used to notify the PrintJobWorker when | 34 // Callback of AskUserForSettings, used to notify the PrintJobWorker when |
55 // print settings are available. | 35 // print settings are available. |
56 typedef Callback1<Result>::Type PrintSettingsCallback; | 36 typedef Callback1<Result>::Type PrintSettingsCallback; |
57 | 37 |
58 // Asks the user what printer and format should be used to print. Updates the | 38 // Asks the user what printer and format should be used to print. Updates the |
59 // context with the select device settings. The result of the call is returned | 39 // context with the select device settings. The result of the call is returned |
60 // in the callback. This is necessary for Linux, which only has an | 40 // in the callback. This is necessary for Linux, which only has an |
61 // asynchronous printing API. | 41 // asynchronous printing API. |
62 void AskUserForSettings(gfx::NativeView parent_view, | 42 virtual void AskUserForSettings(gfx::NativeView parent_view, |
63 int max_pages, | 43 int max_pages, |
64 bool has_selection, | 44 bool has_selection, |
65 PrintSettingsCallback* callback); | 45 PrintSettingsCallback* callback) = 0; |
66 | |
67 #if defined(OS_WIN) && defined(UNIT_TEST) | |
68 // Sets a fake PrintDlgEx function pointer in tests. | |
69 void SetPrintDialog(HRESULT (__stdcall *print_dialog_func)(LPPRINTDLGEX)) { | |
70 print_dialog_func_ = print_dialog_func; | |
71 } | |
72 #endif | |
73 | |
74 #if defined(OS_WIN) | |
75 // Allocates the HDC for a specific DEVMODE. | |
76 static bool AllocateContext(const std::wstring& printer_name, | |
77 const DEVMODE* dev_mode, | |
78 gfx::NativeDrawingContext* context); | |
79 | |
80 // Retrieves the content of a GetPrinter call. | |
81 static void GetPrinterHelper(HANDLE printer, int level, | |
82 scoped_array<uint8>* buffer); | |
83 #endif | |
84 | 46 |
85 // Selects the user's default printer and format. Updates the context with the | 47 // Selects the user's default printer and format. Updates the context with the |
86 // default device settings. | 48 // default device settings. |
87 Result UseDefaultSettings(); | 49 virtual Result UseDefaultSettings() = 0; |
88 | |
89 void SetUseOverlays(bool use_overlays) { | |
90 settings_.use_overlays = use_overlays; | |
91 } | |
92 | 50 |
93 // Initializes with predefined settings. | 51 // Initializes with predefined settings. |
94 Result InitWithSettings(const PrintSettings& settings); | 52 virtual Result InitWithSettings(const PrintSettings& settings) = 0; |
95 | |
96 // Reinitializes the settings to uninitialized for object reuse. | |
97 void ResetSettings(); | |
98 | 53 |
99 // Does platform specific setup of the printer before the printing. Signal the | 54 // Does platform specific setup of the printer before the printing. Signal the |
100 // printer that a document is about to be spooled. | 55 // printer that a document is about to be spooled. |
101 // Warning: This function enters a message loop. That may cause side effects | 56 // Warning: This function enters a message loop. That may cause side effects |
102 // like IPC message processing! Some printers have side-effects on this call | 57 // like IPC message processing! Some printers have side-effects on this call |
103 // like virtual printers that ask the user for the path of the saved document; | 58 // like virtual printers that ask the user for the path of the saved document; |
104 // for example a PDF printer. | 59 // for example a PDF printer. |
105 Result NewDocument(const string16& document_name); | 60 virtual Result NewDocument(const string16& document_name) = 0; |
106 | 61 |
107 // Starts a new page. | 62 // Starts a new page. |
108 Result NewPage(); | 63 virtual Result NewPage() = 0; |
109 | 64 |
110 // Closes the printed page. | 65 // Closes the printed page. |
111 Result PageDone(); | 66 virtual Result PageDone() = 0; |
112 | 67 |
113 // Closes the printing job. After this call the object is ready to start a new | 68 // Closes the printing job. After this call the object is ready to start a new |
114 // document. | 69 // document. |
115 Result DocumentDone(); | 70 virtual Result DocumentDone() = 0; |
116 | 71 |
117 // Cancels printing. Can be used in a multi-threaded context. Takes effect | 72 // Cancels printing. Can be used in a multi-threaded context. Takes effect |
118 // immediately. | 73 // immediately. |
119 void Cancel(); | 74 virtual void Cancel() = 0; |
120 | 75 |
121 // Dismiss the Print... dialog box if shown. | 76 // Dismiss the Print... dialog box if shown. |
122 void DismissDialog(); | 77 virtual void DismissDialog() = 0; |
123 | 78 |
124 gfx::NativeDrawingContext context() { | 79 // Releases the native printing context. |
125 #if defined(OS_WIN) || defined(OS_MACOSX) | 80 virtual void ReleaseContext() = 0; |
126 return context_; | 81 |
127 #else | 82 // Returns the native context used to print. |
128 NOTIMPLEMENTED(); | 83 virtual gfx::NativeDrawingContext context() const = 0; |
129 return NULL; | 84 |
130 #endif | 85 // Creates an instance of this object. Implementers of this interface should |
| 86 // implement this method to create an object of their implementation. The |
| 87 // caller owns the returned object. |
| 88 static PrintingContext* Create(); |
| 89 |
| 90 void set_use_overlays(bool use_overlays) { |
| 91 settings_.use_overlays = use_overlays; |
131 } | 92 } |
132 | 93 |
133 const PrintSettings& settings() const { | 94 const PrintSettings& settings() const { |
134 return settings_; | 95 return settings_; |
135 } | 96 } |
136 | 97 |
137 private: | 98 protected: |
138 // Class that manages the PrintDlgEx() callbacks. This is meant to be a | 99 PrintingContext(); |
139 // temporary object used during the Print... dialog display. | 100 |
140 class CallbackHandler; | 101 // Reinitializes the settings for object reuse. |
| 102 void ResetSettings(); |
141 | 103 |
142 // Does bookkeeping when an error occurs. | 104 // Does bookkeeping when an error occurs. |
143 PrintingContext::Result OnError(); | 105 PrintingContext::Result OnError(); |
144 | 106 |
145 #if defined(OS_WIN) | |
146 // Used in response to the user canceling the printing. | |
147 static BOOL CALLBACK AbortProc(HDC hdc, int nCode); | |
148 | |
149 // Reads the settings from the selected device context. Updates settings_ and | |
150 // its margins. | |
151 bool InitializeSettings(const DEVMODE& dev_mode, | |
152 const std::wstring& new_device_name, | |
153 const PRINTPAGERANGE* ranges, | |
154 int number_ranges, | |
155 bool selection_only); | |
156 | |
157 // Retrieves the printer's default low-level settings. On Windows, context_ is | |
158 // allocated with this call. | |
159 bool GetPrinterSettings(HANDLE printer, | |
160 const std::wstring& device_name); | |
161 | |
162 // Parses the result of a PRINTDLGEX result. | |
163 Result ParseDialogResultEx(const PRINTDLGEX& dialog_options); | |
164 Result ParseDialogResult(const PRINTDLG& dialog_options); | |
165 #elif defined(OS_MACOSX) | |
166 // Read the settings from the given NSPrintInfo (and cache it for later use). | |
167 void ParsePrintInfo(NSPrintInfo* print_info); | |
168 #endif | |
169 | |
170 // On Windows, the selected printer context. | |
171 // On Mac, the current page's context; only valid between NewPage and PageDone | |
172 // call pairs. | |
173 gfx::NativeDrawingContext context_; | |
174 | |
175 #if defined(OS_MACOSX) | |
176 // The native print info object. | |
177 NSPrintInfo* print_info_; | |
178 #endif | |
179 | |
180 // Complete print context settings. | 107 // Complete print context settings. |
181 PrintSettings settings_; | 108 PrintSettings settings_; |
182 | 109 |
183 #if defined(OS_WIN) | |
184 // The dialog box for the time it is shown. | |
185 volatile HWND dialog_box_; | |
186 | |
187 // Function pointer that defaults to PrintDlgEx. It can be changed using | |
188 // SetPrintDialog() in tests. | |
189 HRESULT (__stdcall *print_dialog_func_)(LPPRINTDLGEX); | |
190 #endif | |
191 | |
192 // The dialog box has been dismissed. | 110 // The dialog box has been dismissed. |
193 volatile bool dialog_box_dismissed_; | 111 volatile bool dialog_box_dismissed_; |
194 | 112 |
195 // Is a print job being done. | 113 // Is a print job being done. |
196 volatile bool in_print_job_; | 114 volatile bool in_print_job_; |
197 | 115 |
198 // Did the user cancel the print job. | 116 // Did the user cancel the print job. |
199 volatile bool abort_printing_; | 117 volatile bool abort_printing_; |
200 | 118 |
201 DISALLOW_COPY_AND_ASSIGN(PrintingContext); | 119 DISALLOW_COPY_AND_ASSIGN(PrintingContext); |
202 }; | 120 }; |
203 | 121 |
204 } // namespace printing | 122 } // namespace printing |
205 | 123 |
206 #endif // PRINTING_PRINTING_CONTEXT_H_ | 124 #endif // PRINTING_PRINTING_CONTEXT_H_ |
OLD | NEW |