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