]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dcprint.cpp
added wxUniChar::AsUTF8() for easy conversion in UTF8 build
[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
261205e4 196 if ( ::StartDoc(GetHdc(), &docinfo) <= 0 )
7bcb11d3 197 {
c5bc21bd
VZ
198 wxLogLastError(wxT("StartDoc"));
199 return false;
7bcb11d3 200 }
a17e237f 201
c5bc21bd 202 return true;
7bcb11d3
JS
203}
204
4b7f2165 205void wxPrinterDC::EndDoc()
7bcb11d3
JS
206{
207 if (m_hDC) ::EndDoc((HDC) m_hDC);
208}
209
4b7f2165 210void wxPrinterDC::StartPage()
7bcb11d3
JS
211{
212 if (m_hDC)
213 ::StartPage((HDC) m_hDC);
214}
215
4b7f2165 216void wxPrinterDC::EndPage()
7bcb11d3
JS
217{
218 if (m_hDC)
219 ::EndPage((HDC) m_hDC);
220}
221
f415cab9
JS
222
223wxRect wxPrinterDC::GetPaperRect()
224
225{
226 if (!Ok()) return wxRect(0, 0, 0, 0);
227 int w = ::GetDeviceCaps((HDC) m_hDC, PHYSICALWIDTH);
228 int h = ::GetDeviceCaps((HDC) m_hDC, PHYSICALHEIGHT);
229 int x = -::GetDeviceCaps((HDC) m_hDC, PHYSICALOFFSETX);
230 int y = -::GetDeviceCaps((HDC) m_hDC, PHYSICALOFFSETY);
231 return wxRect(x, y, w, h);
232}
233
234
632aaa18
VZ
235#if !wxUSE_PS_PRINTING
236
7bcb11d3
JS
237// Returns default device and port names
238static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
239{
7ba4fbeb 240 deviceName.clear();
7bcb11d3
JS
241
242 LPDEVNAMES lpDevNames;
161f4f73
VZ
243 LPTSTR lpszDeviceName;
244 LPTSTR lpszPortName;
a17e237f 245
7bcb11d3
JS
246 PRINTDLG pd;
247
248 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
249#ifdef __GNUWIN32__
161f4f73 250 memset(&pd, 0, 66);
7bcb11d3
JS
251 pd.lStructSize = 66; // sizeof(PRINTDLG);
252#else
161f4f73 253 memset(&pd, 0, sizeof(PRINTDLG));
7bcb11d3
JS
254 pd.lStructSize = sizeof(PRINTDLG);
255#endif
256
257 pd.hwndOwner = (HWND)NULL;
258 pd.hDevMode = NULL; // Will be created by PrintDlg
259 pd.hDevNames = NULL; // Ditto
260 pd.Flags = PD_RETURNDEFAULT;
261 pd.nCopies = 1;
a17e237f 262
7bcb11d3
JS
263 if (!PrintDlg((LPPRINTDLG)&pd))
264 {
265 if ( pd.hDevMode )
266 GlobalFree(pd.hDevMode);
267 if (pd.hDevNames)
268 GlobalFree(pd.hDevNames);
a17e237f 269
d71cc120 270 return false;
7bcb11d3 271 }
a17e237f 272
7bcb11d3
JS
273 if (pd.hDevNames)
274 {
275 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
161f4f73
VZ
276 lpszDeviceName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset;
277 lpszPortName = (LPTSTR)lpDevNames + lpDevNames->wOutputOffset;
7bcb11d3
JS
278
279 deviceName = lpszDeviceName;
280 portName = lpszPortName;
60fe7303
JS
281
282 GlobalUnlock(pd.hDevNames);
283 GlobalFree(pd.hDevNames);
284 pd.hDevNames=NULL;
7bcb11d3 285 }
a17e237f 286
7bcb11d3
JS
287 if (pd.hDevMode)
288 {
289 GlobalFree(pd.hDevMode);
290 pd.hDevMode=NULL;
291 }
489f6cf7 292 return ( !deviceName.empty() );
7bcb11d3
JS
293}
294
632aaa18
VZ
295#endif // !wxUSE_PS_PRINTING
296
7bcb11d3
JS
297// Gets an HDC for the specified printer configuration
298WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
299{
632aaa18
VZ
300#if wxUSE_PS_PRINTING
301 // TODO
302 wxUnusedVar(printDataConst);
498b94a6 303 return 0;
632aaa18 304#else // native Windows printing
8850cbd3
RR
305 wxWindowsPrintNativeData *data =
306 (wxWindowsPrintNativeData *) printDataConst.GetNativeData();
498b94a6 307
fd64de59 308 data->TransferFrom( printDataConst );
a17e237f 309
632aaa18
VZ
310 wxString deviceName = printDataConst.GetPrinterName();
311 if ( deviceName.empty() )
7bcb11d3
JS
312 {
313 // Retrieve the default device name
314 wxString portName;
632aaa18 315 if ( !wxGetDefaultDeviceName(deviceName, portName) )
3f8b4a3f
JS
316 {
317 return 0; // Could not get default device name
318 }
7bcb11d3 319 }
a17e237f 320
a17e237f 321
632aaa18
VZ
322 HGLOBAL hDevMode = (HGLOBAL)(DWORD) data->GetDevMode();
323
324 DEVMODE *lpDevMode = hDevMode ? (DEVMODE *)::GlobalLock(hDevMode) : NULL;
325
326 HDC hDC = ::CreateDC(NULL, deviceName, NULL, lpDevMode);
327 if ( !hDC )
328 wxLogLastError(_T("CreateDC(printer)"));
329
330 if ( lpDevMode )
331 ::GlobalUnlock(hDevMode);
a17e237f 332
7bcb11d3 333 return (WXHDC) hDC;
632aaa18 334#endif // PostScript/Windows printing
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 399 wxMemoryDC memDC;
fea35690
VZ
400
401 memDC.SelectObjectAsSource(bmp);
4b7f2165
VZ
402
403 Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
404
405 memDC.SelectObject(wxNullBitmap);
406 }
407}
408
409bool wxPrinterDC::DoBlit(wxCoord xdest, wxCoord ydest,
410 wxCoord width, wxCoord height,
411 wxDC *source,
2eb10e2a 412 wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc),
0cbff120 413 int WXUNUSED(rop), bool useMask,
d699f48b 414 wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask))
4b7f2165 415{
2f52b4e1
VZ
416 wxBitmap& bmp = source->GetSelectedBitmap();
417 wxMask *mask = useMask ? bmp.GetMask() : NULL;
418 if ( mask )
4b7f2165 419 {
0becd470
VZ
420 // If we are printing source colours are screen colours not printer
421 // colours and so we need copy the bitmap pixel by pixel.
4b7f2165 422 RECT rect;
2f52b4e1
VZ
423 HDC dcSrc = GetHdcOf(*source);
424 MemoryHDC dcMask(dcSrc);
425 SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap());
4b7f2165 426
4b7f2165
VZ
427 for (int x = 0; x < width; x++)
428 {
429 for (int y = 0; y < height; y++)
430 {
2f52b4e1 431 COLORREF cref = ::GetPixel(dcMask, x, y);
4b7f2165
VZ
432 if (cref)
433 {
2f52b4e1 434 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y));
4b7f2165
VZ
435 rect.left = xdest + x;
436 rect.right = rect.left + 1;
33ac7e6f 437 rect.top = ydest + y;
4b7f2165
VZ
438 rect.bottom = rect.top + 1;
439 ::FillRect(GetHdc(), &rect, brush);
440 ::DeleteObject(brush);
441 }
442 }
443 }
4b7f2165
VZ
444 }
445 else // no mask
446 {
2f52b4e1 447 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
e5c4c38b 448 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) )
4b7f2165 449 {
2f52b4e1 450 // no support for StretchDIBits
4b7f2165 451
0becd470
VZ
452 // as we are printing, source colours are screen colours not
453 // printer colours and so we need copy the bitmap pixel by pixel.
2f52b4e1 454 HDC dcSrc = GetHdcOf(*source);
4b7f2165
VZ
455 RECT rect;
456 for (int y = 0; y < height; y++)
457 {
0becd470 458 // optimization: draw identical adjacent pixels together.
4b7f2165
VZ
459 for (int x = 0; x < width; x++)
460 {
2f52b4e1 461 COLORREF col = ::GetPixel(dcSrc, x, y);
4b7f2165
VZ
462 HBRUSH brush = ::CreateSolidBrush( col );
463
464 rect.left = xdest + x;
465 rect.top = ydest + y;
2f52b4e1
VZ
466 while( (x + 1 < width) &&
467 (::GetPixel(dcSrc, x + 1, y) == col ) )
4b7f2165
VZ
468 {
469 ++x;
470 }
471 rect.right = xdest + x + 1;
472 rect.bottom = rect.top + 1;
473 ::FillRect((HDC) m_hDC, &rect, brush);
474 ::DeleteObject(brush);
475 }
476 }
477 }
478 }
479
d71cc120 480 return true;
4b7f2165 481}
f6bcfd97
BP
482
483#endif
484 // wxUSE_PRINTING_ARCHITECTURE