]>
Commit | Line | Data |
---|---|---|
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 | docinfo.lpszDatatype = NULL; | |
184 | docinfo.fwType = 0; | |
185 | ||
186 | if (!m_hDC) | |
187 | return false; | |
188 | ||
189 | int ret = ::StartDoc(GetHdc(), &docinfo); | |
190 | ||
191 | if (ret <= 0) | |
192 | { | |
193 | DWORD lastError = GetLastError(); | |
194 | wxLogDebug(wxT("wxDC::StartDoc failed with error: %ld\n"), lastError); | |
195 | } | |
196 | ||
197 | return (ret > 0); | |
198 | } | |
199 | ||
200 | void wxPrinterDC::EndDoc() | |
201 | { | |
202 | if (m_hDC) ::EndDoc((HDC) m_hDC); | |
203 | } | |
204 | ||
205 | void wxPrinterDC::StartPage() | |
206 | { | |
207 | if (m_hDC) | |
208 | ::StartPage((HDC) m_hDC); | |
209 | } | |
210 | ||
211 | void wxPrinterDC::EndPage() | |
212 | { | |
213 | if (m_hDC) | |
214 | ::EndPage((HDC) m_hDC); | |
215 | } | |
216 | ||
217 | // Returns default device and port names | |
218 | static bool wxGetDefaultDeviceName(wxString& deviceName, wxString& portName) | |
219 | { | |
220 | deviceName.clear(); | |
221 | ||
222 | LPDEVNAMES lpDevNames; | |
223 | LPTSTR lpszDeviceName; | |
224 | LPTSTR lpszPortName; | |
225 | ||
226 | PRINTDLG pd; | |
227 | ||
228 | // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68 | |
229 | #ifdef __GNUWIN32__ | |
230 | memset(&pd, 0, 66); | |
231 | pd.lStructSize = 66; // sizeof(PRINTDLG); | |
232 | #else | |
233 | memset(&pd, 0, sizeof(PRINTDLG)); | |
234 | pd.lStructSize = sizeof(PRINTDLG); | |
235 | #endif | |
236 | ||
237 | pd.hwndOwner = (HWND)NULL; | |
238 | pd.hDevMode = NULL; // Will be created by PrintDlg | |
239 | pd.hDevNames = NULL; // Ditto | |
240 | pd.Flags = PD_RETURNDEFAULT; | |
241 | pd.nCopies = 1; | |
242 | ||
243 | if (!PrintDlg((LPPRINTDLG)&pd)) | |
244 | { | |
245 | if ( pd.hDevMode ) | |
246 | GlobalFree(pd.hDevMode); | |
247 | if (pd.hDevNames) | |
248 | GlobalFree(pd.hDevNames); | |
249 | ||
250 | return false; | |
251 | } | |
252 | ||
253 | if (pd.hDevNames) | |
254 | { | |
255 | lpDevNames = (LPDEVNAMES)GlobalLock(pd.hDevNames); | |
256 | lpszDeviceName = (LPTSTR)lpDevNames + lpDevNames->wDeviceOffset; | |
257 | lpszPortName = (LPTSTR)lpDevNames + lpDevNames->wOutputOffset; | |
258 | ||
259 | deviceName = lpszDeviceName; | |
260 | portName = lpszPortName; | |
261 | ||
262 | GlobalUnlock(pd.hDevNames); | |
263 | GlobalFree(pd.hDevNames); | |
264 | pd.hDevNames=NULL; | |
265 | } | |
266 | ||
267 | if (pd.hDevMode) | |
268 | { | |
269 | GlobalFree(pd.hDevMode); | |
270 | pd.hDevMode=NULL; | |
271 | } | |
272 | return ( !deviceName.empty() ); | |
273 | } | |
274 | ||
275 | // Gets an HDC for the specified printer configuration | |
276 | WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& printDataConst) | |
277 | { | |
278 | #if defined(__WXUNIVERSAL__) && (!defined(__WXMSW__) || wxUSE_POSTSCRIPT_ARCHITECTURE_IN_MSW) | |
279 | ||
280 | #if 0 | |
281 | wxPostScriptPrintNativeData *data = | |
282 | (wxPostScriptPrintNativeData *) printDataConst.GetNativeData(); | |
283 | // FIXME: how further ??? | |
284 | #else | |
285 | return 0; | |
286 | #endif | |
287 | ||
288 | #else // Postscript vs. native Windows | |
289 | ||
290 | wxWindowsPrintNativeData *data = | |
291 | (wxWindowsPrintNativeData *) printDataConst.GetNativeData(); | |
292 | ||
293 | data->TransferFrom( printDataConst ); | |
294 | ||
295 | wxChar* driverName = (wxChar*) NULL; | |
296 | ||
297 | wxString devNameStr = printDataConst.GetPrinterName(); | |
298 | wxChar* portName = (wxChar*) NULL; // Obsolete in WIN32 | |
299 | ||
300 | const wxChar* deviceName; | |
301 | if ( !devNameStr ) | |
302 | deviceName = (wxChar*) NULL; | |
303 | else | |
304 | deviceName = devNameStr.c_str(); | |
305 | ||
306 | LPDEVMODE lpDevMode = (LPDEVMODE) NULL; | |
307 | ||
308 | HGLOBAL hDevMode = (HGLOBAL)(DWORD) data->GetDevMode(); | |
309 | ||
310 | if ( hDevMode ) | |
311 | lpDevMode = (DEVMODE*) GlobalLock(hDevMode); | |
312 | ||
313 | if ( !devNameStr ) | |
314 | { | |
315 | // Retrieve the default device name | |
316 | wxString portName; | |
317 | if ( !wxGetDefaultDeviceName(devNameStr, portName) ) | |
318 | { | |
319 | return 0; // Could not get default device name | |
320 | } | |
321 | deviceName = devNameStr.c_str(); | |
322 | } | |
323 | ||
324 | #ifdef __WIN32__ | |
325 | HDC hDC = CreateDC(driverName, deviceName, portName, (DEVMODE *) lpDevMode); | |
326 | #else | |
327 | HDC hDC = CreateDC(driverName, deviceName, portName, (LPSTR) lpDevMode); | |
328 | #endif | |
329 | ||
330 | if (hDevMode && lpDevMode) | |
331 | GlobalUnlock(hDevMode); | |
332 | ||
333 | return (WXHDC) hDC; | |
334 | #endif | |
335 | } | |
336 | ||
337 | // ---------------------------------------------------------------------------- | |
338 | // wxPrinterDC bit blitting/bitmap drawing | |
339 | // ---------------------------------------------------------------------------- | |
340 | ||
341 | // helper of DoDrawBitmap() and DoBlit() | |
342 | static | |
343 | bool DrawBitmapUsingStretchDIBits(HDC hdc, | |
344 | const wxBitmap& bmp, | |
345 | wxCoord x, wxCoord y) | |
346 | { | |
347 | #if wxUSE_WXDIB | |
348 | wxDIB dib(bmp); | |
349 | bool ok = dib.IsOk(); | |
350 | if ( !ok ) | |
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 occurred 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 |