]>
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 // 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 #if defined(__WXUNIVERSAL__) && wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW
60 #define wxUSE_PS_PRINTING 1
62 #define wxUSE_PS_PRINTING 0
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
76 // wxPrinterDC construction
77 // ----------------------------------------------------------------------------
79 // This form is deprecated
80 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
,
81 const wxString
& device_name
,
86 m_isInteractive
= interactive
;
89 m_printData
.SetFilename(file
);
91 #if wxUSE_COMMON_DIALOGS
96 pd
.lStructSize
= sizeof( PRINTDLG
);
97 pd
.hwndOwner
= (HWND
) NULL
;
98 pd
.hDevMode
= (HANDLE
)NULL
;
99 pd
.hDevNames
= (HANDLE
)NULL
;
100 pd
.Flags
= PD_RETURNDC
| PD_NOSELECTION
| PD_NOPAGENUMS
;
106 pd
.hInstance
= (HINSTANCE
)NULL
;
108 m_ok
= PrintDlg( &pd
) != 0;
111 m_hDC
= (WXHDC
) pd
.hDC
;
115 #endif // wxUSE_COMMON_DIALOGS
117 if ( !driver_name
.empty() && !device_name
.empty() && !file
.empty() )
119 m_hDC
= (WXHDC
) CreateDC(driver_name
.wx_str(),
120 device_name
.wx_str(),
124 else // we don't have all parameters, ask the user
126 wxPrintData printData
;
127 printData
.SetOrientation(orientation
);
128 m_hDC
= wxGetPrinterDC(printData
);
131 m_ok
= m_hDC
? true: false;
133 // as we created it, we must delete it as well
140 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
142 m_printData
= printData
;
144 m_isInteractive
= false;
146 m_hDC
= wxGetPrinterDC(printData
);
154 wxPrinterDC::wxPrinterDC(WXHDC dc
)
156 m_isInteractive
= false;
163 void wxPrinterDC::Init()
167 // int width = GetDeviceCaps(m_hDC, VERTRES);
168 // int height = GetDeviceCaps(m_hDC, HORZRES);
169 SetMapMode(wxMM_TEXT
);
171 SetBrush(*wxBLACK_BRUSH
);
172 SetPen(*wxBLACK_PEN
);
176 // ----------------------------------------------------------------------------
177 // wxPrinterDC {Start/End}{Page/Doc} methods
178 // ----------------------------------------------------------------------------
180 bool wxPrinterDC::StartDoc(const wxString
& message
)
183 docinfo
.cbSize
= sizeof(DOCINFO
);
184 docinfo
.lpszDocName
= message
.wx_str();
186 wxString
filename(m_printData
.GetFilename());
188 if (filename
.empty())
189 docinfo
.lpszOutput
= NULL
;
191 docinfo
.lpszOutput
= filename
.wx_str();
193 docinfo
.lpszDatatype
= NULL
;
199 if ( ::StartDoc(GetHdc(), &docinfo
) <= 0 )
201 wxLogLastError(wxT("StartDoc"));
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
);
226 wxRect
wxPrinterDC::GetPaperRect()
229 if (!Ok()) return wxRect(0, 0, 0, 0);
230 int w
= ::GetDeviceCaps((HDC
) m_hDC
, PHYSICALWIDTH
);
231 int h
= ::GetDeviceCaps((HDC
) m_hDC
, PHYSICALHEIGHT
);
232 int x
= -::GetDeviceCaps((HDC
) m_hDC
, PHYSICALOFFSETX
);
233 int y
= -::GetDeviceCaps((HDC
) m_hDC
, PHYSICALOFFSETY
);
234 return wxRect(x
, y
, w
, h
);
238 #if !wxUSE_PS_PRINTING
240 // Returns default device and port names
241 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
245 LPDEVNAMES lpDevNames
;
246 LPTSTR lpszDeviceName
;
251 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
254 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
256 memset(&pd
, 0, sizeof(PRINTDLG
));
257 pd
.lStructSize
= sizeof(PRINTDLG
);
260 pd
.hwndOwner
= (HWND
)NULL
;
261 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
262 pd
.hDevNames
= NULL
; // Ditto
263 pd
.Flags
= PD_RETURNDEFAULT
;
266 if (!PrintDlg((LPPRINTDLG
)&pd
))
269 GlobalFree(pd
.hDevMode
);
271 GlobalFree(pd
.hDevNames
);
278 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
279 lpszDeviceName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
280 lpszPortName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
282 deviceName
= lpszDeviceName
;
283 portName
= lpszPortName
;
285 GlobalUnlock(pd
.hDevNames
);
286 GlobalFree(pd
.hDevNames
);
292 GlobalFree(pd
.hDevMode
);
295 return ( !deviceName
.empty() );
298 #endif // !wxUSE_PS_PRINTING
300 // Gets an HDC for the specified printer configuration
301 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
303 #if wxUSE_PS_PRINTING
305 wxUnusedVar(printDataConst
);
307 #else // native Windows printing
308 wxWindowsPrintNativeData
*data
=
309 (wxWindowsPrintNativeData
*) printDataConst
.GetNativeData();
311 data
->TransferFrom( printDataConst
);
313 wxString deviceName
= printDataConst
.GetPrinterName();
314 if ( deviceName
.empty() )
316 // Retrieve the default device name
318 if ( !wxGetDefaultDeviceName(deviceName
, portName
) )
320 return 0; // Could not get default device name
325 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) data
->GetDevMode();
327 DEVMODE
*lpDevMode
= hDevMode
? (DEVMODE
*)::GlobalLock(hDevMode
) : NULL
;
329 HDC hDC
= ::CreateDC(NULL
, deviceName
.wx_str(), NULL
, lpDevMode
);
331 wxLogLastError(_T("CreateDC(printer)"));
334 ::GlobalUnlock(hDevMode
);
337 #endif // PostScript/Windows printing
340 // ----------------------------------------------------------------------------
341 // wxPrinterDC bit blitting/bitmap drawing
342 // ----------------------------------------------------------------------------
344 // helper of DoDrawBitmap() and DoBlit()
346 bool DrawBitmapUsingStretchDIBits(HDC hdc
,
348 wxCoord x
, wxCoord y
)
352 bool ok
= dib
.IsOk();
357 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
359 wxLogLastError(_T("GetObject(DIBSECTION)"));
364 // ok, we've got all data we need, do blit it
369 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
371 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
373 (LPBITMAPINFO
)&ds
.dsBmih
,
378 wxLogLastError(wxT("StretchDIBits"));
389 void wxPrinterDC::DoDrawBitmap(const wxBitmap
& bmp
,
390 wxCoord x
, wxCoord y
,
393 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
395 int width
= bmp
.GetWidth(),
396 height
= bmp
.GetHeight();
398 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
399 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, x
, y
) )
401 // no support for StretchDIBits() or an error occurred if we got here
404 memDC
.SelectObjectAsSource(bmp
);
406 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
408 memDC
.SelectObject(wxNullBitmap
);
412 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
413 wxCoord width
, wxCoord height
,
415 wxCoord
WXUNUSED(xsrc
), wxCoord
WXUNUSED(ysrc
),
416 int WXUNUSED(rop
), bool useMask
,
417 wxCoord
WXUNUSED(xsrcMask
), wxCoord
WXUNUSED(ysrcMask
))
419 wxBitmap
& bmp
= source
->GetSelectedBitmap();
420 wxMask
*mask
= useMask
? bmp
.GetMask() : NULL
;
423 // If we are printing source colours are screen colours not printer
424 // colours and so we need copy the bitmap pixel by pixel.
426 HDC dcSrc
= GetHdcOf(*source
);
427 MemoryHDC
dcMask(dcSrc
);
428 SelectInHDC
selectMask(dcMask
, (HBITMAP
)mask
->GetMaskBitmap());
430 for (int x
= 0; x
< width
; x
++)
432 for (int y
= 0; y
< height
; y
++)
434 COLORREF cref
= ::GetPixel(dcMask
, x
, y
);
437 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dcSrc
, x
, y
));
438 rect
.left
= xdest
+ x
;
439 rect
.right
= rect
.left
+ 1;
440 rect
.top
= ydest
+ y
;
441 rect
.bottom
= rect
.top
+ 1;
442 ::FillRect(GetHdc(), &rect
, brush
);
443 ::DeleteObject(brush
);
450 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
451 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, xdest
, ydest
) )
453 // no support for StretchDIBits
455 // as we are printing, source colours are screen colours not
456 // printer colours and so we need copy the bitmap pixel by pixel.
457 HDC dcSrc
= GetHdcOf(*source
);
459 for (int y
= 0; y
< height
; y
++)
461 // optimization: draw identical adjacent pixels together.
462 for (int x
= 0; x
< width
; x
++)
464 COLORREF col
= ::GetPixel(dcSrc
, x
, y
);
465 HBRUSH brush
= ::CreateSolidBrush( col
);
467 rect
.left
= xdest
+ x
;
468 rect
.top
= ydest
+ y
;
469 while( (x
+ 1 < width
) &&
470 (::GetPixel(dcSrc
, x
+ 1, y
) == col
) )
474 rect
.right
= xdest
+ x
+ 1;
475 rect
.bottom
= rect
.top
+ 1;
476 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
477 ::DeleteObject(brush
);
487 // wxUSE_PRINTING_ARCHITECTURE