home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 July / CMCD0704.ISO / Software / Freeware / Utilitare / VisualBoyAdvance-1.7.2 / src / win32 / WinHelper.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-17  |  8.1 KB  |  233 lines

  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:
  50.       inline CSize() {};
  51.       inline explicit CSize( const SIZE &size ) { cx = size.cx; cy = size.cy; }
  52.       inline explicit CSize( long nSizeX, long nSizeY ) { cx = nSizeX; cy = nSizeY; }
  53.       inline void Set( long nSizeX, long nSizeY ) { cx = nSizeX; cy = nSizeY; }
  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; }
  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:
  67.       inline CRect() {}
  68.       //      Initialisation constructor
  69.       inline explicit CRect( const RECT& rhs ) { Set( rhs.left, rhs.top, rhs.right, rhs.bottom );}
  70.       inline CRect(int xLeft, int yTop, int xRight, int yBottom) { Set( xLeft, yTop, xRight, yBottom ); }
  71.       //      Get the width of the rectangle
  72.       inline int Width() const { return right - left; }
  73.       //      Get the height of the rectangle
  74.       inline int Height() const { return bottom - top; }
  75.       //      overloaded operator so you don't have to do &rc anymore
  76.       inline operator LPCRECT() const { return this; };
  77.       inline operator LPRECT() { return this; };
  78.       //      Return the SIZE of the rectangle;
  79.       inline CSize Size() const { CSize s( Width(), Height() ); return s; }
  80.       //      Return the top left of the rectangle
  81.       inline POINT TopLeft() const { POINT pt = { left, top }; return pt; }   
  82.       //      Return the bottom right of the rectangle
  83.       inline POINT BottomRight() const { POINT pt = { right, bottom }; return pt; }   
  84.       //      Set the rectangles left, top, right and bottom
  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
  87.       inline bool IsEmpty() const { return left == 0 && right == 0 && top == 0 && bottom == 0 ? true : false; }
  88.       //      Zero out our rectangle
  89.       inline void Empty() { left = right = top = bottom = 0; }
  90.       //      Set the size of the rect but leave the top left position untouched.
  91.       inline void SetSize( const CSize &size ) { bottom = top + size.cy; right = left + size.cx; }
  92.       inline void SetSize( const SIZE &size ) { bottom = top + size.cy; right = left + size.cx; }
  93.       inline void SetSize( int cx, int cy ) { bottom = top + cy; right = left + cx; }
  94.       //      Move the rectangle by an offset
  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
  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
  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
  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:
  130.       inline CPoint() {};
  131.       inline CPoint( LPARAM lParam ) { x = LOWORD( lParam ); y = HIWORD(lParam); }
  132.       inline CPoint( int nX, int nY ) { x = nX; y = nY; }
  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; }
  136.       inline operator LPPOINT () { return this; }
  137.     };
  138.  
  139.  
  140.   class CScrollInfo : public tagSCROLLINFO
  141.     {
  142.     public:
  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:
  152.       inline CCriticalSection()       { ::InitializeCriticalSection(&m_sect); }
  153.       inline ~CCriticalSection() { ::DeleteCriticalSection(&m_sect); }
  154.  
  155.       //      Blocking lock.
  156.       inline void Lock()                      { ::EnterCriticalSection(&m_sect); }
  157.       //      Unlock
  158.       inline void Unlock()            { ::LeaveCriticalSection(&m_sect); }
  159.  
  160.       class CLock
  161.         //
  162.         //      Simple lock class for the critcal section
  163.         {
  164.         public:
  165.           inline CLock( CCriticalSection § ) : m_sect( sect ) { m_sect.Lock(); }
  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.  
  187.   inline bool IsShiftPressed()
  188.     {
  189.       return GetKeyState(VK_SHIFT) & 0x8000 ? true : false;
  190.     }
  191.  
  192.   inline bool IsAltPressed()
  193.     {
  194.       return GetKeyState(VK_MENU) & 0x8000 ? true : false;
  195.     }
  196.  
  197.   inline bool IsControlPressed()
  198.     {
  199.       return GetKeyState(VK_CONTROL) & 0x8000 ? true : false;
  200.     }
  201.  
  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:
  215.       inline CDeferWindowPos( const int nWindows = 1 ) : m_hdlDef( ::BeginDeferWindowPos( nWindows ) ) {}
  216.       inline ~CDeferWindowPos() { R_VERIFY( ::EndDeferWindowPos( m_hdlDef ) ); }
  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.         }
  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.