home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / odtlktv4.zip / ODTLKT / TOOLKIT / BETA / SAMPLES / OPENDOC / PUBUTILS / ALTPOLY.H < prev    next >
Text File  |  1995-11-29  |  8KB  |  233 lines

  1. /********************************************************************/
  2. /*  Licensed Materials - Property of IBM                            */
  3. /*                                                                  */
  4. /*                                                                  */
  5. /* Copyright (C) International Business Machines Corp., 1994.       */
  6. /* Copyright (C) Apple Computer, Inc., 1994                         */
  7. /*                                                                  */
  8. /*  US Government Users Restricted Rights -                         */
  9. /*  Use, duplication, or disclosure restricted                      */
  10. /*  by GSA ADP Schedule Contract with IBM Corp.                     */
  11. /*                                                                  */
  12. /* File:    AltPoly.h                                               */
  13. /*                                                                  */
  14. /* Contains:  OpenDoc polygon: optional C++ savvy classes           */
  15. /*                                                                  */
  16. /* Theory of Operation:                                             */
  17. /*                                                                  */
  18. /*  This is an alternate definition of ODPolygon and ODContour.     */
  19. /*  The data format is identical, but the structs defined here      */
  20. /*  have a lot of useful methods including constructors, accessors  */
  21. /*  and conversion operators.                                       */
  22. /*                                                                  */
  23. /*  To use these instead of the regular structs defined in          */
  24. /*  Polygon.h, just include this header file _before_ Polygon.h.    */
  25. /*  An easy way to do this is to include it first.                  */
  26. /*                                                                  */
  27. /********************************************************************/
  28. #ifndef _ALTPOLY_
  29. #define _ALTPOLY_
  30.  
  31. #ifndef INCL_ODDTS // include non-DTS C++ headers
  32.  
  33. // Make sure that built-in structs do not get re-defined
  34. #ifndef SOM_Module_Polygon_OpenDoc_Polygon_defined
  35.   #define SOM_Module_Polygon_OpenDoc_Polygon_defined 2
  36. #else
  37.   #error "Must include AltPoly.h *before* os2.h!"
  38. #endif
  39.  
  40. #else // include DTS C++ headers
  41.  
  42. // Make sure that built-in structs do not get re-defined
  43. #ifndef _DTS_HH_INCLUDED_Polygon
  44.   #define _DTS_HH_INCLUDED_Polygon 2
  45. #else
  46.   #error "Must include AltPoly.h *before* os2.h!"
  47. #endif
  48.  
  49. #endif // ! INCL_ODDTS
  50.  
  51. #ifndef _ALTPOINT_
  52. #include "AltPoint.h"
  53. #endif
  54.  
  55. #include <stddef.h>          // for size_t
  56.  
  57. //==============================================================================
  58. // Classes used in this interface
  59. //==============================================================================
  60.  
  61. struct ODRect;
  62. class ODStorageUnit;
  63. class ODTransform;
  64. typedef struct _POLYGON POLYGON;    // defined in pmgpi.h
  65.  
  66. //==============================================================================
  67. // ODContour
  68. //==============================================================================
  69.  
  70. struct ODContour
  71. {
  72.   public:
  73.   
  74.   ODSLong  nVertices;
  75.   ODPoint  vertex[1];    // Array size is actually nVertices
  76.   
  77.   ODContour* NextContour( )        const  {return (ODContour*)&vertex[nVertices];}
  78.   ODBoolean  IsRectangular( )      const;
  79.   ODBoolean  AsRectangle( ODRect* )    const;
  80.   void AsPOLYGON( POLYGON& )        const;
  81.   ODBoolean  HasExactRegion( )      const;
  82.  
  83.   ODBoolean  operator== ( const ODContour& )      const;
  84.   ODBoolean  operator!= ( const ODContour &c )      const  {return !(*this==c);}
  85. };
  86.  
  87. //==============================================================================
  88. // ODPolygonData
  89. //==============================================================================
  90.  
  91. struct ODPolygonData {
  92.   ODSLong  nContours;            // Number of contours
  93.   ODContour  firstContour;        // Rest of contours follow after first
  94. };
  95.  
  96. //==============================================================================
  97. // ODPolygon
  98. //==============================================================================
  99.  
  100. class ODPolygon
  101. {
  102.   public:
  103.   
  104.   ODPolygon( );
  105. //~ODPolygon ( );          // IBM CSet++ doesn't treat ODPolygons exactly the same
  106.                            // as ODByteArrays when used as a return value if the 
  107.                            // class has a destructor.
  108.  
  109.   void      Delete( );      // Delete myself & my data
  110.   void      Clear( );      // Just deletes my data
  111.   
  112.   // ACCESSORS:
  113.   
  114.   ODBoolean    HasData( )            const  {return _length!=0;}
  115.   ODULong      GetDataSize( )          const  {return _length;}
  116.   ODPolygonData*  GetData( )            const  {return _buf;}
  117.   
  118.   void      SetData( const ODPolygonData* );  // Does not copy the data!
  119.   
  120.   ODSLong      GetNContours( )          const;
  121.   ODContour*    FirstContour( );
  122.   const ODContour*FirstContour( )          const;
  123.   
  124.   // GEOMETRY:
  125.   
  126.   void    ComputeBoundingBox( ODRect* )    const;
  127.   ODBoolean  IsRectangular( )          const;
  128.   void    Transform( Environment*, ODTransform* );
  129.   
  130.   ODBoolean  operator== ( ODPolygon& )      const;
  131.   ODBoolean  operator!= ( ODPolygon& p )      const  {return !(*this==p);}
  132.   
  133.   ODSLong  Contains( ODPoint )            const;
  134.   ODBoolean  IsEmpty( )              const;
  135.   
  136.   // CONVERSIONS:
  137.   
  138.   ODBoolean  AsRectangle( ODRect* )        const;  // False if nonrectangular
  139.   HRGN       AsRegion( HPS hps ) const;
  140.   ODBoolean  HasExactRegion( )          const;
  141.  
  142.   // ALLOCATION:
  143.   
  144.   ODPolygon*  SetNVertices( ODSLong nVertices );
  145.   ODPolygon*  SetVertices( ODSLong nVertices, const ODPoint *vertices );
  146.   ODPolygon*  SetContours( ODSLong nContours, const ODSLong *contourVertices );
  147.   ODPolygon*  SetRect( const ODRect& );
  148.  
  149.   ODPolygon*  Copy( )                const;
  150.   ODPolygon*  CopyFrom( const ODPolygon& );
  151.   ODPolygon*  MoveFrom( ODPolygon& );    // Justs adjusts pointers, no copying
  152.   
  153.   // INPUT/OUTPUT:
  154.   
  155.   ODPolygon*  ReadFrom( Environment*, ODStorageUnit* );
  156.   ODPolygon*  WriteTo( Environment*, ODStorageUnit* )    const;
  157.   
  158.   private:
  159.   
  160.   void    Realloc( ODULong dataSize );
  161.   
  162.   // DATA MEMBERS:
  163.   
  164.     unsigned long _maximum;            // Exact same data as an ODByteArray
  165.     unsigned long _length;
  166.     ODPolygonData *_buf;
  167. };
  168.  
  169.  
  170. //==============================================================================
  171. // ODTempPolygon
  172. //==============================================================================
  173.  
  174. /*  ODTempPolygon is a polygon whose destructor disposes of its data.
  175.   This is useful if you have a local variable that's a temporary polygon
  176.   and you want to make sure the data gets disposed.
  177. */
  178.  
  179. class ODTempPolygon :public ODPolygon
  180. {
  181. public:
  182.   ODTempPolygon( )  { }          // Just to avoid warnings
  183.    ~ODTempPolygon( )  {this->Clear();}
  184. };
  185.  
  186. /*  ODTempPolygonPtr is a _pointer_ to a polygon, whose destructor deletes
  187.   the polygon structure (and its data.) Yes, it is a class, but due to the
  188.   magic of operator overloading it can be used just as a pointer. See the
  189.   implementation of ODPolygon::Copy in AltPoly.cpp for an example. */
  190.  
  191. class ODTempPolygonPtr
  192. {
  193. public:
  194.   ODTempPolygonPtr( );
  195.   ODTempPolygonPtr( ODPolygon* );
  196.   ~ODTempPolygonPtr( );
  197.   operator ODPolygon* ( )          {return fPoly;}
  198.   ODPolygon* operator-> ( )        {return fPoly;}
  199.   ODPolygon* operator= ( ODPolygon *p )  {return (fPoly=p);}
  200.   ODPolygon* DontDelete( )        {ODPolygon* temp=fPoly; fPoly=kODNULL; return temp;}
  201.   
  202. private:
  203.   ODPolygon *fPoly;
  204. };
  205.  
  206.  
  207. //==============================================================================
  208. // Polygon Edge Iterator
  209. //==============================================================================
  210.  
  211. class PolyEdgeIterator {
  212.   public:
  213.  
  214.   PolyEdgeIterator( const ODPolygon* );
  215.   
  216.   void    CurrentEdge( const ODPoint* &v1, const ODPoint* &v2 );
  217.   const ODContour* CurrentContour( )      {return fCurContour;}
  218.   long    CurrentContourIndex( )      {return fCurContourIndex;}
  219.   long    CurrentEdgeIndex( )        {return fCurVertex;}
  220.   
  221.   ODBoolean  Next( );
  222.   ODBoolean  IsNotComplete( );
  223.   
  224.   private:
  225.   const ODPolygon*  const fPoly;
  226.   const ODContour*      fCurContour;
  227.   long            fCurContourIndex;
  228.   long            fCurVertex;
  229. };
  230.  
  231.  
  232. #endif //_ALTPOLY_
  233.