]> git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
Warning fixes found under hardest mode of OpenWatcom. Seems clean in Borland, MinGW...
[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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 if (ret <= 0)
199 {
200 DWORD lastError = GetLastError();
201 wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError);
202 }
203
204 return (ret > 0);
205 }
206
207 void wxPrinterDC::EndDoc()
208 {
209 if (m_hDC) ::EndDoc((HDC) m_hDC);
210 }
211
212 void wxPrinterDC::StartPage()
213 {
214 if (m_hDC)
215 ::StartPage((HDC) m_hDC);
216 }
217
218 void wxPrinterDC::EndPage()
219 {
220 if (m_hDC)
221 ::EndPage((HDC) m_hDC);
222 }
223
224 // Returns default device and port names
225 static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName)
226 {
227 deviceName.clear();
228
229 LPDEVNAMES lpDevNames;
230 LPTSTR lpszDeviceName;
231 LPTSTR lpszPortName;
232
233 PRINTDLG pd;
234
235 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
236 #ifdef __GNUWIN32__
237 memset(&pd, 0, 66);
238 pd.lStructSize = 66; // sizeof(PRINTDLG);
239 #else
240 memset(&pd, 0, sizeof(PRINTDLG));
241 pd.lStructSize = sizeof(PRINTDLG);
242 #endif
243
244 pd.hwndOwner = (HWND)NULL;
245 pd.hDevMode = NULL; // Will be created by PrintDlg
246 pd.hDevNames = NULL; // Ditto
247 pd.Flags = PD_RETURNDEFAULT;
248 pd.nCopies = 1;
249
250 if (!PrintDlg((LPPRINTDLG)&pd))
251 {
252 if ( pd.hDevMode )
253 GlobalFree(pd.hDevMode);
254 if (pd.hDevNames)
255 GlobalFree(pd.hDevNames);
256
257 return false;
258 }
259
260 if (pd.hDevNames)
261 {
262 lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames);
263 lpszDeviceName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset;
264 lpszPortName = (LPTSTR)lpDevNames + lpDevNames->wOutputOffset;
265
266 deviceName = lpszDeviceName;
267 portName = lpszPortName;
268
269 GlobalUnlock(pd.hDevNames);
270 GlobalFree(pd.hDevNames);
271 pd.hDevNames=NULL;
272 }
273
274 if (pd.hDevMode)
275 {
276 GlobalFree(pd.hDevMode);
277 pd.hDevMode=NULL;
278 }
279 return ( deviceName != wxEmptyString );
280 }
281
282 // Gets an HDC for the specified printer configuration
283 WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst)
284 {
285 wxPrintData printData = printDataConst;
286 printData.ConvertToNative();
287
288 wxChar* driverName = (wxChar*) NULL;
289
290 wxString devNameStr = printData.GetPrinterName();
291 wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32
292
293 const wxChar* deviceName;
294 if ( !devNameStr )
295 deviceName = (wxChar*) NULL;
296 else
297 deviceName = devNameStr.c_str();
298
299 LPDEVMODE lpDevMode = (LPDEVMODE) NULL;
300
301 HGLOBAL hDevMode = (HGLOBAL)(DWORD) printData.GetNativeData();
302
303 if ( hDevMode )
304 lpDevMode = (DEVMODE*) GlobalLock(hDevMode);
305
306 if ( !devNameStr )
307 {
308 // Retrieve the default device name
309 wxString portName;
310 if ( !wxGetDefaultDeviceName(devNameStr, portName) )
311 {
312 return 0; // Could not get default device name
313 }
314 deviceName = devNameStr.c_str();
315 }
316
317 #ifdef __WIN32__
318 HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode);
319 #else
320 HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode);
321 #endif
322
323 if (hDevMode && lpDevMode)
324 GlobalUnlock(hDevMode);
325
326 return (WXHDC) hDC;
327 }
328
329 // ----------------------------------------------------------------------------
330 // wxPrinterDC bit blitting/bitmap drawing
331 // ----------------------------------------------------------------------------
332
333 // helper of DoDrawBitmap() and DoBlit()
334 static
335 bool DrawBitmapUsingStretchDIBits(HDC hdc,
336 const wxBitmap& bmp,
337 wxCoord x, wxCoord y)
338 {
339 #if wxUSE_WXDIB
340 wxDIB dib(bmp);
341 bool ok = dib.IsOk();
342 if ( !ok )
343 return false;
344
345 DIBSECTION ds;
346 if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) )
347 {
348 wxLogLastError(_T("GetObject(DIBSECTION)"));
349
350 return false;
351 }
352
353 // ok, we've got all data we need, do blit it
354 if ( ::StretchDIBits
355 (
356 hdc,
357 x, y,
358 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
359 0, 0,
360 ds.dsBmih.biWidth, ds.dsBmih.biHeight,
361 ds.dsBm.bmBits,
362 (LPBITMAPINFO)&ds.dsBmih,
363 DIB_RGB_COLORS,
364 SRCCOPY
365 ) == GDI_ERROR )
366 {
367 wxLogLastError(wxT("StretchDIBits"));
368
369 return false;
370 }
371
372 return true;
373 #else
374 return false;
375 #endif
376 }
377
378 void wxPrinterDC::DoDrawBitmap(const wxBitmap& bmp,
379 wxCoord x, wxCoord y,
380 bool useMask)
381 {
382 wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
383
384 int width = bmp.GetWidth(),
385 height = bmp.GetHeight();
386
387 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
388 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, x, y) )
389 {
390 // no support for StretchDIBits() or an error occured if we got here
391 wxMemoryDC memDC;
392 memDC.SelectObject(bmp);
393
394 Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
395
396 memDC.SelectObject(wxNullBitmap);
397 }
398 }
399
400 bool wxPrinterDC::DoBlit(wxCoord xdest, wxCoord ydest,
401 wxCoord width, wxCoord height,
402 wxDC *source,
403 wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc),
404 int WXUNUSED(rop), bool useMask,
405 wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask))
406 {
407 wxBitmap& bmp = source->GetSelectedBitmap();
408 wxMask *mask = useMask ? bmp.GetMask() : NULL;
409 if ( mask )
410 {
411 // If we are printing source colours are screen colours not printer
412 // colours and so we need copy the bitmap pixel by pixel.
413 RECT rect;
414 HDC dcSrc = GetHdcOf(*source);
415 MemoryHDC dcMask(dcSrc);
416 SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap());
417
418 for (int x = 0; x < width; x++)
419 {
420 for (int y = 0; y < height; y++)
421 {
422 COLORREF cref = ::GetPixel(dcMask, x, y);
423 if (cref)
424 {
425 HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y));
426 rect.left = xdest + x;
427 rect.right = rect.left + 1;
428 rect.top = ydest + y;
429 rect.bottom = rect.top + 1;
430 ::FillRect(GetHdc(), &rect, brush);
431 ::DeleteObject(brush);
432 }
433 }
434 }
435 }
436 else // no mask
437 {
438 if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
439 !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) )
440 {
441 // no support for StretchDIBits
442
443 // as we are printing, source colours are screen colours not
444 // printer colours and so we need copy the bitmap pixel by pixel.
445 HDC dcSrc = GetHdcOf(*source);
446 RECT rect;
447 for (int y = 0; y < height; y++)
448 {
449 // optimization: draw identical adjacent pixels together.
450 for (int x = 0; x < width; x++)
451 {
452 COLORREF col = ::GetPixel(dcSrc, x, y);
453 HBRUSH brush = ::CreateSolidBrush( col );
454
455 rect.left = xdest + x;
456 rect.top = ydest + y;
457 while( (x + 1 < width) &&
458 (::GetPixel(dcSrc, x + 1, y) == col ) )
459 {
460 ++x;
461 }
462 rect.right = xdest + x + 1;
463 rect.bottom = rect.top + 1;
464 ::FillRect((HDC) m_hDC, &rect, brush);
465 ::DeleteObject(brush);
466 }
467 }
468 }
469 }
470
471 return true;
472 }
473
474 #endif
475 // wxUSE_PRINTING_ARCHITECTURE