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