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

Last change on this file since 187 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.6 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department 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@class QCocoaPrintPanelDelegate;
126
127@interface QCocoaPrintPanelDelegate : NSObject {
128}
129- (void)printPanelDidEnd:(NSPrintPanel *)printPanel
130 returnCode:(int)returnCode contextInfo:(void *)contextInfo;
131@end
132
133@implementation QCocoaPrintPanelDelegate
134- (void)printPanelDidEnd:(NSPrintPanel *)printPanel
135 returnCode:(int)returnCode contextInfo:(void *)contextInfo
136{
137 Q_UNUSED(printPanel);
138
139 QPrintDialogPrivate *d = static_cast<QPrintDialogPrivate *>(contextInfo);
140 QPrintDialog *dialog = d->printDialog();
141 // temporary hack to work around bug in deleteLater() in Qt/Mac Cocoa
142#if 1
143 bool deleteDialog = dialog->testAttribute(Qt::WA_DeleteOnClose);
144 dialog->setAttribute(Qt::WA_DeleteOnClose, false);
145#endif
146
147 if (returnCode == NSOKButton) {
148 UInt32 frompage, topage;
149 PMGetFirstPage(d->ep->settings, &frompage);
150 PMGetLastPage(d->ep->settings, &topage);
151 topage = qMin(UInt32(INT_MAX), topage);
152 dialog->setFromTo(frompage, topage);
153
154 // OK, I need to map these values back let's see
155 // If from is 1 and to is INT_MAX, then print it all
156 // (Apologies to the folks with more than INT_MAX pages)
157 if (dialog->fromPage() == 1 && dialog->toPage() == INT_MAX) {
158 dialog->setPrintRange(QPrintDialog::AllPages);
159 dialog->setFromTo(0, 0);
160 } else {
161 dialog->setPrintRange(QPrintDialog::PageRange); // In a way a lie, but it shouldn't hurt.
162 // Carbon hands us back a very large number here even for ALL, set it to max
163 // in that case to follow the behavior of the other print dialogs.
164 if (dialog->maxPage() < dialog->toPage())
165 dialog->setFromTo(dialog->fromPage(), dialog->maxPage());
166 }
167 // Keep us in sync with file output
168 PMDestinationType dest;
169 PMSessionGetDestinationType(d->ep->session, d->ep->settings, &dest);
170 if (dest == kPMDestinationFile) {
171 QCFType<CFURLRef> file;
172 PMSessionCopyDestinationLocation(d->ep->session, d->ep->settings, &file);
173 UInt8 localFile[2048]; // Assuming there's a POSIX file system here.
174 CFURLGetFileSystemRepresentation(file, true, localFile, sizeof(localFile));
175 d->ep->outputFilename
176 = QString::fromUtf8(reinterpret_cast<const char *>(localFile));
177 } else {
178 // Keep output format.
179 QPrinter::OutputFormat format;
180 format = d->printer->outputFormat();
181 d->printer->setOutputFileName(QString());
182 d->printer->setOutputFormat(format);
183 }
184 }
185
186 dialog->done((returnCode == NSOKButton) ? QDialog::Accepted : QDialog::Rejected);
187#if 1
188 if (deleteDialog)
189 delete dialog;
190#endif
191}
192@end
193
194QT_BEGIN_NAMESPACE
195
196extern void macStartInterceptWindowTitle(QWidget *window);
197extern void macStopInterceptWindowTitle();
198
199
200void QPrintDialogPrivate::initBeforeRun()
201{
202 Q_Q(QPrintDialog);
203 // If someone is reusing a QPrinter object, the end released all our old
204 // information. In this case, we must reinitialize.
205 if (ep->session == 0)
206 ep->initialize();
207
208
209 // It seems the only way that PM lets you use all is if the minimum
210 // for the page range is 1. This _kind of_ makes sense if you think about
211 // it. However, calling PMSetFirstPage() or PMSetLastPage() always enforces
212 // the range.
213 PMSetPageRange(ep->settings, q->minPage(), q->maxPage());
214 if (q->printRange() == QAbstractPrintDialog::PageRange) {
215 PMSetFirstPage(ep->settings, q->fromPage(), false);
216 PMSetLastPage(ep->settings, q->toPage(), false);
217 }
218}
219
220#ifndef QT_MAC_USE_COCOA
221QHash<PMPrintSession, QPrintDialogPrivate *> QPrintDialogPrivate::sheetCallbackMap;
222void QPrintDialogPrivate::openCarbonPrintPanel(Qt::WindowModality modality)
223{
224 Q_Q(QPrintDialog);
225 initBeforeRun();
226 sheetCallbackMap.insert(ep->session, this);
227 if (modality == Qt::ApplicationModal) {
228 QWidget modal_widg(0, Qt::Window);
229 modal_widg.setObjectName(QLatin1String(__FILE__ "__modal_dlg"));
230 modal_widg.createWinId();
231 QApplicationPrivate::enterModal(&modal_widg);
232 QApplicationPrivate::native_modal_dialog_active = true;
233 Boolean acceptStatus;
234 PMSessionPrintDialog(ep->session, ep->settings, ep->format, &acceptStatus);
235 QApplicationPrivate::leaveModal(&modal_widg);
236 QApplicationPrivate::native_modal_dialog_active = false;
237 printDialogSheetDoneCallback(ep->session, 0, acceptStatus);
238 } else {
239 // Window Modal means that we use a sheet at the moment, there's no other way to do it correctly.
240 if (!upp)
241 upp = NewPMSheetDoneUPP(QPrintDialogPrivate::printDialogSheetDoneCallback);
242 PMSessionUseSheets(ep->session, qt_mac_window_for(q->parentWidget()), upp);
243 QApplicationPrivate::native_modal_dialog_active = true;
244 Boolean unused;
245 PMSessionPrintDialog(ep->session, ep->settings, ep->format, &unused);
246 }
247}
248
249void QPrintDialogPrivate::closeCarbonPrintPanel()
250{
251 Q_Q(QPrintDialog);
252 QApplicationPrivate::native_modal_dialog_active = false;
253 if (q->result() == QDialog::Accepted) {
254 UInt32 frompage, topage;
255 PMGetFirstPage(ep->settings, &frompage);
256 PMGetLastPage(ep->settings, &topage);
257 topage = qMin(UInt32(INT_MAX), topage);
258 q->setFromTo(frompage, topage);
259
260 // OK, I need to map these values back let's see
261 // If from is 1 and to is INT_MAX, then print it all
262 // (Apologies to the folks with more than INT_MAX pages)
263 // ...that's a joke.
264 if (q->fromPage() == 1 && q->toPage() == INT_MAX) {
265 q->setPrintRange(QAbstractPrintDialog::AllPages);
266 q->setFromTo(0,0);
267 } else {
268 q->setPrintRange(QAbstractPrintDialog::PageRange); // In a way a lie, but it shouldn't hurt.
269 // Carbon hands us back a very large number here even for ALL, set it to max
270 // in that case to follow the behavior of the other print dialogs.
271 if (q->maxPage() < q->toPage())
272 q->setFromTo(q->fromPage(), q->maxPage());
273 }
274 // Keep us in sync with file output
275 PMDestinationType dest;
276 PMSessionGetDestinationType(ep->session, ep->settings, &dest);
277 if (dest == kPMDestinationFile) {
278 QCFType<CFURLRef> file;
279 PMSessionCopyDestinationLocation(ep->session, ep->settings, &file);
280 UInt8 localFile[2048]; // Assuming there's a POSIX file system here.
281 CFURLGetFileSystemRepresentation(file, true, localFile, sizeof(localFile));
282 ep->outputFilename = QString::fromUtf8(reinterpret_cast<const char *>(localFile));
283 } else {
284 ep->outputFilename = QString();
285 }
286 }
287 sheetCallbackMap.remove(ep->session);
288}
289#else
290void QPrintDialogPrivate::openCocoaPrintPanel(Qt::WindowModality modality)
291{
292 Q_Q(QPrintDialog);
293
294 initBeforeRun();
295
296 QPrintDialog::PrintDialogOptions qtOptions = q->options();
297 NSPrintPanelOptions macOptions = NSPrintPanelShowsCopies;
298 if (qtOptions & QPrintDialog::PrintPageRange)
299 macOptions |= NSPrintPanelShowsPageRange;
300 if (qtOptions & QPrintDialog::PrintShowPageSize)
301 macOptions |= NSPrintPanelShowsPaperSize | NSPrintPanelShowsPageSetupAccessory
302 | NSPrintPanelShowsOrientation;
303
304 macStartInterceptWindowTitle(q);
305 printPanel = [NSPrintPanel printPanel];
306 QCocoaPrintPanelDelegate *delegate = [[QCocoaPrintPanelDelegate alloc] init];
307 [printPanel setOptions:macOptions];
308
309 if (modality == Qt::ApplicationModal) {
310 int rval = [printPanel runModalWithPrintInfo:ep->printInfo];
311 [delegate printPanelDidEnd:printPanel returnCode:rval contextInfo:this];
312 } else {
313 Q_ASSERT(q->parentWidget());
314 NSWindow *windowRef = qt_mac_window_for(q->parentWidget());
315 [printPanel beginSheetWithPrintInfo:ep->printInfo
316 modalForWindow:windowRef
317 delegate:delegate
318 didEndSelector:@selector(printPanelDidEnd:returnCode:contextInfo:)
319 contextInfo:this];
320 }
321
322 macStopInterceptWindowTitle();
323}
324
325void QPrintDialogPrivate::closeCocoaPrintPanel()
326{
327 // ###
328}
329#endif
330
331static bool warnIfNotNative(QPrinter *printer)
332{
333 if (printer->outputFormat() != QPrinter::NativeFormat) {
334 qWarning("QPrintDialog: Cannot be used on non-native printers");
335 return false;
336 }
337 return true;
338}
339
340
341QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
342 : QAbstractPrintDialog(*(new QPrintDialogPrivate), printer, parent)
343{
344 Q_D(QPrintDialog);
345 if (!warnIfNotNative(d->printer))
346 return;
347 d->ep = static_cast<QMacPrintEngine *>(d->printer->paintEngine())->d_func();
348}
349
350QPrintDialog::QPrintDialog(QWidget *parent)
351 : QAbstractPrintDialog(*(new QPrintDialogPrivate), 0, parent)
352{
353 Q_D(QPrintDialog);
354 if (!warnIfNotNative(d->printer))
355 return;
356 d->ep = static_cast<QMacPrintEngine *>(d->printer->paintEngine())->d_func();
357}
358
359QPrintDialog::~QPrintDialog()
360{
361}
362
363int QPrintDialog::exec()
364{
365 Q_D(QPrintDialog);
366 if (!warnIfNotNative(d->printer))
367 return QDialog::Rejected;
368
369#ifndef QT_MAC_USE_COCOA
370 d->openCarbonPrintPanel(Qt::ApplicationModal);
371#else
372 QMacCocoaAutoReleasePool pool;
373
374 d->openCocoaPrintPanel(Qt::ApplicationModal);
375 d->closeCocoaPrintPanel();
376#endif
377 return result();
378}
379
380#ifdef QT3_SUPPORT
381QPrinter *QPrintDialog::printer() const
382{
383 Q_D(const QPrintDialog);
384 return d->printer;
385}
386#endif
387
388/*!
389 \reimp
390*/
391void QPrintDialog::setVisible(bool visible)
392{
393 Q_D(QPrintDialog);
394
395 bool isCurrentlyVisible = (d->printPanel != 0);
396
397 if (!visible == !isCurrentlyVisible)
398 return;
399
400 if (d->printer->outputFormat() != QPrinter::NativeFormat)
401 return;
402
403 if (visible) {
404#ifndef QT_MAC_USE_COCOA
405 d->openCarbonPrintPanel(parentWidget() ? Qt::WindowModal
406 : Qt::ApplicationModal);
407#else
408 d->openCocoaPrintPanel(parentWidget() ? Qt::WindowModal
409 : Qt::ApplicationModal);
410#endif
411 return;
412 } else {
413 if (d->printPanel) {
414#ifndef QT_MAC_USE_COCOA
415 d->closeCarbonPrintPanel();
416#else
417 d->closeCocoaPrintPanel();
418#endif
419 return;
420 }
421 }
422}
423
424QT_END_NAMESPACE
425
426#include "moc_qprintdialog.cpp"
427
428#endif // QT_NO_PRINTDIALOG
Note: See TracBrowser for help on using the repository browser.