Qt 4.8
qprintdialog_win.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QT_NO_PRINTDIALOG
43 
44 #include "qprintdialog.h"
45 
46 #include <qwidget.h>
47 #include <qapplication.h>
48 #include <qmessagebox.h>
49 #include <private/qapplication_p.h>
50 
51 #include <private/qabstractprintdialog_p.h>
52 #include <private/qprintengine_win_p.h>
53 #include <private/qprinter_p.h>
54 
55 #if !defined(PD_NOCURRENTPAGE)
56 #define PD_NOCURRENTPAGE 0x00800000
57 #define PD_RESULT_PRINT 1
58 #define PD_RESULT_APPLY 2
59 #define START_PAGE_GENERAL 0XFFFFFFFF
60 #endif
61 
63 
64 extern void qt_win_eatMouseMove();
65 
67 {
69 public:
71  : ep(0)
72  {
73  }
74 
75  inline void _q_printToFileChanged(int) {}
76  inline void _q_rbPrintRangeToggled(bool) {}
77  inline void _q_printerChanged(int) {}
78  inline void _q_chbPrintLastFirstToggled(bool) {}
79  inline void _q_paperSizeChanged(int) {}
80  inline void _q_btnBrowseClicked() {}
81  inline void _q_btnPropertiesClicked() {}
83 
85 };
86 
87 static void qt_win_setup_PRINTDLGEX(PRINTDLGEX *pd, QWidget *parent,
88  QPrintDialog *pdlg,
89  QPrintDialogPrivate *d, HGLOBAL *tempDevNames)
90 {
91  DEVMODE *devMode = d->ep->devMode;
92 
93  if (devMode) {
94  int size = sizeof(DEVMODE) + devMode->dmDriverExtra;
95  pd->hDevMode = GlobalAlloc(GHND, size);
96  {
97  void *dest = GlobalLock(pd->hDevMode);
98  memcpy(dest, devMode, size);
99  GlobalUnlock(pd->hDevMode);
100  }
101  } else {
102  pd->hDevMode = NULL;
103  }
104  pd->hDevNames = tempDevNames;
105 
106  pd->Flags = PD_RETURNDC;
107  pd->Flags |= PD_USEDEVMODECOPIESANDCOLLATE;
108 
110  pd->Flags |= PD_NOSELECTION;
112  pd->nMinPage = pdlg->minPage();
113  pd->nMaxPage = pdlg->maxPage();
114  }
115 
117  pd->Flags |= PD_DISABLEPRINTTOFILE;
118 
120  pd->Flags |= PD_SELECTION;
122  pd->Flags |= PD_PAGENUMS;
124  pd->Flags |= PD_CURRENTPAGE;
125  else
126  pd->Flags |= PD_ALLPAGES;
127 
128  // As stated by MSDN, to enable collate option when minpage==maxpage==0
129  // set the PD_NOPAGENUMS flag
130  if (pd->nMinPage==0 && pd->nMaxPage==0)
131  pd->Flags |= PD_NOPAGENUMS;
132 
133  // Disable Current Page option if not required as default is Enabled
135  pd->Flags |= PD_NOCURRENTPAGE;
136 
137  // Default to showing the General tab first
138  pd->nStartPage = START_PAGE_GENERAL;
139 
140  // We don't support more than one page range in the QPrinter API yet.
141  pd->nPageRanges = 1;
142  pd->nMaxPageRanges = 1;
143 
144  if (d->ep->printToFile)
145  pd->Flags |= PD_PRINTTOFILE;
146  Q_ASSERT(parent);
147  pd->hwndOwner = parent->window()->winId();
148  pd->lpPageRanges[0].nFromPage = qMax(pdlg->fromPage(), pdlg->minPage());
149  pd->lpPageRanges[0].nToPage = (pdlg->toPage() > 0) ? qMin(pdlg->toPage(), pdlg->maxPage()) : 1;
150  pd->nCopies = d->ep->num_copies;
151 }
152 
154 {
155  if (pd->Flags & PD_SELECTION) {
157  pdlg->setFromTo(0, 0);
158  } else if (pd->Flags & PD_PAGENUMS) {
160  pdlg->setFromTo(pd->lpPageRanges[0].nFromPage, pd->lpPageRanges[0].nToPage);
161  } else if (pd->Flags & PD_CURRENTPAGE) {
163  pdlg->setFromTo(0, 0);
164  } else { // PD_ALLPAGES
166  pdlg->setFromTo(0, 0);
167  }
168 
169  d->ep->printToFile = (pd->Flags & PD_PRINTTOFILE) != 0;
170 
171  d->ep->readDevnames(pd->hDevNames);
172  d->ep->readDevmode(pd->hDevMode);
173  d->ep->updateCustomPaperSize();
174 
175  if (d->ep->printToFile && d->ep->fileName.isEmpty())
176  d->ep->fileName = d->ep->port;
177  else if (!d->ep->printToFile && d->ep->fileName == QLatin1String("FILE:"))
178  d->ep->fileName.clear();
179 }
180 
182 {
183  if (printer->outputFormat() != QPrinter::NativeFormat) {
184  qWarning("QPrintDialog: Cannot be used on non-native printers");
185  return false;
186  }
187  return true;
188 }
189 
191  : QAbstractPrintDialog( *(new QPrintDialogPrivate), printer, parent)
192 {
193  Q_D(QPrintDialog);
194  if (!warnIfNotNative(d->printer))
195  return;
196  d->ep = static_cast<QWin32PrintEngine *>(d->printer->paintEngine())->d_func();
197 }
198 
200  : QAbstractPrintDialog( *(new QPrintDialogPrivate), 0, parent)
201 {
202  Q_D(QPrintDialog);
203  if (!warnIfNotNative(d->printer))
204  return;
205  d->ep = static_cast<QWin32PrintEngine *>(d->printer->paintEngine())->d_func();
206 }
207 
209 {
210 }
211 
212 int QPrintDialog::exec()
213 {
214  if (!warnIfNotNative(printer()))
215  return 0;
216 
217  Q_D(QPrintDialog);
218  return d->openWindowsPrintDialogModally();
219 }
220 
222 {
223  Q_Q(QPrintDialog);
224  QWidget *parent = q->parentWidget();
225  if (parent)
226  parent = parent->window();
227  else
228  parent = QApplication::activeWindow();
229 
230  // If there is no window, fall back to the print dialog itself
231  if (parent == 0)
232  parent = q;
233 
234  QWidget modal_widget;
235  modal_widget.setAttribute(Qt::WA_NoChildEventsForParent, true);
236  modal_widget.setParent(parent, Qt::Window);
237  QApplicationPrivate::enterModal(&modal_widget);
238 
239  HGLOBAL *tempDevNames = ep->createDevNames();
240 
241  bool done;
242  bool result;
243  bool doPrinting;
244 
245  PRINTPAGERANGE pageRange;
246  PRINTDLGEX pd;
247  memset(&pd, 0, sizeof(PRINTDLGEX));
248  pd.lStructSize = sizeof(PRINTDLGEX);
249  pd.lpPageRanges = &pageRange;
250  qt_win_setup_PRINTDLGEX(&pd, parent, q, this, tempDevNames);
251 
252  do {
253  done = true;
254  doPrinting = false;
255  result = (PrintDlgEx(&pd) == S_OK);
256  if (result && (pd.dwResultAction == PD_RESULT_PRINT
257  || pd.dwResultAction == PD_RESULT_APPLY))
258  {
259  doPrinting = (pd.dwResultAction == PD_RESULT_PRINT);
260  if ((pd.Flags & PD_PAGENUMS)
261  && (pd.lpPageRanges[0].nFromPage > pd.lpPageRanges[0].nToPage))
262  {
263  pd.lpPageRanges[0].nFromPage = 1;
264  pd.lpPageRanges[0].nToPage = 1;
265  done = false;
266  }
267  if (pd.hDC == 0)
268  result = false;
269  }
270 
271  if (!done) {
273  QPrintDialog::tr("The 'From' value cannot be greater than the 'To' value."),
274  QPrintDialog::tr("OK"));
275  }
276  } while (!done);
277 
278  QApplicationPrivate::leaveModal(&modal_widget);
279 
281 
282  // write values back...
283  if (result && (pd.dwResultAction == PD_RESULT_PRINT
284  || pd.dwResultAction == PD_RESULT_APPLY))
285  {
286  qt_win_read_back_PRINTDLGEX(&pd, q, this);
287  // update printer validity
288  printer->d_func()->validPrinter = !ep->name.isEmpty();
289  }
290 
291  // Cleanup...
292  GlobalFree(tempDevNames);
293 
294  q->done(result && doPrinting);
295 
296  return result && doPrinting;
297 }
298 
299 void QPrintDialog::setVisible(bool visible)
300 {
301  Q_D(QPrintDialog);
302 
303  // its always modal, so we cannot hide a native print dialog
304  if (!visible)
305  return;
306 
307  if (!warnIfNotNative(d->printer))
308  return;
309 
310  (void)d->openWindowsPrintDialogModally();
311  return;
312 }
313 
315 
316 #include "moc_qprintdialog.cpp"
317 
318 #endif // QT_NO_PRINTDIALOG
double d
Definition: qnumeric_p.h:62
QWidget * parentWidget() const
Returns the parent of this widget, or 0 if it does not have any parent widget.
Definition: qwidget.h:1035
Q_DECL_CONSTEXPR const T & qMin(const T &a, const T &b)
Definition: qglobal.h:1215
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void setParent(QWidget *parent)
Sets the parent of the widget to parent, and resets the window flags.
Definition: qwidget.cpp:10479
#define PD_RESULT_APPLY
QMacPrintEnginePrivate * ep
bool isOptionEnabled(PrintDialogOption option) const
Use QPrintDialog::testOption(option) instead.
void qt_win_eatMouseMove()
static QWidget * activeWindow()
Returns the application top-level window that has the keyboard input focus, or 0 if no application wi...
The QWidget class is the base class of all user interface objects.
Definition: qwidget.h:150
static QString tr(const char *sourceText, const char *comment=0, int n=-1)
static void qt_win_read_back_PRINTDLGEX(PRINTDLGEX *pd, QPrintDialog *pdlg, QPrintDialogPrivate *d)
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
#define Q_D(Class)
Definition: qglobal.h:2482
int minPage() const
Returns the minimum page in the page range.
static StandardButton warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons=Ok, StandardButton defaultButton=NoButton)
Opens a warning message box with the given title and text in front of the specified parent widget...
Q_DECL_CONSTEXPR const T & qMax(const T &a, const T &b)
Definition: qglobal.h:1217
#define Q_Q(Class)
Definition: qglobal.h:2483
#define PD_NOPAGENUMS
~QPrintDialog()
Destroys the print dialog.
The QPrintDialog class provides a dialog for specifying the printer&#39;s configuration.
Definition: qprintdialog.h:81
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
OutputFormat outputFormat() const
Returns the output format for this printer.
Definition: qprinter.cpp:813
#define PD_NOSELECTION
The QPrinter class is a paint device that paints on a printer.
Definition: qprinter.h:66
PrintRange printRange() const
Returns the print range.
int fromPage() const
Returns the first page to be printed By default, this value is set to 0.
void _q_chbPrintLastFirstToggled(bool)
int toPage() const
Returns the last page to be printed.
Q_CORE_EXPORT void qWarning(const char *,...)
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static bool warnIfNotNative(QPrinter *printer)
void setFromTo(int fromPage, int toPage)
Sets the range in the print dialog to be from from to to.
int exec()
Reimplemented Function
#define PD_ALLPAGES
#define START_PAGE_GENERAL
int maxPage() const
Returns the maximum page in the page range.
The QAbstractPrintDialog class provides a base implementation for print dialogs used to configure pri...
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
if(void) toggleToolbarShown
void setAttribute(Qt::WidgetAttribute, bool on=true)
Sets the attribute attribute on this widget if on is true; otherwise clears the attribute.
Definition: qwidget.cpp:11087
QWidget * window() const
Returns the window for this widget, i.e.
Definition: qwidget.cpp:4492
QObject * parent
Definition: qobject.h:92
QWin32PrintEnginePrivate * ep
void setVisible(bool visible)
Reimplemented Function
void setPrintRange(PrintRange range)
Sets the print range option in to be range.
WId winId() const
Returns the window system identifier of the widget.
Definition: qwidget.cpp:2557
#define PD_NOCURRENTPAGE
static void leaveModal(QWidget *)
#define PD_RESULT_PRINT
void _q_rbPrintRangeToggled(bool)
static void qt_win_setup_PRINTDLGEX(PRINTDLGEX *pd, QWidget *parent, QPrintDialog *pdlg, QPrintDialogPrivate *d, HGLOBAL *tempDevNames)
QPrintDialog(QPrinter *printer, QWidget *parent=0)
Constructs a new modal printer dialog for the given printer with the given parent.
static void enterModal(QWidget *)