home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 19.ddi / OWLINC.PAK / POINT.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  17.1 KB  |  525 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   include\owl\point.h
  4. //   Definition of mathematical TPoint, TSize, TRect classes.
  5. //   Also, other simple, inline windows classes: TDropInfo
  6. //   Also, TPointer classes for exception safe pointer management
  7. //----------------------------------------------------------------------------
  8. #if !defined(__OWL_POINT_H)
  9. #define __OWL_POINT_H
  10.  
  11. #if !defined(__OWL_OWLDEFS_H)
  12.   #include <owl\owldefs.h>
  13. #endif
  14. #if !defined(__CLASSLIB_OBJSTRM_H)
  15.   #include <classlib\objstrm.h>
  16. #endif
  17.  
  18. //
  19. // Misc support functions
  20. //
  21. inline void Swap(BYTE far& a, BYTE far& b) {a^=b; b^=a; a^=b;}
  22. inline void Swap(int far& a, int far& b) {a^=b; b^=a; a^=b;}
  23. inline void Swap(LONG far& a, LONG far& b) {a^=b; b^=a; a^=b;}
  24. inline int Min(int a, int b) {return a<b ? a : b;}
  25. inline int Max(int a, int b) {return a>b ? a : b;}
  26. int    _OWLFUNC Sqrt(long val);
  27.  
  28. //
  29. // Inline versions of common RTL functions, overloaded for far ptrs for use
  30. // when in a small data model.
  31. //
  32. #if defined(__SMALL__) || defined(__MEDIUM__)
  33.   inline void far* memccpy(void far* d, const void far* s, int c, size_t n)
  34.                      {return _fmemccpy(d, s, c, n);}
  35.   inline void far* memchr(const void far* s, int c, size_t n)
  36.                      {return _fmemchr(s, c, n);}
  37.   inline int       memcmp(const void far* s1, const void far* s2, size_t n)
  38.                      {return _fmemcmp(s1, s2, n);}
  39.   inline void far* memcpy(void far* d, const void far* s, size_t n)
  40.                      {return _fmemcpy(d, s, n);}
  41.   inline int       memicmp(const void far* s1, const void far* s2, size_t n)
  42.                      {return _fmemicmp(s1, s2, n);}
  43.   inline void far* memset(void far* s, int c, size_t n)
  44.                      {return _fmemset(s, c, n);}
  45.   inline void far* memmove(void far* d, const void far* s, size_t n)
  46.                      {return _fmemmove(d, s, n);}
  47.  
  48.   inline char far* strcat(char far* d, const char far* s)
  49.                      {return _fstrcat(d, s);}
  50.   inline char far* strchr(const char far* s, int c) {return _fstrchr(s, c);}
  51.   inline int       strcmp(const char far* s1, const char far* s2)
  52.                      {return _fstrcmp(s1, s2);}
  53.   inline char far* strcpy(char far* d, const char far* s)
  54.                      {return _fstrcpy(d, s);}
  55.   inline int       stricmp(const char far* s1, const char far* s2)
  56.                      {return _fstricmp(s1, s2);}
  57.   inline size_t    strlen(const char far* s) {return _fstrlen(s);}
  58.   inline char far* strlwr(char far* s) {return _fstrlwr(s);}
  59.   inline char far* strncat(char far* d, const char far* s, size_t maxlen)
  60.                      {return _fstrncat(d, s, maxlen);}
  61.   inline int       strncmp(const char far* s1, const char far* s2, size_t maxlen)
  62.                      {return _fstrncmp(s1, s2, maxlen);}
  63.   inline char far* strncpy(char far* d, const char far* s, size_t maxlen)
  64.                      {return _fstrncpy(d, s, maxlen);}
  65.   inline int       strnicmp(const char far* s1, const char far* s2, size_t maxlen)
  66.                      {return _fstrnicmp(s1, s2, maxlen);}
  67.   inline char far* strrchr(const char far* s, int c) {return _fstrrchr(s, c);}
  68.   inline char far* strtok(char far* s1, const char far* s2)
  69.                      {return _fstrtok(s1, s2);}
  70.   inline char far* strupr(char far* s) {return _fstrupr(s);}
  71. #endif
  72.  
  73. //
  74. // type overloaded version of Window's huge mem copy (hmemcpy) for flat models
  75. //
  76. #if defined(__FLAT__)
  77.   inline void      hmemcpy(void* d, const void* s, long n) {memcpy(d, s, n);}
  78. #endif
  79.  
  80. //
  81. // strnewdup() uses new char[], to allow duplicated strings to be deleted
  82. //
  83. char*  _OWLFUNC strnewdup(const char* s);
  84. #if defined(__SMALL__) || defined(__MEDIUM__)
  85.   char far*  _OWLFUNC strnewdup(const char far* s);
  86.   long atol(const char far* s);
  87. #endif
  88.  
  89.  
  90. class TSize;
  91. class _OWLCLASS TRect;
  92.  
  93. //
  94. // class TPoint
  95. // ----- ------
  96. //
  97. class  TPoint : public tagPOINT {
  98.   public:
  99.     // Constructors
  100.     TPoint() {}
  101.     TPoint(int _x, int _y) {x = _x; y = _y;}
  102.     TPoint(const POINT& point) {x = point.x; y = point.y;}
  103.     TPoint(const SIZE& size) {x = size.cx; y = size.cy;}
  104.     TPoint(DWORD dw) {x = int16(LOWORD(dw)); y = int16(HIWORD(dw));}
  105.  
  106.     // Information functions/operators
  107.     BOOL        operator ==(const TPoint& other) const;
  108.     BOOL        operator !=(const TPoint& other) const;
  109.  
  110.     // Functions/binary-operators that return points or sizes
  111.     TPoint      OffsetBy(int dx, int dy) const {return TPoint(x+dx, y+dy);}
  112.     TPoint      operator +(const TSize& size) const;
  113.     TSize       operator -(const TPoint& point) const;
  114.     TPoint      operator -(const TSize& size) const;
  115.     TPoint      operator -() const {return TPoint(-x, -y);}
  116.  
  117.     // Functions/assignement-operators that modify this point
  118.     TPoint&     Offset(int dx, int dy);
  119.     TPoint&     operator +=(const TSize& size);
  120.     TPoint&     operator -=(const TSize& size);
  121.  
  122.     friend inline ipstream& operator >>(ipstream& is, TPoint& p)
  123.       { return is >> p.x >> p.y; }
  124.     friend inline opstream& operator <<(opstream& os, const TPoint& p)
  125.       { return os << p.x << p.y; }
  126.     friend inline ostream& operator <<(ostream& os, const TPoint& p)
  127.       { return os << '(' << p.x << ',' << p.y << ')'; }
  128.     friend inline istream& operator >>(istream& is, TPoint& p)
  129.       { char c; return is >> c >> p.x >> c >> p.y >> c; }
  130. };
  131.  
  132. //
  133. // class TSize
  134. // ----- -----
  135. //
  136. class  TSize : public tagSIZE {
  137.   public:
  138.     // Constructors
  139.     TSize() {}
  140.     TSize(int dx, int dy) {cx = dx; cy = dy;}
  141.     TSize(const POINT& point) {cx = point.x; cy = point.y;}
  142.     TSize(const SIZE& size) {cx = size.cx; cy = size.cy;}
  143.     TSize(DWORD dw) {cx = LOWORD(dw); cy = HIWORD(dw);}
  144.  
  145.     // Information functions/operators
  146.     BOOL        operator ==(const TSize& other) const;
  147.     BOOL        operator !=(const TSize& other) const;
  148.     int         Magnitude() const;
  149.  
  150.     // Functions/binary-operators that return sizes
  151.     TSize      operator +(const TSize& size) const;
  152.     TSize      operator -(const TSize& size) const;
  153.     TSize      operator -() const {return TSize(-cx, -cy);}
  154.  
  155.     // Functions/assignement-operators that modify this size
  156.     TSize&     operator +=(const TSize& size);
  157.     TSize&     operator -=(const TSize& size);
  158.  
  159.     friend inline ipstream& operator >>(ipstream& is, TSize& s)
  160.         { return is >> s.cx >> s.cy; }
  161.     friend inline opstream& operator <<(opstream& os, const TSize& s)
  162.         { return os << s.cx << s.cy; }
  163.     friend inline ostream& operator <<(ostream& os, const TSize& s)
  164.       { return os << '(' << s.cx << 'x' << s.cy << ')'; }
  165. };
  166.  
  167. //
  168. // class TRect
  169. // ----- -----
  170. //
  171. class _OWLCLASS TRect : public tagRECT {
  172.   public:
  173.     // Constructors
  174.     TRect() {}
  175.     TRect(const RECT far& rect);
  176.     TRect(int _left, int _top, int _right, int _bottom);
  177.     TRect(const TPoint& upLeft, const TPoint& loRight);
  178.     TRect(const TPoint& origin, const TSize& extent);
  179.  
  180.     // (re)Initializers
  181.     void        SetNull();
  182.     void        SetEmpty() {SetNull();}
  183.     void        Set(int _left, int _top, int _right, int _bottom);
  184.  
  185.     // Type Conversion operators
  186.     operator    const TPoint*() const {return (const TPoint*)this;}
  187.     operator    TPoint*() {return (TPoint*)this;}
  188.  
  189.     // Testing functions
  190.     BOOL        IsEmpty() const;
  191.     BOOL        IsNull() const;
  192.     BOOL        operator ==(const TRect& other) const;
  193.     BOOL        operator !=(const TRect& other) const;
  194.  
  195.     // Information/access functions(const and non-const)
  196.     const TPoint& TopLeft() const {return *(TPoint*)&left;}
  197.     TPoint&     TopLeft() {return *(TPoint*)&left;}
  198.     TPoint      TopRight() const {return TPoint(right, top);}
  199.     TPoint      BottomLeft() const {return TPoint(left, bottom);}
  200.     const TPoint& BottomRight() const {return *(TPoint*)&right;}
  201.     TPoint&     BottomRight() {return *(TPoint*)&right;}
  202.     int         Width() const {return right-left;}
  203.     int         Height() const {return bottom-top;}
  204.     TSize       Size() const {return TSize(Width(), Height());}
  205.     long        Area() const {return long(Width())*long(Height());}
  206.  
  207.     BOOL        Contains(const TPoint& point) const;
  208.     BOOL        Contains(const TRect& other) const;
  209.     BOOL        Touches(const TRect& other) const;
  210.     TRect       OffsetBy(int dx, int dy) const;
  211.     TRect       operator +(const TSize& size) const;
  212.     TRect       operator -(const TSize& size) const;
  213.     TRect       InflatedBy(int dx, int dy) const;
  214.     TRect       InflatedBy(const TSize& size) const;
  215.     TRect       Normalized() const;
  216.     TRect       operator &(const TRect& other) const;
  217.     TRect       operator |(const TRect& other) const;
  218.  
  219.     // Manipulation functions/operators
  220.     TRect&      Normalize();
  221.     TRect&      Offset(int dx, int dy);
  222.     TRect&      operator +=(const TSize& delta);
  223.     TRect&      operator -=(const TSize& delta);
  224.     TRect&      Inflate(int dx, int dy);
  225.     TRect&      Inflate(const TSize& delta);
  226.     TRect&      operator &=(const TRect& other);
  227.     TRect&      operator |=(const TRect& other);
  228.  
  229.     friend ipstream& _OWLFUNC operator >>(ipstream& is, TRect& r);
  230.     friend opstream& _OWLFUNC operator <<(opstream& os, const TRect& r);
  231.     friend ostream& _OWLFUNC operator <<(ostream& os, const TRect& r);
  232. };
  233.  
  234. //
  235. // class TResId
  236. // ----- ------
  237. //
  238. // Resource Id class that can be constructed from a integer or string resource
  239. // identifier.
  240. //
  241. class TResId {
  242.   public:
  243.     TResId() : Id(0) {}
  244.     TResId(LPCSTR resString) : Id(resString) {}
  245.     TResId(int resNum) : Id(MAKEINTRESOURCE(resNum)) {}
  246.     operator LPSTR() {return (LPSTR)Id;}
  247.     BOOL IsString() const {return HIWORD(Id);}
  248.  
  249.     friend ipstream& _OWLFUNC operator >>(ipstream& is, TResId& id);
  250.     friend opstream& _OWLFUNC operator <<(opstream& os, const TResId& id);
  251.     friend ostream& _OWLFUNC  operator <<(ostream& os, const TResId& id);
  252.     
  253.   private:
  254.     LPCSTR  Id;
  255. };
  256. #define TResID           TResId     // Short-term backward compatibility
  257.  
  258. //
  259. // class TDropInfo
  260. // ----- ---------
  261. //
  262. class TDropInfo {
  263.   public:
  264.     TDropInfo(HDROP handle) : Handle(handle) {}
  265.  
  266.     operator HDROP() {return Handle;}
  267.  
  268.     UINT DragQueryFile(UINT index, char far* name, UINT nameLen)
  269.            {return ::DragQueryFile(Handle, index, name, nameLen);}
  270.     UINT DragQueryFileCount() {return ::DragQueryFile(Handle, -1, 0, 0);}
  271.     UINT DragQueryFileNameLen(UINT index)
  272.            {return ::DragQueryFile(Handle, index, 0, 0);}
  273.     BOOL DragQueryPoint(TPoint& point)
  274.            {return ::DragQueryPoint(Handle, &point);}
  275.     void DragFinish() {::DragFinish(Handle);}
  276.  
  277.   private:
  278.     HDROP  Handle;
  279. };
  280.  
  281. //
  282. // class TProcInstance
  283. // ----- -------------
  284. //
  285. class TProcInstance {
  286.   public:
  287.     #if defined(__WIN32__)
  288.       TProcInstance(FARPROC p) {Instance = FARPROC(p);}
  289.     #else
  290.       TProcInstance(FARPROC p) {Instance = ::MakeProcInstance(p, _hInstance);}
  291.      ~TProcInstance() {::FreeProcInstance(Instance);}
  292.     #endif
  293.    
  294.     operator FARPROC() {return Instance;}
  295.  
  296.   private:
  297.     FARPROC Instance;
  298. };
  299.  
  300. //
  301. // class TPointer
  302. // ----- --------
  303. //
  304. template<class T> class TPointerBase {
  305.   public:
  306.     T&   operator  *() {return *P;} // should throw exception if P==0
  307.          operator T*() {return P;}
  308.     int  operator  !() {return P==0;}
  309.     void operator  ~() {P = 0;}
  310.     void operator delete(void* p) {((TPointerBase<T>*)p)->P=0;}
  311.   protected:
  312.     TPointerBase(T* pointer) : P(pointer){}
  313.    ~TPointerBase() {delete P;}
  314.     TPointerBase() : P(0){}
  315.     T* P;
  316.   private:
  317.     void* operator new(size_t){return 0;}  // prohibit use of new
  318. };
  319.  
  320. template<class T> class TPointer : public TPointerBase<T> {
  321.   public:
  322.     TPointer() : TPointerBase<T>(){}
  323.     TPointer(T* pointer) : TPointerBase<T>(pointer){}
  324.     T* operator =(T* src) {delete P; return P = src;}
  325.     T* operator =(const TPointer<T>& src)
  326.                           {delete P; return P = src.P;}
  327.     T* operator->() {return P;} // should throw exception if P==0
  328. };
  329.  
  330. class TPointer<char> : public TPointerBase<char> {
  331.   public:
  332.     TPointer() : TPointerBase<char>(){}
  333.     TPointer(char* pointer) : TPointerBase<char>(pointer){}
  334.     char* operator =(char* src) {delete P; return P = src;}
  335.     char* operator =(const TPointer<char>& src)
  336.                                 {delete P; return P = src.P;}
  337.     char& operator[](int i) {return P[i];}
  338. };
  339.  
  340.  
  341. //----------------------------------------------------------------------------
  342. // Inlines
  343. //----------------------------------------------------------------------------
  344.  
  345. inline BOOL TPoint::operator ==(const TPoint& other) const {
  346.   return other.x==x && other.y==y;
  347. }
  348.  
  349. inline BOOL TPoint::operator !=(const TPoint& other) const {
  350.   return other.x!=x || other.y!=y;
  351. }
  352.  
  353. inline TPoint TPoint::operator +(const TSize& size) const {
  354.   return TPoint(x+size.cx, y+size.cy);
  355. }
  356.  
  357. inline TSize TPoint::operator -(const TPoint& point) const {
  358.   return TSize(x-point.x, y-point.y);
  359. }
  360.  
  361. inline TPoint TPoint::operator -(const TSize& size) const {
  362.   return TPoint(x-size.cx, y-size.cy);
  363. }
  364.  
  365. inline TPoint& TPoint::Offset(int dx, int dy) {
  366.   x += dx;
  367.   y += dy;
  368.   return *this;
  369. }
  370.  
  371. inline TPoint& TPoint::operator +=(const TSize& size) {
  372.   x += size.cx;
  373.   y += size.cy;
  374.   return *this;
  375. }
  376.  
  377. inline TPoint& TPoint::operator -=(const TSize& size) {
  378.   x -= size.cx;
  379.   y -= size.cy;
  380.   return *this;
  381. }
  382.  
  383.  
  384. inline BOOL TSize::operator ==(const TSize& other) const {
  385.   return other.cx==cx && other.cy==cy;
  386. }
  387.  
  388. inline BOOL TSize::operator !=(const TSize& other) const {
  389.   return other.cx!=cx || other.cy!=cy;
  390. }
  391.  
  392. inline int TSize::Magnitude() const {
  393.   return Sqrt(long(cx)*long(cx)+long(cy)*long(cy));
  394. }
  395.  
  396. inline TSize TSize::operator +(const TSize& size) const {
  397.   return TSize(cx+size.cx, cy+size.cy);
  398. }
  399.  
  400. inline TSize TSize::operator -(const TSize& size) const {
  401.   return TSize(cx-size.cx, cy-size.cy);
  402. }
  403.  
  404. inline TSize& TSize::operator +=(const TSize& size) {
  405.   cx += size.cx;
  406.   cy += size.cy;
  407.   return *this;
  408. }
  409.  
  410. inline TSize& TSize::operator -=(const TSize& size) {
  411.   cx -= size.cx;
  412.   cy -= size.cy;
  413.   return *this;
  414. }
  415.  
  416.  
  417. inline void TRect::SetNull() {
  418.   left = top = right = bottom = 0;
  419. }
  420.  
  421. inline void TRect::Set(int _left, int _top, int _right, int _bottom) {
  422.   left = _left;
  423.   top = _top;
  424.   right = _right;
  425.   bottom = _bottom;
  426. }
  427.  
  428. inline TRect::TRect(const RECT far& rect) {
  429.   *(RECT far*)this = rect;
  430. }
  431.  
  432. inline TRect::TRect(int _left, int _top, int _right, int _bottom) {
  433.   Set(_left, _top, _right, _bottom);
  434. }
  435.  
  436. inline TRect::TRect(const TPoint& topLeft, const TPoint& bottomRight) {
  437.   Set(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
  438. }
  439.  
  440. inline TRect::TRect(const TPoint& origin, const TSize& extent) {
  441.   Set(origin.x, origin.y, origin.x+extent.cx, origin.y+extent.cy);
  442. }
  443.  
  444. inline BOOL TRect::IsEmpty() const {
  445.   return left>=right || top>=bottom;
  446. }
  447.  
  448. inline BOOL TRect::IsNull() const {
  449.   return !left && !right && !top && !bottom;
  450. }
  451.  
  452. inline BOOL TRect::operator ==(const TRect& other) const {
  453.   return other.left==left && other.top==top
  454.        && other.right==right && other.bottom==bottom;
  455. }
  456.  
  457. inline BOOL TRect::operator !=(const TRect& other) const {
  458.   return !(other==*this);
  459. }
  460.  
  461. inline BOOL TRect::Contains(const TPoint& point) const {
  462.   return point.x >= left && point.x < right
  463.        && point.y >= top && point.y < bottom;
  464. }
  465.  
  466. inline BOOL TRect::Contains(const TRect& other) const {
  467.   return other.left >= left && other.right <= right
  468.        && other.top >= top && other.bottom <= bottom;
  469. }
  470.  
  471. inline BOOL TRect::Touches(const TRect& other) const {
  472.   return other.right > left && other.left < right
  473.        && other.bottom > top && other.top < bottom;
  474. }
  475.  
  476. inline TRect TRect::OffsetBy(int dx, int dy) const {
  477.   return TRect(left+dx, top+dy, right+dx, bottom+dy);
  478. }
  479.  
  480. inline TRect TRect::operator +(const TSize& size) const {
  481.   return OffsetBy(size.cx, size.cy);
  482. }
  483.  
  484. inline TRect TRect::operator -(const TSize& size) const {
  485.   return OffsetBy(-size.cx, -size.cy);
  486. }
  487.  
  488. inline TRect TRect::InflatedBy(int dx, int dy) const {
  489.   return TRect(left-dx, top-dy, right+dx, bottom+dy);
  490. }
  491.  
  492. inline TRect TRect::InflatedBy(const TSize& size) const {
  493.   return InflatedBy(size.cx, size.cy);
  494. }
  495.  
  496. inline TRect TRect::Normalized() const {
  497.   return TRect(Min(left, right), Min(top, bottom),
  498.                Max(left, right), Max(top, bottom));
  499. }
  500.  
  501. inline TRect TRect::operator &(const TRect& other) const {
  502.   return TRect(Max(left, other.left), Max(top, other.top),
  503.                Min(right, other.right), Min(bottom, other.bottom));
  504. }
  505.  
  506. inline TRect TRect::operator |(const TRect& other) const {
  507.   return TRect(Min(left, other.left), Min(top, other.top),
  508.                Max(right, other.right), Max(bottom, other.bottom));
  509. }
  510.  
  511. inline TRect& TRect::operator +=(const TSize& delta) {
  512.   Offset(delta.cx, delta.cy);
  513.   return *this;
  514. }
  515.  
  516. inline TRect& TRect::operator -=(const TSize& delta) {
  517.   return *this += -delta;
  518. }
  519.  
  520. inline TRect& TRect::Inflate(const TSize& delta) {
  521.   return Inflate(delta.cx, delta.cy);
  522. }
  523.  
  524. #endif  // __OWL_POINT_H
  525.