source: trunk/src/gui/dialogs/qprintdialog_mac.mm@ 890

Last change on this file since 890 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 14.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QT_NO_PRINTDIALOG
43
44#include <private/qt_mac_p.h>
45
46#include <qhash.h>
47#include <qprintdialog.h>
48#include <private/qapplication_p.h>
49#include <private/qabstractprintdialog_p.h>
50#include <private/qprintengine_mac_p.h>
51
52QT_BEGIN_NAMESPACE
53
54class QPrintDialogPrivate : public QAbstractPrintDialogPrivate
55{
56 Q_DECLARE_PUBLIC(QPrintDialog)
57
58public:
59 QPrintDialogPrivate() : ep(0), printPanel(0)
60#ifndef QT_MAC_USE_COCOA
61 ,upp(0)
62#endif
63 {}
64#ifndef QT_MAC_USE_COCOA
65 ~QPrintDialogPrivate() {
66 if (upp) {
67 DisposePMSheetDoneUPP(upp);
68 upp = 0;
69 }
70 QHash<PMPrintSession, QPrintDialogPrivate *>::iterator it = sheetCallbackMap.begin();
71 while (it != sheetCallbackMap.end()) {
72 if (it.value() == this) {
73 it = sheetCallbackMap.erase(it);
74 } else {
75 ++it;
76 }
77 }
78 }
79#endif
80
81#ifndef QT_MAC_USE_COCOA
82 void openCarbonPrintPanel(Qt::WindowModality modality);
83 void closeCarbonPrintPanel();
84 static void printDialogSheetDoneCallback(PMPrintSession printSession, WindowRef /*documentWindow*/, Boolean accepted) {
85 QPrintDialogPrivate *priv = sheetCallbackMap.value(printSession);
86 if (!priv) {
87 qWarning("%s:%d: QPrintDialog::exec: Could not retrieve data structure, "
88 "you most likely now have an infinite loop", __FILE__, __LINE__);
89 return;
90 }
91 priv->q_func()->done(accepted ? QDialog::Accepted : QDialog::Rejected);
92 priv->closeCarbonPrintPanel();
93 }
94#else
95 void openCocoaPrintPanel(Qt::WindowModality modality);
96 void closeCocoaPrintPanel();
97#endif
98 void initBeforeRun();
99
100 inline QPrintDialog *printDialog() { return q_func(); }
101
102 inline void _q_printToFileChanged(int) {}
103 inline void _q_rbPrintRangeToggled(bool) {}
104 inline void _q_printerChanged(int) {}
105#ifndef QT_NO_MESSAGEBOX
106 inline void _q_checkFields() {}
107#endif
108 inline void _q_chbPrintLastFirstToggled(bool) {}
109 inline void _q_paperSizeChanged(int) {}
110 inline void _q_btnBrowseClicked() {}
111 inline void _q_btnPropertiesClicked() {}
112
113 QMacPrintEnginePrivate *ep;
114 NSPrintPanel *printPanel;
115#ifndef QT_MAC_USE_COCOA
116 PMSheetDoneUPP upp;
117 static QHash<PMPrintSession, QPrintDialogPrivate *> sheetCallbackMap;
118#endif
119};
120
121QT_END_NAMESPACE
122
123QT_USE_NAMESPACE
124
125#ifdef QT_MAC_USE_COCOA
126
127@class QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate);
128
129@interface QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate) : NSObject {
130}
131- (void)printPanelDidEnd:(NSPrintPanel *)printPanel
132 returnCode:(int)returnCode contextInfo:(void *)contextInfo;
133@end
134
135@implementation QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate)
136- (void)printPanelDidEnd:(NSPrintPanel *)printPanel
137 returnCode:(int)returnCode contextInfo:(void *)contextInfo
138{
139 Q_UNUSED(printPanel);
140
141 QPrintDialogPrivate *d = static_cast<QPrintDialogPrivate *>(contextInfo);
142 QPrintDialog *dialog = d->printDialog();
143
144 if (returnCode == NSOKButton) {
145 UInt32 frompage, topage;
146 PMGetFirstPage(d->ep->settings, &frompage);
147 PMGetLastPage(d->ep->settings, &topage);
148 topage = qMin(UInt32(INT_MAX), topage);
149 dialog->setFromTo(frompage, topage);
150
151 // OK, I need to map these values back let's see
152 // If from is 1 and to is INT_MAX, then print it all
153 // (Apologies to the folks with more than INT_MAX pages)
154 if (dialog->fromPage() == 1 && dialog->toPage() == INT_MAX) {
155 dialog->setPrintRange(QPrintDialog::AllPages);
156 dialog->setFromTo(0, 0);
157 } else {
158 dialog->setPrintRange(QPrintDialog::PageRange); // In a way a lie, but it shouldn't hurt.
159 // Carbon hands us back a very large number here even for ALL, set it to max
160 // in that case to follow the behavior of the other print dialogs.
161 if (dialog->maxPage() < dialog->toPage())
162 dialog->setFromTo(dialog->fromPage(), dialog->maxPage());
163 }
164 // Keep us in sync with file output
165 PMDestinationType dest;
166
167 // If the user selected print to file, the session has been
168 // changed behind our back and our d->ep->session object is a
169 // dangling pointer. Update it based on the "current" session
170 d->ep->session = static_cast<PMPrintSession>([d->ep->printInfo PMPrintSession]);
171
172 PMSessionGetDestinationType(d->ep->session, d->ep->settings, &dest);
173 if (dest == kPMDestinationFile) {
174 QCFType<CFURLRef> file;
175 PMSessionCopyDestinationLocation(d->ep->session, d->ep->settings, &file);
176 UInt8 localFile[2048]; // Assuming there's a POSIX file system here.
177 CFURLGetFileSystemRepresentation(file, true, localFile, sizeof(localFile));
178 d->ep->outputFilename
179 = QString::fromUtf8(reinterpret_cast<const char *>(localFile));
180 } else {
181 // Keep output format.
182 QPrinter::OutputFormat format;
183 format = d->printer->outputFormat();
184 d->printer->setOutputFileName(QString());
185 d->printer->setOutputFormat(format);
186 }
187 }
188
189 dialog->done((returnCode == NSOKButton) ? QDialog::Accepted : QDialog::Rejected);
190}
191@end
192
193#endif
194
195QT_BEGIN_NAMESPACE
196
197extern void macStartInterceptWindowTitle(QWidget *window);
198extern void macStopInterceptWindowTitle();
199
200
201void QPrintDialogPrivate::initBeforeRun()
202{
203 Q_Q(QPrintDialog);
204 // If someone is reusing a QPrinter object, the end released all our old
205 // information. In this case, we must reinitialize.
206 if (ep->session == 0)
207 ep->initialize();
208
209
210 // It seems the only way that PM lets you use all is if the minimum
211 // for the page range is 1. This _kind of_ makes sense if you think about
212 // it. However, calling PMSetFirstPage() or PMSetLastPage() always enforces
213 // the range.
214 PMSetPageRange(ep->settings, q->minPage(), q->maxPage());
215 if (q->printRange() == QAbstractPrintDialog::PageRange) {
216 PMSetFirstPage(ep->settings, q->fromPage(), false);
217 PMSetLastPage(ep->settings, q->toPage(), false);
218 }
219}
220
221#ifndef QT_MAC_USE_COCOA
222QHash<PMPrintSession, QPrintDialogPrivate *> QPrintDialogPrivate::sheetCallbackMap;
223void QPrintDialogPrivate::openCarbonPrintPanel(Qt::WindowModality modality)
224{
225 Q_Q(QPrintDialog);
226 initBeforeRun();
227 sheetCallbackMap.insert(ep->session, this);
228 if (modality == Qt::ApplicationModal) {
229 QWidget modal_widg(0, Qt::Window);
230 modal_widg.setObjectName(QLatin1String(__FILE__ "__modal_dlg"));
231 modal_widg.createWinId();
232 QApplicationPrivate::enterModal(&modal_widg);
233 QApplicationPrivate::native_modal_dialog_active = true;
234 Boolean acceptStatus;
235 PMSessionPrintDialog(ep->session, ep->settings, ep->format, &acceptStatus);
236 QApplicationPrivate::leaveModal(&modal_widg);
237 QApplicationPrivate::native_modal_dialog_active = false;
238 printDialogSheetDoneCallback(ep->session, 0, acceptStatus);
239 } else {
240 // Window Modal means that we use a sheet at the moment, there's no other way to do it correctly.
241 if (!upp)
242 upp = NewPMSheetDoneUPP(QPrintDialogPrivate::printDialogSheetDoneCallback);
243 PMSessionUseSheets(ep->session, qt_mac_window_for(q->parentWidget()), upp);
244 QApplicationPrivate::native_modal_dialog_active = true;
245 Boolean unused;
246 PMSessionPrintDialog(ep->session, ep->settings, ep->format, &unused);
247 }
248}
249
250void QPrintDialogPrivate::closeCarbonPrintPanel()
251{
252 Q_Q(QPrintDialog);
253 QApplicationPrivate::native_modal_dialog_active = false;
254 if (q->result() == QDialog::Accepted) {
255 UInt32 frompage, topage;
256 PMGetFirstPage(ep->settings, &frompage);
257 PMGetLastPage(ep->settings, &topage);
258 topage = qMin(UInt32(INT_MAX), topage);
259 q->setFromTo(frompage, topage);
260
261 // OK, I need to map these values back let's see
262 // If from is 1 and to is INT_MAX, then print it all
263 // (Apologies to the folks with more than INT_MAX pages)
264 // ...that's a joke.
265 if (q->fromPage() == 1 && q->toPage() == INT_MAX) {
266 q->setPrintRange(QAbstractPrintDialog::AllPages);
267 q->setFromTo(0,0);
268 } else {
269 q->setPrintRange(QAbstractPrintDialog::PageRange); // In a way a lie, but it shouldn't hurt.
270 // Carbon hands us back a very large number here even for ALL, set it to max
271 // in that case to follow the behavior of the other print dialogs.
272 if (q->maxPage() < q->toPage())
273 q->setFromTo(q->fromPage(), q->maxPage());
274 }
275 // Keep us in sync with file output
276 PMDestinationType dest;
277 PMSessionGetDestinationType(ep->session, ep->settings, &dest);
278 if (dest == kPMDestinationFile) {
279 QCFType<CFURLRef> file;
280 PMSessionCopyDestinationLocation(ep->session, ep->settings, &file);
281 UInt8 localFile[2048]; // Assuming there's a POSIX file system here.
282 CFURLGetFileSystemRepresentation(file, true, localFile, sizeof(localFile));
283 ep->outputFilename = QString::fromUtf8(reinterpret_cast<const char *>(localFile));
284 } else {
285 ep->outputFilename = QString();
286 }
287 }
288 sheetCallbackMap.remove(ep->session);
289}
290#else
291void QPrintDialogPrivate::openCocoaPrintPanel(Qt::WindowModality modality)
292{
293 Q_Q(QPrintDialog);
294
295 initBeforeRun();
296
297 QPrintDialog::PrintDialogOptions qtOptions = q->options();
298 NSPrintPanelOptions macOptions = NSPrintPanelShowsCopies;
299 if (qtOptions & QPrintDialog::PrintPageRange)
300 macOptions |= NSPrintPanelShowsPageRange;
301 if (qtOptions & QPrintDialog::PrintShowPageSize)
302 macOptions |= NSPrintPanelShowsPaperSize | NSPrintPanelShowsPageSetupAccessory
303 | NSPrintPanelShowsOrientation;
304
305 macStartInterceptWindowTitle(q);
306 printPanel = [NSPrintPanel printPanel];
307 QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate) *delegate = [[QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate) alloc] init];
308 [printPanel setOptions:macOptions];
309
310 if (modality == Qt::ApplicationModal) {
311 int rval = [printPanel runModalWithPrintInfo:ep->printInfo];
312 [delegate printPanelDidEnd:printPanel returnCode:rval contextInfo:this];
313 } else {
314 Q_ASSERT(q->parentWidget());
315 NSWindow *windowRef = qt_mac_window_for(q->parentWidget());
316 [printPanel beginSheetWithPrintInfo:ep->printInfo
317 modalForWindow:windowRef
318 delegate:delegate
319 didEndSelector:@selector(printPanelDidEnd:returnCode:contextInfo:)
320 contextInfo:this];
321 }
322
323 macStopInterceptWindowTitle();
324}
325
326void QPrintDialogPrivate::closeCocoaPrintPanel()
327{
328 // ###
329}
330#endif
331
332static bool warnIfNotNative(QPrinter *printer)
333{
334 if (printer->outputFormat() != QPrinter::NativeFormat) {
335 qWarning("QPrintDialog: Cannot be used on non-native printers");
336 return false;
337 }
338 return true;
339}
340
341
342QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
343 : QAbstractPrintDialog(*(new QPrintDialogPrivate), printer, parent)
344{
345 Q_D(QPrintDialog);
346 if (!warnIfNotNative(d->printer))
347 return;
348 d->ep = static_cast<QMacPrintEngine *>(d->printer->paintEngine())->d_func();
349}
350
351QPrintDialog::QPrintDialog(QWidget *parent)
352 : QAbstractPrintDialog(*(new QPrintDialogPrivate), 0, parent)
353{
354 Q_D(QPrintDialog);
355 if (!warnIfNotNative(d->printer))
356 return;
357 d->ep = static_cast<QMacPrintEngine *>(d->printer->paintEngine())->d_func();
358}
359
360QPrintDialog::~QPrintDialog()
361{
362}
363
364int QPrintDialog::exec()
365{
366 Q_D(QPrintDialog);
367 if (!warnIfNotNative(d->printer))
368 return QDialog::Rejected;
369
370#ifndef QT_MAC_USE_COCOA
371 d->openCarbonPrintPanel(Qt::ApplicationModal);
372#else
373 QMacCocoaAutoReleasePool pool;
374
375 d->openCocoaPrintPanel(Qt::ApplicationModal);
376 d->closeCocoaPrintPanel();
377#endif
378 return result();
379}
380
381#ifdef QT3_SUPPORT
382QPrinter *QPrintDialog::printer() const
383{
384 Q_D(const QPrintDialog);
385 return d->printer;
386}
387#endif
388
389/*!
390 \reimp
391*/
392void QPrintDialog::setVisible(bool visible)
393{
394 Q_D(QPrintDialog);
395
396 bool isCurrentlyVisible = (d->printPanel != 0);
397
398 if (!visible == !isCurrentlyVisible)
399 return;
400
401 if (d->printer->outputFormat() != QPrinter::NativeFormat)
402 return;
403
404 if (visible) {
405#ifndef QT_MAC_USE_COCOA
406 d->openCarbonPrintPanel(parentWidget() ? Qt::WindowModal
407 : Qt::ApplicationModal);
408#else
409 d->openCocoaPrintPanel(parentWidget() ? Qt::WindowModal
410 : Qt::ApplicationModal);
411#endif
412 return;
413 } else {
414 if (d->printPanel) {
415#ifndef QT_MAC_USE_COCOA
416 d->closeCarbonPrintPanel();
417#else
418 d->closeCocoaPrintPanel();
419#endif
420 return;
421 }
422 }
423}
424
425QT_END_NAMESPACE
426
427#include "moc_qprintdialog.cpp"
428
429#endif // QT_NO_PRINTDIALOG
Note: See TracBrowser for help on using the repository browser.