]> git.saurik.com Git - wxWidgets.git/blob - src/msw/printdlg.cpp
Removed comment
[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 //----------------------------------------------------------------------------
46 // wxWindowsPrintNativeData
47 //----------------------------------------------------------------------------
48
49 #if wxDEBUG_LEVEL
50
51 static wxString wxGetPrintDlgError()
52 {
53 DWORD err = CommDlgExtendedError();
54 wxString msg = wxT("Unknown");
55 switch (err)
56 {
57 case CDERR_FINDRESFAILURE: msg = wxT("CDERR_FINDRESFAILURE"); break;
58 case CDERR_INITIALIZATION: msg = wxT("CDERR_INITIALIZATION"); break;
59 case CDERR_LOADRESFAILURE: msg = wxT("CDERR_LOADRESFAILURE"); break;
60 case CDERR_LOADSTRFAILURE: msg = wxT("CDERR_LOADSTRFAILURE"); break;
61 case CDERR_LOCKRESFAILURE: msg = wxT("CDERR_LOCKRESFAILURE"); break;
62 case CDERR_MEMALLOCFAILURE: msg = wxT("CDERR_MEMALLOCFAILURE"); break;
63 case CDERR_MEMLOCKFAILURE: msg = wxT("CDERR_MEMLOCKFAILURE"); break;
64 case CDERR_NOHINSTANCE: msg = wxT("CDERR_NOHINSTANCE"); break;
65 case CDERR_NOHOOK: msg = wxT("CDERR_NOHOOK"); break;
66 case CDERR_NOTEMPLATE: msg = wxT("CDERR_NOTEMPLATE"); break;
67 case CDERR_STRUCTSIZE: msg = wxT("CDERR_STRUCTSIZE"); break;
68 case PDERR_RETDEFFAILURE: msg = wxT("PDERR_RETDEFFAILURE"); break;
69 case PDERR_PRINTERNOTFOUND: msg = wxT("PDERR_PRINTERNOTFOUND"); break;
70 case PDERR_PARSEFAILURE: msg = wxT("PDERR_PARSEFAILURE"); break;
71 case PDERR_NODEVICES: msg = wxT("PDERR_NODEVICES"); break;
72 case PDERR_NODEFAULTPRN: msg = wxT("PDERR_NODEFAULTPRN"); break;
73 case PDERR_LOADDRVFAILURE: msg = wxT("PDERR_LOADDRVFAILURE"); break;
74 case PDERR_INITFAILURE: msg = wxT("PDERR_INITFAILURE"); break;
75 case PDERR_GETDEVMODEFAIL: msg = wxT("PDERR_GETDEVMODEFAIL"); break;
76 case PDERR_DNDMMISMATCH: msg = wxT("PDERR_DNDMMISMATCH"); break;
77 case PDERR_DEFAULTDIFFERENT: msg = wxT("PDERR_DEFAULTDIFFERENT"); break;
78 case PDERR_CREATEICFAILURE: msg = wxT("PDERR_CREATEICFAILURE"); break;
79 default: break;
80 }
81 return msg;
82 }
83
84 #endif // wxDEBUG_LEVEL
85
86
87 static HGLOBAL
88 wxCreateDevNames(const wxString& driverName,
89 const wxString& printerName,
90 const wxString& portName)
91 {
92 HGLOBAL hDev = NULL;
93 // if (!driverName.empty() && !printerName.empty() && !portName.empty())
94 if (driverName.empty() && printerName.empty() && portName.empty())
95 {
96 }
97 else
98 {
99 hDev = GlobalAlloc(GPTR, 4*sizeof(WORD)+
100 ( driverName.length() + 1 +
101 printerName.length() + 1 +
102 portName.length()+1 ) * sizeof(wxChar) );
103 LPDEVNAMES lpDev = (LPDEVNAMES)GlobalLock(hDev);
104 lpDev->wDriverOffset = sizeof(WORD) * 4 / sizeof(wxChar);
105 wxStrcpy((wxChar*)lpDev + lpDev->wDriverOffset, driverName);
106
107 lpDev->wDeviceOffset = (WORD)( lpDev->wDriverOffset +
108 driverName.length() + 1 );
109 wxStrcpy((wxChar*)lpDev + lpDev->wDeviceOffset, printerName);
110
111 lpDev->wOutputOffset = (WORD)( lpDev->wDeviceOffset +
112 printerName.length() + 1 );
113 wxStrcpy((wxChar*)lpDev + lpDev->wOutputOffset, portName);
114
115 lpDev->wDefault = 0;
116
117 GlobalUnlock(hDev);
118 }
119
120 return hDev;
121 }
122
123 IMPLEMENT_CLASS(wxWindowsPrintNativeData, wxPrintNativeDataBase)
124
125 wxWindowsPrintNativeData::wxWindowsPrintNativeData()
126 {
127 m_devMode = NULL;
128 m_devNames = NULL;
129 m_customWindowsPaperId = 0;
130 }
131
132 wxWindowsPrintNativeData::~wxWindowsPrintNativeData()
133 {
134 if ( m_devMode )
135 ::GlobalFree(static_cast<HGLOBAL>(m_devMode));
136
137 if ( m_devNames )
138 ::GlobalFree(static_cast<HGLOBAL>(m_devNames));
139 }
140
141 bool wxWindowsPrintNativeData::IsOk() const
142 {
143 return (m_devMode != NULL) ;
144 }
145
146 bool wxWindowsPrintNativeData::TransferTo( wxPrintData &data )
147 {
148 if ( !m_devMode )
149 return false;
150
151 GlobalPtrLock lockDevMode(m_devMode);
152
153 LPDEVMODE devMode = static_cast<LPDEVMODE>(lockDevMode.Get());
154
155 //// Orientation
156 if (devMode->dmFields & DM_ORIENTATION)
157 data.SetOrientation( devMode->dmOrientation );
158
159 //// Collation
160 if (devMode->dmFields & DM_COLLATE)
161 {
162 if (devMode->dmCollate == DMCOLLATE_TRUE)
163 data.SetCollate( true );
164 else
165 data.SetCollate( false );
166 }
167
168 //// Number of copies
169 if (devMode->dmFields & DM_COPIES)
170 data.SetNoCopies( devMode->dmCopies );
171
172 //// Bin
173 if (devMode->dmFields & DM_DEFAULTSOURCE) {
174 switch (devMode->dmDefaultSource) {
175 case DMBIN_ONLYONE : data.SetBin(wxPRINTBIN_ONLYONE ); break;
176 case DMBIN_LOWER : data.SetBin(wxPRINTBIN_LOWER ); break;
177 case DMBIN_MIDDLE : data.SetBin(wxPRINTBIN_MIDDLE ); break;
178 case DMBIN_MANUAL : data.SetBin(wxPRINTBIN_MANUAL ); break;
179 case DMBIN_ENVELOPE : data.SetBin(wxPRINTBIN_ENVELOPE ); break;
180 case DMBIN_ENVMANUAL : data.SetBin(wxPRINTBIN_ENVMANUAL ); break;
181 case DMBIN_AUTO : data.SetBin(wxPRINTBIN_AUTO ); break;
182 case DMBIN_TRACTOR : data.SetBin(wxPRINTBIN_TRACTOR ); break;
183 case DMBIN_SMALLFMT : data.SetBin(wxPRINTBIN_SMALLFMT ); break;
184 case DMBIN_LARGEFMT : data.SetBin(wxPRINTBIN_LARGEFMT ); break;
185 case DMBIN_LARGECAPACITY : data.SetBin(wxPRINTBIN_LARGECAPACITY ); break;
186 case DMBIN_CASSETTE : data.SetBin(wxPRINTBIN_CASSETTE ); break;
187 case DMBIN_FORMSOURCE : data.SetBin(wxPRINTBIN_FORMSOURCE ); break;
188 default:
189 if (devMode->dmDefaultSource >= DMBIN_USER)
190 data.SetBin((wxPrintBin)((devMode->dmDefaultSource)-DMBIN_USER+(int)wxPRINTBIN_USER));
191 else
192 data.SetBin(wxPRINTBIN_DEFAULT);
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 = 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 = static_cast<HGLOBAL>(m_devMode);
342 WinPrinter printer;
343 LPTSTR szPrinterName = (LPTSTR)data.GetPrinterName().wx_str();
344
345 // From MSDN: How To Modify Printer Settings with the DocumentProperties() Function
346 // The purpose of this is to fill the DEVMODE with privdata from printer driver.
347 // If we have a printer name and OpenPrinter sucessfully returns
348 // this replaces the PrintDlg function which creates the DEVMODE filled only with data from default printer.
349 if ( !m_devMode && !data.GetPrinterName().IsEmpty() )
350 {
351 // Open printer
352 if ( printer.Open( data.GetPrinterName() ) == TRUE )
353 {
354 DWORD dwNeeded, dwRet;
355
356 // Step 1:
357 // Allocate a buffer of the correct size.
358 dwNeeded = DocumentProperties( NULL,
359 printer, // Handle to our printer.
360 szPrinterName, // Name of the printer.
361 NULL, // Asking for size, so
362 NULL, // these are not used.
363 0 ); // Zero returns buffer size.
364
365 LPDEVMODE tempDevMode = static_cast<LPDEVMODE>( GlobalAlloc( GMEM_FIXED | GMEM_ZEROINIT, dwNeeded ) );
366
367 // Step 2:
368 // Get the default DevMode for the printer
369 dwRet = DocumentProperties( NULL,
370 printer,
371 szPrinterName,
372 tempDevMode, // The address of the buffer to fill.
373 NULL, // Not using the input buffer.
374 DM_OUT_BUFFER ); // Have the output buffer filled.
375
376 if ( dwRet != IDOK )
377 {
378 // If failure, cleanup
379 GlobalFree( tempDevMode );
380 printer.Close();
381 }
382 else
383 {
384 hDevMode = tempDevMode;
385 m_devMode = hDevMode;
386 tempDevMode = NULL;
387 }
388 }
389 }
390
391 if ( !m_devMode )
392 {
393 // Use PRINTDLG as a way of creating a DEVMODE object
394 PRINTDLG pd;
395
396 memset(&pd, 0, sizeof(PRINTDLG));
397 #ifdef __WXWINCE__
398 pd.cbStruct = sizeof(PRINTDLG);
399 #else
400 pd.lStructSize = sizeof(PRINTDLG);
401 #endif
402
403 pd.hwndOwner = NULL;
404 pd.hDevMode = NULL; // Will be created by PrintDlg
405 pd.hDevNames = NULL; // Ditto
406
407 pd.Flags = PD_RETURNDEFAULT;
408 pd.nCopies = 1;
409
410 // Fill out the DEVMODE structure
411 // so we can use it as input in the 'real' PrintDlg
412 if (!PrintDlg(&pd))
413 {
414 if ( pd.hDevMode )
415 GlobalFree(pd.hDevMode);
416 if ( pd.hDevNames )
417 GlobalFree(pd.hDevNames);
418 pd.hDevMode = NULL;
419 pd.hDevNames = NULL;
420
421 #if wxDEBUG_LEVEL
422 wxLogDebug(wxT("Printing error: ") + wxGetPrintDlgError());
423 #endif // wxDEBUG_LEVEL
424 }
425 else
426 {
427 hDevMode = pd.hDevMode;
428 m_devMode = hDevMode;
429 pd.hDevMode = NULL;
430
431 // We'll create a new DEVNAMEs structure below.
432 if ( pd.hDevNames )
433 GlobalFree(pd.hDevNames);
434 pd.hDevNames = NULL;
435
436 // hDevNames = pd->hDevNames;
437 // m_devNames = (void*)(long) hDevNames;
438 // pd->hDevnames = NULL;
439
440 }
441 }
442
443 if ( hDevMode )
444 {
445 GlobalPtrLock lockDevMode(hDevMode);
446 DEVMODE * const devMode = static_cast<DEVMODE *>(lockDevMode.Get());
447
448 //// Orientation
449 devMode->dmOrientation = (short)data.GetOrientation();
450
451 //// Collation
452 devMode->dmCollate = (data.GetCollate() ? DMCOLLATE_TRUE : DMCOLLATE_FALSE);
453 devMode->dmFields |= DM_COLLATE;
454
455 //// Number of copies
456 devMode->dmCopies = (short)data.GetNoCopies();
457 devMode->dmFields |= DM_COPIES;
458
459 //// Printer name
460 wxString name = data.GetPrinterName();
461 if (!name.empty())
462 {
463 // NB: the cast is needed in the ANSI build, strangely enough
464 // dmDeviceName is BYTE[] and not char[] there
465 wxStrlcpy(reinterpret_cast<wxChar *>(devMode->dmDeviceName),
466 name.wx_str(),
467 WXSIZEOF(devMode->dmDeviceName));
468 }
469
470 //// Colour
471 if (data.GetColour())
472 devMode->dmColor = DMCOLOR_COLOR;
473 else
474 devMode->dmColor = DMCOLOR_MONOCHROME;
475 devMode->dmFields |= DM_COLOR;
476
477 //// Paper size
478
479 // Paper id has priority over paper size. If id is specified, then size
480 // is ignored (as it can be filled in even for standard paper sizes)
481
482 wxPrintPaperType *paperType = NULL;
483
484 const wxPaperSize paperId = data.GetPaperId();
485 if ( paperId != wxPAPER_NONE && wxThePrintPaperDatabase )
486 {
487 paperType = wxThePrintPaperDatabase->FindPaperType(paperId);
488 }
489
490 if ( paperType )
491 {
492 devMode->dmPaperSize = (short)paperType->GetPlatformId();
493 devMode->dmFields |= DM_PAPERSIZE;
494 }
495 else // custom (or no) paper size
496 {
497 const wxSize paperSize = data.GetPaperSize();
498 if ( paperSize != wxDefaultSize )
499 {
500 // Fall back on specifying the paper size explicitly
501 if(m_customWindowsPaperId != 0)
502 devMode->dmPaperSize = m_customWindowsPaperId;
503 else
504 devMode->dmPaperSize = DMPAPER_USER;
505 devMode->dmPaperWidth = (short)(paperSize.x * 10);
506 devMode->dmPaperLength = (short)(paperSize.y * 10);
507 devMode->dmFields |= DM_PAPERWIDTH;
508 devMode->dmFields |= DM_PAPERLENGTH;
509
510 // A printer driver may or may not also want DM_PAPERSIZE to
511 // be specified. Also, if the printer driver doesn't implement the DMPAPER_USER
512 // size, then this won't work, and even if you found the correct id by
513 // enumerating the driver's paper sizes, it probably won't change the actual size,
514 // it'll just select that custom paper type with its own current setting.
515 // For a discussion on this, see http://www.codeguru.com/forum/showthread.php?threadid=458617
516 // Although m_customWindowsPaperId is intended to work around this, it's
517 // unclear how it can help you set the custom paper size programmatically.
518 }
519 //else: neither paper type nor size specified, don't fill DEVMODE
520 // at all so that the system defaults are used
521 }
522
523 //// Duplex
524 short duplex;
525 switch (data.GetDuplex())
526 {
527 case wxDUPLEX_HORIZONTAL:
528 duplex = DMDUP_HORIZONTAL;
529 break;
530 case wxDUPLEX_VERTICAL:
531 duplex = DMDUP_VERTICAL;
532 break;
533 default:
534 // in fact case wxDUPLEX_SIMPLEX:
535 duplex = DMDUP_SIMPLEX;
536 break;
537 }
538 devMode->dmDuplex = duplex;
539 devMode->dmFields |= DM_DUPLEX;
540
541 //// Quality
542
543 short quality;
544 switch (data.GetQuality())
545 {
546 case wxPRINT_QUALITY_MEDIUM:
547 quality = DMRES_MEDIUM;
548 break;
549 case wxPRINT_QUALITY_LOW:
550 quality = DMRES_LOW;
551 break;
552 case wxPRINT_QUALITY_DRAFT:
553 quality = DMRES_DRAFT;
554 break;
555 case wxPRINT_QUALITY_HIGH:
556 quality = DMRES_HIGH;
557 break;
558 default:
559 quality = (short)data.GetQuality();
560 devMode->dmYResolution = quality;
561 devMode->dmFields |= DM_YRESOLUTION;
562 break;
563 }
564 devMode->dmPrintQuality = quality;
565 devMode->dmFields |= DM_PRINTQUALITY;
566
567 if (data.GetPrivDataLen() > 0)
568 {
569 memcpy( (char *)devMode+devMode->dmSize, data.GetPrivData(), data.GetPrivDataLen() );
570 devMode->dmDriverExtra = (WXWORD)data.GetPrivDataLen();
571 }
572
573 if (data.GetBin() != wxPRINTBIN_DEFAULT)
574 {
575 switch (data.GetBin())
576 {
577 case wxPRINTBIN_ONLYONE: devMode->dmDefaultSource = DMBIN_ONLYONE; break;
578 case wxPRINTBIN_LOWER: devMode->dmDefaultSource = DMBIN_LOWER; break;
579 case wxPRINTBIN_MIDDLE: devMode->dmDefaultSource = DMBIN_MIDDLE; break;
580 case wxPRINTBIN_MANUAL: devMode->dmDefaultSource = DMBIN_MANUAL; break;
581 case wxPRINTBIN_ENVELOPE: devMode->dmDefaultSource = DMBIN_ENVELOPE; break;
582 case wxPRINTBIN_ENVMANUAL: devMode->dmDefaultSource = DMBIN_ENVMANUAL; break;
583 case wxPRINTBIN_AUTO: devMode->dmDefaultSource = DMBIN_AUTO; break;
584 case wxPRINTBIN_TRACTOR: devMode->dmDefaultSource = DMBIN_TRACTOR; break;
585 case wxPRINTBIN_SMALLFMT: devMode->dmDefaultSource = DMBIN_SMALLFMT; break;
586 case wxPRINTBIN_LARGEFMT: devMode->dmDefaultSource = DMBIN_LARGEFMT; break;
587 case wxPRINTBIN_LARGECAPACITY: devMode->dmDefaultSource = DMBIN_LARGECAPACITY; break;
588 case wxPRINTBIN_CASSETTE: devMode->dmDefaultSource = DMBIN_CASSETTE; break;
589 case wxPRINTBIN_FORMSOURCE: devMode->dmDefaultSource = DMBIN_FORMSOURCE; break;
590
591 default:
592 devMode->dmDefaultSource = (short)(DMBIN_USER + data.GetBin() - wxPRINTBIN_USER); // 256 + data.GetBin() - 14 = 242 + data.GetBin()
593 break;
594 }
595
596 devMode->dmFields |= DM_DEFAULTSOURCE;
597 }
598 if (data.GetMedia() != wxPRINTMEDIA_DEFAULT)
599 {
600 devMode->dmMediaType = data.GetMedia();
601 devMode->dmFields |= DM_MEDIATYPE;
602 }
603
604 if( printer )
605 {
606 // Step 3:
607 // Merge the new settings with the old.
608 // This gives the driver an opportunity to update any private
609 // portions of the DevMode structure.
610 DocumentProperties( NULL,
611 printer,
612 szPrinterName,
613 (LPDEVMODE)hDevMode, // Reuse our buffer for output.
614 (LPDEVMODE)hDevMode, // Pass the driver our changes
615 DM_IN_BUFFER | // Commands to Merge our changes and
616 DM_OUT_BUFFER ); // write the result.
617 }
618 }
619
620 if ( m_devNames )
621 {
622 ::GlobalFree(static_cast<HGLOBAL>(m_devNames));
623 }
624
625 // TODO: I hope it's OK to pass some empty strings to DEVNAMES.
626 m_devNames = wxCreateDevNames(wxEmptyString, data.GetPrinterName(), wxEmptyString);
627
628 return true;
629 }
630
631 // ---------------------------------------------------------------------------
632 // wxPrintDialog
633 // ---------------------------------------------------------------------------
634
635 IMPLEMENT_CLASS(wxWindowsPrintDialog, wxPrintDialogBase)
636
637 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintDialogData* data)
638 {
639 Create(p, data);
640 }
641
642 wxWindowsPrintDialog::wxWindowsPrintDialog(wxWindow *p, wxPrintData* data)
643 {
644 wxPrintDialogData data2;
645 if ( data )
646 data2 = *data;
647
648 Create(p, &data2);
649 }
650
651 bool wxWindowsPrintDialog::Create(wxWindow *p, wxPrintDialogData* data)
652 {
653 m_dialogParent = p;
654 m_printerDC = NULL;
655 m_destroyDC = true;
656
657 // MSW handle
658 m_printDlg = NULL;
659
660 if ( data )
661 m_printDialogData = *data;
662
663 return true;
664 }
665
666 wxWindowsPrintDialog::~wxWindowsPrintDialog()
667 {
668 PRINTDLG *pd = (PRINTDLG *) m_printDlg;
669 if (pd && pd->hDevMode)
670 GlobalFree(pd->hDevMode);
671 if ( pd )
672 delete pd;
673
674 if (m_destroyDC && m_printerDC)
675 delete m_printerDC;
676 }
677
678 int wxWindowsPrintDialog::ShowModal()
679 {
680 ConvertToNative( m_printDialogData );
681
682 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
683
684 if (m_dialogParent)
685 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
686 else if (wxTheApp->GetTopWindow())
687 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
688 else
689 pd->hwndOwner = 0;
690
691 bool ret = (PrintDlg( pd ) != 0);
692
693 pd->hwndOwner = 0;
694
695 if ( ret && (pd->hDC) )
696 {
697 wxPrinterDC *pdc = new wxPrinterDCFromHDC( (WXHDC) pd->hDC );
698 m_printerDC = pdc;
699 ConvertFromNative( m_printDialogData );
700 return wxID_OK;
701 }
702 else
703 {
704 return wxID_CANCEL;
705 }
706 }
707
708 wxDC *wxWindowsPrintDialog::GetPrintDC()
709 {
710 if (m_printerDC)
711 {
712 m_destroyDC = false;
713 return m_printerDC;
714 }
715 else
716 return NULL;
717 }
718
719 bool wxWindowsPrintDialog::ConvertToNative( wxPrintDialogData &data )
720 {
721 wxWindowsPrintNativeData *native_data =
722 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
723 data.GetPrintData().ConvertToNative();
724
725 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
726
727 // Shouldn't have been defined anywhere
728 if (pd)
729 return false;
730
731 pd = new PRINTDLG;
732 memset( pd, 0, sizeof(PRINTDLG) );
733 m_printDlg = (void*) pd;
734
735 pd->lStructSize = sizeof(PRINTDLG);
736 pd->hwndOwner = NULL;
737 pd->hDevMode = NULL; // Will be created by PrintDlg
738 pd->hDevNames = NULL; // Ditto
739
740 pd->Flags = PD_RETURNDEFAULT;
741 pd->nCopies = 1;
742
743 // Pass the devmode data to the PRINTDLG structure, since it'll
744 // be needed when PrintDlg is called.
745 if (pd->hDevMode)
746 GlobalFree(pd->hDevMode);
747
748 // Pass the devnames data to the PRINTDLG structure, since it'll
749 // be needed when PrintDlg is called.
750 if (pd->hDevNames)
751 GlobalFree(pd->hDevNames);
752
753 pd->hDevMode = static_cast<HGLOBAL>(native_data->GetDevMode());
754 native_data->SetDevMode(NULL);
755
756 // Shouldn't assert; we should be able to test Ok-ness at a higher level
757 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
758
759 pd->hDevNames = static_cast<HGLOBAL>(native_data->GetDevNames());
760 native_data->SetDevNames(NULL);
761
762
763 pd->hDC = NULL;
764 pd->nFromPage = (WORD)data.GetFromPage();
765 pd->nToPage = (WORD)data.GetToPage();
766 pd->nMinPage = (WORD)data.GetMinPage();
767 pd->nMaxPage = (WORD)data.GetMaxPage();
768 pd->nCopies = (WORD)data.GetNoCopies();
769
770 pd->Flags = PD_RETURNDC;
771 pd->lStructSize = sizeof( PRINTDLG );
772
773 pd->hwndOwner = NULL;
774 pd->hInstance = NULL;
775 pd->lCustData = 0;
776 pd->lpfnPrintHook = NULL;
777 pd->lpfnSetupHook = NULL;
778 pd->lpPrintTemplateName = NULL;
779 pd->lpSetupTemplateName = NULL;
780 pd->hPrintTemplate = NULL;
781 pd->hSetupTemplate = NULL;
782
783 if ( data.GetAllPages() )
784 pd->Flags |= PD_ALLPAGES;
785 if ( data.GetSelection() )
786 pd->Flags |= PD_SELECTION;
787 if ( data.GetCollate() )
788 pd->Flags |= PD_COLLATE;
789 if ( data.GetPrintToFile() )
790 pd->Flags |= PD_PRINTTOFILE;
791 if ( !data.GetEnablePrintToFile() )
792 pd->Flags |= PD_DISABLEPRINTTOFILE;
793 if ( !data.GetEnableSelection() )
794 pd->Flags |= PD_NOSELECTION;
795 if ( !data.GetEnablePageNumbers() )
796 pd->Flags |= PD_NOPAGENUMS;
797 else if ( (!data.GetAllPages()) && (!data.GetSelection()) && (data.GetFromPage() != 0) && (data.GetToPage() != 0))
798 pd->Flags |= PD_PAGENUMS;
799 if ( data.GetEnableHelp() )
800 pd->Flags |= PD_SHOWHELP;
801
802 return true;
803 }
804
805 bool wxWindowsPrintDialog::ConvertFromNative( wxPrintDialogData &data )
806 {
807 PRINTDLG *pd = (PRINTDLG*) m_printDlg;
808 if ( pd == NULL )
809 return false;
810
811 wxWindowsPrintNativeData *native_data =
812 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
813
814 // Pass the devmode data back to the wxPrintData structure where it really belongs.
815 if (pd->hDevMode)
816 {
817 if (native_data->GetDevMode())
818 {
819 ::GlobalFree(static_cast<HGLOBAL>(native_data->GetDevMode()));
820 }
821 native_data->SetDevMode(pd->hDevMode);
822 pd->hDevMode = NULL;
823 }
824
825 // Pass the devnames data back to the wxPrintData structure where it really belongs.
826 if (pd->hDevNames)
827 {
828 if (native_data->GetDevNames())
829 {
830 ::GlobalFree(static_cast<HGLOBAL>(native_data->GetDevNames()));
831 }
832 native_data->SetDevNames(pd->hDevNames);
833 pd->hDevNames = NULL;
834 }
835
836 // Now convert the DEVMODE object, passed down from the PRINTDLG object,
837 // into wxWidgets form.
838 native_data->TransferTo( data.GetPrintData() );
839
840 data.SetFromPage( pd->nFromPage );
841 data.SetToPage( pd->nToPage );
842 data.SetMinPage( pd->nMinPage );
843 data.SetMaxPage( pd->nMaxPage );
844 data.SetNoCopies( pd->nCopies );
845
846 data.SetAllPages( (((pd->Flags & PD_PAGENUMS) != PD_PAGENUMS) && ((pd->Flags & PD_SELECTION) != PD_SELECTION)) );
847 data.SetSelection( ((pd->Flags & PD_SELECTION) == PD_SELECTION) );
848 data.SetCollate( ((pd->Flags & PD_COLLATE) == PD_COLLATE) );
849 data.SetPrintToFile( ((pd->Flags & PD_PRINTTOFILE) == PD_PRINTTOFILE) );
850 data.EnablePrintToFile( ((pd->Flags & PD_DISABLEPRINTTOFILE) != PD_DISABLEPRINTTOFILE) );
851 data.EnableSelection( ((pd->Flags & PD_NOSELECTION) != PD_NOSELECTION) );
852 data.EnablePageNumbers( ((pd->Flags & PD_NOPAGENUMS) != PD_NOPAGENUMS) );
853 data.EnableHelp( ((pd->Flags & PD_SHOWHELP) == PD_SHOWHELP) );
854
855 return true;
856 }
857
858 // ---------------------------------------------------------------------------
859 // wxWidnowsPageSetupDialog
860 // ---------------------------------------------------------------------------
861
862 IMPLEMENT_CLASS(wxWindowsPageSetupDialog, wxPageSetupDialogBase)
863
864 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog()
865 {
866 m_dialogParent = NULL;
867 m_pageDlg = NULL;
868 }
869
870 wxWindowsPageSetupDialog::wxWindowsPageSetupDialog(wxWindow *p, wxPageSetupDialogData *data)
871 {
872 Create(p, data);
873 }
874
875 bool wxWindowsPageSetupDialog::Create(wxWindow *p, wxPageSetupDialogData *data)
876 {
877 m_dialogParent = p;
878 m_pageDlg = NULL;
879
880 if (data)
881 m_pageSetupData = (*data);
882
883 return true;
884 }
885
886 wxWindowsPageSetupDialog::~wxWindowsPageSetupDialog()
887 {
888 PAGESETUPDLG *pd = (PAGESETUPDLG *)m_pageDlg;
889 if ( pd && pd->hDevMode )
890 GlobalFree(pd->hDevMode);
891 if ( pd && pd->hDevNames )
892 GlobalFree(pd->hDevNames);
893 if ( pd )
894 delete pd;
895 }
896
897 int wxWindowsPageSetupDialog::ShowModal()
898 {
899 ConvertToNative( m_pageSetupData );
900
901 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
902 if (m_dialogParent)
903 pd->hwndOwner = (HWND) m_dialogParent->GetHWND();
904 else if (wxTheApp->GetTopWindow())
905 pd->hwndOwner = (HWND) wxTheApp->GetTopWindow()->GetHWND();
906 else
907 pd->hwndOwner = 0;
908 BOOL retVal = PageSetupDlg( pd ) ;
909 pd->hwndOwner = 0;
910 if (retVal)
911 {
912 ConvertFromNative( m_pageSetupData );
913 return wxID_OK;
914 }
915 else
916 return wxID_CANCEL;
917 }
918
919 bool wxWindowsPageSetupDialog::ConvertToNative( wxPageSetupDialogData &data )
920 {
921 wxWindowsPrintNativeData *native_data =
922 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
923 data.GetPrintData().ConvertToNative();
924
925 PAGESETUPDLG *pd = (PAGESETUPDLG*) m_pageDlg;
926
927 // Shouldn't have been defined anywhere
928 if (pd)
929 return false;
930
931 pd = new PAGESETUPDLG;
932 pd->hDevMode = NULL;
933 pd->hDevNames = NULL;
934 m_pageDlg = (void *)pd;
935
936 // Pass the devmode data (created in m_printData.ConvertToNative)
937 // to the PRINTDLG structure, since it'll
938 // be needed when PrintDlg is called.
939
940 if (pd->hDevMode)
941 {
942 GlobalFree(pd->hDevMode);
943 pd->hDevMode = NULL;
944 }
945 pd->hDevMode = (HGLOBAL) native_data->GetDevMode();
946 native_data->SetDevMode(NULL);
947
948 // Shouldn't assert; we should be able to test Ok-ness at a higher level
949 //wxASSERT_MSG( (pd->hDevMode), wxT("hDevMode must be non-NULL in ConvertToNative!"));
950
951 // Pass the devnames data (created in m_printData.ConvertToNative)
952 // to the PRINTDLG structure, since it'll
953 // be needed when PrintDlg is called.
954
955 if (pd->hDevNames)
956 {
957 GlobalFree(pd->hDevNames);
958 pd->hDevNames = NULL;
959 }
960 pd->hDevNames = (HGLOBAL) native_data->GetDevNames();
961 native_data->SetDevNames(NULL);
962
963 // pd->hDevMode = GlobalAlloc(GMEM_MOVEABLE, sizeof(DEVMODE));
964
965 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
966
967 if ( data.GetDefaultMinMargins() )
968 pd->Flags |= PSD_DEFAULTMINMARGINS;
969 if ( !data.GetEnableMargins() )
970 pd->Flags |= PSD_DISABLEMARGINS;
971 if ( !data.GetEnableOrientation() )
972 pd->Flags |= PSD_DISABLEORIENTATION;
973 if ( !data.GetEnablePaper() )
974 pd->Flags |= PSD_DISABLEPAPER;
975 if ( !data.GetEnablePrinter() )
976 pd->Flags |= PSD_DISABLEPRINTER;
977 if ( data.GetDefaultInfo() )
978 pd->Flags |= PSD_RETURNDEFAULT;
979 if ( data.GetEnableHelp() )
980 pd->Flags |= PSD_SHOWHELP;
981
982 // We want the units to be in hundredths of a millimetre
983 pd->Flags |= PSD_INHUNDREDTHSOFMILLIMETERS;
984
985 pd->lStructSize = sizeof( PAGESETUPDLG );
986 pd->hwndOwner = NULL;
987 pd->hInstance = NULL;
988 // PAGESETUPDLG is in hundreds of a mm
989 pd->ptPaperSize.x = data.GetPaperSize().x * 100;
990 pd->ptPaperSize.y = data.GetPaperSize().y * 100;
991
992 pd->rtMinMargin.left = data.GetMinMarginTopLeft().x * 100;
993 pd->rtMinMargin.top = data.GetMinMarginTopLeft().y * 100;
994 pd->rtMinMargin.right = data.GetMinMarginBottomRight().x * 100;
995 pd->rtMinMargin.bottom = data.GetMinMarginBottomRight().y * 100;
996
997 pd->rtMargin.left = data.GetMarginTopLeft().x * 100;
998 pd->rtMargin.top = data.GetMarginTopLeft().y * 100;
999 pd->rtMargin.right = data.GetMarginBottomRight().x * 100;
1000 pd->rtMargin.bottom = data.GetMarginBottomRight().y * 100;
1001
1002 pd->lCustData = 0;
1003 pd->lpfnPageSetupHook = NULL;
1004 pd->lpfnPagePaintHook = NULL;
1005 pd->hPageSetupTemplate = NULL;
1006 pd->lpPageSetupTemplateName = NULL;
1007
1008 /*
1009 if ( pd->hDevMode )
1010 {
1011 DEVMODE *devMode = (DEVMODE*) GlobalLock(pd->hDevMode);
1012 memset(devMode, 0, sizeof(DEVMODE));
1013 devMode->dmSize = sizeof(DEVMODE);
1014 devMode->dmOrientation = m_orientation;
1015 devMode->dmFields = DM_ORIENTATION;
1016 GlobalUnlock(pd->hDevMode);
1017 }
1018 */
1019 return true;
1020 }
1021
1022 bool wxWindowsPageSetupDialog::ConvertFromNative( wxPageSetupDialogData &data )
1023 {
1024 PAGESETUPDLG *pd = (PAGESETUPDLG *) m_pageDlg;
1025 if ( !pd )
1026 return false;
1027
1028 wxWindowsPrintNativeData *native_data =
1029 (wxWindowsPrintNativeData *) data.GetPrintData().GetNativeData();
1030
1031 // Pass the devmode data back to the wxPrintData structure where it really belongs.
1032 if (pd->hDevMode)
1033 {
1034 if (native_data->GetDevMode())
1035 {
1036 // Make sure we don't leak memory
1037 GlobalFree((HGLOBAL) native_data->GetDevMode());
1038 }
1039 native_data->SetDevMode( (void*) pd->hDevMode );
1040 pd->hDevMode = NULL;
1041 }
1042
1043 // Isn't this superfluous? It's called again below.
1044 // data.GetPrintData().ConvertFromNative();
1045
1046 // Pass the devnames data back to the wxPrintData structure where it really belongs.
1047 if (pd->hDevNames)
1048 {
1049 if (native_data->GetDevNames())
1050 {
1051 // Make sure we don't leak memory
1052 GlobalFree((HGLOBAL) native_data->GetDevNames());
1053 }
1054 native_data->SetDevNames((void*) pd->hDevNames);
1055 pd->hDevNames = NULL;
1056 }
1057
1058 data.GetPrintData().ConvertFromNative();
1059
1060 pd->Flags = PSD_MARGINS|PSD_MINMARGINS;
1061
1062 data.SetDefaultMinMargins( ((pd->Flags & PSD_DEFAULTMINMARGINS) == PSD_DEFAULTMINMARGINS) );
1063 data.EnableMargins( ((pd->Flags & PSD_DISABLEMARGINS) != PSD_DISABLEMARGINS) );
1064 data.EnableOrientation( ((pd->Flags & PSD_DISABLEORIENTATION) != PSD_DISABLEORIENTATION) );
1065 data.EnablePaper( ((pd->Flags & PSD_DISABLEPAPER) != PSD_DISABLEPAPER) );
1066 data.EnablePrinter( ((pd->Flags & PSD_DISABLEPRINTER) != PSD_DISABLEPRINTER) );
1067 data.SetDefaultInfo( ((pd->Flags & PSD_RETURNDEFAULT) == PSD_RETURNDEFAULT) );
1068 data.EnableHelp( ((pd->Flags & PSD_SHOWHELP) == PSD_SHOWHELP) );
1069
1070 // PAGESETUPDLG is in hundreds of a mm
1071 if (data.GetPrintData().GetOrientation() == wxLANDSCAPE)
1072 data.SetPaperSize( wxSize(pd->ptPaperSize.y / 100, pd->ptPaperSize.x / 100) );
1073 else
1074 data.SetPaperSize( wxSize(pd->ptPaperSize.x / 100, pd->ptPaperSize.y / 100) );
1075
1076 data.SetMinMarginTopLeft( wxPoint(pd->rtMinMargin.left / 100, pd->rtMinMargin.top / 100) );
1077 data.SetMinMarginBottomRight( wxPoint(pd->rtMinMargin.right / 100, pd->rtMinMargin.bottom / 100) );
1078
1079 data.SetMarginTopLeft( wxPoint(pd->rtMargin.left / 100, pd->rtMargin.top / 100) );
1080 data.SetMarginBottomRight( wxPoint(pd->rtMargin.right / 100, pd->rtMargin.bottom / 100) );
1081
1082 return true;
1083 }
1084
1085 #endif
1086 // wxUSE_PRINTING_ARCHITECTURE