src/gui/dialogs/qprintdialog_mac.mm
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the QtGui module of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 
       
   230 #ifndef QT_MAC_USE_COCOA
       
   231 QHash<PMPrintSession, QPrintDialogPrivate *> QPrintDialogPrivate::sheetCallbackMap;
       
   232 void QPrintDialogPrivate::openCarbonPrintPanel(Qt::WindowModality modality)
       
   233 {
       
   234     Q_Q(QPrintDialog);
       
   235     initBeforeRun();
       
   236     sheetCallbackMap.insert(ep->session, this);
       
   237     if (modality == Qt::ApplicationModal) {
       
   238         QWidget modal_widg(0, Qt::Window);
       
   239         modal_widg.setObjectName(QLatin1String(__FILE__ "__modal_dlg"));
       
   240         modal_widg.createWinId();
       
   241         QApplicationPrivate::enterModal(&modal_widg);
       
   242         QApplicationPrivate::native_modal_dialog_active = true;
       
   243         Boolean acceptStatus;
       
   244         PMSessionPrintDialog(ep->session, ep->settings, ep->format, &acceptStatus);
       
   245         QApplicationPrivate::leaveModal(&modal_widg);
       
   246         QApplicationPrivate::native_modal_dialog_active = false;
       
   247         printDialogSheetDoneCallback(ep->session, 0, acceptStatus);
       
   248     } else {
       
   249         // Window Modal means that we use a sheet at the moment, there's no other way to do it correctly.
       
   250         if (!upp)
       
   251             upp = NewPMSheetDoneUPP(QPrintDialogPrivate::printDialogSheetDoneCallback);
       
   252         PMSessionUseSheets(ep->session, qt_mac_window_for(q->parentWidget()), upp);
       
   253         QApplicationPrivate::native_modal_dialog_active = true;
       
   254         Boolean unused;
       
   255         PMSessionPrintDialog(ep->session, ep->settings, ep->format, &unused);
       
   256     }
       
   257 }
       
   258 
       
   259 void QPrintDialogPrivate::closeCarbonPrintPanel()
       
   260 {
       
   261     Q_Q(QPrintDialog);
       
   262     QApplicationPrivate::native_modal_dialog_active = false;
       
   263     if (q->result() == QDialog::Accepted) {
       
   264         UInt32 frompage, topage;
       
   265         PMGetFirstPage(ep->settings, &frompage);
       
   266         PMGetLastPage(ep->settings, &topage);
       
   267         topage = qMin(UInt32(INT_MAX), topage);
       
   268         q->setFromTo(frompage, topage);
       
   269 
       
   270         // OK, I need to map these values back let's see
       
   271         // If from is 1 and to is INT_MAX, then print it all
       
   272         // (Apologies to the folks with more than INT_MAX pages)
       
   273         // ...that's a joke.
       
   274         if (q->fromPage() == 1 && q->toPage() == INT_MAX) {
       
   275             q->setPrintRange(QAbstractPrintDialog::AllPages);
       
   276             q->setFromTo(0,0);
       
   277         } else {
       
   278             q->setPrintRange(QAbstractPrintDialog::PageRange); // In a way a lie, but it shouldn't hurt.
       
   279             // Carbon hands us back a very large number here even for ALL, set it to max
       
   280             // in that case to follow the behavior of the other print dialogs.
       
   281             if (q->maxPage() < q->toPage())
       
   282                 q->setFromTo(q->fromPage(), q->maxPage());
       
   283         }
       
   284         // Keep us in sync with file output
       
   285         PMDestinationType dest;
       
   286         PMSessionGetDestinationType(ep->session, ep->settings, &dest);
       
   287         if (dest == kPMDestinationFile) {
       
   288             QCFType<CFURLRef> file;
       
   289             PMSessionCopyDestinationLocation(ep->session, ep->settings, &file);
       
   290             UInt8 localFile[2048];  // Assuming there's a POSIX file system here.
       
   291             CFURLGetFileSystemRepresentation(file, true, localFile, sizeof(localFile));
       
   292             ep->outputFilename = QString::fromUtf8(reinterpret_cast<const char *>(localFile));
       
   293         } else {
       
   294             ep->outputFilename = QString();
       
   295         }
       
   296     }
       
   297     sheetCallbackMap.remove(ep->session);
       
   298 }
       
   299 #else
       
   300 void QPrintDialogPrivate::openCocoaPrintPanel(Qt::WindowModality modality)
       
   301 {
       
   302     Q_Q(QPrintDialog);
       
   303 
       
   304     initBeforeRun();
       
   305 
       
   306     QPrintDialog::PrintDialogOptions qtOptions = q->options();
       
   307     NSPrintPanelOptions macOptions = NSPrintPanelShowsCopies;
       
   308     if (qtOptions & QPrintDialog::PrintPageRange)
       
   309         macOptions |= NSPrintPanelShowsPageRange;
       
   310     if (qtOptions & QPrintDialog::PrintShowPageSize)
       
   311         macOptions |= NSPrintPanelShowsPaperSize | NSPrintPanelShowsPageSetupAccessory
       
   312                       | NSPrintPanelShowsOrientation;
       
   313 
       
   314     macStartInterceptWindowTitle(q);
       
   315     printPanel = [NSPrintPanel printPanel];
       
   316     QCocoaPrintPanelDelegate *delegate = [[QCocoaPrintPanelDelegate alloc] init];
       
   317     [printPanel setOptions:macOptions];
       
   318 
       
   319     if (modality == Qt::ApplicationModal) {
       
   320         int rval = [printPanel runModalWithPrintInfo:ep->printInfo];
       
   321         [delegate printPanelDidEnd:printPanel returnCode:rval contextInfo:this];
       
   322     } else {
       
   323         Q_ASSERT(q->parentWidget());
       
   324         NSWindow *windowRef = qt_mac_window_for(q->parentWidget());
       
   325         [printPanel beginSheetWithPrintInfo:ep->printInfo
       
   326                              modalForWindow:windowRef
       
   327                                    delegate:delegate
       
   328                              didEndSelector:@selector(printPanelDidEnd:returnCode:contextInfo:)
       
   329                                 contextInfo:this];
       
   330     }
       
   331 
       
   332     macStopInterceptWindowTitle();
       
   333 }
       
   334 
       
   335 void QPrintDialogPrivate::closeCocoaPrintPanel()
       
   336 {
       
   337     // ###
       
   338 }
       
   339 #endif
       
   340 
       
   341 static bool warnIfNotNative(QPrinter *printer)
       
   342 {
       
   343     if (printer->outputFormat() != QPrinter::NativeFormat) {
       
   344         qWarning("QPrintDialog: Cannot be used on non-native printers");
       
   345         return false;
       
   346     }
       
   347     return true;
       
   348 }
       
   349 
       
   350 
       
   351 QPrintDialog::QPrintDialog(QPrinter *printer, QWidget *parent)
       
   352     : QAbstractPrintDialog(*(new QPrintDialogPrivate), printer, 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 
       
   360 QPrintDialog::QPrintDialog(QWidget *parent)
       
   361     : QAbstractPrintDialog(*(new QPrintDialogPrivate), 0, parent)
       
   362 {
       
   363     Q_D(QPrintDialog);
       
   364     if (!warnIfNotNative(d->printer))
       
   365         return;
       
   366     d->ep = static_cast<QMacPrintEngine *>(d->printer->paintEngine())->d_func();
       
   367 }
       
   368 
       
   369 QPrintDialog::~QPrintDialog()
       
   370 {
       
   371 }
       
   372 
       
   373 int QPrintDialog::exec()
       
   374 {
       
   375     Q_D(QPrintDialog);
       
   376     if (!warnIfNotNative(d->printer))
       
   377         return QDialog::Rejected;
       
   378 
       
   379 #ifndef QT_MAC_USE_COCOA
       
   380     d->openCarbonPrintPanel(Qt::ApplicationModal);
       
   381 #else
       
   382     QMacCocoaAutoReleasePool pool;
       
   383 
       
   384     d->openCocoaPrintPanel(Qt::ApplicationModal);
       
   385     d->closeCocoaPrintPanel();
       
   386 #endif
       
   387     return result();
       
   388 }
       
   389 
       
   390 #ifdef QT3_SUPPORT
       
   391 QPrinter *QPrintDialog::printer() const
       
   392 {
       
   393     Q_D(const QPrintDialog);
       
   394     return d->printer;
       
   395 }
       
   396 #endif
       
   397 
       
   398 /*!
       
   399     \reimp
       
   400 */
       
   401 void QPrintDialog::setVisible(bool visible)
       
   402 {
       
   403     Q_D(QPrintDialog);
       
   404 
       
   405     bool isCurrentlyVisible = (d->printPanel != 0);
       
   406 
       
   407     if (!visible == !isCurrentlyVisible)
       
   408         return;
       
   409 
       
   410     if (d->printer->outputFormat() != QPrinter::NativeFormat)
       
   411         return;
       
   412 
       
   413     if (visible) {
       
   414 #ifndef QT_MAC_USE_COCOA
       
   415         d->openCarbonPrintPanel(parentWidget() ? Qt::WindowModal
       
   416                                                : Qt::ApplicationModal);
       
   417 #else
       
   418         d->openCocoaPrintPanel(parentWidget() ? Qt::WindowModal
       
   419                                               : Qt::ApplicationModal);
       
   420 #endif
       
   421         return;
       
   422     } else {
       
   423         if (d->printPanel) {
       
   424 #ifndef QT_MAC_USE_COCOA
       
   425             d->closeCarbonPrintPanel();
       
   426 #else
       
   427             d->closeCocoaPrintPanel();
       
   428 #endif
       
   429             return;
       
   430         }
       
   431     }
       
   432 }
       
   433 
       
   434 QT_END_NAMESPACE
       
   435 
       
   436 #include "moc_qprintdialog.cpp"
       
   437 
       
   438 #endif // QT_NO_PRINTDIALOG