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