]> git.saurik.com Git - wxWidgets.git/blob - src/msw/printdlg.cpp
6a5a7e4fd552474b95725551c738538fe2569109
[wxWidgets.git] / src / msw / printdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: printdlg.cpp
3 // Purpose: wxPrintDialog, wxPageSetupDialog
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "printdlg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
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)
34
35 #ifndef WX_PRECOMP
36 #include "wx/app.h"
37 #endif
38
39 #include "wx/cmndata.h"
40 #include "wx/printdlg.h"
41 #include "wx/dcprint.h"
42 #include "wx/paper.h"
43
44 #include <stdlib.h>
45
46 #include "wx/msw/private.h"
47
48 #include <commdlg.h>
49
50 #ifndef __WIN32__
51 #include <print.h>
52 #endif
53
54 //----------------------------------------------------------------------------
55 // wxWindowsPrintNativeData
56 //----------------------------------------------------------------------------
57
58 #ifdef __WXDEBUG__
59 static wxString wxGetPrintDlgError()
60 {
61 DWORD err = CommDlgExtendedError();
62 wxString msg = wxT("Unknown");
63 switch (err)
64 {
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;
87 default: break;
88 }
89 return msg;
90 }
91 #endif
92
93 static HGLOBAL wxCreateDevNames(const wxString& driverName, const wxString& printerName, const wxString& portName)
94 {
95 HGLOBAL hDev = NULL;
96 // if (!driverName.IsEmpty() && !printerName.IsEmpty() && !portName.IsEmpty())
97 if (driverName.IsEmpty() && printerName.IsEmpty() && portName.IsEmpty())
98 {
99 }
100 else
101 {
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);
109
110 lpDev->wDeviceOffset = (WORD)( lpDev->wDriverOffset +
111 driverName.Length() + 1 );
112 wxStrcpy((wxChar*)lpDev + lpDev->wDeviceOffset, printerName);
113
114 lpDev->wOutputOffset = (WORD)( lpDev->wDeviceOffset +
115 printerName.Length() + 1 );
116 wxStrcpy((wxChar*)lpDev + lpDev->wOutputOffset, portName);
117
118 lpDev->wDefault = 0;
119
120 GlobalUnlock(hDev);
121 }
122
123 return hDev;
124 }
125
126 IMPLEMENT_CLASS(wxWindowsPrintNativeData, wxPrintNativeDataBase)
127
128 wxWindowsPrintNativeData::wxWindowsPrintNativeData()
129 {
130 m_devMode = (void*) NULL;
131 m_devNames = (void*) NULL;
132 }
133
134 wxWindowsPrintNativeData::~wxWindowsPrintNativeData()
135 {
136 HGLOBAL hDevMode = (HGLOBAL)(DWORD) m_devMode;
137 if ( hDevMode )
138 GlobalFree(hDevMode);
139 HGLOBAL hDevNames = (HGLOBAL)(DWORD) m_devNames;
140 if ( hDevNames )
141 GlobalFree(hDevNames);
142 }
143
144 bool wxWindowsPrintNativeData::Ok() const
145 {
146 return (m_devMode != NULL) ;
147 }
148
149 bool wxWindowsPrintNativeData::TransferTo( wxPrintData &data )
150 {
151 HGLOBAL hDevMode = (HGLOBAL)(DWORD) m_devMode;
152 HGLOBAL hDevNames = (HGLOBAL)(DWORD) m_devNames;
153
154 if (!hDevMode)
155 return false;
156
157 if ( hDevMode )
158 {
159 LPDEVMODE devMode = (LPDEVMODE)GlobalLock(hDevMode);
160
161 //// Orientation
162 if (devMode->dmFields & DM_ORIENTATION)
163 data.SetOrientation( devMode->dmOrientation );
164
165 //// Collation
166 if (devMode->dmFields & DM_COLLATE)
167 {
168 if (devMode->dmCollate == DMCOLLATE_TRUE)
169 data.SetCollate( true );
170 else
171 data.SetCollate( false );
172 }
173
174 //// Number of copies
175 if (devMode->dmFields & DM_COPIES)
176 data.SetNoCopies( devMode->dmCopies );
177
178 if (devMode->dmFields & DM_DEFAULTSOURCE)
179 data.SetBin( (wxPrintBin)devMode->dmDefaultSource );
180
181 //// Printer name
182 if (devMode->dmDeviceName[0] != 0)
183 data.SetPrinterName( devMode->dmDeviceName );
184
185 //// Colour
186 if (devMode->dmFields & DM_COLOR)
187 {
188 if (devMode->dmColor == DMCOLOR_COLOR)
189 data.SetColour( true );
190 else
191 data.SetColour( false );
192 }
193 else
194 data.SetColour( true );
195
196 //// Paper size
197
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))
203 {
204 if (wxThePrintPaperDatabase)
205 {
206 wxPrintPaperType* paper = wxThePrintPaperDatabase->FindPaperTypeByPlatformId(devMode->dmPaperSize);
207 if (paper)
208 {
209 data.SetPaperId( paper->GetId() );
210 data.SetPaperSize( wxSize(paper->GetWidth() / 10,paper->GetHeight() / 10) );
211 }
212 else
213 {
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) );
218 }
219 }
220 else
221 {
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) );
226 }
227 }
228 else if ((devMode->dmFields & DM_PAPERWIDTH) && (devMode->dmFields & DM_PAPERLENGTH))
229 {
230 // DEVMODE is in tenths of a milimeter
231 data.SetPaperId( wxPAPER_NONE );
232 data.SetPaperSize( wxSize(devMode->dmPaperWidth / 10, devMode->dmPaperLength / 10) );
233 }
234 else
235 {
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) );
240 }
241
242 //// Duplex
243
244 if (devMode->dmFields & DM_DUPLEX)
245 {
246 switch (devMode->dmDuplex)
247 {
248 case DMDUP_HORIZONTAL: data.SetDuplex( wxDUPLEX_HORIZONTAL ); break;
249 case DMDUP_VERTICAL: data.SetDuplex( wxDUPLEX_VERTICAL ); break;
250 default:
251 case DMDUP_SIMPLEX: data.SetDuplex( wxDUPLEX_SIMPLEX ); break;
252 }
253 }
254 else
255 data.SetDuplex( wxDUPLEX_SIMPLEX );
256
257 //// Quality
258
259 if (devMode->dmFields & DM_PRINTQUALITY)
260 {
261 switch (devMode->dmPrintQuality)
262 {
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;
267 default:
268 {
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 );
273 break;
274
275 }
276 }
277 }
278 else
279 data.SetQuality( wxPRINT_QUALITY_HIGH );
280
281 GlobalUnlock(hDevMode);
282 }
283
284 if (hDevNames)
285 {
286 LPDEVNAMES lpDevNames = (LPDEVNAMES)GlobalLock(hDevNames);
287 if (lpDevNames)
288 {
289 // TODO: Unicode-ification
290
291 // Get the port name
292 // port is obsolete in WIN32
293 // m_printData.SetPortName((LPSTR)lpDevNames + lpDevNames->wDriverOffset);
294
295 // Get the printer name
296 wxString printerName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset;
297
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!");
300
301 if (printerName != wxT(""))
302 data.SetPrinterName( printerName );
303
304 GlobalUnlock(hDevNames);
305 }
306 }
307
308 return true;
309 }
310
311 bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData &data )
312 {
313 HGLOBAL hDevMode = (HGLOBAL)(DWORD) m_devMode;
314 HGLOBAL hDevNames = (HGLOBAL)(DWORD) m_devNames;
315 if (!hDevMode)
316 {
317 // Use PRINTDLG as a way of creating a DEVMODE object
318 PRINTDLG pd;
319
320 // GNU-WIN32 has the wrong size PRINTDLG - can't work out why.
321 #ifdef __GNUWIN32__
322 memset(&pd, 0, 66);
323 pd.lStructSize = 66;
324 #else
325 memset(&pd, 0, sizeof(PRINTDLG));
326 #ifdef __WXWINCE__
327 pd.cbStruct = sizeof(PRINTDLG);
328 #else
329 pd.lStructSize = sizeof(PRINTDLG);
330 #endif
331 #endif
332
333 pd.hwndOwner = (HWND)NULL;
334 pd.hDevMode = NULL; // Will be created by PrintDlg
335 pd.hDevNames = NULL; // Ditto
336 //pd.hInstance = (HINSTANCE) wxGetInstance();
337
338 pd.Flags = PD_RETURNDEFAULT;
339 pd.nCopies = 1;
340
341 // Fill out the DEVMODE structure
342 // so we can use it as input in the 'real' PrintDlg
343 if (!PrintDlg(&pd))
344 {
345 if ( pd.hDevMode )
346 GlobalFree(pd.hDevMode);
347 if ( pd.hDevNames )
348 GlobalFree(pd.hDevNames);
349 pd.hDevMode = NULL;
350 pd.hDevNames = NULL;
351
352 #if defined(__WXDEBUG__) && defined(__WIN32__)
353 wxString str(wxT("Printing error: "));
354 str += wxGetPrintDlgError();
355 wxLogDebug(str);
356 #endif
357 }
358 else
359 {
360 hDevMode = pd.hDevMode;
361 m_devMode = (void*)(long) hDevMode;
362 pd.hDevMode = NULL;
363
364 // We'll create a new DEVNAMEs structure below.
365 if ( pd.hDevNames )
366 GlobalFree(pd.hDevNames);
367 pd.hDevNames = NULL;
368
369 // hDevNames = pd->hDevNames;
370 // m_devNames = (void*)(long) hDevNames;
371 // pd->hDevnames = NULL;
372
373 }
374 }
375
376 if ( hDevMode )
377 {
378 LPDEVMODE devMode = (LPDEVMODE) GlobalLock(hDevMode);
379
380 //// Orientation
381 devMode->dmOrientation = (short)data.GetOrientation();
382
383 //// Collation
384 devMode->dmCollate = (data.GetCollate() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE);
385 devMode->dmFields |= DM_COLLATE;
386
387 //// Number of copies
388 devMode->dmCopies = (short)data.GetNoCopies();
389 devMode->dmFields |= DM_COPIES;
390
391 //// Printer name
392 wxString name = data.GetPrinterName();
393 if (name != wxT(""))
394 {
395 //int len = wxMin(31, m_printerName.Len());
396 wxStrncpy((wxChar*)devMode->dmDeviceName,name.c_str(),31);
397 devMode->dmDeviceName[31] = wxT('\0');
398 }
399
400 //// Colour
401 if (data.GetColour())
402 devMode->dmColor = DMCOLOR_COLOR;
403 else
404 devMode->dmColor = DMCOLOR_MONOCHROME;
405 devMode->dmFields |= DM_COLOR;
406
407 //// Paper size
408 if (data.GetPaperId() == wxPAPER_NONE)
409 {
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;
416 }
417 else
418 {
419 if (wxThePrintPaperDatabase)
420 {
421 wxPrintPaperType* paper = wxThePrintPaperDatabase->FindPaperType( data.GetPaperId() );
422 if (paper)
423 {
424 devMode->dmPaperSize = (short)paper->GetPlatformId();
425 devMode->dmFields |= DM_PAPERSIZE;
426 }
427 }
428 }
429
430 //// Duplex
431 short duplex;
432 switch (data.GetDuplex())
433 {
434 case wxDUPLEX_HORIZONTAL:
435 duplex = DMDUP_HORIZONTAL;
436 break;
437 case wxDUPLEX_VERTICAL:
438 duplex = DMDUP_VERTICAL;
439 break;
440 default:
441 // in fact case wxDUPLEX_SIMPLEX:
442 duplex = DMDUP_SIMPLEX;
443 break;
444 }
445 devMode->dmDuplex = duplex;
446 devMode->dmFields |= DM_DUPLEX;
447
448 //// Quality
449
450 short quality;
451 switch (data.GetQuality())
452 {
453 case wxPRINT_QUALITY_MEDIUM:
454 quality = DMRES_MEDIUM;
455 break;
456 case wxPRINT_QUALITY_LOW:
457 quality = DMRES_LOW;
458 break;
459 case wxPRINT_QUALITY_DRAFT:
460 quality = DMRES_DRAFT;
461 break;
462 case wxPRINT_QUALITY_HIGH:
463 quality = DMRES_HIGH;
464 break;
465 default:
466 quality = (short)data.GetQuality();
467 break;
468 }
469 devMode->dmPrintQuality = quality;
470 devMode->dmFields |= DM_PRINTQUALITY;
471
472 if (data.GetBin() != wxPRINTBIN_DEFAULT)
473 {
474 switch (data.GetBin())
475 {
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;
489
490 default:
491 devMode->dmDefaultSource = (short)(DMBIN_USER + data.GetBin() - wxPRINTBIN_USER);
492 break;
493 }
494
495 devMode->dmFields |= DM_DEFAULTSOURCE;
496 }
497
498 GlobalUnlock(hDevMode);
499 }
500
501 if ( hDevNames )
502 {
503 GlobalFree(hDevNames);
504 }
505
506 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
507 m_devNames = (void*) (long) wxCreateDevNames(wxT(""), data.GetPrinterName(), wxT(""));
508
509 return true;
510 }
511
512 // ---------------------------------------------------------------------------
513 // wxPrintDialog
514 // ---------------------------------------------------------------------------
515
516 IMPLEMENT_CLASS(wxWindowsPrintDialog, wxPrintDialogBase)
517
518 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintDialogData* data)
519 {
520 Create(p, data);
521 }
522
523 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintData* data)
524 {
525 wxPrintDialogData data2;
526 if ( data )
527 data2 = *data;
528
529 Create(p, &data2);
530 }
531
532 bool wxWindowsPrintDialog::Create(wxWindow *p, wxPrintDialogData* data)
533 {
534 m_dialogParent = p;
535 m_printerDC = NULL;
536 m_destroyDC = true;
537
538 // MSW handle
539 m_printDlg = NULL;
540
541 if ( data )
542 m_printDialogData = *data;
543
544 return true;
545 }
546
547 wxWindowsPrintDialog::~wxWindowsPrintDialog()
548 {
549 PRINTDLG *pd = (PRINTDLG *) m_printDlg;
550 if (pd && pd->hDevMode)
551 GlobalFree(pd->hDevMode);
552 if ( pd )
553 delete pd;
554
555 if (m_destroyDC && m_printerDC)
556 delete m_printerDC;
557 }
558
559 int wxWindowsPrintDialog::ShowModal()
560 {
561 ConvertToNative( m_printDialogData );
562
563 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
564
565 if (m_dialogParent)
566 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
567 else if (wxTheApp->GetTopWindow())
568 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
569 else
570 pd->hwndOwner = 0;
571
572 bool ret = (PrintDlg( pd ) != 0);
573
574 pd->hwndOwner = 0;
575
576 if ( ret != false && (pd->hDC) )
577 {
578 wxPrinterDC *pdc = new wxPrinterDC( (WXHDC) pd->hDC );
579 m_printerDC = pdc;
580 ConvertFromNative( m_printDialogData );
581 return wxID_OK;
582 }
583 else
584 {
585 return wxID_CANCEL;
586 }
587 }
588
589 wxDC *wxWindowsPrintDialog::GetPrintDC()
590 {
591 if (m_printerDC)
592 {
593 m_destroyDC = false;
594 return m_printerDC;
595 }
596 else
597 return (wxDC*) NULL;
598 }
599
600 bool wxWindowsPrintDialog::ConvertToNative( wxPrintDialogData &data )
601 {
602 wxWindowsPrintNativeData *native_data =
603 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
604 data.GetPrintData().ConvertToNative();
605
606 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
607
608 // Shouldn't have been defined anywhere
609 if (pd)
610 return false;
611
612 pd = new PRINTDLG;
613 memset( pd, 0, sizeof(PRINTDLG) );
614 m_printDlg = (void*) pd;
615
616 // GNU-WIN32 has the wrong size PRINTDLG - can't work out why.
617 #ifdef __GNUWIN32__
618 pd->lStructSize = 66;
619 #else
620 pd->lStructSize = sizeof(PRINTDLG);
621 #endif
622 pd->hwndOwner = (HWND)NULL;
623 pd->hDevMode = NULL; // Will be created by PrintDlg
624 pd->hDevNames = NULL; // Ditto
625
626 pd->Flags = PD_RETURNDEFAULT;
627 pd->nCopies = 1;
628
629 // Pass the devmode data to the PRINTDLG structure, since it'll
630 // be needed when PrintDlg is called.
631 if (pd->hDevMode)
632 GlobalFree(pd->hDevMode);
633
634 // Pass the devnames data to the PRINTDLG structure, since it'll
635 // be needed when PrintDlg is called.
636 if (pd->hDevNames)
637 GlobalFree(pd->hDevNames);
638
639 pd->hDevMode = (HGLOBAL)(DWORD) native_data->GetDevMode();
640 native_data->SetDevMode( (void*) NULL);
641
642 // Shouldn't assert; we should be able to test Ok-ness at a higher level
643 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
644
645 pd->hDevNames = (HGLOBAL)(DWORD) native_data->GetDevNames();
646 native_data->SetDevNames( (void*) NULL);
647
648
649 pd->hDC = (HDC) NULL;
650 pd->nFromPage = (WORD)data.GetFromPage();
651 pd->nToPage = (WORD)data.GetToPage();
652 pd->nMinPage = (WORD)data.GetMinPage();
653 pd->nMaxPage = (WORD)data.GetMaxPage();
654 pd->nCopies = (WORD)data.GetNoCopies();
655
656 pd->Flags = PD_RETURNDC;
657
658 #ifdef __GNUWIN32__
659 pd->lStructSize = 66;
660 #else
661 pd->lStructSize = sizeof( PRINTDLG );
662 #endif
663
664 pd->hwndOwner=(HWND)NULL;
665 // pd->hDevNames=(HANDLE)NULL;
666 pd->hInstance=(HINSTANCE)NULL;
667 pd->lCustData = (LPARAM) NULL;
668 pd->lpfnPrintHook = NULL;
669 pd->lpfnSetupHook = NULL;
670 pd->lpPrintTemplateName = NULL;
671 pd->lpSetupTemplateName = NULL;
672 pd->hPrintTemplate = (HGLOBAL) NULL;
673 pd->hSetupTemplate = (HGLOBAL) NULL;
674
675 if ( data.GetAllPages() )
676 pd->Flags |= PD_ALLPAGES;
677 if ( data.GetSelection() )
678 pd->Flags |= PD_SELECTION;
679 if ( data.GetCollate() )
680 pd->Flags |= PD_COLLATE;
681 if ( data.GetPrintToFile() )
682 pd->Flags |= PD_PRINTTOFILE;
683 if ( !data.GetEnablePrintToFile() )
684 pd->Flags |= PD_DISABLEPRINTTOFILE;
685 if ( !data.GetEnableSelection() )
686 pd->Flags |= PD_NOSELECTION;
687 if ( !data.GetEnablePageNumbers() )
688 pd->Flags |= PD_NOPAGENUMS;
689 else if ( (!data.GetAllPages()) && (!data.GetSelection()) && (data.GetFromPage() != 0) && (data.GetToPage() != 0))
690 pd->Flags |= PD_PAGENUMS;
691 if ( data.GetEnableHelp() )
692 pd->Flags |= PD_SHOWHELP;
693 #if WXWIN_COMPATIBILITY_2_4
694 if ( data.GetSetupDialog() )
695 pd->Flags |= PD_PRINTSETUP;
696 #endif
697
698 return true;
699 }
700
701 bool wxWindowsPrintDialog::ConvertFromNative( wxPrintDialogData &data )
702 {
703 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
704 if ( pd == NULL )
705 return false;
706
707 wxWindowsPrintNativeData *native_data =
708 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
709
710 // Pass the devmode data back to the wxPrintData structure where it really belongs.
711 if (pd->hDevMode)
712 {
713 if (native_data->GetDevMode())
714 {
715 // Make sure we don't leak memory
716 GlobalFree( (HGLOBAL)(DWORD) native_data->GetDevMode() );
717 }
718 native_data->SetDevMode( (void*)(long) pd->hDevMode );
719 pd->hDevMode = NULL;
720 }
721
722 // Pass the devnames data back to the wxPrintData structure where it really belongs.
723 if (pd->hDevNames)
724 {
725 if (native_data->GetDevNames())
726 {
727 // Make sure we don't leak memory
728 GlobalFree((HGLOBAL)(DWORD) native_data->GetDevNames());
729 }
730 native_data->SetDevNames((void*)(long) pd->hDevNames);
731 pd->hDevNames = NULL;
732 }
733
734 // Now convert the DEVMODE object, passed down from the PRINTDLG object,
735 // into wxWidgets form.
736 native_data->TransferTo( data.GetPrintData() );
737
738 data.SetFromPage( pd->nFromPage );
739 data.SetToPage( pd->nToPage );
740 data.SetMinPage( pd->nMinPage );
741 data.SetMaxPage( pd->nMaxPage );
742 data.SetNoCopies( pd->nCopies );
743
744 data.SetAllPages( (((pd->Flags & PD_PAGENUMS) != PD_PAGENUMS) && ((pd->Flags & PD_SELECTION) != PD_SELECTION)) );
745 data.SetSelection( ((pd->Flags & PD_SELECTION) == PD_SELECTION) );
746 data.SetCollate( ((pd->Flags & PD_COLLATE) == PD_COLLATE) );
747 data.SetPrintToFile( ((pd->Flags & PD_PRINTTOFILE) == PD_PRINTTOFILE) );
748 data.EnablePrintToFile( ((pd->Flags & PD_DISABLEPRINTTOFILE) != PD_DISABLEPRINTTOFILE) );
749 data.EnableSelection( ((pd->Flags & PD_NOSELECTION) != PD_NOSELECTION) );
750 data.EnablePageNumbers( ((pd->Flags & PD_NOPAGENUMS) != PD_NOPAGENUMS) );
751 data.EnableHelp( ((pd->Flags & PD_SHOWHELP) == PD_SHOWHELP) );
752 #if WXWIN_COMPATIBILITY_2_4
753 data.SetSetupDialog( ((pd->Flags & PD_PRINTSETUP) == PD_PRINTSETUP) );
754 #endif
755 return true;
756 }
757
758 // ---------------------------------------------------------------------------
759 // wxPageSetupDialog
760 // ---------------------------------------------------------------------------
761
762 IMPLEMENT_CLASS(wxPageSetupDialog, wxDialog)
763
764 wxPageSetupDialog::wxPageSetupDialog()
765 {
766 m_dialogParent = NULL;
767 m_pageDlg = NULL;
768 }
769
770 wxPageSetupDialog::wxPageSetupDialog(wxWindow *p, wxPageSetupDialogData *data)
771 {
772 Create(p, data);
773 }
774
775 bool wxPageSetupDialog::Create(wxWindow *p, wxPageSetupDialogData *data)
776 {
777 m_dialogParent = p;
778 m_pageDlg = NULL;
779
780 if (data)
781 m_pageSetupData = (*data);
782
783 return true;
784 }
785
786 wxPageSetupDialog::~wxPageSetupDialog()
787 {
788 PAGESETUPDLG *pd = (PAGESETUPDLG *)m_pageDlg;
789 if ( pd && pd->hDevMode )
790 GlobalFree(pd->hDevMode);
791 if ( pd && pd->hDevNames )
792 GlobalFree(pd->hDevNames);
793 if ( pd )
794 delete pd;
795 }
796
797 int wxPageSetupDialog::ShowModal()
798 {
799 ConvertToNative( m_pageSetupData );
800
801 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
802 if (m_dialogParent)
803 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
804 else if (wxTheApp->GetTopWindow())
805 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
806 else
807 pd->hwndOwner = 0;
808 BOOL retVal = PageSetupDlg( pd ) ;
809 pd->hwndOwner = 0;
810 if (retVal)
811 {
812 ConvertFromNative( m_pageSetupData );
813 return wxID_OK;
814 }
815 else
816 return wxID_CANCEL;
817 }
818
819 bool wxPageSetupDialog::ConvertToNative( wxPageSetupDialogData &data )
820 {
821 wxWindowsPrintNativeData *native_data =
822 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
823 data.GetPrintData().ConvertToNative();
824
825 PAGESETUPDLG *pd = (PAGESETUPDLG*) m_pageDlg;
826
827 // Shouldn't have been defined anywhere
828 if (pd)
829 return false;
830
831 pd = new PAGESETUPDLG;
832 pd->hDevMode = NULL;
833 pd->hDevNames = NULL;
834 m_pageDlg = (void *)pd;
835
836 // Pass the devmode data (created in m_printData.ConvertToNative)
837 // to the PRINTDLG structure, since it'll
838 // be needed when PrintDlg is called.
839
840 if (pd->hDevMode)
841 {
842 GlobalFree(pd->hDevMode);
843 pd->hDevMode = NULL;
844 }
845 pd->hDevMode = (HGLOBAL) native_data->GetDevMode();
846 native_data->SetDevMode( (void*) NULL );
847
848 // Shouldn't assert; we should be able to test Ok-ness at a higher level
849 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
850
851 // Pass the devnames data (created in m_printData.ConvertToNative)
852 // to the PRINTDLG structure, since it'll
853 // be needed when PrintDlg is called.
854
855 if (pd->hDevNames)
856 {
857 GlobalFree(pd->hDevNames);
858 pd->hDevNames = NULL;
859 }
860 pd->hDevNames = (HGLOBAL) native_data->GetDevNames();
861 native_data->SetDevNames((void*) NULL);
862
863 // pd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
864
865 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
866
867 if ( data.GetDefaultMinMargins() )
868 pd->Flags |= PSD_DEFAULTMINMARGINS;
869 if ( !data.GetEnableMargins() )
870 pd->Flags |= PSD_DISABLEMARGINS;
871 if ( !data.GetEnableOrientation() )
872 pd->Flags |= PSD_DISABLEORIENTATION;
873 if ( !data.GetEnablePaper() )
874 pd->Flags |= PSD_DISABLEPAPER;
875 if ( !data.GetEnablePrinter() )
876 pd->Flags |= PSD_DISABLEPRINTER;
877 if ( data.GetDefaultInfo() )
878 pd->Flags |= PSD_RETURNDEFAULT;
879 if ( data.GetEnableHelp() )
880 pd->Flags |= PSD_SHOWHELP;
881
882 // We want the units to be in hundredths of a millimetre
883 pd->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
884
885 pd->lStructSize = sizeof( PAGESETUPDLG );
886 pd->hwndOwner=(HWND)NULL;
887 // pd->hDevNames=(HWND)NULL;
888 pd->hInstance=(HINSTANCE)NULL;
889 // PAGESETUPDLG is in hundreds of a mm
890 pd->ptPaperSize.x = data.GetPaperSize().x * 100;
891 pd->ptPaperSize.y = data.GetPaperSize().y * 100;
892
893 pd->rtMinMargin.left = data.GetMinMarginTopLeft().x * 100;
894 pd->rtMinMargin.top = data.GetMinMarginTopLeft().y * 100;
895 pd->rtMinMargin.right = data.GetMinMarginBottomRight().x * 100;
896 pd->rtMinMargin.bottom = data.GetMinMarginBottomRight().y * 100;
897
898 pd->rtMargin.left = data.GetMarginTopLeft().x * 100;
899 pd->rtMargin.top = data.GetMarginTopLeft().y * 100;
900 pd->rtMargin.right = data.GetMarginBottomRight().x * 100;
901 pd->rtMargin.bottom = data.GetMarginBottomRight().y * 100;
902
903 pd->lCustData = 0;
904 pd->lpfnPageSetupHook = NULL;
905 pd->lpfnPagePaintHook = NULL;
906 pd->hPageSetupTemplate = NULL;
907 pd->lpPageSetupTemplateName = NULL;
908
909 /*
910 if ( pd->hDevMode )
911 {
912 DEVMODE *devMode = (DEVMODE*) GlobalLock(pd->hDevMode);
913 memset(devMode, 0, sizeof(DEVMODE));
914 devMode->dmSize = sizeof(DEVMODE);
915 devMode->dmOrientation = m_orientation;
916 devMode->dmFields = DM_ORIENTATION;
917 GlobalUnlock(pd->hDevMode);
918 }
919 */
920 return true;
921 }
922
923 bool wxPageSetupDialog::ConvertFromNative( wxPageSetupDialogData &data )
924 {
925 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
926 if ( !pd )
927 return false;
928
929 wxWindowsPrintNativeData *native_data =
930 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
931
932 // Pass the devmode data back to the wxPrintData structure where it really belongs.
933 if (pd->hDevMode)
934 {
935 if (native_data->GetDevMode())
936 {
937 // Make sure we don't leak memory
938 GlobalFree((HGLOBAL) native_data->GetDevMode());
939 }
940 native_data->SetDevMode( (void*) pd->hDevMode );
941 pd->hDevMode = NULL;
942 }
943
944 // Isn't this superfluous? It's called again below.
945 // data.GetPrintData().ConvertFromNative();
946
947 // Pass the devnames data back to the wxPrintData structure where it really belongs.
948 if (pd->hDevNames)
949 {
950 if (native_data->GetDevNames())
951 {
952 // Make sure we don't leak memory
953 GlobalFree((HGLOBAL) native_data->GetDevNames());
954 }
955 native_data->SetDevNames((void*) pd->hDevNames);
956 pd->hDevNames = NULL;
957 }
958
959 data.GetPrintData().ConvertFromNative();
960
961 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
962
963 data.SetDefaultMinMargins( ((pd->Flags & PSD_DEFAULTMINMARGINS) == PSD_DEFAULTMINMARGINS) );
964 data.EnableMargins( ((pd->Flags & PSD_DISABLEMARGINS) != PSD_DISABLEMARGINS) );
965 data.EnableOrientation( ((pd->Flags & PSD_DISABLEORIENTATION) != PSD_DISABLEORIENTATION) );
966 data.EnablePaper( ((pd->Flags & PSD_DISABLEPAPER) != PSD_DISABLEPAPER) );
967 data.EnablePrinter( ((pd->Flags & PSD_DISABLEPRINTER) != PSD_DISABLEPRINTER) );
968 data.SetDefaultInfo( ((pd->Flags & PSD_RETURNDEFAULT) == PSD_RETURNDEFAULT) );
969 data.EnableHelp( ((pd->Flags & PSD_SHOWHELP) == PSD_SHOWHELP) );
970
971 // PAGESETUPDLG is in hundreds of a mm
972 data.SetPaperSize( wxSize(pd->ptPaperSize.x / 100, pd->ptPaperSize.y / 100) );
973
974 data.SetMinMarginTopLeft( wxPoint(pd->rtMinMargin.left / 100, pd->rtMinMargin.top / 100) );
975 data.SetMinMarginBottomRight( wxPoint(pd->rtMinMargin.right / 100, pd->rtMinMargin.bottom / 100) );
976
977 data.SetMarginTopLeft( wxPoint(pd->rtMargin.left / 100, pd->rtMargin.top / 100) );
978 data.SetMarginBottomRight( wxPoint(pd->rtMargin.right / 100, pd->rtMargin.bottom / 100) );
979
980 return true;
981 }
982
983 #endif
984 // wxUSE_PRINTING_ARCHITECTURE