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