home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Audio-DSP / NU / Source / BMList.m < prev    next >
Encoding:
Text File  |  1992-04-05  |  2.7 KB  |  126 lines

  1. #import "BMList.h"
  2. #import <objc/List.h>
  3. #import <strings.h>
  4. #import <appkit/NXImage.h>
  5. #import <dpsclient/psops.h>
  6. #import <dpsclient/wraps.h>
  7. #import "MenuManager.h" 
  8. // instances of this class represent
  9. // a single instance of an NXImage.
  10. // The class is used by BMShared_ to
  11. // hold data for shared bitmaps.  
  12. // Every time a bitmap is allocated,
  13. // it is kept in a list.  Class records
  14. // allow the list to be searched.
  15. // If a bitmap with the same className
  16. // and same size is found, it is returned,
  17. // rather than a new instance, and the
  18. // refCount is increased.  When an
  19. // instance is freed, its refcount
  20. // is decreased.  It is only "really"
  21. // freed if its refcount reaches 0.
  22.  
  23. static id BMList_ = nil ;
  24. @implementation BMList:Object
  25. { @public
  26.   NXSize size ;
  27.   char className[24] ;
  28.   id image ;
  29.   int refCount ;
  30. }
  31.  
  32. + show ;
  33. { // for verification: dump out BMList
  34.   int i, count ;
  35.   count = [BMList_ count] ;
  36.   for(i = 0 ; i < count ; i++)
  37.     [[BMList_ objectAt: i] show] ;
  38.   return self ;
  39. }
  40.  
  41. + BitMapList ;
  42. { // give access to the list of instances
  43.   return BMList_ ;
  44. }
  45.  
  46. + newForClass: (char *) aClass size: (NXSize *) aSize ;
  47. { int i, knt ;
  48.   BMList *aBM ;
  49.   if(!BMList_)
  50.   { BMList_ = [[List alloc] init] ;
  51.   }
  52.   knt = [BMList_ count] ;
  53.   for(i = 0 ; i < knt ; i++)
  54.   { // search for bitmap of same size and class
  55.     aBM = [BMList_ objectAt: i] ;
  56.     if( (aBM->size.width == aSize->width) &&
  57.         (aBM->size.height == aSize->height) &&
  58.         (!strcmp(aBM->className, aClass)) )
  59.     { // then we already have this bitMap
  60.       aBM->refCount++ ;
  61.       return aBM ;
  62.     }  
  63.   }
  64.   // if we get here, then must create new bitmap
  65.   return [[super alloc] initForClass: aClass size: aSize] ;
  66. }
  67.  
  68. - initForClass: (char *) aClass size: (NXSize *) aSize ;
  69. { // make a new, transparent bitmap
  70.   NXRect aRect = {{0.0,0.0},{0.0,0.0}} ;
  71.   [super init] ;
  72.   strcpy(className, aClass) ;
  73.   size = aRect.size = *aSize ;
  74.   refCount = 1 ;
  75.   image = [[NXImage alloc] initSize: &aRect.size] ;
  76.   [image lockFocus] ;
  77.   PSgsave() ;
  78.   NXEraseRect(&aRect) ;
  79.   PSsetalpha(0.0) ;
  80.   PSrectfill(0.0,0.0,size.width,size.height) ;
  81.   PSgrestore() ;
  82.   [image unlockFocus] ;
  83.   [BMList_ addObject: self] ;
  84.   return self ;
  85. }
  86.  
  87. - (char *) className ;
  88. { return className ;
  89. }
  90.  
  91. - free ;
  92. { // only "really" free if refCount == 0
  93.   refCount-- ;
  94.   if(refCount == 0)
  95.   { [BMList_ removeObject: self] ;
  96.     return [super free] ;
  97.   }
  98.   return self ;
  99. }
  100.  
  101. - image ;
  102. { return image ;
  103. }
  104.  
  105. - newRef ;
  106. { // increase my reference count
  107.   refCount++ ;
  108.   return self ;
  109. }
  110. - (int) refCount ;
  111. { return refCount ;
  112. }
  113.  
  114. - show ;
  115. { // print my state
  116.   [Nu printf: "classname = %s\n"
  117.       "size= %f %f\n"
  118.       "image= %s\n"
  119.       "refcount= %d\n\n",
  120.   className, size.width, size.height, image, refCount] ;
  121.   return self ;
  122. }
  123.  
  124. @end
  125.  
  126.