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