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