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