]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/msw/dcprint.cpp
added wxUniChar::AsUTF8() for easy conversion in UTF8 build
[wxWidgets.git] / src / msw / dcprint.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/msw/dcprint.cpp
3// Purpose: wxPrinterDC class
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
8// Copyright: (c) Julian Smart
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_PRINTING_ARCHITECTURE
28
29#include "wx/dcprint.h"
30
31#ifndef WX_PRECOMP
32 #include "wx/msw/wrapcdlg.h"
33 #include "wx/string.h"
34 #include "wx/log.h"
35 #include "wx/window.h"
36 #include "wx/dcmemory.h"
37 #include "wx/math.h"
38#endif
39
40#include "wx/msw/private.h"
41
42#if wxUSE_WXDIB
43 #include "wx/msw/dib.h"
44#endif
45
46#include "wx/printdlg.h"
47#include "wx/msw/printdlg.h"
48
49#ifndef __WIN32__
50 #include <print.h>
51#endif
52
53// mingw32 defines GDI_ERROR incorrectly
54#if defined(__GNUWIN32__) || !defined(GDI_ERROR)
55 #undef GDI_ERROR
56 #define GDI_ERROR ((int)-1)
57#endif
58
59#if defined(__WXUNIVERSAL__) && wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW
60 #define wxUSE_PS_PRINTING 1
61#else
62 #define wxUSE_PS_PRINTING 0
63#endif
64
65// ----------------------------------------------------------------------------
66// wxWin macros
67// ----------------------------------------------------------------------------
68
69IMPLEMENT_CLASS(wxPrinterDC, wxDC)
70
71// ============================================================================
72// implementation
73// ============================================================================
74
75// ----------------------------------------------------------------------------
76// wxPrinterDC construction
77// ----------------------------------------------------------------------------
78
79// This form is deprecated
80wxPrinterDC::wxPrinterDC(const wxString& driver_name,
81 const wxString& device_name,
82 const wxString& file,
83 bool interactive,
84 int orientation)
85{
86 m_isInteractive = interactive;
87
88 if ( !file.empty() )
89 m_printData.SetFilename(file);
90
91#if wxUSE_COMMON_DIALOGS
92 if ( interactive )
93 {
94 PRINTDLG pd;
95
96 pd.lStructSize = sizeof( PRINTDLG );
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 )
110 {
111 m_hDC = (WXHDC) pd.hDC;
112 }
113 }
114 else
115#endif // wxUSE_COMMON_DIALOGS
116 {
117 if ( !driver_name.empty() && !device_name.empty() && !file.empty() )
118 {
119 m_hDC = (WXHDC) CreateDC(driver_name, device_name, file, NULL);
120 }
121 else // we don't have all parameters, ask the user
122 {
123 wxPrintData printData;
124 printData.SetOrientation(orientation);
125 m_hDC = wxGetPrinterDC(printData);
126 }
127
128 m_ok = m_hDC ? true: false;
129
130 // as we created it, we must delete it as well
131 m_bOwnsDC = true;
132 }
133
134 Init();
135}
136
137wxPrinterDC::wxPrinterDC(const wxPrintData& printData)
138{
139 m_printData = printData;
140
141 m_isInteractive = false;
142
143 m_hDC = wxGetPrinterDC(printData);
144 m_ok = m_hDC != 0;
145 m_bOwnsDC = true;
146
147 Init();
148}
149
150
151wxPrinterDC::wxPrinterDC(WXHDC dc)
152{
153 m_isInteractive = false;
154
155 m_hDC = dc;
156 m_bOwnsDC = true;
157 m_ok = true;
158}
159
160void wxPrinterDC::Init()
161{
162 if ( m_hDC )
163 {
164 // int width = GetDeviceCaps(m_hDC, VERTRES);
165 // int height = GetDeviceCaps(m_hDC, HORZRES);
166 SetMapMode(wxMM_TEXT);
167
168 SetBrush(*wxBLACK_BRUSH);
169 SetPen(*wxBLACK_PEN);
170 }
171}
172
173// ----------------------------------------------------------------------------
174// wxPrinterDC {Start/End}{Page/Doc} methods
175// ----------------------------------------------------------------------------
176
177bool wxPrinterDC::StartDoc(const wxString& message)
178{
179 DOCINFO docinfo;
180 docinfo.cbSize = sizeof(DOCINFO);
181 docinfo.lpszDocName = (const wxChar*)message;
182
183 wxString filename(m_printData.GetFilename());
184
185 if (filename.empty())
186 docinfo.lpszOutput = NULL;
187 else
188 docinfo.lpszOutput = (const wxChar *) filename;
189
190 docinfo.lpszDatatype = NULL;
191 docinfo.fwType = 0;
192
193 if (!m_hDC)
194 return false;
195
196 if ( ::StartDoc(GetHdc(), &docinfo) <= 0 )
197 {
198 wxLogLastError(wxT("StartDoc"));
199 return false;
200 }
201
202 return true;
203}
204
205void wxPrinterDC::EndDoc()
206{
207 if (m_hDC) ::EndDoc((HDC) m_hDC);
208}
209
210void wxPrinterDC::StartPage()
211{
212 if (m_hDC)
213 ::StartPage((HDC) m_hDC);
214}
215
216void wxPrinterDC::EndPage()
217{
218 if (m_hDC)
219 ::EndPage((HDC) m_hDC);
220}
221
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
235#if !wxUSE_PS_PRINTING
236
237// Returns default device and port names
238static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
239{
240 deviceName.clear();
241
242 LPDEVNAMES lpDevNames;
243 LPTSTR lpszDeviceName;
244 LPTSTR lpszPortName;
245
246 PRINTDLG pd;
247
248 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
249#ifdef __GNUWIN32__
250 memset(&pd, 0, 66);
251 pd.lStructSize = 66; // sizeof(PRINTDLG);
252#else
253 memset(&pd, 0, sizeof(PRINTDLG));
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;
262
263 if (!PrintDlg((LPPRINTDLG)&pd))
264 {
265 if ( pd.hDevMode )
266 GlobalFree(pd.hDevMode);
267 if (pd.hDevNames)
268 GlobalFree(pd.hDevNames);
269
270 return false;
271 }
272
273 if (pd.hDevNames)
274 {
275 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
276 lpszDeviceName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset;
277 lpszPortName = (LPTSTR)lpDevNames + lpDevNames->wOutputOffset;
278
279 deviceName = lpszDeviceName;
280 portName = lpszPortName;
281
282 GlobalUnlock(pd.hDevNames);
283 GlobalFree(pd.hDevNames);
284 pd.hDevNames=NULL;
285 }
286
287 if (pd.hDevMode)
288 {
289 GlobalFree(pd.hDevMode);
290 pd.hDevMode=NULL;
291 }
292 return ( !deviceName.empty() );
293}
294
295#endif // !wxUSE_PS_PRINTING
296
297// Gets an HDC for the specified printer configuration
298WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
299{
300#if wxUSE_PS_PRINTING
301 // TODO
302 wxUnusedVar(printDataConst);
303 return 0;
304#else // native Windows printing
305 wxWindowsPrintNativeData *data =
306 (wxWindowsPrintNativeData *) printDataConst.GetNativeData();
307
308 data->TransferFrom( printDataConst );
309
310 wxString deviceName = printDataConst.GetPrinterName();
311 if ( deviceName.empty() )
312 {
313 // Retrieve the default device name
314 wxString portName;
315 if ( !wxGetDefaultDeviceName(deviceName, portName) )
316 {
317 return 0; // Could not get default device name
318 }
319 }
320
321
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);
332
333 return (WXHDC) hDC;
334#endif // PostScript/Windows printing
335}
336
337// ----------------------------------------------------------------------------
338// wxPrinterDC bit blitting/bitmap drawing
339// ----------------------------------------------------------------------------
340
341// helper of DoDrawBitmap() and DoBlit()
342static
343bool DrawBitmapUsingStretchDIBits(HDC hdc,
344 const wxBitmap& bmp,
345 wxCoord x, wxCoord y)
346{
347#if wxUSE_WXDIB
348 wxDIB dib(bmp);
349 bool ok = dib.IsOk();
350 if ( !ok )
351 return false;
352
353 DIBSECTION ds;
354 if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) )
355 {
356 wxLogLastError(_T("GetObject(DIBSECTION)"));
357
358 return false;
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"));
376
377 return false;
378 }
379
380 return true;
381#else
382 return false;
383#endif
384}
385
386void wxPrinterDC::DoDrawBitmap(const wxBitmap& bmp,
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
395 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
396 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, x, y) )
397 {
398 // no support for StretchDIBits() or an error occurred if we got here
399 wxMemoryDC memDC;
400
401 memDC.SelectObjectAsSource(bmp);
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,
412 wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc),
413 int WXUNUSED(rop), bool useMask,
414 wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask))
415{
416 wxBitmap& bmp = source->GetSelectedBitmap();
417 wxMask *mask = useMask ? bmp.GetMask() : NULL;
418 if ( mask )
419 {
420 // If we are printing source colours are screen colours not printer
421 // colours and so we need copy the bitmap pixel by pixel.
422 RECT rect;
423 HDC dcSrc = GetHdcOf(*source);
424 MemoryHDC dcMask(dcSrc);
425 SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap());
426
427 for (int x = 0; x < width; x++)
428 {
429 for (int y = 0; y < height; y++)
430 {
431 COLORREF cref = ::GetPixel(dcMask, x, y);
432 if (cref)
433 {
434 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y));
435 rect.left = xdest + x;
436 rect.right = rect.left + 1;
437 rect.top = ydest + y;
438 rect.bottom = rect.top + 1;
439 ::FillRect(GetHdc(), &rect, brush);
440 ::DeleteObject(brush);
441 }
442 }
443 }
444 }
445 else // no mask
446 {
447 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
448 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) )
449 {
450 // no support for StretchDIBits
451
452 // as we are printing, source colours are screen colours not
453 // printer colours and so we need copy the bitmap pixel by pixel.
454 HDC dcSrc = GetHdcOf(*source);
455 RECT rect;
456 for (int y = 0; y < height; y++)
457 {
458 // optimization: draw identical adjacent pixels together.
459 for (int x = 0; x < width; x++)
460 {
461 COLORREF col = ::GetPixel(dcSrc, x, y);
462 HBRUSH brush = ::CreateSolidBrush( col );
463
464 rect.left = xdest + x;
465 rect.top = ydest + y;
466 while( (x + 1 < width) &&
467 (::GetPixel(dcSrc, x + 1, y) == col ) )
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
480 return true;
481}
482
483#endif
484 // wxUSE_PRINTING_ARCHITECTURE