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