]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/dcprint.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/dcprint.cpp
3 // Purpose: wxPrinterDC class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dcprint.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
34 #include "wx/window.h"
35 #include "wx/dcmemory.h"
38 #if wxUSE_PRINTING_ARCHITECTURE
40 #include "wx/msw/private.h"
41 #include "wx/dcprint.h"
44 #if wxUSE_COMMON_DIALOGS || defined(__WXWINE__)
52 // mingw32 defines GDI_ERROR incorrectly
55 #define GDI_ERROR ((int)-1)
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 IMPLEMENT_CLASS(wxPrinterDC
, wxDC
)
64 // ============================================================================
66 // ============================================================================
68 // ----------------------------------------------------------------------------
69 // wxPrinterDC construction
70 // ----------------------------------------------------------------------------
72 // This form is deprecated
73 wxPrinterDC::wxPrinterDC(const wxString
& driver_name
,
74 const wxString
& device_name
,
79 m_isInteractive
= interactive
;
82 m_printData
.SetFilename(file
);
84 #if wxUSE_COMMON_DIALOGS
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
;
99 pd
.hInstance
= (HINSTANCE
)NULL
;
101 m_ok
= PrintDlg( &pd
) != 0;
104 m_hDC
= (WXHDC
) pd
.hDC
;
108 #endif // wxUSE_COMMON_DIALOGS
110 if ( !driver_name
.empty() && !device_name
.empty() && !file
.empty() )
112 m_hDC
= (WXHDC
) CreateDC(driver_name
, device_name
, file
, NULL
);
114 else // we don't have all parameters, ask the user
116 wxPrintData printData
;
117 printData
.SetOrientation(orientation
);
118 m_hDC
= wxGetPrinterDC(printData
);
121 m_ok
= m_hDC
? TRUE
: FALSE
;
123 // as we created it, we must delete it as well
130 wxPrinterDC::wxPrinterDC(const wxPrintData
& printData
)
132 m_printData
= printData
;
134 m_isInteractive
= FALSE
;
136 m_hDC
= wxGetPrinterDC(printData
);
144 wxPrinterDC::wxPrinterDC(WXHDC dc
)
146 m_isInteractive
= FALSE
;
153 void wxPrinterDC::Init()
157 // int width = GetDeviceCaps(m_hDC, VERTRES);
158 // int height = GetDeviceCaps(m_hDC, HORZRES);
159 SetMapMode(wxMM_TEXT
);
161 SetBrush(*wxBLACK_BRUSH
);
162 SetPen(*wxBLACK_PEN
);
166 // ----------------------------------------------------------------------------
167 // wxPrinterDC {Start/End}{Page/Doc} methods
168 // ----------------------------------------------------------------------------
170 bool wxPrinterDC::StartDoc(const wxString
& message
)
173 docinfo
.cbSize
= sizeof(DOCINFO
);
174 docinfo
.lpszDocName
= (const wxChar
*)message
;
176 wxString
filename(m_printData
.GetFilename());
178 if (filename
.IsEmpty())
179 docinfo
.lpszOutput
= NULL
;
181 docinfo
.lpszOutput
= (const wxChar
*) filename
;
183 #if defined(__WIN95__)
184 docinfo
.lpszDatatype
= NULL
;
191 int ret
= ::StartDoc(GetHdc(), &docinfo
);
196 DWORD lastError
= GetLastError();
197 wxLogDebug(wxT("wxDC::StartDoc failed with error: %d\n"), lastError
);
204 void wxPrinterDC::EndDoc()
206 if (m_hDC
) ::EndDoc((HDC
) m_hDC
);
209 void wxPrinterDC::StartPage()
212 ::StartPage((HDC
) m_hDC
);
215 void wxPrinterDC::EndPage()
218 ::EndPage((HDC
) m_hDC
);
221 // Returns default device and port names
222 static bool wxGetDefaultDeviceName(wxString
& deviceName
, wxString
& portName
)
226 LPDEVNAMES lpDevNames
;
227 LPSTR lpszDriverName
;
228 LPSTR lpszDeviceName
;
233 // Cygwin has trouble believing PRINTDLG is 66 bytes - thinks it is 68
235 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
237 pd
.lStructSize
= sizeof(PRINTDLG
);
240 pd
.hwndOwner
= (HWND
)NULL
;
241 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
242 pd
.hDevNames
= NULL
; // Ditto
243 pd
.Flags
= PD_RETURNDEFAULT
;
246 if (!PrintDlg((LPPRINTDLG
)&pd
))
249 GlobalFree(pd
.hDevMode
);
251 GlobalFree(pd
.hDevNames
);
258 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
259 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
260 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
261 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
263 deviceName
= lpszDeviceName
;
264 portName
= lpszPortName
;
266 GlobalUnlock(pd
.hDevNames
);
267 GlobalFree(pd
.hDevNames
);
273 GlobalFree(pd
.hDevMode
);
276 return ( deviceName
!= wxT("") );
280 // This uses defaults, except for orientation, so we should eliminate this function
281 // and use the 2nd form (passing wxPrintData) instead.
282 WXHDC
wxGetPrinterDC(int orientation
)
285 LPDEVMODE lpDevMode
= NULL
;
286 LPDEVNAMES lpDevNames
;
287 LPSTR lpszDriverName
;
288 LPSTR lpszDeviceName
;
292 // __GNUWIN32__ has trouble believing PRINTDLG is 66 bytes - thinks it is 68
294 pd
.lStructSize
= 66; // sizeof(PRINTDLG);
296 pd
.lStructSize
= sizeof(PRINTDLG
);
298 pd
.hwndOwner
= (HWND
)NULL
;
299 pd
.hDevMode
= NULL
; // Will be created by PrintDlg
300 pd
.hDevNames
= NULL
; // Ditto
301 pd
.Flags
= PD_RETURNDEFAULT
;
304 if (!PrintDlg((LPPRINTDLG
)&pd
))
307 GlobalFree(pd
.hDevMode
);
309 GlobalFree(pd
.hDevNames
);
317 GlobalFree(pd
.hDevMode
);
320 lpDevNames
= (LPDEVNAMES
)GlobalLock(pd
.hDevNames
);
321 lpszDriverName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDriverOffset
;
322 lpszDeviceName
= (LPSTR
)lpDevNames
+ lpDevNames
->wDeviceOffset
;
323 lpszPortName
= (LPSTR
)lpDevNames
+ lpDevNames
->wOutputOffset
;
324 GlobalUnlock(pd
.hDevNames
);
328 lpDevMode
= (DEVMODE
*) GlobalLock(pd
.hDevMode
);
329 lpDevMode
->dmOrientation
= orientation
;
330 lpDevMode
->dmFields
|= DM_ORIENTATION
;
334 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (DEVMODE
*)lpDevMode
);
336 hDC
= CreateDC(lpszDriverName
, lpszDeviceName
, lpszPortName
, (LPSTR
)lpDevMode
);
339 if (pd
.hDevMode
&& lpDevMode
)
340 GlobalUnlock(pd
.hDevMode
);
344 GlobalFree(pd
.hDevNames
);
349 GlobalFree(pd
.hDevMode
);
356 // Gets an HDC for the specified printer configuration
357 WXHDC WXDLLEXPORT
wxGetPrinterDC(const wxPrintData
& printDataConst
)
359 wxPrintData printData
= printDataConst
;
360 printData
.ConvertToNative();
362 wxChar
* driverName
= (wxChar
*) NULL
;
364 wxString devNameStr
= printData
.GetPrinterName();
365 wxChar
* portName
= (wxChar
*) NULL
; // Obsolete in WIN32
367 const wxChar
* deviceName
;
369 deviceName
= (wxChar
*) NULL
;
371 deviceName
= devNameStr
.c_str();
373 LPDEVMODE lpDevMode
= (LPDEVMODE
) NULL
;
375 HGLOBAL hDevMode
= (HGLOBAL
)(DWORD
) printData
.GetNativeData();
378 lpDevMode
= (DEVMODE
*) GlobalLock(hDevMode
);
382 // Retrieve the default device name
388 #endif // Debug/Release
389 wxGetDefaultDeviceName(devNameStr
, portName
);
391 wxASSERT_MSG( ret
, wxT("Could not get default device name.") );
393 deviceName
= devNameStr
.c_str();
397 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (DEVMODE
*) lpDevMode
);
399 HDC hDC
= CreateDC(driverName
, deviceName
, portName
, (LPSTR
) lpDevMode
);
402 if (hDevMode
&& lpDevMode
)
403 GlobalUnlock(hDevMode
);
408 // ----------------------------------------------------------------------------
409 // wxPrinterDC bit blitting/bitmap drawing
410 // ----------------------------------------------------------------------------
412 // Win16 doesn't define GDI_ERROR.
417 void wxPrinterDC::DoDrawBitmap(const wxBitmap
&bmp
,
418 wxCoord x
, wxCoord y
,
421 wxCHECK_RET( bmp
.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
423 int width
= bmp
.GetWidth(),
424 height
= bmp
.GetHeight();
426 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
)
428 BITMAPINFO
*info
= (BITMAPINFO
*) malloc( sizeof( BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
) );
429 memset( info
, 0, sizeof( BITMAPINFOHEADER
) );
431 int iBitsSize
= ((width
+ 3 ) & ~3 ) * height
;
433 void* bits
= malloc( iBitsSize
);
435 info
->bmiHeader
.biSize
= sizeof( BITMAPINFOHEADER
);
436 info
->bmiHeader
.biWidth
= width
;
437 info
->bmiHeader
.biHeight
= height
;
438 info
->bmiHeader
.biPlanes
= 1;
439 info
->bmiHeader
.biBitCount
= 8;
440 info
->bmiHeader
.biCompression
= BI_RGB
;
443 if ( GetDIBits(display
, GetHbitmapOf(bmp
), 0,
444 bmp
.GetHeight(), bits
, info
,
447 if ( ::StretchDIBits(GetHdc(), x
, y
,
449 0 , 0, width
, height
,
451 DIB_RGB_COLORS
, SRCCOPY
) == GDI_ERROR
)
453 wxLogLastError(wxT("StretchDIBits"));
460 else // no support for StretchDIBits()
463 memDC
.SelectObject(bmp
);
465 Blit(x
, y
, width
, height
, &memDC
, 0, 0, wxCOPY
, useMask
);
467 memDC
.SelectObject(wxNullBitmap
);
471 bool wxPrinterDC::DoBlit(wxCoord xdest
, wxCoord ydest
,
472 wxCoord width
, wxCoord height
,
474 wxCoord xsrc
, wxCoord ysrc
,
475 int WXUNUSED(rop
), bool useMask
)
481 // If we are printing source colours are screen colours
482 // not printer colours and so we need copy the bitmap
485 HDC dc_src
= GetHdcOf(*source
);
486 HDC dc_mask
= ::CreateCompatibleDC(dc_src
);
488 ::SelectObject(dc_mask
, (HBITMAP
) source
->GetSelectedBitmap().GetMask()->GetMaskBitmap());
489 for (int x
= 0; x
< width
; x
++)
491 for (int y
= 0; y
< height
; y
++)
493 COLORREF cref
= ::GetPixel(dc_mask
, x
, y
);
496 HBRUSH brush
= ::CreateSolidBrush(::GetPixel(dc_src
, x
, y
));
497 rect
.left
= xdest
+ x
;
498 rect
.right
= rect
.left
+ 1;
499 rect
.top
= ydest
+ y
;
500 rect
.bottom
= rect
.top
+ 1;
501 ::FillRect(GetHdc(), &rect
, brush
);
502 ::DeleteObject(brush
);
506 ::SelectObject(dc_mask
, 0);
511 if ( ::GetDeviceCaps(GetHdc(), RASTERCAPS
) & RC_STRETCHDIB
)
513 wxBitmap
& bmp
= source
->GetSelectedBitmap();
514 int width
= bmp
.GetWidth(),
515 height
= bmp
.GetHeight();
517 BITMAPINFO
*info
= (BITMAPINFO
*) malloc( sizeof( BITMAPINFOHEADER
) + 256 * sizeof(RGBQUAD
) );
518 int iBitsSize
= ((width
+ 3 ) & ~3 ) * height
;
520 void* bits
= malloc( iBitsSize
);
522 memset( info
, 0 , sizeof( BITMAPINFOHEADER
) );
524 info
->bmiHeader
.biSize
= sizeof( BITMAPINFOHEADER
);
525 info
->bmiHeader
.biWidth
= width
;
526 info
->bmiHeader
.biHeight
= height
;
527 info
->bmiHeader
.biPlanes
= 1;
528 info
->bmiHeader
.biBitCount
= 8;
529 info
->bmiHeader
.biCompression
= BI_RGB
;
532 if ( !::GetDIBits(display
, GetHbitmapOf(bmp
), 0,
533 height
, bits
, info
, DIB_RGB_COLORS
) )
535 wxLogLastError(wxT("GetDIBits"));
542 success
= ::StretchDIBits(GetHdc(), xdest
, ydest
,
548 SRCCOPY
) != GDI_ERROR
;
551 wxLogLastError(wxT("StretchDIBits"));
558 else // no support for StretchDIBits
560 // as we are printing, source colours are screen colours not printer
561 // colours and so we need copy the bitmap pixel by pixel.
562 HDC dc_src
= GetHdcOf(*source
);
564 for (int y
= 0; y
< height
; y
++)
566 // This is Stefan Csomor's optimisation, where identical adjacent
567 // pixels are drawn together.
568 for (int x
= 0; x
< width
; x
++)
570 COLORREF col
= ::GetPixel(dc_src
, x
, y
);
571 HBRUSH brush
= ::CreateSolidBrush( col
);
573 rect
.left
= xdest
+ x
;
574 rect
.top
= ydest
+ y
;
575 while( (x
+ 1 < width
) && (::GetPixel(dc_src
, x
+ 1, y
) == col
) )
579 rect
.right
= xdest
+ x
+ 1;
580 rect
.bottom
= rect
.top
+ 1;
581 ::FillRect((HDC
) m_hDC
, &rect
, brush
);
582 ::DeleteObject(brush
);
592 // wxUSE_PRINTING_ARCHITECTURE