+// ----------------------------------------------------------------------------
+// wxPrinterDC bit blitting/bitmap drawing
+// ----------------------------------------------------------------------------
+
+// helper of DoDrawBitmap() and DoBlit()
+static
+bool DrawBitmapUsingStretchDIBits(HDC hdc,
+ const wxBitmap& bmp,
+ wxCoord x, wxCoord y)
+{
+#if wxUSE_WXDIB
+ wxDIB dib(bmp);
+ bool ok = dib.IsOk();
+ if ( !ok )
+ return FALSE;
+
+ DIBSECTION ds;
+ if ( !::GetObject(dib.GetHandle(), sizeof(ds), &ds) )
+ {
+ wxLogLastError(_T("GetObject(DIBSECTION)"));
+
+ return FALSE;
+ }
+
+ // ok, we've got all data we need, do blit it
+ if ( ::StretchDIBits
+ (
+ hdc,
+ x, y,
+ ds.dsBmih.biWidth, ds.dsBmih.biHeight,
+ 0, 0,
+ ds.dsBmih.biWidth, ds.dsBmih.biHeight,
+ ds.dsBm.bmBits,
+ (LPBITMAPINFO)&ds.dsBmih,
+ DIB_RGB_COLORS,
+ SRCCOPY
+ ) == GDI_ERROR )
+ {
+ wxLogLastError(wxT("StretchDIBits"));
+
+ return FALSE;
+ }
+
+ return TRUE;
+#else
+ return FALSE;
+#endif
+}
+
+void wxPrinterDC::DoDrawBitmap(const wxBitmap& bmp,
+ wxCoord x, wxCoord y,
+ bool useMask)
+{
+ wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxPrinterDC::DrawBitmap") );
+
+ int width = bmp.GetWidth(),
+ height = bmp.GetHeight();
+
+ if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
+ !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, x, y) )
+ {
+ // no support for StretchDIBits() or an error occured if we got here
+ wxMemoryDC memDC;
+ memDC.SelectObject(bmp);
+
+ Blit(x, y, width, height, &memDC, 0, 0, wxCOPY, useMask);
+
+ memDC.SelectObject(wxNullBitmap);
+ }
+}
+
+bool wxPrinterDC::DoBlit(wxCoord xdest, wxCoord ydest,
+ wxCoord width, wxCoord height,
+ wxDC *source,
+ wxCoord WXUNUSED(xsrc), wxCoord WXUNUSED(ysrc),
+ int WXUNUSED(rop), bool useMask,
+ wxCoord WXUNUSED(xsrcMask), wxCoord WXUNUSED(ysrcMask))
+{
+ wxBitmap& bmp = source->GetSelectedBitmap();
+ wxMask *mask = useMask ? bmp.GetMask() : NULL;
+ if ( mask )
+ {
+ // If we are printing source colours are screen colours not printer
+ // colours and so we need copy the bitmap pixel by pixel.
+ RECT rect;
+ HDC dcSrc = GetHdcOf(*source);
+ MemoryHDC dcMask(dcSrc);
+ SelectInHDC selectMask(dcMask, (HBITMAP)mask->GetMaskBitmap());
+
+ for (int x = 0; x < width; x++)
+ {
+ for (int y = 0; y < height; y++)
+ {
+ COLORREF cref = ::GetPixel(dcMask, x, y);
+ if (cref)
+ {
+ HBRUSH brush = ::CreateSolidBrush(::GetPixel(dcSrc, x, y));
+ rect.left = xdest + x;
+ rect.right = rect.left + 1;
+ rect.top = ydest + y;
+ rect.bottom = rect.top + 1;
+ ::FillRect(GetHdc(), &rect, brush);
+ ::DeleteObject(brush);
+ }
+ }
+ }
+ }
+ else // no mask
+ {
+ if ( !(::GetDeviceCaps(GetHdc(), RASTERCAPS) & RC_STRETCHDIB) ||
+ !DrawBitmapUsingStretchDIBits(GetHdc(), bmp, xdest, ydest) )
+ {
+ // no support for StretchDIBits
+
+ // as we are printing, source colours are screen colours not
+ // printer colours and so we need copy the bitmap pixel by pixel.
+ HDC dcSrc = GetHdcOf(*source);
+ RECT rect;
+ for (int y = 0; y < height; y++)
+ {
+ // optimization: draw identical adjacent pixels together.
+ for (int x = 0; x < width; x++)
+ {
+ COLORREF col = ::GetPixel(dcSrc, x, y);
+ HBRUSH brush = ::CreateSolidBrush( col );
+
+ rect.left = xdest + x;
+ rect.top = ydest + y;
+ while( (x + 1 < width) &&
+ (::GetPixel(dcSrc, x + 1, y) == col ) )
+ {
+ ++x;
+ }
+ rect.right = xdest + x + 1;
+ rect.bottom = rect.top + 1;
+ ::FillRect((HDC) m_hDC, &rect, brush);
+ ::DeleteObject(brush);
+ }
+ }
+ }
+ }
+
+ return TRUE;
+}
+
+#endif
+ // wxUSE_PRINTING_ARCHITECTURE