]> git.saurik.com Git - wxWidgets.git/blame - src/msw/printdlg.cpp
Fixed small alignment issues
[wxWidgets.git] / src / msw / printdlg.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
6d50343d 2// Name: src/msw/printdlg.cpp
2bda0e17
KB
3// Purpose: wxPrintDialog, wxPageSetupDialog
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
103aec29
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17 20// For compilers that support precompilation, includes "wx.h".
2bda0e17
KB
21#include "wx/wxprec.h"
22
a3b46648 23#ifdef __BORLANDC__
103aec29 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
12bdd77c
JS
27// Don't use the Windows print dialog if we're in wxUniv mode and using
28// the PostScript architecture
29#if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
f6bcfd97
BP
30
31#ifndef WX_PRECOMP
57bd4c60 32 #include "wx/msw/wrapcdlg.h"
f6bcfd97 33 #include "wx/app.h"
6d50343d 34 #include "wx/dcprint.h"
ce5d92e1 35 #include "wx/cmndata.h"
f6bcfd97
BP
36#endif
37
2bda0e17 38#include "wx/printdlg.h"
08680429 39#include "wx/msw/printdlg.h"
888dde65 40#include "wx/msw/dcprint.h"
8850cbd3 41#include "wx/paper.h"
2bda0e17 42
2bda0e17 43#include <stdlib.h>
42e69d6b 44
8850cbd3
RR
45//----------------------------------------------------------------------------
46// wxWindowsPrintNativeData
47//----------------------------------------------------------------------------
48
8850cbd3
RR
49static wxString wxGetPrintDlgError()
50{
51 DWORD err = CommDlgExtendedError();
52 wxString msg = wxT("Unknown");
53 switch (err)
54 {
55 case CDERR_FINDRESFAILURE: msg = wxT("CDERR_FINDRESFAILURE"); break;
56 case CDERR_INITIALIZATION: msg = wxT("CDERR_INITIALIZATION"); break;
57 case CDERR_LOADRESFAILURE: msg = wxT("CDERR_LOADRESFAILURE"); break;
58 case CDERR_LOADSTRFAILURE: msg = wxT("CDERR_LOADSTRFAILURE"); break;
59 case CDERR_LOCKRESFAILURE: msg = wxT("CDERR_LOCKRESFAILURE"); break;
60 case CDERR_MEMALLOCFAILURE: msg = wxT("CDERR_MEMALLOCFAILURE"); break;
61 case CDERR_MEMLOCKFAILURE: msg = wxT("CDERR_MEMLOCKFAILURE"); break;
62 case CDERR_NOHINSTANCE: msg = wxT("CDERR_NOHINSTANCE"); break;
63 case CDERR_NOHOOK: msg = wxT("CDERR_NOHOOK"); break;
64 case CDERR_NOTEMPLATE: msg = wxT("CDERR_NOTEMPLATE"); break;
65 case CDERR_STRUCTSIZE: msg = wxT("CDERR_STRUCTSIZE"); break;
66 case PDERR_RETDEFFAILURE: msg = wxT("PDERR_RETDEFFAILURE"); break;
67 case PDERR_PRINTERNOTFOUND: msg = wxT("PDERR_PRINTERNOTFOUND"); break;
68 case PDERR_PARSEFAILURE: msg = wxT("PDERR_PARSEFAILURE"); break;
69 case PDERR_NODEVICES: msg = wxT("PDERR_NODEVICES"); break;
70 case PDERR_NODEFAULTPRN: msg = wxT("PDERR_NODEFAULTPRN"); break;
71 case PDERR_LOADDRVFAILURE: msg = wxT("PDERR_LOADDRVFAILURE"); break;
72 case PDERR_INITFAILURE: msg = wxT("PDERR_INITFAILURE"); break;
73 case PDERR_GETDEVMODEFAIL: msg = wxT("PDERR_GETDEVMODEFAIL"); break;
74 case PDERR_DNDMMISMATCH: msg = wxT("PDERR_DNDMMISMATCH"); break;
75 case PDERR_DEFAULTDIFFERENT: msg = wxT("PDERR_DEFAULTDIFFERENT"); break;
76 case PDERR_CREATEICFAILURE: msg = wxT("PDERR_CREATEICFAILURE"); break;
77 default: break;
78 }
79 return msg;
80}
8850cbd3 81
bd84061d
VZ
82static HGLOBAL
83wxCreateDevNames(const wxString& driverName,
84 const wxString& printerName,
85 const wxString& portName)
8850cbd3
RR
86{
87 HGLOBAL hDev = NULL;
f31a4098
WS
88 // if (!driverName.empty() && !printerName.empty() && !portName.empty())
89 if (driverName.empty() && printerName.empty() && portName.empty())
8850cbd3
RR
90 {
91 }
92 else
93 {
94 hDev = GlobalAlloc(GPTR, 4*sizeof(WORD)+
6d50343d
WS
95 ( driverName.length() + 1 +
96 printerName.length() + 1 +
97 portName.length()+1 ) * sizeof(wxChar) );
8850cbd3
RR
98 LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(hDev);
99 lpDev->wDriverOffset = sizeof(WORD) * 4 / sizeof(wxChar);
100 wxStrcpy((wxChar*)lpDev + lpDev->wDriverOffset, driverName);
101
102 lpDev->wDeviceOffset = (WORD)( lpDev->wDriverOffset +
6d50343d 103 driverName.length() + 1 );
8850cbd3
RR
104 wxStrcpy((wxChar*)lpDev + lpDev->wDeviceOffset, printerName);
105
106 lpDev->wOutputOffset = (WORD)( lpDev->wDeviceOffset +
6d50343d 107 printerName.length() + 1 );
8850cbd3
RR
108 wxStrcpy((wxChar*)lpDev + lpDev->wOutputOffset, portName);
109
110 lpDev->wDefault = 0;
111
112 GlobalUnlock(hDev);
113 }
114
115 return hDev;
116}
117
118IMPLEMENT_CLASS(wxWindowsPrintNativeData, wxPrintNativeDataBase)
119
120wxWindowsPrintNativeData::wxWindowsPrintNativeData()
121{
dca0f651
VZ
122 m_devMode = NULL;
123 m_devNames = NULL;
3168b4c3 124 m_customWindowsPaperId = 0;
8850cbd3
RR
125}
126
127wxWindowsPrintNativeData::~wxWindowsPrintNativeData()
128{
dca0f651 129 if ( m_devMode )
5c33522f 130 ::GlobalFree(static_cast<HGLOBAL>(m_devMode));
dca0f651
VZ
131
132 if ( m_devNames )
5c33522f 133 ::GlobalFree(static_cast<HGLOBAL>(m_devNames));
8850cbd3
RR
134}
135
b7cacb43 136bool wxWindowsPrintNativeData::IsOk() const
8850cbd3
RR
137{
138 return (m_devMode != NULL) ;
139}
140
fd64de59 141bool wxWindowsPrintNativeData::TransferTo( wxPrintData &data )
8850cbd3 142{
dca0f651 143 if ( !m_devMode )
8850cbd3 144 return false;
8850cbd3 145
dca0f651 146 GlobalPtrLock lockDevMode(m_devMode);
8850cbd3 147
5c33522f 148 LPDEVMODE devMode = static_cast<LPDEVMODE>(lockDevMode.Get());
8850cbd3 149
dca0f651
VZ
150 //// Orientation
151 if (devMode->dmFields & DM_ORIENTATION)
152 data.SetOrientation( devMode->dmOrientation );
8850cbd3 153
dca0f651
VZ
154 //// Collation
155 if (devMode->dmFields & DM_COLLATE)
156 {
157 if (devMode->dmCollate == DMCOLLATE_TRUE)
158 data.SetCollate( true );
8850cbd3 159 else
dca0f651
VZ
160 data.SetCollate( false );
161 }
162
163 //// Number of copies
164 if (devMode->dmFields & DM_COPIES)
165 data.SetNoCopies( devMode->dmCopies );
166
167 //// Bin
168 if (devMode->dmFields & DM_DEFAULTSOURCE) {
169 switch (devMode->dmDefaultSource) {
170 case DMBIN_ONLYONE : data.SetBin(wxPRINTBIN_ONLYONE ); break;
171 case DMBIN_LOWER : data.SetBin(wxPRINTBIN_LOWER ); break;
172 case DMBIN_MIDDLE : data.SetBin(wxPRINTBIN_MIDDLE ); break;
173 case DMBIN_MANUAL : data.SetBin(wxPRINTBIN_MANUAL ); break;
174 case DMBIN_ENVELOPE : data.SetBin(wxPRINTBIN_ENVELOPE ); break;
175 case DMBIN_ENVMANUAL : data.SetBin(wxPRINTBIN_ENVMANUAL ); break;
176 case DMBIN_AUTO : data.SetBin(wxPRINTBIN_AUTO ); break;
177 case DMBIN_TRACTOR : data.SetBin(wxPRINTBIN_TRACTOR ); break;
178 case DMBIN_SMALLFMT : data.SetBin(wxPRINTBIN_SMALLFMT ); break;
179 case DMBIN_LARGEFMT : data.SetBin(wxPRINTBIN_LARGEFMT ); break;
180 case DMBIN_LARGECAPACITY : data.SetBin(wxPRINTBIN_LARGECAPACITY ); break;
181 case DMBIN_CASSETTE : data.SetBin(wxPRINTBIN_CASSETTE ); break;
182 case DMBIN_FORMSOURCE : data.SetBin(wxPRINTBIN_FORMSOURCE ); break;
183 default:
bd84061d 184 if (devMode->dmDefaultSource >= DMBIN_USER)
dca0f651 185 data.SetBin((wxPrintBin)((devMode->dmDefaultSource)-DMBIN_USER+(int)wxPRINTBIN_USER));
bd84061d 186 else
dca0f651 187 data.SetBin(wxPRINTBIN_DEFAULT);
dca0f651
VZ
188 }
189 } else {
190 data.SetBin(wxPRINTBIN_DEFAULT);
191 }
192 if (devMode->dmFields & DM_MEDIATYPE)
193 {
194 wxASSERT( (int)devMode->dmMediaType != wxPRINTMEDIA_DEFAULT );
195 data.SetMedia(devMode->dmMediaType);
196 }
197 //// Printer name
198 if (devMode->dmDeviceName[0] != 0)
199 // This syntax fixes a crash when using VS 7.1
200 data.SetPrinterName( wxString(devMode->dmDeviceName, CCHDEVICENAME) );
201
202 //// Colour
203 if (devMode->dmFields & DM_COLOR)
204 {
205 if (devMode->dmColor == DMCOLOR_COLOR)
8850cbd3 206 data.SetColour( true );
dca0f651
VZ
207 else
208 data.SetColour( false );
209 }
210 else
211 data.SetColour( true );
8850cbd3 212
dca0f651 213 //// Paper size
8850cbd3 214
dca0f651
VZ
215 // We don't know size of user defined paper and some buggy drivers
216 // set both DM_PAPERSIZE and DM_PAPERWIDTH & DM_PAPERLENGTH. Since
217 // dmPaperSize >= DMPAPER_USER wouldn't be in wxWin's database, this
218 // code wouldn't set m_paperSize correctly.
21c358b2 219
dca0f651
VZ
220 bool foundPaperSize = false;
221 if ((devMode->dmFields & DM_PAPERSIZE) && (devMode->dmPaperSize < DMPAPER_USER))
222 {
223 if (wxThePrintPaperDatabase)
8850cbd3 224 {
dca0f651
VZ
225 wxPrintPaperType* paper = wxThePrintPaperDatabase->FindPaperTypeByPlatformId(devMode->dmPaperSize);
226 if (paper)
8850cbd3 227 {
dca0f651
VZ
228 data.SetPaperId( paper->GetId() );
229 data.SetPaperSize( wxSize(paper->GetWidth() / 10,paper->GetHeight() / 10) );
3168b4c3 230 m_customWindowsPaperId = 0;
dca0f651 231 foundPaperSize = true;
8850cbd3
RR
232 }
233 }
dca0f651
VZ
234 else
235 {
236 // Shouldn't really get here
237 wxFAIL_MSG(wxT("Paper database wasn't initialized in wxPrintData::ConvertFromNative."));
238 data.SetPaperId( wxPAPER_NONE );
239 data.SetPaperSize( wxSize(0,0) );
240 m_customWindowsPaperId = 0;
3168b4c3 241
dca0f651 242 return false;
8850cbd3 243 }
dca0f651 244 }
8850cbd3 245
dca0f651
VZ
246 if (!foundPaperSize) {
247 if ((devMode->dmFields & DM_PAPERWIDTH) && (devMode->dmFields & DM_PAPERLENGTH))
8850cbd3 248 {
dca0f651
VZ
249 // DEVMODE is in tenths of a millimeter
250 data.SetPaperSize( wxSize(devMode->dmPaperWidth / 10, devMode->dmPaperLength / 10) );
251 data.SetPaperId( wxPAPER_NONE );
252 m_customWindowsPaperId = devMode->dmPaperSize;
8850cbd3
RR
253 }
254 else
dca0f651
VZ
255 {
256 // Often will reach this for non-standard paper sizes (sizes which
257 // wouldn't be in wxWidget's paper database). Setting
258 // m_customWindowsPaperId to devMode->dmPaperSize should be enough
259 // to get this paper size working.
260 data.SetPaperSize( wxSize(0,0) );
261 data.SetPaperId( wxPAPER_NONE );
262 m_customWindowsPaperId = devMode->dmPaperSize;
263 }
264 }
8850cbd3 265
dca0f651
VZ
266 //// Duplex
267
268 if (devMode->dmFields & DM_DUPLEX)
269 {
270 switch (devMode->dmDuplex)
271 {
272 case DMDUP_HORIZONTAL: data.SetDuplex( wxDUPLEX_HORIZONTAL ); break;
273 case DMDUP_VERTICAL: data.SetDuplex( wxDUPLEX_VERTICAL ); break;
274 default:
275 case DMDUP_SIMPLEX: data.SetDuplex( wxDUPLEX_SIMPLEX ); break;
276 }
277 }
278 else
279 data.SetDuplex( wxDUPLEX_SIMPLEX );
8850cbd3 280
dca0f651
VZ
281 //// Quality
282
283 if (devMode->dmFields & DM_PRINTQUALITY)
284 {
285 switch (devMode->dmPrintQuality)
8850cbd3 286 {
dca0f651
VZ
287 case DMRES_MEDIUM: data.SetQuality( wxPRINT_QUALITY_MEDIUM ); break;
288 case DMRES_LOW: data.SetQuality( wxPRINT_QUALITY_LOW ); break;
289 case DMRES_DRAFT: data.SetQuality( wxPRINT_QUALITY_DRAFT ); break;
290 case DMRES_HIGH: data.SetQuality( wxPRINT_QUALITY_HIGH ); break;
291 default:
8850cbd3 292 {
dca0f651
VZ
293 // TODO: if the printer fills in the resolution in DPI, how
294 // will the application know if it's high, low, draft etc.??
295 // wxFAIL_MSG("Warning: DM_PRINTQUALITY was not one of the standard values.");
296 data.SetQuality( devMode->dmPrintQuality );
297 break;
8850cbd3 298
8850cbd3
RR
299 }
300 }
8850cbd3 301 }
dca0f651
VZ
302 else
303 data.SetQuality( wxPRINT_QUALITY_HIGH );
304
305 if (devMode->dmDriverExtra > 0)
306 data.SetPrivData( (char *)devMode+devMode->dmSize, devMode->dmDriverExtra );
307 else
308 data.SetPrivData( NULL, 0 );
8850cbd3 309
dca0f651 310 if ( m_devNames )
8850cbd3 311 {
dca0f651 312 GlobalPtrLock lockDevNames(m_devNames);
5c33522f 313 LPDEVNAMES lpDevNames = static_cast<LPDEVNAMES>(lockDevNames.Get());
8850cbd3 314
dca0f651 315 // TODO: Unicode-ification
8850cbd3 316
dca0f651
VZ
317 // Get the port name
318 // port is obsolete in WIN32
319 // m_printData.SetPortName((LPSTR)lpDevNames + lpDevNames->wDriverOffset);
8850cbd3 320
dca0f651
VZ
321 // Get the printer name
322 wxString printerName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset;
8850cbd3 323
dca0f651
VZ
324 // Not sure if we should check for this mismatch
325// wxASSERT_MSG( (m_printerName.empty() || (devName == m_printerName)), "Printer name obtained from DEVMODE and DEVNAMES were different!");
8850cbd3 326
dca0f651
VZ
327 if (!printerName.empty())
328 data.SetPrinterName( printerName );
8850cbd3 329 }
f31a4098 330
8850cbd3
RR
331 return true;
332}
333
fd64de59 334bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData &data )
8850cbd3 335{
5c33522f 336 HGLOBAL hDevMode = static_cast<HGLOBAL>(m_devMode);
dca0f651 337 if ( !m_devMode )
8850cbd3
RR
338 {
339 // Use PRINTDLG as a way of creating a DEVMODE object
340 PRINTDLG pd;
341
8850cbd3
RR
342 memset(&pd, 0, sizeof(PRINTDLG));
343#ifdef __WXWINCE__
344 pd.cbStruct = sizeof(PRINTDLG);
345#else
346 pd.lStructSize = sizeof(PRINTDLG);
8850cbd3
RR
347#endif
348
bd84061d 349 pd.hwndOwner = NULL;
8850cbd3
RR
350 pd.hDevMode = NULL; // Will be created by PrintDlg
351 pd.hDevNames = NULL; // Ditto
8850cbd3
RR
352
353 pd.Flags = PD_RETURNDEFAULT;
354 pd.nCopies = 1;
355
356 // Fill out the DEVMODE structure
357 // so we can use it as input in the 'real' PrintDlg
358 if (!PrintDlg(&pd))
359 {
360 if ( pd.hDevMode )
361 GlobalFree(pd.hDevMode);
362 if ( pd.hDevNames )
363 GlobalFree(pd.hDevNames);
364 pd.hDevMode = NULL;
365 pd.hDevNames = NULL;
366
4b6a582b 367 wxLogDebug(wxT("Printing error: ") + wxGetPrintDlgError());
8850cbd3
RR
368 }
369 else
370 {
371 hDevMode = pd.hDevMode;
dca0f651 372 m_devMode = hDevMode;
8850cbd3
RR
373 pd.hDevMode = NULL;
374
375 // We'll create a new DEVNAMEs structure below.
376 if ( pd.hDevNames )
377 GlobalFree(pd.hDevNames);
378 pd.hDevNames = NULL;
379
380 // hDevNames = pd->hDevNames;
381 // m_devNames = (void*)(long) hDevNames;
382 // pd->hDevnames = NULL;
383
384 }
385 }
386
387 if ( hDevMode )
388 {
dca0f651 389 GlobalPtrLock lockDevMode(hDevMode);
5c33522f 390 DEVMODE * const devMode = static_cast<DEVMODE *>(lockDevMode.Get());
8850cbd3
RR
391
392 //// Orientation
393 devMode->dmOrientation = (short)data.GetOrientation();
394
395 //// Collation
396 devMode->dmCollate = (data.GetCollate() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE);
397 devMode->dmFields |= DM_COLLATE;
398
399 //// Number of copies
400 devMode->dmCopies = (short)data.GetNoCopies();
401 devMode->dmFields |= DM_COPIES;
402
403 //// Printer name
404 wxString name = data.GetPrinterName();
f31a4098 405 if (!name.empty())
8850cbd3 406 {
47af39fb
VZ
407 // NB: the cast is needed in the ANSI build, strangely enough
408 // dmDeviceName is BYTE[] and not char[] there
e408bf52 409 wxStrlcpy(reinterpret_cast<wxChar *>(devMode->dmDeviceName),
47af39fb 410 name.wx_str(),
e408bf52 411 WXSIZEOF(devMode->dmDeviceName));
8850cbd3
RR
412 }
413
414 //// Colour
415 if (data.GetColour())
416 devMode->dmColor = DMCOLOR_COLOR;
417 else
418 devMode->dmColor = DMCOLOR_MONOCHROME;
419 devMode->dmFields |= DM_COLOR;
420
421 //// Paper size
d5eaa19f
VZ
422
423 // Paper id has priority over paper size. If id is specified, then size
424 // is ignored (as it can be filled in even for standard paper sizes)
425
426 wxPrintPaperType *paperType = NULL;
427
428 const wxPaperSize paperId = data.GetPaperId();
429 if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase )
8850cbd3 430 {
d5eaa19f 431 paperType = wxThePrintPaperDatabase->FindPaperType(paperId);
8850cbd3 432 }
d5eaa19f
VZ
433
434 if ( paperType )
8850cbd3 435 {
d5eaa19f
VZ
436 devMode->dmPaperSize = (short)paperType->GetPlatformId();
437 devMode->dmFields |= DM_PAPERSIZE;
438 }
439 else // custom (or no) paper size
440 {
441 const wxSize paperSize = data.GetPaperSize();
442 if ( paperSize != wxDefaultSize )
8850cbd3 443 {
d5eaa19f
VZ
444 // Fall back on specifying the paper size explicitly
445 if(m_customWindowsPaperId != 0)
446 devMode->dmPaperSize = m_customWindowsPaperId;
21c358b2 447 else
21c358b2 448 devMode->dmPaperSize = DMPAPER_USER;
d5eaa19f
VZ
449 devMode->dmPaperWidth = (short)(paperSize.x * 10);
450 devMode->dmPaperLength = (short)(paperSize.y * 10);
451 devMode->dmFields |= DM_PAPERWIDTH;
452 devMode->dmFields |= DM_PAPERLENGTH;
8850cbd3 453 }
d5eaa19f
VZ
454 //else: neither paper type nor size specified, don't fill DEVMODE
455 // at all so that the system defaults are used
8850cbd3
RR
456 }
457
458 //// Duplex
459 short duplex;
460 switch (data.GetDuplex())
461 {
462 case wxDUPLEX_HORIZONTAL:
463 duplex = DMDUP_HORIZONTAL;
464 break;
465 case wxDUPLEX_VERTICAL:
466 duplex = DMDUP_VERTICAL;
467 break;
468 default:
469 // in fact case wxDUPLEX_SIMPLEX:
470 duplex = DMDUP_SIMPLEX;
471 break;
472 }
473 devMode->dmDuplex = duplex;
474 devMode->dmFields |= DM_DUPLEX;
475
476 //// Quality
477
478 short quality;
479 switch (data.GetQuality())
480 {
481 case wxPRINT_QUALITY_MEDIUM:
482 quality = DMRES_MEDIUM;
483 break;
484 case wxPRINT_QUALITY_LOW:
485 quality = DMRES_LOW;
486 break;
487 case wxPRINT_QUALITY_DRAFT:
488 quality = DMRES_DRAFT;
489 break;
490 case wxPRINT_QUALITY_HIGH:
491 quality = DMRES_HIGH;
492 break;
493 default:
494 quality = (short)data.GetQuality();
495 break;
496 }
497 devMode->dmPrintQuality = quality;
498 devMode->dmFields |= DM_PRINTQUALITY;
4e22a1f2 499
aa96f01c
RR
500 if (data.GetPrivDataLen() > 0)
501 {
502 memcpy( (char *)devMode+devMode->dmSize, data.GetPrivData(), data.GetPrivDataLen() );
4e22a1f2 503 devMode->dmDriverExtra = (WXWORD)data.GetPrivDataLen();
aa96f01c 504 }
8850cbd3
RR
505
506 if (data.GetBin() != wxPRINTBIN_DEFAULT)
507 {
508 switch (data.GetBin())
509 {
510 case wxPRINTBIN_ONLYONE: devMode->dmDefaultSource = DMBIN_ONLYONE; break;
511 case wxPRINTBIN_LOWER: devMode->dmDefaultSource = DMBIN_LOWER; break;
512 case wxPRINTBIN_MIDDLE: devMode->dmDefaultSource = DMBIN_MIDDLE; break;
513 case wxPRINTBIN_MANUAL: devMode->dmDefaultSource = DMBIN_MANUAL; break;
514 case wxPRINTBIN_ENVELOPE: devMode->dmDefaultSource = DMBIN_ENVELOPE; break;
515 case wxPRINTBIN_ENVMANUAL: devMode->dmDefaultSource = DMBIN_ENVMANUAL; break;
516 case wxPRINTBIN_AUTO: devMode->dmDefaultSource = DMBIN_AUTO; break;
517 case wxPRINTBIN_TRACTOR: devMode->dmDefaultSource = DMBIN_TRACTOR; break;
518 case wxPRINTBIN_SMALLFMT: devMode->dmDefaultSource = DMBIN_SMALLFMT; break;
519 case wxPRINTBIN_LARGEFMT: devMode->dmDefaultSource = DMBIN_LARGEFMT; break;
520 case wxPRINTBIN_LARGECAPACITY: devMode->dmDefaultSource = DMBIN_LARGECAPACITY; break;
521 case wxPRINTBIN_CASSETTE: devMode->dmDefaultSource = DMBIN_CASSETTE; break;
522 case wxPRINTBIN_FORMSOURCE: devMode->dmDefaultSource = DMBIN_FORMSOURCE; break;
523
524 default:
fde05480 525 devMode->dmDefaultSource = (short)(DMBIN_USER + data.GetBin() - wxPRINTBIN_USER); // 256 + data.GetBin() - 14 = 242 + data.GetBin()
8850cbd3
RR
526 break;
527 }
528
529 devMode->dmFields |= DM_DEFAULTSOURCE;
530 }
781609f2
JS
531 if (data.GetMedia() != wxPRINTMEDIA_DEFAULT)
532 {
533 devMode->dmMediaType = data.GetMedia();
534 devMode->dmFields |= DM_MEDIATYPE;
535 }
8850cbd3
RR
536 }
537
dca0f651 538 if ( m_devNames )
8850cbd3 539 {
5c33522f 540 ::GlobalFree(static_cast<HGLOBAL>(m_devNames));
8850cbd3
RR
541 }
542
543 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
dca0f651 544 m_devNames = wxCreateDevNames(wxEmptyString, data.GetPrinterName(), wxEmptyString);
f31a4098 545
8850cbd3
RR
546 return true;
547}
f31a4098 548
103aec29
VZ
549// ---------------------------------------------------------------------------
550// wxPrintDialog
551// ---------------------------------------------------------------------------
552
c061373d 553IMPLEMENT_CLASS(wxWindowsPrintDialog, wxPrintDialogBase)
2bda0e17 554
c061373d 555wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintDialogData* data)
2bda0e17 556{
7bcb11d3 557 Create(p, data);
2bda0e17
KB
558}
559
c061373d 560wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintData* data)
103aec29
VZ
561{
562 wxPrintDialogData data2;
563 if ( data )
564 data2 = *data;
565
566 Create(p, &data2);
567}
568
c061373d 569bool wxWindowsPrintDialog::Create(wxWindow *p, wxPrintDialogData* data)
2bda0e17 570{
7bcb11d3
JS
571 m_dialogParent = p;
572 m_printerDC = NULL;
078cf5cb 573 m_destroyDC = true;
7bcb11d3 574
93c2f401
RR
575 // MSW handle
576 m_printDlg = NULL;
f31a4098 577
7bcb11d3
JS
578 if ( data )
579 m_printDialogData = *data;
103aec29 580
078cf5cb 581 return true;
2bda0e17
KB
582}
583
c061373d 584wxWindowsPrintDialog::~wxWindowsPrintDialog()
2bda0e17 585{
93c2f401
RR
586 PRINTDLG *pd = (PRINTDLG *) m_printDlg;
587 if (pd && pd->hDevMode)
588 GlobalFree(pd->hDevMode);
589 if ( pd )
590 delete pd;
591
7bcb11d3
JS
592 if (m_destroyDC && m_printerDC)
593 delete m_printerDC;
2bda0e17
KB
594}
595
c061373d 596int wxWindowsPrintDialog::ShowModal()
2bda0e17 597{
93c2f401 598 ConvertToNative( m_printDialogData );
103aec29 599
93c2f401 600 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
f31a4098 601
f6bcfd97 602 if (m_dialogParent)
93c2f401 603 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
f6bcfd97 604 else if (wxTheApp->GetTopWindow())
93c2f401 605 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
f6bcfd97 606 else
93c2f401 607 pd->hwndOwner = 0;
f6bcfd97 608
93c2f401 609 bool ret = (PrintDlg( pd ) != 0);
f6bcfd97 610
93c2f401 611 pd->hwndOwner = 0;
f6bcfd97 612
d0ee33f5 613 if ( ret && (pd->hDC) )
7bcb11d3 614 {
888dde65 615 wxPrinterDC *pdc = new wxPrinterDCFromHDC( (WXHDC) pd->hDC );
7bcb11d3 616 m_printerDC = pdc;
93c2f401 617 ConvertFromNative( m_printDialogData );
7bcb11d3
JS
618 return wxID_OK;
619 }
620 else
621 {
7bcb11d3
JS
622 return wxID_CANCEL;
623 }
2bda0e17
KB
624}
625
c061373d 626wxDC *wxWindowsPrintDialog::GetPrintDC()
2bda0e17 627{
7bcb11d3
JS
628 if (m_printerDC)
629 {
078cf5cb 630 m_destroyDC = false;
7bcb11d3
JS
631 return m_printerDC;
632 }
633 else
bd84061d 634 return NULL;
2bda0e17
KB
635}
636
93c2f401
RR
637bool wxWindowsPrintDialog::ConvertToNative( wxPrintDialogData &data )
638{
639 wxWindowsPrintNativeData *native_data =
640 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
641 data.GetPrintData().ConvertToNative();
f31a4098 642
93c2f401
RR
643 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
644
645 // Shouldn't have been defined anywhere
646 if (pd)
647 return false;
f31a4098 648
93c2f401
RR
649 pd = new PRINTDLG;
650 memset( pd, 0, sizeof(PRINTDLG) );
651 m_printDlg = (void*) pd;
652
93c2f401 653 pd->lStructSize = sizeof(PRINTDLG);
bd84061d 654 pd->hwndOwner = NULL;
93c2f401
RR
655 pd->hDevMode = NULL; // Will be created by PrintDlg
656 pd->hDevNames = NULL; // Ditto
657
658 pd->Flags = PD_RETURNDEFAULT;
659 pd->nCopies = 1;
f31a4098 660
93c2f401
RR
661 // Pass the devmode data to the PRINTDLG structure, since it'll
662 // be needed when PrintDlg is called.
663 if (pd->hDevMode)
664 GlobalFree(pd->hDevMode);
665
666 // Pass the devnames data to the PRINTDLG structure, since it'll
667 // be needed when PrintDlg is called.
668 if (pd->hDevNames)
669 GlobalFree(pd->hDevNames);
670
5c33522f 671 pd->hDevMode = static_cast<HGLOBAL>(native_data->GetDevMode());
dca0f651 672 native_data->SetDevMode(NULL);
93c2f401
RR
673
674 // Shouldn't assert; we should be able to test Ok-ness at a higher level
675 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
676
5c33522f 677 pd->hDevNames = static_cast<HGLOBAL>(native_data->GetDevNames());
dca0f651 678 native_data->SetDevNames(NULL);
93c2f401
RR
679
680
bd84061d 681 pd->hDC = NULL;
93c2f401
RR
682 pd->nFromPage = (WORD)data.GetFromPage();
683 pd->nToPage = (WORD)data.GetToPage();
684 pd->nMinPage = (WORD)data.GetMinPage();
685 pd->nMaxPage = (WORD)data.GetMaxPage();
686 pd->nCopies = (WORD)data.GetNoCopies();
687
688 pd->Flags = PD_RETURNDC;
93c2f401 689 pd->lStructSize = sizeof( PRINTDLG );
93c2f401 690
bd84061d
VZ
691 pd->hwndOwner = NULL;
692 pd->hInstance = NULL;
312242ea 693 pd->lCustData = 0;
93c2f401
RR
694 pd->lpfnPrintHook = NULL;
695 pd->lpfnSetupHook = NULL;
696 pd->lpPrintTemplateName = NULL;
697 pd->lpSetupTemplateName = NULL;
bd84061d
VZ
698 pd->hPrintTemplate = NULL;
699 pd->hSetupTemplate = NULL;
93c2f401
RR
700
701 if ( data.GetAllPages() )
702 pd->Flags |= PD_ALLPAGES;
703 if ( data.GetSelection() )
704 pd->Flags |= PD_SELECTION;
705 if ( data.GetCollate() )
706 pd->Flags |= PD_COLLATE;
707 if ( data.GetPrintToFile() )
708 pd->Flags |= PD_PRINTTOFILE;
709 if ( !data.GetEnablePrintToFile() )
710 pd->Flags |= PD_DISABLEPRINTTOFILE;
711 if ( !data.GetEnableSelection() )
712 pd->Flags |= PD_NOSELECTION;
713 if ( !data.GetEnablePageNumbers() )
714 pd->Flags |= PD_NOPAGENUMS;
715 else if ( (!data.GetAllPages()) && (!data.GetSelection()) && (data.GetFromPage() != 0) && (data.GetToPage() != 0))
716 pd->Flags |= PD_PAGENUMS;
717 if ( data.GetEnableHelp() )
718 pd->Flags |= PD_SHOWHELP;
f31a4098 719
93c2f401
RR
720 return true;
721}
722
723bool wxWindowsPrintDialog::ConvertFromNative( wxPrintDialogData &data )
724{
725 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
726 if ( pd == NULL )
727 return false;
728
729 wxWindowsPrintNativeData *native_data =
730 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
f31a4098 731
93c2f401
RR
732 // Pass the devmode data back to the wxPrintData structure where it really belongs.
733 if (pd->hDevMode)
734 {
735 if (native_data->GetDevMode())
736 {
5c33522f 737 ::GlobalFree(static_cast<HGLOBAL>(native_data->GetDevMode()));
93c2f401 738 }
dca0f651 739 native_data->SetDevMode(pd->hDevMode);
93c2f401
RR
740 pd->hDevMode = NULL;
741 }
742
743 // Pass the devnames data back to the wxPrintData structure where it really belongs.
744 if (pd->hDevNames)
745 {
746 if (native_data->GetDevNames())
747 {
5c33522f 748 ::GlobalFree(static_cast<HGLOBAL>(native_data->GetDevNames()));
93c2f401 749 }
dca0f651 750 native_data->SetDevNames(pd->hDevNames);
93c2f401
RR
751 pd->hDevNames = NULL;
752 }
753
754 // Now convert the DEVMODE object, passed down from the PRINTDLG object,
755 // into wxWidgets form.
756 native_data->TransferTo( data.GetPrintData() );
757
758 data.SetFromPage( pd->nFromPage );
759 data.SetToPage( pd->nToPage );
760 data.SetMinPage( pd->nMinPage );
761 data.SetMaxPage( pd->nMaxPage );
762 data.SetNoCopies( pd->nCopies );
763
764 data.SetAllPages( (((pd->Flags & PD_PAGENUMS) != PD_PAGENUMS) && ((pd->Flags & PD_SELECTION) != PD_SELECTION)) );
765 data.SetSelection( ((pd->Flags & PD_SELECTION) == PD_SELECTION) );
766 data.SetCollate( ((pd->Flags & PD_COLLATE) == PD_COLLATE) );
767 data.SetPrintToFile( ((pd->Flags & PD_PRINTTOFILE) == PD_PRINTTOFILE) );
768 data.EnablePrintToFile( ((pd->Flags & PD_DISABLEPRINTTOFILE) != PD_DISABLEPRINTTOFILE) );
769 data.EnableSelection( ((pd->Flags & PD_NOSELECTION) != PD_NOSELECTION) );
770 data.EnablePageNumbers( ((pd->Flags & PD_NOPAGENUMS) != PD_NOPAGENUMS) );
771 data.EnableHelp( ((pd->Flags & PD_SHOWHELP) == PD_SHOWHELP) );
c1dc9f83 772
93c2f401
RR
773 return true;
774}
775
103aec29 776// ---------------------------------------------------------------------------
08680429 777// wxWidnowsPageSetupDialog
103aec29 778// ---------------------------------------------------------------------------
2bda0e17 779
08680429 780IMPLEMENT_CLASS(wxWindowsPageSetupDialog, wxPageSetupDialogBase)
c061373d 781
08680429 782wxWindowsPageSetupDialog::wxWindowsPageSetupDialog()
2bda0e17 783{
7bcb11d3 784 m_dialogParent = NULL;
b45dfd0a 785 m_pageDlg = NULL;
2bda0e17
KB
786}
787
08680429 788wxWindowsPageSetupDialog::wxWindowsPageSetupDialog(wxWindow *p, wxPageSetupDialogData *data)
2bda0e17 789{
7bcb11d3 790 Create(p, data);
2bda0e17
KB
791}
792
08680429 793bool wxWindowsPageSetupDialog::Create(wxWindow *p, wxPageSetupDialogData *data)
2bda0e17 794{
7bcb11d3 795 m_dialogParent = p;
b45dfd0a 796 m_pageDlg = NULL;
103aec29 797
7bcb11d3
JS
798 if (data)
799 m_pageSetupData = (*data);
103aec29 800
078cf5cb 801 return true;
2bda0e17
KB
802}
803
08680429 804wxWindowsPageSetupDialog::~wxWindowsPageSetupDialog()
2bda0e17 805{
b45dfd0a
RR
806 PAGESETUPDLG *pd = (PAGESETUPDLG *)m_pageDlg;
807 if ( pd && pd->hDevMode )
808 GlobalFree(pd->hDevMode);
809 if ( pd && pd->hDevNames )
810 GlobalFree(pd->hDevNames);
811 if ( pd )
812 delete pd;
2bda0e17
KB
813}
814
08680429 815int wxWindowsPageSetupDialog::ShowModal()
2bda0e17 816{
b45dfd0a 817 ConvertToNative( m_pageSetupData );
f31a4098 818
b45dfd0a 819 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
f6bcfd97 820 if (m_dialogParent)
b45dfd0a 821 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
f6bcfd97 822 else if (wxTheApp->GetTopWindow())
b45dfd0a 823 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
f6bcfd97 824 else
b45dfd0a
RR
825 pd->hwndOwner = 0;
826 BOOL retVal = PageSetupDlg( pd ) ;
827 pd->hwndOwner = 0;
f6bcfd97 828 if (retVal)
2bda0e17 829 {
b45dfd0a 830 ConvertFromNative( m_pageSetupData );
7bcb11d3 831 return wxID_OK;
2bda0e17
KB
832 }
833 else
7bcb11d3 834 return wxID_CANCEL;
b45dfd0a
RR
835}
836
08680429 837bool wxWindowsPageSetupDialog::ConvertToNative( wxPageSetupDialogData &data )
b45dfd0a
RR
838{
839 wxWindowsPrintNativeData *native_data =
840 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
841 data.GetPrintData().ConvertToNative();
842
843 PAGESETUPDLG *pd = (PAGESETUPDLG*) m_pageDlg;
844
845 // Shouldn't have been defined anywhere
846 if (pd)
847 return false;
f31a4098 848
b45dfd0a
RR
849 pd = new PAGESETUPDLG;
850 pd->hDevMode = NULL;
851 pd->hDevNames = NULL;
852 m_pageDlg = (void *)pd;
853
854 // Pass the devmode data (created in m_printData.ConvertToNative)
855 // to the PRINTDLG structure, since it'll
856 // be needed when PrintDlg is called.
857
858 if (pd->hDevMode)
859 {
860 GlobalFree(pd->hDevMode);
861 pd->hDevMode = NULL;
862 }
863 pd->hDevMode = (HGLOBAL) native_data->GetDevMode();
bd84061d 864 native_data->SetDevMode(NULL);
b45dfd0a
RR
865
866 // Shouldn't assert; we should be able to test Ok-ness at a higher level
867 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
868
869 // Pass the devnames data (created in m_printData.ConvertToNative)
870 // to the PRINTDLG structure, since it'll
871 // be needed when PrintDlg is called.
872
873 if (pd->hDevNames)
874 {
875 GlobalFree(pd->hDevNames);
876 pd->hDevNames = NULL;
877 }
878 pd->hDevNames = (HGLOBAL) native_data->GetDevNames();
bd84061d 879 native_data->SetDevNames(NULL);
b45dfd0a
RR
880
881// pd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
882
883 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
884
885 if ( data.GetDefaultMinMargins() )
886 pd->Flags |= PSD_DEFAULTMINMARGINS;
887 if ( !data.GetEnableMargins() )
888 pd->Flags |= PSD_DISABLEMARGINS;
889 if ( !data.GetEnableOrientation() )
890 pd->Flags |= PSD_DISABLEORIENTATION;
891 if ( !data.GetEnablePaper() )
892 pd->Flags |= PSD_DISABLEPAPER;
893 if ( !data.GetEnablePrinter() )
894 pd->Flags |= PSD_DISABLEPRINTER;
895 if ( data.GetDefaultInfo() )
896 pd->Flags |= PSD_RETURNDEFAULT;
897 if ( data.GetEnableHelp() )
898 pd->Flags |= PSD_SHOWHELP;
899
900 // We want the units to be in hundredths of a millimetre
901 pd->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
902
903 pd->lStructSize = sizeof( PAGESETUPDLG );
bd84061d
VZ
904 pd->hwndOwner = NULL;
905 pd->hInstance = NULL;
b45dfd0a
RR
906 // PAGESETUPDLG is in hundreds of a mm
907 pd->ptPaperSize.x = data.GetPaperSize().x * 100;
908 pd->ptPaperSize.y = data.GetPaperSize().y * 100;
909
910 pd->rtMinMargin.left = data.GetMinMarginTopLeft().x * 100;
911 pd->rtMinMargin.top = data.GetMinMarginTopLeft().y * 100;
912 pd->rtMinMargin.right = data.GetMinMarginBottomRight().x * 100;
913 pd->rtMinMargin.bottom = data.GetMinMarginBottomRight().y * 100;
914
915 pd->rtMargin.left = data.GetMarginTopLeft().x * 100;
916 pd->rtMargin.top = data.GetMarginTopLeft().y * 100;
917 pd->rtMargin.right = data.GetMarginBottomRight().x * 100;
918 pd->rtMargin.bottom = data.GetMarginBottomRight().y * 100;
919
920 pd->lCustData = 0;
921 pd->lpfnPageSetupHook = NULL;
922 pd->lpfnPagePaintHook = NULL;
923 pd->hPageSetupTemplate = NULL;
924 pd->lpPageSetupTemplateName = NULL;
925
926/*
927 if ( pd->hDevMode )
928 {
929 DEVMODE *devMode = (DEVMODE*) GlobalLock(pd->hDevMode);
930 memset(devMode, 0, sizeof(DEVMODE));
931 devMode->dmSize = sizeof(DEVMODE);
932 devMode->dmOrientation = m_orientation;
933 devMode->dmFields = DM_ORIENTATION;
934 GlobalUnlock(pd->hDevMode);
935 }
936*/
937 return true;
938}
939
08680429 940bool wxWindowsPageSetupDialog::ConvertFromNative( wxPageSetupDialogData &data )
b45dfd0a
RR
941{
942 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
943 if ( !pd )
944 return false;
945
946 wxWindowsPrintNativeData *native_data =
947 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
f31a4098 948
b45dfd0a
RR
949 // Pass the devmode data back to the wxPrintData structure where it really belongs.
950 if (pd->hDevMode)
951 {
952 if (native_data->GetDevMode())
953 {
954 // Make sure we don't leak memory
955 GlobalFree((HGLOBAL) native_data->GetDevMode());
956 }
957 native_data->SetDevMode( (void*) pd->hDevMode );
958 pd->hDevMode = NULL;
959 }
f31a4098 960
b45dfd0a
RR
961 // Isn't this superfluous? It's called again below.
962 // data.GetPrintData().ConvertFromNative();
963
964 // Pass the devnames data back to the wxPrintData structure where it really belongs.
965 if (pd->hDevNames)
966 {
967 if (native_data->GetDevNames())
968 {
969 // Make sure we don't leak memory
970 GlobalFree((HGLOBAL) native_data->GetDevNames());
971 }
972 native_data->SetDevNames((void*) pd->hDevNames);
973 pd->hDevNames = NULL;
974 }
975
976 data.GetPrintData().ConvertFromNative();
977
978 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
979
980 data.SetDefaultMinMargins( ((pd->Flags & PSD_DEFAULTMINMARGINS) == PSD_DEFAULTMINMARGINS) );
981 data.EnableMargins( ((pd->Flags & PSD_DISABLEMARGINS) != PSD_DISABLEMARGINS) );
982 data.EnableOrientation( ((pd->Flags & PSD_DISABLEORIENTATION) != PSD_DISABLEORIENTATION) );
983 data.EnablePaper( ((pd->Flags & PSD_DISABLEPAPER) != PSD_DISABLEPAPER) );
984 data.EnablePrinter( ((pd->Flags & PSD_DISABLEPRINTER) != PSD_DISABLEPRINTER) );
985 data.SetDefaultInfo( ((pd->Flags & PSD_RETURNDEFAULT) == PSD_RETURNDEFAULT) );
986 data.EnableHelp( ((pd->Flags & PSD_SHOWHELP) == PSD_SHOWHELP) );
987
988 // PAGESETUPDLG is in hundreds of a mm
fa4f4c58
RR
989 if (data.GetPrintData().GetOrientation() == wxLANDSCAPE)
990 data.SetPaperSize( wxSize(pd->ptPaperSize.y / 100, pd->ptPaperSize.x / 100) );
991 else
992 data.SetPaperSize( wxSize(pd->ptPaperSize.x / 100, pd->ptPaperSize.y / 100) );
b45dfd0a
RR
993
994 data.SetMinMarginTopLeft( wxPoint(pd->rtMinMargin.left / 100, pd->rtMinMargin.top / 100) );
995 data.SetMinMarginBottomRight( wxPoint(pd->rtMinMargin.right / 100, pd->rtMinMargin.bottom / 100) );
996
997 data.SetMarginTopLeft( wxPoint(pd->rtMargin.left / 100, pd->rtMargin.top / 100) );
998 data.SetMarginBottomRight( wxPoint(pd->rtMargin.right / 100, pd->rtMargin.bottom / 100) );
f31a4098 999
b45dfd0a 1000 return true;
2bda0e17
KB
1001}
1002
f6bcfd97
BP
1003#endif
1004 // wxUSE_PRINTING_ARCHITECTURE