home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / utility / StRegionHandle.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.2 KB  |  105 lines

  1. //////////////////////////////////
  2.  
  3. //
  4. // StRegionHandle.h
  5. //
  6. //    by Drew Thaler, athaler@umich.edu
  7. //    released 9/21/96
  8. //
  9. //    This source code is public domain.
  10. //
  11.  
  12. //////////////////////////////////
  13.  
  14. // MGY: renamed to StRegionHandle (from StRegion) to avoid conflict with
  15. // existing StRegion.
  16.  
  17. #ifndef _StRegionHandle_H_
  18. #define _StRegionHandle_H_
  19.  
  20.  
  21. #pragma mark -- StRegionHandle --
  22.  
  23. // -------------------------------------------------------------------------
  24. //    Ñ StRegionHandle
  25. // -------------------------------------------------------------------------
  26. //
  27. //    This class is an abstraction of the Macintosh Region data structure,
  28. //    implemented entirely with inline functions.  It's meant to provide an
  29. //    easy-to-use interface to most of the standard region operations -- copying,
  30. //    adding, subtracting, xor'ing, intersecting, converting to/from rectangles,
  31. //    etc -- while using the magic of C++ to protect the user from the gory details.
  32. //    
  33. //    Benefits:    Ñ Automatic memory management -- say goodbye to NewRgn, DisposeRgn, etc!
  34. //                Ñ Standard operators like +=, -=, &=, ==, etc for both Rects and RgnHandles
  35. //                Ñ Automatic coercion operators, can use directly in Toolbox calls
  36. //                Ñ No extra function calls: feels like C++, generates code like C.
  37. //
  38.  
  39.  
  40. class StRegionHandle
  41. {
  42.  
  43. public:
  44.     StRegionHandle()                            { mRegion = ::NewRgn(); }
  45.     StRegionHandle(const RgnHandle fromRgn)    { mRegion = ::NewRgn(); ::CopyRgn( fromRgn, mRegion ); }
  46.     StRegionHandle(const Rect &fromRect)        { mRegion = ::NewRgn(); ::RectRgn( mRegion, &fromRect ); }
  47.     ~StRegionHandle()                            { if (mRegion) ::DisposeRgn(mRegion); }
  48.     
  49.     Rect        bbox() const                    { return (**mRegion).rgnBBox; }
  50.     void        GetBounds(Rect &outRect) const    { outRect = (**mRegion).rgnBBox; }
  51.     void        Clear()                            { ::SetEmptyRgn(mRegion); }
  52.     Boolean        IsEmpty() const                    { return ::EmptyRgn(mRegion); }
  53.     RgnHandle    Detach()                        { RgnHandle oldRegion = mRegion; mRegion = ::NewRgn(); return oldRegion; }
  54.     
  55.     Boolean        IsValid()                        { return (mRegion!=nil); }
  56.     
  57.     operator RgnHandle() const                    { return mRegion; }
  58.     operator Handle() const                        { return (Handle)mRegion; }
  59.     
  60.     RgnHandle &    operator += ( RgnHandle r )        { ::UnionRgn(mRegion,r,mRegion); return mRegion; }
  61.     RgnHandle &    operator -= ( RgnHandle r )        { ::DiffRgn(mRegion,r,mRegion); return mRegion; }
  62.     RgnHandle &    operator |= ( RgnHandle r )        { ::UnionRgn(mRegion,r,mRegion); return mRegion; }
  63.     RgnHandle &    operator &= ( RgnHandle r )        { ::SectRgn(mRegion,r,mRegion); return mRegion; }
  64.     RgnHandle &    operator ^= ( RgnHandle r )        { ::XorRgn(mRegion,r,mRegion); return mRegion; }
  65.     RgnHandle & operator = ( RgnHandle r )        { ::CopyRgn(r,mRegion); return mRegion; }
  66.     
  67.     RgnHandle & operator += ( Rect & rect )        { StRegionHandle r(rect); ::UnionRgn(mRegion,r,mRegion); return mRegion; }
  68.     RgnHandle & operator -= ( Rect & rect )        { StRegionHandle r(rect); ::DiffRgn(mRegion,r,mRegion); return mRegion; }
  69.     RgnHandle & operator |= ( Rect & rect )        { StRegionHandle r(rect); ::UnionRgn(mRegion,r,mRegion); return mRegion; }
  70.     RgnHandle & operator &= ( Rect & rect )        { StRegionHandle r(rect); ::SectRgn(mRegion,r,mRegion); return mRegion; }
  71.     RgnHandle & operator ^= ( Rect & rect )        { StRegionHandle r(rect); ::XorRgn(mRegion,r,mRegion); return mRegion; }
  72.     RgnHandle & operator = ( Rect &r )            { ::RectRgn(mRegion,&r); return mRegion; }
  73.     
  74.     Boolean operator == ( RgnHandle r )            { return ::EqualRgn(r,mRegion); }
  75.     Boolean operator != ( RgnHandle r )            { return !::EqualRgn(r,mRegion); }
  76.     
  77.     Boolean operator == ( Rect & rect )            { StRegionHandle r(rect); return ::EqualRgn(r,mRegion); }
  78.     Boolean operator != ( Rect & rect )            { StRegionHandle r(rect); return !::EqualRgn(r,mRegion); }
  79.     
  80.     // why not, so you can do "if rgn == 0", right?
  81.     RgnHandle & operator = ( int x )            { ::SetEmptyRgn(mRegion); return mRegion; }
  82.     Boolean operator == ( int x )                { return ( (x==0) && ::EmptyRgn(mRegion) ); }
  83.     Boolean operator != ( int x )                { return ( (x!=0) || !::EmptyRgn(mRegion) ); }
  84.     
  85.     
  86. private:
  87.     RgnHandle    mRegion;    
  88. };
  89.  
  90.  
  91. //////////////////////////////////
  92.  
  93.  
  94. #pragma mark -- StTempRegion --
  95.  
  96. typedef class StRegionHandle    StTempRegion;
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. #endif // _StRegionHandle_H_
  104.  
  105.