]>
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" |
0c589ad0 BM |
41 | #include "wx/dcprint.h" |
42 | #include "math.h" | |
2bda0e17 | 43 | |
b4da152e | 44 | #if wxUSE_COMMON_DIALOGS |
4b7f2165 | 45 | #include <commdlg.h> |
2bda0e17 KB |
46 | #endif |
47 | ||
48 | #ifndef __WIN32__ | |
4b7f2165 | 49 | #include <print.h> |
2bda0e17 KB |
50 | #endif |
51 | ||
3c1a88d8 VZ |
52 | // mingw32 defines GDI_ERROR incorrectly |
53 | #ifdef __GNUWIN32__ | |
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 | |
7ba4fbeb VZ |
121 | m_ok = m_hDC ? TRUE: FALSE; |
122 | ||
123 | // as we created it, we must delete it as well | |
124 | m_bOwnsDC = TRUE; | |
125 | } | |
126 | ||
127 | Init(); | |
7bcb11d3 JS |
128 | } |
129 | ||
130 | wxPrinterDC::wxPrinterDC(const wxPrintData& printData) | |
131 | { | |
132 | m_printData = printData; | |
133 | ||
134 | m_isInteractive = FALSE; | |
135 | ||
136 | m_hDC = wxGetPrinterDC(printData); | |
7ba4fbeb VZ |
137 | m_ok = m_hDC != 0; |
138 | m_bOwnsDC = TRUE; | |
a17e237f | 139 | |
7ba4fbeb | 140 | Init(); |
2bda0e17 KB |
141 | } |
142 | ||
7bcb11d3 | 143 | |
7ba4fbeb | 144 | wxPrinterDC::wxPrinterDC(WXHDC dc) |
2bda0e17 | 145 | { |
7bcb11d3 | 146 | m_isInteractive = FALSE; |
a17e237f | 147 | |
7ba4fbeb VZ |
148 | m_hDC = dc; |
149 | m_bOwnsDC = TRUE; | |
7bcb11d3 | 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 | ||
178 | if (filename.IsEmpty()) | |
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 JS |
188 | if (!m_hDC) |
189 | return FALSE; | |
a17e237f | 190 | |
4b7f2165 | 191 | int ret = ::StartDoc(GetHdc(), &docinfo); |
a17e237f | 192 | |
7bcb11d3 JS |
193 | #ifndef __WIN16__ |
194 | if (ret <= 0) | |
195 | { | |
196 | DWORD lastError = GetLastError(); | |
9b601c24 | 197 | wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError); |
7bcb11d3 JS |
198 | } |
199 | #endif | |
a17e237f | 200 | |
7bcb11d3 JS |
201 | return (ret > 0); |
202 | } | |
203 | ||
4b7f2165 | 204 | void wxPrinterDC::EndDoc() |
7bcb11d3 JS |
205 | { |
206 | if (m_hDC) ::EndDoc((HDC) m_hDC); | |
207 | } | |
208 | ||
4b7f2165 | 209 | void wxPrinterDC::StartPage() |
7bcb11d3 JS |
210 | { |
211 | if (m_hDC) | |
212 | ::StartPage((HDC) m_hDC); | |
213 | } | |
214 | ||
4b7f2165 | 215 | void wxPrinterDC::EndPage() |
7bcb11d3 JS |
216 | { |
217 | if (m_hDC) | |
218 | ::EndPage((HDC) m_hDC); | |
219 | } | |
220 | ||
221 | // Returns default device and port names | |
222 | static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName) | |
223 | { | |
7ba4fbeb | 224 | deviceName.clear(); |
7bcb11d3 JS |
225 | |
226 | LPDEVNAMES lpDevNames; | |
161f4f73 VZ |
227 | LPTSTR lpszDriverName; |
228 | LPTSTR lpszDeviceName; | |
229 | LPTSTR lpszPortName; | |
a17e237f | 230 | |
7bcb11d3 JS |
231 | PRINTDLG pd; |
232 | ||
233 | // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
234 | #ifdef __GNUWIN32__ | |
161f4f73 | 235 | memset(&pd, 0, 66); |
7bcb11d3 JS |
236 | pd.lStructSize = 66; // sizeof(PRINTDLG); |
237 | #else | |
161f4f73 | 238 | memset(&pd, 0, sizeof(PRINTDLG)); |
7bcb11d3 JS |
239 | pd.lStructSize = sizeof(PRINTDLG); |
240 | #endif | |
241 | ||
242 | pd.hwndOwner = (HWND)NULL; | |
243 | pd.hDevMode = NULL; // Will be created by PrintDlg | |
244 | pd.hDevNames = NULL; // Ditto | |
245 | pd.Flags = PD_RETURNDEFAULT; | |
246 | pd.nCopies = 1; | |
a17e237f | 247 | |
7bcb11d3 JS |
248 | if (!PrintDlg((LPPRINTDLG)&pd)) |
249 | { | |
250 | if ( pd.hDevMode ) | |
251 | GlobalFree(pd.hDevMode); | |
252 | if (pd.hDevNames) | |
253 | GlobalFree(pd.hDevNames); | |
a17e237f | 254 | |
7bcb11d3 JS |
255 | return FALSE; |
256 | } | |
a17e237f | 257 | |
7bcb11d3 JS |
258 | if (pd.hDevNames) |
259 | { | |
260 | lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames); | |
161f4f73 VZ |
261 | lpszDriverName = (LPTSTR)lpDevNames + lpDevNames->wDriverOffset; |
262 | lpszDeviceName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset; | |
263 | lpszPortName = (LPTSTR)lpDevNames + lpDevNames->wOutputOffset; | |
7bcb11d3 JS |
264 | |
265 | deviceName = lpszDeviceName; | |
266 | portName = lpszPortName; | |
60fe7303 JS |
267 | |
268 | GlobalUnlock(pd.hDevNames); | |
269 | GlobalFree(pd.hDevNames); | |
270 | pd.hDevNames=NULL; | |
7bcb11d3 | 271 | } |
a17e237f | 272 | |
7bcb11d3 JS |
273 | if (pd.hDevMode) |
274 | { | |
275 | GlobalFree(pd.hDevMode); | |
276 | pd.hDevMode=NULL; | |
277 | } | |
223d09f6 | 278 | return ( deviceName != wxT("") ); |
7bcb11d3 JS |
279 | } |
280 | ||
281 | #if 0 | |
282 | // This uses defaults, except for orientation, so we should eliminate this function | |
283 | // and use the 2nd form (passing wxPrintData) instead. | |
2bda0e17 KB |
284 | WXHDC wxGetPrinterDC(int orientation) |
285 | { | |
286 | HDC hDC; | |
287 | LPDEVMODE lpDevMode = NULL; | |
288 | LPDEVNAMES lpDevNames; | |
289 | LPSTR lpszDriverName; | |
290 | LPSTR lpszDeviceName; | |
291 | LPSTR lpszPortName; | |
a17e237f | 292 | |
2bda0e17 KB |
293 | PRINTDLG pd; |
294 | // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
7bcb11d3 | 295 | #ifdef __GNUWIN32__ |
2bda0e17 | 296 | pd.lStructSize = 66; // sizeof(PRINTDLG); |
7bcb11d3 JS |
297 | #else |
298 | pd.lStructSize = sizeof(PRINTDLG); | |
299 | #endif | |
2bda0e17 KB |
300 | pd.hwndOwner = (HWND)NULL; |
301 | pd.hDevMode = NULL; // Will be created by PrintDlg | |
302 | pd.hDevNames = NULL; // Ditto | |
303 | pd.Flags = PD_RETURNDEFAULT; | |
304 | pd.nCopies = 1; | |
a17e237f | 305 | |
2bda0e17 KB |
306 | if (!PrintDlg((LPPRINTDLG)&pd)) |
307 | { | |
308 | if ( pd.hDevMode ) | |
309 | GlobalFree(pd.hDevMode); | |
310 | if (pd.hDevNames) | |
311 | GlobalFree(pd.hDevNames); | |
a17e237f | 312 | |
2bda0e17 KB |
313 | return(0); |
314 | } | |
a17e237f | 315 | |
2bda0e17 KB |
316 | if (!pd.hDevNames) |
317 | { | |
318 | if ( pd.hDevMode ) | |
319 | GlobalFree(pd.hDevMode); | |
320 | } | |
a17e237f | 321 | |
2bda0e17 KB |
322 | lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames); |
323 | lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset; | |
324 | lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset; | |
325 | lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset; | |
326 | GlobalUnlock(pd.hDevNames); | |
a17e237f | 327 | |
2bda0e17 KB |
328 | if ( pd.hDevMode ) |
329 | { | |
330 | lpDevMode = (DEVMODE*) GlobalLock(pd.hDevMode); | |
331 | lpDevMode->dmOrientation = orientation; | |
332 | lpDevMode->dmFields |= DM_ORIENTATION; | |
333 | } | |
a17e237f | 334 | |
2bda0e17 KB |
335 | #ifdef __WIN32__ |
336 | hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (DEVMODE *)lpDevMode); | |
337 | #else | |
338 | hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode); | |
339 | #endif | |
a17e237f | 340 | |
2bda0e17 KB |
341 | if (pd.hDevMode && lpDevMode) |
342 | GlobalUnlock(pd.hDevMode); | |
a17e237f | 343 | |
2bda0e17 KB |
344 | if (pd.hDevNames) |
345 | { | |
7bcb11d3 JS |
346 | GlobalFree(pd.hDevNames); |
347 | pd.hDevNames=NULL; | |
2bda0e17 KB |
348 | } |
349 | if (pd.hDevMode) | |
350 | { | |
351 | GlobalFree(pd.hDevMode); | |
352 | pd.hDevMode=NULL; | |
353 | } | |
354 | return (WXHDC) hDC; | |
355 | } | |
7ba4fbeb | 356 | #endif // 0 |
7bcb11d3 JS |
357 | |
358 | // Gets an HDC for the specified printer configuration | |
359 | WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst) | |
360 | { | |
361 | wxPrintData printData = printDataConst; | |
362 | printData.ConvertToNative(); | |
a17e237f | 363 | |
837e5743 | 364 | wxChar* driverName = (wxChar*) NULL; |
a17e237f | 365 | |
7bcb11d3 | 366 | wxString devNameStr = printData.GetPrinterName(); |
837e5743 | 367 | wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32 |
a17e237f | 368 | |
4b7f2165 VZ |
369 | const wxChar* deviceName; |
370 | if ( !devNameStr ) | |
837e5743 | 371 | deviceName = (wxChar*) NULL; |
7bcb11d3 | 372 | else |
4b7f2165 | 373 | deviceName = devNameStr.c_str(); |
7bcb11d3 JS |
374 | |
375 | LPDEVMODE lpDevMode = (LPDEVMODE) NULL; | |
376 | ||
48c12cb1 | 377 | HGLOBAL hDevMode = (HGLOBAL)(DWORD) printData.GetNativeData(); |
7bcb11d3 JS |
378 | |
379 | if ( hDevMode ) | |
380 | lpDevMode = (DEVMODE*) GlobalLock(hDevMode); | |
381 | ||
4b7f2165 | 382 | if ( !devNameStr ) |
7bcb11d3 JS |
383 | { |
384 | // Retrieve the default device name | |
385 | wxString portName; | |
a17e237f VZ |
386 | #ifdef __WXDEBUG__ |
387 | bool ret = | |
388 | #else // !Debug | |
389 | (void) | |
390 | #endif // Debug/Release | |
391 | wxGetDefaultDeviceName(devNameStr, portName); | |
7bcb11d3 | 392 | |
223d09f6 | 393 | wxASSERT_MSG( ret, wxT("Could not get default device name.") ); |
2bda0e17 | 394 | |
4b7f2165 | 395 | deviceName = devNameStr.c_str(); |
7bcb11d3 | 396 | } |
a17e237f | 397 | |
7bcb11d3 JS |
398 | #ifdef __WIN32__ |
399 | HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode); | |
400 | #else | |
401 | HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode); | |
402 | #endif | |
a17e237f | 403 | |
7bcb11d3 JS |
404 | if (hDevMode && lpDevMode) |
405 | GlobalUnlock(hDevMode); | |
a17e237f | 406 | |
7bcb11d3 JS |
407 | return (WXHDC) hDC; |
408 | } | |
2bda0e17 | 409 | |
4b7f2165 VZ |
410 | // ---------------------------------------------------------------------------- |
411 | // wxPrinterDC bit blitting/bitmap drawing | |
412 | // ---------------------------------------------------------------------------- | |
413 | ||
f6081a04 JS |
414 | // Win16 doesn't define GDI_ERROR. |
415 | #ifndef GDI_ERROR | |
416 | #define GDI_ERROR -1 | |
417 | #endif | |
418 | ||
e11f2e16 JS |
419 | // Just in case we want to go back to using 8 bits for |
420 | // any reason: set this to 0 for 8 bits. | |
421 | #define wxUSE_DRAWBITMAP_24BITS 1 | |
422 | ||
4b7f2165 VZ |
423 | void wxPrinterDC::DoDrawBitmap(const wxBitmap &bmp, |
424 | wxCoord x, wxCoord y, | |
425 | bool useMask) | |
426 | { | |
427 | wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") ); | |
428 | ||
429 | int width = bmp.GetWidth(), | |
430 | height = bmp.GetHeight(); | |
431 | ||
432 | if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB ) | |
433 | { | |
0becd470 | 434 | #if wxUSE_DIB_FOR_BITMAP |
8bbbae21 | 435 | if ( bmp.IsDIB() ) |
0becd470 VZ |
436 | { |
437 | DIBSECTION dib; | |
438 | if ( ::GetObject(GetHbitmapOf(bmp), | |
439 | sizeof(dib), | |
440 | &dib) == sizeof(dib) ) | |
441 | { | |
442 | if ( ::StretchDIBits | |
443 | ( | |
444 | GetHdc(), | |
445 | x, y, | |
446 | width, height, | |
447 | 0, 0, | |
448 | width, height, | |
449 | dib.dsBm.bmBits, | |
450 | (LPBITMAPINFO)&dib.dsBmih, | |
451 | DIB_RGB_COLORS, | |
452 | SRCCOPY | |
453 | ) == GDI_ERROR ) | |
454 | { | |
455 | wxLogLastError(wxT("StretchDIBits")); | |
456 | } | |
457 | } | |
458 | else | |
459 | { | |
460 | wxLogLastError(wxT("GetObject")); | |
461 | } | |
462 | } | |
463 | else | |
464 | #endif // wxUSE_DIB_FOR_BITMAP | |
465 | { | |
466 | BITMAPINFO *info = (BITMAPINFO *) malloc( sizeof( BITMAPINFOHEADER ) + 256 * sizeof(RGBQUAD ) ); | |
467 | memset( info, 0, sizeof( BITMAPINFOHEADER ) ); | |
4b7f2165 | 468 | |
e11f2e16 | 469 | #if wxUSE_DRAWBITMAP_24BITS |
0becd470 | 470 | int iBitsSize = (((width * 3) + 3 ) & ~3 ) * height; |
e11f2e16 | 471 | #else |
0becd470 | 472 | int iBitsSize = ((width + 3 ) & ~3 ) * height ; |
e11f2e16 | 473 | #endif |
4b7f2165 | 474 | |
0becd470 | 475 | void* bits = malloc( iBitsSize ); |
4b7f2165 | 476 | |
0becd470 VZ |
477 | info->bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); |
478 | info->bmiHeader.biWidth = width; | |
479 | info->bmiHeader.biHeight = height; | |
480 | info->bmiHeader.biPlanes = 1; | |
e11f2e16 | 481 | #if wxUSE_DRAWBITMAP_24BITS |
0becd470 | 482 | info->bmiHeader.biBitCount = 24; |
e11f2e16 | 483 | #else |
0becd470 VZ |
484 | info->bmiHeader.biBitCount = 8; |
485 | #endif | |
486 | info->bmiHeader.biCompression = BI_RGB; | |
487 | ||
488 | ScreenHDC display; | |
489 | if ( GetDIBits(display, GetHbitmapOf(bmp), 0, | |
490 | bmp.GetHeight(), bits, info, | |
491 | DIB_RGB_COLORS) ) | |
4b7f2165 | 492 | { |
0becd470 VZ |
493 | if ( ::StretchDIBits(GetHdc(), x, y, |
494 | width, height, | |
495 | 0 , 0, width, height, | |
496 | bits, info, | |
497 | DIB_RGB_COLORS, SRCCOPY) == GDI_ERROR ) | |
498 | { | |
499 | wxLogLastError(wxT("StretchDIBits")); | |
500 | } | |
4b7f2165 | 501 | } |
4b7f2165 | 502 | |
0becd470 VZ |
503 | free(bits); |
504 | free(info); | |
505 | } | |
4b7f2165 VZ |
506 | } |
507 | else // no support for StretchDIBits() | |
508 | { | |
509 | wxMemoryDC memDC; | |
510 | memDC.SelectObject(bmp); | |
511 | ||
512 | Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask); | |
513 | ||
514 | memDC.SelectObject(wxNullBitmap); | |
515 | } | |
516 | } | |
517 | ||
518 | bool wxPrinterDC::DoBlit(wxCoord xdest, wxCoord ydest, | |
519 | wxCoord width, wxCoord height, | |
520 | wxDC *source, | |
521 | wxCoord xsrc, wxCoord ysrc, | |
0cbff120 | 522 | int WXUNUSED(rop), bool useMask, |
d699f48b | 523 | wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask)) |
4b7f2165 VZ |
524 | { |
525 | bool success = TRUE; | |
526 | ||
527 | if ( useMask ) | |
528 | { | |
0becd470 VZ |
529 | // If we are printing source colours are screen colours not printer |
530 | // colours and so we need copy the bitmap pixel by pixel. | |
4b7f2165 VZ |
531 | RECT rect; |
532 | HDC dc_src = GetHdcOf(*source); | |
533 | HDC dc_mask = ::CreateCompatibleDC(dc_src); | |
534 | ||
535 | ::SelectObject(dc_mask, (HBITMAP) source->GetSelectedBitmap().GetMask()->GetMaskBitmap()); | |
536 | for (int x = 0; x < width; x++) | |
537 | { | |
538 | for (int y = 0; y < height; y++) | |
539 | { | |
540 | COLORREF cref = ::GetPixel(dc_mask, x, y); | |
541 | if (cref) | |
542 | { | |
543 | HBRUSH brush = ::CreateSolidBrush(::GetPixel(dc_src, x, y)); | |
544 | rect.left = xdest + x; | |
545 | rect.right = rect.left + 1; | |
33ac7e6f | 546 | rect.top = ydest + y; |
4b7f2165 VZ |
547 | rect.bottom = rect.top + 1; |
548 | ::FillRect(GetHdc(), &rect, brush); | |
549 | ::DeleteObject(brush); | |
550 | } | |
551 | } | |
552 | } | |
553 | ::SelectObject(dc_mask, 0); | |
554 | ::DeleteDC(dc_mask); | |
555 | } | |
556 | else // no mask | |
557 | { | |
558 | if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB ) | |
559 | { | |
560 | wxBitmap& bmp = source->GetSelectedBitmap(); | |
561 | int width = bmp.GetWidth(), | |
562 | height = bmp.GetHeight(); | |
0becd470 | 563 | #if wxUSE_DIB_FOR_BITMAP |
8bbbae21 | 564 | if ( bmp.IsDIB() ) |
0becd470 VZ |
565 | { |
566 | DIBSECTION dib; | |
567 | if( ::GetObject(GetHbitmapOf(bmp), | |
568 | sizeof(dib), | |
569 | &dib) == sizeof(dib) ) | |
570 | { | |
571 | if ( ::StretchDIBits | |
572 | ( | |
573 | GetHdc(), | |
574 | xdest, ydest, | |
575 | width, height, | |
576 | xsrc, ysrc, | |
577 | width, height, | |
578 | dib.dsBm.bmBits, | |
579 | (LPBITMAPINFO)&dib.dsBmih, | |
580 | DIB_RGB_COLORS, | |
581 | SRCCOPY | |
582 | ) == GDI_ERROR ) | |
583 | { | |
584 | wxLogLastError(wxT("StretchDIBits")); | |
585 | } | |
586 | } | |
587 | else | |
588 | { | |
589 | wxLogLastError(wxT("GetObject")); | |
590 | } | |
591 | } | |
592 | else | |
593 | #endif // wxUSE_DIB_FOR_BITMAP | |
594 | { | |
595 | BITMAPINFO *info = (BITMAPINFO *) malloc( sizeof( BITMAPINFOHEADER ) + 256 * sizeof(RGBQUAD ) ); | |
596 | #if wxUSE_DRAWBITMAP_24BITS | |
597 | int iBitsSize = (((width * 3) + 3 ) & ~3 ) * height; | |
598 | #else | |
599 | int iBitsSize = ((width + 3 ) & ~3 ) * height ; | |
600 | #endif | |
4b7f2165 | 601 | |
0becd470 | 602 | void* bits = malloc( iBitsSize ); |
4b7f2165 | 603 | |
0becd470 | 604 | memset( info , 0 , sizeof( BITMAPINFOHEADER ) ); |
4b7f2165 | 605 | |
0becd470 VZ |
606 | info->bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); |
607 | info->bmiHeader.biWidth = width; | |
608 | info->bmiHeader.biHeight = height; | |
609 | info->bmiHeader.biPlanes = 1; | |
610 | #if wxUSE_DRAWBITMAP_24BITS | |
611 | info->bmiHeader.biBitCount = 24; | |
612 | #else | |
613 | info->bmiHeader.biBitCount = 8; | |
614 | #endif | |
615 | info->bmiHeader.biCompression = BI_RGB; | |
4b7f2165 | 616 | |
0becd470 VZ |
617 | ScreenHDC display; |
618 | if ( !::GetDIBits(display, GetHbitmapOf(bmp), 0, | |
619 | height, bits, info, DIB_RGB_COLORS) ) | |
620 | { | |
621 | wxLogLastError(wxT("GetDIBits")); | |
4b7f2165 | 622 | |
0becd470 VZ |
623 | success = FALSE; |
624 | } | |
4b7f2165 | 625 | |
0becd470 | 626 | if ( success ) |
4b7f2165 | 627 | { |
0becd470 VZ |
628 | success = ::StretchDIBits(GetHdc(), xdest, ydest, |
629 | width, height, | |
630 | xsrc, ysrc, | |
631 | width, height, | |
632 | bits, info , | |
633 | DIB_RGB_COLORS, | |
634 | SRCCOPY) != GDI_ERROR; | |
635 | if ( !success ) | |
636 | { | |
637 | wxLogLastError(wxT("StretchDIBits")); | |
638 | } | |
4b7f2165 | 639 | } |
4b7f2165 | 640 | |
0becd470 VZ |
641 | free(bits); |
642 | free(info); | |
643 | } | |
4b7f2165 VZ |
644 | } |
645 | else // no support for StretchDIBits | |
646 | { | |
0becd470 VZ |
647 | // as we are printing, source colours are screen colours not |
648 | // printer colours and so we need copy the bitmap pixel by pixel. | |
4b7f2165 VZ |
649 | HDC dc_src = GetHdcOf(*source); |
650 | RECT rect; | |
651 | for (int y = 0; y < height; y++) | |
652 | { | |
0becd470 | 653 | // optimization: draw identical adjacent pixels together. |
4b7f2165 VZ |
654 | for (int x = 0; x < width; x++) |
655 | { | |
656 | COLORREF col = ::GetPixel(dc_src, x, y); | |
657 | HBRUSH brush = ::CreateSolidBrush( col ); | |
658 | ||
659 | rect.left = xdest + x; | |
660 | rect.top = ydest + y; | |
661 | while( (x + 1 < width) && (::GetPixel(dc_src, x + 1, y) == col ) ) | |
662 | { | |
663 | ++x; | |
664 | } | |
665 | rect.right = xdest + x + 1; | |
666 | rect.bottom = rect.top + 1; | |
667 | ::FillRect((HDC) m_hDC, &rect, brush); | |
668 | ::DeleteObject(brush); | |
669 | } | |
670 | } | |
671 | } | |
672 | } | |
673 | ||
674 | return success; | |
675 | } | |
f6bcfd97 BP |
676 | |
677 | #endif | |
678 | // wxUSE_PRINTING_ARCHITECTURE |