1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 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 |
|
---|
52 | QT_BEGIN_NAMESPACE
|
---|
53 |
|
---|
54 | class QPrintDialogPrivate : public QAbstractPrintDialogPrivate
|
---|
55 | {
|
---|
56 | Q_DECLARE_PUBLIC(QPrintDialog)
|
---|
57 |
|
---|
58 | public:
|
---|
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 |
|
---|
121 | QT_END_NAMESPACE
|
---|
122 |
|
---|
123 | QT_USE_NAMESPACE
|
---|
124 |
|
---|
125 | #ifdef QT_MAC_USE_COCOA
|
---|
126 |
|
---|
127 | @class QCocoaPrintPanelDelegate;
|
---|
128 |
|
---|
129 | @interface QCocoaPrintPanelDelegate : NSObject {
|
---|
130 | }
|
---|
131 | - (void)printPanelDidEnd:(NSPrintPanel *)printPanel
|
---|
132 | returnCode:(int)returnCode contextInfo:(void *)contextInfo;
|
---|
133 | @end
|
---|
134 |
|
---|
135 | @implementation 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 | // temporary hack to work around bug in deleteLater() in Qt/Mac Cocoa
|
---|
144 | #if 1
|
---|
145 | bool deleteDialog = dialog->testAttribute(Qt::WA_DeleteOnClose);
|
---|
146 | dialog->setAttribute(Qt::WA_DeleteOnClose, false);
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | if (returnCode == NSOKButton) {
|
---|
150 | UInt32 frompage, topage;
|
---|
151 | PMGetFirstPage(d->ep->settings, &frompage);
|
---|
152 | PMGetLastPage(d->ep->settings, &topage);
|
---|
153 | topage = qMin(UInt32(INT_MAX), topage);
|
---|
154 | dialog->setFromTo(frompage, topage);
|
---|
155 |
|
---|
156 | // OK, I need to map these values back let's see
|
---|
157 | // If from is 1 and to is INT_MAX, then print it all
|
---|
158 | // (Apologies to the folks with more than INT_MAX pages)
|
---|
159 | if (dialog->fromPage() == 1 && dialog->toPage() == INT_MAX) {
|
---|
160 | dialog->setPrintRange(QPrintDialog::AllPages);
|
---|
161 | dialog->setFromTo(0, 0);
|
---|
162 | } else {
|
---|
163 | dialog->setPrintRange(QPrintDialog::PageRange); // In a way a lie, but it shouldn't hurt.
|
---|
164 | // Carbon hands us back a very large number here even for ALL, set it to max
|
---|
165 | // in that case to follow the behavior of the other print dialogs.
|
---|
166 | if (dialog->maxPage() < dialog->toPage())
|
---|
167 | dialog->setFromTo(dialog->fromPage(), dialog->maxPage());
|
---|
168 | }
|
---|
169 | // Keep us in sync with file output
|
---|
170 | PMDestinationType dest;
|
---|
171 |
|
---|
172 | // If the user selected print to file, the session has been
|
---|
173 | // changed behind our back and our d->ep->session object is a
|
---|
174 | // dangling pointer. Update it based on the "current" session
|
---|
175 | d->ep->session = static_cast<PMPrintSession>([d->ep->printInfo PMPrintSession]);
|
---|
176 |
|
---|
177 | PMSessionGetDestinationType(d->ep->session, d->ep->settings, &dest);
|
---|
178 | if (dest == kPMDestinationFile) {
|
---|
179 | QCFType<CFURLRef> file;
|
---|
180 | PMSessionCopyDestinationLocation(d->ep->session, d->ep->settings, &file);
|
---|
181 | UInt8 localFile[2048]; // Assuming there's a POSIX file system here.
|
---|
182 | CFURLGetFileSystemRepresentation(file, true, localFile, sizeof(localFile));
|
---|
183 | d->ep->outputFilename
|
---|
184 | = QString::fromUtf8(reinterpret_cast<const char *>(localFile));
|
---|
185 | } else {
|
---|
186 | // Keep output format.
|
---|
187 | QPrinter::OutputFormat format;
|
---|
188 | format = d->printer->outputFormat();
|
---|
189 | d->printer->setOutputFileName(QString());
|
---|
190 | d->printer->setOutputFormat(format);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | dialog->done((returnCode == NSOKButton) ? QDialog::Accepted : QDialog::Rejected);
|
---|
195 | #if 1
|
---|
196 | if (deleteDialog)
|
---|
197 | delete dialog;
|
---|
198 | #endif
|
---|
199 | }
|
---|
200 | @end
|
---|
201 |
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | QT_BEGIN_NAMESPACE
|
---|
205 |
|
---|
206 | extern void macStartInterceptWindowTitle(QWidget *window);
|
---|
207 | extern void macStopInterceptWindowTitle();
|
---|
208 |
|
---|
209 |
|
---|
210 | void QPrintDialogPrivate::initBeforeRun()
|
---|
211 | {
|
---|
212 | Q_Q(QPrintDialog);
|
---|
213 | // If someone is reusing a QPrinter object, the end released all our old
|
---|
214 | // information. In this case, we must reinitialize.
|
---|
215 | if (ep->session == 0)
|
---|
216 | ep->initialize();
|
---|
217 |
|
---|
218 |
|
---|
219 | // It seems the only way that PM lets you use all is if the minimum
|
---|
220 | // for the page range is 1. This _kind of_ makes sense if you think about
|
---|
221 | // it. However, calling PMSetFirstPage() or PMSetLastPage() always enforces
|
---|
222 | // the range.
|
---|
223 | PMSetPageRange(ep->settings, q->minPage(), q->maxPage());
|
---|
224 | if (q->printRange() == QAbstractPrintDialog::PageRange) {
|
---|
225 | PMSetFirstPage(ep->settings, q->fromPage(), false);
|
---|
226 | PMSetLastPage(ep->settings, q->toPage(), false);
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
|
---|