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
)
198 GlobalPtrLock
lockDevMode(m_devMode
);
200 LPDEVMODE devMode
= static_cast<LPDEVMODE
>(lockDevMode
.Get());
203 if (devMode
->dmFields
& DM_ORIENTATION
)
204 data
.SetOrientation( devMode
->dmOrientation
);
207 if (devMode
->dmFields
& DM_COLLATE
)
209 if (devMode
->dmCollate
== DMCOLLATE_TRUE
)
210 data
.SetCollate( true );
212 data
.SetCollate( false );
215 //// Number of copies
216 if (devMode
->dmFields
& DM_COPIES
)
217 data
.SetNoCopies( devMode
->dmCopies
);
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;
236 if (devMode
->dmDefaultSource
>= DMBIN_USER
)
237 data
.SetBin((wxPrintBin
)((devMode
->dmDefaultSource
)-DMBIN_USER
+(int)wxPRINTBIN_USER
));
239 data
.SetBin(wxPRINTBIN_DEFAULT
);
242 data
.SetBin(wxPRINTBIN_DEFAULT
);
244 if (devMode
->dmFields
& DM_MEDIATYPE
)
246 wxASSERT( (int)devMode
->dmMediaType
!= wxPRINTMEDIA_DEFAULT
);
247 data
.SetMedia(devMode
->dmMediaType
);
250 if (devMode
->dmDeviceName
[0] != 0)
251 // This syntax fixes a crash when using VS 7.1
252 data
.SetPrinterName( wxString(devMode
->dmDeviceName
, CCHDEVICENAME
) );
255 if (devMode
->dmFields
& DM_COLOR
)
257 if (devMode
->dmColor
== DMCOLOR_COLOR
)
258 data
.SetColour( true );
260 data
.SetColour( false );
263 data
.SetColour( true );
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.
272 bool foundPaperSize
= false;
273 if ((devMode
->dmFields
& DM_PAPERSIZE
) && (devMode
->dmPaperSize
< DMPAPER_USER
))
275 if (wxThePrintPaperDatabase
)
277 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperTypeByPlatformId(devMode
->dmPaperSize
);
280 data
.SetPaperId( paper
->GetId() );
281 data
.SetPaperSize( wxSize(paper
->GetWidth() / 10,paper
->GetHeight() / 10) );
282 m_customWindowsPaperId
= 0;
283 foundPaperSize
= true;
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;
298 if (!foundPaperSize
) {
299 if ((devMode
->dmFields
& DM_PAPERWIDTH
) && (devMode
->dmFields
& DM_PAPERLENGTH
))
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
;
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
;
320 if (devMode
->dmFields
& DM_DUPLEX
)
322 switch (devMode
->dmDuplex
)
324 case DMDUP_HORIZONTAL
: data
.SetDuplex( wxDUPLEX_HORIZONTAL
); break;
325 case DMDUP_VERTICAL
: data
.SetDuplex( wxDUPLEX_VERTICAL
); break;
327 case DMDUP_SIMPLEX
: data
.SetDuplex( wxDUPLEX_SIMPLEX
); break;
331 data
.SetDuplex( wxDUPLEX_SIMPLEX
);
335 if (devMode
->dmFields
& DM_PRINTQUALITY
)
337 switch (devMode
->dmPrintQuality
)
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;
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
);
355 data
.SetQuality( wxPRINT_QUALITY_HIGH
);
357 if (devMode
->dmDriverExtra
> 0)
358 data
.SetPrivData( (char *)devMode
+devMode
->dmSize
, devMode
->dmDriverExtra
);
360 data
.SetPrivData( NULL
, 0 );
364 GlobalPtrLock
lockDevNames(m_devNames
);
365 LPDEVNAMES lpDevNames
= static_cast<LPDEVNAMES
>(lockDevNames
.Get());
367 // TODO: Unicode-ification
370 // port is obsolete in WIN32
371 // m_printData.SetPortName((LPSTR)lpDevNames + lpDevNames->wDriverOffset);
373 // Get the printer name
374 wxString printerName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
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!");
379 if (!printerName
.empty())
380 data
.SetPrinterName( printerName
);
386 bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData
&data
)
388 HGLOBAL hDevMode
= static_cast<HGLOBAL
>(m_devMode
);
390 LPTSTR szPrinterName
= (LPTSTR
)data
.GetPrinterName().wx_str();
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() )
399 if ( printer
.Open( data
.GetPrinterName() ) == TRUE
)
401 DWORD dwNeeded
, dwRet
;
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.
412 LPDEVMODE tempDevMode
= static_cast<LPDEVMODE
>( GlobalAlloc( GMEM_FIXED
| GMEM_ZEROINIT
, dwNeeded
) );
415 // Get the default DevMode for the printer
416 dwRet
= DocumentProperties( NULL
,
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.
425 // If failure, cleanup
426 GlobalFree( tempDevMode
);
431 hDevMode
= tempDevMode
;
432 m_devMode
= hDevMode
;
440 // Use PRINTDLG as a way of creating a DEVMODE object
443 memset(&pd
, 0, sizeof(PRINTDLG
));
445 pd
.cbStruct
= sizeof(PRINTDLG
);
447 pd
.lStructSize
= sizeof(PRINTDLG
);
451 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
452 pd
.hDevNames
= NULL
; // Ditto
454 pd
.Flags
= PD_RETURNDEFAULT
;
457 // Fill out the DEVMODE structure
458 // so we can use it as input in the 'real' PrintDlg
462 GlobalFree(pd
.hDevMode
);
464 GlobalFree(pd
.hDevNames
);
469 wxLogDebug(wxT("Printing error: ") + wxGetPrintDlgError());
470 #endif // wxDEBUG_LEVEL
474 hDevMode
= pd
.hDevMode
;
475 m_devMode
= hDevMode
;
478 // We'll create a new DEVNAMEs structure below.
480 GlobalFree(pd
.hDevNames
);
483 // hDevNames = pd->hDevNames;
484 // m_devNames = (void*)(long) hDevNames;
485 // pd->hDevnames = NULL;
492 GlobalPtrLock
lockDevMode(hDevMode
);
493 DEVMODE
* const devMode
= static_cast<DEVMODE
*>(lockDevMode
.Get());
496 devMode
->dmOrientation
= (short)data
.GetOrientation();
499 devMode
->dmCollate
= (data
.GetCollate() ? DMCOLLATE_TRUE
: DMCOLLATE_FALSE
);
500 devMode
->dmFields
|= DM_COLLATE
;
502 //// Number of copies
503 devMode
->dmCopies
= (short)data
.GetNoCopies();
504 devMode
->dmFields
|= DM_COPIES
;
507 wxString name
= data
.GetPrinterName();
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
),
514 WXSIZEOF(devMode
->dmDeviceName
));
518 if (data
.GetColour())
519 devMode
->dmColor
= DMCOLOR_COLOR
;
521 devMode
->dmColor
= DMCOLOR_MONOCHROME
;
522 devMode
->dmFields
|= DM_COLOR
;
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)
529 wxPrintPaperType
*paperType
= NULL
;
531 const wxPaperSize paperId
= data
.GetPaperId();
532 if ( paperId
!= wxPAPER_NONE
&& wxThePrintPaperDatabase
)
534 paperType
= wxThePrintPaperDatabase
->FindPaperType(paperId
);
539 devMode
->dmPaperSize
= (short)paperType
->GetPlatformId();
540 devMode
->dmFields
|= DM_PAPERSIZE
;
542 else // custom (or no) paper size
544 const wxSize paperSize
= data
.GetPaperSize();
545 if ( paperSize
!= wxDefaultSize
)
547 // Fall back on specifying the paper size explicitly
548 if(m_customWindowsPaperId
!= 0)
549 devMode
->dmPaperSize
= m_customWindowsPaperId
;
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
;
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.
566 //else: neither paper type nor size specified, don't fill DEVMODE
567 // at all so that the system defaults are used
572 switch (data
.GetDuplex())
574 case wxDUPLEX_HORIZONTAL
:
575 duplex
= DMDUP_HORIZONTAL
;
577 case wxDUPLEX_VERTICAL
:
578 duplex
= DMDUP_VERTICAL
;
581 // in fact case wxDUPLEX_SIMPLEX:
582 duplex
= DMDUP_SIMPLEX
;
585 devMode
->dmDuplex
= duplex
;
586 devMode
->dmFields
|= DM_DUPLEX
;
591 switch (data
.GetQuality())
593 case wxPRINT_QUALITY_MEDIUM
:
594 quality
= DMRES_MEDIUM
;
596 case wxPRINT_QUALITY_LOW
:
599 case wxPRINT_QUALITY_DRAFT
:
600 quality
= DMRES_DRAFT
;
602 case wxPRINT_QUALITY_HIGH
:
603 quality
= DMRES_HIGH
;
606 quality
= (short)data
.GetQuality();
607 devMode
->dmYResolution
= quality
;
608 devMode
->dmFields
|= DM_YRESOLUTION
;
611 devMode
->dmPrintQuality
= quality
;
612 devMode
->dmFields
|= DM_PRINTQUALITY
;
614 if (data
.GetPrivDataLen() > 0)
616 memcpy( (char *)devMode
+devMode
->dmSize
, data
.GetPrivData(), data
.GetPrivDataLen() );
617 devMode
->dmDriverExtra
= (WXWORD
)data
.GetPrivDataLen();
620 if (data
.GetBin() != wxPRINTBIN_DEFAULT
)
622 switch (data
.GetBin())
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;
639 devMode
->dmDefaultSource
= (short)(DMBIN_USER
+ data
.GetBin() - wxPRINTBIN_USER
); // 256 + data.GetBin() - 14 = 242 + data.GetBin()
643 devMode
->dmFields
|= DM_DEFAULTSOURCE
;
645 if (data
.GetMedia() != wxPRINTMEDIA_DEFAULT
)
647 devMode
->dmMediaType
= data
.GetMedia();
648 devMode
->dmFields
|= DM_MEDIATYPE
;
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
,
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.
669 ::GlobalFree(static_cast<HGLOBAL
>(m_devNames
));
672 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
673 m_devNames
= wxCreateDevNames(wxEmptyString
, data
.GetPrinterName(), wxEmptyString
);
678 // ---------------------------------------------------------------------------
680 // ---------------------------------------------------------------------------
682 IMPLEMENT_CLASS(wxWindowsPrintDialog
, wxPrintDialogBase
)
684 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintDialogData
* data
)
689 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintData
* data
)
691 wxPrintDialogData data2
;
698 bool wxWindowsPrintDialog::Create(wxWindow
*p
, wxPrintDialogData
* data
)
708 m_printDialogData
= *data
;
713 wxWindowsPrintDialog::~wxWindowsPrintDialog()
715 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
716 if (pd
&& pd
->hDevMode
)
717 GlobalFree(pd
->hDevMode
);
721 if (m_destroyDC
&& m_printerDC
)
725 int wxWindowsPrintDialog::ShowModal()
727 ConvertToNative( m_printDialogData
);
729 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
732 pd
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
733 else if (wxTheApp
->GetTopWindow())
734 pd
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
738 bool ret
= (PrintDlg( pd
) != 0);
742 if ( ret
&& (pd
->hDC
) )
744 wxPrinterDC
*pdc
= new wxPrinterDCFromHDC( (WXHDC
) pd
->hDC
);
746 ConvertFromNative( m_printDialogData
);
755 wxDC
*wxWindowsPrintDialog::GetPrintDC()
766 bool wxWindowsPrintDialog::ConvertToNative( wxPrintDialogData
&data
)
768 wxWindowsPrintNativeData
*native_data
=
769 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
770 data
.GetPrintData().ConvertToNative();
772 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
774 // Shouldn't have been defined anywhere
779 memset( pd
, 0, sizeof(PRINTDLG
) );
780 m_printDlg
= (void*) pd
;
782 pd
->lStructSize
= sizeof(PRINTDLG
);
783 pd
->hwndOwner
= NULL
;
784 pd
->hDevMode
= NULL
; // Will be created by PrintDlg
785 pd
->hDevNames
= NULL
; // Ditto
787 pd
->Flags
= PD_RETURNDEFAULT
;
790 // Pass the devmode data to the PRINTDLG structure, since it'll
791 // be needed when PrintDlg is called.
793 GlobalFree(pd
->hDevMode
);
795 // Pass the devnames data to the PRINTDLG structure, since it'll
796 // be needed when PrintDlg is called.
798 GlobalFree(pd
->hDevNames
);
800 pd
->hDevMode
= static_cast<HGLOBAL
>(native_data
->GetDevMode());
801 native_data
->SetDevMode(NULL
);
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!"));
806 pd
->hDevNames
= static_cast<HGLOBAL
>(native_data
->GetDevNames());
807 native_data
->SetDevNames(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();
817 pd
->Flags
= PD_RETURNDC
;
818 pd
->lStructSize
= sizeof( PRINTDLG
);
820 pd
->hwndOwner
= NULL
;
821 pd
->hInstance
= NULL
;
823 pd
->lpfnPrintHook
= NULL
;
824 pd
->lpfnSetupHook
= NULL
;
825 pd
->lpPrintTemplateName
= NULL
;
826 pd
->lpSetupTemplateName
= NULL
;
827 pd
->hPrintTemplate
= NULL
;
828 pd
->hSetupTemplate
= NULL
;
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
;
852 bool wxWindowsPrintDialog::ConvertFromNative( wxPrintDialogData
&data
)
854 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
858 wxWindowsPrintNativeData
*native_data
=
859 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
861 // Pass the devmode data back to the wxPrintData structure where it really belongs.
864 if (native_data
->GetDevMode())
866 ::GlobalFree(static_cast<HGLOBAL
>(native_data
->GetDevMode()));
868 native_data
->SetDevMode(pd
->hDevMode
);
872 // Pass the devnames data back to the wxPrintData structure where it really belongs.
875 if (native_data
->GetDevNames())
877 ::GlobalFree(static_cast<HGLOBAL
>(native_data
->GetDevNames()));
879 native_data
->SetDevNames(pd
->hDevNames
);
880 pd
->hDevNames
= NULL
;
883 // Now convert the DEVMODE object, passed down from the PRINTDLG object,
884 // into wxWidgets form.
885 native_data
->TransferTo( data
.GetPrintData() );
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
);
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
) );
905 // ---------------------------------------------------------------------------
906 // wxWidnowsPageSetupDialog
907 // ---------------------------------------------------------------------------
909 IMPLEMENT_CLASS(wxWindowsPageSetupDialog
, wxPageSetupDialogBase
)
911 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog()
913 m_dialogParent
= NULL
;
917 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog(wxWindow
*p
, wxPageSetupDialogData
*data
)
922 bool wxWindowsPageSetupDialog::Create(wxWindow
*p
, wxPageSetupDialogData
*data
)
928 m_pageSetupData
= (*data
);
933 wxWindowsPageSetupDialog::~wxWindowsPageSetupDialog()
935 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*)m_pageDlg
;
936 if ( pd
&& pd
->hDevMode
)
937 GlobalFree(pd
->hDevMode
);
938 if ( pd
&& pd
->hDevNames
)
939 GlobalFree(pd
->hDevNames
);
944 int wxWindowsPageSetupDialog::ShowModal()
946 ConvertToNative( m_pageSetupData
);
948 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
950 pd
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
951 else if (wxTheApp
->GetTopWindow())
952 pd
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
955 BOOL retVal
= PageSetupDlg( pd
) ;
959 ConvertFromNative( m_pageSetupData
);
966 bool wxWindowsPageSetupDialog::ConvertToNative( wxPageSetupDialogData
&data
)
968 wxWindowsPrintNativeData
*native_data
=
969 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
970 data
.GetPrintData().ConvertToNative();
972 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
974 // Shouldn't have been defined anywhere
978 pd
= new PAGESETUPDLG
;
980 pd
->hDevNames
= NULL
;
981 m_pageDlg
= (void *)pd
;
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.
989 GlobalFree(pd
->hDevMode
);
992 pd
->hDevMode
= (HGLOBAL
) native_data
->GetDevMode();
993 native_data
->SetDevMode(NULL
);
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!"));
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.
1004 GlobalFree(pd
->hDevNames
);
1005 pd
->hDevNames
= NULL
;
1007 pd
->hDevNames
= (HGLOBAL
) native_data
->GetDevNames();
1008 native_data
->SetDevNames(NULL
);
1010 // pd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
1012 pd
->Flags
= PSD_MARGINS
|PSD_MINMARGINS
;
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
;
1029 // We want the units to be in hundredths of a millimetre
1030 pd
->Flags
|= PSD_INHUNDREDTHSOFMILLIMETERS
;
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;
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;
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;
1050 pd
->lpfnPageSetupHook
= NULL
;
1051 pd
->lpfnPagePaintHook
= NULL
;
1052 pd
->hPageSetupTemplate
= NULL
;
1053 pd
->lpPageSetupTemplateName
= NULL
;
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);
1069 bool wxWindowsPageSetupDialog::ConvertFromNative( wxPageSetupDialogData
&data
)
1071 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
1075 wxWindowsPrintNativeData
*native_data
=
1076 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
1078 // Pass the devmode data back to the wxPrintData structure where it really belongs.
1081 if (native_data
->GetDevMode())
1083 // Make sure we don't leak memory
1084 GlobalFree((HGLOBAL
) native_data
->GetDevMode());
1086 native_data
->SetDevMode( (void*) pd
->hDevMode
);
1087 pd
->hDevMode
= NULL
;
1090 // Isn't this superfluous? It's called again below.
1091 // data.GetPrintData().ConvertFromNative();
1093 // Pass the devnames data back to the wxPrintData structure where it really belongs.
1096 if (native_data
->GetDevNames())
1098 // Make sure we don't leak memory
1099 GlobalFree((HGLOBAL
) native_data
->GetDevNames());
1101 native_data
->SetDevNames((void*) pd
->hDevNames
);
1102 pd
->hDevNames
= NULL
;
1105 data
.GetPrintData().ConvertFromNative();
1107 pd
->Flags
= PSD_MARGINS
|PSD_MINMARGINS
;
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
) );
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) );
1121 data
.SetPaperSize( wxSize(pd
->ptPaperSize
.x
/ 100, pd
->ptPaperSize
.y
/ 100) );
1123 data
.SetMinMarginTopLeft( wxPoint(pd
->rtMinMargin
.left
/ 100, pd
->rtMinMargin
.top
/ 100) );
1124 data
.SetMinMarginBottomRight( wxPoint(pd
->rtMinMargin
.right
/ 100, pd
->rtMinMargin
.bottom
/ 100) );
1126 data
.SetMarginTopLeft( wxPoint(pd
->rtMargin
.left
/ 100, pd
->rtMargin
.top
/ 100) );
1127 data
.SetMarginBottomRight( wxPoint(pd
->rtMargin
.right
/ 100, pd
->rtMargin
.bottom
/ 100) );
1133 // wxUSE_PRINTING_ARCHITECTURE