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