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 //----------------------------------------------------------------------------
46 // wxWindowsPrintNativeData
47 //----------------------------------------------------------------------------
51 static wxString
wxGetPrintDlgError()
53 DWORD err
= CommDlgExtendedError();
54 wxString msg
= wxT("Unknown");
57 case CDERR_FINDRESFAILURE
: msg
= wxT("CDERR_FINDRESFAILURE"); break;
58 case CDERR_INITIALIZATION
: msg
= wxT("CDERR_INITIALIZATION"); break;
59 case CDERR_LOADRESFAILURE
: msg
= wxT("CDERR_LOADRESFAILURE"); break;
60 case CDERR_LOADSTRFAILURE
: msg
= wxT("CDERR_LOADSTRFAILURE"); break;
61 case CDERR_LOCKRESFAILURE
: msg
= wxT("CDERR_LOCKRESFAILURE"); break;
62 case CDERR_MEMALLOCFAILURE
: msg
= wxT("CDERR_MEMALLOCFAILURE"); break;
63 case CDERR_MEMLOCKFAILURE
: msg
= wxT("CDERR_MEMLOCKFAILURE"); break;
64 case CDERR_NOHINSTANCE
: msg
= wxT("CDERR_NOHINSTANCE"); break;
65 case CDERR_NOHOOK
: msg
= wxT("CDERR_NOHOOK"); break;
66 case CDERR_NOTEMPLATE
: msg
= wxT("CDERR_NOTEMPLATE"); break;
67 case CDERR_STRUCTSIZE
: msg
= wxT("CDERR_STRUCTSIZE"); break;
68 case PDERR_RETDEFFAILURE
: msg
= wxT("PDERR_RETDEFFAILURE"); break;
69 case PDERR_PRINTERNOTFOUND
: msg
= wxT("PDERR_PRINTERNOTFOUND"); break;
70 case PDERR_PARSEFAILURE
: msg
= wxT("PDERR_PARSEFAILURE"); break;
71 case PDERR_NODEVICES
: msg
= wxT("PDERR_NODEVICES"); break;
72 case PDERR_NODEFAULTPRN
: msg
= wxT("PDERR_NODEFAULTPRN"); break;
73 case PDERR_LOADDRVFAILURE
: msg
= wxT("PDERR_LOADDRVFAILURE"); break;
74 case PDERR_INITFAILURE
: msg
= wxT("PDERR_INITFAILURE"); break;
75 case PDERR_GETDEVMODEFAIL
: msg
= wxT("PDERR_GETDEVMODEFAIL"); break;
76 case PDERR_DNDMMISMATCH
: msg
= wxT("PDERR_DNDMMISMATCH"); break;
77 case PDERR_DEFAULTDIFFERENT
: msg
= wxT("PDERR_DEFAULTDIFFERENT"); break;
78 case PDERR_CREATEICFAILURE
: msg
= wxT("PDERR_CREATEICFAILURE"); break;
84 #endif // wxDEBUG_LEVEL
88 wxCreateDevNames(const wxString
& driverName
,
89 const wxString
& printerName
,
90 const wxString
& portName
)
93 // if (!driverName.empty() && !printerName.empty() && !portName.empty())
94 if (driverName
.empty() && printerName
.empty() && portName
.empty())
99 hDev
= GlobalAlloc(GPTR
, 4*sizeof(WORD
)+
100 ( driverName
.length() + 1 +
101 printerName
.length() + 1 +
102 portName
.length()+1 ) * sizeof(wxChar
) );
103 LPDEVNAMES lpDev
= (LPDEVNAMES
)GlobalLock(hDev
);
104 lpDev
->wDriverOffset
= sizeof(WORD
) * 4 / sizeof(wxChar
);
105 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wDriverOffset
, driverName
);
107 lpDev
->wDeviceOffset
= (WORD
)( lpDev
->wDriverOffset
+
108 driverName
.length() + 1 );
109 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wDeviceOffset
, printerName
);
111 lpDev
->wOutputOffset
= (WORD
)( lpDev
->wDeviceOffset
+
112 printerName
.length() + 1 );
113 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wOutputOffset
, portName
);
123 IMPLEMENT_CLASS(wxWindowsPrintNativeData
, wxPrintNativeDataBase
)
125 wxWindowsPrintNativeData::wxWindowsPrintNativeData()
129 m_customWindowsPaperId
= 0;
132 wxWindowsPrintNativeData::~wxWindowsPrintNativeData()
135 ::GlobalFree(static_cast<HGLOBAL
>(m_devMode
));
138 ::GlobalFree(static_cast<HGLOBAL
>(m_devNames
));
141 bool wxWindowsPrintNativeData::IsOk() const
143 return (m_devMode
!= NULL
) ;
146 bool wxWindowsPrintNativeData::TransferTo( wxPrintData
&data
)
151 GlobalPtrLock
lockDevMode(m_devMode
);
153 LPDEVMODE devMode
= static_cast<LPDEVMODE
>(lockDevMode
.Get());
156 if (devMode
->dmFields
& DM_ORIENTATION
)
157 data
.SetOrientation( devMode
->dmOrientation
);
160 if (devMode
->dmFields
& DM_COLLATE
)
162 if (devMode
->dmCollate
== DMCOLLATE_TRUE
)
163 data
.SetCollate( true );
165 data
.SetCollate( false );
168 //// Number of copies
169 if (devMode
->dmFields
& DM_COPIES
)
170 data
.SetNoCopies( devMode
->dmCopies
);
173 if (devMode
->dmFields
& DM_DEFAULTSOURCE
) {
174 switch (devMode
->dmDefaultSource
) {
175 case DMBIN_ONLYONE
: data
.SetBin(wxPRINTBIN_ONLYONE
); break;
176 case DMBIN_LOWER
: data
.SetBin(wxPRINTBIN_LOWER
); break;
177 case DMBIN_MIDDLE
: data
.SetBin(wxPRINTBIN_MIDDLE
); break;
178 case DMBIN_MANUAL
: data
.SetBin(wxPRINTBIN_MANUAL
); break;
179 case DMBIN_ENVELOPE
: data
.SetBin(wxPRINTBIN_ENVELOPE
); break;
180 case DMBIN_ENVMANUAL
: data
.SetBin(wxPRINTBIN_ENVMANUAL
); break;
181 case DMBIN_AUTO
: data
.SetBin(wxPRINTBIN_AUTO
); break;
182 case DMBIN_TRACTOR
: data
.SetBin(wxPRINTBIN_TRACTOR
); break;
183 case DMBIN_SMALLFMT
: data
.SetBin(wxPRINTBIN_SMALLFMT
); break;
184 case DMBIN_LARGEFMT
: data
.SetBin(wxPRINTBIN_LARGEFMT
); break;
185 case DMBIN_LARGECAPACITY
: data
.SetBin(wxPRINTBIN_LARGECAPACITY
); break;
186 case DMBIN_CASSETTE
: data
.SetBin(wxPRINTBIN_CASSETTE
); break;
187 case DMBIN_FORMSOURCE
: data
.SetBin(wxPRINTBIN_FORMSOURCE
); break;
189 if (devMode
->dmDefaultSource
>= DMBIN_USER
)
190 data
.SetBin((wxPrintBin
)((devMode
->dmDefaultSource
)-DMBIN_USER
+(int)wxPRINTBIN_USER
));
192 data
.SetBin(wxPRINTBIN_DEFAULT
);
195 data
.SetBin(wxPRINTBIN_DEFAULT
);
197 if (devMode
->dmFields
& DM_MEDIATYPE
)
199 wxASSERT( (int)devMode
->dmMediaType
!= wxPRINTMEDIA_DEFAULT
);
200 data
.SetMedia(devMode
->dmMediaType
);
203 if (devMode
->dmDeviceName
[0] != 0)
204 // This syntax fixes a crash when using VS 7.1
205 data
.SetPrinterName( wxString(devMode
->dmDeviceName
, CCHDEVICENAME
) );
208 if (devMode
->dmFields
& DM_COLOR
)
210 if (devMode
->dmColor
== DMCOLOR_COLOR
)
211 data
.SetColour( true );
213 data
.SetColour( false );
216 data
.SetColour( true );
220 // We don't know size of user defined paper and some buggy drivers
221 // set both DM_PAPERSIZE and DM_PAPERWIDTH & DM_PAPERLENGTH. Since
222 // dmPaperSize >= DMPAPER_USER wouldn't be in wxWin's database, this
223 // code wouldn't set m_paperSize correctly.
225 bool foundPaperSize
= false;
226 if ((devMode
->dmFields
& DM_PAPERSIZE
) && (devMode
->dmPaperSize
< DMPAPER_USER
))
228 if (wxThePrintPaperDatabase
)
230 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperTypeByPlatformId(devMode
->dmPaperSize
);
233 data
.SetPaperId( paper
->GetId() );
234 data
.SetPaperSize( wxSize(paper
->GetWidth() / 10,paper
->GetHeight() / 10) );
235 m_customWindowsPaperId
= 0;
236 foundPaperSize
= true;
241 // Shouldn't really get here
242 wxFAIL_MSG(wxT("Paper database wasn't initialized in wxPrintData::ConvertFromNative."));
243 data
.SetPaperId( wxPAPER_NONE
);
244 data
.SetPaperSize( wxSize(0,0) );
245 m_customWindowsPaperId
= 0;
251 if (!foundPaperSize
) {
252 if ((devMode
->dmFields
& DM_PAPERWIDTH
) && (devMode
->dmFields
& DM_PAPERLENGTH
))
254 // DEVMODE is in tenths of a millimeter
255 data
.SetPaperSize( wxSize(devMode
->dmPaperWidth
/ 10, devMode
->dmPaperLength
/ 10) );
256 data
.SetPaperId( wxPAPER_NONE
);
257 m_customWindowsPaperId
= devMode
->dmPaperSize
;
261 // Often will reach this for non-standard paper sizes (sizes which
262 // wouldn't be in wxWidget's paper database). Setting
263 // m_customWindowsPaperId to devMode->dmPaperSize should be enough
264 // to get this paper size working.
265 data
.SetPaperSize( wxSize(0,0) );
266 data
.SetPaperId( wxPAPER_NONE
);
267 m_customWindowsPaperId
= devMode
->dmPaperSize
;
273 if (devMode
->dmFields
& DM_DUPLEX
)
275 switch (devMode
->dmDuplex
)
277 case DMDUP_HORIZONTAL
: data
.SetDuplex( wxDUPLEX_HORIZONTAL
); break;
278 case DMDUP_VERTICAL
: data
.SetDuplex( wxDUPLEX_VERTICAL
); break;
280 case DMDUP_SIMPLEX
: data
.SetDuplex( wxDUPLEX_SIMPLEX
); break;
284 data
.SetDuplex( wxDUPLEX_SIMPLEX
);
288 if (devMode
->dmFields
& DM_PRINTQUALITY
)
290 switch (devMode
->dmPrintQuality
)
292 case DMRES_MEDIUM
: data
.SetQuality( wxPRINT_QUALITY_MEDIUM
); break;
293 case DMRES_LOW
: data
.SetQuality( wxPRINT_QUALITY_LOW
); break;
294 case DMRES_DRAFT
: data
.SetQuality( wxPRINT_QUALITY_DRAFT
); break;
295 case DMRES_HIGH
: data
.SetQuality( wxPRINT_QUALITY_HIGH
); break;
298 // TODO: if the printer fills in the resolution in DPI, how
299 // will the application know if it's high, low, draft etc.??
300 // wxFAIL_MSG("Warning: DM_PRINTQUALITY was not one of the standard values.");
301 data
.SetQuality( devMode
->dmPrintQuality
);
308 data
.SetQuality( wxPRINT_QUALITY_HIGH
);
310 if (devMode
->dmDriverExtra
> 0)
311 data
.SetPrivData( (char *)devMode
+devMode
->dmSize
, devMode
->dmDriverExtra
);
313 data
.SetPrivData( NULL
, 0 );
317 GlobalPtrLock
lockDevNames(m_devNames
);
318 LPDEVNAMES lpDevNames
= static_cast<LPDEVNAMES
>(lockDevNames
.Get());
320 // TODO: Unicode-ification
323 // port is obsolete in WIN32
324 // m_printData.SetPortName((LPSTR)lpDevNames + lpDevNames->wDriverOffset);
326 // Get the printer name
327 wxString printerName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
329 // Not sure if we should check for this mismatch
330 // wxASSERT_MSG( (m_printerName.empty() || (devName == m_printerName)), "Printer name obtained from DEVMODE and DEVNAMES were different!");
332 if (!printerName
.empty())
333 data
.SetPrinterName( printerName
);
339 bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData
&data
)
341 HGLOBAL hDevMode
= static_cast<HGLOBAL
>(m_devMode
);
343 LPTSTR szPrinterName
= (LPTSTR
)data
.GetPrinterName().wx_str();
345 // From MSDN: How To Modify Printer Settings with the DocumentProperties() Function
346 // The purpose of this is to fill the DEVMODE with privdata from printer driver.
347 // If we have a printer name and OpenPrinter sucessfully returns
348 // this replaces the PrintDlg function which creates the DEVMODE filled only with data from default printer.
349 if ( !m_devMode
&& !data
.GetPrinterName().IsEmpty() )
352 if ( printer
.Open( data
.GetPrinterName() ) == TRUE
)
354 DWORD dwNeeded
, dwRet
;
357 // Allocate a buffer of the correct size.
358 dwNeeded
= DocumentProperties( NULL
,
359 printer
, // Handle to our printer.
360 szPrinterName
, // Name of the printer.
361 NULL
, // Asking for size, so
362 NULL
, // these are not used.
363 0 ); // Zero returns buffer size.
365 LPDEVMODE tempDevMode
= static_cast<LPDEVMODE
>( GlobalAlloc( GMEM_FIXED
| GMEM_ZEROINIT
, dwNeeded
) );
368 // Get the default DevMode for the printer
369 dwRet
= DocumentProperties( NULL
,
372 tempDevMode
, // The address of the buffer to fill.
373 NULL
, // Not using the input buffer.
374 DM_OUT_BUFFER
); // Have the output buffer filled.
378 // If failure, cleanup
379 GlobalFree( tempDevMode
);
384 hDevMode
= tempDevMode
;
385 m_devMode
= hDevMode
;
393 // Use PRINTDLG as a way of creating a DEVMODE object
396 memset(&pd
, 0, sizeof(PRINTDLG
));
398 pd
.cbStruct
= sizeof(PRINTDLG
);
400 pd
.lStructSize
= sizeof(PRINTDLG
);
404 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
405 pd
.hDevNames
= NULL
; // Ditto
407 pd
.Flags
= PD_RETURNDEFAULT
;
410 // Fill out the DEVMODE structure
411 // so we can use it as input in the 'real' PrintDlg
415 GlobalFree(pd
.hDevMode
);
417 GlobalFree(pd
.hDevNames
);
422 wxLogDebug(wxT("Printing error: ") + wxGetPrintDlgError());
423 #endif // wxDEBUG_LEVEL
427 hDevMode
= pd
.hDevMode
;
428 m_devMode
= hDevMode
;
431 // We'll create a new DEVNAMEs structure below.
433 GlobalFree(pd
.hDevNames
);
436 // hDevNames = pd->hDevNames;
437 // m_devNames = (void*)(long) hDevNames;
438 // pd->hDevnames = NULL;
445 GlobalPtrLock
lockDevMode(hDevMode
);
446 DEVMODE
* const devMode
= static_cast<DEVMODE
*>(lockDevMode
.Get());
449 devMode
->dmOrientation
= (short)data
.GetOrientation();
452 devMode
->dmCollate
= (data
.GetCollate() ? DMCOLLATE_TRUE
: DMCOLLATE_FALSE
);
453 devMode
->dmFields
|= DM_COLLATE
;
455 //// Number of copies
456 devMode
->dmCopies
= (short)data
.GetNoCopies();
457 devMode
->dmFields
|= DM_COPIES
;
460 wxString name
= data
.GetPrinterName();
463 // NB: the cast is needed in the ANSI build, strangely enough
464 // dmDeviceName is BYTE[] and not char[] there
465 wxStrlcpy(reinterpret_cast<wxChar
*>(devMode
->dmDeviceName
),
467 WXSIZEOF(devMode
->dmDeviceName
));
471 if (data
.GetColour())
472 devMode
->dmColor
= DMCOLOR_COLOR
;
474 devMode
->dmColor
= DMCOLOR_MONOCHROME
;
475 devMode
->dmFields
|= DM_COLOR
;
479 // Paper id has priority over paper size. If id is specified, then size
480 // is ignored (as it can be filled in even for standard paper sizes)
482 wxPrintPaperType
*paperType
= NULL
;
484 const wxPaperSize paperId
= data
.GetPaperId();
485 if ( paperId
!= wxPAPER_NONE
&& wxThePrintPaperDatabase
)
487 paperType
= wxThePrintPaperDatabase
->FindPaperType(paperId
);
492 devMode
->dmPaperSize
= (short)paperType
->GetPlatformId();
493 devMode
->dmFields
|= DM_PAPERSIZE
;
495 else // custom (or no) paper size
497 const wxSize paperSize
= data
.GetPaperSize();
498 if ( paperSize
!= wxDefaultSize
)
500 // Fall back on specifying the paper size explicitly
501 if(m_customWindowsPaperId
!= 0)
502 devMode
->dmPaperSize
= m_customWindowsPaperId
;
504 devMode
->dmPaperSize
= DMPAPER_USER
;
505 devMode
->dmPaperWidth
= (short)(paperSize
.x
* 10);
506 devMode
->dmPaperLength
= (short)(paperSize
.y
* 10);
507 devMode
->dmFields
|= DM_PAPERWIDTH
;
508 devMode
->dmFields
|= DM_PAPERLENGTH
;
510 // A printer driver may or may not also want DM_PAPERSIZE to
511 // be specified. Also, if the printer driver doesn't implement the DMPAPER_USER
512 // size, then this won't work, and even if you found the correct id by
513 // enumerating the driver's paper sizes, it probably won't change the actual size,
514 // it'll just select that custom paper type with its own current setting.
515 // For a discussion on this, see http://www.codeguru.com/forum/showthread.php?threadid=458617
516 // Although m_customWindowsPaperId is intended to work around this, it's
517 // unclear how it can help you set the custom paper size programmatically.
519 //else: neither paper type nor size specified, don't fill DEVMODE
520 // at all so that the system defaults are used
525 switch (data
.GetDuplex())
527 case wxDUPLEX_HORIZONTAL
:
528 duplex
= DMDUP_HORIZONTAL
;
530 case wxDUPLEX_VERTICAL
:
531 duplex
= DMDUP_VERTICAL
;
534 // in fact case wxDUPLEX_SIMPLEX:
535 duplex
= DMDUP_SIMPLEX
;
538 devMode
->dmDuplex
= duplex
;
539 devMode
->dmFields
|= DM_DUPLEX
;
544 switch (data
.GetQuality())
546 case wxPRINT_QUALITY_MEDIUM
:
547 quality
= DMRES_MEDIUM
;
549 case wxPRINT_QUALITY_LOW
:
552 case wxPRINT_QUALITY_DRAFT
:
553 quality
= DMRES_DRAFT
;
555 case wxPRINT_QUALITY_HIGH
:
556 quality
= DMRES_HIGH
;
559 quality
= (short)data
.GetQuality();
560 devMode
->dmYResolution
= quality
;
561 devMode
->dmFields
|= DM_YRESOLUTION
;
564 devMode
->dmPrintQuality
= quality
;
565 devMode
->dmFields
|= DM_PRINTQUALITY
;
567 if (data
.GetPrivDataLen() > 0)
569 memcpy( (char *)devMode
+devMode
->dmSize
, data
.GetPrivData(), data
.GetPrivDataLen() );
570 devMode
->dmDriverExtra
= (WXWORD
)data
.GetPrivDataLen();
573 if (data
.GetBin() != wxPRINTBIN_DEFAULT
)
575 switch (data
.GetBin())
577 case wxPRINTBIN_ONLYONE
: devMode
->dmDefaultSource
= DMBIN_ONLYONE
; break;
578 case wxPRINTBIN_LOWER
: devMode
->dmDefaultSource
= DMBIN_LOWER
; break;
579 case wxPRINTBIN_MIDDLE
: devMode
->dmDefaultSource
= DMBIN_MIDDLE
; break;
580 case wxPRINTBIN_MANUAL
: devMode
->dmDefaultSource
= DMBIN_MANUAL
; break;
581 case wxPRINTBIN_ENVELOPE
: devMode
->dmDefaultSource
= DMBIN_ENVELOPE
; break;
582 case wxPRINTBIN_ENVMANUAL
: devMode
->dmDefaultSource
= DMBIN_ENVMANUAL
; break;
583 case wxPRINTBIN_AUTO
: devMode
->dmDefaultSource
= DMBIN_AUTO
; break;
584 case wxPRINTBIN_TRACTOR
: devMode
->dmDefaultSource
= DMBIN_TRACTOR
; break;
585 case wxPRINTBIN_SMALLFMT
: devMode
->dmDefaultSource
= DMBIN_SMALLFMT
; break;
586 case wxPRINTBIN_LARGEFMT
: devMode
->dmDefaultSource
= DMBIN_LARGEFMT
; break;
587 case wxPRINTBIN_LARGECAPACITY
: devMode
->dmDefaultSource
= DMBIN_LARGECAPACITY
; break;
588 case wxPRINTBIN_CASSETTE
: devMode
->dmDefaultSource
= DMBIN_CASSETTE
; break;
589 case wxPRINTBIN_FORMSOURCE
: devMode
->dmDefaultSource
= DMBIN_FORMSOURCE
; break;
592 devMode
->dmDefaultSource
= (short)(DMBIN_USER
+ data
.GetBin() - wxPRINTBIN_USER
); // 256 + data.GetBin() - 14 = 242 + data.GetBin()
596 devMode
->dmFields
|= DM_DEFAULTSOURCE
;
598 if (data
.GetMedia() != wxPRINTMEDIA_DEFAULT
)
600 devMode
->dmMediaType
= data
.GetMedia();
601 devMode
->dmFields
|= DM_MEDIATYPE
;
607 // Merge the new settings with the old.
608 // This gives the driver an opportunity to update any private
609 // portions of the DevMode structure.
610 DocumentProperties( NULL
,
613 (LPDEVMODE
)hDevMode
, // Reuse our buffer for output.
614 (LPDEVMODE
)hDevMode
, // Pass the driver our changes
615 DM_IN_BUFFER
| // Commands to Merge our changes and
616 DM_OUT_BUFFER
); // write the result.
622 ::GlobalFree(static_cast<HGLOBAL
>(m_devNames
));
625 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
626 m_devNames
= wxCreateDevNames(wxEmptyString
, data
.GetPrinterName(), wxEmptyString
);
631 // ---------------------------------------------------------------------------
633 // ---------------------------------------------------------------------------
635 IMPLEMENT_CLASS(wxWindowsPrintDialog
, wxPrintDialogBase
)
637 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintDialogData
* data
)
642 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintData
* data
)
644 wxPrintDialogData data2
;
651 bool wxWindowsPrintDialog::Create(wxWindow
*p
, wxPrintDialogData
* data
)
661 m_printDialogData
= *data
;
666 wxWindowsPrintDialog::~wxWindowsPrintDialog()
668 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
669 if (pd
&& pd
->hDevMode
)
670 GlobalFree(pd
->hDevMode
);
674 if (m_destroyDC
&& m_printerDC
)
678 int wxWindowsPrintDialog::ShowModal()
680 ConvertToNative( m_printDialogData
);
682 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
685 pd
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
686 else if (wxTheApp
->GetTopWindow())
687 pd
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
691 bool ret
= (PrintDlg( pd
) != 0);
695 if ( ret
&& (pd
->hDC
) )
697 wxPrinterDC
*pdc
= new wxPrinterDCFromHDC( (WXHDC
) pd
->hDC
);
699 ConvertFromNative( m_printDialogData
);
708 wxDC
*wxWindowsPrintDialog::GetPrintDC()
719 bool wxWindowsPrintDialog::ConvertToNative( wxPrintDialogData
&data
)
721 wxWindowsPrintNativeData
*native_data
=
722 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
723 data
.GetPrintData().ConvertToNative();
725 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
727 // Shouldn't have been defined anywhere
732 memset( pd
, 0, sizeof(PRINTDLG
) );
733 m_printDlg
= (void*) pd
;
735 pd
->lStructSize
= sizeof(PRINTDLG
);
736 pd
->hwndOwner
= NULL
;
737 pd
->hDevMode
= NULL
; // Will be created by PrintDlg
738 pd
->hDevNames
= NULL
; // Ditto
740 pd
->Flags
= PD_RETURNDEFAULT
;
743 // Pass the devmode data to the PRINTDLG structure, since it'll
744 // be needed when PrintDlg is called.
746 GlobalFree(pd
->hDevMode
);
748 // Pass the devnames data to the PRINTDLG structure, since it'll
749 // be needed when PrintDlg is called.
751 GlobalFree(pd
->hDevNames
);
753 pd
->hDevMode
= static_cast<HGLOBAL
>(native_data
->GetDevMode());
754 native_data
->SetDevMode(NULL
);
756 // Shouldn't assert; we should be able to test Ok-ness at a higher level
757 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
759 pd
->hDevNames
= static_cast<HGLOBAL
>(native_data
->GetDevNames());
760 native_data
->SetDevNames(NULL
);
764 pd
->nFromPage
= (WORD
)data
.GetFromPage();
765 pd
->nToPage
= (WORD
)data
.GetToPage();
766 pd
->nMinPage
= (WORD
)data
.GetMinPage();
767 pd
->nMaxPage
= (WORD
)data
.GetMaxPage();
768 pd
->nCopies
= (WORD
)data
.GetNoCopies();
770 pd
->Flags
= PD_RETURNDC
;
771 pd
->lStructSize
= sizeof( PRINTDLG
);
773 pd
->hwndOwner
= NULL
;
774 pd
->hInstance
= NULL
;
776 pd
->lpfnPrintHook
= NULL
;
777 pd
->lpfnSetupHook
= NULL
;
778 pd
->lpPrintTemplateName
= NULL
;
779 pd
->lpSetupTemplateName
= NULL
;
780 pd
->hPrintTemplate
= NULL
;
781 pd
->hSetupTemplate
= NULL
;
783 if ( data
.GetAllPages() )
784 pd
->Flags
|= PD_ALLPAGES
;
785 if ( data
.GetSelection() )
786 pd
->Flags
|= PD_SELECTION
;
787 if ( data
.GetCollate() )
788 pd
->Flags
|= PD_COLLATE
;
789 if ( data
.GetPrintToFile() )
790 pd
->Flags
|= PD_PRINTTOFILE
;
791 if ( !data
.GetEnablePrintToFile() )
792 pd
->Flags
|= PD_DISABLEPRINTTOFILE
;
793 if ( !data
.GetEnableSelection() )
794 pd
->Flags
|= PD_NOSELECTION
;
795 if ( !data
.GetEnablePageNumbers() )
796 pd
->Flags
|= PD_NOPAGENUMS
;
797 else if ( (!data
.GetAllPages()) && (!data
.GetSelection()) && (data
.GetFromPage() != 0) && (data
.GetToPage() != 0))
798 pd
->Flags
|= PD_PAGENUMS
;
799 if ( data
.GetEnableHelp() )
800 pd
->Flags
|= PD_SHOWHELP
;
805 bool wxWindowsPrintDialog::ConvertFromNative( wxPrintDialogData
&data
)
807 PRINTDLG
*pd
= (PRINTDLG
*) m_printDlg
;
811 wxWindowsPrintNativeData
*native_data
=
812 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
814 // Pass the devmode data back to the wxPrintData structure where it really belongs.
817 if (native_data
->GetDevMode())
819 ::GlobalFree(static_cast<HGLOBAL
>(native_data
->GetDevMode()));
821 native_data
->SetDevMode(pd
->hDevMode
);
825 // Pass the devnames data back to the wxPrintData structure where it really belongs.
828 if (native_data
->GetDevNames())
830 ::GlobalFree(static_cast<HGLOBAL
>(native_data
->GetDevNames()));
832 native_data
->SetDevNames(pd
->hDevNames
);
833 pd
->hDevNames
= NULL
;
836 // Now convert the DEVMODE object, passed down from the PRINTDLG object,
837 // into wxWidgets form.
838 native_data
->TransferTo( data
.GetPrintData() );
840 data
.SetFromPage( pd
->nFromPage
);
841 data
.SetToPage( pd
->nToPage
);
842 data
.SetMinPage( pd
->nMinPage
);
843 data
.SetMaxPage( pd
->nMaxPage
);
844 data
.SetNoCopies( pd
->nCopies
);
846 data
.SetAllPages( (((pd
->Flags
& PD_PAGENUMS
) != PD_PAGENUMS
) && ((pd
->Flags
& PD_SELECTION
) != PD_SELECTION
)) );
847 data
.SetSelection( ((pd
->Flags
& PD_SELECTION
) == PD_SELECTION
) );
848 data
.SetCollate( ((pd
->Flags
& PD_COLLATE
) == PD_COLLATE
) );
849 data
.SetPrintToFile( ((pd
->Flags
& PD_PRINTTOFILE
) == PD_PRINTTOFILE
) );
850 data
.EnablePrintToFile( ((pd
->Flags
& PD_DISABLEPRINTTOFILE
) != PD_DISABLEPRINTTOFILE
) );
851 data
.EnableSelection( ((pd
->Flags
& PD_NOSELECTION
) != PD_NOSELECTION
) );
852 data
.EnablePageNumbers( ((pd
->Flags
& PD_NOPAGENUMS
) != PD_NOPAGENUMS
) );
853 data
.EnableHelp( ((pd
->Flags
& PD_SHOWHELP
) == PD_SHOWHELP
) );
858 // ---------------------------------------------------------------------------
859 // wxWidnowsPageSetupDialog
860 // ---------------------------------------------------------------------------
862 IMPLEMENT_CLASS(wxWindowsPageSetupDialog
, wxPageSetupDialogBase
)
864 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog()
866 m_dialogParent
= NULL
;
870 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog(wxWindow
*p
, wxPageSetupDialogData
*data
)
875 bool wxWindowsPageSetupDialog::Create(wxWindow
*p
, wxPageSetupDialogData
*data
)
881 m_pageSetupData
= (*data
);
886 wxWindowsPageSetupDialog::~wxWindowsPageSetupDialog()
888 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*)m_pageDlg
;
889 if ( pd
&& pd
->hDevMode
)
890 GlobalFree(pd
->hDevMode
);
891 if ( pd
&& pd
->hDevNames
)
892 GlobalFree(pd
->hDevNames
);
897 int wxWindowsPageSetupDialog::ShowModal()
899 ConvertToNative( m_pageSetupData
);
901 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
903 pd
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
904 else if (wxTheApp
->GetTopWindow())
905 pd
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
908 BOOL retVal
= PageSetupDlg( pd
) ;
912 ConvertFromNative( m_pageSetupData
);
919 bool wxWindowsPageSetupDialog::ConvertToNative( wxPageSetupDialogData
&data
)
921 wxWindowsPrintNativeData
*native_data
=
922 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
923 data
.GetPrintData().ConvertToNative();
925 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
927 // Shouldn't have been defined anywhere
931 pd
= new PAGESETUPDLG
;
933 pd
->hDevNames
= NULL
;
934 m_pageDlg
= (void *)pd
;
936 // Pass the devmode data (created in m_printData.ConvertToNative)
937 // to the PRINTDLG structure, since it'll
938 // be needed when PrintDlg is called.
942 GlobalFree(pd
->hDevMode
);
945 pd
->hDevMode
= (HGLOBAL
) native_data
->GetDevMode();
946 native_data
->SetDevMode(NULL
);
948 // Shouldn't assert; we should be able to test Ok-ness at a higher level
949 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
951 // Pass the devnames data (created in m_printData.ConvertToNative)
952 // to the PRINTDLG structure, since it'll
953 // be needed when PrintDlg is called.
957 GlobalFree(pd
->hDevNames
);
958 pd
->hDevNames
= NULL
;
960 pd
->hDevNames
= (HGLOBAL
) native_data
->GetDevNames();
961 native_data
->SetDevNames(NULL
);
963 // pd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
965 pd
->Flags
= PSD_MARGINS
|PSD_MINMARGINS
;
967 if ( data
.GetDefaultMinMargins() )
968 pd
->Flags
|= PSD_DEFAULTMINMARGINS
;
969 if ( !data
.GetEnableMargins() )
970 pd
->Flags
|= PSD_DISABLEMARGINS
;
971 if ( !data
.GetEnableOrientation() )
972 pd
->Flags
|= PSD_DISABLEORIENTATION
;
973 if ( !data
.GetEnablePaper() )
974 pd
->Flags
|= PSD_DISABLEPAPER
;
975 if ( !data
.GetEnablePrinter() )
976 pd
->Flags
|= PSD_DISABLEPRINTER
;
977 if ( data
.GetDefaultInfo() )
978 pd
->Flags
|= PSD_RETURNDEFAULT
;
979 if ( data
.GetEnableHelp() )
980 pd
->Flags
|= PSD_SHOWHELP
;
982 // We want the units to be in hundredths of a millimetre
983 pd
->Flags
|= PSD_INHUNDREDTHSOFMILLIMETERS
;
985 pd
->lStructSize
= sizeof( PAGESETUPDLG
);
986 pd
->hwndOwner
= NULL
;
987 pd
->hInstance
= NULL
;
988 // PAGESETUPDLG is in hundreds of a mm
989 pd
->ptPaperSize
.x
= data
.GetPaperSize().x
* 100;
990 pd
->ptPaperSize
.y
= data
.GetPaperSize().y
* 100;
992 pd
->rtMinMargin
.left
= data
.GetMinMarginTopLeft().x
* 100;
993 pd
->rtMinMargin
.top
= data
.GetMinMarginTopLeft().y
* 100;
994 pd
->rtMinMargin
.right
= data
.GetMinMarginBottomRight().x
* 100;
995 pd
->rtMinMargin
.bottom
= data
.GetMinMarginBottomRight().y
* 100;
997 pd
->rtMargin
.left
= data
.GetMarginTopLeft().x
* 100;
998 pd
->rtMargin
.top
= data
.GetMarginTopLeft().y
* 100;
999 pd
->rtMargin
.right
= data
.GetMarginBottomRight().x
* 100;
1000 pd
->rtMargin
.bottom
= data
.GetMarginBottomRight().y
* 100;
1003 pd
->lpfnPageSetupHook
= NULL
;
1004 pd
->lpfnPagePaintHook
= NULL
;
1005 pd
->hPageSetupTemplate
= NULL
;
1006 pd
->lpPageSetupTemplateName
= NULL
;
1011 DEVMODE *devMode = (DEVMODE*) GlobalLock(pd->hDevMode);
1012 memset(devMode, 0, sizeof(DEVMODE));
1013 devMode->dmSize = sizeof(DEVMODE);
1014 devMode->dmOrientation = m_orientation;
1015 devMode->dmFields = DM_ORIENTATION;
1016 GlobalUnlock(pd->hDevMode);
1022 bool wxWindowsPageSetupDialog::ConvertFromNative( wxPageSetupDialogData
&data
)
1024 PAGESETUPDLG
*pd
= (PAGESETUPDLG
*) m_pageDlg
;
1028 wxWindowsPrintNativeData
*native_data
=
1029 (wxWindowsPrintNativeData
*) data
.GetPrintData().GetNativeData();
1031 // Pass the devmode data back to the wxPrintData structure where it really belongs.
1034 if (native_data
->GetDevMode())
1036 // Make sure we don't leak memory
1037 GlobalFree((HGLOBAL
) native_data
->GetDevMode());
1039 native_data
->SetDevMode( (void*) pd
->hDevMode
);
1040 pd
->hDevMode
= NULL
;
1043 // Isn't this superfluous? It's called again below.
1044 // data.GetPrintData().ConvertFromNative();
1046 // Pass the devnames data back to the wxPrintData structure where it really belongs.
1049 if (native_data
->GetDevNames())
1051 // Make sure we don't leak memory
1052 GlobalFree((HGLOBAL
) native_data
->GetDevNames());
1054 native_data
->SetDevNames((void*) pd
->hDevNames
);
1055 pd
->hDevNames
= NULL
;
1058 data
.GetPrintData().ConvertFromNative();
1060 pd
->Flags
= PSD_MARGINS
|PSD_MINMARGINS
;
1062 data
.SetDefaultMinMargins( ((pd
->Flags
& PSD_DEFAULTMINMARGINS
) == PSD_DEFAULTMINMARGINS
) );
1063 data
.EnableMargins( ((pd
->Flags
& PSD_DISABLEMARGINS
) != PSD_DISABLEMARGINS
) );
1064 data
.EnableOrientation( ((pd
->Flags
& PSD_DISABLEORIENTATION
) != PSD_DISABLEORIENTATION
) );
1065 data
.EnablePaper( ((pd
->Flags
& PSD_DISABLEPAPER
) != PSD_DISABLEPAPER
) );
1066 data
.EnablePrinter( ((pd
->Flags
& PSD_DISABLEPRINTER
) != PSD_DISABLEPRINTER
) );
1067 data
.SetDefaultInfo( ((pd
->Flags
& PSD_RETURNDEFAULT
) == PSD_RETURNDEFAULT
) );
1068 data
.EnableHelp( ((pd
->Flags
& PSD_SHOWHELP
) == PSD_SHOWHELP
) );
1070 // PAGESETUPDLG is in hundreds of a mm
1071 if (data
.GetPrintData().GetOrientation() == wxLANDSCAPE
)
1072 data
.SetPaperSize( wxSize(pd
->ptPaperSize
.y
/ 100, pd
->ptPaperSize
.x
/ 100) );
1074 data
.SetPaperSize( wxSize(pd
->ptPaperSize
.x
/ 100, pd
->ptPaperSize
.y
/ 100) );
1076 data
.SetMinMarginTopLeft( wxPoint(pd
->rtMinMargin
.left
/ 100, pd
->rtMinMargin
.top
/ 100) );
1077 data
.SetMinMarginBottomRight( wxPoint(pd
->rtMinMargin
.right
/ 100, pd
->rtMinMargin
.bottom
/ 100) );
1079 data
.SetMarginTopLeft( wxPoint(pd
->rtMargin
.left
/ 100, pd
->rtMargin
.top
/ 100) );
1080 data
.SetMarginBottomRight( wxPoint(pd
->rtMargin
.right
/ 100, pd
->rtMargin
.bottom
/ 100) );
1086 // wxUSE_PRINTING_ARCHITECTURE