view 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 source
1 /*----------------------------------------------------------------------
2 Copyright (c) 1998 Gipsysoft. All Rights Reserved.
3 Please see the file "licence.txt" for licencing details.
4 File: WinHelper.h
5 Owner: russf@gipsysoft.com
6 Purpose: Windows helper functions, classes, structures and macros
7 that make life a little easier
8 These should all be zero impact classes etc. that is they
9 should *not* have a cpp file associated with them.
10 ----------------------------------------------------------------------*/
11 #ifndef WINHELPER_H
12 #define WINHELPER_H
14 //#ifndef DEBUGHLP_H
15 // #include <DebugHlp.h>
16 //#endif // DEBUGHLP_H
18 #ifndef FASTCALL
19 #define FASTCALL
20 #endif // FASTCALL
22 extern void AssertFailed(char *, int, char *);
23 extern void ApiFailure(char *, int, char *);
25 #define R_VERIFY(a) R_ASSERT(a)
26 #define R_ASSERT(a) \
27 do { \
28 if (!(a)) { \
29 AssertFailed(__FILE__, __LINE__, # a); \
30 } \
31 } while (0);
33 #define VAPI(a) \
34 do { \
35 if (!(a)) { \
36 ApiFailure(__FILE__, __LINE__, # a); \
37 } \
38 } while (0);
40 #define ASSERT_VALID_HWND(a) ASSERT(::IsWindow(a))
42 namespace WinHelper
43 {
44 class CSize : public tagSIZE
45 //
46 // Wrapper for the SIZE structure
47 {
48 public:
49 inline CSize() {};
50 inline explicit CSize(const SIZE &size) { cx = size.cx; cy = size.cy; }
51 inline explicit CSize(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; }
52 inline void Set(long nSizeX, long nSizeY) { cx = nSizeX; cy = nSizeY; }
53 inline operator LPSIZE() { return this; };
55 inline bool operator !=(const SIZE &size) const { return cx != size.cx || cy != size.cy;}
56 inline CSize & operator =(const SIZE &size) { cx = size.cx; cy = size.cy; return *this; }
57 inline void Empty() { cx = cy = 0; }
58 };
60 class CRect : public tagRECT
61 //
62 // Wrapper for a RECT structure
63 {
64 public:
65 inline CRect() {}
66 // Initialisation constructor
67 inline explicit CRect(const RECT& rhs) { Set(rhs.left, rhs.top, rhs.right, rhs.bottom);}
68 inline CRect(int xLeft, int yTop, int xRight, int yBottom) { Set(xLeft, yTop, xRight, yBottom); }
69 // Get the width of the rectangle
70 inline int Width() const { return right - left; }
71 // Get the height of the rectangle
72 inline int Height() const { return bottom - top; }
73 // overloaded operator so you don't have to do &rc anymore
74 inline operator LPCRECT() const { return this; };
75 inline operator LPRECT() { return this; };
76 // Return the SIZE of the rectangle;
77 inline CSize Size() const { CSize s(Width(), Height()); return s; }
78 // Return the top left of the rectangle
79 inline POINT TopLeft() const { POINT pt = { left, top }; return pt; }
80 // Return the bottom right of the rectangle
81 inline POINT BottomRight() const { POINT pt = { right, bottom }; return pt; }
82 // Set the rectangles left, top, right and bottom
83 inline void Set(int xLeft, int yTop, int xRight, int yBottom) { top = yTop; bottom = yBottom; right = xRight; left =
84 xLeft; }
85 // Return true if the rectangle contains all zeros
86 inline bool IsEmpty() const { return left == 0 && right == 0 && top == 0 && bottom == 0 ? true : false; }
87 // Zero out our rectangle
88 inline void Empty() { left = right = top = bottom = 0; }
89 // Set the size of the rect but leave the top left position untouched.
90 inline void SetSize(const CSize &size) { bottom = top + size.cy; right = left + size.cx; }
91 inline void SetSize(const SIZE &size) { bottom = top + size.cy; right = left + size.cx; }
92 inline void SetSize(int cx, int cy) { bottom = top + cy; right = left + cx; }
93 // Move the rectangle by an offset
94 inline void Offset(int cx, int cy)
95 {
96 top += cy;
97 bottom += cy;
98 right += cx;
99 left += cx;
100 }
102 // Inflate the rectangle by the cx and cy, use negative to shrink the rectangle
103 inline void Inflate(int cx, int cy)
104 {
105 top -= cy;
106 bottom += cy;
107 right += cx;
108 left -= cx;
109 }
111 // Assignment from a RECT
112 inline CRect &operator =(const RECT&rhs)
113 {
114 left = rhs.left; top = rhs.top;
115 right = rhs.right; bottom = rhs.bottom;
116 return *this;
117 }
119 // Return true if the point passed is within the rectangle
120 inline bool PtInRect(const POINT &pt) const { return (pt.x >= left && pt.x < right && pt.y >= top && pt.y <
121 bottom); }
122 // Return true if the rectangle passed overlaps this rectangle
123 inline bool Intersect(const RECT &rc) const { return (rc.left < right &&
124 rc.right > left && rc.top < bottom && rc.bottom > top); }
125 };
127 class CPoint : public tagPOINT
128 //
129 // Wrapper for the POINT structure
130 {
131 public:
132 inline CPoint() {};
133 inline CPoint(LPARAM lParam) { x = LOWORD(lParam); y = HIWORD(lParam); }
134 inline CPoint(int nX, int nY) { x = nX; y = nY; }
135 inline CPoint(const POINT &pt) { x = pt.x; y = pt.y; }
136 inline bool operator ==(const CPoint &rhs) const { return x == rhs.x && y == rhs.y; }
137 inline bool operator !=(const CPoint &rhs) const { return x != rhs.x || y != rhs.y; }
138 inline operator LPPOINT() { return this; }
139 };
141 class CScrollInfo : public tagSCROLLINFO
142 {
143 public:
144 CScrollInfo(UINT fPassedMask) { cbSize = sizeof(tagSCROLLINFO); fMask = fPassedMask; }
145 };
147 class CCriticalSection
148 //
149 // Simple crtical section handler/wrapper
150 {
151 public:
152 inline CCriticalSection() { ::InitializeCriticalSection(&m_sect); }
153 inline ~CCriticalSection() { ::DeleteCriticalSection(&m_sect); }
155 // Blocking lock.
156 inline void Lock() { ::EnterCriticalSection(&m_sect); }
157 // Unlock
158 inline void Unlock() { ::LeaveCriticalSection(&m_sect); }
160 class CLock
161 //
162 // Simple lock class for the critcal section
163 {
164 public:
165 inline CLock(CCriticalSection &sect) : m_sect(sect) { m_sect.Lock(); }
166 inline ~CLock() { m_sect.Unlock(); }
167 private:
168 CCriticalSection &m_sect;
170 CLock();
171 CLock(const CLock &);
172 CLock & operator =(const CLock &);
173 };
174 private:
175 CRITICAL_SECTION m_sect;
177 CCriticalSection(const CCriticalSection &);
178 CCriticalSection & operator =(const CCriticalSection &);
179 };
181 #define ZeroStructure(t) ZeroMemory(&t, sizeof(t))
182 #define countof(t) (sizeof((t)) / sizeof((t)[0]))
183 #define UNREF(P) UNREFERENCED_PARAMETER(P)
185 inline bool IsShiftPressed()
186 {
187 return GetKeyState(VK_SHIFT) & 0x8000 ? true : false;
188 }
190 inline bool IsAltPressed()
191 {
192 return GetKeyState(VK_MENU) & 0x8000 ? true : false;
193 }
195 inline bool IsControlPressed()
196 {
197 return GetKeyState(VK_CONTROL) & 0x8000 ? true : false;
198 }
200 inline HICON LoadIcon16x16(HINSTANCE hInst, UINT uID)
201 //
202 // Load a 16x16 icon from the same resource as the other size icons.
203 {
204 return reinterpret_cast<HICON>(::LoadImage(hInst, MAKEINTRESOURCE(uID), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR));
205 }
207 class CDeferWindowPos
208 //
209 // Wrapper for the Begin, Defer and End WindowPos functions. Nothing glamorous.
210 {
211 public:
212 inline CDeferWindowPos(const int nWindows = 1) : m_hdlDef(::BeginDeferWindowPos(nWindows)) {}
213 inline ~CDeferWindowPos() { R_VERIFY(::EndDeferWindowPos(m_hdlDef)); }
214 inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, int x, int y, int cx, int cy, UINT uFlags)
215 {
216 return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, x, y, cx, cy, uFlags);
217 }
219 inline HDWP DeferWindowPos(HWND hWnd, HWND hWndInsertAfter, const CRect &rc, UINT uFlags)
220 {
221 return ::DeferWindowPos(m_hdlDef, hWnd, hWndInsertAfter, rc.left, rc.top, rc.Width(), rc.Height(), uFlags);
222 }
224 private:
225 HDWP m_hdlDef;
226 };
227 } // WinHelper
229 #endif //WINHELPER_H