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"
27 #if wxUSE_PRINTING_ARCHITECTURE
29 #include "wx/dcprint.h"
32 #include "wx/msw/wrapcdlg.h"
33 #include "wx/string.h"
35 #include "wx/window.h"
36 #include "wx/dcmemory.h"
40 #include "wx/msw/private.h"
43 #include "wx/msw/dib.h"
46 #include "wx/printdlg.h"
47 #include "wx/msw/printdlg.h"
53 // mingw32 defines GDI_ERROR incorrectly
54 #if defined(__GNUWIN32__) || !defined(GDI_ERROR)
56 #define GDI_ERROR ((int)-1)
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
65 // ============================================================================
67 // ============================================================================
69 // ----------------------------------------------------------------------------
70 // wxPrinterDC construction
71 // ----------------------------------------------------------------------------
73 // This form is deprecated
74 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
,
75 const wxString
& device_name
,
80 m_isInteractive
= interactive
;
83 m_printData
.SetFilename(file
);
85 #if wxUSE_COMMON_DIALOGS
90 pd
.lStructSize
= sizeof( PRINTDLG
);
91 pd
.hwndOwner
= (HWND
) NULL
;
92 pd
.hDevMode
= (HANDLE
)NULL
;
93 pd
.hDevNames
= (HANDLE
)NULL
;
94 pd
.Flags
= PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
100 pd
.hInstance
= (HINSTANCE
)NULL
;
102 m_ok
= PrintDlg( &pd
) != 0;
105 m_hDC
= (WXHDC
) pd
.hDC
;
109 #endif // wxUSE_COMMON_DIALOGS
111 if ( !driver_name
.empty() && !device_name
.empty() && !file
.empty() )
113 m_hDC
= (WXHDC
) CreateDC(driver_name
, device_name
, file
, NULL
);
115 else // we don't have all parameters, ask the user
117 wxPrintData printData
;
118 printData
.SetOrientation(orientation
);
119 m_hDC
= wxGetPrinterDC(printData
);
122 m_ok
= m_hDC
? true: false;
124 // as we created it, we must delete it as well
131 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
133 m_printData
= printData
;
135 m_isInteractive
= false;
137 m_hDC
= wxGetPrinterDC(printData
);
145 wxPrinterDC::wxPrinterDC(WXHDC dc
)
147 m_isInteractive
= false;
154 void wxPrinterDC::Init()
158 // int width = GetDeviceCaps(m_hDC, VERTRES);
159 // int height = GetDeviceCaps(m_hDC, HORZRES);
160 SetMapMode(wxMM_TEXT
);
162 SetBrush(*wxBLACK_BRUSH
);
163 SetPen(*wxBLACK_PEN
);
167 // ----------------------------------------------------------------------------
168 // wxPrinterDC {Start/End}{Page/Doc} methods
169 // ----------------------------------------------------------------------------
171 bool wxPrinterDC::StartDoc(const wxString
& message
)
174 docinfo
.cbSize
= sizeof(DOCINFO
);
175 docinfo
.lpszDocName
= (const wxChar
*)message
;
177 wxString
filename(m_printData
.GetFilename());
179 if (filename
.empty())
180 docinfo
.lpszOutput
= NULL
;
182 docinfo
.lpszOutput
= (const wxChar
*) filename
;
184 docinfo
.lpszDatatype
= NULL
;
190 int ret
= ::StartDoc(GetHdc(), &docinfo
);
194 DWORD lastError
= GetLastError();
195 wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError
);
201 void wxPrinterDC::EndDoc()
203 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
206 void wxPrinterDC::StartPage()
209 ::StartPage((HDC
) m_hDC
);
212 void wxPrinterDC::EndPage()
215 ::EndPage((HDC
) m_hDC
);
218 // Returns default device and port names
219 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
223 LPDEVNAMES lpDevNames
;
224 LPTSTR lpszDeviceName
;
229 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
232 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
234 memset(&pd
, 0, sizeof(PRINTDLG
));
235 pd
.lStructSize
= sizeof(PRINTDLG
);
238 pd
.hwndOwner
= (HWND
)NULL
;
239 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
240 pd
.hDevNames
= NULL
; // Ditto
241 pd
.Flags
= PD_RETURNDEFAULT
;
244 if (!PrintDlg((LPPRINTDLG
)&pd
))
247 GlobalFree(pd
.hDevMode
);
249 GlobalFree(pd
.hDevNames
);
256 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
257 lpszDeviceName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
258 lpszPortName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
260 deviceName
= lpszDeviceName
;
261 portName
= lpszPortName
;
263 GlobalUnlock(pd
.hDevNames
);
264 GlobalFree(pd
.hDevNames
);
270 GlobalFree(pd
.hDevMode
);
273 return ( !deviceName
.empty() );
276 // Gets an HDC for the specified printer configuration
277 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
279 #if defined(__WXUNIVERSAL__) && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
282 wxPostScriptPrintNativeData
*data
=
283 (wxPostScriptPrintNativeData
*) printDataConst
.GetNativeData();
284 // FIXME: how further ???
289 #else // Postscript vs. native Windows
291 wxWindowsPrintNativeData
*data
=
292 (wxWindowsPrintNativeData
*) printDataConst
.GetNativeData();
294 data
->TransferFrom( printDataConst
);
296 wxChar
* driverName
= (wxChar
*) NULL
;
298 wxString devNameStr
= printDataConst
.GetPrinterName();
299 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
301 const wxChar
* deviceName
;
303 deviceName
= (wxChar
*) NULL
;
305 deviceName
= devNameStr
.c_str();
307 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
309 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) data
->GetDevMode();
312 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
316 // Retrieve the default device name
318 if ( !wxGetDefaultDeviceName(devNameStr
, portName
) )
320 return 0; // Could not get default device name
322 deviceName
= devNameStr
.c_str();
326 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
328 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
331 if (hDevMode
&& lpDevMode
)
332 GlobalUnlock(hDevMode
);
338 // ----------------------------------------------------------------------------
339 // wxPrinterDC bit blitting/bitmap drawing
340 // ----------------------------------------------------------------------------
342 // helper of DoDrawBitmap() and DoBlit()
344 bool DrawBitmapUsingStretchDIBits(HDC hdc
,
346 wxCoord x
, wxCoord y
)
350 bool ok
= dib
.IsOk();
355 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
357 wxLogLastError(_T("GetObject(DIBSECTION)"));
362 // ok, we've got all data we need, do blit it
367 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
369 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
371 (LPBITMAPINFO
)&ds
.dsBmih
,
376 wxLogLastError(wxT("StretchDIBits"));
387 void wxPrinterDC::DoDrawBitmap(const wxBitmap
& bmp
,
388 wxCoord x
, wxCoord y
,
391 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
393 int width
= bmp
.GetWidth(),
394 height
= bmp
.GetHeight();
396 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
397 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, x
, y
) )
399 // no support for StretchDIBits() or an error occurred if we got here
401 memDC
.SelectObject(bmp
);
403 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
405 memDC
.SelectObject(wxNullBitmap
);
409 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
410 wxCoord width
, wxCoord height
,
412 wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
),
413 int WXUNUSED(rop
), bool useMask
,
414 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
))
416 wxBitmap
& bmp
= source
->GetSelectedBitmap();
417 wxMask
*mask
= useMask
? bmp
.GetMask() : NULL
;
420 // If we are printing source colours are screen colours not printer
421 // colours and so we need copy the bitmap pixel by pixel.
423 HDC dcSrc
= GetHdcOf(*source
);
424 MemoryHDC
dcMask(dcSrc
);
425 SelectInHDC
selectMask(dcMask
, (HBITMAP
)mask
->GetMaskBitmap());
427 for (int x
= 0; x
< width
; x
++)
429 for (int y
= 0; y
< height
; y
++)
431 COLORREF cref
= ::GetPixel(dcMask
, x
, y
);
434 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dcSrc
, x
, y
));
435 rect
.left
= xdest
+ x
;
436 rect
.right
= rect
.left
+ 1;
437 rect
.top
= ydest
+ y
;
438 rect
.bottom
= rect
.top
+ 1;
439 ::FillRect(GetHdc(), &rect
, brush
);
440 ::DeleteObject(brush
);
447 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
448 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, xdest
, ydest
) )
450 // no support for StretchDIBits
452 // as we are printing, source colours are screen colours not
453 // printer colours and so we need copy the bitmap pixel by pixel.
454 HDC dcSrc
= GetHdcOf(*source
);
456 for (int y
= 0; y
< height
; y
++)
458 // optimization: draw identical adjacent pixels together.
459 for (int x
= 0; x
< width
; x
++)
461 COLORREF col
= ::GetPixel(dcSrc
, x
, y
);
462 HBRUSH brush
= ::CreateSolidBrush( col
);
464 rect
.left
= xdest
+ x
;
465 rect
.top
= ydest
+ y
;
466 while( (x
+ 1 < width
) &&
467 (::GetPixel(dcSrc
, x
+ 1, y
) == col
) )
471 rect
.right
= xdest
+ x
+ 1;
472 rect
.bottom
= rect
.top
+ 1;
473 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
474 ::DeleteObject(brush
);
484 // wxUSE_PRINTING_ARCHITECTURE