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