+// ---------------------------------------------------------------------------
+// useful macros and functions
+// ---------------------------------------------------------------------------
+
+// a wrapper macro for ZeroMemory()
+#if defined(__WIN32__) && !defined(__WXMICROWIN__)
+#define wxZeroMemory(obj) ::ZeroMemory(&obj, sizeof(obj))
+#else
+#define wxZeroMemory(obj) memset((void*) & obj, 0, sizeof(obj))
+#endif
+
+// This one is a macro so that it can be tested with #ifdef, it will be
+// undefined if it cannot be implemented for a given compiler.
+// Vc++, bcc, dmc, ow, mingw, codewarrior (and rsxnt) have _get_osfhandle.
+// Cygwin has get_osfhandle. Others are currently unknown, e.g. Salford,
+// Intel, Visual Age.
+#if defined(__WXWINCE__)
+ #define wxGetOSFHandle(fd) ((HANDLE)fd)
+ #define wxOpenOSFHandle(h, flags) ((int)wxPtrToUInt(h))
+#elif defined(__CYGWIN__)
+ #define wxGetOSFHandle(fd) ((HANDLE)get_osfhandle(fd))
+#elif defined(__VISUALC__) \
+ || defined(__BORLANDC__) \
+ || defined(__DMC__) \
+ || defined(__WATCOMC__) \
+ || defined(__MINGW32__) \
+ || (defined(__MWERKS__) && defined(__MSL__))
+ #define wxGetOSFHandle(fd) ((HANDLE)_get_osfhandle(fd))
+ #define wxOpenOSFHandle(h, flags) (_open_osfhandle(wxPtrToUInt(h), flags))
+ #define wx_fdopen _fdopen
+#endif
+
+// close the handle in the class dtor
+class AutoHANDLE
+{
+public:
+ wxEXPLICIT AutoHANDLE(HANDLE handle) : m_handle(handle) { }
+
+ bool IsOk() const { return m_handle != INVALID_HANDLE_VALUE; }
+ operator HANDLE() const { return m_handle; }
+
+ ~AutoHANDLE() { if ( IsOk() ) ::CloseHandle(m_handle); }
+
+protected:
+ HANDLE m_handle;
+};
+
+// a template to make initializing Windows styructs less painful: it zeroes all
+// the struct fields and also sets cbSize member to the correct value (and so
+// can be only used with structures which have this member...)
+template <class T>
+struct WinStruct : public T
+{
+ WinStruct()
+ {
+ ::ZeroMemory(this, sizeof(T));
+
+ // explicit qualification is required here for this to be valid C++
+ this->cbSize = sizeof(T);
+ }
+};
+
+
+#if wxUSE_GUI
+
+#include "wx/gdicmn.h"
+#include "wx/colour.h"
+
+// make conversion from wxColour and COLORREF a bit less painful
+inline COLORREF wxColourToRGB(const wxColour& c)
+{
+ return RGB(c.Red(), c.Green(), c.Blue());
+}
+
+inline COLORREF wxColourToPalRGB(const wxColour& c)
+{
+ return PALETTERGB(c.Red(), c.Green(), c.Blue());
+}
+
+inline wxColour wxRGBToColour(COLORREF rgb)
+{
+ return wxColour(GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));
+}
+
+inline void wxRGBToColour(wxColour& c, COLORREF rgb)
+{
+ c.Set(GetRValue(rgb), GetGValue(rgb), GetBValue(rgb));
+}
+
+// get the standard colour map for some standard colours - see comment in this
+// function to understand why is it needed and when should it be used
+//
+// it returns a wxCOLORMAP (can't use COLORMAP itself here as comctl32.dll
+// might be not included/available) array of size wxSTD_COLOUR_MAX
+//
+// NB: if you change these colours, update wxBITMAP_STD_COLOURS in the
+// resources as well: it must have the same number of pixels!
+enum wxSTD_COLOUR
+{
+ wxSTD_COL_BTNTEXT,
+ wxSTD_COL_BTNSHADOW,
+ wxSTD_COL_BTNFACE,
+ wxSTD_COL_BTNHIGHLIGHT,
+ wxSTD_COL_MAX
+};
+
+struct WXDLLIMPEXP_CORE wxCOLORMAP
+{
+ COLORREF from, to;
+};
+
+// this function is implemented in src/msw/window.cpp
+extern wxCOLORMAP *wxGetStdColourMap();
+
+// create a wxRect from Windows RECT
+inline wxRect wxRectFromRECT(const RECT& rc)
+{
+ return wxRect(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
+}
+
+// copy Windows RECT to our wxRect
+inline void wxCopyRECTToRect(const RECT& rc, wxRect& rect)
+{
+ rect = wxRectFromRECT(rc);
+}
+
+// and vice versa
+inline void wxCopyRectToRECT(const wxRect& rect, RECT& rc)
+{
+ // note that we don't use wxRect::GetRight() as it is one of compared to
+ // wxRectFromRECT() above
+ rc.top = rect.y;
+ rc.left = rect.x;
+ rc.right = rect.x + rect.width;
+ rc.bottom = rect.y + rect.height;
+}
+
+// translations between HIMETRIC units (which OLE likes) and pixels (which are
+// liked by all the others) - implemented in msw/utilsexc.cpp
+extern void HIMETRICToPixel(LONG *x, LONG *y);
+extern void HIMETRICToPixel(LONG *x, LONG *y, HDC hdcRef);
+extern void PixelToHIMETRIC(LONG *x, LONG *y);
+extern void PixelToHIMETRIC(LONG *x, LONG *y, HDC hdcRef);
+
+// Windows convention of the mask is opposed to the wxWidgets one, so we need
+// to invert the mask each time we pass one/get one to/from Windows
+extern HBITMAP wxInvertMask(HBITMAP hbmpMask, int w = 0, int h = 0);
+
+// Creates an icon or cursor depending from a bitmap
+//
+// The bitmap must be valid and it should have a mask. If it doesn't, a default
+// mask is created using light grey as the transparent colour.
+extern HICON wxBitmapToHICON(const wxBitmap& bmp);
+
+// Same requirments as above apply and the bitmap must also have the correct
+// size.
+extern
+HCURSOR wxBitmapToHCURSOR(const wxBitmap& bmp, int hotSpotX, int hotSpotY);
+
+
+#if wxUSE_OWNER_DRAWN
+
+// Draw the bitmap in specified state (this is used by owner drawn controls)
+enum wxDSBStates
+{
+ wxDSB_NORMAL = 0,
+ wxDSB_SELECTED,
+ wxDSB_DISABLED
+};
+
+extern
+BOOL wxDrawStateBitmap(HDC hDC, HBITMAP hBitmap, int x, int y, UINT uState);
+
+#endif // wxUSE_OWNER_DRAWN
+
+// get (x, y) from DWORD - notice that HI/LOWORD can *not* be used because they
+// will fail on system with multiple monitors where the coords may be negative
+//
+// these macros are standard now (Win98) but some older headers don't have them
+#ifndef GET_X_LPARAM
+ #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
+ #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
+#endif // GET_X_LPARAM
+
+// get the current state of SHIFT/CTRL/ALT keys
+inline bool wxIsModifierDown(int vk)
+{
+ // GetKeyState() returns different negative values on WinME and WinNT,
+ // so simply test for negative value.
+ return ::GetKeyState(vk) < 0;
+}
+
+inline bool wxIsShiftDown()
+{
+ return wxIsModifierDown(VK_SHIFT);
+}
+
+inline bool wxIsCtrlDown()
+{
+ return wxIsModifierDown(VK_CONTROL);
+}
+
+inline bool wxIsAltDown()
+{
+ return wxIsModifierDown(VK_MENU);
+}
+
+inline bool wxIsAnyModifierDown()
+{
+ return wxIsShiftDown() || wxIsCtrlDown() || wxIsAltDown();
+}
+
+// wrapper around GetWindowRect() and GetClientRect() APIs doing error checking
+// for Win32
+inline RECT wxGetWindowRect(HWND hwnd)
+{
+ RECT rect;
+
+ if ( !::GetWindowRect(hwnd, &rect) )
+ {
+ wxLogLastError(wxT("GetWindowRect"));
+ }
+
+ return rect;
+}
+
+inline RECT wxGetClientRect(HWND hwnd)
+{
+ RECT rect;
+
+ if ( !::GetClientRect(hwnd, &rect) )
+ {
+ wxLogLastError(wxT("GetClientRect"));
+ }
+
+ return rect;
+}
+
+// ---------------------------------------------------------------------------
+// small helper classes
+// ---------------------------------------------------------------------------
+
+// create an instance of this class and use it as the HDC for screen, will
+// automatically release the DC going out of scope
+class ScreenHDC
+{
+public:
+ ScreenHDC() { m_hdc = ::GetDC(NULL); }
+ ~ScreenHDC() { ::ReleaseDC(NULL, m_hdc); }
+
+ operator HDC() const { return m_hdc; }
+
+private:
+ HDC m_hdc;
+
+ wxDECLARE_NO_COPY_CLASS(ScreenHDC);
+};
+
+// the same as ScreenHDC but for window DCs
+class WindowHDC
+{
+public:
+ WindowHDC(HWND hwnd) { m_hdc = ::GetDC(m_hwnd = hwnd); }
+ ~WindowHDC() { ::ReleaseDC(m_hwnd, m_hdc); }
+
+ operator HDC() const { return m_hdc; }
+
+private:
+ HWND m_hwnd;
+ HDC m_hdc;
+
+ wxDECLARE_NO_COPY_CLASS(WindowHDC);
+};
+
+// the same as ScreenHDC but for memory DCs: creates the HDC compatible with
+// the given one (screen by default) in ctor and destroys it in dtor
+class MemoryHDC
+{
+public:
+ MemoryHDC(HDC hdc = 0) { m_hdc = ::CreateCompatibleDC(hdc); }
+ ~MemoryHDC() { ::DeleteDC(m_hdc); }
+
+ operator HDC() const { return m_hdc; }
+
+private:
+ HDC m_hdc;
+
+ wxDECLARE_NO_COPY_CLASS(MemoryHDC);
+};
+
+// a class which selects a GDI object into a DC in its ctor and deselects in
+// dtor
+class SelectInHDC
+{
+private:
+ void DoInit(HGDIOBJ hgdiobj) { m_hgdiobj = ::SelectObject(m_hdc, hgdiobj); }
+
+public:
+ SelectInHDC() : m_hdc(NULL), m_hgdiobj(NULL) { }
+ SelectInHDC(HDC hdc, HGDIOBJ hgdiobj) : m_hdc(hdc) { DoInit(hgdiobj); }
+
+ void Init(HDC hdc, HGDIOBJ hgdiobj)
+ {
+ wxASSERT_MSG( !m_hdc, wxT("initializing twice?") );
+
+ m_hdc = hdc;
+
+ DoInit(hgdiobj);
+ }
+
+ ~SelectInHDC() { if ( m_hdc ) ::SelectObject(m_hdc, m_hgdiobj); }
+
+ // return true if the object was successfully selected
+ operator bool() const { return m_hgdiobj != 0; }
+
+private:
+ HDC m_hdc;
+ HGDIOBJ m_hgdiobj;
+
+ wxDECLARE_NO_COPY_CLASS(SelectInHDC);
+};
+
+// 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, wxT("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(); }
+};