+// a class which cleans up any GDI object
+class AutoGDIObject
+{
+protected:
+ AutoGDIObject() { m_gdiobj = NULL; }
+ AutoGDIObject(HGDIOBJ gdiobj) : m_gdiobj(gdiobj) { }
+ ~AutoGDIObject() { if ( m_gdiobj ) ::DeleteObject(m_gdiobj); }
+
+ void InitGdiobj(HGDIOBJ gdiobj)
+ {
+ wxASSERT_MSG( !m_gdiobj, _T("initializing twice?") );
+
+ m_gdiobj = gdiobj;
+ }
+
+ HGDIOBJ GetObject() const { return m_gdiobj; }
+
+private:
+ HGDIOBJ m_gdiobj;
+};
+
+// TODO: all this asks for using a AutoHandler<T, CreateFunc> template...
+
+// a class for temporary brushes
+class AutoHBRUSH : private AutoGDIObject
+{
+public:
+ AutoHBRUSH(COLORREF col)
+ : AutoGDIObject(::CreateSolidBrush(col)) { }
+
+ operator HBRUSH() const { return (HBRUSH)GetObject(); }
+};
+
+// a class for temporary fonts
+class AutoHFONT : private AutoGDIObject
+{
+private:
+public:
+ AutoHFONT()
+ : AutoGDIObject() { }
+
+ AutoHFONT(const LOGFONT& lf)
+ : AutoGDIObject(::CreateFontIndirect(&lf)) { }
+
+ void Init(const LOGFONT& lf) { InitGdiobj(::CreateFontIndirect(&lf)); }
+
+ operator HFONT() const { return (HFONT)GetObject(); }
+};
+
+// a class for temporary pens
+class AutoHPEN : private AutoGDIObject
+{
+public:
+ AutoHPEN(COLORREF col)
+ : AutoGDIObject(::CreatePen(PS_SOLID, 0, col)) { }
+
+ operator HPEN() const { return (HPEN)GetObject(); }
+};
+
+// classes for temporary bitmaps
+class AutoHBITMAP : private AutoGDIObject
+{
+public:
+ AutoHBITMAP(HBITMAP hbmp) : AutoGDIObject(hbmp) { }
+
+ operator HBITMAP() const { return (HBITMAP)GetObject(); }
+};
+
+class CompatibleBitmap : public AutoHBITMAP
+{
+public:
+ CompatibleBitmap(HDC hdc, int w, int h)
+ : AutoHBITMAP(::CreateCompatibleBitmap(hdc, w, h))
+ {
+ }
+};
+
+class MonoBitmap : public AutoHBITMAP
+{
+public:
+ MonoBitmap(int w, int h)
+ : AutoHBITMAP(::CreateBitmap(w, h, 1, 1, 0))
+ {
+ }
+};
+
+// class automatically destroys the region object
+class AutoHRGN : private AutoGDIObject
+{
+public:
+ AutoHRGN(HRGN hrgn) : AutoGDIObject(hrgn) { }
+
+ operator HRGN() const { return (HRGN)GetObject(); }
+};
+
+// class sets the specified clipping region during its life time
+class HDCClipper
+{
+public:
+ HDCClipper(HDC hdc, HRGN hrgn)
+ : m_hdc(hdc)
+ {
+ if ( !::SelectClipRgn(hdc, hrgn) )
+ wxLogLastError(_T("SelectClipRgn"));
+ }
+
+ ~HDCClipper()
+ {
+ ::SelectClipRgn(m_hdc, NULL);
+ }
+
+private:
+ HDC m_hdc;
+
+ DECLARE_NO_COPY_CLASS(HDCClipper)
+};
+
+// set the given map mode for the life time of this object
+//
+// NB: SetMapMode() is not supported by CE so we also define a helper macro
+// to avoid using it there
+#ifdef __WXWINCE__
+ #define wxCHANGE_HDC_MAP_MODE(hdc, mm)
+#else // !__WXWINCE__
+ class HDCMapModeChanger
+ {
+ public:
+ HDCMapModeChanger(HDC hdc, int mm)
+ : m_hdc(hdc)
+ {
+ m_modeOld = ::SetMapMode(hdc, mm);
+ if ( !m_modeOld )
+ wxLogLastError(_T("SelectClipRgn"));
+ }
+
+ ~HDCMapModeChanger()
+ {
+ if ( m_modeOld )
+ ::SetMapMode(m_hdc, m_modeOld);
+ }
+
+ private:
+ HDC m_hdc;
+ int m_modeOld;
+
+ DECLARE_NO_COPY_CLASS(HDCMapModeChanger)
+ };
+
+ #define wxCHANGE_HDC_MAP_MODE(hdc, mm) \
+ HDCMapModeChanger wxMAKE_UNIQUE_NAME(wxHDCMapModeChanger)(hdc, mm)
+#endif // __WXWINCE__/!__WXWINCE__
+
+// smart buffeer using GlobalAlloc/GlobalFree()
+class GlobalPtr
+{
+public:
+ // allocates a block of given size
+ GlobalPtr(size_t size, unsigned flags = GMEM_MOVEABLE)
+ {
+ m_hGlobal = ::GlobalAlloc(flags, size);
+ if ( !m_hGlobal )
+ wxLogLastError(_T("GlobalAlloc"));
+ }
+
+ ~GlobalPtr()
+ {
+ if ( m_hGlobal && ::GlobalFree(m_hGlobal) )
+ wxLogLastError(_T("GlobalFree"));
+ }
+
+ // implicit conversion
+ operator HGLOBAL() const { return m_hGlobal; }
+
+private:
+ HGLOBAL m_hGlobal;
+
+ DECLARE_NO_COPY_CLASS(GlobalPtr)