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