1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/printdlg.cpp
3 // Purpose: wxPrintDialog, wxPageSetupDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
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)
32 #include "wx/msw/wrapcdlg.h"
34 #include "wx/dcprint.h"
35 #include "wx/cmndata.h"
38 #include "wx/printdlg.h"
39 #include "wx/msw/printdlg.h"
40 #include "wx/msw/dcprint.h"
45 // smart pointer like class using OpenPrinter and ClosePrinter
52 m_hPrinter
= (HANDLE
)NULL
;
55 WinPrinter( const wxString
& printerName
)
65 BOOL
Open( const wxString
& printerName
, LPPRINTER_DEFAULTS pDefault
=(LPPRINTER_DEFAULTS
)NULL
)
68 return OpenPrinter( (LPTSTR
)printerName
.wx_str(), &m_hPrinter
, pDefault
);
76 result
= ClosePrinter( m_hPrinter
);
77 m_hPrinter
= (HANDLE
)NULL
;
82 operator HANDLE() { return m_hPrinter
; }
83 operator bool() { return m_hPrinter
!= (HANDLE
)NULL
; }
88 wxDECLARE_NO_COPY_CLASS(WinPrinter
);
92 //----------------------------------------------------------------------------
93 // wxWindowsPrintNativeData
94 //----------------------------------------------------------------------------
98 static wxString
wxGetPrintDlgError()
100 DWORD err
= CommDlgExtendedError();
101 wxString msg
= wxT("Unknown");
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;
131 #endif // wxDEBUG_LEVEL
135 wxCreateDevNames(const wxString
& driverName
,
136 const wxString
& printerName
,
137 const wxString
& portName
)
140 // if (!driverName.empty() && !printerName.empty() && !portName.empty())
141 if (driverName
.empty() && printerName
.empty() && portName
.empty())
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
);
154 lpDev
->wDeviceOffset
= (WORD
)( lpDev
->wDriverOffset
+
155 driverName
.length() + 1 );
156 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wDeviceOffset
, printerName
);
158 lpDev
->wOutputOffset
= (WORD
)( lpDev
->wDeviceOffset
+
159 printerName
.length() + 1 );
160 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wOutputOffset
, portName
);
170 IMPLEMENT_CLASS(wxWindowsPrintNativeData
, wxPrintNativeDataBase
)
172 wxWindowsPrintNativeData::wxWindowsPrintNativeData()
176 m_customWindowsPaperId
= 0;
179 wxWindowsPrintNativeData::~wxWindowsPrintNativeData()
182 ::GlobalFree(static_cast<HGLOBAL
>(m_devMode
));
185 ::GlobalFree(static_cast<HGLOBAL
>(m_devNames
));
188 bool wxWindowsPrintNativeData::IsOk() const
190 return (m_devMode
!= NULL
) ;
193 bool wxWindowsPrintNativeData::TransferTo( wxPrintData
&data
)
201 GlobalPtrLock
lockDevMode(m_devMode
);
203 LPDEVMODE devMode
= static_cast<LPDEVMODE
>(lockDevMode
.Get());
206 if (devMode
->dmFields
& DM_ORIENTATION
)
207 data
.SetOrientation( (wxPrintOrientation
)devMode
->dmOrientation
);
210 if (devMode
->dmFields
& DM_COLLATE
)
212 if (devMode
->dmCollate
== DMCOLLATE_TRUE
)
213 data
.SetCollate( true );
215 data
.SetCollate( false );
218 //// Number of copies
219 if (devMode
->dmFields
& DM_COPIES
)
220 data
.SetNoCopies( devMode
->dmCopies
);
223 if (devMode
->dmFields
& DM_DEFAULTSOURCE
) {
224 switch (devMode
->dmDefaultSource
) {
225 case DMBIN_ONLYONE
: data
.SetBin(wxPRINTBIN_ONLYONE
); break;
226 case DMBIN_LOWER
: data
.SetBin(wxPRINTBIN_LOWER
); break;
227 case DMBIN_MIDDLE
: data
.SetBin(wxPRINTBIN_MIDDLE
); break;
228 case DMBIN_MANUAL
: data
.SetBin(wxPRINTBIN_MANUAL
); break;
229 case DMBIN_ENVELOPE
: data
.SetBin(wxPRINTBIN_ENVELOPE
); break;
230 case DMBIN_ENVMANUAL
: data
.SetBin(wxPRINTBIN_ENVMANUAL
); break;
231 case DMBIN_AUTO
: data
.SetBin(wxPRINTBIN_AUTO
); break;
232 case DMBIN_TRACTOR
: data
.SetBin(wxPRINTBIN_TRACTOR
); break;
233 case DMBIN_SMALLFMT
: data
.SetBin(wxPRINTBIN_SMALLFMT
); break;
234 case DMBIN_LARGEFMT
: data
.SetBin(wxPRINTBIN_LARGEFMT
); break;
235 case DMBIN_LARGECAPACITY
: data
.SetBin(wxPRINTBIN_LARGECAPACITY
); break;
236 case DMBIN_CASSETTE
: data
.SetBin(wxPRINTBIN_CASSETTE
); break;
237 case DMBIN_FORMSOURCE
: data
.SetBin(wxPRINTBIN_FORMSOURCE
); break;
239 if (devMode
->dmDefaultSource
>= DMBIN_USER
)
240 data
.SetBin((wxPrintBin
)((devMode
->dmDefaultSource
)-DMBIN_USER
+(int)wxPRINTBIN_USER
));
242 data
.SetBin(wxPRINTBIN_DEFAULT
);
245 data
.SetBin(wxPRINTBIN_DEFAULT
);
247 if (devMode
->dmFields
& DM_MEDIATYPE
)
249 wxASSERT( (int)devMode
->dmMediaType
!= wxPRINTMEDIA_DEFAULT
);
250 data
.SetMedia(devMode
->dmMediaType
);
253 if (devMode
->dmDeviceName
[0] != 0)
254 // This syntax fixes a crash when using VS 7.1
255 data
.SetPrinterName( wxString(devMode
->dmDeviceName
, CCHDEVICENAME
) );
258 if (devMode
->dmFields
& DM_COLOR
)
260 if (devMode
->dmColor
== DMCOLOR_COLOR
)
261 data
.SetColour( true );
263 data
.SetColour( false );
266 data
.SetColour( true );
270 // We don't know size of user defined paper and some buggy drivers
271 // set both DM_PAPERSIZE and DM_PAPERWIDTH & DM_PAPERLENGTH. Since
272 // dmPaperSize >= DMPAPER_USER wouldn't be in wxWin's database, this
273 // code wouldn't set m_paperSize correctly.
275 bool foundPaperSize
= false;
276 if ((devMode
->dmFields
& DM_PAPERSIZE
) && (devMode
->dmPaperSize
< DMPAPER_USER
))
278 if (wxThePrintPaperDatabase
)
280 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperTypeByPlatformId(devMode
->dmPaperSize
);
283 data
.SetPaperId( paper
->GetId() );
284 data
.SetPaperSize( wxSize(paper
->GetWidth() / 10,paper
->GetHeight() / 10) );
285 m_customWindowsPaperId
= 0;
286 foundPaperSize
= true;
291 // Shouldn't really get here
292 wxFAIL_MSG(wxT("Paper database wasn't initialized in wxPrintData::ConvertFromNative."));
293 data
.SetPaperId( wxPAPER_NONE
);
294 data
.SetPaperSize( wxSize(0,0) );
295 m_customWindowsPaperId
= 0;
301 if (!foundPaperSize
) {
302 if ((devMode
->dmFields
& DM_PAPERWIDTH
) && (devMode
->dmFields
& DM_PAPERLENGTH
))
304 // DEVMODE is in tenths of a millimeter
305 data
.SetPaperSize( wxSize(devMode
->dmPaperWidth
/ 10, devMode
->dmPaperLength
/ 10) );
306 data
.SetPaperId( wxPAPER_NONE
);
307 m_customWindowsPaperId
= devMode
->dmPaperSize
;
311 // Often will reach this for non-standard paper sizes (sizes which
312 // wouldn't be in wxWidget's paper database). Setting
313 // m_customWindowsPaperId to devMode->dmPaperSize should be enough
314 // to get this paper size working.
315 data
.SetPaperSize( wxSize(0,0) );
316 data
.SetPaperId( wxPAPER_NONE
);
317 m_customWindowsPaperId
= devMode
->dmPaperSize
;
323 if (devMode
->dmFields
& DM_DUPLEX
)
325 switch (devMode
->dmDuplex
)
327 case DMDUP_HORIZONTAL
: data
.SetDuplex( wxDUPLEX_HORIZONTAL
); break;
328 case DMDUP_VERTICAL
: data
.SetDuplex( wxDUPLEX_VERTICAL
); break;
330 case DMDUP_SIMPLEX
: data
.SetDuplex( wxDUPLEX_SIMPLEX
); break;
334 data
.SetDuplex( wxDUPLEX_SIMPLEX
);
338 if (devMode
->dmFields
& DM_PRINTQUALITY
)
340 switch (devMode
->dmPrintQuality
)
342 case DMRES_MEDIUM
: data
.SetQuality( wxPRINT_QUALITY_MEDIUM
); break;
343 case DMRES_LOW
: data
.SetQuality( wxPRINT_QUALITY_LOW
); break;
344 case DMRES_DRAFT
: data
.SetQuality( wxPRINT_QUALITY_DRAFT
); break;
345 case DMRES_HIGH
: data
.SetQuality( wxPRINT_QUALITY_HIGH
); break;
348 // TODO: if the printer fills in the resolution in DPI, how
349 // will the application know if it's high, low, draft etc.??
350 // wxFAIL_MSG("Warning: DM_PRINTQUALITY was not one of the standard values.");
351 data
.SetQuality( devMode
->dmPrintQuality
);
358 data
.SetQuality( wxPRINT_QUALITY_HIGH
);
360 if (devMode
->dmDriverExtra
> 0)
361 data
.SetPrivData( (char *)devMode
+devMode
->dmSize
, devMode
->dmDriverExtra
);
363 data
.SetPrivData( NULL
, 0 );
367 GlobalPtrLock
lockDevNames(m_devNames
);
368 LPDEVNAMES lpDevNames
= static_cast<LPDEVNAMES
>(lockDevNames
.Get());
370 // TODO: Unicode-ification
373 // port is obsolete in WIN32
374 // m_printData.SetPortName((LPSTR)lpDevNames + lpDevNames->wDriverOffset);
376 // Get the printer name
377 wxString printerName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
379 // Not sure if we should check for this mismatch
380 // wxASSERT_MSG( (m_printerName.empty() || (devName == m_printerName)), "Printer name obtained from DEVMODE and DEVNAMES were different!");
382 if (!printerName
.empty())
383 data
.SetPrinterName( printerName
);
389 void wxWindowsPrintNativeData::InitializeDevMode(const wxString
& printerName
, WinPrinter
* printer
)
394 LPTSTR szPrinterName
= (LPTSTR
)printerName
.wx_str();
396 // From MSDN: How To Modify Printer Settings with the DocumentProperties() Function
397 // The purpose of this is to fill the DEVMODE with privdata from printer driver.
398 // If we have a printer name and OpenPrinter sucessfully returns
399 // this replaces the PrintDlg function which creates the DEVMODE filled only with data from default printer.
400 if ( !m_devMode
&& !printerName
.IsEmpty() )
403 if ( printer
&& printer
->Open( printerName
) == TRUE
)
405 DWORD dwNeeded
, dwRet
;
408 // Allocate a buffer of the correct size.
409 dwNeeded
= DocumentProperties( NULL
,
410 printer
, // Handle to our printer.
411 szPrinterName
, // Name of the printer.
412 NULL
, // Asking for size, so
413 NULL
, // these are not used.
414 0 ); // Zero returns buffer size.
416 LPDEVMODE tempDevMode
= static_cast<LPDEVMODE
>( GlobalAlloc( GMEM_FIXED
| GMEM_ZEROINIT
, dwNeeded
) );
419 // Get the default DevMode for the printer
420 dwRet
= DocumentProperties( NULL
,
423 tempDevMode
, // The address of the buffer to fill.
424 NULL
, // Not using the input buffer.
425 DM_OUT_BUFFER
); // Have the output buffer filled.
429 // If failure, cleanup
430 GlobalFree( tempDevMode
);
435 m_devMode
= tempDevMode
;
443 // Use PRINTDLG as a way of creating a DEVMODE object
446 memset(&pd
, 0, sizeof(PRINTDLG
));
448 pd
.cbStruct
= sizeof(PRINTDLG
);
450 pd
.lStructSize
= sizeof(PRINTDLG
);
454 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
455 pd
.hDevNames
= NULL
; // Ditto
457 pd
.Flags
= PD_RETURNDEFAULT
;
460 // Fill out the DEVMODE structure
461 // so we can use it as input in the 'real' PrintDlg
465 GlobalFree(pd
.hDevMode
);
467 GlobalFree(pd
.hDevNames
);
472 wxLogDebug(wxT("Printing error: ") + wxGetPrintDlgError());
473 #endif // wxDEBUG_LEVEL
477 m_devMode
= pd
.hDevMode
;
480 // We'll create a new DEVNAMEs structure below.
482 GlobalFree(pd
.hDevNames
);
485 // hDevNames = pd->hDevNames;
486 // m_devNames = (void*)(long) hDevNames;
487 // pd->hDevnames = NULL;
494 bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData
&data
)
497 LPTSTR szPrinterName
= (LPTSTR
)data
.GetPrinterName().wx_str();
500 InitializeDevMode(data
.GetPrinterName(), &printer
);
502 HGLOBAL hDevMode
= static_cast<HGLOBAL
>(m_devMode
);
506 GlobalPtrLock
lockDevMode(hDevMode
);
507 DEVMODE
* const devMode
= static_cast<DEVMODE
*>(lockDevMode
.Get());
510 devMode
->dmOrientation
= (short)data
.GetOrientation();
513 devMode
->dmCollate
= (data
.GetCollate() ? DMCOLLATE_TRUE
: DMCOLLATE_FALSE
);
514 devMode
->dmFields
|= DM_COLLATE
;
516 //// Number of copies
517 devMode
->dmCopies
= (short)data
.GetNoCopies();
518 devMode
->dmFields
|= DM_COPIES
;
521 wxString name
= data
.GetPrinterName();
524 // NB: the cast is needed in the ANSI build, strangely enough
525 // dmDeviceName is BYTE[] and not char[] there
526 wxStrlcpy(reinterpret_cast<wxChar
*>(devMode
->dmDeviceName
),
528 WXSIZEOF(devMode
->dmDeviceName
));
532 if (data
.GetColour())
533 devMode
->dmColor
= DMCOLOR_COLOR
;
535 devMode
->dmColor
= DMCOLOR_MONOCHROME
;
536 devMode
->dmFields
|= DM_COLOR
;
540 // Paper id has priority over paper size. If id is specified, then size
541 // is ignored (as it can be filled in even for standard paper sizes)
543 wxPrintPaperType
*paperType
= NULL
;
545 const wxPaperSize paperId
= data
.GetPaperId();
546 if ( paperId
!= wxPAPER_NONE
&& wxThePrintPaperDatabase
)
548 paperType
= wxThePrintPaperDatabase
->FindPaperType(paperId
);
553 devMode
->dmPaperSize
= (short)paperType
->GetPlatformId();
554 devMode
->dmFields
|= DM_PAPERSIZE
;
556 else // custom (or no) paper size
558 const wxSize paperSize
= data
.GetPaperSize();
559 if ( paperSize
!= wxDefaultSize
)
561 // Fall back on specifying the paper size explicitly
562 if(m_customWindowsPaperId
!= 0)
563 devMode
->dmPaperSize
= m_customWindowsPaperId
;
565 devMode
->dmPaperSize
= DMPAPER_USER
;
566 devMode
->dmPaperWidth
= (short)(paperSize
.x
* 10);
567 devMode
->dmPaperLength
= (short)(paperSize
.y
* 10);
568 devMode
->dmFields
|= DM_PAPERWIDTH
;
569 devMode
->dmFields
|= DM_PAPERLENGTH
;
571 // A printer driver may or may not also want DM_PAPERSIZE to
572 // be specified. Also, if the printer driver doesn't implement the DMPAPER_USER
573 // size, then this won't work, and even if you found the correct id by
574 // enumerating the driver's paper sizes, it probably won't change the actual size,
575 // it'll just select that custom paper type with its own current setting.
576 // For a discussion on this, see http://www.codeguru.com/forum/showthread.php?threadid=458617
577 // Although m_customWindowsPaperId is intended to work around this, it's
578 // unclear how it can help you set the custom paper size programmatically.
580 //else: neither paper type nor size specified, don't fill DEVMODE
581 // at all so that the system defaults are used
586 switch (data
.GetDuplex())
588 case wxDUPLEX_HORIZONTAL
:
589 duplex
= DMDUP_HORIZONTAL
;
591 case wxDUPLEX_VERTICAL
:
592 duplex
= DMDUP_VERTICAL
;
595 // in fact case wxDUPLEX_SIMPLEX:
596 duplex
= DMDUP_SIMPLEX
;
599 devMode
->dmDuplex
= duplex
;
600 devMode
->dmFields
|= DM_DUPLEX
;
605 switch (data
.GetQuality())
607 case wxPRINT_QUALITY_MEDIUM
:
608 quality
= DMRES_MEDIUM
;
610 case wxPRINT_QUALITY_LOW
:
613 case wxPRINT_QUALITY_DRAFT
:
614 quality
= DMRES_DRAFT
;
616 case wxPRINT_QUALITY_HIGH
:
617 quality
= DMRES_HIGH
;
620 quality
= (short)data
.GetQuality();
621 devMode
->dmYResolution
= quality
;
622 devMode
->dmFields
|= DM_YRESOLUTION
;
625 devMode
->dmPrintQuality
= quality
;
626 devMode
->dmFields
|= DM_PRINTQUALITY
;
628 if (data
.GetPrivDataLen() > 0)
630 memcpy( (char *)devMode
+devMode
->dmSize
, data
.GetPrivData(), data
.GetPrivDataLen() );
631 devMode
->dmDriverExtra
= (WXWORD
)data
.GetPrivDataLen();
634 if (data
.GetBin() != wxPRINTBIN_DEFAULT
)
636 switch (data
.GetBin())
638 case wxPRINTBIN_ONLYONE
: devMode
->dmDefaultSource
= DMBIN_ONLYONE
; break;
639 case wxPRINTBIN_LOWER
: devMode
->dmDefaultSource
= DMBIN_LOWER
; break;
640 case wxPRINTBIN_MIDDLE
: devMode
->dmDefaultSource
= DMBIN_MIDDLE
; break;
641 case wxPRINTBIN_MANUAL
: devMode
->dmDefaultSource
= DMBIN_MANUAL
; break;
642 case wxPRINTBIN_ENVELOPE
: devMode
->dmDefaultSource
= DMBIN_ENVELOPE
; break;
643 case wxPRINTBIN_ENVMANUAL
: devMode
->dmDefaultSource
= DMBIN_ENVMANUAL
; break;
644 case wxPRINTBIN_AUTO
: devMode
->dmDefaultSource
= DMBIN_AUTO
; break;
645 case wxPRINTBIN_TRACTOR
: devMode
->dmDefaultSource
= DMBIN_TRACTOR
; break;
646 case wxPRINTBIN_SMALLFMT
: devMode
->dmDefaultSource
= DMBIN_SMALLFMT
; break;
647 case wxPRINTBIN_LARGEFMT
: devMode
->dmDefaultSource
= DMBIN_LARGEFMT
; break;
648 case wxPRINTBIN_LARGECAPACITY
: devMode
->dmDefaultSource
= DMBIN_LARGECAPACITY
; break;
649 case wxPRINTBIN_CASSETTE
: devMode
->dmDefaultSource
= DMBIN_CASSETTE
; break;
650 case wxPRINTBIN_FORMSOURCE
: devMode
->dmDefaultSource
= DMBIN_FORMSOURCE
; break;
653 devMode
->dmDefaultSource
= (short)(DMBIN_USER
+ data
.GetBin() - wxPRINTBIN_USER
); // 256 + data.GetBin() - 14 = 242 + data.GetBin()
657 devMode
->dmFields
|= DM_DEFAULTSOURCE
;
659 if (data
.GetMedia() != wxPRINTMEDIA_DEFAULT
)
661 devMode
->dmMediaType
= data
.GetMedia();
662 devMode
->dmFields
|= DM_MEDIATYPE
;
668 // Merge the new settings with the old.
669 // This gives the driver an opportunity to update any private
670 // portions of the DevMode structure.
671 DocumentProperties( NULL
,
674 (LPDEVMODE
)hDevMode
, // Reuse our buffer for output.
675 (LPDEVMODE
)hDevMode
, // Pass the driver our changes
676 DM_IN_BUFFER
| // Commands to Merge our changes and
677 DM_OUT_BUFFER
); // write the result.
683 ::GlobalFree(static_cast<HGLOBAL
>(m_devNames
));
686 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
687 m_devNames
= wxCreateDevNames(wxEmptyString
, data
.GetPrinterName(), wxEmptyString
);
692 // ---------------------------------------------------------------------------
694 // ---------------------------------------------------------------------------
696 IMPLEMENT_CLASS(wxWindowsPrintDialog
, wxPrintDialogBase
)
698 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintDialogData
* data
)
703 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintData
* data
)
705 wxPrintDialogData data2
;
712 bool wxWindowsPrintDialog::Create(wxWindow
*p
, wxPrintDialogData
* data
)
722 m_printDialogData
= *data
;
727 wxWindowsPrintDialog::~wxWindowsPrintDialog()
729 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
730 if (pd
&& pd
->hDevMode
)
731 GlobalFree(pd
->hDevMode
);
735 if (m_destroyDC
&& m_printerDC
)
739 int wxWindowsPrintDialog::ShowModal()
741 ConvertToNative( m_printDialogData
);
743 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
746 pd
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
747 else if (wxTheApp
->GetTopWindow())
748 pd
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
752 bool ret
= (PrintDlg( pd
) != 0);
756 if ( ret
&& (pd
->hDC
) )
758 wxPrinterDC
*pdc
= new wxPrinterDCFromHDC( (WXHDC
) pd
->hDC
);
760 ConvertFromNative( m_printDialogData
);
769 wxDC
*wxWindowsPrintDialog::GetPrintDC()
780 bool wxWindowsPrintDialog::ConvertToNative( wxPrintDialogData
&data
)
782 wxWindowsPrintNativeData
*native_data
=
783 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
784 data
.GetPrintData().ConvertToNative();
786 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
788 // Shouldn't have been defined anywhere
793 memset( pd
, 0, sizeof(PRINTDLG
) );
794 m_printDlg
= (void*) pd
;
796 pd
->lStructSize
= sizeof(PRINTDLG
);
797 pd
->hwndOwner
= NULL
;
798 pd
->hDevMode
= NULL
; // Will be created by PrintDlg
799 pd
->hDevNames
= NULL
; // Ditto
801 pd
->Flags
= PD_RETURNDEFAULT
;
804 // Pass the devmode data to the PRINTDLG structure, since it'll
805 // be needed when PrintDlg is called.
807 GlobalFree(pd
->hDevMode
);
809 // Pass the devnames data to the PRINTDLG structure, since it'll
810 // be needed when PrintDlg is called.
812 GlobalFree(pd
->hDevNames
);
814 pd
->hDevMode
= static_cast<HGLOBAL
>(native_data
->GetDevMode());
815 native_data
->SetDevMode(NULL
);
817 // Shouldn't assert; we should be able to test Ok-ness at a higher level
818 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
820 pd
->hDevNames
= static_cast<HGLOBAL
>(native_data
->GetDevNames());
821 native_data
->SetDevNames(NULL
);
825 pd
->nFromPage
= (WORD
)data
.GetFromPage();
826 pd
->nToPage
= (WORD
)data
.GetToPage();
827 pd
->nMinPage
= (WORD
)data
.GetMinPage();
828 pd
->nMaxPage
= (WORD
)data
.GetMaxPage();
829 pd
->nCopies
= (WORD
)data
.GetNoCopies();
831 pd
->Flags
= PD_RETURNDC
;
832 pd
->lStructSize
= sizeof( PRINTDLG
);
834 pd
->hwndOwner
= NULL
;
835 pd
->hInstance
= NULL
;
837 pd
->lpfnPrintHook
= NULL
;
838 pd
->lpfnSetupHook
= NULL
;
839 pd
->lpPrintTemplateName
= NULL
;
840 pd
->lpSetupTemplateName
= NULL
;
841 pd
->hPrintTemplate
= NULL
;
842 pd
->hSetupTemplate
= NULL
;
844 if ( data
.GetAllPages() )
845 pd
->Flags
|= PD_ALLPAGES
;
846 if ( data
.GetSelection() )
847 pd
->Flags
|= PD_SELECTION
;
848 if ( data
.GetCollate() )
849 pd
->Flags
|= PD_COLLATE
;
850 if ( data
.GetPrintToFile() )
851 pd
->Flags
|= PD_PRINTTOFILE
;
852 if ( !data
.GetEnablePrintToFile() )
853 pd
->Flags
|= PD_DISABLEPRINTTOFILE
;
854 if ( !data
.GetEnableSelection() )
855 pd
->Flags
|= PD_NOSELECTION
;
856 if ( !data
.GetEnablePageNumbers() )
857 pd
->Flags
|= PD_NOPAGENUMS
;
858 else if ( (!data
.GetAllPages()) && (!data
.GetSelection()) && (data
.GetFromPage() != 0) && (data
.GetToPage() != 0))
859 pd
->Flags
|= PD_PAGENUMS
;
860 if ( data
.GetEnableHelp() )
861 pd
->Flags
|= PD_SHOWHELP
;
866 bool wxWindowsPrintDialog::ConvertFromNative( wxPrintDialogData
&data
)
868 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
872 wxWindowsPrintNativeData
*native_data
=
873 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
875 // Pass the devmode data back to the wxPrintData structure where it really belongs.
878 if (native_data
->GetDevMode())
880 ::GlobalFree(static_cast<HGLOBAL
>(native_data
->GetDevMode()));
882 native_data
->SetDevMode(pd
->hDevMode
);
886 // Pass the devnames data back to the wxPrintData structure where it really belongs.
889 if (native_data
->GetDevNames())
891 ::GlobalFree(static_cast<HGLOBAL
>(native_data
->GetDevNames()));
893 native_data
->SetDevNames(pd
->hDevNames
);
894 pd
->hDevNames
= NULL
;
897 // Now convert the DEVMODE object, passed down from the PRINTDLG object,
898 // into wxWidgets form.
899 native_data
->TransferTo( data
.GetPrintData() );
901 data
.SetFromPage( pd
->nFromPage
);
902 data
.SetToPage( pd
->nToPage
);
903 data
.SetMinPage( pd
->nMinPage
);
904 data
.SetMaxPage( pd
->nMaxPage
);
905 data
.SetNoCopies( pd
->nCopies
);
907 data
.SetAllPages( (((pd
->Flags
& PD_PAGENUMS
) != PD_PAGENUMS
) && ((pd
->Flags
& PD_SELECTION
) != PD_SELECTION
)) );
908 data
.SetSelection( ((pd
->Flags
& PD_SELECTION
) == PD_SELECTION
) );
909 data
.SetCollate( ((pd
->Flags
& PD_COLLATE
) == PD_COLLATE
) );
910 data
.SetPrintToFile( ((pd
->Flags
& PD_PRINTTOFILE
) == PD_PRINTTOFILE
) );
911 data
.EnablePrintToFile( ((pd
->Flags
& PD_DISABLEPRINTTOFILE
) != PD_DISABLEPRINTTOFILE
) );
912 data
.EnableSelection( ((pd
->Flags
& PD_NOSELECTION
) != PD_NOSELECTION
) );
913 data
.EnablePageNumbers( ((pd
->Flags
& PD_NOPAGENUMS
) != PD_NOPAGENUMS
) );
914 data
.EnableHelp( ((pd
->Flags
& PD_SHOWHELP
) == PD_SHOWHELP
) );
919 // ---------------------------------------------------------------------------
920 // wxWidnowsPageSetupDialog
921 // ---------------------------------------------------------------------------
923 IMPLEMENT_CLASS(wxWindowsPageSetupDialog
, wxPageSetupDialogBase
)
925 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog()
927 m_dialogParent
= NULL
;
931 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog(wxWindow
*p
, wxPageSetupDialogData
*data
)
936 bool wxWindowsPageSetupDialog::Create(wxWindow
*p
, wxPageSetupDialogData
*data
)
942 m_pageSetupData
= (*data
);
947 wxWindowsPageSetupDialog::~wxWindowsPageSetupDialog()
949 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*)m_pageDlg
;
950 if ( pd
&& pd
->hDevMode
)
951 GlobalFree(pd
->hDevMode
);
952 if ( pd
&& pd
->hDevNames
)
953 GlobalFree(pd
->hDevNames
);
958 int wxWindowsPageSetupDialog::ShowModal()
960 ConvertToNative( m_pageSetupData
);
962 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
964 pd
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
965 else if (wxTheApp
->GetTopWindow())
966 pd
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
969 BOOL retVal
= PageSetupDlg( pd
) ;
973 ConvertFromNative( m_pageSetupData
);
980 bool wxWindowsPageSetupDialog::ConvertToNative( wxPageSetupDialogData
&data
)
982 wxWindowsPrintNativeData
*native_data
=
983 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
984 data
.GetPrintData().ConvertToNative();
986 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
988 // Shouldn't have been defined anywhere
992 pd
= new PAGESETUPDLG
;
994 pd
->hDevNames
= NULL
;
995 m_pageDlg
= (void *)pd
;
997 // Pass the devmode data (created in m_printData.ConvertToNative)
998 // to the PRINTDLG structure, since it'll
999 // be needed when PrintDlg is called.
1003 GlobalFree(pd
->hDevMode
);
1004 pd
->hDevMode
= NULL
;
1006 pd
->hDevMode
= (HGLOBAL
) native_data
->GetDevMode();
1007 native_data
->SetDevMode(NULL
);
1009 // Shouldn't assert; we should be able to test Ok-ness at a higher level
1010 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
1012 // Pass the devnames data (created in m_printData.ConvertToNative)
1013 // to the PRINTDLG structure, since it'll
1014 // be needed when PrintDlg is called.
1018 GlobalFree(pd
->hDevNames
);
1019 pd
->hDevNames
= NULL
;
1021 pd
->hDevNames
= (HGLOBAL
) native_data
->GetDevNames();
1022 native_data
->SetDevNames(NULL
);
1024 // pd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
1026 pd
->Flags
= PSD_MARGINS
|PSD_MINMARGINS
;
1028 if ( data
.GetDefaultMinMargins() )
1029 pd
->Flags
|= PSD_DEFAULTMINMARGINS
;
1030 if ( !data
.GetEnableMargins() )
1031 pd
->Flags
|= PSD_DISABLEMARGINS
;
1032 if ( !data
.GetEnableOrientation() )
1033 pd
->Flags
|= PSD_DISABLEORIENTATION
;
1034 if ( !data
.GetEnablePaper() )
1035 pd
->Flags
|= PSD_DISABLEPAPER
;
1036 if ( !data
.GetEnablePrinter() )
1037 pd
->Flags
|= PSD_DISABLEPRINTER
;
1038 if ( data
.GetDefaultInfo() )
1039 pd
->Flags
|= PSD_RETURNDEFAULT
;
1040 if ( data
.GetEnableHelp() )
1041 pd
->Flags
|= PSD_SHOWHELP
;
1043 // We want the units to be in hundredths of a millimetre
1044 pd
->Flags
|= PSD_INHUNDREDTHSOFMILLIMETERS
;
1046 pd
->lStructSize
= sizeof( PAGESETUPDLG
);
1047 pd
->hwndOwner
= NULL
;
1048 pd
->hInstance
= NULL
;
1049 // PAGESETUPDLG is in hundreds of a mm
1050 pd
->ptPaperSize
.x
= data
.GetPaperSize().x
* 100;
1051 pd
->ptPaperSize
.y
= data
.GetPaperSize().y
* 100;
1053 pd
->rtMinMargin
.left
= data
.GetMinMarginTopLeft().x
* 100;
1054 pd
->rtMinMargin
.top
= data
.GetMinMarginTopLeft().y
* 100;
1055 pd
->rtMinMargin
.right
= data
.GetMinMarginBottomRight().x
* 100;
1056 pd
->rtMinMargin
.bottom
= data
.GetMinMarginBottomRight().y
* 100;
1058 pd
->rtMargin
.left
= data
.GetMarginTopLeft().x
* 100;
1059 pd
->rtMargin
.top
= data
.GetMarginTopLeft().y
* 100;
1060 pd
->rtMargin
.right
= data
.GetMarginBottomRight().x
* 100;
1061 pd
->rtMargin
.bottom
= data
.GetMarginBottomRight().y
* 100;
1064 pd
->lpfnPageSetupHook
= NULL
;
1065 pd
->lpfnPagePaintHook
= NULL
;
1066 pd
->hPageSetupTemplate
= NULL
;
1067 pd
->lpPageSetupTemplateName
= NULL
;
1072 DEVMODE *devMode = (DEVMODE*) GlobalLock(pd->hDevMode);
1073 memset(devMode, 0, sizeof(DEVMODE));
1074 devMode->dmSize = sizeof(DEVMODE);
1075 devMode->dmOrientation = m_orientation;
1076 devMode->dmFields = DM_ORIENTATION;
1077 GlobalUnlock(pd->hDevMode);
1083 bool wxWindowsPageSetupDialog::ConvertFromNative( wxPageSetupDialogData
&data
)
1085 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
1089 wxWindowsPrintNativeData
*native_data
=
1090 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
1092 // Pass the devmode data back to the wxPrintData structure where it really belongs.
1095 if (native_data
->GetDevMode())
1097 // Make sure we don't leak memory
1098 GlobalFree((HGLOBAL
) native_data
->GetDevMode());
1100 native_data
->SetDevMode( (void*) pd
->hDevMode
);
1101 pd
->hDevMode
= NULL
;
1104 // Isn't this superfluous? It's called again below.
1105 // data.GetPrintData().ConvertFromNative();
1107 // Pass the devnames data back to the wxPrintData structure where it really belongs.
1110 if (native_data
->GetDevNames())
1112 // Make sure we don't leak memory
1113 GlobalFree((HGLOBAL
) native_data
->GetDevNames());
1115 native_data
->SetDevNames((void*) pd
->hDevNames
);
1116 pd
->hDevNames
= NULL
;
1119 data
.GetPrintData().ConvertFromNative();
1121 pd
->Flags
= PSD_MARGINS
|PSD_MINMARGINS
;
1123 data
.SetDefaultMinMargins( ((pd
->Flags
& PSD_DEFAULTMINMARGINS
) == PSD_DEFAULTMINMARGINS
) );
1124 data
.EnableMargins( ((pd
->Flags
& PSD_DISABLEMARGINS
) != PSD_DISABLEMARGINS
) );
1125 data
.EnableOrientation( ((pd
->Flags
& PSD_DISABLEORIENTATION
) != PSD_DISABLEORIENTATION
) );
1126 data
.EnablePaper( ((pd
->Flags
& PSD_DISABLEPAPER
) != PSD_DISABLEPAPER
) );
1127 data
.EnablePrinter( ((pd
->Flags
& PSD_DISABLEPRINTER
) != PSD_DISABLEPRINTER
) );
1128 data
.SetDefaultInfo( ((pd
->Flags
& PSD_RETURNDEFAULT
) == PSD_RETURNDEFAULT
) );
1129 data
.EnableHelp( ((pd
->Flags
& PSD_SHOWHELP
) == PSD_SHOWHELP
) );
1131 // PAGESETUPDLG is in hundreds of a mm
1132 if (data
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
1133 data
.SetPaperSize( wxSize(pd
->ptPaperSize
.y
/ 100, pd
->ptPaperSize
.x
/ 100) );
1135 data
.SetPaperSize( wxSize(pd
->ptPaperSize
.x
/ 100, pd
->ptPaperSize
.y
/ 100) );
1137 data
.SetMinMarginTopLeft( wxPoint(pd
->rtMinMargin
.left
/ 100, pd
->rtMinMargin
.top
/ 100) );
1138 data
.SetMinMarginBottomRight( wxPoint(pd
->rtMinMargin
.right
/ 100, pd
->rtMinMargin
.bottom
/ 100) );
1140 data
.SetMarginTopLeft( wxPoint(pd
->rtMargin
.left
/ 100, pd
->rtMargin
.top
/ 100) );
1141 data
.SetMarginBottomRight( wxPoint(pd
->rtMargin
.right
/ 100, pd
->rtMargin
.bottom
/ 100) );
1147 // wxUSE_PRINTING_ARCHITECTURE