Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qprintdialog_win.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include <QtPrintSupport/qtprintsupportglobal.h>
5
6#include "qprintdialog.h"
7
8#include <qwidget.h>
9#include <qapplication.h>
10#include <qmessagebox.h>
11#include <private/qapplication_p.h>
12
14#include <private/qprintengine_win_p.h>
15#include "../kernel/qprinter_p.h"
16
17#if !defined(PD_NOCURRENTPAGE)
18#define PD_NOCURRENTPAGE 0x00800000
19#define PD_RESULT_PRINT 1
20#define PD_RESULT_APPLY 2
21#define START_PAGE_GENERAL 0XFFFFFFFF
22#endif
23
25
26using namespace Qt::StringLiterals;
27
28//extern void qt_win_eatMouseMove();
29
31{
32 Q_DECLARE_PUBLIC(QPrintDialog)
33public:
35 : engine(0), ep(0)
36 {
37 }
38
40
43};
44
45static void qt_win_setup_PRINTDLGEX(PRINTDLGEX *pd, QWidget *parent,
46 QPrintDialog *pdlg,
47 QPrintDialogPrivate *d, HGLOBAL *tempDevNames)
48{
49 DEVMODE *devMode = d->ep->devMode;
50
51 if (devMode) {
52 int size = sizeof(DEVMODE) + devMode->dmDriverExtra;
53 pd->hDevMode = GlobalAlloc(GHND, size);
54 {
55 void *dest = GlobalLock(pd->hDevMode);
56 memcpy(dest, devMode, size);
57 GlobalUnlock(pd->hDevMode);
58 }
59 } else {
60 pd->hDevMode = NULL;
61 }
62 pd->hDevNames = tempDevNames;
63
64 pd->Flags = PD_RETURNDC;
65 pd->Flags |= PD_USEDEVMODECOPIESANDCOLLATE;
66
68 pd->Flags |= PD_NOSELECTION;
70 pd->nMinPage = pdlg->minPage();
71 pd->nMaxPage = pdlg->maxPage();
72 }
73
75 pd->Flags |= PD_DISABLEPRINTTOFILE;
76
78 pd->Flags |= PD_SELECTION;
80 pd->Flags |= PD_PAGENUMS;
82 pd->Flags |= PD_CURRENTPAGE;
83 else
84 pd->Flags |= PD_ALLPAGES;
85
86 // As stated by MSDN, to enable collate option when minpage==maxpage==0
87 // set the PD_NOPAGENUMS flag
88 if (pd->nMinPage==0 && pd->nMaxPage==0)
89 pd->Flags |= PD_NOPAGENUMS;
90
91 // Disable Current Page option if not required as default is Enabled
93 pd->Flags |= PD_NOCURRENTPAGE;
94
95 // Default to showing the General tab first
96 pd->nStartPage = START_PAGE_GENERAL;
97
98 // We don't support more than one page range in the QPrinter API yet.
99 pd->nPageRanges = 1;
100 pd->nMaxPageRanges = 1;
101
102 if (d->ep->printToFile)
103 pd->Flags |= PD_PRINTTOFILE;
105 QWindow *parentWindow = parent->windowHandle();
106 pd->hwndOwner = parentWindow ? (HWND)QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow) : 0;
107 pd->lpPageRanges[0].nFromPage = qMax(pdlg->fromPage(), pdlg->minPage());
108 pd->lpPageRanges[0].nToPage = (pdlg->toPage() > 0) ? qMin(pdlg->toPage(), pdlg->maxPage()) : 1;
109 pd->nCopies = d->printer->copyCount();
110}
111
113{
114 if (pd->Flags & PD_SELECTION) {
117 } else if (pd->Flags & PD_PAGENUMS) {
119 pdlg->setFromTo(pd->lpPageRanges[0].nFromPage, pd->lpPageRanges[0].nToPage);
120 } else if (pd->Flags & PD_CURRENTPAGE) {
123 } else { // PD_ALLPAGES
126 }
127
128 d->ep->printToFile = (pd->Flags & PD_PRINTTOFILE) != 0;
129
130 d->engine->setGlobalDevMode(pd->hDevNames, pd->hDevMode);
131
132 if (d->ep->printToFile && d->ep->fileName.isEmpty())
133 d->ep->fileName = "FILE:"_L1;
134 else if (!d->ep->printToFile && d->ep->fileName == "FILE:"_L1)
135 d->ep->fileName.clear();
136}
137
138static bool warnIfNotNative(QPrinter *printer)
139{
140 if (printer->outputFormat() != QPrinter::NativeFormat) {
141 qWarning("QPrintDialog: Cannot be used on non-native printers");
142 return false;
143 }
144 return true;
145}
146
149{
150 Q_D(QPrintDialog);
151 if (!warnIfNotNative(d->printer))
152 return;
153 d->engine = static_cast<QWin32PrintEngine *>(d->printer->printEngine());
154 d->ep = static_cast<QWin32PrintEngine *>(d->printer->printEngine())->d_func();
156}
157
160{
161 Q_D(QPrintDialog);
162 if (!warnIfNotNative(d->printer))
163 return;
164 d->engine = static_cast<QWin32PrintEngine *>(d->printer->printEngine());
165 d->ep = static_cast<QWin32PrintEngine *>(d->printer->printEngine())->d_func();
167}
168
170{
171}
172
174{
175 if (!warnIfNotNative(printer()))
176 return 0;
177
178 Q_D(QPrintDialog);
179 return d->openWindowsPrintDialogModally();
180}
181
183{
184 Q_Q(QPrintDialog);
185 QWidget *parent = q->parentWidget();
186 if (parent)
187 parent = parent->window();
188 else
190
191 // If there is no window, fall back to the print dialog itself
192 if (parent == 0)
193 parent = q;
194
195 q->QDialog::setVisible(true);
196
197 HGLOBAL *tempDevNames = engine->createGlobalDevNames();
198
199 bool done;
200 bool result;
201 bool doPrinting;
202
203 PRINTPAGERANGE pageRange;
204 PRINTDLGEX pd;
205 memset(&pd, 0, sizeof(PRINTDLGEX));
206 pd.lStructSize = sizeof(PRINTDLGEX);
207 pd.lpPageRanges = &pageRange;
208 qt_win_setup_PRINTDLGEX(&pd, parent, q, this, tempDevNames);
209
210 do {
211 done = true;
212 doPrinting = false;
213 result = (PrintDlgEx(&pd) == S_OK);
214 if (result && (pd.dwResultAction == PD_RESULT_PRINT
215 || pd.dwResultAction == PD_RESULT_APPLY))
216 {
217 doPrinting = (pd.dwResultAction == PD_RESULT_PRINT);
218 if ((pd.Flags & PD_PAGENUMS)
219 && (pd.lpPageRanges[0].nFromPage > pd.lpPageRanges[0].nToPage))
220 {
221 pd.lpPageRanges[0].nFromPage = 1;
222 pd.lpPageRanges[0].nToPage = 1;
223 done = false;
224 }
225 if (pd.hDC == 0)
226 result = false;
227 }
228
229 if (!done) {
230 QMessageBox::warning(nullptr,
231 QPrintDialog::tr("Print"),
232 QPrintDialog::tr("The 'From' value cannot be greater than the 'To' value."));
233 }
234 } while (!done);
235
236 q->QDialog::setVisible(false);
237
238// qt_win_eatMouseMove();
239
240 // write values back...
241 if (result && (pd.dwResultAction == PD_RESULT_PRINT
242 || pd.dwResultAction == PD_RESULT_APPLY))
243 {
245 // update printer validity
246 printer->d_func()->validPrinter = !printer->printerName().isEmpty();
247 }
248
249 // Cleanup...
250 GlobalFree(tempDevNames);
251
252 q->done(result && doPrinting);
253
254 return result && doPrinting;
255}
256
257void QPrintDialog::setVisible(bool visible)
258{
259 Q_D(QPrintDialog);
260
261 // its always modal, so we cannot hide a native print dialog
262 if (!visible)
263 return;
264
265 if (!warnIfNotNative(d->printer))
266 return;
267
268 (void)d->openWindowsPrintDialogModally();
269 return;
270}
271
273
274#include "moc_qprintdialog.cpp"
The QAbstractPrintDialog class provides a base implementation for print dialogs used to configure pri...
PrintRange printRange() const
Returns the print range.
void setFromTo(int fromPage, int toPage)
Sets the range in the print dialog to be from from to to.
int toPage() const
Returns the last page to be printed.
int maxPage() const
Returns the maximum page in the page range.
int minPage() const
Returns the minimum page in the page range.
QPrinter * printer() const
Returns the printer that this printer dialog operates on.
int fromPage() const
Returns the first page to be printed By default, this value is set to 0.
void setPrintRange(PrintRange range)
Sets the print range option in to be range.
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or \nullptr if no applica...
static QPlatformNativeInterface * platformNativeInterface()
static StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
The QPageRanges class represents a collection of page ranges.
Definition qpageranges.h:21
virtual void setPageRanges(const QPageRanges &ranges)
virtual void * nativeResourceForWindow(const QByteArray &resource, QWindow *window)
QWin32PrintEnginePrivate * ep
QWin32PrintEngine * engine
The QPrintDialog class provides a dialog for specifying the printer's configuration.
int exec() override
\reimp
~QPrintDialog()
Destroys the print dialog.
bool testOption(PrintDialogOption option) const
Returns true if the given option is enabled; otherwise, returns false.
QPrintDialog(QPrinter *printer, QWidget *parent=nullptr)
Constructs a new modal printer dialog for the given printer with the given parent.
\reentrant
Definition qprinter.h:28
@ NativeFormat
Definition qprinter.h:69
QString printerName() const
Returns the printer name.
Definition qprinter.cpp:616
OutputFormat outputFormat() const
Definition qprinter.cpp:565
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition qstring.h:1083
The QWidget class is the base class of all user interface objects.
Definition qwidget.h:99
virtual void setVisible(bool visible)
Definition qwidget.cpp:8329
bool visible
whether the widget is visible
Definition qwidget.h:144
HGLOBAL * createGlobalDevNames()
\inmodule QtGui
Definition qwindow.h:63
Combined button and popup list for selecting options.
@ WA_DontShowOnScreen
Definition qnamespace.h:382
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
#define qWarning
Definition qlogging.h:162
constexpr const T & qMin(const T &a, const T &b)
Definition qminmax.h:40
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
GLuint64EXT * result
[6]
static bool warnIfNotNative(QPrinter *printer)
static void qt_win_setup_PRINTDLGEX(PRINTDLGEX *pd, QWidget *parent, QPrintDialog *pdlg, QPrintDialogPrivate *d, HGLOBAL *tempDevNames)
#define START_PAGE_GENERAL
#define PD_RESULT_PRINT
#define PD_NOCURRENTPAGE
static void qt_win_read_back_PRINTDLGEX(PRINTDLGEX *pd, QPrintDialog *pdlg, QPrintDialogPrivate *d)
#define PD_RESULT_APPLY
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
app setAttribute(Qt::AA_DontShowIconsInMenus)
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent