PageRenderTime 36ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/qt/qtbase/src/printsupport/dialogs/qpagesetupdialog_win.cpp

https://code.google.com/
C++ | 167 lines | 105 code | 20 blank | 42 comment | 16 complexity | 38b69e606c62f7b7d8884fb3e849a83c MD5 | raw file
Possible License(s): LGPL-3.0, CC-BY-SA-4.0, MIT, AGPL-3.0, BSD-3-Clause, LGPL-2.1, CC0-1.0, GPL-2.0, LGPL-2.0, GPL-3.0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 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. #include "qpagesetupdialog.h"
  42. #ifndef QT_NO_PRINTDIALOG
  43. #include <qapplication.h>
  44. #include "../kernel/qprintengine_win_p.h"
  45. #include "qpagesetupdialog_p.h"
  46. #include "qprinter.h"
  47. #include <qpa/qplatformnativeinterface.h>
  48. QT_BEGIN_NAMESPACE
  49. QPageSetupDialog::QPageSetupDialog(QPrinter *printer, QWidget *parent)
  50. : QDialog(*(new QPageSetupDialogPrivate(printer)), parent)
  51. {
  52. setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
  53. setAttribute(Qt::WA_DontShowOnScreen);
  54. }
  55. QPageSetupDialog::QPageSetupDialog(QWidget *parent)
  56. : QDialog(*(new QPageSetupDialogPrivate(0)), parent)
  57. {
  58. setWindowTitle(QCoreApplication::translate("QPrintPreviewDialog", "Page Setup"));
  59. setAttribute(Qt::WA_DontShowOnScreen);
  60. }
  61. int QPageSetupDialog::exec()
  62. {
  63. Q_D(QPageSetupDialog);
  64. if (d->printer->outputFormat() != QPrinter::NativeFormat)
  65. return Rejected;
  66. QWin32PrintEngine *engine = static_cast<QWin32PrintEngine*>(d->printer->paintEngine());
  67. QWin32PrintEnginePrivate *ep = static_cast<QWin32PrintEnginePrivate *>(engine->d_ptr.data());
  68. PAGESETUPDLG psd;
  69. memset(&psd, 0, sizeof(PAGESETUPDLG));
  70. psd.lStructSize = sizeof(PAGESETUPDLG);
  71. // we need a temp DEVMODE struct if we don't have a global DEVMODE
  72. HGLOBAL hDevMode = 0;
  73. int devModeSize = 0;
  74. if (!engine->globalDevMode()) {
  75. devModeSize = sizeof(DEVMODE) + ep->devMode->dmDriverExtra;
  76. hDevMode = GlobalAlloc(GHND, devModeSize);
  77. if (hDevMode) {
  78. void *dest = GlobalLock(hDevMode);
  79. memcpy(dest, ep->devMode, devModeSize);
  80. GlobalUnlock(hDevMode);
  81. }
  82. psd.hDevMode = hDevMode;
  83. } else {
  84. psd.hDevMode = engine->globalDevMode();
  85. }
  86. HGLOBAL *tempDevNames = engine->createGlobalDevNames();
  87. psd.hDevNames = tempDevNames;
  88. QWidget *parent = parentWidget();
  89. parent = parent ? parent->window() : QApplication::activeWindow();
  90. Q_ASSERT(!parent ||parent->testAttribute(Qt::WA_WState_Created));
  91. QWindow *parentWindow = parent->windowHandle();
  92. psd.hwndOwner = parentWindow ? (HWND)QGuiApplication::platformNativeInterface()->nativeResourceForWindow("handle", parentWindow) : 0;
  93. psd.Flags = PSD_MARGINS;
  94. QPageLayout layout = d->printer->pageLayout();
  95. switch (layout.units()) {
  96. case QPageLayout::Millimeter:
  97. case QPageLayout::Inch:
  98. break;
  99. case QPageLayout::Point:
  100. case QPageLayout::Pica:
  101. case QPageLayout::Didot:
  102. case QPageLayout::Cicero:
  103. layout.setUnits(QLocale::system().measurementSystem() == QLocale::MetricSystem ? QPageLayout::Millimeter
  104. : QPageLayout::Inch);
  105. break;
  106. }
  107. qreal multiplier = 1.0;
  108. if (layout.units() == QPageLayout::Millimeter) {
  109. psd.Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
  110. multiplier = 100.0;
  111. } else { // QPageLayout::Inch)
  112. psd.Flags |= PSD_INTHOUSANDTHSOFINCHES;
  113. multiplier = 1000.0;
  114. }
  115. psd.rtMargin.left = layout.margins().left() * multiplier;
  116. psd.rtMargin.top = layout.margins().top() * multiplier;
  117. psd.rtMargin.right = layout.margins().right() * multiplier;
  118. psd.rtMargin.bottom = layout.margins().bottom() * multiplier;
  119. QDialog::setVisible(true);
  120. bool result = PageSetupDlg(&psd);
  121. QDialog::setVisible(false);
  122. if (result) {
  123. engine->setGlobalDevMode(psd.hDevNames, psd.hDevMode);
  124. d->printer->setPageMargins(QMarginsF(psd.rtMargin.left / multiplier, psd.rtMargin.right / multiplier,
  125. psd.rtMargin.top / multiplier, psd.rtMargin.bottom / multiplier),
  126. layout.units());
  127. // copy from our temp DEVMODE struct
  128. if (!engine->globalDevMode() && hDevMode) {
  129. void *src = GlobalLock(hDevMode);
  130. memcpy(ep->devMode, src, devModeSize);
  131. GlobalUnlock(hDevMode);
  132. }
  133. }
  134. if (!engine->globalDevMode() && hDevMode)
  135. GlobalFree(hDevMode);
  136. GlobalFree(tempDevNames);
  137. done(result);
  138. return result;
  139. }
  140. void QPageSetupDialog::setVisible(bool visible)
  141. {
  142. if (!visible)
  143. return;
  144. exec();
  145. }
  146. QT_END_NAMESPACE
  147. #endif