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