]> git.saurik.com Git - wxWidgets.git/blob - src/msw/printdlg.cpp
DMC typo fixes.
[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/msw/printdlg.h"
42 #include "wx/dcprint.h"
43 #include "wx/paper.h"
44
45 #include <stdlib.h>
46
47 #include "wx/msw/wrapcdlg.h"
48
49 #ifndef __WIN32__
50 #include <print.h>
51 #endif
52
53 //----------------------------------------------------------------------------
54 // wxWindowsPrintNativeData
55 //----------------------------------------------------------------------------
56
57 #ifdef __WXDEBUG__
58 static wxString wxGetPrintDlgError()
59 {
60 DWORD err = CommDlgExtendedError();
61 wxString msg = wxT("Unknown");
62 switch (err)
63 {
64 case CDERR_FINDRESFAILURE: msg = wxT("CDERR_FINDRESFAILURE"); break;
65 case CDERR_INITIALIZATION: msg = wxT("CDERR_INITIALIZATION"); break;
66 case CDERR_LOADRESFAILURE: msg = wxT("CDERR_LOADRESFAILURE"); break;
67 case CDERR_LOADSTRFAILURE: msg = wxT("CDERR_LOADSTRFAILURE"); break;
68 case CDERR_LOCKRESFAILURE: msg = wxT("CDERR_LOCKRESFAILURE"); break;
69 case CDERR_MEMALLOCFAILURE: msg = wxT("CDERR_MEMALLOCFAILURE"); break;
70 case CDERR_MEMLOCKFAILURE: msg = wxT("CDERR_MEMLOCKFAILURE"); break;
71 case CDERR_NOHINSTANCE: msg = wxT("CDERR_NOHINSTANCE"); break;
72 case CDERR_NOHOOK: msg = wxT("CDERR_NOHOOK"); break;
73 case CDERR_NOTEMPLATE: msg = wxT("CDERR_NOTEMPLATE"); break;
74 case CDERR_STRUCTSIZE: msg = wxT("CDERR_STRUCTSIZE"); break;
75 case PDERR_RETDEFFAILURE: msg = wxT("PDERR_RETDEFFAILURE"); break;
76 case PDERR_PRINTERNOTFOUND: msg = wxT("PDERR_PRINTERNOTFOUND"); break;
77 case PDERR_PARSEFAILURE: msg = wxT("PDERR_PARSEFAILURE"); break;
78 case PDERR_NODEVICES: msg = wxT("PDERR_NODEVICES"); break;
79 case PDERR_NODEFAULTPRN: msg = wxT("PDERR_NODEFAULTPRN"); break;
80 case PDERR_LOADDRVFAILURE: msg = wxT("PDERR_LOADDRVFAILURE"); break;
81 case PDERR_INITFAILURE: msg = wxT("PDERR_INITFAILURE"); break;
82 case PDERR_GETDEVMODEFAIL: msg = wxT("PDERR_GETDEVMODEFAIL"); break;
83 case PDERR_DNDMMISMATCH: msg = wxT("PDERR_DNDMMISMATCH"); break;
84 case PDERR_DEFAULTDIFFERENT: msg = wxT("PDERR_DEFAULTDIFFERENT"); break;
85 case PDERR_CREATEICFAILURE: msg = wxT("PDERR_CREATEICFAILURE"); break;
86 default: break;
87 }
88 return msg;
89 }
90 #endif
91
92 static HGLOBAL wxCreateDevNames(const wxString& driverName, const wxString& printerName, const wxString& portName)
93 {
94 HGLOBAL hDev = NULL;
95 // if (!driverName.empty() && !printerName.empty() && !portName.empty())
96 if (driverName.empty() && printerName.empty() && portName.empty())
97 {
98 }
99 else
100 {
101 hDev = GlobalAlloc(GPTR, 4*sizeof(WORD)+
102 ( driverName.Length() + 1 +
103 printerName.Length() + 1 +
104 portName.Length()+1 ) * sizeof(wxChar) );
105 LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(hDev);
106 lpDev->wDriverOffset = sizeof(WORD) * 4 / sizeof(wxChar);
107 wxStrcpy((wxChar*)lpDev + lpDev->wDriverOffset, driverName);
108
109 lpDev->wDeviceOffset = (WORD)( lpDev->wDriverOffset +
110 driverName.Length() + 1 );
111 wxStrcpy((wxChar*)lpDev + lpDev->wDeviceOffset, printerName);
112
113 lpDev->wOutputOffset = (WORD)( lpDev->wDeviceOffset +
114 printerName.Length() + 1 );
115 wxStrcpy((wxChar*)lpDev + lpDev->wOutputOffset, portName);
116
117 lpDev->wDefault = 0;
118
119 GlobalUnlock(hDev);
120 }
121
122 return hDev;
123 }
124
125 IMPLEMENT_CLASS(wxWindowsPrintNativeData, wxPrintNativeDataBase)
126
127 wxWindowsPrintNativeData::wxWindowsPrintNativeData()
128 {
129 m_devMode = (void*) NULL;
130 m_devNames = (void*) NULL;
131 m_customWindowsPaperId = 0;
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
203 bool foundPaperSize = false;
204 if ((devMode->dmFields & DM_PAPERSIZE) && (devMode->dmPaperSize < DMPAPER_USER))
205 {
206 if (wxThePrintPaperDatabase)
207 {
208 wxPrintPaperType* paper = wxThePrintPaperDatabase->FindPaperTypeByPlatformId(devMode->dmPaperSize);
209 if (paper)
210 {
211 data.SetPaperId( paper->GetId() );
212 data.SetPaperSize( wxSize(paper->GetWidth() / 10,paper->GetHeight() / 10) );
213 m_customWindowsPaperId = 0;
214 foundPaperSize = true;
215 }
216 }
217 else
218 {
219 // Shouldn't really get here
220 wxFAIL_MSG(wxT("Paper database wasn't initialized in wxPrintData::ConvertFromNative."));
221 data.SetPaperId( wxPAPER_NONE );
222 data.SetPaperSize( wxSize(0,0) );
223 m_customWindowsPaperId = 0;
224
225 GlobalUnlock(hDevMode);
226 return false;
227 }
228 }
229
230 if (!foundPaperSize && (devMode->dmFields & DM_PAPERWIDTH) && (devMode->dmFields & DM_PAPERLENGTH))
231 {
232 // DEVMODE is in tenths of a milimeter
233 data.SetPaperSize( wxSize(devMode->dmPaperWidth / 10, devMode->dmPaperLength / 10) );
234 data.SetPaperId( wxPAPER_NONE );
235 m_customWindowsPaperId = devMode->dmPaperSize;
236 }
237 else
238 {
239 // Often will reach this for non-standard paper sizes (sizes which
240 // wouldn't be in wxWidget's paper database). Setting
241 // m_customWindowsPaperId to devMode->dmPaperSize should be enough
242 // to get this paper size working.
243 data.SetPaperSize( wxSize(0,0) );
244 data.SetPaperId( wxPAPER_NONE );
245 m_customWindowsPaperId = devMode->dmPaperSize;
246 }
247
248 //// Duplex
249
250 if (devMode->dmFields & DM_DUPLEX)
251 {
252 switch (devMode->dmDuplex)
253 {
254 case DMDUP_HORIZONTAL: data.SetDuplex( wxDUPLEX_HORIZONTAL ); break;
255 case DMDUP_VERTICAL: data.SetDuplex( wxDUPLEX_VERTICAL ); break;
256 default:
257 case DMDUP_SIMPLEX: data.SetDuplex( wxDUPLEX_SIMPLEX ); break;
258 }
259 }
260 else
261 data.SetDuplex( wxDUPLEX_SIMPLEX );
262
263 //// Quality
264
265 if (devMode->dmFields & DM_PRINTQUALITY)
266 {
267 switch (devMode->dmPrintQuality)
268 {
269 case DMRES_MEDIUM: data.SetQuality( wxPRINT_QUALITY_MEDIUM ); break;
270 case DMRES_LOW: data.SetQuality( wxPRINT_QUALITY_LOW ); break;
271 case DMRES_DRAFT: data.SetQuality( wxPRINT_QUALITY_DRAFT ); break;
272 case DMRES_HIGH: data.SetQuality( wxPRINT_QUALITY_HIGH ); break;
273 default:
274 {
275 // TODO: if the printer fills in the resolution in DPI, how
276 // will the application know if it's high, low, draft etc.??
277 // wxFAIL_MSG("Warning: DM_PRINTQUALITY was not one of the standard values.");
278 data.SetQuality( devMode->dmPrintQuality );
279 break;
280
281 }
282 }
283 }
284 else
285 data.SetQuality( wxPRINT_QUALITY_HIGH );
286
287 if (devMode->dmDriverExtra > 0)
288 data.SetPrivData( (char *)devMode+devMode->dmSize, devMode->dmDriverExtra );
289 else
290 data.SetPrivData( NULL, 0 );
291
292 GlobalUnlock(hDevMode);
293 }
294
295 if (hDevNames)
296 {
297 LPDEVNAMES lpDevNames = (LPDEVNAMES)GlobalLock(hDevNames);
298 if (lpDevNames)
299 {
300 // TODO: Unicode-ification
301
302 // Get the port name
303 // port is obsolete in WIN32
304 // m_printData.SetPortName((LPSTR)lpDevNames + lpDevNames->wDriverOffset);
305
306 // Get the printer name
307 wxString printerName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset;
308
309 // Not sure if we should check for this mismatch
310 // wxASSERT_MSG( (m_printerName.empty() || (devName == m_printerName)), "Printer name obtained from DEVMODE and DEVNAMES were different!");
311
312 if (!printerName.empty())
313 data.SetPrinterName( printerName );
314
315 GlobalUnlock(hDevNames);
316 }
317 }
318
319 return true;
320 }
321
322 bool wxWindowsPrintNativeData::TransferFrom( const wxPrintData &data )
323 {
324 HGLOBAL hDevMode = (HGLOBAL)(DWORD) m_devMode;
325 HGLOBAL hDevNames = (HGLOBAL)(DWORD) m_devNames;
326 if (!hDevMode)
327 {
328 // Use PRINTDLG as a way of creating a DEVMODE object
329 PRINTDLG pd;
330
331 // GNU-WIN32 has the wrong size PRINTDLG - can't work out why.
332 #ifdef __GNUWIN32__
333 memset(&pd, 0, 66);
334 pd.lStructSize = 66;
335 #else
336 memset(&pd, 0, sizeof(PRINTDLG));
337 #ifdef __WXWINCE__
338 pd.cbStruct = sizeof(PRINTDLG);
339 #else
340 pd.lStructSize = sizeof(PRINTDLG);
341 #endif
342 #endif
343
344 pd.hwndOwner = (HWND)NULL;
345 pd.hDevMode = NULL; // Will be created by PrintDlg
346 pd.hDevNames = NULL; // Ditto
347 //pd.hInstance = (HINSTANCE) wxGetInstance();
348
349 pd.Flags = PD_RETURNDEFAULT;
350 pd.nCopies = 1;
351
352 // Fill out the DEVMODE structure
353 // so we can use it as input in the 'real' PrintDlg
354 if (!PrintDlg(&pd))
355 {
356 if ( pd.hDevMode )
357 GlobalFree(pd.hDevMode);
358 if ( pd.hDevNames )
359 GlobalFree(pd.hDevNames);
360 pd.hDevMode = NULL;
361 pd.hDevNames = NULL;
362
363 #if defined(__WXDEBUG__) && defined(__WIN32__)
364 wxString str(wxT("Printing error: "));
365 str += wxGetPrintDlgError();
366 wxLogDebug(str);
367 #endif
368 }
369 else
370 {
371 hDevMode = pd.hDevMode;
372 m_devMode = (void*)(long) hDevMode;
373 pd.hDevMode = NULL;
374
375 // We'll create a new DEVNAMEs structure below.
376 if ( pd.hDevNames )
377 GlobalFree(pd.hDevNames);
378 pd.hDevNames = NULL;
379
380 // hDevNames = pd->hDevNames;
381 // m_devNames = (void*)(long) hDevNames;
382 // pd->hDevnames = NULL;
383
384 }
385 }
386
387 if ( hDevMode )
388 {
389 LPDEVMODE devMode = (LPDEVMODE) GlobalLock(hDevMode);
390
391 //// Orientation
392 devMode->dmOrientation = (short)data.GetOrientation();
393
394 //// Collation
395 devMode->dmCollate = (data.GetCollate() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE);
396 devMode->dmFields |= DM_COLLATE;
397
398 //// Number of copies
399 devMode->dmCopies = (short)data.GetNoCopies();
400 devMode->dmFields |= DM_COPIES;
401
402 //// Printer name
403 wxString name = data.GetPrinterName();
404 if (!name.empty())
405 {
406 //int len = wxMin(31, m_printerName.Len());
407 wxStrncpy((wxChar*)devMode->dmDeviceName,name.c_str(),31);
408 devMode->dmDeviceName[31] = wxT('\0');
409 }
410
411 //// Colour
412 if (data.GetColour())
413 devMode->dmColor = DMCOLOR_COLOR;
414 else
415 devMode->dmColor = DMCOLOR_MONOCHROME;
416 devMode->dmFields |= DM_COLOR;
417
418 //// Paper size
419 if (data.GetPaperId() == wxPAPER_NONE)
420 {
421 // DEVMODE is in tenths of a milimeter
422 devMode->dmPaperWidth = (short)(data.GetPaperSize().x * 10);
423 devMode->dmPaperLength = (short)(data.GetPaperSize().y * 10);
424 if(m_customWindowsPaperId != 0)
425 devMode->dmPaperSize = m_customWindowsPaperId;
426 else
427 devMode->dmPaperSize = DMPAPER_USER;
428 devMode->dmFields |= DM_PAPERWIDTH;
429 devMode->dmFields |= DM_PAPERLENGTH;
430 }
431 else
432 {
433 if (wxThePrintPaperDatabase)
434 {
435 wxPrintPaperType* paper = wxThePrintPaperDatabase->FindPaperType( data.GetPaperId() );
436 if (paper)
437 {
438 devMode->dmPaperSize = (short)paper->GetPlatformId();
439 devMode->dmFields |= DM_PAPERSIZE;
440 }
441 else
442 {
443 // Fall back on specifying the paper size explicitly
444 devMode->dmPaperWidth = (short)(data.GetPaperSize().x * 10);
445 devMode->dmPaperLength = (short)(data.GetPaperSize().y * 10);
446 devMode->dmPaperSize = DMPAPER_USER;
447 devMode->dmFields |= DM_PAPERWIDTH;
448 devMode->dmFields |= DM_PAPERLENGTH;
449 }
450 }
451 }
452
453 //// Duplex
454 short duplex;
455 switch (data.GetDuplex())
456 {
457 case wxDUPLEX_HORIZONTAL:
458 duplex = DMDUP_HORIZONTAL;
459 break;
460 case wxDUPLEX_VERTICAL:
461 duplex = DMDUP_VERTICAL;
462 break;
463 default:
464 // in fact case wxDUPLEX_SIMPLEX:
465 duplex = DMDUP_SIMPLEX;
466 break;
467 }
468 devMode->dmDuplex = duplex;
469 devMode->dmFields |= DM_DUPLEX;
470
471 //// Quality
472
473 short quality;
474 switch (data.GetQuality())
475 {
476 case wxPRINT_QUALITY_MEDIUM:
477 quality = DMRES_MEDIUM;
478 break;
479 case wxPRINT_QUALITY_LOW:
480 quality = DMRES_LOW;
481 break;
482 case wxPRINT_QUALITY_DRAFT:
483 quality = DMRES_DRAFT;
484 break;
485 case wxPRINT_QUALITY_HIGH:
486 quality = DMRES_HIGH;
487 break;
488 default:
489 quality = (short)data.GetQuality();
490 break;
491 }
492 devMode->dmPrintQuality = quality;
493 devMode->dmFields |= DM_PRINTQUALITY;
494
495 if (data.GetPrivDataLen() > 0)
496 {
497 memcpy( (char *)devMode+devMode->dmSize, data.GetPrivData(), data.GetPrivDataLen() );
498 devMode->dmDriverExtra = (WXWORD)data.GetPrivDataLen();
499 }
500
501 if (data.GetBin() != wxPRINTBIN_DEFAULT)
502 {
503 switch (data.GetBin())
504 {
505 case wxPRINTBIN_ONLYONE: devMode->dmDefaultSource = DMBIN_ONLYONE; break;
506 case wxPRINTBIN_LOWER: devMode->dmDefaultSource = DMBIN_LOWER; break;
507 case wxPRINTBIN_MIDDLE: devMode->dmDefaultSource = DMBIN_MIDDLE; break;
508 case wxPRINTBIN_MANUAL: devMode->dmDefaultSource = DMBIN_MANUAL; break;
509 case wxPRINTBIN_ENVELOPE: devMode->dmDefaultSource = DMBIN_ENVELOPE; break;
510 case wxPRINTBIN_ENVMANUAL: devMode->dmDefaultSource = DMBIN_ENVMANUAL; break;
511 case wxPRINTBIN_AUTO: devMode->dmDefaultSource = DMBIN_AUTO; break;
512 case wxPRINTBIN_TRACTOR: devMode->dmDefaultSource = DMBIN_TRACTOR; break;
513 case wxPRINTBIN_SMALLFMT: devMode->dmDefaultSource = DMBIN_SMALLFMT; break;
514 case wxPRINTBIN_LARGEFMT: devMode->dmDefaultSource = DMBIN_LARGEFMT; break;
515 case wxPRINTBIN_LARGECAPACITY: devMode->dmDefaultSource = DMBIN_LARGECAPACITY; break;
516 case wxPRINTBIN_CASSETTE: devMode->dmDefaultSource = DMBIN_CASSETTE; break;
517 case wxPRINTBIN_FORMSOURCE: devMode->dmDefaultSource = DMBIN_FORMSOURCE; break;
518
519 default:
520 devMode->dmDefaultSource = (short)(DMBIN_USER + data.GetBin() - wxPRINTBIN_USER);
521 break;
522 }
523
524 devMode->dmFields |= DM_DEFAULTSOURCE;
525 }
526
527 GlobalUnlock(hDevMode);
528 }
529
530 if ( hDevNames )
531 {
532 GlobalFree(hDevNames);
533 }
534
535 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
536 m_devNames = (void*) (long) wxCreateDevNames(wxEmptyString, data.GetPrinterName(), wxEmptyString);
537
538 return true;
539 }
540
541 // ---------------------------------------------------------------------------
542 // wxPrintDialog
543 // ---------------------------------------------------------------------------
544
545 IMPLEMENT_CLASS(wxWindowsPrintDialog, wxPrintDialogBase)
546
547 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintDialogData* data)
548 {
549 Create(p, data);
550 }
551
552 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintData* data)
553 {
554 wxPrintDialogData data2;
555 if ( data )
556 data2 = *data;
557
558 Create(p, &data2);
559 }
560
561 bool wxWindowsPrintDialog::Create(wxWindow *p, wxPrintDialogData* data)
562 {
563 m_dialogParent = p;
564 m_printerDC = NULL;
565 m_destroyDC = true;
566
567 // MSW handle
568 m_printDlg = NULL;
569
570 if ( data )
571 m_printDialogData = *data;
572
573 return true;
574 }
575
576 wxWindowsPrintDialog::~wxWindowsPrintDialog()
577 {
578 PRINTDLG *pd = (PRINTDLG *) m_printDlg;
579 if (pd && pd->hDevMode)
580 GlobalFree(pd->hDevMode);
581 if ( pd )
582 delete pd;
583
584 if (m_destroyDC && m_printerDC)
585 delete m_printerDC;
586 }
587
588 int wxWindowsPrintDialog::ShowModal()
589 {
590 ConvertToNative( m_printDialogData );
591
592 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
593
594 if (m_dialogParent)
595 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
596 else if (wxTheApp->GetTopWindow())
597 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
598 else
599 pd->hwndOwner = 0;
600
601 bool ret = (PrintDlg( pd ) != 0);
602
603 pd->hwndOwner = 0;
604
605 if ( ret != false && (pd->hDC) )
606 {
607 wxPrinterDC *pdc = new wxPrinterDC( (WXHDC) pd->hDC );
608 m_printerDC = pdc;
609 ConvertFromNative( m_printDialogData );
610 return wxID_OK;
611 }
612 else
613 {
614 return wxID_CANCEL;
615 }
616 }
617
618 wxDC *wxWindowsPrintDialog::GetPrintDC()
619 {
620 if (m_printerDC)
621 {
622 m_destroyDC = false;
623 return m_printerDC;
624 }
625 else
626 return (wxDC*) NULL;
627 }
628
629 bool wxWindowsPrintDialog::ConvertToNative( wxPrintDialogData &data )
630 {
631 wxWindowsPrintNativeData *native_data =
632 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
633 data.GetPrintData().ConvertToNative();
634
635 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
636
637 // Shouldn't have been defined anywhere
638 if (pd)
639 return false;
640
641 pd = new PRINTDLG;
642 memset( pd, 0, sizeof(PRINTDLG) );
643 m_printDlg = (void*) pd;
644
645 // GNU-WIN32 has the wrong size PRINTDLG - can't work out why.
646 #ifdef __GNUWIN32__
647 pd->lStructSize = 66;
648 #else
649 pd->lStructSize = sizeof(PRINTDLG);
650 #endif
651 pd->hwndOwner = (HWND)NULL;
652 pd->hDevMode = NULL; // Will be created by PrintDlg
653 pd->hDevNames = NULL; // Ditto
654
655 pd->Flags = PD_RETURNDEFAULT;
656 pd->nCopies = 1;
657
658 // Pass the devmode data to the PRINTDLG structure, since it'll
659 // be needed when PrintDlg is called.
660 if (pd->hDevMode)
661 GlobalFree(pd->hDevMode);
662
663 // Pass the devnames data to the PRINTDLG structure, since it'll
664 // be needed when PrintDlg is called.
665 if (pd->hDevNames)
666 GlobalFree(pd->hDevNames);
667
668 pd->hDevMode = (HGLOBAL)(DWORD) native_data->GetDevMode();
669 native_data->SetDevMode( (void*) NULL);
670
671 // Shouldn't assert; we should be able to test Ok-ness at a higher level
672 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
673
674 pd->hDevNames = (HGLOBAL)(DWORD) native_data->GetDevNames();
675 native_data->SetDevNames( (void*) NULL);
676
677
678 pd->hDC = (HDC) NULL;
679 pd->nFromPage = (WORD)data.GetFromPage();
680 pd->nToPage = (WORD)data.GetToPage();
681 pd->nMinPage = (WORD)data.GetMinPage();
682 pd->nMaxPage = (WORD)data.GetMaxPage();
683 pd->nCopies = (WORD)data.GetNoCopies();
684
685 pd->Flags = PD_RETURNDC;
686
687 #ifdef __GNUWIN32__
688 pd->lStructSize = 66;
689 #else
690 pd->lStructSize = sizeof( PRINTDLG );
691 #endif
692
693 pd->hwndOwner=(HWND)NULL;
694 // pd->hDevNames=(HANDLE)NULL;
695 pd->hInstance=(HINSTANCE)NULL;
696 pd->lCustData = (LPARAM) NULL;
697 pd->lpfnPrintHook = NULL;
698 pd->lpfnSetupHook = NULL;
699 pd->lpPrintTemplateName = NULL;
700 pd->lpSetupTemplateName = NULL;
701 pd->hPrintTemplate = (HGLOBAL) NULL;
702 pd->hSetupTemplate = (HGLOBAL) NULL;
703
704 if ( data.GetAllPages() )
705 pd->Flags |= PD_ALLPAGES;
706 if ( data.GetSelection() )
707 pd->Flags |= PD_SELECTION;
708 if ( data.GetCollate() )
709 pd->Flags |= PD_COLLATE;
710 if ( data.GetPrintToFile() )
711 pd->Flags |= PD_PRINTTOFILE;
712 if ( !data.GetEnablePrintToFile() )
713 pd->Flags |= PD_DISABLEPRINTTOFILE;
714 if ( !data.GetEnableSelection() )
715 pd->Flags |= PD_NOSELECTION;
716 if ( !data.GetEnablePageNumbers() )
717 pd->Flags |= PD_NOPAGENUMS;
718 else if ( (!data.GetAllPages()) && (!data.GetSelection()) && (data.GetFromPage() != 0) && (data.GetToPage() != 0))
719 pd->Flags |= PD_PAGENUMS;
720 if ( data.GetEnableHelp() )
721 pd->Flags |= PD_SHOWHELP;
722 #if WXWIN_COMPATIBILITY_2_4
723 if ( data.GetSetupDialog() )
724 pd->Flags |= PD_PRINTSETUP;
725 #endif
726
727 return true;
728 }
729
730 bool wxWindowsPrintDialog::ConvertFromNative( wxPrintDialogData &data )
731 {
732 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
733 if ( pd == NULL )
734 return false;
735
736 wxWindowsPrintNativeData *native_data =
737 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
738
739 // Pass the devmode data back to the wxPrintData structure where it really belongs.
740 if (pd->hDevMode)
741 {
742 if (native_data->GetDevMode())
743 {
744 // Make sure we don't leak memory
745 GlobalFree( (HGLOBAL)(DWORD) native_data->GetDevMode() );
746 }
747 native_data->SetDevMode( (void*)(long) pd->hDevMode );
748 pd->hDevMode = NULL;
749 }
750
751 // Pass the devnames data back to the wxPrintData structure where it really belongs.
752 if (pd->hDevNames)
753 {
754 if (native_data->GetDevNames())
755 {
756 // Make sure we don't leak memory
757 GlobalFree((HGLOBAL)(DWORD) native_data->GetDevNames());
758 }
759 native_data->SetDevNames((void*)(long) pd->hDevNames);
760 pd->hDevNames = NULL;
761 }
762
763 // Now convert the DEVMODE object, passed down from the PRINTDLG object,
764 // into wxWidgets form.
765 native_data->TransferTo( data.GetPrintData() );
766
767 data.SetFromPage( pd->nFromPage );
768 data.SetToPage( pd->nToPage );
769 data.SetMinPage( pd->nMinPage );
770 data.SetMaxPage( pd->nMaxPage );
771 data.SetNoCopies( pd->nCopies );
772
773 data.SetAllPages( (((pd->Flags & PD_PAGENUMS) != PD_PAGENUMS) && ((pd->Flags & PD_SELECTION) != PD_SELECTION)) );
774 data.SetSelection( ((pd->Flags & PD_SELECTION) == PD_SELECTION) );
775 data.SetCollate( ((pd->Flags & PD_COLLATE) == PD_COLLATE) );
776 data.SetPrintToFile( ((pd->Flags & PD_PRINTTOFILE) == PD_PRINTTOFILE) );
777 data.EnablePrintToFile( ((pd->Flags & PD_DISABLEPRINTTOFILE) != PD_DISABLEPRINTTOFILE) );
778 data.EnableSelection( ((pd->Flags & PD_NOSELECTION) != PD_NOSELECTION) );
779 data.EnablePageNumbers( ((pd->Flags & PD_NOPAGENUMS) != PD_NOPAGENUMS) );
780 data.EnableHelp( ((pd->Flags & PD_SHOWHELP) == PD_SHOWHELP) );
781 #if WXWIN_COMPATIBILITY_2_4
782 data.SetSetupDialog( ((pd->Flags & PD_PRINTSETUP) == PD_PRINTSETUP) );
783 #endif
784 return true;
785 }
786
787 // ---------------------------------------------------------------------------
788 // wxWidnowsPageSetupDialog
789 // ---------------------------------------------------------------------------
790
791 IMPLEMENT_CLASS(wxWindowsPageSetupDialog, wxPageSetupDialogBase)
792
793 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog()
794 {
795 m_dialogParent = NULL;
796 m_pageDlg = NULL;
797 }
798
799 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog(wxWindow *p, wxPageSetupDialogData *data)
800 {
801 Create(p, data);
802 }
803
804 bool wxWindowsPageSetupDialog::Create(wxWindow *p, wxPageSetupDialogData *data)
805 {
806 m_dialogParent = p;
807 m_pageDlg = NULL;
808
809 if (data)
810 m_pageSetupData = (*data);
811
812 return true;
813 }
814
815 wxWindowsPageSetupDialog::~wxWindowsPageSetupDialog()
816 {
817 PAGESETUPDLG *pd = (PAGESETUPDLG *)m_pageDlg;
818 if ( pd && pd->hDevMode )
819 GlobalFree(pd->hDevMode);
820 if ( pd && pd->hDevNames )
821 GlobalFree(pd->hDevNames);
822 if ( pd )
823 delete pd;
824 }
825
826 int wxWindowsPageSetupDialog::ShowModal()
827 {
828 ConvertToNative( m_pageSetupData );
829
830 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
831 if (m_dialogParent)
832 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
833 else if (wxTheApp->GetTopWindow())
834 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
835 else
836 pd->hwndOwner = 0;
837 BOOL retVal = PageSetupDlg( pd ) ;
838 pd->hwndOwner = 0;
839 if (retVal)
840 {
841 ConvertFromNative( m_pageSetupData );
842 return wxID_OK;
843 }
844 else
845 return wxID_CANCEL;
846 }
847
848 bool wxWindowsPageSetupDialog::ConvertToNative( wxPageSetupDialogData &data )
849 {
850 wxWindowsPrintNativeData *native_data =
851 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
852 data.GetPrintData().ConvertToNative();
853
854 PAGESETUPDLG *pd = (PAGESETUPDLG*) m_pageDlg;
855
856 // Shouldn't have been defined anywhere
857 if (pd)
858 return false;
859
860 pd = new PAGESETUPDLG;
861 pd->hDevMode = NULL;
862 pd->hDevNames = NULL;
863 m_pageDlg = (void *)pd;
864
865 // Pass the devmode data (created in m_printData.ConvertToNative)
866 // to the PRINTDLG structure, since it'll
867 // be needed when PrintDlg is called.
868
869 if (pd->hDevMode)
870 {
871 GlobalFree(pd->hDevMode);
872 pd->hDevMode = NULL;
873 }
874 pd->hDevMode = (HGLOBAL) native_data->GetDevMode();
875 native_data->SetDevMode( (void*) NULL );
876
877 // Shouldn't assert; we should be able to test Ok-ness at a higher level
878 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
879
880 // Pass the devnames data (created in m_printData.ConvertToNative)
881 // to the PRINTDLG structure, since it'll
882 // be needed when PrintDlg is called.
883
884 if (pd->hDevNames)
885 {
886 GlobalFree(pd->hDevNames);
887 pd->hDevNames = NULL;
888 }
889 pd->hDevNames = (HGLOBAL) native_data->GetDevNames();
890 native_data->SetDevNames((void*) NULL);
891
892 // pd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
893
894 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
895
896 if ( data.GetDefaultMinMargins() )
897 pd->Flags |= PSD_DEFAULTMINMARGINS;
898 if ( !data.GetEnableMargins() )
899 pd->Flags |= PSD_DISABLEMARGINS;
900 if ( !data.GetEnableOrientation() )
901 pd->Flags |= PSD_DISABLEORIENTATION;
902 if ( !data.GetEnablePaper() )
903 pd->Flags |= PSD_DISABLEPAPER;
904 if ( !data.GetEnablePrinter() )
905 pd->Flags |= PSD_DISABLEPRINTER;
906 if ( data.GetDefaultInfo() )
907 pd->Flags |= PSD_RETURNDEFAULT;
908 if ( data.GetEnableHelp() )
909 pd->Flags |= PSD_SHOWHELP;
910
911 // We want the units to be in hundredths of a millimetre
912 pd->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
913
914 pd->lStructSize = sizeof( PAGESETUPDLG );
915 pd->hwndOwner=(HWND)NULL;
916 // pd->hDevNames=(HWND)NULL;
917 pd->hInstance=(HINSTANCE)NULL;
918 // PAGESETUPDLG is in hundreds of a mm
919 pd->ptPaperSize.x = data.GetPaperSize().x * 100;
920 pd->ptPaperSize.y = data.GetPaperSize().y * 100;
921
922 pd->rtMinMargin.left = data.GetMinMarginTopLeft().x * 100;
923 pd->rtMinMargin.top = data.GetMinMarginTopLeft().y * 100;
924 pd->rtMinMargin.right = data.GetMinMarginBottomRight().x * 100;
925 pd->rtMinMargin.bottom = data.GetMinMarginBottomRight().y * 100;
926
927 pd->rtMargin.left = data.GetMarginTopLeft().x * 100;
928 pd->rtMargin.top = data.GetMarginTopLeft().y * 100;
929 pd->rtMargin.right = data.GetMarginBottomRight().x * 100;
930 pd->rtMargin.bottom = data.GetMarginBottomRight().y * 100;
931
932 pd->lCustData = 0;
933 pd->lpfnPageSetupHook = NULL;
934 pd->lpfnPagePaintHook = NULL;
935 pd->hPageSetupTemplate = NULL;
936 pd->lpPageSetupTemplateName = NULL;
937
938 /*
939 if ( pd->hDevMode )
940 {
941 DEVMODE *devMode = (DEVMODE*) GlobalLock(pd->hDevMode);
942 memset(devMode, 0, sizeof(DEVMODE));
943 devMode->dmSize = sizeof(DEVMODE);
944 devMode->dmOrientation = m_orientation;
945 devMode->dmFields = DM_ORIENTATION;
946 GlobalUnlock(pd->hDevMode);
947 }
948 */
949 return true;
950 }
951
952 bool wxWindowsPageSetupDialog::ConvertFromNative( wxPageSetupDialogData &data )
953 {
954 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
955 if ( !pd )
956 return false;
957
958 wxWindowsPrintNativeData *native_data =
959 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
960
961 // Pass the devmode data back to the wxPrintData structure where it really belongs.
962 if (pd->hDevMode)
963 {
964 if (native_data->GetDevMode())
965 {
966 // Make sure we don't leak memory
967 GlobalFree((HGLOBAL) native_data->GetDevMode());
968 }
969 native_data->SetDevMode( (void*) pd->hDevMode );
970 pd->hDevMode = NULL;
971 }
972
973 // Isn't this superfluous? It's called again below.
974 // data.GetPrintData().ConvertFromNative();
975
976 // Pass the devnames data back to the wxPrintData structure where it really belongs.
977 if (pd->hDevNames)
978 {
979 if (native_data->GetDevNames())
980 {
981 // Make sure we don't leak memory
982 GlobalFree((HGLOBAL) native_data->GetDevNames());
983 }
984 native_data->SetDevNames((void*) pd->hDevNames);
985 pd->hDevNames = NULL;
986 }
987
988 data.GetPrintData().ConvertFromNative();
989
990 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
991
992 data.SetDefaultMinMargins( ((pd->Flags & PSD_DEFAULTMINMARGINS) == PSD_DEFAULTMINMARGINS) );
993 data.EnableMargins( ((pd->Flags & PSD_DISABLEMARGINS) != PSD_DISABLEMARGINS) );
994 data.EnableOrientation( ((pd->Flags & PSD_DISABLEORIENTATION) != PSD_DISABLEORIENTATION) );
995 data.EnablePaper( ((pd->Flags & PSD_DISABLEPAPER) != PSD_DISABLEPAPER) );
996 data.EnablePrinter( ((pd->Flags & PSD_DISABLEPRINTER) != PSD_DISABLEPRINTER) );
997 data.SetDefaultInfo( ((pd->Flags & PSD_RETURNDEFAULT) == PSD_RETURNDEFAULT) );
998 data.EnableHelp( ((pd->Flags & PSD_SHOWHELP) == PSD_SHOWHELP) );
999
1000 // PAGESETUPDLG is in hundreds of a mm
1001 data.SetPaperSize( wxSize(pd->ptPaperSize.x / 100, pd->ptPaperSize.y / 100) );
1002
1003 data.SetMinMarginTopLeft( wxPoint(pd->rtMinMargin.left / 100, pd->rtMinMargin.top / 100) );
1004 data.SetMinMarginBottomRight( wxPoint(pd->rtMinMargin.right / 100, pd->rtMinMargin.bottom / 100) );
1005
1006 data.SetMarginTopLeft( wxPoint(pd->rtMargin.left / 100, pd->rtMargin.top / 100) );
1007 data.SetMarginBottomRight( wxPoint(pd->rtMargin.right / 100, pd->rtMargin.bottom / 100) );
1008
1009 return true;
1010 }
1011
1012 #endif
1013 // wxUSE_PRINTING_ARCHITECTURE