]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
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"
49 #if wxUSE_COMMON_DIALOGS
57 // mingw32 defines GDI_ERROR incorrectly
58 #if defined(__GNUWIN32__) || !defined(GDI_ERROR)
60 #define GDI_ERROR ((int)-1)
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
69 // ============================================================================
71 // ============================================================================
73 // ----------------------------------------------------------------------------
74 // wxPrinterDC construction
75 // ----------------------------------------------------------------------------
77 // This form is deprecated
78 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
,
79 const wxString
& device_name
,
84 m_isInteractive
= interactive
;
87 m_printData
.SetFilename(file
);
89 #if wxUSE_COMMON_DIALOGS
94 pd
.lStructSize
= sizeof( PRINTDLG
);
95 pd
.hwndOwner
= (HWND
) NULL
;
96 pd
.hDevMode
= (HANDLE
)NULL
;
97 pd
.hDevNames
= (HANDLE
)NULL
;
98 pd
.Flags
= PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
104 pd
.hInstance
= (HINSTANCE
)NULL
;
106 m_ok
= PrintDlg( &pd
) != 0;
109 m_hDC
= (WXHDC
) pd
.hDC
;
113 #endif // wxUSE_COMMON_DIALOGS
115 if ( !driver_name
.empty() && !device_name
.empty() && !file
.empty() )
117 m_hDC
= (WXHDC
) CreateDC(driver_name
, device_name
, file
, NULL
);
119 else // we don't have all parameters, ask the user
121 wxPrintData printData
;
122 printData
.SetOrientation(orientation
);
123 m_hDC
= wxGetPrinterDC(printData
);
126 m_ok
= m_hDC
? TRUE
: FALSE
;
128 // as we created it, we must delete it as well
135 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
137 m_printData
= printData
;
139 m_isInteractive
= FALSE
;
141 m_hDC
= wxGetPrinterDC(printData
);
149 wxPrinterDC::wxPrinterDC(WXHDC dc
)
151 m_isInteractive
= FALSE
;
158 void wxPrinterDC::Init()
162 // int width = GetDeviceCaps(m_hDC, VERTRES);
163 // int height = GetDeviceCaps(m_hDC, HORZRES);
164 SetMapMode(wxMM_TEXT
);
166 SetBrush(*wxBLACK_BRUSH
);
167 SetPen(*wxBLACK_PEN
);
171 // ----------------------------------------------------------------------------
172 // wxPrinterDC {Start/End}{Page/Doc} methods
173 // ----------------------------------------------------------------------------
175 bool wxPrinterDC::StartDoc(const wxString
& message
)
178 docinfo
.cbSize
= sizeof(DOCINFO
);
179 docinfo
.lpszDocName
= (const wxChar
*)message
;
181 wxString
filename(m_printData
.GetFilename());
183 if (filename
.IsEmpty())
184 docinfo
.lpszOutput
= NULL
;
186 docinfo
.lpszOutput
= (const wxChar
*) filename
;
188 #if defined(__WIN95__)
189 docinfo
.lpszDatatype
= NULL
;
196 int ret
= ::StartDoc(GetHdc(), &docinfo
);
201 DWORD lastError
= GetLastError();
202 wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError
);
209 void wxPrinterDC::EndDoc()
211 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
214 void wxPrinterDC::StartPage()
217 ::StartPage((HDC
) m_hDC
);
220 void wxPrinterDC::EndPage()
223 ::EndPage((HDC
) m_hDC
);
226 // Returns default device and port names
227 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
231 LPDEVNAMES lpDevNames
;
232 LPTSTR lpszDeviceName
;
237 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
240 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
242 memset(&pd
, 0, sizeof(PRINTDLG
));
243 pd
.lStructSize
= sizeof(PRINTDLG
);
246 pd
.hwndOwner
= (HWND
)NULL
;
247 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
248 pd
.hDevNames
= NULL
; // Ditto
249 pd
.Flags
= PD_RETURNDEFAULT
;
252 if (!PrintDlg((LPPRINTDLG
)&pd
))
255 GlobalFree(pd
.hDevMode
);
257 GlobalFree(pd
.hDevNames
);
264 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
265 lpszDeviceName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
266 lpszPortName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
268 deviceName
= lpszDeviceName
;
269 portName
= lpszPortName
;
271 GlobalUnlock(pd
.hDevNames
);
272 GlobalFree(pd
.hDevNames
);
278 GlobalFree(pd
.hDevMode
);
281 return ( deviceName
!= wxEmptyString
);
284 // Gets an HDC for the specified printer configuration
285 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
287 wxPrintData printData
= printDataConst
;
288 printData
.ConvertToNative();
290 wxChar
* driverName
= (wxChar
*) NULL
;
292 wxString devNameStr
= printData
.GetPrinterName();
293 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
295 const wxChar
* deviceName
;
297 deviceName
= (wxChar
*) NULL
;
299 deviceName
= devNameStr
.c_str();
301 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
303 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) printData
.GetNativeData();
306 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
310 // Retrieve the default device name
316 #endif // Debug/Release
317 wxGetDefaultDeviceName(devNameStr
, portName
);
319 wxASSERT_MSG( ret
, wxT("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
);
336 // ----------------------------------------------------------------------------
337 // wxPrinterDC bit blitting/bitmap drawing
338 // ----------------------------------------------------------------------------
340 // helper of DoDrawBitmap() and DoBlit()
342 bool DrawBitmapUsingStretchDIBits(HDC hdc
,
344 wxCoord x
, wxCoord y
)
348 bool ok
= dib
.IsOk();
353 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
355 wxLogLastError(_T("GetObject(DIBSECTION)"));
360 // ok, we've got all data we need, do blit it
365 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
367 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
369 (LPBITMAPINFO
)&ds
.dsBmih
,
374 wxLogLastError(wxT("StretchDIBits"));
385 void wxPrinterDC::DoDrawBitmap(const wxBitmap
& bmp
,
386 wxCoord x
, wxCoord y
,
389 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
391 int width
= bmp
.GetWidth(),
392 height
= bmp
.GetHeight();
394 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
395 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, x
, y
) )
397 // no support for StretchDIBits() or an error occured if we got here
399 memDC
.SelectObject(bmp
);
401 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
403 memDC
.SelectObject(wxNullBitmap
);
407 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
408 wxCoord width
, wxCoord height
,
410 wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
),
411 int WXUNUSED(rop
), bool useMask
,
412 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
))
414 wxBitmap
& bmp
= source
->GetSelectedBitmap();
415 wxMask
*mask
= useMask
? bmp
.GetMask() : NULL
;
418 // If we are printing source colours are screen colours not printer
419 // colours and so we need copy the bitmap pixel by pixel.
421 HDC dcSrc
= GetHdcOf(*source
);
422 MemoryHDC
dcMask(dcSrc
);
423 SelectInHDC
selectMask(dcMask
, (HBITMAP
)mask
->GetMaskBitmap());
425 for (int x
= 0; x
< width
; x
++)
427 for (int y
= 0; y
< height
; y
++)
429 COLORREF cref
= ::GetPixel(dcMask
, x
, y
);
432 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dcSrc
, x
, y
));
433 rect
.left
= xdest
+ x
;
434 rect
.right
= rect
.left
+ 1;
435 rect
.top
= ydest
+ y
;
436 rect
.bottom
= rect
.top
+ 1;
437 ::FillRect(GetHdc(), &rect
, brush
);
438 ::DeleteObject(brush
);
445 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
446 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, xdest
, ydest
) )
448 // no support for StretchDIBits
450 // as we are printing, source colours are screen colours not
451 // printer colours and so we need copy the bitmap pixel by pixel.
452 HDC dcSrc
= GetHdcOf(*source
);
454 for (int y
= 0; y
< height
; y
++)
456 // optimization: draw identical adjacent pixels together.
457 for (int x
= 0; x
< width
; x
++)
459 COLORREF col
= ::GetPixel(dcSrc
, x
, y
);
460 HBRUSH brush
= ::CreateSolidBrush( col
);
462 rect
.left
= xdest
+ x
;
463 rect
.top
= ydest
+ y
;
464 while( (x
+ 1 < width
) &&
465 (::GetPixel(dcSrc
, x
+ 1, y
) == col
) )
469 rect
.right
= xdest
+ x
+ 1;
470 rect
.bottom
= rect
.top
+ 1;
471 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
472 ::DeleteObject(brush
);
482 // wxUSE_PRINTING_ARCHITECTURE