home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc Source Code / Utilities / Interfaces / AltPoly.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-22  |  7.8 KB  |  278 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        AltPoly.h
  3.  
  4.     Contains:    OpenDoc polygon: optional C++ savvy classes
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     
  11.     
  12.     
  13.     Theory of Operation:
  14.     
  15.         This is an alternate definition of ODPolygon and ODContour. The data format is
  16.         identical, but the structs defined here have a lot of useful methods including
  17.         constructors, accessors and conversion operators.
  18.         
  19.         To use these instead of the regular structs defined in Polygon.h, just include
  20.         this header file _before_ Polygon.h. An easy way to do this is to include it
  21.         first.
  22.         
  23.         QuickDraw GX users take note:
  24.         ODContour is identical in data format to a gxPolygon.
  25.         ODPolygonData (the data stored inside an ODPolygon) is identical in data
  26.             format to a gxPolygons <sic>.
  27.         See <GXTypes.h>.
  28.     
  29.         ** The way things are done has changed since A6. SOM wants all variable-sized
  30.         structures to adhere to a common data forma, where a small struct points to
  31.         the data and stores its size. This is so DSOM can tell how to copy the data
  32.         across an address-space boundary. The ODPolygon structure has most of the same
  33.         methods as before, but you can now create them directly. However, to get the
  34.         actual polygon data you'll need to call the GetData method. **
  35.     
  36.     In Progress:
  37.         
  38. */
  39.  
  40.  
  41. #ifndef _ALTPOLY_
  42. #define _ALTPOLY_
  43.  
  44. #ifdef SOM_Module_OpenDoc_Polygon_defined
  45.     #error "Must include AltPoly.h *before* Polygon.xh!"
  46. #else
  47.     /* Make sure Polygon.xh does NOT get included later! */
  48.     #define SOM_Module_OpenDoc_Polygon_defined 2
  49. #endif
  50.  
  51. #ifndef _ODTYPES_
  52. #include "ODTypes.h"
  53. #endif
  54.  
  55. #ifndef _EXCEPT_
  56. #include <Except.h>                    /* For Destructo, used by ODTempPolygon */
  57. #endif
  58.  
  59. #include <stddef.h>                    /* for size_t */
  60.  
  61. #if _PLATFORM_MACINTOSH_
  62.     #ifndef __QUICKDRAW__
  63.     #include <QuickDraw.h>                /* for Region and Polygon types */
  64.     #endif
  65.     #ifndef __FIXMATH__
  66.     #include <FixMath.h>                /* Must include before GX headers... */
  67.     #endif
  68.     #ifndef __GXTYPES__
  69.     #include <GXTypes.h>                /* for gxShape type */
  70.     #endif
  71. #endif
  72.  
  73.  
  74. //==============================================================================
  75. // Classes used in this interface
  76. //==============================================================================
  77.  
  78. struct ODRect;
  79. class ODStorageUnit;
  80. class ODTransform;
  81.  
  82. //==============================================================================
  83. // ODContour
  84. //==============================================================================
  85.  
  86. struct ODContour
  87. {
  88.     public:
  89.     
  90.     ODSLong    nVertices;
  91.     ODPoint    vertex[1];        // Array size is actually nVertices
  92.     
  93.     ODContour* NextContour( )                const    {return (ODContour*)&vertex[nVertices];}
  94.     ODBoolean    IsRectangular( )            const;
  95.     ODBoolean    AsRectangle( ODRect* )        const;
  96. #if _PLATFORM_MACINTOSH_
  97.     PolyHandle    AsQDPolygon( )                const;
  98.     ODBoolean    HasExactRegion( )            const;
  99. #endif
  100.     
  101.     ODBoolean    operator== ( const ODContour& )            const;
  102.     ODBoolean    operator!= ( const ODContour &c )        const    {return !(*this==c);}
  103. };
  104.  
  105. //==============================================================================
  106. // ODPolygonData
  107. //==============================================================================
  108.  
  109. struct ODPolygonData {
  110.     ODSLong    nContours;                        // Number of contours
  111.     ODContour    firstContour;                // Rest of contours follow after first
  112. };
  113.  
  114. //==============================================================================
  115. // ODPolygon
  116. //==============================================================================
  117.  
  118. class ODPolygon
  119. {
  120.     public:
  121.     
  122.                     ODPolygon( );
  123. #if ODDebug
  124.                    ~ODPolygon( );        // Delete myself, but not data
  125. #endif
  126.     
  127.     void            Delete( );            // Delete myself & my data
  128.     void            Clear( );            // Just deletes my data
  129.     
  130.     // ACCESSORS:
  131.     
  132.     ODBoolean        HasData( )                        const    {return _length!=0;}
  133.     ODULong            GetDataSize( )                    const    {return _length;}
  134.     ODPolygonData*    GetData( )                        const    {return _buf;}
  135.     
  136.     void            SetData( const ODPolygonData* );    // Does not copy the data!
  137.     
  138.     ODSLong            GetNContours( )                    const;
  139.     ODContour*        FirstContour( );
  140.     const ODContour*FirstContour( )                    const;
  141.     
  142.     // GEOMETRY:
  143.     
  144.     void        ComputeBoundingBox( ODRect* )        const;
  145.     ODBoolean    IsRectangular( )                    const;
  146.     void        Transform( Environment*, ODTransform* );
  147.     
  148.     ODBoolean    operator== ( ODPolygon& )            const;
  149.     ODBoolean    operator!= ( ODPolygon& p )            const    {return !(*this==p);}
  150.     
  151.     ODSLong    Contains( ODPoint )                        const;
  152.     ODBoolean    IsEmpty( )                            const;
  153.     
  154.     // CONVERSIONS:
  155.     
  156.     ODBoolean    AsRectangle( ODRect* )                const;    // False if nonrectangular
  157. #if _PLATFORM_MACINTOSH_
  158.     ODBoolean    HasExactRegion( )                    const;
  159.     RgnHandle    AsQDRegion( )                        const;
  160.     gxShape        AsGXShape( )                        const;
  161. #endif
  162.     
  163.     // ALLOCATION:
  164.     
  165.     ODPolygon*    SetNVertices( ODSLong nVertices );
  166.     ODPolygon*    SetVertices( ODSLong nVertices, const ODPoint *vertices );
  167.     ODPolygon*    SetContours( ODSLong nContours, const ODSLong *contourVertices );
  168.     ODPolygon*    SetRect( const ODRect& );
  169.  
  170.     ODPolygon*    Copy( )                                const;
  171.     ODPolygon*    CopyFrom( const ODPolygon& );
  172. #if _PLATFORM_MACINTOSH_
  173.     ODPolygon*    CopyFrom( gxShape );        // Accepts rect, polygon(s), path(s)
  174. #endif
  175.     ODPolygon*    MoveFrom( ODPolygon& );        // Justs adjusts pointers, no copying
  176.     
  177.     // INPUT/OUTPUT:
  178.     
  179.     ODPolygon*    ReadFrom( Environment*, ODStorageUnit* );
  180.     ODPolygon*    WriteTo( Environment*, ODStorageUnit* )        const;
  181.     
  182.     private:
  183.     
  184.     void        Realloc( ODULong dataSize );
  185.     
  186.     // DATA MEMBERS:
  187.     
  188.     unsigned long _maximum;                        // Exact same data as an ODByteArray
  189.     unsigned long _length;
  190.     ODPolygonData *_buf;
  191. };
  192.  
  193.  
  194. //==============================================================================
  195. // ODTempPolygon
  196. //==============================================================================
  197.  
  198. /*    ODTempPolygon is a polygon whose destructor disposes of its data.
  199.     This is useful if you have a local variable that's a temporary polygon
  200.     and you want to make sure the data gets disposed.
  201.     This _is_ exception-safe: inheriting from Destructo guarantees that the
  202.     data will be cleaned up even if an exception is thrown.
  203. */
  204.  
  205. class ODTempPolygon :public ODPolygon, Destructo
  206. {
  207. public:
  208.     ODTempPolygon( );
  209.    ~ODTempPolygon( );
  210. };
  211.  
  212.  
  213. /*    ODTempPolygonPtr is a _pointer_ to a polygon, whose destructor deletes
  214.     the polygon structure (and its data.) Yes, it is a class, but due to the
  215.     magic of operator overloading it can be used just as a pointer. See the
  216.     implementation of ODPolygon::Copy in AltPoly.cpp for an example. */
  217.  
  218. class ODTempPolygonPtr :Destructo
  219. {
  220. public:
  221.     ODTempPolygonPtr( );
  222.     ODTempPolygonPtr( ODPolygon* );
  223.     ~ODTempPolygonPtr( );
  224.     operator ODPolygon* ( )                    {return fPoly;}
  225.     ODPolygon* operator-> ( )                {return fPoly;}
  226.     ODPolygon* operator= ( ODPolygon *p )    {return (fPoly=p);}
  227.     ODPolygon* DontDelete( )                {ODPolygon* temp=fPoly; fPoly=kODNULL; return temp;}
  228.     
  229. private:
  230.     ODPolygon *fPoly;
  231. };
  232.  
  233.  
  234. /*    TempGXShape is a temporary reference to a gxShape. It will be released when
  235.     the reference goes out of scope. For an example, see the implementation of
  236.     ODPolygon::CopyFrom( gxShape ). */
  237.  
  238. class TempGXShape :Destructo
  239. {
  240. public:
  241.     TempGXShape( );
  242.     TempGXShape( gxShape );
  243.     ~TempGXShape( );
  244.     operator gxShape ( )                {return fShape;}
  245.     gxShape operator= ( gxShape s )        {return (fShape=s);}
  246.     gxShape DontRelease( )                {gxShape temp=fShape; fShape=kODNULL; return temp;}
  247.     
  248. private:
  249.     gxShape fShape;
  250. };
  251.  
  252.  
  253. //==============================================================================
  254. // Polygon Edge Iterator
  255. //==============================================================================
  256.  
  257. class PolyEdgeIterator {
  258.     public:
  259.  
  260.     PolyEdgeIterator( const ODPolygon* );
  261.     
  262.     void        CurrentEdge( const ODPoint* &v1, const ODPoint* &v2 );
  263.     const ODContour* CurrentContour( )            {return fCurContour;}
  264.     long        CurrentContourIndex( )            {return fCurContourIndex;}
  265.     long        CurrentEdgeIndex( )                {return fCurVertex;}
  266.     
  267.     ODBoolean    Next( );
  268.     ODBoolean    IsNotComplete( );
  269.     
  270.     private:
  271.     const ODPolygon*    const fPoly;
  272.     const ODContour*          fCurContour;
  273.     long                      fCurContourIndex;
  274.     long                      fCurVertex;
  275. };
  276.  
  277.  
  278. #endif //_ALTPOLY_