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