X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/d3211838bf578eb8ada5a608b66626a6bde6846a..2fe212b0336512aac9eace69fab09ce856b0bf4b:/src/msw/dc.cpp diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index edd4fe4fa5..dcf230b0b6 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -40,7 +40,9 @@ #include "wx/icon.h" #endif +#include "wx/sysopt.h" #include "wx/dcprint.h" +#include "wx/module.h" #include #include @@ -176,7 +178,9 @@ wxDC::wxDC() m_oldPen = 0; m_oldBrush = 0; m_oldFont = 0; +#if wxUSE_PALETTE m_oldPalette = 0; +#endif // wxUSE_PALETTE m_bOwnsDC = FALSE; m_hDC = 0; @@ -244,16 +248,21 @@ void wxDC::SelectOldObjects(WXHDC dc) ::SelectObject((HDC) dc, (HFONT) m_oldFont); } m_oldFont = 0; + +#if wxUSE_PALETTE if (m_oldPalette) { - ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, TRUE); + ::SelectPalette((HDC) dc, (HPALETTE) m_oldPalette, FALSE); } m_oldPalette = 0; +#endif // wxUSE_PALETTE } m_brush = wxNullBrush; m_pen = wxNullPen; +#if wxUSE_PALETTE m_palette = wxNullPalette; +#endif // wxUSE_PALETTE m_font = wxNullFont; m_backgroundBrush = wxNullBrush; m_selectedBitmap = wxNullBitmap; @@ -263,24 +272,37 @@ void wxDC::SelectOldObjects(WXHDC dc) // clipping // --------------------------------------------------------------------------- -#define DO_SET_CLIPPING_BOX() \ -{ \ - RECT rect; \ - \ - GetClipBox(GetHdc(), &rect); \ - \ - m_clipX1 = (wxCoord) XDEV2LOG(rect.left); \ - m_clipY1 = (wxCoord) YDEV2LOG(rect.top); \ - m_clipX2 = (wxCoord) XDEV2LOG(rect.right); \ - m_clipY2 = (wxCoord) YDEV2LOG(rect.bottom); \ +void wxDC::UpdateClipBox() +{ +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + + RECT rect; + GetClipBox(GetHdc(), &rect); + + m_clipX1 = (wxCoord) XDEV2LOG(rect.left); + m_clipY1 = (wxCoord) YDEV2LOG(rect.top); + m_clipX2 = (wxCoord) XDEV2LOG(rect.right); + m_clipY2 = (wxCoord) YDEV2LOG(rect.bottom); } -void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch) +void wxDC::DoSetClippingRegion(wxCoord x, wxCoord y, wxCoord w, wxCoord h) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_clipping = TRUE; - HRGN hrgn = ::CreateRectRgn(XLOG2DEV(cx), YLOG2DEV(cy), - XLOG2DEV(cx + cw), YLOG2DEV(cy + ch)); + // the region coords are always the device ones, so do the translation + // manually + // + // FIXME: possible +/-1 error here, to check! + HRGN hrgn = ::CreateRectRgn(LogicalToDeviceX(x), + LogicalToDeviceY(y), + LogicalToDeviceX(x + w), + LogicalToDeviceY(y + h)); if ( !hrgn ) { wxLogLastError(_T("CreateRectRgn")); @@ -291,28 +313,37 @@ void wxDC::DoSetClippingRegion(wxCoord cx, wxCoord cy, wxCoord cw, wxCoord ch) { wxLogLastError(_T("SelectClipRgn")); } + DeleteObject(hrgn); - DO_SET_CLIPPING_BOX() + UpdateClipBox(); } } void wxDC::DoSetClippingRegionAsRegion(const wxRegion& region) { - wxCHECK_RET( region.GetHRGN(), wxT("invalid clipping region") ); +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + + wxCHECK_RET( GetHrgnOf(region), wxT("invalid clipping region") ); m_clipping = TRUE; #ifdef __WIN16__ - SelectClipRgn(GetHdc(), (HRGN) region.GetHRGN()); -#else - ExtSelectClipRgn(GetHdc(), (HRGN) region.GetHRGN(), RGN_AND); -#endif + SelectClipRgn(GetHdc(), GetHrgnOf(region)); +#else // Win32 + ExtSelectClipRgn(GetHdc(), GetHrgnOf(region), RGN_AND); +#endif // Win16/32 - DO_SET_CLIPPING_BOX() + UpdateClipBox(); } void wxDC::DestroyClippingRegion() { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + if (m_clipping && m_hDC) { // TODO: this should restore the previous clipping region, @@ -322,6 +353,7 @@ void wxDC::DestroyClippingRegion() SelectClipRgn(GetHdc(), rgn); DeleteObject(rgn); } + m_clipping = FALSE; } @@ -336,14 +368,23 @@ bool wxDC::CanDrawBitmap() const bool wxDC::CanGetTextExtent() const { +#ifdef __WXMICROWIN__ + // TODO Extend MicroWindows' GetDeviceCaps function + return TRUE; +#else // What sort of display is it? int technology = ::GetDeviceCaps(GetHdc(), TECHNOLOGY); return (technology == DT_RASDISPLAY) || (technology == DT_RASPRINTER); +#endif } int wxDC::GetDepth() const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return 16; +#endif + return (int)::GetDeviceCaps(GetHdc(), BITSPIXEL); } @@ -353,6 +394,10 @@ int wxDC::GetDepth() const void wxDC::Clear() { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + RECT rect; if ( m_canvas ) { @@ -387,6 +432,10 @@ void wxDC::Clear() void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + if ( !::ExtFloodFill(GetHdc(), XLOG2DEV(x), YLOG2DEV(y), col.GetPixel(), style == wxFLOOD_SURFACE ? FLOODFILLSURFACE @@ -412,6 +461,10 @@ void wxDC::DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style) bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return FALSE; +#endif + wxCHECK_MSG( col, FALSE, _T("NULL colour parameter in wxDC::GetPixel") ); // get the color of the pixel @@ -424,6 +477,10 @@ bool wxDC::DoGetPixel(wxCoord x, wxCoord y, wxColour *col) const void wxDC::DoCrossHair(wxCoord x, wxCoord y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxCoord x1 = x-VIEWPORT_EXTENT; wxCoord y1 = y-VIEWPORT_EXTENT; wxCoord x2 = x+VIEWPORT_EXTENT; @@ -441,6 +498,10 @@ void wxDC::DoCrossHair(wxCoord x, wxCoord y) void wxDC::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + (void)MoveToEx(GetHdc(), XLOG2DEV(x1), YLOG2DEV(y1), NULL); (void)LineTo(GetHdc(), XLOG2DEV(x2), YLOG2DEV(y2)); @@ -458,6 +519,10 @@ void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, wxCoord xc, wxCoord yc) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling double dx = xc - x1; @@ -506,10 +571,14 @@ void wxDC::DoDrawArc(wxCoord x1, wxCoord y1, void wxDC::DoDrawCheckMark(wxCoord x1, wxCoord y1, wxCoord width, wxCoord height) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxCoord x2 = x1 + width, y2 = y1 + height; -#if defined(__WIN32__) && !defined(__SC__) +#if defined(__WIN32__) && !defined(__SC__) && !defined(__WXMICROWIN__) RECT rect; rect.left = x1; rect.top = y1; @@ -525,9 +594,9 @@ void wxDC::DoDrawCheckMark(wxCoord x1, wxCoord y1, HPEN hBrushOld = (HPEN)::SelectObject(GetHdc(), whiteBrush); ::SetROP2(GetHdc(), R2_COPYPEN); Rectangle(GetHdc(), x1, y1, x2, y2); - MoveTo(GetHdc(), x1, y1); + MoveToEx(GetHdc(), x1, y1, NULL); LineTo(GetHdc(), x2, y2); - MoveTo(GetHdc(), x2, y1); + MoveToEx(GetHdc(), x2, y1, NULL); LineTo(GetHdc(), x1, y2); ::SelectObject(GetHdc(), hPenOld); ::SelectObject(GetHdc(), hBrushOld); @@ -540,6 +609,10 @@ void wxDC::DoDrawCheckMark(wxCoord x1, wxCoord y1, void wxDC::DoDrawPoint(wxCoord x, wxCoord y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + COLORREF color = 0x00ffffff; if (m_pen.Ok()) { @@ -553,6 +626,10 @@ void wxDC::DoDrawPoint(wxCoord x, wxCoord y) void wxDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset,int fillStyle) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling // Do things less efficiently if we have offsets @@ -586,6 +663,10 @@ void wxDC::DoDrawPolygon(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffs void wxDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + // Do things less efficiently if we have offsets if (xoffset != 0 || yoffset != 0) { @@ -613,6 +694,10 @@ void wxDC::DoDrawLines(int n, wxPoint points[], wxCoord xoffset, wxCoord yoffset void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling wxCoord x2 = x + width; @@ -650,6 +735,10 @@ void wxDC::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height, double radius) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling // Now, a negative radius value is interpreted to mean @@ -686,6 +775,10 @@ void wxDC::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord h void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling wxCoord x2 = (x+width); @@ -700,6 +793,10 @@ void wxDC::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord height) // Chris Breeze 20/5/98: first implementation of DrawEllipticArc on Windows void wxDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,double ea) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxColourChanger cc(*this); // needed for wxSTIPPLE_MASK_OPAQUE handling wxCoord x2 = x + w; @@ -743,6 +840,10 @@ void wxDC::DoDrawEllipticArc(wxCoord x,wxCoord y,wxCoord w,wxCoord h,double sa,d void wxDC::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxCHECK_RET( icon.Ok(), wxT("invalid icon in DrawIcon") ); #ifdef __WIN32__ @@ -757,12 +858,17 @@ void wxDC::DoDrawIcon(const wxIcon& icon, wxCoord x, wxCoord y) void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask ) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxCHECK_RET( bmp.Ok(), _T("invalid bitmap in wxDC::DrawBitmap") ); int width = bmp.GetWidth(), height = bmp.GetHeight(); HBITMAP hbmpMask = 0; + HPALETTE oldPal = 0; if ( useMask ) { @@ -787,15 +893,23 @@ void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask // than the wxWindows fall-back implementation. So we need // to be able to switch this on and off at runtime. bool ok = FALSE; - if (wxSystemSettings::GetOptionInt(wxT("no-maskblt")) == 0) +#if wxUSE_SYSTEM_OPTIONS + if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0) +#endif { + HDC cdc = GetHdc(); HDC hdcMem = ::CreateCompatibleDC(GetHdc()); ::SelectObject(hdcMem, GetHbitmapOf(bmp)); - - ok = ::MaskBlt(GetHdc(), x, y, width, height, + if (bmp.GetPalette() && (::GetDeviceCaps(cdc,BITSPIXEL) <= 8)) { + oldPal = ::SelectPalette( hdcMem, (HPALETTE)bmp.GetPalette()->GetHPALETTE(), FALSE); + ::RealizePalette(hdcMem); + } + ok = ::MaskBlt(cdc, x, y, width, height, hdcMem, 0, 0, hbmpMask, 0, 0, MAKEROP4(SRCCOPY, DSTCOPY)) != 0; + if (oldPal) + ::SelectPalette(hdcMem, oldPal, FALSE); ::DeleteDC(hdcMem); } @@ -831,8 +945,14 @@ void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask ::SetBkColor(GetHdc(), m_textBackgroundColour.GetPixel() ); } + if (bmp.GetPalette() && (::GetDeviceCaps(cdc,BITSPIXEL) <= 8)) { + oldPal = ::SelectPalette( memdc, (HPALETTE)bmp.GetPalette()->GetHPALETTE(), FALSE); + ::RealizePalette(memdc); + } ::SelectObject( memdc, hbitmap ); ::BitBlt( cdc, x, y, width, height, memdc, 0, 0, SRCCOPY); + if (oldPal) + ::SelectPalette(memdc, oldPal, FALSE); ::DeleteDC( memdc ); ::SetTextColor(GetHdc(), old_textground); @@ -842,6 +962,10 @@ void wxDC::DoDrawBitmap( const wxBitmap &bmp, wxCoord x, wxCoord y, bool useMask void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + DrawAnyText(text, x, y); // update the bounding box @@ -854,6 +978,10 @@ void wxDC::DoDrawText(const wxString& text, wxCoord x, wxCoord y) void wxDC::DrawAnyText(const wxString& text, wxCoord x, wxCoord y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + // prepare for drawing the text if ( m_textForegroundColour.Ok() ) SetTextColor(GetHdc(), m_textForegroundColour.GetPixel()); @@ -886,6 +1014,10 @@ void wxDC::DoDrawRotatedText(const wxString& text, wxCoord x, wxCoord y, double angle) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + // we test that we have some font because otherwise we should still use the // "else" part below to avoid that DrawRotatedText(angle = 180) and // DrawRotatedText(angle = 0) use different fonts (we can't use the default @@ -894,11 +1026,13 @@ void wxDC::DoDrawRotatedText(const wxString& text, { DoDrawText(text, x, y); } +#ifndef __WXMICROWIN__ else { - // NB: don't take DEFAULT_GUI_FONT because it's not TrueType and so - // can't have non zero orientation/escapement - wxFont font = m_font.Ok() ? m_font : *wxNORMAL_FONT; + // NB: don't take DEFAULT_GUI_FONT (a.k.a. wxSYS_DEFAULT_GUI_FONT) + // because it's not TrueType and so can't have non zero + // orientation/escapement under Win9x + wxFont font = m_font.Ok() ? m_font : *wxSWISS_FONT; HFONT hfont = (HFONT)font.GetResourceHandle(); LOGFONT lf; if ( ::GetObject(hfont, sizeof(lf), &lf) == 0 ) @@ -944,19 +1078,26 @@ void wxDC::DoDrawRotatedText(const wxString& text, CalcBoundingBox(x, y); CalcBoundingBox(x + h*sin(rad), y + h*cos(rad)); } +#endif } // --------------------------------------------------------------------------- // set GDI objects // --------------------------------------------------------------------------- +#if wxUSE_PALETTE + void wxDC::SetPalette(const wxPalette& palette) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + // Set the old object temporarily, in case the assignment deletes an object // that's not yet selected out. if (m_oldPalette) { - ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, TRUE); + ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, FALSE); m_oldPalette = 0; } @@ -968,7 +1109,7 @@ void wxDC::SetPalette(const wxPalette& palette) // the original colourmap if (m_oldPalette) { - ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, TRUE); + ::SelectPalette(GetHdc(), (HPALETTE) m_oldPalette, FALSE); m_oldPalette = 0; } @@ -977,7 +1118,7 @@ void wxDC::SetPalette(const wxPalette& palette) if (m_palette.Ok() && m_palette.GetHPALETTE()) { - HPALETTE oldPal = ::SelectPalette(GetHdc(), (HPALETTE) m_palette.GetHPALETTE(), TRUE); + HPALETTE oldPal = ::SelectPalette(GetHdc(), (HPALETTE) m_palette.GetHPALETTE(), FALSE); if (!m_oldPalette) m_oldPalette = (WXHPALETTE) oldPal; @@ -985,8 +1126,14 @@ void wxDC::SetPalette(const wxPalette& palette) } } +#endif // wxUSE_PALETTE + void wxDC::SetFont(const wxFont& the_font) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + // Set the old object temporarily, in case the assignment deletes an object // that's not yet selected out. if (m_oldFont) @@ -1018,6 +1165,10 @@ void wxDC::SetFont(const wxFont& the_font) void wxDC::SetPen(const wxPen& pen) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + // Set the old object temporarily, in case the assignment deletes an object // that's not yet selected out. if (m_oldPen) @@ -1048,6 +1199,10 @@ void wxDC::SetPen(const wxPen& pen) void wxDC::SetBrush(const wxBrush& brush) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + // Set the old object temporarily, in case the assignment deletes an object // that's not yet selected out. if (m_oldBrush) @@ -1095,6 +1250,10 @@ void wxDC::SetBrush(const wxBrush& brush) void wxDC::SetBackground(const wxBrush& brush) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_backgroundBrush = brush; if (!m_backgroundBrush.Ok()) @@ -1136,6 +1295,10 @@ void wxDC::SetBackground(const wxBrush& brush) void wxDC::SetBackgroundMode(int mode) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_backgroundMode = mode; // SetBackgroundColour now only refers to text background @@ -1152,6 +1315,10 @@ void wxDC::SetBackgroundMode(int mode) void wxDC::SetLogicalFunction(int function) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_logicalFunction = function; SetRop(m_hDC); @@ -1215,6 +1382,10 @@ void wxDC::EndPage() wxCoord wxDC::GetCharHeight() const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return 0; +#endif + TEXTMETRIC lpTextMetric; GetTextMetrics(GetHdc(), &lpTextMetric); @@ -1224,6 +1395,10 @@ wxCoord wxDC::GetCharHeight() const wxCoord wxDC::GetCharWidth() const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return 0; +#endif + TEXTMETRIC lpTextMetric; GetTextMetrics(GetHdc(), &lpTextMetric); @@ -1235,6 +1410,17 @@ void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y, wxCoord *descent, wxCoord *externalLeading, wxFont *font) const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) + { + if (x) *x = 0; + if (y) *y = 0; + if (descent) *descent = 0; + if (externalLeading) *externalLeading = 0; + return; + } +#endif + HFONT hfontOld; if ( font ) { @@ -1266,6 +1452,10 @@ void wxDC::DoGetTextExtent(const wxString& string, wxCoord *x, wxCoord *y, void wxDC::SetMapMode(int mode) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_mappingMode = mode; int pixel_width = 0; @@ -1334,6 +1524,10 @@ void wxDC::SetMapMode(int mode) void wxDC::SetUserScale(double x, double y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_userScaleX = x; m_userScaleY = y; @@ -1342,6 +1536,10 @@ void wxDC::SetUserScale(double x, double y) void wxDC::SetAxisOrientation(bool xLeftRight, bool yBottomUp) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_signX = xLeftRight ? 1 : -1; m_signY = yBottomUp ? -1 : 1; @@ -1350,6 +1548,10 @@ void wxDC::SetAxisOrientation(bool xLeftRight, bool yBottomUp) void wxDC::SetSystemScale(double x, double y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_scaleX = x; m_scaleY = y; @@ -1358,6 +1560,10 @@ void wxDC::SetSystemScale(double x, double y) void wxDC::SetLogicalOrigin(wxCoord x, wxCoord y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_logicalOriginX = x; m_logicalOriginY = y; @@ -1366,6 +1572,10 @@ void wxDC::SetLogicalOrigin(wxCoord x, wxCoord y) void wxDC::SetDeviceOrigin(wxCoord x, wxCoord y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_deviceOriginX = x; m_deviceOriginY = y; @@ -1427,8 +1637,13 @@ wxCoord wxDCBase::LogicalToDeviceYRel(wxCoord y) const bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, wxCoord width, wxCoord height, wxDC *source, wxCoord xsrc, wxCoord ysrc, - int rop, bool useMask) + int rop, bool useMask, + wxCoord xsrcMask, wxCoord ysrcMask) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return FALSE; +#endif + wxMask *mask = NULL; if ( useMask ) { @@ -1443,6 +1658,11 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, } } + if (xsrcMask == -1 && ysrcMask == -1) + { + xsrcMask = xsrc; ysrcMask = ysrc; + } + COLORREF old_textground = ::GetTextColor(GetHdc()); COLORREF old_background = ::GetBkColor(GetHdc()); if (m_textForegroundColour.Ok()) @@ -1491,11 +1711,13 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, // On some systems, MaskBlt succeeds yet is much much slower // than the wxWindows fall-back implementation. So we need // to be able to switch this on and off at runtime. - if (wxSystemSettings::GetOptionInt(wxT("no-maskblt")) == 0) +#if wxUSE_SYSTEM_OPTIONS + if (wxSystemOptions::GetOptionInt(wxT("no-maskblt")) == 0) +#endif { success = ::MaskBlt(GetHdc(), xdest, ydest, width, height, GetHdcOf(*source), xsrc, ysrc, - (HBITMAP)mask->GetMaskBitmap(), xsrc, ysrc, + (HBITMAP)mask->GetMaskBitmap(), xsrcMask, ysrcMask, MAKEROP4(dwRop, DSTCOPY)) != 0; } @@ -1503,11 +1725,28 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, #endif // Win32 { // Blit bitmap with mask + HDC dc_mask ; + HDC dc_buffer ; + HBITMAP buffer_bmap ; +#if wxUSE_DC_CACHEING // create a temp buffer bitmap and DCs to access it and the mask - HDC dc_mask = ::CreateCompatibleDC(GetHdcOf(*source)); - HDC dc_buffer = ::CreateCompatibleDC(GetHdc()); - HBITMAP buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), width, height); + wxDCCacheEntry* dcCacheEntry1 = FindDCInCache(NULL, source->GetHDC()); + dc_mask = (HDC) dcCacheEntry1->m_dc; + + wxDCCacheEntry* dcCacheEntry2 = FindDCInCache(dcCacheEntry1, GetHDC()); + dc_buffer = (HDC) dcCacheEntry2->m_dc; + + wxDCCacheEntry* bitmapCacheEntry = FindBitmapInCache(GetHDC(), + width, height); + + buffer_bmap = (HBITMAP) bitmapCacheEntry->m_bitmap; +#else // !wxUSE_DC_CACHEING + // create a temp buffer bitmap and DCs to access it and the mask + dc_mask = ::CreateCompatibleDC(GetHdcOf(*source)); + dc_buffer = ::CreateCompatibleDC(GetHdc()); + buffer_bmap = ::CreateCompatibleBitmap(GetHdc(), width, height); +#endif // wxUSE_DC_CACHEING/!wxUSE_DC_CACHEING ::SelectObject(dc_mask, (HBITMAP) mask->GetMaskBitmap()); ::SelectObject(dc_buffer, buffer_bmap); @@ -1529,7 +1768,7 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, COLORREF prevBkCol = ::SetBkColor(GetHdc(), RGB(255, 255, 255)); COLORREF prevCol = ::SetTextColor(GetHdc(), RGB(0, 0, 0)); if ( !::BitBlt(dc_buffer, 0, 0, (int)width, (int)height, - dc_mask, xsrc, ysrc, SRCAND) ) + dc_mask, xsrcMask, ysrcMask, SRCAND) ) { wxLogLastError(wxT("BitBlt")); } @@ -1538,7 +1777,7 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, ::SetBkColor(GetHdc(), RGB(0, 0, 0)); ::SetTextColor(GetHdc(), RGB(255, 255, 255)); if ( !::BitBlt(GetHdc(), xdest, ydest, (int)width, (int)height, - dc_mask, xsrc, ysrc, SRCAND) ) + dc_mask, xsrcMask, ysrcMask, SRCAND) ) { wxLogLastError(wxT("BitBlt")); } @@ -1556,10 +1795,15 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, // tidy up temporary DCs and bitmap ::SelectObject(dc_mask, 0); - ::DeleteDC(dc_mask); ::SelectObject(dc_buffer, 0); - ::DeleteDC(dc_buffer); - ::DeleteObject(buffer_bmap); + +#if !wxUSE_DC_CACHEING + { + ::DeleteDC(dc_mask); + ::DeleteDC(dc_buffer); + ::DeleteObject(buffer_bmap); + } +#endif } } else // no mask, just BitBlt() it @@ -1580,18 +1824,30 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, void wxDC::DoGetSize(int *w, int *h) const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZRES); if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTRES); } void wxDC::DoGetSizeMM(int *w, int *h) const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZSIZE); if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTSIZE); } wxSize wxDC::GetPPI() const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return wxSize(); +#endif + int x = ::GetDeviceCaps(GetHdc(), LOGPIXELSX); int y = ::GetDeviceCaps(GetHdc(), LOGPIXELSY); @@ -1601,6 +1857,10 @@ wxSize wxDC::GetPPI() const // For use by wxWindows only, unless custom units are required. void wxDC::SetLogicalScale(double x, double y) { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + m_logicalScaleX = x; m_logicalScaleY = y; } @@ -1610,6 +1870,10 @@ void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y, float *descent, float *externalLeading, wxFont *theFont, bool use16bit) const { +#ifdef __WXMICROWIN__ + if (!GetHDC()) return; +#endif + wxCoord x1, y1, descent1, externalLeading1; GetTextExtent(string, & x1, & y1, & descent1, & externalLeading1, theFont, use16bit); *x = x1; *y = y1; @@ -1620,4 +1884,141 @@ void wxDC::DoGetTextExtent(const wxString& string, float *x, float *y, } #endif +#if wxUSE_DC_CACHEING + +/* + * This implementation is a bit ugly and uses the old-fashioned wxList class, so I will + * improve it in due course, either using arrays, or simply storing pointers to one + * entry for the bitmap, and two for the DCs. -- JACS + */ + +wxList wxDC::sm_bitmapCache; +wxList wxDC::sm_dcCache; + +wxDCCacheEntry::wxDCCacheEntry(WXHBITMAP hBitmap, int w, int h, int depth) +{ + m_bitmap = hBitmap; + m_dc = 0; + m_width = w; + m_height = h; + m_depth = depth; +} + +wxDCCacheEntry::wxDCCacheEntry(WXHDC hDC, int depth) +{ + m_bitmap = 0; + m_dc = hDC; + m_width = 0; + m_height = 0; + m_depth = depth; +} + +wxDCCacheEntry::~wxDCCacheEntry() +{ + if (m_bitmap) + ::DeleteObject((HBITMAP) m_bitmap); + if (m_dc) + ::DeleteDC((HDC) m_dc); +} + +wxDCCacheEntry* wxDC::FindBitmapInCache(WXHDC dc, int w, int h) +{ + int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL); + wxNode* node = sm_bitmapCache.First(); + while (node) + { + wxDCCacheEntry* entry = (wxDCCacheEntry*) node->Data(); + + if (entry->m_depth == depth) + { + if (entry->m_width < w || entry->m_height < h) + { + ::DeleteObject((HBITMAP) entry->m_bitmap); + entry->m_bitmap = (WXHBITMAP) ::CreateCompatibleBitmap((HDC) dc, w, h); + if ( !entry->m_bitmap) + { + wxLogLastError(wxT("CreateCompatibleBitmap")); + } + entry->m_width = w; entry->m_height = h; + return entry; + } + return entry; + } + + node = node->Next(); + } + WXHBITMAP hBitmap = (WXHBITMAP) ::CreateCompatibleBitmap((HDC) dc, w, h); + if ( !hBitmap) + { + wxLogLastError(wxT("CreateCompatibleBitmap")); + } + wxDCCacheEntry* entry = new wxDCCacheEntry(hBitmap, w, h, depth); + AddToBitmapCache(entry); + return entry; +} + +wxDCCacheEntry* wxDC::FindDCInCache(wxDCCacheEntry* notThis, WXHDC dc) +{ + int depth = ::GetDeviceCaps((HDC) dc, PLANES) * ::GetDeviceCaps((HDC) dc, BITSPIXEL); + wxNode* node = sm_dcCache.First(); + while (node) + { + wxDCCacheEntry* entry = (wxDCCacheEntry*) node->Data(); + + // Don't return the same one as we already have + if (!notThis || (notThis != entry)) + { + if (entry->m_depth == depth) + { + return entry; + } + } + + node = node->Next(); + } + WXHDC hDC = (WXHDC) ::CreateCompatibleDC((HDC) dc); + if ( !hDC) + { + wxLogLastError(wxT("CreateCompatibleDC")); + } + wxDCCacheEntry* entry = new wxDCCacheEntry(hDC, depth); + AddToDCCache(entry); + return entry; +} + +void wxDC::AddToBitmapCache(wxDCCacheEntry* entry) +{ + sm_bitmapCache.Append(entry); +} + +void wxDC::AddToDCCache(wxDCCacheEntry* entry) +{ + sm_dcCache.Append(entry); +} + +void wxDC::ClearCache() +{ + sm_bitmapCache.DeleteContents(TRUE); + sm_bitmapCache.Clear(); + sm_bitmapCache.DeleteContents(FALSE); + sm_dcCache.DeleteContents(TRUE); + sm_dcCache.Clear(); + sm_dcCache.DeleteContents(FALSE); +} + +// Clean up cache at app exit +class wxDCModule : public wxModule +{ +public: + virtual bool OnInit() { return TRUE; } + virtual void OnExit() { wxDC::ClearCache(); } + +private: + DECLARE_DYNAMIC_CLASS(wxDCModule) +}; + +IMPLEMENT_DYNAMIC_CLASS(wxDCModule, wxModule) + +#endif + // wxUSE_DC_CACHEING