blob: d5c6b00b2da9fce228483f244db600bdf110ec24 [file] [log] [blame]
[email protected]1407b6e2010-08-27 21:39:481// Copyright (c) 2010 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]8ff1d422009-07-07 21:31:395#ifndef PRINTING_PRINTING_CONTEXT_H_
6#define PRINTING_PRINTING_CONTEXT_H_
initial.commit09911bf2008-07-26 23:55:297
[email protected]8ff1d422009-07-07 21:31:398#include "build/build_config.h"
9
10#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2911#include <ocidl.h>
12#include <commdlg.h>
[email protected]8ff1d422009-07-07 21:31:3913#endif
14
initial.commit09911bf2008-07-26 23:55:2915#include <string>
16
17#include "base/basictypes.h"
[email protected]b7191422010-09-21 19:18:0518#include "base/callback.h"
[email protected]1407b6e2010-08-27 21:39:4819#if !(defined(OS_WIN) || defined(OS_MACOSX))
20// TODO(port) Remove after implementing PrintingContext::context()
[email protected]b75dca82009-10-13 18:46:2121#include "base/logging.h"
[email protected]1407b6e2010-08-27 21:39:4822#endif
[email protected]d8254622010-08-13 19:15:4623#include "base/scoped_ptr.h"
[email protected]d9d42992010-09-13 19:39:1924#include "base/string16.h"
[email protected]5c7293a2010-03-17 06:40:5725#include "gfx/native_widget_types.h"
[email protected]8ff1d422009-07-07 21:31:3926#include "printing/print_settings.h"
initial.commit09911bf2008-07-26 23:55:2927
[email protected]b75dca82009-10-13 18:46:2128#if defined(OS_MACOSX)
29#include "base/scoped_cftyperef.h"
30#ifdef __OBJC__
31@class NSPrintInfo;
32#else
33class NSPrintInfo;
34#endif // __OBJC__
35#endif // OS_MACOSX
36
initial.commit09911bf2008-07-26 23:55:2937namespace 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.
42class 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]b7191422010-09-21 19:18:0554 // Callback of AskUserForSettings, used to notify the PrintJobWorker when
55 // print settings are available.
56 typedef Callback1<Result>::Type PrintSettingsCallback;
57
initial.commit09911bf2008-07-26 23:55:2958 // Asks the user what printer and format should be used to print. Updates the
[email protected]b7191422010-09-21 19:18:0559 // 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.commit09911bf2008-07-26 23:55:2966
[email protected]d8254622010-08-13 19:15:4667#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.commit09911bf2008-07-26 23:55:2985 // Selects the user's default printer and format. Updates the context with the
86 // default device settings.
87 Result UseDefaultSettings();
88
[email protected]38bba4f2010-03-12 05:29:0789 void SetUseOverlays(bool use_overlays) {
90 settings_.use_overlays = use_overlays;
91 }
92
initial.commit09911bf2008-07-26 23:55:2993 // 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]d9d42992010-09-13 19:39:19105 Result NewDocument(const string16& document_name);
initial.commit09911bf2008-07-26 23:55:29106
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]8ff1d422009-07-07 21:31:39117 // Cancels printing. Can be used in a multi-threaded context. Takes effect
initial.commit09911bf2008-07-26 23:55:29118 // immediately.
119 void Cancel();
120
121 // Dismiss the Print... dialog box if shown.
122 void DismissDialog();
123
[email protected]b75dca82009-10-13 18:46:21124 gfx::NativeDrawingContext context() {
125#if defined(OS_WIN) || defined(OS_MACOSX)
126 return context_;
127#else
128 NOTIMPLEMENTED();
129 return NULL;
[email protected]8ff1d422009-07-07 21:31:39130#endif
[email protected]b75dca82009-10-13 18:46:21131 }
initial.commit09911bf2008-07-26 23:55:29132
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]c8ad40c2009-06-08 17:05:21142 // Does bookkeeping when an error occurs.
143 PrintingContext::Result OnError();
initial.commit09911bf2008-07-26 23:55:29144
[email protected]8ff1d422009-07-07 21:31:39145#if defined(OS_WIN)
[email protected]c8ad40c2009-06-08 17:05:21146 // Used in response to the user canceling the printing.
initial.commit09911bf2008-07-26 23:55:29147 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]c8ad40c2009-06-08 17:05:21154 int number_ranges,
155 bool selection_only);
initial.commit09911bf2008-07-26 23:55:29156
[email protected]b75dca82009-10-13 18:46:21157 // Retrieves the printer's default low-level settings. On Windows, context_ is
158 // allocated with this call.
initial.commit09911bf2008-07-26 23:55:29159 bool GetPrinterSettings(HANDLE printer,
160 const std::wstring& device_name);
161
initial.commit09911bf2008-07-26 23:55:29162 // Parses the result of a PRINTDLGEX result.
163 Result ParseDialogResultEx(const PRINTDLGEX& dialog_options);
164 Result ParseDialogResult(const PRINTDLG& dialog_options);
[email protected]b75dca82009-10-13 18:46:21165#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]8ff1d422009-07-07 21:31:39178#endif
initial.commit09911bf2008-07-26 23:55:29179
180 // Complete print context settings.
181 PrintSettings settings_;
182
[email protected]0ae80b892008-10-15 17:56:40183#ifndef NDEBUG
initial.commit09911bf2008-07-26 23:55:29184 // Current page number in the print job.
185 int page_number_;
186#endif
187
[email protected]8ff1d422009-07-07 21:31:39188#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29189 // The dialog box for the time it is shown.
190 volatile HWND dialog_box_;
[email protected]d8254622010-08-13 19:15:46191
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]8ff1d422009-07-07 21:31:39195#endif
initial.commit09911bf2008-07-26 23:55:29196
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]5930cb62009-12-08 02:04:22206 DISALLOW_COPY_AND_ASSIGN(PrintingContext);
initial.commit09911bf2008-07-26 23:55:29207};
208
209} // namespace printing
210
[email protected]8ff1d422009-07-07 21:31:39211#endif // PRINTING_PRINTING_CONTEXT_H_