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"
50 #if wxUSE_COMMON_DIALOGS
58 // mingw32 defines GDI_ERROR incorrectly
59 #if defined(__GNUWIN32__) || !defined(GDI_ERROR)
61 #define GDI_ERROR ((int)-1)
64 // ----------------------------------------------------------------------------
66 // ----------------------------------------------------------------------------
68 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
70 // ============================================================================
72 // ============================================================================
74 // ----------------------------------------------------------------------------
75 // wxPrinterDC construction
76 // ----------------------------------------------------------------------------
78 // This form is deprecated
79 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
,
80 const wxString
& device_name
,
85 m_isInteractive
= interactive
;
88 m_printData
.SetFilename(file
);
90 #if wxUSE_COMMON_DIALOGS
95 pd
.lStructSize
= sizeof( PRINTDLG
);
96 pd
.hwndOwner
= (HWND
) NULL
;
97 pd
.hDevMode
= (HANDLE
)NULL
;
98 pd
.hDevNames
= (HANDLE
)NULL
;
99 pd
.Flags
= PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
105 pd
.hInstance
= (HINSTANCE
)NULL
;
107 m_ok
= PrintDlg( &pd
) != 0;
110 m_hDC
= (WXHDC
) pd
.hDC
;
114 #endif // wxUSE_COMMON_DIALOGS
116 if ( !driver_name
.empty() && !device_name
.empty() && !file
.empty() )
118 m_hDC
= (WXHDC
) CreateDC(driver_name
, device_name
, file
, NULL
);
120 else // we don't have all parameters, ask the user
122 wxPrintData printData
;
123 printData
.SetOrientation(orientation
);
124 m_hDC
= wxGetPrinterDC(printData
);
127 m_ok
= m_hDC
? true: false;
129 // as we created it, we must delete it as well
136 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
138 m_printData
= printData
;
140 m_isInteractive
= false;
142 m_hDC
= wxGetPrinterDC(printData
);
150 wxPrinterDC::wxPrinterDC(WXHDC dc
)
152 m_isInteractive
= false;
159 void wxPrinterDC::Init()
163 // int width = GetDeviceCaps(m_hDC, VERTRES);
164 // int height = GetDeviceCaps(m_hDC, HORZRES);
165 SetMapMode(wxMM_TEXT
);
167 SetBrush(*wxBLACK_BRUSH
);
168 SetPen(*wxBLACK_PEN
);
172 // ----------------------------------------------------------------------------
173 // wxPrinterDC {Start/End}{Page/Doc} methods
174 // ----------------------------------------------------------------------------
176 bool wxPrinterDC::StartDoc(const wxString
& message
)
179 docinfo
.cbSize
= sizeof(DOCINFO
);
180 docinfo
.lpszDocName
= (const wxChar
*)message
;
182 wxString
filename(m_printData
.GetFilename());
184 if (filename
.IsEmpty())
185 docinfo
.lpszOutput
= NULL
;
187 docinfo
.lpszOutput
= (const wxChar
*) filename
;
189 #if defined(__WIN95__)
190 docinfo
.lpszDatatype
= NULL
;
197 int ret
= ::StartDoc(GetHdc(), &docinfo
);
201 DWORD lastError
= GetLastError();
202 wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError
);
208 void wxPrinterDC::EndDoc()
210 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
213 void wxPrinterDC::StartPage()
216 ::StartPage((HDC
) m_hDC
);
219 void wxPrinterDC::EndPage()
222 ::EndPage((HDC
) m_hDC
);
225 // Returns default device and port names
226 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
230 LPDEVNAMES lpDevNames
;
231 LPTSTR lpszDeviceName
;
236 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
239 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
241 memset(&pd
, 0, sizeof(PRINTDLG
));
242 pd
.lStructSize
= sizeof(PRINTDLG
);
245 pd
.hwndOwner
= (HWND
)NULL
;
246 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
247 pd
.hDevNames
= NULL
; // Ditto
248 pd
.Flags
= PD_RETURNDEFAULT
;
251 if (!PrintDlg((LPPRINTDLG
)&pd
))
254 GlobalFree(pd
.hDevMode
);
256 GlobalFree(pd
.hDevNames
);
263 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
264 lpszDeviceName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
265 lpszPortName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
267 deviceName
= lpszDeviceName
;
268 portName
= lpszPortName
;
270 GlobalUnlock(pd
.hDevNames
);
271 GlobalFree(pd
.hDevNames
);
277 GlobalFree(pd
.hDevMode
);
280 return ( deviceName
!= wxEmptyString
);
283 // Gets an HDC for the specified printer configuration
284 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
286 #if defined(__WXUNIVERSAL__) && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW)
289 wxPostScriptPrintNativeData
*data
=
290 (wxPostScriptPrintNativeData
*) printDataConst
.GetNativeData();
291 // FIXME: how further ???
296 #else // Postscript vs. native Windows
298 wxWindowsPrintNativeData
*data
=
299 (wxWindowsPrintNativeData
*) printDataConst
.GetNativeData();
301 data
->TransferFrom( printDataConst
);
303 wxChar
* driverName
= (wxChar
*) NULL
;
305 wxString devNameStr
= printDataConst
.GetPrinterName();
306 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
308 const wxChar
* deviceName
;
310 deviceName
= (wxChar
*) NULL
;
312 deviceName
= devNameStr
.c_str();
314 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
316 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) data
->GetDevMode();
319 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
323 // Retrieve the default device name
325 if ( !wxGetDefaultDeviceName(devNameStr
, portName
) )
327 return 0; // Could not get default device name
329 deviceName
= devNameStr
.c_str();
333 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
335 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
338 if (hDevMode
&& lpDevMode
)
339 GlobalUnlock(hDevMode
);
345 // ----------------------------------------------------------------------------
346 // wxPrinterDC bit blitting/bitmap drawing
347 // ----------------------------------------------------------------------------
349 // helper of DoDrawBitmap() and DoBlit()
351 bool DrawBitmapUsingStretchDIBits(HDC hdc
,
353 wxCoord x
, wxCoord y
)
357 bool ok
= dib
.IsOk();
362 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
364 wxLogLastError(_T("GetObject(DIBSECTION)"));
369 // ok, we've got all data we need, do blit it
374 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
376 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
378 (LPBITMAPINFO
)&ds
.dsBmih
,
383 wxLogLastError(wxT("StretchDIBits"));
394 void wxPrinterDC::DoDrawBitmap(const wxBitmap
& bmp
,
395 wxCoord x
, wxCoord y
,
398 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
400 int width
= bmp
.GetWidth(),
401 height
= bmp
.GetHeight();
403 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
404 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, x
, y
) )
406 // no support for StretchDIBits() or an error occured if we got here
408 memDC
.SelectObject(bmp
);
410 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
412 memDC
.SelectObject(wxNullBitmap
);
416 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
417 wxCoord width
, wxCoord height
,
419 wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
),
420 int WXUNUSED(rop
), bool useMask
,
421 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
))
423 wxBitmap
& bmp
= source
->GetSelectedBitmap();
424 wxMask
*mask
= useMask
? bmp
.GetMask() : NULL
;
427 // If we are printing source colours are screen colours not printer
428 // colours and so we need copy the bitmap pixel by pixel.
430 HDC dcSrc
= GetHdcOf(*source
);
431 MemoryHDC
dcMask(dcSrc
);
432 SelectInHDC
selectMask(dcMask
, (HBITMAP
)mask
->GetMaskBitmap());
434 for (int x
= 0; x
< width
; x
++)
436 for (int y
= 0; y
< height
; y
++)
438 COLORREF cref
= ::GetPixel(dcMask
, x
, y
);
441 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dcSrc
, x
, y
));
442 rect
.left
= xdest
+ x
;
443 rect
.right
= rect
.left
+ 1;
444 rect
.top
= ydest
+ y
;
445 rect
.bottom
= rect
.top
+ 1;
446 ::FillRect(GetHdc(), &rect
, brush
);
447 ::DeleteObject(brush
);
454 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
455 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, xdest
, ydest
) )
457 // no support for StretchDIBits
459 // as we are printing, source colours are screen colours not
460 // printer colours and so we need copy the bitmap pixel by pixel.
461 HDC dcSrc
= GetHdcOf(*source
);
463 for (int y
= 0; y
< height
; y
++)
465 // optimization: draw identical adjacent pixels together.
466 for (int x
= 0; x
< width
; x
++)
468 COLORREF col
= ::GetPixel(dcSrc
, x
, y
);
469 HBRUSH brush
= ::CreateSolidBrush( col
);
471 rect
.left
= xdest
+ x
;
472 rect
.top
= ydest
+ y
;
473 while( (x
+ 1 < width
) &&
474 (::GetPixel(dcSrc
, x
+ 1, y
) == col
) )
478 rect
.right
= xdest
+ x
+ 1;
479 rect
.bottom
= rect
.top
+ 1;
480 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
481 ::DeleteObject(brush
);
491 // wxUSE_PRINTING_ARCHITECTURE