source: trunk/src/gui/painting/qprinterinfo_win.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 6.9 KB
RevLine 
[2]1/****************************************************************************
2**
[846]3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
[561]4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
[2]6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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**
[561]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.
[2]27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
[561]36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
[2]38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qprinterinfo.h"
43
44#include <qstringlist.h>
45
[561]46#include <qt_windows.h>
[2]47
48QT_BEGIN_NAMESPACE
49
50#ifndef QT_NO_PRINTER
51
52extern QPrinter::PaperSize mapDevmodePaperSize(int s);
53
54class QPrinterInfoPrivate
55{
56Q_DECLARE_PUBLIC(QPrinterInfo)
57public:
58 ~QPrinterInfoPrivate();
59 QPrinterInfoPrivate();
60 QPrinterInfoPrivate(const QString& name);
61
62private:
63 QString m_name;
64 bool m_default;
65 bool m_isNull;
66
67 QPrinterInfo* q_ptr;
68};
69
70static QPrinterInfoPrivate nullQPrinterInfoPrivate;
71
[561]72class QPrinterInfoPrivateDeleter
73{
74public:
75 static inline void cleanup(QPrinterInfoPrivate *d)
76 {
77 if (d != &nullQPrinterInfoPrivate)
78 delete d;
79 }
80};
81
[2]82/////////////////////////////////////////////////////////////////////////////
83/////////////////////////////////////////////////////////////////////////////
84
85QList<QPrinterInfo> QPrinterInfo::availablePrinters()
86{
87 QList<QPrinterInfo> printers;
88 LPBYTE buffer;
89 DWORD needed = 0;
90 DWORD returned = 0;
91
[561]92 if ( !EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS, NULL, 4, 0, 0, &needed, &returned))
93 {
94 buffer = new BYTE[needed];
95 if (!EnumPrinters(PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS , NULL,
96 4, buffer, needed, &needed, &returned))
97 {
98 delete [] buffer;
99 return printers;
100 }
101 PPRINTER_INFO_4 infoList = reinterpret_cast<PPRINTER_INFO_4>(buffer);
102 QPrinterInfo defPrn = defaultPrinter();
103 for (uint i = 0; i < returned; ++i) {
104 printers.append(QPrinterInfo(QString::fromWCharArray(infoList[i].pPrinterName)));
105 if (printers.at(i).printerName() == defPrn.printerName())
106 printers[i].d_ptr->m_default = true;
107 }
108 delete [] buffer;
109 }
[2]110
111 return printers;
112}
113
114QPrinterInfo QPrinterInfo::defaultPrinter()
115{
116 QString noPrinters(QLatin1String("qt_no_printers"));
[561]117 wchar_t buffer[256];
118 GetProfileString(L"windows", L"device", (wchar_t*)noPrinters.utf16(), buffer, 256);
119 QString output = QString::fromWCharArray(buffer);
[2]120
121 // Filter out the name of the printer, which should be everything
122 // before a comma.
123 bool noConfiguredPrinters = (output == noPrinters);
124 QStringList info = output.split(QLatin1Char(','));
125 QString printerName = noConfiguredPrinters ? QString() : info.at(0);
126
127 QPrinterInfo prn(printerName);
128 prn.d_ptr->m_default = true;
129 if (noConfiguredPrinters)
130 prn.d_ptr->m_isNull = true;
131 return prn;
132}
133
134/////////////////////////////////////////////////////////////////////////////
135/////////////////////////////////////////////////////////////////////////////
136
137QPrinterInfo::QPrinterInfo()
[561]138 : d_ptr(&nullQPrinterInfoPrivate)
[2]139{
140}
141
142QPrinterInfo::QPrinterInfo(const QString& name)
[561]143 : d_ptr(new QPrinterInfoPrivate(name))
[2]144{
145 d_ptr->q_ptr = this;
146}
147
148QPrinterInfo::QPrinterInfo(const QPrinterInfo& src)
[561]149 : d_ptr(&nullQPrinterInfoPrivate)
[2]150{
151 *this = src;
152}
153
154QPrinterInfo::QPrinterInfo(const QPrinter& prn)
[561]155 : d_ptr(&nullQPrinterInfoPrivate)
[2]156{
157 QList<QPrinterInfo> list = availablePrinters();
158 for (int c = 0; c < list.size(); ++c) {
159 if (prn.printerName() == list[c].printerName()) {
160 *this = list[c];
161 return;
162 }
163 }
164
165 *this = QPrinterInfo();
166}
167
168QPrinterInfo::~QPrinterInfo()
169{
170}
171
172QPrinterInfo& QPrinterInfo::operator=(const QPrinterInfo& src)
173{
174 Q_ASSERT(d_ptr);
[561]175 d_ptr.reset(new QPrinterInfoPrivate(*src.d_ptr));
[2]176 d_ptr->q_ptr = this;
177 return *this;
178}
179
180QString QPrinterInfo::printerName() const
181{
182 const Q_D(QPrinterInfo);
183 return d->m_name;
184}
185
186bool QPrinterInfo::isNull() const
187{
188 const Q_D(QPrinterInfo);
189 return d->m_isNull;
190}
191
192bool QPrinterInfo::isDefault() const
193{
194 const Q_D(QPrinterInfo);
195 return d->m_default;
196}
197
198QList<QPrinter::PaperSize> QPrinterInfo::supportedPaperSizes() const
199{
200 const Q_D(QPrinterInfo);
201 QList<QPrinter::PaperSize> paperList;
202
[561]203 DWORD size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->m_name.utf16()),
204 NULL, DC_PAPERS, NULL, NULL);
205 if ((int)size == -1)
206 return paperList;
[2]207
[561]208 wchar_t *papers = new wchar_t[size];
209 size = DeviceCapabilities(reinterpret_cast<const wchar_t*>(d->m_name.utf16()),
210 NULL, DC_PAPERS, papers, NULL);
211
[2]212 for (int c = 0; c < (int)size; ++c) {
213 paperList.append(mapDevmodePaperSize(papers[c]));
214 }
215
216 delete [] papers;
217
218 return paperList;
219}
220
221/////////////////////////////////////////////////////////////////////////////
222/////////////////////////////////////////////////////////////////////////////
223
224QPrinterInfoPrivate::QPrinterInfoPrivate() :
225 m_default(false),
226 m_isNull(true),
227 q_ptr(NULL)
228{
229}
230
231QPrinterInfoPrivate::QPrinterInfoPrivate(const QString& name) :
232 m_name(name),
233 m_default(false),
234 m_isNull(false),
235 q_ptr(NULL)
236{
237}
238
239QPrinterInfoPrivate::~QPrinterInfoPrivate()
240{
241}
242
243#endif // QT_NO_PRINTER
244
245QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.