]>
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
, device_name
, file
, NULL
);
121 else // we don't have all parameters, ask the user
123 wxPrintData printData
;
124 printData
.SetOrientation(orientation
);
125 m_hDC
= wxGetPrinterDC(printData
);
128 m_ok
= m_hDC
? true: false;
130 // as we created it, we must delete it as well
137 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
139 m_printData
= printData
;
141 m_isInteractive
= false;
143 m_hDC
= wxGetPrinterDC(printData
);
151 wxPrinterDC::wxPrinterDC(WXHDC dc
)
153 m_isInteractive
= false;
160 void wxPrinterDC::Init()
164 // int width = GetDeviceCaps(m_hDC, VERTRES);
165 // int height = GetDeviceCaps(m_hDC, HORZRES);
166 SetMapMode(wxMM_TEXT
);
168 SetBrush(*wxBLACK_BRUSH
);
169 SetPen(*wxBLACK_PEN
);
173 // ----------------------------------------------------------------------------
174 // wxPrinterDC {Start/End}{Page/Doc} methods
175 // ----------------------------------------------------------------------------
177 bool wxPrinterDC::StartDoc(const wxString
& message
)
180 docinfo
.cbSize
= sizeof(DOCINFO
);
181 docinfo
.lpszDocName
= (const wxChar
*)message
;
183 wxString
filename(m_printData
.GetFilename());
185 if (filename
.empty())
186 docinfo
.lpszOutput
= NULL
;
188 docinfo
.lpszOutput
= (const wxChar
*) filename
;
190 docinfo
.lpszDatatype
= NULL
;
196 if ( ::StartDoc(GetHdc(), &docinfo
) <= 0 )
198 wxLogLastError(wxT("StartDoc"));
205 void wxPrinterDC::EndDoc()
207 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
210 void wxPrinterDC::StartPage()
213 ::StartPage((HDC
) m_hDC
);
216 void wxPrinterDC::EndPage()
219 ::EndPage((HDC
) m_hDC
);
223 wxRect
wxPrinterDC::GetPaperRect()
226 if (!Ok()) return wxRect(0, 0, 0, 0);
227 int w
= ::GetDeviceCaps((HDC
) m_hDC
, PHYSICALWIDTH
);
228 int h
= ::GetDeviceCaps((HDC
) m_hDC
, PHYSICALHEIGHT
);
229 int x
= -::GetDeviceCaps((HDC
) m_hDC
, PHYSICALOFFSETX
);
230 int y
= -::GetDeviceCaps((HDC
) m_hDC
, PHYSICALOFFSETY
);
231 return wxRect(x
, y
, w
, h
);
235 #if !wxUSE_PS_PRINTING
237 // Returns default device and port names
238 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
242 LPDEVNAMES lpDevNames
;
243 LPTSTR lpszDeviceName
;
248 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
251 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
253 memset(&pd
, 0, sizeof(PRINTDLG
));
254 pd
.lStructSize
= sizeof(PRINTDLG
);
257 pd
.hwndOwner
= (HWND
)NULL
;
258 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
259 pd
.hDevNames
= NULL
; // Ditto
260 pd
.Flags
= PD_RETURNDEFAULT
;
263 if (!PrintDlg((LPPRINTDLG
)&pd
))
266 GlobalFree(pd
.hDevMode
);
268 GlobalFree(pd
.hDevNames
);
275 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
276 lpszDeviceName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
277 lpszPortName
= (LPTSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
279 deviceName
= lpszDeviceName
;
280 portName
= lpszPortName
;
282 GlobalUnlock(pd
.hDevNames
);
283 GlobalFree(pd
.hDevNames
);
289 GlobalFree(pd
.hDevMode
);
292 return ( !deviceName
.empty() );
295 #endif // !wxUSE_PS_PRINTING
297 // Gets an HDC for the specified printer configuration
298 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
300 #if wxUSE_PS_PRINTING
302 wxUnusedVar(printDataConst
);
304 #else // native Windows printing
305 wxWindowsPrintNativeData
*data
=
306 (wxWindowsPrintNativeData
*) printDataConst
.GetNativeData();
308 data
->TransferFrom( printDataConst
);
310 wxString deviceName
= printDataConst
.GetPrinterName();
311 if ( deviceName
.empty() )
313 // Retrieve the default device name
315 if ( !wxGetDefaultDeviceName(deviceName
, portName
) )
317 return 0; // Could not get default device name
322 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) data
->GetDevMode();
324 DEVMODE
*lpDevMode
= hDevMode
? (DEVMODE
*)::GlobalLock(hDevMode
) : NULL
;
326 HDC hDC
= ::CreateDC(NULL
, deviceName
, NULL
, lpDevMode
);
328 wxLogLastError(_T("CreateDC(printer)"));
331 ::GlobalUnlock(hDevMode
);
334 #endif // PostScript/Windows printing
337 // ----------------------------------------------------------------------------
338 // wxPrinterDC bit blitting/bitmap drawing
339 // ----------------------------------------------------------------------------
341 // helper of DoDrawBitmap() and DoBlit()
343 bool DrawBitmapUsingStretchDIBits(HDC hdc
,
345 wxCoord x
, wxCoord y
)
349 bool ok
= dib
.IsOk();
354 if ( !::GetObject(dib
.GetHandle(), sizeof(ds
), &ds
) )
356 wxLogLastError(_T("GetObject(DIBSECTION)"));
361 // ok, we've got all data we need, do blit it
366 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
368 ds
.dsBmih
.biWidth
, ds
.dsBmih
.biHeight
,
370 (LPBITMAPINFO
)&ds
.dsBmih
,
375 wxLogLastError(wxT("StretchDIBits"));
386 void wxPrinterDC::DoDrawBitmap(const wxBitmap
& bmp
,
387 wxCoord x
, wxCoord y
,
390 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
392 int width
= bmp
.GetWidth(),
393 height
= bmp
.GetHeight();
395 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
) ||
396 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp
, x
, y
) )
398 // no support for StretchDIBits() or an error occurred if we got here
401 memDC
.SelectObjectAsSource(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