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