//---------------------------------------------------------------------------- // ObjectSupport // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved // // Classes for geometric encapsulation //---------------------------------------------------------------------------- #if !defined(OSL_GEOMETRY_H) #define OSL_GEOMETRY_H #if !defined(OSL_DEFS_H) # include #endif #if !defined(OSL_INLINES_H) # include #endif #if !defined(CLASSLIB_OBJSTRM_H) //!CQ move this to osl/streaming # include #endif class TSize; class _BIDSCLASS TRect; // // Geometry C-structs compatible with MSW // #if !defined(BI_PLAT_MSW) struct tagPOINT { int x; int y; }; typedef struct tagPOINT POINT; struct tagSIZE { int cx; int cy; }; typedef struct tagSIZE SIZE; struct tagRECT { int left; int top; int right; int bottom; }; typedef struct tagRECT RECT; #endif // // class TPoint // ----- ------ // class TPoint : public tagPOINT { public: // Constructors TPoint() {} TPoint(int _x, int _y) {x = _x; y = _y;} TPoint(const POINT far& point) {x = point.x; y = point.y;} TPoint(const TPoint far& point) {x = point.x; y = point.y;} TPoint(const SIZE far& size) {x = size.cx; y = size.cy;} TPoint(uint32 dw) {x = int16(LOWORD(dw)); y = int16(HIWORD(dw));} // Information functions/operators bool operator ==(const TPoint& other) const; bool operator !=(const TPoint& other) const; // Functions/binary-operators that return points or sizes TPoint OffsetBy(int dx, int dy) const {return TPoint(x+dx, y+dy);} TPoint operator +(const TSize& size) const; TSize operator -(const TPoint& point) const; TPoint operator -(const TSize& size) const; TPoint operator -() const {return TPoint(-x, -y);} // Functions/assignement-operators that modify this point TPoint& Offset(int dx, int dy); TPoint& operator +=(const TSize& size); TPoint& operator -=(const TSize& size); }; inline ipstream& operator >>(ipstream& is, TPoint& p) { return is >> p.x >> p.y; } inline opstream& operator <<(opstream& os, const TPoint& p) { return os << p.x << p.y; } inline ostream& operator <<(ostream& os, const TPoint& p) { return os << '(' << p.x << ',' << p.y << ')'; } inline istream& operator >>(istream& is, TPoint& p) { char c; return is >> c >> p.x >> c >> p.y >> c; } // // class TSize // ----- ----- // class TSize : public tagSIZE { public: // Constructors TSize() {} TSize(int dx, int dy) {cx = dx; cy = dy;} TSize(const POINT far& point) {cx = point.x; cy = point.y;} TSize(const SIZE far& size) {cx = size.cx; cy = size.cy;} TSize(const TSize far& size) {cx = size.cx; cy = size.cy;} TSize(uint32 dw) {cx = LOWORD(dw); cy = HIWORD(dw);} // Information functions/operators bool operator ==(const TSize& other) const; bool operator !=(const TSize& other) const; int Magnitude() const; // Functions/binary-operators that return sizes TSize operator +(const TSize& size) const; TSize operator -(const TSize& size) const; TSize operator -() const {return TSize(-cx, -cy);} // Functions/assignement-operators that modify this size TSize& operator +=(const TSize& size); TSize& operator -=(const TSize& size); }; inline ipstream& operator >>(ipstream& is, TSize& s) { return is >> s.cx >> s.cy; } inline opstream& operator <<(opstream& os, const TSize& s) { return os << s.cx << s.cy; } inline ostream& operator <<(ostream& os, const TSize& s) { return os << '(' << s.cx << 'x' << s.cy << ')'; } // // class TRect // ----- ----- // class _BIDSCLASS TRect : public tagRECT { public: // Constructors TRect() {} TRect(const RECT far& rect); TRect(const TRect far& rect); TRect(int _left, int _top, int _right, int _bottom); TRect(const TPoint& upLeft, const TPoint& loRight); TRect(const TPoint& origin, const TSize& extent); // (re)Initializers void SetNull(); void SetEmpty() {SetNull();} void Set(int _left, int _top, int _right, int _bottom); // Type Conversion operators operator const TPoint*() const {return (const TPoint*)this;} operator TPoint*() {return (TPoint*)this;} // Testing functions bool IsEmpty() const; bool IsNull() const; bool operator ==(const TRect& other) const; bool operator !=(const TRect& other) const; // Information/access functions(const and non-const) const TPoint& TopLeft() const {return *(TPoint*)&left;} TPoint& TopLeft() {return *(TPoint*)&left;} TPoint TopRight() const {return TPoint(right, top);} TPoint BottomLeft() const {return TPoint(left, bottom);} const TPoint& BottomRight() const {return *(TPoint*)&right;} TPoint& BottomRight() {return *(TPoint*)&right;} int Width() const {return right-left;} int Height() const {return bottom-top;} TSize Size() const {return TSize(Width(), Height());} long Area() const {return long(Width())*long(Height());} bool Contains(const TPoint& point) const; bool Contains(const TRect& other) const; bool Touches(const TRect& other) const; TRect OffsetBy(int dx, int dy) const; TRect operator +(const TSize& size) const; TRect operator -(const TSize& size) const; TRect InflatedBy(int dx, int dy) const; TRect InflatedBy(const TSize& size) const; TRect Normalized() const; TRect operator &(const TRect& other) const; TRect operator |(const TRect& other) const; // Manipulation functions/operators TRect& Normalize(); TRect& Offset(int dx, int dy); TRect& operator +=(const TSize& delta); TRect& operator -=(const TSize& delta); TRect& Inflate(int dx, int dy); TRect& Inflate(const TSize& delta); TRect& operator &=(const TRect& other); TRect& operator |=(const TRect& other); }; ipstream& _BIDSFUNC operator >>(ipstream& is, TRect& r); opstream& _BIDSFUNC operator <<(opstream& os, const TRect& r); ostream& _BIDSFUNC operator <<(ostream& os, const TRect& r); // // class TResId // ----- ------ // // Resource Id class that can be constructed from a integer or string resource // identifier. // class TResId { public: TResId() : Id(0) {} TResId(const char far* resString) : Id(resString) {} TResId(int resNum) : Id((const char far*)uint32(unsigned(resNum))) {} operator char far*() {return (char far*)Id;} bool IsString() const {return ToBool(HIWORD(Id));} private: const char far* Id; friend ipstream& _BIDSFUNC operator >>(ipstream& is, TResId& id); friend opstream& _BIDSFUNC operator <<(opstream& os, const TResId& id); friend ostream& _BIDSFUNC operator <<(ostream& os, const TResId& id); }; #if defined(BI_PLAT_MSW) // // class TDropInfo // ----- --------- // class TDropInfo { public: TDropInfo(HDROP handle) : Handle(handle) {} operator HDROP() {return Handle;} uint DragQueryFile(uint index, char far* name, uint nameLen) {return ::DragQueryFile(Handle, index, name, nameLen);} uint DragQueryFileCount() {return ::DragQueryFile(Handle, -1, 0, 0);} uint DragQueryFileNameLen(uint index) {return ::DragQueryFile(Handle, index, 0, 0);} bool DragQueryPoint(TPoint& point) {return ::DragQueryPoint(Handle, &point);} void DragFinish() {::DragFinish(Handle);} private: HDROP Handle; }; // // class TProcInstance // ----- ------------- // class TProcInstance { public: #if defined(BI_PLAT_WIN16) TProcInstance(FARPROC p) {Instance = ::MakeProcInstance(p, _hInstance);} ~TProcInstance() {::FreeProcInstance(Instance);} #else TProcInstance(FARPROC p) {Instance = FARPROC(p);} #endif operator FARPROC() {return Instance;} private: FARPROC Instance; }; #endif // BI_PLAT_MSW // // class TPointer // ----- -------- // template class TPointerBase { public: T& operator *() {return *P;} operator T*() {return P;} operator T&() {return *P;} int operator !() {return P==0;} void operator ~() {P = 0;} protected: TPointerBase(T* pointer) : P(pointer) {} ~TPointerBase() {delete P;} TPointerBase() : P(0){} T* P; private: void* operator new(size_t) {return 0;} // prohibit use of new void operator delete(void* p) {((TPointerBase*)p)->P=0;} }; template class TPointer : public TPointerBase { public: TPointer() : TPointerBase(){} TPointer(T* pointer) : TPointerBase(pointer) {} T* operator =(T* src) {delete P; return P = src;} T* operator =(const TPointer& src) {delete P; return P = src.P;} T* operator->() {return P;} // should throw exception if P==0 }; class TPointer : public TPointerBase { public: TPointer() : TPointerBase(){} TPointer(char* pointer) : TPointerBase(pointer) {} char* operator =(char* src) {delete P; return P = src;} char* operator =(const TPointer& src) {delete P; return P = src.P;} char& operator[](int i) {return P[i];} }; // // class TCmdLine // ----- -------- // Command line argument processing class, processes in the form: // | {-/}