rlm@1: /*---------------------------------------------------------------------- rlm@1: Copyright (c) 1998 Gipsysoft. All Rights Reserved. rlm@1: Please see the file "licence.txt" for licencing details. rlm@1: File: WinHelper.h rlm@1: Owner: russf@gipsysoft.com rlm@1: Purpose: Windows helper functions, classes, structures and macros rlm@1: that make life a little easier rlm@1: These should all be zero impact classes etc. that is they rlm@1: should *not* have a cpp file associated with them. rlm@1: ----------------------------------------------------------------------*/ rlm@1: #ifndef WINHELPER_H rlm@1: #define WINHELPER_H rlm@1: rlm@1: //#ifndef DEBUGHLP_H rlm@1: // #include rlm@1: //#endif // DEBUGHLP_H rlm@1: rlm@1: #ifndef FASTCALL rlm@1: #define FASTCALL rlm@1: #endif // FASTCALL rlm@1: rlm@1: extern void AssertFailed(char *, int, char *); rlm@1: extern void ApiFailure(char *, int, char *); rlm@1: rlm@1: #define R_VERIFY(a) R_ASSERT(a) rlm@1: #define R_ASSERT(a) \ rlm@1: do { \ rlm@1: if (!(a)) { \ rlm@1: AssertFailed(__FILE__, __LINE__, # a); \ rlm@1: } \ rlm@1: } while (0); rlm@1: rlm@1: #define VAPI(a) \ rlm@1: do { \ rlm@1: if (!(a)) { \ rlm@1: ApiFailure(__FILE__, __LINE__, # a); \ rlm@1: } \ rlm@1: } while (0); rlm@1: rlm@1: #define ASSERT_VALID_HWND(a) ASSERT(::IsWindow(a)) rlm@1: rlm@1: namespace WinHelper rlm@1: { rlm@1: class CSize : public tagSIZE rlm@1: // rlm@1: // Wrapper for the SIZE structure rlm@1: { rlm@1: public: rlm@1: inline CSize() {}; rlm@1: inline explicit CSize(const SIZE &size) { cx = size.cx; cy = size.cy; } rlm@1: inline explicit CSize(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; } rlm@1: inline void Set(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; } rlm@1: inline operator LPSIZE() { return this; }; rlm@1: rlm@1: inline bool operator !=(const SIZE &size) const { return cx != size.cx || cy != size.cy;} rlm@1: inline CSize & operator =(const SIZE &size) { cx = size.cx; cy = size.cy; return *this; } rlm@1: inline void Empty() { cx = cy = 0; } rlm@1: }; rlm@1: rlm@1: class CRect : public tagRECT rlm@1: // rlm@1: // Wrapper for a RECT structure rlm@1: { rlm@1: public: rlm@1: inline CRect() {} rlm@1: // Initialisation constructor rlm@1: inline explicit CRect(const RECT& rhs) { Set(rhs.left, rhs.top, rhs.right, rhs.bottom);} rlm@1: inline CRect(int xLeft, int yTop, int xRight, int yBottom) { Set(xLeft, yTop, xRight, yBottom); } rlm@1: // Get the width of the rectangle rlm@1: inline int Width() const { return right - left; } rlm@1: // Get the height of the rectangle rlm@1: inline int Height() const { return bottom - top; } rlm@1: // overloaded operator so you don't have to do &rc anymore rlm@1: inline operator LPCRECT() const { return this; }; rlm@1: inline operator LPRECT() { return this; }; rlm@1: // Return the SIZE of the rectangle; rlm@1: inline CSize Size() const { CSize s(Width(), Height()); return s; } rlm@1: // Return the top left of the rectangle rlm@1: inline POINT TopLeft() const { POINT pt = { left, top }; return pt; } rlm@1: // Return the bottom right of the rectangle rlm@1: inline POINT BottomRight() const { POINT pt = { right, bottom }; return pt; } rlm@1: // Set the rectangles left, top, right and bottom rlm@1: inline void Set(int xLeft, int yTop, int xRight, int yBottom) { top = yTop; bottom = yBottom; right = xRight; left = rlm@1: xLeft; } rlm@1: // Return true if the rectangle contains all zeros rlm@1: inline bool IsEmpty() const { return left == 0 && right == 0 && top == 0 && bottom == 0 ? true : false; } rlm@1: // Zero out our rectangle rlm@1: inline void Empty() { left = right = top = bottom = 0; } rlm@1: // Set the size of the rect but leave the top left position untouched. rlm@1: inline void SetSize(const CSize &size) { bottom = top + size.cy; right = left + size.cx; } rlm@1: inline void SetSize(const SIZE &size) { bottom = top + size.cy; right = left + size.cx; } rlm@1: inline void SetSize(int cx, int cy) { bottom = top + cy; right = left + cx; } rlm@1: // Move the rectangle by an offset rlm@1: inline void Offset(int cx, int cy) rlm@1: { rlm@1: top += cy; rlm@1: bottom += cy; rlm@1: right += cx; rlm@1: left += cx; rlm@1: } rlm@1: rlm@1: // Inflate the rectangle by the cx and cy, use negative to shrink the rectangle rlm@1: inline void Inflate(int cx, int cy) rlm@1: { rlm@1: top -= cy; rlm@1: bottom += cy; rlm@1: right += cx; rlm@1: left -= cx; rlm@1: } rlm@1: rlm@1: // Assignment from a RECT rlm@1: inline CRect &operator =(const RECT&rhs) rlm@1: { rlm@1: left = rhs.left; top = rhs.top; rlm@1: right = rhs.right; bottom = rhs.bottom; rlm@1: return *this; rlm@1: } rlm@1: rlm@1: // Return true if the point passed is within the rectangle rlm@1: inline bool PtInRect(const POINT &pt) const { return (pt.x >= left && pt.x < right && pt.y >= top && pt.y < rlm@1: bottom); } rlm@1: // Return true if the rectangle passed overlaps this rectangle rlm@1: inline bool Intersect(const RECT &rc) const { return (rc.left < right && rlm@1: rc.right > left && rc.top < bottom && rc.bottom > top); } rlm@1: }; rlm@1: rlm@1: class CPoint : public tagPOINT rlm@1: // rlm@1: // Wrapper for the POINT structure rlm@1: { rlm@1: public: rlm@1: inline CPoint() {}; rlm@1: inline CPoint(LPARAM lParam) { x = LOWORD(lParam); y = HIWORD(lParam); } rlm@1: inline CPoint(int nX, int nY) { x = nX; y = nY; } rlm@1: inline CPoint(const POINT &pt) { x = pt.x; y = pt.y; } rlm@1: inline bool operator ==(const CPoint &rhs) const { return x == rhs.x && y == rhs.y; } rlm@1: inline bool operator !=(const CPoint &rhs) const { return x != rhs.x || y != rhs.y; } rlm@1: inline operator LPPOINT() { return this; } rlm@1: }; rlm@1: rlm@1: class CScrollInfo : public tagSCROLLINFO rlm@1: { rlm@1: public: rlm@1: CScrollInfo(UINT fPassedMask) { cbSize = sizeof(tagSCROLLINFO); fMask = fPassedMask; } rlm@1: }; rlm@1: rlm@1: class CCriticalSection rlm@1: // rlm@1: // Simple crtical section handler/wrapper rlm@1: { rlm@1: public: rlm@1: inline CCriticalSection() { ::InitializeCriticalSection(&m_sect); } rlm@1: inline ~CCriticalSection() { ::DeleteCriticalSection(&m_sect); } rlm@1: rlm@1: // Blocking lock. rlm@1: inline void Lock() { ::EnterCriticalSection(&m_sect); } rlm@1: // Unlock rlm@1: inline void Unlock() { ::LeaveCriticalSection(&m_sect); } rlm@1: rlm@1: class CLock rlm@1: // rlm@1: // Simple lock class for the critcal section rlm@1: { rlm@1: public: rlm@1: inline CLock(CCriticalSection §) : m_sect(sect) { m_sect.Lock(); } rlm@1: inline ~CLock() { m_sect.Unlock(); } rlm@1: private: rlm@1: CCriticalSection &m_sect; rlm@1: rlm@1: CLock(); rlm@1: CLock(const CLock &); rlm@1: CLock & operator =(const CLock &); rlm@1: }; rlm@1: private: rlm@1: CRITICAL_SECTION m_sect; rlm@1: rlm@1: CCriticalSection(const CCriticalSection &); rlm@1: CCriticalSection & operator =(const CCriticalSection &); rlm@1: }; rlm@1: rlm@1: #define ZeroStructure(t) ZeroMemory(&t, sizeof(t)) rlm@1: #define countof(t) (sizeof((t)) / sizeof((t)[0])) rlm@1: #define UNREF(P) UNREFERENCED_PARAMETER(P) rlm@1: rlm@1: inline bool IsShiftPressed() rlm@1: { rlm@1: return GetKeyState(VK_SHIFT) & 0x8000 ? true : false; rlm@1: } rlm@1: rlm@1: inline bool IsAltPressed() rlm@1: { rlm@1: return GetKeyState(VK_MENU) & 0x8000 ? true : false; rlm@1: } rlm@1: rlm@1: inline bool IsControlPressed() rlm@1: { rlm@1: return GetKeyState(VK_CONTROL) & 0x8000 ? true : false; rlm@1: } rlm@1: rlm@1: inline HICON LoadIcon16x16(HINSTANCE hInst, UINT uID) rlm@1: // rlm@1: // Load a 16x16 icon from the same resource as the other size icons. rlm@1: { rlm@1: return reinterpret_cast(::LoadImage(hInst, MAKEINTRESOURCE(uID), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR)); rlm@1: } rlm@1: rlm@1: class CDeferWindowPos rlm@1: // rlm@1: // Wrapper for the Begin, Defer and End WindowPos functions. Nothing glamorous. rlm@1: { rlm@1: public: rlm@1: inline CDeferWindowPos(const int nWindows = 1) : m_hdlDef(::BeginDeferWindowPos(nWindows)) {} rlm@1: inline ~CDeferWindowPos() { R_VERIFY(::EndDeferWindowPos(m_hdlDef)); } rlm@1: inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags) rlm@1: { rlm@1: return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags); rlm@1: } rlm@1: rlm@1: inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, const CRect &rc, UINT uFlags) rlm@1: { rlm@1: return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, rc.left, rc.top, rc.Width(), rc.Height(), uFlags); rlm@1: } rlm@1: rlm@1: private: rlm@1: HDWP m_hdlDef; rlm@1: }; rlm@1: } // WinHelper rlm@1: rlm@1: #endif //WINHELPER_H