]>
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 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | #ifdef __GNUG__ | |
21 | #pragma implementation "dcprint.h" | |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
31 | #ifndef WX_PRECOMP | |
32 | #include "wx/string.h" | |
33 | #include "wx/log.h" | |
34 | #include "wx/window.h" | |
35 | #include "wx/dcmemory.h" | |
36 | #endif | |
37 | ||
38 | #if wxUSE_PRINTING_ARCHITECTURE | |
39 | ||
40 | #include "wx/msw/private.h" | |
41 | #include "wx/dcprint.h" | |
42 | #include "math.h" | |
43 | ||
44 | #if wxUSE_COMMON_DIALOGS || defined(__WXWINE__) | |
45 | #include <commdlg.h> | |
46 | #endif | |
47 | ||
48 | #ifndef __WIN32__ | |
49 | #include <print.h> | |
50 | #endif | |
51 | ||
52 | // mingw32 defines GDI_ERROR incorrectly | |
53 | #ifdef __GNUWIN32__ | |
54 | #undef GDI_ERROR | |
55 | #define GDI_ERROR ((int)-1) | |
56 | #endif | |
57 | ||
58 | // ---------------------------------------------------------------------------- | |
59 | // wxWin macros | |
60 | // ---------------------------------------------------------------------------- | |
61 | ||
62 | IMPLEMENT_CLASS(wxPrinterDC, wxDC) | |
63 | ||
64 | // ============================================================================ | |
65 | // implementation | |
66 | // ============================================================================ | |
67 | ||
68 | // ---------------------------------------------------------------------------- | |
69 | // wxPrinterDC construction | |
70 | // ---------------------------------------------------------------------------- | |
71 | ||
72 | // This form is deprecated | |
73 | wxPrinterDC::wxPrinterDC(const wxString& driver_name, | |
74 | const wxString& device_name, | |
75 | const wxString& file, | |
76 | bool interactive, | |
77 | int orientation) | |
78 | { | |
79 | m_isInteractive = interactive; | |
80 | ||
81 | if ( !file.empty() ) | |
82 | m_printData.SetFilename(file); | |
83 | ||
84 | #if wxUSE_COMMON_DIALOGS | |
85 | if ( interactive ) | |
86 | { | |
87 | PRINTDLG pd; | |
88 | ||
89 | pd.lStructSize = sizeof( PRINTDLG ); | |
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 ) | |
103 | { | |
104 | m_hDC = (WXHDC) pd.hDC; | |
105 | } | |
106 | } | |
107 | else | |
108 | #endif // wxUSE_COMMON_DIALOGS | |
109 | { | |
110 | if ( !driver_name.empty() && !device_name.empty() && !file.empty() ) | |
111 | { | |
112 | m_hDC = (WXHDC) CreateDC(driver_name, device_name, file, NULL); | |
113 | } | |
114 | else // we don't have all parameters, ask the user | |
115 | { | |
116 | wxPrintData printData; | |
117 | printData.SetOrientation(orientation); | |
118 | m_hDC = wxGetPrinterDC(printData); | |
119 | } | |
120 | ||
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(); | |
128 | } | |
129 | ||
130 | wxPrinterDC::wxPrinterDC(const wxPrintData& printData) | |
131 | { | |
132 | m_printData = printData; | |
133 | ||
134 | m_isInteractive = FALSE; | |
135 | ||
136 | m_hDC = wxGetPrinterDC(printData); | |
137 | m_ok = m_hDC != 0; | |
138 | m_bOwnsDC = TRUE; | |
139 | ||
140 | Init(); | |
141 | } | |
142 | ||
143 | ||
144 | wxPrinterDC::wxPrinterDC(WXHDC dc) | |
145 | { | |
146 | m_isInteractive = FALSE; | |
147 | ||
148 | m_hDC = dc; | |
149 | m_bOwnsDC = TRUE; | |
150 | m_ok = TRUE; | |
151 | } | |
152 | ||
153 | void wxPrinterDC::Init() | |
154 | { | |
155 | if ( m_hDC ) | |
156 | { | |
157 | // int width = GetDeviceCaps(m_hDC, VERTRES); | |
158 | // int height = GetDeviceCaps(m_hDC, HORZRES); | |
159 | SetMapMode(wxMM_TEXT); | |
160 | ||
161 | SetBrush(*wxBLACK_BRUSH); | |
162 | SetPen(*wxBLACK_PEN); | |
163 | } | |
164 | } | |
165 | ||
166 | // ---------------------------------------------------------------------------- | |
167 | // wxPrinterDC {Start/End}{Page/Doc} methods | |
168 | // ---------------------------------------------------------------------------- | |
169 | ||
170 | bool wxPrinterDC::StartDoc(const wxString& message) | |
171 | { | |
172 | DOCINFO docinfo; | |
173 | docinfo.cbSize = sizeof(DOCINFO); | |
174 | docinfo.lpszDocName = (const wxChar*)message; | |
175 | ||
176 | wxString filename(m_printData.GetFilename()); | |
177 | ||
178 | if (filename.IsEmpty()) | |
179 | docinfo.lpszOutput = NULL; | |
180 | else | |
181 | docinfo.lpszOutput = (const wxChar *) filename; | |
182 | ||
183 | #if defined(__WIN95__) | |
184 | docinfo.lpszDatatype = NULL; | |
185 | docinfo.fwType = 0; | |
186 | #endif | |
187 | ||
188 | if (!m_hDC) | |
189 | return FALSE; | |
190 | ||
191 | int ret = ::StartDoc(GetHdc(), &docinfo); | |
192 | ||
193 | #ifndef __WIN16__ | |
194 | if (ret <= 0) | |
195 | { | |
196 | DWORD lastError = GetLastError(); | |
197 | wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError); | |
198 | } | |
199 | #endif | |
200 | ||
201 | return (ret > 0); | |
202 | } | |
203 | ||
204 | void wxPrinterDC::EndDoc() | |
205 | { | |
206 | if (m_hDC) ::EndDoc((HDC) m_hDC); | |
207 | } | |
208 | ||
209 | void wxPrinterDC::StartPage() | |
210 | { | |
211 | if (m_hDC) | |
212 | ::StartPage((HDC) m_hDC); | |
213 | } | |
214 | ||
215 | void wxPrinterDC::EndPage() | |
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 | { | |
224 | deviceName.clear(); | |
225 | ||
226 | LPDEVNAMES lpDevNames; | |
227 | LPSTR lpszDriverName; | |
228 | LPSTR lpszDeviceName; | |
229 | LPSTR lpszPortName; | |
230 | ||
231 | PRINTDLG pd; | |
232 | ||
233 | // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
234 | #ifdef __GNUWIN32__ | |
235 | pd.lStructSize = 66; // sizeof(PRINTDLG); | |
236 | #else | |
237 | pd.lStructSize = sizeof(PRINTDLG); | |
238 | #endif | |
239 | ||
240 | pd.hwndOwner = (HWND)NULL; | |
241 | pd.hDevMode = NULL; // Will be created by PrintDlg | |
242 | pd.hDevNames = NULL; // Ditto | |
243 | pd.Flags = PD_RETURNDEFAULT; | |
244 | pd.nCopies = 1; | |
245 | ||
246 | if (!PrintDlg((LPPRINTDLG)&pd)) | |
247 | { | |
248 | if ( pd.hDevMode ) | |
249 | GlobalFree(pd.hDevMode); | |
250 | if (pd.hDevNames) | |
251 | GlobalFree(pd.hDevNames); | |
252 | ||
253 | return FALSE; | |
254 | } | |
255 | ||
256 | if (pd.hDevNames) | |
257 | { | |
258 | lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames); | |
259 | lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset; | |
260 | lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset; | |
261 | lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset; | |
262 | ||
263 | deviceName = lpszDeviceName; | |
264 | portName = lpszPortName; | |
265 | ||
266 | GlobalUnlock(pd.hDevNames); | |
267 | GlobalFree(pd.hDevNames); | |
268 | pd.hDevNames=NULL; | |
269 | } | |
270 | ||
271 | if (pd.hDevMode) | |
272 | { | |
273 | GlobalFree(pd.hDevMode); | |
274 | pd.hDevMode=NULL; | |
275 | } | |
276 | return ( deviceName != wxT("") ); | |
277 | } | |
278 | ||
279 | #if 0 | |
280 | // This uses defaults, except for orientation, so we should eliminate this function | |
281 | // and use the 2nd form (passing wxPrintData) instead. | |
282 | WXHDC wxGetPrinterDC(int orientation) | |
283 | { | |
284 | HDC hDC; | |
285 | LPDEVMODE lpDevMode = NULL; | |
286 | LPDEVNAMES lpDevNames; | |
287 | LPSTR lpszDriverName; | |
288 | LPSTR lpszDeviceName; | |
289 | LPSTR lpszPortName; | |
290 | ||
291 | PRINTDLG pd; | |
292 | // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
293 | #ifdef __GNUWIN32__ | |
294 | pd.lStructSize = 66; // sizeof(PRINTDLG); | |
295 | #else | |
296 | pd.lStructSize = sizeof(PRINTDLG); | |
297 | #endif | |
298 | pd.hwndOwner = (HWND)NULL; | |
299 | pd.hDevMode = NULL; // Will be created by PrintDlg | |
300 | pd.hDevNames = NULL; // Ditto | |
301 | pd.Flags = PD_RETURNDEFAULT; | |
302 | pd.nCopies = 1; | |
303 | ||
304 | if (!PrintDlg((LPPRINTDLG)&pd)) | |
305 | { | |
306 | if ( pd.hDevMode ) | |
307 | GlobalFree(pd.hDevMode); | |
308 | if (pd.hDevNames) | |
309 | GlobalFree(pd.hDevNames); | |
310 | ||
311 | return(0); | |
312 | } | |
313 | ||
314 | if (!pd.hDevNames) | |
315 | { | |
316 | if ( pd.hDevMode ) | |
317 | GlobalFree(pd.hDevMode); | |
318 | } | |
319 | ||
320 | lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames); | |
321 | lpszDriverName = (LPSTR)lpDevNames + lpDevNames->wDriverOffset; | |
322 | lpszDeviceName = (LPSTR)lpDevNames + lpDevNames->wDeviceOffset; | |
323 | lpszPortName = (LPSTR)lpDevNames + lpDevNames->wOutputOffset; | |
324 | GlobalUnlock(pd.hDevNames); | |
325 | ||
326 | if ( pd.hDevMode ) | |
327 | { | |
328 | lpDevMode = (DEVMODE*) GlobalLock(pd.hDevMode); | |
329 | lpDevMode->dmOrientation = orientation; | |
330 | lpDevMode->dmFields |= DM_ORIENTATION; | |
331 | } | |
332 | ||
333 | #ifdef __WIN32__ | |
334 | hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (DEVMODE *)lpDevMode); | |
335 | #else | |
336 | hDC = CreateDC(lpszDriverName, lpszDeviceName, lpszPortName, (LPSTR)lpDevMode); | |
337 | #endif | |
338 | ||
339 | if (pd.hDevMode && lpDevMode) | |
340 | GlobalUnlock(pd.hDevMode); | |
341 | ||
342 | if (pd.hDevNames) | |
343 | { | |
344 | GlobalFree(pd.hDevNames); | |
345 | pd.hDevNames=NULL; | |
346 | } | |
347 | if (pd.hDevMode) | |
348 | { | |
349 | GlobalFree(pd.hDevMode); | |
350 | pd.hDevMode=NULL; | |
351 | } | |
352 | return (WXHDC) hDC; | |
353 | } | |
354 | #endif // 0 | |
355 | ||
356 | // Gets an HDC for the specified printer configuration | |
357 | WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst) | |
358 | { | |
359 | wxPrintData printData = printDataConst; | |
360 | printData.ConvertToNative(); | |
361 | ||
362 | wxChar* driverName = (wxChar*) NULL; | |
363 | ||
364 | wxString devNameStr = printData.GetPrinterName(); | |
365 | wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32 | |
366 | ||
367 | const wxChar* deviceName; | |
368 | if ( !devNameStr ) | |
369 | deviceName = (wxChar*) NULL; | |
370 | else | |
371 | deviceName = devNameStr.c_str(); | |
372 | ||
373 | LPDEVMODE lpDevMode = (LPDEVMODE) NULL; | |
374 | ||
375 | HGLOBAL hDevMode = (HGLOBAL)(DWORD) printData.GetNativeData(); | |
376 | ||
377 | if ( hDevMode ) | |
378 | lpDevMode = (DEVMODE*) GlobalLock(hDevMode); | |
379 | ||
380 | if ( !devNameStr ) | |
381 | { | |
382 | // Retrieve the default device name | |
383 | wxString portName; | |
384 | #ifdef __WXDEBUG__ | |
385 | bool ret = | |
386 | #else // !Debug | |
387 | (void) | |
388 | #endif // Debug/Release | |
389 | wxGetDefaultDeviceName(devNameStr, portName); | |
390 | ||
391 | wxASSERT_MSG( ret, wxT("Could not get default device name.") ); | |
392 | ||
393 | deviceName = devNameStr.c_str(); | |
394 | } | |
395 | ||
396 | #ifdef __WIN32__ | |
397 | HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode); | |
398 | #else | |
399 | HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode); | |
400 | #endif | |
401 | ||
402 | if (hDevMode && lpDevMode) | |
403 | GlobalUnlock(hDevMode); | |
404 | ||
405 | return (WXHDC) hDC; | |
406 | } | |
407 | ||
408 | // ---------------------------------------------------------------------------- | |
409 | // wxPrinterDC bit blitting/bitmap drawing | |
410 | // ---------------------------------------------------------------------------- | |
411 | ||
412 | // Win16 doesn't define GDI_ERROR. | |
413 | #ifndef GDI_ERROR | |
414 | #define GDI_ERROR -1 | |
415 | #endif | |
416 | ||
417 | void wxPrinterDC::DoDrawBitmap(const wxBitmap &bmp, | |
418 | wxCoord x, wxCoord y, | |
419 | bool useMask) | |
420 | { | |
421 | wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") ); | |
422 | ||
423 | int width = bmp.GetWidth(), | |
424 | height = bmp.GetHeight(); | |
425 | ||
426 | if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB ) | |
427 | { | |
428 | BITMAPINFO *info = (BITMAPINFO *) malloc( sizeof( BITMAPINFOHEADER ) + 256 * sizeof(RGBQUAD ) ); | |
429 | memset( info, 0, sizeof( BITMAPINFOHEADER ) ); | |
430 | ||
431 | int iBitsSize = ((width + 3 ) & ~3 ) * height; | |
432 | ||
433 | void* bits = malloc( iBitsSize ); | |
434 | ||
435 | info->bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); | |
436 | info->bmiHeader.biWidth = width; | |
437 | info->bmiHeader.biHeight = height; | |
438 | info->bmiHeader.biPlanes = 1; | |
439 | info->bmiHeader.biBitCount = 8; | |
440 | info->bmiHeader.biCompression = BI_RGB; | |
441 | ||
442 | ScreenHDC display; | |
443 | if ( GetDIBits(display, GetHbitmapOf(bmp), 0, | |
444 | bmp.GetHeight(), bits, info, | |
445 | DIB_RGB_COLORS) ) | |
446 | { | |
447 | if ( ::StretchDIBits(GetHdc(), x, y, | |
448 | width, height, | |
449 | 0 , 0, width, height, | |
450 | bits, info, | |
451 | DIB_RGB_COLORS, SRCCOPY) == GDI_ERROR ) | |
452 | { | |
453 | wxLogLastError(wxT("StretchDIBits")); | |
454 | } | |
455 | } | |
456 | ||
457 | free(bits); | |
458 | free(info); | |
459 | } | |
460 | else // no support for StretchDIBits() | |
461 | { | |
462 | wxMemoryDC memDC; | |
463 | memDC.SelectObject(bmp); | |
464 | ||
465 | Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask); | |
466 | ||
467 | memDC.SelectObject(wxNullBitmap); | |
468 | } | |
469 | } | |
470 | ||
471 | bool wxPrinterDC::DoBlit(wxCoord xdest, wxCoord ydest, | |
472 | wxCoord width, wxCoord height, | |
473 | wxDC *source, | |
474 | wxCoord xsrc, wxCoord ysrc, | |
475 | int rop, bool useMask) | |
476 | { | |
477 | bool success = TRUE; | |
478 | ||
479 | if ( useMask ) | |
480 | { | |
481 | // If we are printing source colours are screen colours | |
482 | // not printer colours and so we need copy the bitmap | |
483 | // pixel by pixel. | |
484 | RECT rect; | |
485 | HDC dc_src = GetHdcOf(*source); | |
486 | HDC dc_mask = ::CreateCompatibleDC(dc_src); | |
487 | ||
488 | ::SelectObject(dc_mask, (HBITMAP) source->GetSelectedBitmap().GetMask()->GetMaskBitmap()); | |
489 | for (int x = 0; x < width; x++) | |
490 | { | |
491 | for (int y = 0; y < height; y++) | |
492 | { | |
493 | COLORREF cref = ::GetPixel(dc_mask, x, y); | |
494 | if (cref) | |
495 | { | |
496 | HBRUSH brush = ::CreateSolidBrush(::GetPixel(dc_src, x, y)); | |
497 | rect.left = xdest + x; | |
498 | rect.right = rect.left + 1; | |
499 | rect.top = ydest + y; | |
500 | rect.bottom = rect.top + 1; | |
501 | ::FillRect(GetHdc(), &rect, brush); | |
502 | ::DeleteObject(brush); | |
503 | } | |
504 | } | |
505 | } | |
506 | ::SelectObject(dc_mask, 0); | |
507 | ::DeleteDC(dc_mask); | |
508 | } | |
509 | else // no mask | |
510 | { | |
511 | if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB ) | |
512 | { | |
513 | wxBitmap& bmp = source->GetSelectedBitmap(); | |
514 | int width = bmp.GetWidth(), | |
515 | height = bmp.GetHeight(); | |
516 | ||
517 | BITMAPINFO *info = (BITMAPINFO *) malloc( sizeof( BITMAPINFOHEADER ) + 256 * sizeof(RGBQUAD ) ); | |
518 | int iBitsSize = ((width + 3 ) & ~3 ) * height; | |
519 | ||
520 | void* bits = malloc( iBitsSize ); | |
521 | ||
522 | memset( info , 0 , sizeof( BITMAPINFOHEADER ) ); | |
523 | ||
524 | info->bmiHeader.biSize = sizeof( BITMAPINFOHEADER ); | |
525 | info->bmiHeader.biWidth = width; | |
526 | info->bmiHeader.biHeight = height; | |
527 | info->bmiHeader.biPlanes = 1; | |
528 | info->bmiHeader.biBitCount = 8; | |
529 | info->bmiHeader.biCompression = BI_RGB; | |
530 | ||
531 | ScreenHDC display; | |
532 | if ( !::GetDIBits(display, GetHbitmapOf(bmp), 0, | |
533 | height, bits, info, DIB_RGB_COLORS) ) | |
534 | { | |
535 | wxLogLastError(wxT("GetDIBits")); | |
536 | ||
537 | success = FALSE; | |
538 | } | |
539 | ||
540 | if ( success ) | |
541 | { | |
542 | success = ::StretchDIBits(GetHdc(), xdest, ydest, | |
543 | width, height, | |
544 | xsrc, ysrc, | |
545 | width, height, | |
546 | bits, info , | |
547 | DIB_RGB_COLORS, | |
548 | SRCCOPY) != GDI_ERROR; | |
549 | if ( !success ) | |
550 | { | |
551 | wxLogLastError(wxT("StretchDIBits")); | |
552 | } | |
553 | } | |
554 | ||
555 | free(bits); | |
556 | free(info); | |
557 | } | |
558 | else // no support for StretchDIBits | |
559 | { | |
560 | // as we are printing, source colours are screen colours not printer | |
561 | // colours and so we need copy the bitmap pixel by pixel. | |
562 | HDC dc_src = GetHdcOf(*source); | |
563 | RECT rect; | |
564 | for (int y = 0; y < height; y++) | |
565 | { | |
566 | // This is Stefan Csomor's optimisation, where identical adjacent | |
567 | // pixels are drawn together. | |
568 | for (int x = 0; x < width; x++) | |
569 | { | |
570 | COLORREF col = ::GetPixel(dc_src, x, y); | |
571 | HBRUSH brush = ::CreateSolidBrush( col ); | |
572 | ||
573 | rect.left = xdest + x; | |
574 | rect.top = ydest + y; | |
575 | while( (x + 1 < width) && (::GetPixel(dc_src, x + 1, y) == col ) ) | |
576 | { | |
577 | ++x; | |
578 | } | |
579 | rect.right = xdest + x + 1; | |
580 | rect.bottom = rect.top + 1; | |
581 | ::FillRect((HDC) m_hDC, &rect, brush); | |
582 | ::DeleteObject(brush); | |
583 | } | |
584 | } | |
585 | } | |
586 | } | |
587 | ||
588 | return success; | |
589 | } | |
590 | ||
591 | #endif | |
592 | // wxUSE_PRINTING_ARCHITECTURE |