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