1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qpagesetupdialog.h"
43 
44 #ifndef QT_NO_PRINTDIALOG
45 #include <qapplication.h>
46 
47 #include <private/qprintengine_win_p.h>
48 #include <private/qabstractpagesetupdialog_p.h>
49 
50 QT_BEGIN_NAMESPACE
51 
52 class QPageSetupDialogPrivate : public QAbstractPageSetupDialogPrivate
53 {
54 };
55 
QPageSetupDialog(QPrinter * printer,QWidget * parent)56 QPageSetupDialog::QPageSetupDialog(QPrinter *printer, QWidget *parent)
57     : QAbstractPageSetupDialog(*(new QPageSetupDialogPrivate), printer, parent)
58 {
59 }
60 
QPageSetupDialog(QWidget * parent)61 QPageSetupDialog::QPageSetupDialog(QWidget *parent)
62     : QAbstractPageSetupDialog(*(new QPageSetupDialogPrivate), 0, parent)
63 {
64 }
65 
exec()66 int QPageSetupDialog::exec()
67 {
68     Q_D(QPageSetupDialog);
69 
70     if (d->printer->outputFormat() != QPrinter::NativeFormat)
71         return Rejected;
72 
73     QWin32PrintEngine *engine = static_cast<QWin32PrintEngine*>(d->printer->paintEngine());
74     QWin32PrintEnginePrivate *ep = static_cast<QWin32PrintEnginePrivate *>(engine->d_ptr.data());
75 
76     PAGESETUPDLG psd;
77     memset(&psd, 0, sizeof(PAGESETUPDLG));
78     psd.lStructSize = sizeof(PAGESETUPDLG);
79 
80     // we need a temp DEVMODE struct if we don't have a global DEVMODE
81     HGLOBAL hDevMode = 0;
82     int devModeSize = 0;
83     if (!ep->globalDevMode) {
84         devModeSize = sizeof(DEVMODE) + ep->devMode->dmDriverExtra;
85         hDevMode = GlobalAlloc(GHND, devModeSize);
86         if (hDevMode) {
87             void *dest = GlobalLock(hDevMode);
88             memcpy(dest, ep->devMode, devModeSize);
89             GlobalUnlock(hDevMode);
90         }
91         psd.hDevMode = hDevMode;
92     } else {
93         psd.hDevMode = ep->devMode;
94     }
95 
96     HGLOBAL *tempDevNames = ep->createDevNames();
97     psd.hDevNames = tempDevNames;
98 
99     QWidget *parent = parentWidget();
100     parent = parent ? parent->window() : QApplication::activeWindow();
101     Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created));
102     psd.hwndOwner = parent ? parent->winId() : 0;
103 
104     psd.Flags = PSD_MARGINS;
105     double multiplier = 1;
106     switch (QLocale::system().measurementSystem()) {
107     case QLocale::MetricSystem:
108         psd.Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
109         multiplier = 1;
110         break;
111     case QLocale::ImperialSystem:
112         psd.Flags |= PSD_INTHOUSANDTHSOFINCHES;
113         multiplier = 25.4/10;
114         break;
115     }
116 
117     QRect marginRect = ep->getPageMargins();
118     psd.rtMargin.left   = marginRect.left()   / multiplier;
119     psd.rtMargin.top    = marginRect.top()    / multiplier;
120     psd.rtMargin.right  = marginRect.width()  / multiplier;;
121     psd.rtMargin.bottom = marginRect.height() / multiplier;;
122 
123     bool result = PageSetupDlg(&psd);
124     if (result) {
125         ep->readDevnames(psd.hDevNames);
126         ep->readDevmode(psd.hDevMode);
127 
128         QRect theseMargins = QRect(psd.rtMargin.left   * multiplier,
129                                    psd.rtMargin.top    * multiplier,
130                                    psd.rtMargin.right  * multiplier,
131                                    psd.rtMargin.bottom * multiplier);
132 
133         if (theseMargins != marginRect) {
134             ep->setPageMargins(psd.rtMargin.left   * multiplier,
135                                psd.rtMargin.top    * multiplier,
136                                psd.rtMargin.right  * multiplier,
137                                psd.rtMargin.bottom * multiplier);
138         }
139 
140         ep->updateCustomPaperSize();
141 
142         // copy from our temp DEVMODE struct
143         if (!ep->globalDevMode && hDevMode) {
144             void *src = GlobalLock(hDevMode);
145             memcpy(ep->devMode, src, devModeSize);
146             GlobalUnlock(hDevMode);
147         }
148     }
149 
150     if (!ep->globalDevMode && hDevMode)
151         GlobalFree(hDevMode);
152     GlobalFree(tempDevNames);
153     done(result);
154     return result;
155 }
156 
setVisible(bool visible)157 void QPageSetupDialog::setVisible(bool visible)
158 {
159     if (!visible)
160         return;
161     exec();
162 }
163 
164 QT_END_NAMESPACE
165 #endif
166