[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #ifndef PRINTING_PRINTING_CONTEXT_H_ |
| 6 | #define PRINTING_PRINTING_CONTEXT_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 8 | #include "build/build_config.h" |
| 9 | |
| 10 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 11 | #include <ocidl.h> |
| 12 | #include <commdlg.h> |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 13 | #endif |
| 14 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include <string> |
| 16 | |
| 17 | #include "base/basictypes.h" |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame^] | 18 | #include "base/callback.h" |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 19 | #if !(defined(OS_WIN) || defined(OS_MACOSX)) |
| 20 | // TODO(port) Remove after implementing PrintingContext::context() |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 21 | #include "base/logging.h" |
[email protected] | 1407b6e | 2010-08-27 21:39:48 | [diff] [blame] | 22 | #endif |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 23 | #include "base/scoped_ptr.h" |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 24 | #include "base/string16.h" |
[email protected] | 5c7293a | 2010-03-17 06:40:57 | [diff] [blame] | 25 | #include "gfx/native_widget_types.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 26 | #include "printing/print_settings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | namespace printing { |
| 38 | |
| 39 | // Describe the user selected printing context for Windows. This includes the |
| 40 | // OS-dependent UI to ask the user about the print settings. This class directly |
| 41 | // talk to the printer and manages the document and pages breaks. |
| 42 | class PrintingContext { |
| 43 | public: |
| 44 | // Tri-state result for user behavior-dependent functions. |
| 45 | enum Result { |
| 46 | OK, |
| 47 | CANCEL, |
| 48 | FAILED, |
| 49 | }; |
| 50 | |
| 51 | PrintingContext(); |
| 52 | ~PrintingContext(); |
| 53 | |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame^] | 54 | // Callback of AskUserForSettings, used to notify the PrintJobWorker when |
| 55 | // print settings are available. |
| 56 | typedef Callback1<Result>::Type PrintSettingsCallback; |
| 57 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | // Asks the user what printer and format should be used to print. Updates the |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame^] | 59 | // 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 |
| 61 | // asynchronous printing API. |
| 62 | void AskUserForSettings(gfx::NativeView parent_view, |
| 63 | int max_pages, |
| 64 | bool has_selection, |
| 65 | PrintSettingsCallback* callback); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 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 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | // Selects the user's default printer and format. Updates the context with the |
| 86 | // default device settings. |
| 87 | Result UseDefaultSettings(); |
| 88 | |
[email protected] | 38bba4f | 2010-03-12 05:29:07 | [diff] [blame] | 89 | void SetUseOverlays(bool use_overlays) { |
| 90 | settings_.use_overlays = use_overlays; |
| 91 | } |
| 92 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 93 | // Initializes with predefined settings. |
| 94 | Result InitWithSettings(const PrintSettings& settings); |
| 95 | |
| 96 | // Reinitializes the settings to uninitialized for object reuse. |
| 97 | void ResetSettings(); |
| 98 | |
| 99 | // Does platform specific setup of the printer before the printing. Signal the |
| 100 | // printer that a document is about to be spooled. |
| 101 | // 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 |
| 103 | // like virtual printers that ask the user for the path of the saved document; |
| 104 | // for example a PDF printer. |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 105 | Result NewDocument(const string16& document_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | |
| 107 | // Starts a new page. |
| 108 | Result NewPage(); |
| 109 | |
| 110 | // Closes the printed page. |
| 111 | Result PageDone(); |
| 112 | |
| 113 | // Closes the printing job. After this call the object is ready to start a new |
| 114 | // document. |
| 115 | Result DocumentDone(); |
| 116 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 117 | // Cancels printing. Can be used in a multi-threaded context. Takes effect |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | // immediately. |
| 119 | void Cancel(); |
| 120 | |
| 121 | // Dismiss the Print... dialog box if shown. |
| 122 | void DismissDialog(); |
| 123 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 124 | gfx::NativeDrawingContext context() { |
| 125 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 126 | return context_; |
| 127 | #else |
| 128 | NOTIMPLEMENTED(); |
| 129 | return NULL; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 130 | #endif |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 131 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | |
| 133 | const PrintSettings& settings() const { |
| 134 | return settings_; |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | // Class that manages the PrintDlgEx() callbacks. This is meant to be a |
| 139 | // temporary object used during the Print... dialog display. |
| 140 | class CallbackHandler; |
| 141 | |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 142 | // Does bookkeeping when an error occurs. |
| 143 | PrintingContext::Result OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 145 | #if defined(OS_WIN) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 146 | // Used in response to the user canceling the printing. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 154 | int number_ranges, |
| 155 | bool selection_only); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 157 | // Retrieves the printer's default low-level settings. On Windows, context_ is |
| 158 | // allocated with this call. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 159 | bool GetPrinterSettings(HANDLE printer, |
| 160 | const std::wstring& device_name); |
| 161 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 162 | // Parses the result of a PRINTDLGEX result. |
| 163 | Result ParseDialogResultEx(const PRINTDLGEX& dialog_options); |
| 164 | Result ParseDialogResult(const PRINTDLG& dialog_options); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 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_; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 178 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | |
| 180 | // Complete print context settings. |
| 181 | PrintSettings settings_; |
| 182 | |
[email protected] | 0ae80b89 | 2008-10-15 17:56:40 | [diff] [blame] | 183 | #ifndef NDEBUG |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | // Current page number in the print job. |
| 185 | int page_number_; |
| 186 | #endif |
| 187 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 188 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | // The dialog box for the time it is shown. |
| 190 | volatile HWND dialog_box_; |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 191 | |
| 192 | // Function pointer that defaults to PrintDlgEx. It can be changed using |
| 193 | // SetPrintDialog() in tests. |
| 194 | HRESULT (__stdcall *print_dialog_func_)(LPPRINTDLGEX); |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 195 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 196 | |
| 197 | // The dialog box has been dismissed. |
| 198 | volatile bool dialog_box_dismissed_; |
| 199 | |
| 200 | // Is a print job being done. |
| 201 | volatile bool in_print_job_; |
| 202 | |
| 203 | // Did the user cancel the print job. |
| 204 | volatile bool abort_printing_; |
| 205 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 206 | DISALLOW_COPY_AND_ASSIGN(PrintingContext); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | } // namespace printing |
| 210 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 211 | #endif // PRINTING_PRINTING_CONTEXT_H_ |