license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
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 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 17 | #include "app/gfx/native_widget_types.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | #include "base/basictypes.h" |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 19 | #include "base/logging.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 20 | #include "printing/print_settings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 22 | #if defined(OS_MACOSX) |
| 23 | #include "base/scoped_cftyperef.h" |
| 24 | #ifdef __OBJC__ |
| 25 | @class NSPrintInfo; |
| 26 | #else |
| 27 | class NSPrintInfo; |
| 28 | #endif // __OBJC__ |
| 29 | #endif // OS_MACOSX |
| 30 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | namespace printing { |
| 32 | |
| 33 | // Describe the user selected printing context for Windows. This includes the |
| 34 | // OS-dependent UI to ask the user about the print settings. This class directly |
| 35 | // talk to the printer and manages the document and pages breaks. |
| 36 | class PrintingContext { |
| 37 | public: |
| 38 | // Tri-state result for user behavior-dependent functions. |
| 39 | enum Result { |
| 40 | OK, |
| 41 | CANCEL, |
| 42 | FAILED, |
| 43 | }; |
| 44 | |
| 45 | PrintingContext(); |
| 46 | ~PrintingContext(); |
| 47 | |
| 48 | // Asks the user what printer and format should be used to print. Updates the |
| 49 | // context with the select device settings. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 50 | Result AskUserForSettings(gfx::NativeWindow window, int max_pages, |
| 51 | bool has_selection); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | |
| 53 | // Selects the user's default printer and format. Updates the context with the |
| 54 | // default device settings. |
| 55 | Result UseDefaultSettings(); |
| 56 | |
| 57 | // Initializes with predefined settings. |
| 58 | Result InitWithSettings(const PrintSettings& settings); |
| 59 | |
| 60 | // Reinitializes the settings to uninitialized for object reuse. |
| 61 | void ResetSettings(); |
| 62 | |
| 63 | // Does platform specific setup of the printer before the printing. Signal the |
| 64 | // printer that a document is about to be spooled. |
| 65 | // Warning: This function enters a message loop. That may cause side effects |
| 66 | // like IPC message processing! Some printers have side-effects on this call |
| 67 | // like virtual printers that ask the user for the path of the saved document; |
| 68 | // for example a PDF printer. |
| 69 | Result NewDocument(const std::wstring& document_name); |
| 70 | |
| 71 | // Starts a new page. |
| 72 | Result NewPage(); |
| 73 | |
| 74 | // Closes the printed page. |
| 75 | Result PageDone(); |
| 76 | |
| 77 | // Closes the printing job. After this call the object is ready to start a new |
| 78 | // document. |
| 79 | Result DocumentDone(); |
| 80 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 81 | // Cancels printing. Can be used in a multi-threaded context. Takes effect |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | // immediately. |
| 83 | void Cancel(); |
| 84 | |
| 85 | // Dismiss the Print... dialog box if shown. |
| 86 | void DismissDialog(); |
| 87 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 88 | gfx::NativeDrawingContext context() { |
| 89 | #if defined(OS_WIN) || defined(OS_MACOSX) |
| 90 | return context_; |
| 91 | #else |
| 92 | NOTIMPLEMENTED(); |
| 93 | return NULL; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 94 | #endif |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 95 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 96 | |
| 97 | const PrintSettings& settings() const { |
| 98 | return settings_; |
| 99 | } |
| 100 | |
| 101 | private: |
| 102 | // Class that manages the PrintDlgEx() callbacks. This is meant to be a |
| 103 | // temporary object used during the Print... dialog display. |
| 104 | class CallbackHandler; |
| 105 | |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 106 | // Does bookkeeping when an error occurs. |
| 107 | PrintingContext::Result OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 109 | #if defined(OS_WIN) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 110 | // Used in response to the user canceling the printing. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | static BOOL CALLBACK AbortProc(HDC hdc, int nCode); |
| 112 | |
| 113 | // Reads the settings from the selected device context. Updates settings_ and |
| 114 | // its margins. |
| 115 | bool InitializeSettings(const DEVMODE& dev_mode, |
| 116 | const std::wstring& new_device_name, |
| 117 | const PRINTPAGERANGE* ranges, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 118 | int number_ranges, |
| 119 | bool selection_only); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 121 | // Retrieves the printer's default low-level settings. On Windows, context_ is |
| 122 | // allocated with this call. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 123 | bool GetPrinterSettings(HANDLE printer, |
| 124 | const std::wstring& device_name); |
| 125 | |
| 126 | // Allocates the HDC for a specific DEVMODE. |
| 127 | bool AllocateContext(const std::wstring& printer_name, |
| 128 | const DEVMODE* dev_mode); |
| 129 | |
| 130 | // Parses the result of a PRINTDLGEX result. |
| 131 | Result ParseDialogResultEx(const PRINTDLGEX& dialog_options); |
| 132 | Result ParseDialogResult(const PRINTDLG& dialog_options); |
| 133 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame^] | 134 | #elif defined(OS_MACOSX) |
| 135 | // Read the settings from the given NSPrintInfo (and cache it for later use). |
| 136 | void ParsePrintInfo(NSPrintInfo* print_info); |
| 137 | #endif |
| 138 | |
| 139 | // On Windows, the selected printer context. |
| 140 | // On Mac, the current page's context; only valid between NewPage and PageDone |
| 141 | // call pairs. |
| 142 | gfx::NativeDrawingContext context_; |
| 143 | |
| 144 | #if defined(OS_MACOSX) |
| 145 | // The native print info object. |
| 146 | NSPrintInfo* print_info_; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 147 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 148 | |
| 149 | // Complete print context settings. |
| 150 | PrintSettings settings_; |
| 151 | |
[email protected] | 0ae80b89 | 2008-10-15 17:56:40 | [diff] [blame] | 152 | #ifndef NDEBUG |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 153 | // Current page number in the print job. |
| 154 | int page_number_; |
| 155 | #endif |
| 156 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 157 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | // The dialog box for the time it is shown. |
| 159 | volatile HWND dialog_box_; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 160 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | |
| 162 | // The dialog box has been dismissed. |
| 163 | volatile bool dialog_box_dismissed_; |
| 164 | |
| 165 | // Is a print job being done. |
| 166 | volatile bool in_print_job_; |
| 167 | |
| 168 | // Did the user cancel the print job. |
| 169 | volatile bool abort_printing_; |
| 170 | |
| 171 | DISALLOW_EVIL_CONSTRUCTORS(PrintingContext); |
| 172 | }; |
| 173 | |
| 174 | } // namespace printing |
| 175 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 176 | #endif // PRINTING_PRINTING_CONTEXT_H_ |