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