1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dcprint.cpp
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
28 #include "wx/string.h"
30 #include "wx/window.h"
31 #include "wx/dcmemory.h"
34 #if wxUSE_PRINTING_ARCHITECTURE
36 #include "wx/msw/private.h"
39 #include "wx/msw/dib.h"
42 #include "wx/dcprint.h"
43 #include "wx/printdlg.h"
44 #include "wx/msw/printdlg.h"
47 #include "wx/msw/wrapcdlg.h"
52 // mingw32 defines GDI_ERROR incorrectly
53 #if defined(__GNUWIN32__) || !defined(GDI_ERROR)
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
,
74 const wxString
& device_name
,
79 m_isInteractive
= interactive
;
82 m_printData
.SetFilename(file
);
84 #if wxUSE_COMMON_DIALOGS
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
;
99 pd
.hInstance
= (HINSTANCE
)NULL
;
101 m_ok
= PrintDlg( &pd
) != 0;
104 m_hDC
= (WXHDC
) pd
.hDC
;
108 #endif // wxUSE_COMMON_DIALOGS
110 if ( !driver_name
.empty() && !device_name
.empty() && !file
.empty() )
112 m_hDC
= (WXHDC
) CreateDC(driver_name
, device_name
, file
, NULL
);
114 else // we don't have all parameters, ask the user
116 wxPrintData printData
;
117 printData
.SetOrientation(orientation
);
118 m_hDC
= wxGetPrinterDC(printData
);
121 m_ok
= m_hDC
? true: false;
123 // as we created it, we must delete it as well
130 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
132 m_printData
= printData
;
134 m_isInteractive
= false;
136 m_hDC
= wxGetPrinterDC(printData
);
144 wxPrinterDC::wxPrinterDC(WXHDC dc
)
146 m_isInteractive
= false;
153 void wxPrinterDC::Init()
157 // int width = GetDeviceCaps(m_hDC, VERTRES);
158 // int height = GetDeviceCaps(m_hDC, HORZRES);
159 SetMapMode(wxMM_TEXT
);
161 SetBrush(*wxBLACK_BRUSH
);
162 SetPen(*wxBLACK_PEN
);
166 // ----------------------------------------------------------------------------
167 // wxPrinterDC {Start/End}{Page/Doc} methods
168 // ----------------------------------------------------------------------------
170 bool wxPrinterDC::StartDoc(const wxString
& message
)
173 docinfo
.cbSize
= sizeof(DOCINFO
);
174 docinfo
.lpszDocName
= (const wxChar
*)message
;
176 wxString
filename(m_printData
.GetFilename());
178 if (filename
.empty())
179 docinfo
.lpszOutput
= NULL
;
181 docinfo
.lpszOutput
= (const wxChar
*) filename
;
183 docinfo
.lpszDatatype
= NULL
;
189 int ret
= ::StartDoc(GetHdc(), &docinfo
);
193 DWORD lastError
= GetLastError();
194 wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError
);
200 void wxPrinterDC::EndDoc()
202 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
205 void wxPrinterDC::StartPage()
208 ::StartPage((HDC
) m_hDC
);
211 void wxPrinterDC::EndPage()
214 ::EndPage((HDC
) m_hDC
);
217 // Returns default device and port names
218 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
222 LPDEVNAMES lpDevNames
;
223 LPTSTR lpszDeviceName
;
228 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
231 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
233 memset(&pd
, 0, sizeof(PRINTDLG
));
234 pd
.lStructSize
= sizeof(PRINTDLG
);
237 pd
.hwndOwner
= (HWND
)NULL
;
238 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
239 pd
.hDevNames
= NULL
; // Ditto
240 pd
.Flags
= PD_RETURNDEFAULT
;
243 if (!PrintDlg((LPPRINTDLG
)&pd
))
246 GlobalFree(pd
.hDevMode
);
248 GlobalFree(pd
.hDevNames
);
255 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
256 lpszDeviceName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
257 lpszPortName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
259 deviceName
= lpszDeviceName
;
260 portName
= lpszPortName
;
262 GlobalUnlock(pd
.hDevNames
);
263 GlobalFree(pd
.hDevNames
);
269 GlobalFree(pd
.hDevMode
);
272 return ( !deviceName
.empty() );
275 // Gets an HDC for the specified printer configuration
276 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
278 #if defined(__WXUNIVERSAL__) && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
281 wxPostScriptPrintNativeData
*data
=
282 (wxPostScriptPrintNativeData
*) printDataConst
.GetNativeData();
283 // FIXME: how further ???
288 #else // Postscript vs. native Windows
290 wxWindowsPrintNativeData
*data
=
291 (wxWindowsPrintNativeData
*) printDataConst
.GetNativeData();
293 data
->TransferFrom( printDataConst
);
295 wxChar
* driverName
= (wxChar
*) NULL
;
297 wxString devNameStr
= printDataConst
.GetPrinterName();
298 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
300 const wxChar
* deviceName
;
302 deviceName
= (wxChar
*) NULL
;
304 deviceName
= devNameStr
.c_str();
306 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
308 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) data
->GetDevMode();
311 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
315 // Retrieve the default device name
317 if ( !wxGetDefaultDeviceName(devNameStr
, portName
) )
319 return 0; // Could not get default device name
321 deviceName
= devNameStr
.c_str();
325 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
327 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
330 if (hDevMode
&& lpDevMode
)
331 GlobalUnlock(hDevMode
);
337 // ----------------------------------------------------------------------------
338 // wxPrinterDC bit blitting/bitmap drawing
339 // ----------------------------------------------------------------------------
341 // helper of DoDrawBitmap() and DoBlit()
343 bool DrawBitmapUsingStretchDIBits(HDC hdc
,
345 wxCoord x
, wxCoord y
)
349 bool ok
= dib
.IsOk();
354 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
356 wxLogLastError(_T("GetObject(DIBSECTION)"));
361 // ok, we've got all data we need, do blit it
366 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
368 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
370 (LPBITMAPINFO
)&ds
.dsBmih
,
375 wxLogLastError(wxT("StretchDIBits"));
386 void wxPrinterDC::DoDrawBitmap(const wxBitmap
& bmp
,
387 wxCoord x
, wxCoord y
,
390 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
392 int width
= bmp
.GetWidth(),
393 height
= bmp
.GetHeight();
395 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
396 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, x
, y
) )
398 // no support for StretchDIBits() or an error occurred if we got here
400 memDC
.SelectObject(bmp
);
402 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
404 memDC
.SelectObject(wxNullBitmap
);
408 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
409 wxCoord width
, wxCoord height
,
411 wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
),
412 int WXUNUSED(rop
), bool useMask
,
413 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
))
415 wxBitmap
& bmp
= source
->GetSelectedBitmap();
416 wxMask
*mask
= useMask
? bmp
.GetMask() : NULL
;
419 // If we are printing source colours are screen colours not printer
420 // colours and so we need copy the bitmap pixel by pixel.
422 HDC dcSrc
= GetHdcOf(*source
);
423 MemoryHDC
dcMask(dcSrc
);
424 SelectInHDC
selectMask(dcMask
, (HBITMAP
)mask
->GetMaskBitmap());
426 for (int x
= 0; x
< width
; x
++)
428 for (int y
= 0; y
< height
; y
++)
430 COLORREF cref
= ::GetPixel(dcMask
, x
, y
);
433 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dcSrc
, x
, y
));
434 rect
.left
= xdest
+ x
;
435 rect
.right
= rect
.left
+ 1;
436 rect
.top
= ydest
+ y
;
437 rect
.bottom
= rect
.top
+ 1;
438 ::FillRect(GetHdc(), &rect
, brush
);
439 ::DeleteObject(brush
);
446 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
447 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, xdest
, ydest
) )
449 // no support for StretchDIBits
451 // as we are printing, source colours are screen colours not
452 // printer colours and so we need copy the bitmap pixel by pixel.
453 HDC dcSrc
= GetHdcOf(*source
);
455 for (int y
= 0; y
< height
; y
++)
457 // optimization: draw identical adjacent pixels together.
458 for (int x
= 0; x
< width
; x
++)
460 COLORREF col
= ::GetPixel(dcSrc
, x
, y
);
461 HBRUSH brush
= ::CreateSolidBrush( col
);
463 rect
.left
= xdest
+ x
;
464 rect
.top
= ydest
+ y
;
465 while( (x
+ 1 < width
) &&
466 (::GetPixel(dcSrc
, x
+ 1, y
) == col
) )
470 rect
.right
= xdest
+ x
+ 1;
471 rect
.bottom
= rect
.top
+ 1;
472 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
473 ::DeleteObject(brush
);
483 // wxUSE_PRINTING_ARCHITECTURE