diff src/win32/WinHelper.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/WinHelper.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,229 @@
     1.4 +/*----------------------------------------------------------------------
     1.5 +   Copyright (c) 1998 Gipsysoft. All Rights Reserved.
     1.6 +   Please see the file "licence.txt" for licencing details.
     1.7 +   File:   WinHelper.h
     1.8 +   Owner:  russf@gipsysoft.com
     1.9 +   Purpose:        Windows helper functions, classes, structures and macros
    1.10 +   that make life a little easier
    1.11 +   These should all be zero impact classes etc. that is they
    1.12 +   should *not* have a cpp file associated with them.
    1.13 +   ----------------------------------------------------------------------*/
    1.14 +#ifndef WINHELPER_H
    1.15 +#define WINHELPER_H
    1.16 +
    1.17 +//#ifndef DEBUGHLP_H
    1.18 +//      #include <DebugHlp.h>
    1.19 +//#endif        //      DEBUGHLP_H
    1.20 +
    1.21 +#ifndef FASTCALL
    1.22 +#define FASTCALL
    1.23 +#endif  //      FASTCALL
    1.24 +
    1.25 +extern void AssertFailed(char *, int, char *);
    1.26 +extern void ApiFailure(char *, int, char *);
    1.27 +
    1.28 +#define R_VERIFY(a) R_ASSERT(a)
    1.29 +#define R_ASSERT(a) \
    1.30 +    do { \
    1.31 +		if (!(a)) { \
    1.32 +			AssertFailed(__FILE__, __LINE__, # a); \
    1.33 +		} \
    1.34 +	} while (0);
    1.35 +
    1.36 +#define VAPI(a) \
    1.37 +    do { \
    1.38 +		if (!(a)) { \
    1.39 +			ApiFailure(__FILE__, __LINE__, # a); \
    1.40 +		} \
    1.41 +	} while (0);
    1.42 +
    1.43 +#define ASSERT_VALID_HWND(a) ASSERT(::IsWindow(a))
    1.44 +
    1.45 +namespace WinHelper
    1.46 +{
    1.47 +	class CSize : public tagSIZE
    1.48 +		//
    1.49 +		//      Wrapper for the SIZE structure
    1.50 +	{
    1.51 +public:
    1.52 +		inline CSize() {};
    1.53 +		inline explicit CSize(const SIZE &size) { cx = size.cx; cy = size.cy; }
    1.54 +		inline explicit CSize(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; }
    1.55 +		inline void Set(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; }
    1.56 +		inline operator LPSIZE() { return this; };
    1.57 +
    1.58 +		inline bool operator !=(const SIZE &size) const { return cx != size.cx || cy != size.cy;}
    1.59 +		inline CSize & operator =(const SIZE &size) { cx = size.cx; cy = size.cy; return *this; }
    1.60 +		inline void Empty() { cx = cy = 0; }
    1.61 +	};
    1.62 +
    1.63 +	class CRect : public tagRECT
    1.64 +		//
    1.65 +		//      Wrapper for a RECT structure
    1.66 +	{
    1.67 +public:
    1.68 +		inline CRect() {}
    1.69 +		//      Initialisation constructor
    1.70 +		inline explicit CRect(const RECT& rhs) { Set(rhs.left, rhs.top, rhs.right, rhs.bottom);}
    1.71 +		inline CRect(int xLeft, int yTop, int xRight, int yBottom) { Set(xLeft, yTop, xRight, yBottom); }
    1.72 +		//      Get the width of the rectangle
    1.73 +		inline int Width() const { return right - left; }
    1.74 +		//      Get the height of the rectangle
    1.75 +		inline int Height() const { return bottom - top; }
    1.76 +		//      overloaded operator so you don't have to do &rc anymore
    1.77 +		inline operator LPCRECT() const { return this; };
    1.78 +		inline operator LPRECT() { return this; };
    1.79 +		//      Return the SIZE of the rectangle;
    1.80 +		inline CSize Size() const { CSize s(Width(), Height()); return s; }
    1.81 +		//      Return the top left of the rectangle
    1.82 +		inline POINT TopLeft() const { POINT pt = { left, top }; return pt; }
    1.83 +		//      Return the bottom right of the rectangle
    1.84 +		inline POINT BottomRight() const { POINT pt = { right, bottom }; return pt; }
    1.85 +		//      Set the rectangles left, top, right and bottom
    1.86 +		inline void Set(int xLeft, int yTop, int xRight, int yBottom) { top = yTop; bottom = yBottom; right = xRight; left =
    1.87 +			                                                                xLeft; }
    1.88 +		//      Return true if the rectangle contains all zeros
    1.89 +		inline bool IsEmpty() const { return left == 0 && right == 0 && top == 0 && bottom == 0 ? true : false; }
    1.90 +		//      Zero out our rectangle
    1.91 +		inline void Empty() { left = right = top = bottom = 0; }
    1.92 +		//      Set the size of the rect but leave the top left position untouched.
    1.93 +		inline void SetSize(const CSize &size) { bottom = top + size.cy; right = left + size.cx; }
    1.94 +		inline void SetSize(const SIZE &size) { bottom = top + size.cy; right = left + size.cx; }
    1.95 +		inline void SetSize(int cx, int cy) { bottom = top + cy; right = left + cx; }
    1.96 +		//      Move the rectangle by an offset
    1.97 +		inline void Offset(int cx, int cy)
    1.98 +		{
    1.99 +			top    += cy;
   1.100 +			bottom += cy;
   1.101 +			right  += cx;
   1.102 +			left   += cx;
   1.103 +		}
   1.104 +
   1.105 +		//      Inflate the rectangle by the cx and cy, use negative to shrink the rectangle
   1.106 +		inline void Inflate(int cx, int cy)
   1.107 +		{
   1.108 +			top    -= cy;
   1.109 +			bottom += cy;
   1.110 +			right  += cx;
   1.111 +			left   -= cx;
   1.112 +		}
   1.113 +
   1.114 +		//      Assignment from a RECT
   1.115 +		inline CRect &operator =(const RECT&rhs)
   1.116 +		{
   1.117 +			left  = rhs.left; top = rhs.top;
   1.118 +			right = rhs.right; bottom = rhs.bottom;
   1.119 +			return *this;
   1.120 +		}
   1.121 +
   1.122 +		//      Return true if the point passed is within the rectangle
   1.123 +		inline bool PtInRect(const POINT &pt) const {       return  (pt.x >= left && pt.x < right && pt.y >= top && pt.y <
   1.124 +			                                                         bottom); }
   1.125 +		//      Return true if the rectangle passed overlaps this rectangle
   1.126 +		inline bool Intersect(const RECT &rc) const { return (rc.left < right &&
   1.127 +			                                                            rc.right > left && rc.top < bottom && rc.bottom > top); }
   1.128 +	};
   1.129 +
   1.130 +	class CPoint : public tagPOINT
   1.131 +		//
   1.132 +		//      Wrapper for the POINT structure
   1.133 +	{
   1.134 +public:
   1.135 +		inline CPoint() {};
   1.136 +		inline CPoint(LPARAM lParam) { x = LOWORD(lParam); y = HIWORD(lParam); }
   1.137 +		inline CPoint(int nX, int nY) { x = nX; y = nY; }
   1.138 +		inline CPoint(const POINT &pt) { x = pt.x; y = pt.y; }
   1.139 +		inline bool operator ==(const CPoint &rhs) const { return x == rhs.x && y == rhs.y; }
   1.140 +		inline bool operator !=(const CPoint &rhs) const { return x != rhs.x || y != rhs.y; }
   1.141 +		inline operator LPPOINT() { return this; }
   1.142 +	};
   1.143 +
   1.144 +	class CScrollInfo : public tagSCROLLINFO
   1.145 +	{
   1.146 +public:
   1.147 +		CScrollInfo(UINT fPassedMask) { cbSize = sizeof(tagSCROLLINFO); fMask = fPassedMask; }
   1.148 +	};
   1.149 +
   1.150 +	class CCriticalSection
   1.151 +	//
   1.152 +	//      Simple crtical section handler/wrapper
   1.153 +	{
   1.154 +public:
   1.155 +		inline CCriticalSection()       { ::InitializeCriticalSection(&m_sect); }
   1.156 +		inline ~CCriticalSection() { ::DeleteCriticalSection(&m_sect); }
   1.157 +
   1.158 +		//      Blocking lock.
   1.159 +		inline void Lock()                      { ::EnterCriticalSection(&m_sect); }
   1.160 +		//      Unlock
   1.161 +		inline void Unlock()            { ::LeaveCriticalSection(&m_sect); }
   1.162 +
   1.163 +		class CLock
   1.164 +		//
   1.165 +		//      Simple lock class for the critcal section
   1.166 +		{
   1.167 +public:
   1.168 +			inline CLock(CCriticalSection &sect) : m_sect(sect) { m_sect.Lock(); }
   1.169 +			inline ~CLock() { m_sect.Unlock(); }
   1.170 +private:
   1.171 +			CCriticalSection &m_sect;
   1.172 +
   1.173 +			CLock();
   1.174 +			CLock(const CLock &);
   1.175 +			CLock & operator =(const CLock &);
   1.176 +		};
   1.177 +private:
   1.178 +		CRITICAL_SECTION m_sect;
   1.179 +
   1.180 +		CCriticalSection(const CCriticalSection &);
   1.181 +		CCriticalSection & operator =(const CCriticalSection &);
   1.182 +	};
   1.183 +
   1.184 +#define ZeroStructure(t) ZeroMemory(&t, sizeof(t))
   1.185 +#define countof(t)    (sizeof((t)) / sizeof((t)[0]))
   1.186 +#define UNREF(P) UNREFERENCED_PARAMETER(P)
   1.187 +
   1.188 +	inline bool IsShiftPressed()
   1.189 +	{
   1.190 +		return GetKeyState(VK_SHIFT) & 0x8000 ? true : false;
   1.191 +	}
   1.192 +
   1.193 +	inline bool IsAltPressed()
   1.194 +	{
   1.195 +		return GetKeyState(VK_MENU) & 0x8000 ? true : false;
   1.196 +	}
   1.197 +
   1.198 +	inline bool IsControlPressed()
   1.199 +	{
   1.200 +		return GetKeyState(VK_CONTROL) & 0x8000 ? true : false;
   1.201 +	}
   1.202 +
   1.203 +	inline HICON LoadIcon16x16(HINSTANCE hInst, UINT uID)
   1.204 +	//
   1.205 +	//      Load a 16x16 icon from the same resource as the other size icons.
   1.206 +	{
   1.207 +		return reinterpret_cast<HICON>(::LoadImage(hInst, MAKEINTRESOURCE(uID), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
   1.208 +	}
   1.209 +
   1.210 +	class CDeferWindowPos
   1.211 +	//
   1.212 +	//      Wrapper for the Begin, Defer and End WindowPos functions. Nothing glamorous.
   1.213 +	{
   1.214 +public:
   1.215 +		inline CDeferWindowPos(const int nWindows = 1) : m_hdlDef(::BeginDeferWindowPos(nWindows)) {}
   1.216 +		inline ~CDeferWindowPos() { R_VERIFY(::EndDeferWindowPos(m_hdlDef)); }
   1.217 +		inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags)
   1.218 +		{
   1.219 +			return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
   1.220 +		}
   1.221 +
   1.222 +		inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, const CRect &rc, UINT uFlags)
   1.223 +		{
   1.224 +			return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, rc.left, rc.top, rc.Width(), rc.Height(), uFlags);
   1.225 +		}
   1.226 +
   1.227 +private:
   1.228 +		HDWP m_hdlDef;
   1.229 +	};
   1.230 +}       //      WinHelper
   1.231 +
   1.232 +#endif //WINHELPER_H