From 7d09b97f5321d0accd758eb420c008853ad9ec26 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 25 Dec 2005 02:34:42 +0000 Subject: [PATCH] disallow creation of wxDC objects and made wxDC an ABC; use wxDCTemp instead of wxDC in wx code; fixed WinCE bug with deleting a DC which should be released in wxListBox::MSWOnMeasure() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@36564 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/dc.h | 65 ++++++++++++++++++++++++++++++++++----- include/wx/msw/dcprint.h | 12 +++++--- include/wx/msw/dcscreen.h | 24 +++++++++------ src/msw/bmpbuttn.cpp | 3 +- src/msw/dc.cpp | 25 +++------------ src/msw/dcscreen.cpp | 18 ++++++++--- src/msw/listbox.cpp | 17 +++++----- 7 files changed, 108 insertions(+), 56 deletions(-) diff --git a/include/wx/msw/dc.h b/include/wx/msw/dc.h index 1447eb0cbe..1a2e82afbb 100644 --- a/include/wx/msw/dc.h +++ b/include/wx/msw/dc.h @@ -9,8 +9,8 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifndef _WX_DC_H_ -#define _WX_DC_H_ +#ifndef _WX_MSW_DC_H_ +#define _WX_MSW_DC_H_ #include "wx/defs.h" @@ -40,10 +40,12 @@ public: }; #endif +// this is an ABC: use one of the derived classes to create a DC associated +// with a window, screen, printer and so on class WXDLLEXPORT wxDC : public wxDCBase { public: - wxDC(); + wxDC(WXHDC hDC) { Init(); m_hDC = hDC; } ~wxDC(); // implement base class pure virtuals @@ -138,6 +140,26 @@ public: #endif protected: + void Init() + { + m_canvas = NULL; + m_bOwnsDC = false; + m_hDC = NULL; + + m_oldBitmap = NULL; + m_oldPen = NULL; + m_oldBrush = NULL; + m_oldFont = NULL; + +#if wxUSE_PALETTE + m_oldPalette = NULL; +#endif // wxUSE_PALETTE + } + + // create an uninitialized DC: this should be only used by the derived + // classes + wxDC() { Init(); } + virtual bool DoFloodFill(wxCoord x, wxCoord y, const wxColour& col, int style = wxFLOOD_SURFACE); @@ -186,7 +208,6 @@ protected: virtual void DoGetClippingBox(wxCoord *x, wxCoord *y, wxCoord *w, wxCoord *h) const; - virtual void DoGetSize(int *width, int *height) const; virtual void DoGetSizeMM(int* width, int* height) const; virtual void DoDrawLines(int n, wxPoint points[], @@ -216,6 +237,17 @@ protected: // common part of DoSetClippingRegion() and DoSetClippingRegionAsRegion() void SetClippingHrgn(WXHRGN hrgn); + // implementation of DoGetSize() for wxScreen/PrinterDC: this simply + // returns the size of the entire device this DC is associated with + // + // notice that we intentionally put it in a separate function instead of + // DoGetSize() itself because we want it to remain pure virtual both + // because each derived class should take care to define it as needed (this + // implementation is not at all always appropriate) and because we want + // wxDC to be an ABC to prevent it from being created directly + void GetDeviceSize(int *width, int *height) const; + + // MSW-specific member variables // ----------------------------- @@ -259,12 +291,29 @@ protected: class WXDLLEXPORT wxDCTemp : public wxDC { public: - wxDCTemp(WXHDC hdc) { SetHDC(hdc); } - virtual ~wxDCTemp() { SetHDC((WXHDC)NULL); } + wxDCTemp(WXHDC hdc) : wxDC(hdc) + { + } + + virtual ~wxDCTemp() + { + // prevent base class dtor from freeing it + SetHDC((WXHDC)NULL); + } + + virtual void DoGetSize(int *w, int *h) const + { + wxFAIL_MSG( _T("no way to retrieve the size of generic DC") ); + + if ( w ) + *w = 0; + if ( h ) + *h = 0; + } private: DECLARE_NO_COPY_CLASS(wxDCTemp) }; -#endif - // _WX_DC_H_ +#endif // _WX_MSW_DC_H_ + diff --git a/include/wx/msw/dcprint.h b/include/wx/msw/dcprint.h index 9329b8523d..8847a2d65a 100644 --- a/include/wx/msw/dcprint.h +++ b/include/wx/msw/dcprint.h @@ -9,8 +9,8 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifndef _WX_DCPRINT_H_ -#define _WX_DCPRINT_H_ +#ifndef _WX_MSW_DCPRINT_H_ +#define _WX_MSW_DCPRINT_H_ #if wxUSE_PRINTING_ARCHITECTURE @@ -41,6 +41,11 @@ protected: wxCoord width, wxCoord height, wxDC *source, wxCoord xsrc, wxCoord ysrc, int rop = wxCOPY, bool useMask = false, wxCoord xsrcMask = wxDefaultCoord, wxCoord ysrcMask = wxDefaultCoord); + virtual void DoGetSize(int *w, int *h) const + { + GetDeviceSize(w, h); + } + // init the dc void Init(); @@ -59,6 +64,5 @@ WXHDC WXDLLEXPORT wxGetPrinterDC(const wxPrintData& data); #endif // wxUSE_PRINTING_ARCHITECTURE -#endif - // _WX_DCPRINT_H_ +#endif // _WX_MSW_DCPRINT_H_ diff --git a/include/wx/msw/dcscreen.h b/include/wx/msw/dcscreen.h index f337f90b4f..85da593a3b 100644 --- a/include/wx/msw/dcscreen.h +++ b/include/wx/msw/dcscreen.h @@ -9,29 +9,33 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifndef _WX_DCSCREEN_H_ -#define _WX_DCSCREEN_H_ +#ifndef _WX_MSW_DCSCREEN_H_ +#define _WX_MSW_DCSCREEN_H_ -#include "wx/dcclient.h" +#include "wx/dc.h" -class WXDLLEXPORT wxScreenDC : public wxWindowDC +class WXDLLEXPORT wxScreenDC : public wxDC { public: // Create a DC representing the whole screen wxScreenDC(); + // these functions are obsolete and shouldn't be used + // Compatibility with X's requirements for drawing on top of all windows - static bool StartDrawingOnTop(wxWindow* WXUNUSED(window)) { return true; } - static bool StartDrawingOnTop(wxRect* WXUNUSED(rect) = NULL) { return true; } - static bool EndDrawingOnTop() { return true; } + wxDEPRECATED( static bool StartDrawingOnTop(wxWindow* window) ); + wxDEPRECATED( static bool StartDrawingOnTop(wxRect* rect = NULL) ); + wxDEPRECATED( static bool EndDrawingOnTop() ); protected: - virtual void DoGetSize(int *width, int *height) const; + virtual void DoGetSize(int *w, int *h) const + { + GetDeviceSize(w, h); + } private: DECLARE_DYNAMIC_CLASS_NO_COPY(wxScreenDC) }; -#endif - // _WX_DCSCREEN_H_ +#endif // _WX_MSW_DCSCREEN_H_ diff --git a/src/msw/bmpbuttn.cpp b/src/msw/bmpbuttn.cpp index 3aef7085d1..3b67925eaa 100644 --- a/src/msw/bmpbuttn.cpp +++ b/src/msw/bmpbuttn.cpp @@ -304,8 +304,7 @@ bool wxBitmapButton::MSWOnDraw(WXDRAWITEMSTRUCT *item) } // draw the bitmap - wxDC dst; - dst.SetHDC((WXHDC) hDC, false); + wxDCTemp dst((WXHDC)hDC); dst.DrawBitmap(*bitmap, x1, y1, true); // draw focus / disabled state, if auto-drawing diff --git a/src/msw/dc.cpp b/src/msw/dc.cpp index cf75d3e84e..20bf623044 100644 --- a/src/msw/dc.cpp +++ b/src/msw/dc.cpp @@ -258,23 +258,6 @@ wxColourChanger::~wxColourChanger() // wxDC // --------------------------------------------------------------------------- -// Default constructor -wxDC::wxDC() -{ - m_canvas = NULL; - - m_oldBitmap = 0; - m_oldPen = 0; - m_oldBrush = 0; - m_oldFont = 0; -#if wxUSE_PALETTE - m_oldPalette = 0; -#endif // wxUSE_PALETTE - - m_bOwnsDC = false; - m_hDC = 0; -} - wxDC::~wxDC() { if ( m_hDC != 0 ) @@ -2285,12 +2268,14 @@ bool wxDC::DoBlit(wxCoord xdest, wxCoord ydest, return success; } -void wxDC::DoGetSize(int *w, int *h) const +void wxDC::GetDeviceSize(int *width, int *height) const { WXMICROWIN_CHECK_HDC - if ( w ) *w = ::GetDeviceCaps(GetHdc(), HORZRES); - if ( h ) *h = ::GetDeviceCaps(GetHdc(), VERTRES); + if ( width ) + *width = ::GetDeviceCaps(GetHdc(), HORZRES); + if ( height ) + *height = ::GetDeviceCaps(GetHdc(), VERTRES); } void wxDC::DoGetSizeMM(int *w, int *h) const diff --git a/src/msw/dcscreen.cpp b/src/msw/dcscreen.cpp index e638335d78..eb843c9bff 100644 --- a/src/msw/dcscreen.cpp +++ b/src/msw/dcscreen.cpp @@ -25,7 +25,7 @@ #include "wx/dcscreen.h" -IMPLEMENT_DYNAMIC_CLASS(wxScreenDC, wxWindowDC) +IMPLEMENT_DYNAMIC_CLASS(wxScreenDC, wxDC) // Create a DC representing the whole screen wxScreenDC::wxScreenDC() @@ -37,10 +37,18 @@ wxScreenDC::wxScreenDC() ::SetBkMode( GetHdc(), TRANSPARENT ); } -void wxScreenDC::DoGetSize(int *width, int *height) const +// deprecated functions +bool wxScreenDC::StartDrawingOnTop(wxWindow* WXUNUSED(window)) { - // skip wxWindowDC version because it doesn't work without a valid m_canvas - // (which we don't have) - wxDC::DoGetSize(width, height); + return true; } +bool wxScreenDC::StartDrawingOnTop(wxRect* WXUNUSED(rect)) +{ + return true; +} + +bool wxScreenDC::EndDrawingOnTop() +{ + return true; +} diff --git a/src/msw/listbox.cpp b/src/msw/listbox.cpp index 71cc9a44a0..1f8a77955d 100644 --- a/src/msw/listbox.cpp +++ b/src/msw/listbox.cpp @@ -776,16 +776,19 @@ bool wxListBox::MSWOnMeasure(WXMEASUREITEMSTRUCT *item) HDC hdc = CreateIC(wxT("DISPLAY"), NULL, NULL, 0); #endif - wxDC dc; - dc.SetHDC((WXHDC)hdc); - dc.SetFont(GetFont()); - - pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; - pStruct->itemWidth = dc.GetCharWidth(); + { + wxDCTemp dc((WXHDC)hdc); + dc.SetFont(GetFont()); - dc.SetHDC(0); + pStruct->itemHeight = dc.GetCharHeight() + 2*OWNER_DRAWN_LISTBOX_EXTRA_SPACE; + pStruct->itemWidth = dc.GetCharWidth(); + } +#ifdef __WXWINCE__ + ReleaseDC(NULL, hdc); +#else DeleteDC(hdc); +#endif return true; } -- 2.45.2