]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dcprint.cpp
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dcprint.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
34 #include "wx/window.h"
35 #include "wx/dcmemory.h"
38 #if wxUSE_PRINTING_ARCHITECTURE
40 #include "wx/msw/private.h"
41 #include "wx/dcprint.h"
44 #if wxUSE_COMMON_DIALOGS || defined(__WXWINE__)
52 // mingw32 defines GDI_ERROR incorrectly
55 #define GDI_ERROR ((int)-1)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxPrinterDC construction
70 // ----------------------------------------------------------------------------
72 // This form is deprecated
73 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
, const wxString
& device_name
, const wxString
& file
, bool interactive
, int orientation
)
75 m_isInteractive
= interactive
;
78 m_printData
.SetFilename(file
);
80 #if wxUSE_COMMON_DIALOGS
85 pd
.lStructSize
= sizeof( PRINTDLG
);
86 pd
.hwndOwner
=(HWND
) NULL
;
87 pd
.hDevMode
=(HANDLE
)NULL
;
88 pd
.hDevNames
=(HANDLE
)NULL
;
89 pd
.Flags
=PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
95 pd
.hInstance
=(HINSTANCE
)NULL
;
97 if ( PrintDlg( &pd
) != 0 )
99 m_hDC
= (WXHDC
) pd
.hDC
;
108 // m_dontDelete = TRUE;
111 #endif // wxUSE_COMMON_DIALOGS
112 if ((!driver_name
.IsNull() && driver_name
!= wxT("")) &&
113 (!device_name
.IsNull() && device_name
!= wxT("")) &&
114 (!file
.IsNull() && file
!= wxT("")))
116 m_hDC
= (WXHDC
) CreateDC(WXSTRINGCAST driver_name
, WXSTRINGCAST device_name
, WXSTRINGCAST file
, NULL
);
117 m_ok
= m_hDC
? TRUE
: FALSE
;
121 wxPrintData printData
;
122 printData
.SetOrientation(orientation
);
123 m_hDC
= wxGetPrinterDC(printData
);
124 m_ok
= m_hDC
? TRUE
: FALSE
;
129 // int width = GetDeviceCaps(m_hDC, VERTRES);
130 // int height = GetDeviceCaps(m_hDC, HORZRES);
131 SetMapMode(wxMM_TEXT
);
133 SetBrush(*wxBLACK_BRUSH
);
134 SetPen(*wxBLACK_PEN
);
137 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
139 m_printData
= printData
;
141 m_isInteractive
= FALSE
;
143 m_hDC
= wxGetPrinterDC(printData
);
147 SetMapMode(wxMM_TEXT
);
149 SetBrush(*wxBLACK_BRUSH
);
150 SetPen(*wxBLACK_PEN
);
154 wxPrinterDC::wxPrinterDC(WXHDC theDC
)
156 m_isInteractive
= FALSE
;
162 // int width = GetDeviceCaps(m_hDC, VERTRES);
163 // int height = GetDeviceCaps(m_hDC, HORZRES);
164 SetMapMode(wxMM_TEXT
);
166 SetBrush(*wxBLACK_BRUSH
);
167 SetPen(*wxBLACK_PEN
);
170 wxPrinterDC::~wxPrinterDC()
174 // ----------------------------------------------------------------------------
175 // wxPrinterDC {Start/End}{Page/Doc} methods
176 // ----------------------------------------------------------------------------
178 bool wxPrinterDC::StartDoc(const wxString
& message
)
181 docinfo
.cbSize
= sizeof(DOCINFO
);
182 docinfo
.lpszDocName
= (const wxChar
*)message
;
184 wxString
filename(m_printData
.GetFilename());
186 if (filename
.IsEmpty())
187 docinfo
.lpszOutput
= NULL
;
189 docinfo
.lpszOutput
= (const wxChar
*) filename
;
191 #if defined(__WIN95__)
192 docinfo
.lpszDatatype
= NULL
;
199 int ret
= ::StartDoc(GetHdc(), &docinfo
);
204 DWORD lastError
= GetLastError();
205 wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError
);
212 void wxPrinterDC::EndDoc()
214 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
217 void wxPrinterDC::StartPage()
220 ::StartPage((HDC
) m_hDC
);
223 void wxPrinterDC::EndPage()
226 ::EndPage((HDC
) m_hDC
);
229 // Returns default device and port names
230 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
234 LPDEVNAMES lpDevNames
;
235 LPSTR lpszDriverName
;
236 LPSTR lpszDeviceName
;
241 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
243 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
245 pd
.lStructSize
= sizeof(PRINTDLG
);
248 pd
.hwndOwner
= (HWND
)NULL
;
249 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
250 pd
.hDevNames
= NULL
; // Ditto
251 pd
.Flags
= PD_RETURNDEFAULT
;
254 if (!PrintDlg((LPPRINTDLG
)&pd
))
257 GlobalFree(pd
.hDevMode
);
259 GlobalFree(pd
.hDevNames
);
266 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
267 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
268 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
269 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
271 deviceName
= lpszDeviceName
;
272 portName
= lpszPortName
;
274 GlobalUnlock(pd
.hDevNames
);
275 GlobalFree(pd
.hDevNames
);
281 GlobalFree(pd
.hDevMode
);
284 return ( deviceName
!= wxT("") );
288 // This uses defaults, except for orientation, so we should eliminate this function
289 // and use the 2nd form (passing wxPrintData) instead.
290 WXHDC
wxGetPrinterDC(int orientation
)
293 LPDEVMODE lpDevMode
= NULL
;
294 LPDEVNAMES lpDevNames
;
295 LPSTR lpszDriverName
;
296 LPSTR lpszDeviceName
;
300 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
302 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
304 pd
.lStructSize
= sizeof(PRINTDLG
);
306 pd
.hwndOwner
= (HWND
)NULL
;
307 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
308 pd
.hDevNames
= NULL
; // Ditto
309 pd
.Flags
= PD_RETURNDEFAULT
;
312 if (!PrintDlg((LPPRINTDLG
)&pd
))
315 GlobalFree(pd
.hDevMode
);
317 GlobalFree(pd
.hDevNames
);
325 GlobalFree(pd
.hDevMode
);
328 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
329 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
330 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
331 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
332 GlobalUnlock(pd
.hDevNames
);
336 lpDevMode
= (DEVMODE
*) GlobalLock(pd
.hDevMode
);
337 lpDevMode
->dmOrientation
= orientation
;
338 lpDevMode
->dmFields
|= DM_ORIENTATION
;
342 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (DEVMODE
*)lpDevMode
);
344 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (LPSTR
)lpDevMode
);
347 if (pd
.hDevMode
&& lpDevMode
)
348 GlobalUnlock(pd
.hDevMode
);
352 GlobalFree(pd
.hDevNames
);
357 GlobalFree(pd
.hDevMode
);
364 // Gets an HDC for the specified printer configuration
365 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
367 wxPrintData printData
= printDataConst
;
368 printData
.ConvertToNative();
370 wxChar
* driverName
= (wxChar
*) NULL
;
372 wxString devNameStr
= printData
.GetPrinterName();
373 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
375 const wxChar
* deviceName
;
377 deviceName
= (wxChar
*) NULL
;
379 deviceName
= devNameStr
.c_str();
381 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
383 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) printData
.GetNativeData();
386 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
390 // Retrieve the default device name
396 #endif // Debug/Release
397 wxGetDefaultDeviceName(devNameStr
, portName
);
399 wxASSERT_MSG( ret
, wxT("Could not get default device name.") );
401 deviceName
= devNameStr
.c_str();
405 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
407 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
410 if (hDevMode
&& lpDevMode
)
411 GlobalUnlock(hDevMode
);
416 // ----------------------------------------------------------------------------
417 // wxPrinterDC bit blitting/bitmap drawing
418 // ----------------------------------------------------------------------------
420 // Win16 doesn't define GDI_ERROR.
425 void wxPrinterDC::DoDrawBitmap(const wxBitmap
&bmp
,
426 wxCoord x
, wxCoord y
,
429 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
431 int width
= bmp
.GetWidth(),
432 height
= bmp
.GetHeight();
434 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
)
436 BITMAPINFO
*info
= (BITMAPINFO
*) malloc( sizeof( BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
) );
437 memset( info
, 0, sizeof( BITMAPINFOHEADER
) );
439 int iBitsSize
= ((width
+ 3 ) & ~3 ) * height
;
441 void* bits
= malloc( iBitsSize
);
443 info
->bmiHeader
.biSize
= sizeof( BITMAPINFOHEADER
);
444 info
->bmiHeader
.biWidth
= width
;
445 info
->bmiHeader
.biHeight
= height
;
446 info
->bmiHeader
.biPlanes
= 1;
447 info
->bmiHeader
.biBitCount
= 8;
448 info
->bmiHeader
.biCompression
= BI_RGB
;
451 if ( GetDIBits(display
, GetHbitmapOf(bmp
), 0,
452 bmp
.GetHeight(), bits
, info
,
455 if ( ::StretchDIBits(GetHdc(), x
, y
,
457 0 , 0, width
, height
,
459 DIB_RGB_COLORS
, SRCCOPY
) == GDI_ERROR
)
461 wxLogLastError(wxT("StretchDIBits"));
468 else // no support for StretchDIBits()
471 memDC
.SelectObject(bmp
);
473 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
475 memDC
.SelectObject(wxNullBitmap
);
479 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
480 wxCoord width
, wxCoord height
,
482 wxCoord xsrc
, wxCoord ysrc
,
483 int rop
, bool useMask
)
489 // If we are printing source colours are screen colours
490 // not printer colours and so we need copy the bitmap
493 HDC dc_src
= GetHdcOf(*source
);
494 HDC dc_mask
= ::CreateCompatibleDC(dc_src
);
496 ::SelectObject(dc_mask
, (HBITMAP
) source
->GetSelectedBitmap().GetMask()->GetMaskBitmap());
497 for (int x
= 0; x
< width
; x
++)
499 for (int y
= 0; y
< height
; y
++)
501 COLORREF cref
= ::GetPixel(dc_mask
, x
, y
);
504 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dc_src
, x
, y
));
505 rect
.left
= xdest
+ x
;
506 rect
.right
= rect
.left
+ 1;
507 rect
.top
= ydest
+ y
;
508 rect
.bottom
= rect
.top
+ 1;
509 ::FillRect(GetHdc(), &rect
, brush
);
510 ::DeleteObject(brush
);
514 ::SelectObject(dc_mask
, 0);
519 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
)
521 wxBitmap
& bmp
= source
->GetSelectedBitmap();
522 int width
= bmp
.GetWidth(),
523 height
= bmp
.GetHeight();
525 BITMAPINFO
*info
= (BITMAPINFO
*) malloc( sizeof( BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
) );
526 int iBitsSize
= ((width
+ 3 ) & ~3 ) * height
;
528 void* bits
= malloc( iBitsSize
);
530 memset( info
, 0 , sizeof( BITMAPINFOHEADER
) );
532 info
->bmiHeader
.biSize
= sizeof( BITMAPINFOHEADER
);
533 info
->bmiHeader
.biWidth
= width
;
534 info
->bmiHeader
.biHeight
= height
;
535 info
->bmiHeader
.biPlanes
= 1;
536 info
->bmiHeader
.biBitCount
= 8;
537 info
->bmiHeader
.biCompression
= BI_RGB
;
540 if ( !::GetDIBits(display
, GetHbitmapOf(bmp
), 0,
541 height
, bits
, info
, DIB_RGB_COLORS
) )
543 wxLogLastError(wxT("GetDIBits"));
550 success
= ::StretchDIBits(GetHdc(), xdest
, ydest
,
556 SRCCOPY
) != GDI_ERROR
;
559 wxLogLastError(wxT("StretchDIBits"));
566 else // no support for StretchDIBits
568 // as we are printing, source colours are screen colours not printer
569 // colours and so we need copy the bitmap pixel by pixel.
570 HDC dc_src
= GetHdcOf(*source
);
572 for (int y
= 0; y
< height
; y
++)
574 // This is Stefan Csomor's optimisation, where identical adjacent
575 // pixels are drawn together.
576 for (int x
= 0; x
< width
; x
++)
578 COLORREF col
= ::GetPixel(dc_src
, x
, y
);
579 HBRUSH brush
= ::CreateSolidBrush( col
);
581 rect
.left
= xdest
+ x
;
582 rect
.top
= ydest
+ y
;
583 while( (x
+ 1 < width
) && (::GetPixel(dc_src
, x
+ 1, y
) == col
) )
587 rect
.right
= xdest
+ x
+ 1;
588 rect
.bottom
= rect
.top
+ 1;
589 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
590 ::DeleteObject(brush
);
600 // wxUSE_PRINTING_ARCHITECTURE