home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / BlobMgr / Library Folder / New.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-25  |  6.1 KB  |  308 lines  |  [TEXT/KAHL]

  1. # include    "BlobMgr.h"
  2.  
  3.  
  4. # define    New(typename)    (typename **) NewHandle ((Size) sizeof (typename))
  5.  
  6. /*
  7.  * Handle to list of all existing blob sets - empty initially
  8.  */
  9.  
  10. static BlobSetHandle    blobSetMaster = nil;
  11.  
  12.  
  13. /* ---------------------------------------------------------------------- */
  14. /*                        Object Creation Routines                         */
  15. /* ---------------------------------------------------------------------- */
  16.  
  17.  
  18.  
  19. pascal void
  20. NewBlobMatch (BlobHandle b1, BlobHandle b2)
  21. {
  22. MatchHandle    m;
  23.  
  24.     m = New (MatchRecord);
  25.     if (m != nil)
  26.     {
  27.         (**m).mBlob = b1;
  28.         (**m).nextMatch = (**b2).matches;
  29.         (**b2).matches = m;
  30.     }
  31. }
  32.  
  33.  
  34. /*
  35.  * Add a blob to (the END of) a blob set
  36.  */
  37.  
  38. pascal void
  39. AddBlob (BlobHandle b, BlobSetHandle bSet)
  40. {
  41.     if ((**bSet).firstBlob == (BlobHandle) nil)
  42.     {
  43.         (**bSet).firstBlob = b;
  44.         (**bSet).lastBlob = b;
  45.     }
  46.     else
  47.     {
  48.         (**((**bSet).lastBlob)).nextBlob = b;
  49.         (**bSet).lastBlob = b;
  50.     }
  51.     (**b).nextBlob = (BlobHandle) nil;
  52. }
  53.  
  54.  
  55. /*
  56.  * Create a new blob, adding it to the given set.    The blob is enabled
  57.  * or not according to the enable parameter.    The blob is given empty
  58.  * static and drag regions and rectangles, nil picture, match set, and
  59.  * glob handles.    The drawing modes are set according to statDraw
  60.  * and dragDraw.    The blob is set to be single-use or not by the
  61.  * glueMax parameter.    mustMatch determines whether the blob must
  62.  * have a glue blob to be matched.    The refCon is installed in the
  63.  * record.
  64.  */
  65.  
  66. pascal BlobHandle
  67. NewBlob (BlobSetHandle bSet,            /* set to add blob to */
  68.                     Boolean enable,        /* whether blob is enabled or not */
  69.                     short glueMVal,        /* maximum glue count */
  70.                     Boolean mustMatch,    /* whether must have glob to be matched */
  71.                     long refCon)        /* reference constant */
  72. {
  73. BlobHandle    bHand;
  74. BlobPtr        b;
  75. Rect        r;
  76.  
  77.     bHand = New (BlobRecord);
  78.     if (bHand != nil)
  79.     {
  80.         AddBlob (bHand, bSet);    /* sets nextBlob field */
  81.         b = *bHand;
  82.         b->flags = 0 | bPicMask;    /* clear flags, then set appropriate fields */
  83.         if (enable)
  84.             EnableBlob (bHand);
  85.         if (mustMatch)
  86.             SetBlobFlags (bHand, bNeedGlobMask);
  87.         b->bPicProc.bPic = nil;            /* no picture or drawing proc */
  88.         SetRect (&r, 0, 0, 0, 0);
  89.         b->statRect = r;                /* empty rects */
  90.         b->dragRect = r;
  91.         b->statRgn = NewRgn ();            /* empty regions */
  92.         b->dragRgn = NewRgn ();
  93.         b->glueMax = glueMVal;
  94.         b->glueCount = 0;
  95.         b->glob = nil;
  96.         b->matches = nil;
  97.         b->bRefCon = refCon;
  98.  
  99.     }
  100.     return (bHand);
  101. }
  102.  
  103.  
  104. /*
  105.  * Create a new, empty blob set.  Add it to the master blob set list.
  106.  * Return a handle to it.
  107.  */
  108.  
  109. pascal BlobSetHandle
  110. NewBlobSet (void)
  111. {
  112. BlobSetHandle    bSet;
  113.  
  114.     bSet = New (BlobSetRecord);
  115.     if (bSet != nil)
  116.     {
  117.         (**bSet).firstBlob = (BlobHandle) nil;    /* initialize to empty set */
  118.         (**bSet).lastBlob = (BlobHandle) nil;
  119.         (**bSet).nextBlobSet = blobSetMaster;    /* add to master list */
  120.         blobSetMaster = bSet;
  121.     }
  122.  
  123.     return (bSet);
  124. }
  125.  
  126.  
  127. /* ---------------------------------------------------------------------- */
  128. /*                         Object Disposal Routines                        */
  129. /* ---------------------------------------------------------------------- */
  130.  
  131.  
  132. pascal void
  133. DisposeBlobPic (BlobHandle b)
  134. {
  135.     if (PicBlob (b) && (**b).bPicProc.bPic != nil)
  136.     {
  137.         KillPicture ((**b).bPicProc.bPic);
  138.         (**b).bPicProc.bPic = nil;
  139.     }
  140. }
  141.  
  142.  
  143. pascal void
  144. DisposeBlobMatchSet (BlobHandle b)
  145. {
  146. MatchHandle    m1, m2;
  147.  
  148.     m1 = (**b).matches;
  149.     (**b).matches = nil;
  150.     for (;;)
  151.     {
  152.         if (m1 == nil) return;
  153.         m2 = (**m1).nextMatch;
  154.         DisposeHandle ((Handle) m1);
  155.         m1 = m2;
  156.     }
  157. }
  158.  
  159.  
  160. pascal void
  161. DisposeBlob (BlobHandle b)
  162. {
  163.     DisposeRgn ((**b).statRgn);
  164.     DisposeRgn ((**b).dragRgn);
  165.     DisposeBlobPic (b);
  166.     DisposeBlobMatchSet (b);
  167.     DisposeHandle ((Handle) b);
  168. }
  169.  
  170.  
  171. /*
  172.  * Remove blob set from the master blob set list, then dispose of
  173.  * everything in the list.    All handles to the set or elements in
  174.  * it become invalid.
  175.  */
  176.  
  177. pascal void
  178. DisposeBlobSet (BlobSetHandle bSet)
  179. {
  180. BlobHandle        b1, b2;
  181. BlobSetHandle    bSet2;
  182.  
  183.     /*
  184.      * Remove set from master list
  185.      */
  186.     if (bSet == blobSetMaster)    /* first set in list */
  187.         blobSetMaster = (**blobSetMaster).nextBlobSet;
  188.     else
  189.     {
  190.         bSet2 = blobSetMaster;
  191.         for (;;)
  192.         {
  193.             if (bSet2 == nil) return;    /* set isn't in master list */
  194.             if ((**bSet2).nextBlobSet == bSet)    /* found it */
  195.             {
  196.                 (**bSet2).nextBlobSet = (**bSet).nextBlobSet;
  197.                 break;
  198.             }
  199.             bSet2 = (**bSet2).nextBlobSet;        /* try next one */
  200.         }
  201.     }
  202.     /*
  203.      * Toss elements of set
  204.      */
  205.     b1 = (**bSet).firstBlob;
  206.     for (;;)
  207.     {
  208.         if (b1 == nil) break;
  209.         b2 = (**b1).nextBlob;
  210.         DisposeBlob (b1);
  211.         b1 = b2;
  212.     }
  213.     /*
  214.      * Toss set header
  215.      */
  216.     DisposeHandle ((Handle) bSet);
  217. }
  218.  
  219.  
  220. /*
  221.  * Dispose of all existing sets.    This is the routine to call at
  222.  * the end of the host program to dispose of all Blob Manager
  223.  * structures.
  224.  */
  225.  
  226. pascal void
  227. DisposeBlobSets (void)
  228. {
  229.     for (;;)
  230.     {
  231.         if (blobSetMaster == nil) break;    /* no more sets */
  232.         DisposeBlobSet (blobSetMaster);        /* changes the master list */
  233.     }
  234. }
  235.  
  236.  
  237. /*
  238.  * Delete a blob from a blob list.    Should not be done lightly, e.g.,
  239.  * if the blob is some other blob's glob.    Check to be sure that the
  240.  * current glueCount is zero first.    All handles to the blob become
  241.  * invalid.
  242.  */
  243.  
  244. pascal void
  245. ClobberBlob (BlobHandle b, BlobSetHandle bSet)
  246. {
  247. BlobHandle    b2;
  248.  
  249.     if ((**bSet).firstBlob == b)    /* special case - first in list */
  250.     {
  251.         (**bSet).firstBlob = (**b).nextBlob;
  252.         if ((**bSet).lastBlob == b)    /* if true, set is now empty */
  253.             (**bSet).lastBlob = nil;
  254.     }
  255.     else
  256.     {
  257.         b2 = (**bSet).firstBlob;
  258.         for (;;)
  259.         {
  260.             if (b2 == nil) return;    /* NOT break! */
  261.             if ((**b2).nextBlob == b)
  262.             {
  263.                 (**b2).nextBlob = (**b).nextBlob;
  264.                 if ((**bSet).lastBlob == b)        /* clobbering last one */
  265.                     (**bSet).lastBlob = b2;        /* back pointer up */
  266.                 break;
  267.             }
  268.             b2 = (**b2).nextBlob;
  269.         }
  270.     }
  271.     DisposeBlob (b);
  272. }
  273.  
  274.  
  275. /*
  276.  * Delete s from the match set of d
  277.  */
  278.  
  279. pascal void
  280. ClobberBlobMatch (BlobHandle b1, BlobHandle b2)
  281. {
  282. MatchHandle    m1, m2;
  283.  
  284.     m1 = (**b2).matches;
  285.     if (m1 != nil)        /* don't look if match set empty */
  286.     {
  287.         if ((**m1).mBlob == b1)    /* is it the first one in the list? */
  288.         {
  289.             (**b2).matches = (**m1).nextMatch;
  290.         }
  291.         else        /* it's not first one */
  292.         {
  293.             for (;;)
  294.             {
  295.                 m2 = (**m1).nextMatch;
  296.                 if (m2 == nil) return;        /* NOT break! */
  297.                 if ((**m2).mBlob == b1)     /* found it */
  298.                 {
  299.                     (**m1).nextMatch = (**m2).nextMatch;
  300.                     break;
  301.                 }
  302.                 m1 = m2;
  303.             }
  304.         }
  305.         DisposeHandle ((Handle) m2);
  306.     }
  307. }
  308.