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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
43 #include "wx/msw/dib.h"
46 #include "wx/dcprint.h"
47 #include "wx/printdlg.h"
48 #include "wx/msw/printdlg.h"
51 #include "wx/msw/wrapcdlg.h"
56 // mingw32 defines GDI_ERROR incorrectly
57 #if defined(__GNUWIN32__) || !defined(GDI_ERROR)
59 #define GDI_ERROR ((int)-1)
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wxPrinterDC construction
74 // ----------------------------------------------------------------------------
76 // This form is deprecated
77 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
,
78 const wxString
& device_name
,
83 m_isInteractive
= interactive
;
86 m_printData
.SetFilename(file
);
88 #if wxUSE_COMMON_DIALOGS
93 pd
.lStructSize
= sizeof( PRINTDLG
);
94 pd
.hwndOwner
= (HWND
) NULL
;
95 pd
.hDevMode
= (HANDLE
)NULL
;
96 pd
.hDevNames
= (HANDLE
)NULL
;
97 pd
.Flags
= PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
103 pd
.hInstance
= (HINSTANCE
)NULL
;
105 m_ok
= PrintDlg( &pd
) != 0;
108 m_hDC
= (WXHDC
) pd
.hDC
;
112 #endif // wxUSE_COMMON_DIALOGS
114 if ( !driver_name
.empty() && !device_name
.empty() && !file
.empty() )
116 m_hDC
= (WXHDC
) CreateDC(driver_name
, device_name
, file
, NULL
);
118 else // we don't have all parameters, ask the user
120 wxPrintData printData
;
121 printData
.SetOrientation(orientation
);
122 m_hDC
= wxGetPrinterDC(printData
);
125 m_ok
= m_hDC
? true: false;
127 // as we created it, we must delete it as well
134 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
136 m_printData
= printData
;
138 m_isInteractive
= false;
140 m_hDC
= wxGetPrinterDC(printData
);
148 wxPrinterDC::wxPrinterDC(WXHDC dc
)
150 m_isInteractive
= false;
157 void wxPrinterDC::Init()
161 // int width = GetDeviceCaps(m_hDC, VERTRES);
162 // int height = GetDeviceCaps(m_hDC, HORZRES);
163 SetMapMode(wxMM_TEXT
);
165 SetBrush(*wxBLACK_BRUSH
);
166 SetPen(*wxBLACK_PEN
);
170 // ----------------------------------------------------------------------------
171 // wxPrinterDC {Start/End}{Page/Doc} methods
172 // ----------------------------------------------------------------------------
174 bool wxPrinterDC::StartDoc(const wxString
& message
)
177 docinfo
.cbSize
= sizeof(DOCINFO
);
178 docinfo
.lpszDocName
= (const wxChar
*)message
;
180 wxString
filename(m_printData
.GetFilename());
182 if (filename
.empty())
183 docinfo
.lpszOutput
= NULL
;
185 docinfo
.lpszOutput
= (const wxChar
*) filename
;
187 #if defined(__WIN95__)
188 docinfo
.lpszDatatype
= NULL
;
195 int ret
= ::StartDoc(GetHdc(), &docinfo
);
199 DWORD lastError
= GetLastError();
200 wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError
);
206 void wxPrinterDC::EndDoc()
208 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
211 void wxPrinterDC::StartPage()
214 ::StartPage((HDC
) m_hDC
);
217 void wxPrinterDC::EndPage()
220 ::EndPage((HDC
) m_hDC
);
223 // Returns default device and port names
224 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
228 LPDEVNAMES lpDevNames
;
229 LPTSTR lpszDeviceName
;
234 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
237 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
239 memset(&pd
, 0, sizeof(PRINTDLG
));
240 pd
.lStructSize
= sizeof(PRINTDLG
);
243 pd
.hwndOwner
= (HWND
)NULL
;
244 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
245 pd
.hDevNames
= NULL
; // Ditto
246 pd
.Flags
= PD_RETURNDEFAULT
;
249 if (!PrintDlg((LPPRINTDLG
)&pd
))
252 GlobalFree(pd
.hDevMode
);
254 GlobalFree(pd
.hDevNames
);
261 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
262 lpszDeviceName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
263 lpszPortName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
265 deviceName
= lpszDeviceName
;
266 portName
= lpszPortName
;
268 GlobalUnlock(pd
.hDevNames
);
269 GlobalFree(pd
.hDevNames
);
275 GlobalFree(pd
.hDevMode
);
278 return ( !deviceName
.empty() );
281 // Gets an HDC for the specified printer configuration
282 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
284 #if defined(__WXUNIVERSAL__) && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
287 wxPostScriptPrintNativeData
*data
=
288 (wxPostScriptPrintNativeData
*) printDataConst
.GetNativeData();
289 // FIXME: how further ???
294 #else // Postscript vs. native Windows
296 wxWindowsPrintNativeData
*data
=
297 (wxWindowsPrintNativeData
*) printDataConst
.GetNativeData();
299 data
->TransferFrom( printDataConst
);
301 wxChar
* driverName
= (wxChar
*) NULL
;
303 wxString devNameStr
= printDataConst
.GetPrinterName();
304 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
306 const wxChar
* deviceName
;
308 deviceName
= (wxChar
*) NULL
;
310 deviceName
= devNameStr
.c_str();
312 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
314 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) data
->GetDevMode();
317 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
321 // Retrieve the default device name
323 if ( !wxGetDefaultDeviceName(devNameStr
, portName
) )
325 return 0; // Could not get default device name
327 deviceName
= devNameStr
.c_str();
331 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
333 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
336 if (hDevMode
&& lpDevMode
)
337 GlobalUnlock(hDevMode
);
343 // ----------------------------------------------------------------------------
344 // wxPrinterDC bit blitting/bitmap drawing
345 // ----------------------------------------------------------------------------
347 // helper of DoDrawBitmap() and DoBlit()
349 bool DrawBitmapUsingStretchDIBits(HDC hdc
,
351 wxCoord x
, wxCoord y
)
355 bool ok
= dib
.IsOk();
360 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
362 wxLogLastError(_T("GetObject(DIBSECTION)"));
367 // ok, we've got all data we need, do blit it
372 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
374 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
376 (LPBITMAPINFO
)&ds
.dsBmih
,
381 wxLogLastError(wxT("StretchDIBits"));
392 void wxPrinterDC::DoDrawBitmap(const wxBitmap
& bmp
,
393 wxCoord x
, wxCoord y
,
396 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
398 int width
= bmp
.GetWidth(),
399 height
= bmp
.GetHeight();
401 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
402 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, x
, y
) )
404 // no support for StretchDIBits() or an error occured if we got here
406 memDC
.SelectObject(bmp
);
408 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
410 memDC
.SelectObject(wxNullBitmap
);
414 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
415 wxCoord width
, wxCoord height
,
417 wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
),
418 int WXUNUSED(rop
), bool useMask
,
419 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
))
421 wxBitmap
& bmp
= source
->GetSelectedBitmap();
422 wxMask
*mask
= useMask
? bmp
.GetMask() : NULL
;
425 // If we are printing source colours are screen colours not printer
426 // colours and so we need copy the bitmap pixel by pixel.
428 HDC dcSrc
= GetHdcOf(*source
);
429 MemoryHDC
dcMask(dcSrc
);
430 SelectInHDC
selectMask(dcMask
, (HBITMAP
)mask
->GetMaskBitmap());
432 for (int x
= 0; x
< width
; x
++)
434 for (int y
= 0; y
< height
; y
++)
436 COLORREF cref
= ::GetPixel(dcMask
, x
, y
);
439 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dcSrc
, x
, y
));
440 rect
.left
= xdest
+ x
;
441 rect
.right
= rect
.left
+ 1;
442 rect
.top
= ydest
+ y
;
443 rect
.bottom
= rect
.top
+ 1;
444 ::FillRect(GetHdc(), &rect
, brush
);
445 ::DeleteObject(brush
);
452 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
453 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, xdest
, ydest
) )
455 // no support for StretchDIBits
457 // as we are printing, source colours are screen colours not
458 // printer colours and so we need copy the bitmap pixel by pixel.
459 HDC dcSrc
= GetHdcOf(*source
);
461 for (int y
= 0; y
< height
; y
++)
463 // optimization: draw identical adjacent pixels together.
464 for (int x
= 0; x
< width
; x
++)
466 COLORREF col
= ::GetPixel(dcSrc
, x
, y
);
467 HBRUSH brush
= ::CreateSolidBrush( col
);
469 rect
.left
= xdest
+ x
;
470 rect
.top
= ydest
+ y
;
471 while( (x
+ 1 < width
) &&
472 (::GetPixel(dcSrc
, x
+ 1, y
) == col
) )
476 rect
.right
= xdest
+ x
+ 1;
477 rect
.bottom
= rect
.top
+ 1;
478 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
479 ::DeleteObject(brush
);
489 // wxUSE_PRINTING_ARCHITECTURE