[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 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 | **
|
---|
[561] | 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.
|
---|
[2] | 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 | **
|
---|
[561] | 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
[2] | 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 |
|
---|
[561] | 125 | #ifdef QT_MAC_USE_COCOA
|
---|
| 126 |
|
---|
[846] | 127 | @class QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate);
|
---|
[2] | 128 |
|
---|
[846] | 129 | @interface QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate) : NSObject {
|
---|
[2] | 130 | }
|
---|
| 131 | - (void)printPanelDidEnd:(NSPrintPanel *)printPanel
|
---|
| 132 | returnCode:(int)returnCode contextInfo:(void *)contextInfo;
|
---|
| 133 | @end
|
---|
| 134 |
|
---|
[846] | 135 | @implementation QT_MANGLE_NAMESPACE(QCocoaPrintPanelDelegate)
|
---|
[2] | 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;
|
---|
[561] | 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 |
|
---|
[2] | 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 |
|
---|
[561] | 193 | #endif
|
---|
| 194 |
|
---|
[2] | 195 | QT_BEGIN_NAMESPACE
|
---|
| 196 |
|
---|
| 197 | extern void macStartInterceptWindowTitle(QWidget *window);
|
---|
| 198 | extern void macStopInterceptWindowTitle();
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | void 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
|
---|
| 222 | QHash<PMPrintSession, QPrintDialogPrivate *> QPrintDialogPrivate::sheetCallbackMap;
|
---|
| 223 | void 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 |
|
---|
| 250 | void 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);
|
---|
| |
---|