]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/printdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxPrintDialog, wxPageSetupDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "printdlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
31 // Don't use the Windows print dialog if we're in wxUniv mode and using
32 // the PostScript architecture
33 #if wxUSE_PRINTING_ARCHITECTURE && (!defined(__WXUNIVERSAL__) || !wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
39 #include "wx/cmndata.h"
40 #include "wx/printdlg.h"
41 #include "wx/dcprint.h"
46 #include "wx/msw/private.h"
54 //----------------------------------------------------------------------------
55 // wxWindowsPrintNativeData
56 //----------------------------------------------------------------------------
59 static wxString
wxGetPrintDlgError()
61 DWORD err
= CommDlgExtendedError();
62 wxString msg
= wxT("Unknown");
65 case CDERR_FINDRESFAILURE
: msg
= wxT("CDERR_FINDRESFAILURE"); break;
66 case CDERR_INITIALIZATION
: msg
= wxT("CDERR_INITIALIZATION"); break;
67 case CDERR_LOADRESFAILURE
: msg
= wxT("CDERR_LOADRESFAILURE"); break;
68 case CDERR_LOADSTRFAILURE
: msg
= wxT("CDERR_LOADSTRFAILURE"); break;
69 case CDERR_LOCKRESFAILURE
: msg
= wxT("CDERR_LOCKRESFAILURE"); break;
70 case CDERR_MEMALLOCFAILURE
: msg
= wxT("CDERR_MEMALLOCFAILURE"); break;
71 case CDERR_MEMLOCKFAILURE
: msg
= wxT("CDERR_MEMLOCKFAILURE"); break;
72 case CDERR_NOHINSTANCE
: msg
= wxT("CDERR_NOHINSTANCE"); break;
73 case CDERR_NOHOOK
: msg
= wxT("CDERR_NOHOOK"); break;
74 case CDERR_NOTEMPLATE
: msg
= wxT("CDERR_NOTEMPLATE"); break;
75 case CDERR_STRUCTSIZE
: msg
= wxT("CDERR_STRUCTSIZE"); break;
76 case PDERR_RETDEFFAILURE
: msg
= wxT("PDERR_RETDEFFAILURE"); break;
77 case PDERR_PRINTERNOTFOUND
: msg
= wxT("PDERR_PRINTERNOTFOUND"); break;
78 case PDERR_PARSEFAILURE
: msg
= wxT("PDERR_PARSEFAILURE"); break;
79 case PDERR_NODEVICES
: msg
= wxT("PDERR_NODEVICES"); break;
80 case PDERR_NODEFAULTPRN
: msg
= wxT("PDERR_NODEFAULTPRN"); break;
81 case PDERR_LOADDRVFAILURE
: msg
= wxT("PDERR_LOADDRVFAILURE"); break;
82 case PDERR_INITFAILURE
: msg
= wxT("PDERR_INITFAILURE"); break;
83 case PDERR_GETDEVMODEFAIL
: msg
= wxT("PDERR_GETDEVMODEFAIL"); break;
84 case PDERR_DNDMMISMATCH
: msg
= wxT("PDERR_DNDMMISMATCH"); break;
85 case PDERR_DEFAULTDIFFERENT
: msg
= wxT("PDERR_DEFAULTDIFFERENT"); break;
86 case PDERR_CREATEICFAILURE
: msg
= wxT("PDERR_CREATEICFAILURE"); break;
93 static HGLOBAL
wxCreateDevNames(const wxString
& driverName
, const wxString
& printerName
, const wxString
& portName
)
96 // if (!driverName.IsEmpty() && !printerName.IsEmpty() && !portName.IsEmpty())
97 if (driverName
.IsEmpty() && printerName
.IsEmpty() && portName
.IsEmpty())
102 hDev
= GlobalAlloc(GPTR
, 4*sizeof(WORD
)+
103 ( driverName
.Length() + 1 +
104 printerName
.Length() + 1 +
105 portName
.Length()+1 ) * sizeof(wxChar
) );
106 LPDEVNAMES lpDev
= (LPDEVNAMES
)GlobalLock(hDev
);
107 lpDev
->wDriverOffset
= sizeof(WORD
) * 4 / sizeof(wxChar
);
108 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wDriverOffset
, driverName
);
110 lpDev
->wDeviceOffset
= (WORD
)( lpDev
->wDriverOffset
+
111 driverName
.Length() + 1 );
112 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wDeviceOffset
, printerName
);
114 lpDev
->wOutputOffset
= (WORD
)( lpDev
->wDeviceOffset
+
115 printerName
.Length() + 1 );
116 wxStrcpy((wxChar
*)lpDev
+ lpDev
->wOutputOffset
, portName
);
126 IMPLEMENT_CLASS(wxWindowsPrintNativeData
, wxPrintNativeDataBase
)
128 wxWindowsPrintNativeData::wxWindowsPrintNativeData()
130 m_devMode
= (void*) NULL
;
131 m_devNames
= (void*) NULL
;
134 wxWindowsPrintNativeData::~wxWindowsPrintNativeData()
136 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) m_devMode
;
138 GlobalFree(hDevMode
);
139 HGLOBAL hDevNames
= (HGLOBAL
)(DWORD
) m_devNames
;
141 GlobalFree(hDevNames
);
144 bool wxWindowsPrintNativeData::Ok() const
146 return (m_devMode
!= NULL
) ;
149 bool wxWindowsPrintNativeData::TransferTo( wxPrintData
&data
)
151 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) m_devMode
;
152 HGLOBAL hDevNames
= (HGLOBAL
)(DWORD
) m_devNames
;
159 LPDEVMODE devMode
= (LPDEVMODE
)GlobalLock(hDevMode
);
162 if (devMode
->dmFields
& DM_ORIENTATION
)
163 data
.SetOrientation( devMode
->dmOrientation
);
166 if (devMode
->dmFields
& DM_COLLATE
)
168 if (devMode
->dmCollate
== DMCOLLATE_TRUE
)
169 data
.SetCollate( true );
171 data
.SetCollate( false );
174 //// Number of copies
175 if (devMode
->dmFields
& DM_COPIES
)
176 data
.SetNoCopies( devMode
->dmCopies
);
178 if (devMode
->dmFields
& DM_DEFAULTSOURCE
)
179 data
.SetBin( (wxPrintBin
)devMode
->dmDefaultSource
);
182 if (devMode
->dmDeviceName
[0] != 0)
183 data
.SetPrinterName( devMode
->dmDeviceName
);
186 if (devMode
->dmFields
& DM_COLOR
)
188 if (devMode
->dmColor
== DMCOLOR_COLOR
)
189 data
.SetColour( true );
191 data
.SetColour( false );
194 data
.SetColour( true );
198 // We don't know size of user defined paper and some buggy drivers
199 // set both DM_PAPERSIZE and DM_PAPERWIDTH & DM_PAPERLENGTH. Since
200 // dmPaperSize >= DMPAPER_USER wouldn't be in wxWin's database, this
201 // code wouldn't set m_paperSize correctly.
202 if ((devMode
->dmFields
& DM_PAPERSIZE
) && (devMode
->dmPaperSize
< DMPAPER_USER
))
204 if (wxThePrintPaperDatabase
)
206 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperTypeByPlatformId(devMode
->dmPaperSize
);
209 data
.SetPaperId( paper
->GetId() );
210 data
.SetPaperSize( wxSize(paper
->GetWidth() / 10,paper
->GetHeight() / 10) );
214 // Shouldn't really get here
215 wxFAIL_MSG(wxT("Couldn't find paper size in paper database."));
216 data
.SetPaperId( wxPAPER_NONE
);
217 data
.SetPaperSize( wxSize(0,0) );
222 // Shouldn't really get here
223 wxFAIL_MSG(wxT("Paper database wasn't initialized in wxPrintData::ConvertFromNative."));
224 data
.SetPaperId( wxPAPER_NONE
);
225 data
.SetPaperSize( wxSize(0,0) );
228 else if ((devMode
->dmFields
& DM_PAPERWIDTH
) && (devMode
->dmFields
& DM_PAPERLENGTH
))
230 // DEVMODE is in tenths of a milimeter
231 data
.SetPaperId( wxPAPER_NONE
);
232 data
.SetPaperSize( wxSize(devMode
->dmPaperWidth
/ 10, devMode
->dmPaperLength
/ 10) );
236 // Shouldn't really get here
237 wxFAIL_MSG(wxT("Couldn't find paper size from DEVMODE."));
238 data
.SetPaperId( wxPAPER_NONE
);
239 data
.SetPaperSize( wxSize(0,0) );
244 if (devMode
->dmFields
& DM_DUPLEX
)
246 switch (devMode
->dmDuplex
)
248 case DMDUP_HORIZONTAL
: data
.SetDuplex( wxDUPLEX_HORIZONTAL
); break;
249 case DMDUP_VERTICAL
: data
.SetDuplex( wxDUPLEX_VERTICAL
); break;
251 case DMDUP_SIMPLEX
: data
.SetDuplex( wxDUPLEX_SIMPLEX
); break;
255 data
.SetDuplex( wxDUPLEX_SIMPLEX
);
259 if (devMode
->dmFields
& DM_PRINTQUALITY
)
261 switch (devMode
->dmPrintQuality
)
263 case DMRES_MEDIUM
: data
.SetQuality( wxPRINT_QUALITY_MEDIUM
); break;
264 case DMRES_LOW
: data
.SetQuality( wxPRINT_QUALITY_LOW
); break;
265 case DMRES_DRAFT
: data
.SetQuality( wxPRINT_QUALITY_DRAFT
); break;
266 case DMRES_HIGH
: data
.SetQuality( wxPRINT_QUALITY_HIGH
); break;
269 // TODO: if the printer fills in the resolution in DPI, how
270 // will the application know if it's high, low, draft etc.??
271 // wxFAIL_MSG("Warning: DM_PRINTQUALITY was not one of the standard values.");
272 data
.SetQuality( devMode
->dmPrintQuality
);
279 data
.SetQuality( wxPRINT_QUALITY_HIGH
);
281 GlobalUnlock(hDevMode
);
286 LPDEVNAMES lpDevNames
= (LPDEVNAMES
)GlobalLock(hDevNames
);
289 // TODO: Unicode-ification
292 // port is obsolete in WIN32
293 // m_printData.SetPortName((LPSTR)lpDevNames + lpDevNames->wDriverOffset);
295 // Get the printer name
296 wxString printerName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
298 // Not sure if we should check for this mismatch
299 // wxASSERT_MSG( (m_printerName == "" || (devName == m_printerName)), "Printer name obtained from DEVMODE and DEVNAMES were different!");
301 if (printerName
!= wxT(""))
302 data
.SetPrinterName( printerName
);
304 GlobalUnlock(hDevNames
);
311 bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData
&data
)
313 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) m_devMode
;
314 HGLOBAL hDevNames
= (HGLOBAL
)(DWORD
) m_devNames
;
317 // Use PRINTDLG as a way of creating a DEVMODE object
320 // GNU-WIN32 has the wrong size PRINTDLG - can't work out why.
325 memset(&pd
, 0, sizeof(PRINTDLG
));
327 pd
.cbStruct
= sizeof(PRINTDLG
);
329 pd
.lStructSize
= sizeof(PRINTDLG
);
333 pd
.hwndOwner
= (HWND
)NULL
;
334 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
335 pd
.hDevNames
= NULL
; // Ditto
336 //pd.hInstance = (HINSTANCE) wxGetInstance();
338 pd
.Flags
= PD_RETURNDEFAULT
;
341 // Fill out the DEVMODE structure
342 // so we can use it as input in the 'real' PrintDlg
346 GlobalFree(pd
.hDevMode
);
348 GlobalFree(pd
.hDevNames
);
352 #if defined(__WXDEBUG__) && defined(__WIN32__)
353 wxString
str(wxT("Printing error: "));
354 str
+= wxGetPrintDlgError();
360 hDevMode
= pd
.hDevMode
;
361 m_devMode
= (void*)(long) hDevMode
;
364 // We'll create a new DEVNAMEs structure below.
366 GlobalFree(pd
.hDevNames
);
369 // hDevNames = pd->hDevNames;
370 // m_devNames = (void*)(long) hDevNames;
371 // pd->hDevnames = NULL;
378 LPDEVMODE devMode
= (LPDEVMODE
) GlobalLock(hDevMode
);
381 devMode
->dmOrientation
= (short)data
.GetOrientation();
384 devMode
->dmCollate
= (data
.GetCollate() ? DMCOLLATE_TRUE
: DMCOLLATE_FALSE
);
385 devMode
->dmFields
|= DM_COLLATE
;
387 //// Number of copies
388 devMode
->dmCopies
= (short)data
.GetNoCopies();
389 devMode
->dmFields
|= DM_COPIES
;
392 wxString name
= data
.GetPrinterName();
395 //int len = wxMin(31, m_printerName.Len());
396 wxStrncpy((wxChar
*)devMode
->dmDeviceName
,name
.c_str(),31);
397 devMode
->dmDeviceName
[31] = wxT('\0');
401 if (data
.GetColour())
402 devMode
->dmColor
= DMCOLOR_COLOR
;
404 devMode
->dmColor
= DMCOLOR_MONOCHROME
;
405 devMode
->dmFields
|= DM_COLOR
;
408 if (data
.GetPaperId() == wxPAPER_NONE
)
410 // DEVMODE is in tenths of a milimeter
411 devMode
->dmPaperWidth
= (short)(data
.GetPaperSize().x
* 10);
412 devMode
->dmPaperLength
= (short)(data
.GetPaperSize().y
* 10);
413 devMode
->dmPaperSize
= DMPAPER_USER
;
414 devMode
->dmFields
|= DM_PAPERWIDTH
;
415 devMode
->dmFields
|= DM_PAPERLENGTH
;
419 if (wxThePrintPaperDatabase
)
421 wxPrintPaperType
* paper
= wxThePrintPaperDatabase
->FindPaperType( data
.GetPaperId() );
424 devMode
->dmPaperSize
= (short)paper
->GetPlatformId();
425 devMode
->dmFields
|= DM_PAPERSIZE
;
432 switch (data
.GetDuplex())
434 case wxDUPLEX_HORIZONTAL
:
435 duplex
= DMDUP_HORIZONTAL
;
437 case wxDUPLEX_VERTICAL
:
438 duplex
= DMDUP_VERTICAL
;
441 // in fact case wxDUPLEX_SIMPLEX:
442 duplex
= DMDUP_SIMPLEX
;
445 devMode
->dmDuplex
= duplex
;
446 devMode
->dmFields
|= DM_DUPLEX
;
451 switch (data
.GetQuality())
453 case wxPRINT_QUALITY_MEDIUM
:
454 quality
= DMRES_MEDIUM
;
456 case wxPRINT_QUALITY_LOW
:
459 case wxPRINT_QUALITY_DRAFT
:
460 quality
= DMRES_DRAFT
;
462 case wxPRINT_QUALITY_HIGH
:
463 quality
= DMRES_HIGH
;
466 quality
= (short)data
.GetQuality();
469 devMode
->dmPrintQuality
= quality
;
470 devMode
->dmFields
|= DM_PRINTQUALITY
;
472 if (data
.GetBin() != wxPRINTBIN_DEFAULT
)
474 switch (data
.GetBin())
476 case wxPRINTBIN_ONLYONE
: devMode
->dmDefaultSource
= DMBIN_ONLYONE
; break;
477 case wxPRINTBIN_LOWER
: devMode
->dmDefaultSource
= DMBIN_LOWER
; break;
478 case wxPRINTBIN_MIDDLE
: devMode
->dmDefaultSource
= DMBIN_MIDDLE
; break;
479 case wxPRINTBIN_MANUAL
: devMode
->dmDefaultSource
= DMBIN_MANUAL
; break;
480 case wxPRINTBIN_ENVELOPE
: devMode
->dmDefaultSource
= DMBIN_ENVELOPE
; break;
481 case wxPRINTBIN_ENVMANUAL
: devMode
->dmDefaultSource
= DMBIN_ENVMANUAL
; break;
482 case wxPRINTBIN_AUTO
: devMode
->dmDefaultSource
= DMBIN_AUTO
; break;
483 case wxPRINTBIN_TRACTOR
: devMode
->dmDefaultSource
= DMBIN_TRACTOR
; break;
484 case wxPRINTBIN_SMALLFMT
: devMode
->dmDefaultSource
= DMBIN_SMALLFMT
; break;
485 case wxPRINTBIN_LARGEFMT
: devMode
->dmDefaultSource
= DMBIN_LARGEFMT
; break;
486 case wxPRINTBIN_LARGECAPACITY
: devMode
->dmDefaultSource
= DMBIN_LARGECAPACITY
; break;
487 case wxPRINTBIN_CASSETTE
: devMode
->dmDefaultSource
= DMBIN_CASSETTE
; break;
488 case wxPRINTBIN_FORMSOURCE
: devMode
->dmDefaultSource
= DMBIN_FORMSOURCE
; break;
491 devMode
->dmDefaultSource
= (short)(DMBIN_USER
+ data
.GetBin() - wxPRINTBIN_USER
);
495 devMode
->dmFields
|= DM_DEFAULTSOURCE
;
498 GlobalUnlock(hDevMode
);
503 GlobalFree(hDevNames
);
506 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
507 m_devNames
= (void*) (long) wxCreateDevNames(wxT(""), data
.GetPrinterName(), wxT(""));
512 // ---------------------------------------------------------------------------
514 // ---------------------------------------------------------------------------
516 IMPLEMENT_CLASS(wxWindowsPrintDialog
, wxPrintDialogBase
)
518 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintDialogData
* data
)
523 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow
*p
, wxPrintData
* data
)
525 wxPrintDialogData data2
;
532 bool wxWindowsPrintDialog::Create(wxWindow
*p
, wxPrintDialogData
* data
)
539 m_printDialogData
= *data
;
541 m_printDialogData
.SetOwnerWindow(p
);
546 wxWindowsPrintDialog::~wxWindowsPrintDialog()
548 if (m_destroyDC
&& m_printerDC
)
552 int wxWindowsPrintDialog::ShowModal()
554 m_printDialogData
.ConvertToNative();
556 PRINTDLG
* p
= (PRINTDLG
*)m_printDialogData
.GetNativeData() ;
558 p
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
559 else if (wxTheApp
->GetTopWindow())
560 p
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
564 bool ret
= (PrintDlg( p
) != 0);
568 if ( ret
!= false && ((PRINTDLG
*)m_printDialogData
.GetNativeData())->hDC
)
570 wxPrinterDC
*pdc
= new wxPrinterDC((WXHDC
) ((PRINTDLG
*)m_printDialogData
.GetNativeData())->hDC
);
572 m_printDialogData
.ConvertFromNative();
581 wxDC
*wxWindowsPrintDialog::GetPrintDC()
592 // ---------------------------------------------------------------------------
594 // ---------------------------------------------------------------------------
596 IMPLEMENT_CLASS(wxPageSetupDialog
, wxDialog
)
598 wxPageSetupDialog::wxPageSetupDialog()
600 m_dialogParent
= NULL
;
603 wxPageSetupDialog::wxPageSetupDialog(wxWindow
*p
, wxPageSetupData
*data
)
608 bool wxPageSetupDialog::Create(wxWindow
*p
, wxPageSetupData
*data
)
613 m_pageSetupData
= (*data
);
615 #if defined(__WIN95__)
616 m_pageSetupData
.SetOwnerWindow(p
);
621 wxPageSetupDialog::~wxPageSetupDialog()
625 int wxPageSetupDialog::ShowModal()
628 m_pageSetupData
.ConvertToNative();
629 PAGESETUPDLG
*p
= (PAGESETUPDLG
*)m_pageSetupData
.GetNativeData();
631 p
->hwndOwner
= (HWND
) m_dialogParent
->GetHWND();
632 else if (wxTheApp
->GetTopWindow())
633 p
->hwndOwner
= (HWND
) wxTheApp
->GetTopWindow()->GetHWND();
636 BOOL retVal
= PageSetupDlg( p
) ;
640 m_pageSetupData
.ConvertFromNative();
646 wxGenericPageSetupDialog
*genericPageSetupDialog
= new wxGenericPageSetupDialog(GetParent(), & m_pageSetupData
);
647 int ret
= genericPageSetupDialog
->ShowModal();
648 m_pageSetupData
= genericPageSetupDialog
->GetPageSetupData();
649 genericPageSetupDialog
->Close(true);
655 // wxUSE_PRINTING_ARCHITECTURE