home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / OSL_INC.PAK / GEOMETRY.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  16KB  |  521 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectSupport
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Classes for geometric encapsulation
  6. //----------------------------------------------------------------------------
  7. #if !defined(OSL_GEOMETRY_H)
  8. #define OSL_GEOMETRY_H
  9.  
  10. #if !defined(OSL_DEFS_H)
  11. # include <osl/defs.h>
  12. #endif
  13. #if !defined(OSL_INLINES_H)
  14. # include <osl/inlines.h>
  15. #endif
  16.  
  17. #if !defined(CLASSLIB_OBJSTRM_H)  //!CQ move this to osl/streaming
  18. # include <classlib/objstrm.h>
  19. #endif
  20.  
  21. class TSize;
  22. class _BIDSCLASS TRect;
  23.  
  24. //
  25. // Geometry C-structs compatible with MSW
  26. //
  27. #if !defined(BI_PLAT_MSW)
  28. struct tagPOINT {
  29.   int x;
  30.   int y;
  31. };
  32. typedef struct tagPOINT POINT;
  33.  
  34. struct tagSIZE {
  35.   int cx;
  36.   int cy;
  37. };
  38. typedef struct tagSIZE SIZE;
  39.  
  40. struct tagRECT {
  41.   int left;
  42.   int top;
  43.   int right;
  44.   int bottom;
  45. };
  46. typedef struct tagRECT RECT;
  47. #endif
  48.  
  49. //
  50. // class TPoint
  51. // ----- ------
  52. //
  53. class  TPoint : public tagPOINT {
  54.   public:
  55.     // Constructors
  56.     TPoint() {}
  57.     TPoint(int _x, int _y) {x = _x; y = _y;}
  58.     TPoint(const POINT far& point) {x = point.x; y = point.y;}
  59.     TPoint(const TPoint far& point) {x = point.x; y = point.y;}
  60.     TPoint(const SIZE far& size) {x = size.cx; y = size.cy;}
  61.     TPoint(uint32 dw) {x = int16(LOWORD(dw)); y = int16(HIWORD(dw));}
  62.  
  63.     // Information functions/operators
  64.     bool        operator ==(const TPoint& other) const;
  65.     bool        operator !=(const TPoint& other) const;
  66.  
  67.     // Functions/binary-operators that return points or sizes
  68.     TPoint      OffsetBy(int dx, int dy) const {return TPoint(x+dx, y+dy);}
  69.     TPoint      operator +(const TSize& size) const;
  70.     TSize       operator -(const TPoint& point) const;
  71.     TPoint      operator -(const TSize& size) const;
  72.     TPoint      operator -() const {return TPoint(-x, -y);}
  73.  
  74.     // Functions/assignement-operators that modify this point
  75.     TPoint&     Offset(int dx, int dy);
  76.     TPoint&     operator +=(const TSize& size);
  77.     TPoint&     operator -=(const TSize& size);
  78. };
  79. inline ipstream& operator >>(ipstream& is, TPoint& p)
  80.   { return is >> p.x >> p.y; }
  81. inline opstream& operator <<(opstream& os, const TPoint& p)
  82.   { return os << p.x << p.y; }
  83. inline ostream& operator <<(ostream& os, const TPoint& p)
  84.   { return os << '(' << p.x << ',' << p.y << ')'; }
  85. inline istream& operator >>(istream& is, TPoint& p)
  86.   { char c; return is >> c >> p.x >> c >> p.y >> c; }
  87.  
  88. //
  89. // class TSize
  90. // ----- -----
  91. //
  92. class  TSize : public tagSIZE {
  93.   public:
  94.     // Constructors
  95.     TSize() {}
  96.     TSize(int dx, int dy) {cx = dx; cy = dy;}
  97.     TSize(const POINT far& point) {cx = point.x; cy = point.y;}
  98.     TSize(const SIZE far& size) {cx = size.cx; cy = size.cy;}
  99.     TSize(const TSize far& size) {cx = size.cx; cy = size.cy;}
  100.     TSize(uint32 dw) {cx = LOWORD(dw); cy = HIWORD(dw);}
  101.  
  102.     // Information functions/operators
  103.     bool        operator ==(const TSize& other) const;
  104.     bool        operator !=(const TSize& other) const;
  105.     int         Magnitude() const;
  106.  
  107.     // Functions/binary-operators that return sizes
  108.     TSize      operator +(const TSize& size) const;
  109.     TSize      operator -(const TSize& size) const;
  110.     TSize      operator -() const {return TSize(-cx, -cy);}
  111.  
  112.     // Functions/assignement-operators that modify this size
  113.     TSize&     operator +=(const TSize& size);
  114.     TSize&     operator -=(const TSize& size);
  115. };
  116. inline ipstream& operator >>(ipstream& is, TSize& s)
  117.   { return is >> s.cx >> s.cy; }
  118. inline opstream& operator <<(opstream& os, const TSize& s)
  119.   { return os << s.cx << s.cy; }
  120. inline ostream& operator <<(ostream& os, const TSize& s)
  121.   { return os << '(' << s.cx << 'x' << s.cy << ')'; }
  122.  
  123. //
  124. // class TRect
  125. // ----- -----
  126. //
  127. class _BIDSCLASS TRect : public tagRECT {
  128.   public:
  129.     // Constructors
  130.     TRect() {}
  131.     TRect(const RECT far& rect);
  132.     TRect(const TRect far& rect);
  133.     TRect(int _left, int _top, int _right, int _bottom);
  134.     TRect(const TPoint& upLeft, const TPoint& loRight);
  135.     TRect(const TPoint& origin, const TSize& extent);
  136.  
  137.     // (re)Initializers
  138.     void        SetNull();
  139.     void        SetEmpty() {SetNull();}
  140.     void        Set(int _left, int _top, int _right, int _bottom);
  141.  
  142.     // Type Conversion operators
  143.     operator    const TPoint*() const {return (const TPoint*)this;}
  144.     operator    TPoint*() {return (TPoint*)this;}
  145.  
  146.     // Testing functions
  147.     bool        IsEmpty() const;
  148.     bool        IsNull() const;
  149.     bool        operator ==(const TRect& other) const;
  150.     bool        operator !=(const TRect& other) const;
  151.  
  152.     // Information/access functions(const and non-const)
  153.     const TPoint& TopLeft() const {return *(TPoint*)&left;}
  154.     TPoint&     TopLeft() {return *(TPoint*)&left;}
  155.     TPoint      TopRight() const {return TPoint(right, top);}
  156.     TPoint      BottomLeft() const {return TPoint(left, bottom);}
  157.     const TPoint& BottomRight() const {return *(TPoint*)&right;}
  158.     TPoint&     BottomRight() {return *(TPoint*)&right;}
  159.     int         Width() const {return right-left;}
  160.     int         Height() const {return bottom-top;}
  161.     TSize       Size() const {return TSize(Width(), Height());}
  162.     long        Area() const {return long(Width())*long(Height());}
  163.  
  164.     bool        Contains(const TPoint& point) const;
  165.     bool        Contains(const TRect& other) const;
  166.     bool        Touches(const TRect& other) const;
  167.     TRect       OffsetBy(int dx, int dy) const;
  168.     TRect       operator +(const TSize& size) const;
  169.     TRect       operator -(const TSize& size) const;
  170.     TRect       InflatedBy(int dx, int dy) const;
  171.     TRect       InflatedBy(const TSize& size) const;
  172.     TRect       Normalized() const;
  173.     TRect       operator &(const TRect& other) const;
  174.     TRect       operator |(const TRect& other) const;
  175.  
  176.     // Manipulation functions/operators
  177.     TRect&      Normalize();
  178.     TRect&      Offset(int dx, int dy);
  179.     TRect&      operator +=(const TSize& delta);
  180.     TRect&      operator -=(const TSize& delta);
  181.     TRect&      Inflate(int dx, int dy);
  182.     TRect&      Inflate(const TSize& delta);
  183.     TRect&      operator &=(const TRect& other);
  184.     TRect&      operator |=(const TRect& other);
  185.  
  186. };
  187. ipstream& _BIDSFUNC operator >>(ipstream& is, TRect& r);
  188. opstream& _BIDSFUNC operator <<(opstream& os, const TRect& r);
  189. ostream& _BIDSFUNC operator <<(ostream& os, const TRect& r);
  190.  
  191. //
  192. // class TResId
  193. // ----- ------
  194. //
  195. // Resource Id class that can be constructed from a integer or string resource
  196. // identifier.
  197. //
  198. class TResId {
  199.   public:
  200.     TResId() : Id(0) {}
  201.     TResId(const char far* resString) : Id(resString) {}
  202.     TResId(int resNum) : Id((const char far*)uint32(unsigned(resNum))) {}
  203.     operator char far*() {return (char far*)Id;}
  204.     bool IsString() const {return ToBool(HIWORD(Id));}
  205.  
  206.   private:
  207.     const char far*  Id;
  208.  
  209.   friend ipstream& _BIDSFUNC operator >>(ipstream& is, TResId& id);
  210.   friend opstream& _BIDSFUNC operator <<(opstream& os, const TResId& id);
  211.   friend ostream& _BIDSFUNC  operator <<(ostream& os, const TResId& id);
  212. };
  213.  
  214. #if defined(BI_PLAT_MSW)
  215.  
  216. //
  217. // class TDropInfo
  218. // ----- ---------
  219. //
  220. class TDropInfo {
  221.   public:
  222.     TDropInfo(HDROP handle) : Handle(handle) {}
  223.  
  224.     operator HDROP() {return Handle;}
  225.  
  226.     uint DragQueryFile(uint index, char far* name, uint nameLen)
  227.            {return ::DragQueryFile(Handle, index, name, nameLen);}
  228.     uint DragQueryFileCount() {return ::DragQueryFile(Handle, -1, 0, 0);}
  229.     uint DragQueryFileNameLen(uint index)
  230.            {return ::DragQueryFile(Handle, index, 0, 0);}
  231.     bool DragQueryPoint(TPoint& point)
  232.            {return ::DragQueryPoint(Handle, &point);}
  233.     void DragFinish() {::DragFinish(Handle);}
  234.  
  235.   private:
  236.     HDROP  Handle;
  237. };
  238.  
  239. //
  240. // class TProcInstance
  241. // ----- -------------
  242. //
  243. class TProcInstance {
  244.   public:
  245.     #if defined(BI_PLAT_WIN16)
  246.       TProcInstance(FARPROC p) {Instance = ::MakeProcInstance(p, _hInstance);}
  247.      ~TProcInstance() {::FreeProcInstance(Instance);}
  248.     #else
  249.       TProcInstance(FARPROC p) {Instance = FARPROC(p);}
  250.     #endif
  251.    
  252.     operator FARPROC() {return Instance;}
  253.  
  254.   private:
  255.     FARPROC Instance;
  256. };
  257. #endif  // BI_PLAT_MSW
  258.  
  259. //
  260. // class TPointer
  261. // ----- --------
  262. //
  263. template<class T> class TPointerBase {
  264.   public:
  265.     T&   operator  *() {return *P;}
  266.          operator T*() {return P;}
  267.          operator T&() {return *P;}
  268.     int  operator  !() {return P==0;}
  269.     void operator  ~() {P = 0;}
  270.  
  271.   protected:
  272.     TPointerBase(T* pointer) : P(pointer) {}
  273.    ~TPointerBase() {delete P;}
  274.     TPointerBase() : P(0){}
  275.     T* P;
  276.  
  277.   private:
  278.     void* operator new(size_t) {return 0;}  // prohibit use of new
  279.     void operator delete(void* p) {((TPointerBase<T>*)p)->P=0;}
  280. };
  281.  
  282. template<class T> class TPointer : public TPointerBase<T> {
  283.   public:
  284.     TPointer() : TPointerBase<T>(){}
  285.     TPointer(T* pointer) : TPointerBase<T>(pointer) {}
  286.     T* operator =(T* src) {delete P; return P = src;}
  287.     T* operator =(const TPointer<T>& src)
  288.                           {delete P; return P = src.P;}
  289.     T* operator->() {return P;} // should throw exception if P==0
  290. };
  291.  
  292. class TPointer<char> : public TPointerBase<char> {
  293.   public:
  294.     TPointer() : TPointerBase<char>(){}
  295.     TPointer(char* pointer) : TPointerBase<char>(pointer) {}
  296.     char* operator =(char* src) {delete P; return P = src;}
  297.     char* operator =(const TPointer<char>& src)
  298.                                 {delete P; return P = src.P;}
  299.     char& operator[](int i) {return P[i];}
  300. };
  301.  
  302. //
  303. // class TCmdLine
  304. // ----- --------
  305. // Command line argument processing class, processes in the form:
  306. //  <Name> | {-/}<Option>[{:=}<Value>] ...
  307. //
  308. class _BIDSCLASS TCmdLine {
  309.   public:
  310.     enum TKind {
  311.       Start,   // no tokens have been parsed yet
  312.       Name,    // name type token, has no leading / or -
  313.       Option,  // option type token. leading / or - skipped by Token
  314.       Value,   // value for name or option. leading : or = skipped by Token
  315.       Done     // no more tokens
  316.     };
  317.     TCmdLine(const char far* cmdLine);
  318.    ~TCmdLine();
  319.  
  320.     TKind NextToken(bool removeCurrent=false);
  321.     const char* GetLine() const {return Buffer;}
  322.     void Reset();
  323.  
  324.     TKind Kind;       // Kind of current token
  325.     char* Token;      // ptr to current token. (Not 0-terminated)
  326.     int   TokenLen;   // length of current token
  327.  
  328.   private:
  329.     char* Buffer;     // command line buffer
  330.     char* TokenStart; // actual start of current token
  331. };
  332.  
  333. //----------------------------------------------------------------------------
  334. // Inlines
  335. //----------------------------------------------------------------------------
  336.  
  337. inline bool TPoint::operator ==(const TPoint& other) const {
  338.   return ToBool(other.x==x && other.y==y);
  339. }
  340.  
  341. inline bool TPoint::operator !=(const TPoint& other) const {
  342.   return ToBool(other.x!=x || other.y!=y);
  343. }
  344.  
  345. inline TPoint TPoint::operator +(const TSize& size) const {
  346.   return TPoint(x+size.cx, y+size.cy);
  347. }
  348.  
  349. inline TSize TPoint::operator -(const TPoint& point) const {
  350.   return TSize(x-point.x, y-point.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 TPoint& TPoint::Offset(int dx, int dy) {
  358.   x += dx;
  359.   y += dy;
  360.   return *this;
  361. }
  362.  
  363. inline TPoint& TPoint::operator +=(const TSize& size) {
  364.   x += size.cx;
  365.   y += size.cy;
  366.   return *this;
  367. }
  368.  
  369. inline TPoint& TPoint::operator -=(const TSize& size) {
  370.   x -= size.cx;
  371.   y -= size.cy;
  372.   return *this;
  373. }
  374.  
  375.  
  376. inline bool TSize::operator ==(const TSize& other) const {
  377.   return ToBool(other.cx==cx && other.cy==cy);
  378. }
  379.  
  380. inline bool TSize::operator !=(const TSize& other) const {
  381.   return ToBool(other.cx!=cx || other.cy!=cy);
  382. }
  383.  
  384. inline int TSize::Magnitude() const {
  385.   return Sqrt(long(cx)*long(cx)+long(cy)*long(cy));
  386. }
  387.  
  388. inline TSize TSize::operator +(const TSize& size) const {
  389.   return TSize(cx+size.cx, cy+size.cy);
  390. }
  391.  
  392. inline TSize TSize::operator -(const TSize& size) const {
  393.   return TSize(cx-size.cx, cy-size.cy);
  394. }
  395.  
  396. inline TSize& TSize::operator +=(const TSize& size) {
  397.   cx += size.cx;
  398.   cy += size.cy;
  399.   return *this;
  400. }
  401.  
  402. inline TSize& TSize::operator -=(const TSize& size) {
  403.   cx -= size.cx;
  404.   cy -= size.cy;
  405.   return *this;
  406. }
  407.  
  408.  
  409. inline void TRect::SetNull() {
  410.   left = top = right = bottom = 0;
  411. }
  412.  
  413. inline void TRect::Set(int _left, int _top, int _right, int _bottom) {
  414.   left = _left;
  415.   top = _top;
  416.   right = _right;
  417.   bottom = _bottom;
  418. }
  419.  
  420. inline TRect::TRect(const RECT far& rect) {
  421.   *(RECT far*)this = rect;
  422. }
  423.  
  424. inline TRect::TRect(const TRect far& rect) {
  425.   *(RECT far*)this = *(RECT far*)▭
  426. }
  427.  
  428. inline TRect::TRect(int _left, int _top, int _right, int _bottom) {
  429.   Set(_left, _top, _right, _bottom);
  430. }
  431.  
  432. inline TRect::TRect(const TPoint& topLeft, const TPoint& bottomRight) {
  433.   Set(topLeft.x, topLeft.y, bottomRight.x, bottomRight.y);
  434. }
  435.  
  436. inline TRect::TRect(const TPoint& origin, const TSize& extent) {
  437.   Set(origin.x, origin.y, origin.x+extent.cx, origin.y+extent.cy);
  438. }
  439.  
  440. inline bool TRect::IsEmpty() const {
  441.   return ToBool(left>=right || top>=bottom);
  442. }
  443.  
  444. inline bool TRect::IsNull() const {
  445.   return ToBool(!left && !right && !top && !bottom);
  446. }
  447.  
  448. inline bool TRect::operator ==(const TRect& other) const {
  449.   return ToBool(other.left==left   && other.top==top
  450.              && other.right==right && other.bottom==bottom);
  451. }
  452.  
  453. inline bool TRect::operator !=(const TRect& other) const {
  454.   return ToBool(!(other==*this));
  455. }
  456.  
  457. inline bool TRect::Contains(const TPoint& point) const {
  458.   return ToBool(point.x >= left && point.x < right
  459.              && point.y >= top  && point.y < bottom);
  460. }
  461.  
  462. inline bool TRect::Contains(const TRect& other) const {
  463.   return ToBool(other.left >= left && other.right <= right
  464.              && other.top >= top   && other.bottom <= bottom);
  465. }
  466.  
  467. inline bool TRect::Touches(const TRect& other) const {
  468.   return ToBool(other.right > left && other.left < right
  469.              && other.bottom > top && other.top < bottom);
  470. }
  471.  
  472. inline TRect TRect::OffsetBy(int dx, int dy) const {
  473.   return TRect(left+dx, top+dy, right+dx, bottom+dy);
  474. }
  475.  
  476. inline TRect TRect::operator +(const TSize& size) const {
  477.   return OffsetBy(size.cx, size.cy);
  478. }
  479.  
  480. inline TRect TRect::operator -(const TSize& size) const {
  481.   return OffsetBy(-size.cx, -size.cy);
  482. }
  483.  
  484. inline TRect TRect::InflatedBy(int dx, int dy) const {
  485.   return TRect(left-dx, top-dy, right+dx, bottom+dy);
  486. }
  487.  
  488. inline TRect TRect::InflatedBy(const TSize& size) const {
  489.   return InflatedBy(size.cx, size.cy);
  490. }
  491.  
  492. inline TRect TRect::Normalized() const {
  493.   return TRect(Min(left, right), Min(top, bottom),
  494.                Max(left, right), Max(top, bottom));
  495. }
  496.  
  497. inline TRect TRect::operator &(const TRect& other) const {
  498.   return TRect(Max(left, other.left), Max(top, other.top),
  499.                Min(right, other.right), Min(bottom, other.bottom));
  500. }
  501.  
  502. inline TRect TRect::operator |(const TRect& other) const {
  503.   return TRect(Min(left, other.left), Min(top, other.top),
  504.                Max(right, other.right), Max(bottom, other.bottom));
  505. }
  506.  
  507. inline TRect& TRect::operator +=(const TSize& delta) {
  508.   Offset(delta.cx, delta.cy);
  509.   return *this;
  510. }
  511.  
  512. inline TRect& TRect::operator -=(const TSize& delta) {
  513.   return *this += -delta;
  514. }
  515.  
  516. inline TRect& TRect::Inflate(const TSize& delta) {
  517.   return Inflate(delta.cx, delta.cy);
  518. }
  519.  
  520. #endif  // OSL_GEOMETRY_H
  521.