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