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