]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
b49d678fb8184e459282e24744d95715916c9828
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 #include "wx/msw/private.h"
39 #include "wx/dcprint.h"
42 #if wxUSE_COMMON_DIALOGS || defined(__WXWINE__)
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
53 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
55 // ============================================================================
57 // ============================================================================
59 // ----------------------------------------------------------------------------
60 // wxPrinterDC construction
61 // ----------------------------------------------------------------------------
63 // This form is deprecated
64 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
, const wxString
& device_name
, const wxString
& file
, bool interactive
, int orientation
)
66 m_isInteractive
= interactive
;
69 m_printData
.SetFilename(file
);
71 #if wxUSE_COMMON_DIALOGS
76 pd
.lStructSize
= sizeof( PRINTDLG
);
77 pd
.hwndOwner
=(HWND
) NULL
;
78 pd
.hDevMode
=(HANDLE
)NULL
;
79 pd
.hDevNames
=(HANDLE
)NULL
;
80 pd
.Flags
=PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
86 pd
.hInstance
=(HINSTANCE
)NULL
;
88 if ( PrintDlg( &pd
) != 0 )
90 m_hDC
= (WXHDC
) pd
.hDC
;
99 // m_dontDelete = TRUE;
102 #endif // wxUSE_COMMON_DIALOGS
103 if ((!driver_name
.IsNull() && driver_name
!= wxT("")) &&
104 (!device_name
.IsNull() && device_name
!= wxT("")) &&
105 (!file
.IsNull() && file
!= wxT("")))
107 m_hDC
= (WXHDC
) CreateDC(WXSTRINGCAST driver_name
, WXSTRINGCAST device_name
, WXSTRINGCAST file
, NULL
);
108 m_ok
= m_hDC
? TRUE
: FALSE
;
112 wxPrintData printData
;
113 printData
.SetOrientation(orientation
);
114 m_hDC
= wxGetPrinterDC(printData
);
115 m_ok
= m_hDC
? TRUE
: FALSE
;
120 // int width = GetDeviceCaps(m_hDC, VERTRES);
121 // int height = GetDeviceCaps(m_hDC, HORZRES);
122 SetMapMode(wxMM_TEXT
);
124 SetBrush(*wxBLACK_BRUSH
);
125 SetPen(*wxBLACK_PEN
);
128 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
130 m_printData
= printData
;
132 m_isInteractive
= FALSE
;
134 m_hDC
= wxGetPrinterDC(printData
);
138 SetMapMode(wxMM_TEXT
);
140 SetBrush(*wxBLACK_BRUSH
);
141 SetPen(*wxBLACK_PEN
);
145 wxPrinterDC::wxPrinterDC(WXHDC theDC
)
147 m_isInteractive
= FALSE
;
153 // int width = GetDeviceCaps(m_hDC, VERTRES);
154 // int height = GetDeviceCaps(m_hDC, HORZRES);
155 SetMapMode(wxMM_TEXT
);
157 SetBrush(*wxBLACK_BRUSH
);
158 SetPen(*wxBLACK_PEN
);
161 wxPrinterDC::~wxPrinterDC()
165 // ----------------------------------------------------------------------------
166 // wxPrinterDC {Start/End}{Page/Doc} methods
167 // ----------------------------------------------------------------------------
169 bool wxPrinterDC::StartDoc(const wxString
& message
)
172 docinfo
.cbSize
= sizeof(DOCINFO
);
173 docinfo
.lpszDocName
= (const wxChar
*)message
;
175 wxString
filename(m_printData
.GetFilename());
177 if (filename
.IsEmpty())
178 docinfo
.lpszOutput
= NULL
;
180 docinfo
.lpszOutput
= (const wxChar
*) filename
;
182 #if defined(__WIN95__)
183 docinfo
.lpszDatatype
= NULL
;
190 int ret
= ::StartDoc(GetHdc(), &docinfo
);
195 DWORD lastError
= GetLastError();
196 wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError
);
203 void wxPrinterDC::EndDoc()
205 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
208 void wxPrinterDC::StartPage()
211 ::StartPage((HDC
) m_hDC
);
214 void wxPrinterDC::EndPage()
217 ::EndPage((HDC
) m_hDC
);
220 // Returns default device and port names
221 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
225 LPDEVNAMES lpDevNames
;
226 LPSTR lpszDriverName
;
227 LPSTR lpszDeviceName
;
232 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
234 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
236 pd
.lStructSize
= sizeof(PRINTDLG
);
239 pd
.hwndOwner
= (HWND
)NULL
;
240 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
241 pd
.hDevNames
= NULL
; // Ditto
242 pd
.Flags
= PD_RETURNDEFAULT
;
245 if (!PrintDlg((LPPRINTDLG
)&pd
))
248 GlobalFree(pd
.hDevMode
);
250 GlobalFree(pd
.hDevNames
);
257 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
258 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
259 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
260 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
262 deviceName
= lpszDeviceName
;
263 portName
= lpszPortName
;
265 GlobalUnlock(pd
.hDevNames
);
266 GlobalFree(pd
.hDevNames
);
272 GlobalFree(pd
.hDevMode
);
275 return ( deviceName
!= wxT("") );
279 // This uses defaults, except for orientation, so we should eliminate this function
280 // and use the 2nd form (passing wxPrintData) instead.
281 WXHDC
wxGetPrinterDC(int orientation
)
284 LPDEVMODE lpDevMode
= NULL
;
285 LPDEVNAMES lpDevNames
;
286 LPSTR lpszDriverName
;
287 LPSTR lpszDeviceName
;
291 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
293 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
295 pd
.lStructSize
= sizeof(PRINTDLG
);
297 pd
.hwndOwner
= (HWND
)NULL
;
298 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
299 pd
.hDevNames
= NULL
; // Ditto
300 pd
.Flags
= PD_RETURNDEFAULT
;
303 if (!PrintDlg((LPPRINTDLG
)&pd
))
306 GlobalFree(pd
.hDevMode
);
308 GlobalFree(pd
.hDevNames
);
316 GlobalFree(pd
.hDevMode
);
319 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
320 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
321 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
322 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
323 GlobalUnlock(pd
.hDevNames
);
327 lpDevMode
= (DEVMODE
*) GlobalLock(pd
.hDevMode
);
328 lpDevMode
->dmOrientation
= orientation
;
329 lpDevMode
->dmFields
|= DM_ORIENTATION
;
333 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (DEVMODE
*)lpDevMode
);
335 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (LPSTR
)lpDevMode
);
338 if (pd
.hDevMode
&& lpDevMode
)
339 GlobalUnlock(pd
.hDevMode
);
343 GlobalFree(pd
.hDevNames
);
348 GlobalFree(pd
.hDevMode
);
355 // Gets an HDC for the specified printer configuration
356 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
358 wxPrintData printData
= printDataConst
;
359 printData
.ConvertToNative();
361 wxChar
* driverName
= (wxChar
*) NULL
;
363 wxString devNameStr
= printData
.GetPrinterName();
364 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
366 const wxChar
* deviceName
;
368 deviceName
= (wxChar
*) NULL
;
370 deviceName
= devNameStr
.c_str();
372 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
374 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) printData
.GetNativeData();
377 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
381 // Retrieve the default device name
387 #endif // Debug/Release
388 wxGetDefaultDeviceName(devNameStr
, portName
);
390 wxASSERT_MSG( ret
, wxT("Could not get default device name.") );
392 deviceName
= devNameStr
.c_str();
396 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
398 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
401 if (hDevMode
&& lpDevMode
)
402 GlobalUnlock(hDevMode
);
407 // ----------------------------------------------------------------------------
408 // wxPrinterDC bit blitting/bitmap drawing
409 // ----------------------------------------------------------------------------
411 void wxPrinterDC::DoDrawBitmap(const wxBitmap
&bmp
,
412 wxCoord x
, wxCoord y
,
415 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
417 int width
= bmp
.GetWidth(),
418 height
= bmp
.GetHeight();
420 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
)
422 BITMAPINFO
*info
= (BITMAPINFO
*) malloc( sizeof( BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
) );
423 memset( info
, 0, sizeof( BITMAPINFOHEADER
) );
425 int iBitsSize
= ((width
+ 3 ) & ~3 ) * height
;
427 void* bits
= malloc( iBitsSize
);
429 info
->bmiHeader
.biSize
= sizeof( BITMAPINFOHEADER
);
430 info
->bmiHeader
.biWidth
= width
;
431 info
->bmiHeader
.biHeight
= height
;
432 info
->bmiHeader
.biPlanes
= 1;
433 info
->bmiHeader
.biBitCount
= 8;
434 info
->bmiHeader
.biCompression
= BI_RGB
;
437 if ( GetDIBits(display
, GetHbitmapOf(bmp
), 0,
438 bmp
.GetHeight(), bits
, info
,
441 if ( ::StretchDIBits(GetHdc(), x
, y
,
443 0 , 0, width
, height
,
445 DIB_RGB_COLORS
, SRCCOPY
) == GDI_ERROR
)
447 wxLogLastError("StretchDIBits");
454 else // no support for StretchDIBits()
457 memDC
.SelectObject(bmp
);
459 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
461 memDC
.SelectObject(wxNullBitmap
);
465 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
466 wxCoord width
, wxCoord height
,
468 wxCoord xsrc
, wxCoord ysrc
,
469 int rop
, bool useMask
)
475 // If we are printing source colours are screen colours
476 // not printer colours and so we need copy the bitmap
479 HDC dc_src
= GetHdcOf(*source
);
480 HDC dc_mask
= ::CreateCompatibleDC(dc_src
);
482 ::SelectObject(dc_mask
, (HBITMAP
) source
->GetSelectedBitmap().GetMask()->GetMaskBitmap());
483 for (int x
= 0; x
< width
; x
++)
485 for (int y
= 0; y
< height
; y
++)
487 COLORREF cref
= ::GetPixel(dc_mask
, x
, y
);
490 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dc_src
, x
, y
));
491 rect
.left
= xdest
+ x
;
492 rect
.right
= rect
.left
+ 1;
493 rect
.top
= ydest
+ y
;
494 rect
.bottom
= rect
.top
+ 1;
495 ::FillRect(GetHdc(), &rect
, brush
);
496 ::DeleteObject(brush
);
500 ::SelectObject(dc_mask
, 0);
505 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
)
507 wxBitmap
& bmp
= source
->GetSelectedBitmap();
508 int width
= bmp
.GetWidth(),
509 height
= bmp
.GetHeight();
511 BITMAPINFO
*info
= (BITMAPINFO
*) malloc( sizeof( BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
) );
512 int iBitsSize
= ((width
+ 3 ) & ~3 ) * height
;
514 void* bits
= malloc( iBitsSize
);
516 memset( info
, 0 , sizeof( BITMAPINFOHEADER
) );
518 info
->bmiHeader
.biSize
= sizeof( BITMAPINFOHEADER
);
519 info
->bmiHeader
.biWidth
= width
;
520 info
->bmiHeader
.biHeight
= height
;
521 info
->bmiHeader
.biPlanes
= 1;
522 info
->bmiHeader
.biBitCount
= 8;
523 info
->bmiHeader
.biCompression
= BI_RGB
;
526 if ( !::GetDIBits(display
, GetHbitmapOf(bmp
), 0,
527 height
, bits
, info
, DIB_RGB_COLORS
) )
529 wxLogLastError("GetDIBits");
536 success
= ::StretchDIBits(GetHdc(), xdest
, ydest
,
542 SRCCOPY
) != GDI_ERROR
;
545 wxLogLastError("StretchDIBits");
552 else // no support for StretchDIBits
554 // as we are printing, source colours are screen colours not printer
555 // colours and so we need copy the bitmap pixel by pixel.
556 HDC dc_src
= GetHdcOf(*source
);
558 for (int y
= 0; y
< height
; y
++)
560 // This is Stefan Csomor's optimisation, where identical adjacent
561 // pixels are drawn together.
562 for (int x
= 0; x
< width
; x
++)
564 COLORREF col
= ::GetPixel(dc_src
, x
, y
);
565 HBRUSH brush
= ::CreateSolidBrush( col
);
567 rect
.left
= xdest
+ x
;
568 rect
.top
= ydest
+ y
;
569 while( (x
+ 1 < width
) && (::GetPixel(dc_src
, x
+ 1, y
) == col
) )
573 rect
.right
= xdest
+ x
+ 1;
574 rect
.bottom
= rect
.top
+ 1;
575 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
576 ::DeleteObject(brush
);