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