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