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