]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/msw/dcprint.cpp | |
3 | // Purpose: wxPrinterDC class | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // Copyright: (c) Julian Smart | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // For compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_PRINTING_ARCHITECTURE | |
27 | ||
28 | #include "wx/dcprint.h" | |
29 | #include "wx/msw/dcprint.h" | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/msw/wrapcdlg.h" | |
33 | #include "wx/string.h" | |
34 | #include "wx/log.h" | |
35 | #include "wx/window.h" | |
36 | #include "wx/dcmemory.h" | |
37 | #include "wx/math.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/msw/private.h" | |
41 | ||
42 | #if wxUSE_WXDIB | |
43 | #include "wx/msw/dib.h" | |
44 | #endif | |
45 | ||
46 | #include "wx/printdlg.h" | |
47 | #include "wx/msw/printdlg.h" | |
48 | ||
49 | #ifndef __WIN32__ | |
50 | #include <print.h> | |
51 | #endif | |
52 | ||
53 | // mingw32 defines GDI_ERROR incorrectly | |
54 | #if defined(__GNUWIN32__) || !defined(GDI_ERROR) | |
55 | #undef GDI_ERROR | |
56 | #define GDI_ERROR ((int)-1) | |
57 | #endif | |
58 | ||
59 | #if defined(__WXUNIVERSAL__) && wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW | |
60 | #define wxUSE_PS_PRINTING 1 | |
61 | #else | |
62 | #define wxUSE_PS_PRINTING 0 | |
63 | #endif | |
64 | ||
65 | // ---------------------------------------------------------------------------- | |
66 | // wxWin macros | |
67 | // ---------------------------------------------------------------------------- | |
68 | ||
69 | IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl, wxMSWDCImpl) | |
70 | ||
71 | // ============================================================================ | |
72 | // implementation | |
73 | // ============================================================================ | |
74 | ||
75 | // ---------------------------------------------------------------------------- | |
76 | // wxPrinterDC construction | |
77 | // ---------------------------------------------------------------------------- | |
78 | ||
79 | #if 0 | |
80 | // This form is deprecated | |
81 | wxPrinterDC::wxPrinterDC(const wxString& driver_name, | |
82 | const wxString& device_name, | |
83 | const wxString& file, | |
84 | bool interactive, | |
85 | wxPrintOrientation orientation) | |
86 | { | |
87 | m_isInteractive = interactive; | |
88 | ||
89 | if ( !file.empty() ) | |
90 | m_printData.SetFilename(file); | |
91 | ||
92 | #if wxUSE_COMMON_DIALOGS | |
93 | if ( interactive ) | |
94 | { | |
95 | PRINTDLG pd; | |
96 | ||
97 | pd.lStructSize = sizeof( PRINTDLG ); | |
98 | pd.hwndOwner = (HWND) NULL; | |
99 | pd.hDevMode = (HANDLE)NULL; | |
100 | pd.hDevNames = (HANDLE)NULL; | |
101 | pd.Flags = PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS; | |
102 | pd.nFromPage = 0; | |
103 | pd.nToPage = 0; | |
104 | pd.nMinPage = 0; | |
105 | pd.nMaxPage = 0; | |
106 | pd.nCopies = 1; | |
107 | pd.hInstance = (HINSTANCE)NULL; | |
108 | ||
109 | m_ok = PrintDlg( &pd ) != 0; | |
110 | if ( m_ok ) | |
111 | { | |
112 | m_hDC = (WXHDC) pd.hDC; | |
113 | } | |
114 | } | |
115 | else | |
116 | #endif // wxUSE_COMMON_DIALOGS | |
117 | { | |
118 | if ( !driver_name.empty() && !device_name.empty() && !file.empty() ) | |
119 | { | |
120 | m_hDC = (WXHDC) CreateDC(driver_name.t_str(), | |
121 | device_name.t_str(), | |
122 | file.fn_str(), | |
123 | NULL); | |
124 | } | |
125 | else // we don't have all parameters, ask the user | |
126 | { | |
127 | wxPrintData printData; | |
128 | printData.SetOrientation(orientation); | |
129 | m_hDC = wxGetPrinterDC(printData); | |
130 | } | |
131 | ||
132 | m_ok = m_hDC ? true: false; | |
133 | ||
134 | // as we created it, we must delete it as well | |
135 | m_bOwnsDC = true; | |
136 | } | |
137 | ||
138 | Init(); | |
139 | } | |
140 | #endif | |
141 | ||
142 | wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printData ) : | |
143 | wxMSWDCImpl( owner ) | |
144 | { | |
145 | m_printData = printData; | |
146 | ||
147 | m_isInteractive = false; | |
148 | ||
149 | m_hDC = wxGetPrinterDC(printData); | |
150 | m_ok = m_hDC != 0; | |
151 | m_bOwnsDC = true; | |
152 | ||
153 | Init(); | |
154 | } | |
155 | ||
156 | ||
157 | wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, WXHDC dc ) : | |
158 | wxMSWDCImpl( owner ) | |
159 | { | |
160 | m_isInteractive = false; | |
161 | ||
162 | m_hDC = dc; | |
163 | m_bOwnsDC = true; | |
164 | m_ok = true; | |
165 | } | |
166 | ||
167 | void wxPrinterDCImpl::Init() | |
168 | { | |
169 | if ( m_hDC ) | |
170 | { | |
171 | // int width = GetDeviceCaps(m_hDC, VERTRES); | |
172 | // int height = GetDeviceCaps(m_hDC, HORZRES); | |
173 | SetMapMode(wxMM_TEXT); | |
174 | ||
175 | SetBrush(*wxBLACK_BRUSH); | |
176 | SetPen(*wxBLACK_PEN); | |
177 | } | |
178 | } | |
179 | ||
180 | // ---------------------------------------------------------------------------- | |
181 | // wxPrinterDCImpl {Start/End}{Page/Doc} methods | |
182 | // ---------------------------------------------------------------------------- | |
183 | ||
184 | bool wxPrinterDCImpl::StartDoc(const wxString& message) | |
185 | { | |
186 | DOCINFO docinfo; | |
187 | docinfo.cbSize = sizeof(DOCINFO); | |
188 | docinfo.lpszDocName = message.t_str(); | |
189 | ||
190 | wxString filename(m_printData.GetFilename()); | |
191 | ||
192 | if (filename.empty()) | |
193 | docinfo.lpszOutput = NULL; | |
194 | else | |
195 | docinfo.lpszOutput = filename.t_str(); | |
196 | ||
197 | docinfo.lpszDatatype = NULL; | |
198 | docinfo.fwType = 0; | |
199 | ||
200 | if (!m_hDC) | |
201 | return false; | |
202 | ||
203 | if ( ::StartDoc(GetHdc(), &docinfo) <= 0 ) | |
204 | { | |
205 | wxLogLastError(wxT("StartDoc")); | |
206 | return false; | |
207 | } | |
208 | ||
209 | return true; | |
210 | } | |
211 | ||
212 | void wxPrinterDCImpl::EndDoc() | |
213 | { | |
214 | if (m_hDC) ::EndDoc((HDC) m_hDC); | |
215 | } | |
216 | ||
217 | void wxPrinterDCImpl::StartPage() | |
218 | { | |
219 | if (m_hDC) | |
220 | ::StartPage((HDC) m_hDC); | |
221 | } | |
222 | ||
223 | void wxPrinterDCImpl::EndPage() | |
224 | { | |
225 | if (m_hDC) | |
226 | ::EndPage((HDC) m_hDC); | |
227 | } | |
228 | ||
229 | ||
230 | wxRect wxPrinterDCImpl::GetPaperRect() const | |
231 | ||
232 | { | |
233 | if (!IsOk()) return wxRect(0, 0, 0, 0); | |
234 | int w = ::GetDeviceCaps((HDC) m_hDC, PHYSICALWIDTH); | |
235 | int h = ::GetDeviceCaps((HDC) m_hDC, PHYSICALHEIGHT); | |
236 | int x = -::GetDeviceCaps((HDC) m_hDC, PHYSICALOFFSETX); | |
237 | int y = -::GetDeviceCaps((HDC) m_hDC, PHYSICALOFFSETY); | |
238 | return wxRect(x, y, w, h); | |
239 | } | |
240 | ||
241 | ||
242 | #if !wxUSE_PS_PRINTING | |
243 | ||
244 | // Returns default device and port names | |
245 | static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName) | |
246 | { | |
247 | deviceName.clear(); | |
248 | ||
249 | LPDEVNAMES lpDevNames; | |
250 | LPTSTR lpszDeviceName; | |
251 | LPTSTR lpszPortName; | |
252 | ||
253 | PRINTDLG pd; | |
254 | ||
255 | // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
256 | #ifdef __GNUWIN32__ | |
257 | memset(&pd, 0, 66); | |
258 | pd.lStructSize = 66; // sizeof(PRINTDLG); | |
259 | #else | |
260 | memset(&pd, 0, sizeof(PRINTDLG)); | |
261 | pd.lStructSize = sizeof(PRINTDLG); | |
262 | #endif | |
263 | ||
264 | pd.hwndOwner = (HWND)NULL; | |
265 | pd.hDevMode = NULL; // Will be created by PrintDlg | |
266 | pd.hDevNames = NULL; // Ditto | |
267 | pd.Flags = PD_RETURNDEFAULT; | |
268 | pd.nCopies = 1; | |
269 | ||
270 | if (!PrintDlg((LPPRINTDLG)&pd)) | |
271 | { | |
272 | if ( pd.hDevMode ) | |
273 | GlobalFree(pd.hDevMode); | |
274 | if (pd.hDevNames) | |
275 | GlobalFree(pd.hDevNames); | |
276 | ||
277 | return false; | |
278 | } | |
279 | ||
280 | if (pd.hDevNames) | |
281 | { | |
282 | lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames); | |
283 | lpszDeviceName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset; | |
284 | lpszPortName = (LPTSTR)lpDevNames + lpDevNames->wOutputOffset; | |
285 | ||
286 | deviceName = lpszDeviceName; | |
287 | portName = lpszPortName; | |
288 | ||
289 | GlobalUnlock(pd.hDevNames); | |
290 | GlobalFree(pd.hDevNames); | |
291 | pd.hDevNames=NULL; | |
292 | } | |
293 | ||
294 | if (pd.hDevMode) | |
295 | { | |
296 | GlobalFree(pd.hDevMode); | |
297 | pd.hDevMode=NULL; | |
298 | } | |
299 | return ( !deviceName.empty() ); | |
300 | } | |
301 | ||
302 | #endif // !wxUSE_PS_PRINTING | |
303 | ||
304 | // Gets an HDC for the specified printer configuration | |
305 | WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst) | |
306 | { | |
307 | #if wxUSE_PS_PRINTING | |
308 | // TODO | |
309 | wxUnusedVar(printDataConst); | |
310 | return 0; | |
311 | #else // native Windows printing | |
312 | wxWindowsPrintNativeData *data = | |
313 | (wxWindowsPrintNativeData *) printDataConst.GetNativeData(); | |
314 | ||
315 | data->TransferFrom( printDataConst ); | |
316 | ||
317 | wxString deviceName = printDataConst.GetPrinterName(); | |
318 | if ( deviceName.empty() ) | |
319 | { | |
320 | // Retrieve the default device name | |
321 | wxString portName; | |
322 | if ( !wxGetDefaultDeviceName(deviceName, portName) ) | |
323 | { | |
324 | return 0; // Could not get default device name | |
325 | } | |
326 | } | |
327 | ||
328 | ||
329 | GlobalPtrLock lockDevMode; | |
330 | const HGLOBAL devMode = data->GetDevMode(); | |
331 | if ( devMode ) | |
332 | lockDevMode.Init(devMode); | |
333 | ||
334 | HDC hDC = ::CreateDC | |
335 | ( | |
336 | NULL, // no driver name as we use device name | |
337 | deviceName.t_str(), | |
338 | NULL, // unused | |
339 | static_cast<DEVMODE *>(lockDevMode.Get()) | |
340 | ); | |
341 | if ( !hDC ) | |
342 | { | |
343 | wxLogLastError(wxT("CreateDC(printer)")); | |
344 | } | |
345 | ||
346 | return (WXHDC) hDC; | |
347 | #endif // PostScript/Windows printing | |
348 | } | |
349 | ||
350 | // ---------------------------------------------------------------------------- | |
351 | // wxPrinterDCImpl bit blitting/bitmap drawing | |
352 | // ---------------------------------------------------------------------------- | |
353 | ||
354 | // helper of DoDrawBitmap() and DoBlit() | |
355 | static | |
356 | bool DrawBitmapUsingStretchDIBits(HDC hdc, | |
357 | const wxBitmap& bmp, | |
358 | wxCoord x, wxCoord y) | |
359 | { | |
360 | #if wxUSE_WXDIB | |
361 | wxDIB dib(bmp); | |
362 | bool ok = dib.IsOk(); | |
363 | if ( !ok ) | |
364 | return false; | |
365 | ||
366 | DIBSECTION ds; | |
367 | if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) ) | |
368 | { | |
369 | wxLogLastError(wxT("GetObject(DIBSECTION)")); | |
370 | ||
371 | return false; | |
372 | } | |
373 | ||
374 | // ok, we've got all data we need, do blit it | |
375 | if ( ::StretchDIBits | |
376 | ( | |
377 | hdc, | |
378 | x, y, | |
379 | ds.dsBmih.biWidth, ds.dsBmih.biHeight, | |
380 | 0, 0, | |
381 | ds.dsBmih.biWidth, ds.dsBmih.biHeight, | |
382 | ds.dsBm.bmBits, | |
383 | (LPBITMAPINFO)&ds.dsBmih, | |
384 | DIB_RGB_COLORS, | |
385 | SRCCOPY | |
386 | ) == GDI_ERROR ) | |
387 | { | |
388 | wxLogLastError(wxT("StretchDIBits")); | |
389 | ||
390 | return false; | |
391 | } | |
392 | ||
393 | return true; | |
394 | #else | |
395 | return false; | |
396 | #endif | |
397 | } | |
398 | ||
399 | void wxPrinterDCImpl::DoDrawBitmap(const wxBitmap& bmp, | |
400 | wxCoord x, wxCoord y, | |
401 | bool useMask) | |
402 | { | |
403 | wxCHECK_RET( bmp.IsOk(), wxT("invalid bitmap in wxPrinterDC::DrawBitmap") ); | |
404 | ||
405 | int width = bmp.GetWidth(), | |
406 | height = bmp.GetHeight(); | |
407 | ||
408 | if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) || | |
409 | !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, x, y) ) | |
410 | { | |
411 | // no support for StretchDIBits() or an error occurred if we got here | |
412 | wxMemoryDC memDC; | |
413 | ||
414 | memDC.SelectObjectAsSource(bmp); | |
415 | ||
416 | GetOwner()->Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask); | |
417 | ||
418 | memDC.SelectObject(wxNullBitmap); | |
419 | } | |
420 | } | |
421 | ||
422 | bool wxPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest, | |
423 | wxCoord width, wxCoord height, | |
424 | wxDC *source, | |
425 | wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc), | |
426 | wxRasterOperationMode WXUNUSED(rop), bool useMask, | |
427 | wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask)) | |
428 | { | |
429 | wxDCImpl *impl = source->GetImpl(); | |
430 | wxMSWDCImpl *msw_impl = wxDynamicCast(impl, wxMSWDCImpl); | |
431 | if (!msw_impl) | |
432 | return false; | |
433 | ||
434 | wxBitmap& bmp = msw_impl->GetSelectedBitmap(); | |
435 | wxMask *mask = useMask ? bmp.GetMask() : NULL; | |
436 | if ( mask ) | |
437 | { | |
438 | // If we are printing source colours are screen colours not printer | |
439 | // colours and so we need copy the bitmap pixel by pixel. | |
440 | RECT rect; | |
441 | HDC dcSrc = GetHdcOf(*msw_impl); | |
442 | MemoryHDC dcMask(dcSrc); | |
443 | SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap()); | |
444 | ||
445 | for (int x = 0; x < width; x++) | |
446 | { | |
447 | for (int y = 0; y < height; y++) | |
448 | { | |
449 | COLORREF cref = ::GetPixel(dcMask, x, y); | |
450 | if (cref) | |
451 | { | |
452 | HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y)); | |
453 | rect.left = xdest + x; | |
454 | rect.right = rect.left + 1; | |
455 | rect.top = ydest + y; | |
456 | rect.bottom = rect.top + 1; | |
457 | ::FillRect(GetHdc(), &rect, brush); | |
458 | ::DeleteObject(brush); | |
459 | } | |
460 | } | |
461 | } | |
462 | } | |
463 | else // no mask | |
464 | { | |
465 | if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) || | |
466 | !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) ) | |
467 | { | |
468 | // no support for StretchDIBits | |
469 | ||
470 | // as we are printing, source colours are screen colours not | |
471 | // printer colours and so we need copy the bitmap pixel by pixel. | |
472 | HDC dcSrc = GetHdcOf(*msw_impl); | |
473 | RECT rect; | |
474 | for (int y = 0; y < height; y++) | |
475 | { | |
476 | // optimization: draw identical adjacent pixels together. | |
477 | for (int x = 0; x < width; x++) | |
478 | { | |
479 | COLORREF col = ::GetPixel(dcSrc, x, y); | |
480 | HBRUSH brush = ::CreateSolidBrush( col ); | |
481 | ||
482 | rect.left = xdest + x; | |
483 | rect.top = ydest + y; | |
484 | while( (x + 1 < width) && | |
485 | (::GetPixel(dcSrc, x + 1, y) == col ) ) | |
486 | { | |
487 | ++x; | |
488 | } | |
489 | rect.right = xdest + x + 1; | |
490 | rect.bottom = rect.top + 1; | |
491 | ::FillRect((HDC) m_hDC, &rect, brush); | |
492 | ::DeleteObject(brush); | |
493 | } | |
494 | } | |
495 | } | |
496 | } | |
497 | ||
498 | return true; | |
499 | } | |
500 | ||
501 | #endif | |
502 | // wxUSE_PRINTING_ARCHITECTURE |