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