blob: d1bfca74169d608af6df6a6fc3f0c9cc7d8251da [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.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
[email protected]b75dca82009-10-13 18:46:2117#include "app/gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/basictypes.h"
[email protected]b75dca82009-10-13 18:46:2119#include "base/logging.h"
[email protected]8ff1d422009-07-07 21:31:3920#include "printing/print_settings.h"
initial.commit09911bf2008-07-26 23:55:2921
[email protected]b75dca82009-10-13 18:46:2122#if defined(OS_MACOSX)
23#include "base/scoped_cftyperef.h"
24#ifdef __OBJC__
25@class NSPrintInfo;
26#else
27class NSPrintInfo;
28#endif // __OBJC__
29#endif // OS_MACOSX
30
initial.commit09911bf2008-07-26 23:55:2931namespace 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.
36class 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]b75dca82009-10-13 18:46:2150 Result AskUserForSettings(gfx::NativeWindow window, int max_pages,
51 bool has_selection);
initial.commit09911bf2008-07-26 23:55:2952
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]8ff1d422009-07-07 21:31:3981 // Cancels printing. Can be used in a multi-threaded context. Takes effect
initial.commit09911bf2008-07-26 23:55:2982 // immediately.
83 void Cancel();
84
85 // Dismiss the Print... dialog box if shown.
86 void DismissDialog();
87
[email protected]b75dca82009-10-13 18:46:2188 gfx::NativeDrawingContext context() {
89#if defined(OS_WIN) || defined(OS_MACOSX)
90 return context_;
91#else
92 NOTIMPLEMENTED();
93 return NULL;
[email protected]8ff1d422009-07-07 21:31:3994#endif
[email protected]b75dca82009-10-13 18:46:2195 }
initial.commit09911bf2008-07-26 23:55:2996
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]c8ad40c2009-06-08 17:05:21106 // Does bookkeeping when an error occurs.
107 PrintingContext::Result OnError();
initial.commit09911bf2008-07-26 23:55:29108
[email protected]8ff1d422009-07-07 21:31:39109#if defined(OS_WIN)
[email protected]c8ad40c2009-06-08 17:05:21110 // Used in response to the user canceling the printing.
initial.commit09911bf2008-07-26 23:55:29111 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]c8ad40c2009-06-08 17:05:21118 int number_ranges,
119 bool selection_only);
initial.commit09911bf2008-07-26 23:55:29120
[email protected]b75dca82009-10-13 18:46:21121 // Retrieves the printer's default low-level settings. On Windows, context_ is
122 // allocated with this call.
initial.commit09911bf2008-07-26 23:55:29123 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]b75dca82009-10-13 18:46:21134#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]8ff1d422009-07-07 21:31:39147#endif
initial.commit09911bf2008-07-26 23:55:29148
149 // Complete print context settings.
150 PrintSettings settings_;
151
[email protected]0ae80b892008-10-15 17:56:40152#ifndef NDEBUG
initial.commit09911bf2008-07-26 23:55:29153 // Current page number in the print job.
154 int page_number_;
155#endif
156
[email protected]8ff1d422009-07-07 21:31:39157#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29158 // The dialog box for the time it is shown.
159 volatile HWND dialog_box_;
[email protected]8ff1d422009-07-07 21:31:39160#endif
initial.commit09911bf2008-07-26 23:55:29161
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]8ff1d422009-07-07 21:31:39176#endif // PRINTING_PRINTING_CONTEXT_H_