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