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