]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcprint.cpp
Forgot header with OSX prefix
[wxWidgets.git] / src / msw / dcprint.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
4b7f2165 2// Name: src/msw/dcprint.cpp
2bda0e17
KB
3// Purpose: wxPrinterDC class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
4b7f2165
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
4b7f2165 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
6d50343d
WS
27#if wxUSE_PRINTING_ARCHITECTURE
28
29#include "wx/dcprint.h"
888dde65 30#include "wx/msw/dcprint.h"
6d50343d 31
2bda0e17 32#ifndef WX_PRECOMP
57bd4c60 33 #include "wx/msw/wrapcdlg.h"
4b7f2165
VZ
34 #include "wx/string.h"
35 #include "wx/log.h"
36 #include "wx/window.h"
475f6e7a 37 #include "wx/dcmemory.h"
18680f86 38 #include "wx/math.h"
2bda0e17
KB
39#endif
40
42e69d6b 41#include "wx/msw/private.h"
4676948b
JS
42
43#if wxUSE_WXDIB
6d50343d 44 #include "wx/msw/dib.h"
4676948b
JS
45#endif
46
8850cbd3 47#include "wx/printdlg.h"
08680429 48#include "wx/msw/printdlg.h"
2bda0e17 49
2bda0e17 50#ifndef __WIN32__
4b7f2165 51 #include <print.h>
2bda0e17
KB
52#endif
53
3c1a88d8 54// mingw32 defines GDI_ERROR incorrectly
2f52b4e1 55#if defined(__GNUWIN32__) || !defined(GDI_ERROR)
3c1a88d8
VZ
56 #undef GDI_ERROR
57 #define GDI_ERROR ((int)-1)
58#endif
59
12acbc9b 60#if defined(__WXUNIVERSAL__) && wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW
632aaa18
VZ
61 #define wxUSE_PS_PRINTING 1
62#else
63 #define wxUSE_PS_PRINTING 0
64#endif
65
4b7f2165
VZ
66// ----------------------------------------------------------------------------
67// wxWin macros
68// ----------------------------------------------------------------------------
3c1a88d8 69
888dde65 70IMPLEMENT_ABSTRACT_CLASS(wxPrinterDCImpl, wxMSWDCImpl)
2bda0e17 71
4b7f2165
VZ
72// ============================================================================
73// implementation
74// ============================================================================
75
76// ----------------------------------------------------------------------------
77// wxPrinterDC construction
78// ----------------------------------------------------------------------------
79
888dde65 80#if 0
7bcb11d3 81// This form is deprecated
7ba4fbeb
VZ
82wxPrinterDC::wxPrinterDC(const wxString& driver_name,
83 const wxString& device_name,
84 const wxString& file,
85 bool interactive,
86 int orientation)
2bda0e17 87{
7bcb11d3 88 m_isInteractive = interactive;
a17e237f 89
7ba4fbeb 90 if ( !file.empty() )
7bcb11d3 91 m_printData.SetFilename(file);
a17e237f 92
47d67540 93#if wxUSE_COMMON_DIALOGS
7ba4fbeb 94 if ( interactive )
7bcb11d3
JS
95 {
96 PRINTDLG pd;
a17e237f 97
7bcb11d3 98 pd.lStructSize = sizeof( PRINTDLG );
7ba4fbeb
VZ
99 pd.hwndOwner = (HWND) NULL;
100 pd.hDevMode = (HANDLE)NULL;
101 pd.hDevNames = (HANDLE)NULL;
102 pd.Flags = PD_RETURNDC | PD_NOSELECTION | PD_NOPAGENUMS;
103 pd.nFromPage = 0;
104 pd.nToPage = 0;
105 pd.nMinPage = 0;
106 pd.nMaxPage = 0;
107 pd.nCopies = 1;
108 pd.hInstance = (HINSTANCE)NULL;
109
110 m_ok = PrintDlg( &pd ) != 0;
111 if ( m_ok )
7bcb11d3
JS
112 {
113 m_hDC = (WXHDC) pd.hDC;
7bcb11d3 114 }
7bcb11d3
JS
115 }
116 else
4b7f2165 117#endif // wxUSE_COMMON_DIALOGS
7ba4fbeb
VZ
118 {
119 if ( !driver_name.empty() && !device_name.empty() && !file.empty() )
7bcb11d3 120 {
e0a050e3
VS
121 m_hDC = (WXHDC) CreateDC(driver_name.wx_str(),
122 device_name.wx_str(),
123 file.fn_str(),
124 NULL);
7bcb11d3 125 }
7ba4fbeb 126 else // we don't have all parameters, ask the user
7bcb11d3
JS
127 {
128 wxPrintData printData;
129 printData.SetOrientation(orientation);
130 m_hDC = wxGetPrinterDC(printData);
7bcb11d3 131 }
a17e237f 132
d71cc120 133 m_ok = m_hDC ? true: false;
7ba4fbeb
VZ
134
135 // as we created it, we must delete it as well
d71cc120 136 m_bOwnsDC = true;
7ba4fbeb
VZ
137 }
138
139 Init();
7bcb11d3 140}
888dde65 141#endif
7bcb11d3 142
888dde65
RR
143wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, const wxPrintData& printData ) :
144 wxMSWDCImpl( owner )
7bcb11d3
JS
145{
146 m_printData = printData;
147
d71cc120 148 m_isInteractive = false;
7bcb11d3
JS
149
150 m_hDC = wxGetPrinterDC(printData);
7ba4fbeb 151 m_ok = m_hDC != 0;
d71cc120 152 m_bOwnsDC = true;
a17e237f 153
7ba4fbeb 154 Init();
2bda0e17
KB
155}
156
7bcb11d3 157
888dde65
RR
158wxPrinterDCImpl::wxPrinterDCImpl( wxPrinterDC *owner, WXHDC dc ) :
159 wxMSWDCImpl( owner )
2bda0e17 160{
d71cc120 161 m_isInteractive = false;
a17e237f 162
7ba4fbeb 163 m_hDC = dc;
d71cc120
WS
164 m_bOwnsDC = true;
165 m_ok = true;
7ba4fbeb
VZ
166}
167
888dde65 168void wxPrinterDCImpl::Init()
7ba4fbeb
VZ
169{
170 if ( m_hDC )
7bcb11d3
JS
171 {
172 // int width = GetDeviceCaps(m_hDC, VERTRES);
173 // int height = GetDeviceCaps(m_hDC, HORZRES);
174 SetMapMode(wxMM_TEXT);
2bda0e17 175
7ba4fbeb
VZ
176 SetBrush(*wxBLACK_BRUSH);
177 SetPen(*wxBLACK_PEN);
178 }
2bda0e17
KB
179}
180
4b7f2165 181// ----------------------------------------------------------------------------
888dde65 182// wxPrinterDCImpl {Start/End}{Page/Doc} methods
4b7f2165
VZ
183// ----------------------------------------------------------------------------
184
888dde65 185bool wxPrinterDCImpl::StartDoc(const wxString& message)
7bcb11d3
JS
186{
187 DOCINFO docinfo;
188 docinfo.cbSize = sizeof(DOCINFO);
e0a050e3 189 docinfo.lpszDocName = message.wx_str();
7bcb11d3
JS
190
191 wxString filename(m_printData.GetFilename());
192
b713f891 193 if (filename.empty())
7bcb11d3
JS
194 docinfo.lpszOutput = NULL;
195 else
e0a050e3 196 docinfo.lpszOutput = filename.wx_str();
7bcb11d3 197
7bcb11d3
JS
198 docinfo.lpszDatatype = NULL;
199 docinfo.fwType = 0;
a17e237f 200
7bcb11d3 201 if (!m_hDC)
d71cc120 202 return false;
a17e237f 203
261205e4 204 if ( ::StartDoc(GetHdc(), &docinfo) <= 0 )
7bcb11d3 205 {
c5bc21bd
VZ
206 wxLogLastError(wxT("StartDoc"));
207 return false;
7bcb11d3 208 }
a17e237f 209
c5bc21bd 210 return true;
7bcb11d3
JS
211}
212
888dde65 213void wxPrinterDCImpl::EndDoc()
7bcb11d3
JS
214{
215 if (m_hDC) ::EndDoc((HDC) m_hDC);
216}
217
888dde65 218void wxPrinterDCImpl::StartPage()
7bcb11d3
JS
219{
220 if (m_hDC)
221 ::StartPage((HDC) m_hDC);
222}
223
888dde65 224void wxPrinterDCImpl::EndPage()
7bcb11d3
JS
225{
226 if (m_hDC)
227 ::EndPage((HDC) m_hDC);
228}
229
f415cab9 230
6d52ca53 231wxRect wxPrinterDCImpl::GetPaperRect() const
f415cab9
JS
232
233{
888dde65 234 if (!IsOk()) return wxRect(0, 0, 0, 0);
f415cab9
JS
235 int w = ::GetDeviceCaps((HDC) m_hDC, PHYSICALWIDTH);
236 int h = ::GetDeviceCaps((HDC) m_hDC, PHYSICALHEIGHT);
237 int x = -::GetDeviceCaps((HDC) m_hDC, PHYSICALOFFSETX);
238 int y = -::GetDeviceCaps((HDC) m_hDC, PHYSICALOFFSETY);
239 return wxRect(x, y, w, h);
240}
241
242
632aaa18
VZ
243#if !wxUSE_PS_PRINTING
244
7bcb11d3
JS
245// Returns default device and port names
246static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
247{
7ba4fbeb 248 deviceName.clear();
7bcb11d3
JS
249
250 LPDEVNAMES lpDevNames;
161f4f73
VZ
251 LPTSTR lpszDeviceName;
252 LPTSTR lpszPortName;
a17e237f 253
7bcb11d3
JS
254 PRINTDLG pd;
255
256 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
257#ifdef __GNUWIN32__
161f4f73 258 memset(&pd, 0, 66);
7bcb11d3
JS
259 pd.lStructSize = 66; // sizeof(PRINTDLG);
260#else
161f4f73 261 memset(&pd, 0, sizeof(PRINTDLG));
7bcb11d3
JS
262 pd.lStructSize = sizeof(PRINTDLG);
263#endif
264
265 pd.hwndOwner = (HWND)NULL;
266 pd.hDevMode = NULL; // Will be created by PrintDlg
267 pd.hDevNames = NULL; // Ditto
268 pd.Flags = PD_RETURNDEFAULT;
269 pd.nCopies = 1;
a17e237f 270
7bcb11d3
JS
271 if (!PrintDlg((LPPRINTDLG)&pd))
272 {
273 if ( pd.hDevMode )
274 GlobalFree(pd.hDevMode);
275 if (pd.hDevNames)
276 GlobalFree(pd.hDevNames);
a17e237f 277
d71cc120 278 return false;
7bcb11d3 279 }
a17e237f 280
7bcb11d3
JS
281 if (pd.hDevNames)
282 {
283 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
161f4f73
VZ
284 lpszDeviceName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset;
285 lpszPortName = (LPTSTR)lpDevNames + lpDevNames->wOutputOffset;
7bcb11d3
JS
286
287 deviceName = lpszDeviceName;
288 portName = lpszPortName;
60fe7303
JS
289
290 GlobalUnlock(pd.hDevNames);
291 GlobalFree(pd.hDevNames);
292 pd.hDevNames=NULL;
7bcb11d3 293 }
a17e237f 294
7bcb11d3
JS
295 if (pd.hDevMode)
296 {
297 GlobalFree(pd.hDevMode);
298 pd.hDevMode=NULL;
299 }
489f6cf7 300 return ( !deviceName.empty() );
7bcb11d3
JS
301}
302
632aaa18
VZ
303#endif // !wxUSE_PS_PRINTING
304
7bcb11d3
JS
305// Gets an HDC for the specified printer configuration
306WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
307{
632aaa18
VZ
308#if wxUSE_PS_PRINTING
309 // TODO
310 wxUnusedVar(printDataConst);
498b94a6 311 return 0;
632aaa18 312#else // native Windows printing
8850cbd3
RR
313 wxWindowsPrintNativeData *data =
314 (wxWindowsPrintNativeData *) printDataConst.GetNativeData();
498b94a6 315
fd64de59 316 data->TransferFrom( printDataConst );
a17e237f 317
632aaa18
VZ
318 wxString deviceName = printDataConst.GetPrinterName();
319 if ( deviceName.empty() )
7bcb11d3
JS
320 {
321 // Retrieve the default device name
322 wxString portName;
632aaa18 323 if ( !wxGetDefaultDeviceName(deviceName, portName) )
3f8b4a3f
JS
324 {
325 return 0; // Could not get default device name
326 }
7bcb11d3 327 }
a17e237f 328
a17e237f 329
2d2b68ba
VZ
330 GlobalPtrLock lockDevMode;
331 const HGLOBAL devMode = data->GetDevMode();
332 if ( devMode )
333 lockDevMode.Init(devMode);
334
335 HDC hDC = ::CreateDC
336 (
337 NULL, // no driver name as we use device name
338 deviceName.wx_str(),
339 NULL, // unused
5c33522f 340 static_cast<DEVMODE *>(lockDevMode.Get())
2d2b68ba 341 );
632aaa18
VZ
342 if ( !hDC )
343 wxLogLastError(_T("CreateDC(printer)"));
344
7bcb11d3 345 return (WXHDC) hDC;
632aaa18 346#endif // PostScript/Windows printing
7bcb11d3 347}
2bda0e17 348
4b7f2165 349// ----------------------------------------------------------------------------
888dde65 350// wxPrinterDCImpl bit blitting/bitmap drawing
4b7f2165
VZ
351// ----------------------------------------------------------------------------
352
2f52b4e1
VZ
353// helper of DoDrawBitmap() and DoBlit()
354static
355bool DrawBitmapUsingStretchDIBits(HDC hdc,
356 const wxBitmap& bmp,
357 wxCoord x, wxCoord y)
358{
4676948b 359#if wxUSE_WXDIB
2f52b4e1 360 wxDIB dib(bmp);
999836aa
VZ
361 bool ok = dib.IsOk();
362 if ( !ok )
d71cc120 363 return false;
2f52b4e1
VZ
364
365 DIBSECTION ds;
366 if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) )
367 {
368 wxLogLastError(_T("GetObject(DIBSECTION)"));
369
d71cc120 370 return false;
2f52b4e1
VZ
371 }
372
373 // ok, we've got all data we need, do blit it
374 if ( ::StretchDIBits
375 (
376 hdc,
377 x, y,
378 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
379 0, 0,
380 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
381 ds.dsBm.bmBits,
382 (LPBITMAPINFO)&ds.dsBmih,
383 DIB_RGB_COLORS,
384 SRCCOPY
385 ) == GDI_ERROR )
386 {
387 wxLogLastError(wxT("StretchDIBits"));
f6081a04 388
d71cc120 389 return false;
2f52b4e1 390 }
84968677 391
d71cc120 392 return true;
4676948b 393#else
d71cc120 394 return false;
4676948b 395#endif
2f52b4e1 396}
e11f2e16 397
888dde65 398void wxPrinterDCImpl::DoDrawBitmap(const wxBitmap& bmp,
4b7f2165
VZ
399 wxCoord x, wxCoord y,
400 bool useMask)
401{
402 wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
403
404 int width = bmp.GetWidth(),
405 height = bmp.GetHeight();
406
2f52b4e1
VZ
407 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
408 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, x, y) )
4b7f2165 409 {
3103e8a9 410 // no support for StretchDIBits() or an error occurred if we got here
4b7f2165 411 wxMemoryDC memDC;
fea35690
VZ
412
413 memDC.SelectObjectAsSource(bmp);
4b7f2165 414
888dde65 415 GetOwner()->Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
4b7f2165
VZ
416
417 memDC.SelectObject(wxNullBitmap);
418 }
419}
420
888dde65 421bool wxPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest,
4b7f2165
VZ
422 wxCoord width, wxCoord height,
423 wxDC *source,
2eb10e2a 424 wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc),
89efaf2b 425 wxRasterOperationMode WXUNUSED(rop), bool useMask,
d699f48b 426 wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask))
4b7f2165 427{
888dde65
RR
428 wxDCImpl *impl = source->GetImpl();
429 wxMSWDCImpl *msw_impl = wxDynamicCast(impl, wxMSWDCImpl);
430 if (!msw_impl)
431 return false;
432
433 wxBitmap& bmp = msw_impl->GetSelectedBitmap();
2f52b4e1
VZ
434 wxMask *mask = useMask ? bmp.GetMask() : NULL;
435 if ( mask )
4b7f2165 436 {
0becd470
VZ
437 // If we are printing source colours are screen colours not printer
438 // colours and so we need copy the bitmap pixel by pixel.
4b7f2165 439 RECT rect;
888dde65 440 HDC dcSrc = GetHdcOf(*msw_impl);
2f52b4e1
VZ
441 MemoryHDC dcMask(dcSrc);
442 SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap());
4b7f2165 443
4b7f2165
VZ
444 for (int x = 0; x < width; x++)
445 {
446 for (int y = 0; y < height; y++)
447 {
2f52b4e1 448 COLORREF cref = ::GetPixel(dcMask, x, y);
4b7f2165
VZ
449 if (cref)
450 {
2f52b4e1 451 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y));
4b7f2165
VZ
452 rect.left = xdest + x;
453 rect.right = rect.left + 1;
33ac7e6f 454 rect.top = ydest + y;
4b7f2165
VZ
455 rect.bottom = rect.top + 1;
456 ::FillRect(GetHdc(), &rect, brush);
457 ::DeleteObject(brush);
458 }
459 }
460 }
4b7f2165
VZ
461 }
462 else // no mask
463 {
2f52b4e1 464 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
e5c4c38b 465 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) )
4b7f2165 466 {
2f52b4e1 467 // no support for StretchDIBits
4b7f2165 468
0becd470
VZ
469 // as we are printing, source colours are screen colours not
470 // printer colours and so we need copy the bitmap pixel by pixel.
888dde65 471 HDC dcSrc = GetHdcOf(*msw_impl);
4b7f2165
VZ
472 RECT rect;
473 for (int y = 0; y < height; y++)
474 {
0becd470 475 // optimization: draw identical adjacent pixels together.
4b7f2165
VZ
476 for (int x = 0; x < width; x++)
477 {
2f52b4e1 478 COLORREF col = ::GetPixel(dcSrc, x, y);
4b7f2165
VZ
479 HBRUSH brush = ::CreateSolidBrush( col );
480
481 rect.left = xdest + x;
482 rect.top = ydest + y;
2f52b4e1
VZ
483 while( (x + 1 < width) &&
484 (::GetPixel(dcSrc, x + 1, y) == col ) )
4b7f2165
VZ
485 {
486 ++x;
487 }
488 rect.right = xdest + x + 1;
489 rect.bottom = rect.top + 1;
490 ::FillRect((HDC) m_hDC, &rect, brush);
491 ::DeleteObject(brush);
492 }
493 }
494 }
495 }
496
d71cc120 497 return true;
4b7f2165 498}
f6bcfd97
BP
499
500#endif
501 // wxUSE_PRINTING_ARCHITECTURE