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