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