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