]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcprint.cpp
Fix most of the Objective-C GC problems by using the stronger CFRetain/CFRelease...
[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
888dde65 231wxRect wxPrinterDCImpl::GetPaperRect()
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
632aaa18
VZ
330 HGLOBAL hDevMode = (HGLOBAL)(DWORD) data->GetDevMode();
331
332 DEVMODE *lpDevMode = hDevMode ? (DEVMODE *)::GlobalLock(hDevMode) : NULL;
333
e0a050e3 334 HDC hDC = ::CreateDC(NULL, deviceName.wx_str(), NULL, lpDevMode);
632aaa18
VZ
335 if ( !hDC )
336 wxLogLastError(_T("CreateDC(printer)"));
337
338 if ( lpDevMode )
339 ::GlobalUnlock(hDevMode);
a17e237f 340
7bcb11d3 341 return (WXHDC) hDC;
632aaa18 342#endif // PostScript/Windows printing
7bcb11d3 343}
2bda0e17 344
4b7f2165 345// ----------------------------------------------------------------------------
888dde65 346// wxPrinterDCImpl bit blitting/bitmap drawing
4b7f2165
VZ
347// ----------------------------------------------------------------------------
348
2f52b4e1
VZ
349// helper of DoDrawBitmap() and DoBlit()
350static
351bool DrawBitmapUsingStretchDIBits(HDC hdc,
352 const wxBitmap& bmp,
353 wxCoord x, wxCoord y)
354{
4676948b 355#if wxUSE_WXDIB
2f52b4e1 356 wxDIB dib(bmp);
999836aa
VZ
357 bool ok = dib.IsOk();
358 if ( !ok )
d71cc120 359 return false;
2f52b4e1
VZ
360
361 DIBSECTION ds;
362 if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) )
363 {
364 wxLogLastError(_T("GetObject(DIBSECTION)"));
365
d71cc120 366 return false;
2f52b4e1
VZ
367 }
368
369 // ok, we've got all data we need, do blit it
370 if ( ::StretchDIBits
371 (
372 hdc,
373 x, y,
374 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
375 0, 0,
376 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
377 ds.dsBm.bmBits,
378 (LPBITMAPINFO)&ds.dsBmih,
379 DIB_RGB_COLORS,
380 SRCCOPY
381 ) == GDI_ERROR )
382 {
383 wxLogLastError(wxT("StretchDIBits"));
f6081a04 384
d71cc120 385 return false;
2f52b4e1 386 }
84968677 387
d71cc120 388 return true;
4676948b 389#else
d71cc120 390 return false;
4676948b 391#endif
2f52b4e1 392}
e11f2e16 393
888dde65 394void wxPrinterDCImpl::DoDrawBitmap(const wxBitmap& bmp,
4b7f2165
VZ
395 wxCoord x, wxCoord y,
396 bool useMask)
397{
398 wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
399
400 int width = bmp.GetWidth(),
401 height = bmp.GetHeight();
402
2f52b4e1
VZ
403 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
404 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, x, y) )
4b7f2165 405 {
3103e8a9 406 // no support for StretchDIBits() or an error occurred if we got here
4b7f2165 407 wxMemoryDC memDC;
fea35690
VZ
408
409 memDC.SelectObjectAsSource(bmp);
4b7f2165 410
888dde65 411 GetOwner()->Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
4b7f2165
VZ
412
413 memDC.SelectObject(wxNullBitmap);
414 }
415}
416
888dde65 417bool wxPrinterDCImpl::DoBlit(wxCoord xdest, wxCoord ydest,
4b7f2165
VZ
418 wxCoord width, wxCoord height,
419 wxDC *source,
2eb10e2a 420 wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc),
0cbff120 421 int WXUNUSED(rop), bool useMask,
d699f48b 422 wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask))
4b7f2165 423{
888dde65
RR
424 wxDCImpl *impl = source->GetImpl();
425 wxMSWDCImpl *msw_impl = wxDynamicCast(impl, wxMSWDCImpl);
426 if (!msw_impl)
427 return false;
428
429 wxBitmap& bmp = msw_impl->GetSelectedBitmap();
2f52b4e1
VZ
430 wxMask *mask = useMask ? bmp.GetMask() : NULL;
431 if ( mask )
4b7f2165 432 {
0becd470
VZ
433 // If we are printing source colours are screen colours not printer
434 // colours and so we need copy the bitmap pixel by pixel.
4b7f2165 435 RECT rect;
888dde65 436 HDC dcSrc = GetHdcOf(*msw_impl);
2f52b4e1
VZ
437 MemoryHDC dcMask(dcSrc);
438 SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap());
4b7f2165 439
4b7f2165
VZ
440 for (int x = 0; x < width; x++)
441 {
442 for (int y = 0; y < height; y++)
443 {
2f52b4e1 444 COLORREF cref = ::GetPixel(dcMask, x, y);
4b7f2165
VZ
445 if (cref)
446 {
2f52b4e1 447 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y));
4b7f2165
VZ
448 rect.left = xdest + x;
449 rect.right = rect.left + 1;
33ac7e6f 450 rect.top = ydest + y;
4b7f2165
VZ
451 rect.bottom = rect.top + 1;
452 ::FillRect(GetHdc(), &rect, brush);
453 ::DeleteObject(brush);
454 }
455 }
456 }
4b7f2165
VZ
457 }
458 else // no mask
459 {
2f52b4e1 460 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
e5c4c38b 461 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) )
4b7f2165 462 {
2f52b4e1 463 // no support for StretchDIBits
4b7f2165 464
0becd470
VZ
465 // as we are printing, source colours are screen colours not
466 // printer colours and so we need copy the bitmap pixel by pixel.
888dde65 467 HDC dcSrc = GetHdcOf(*msw_impl);
4b7f2165
VZ
468 RECT rect;
469 for (int y = 0; y < height; y++)
470 {
0becd470 471 // optimization: draw identical adjacent pixels together.
4b7f2165
VZ
472 for (int x = 0; x < width; x++)
473 {
2f52b4e1 474 COLORREF col = ::GetPixel(dcSrc, x, y);
4b7f2165
VZ
475 HBRUSH brush = ::CreateSolidBrush( col );
476
477 rect.left = xdest + x;
478 rect.top = ydest + y;
2f52b4e1
VZ
479 while( (x + 1 < width) &&
480 (::GetPixel(dcSrc, x + 1, y) == col ) )
4b7f2165
VZ
481 {
482 ++x;
483 }
484 rect.right = xdest + x + 1;
485 rect.bottom = rect.top + 1;
486 ::FillRect((HDC) m_hDC, &rect, brush);
487 ::DeleteObject(brush);
488 }
489 }
490 }
491 }
492
d71cc120 493 return true;
4b7f2165 494}
f6bcfd97
BP
495
496#endif
497 // wxUSE_PRINTING_ARCHITECTURE