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