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