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