home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Programming / GDBbundle-1.0-MIS / src / TextEdit / Info.bproj / InfoView.m < prev    next >
Encoding:
Text File  |  1997-04-01  |  4.6 KB  |  183 lines

  1. /* Author Cristian Cserveny, 1994.
  2.    Converted to the OpenStep version by Ovidiu Predescu, 1997.
  3. */
  4.  
  5. #import <AppKit/AppKit.h>
  6. #import "InfoView.h"
  7.  
  8. #define PERIOD        0.005
  9. #define TILES        16
  10. #define GENERATOR    14
  11. #define SMALLMARG    1
  12. #define BIGMARG        2
  13.  
  14. @implementation InfoView
  15.  
  16. - (void)awakeFromNib
  17. {
  18.   NSRect rect = [self bounds];
  19.   NSImage* appImage = [NSApp applicationIconImage];
  20.   NSSize size;
  21.  
  22.   images = [NSMutableArray new];
  23.   [images addObject:appImage];
  24.  
  25.   size = [appImage size];
  26.   point.x = (rect.size.width - size.width) / 2;
  27.   point.y = (rect.size.height - size.height) / 2;
  28.   xTile = (rect.size.width - SMALLMARG - BIGMARG) / TILES;
  29.   yTile = (rect.size.height - SMALLMARG - BIGMARG) / TILES;
  30.  
  31.   resultImage = [[NSImage alloc] initWithSize:rect.size];
  32.   [resultImage lockFocus];
  33.   PSsetgray(NSLightGray);
  34.   NSRectFill(rect);
  35.   rect.origin.x++;
  36.   rect.size.height--;
  37.   rect.size.width--;
  38.   PSsetgray(NSBlack);
  39.   NSRectFill(rect);
  40.   rect.origin.x--;
  41.   rect.origin.y++;
  42.   PSsetgray(NSWhite);
  43.   NSRectFill(rect);
  44.   [appImage compositeToPoint:point operation:NSCompositeSourceOver];
  45.   [resultImage unlockFocus];
  46.  
  47.   currentImageIndex = 1;
  48. }
  49.  
  50. - (void)dealloc
  51. {
  52.   [images release];
  53.   [resultImage release];
  54.   [super dealloc];
  55. }
  56.  
  57. - (void)showImageNumber:(int)index
  58. {
  59.   if (index < 0 || index >= [images count] || imagesToDisplay)
  60.     return;
  61.  
  62.   currentImageIndex = index;
  63.   tickNo = 1;
  64.   imagesToDisplay = 1;
  65.   [NSTimer scheduledTimerWithTimeInterval:PERIOD
  66.                                    target:self
  67.                                  selector:@selector(animateOneImage:)
  68.                                  userInfo:nil
  69.                                   repeats:YES];
  70. }
  71.  
  72. - (void)showAllImages
  73. {
  74.   if ([images count] <= 1 || imagesToDisplay)
  75.     return;
  76.  
  77.   imagesToDisplay = [images count];
  78.   tickNo = 1;
  79.   [NSTimer scheduledTimerWithTimeInterval:PERIOD
  80.                                    target:self
  81.                                  selector:@selector(animateAllImages:)
  82.                                  userInfo:nil
  83.                                   repeats:NO];
  84. }
  85.  
  86. - (void)mouseDown:(NSEvent*)event
  87. {
  88.   if (!imagesToDisplay)
  89.     [self showAllImages];
  90. }
  91.  
  92. - (void)addImageNamed:(NSString*)imageName
  93. {
  94.   NSBundle* bundle = [NSBundle bundleForClass:isa];
  95.   NSString* path = [bundle pathForResource:imageName ofType:@"tiff"];
  96.   NSImage* image = [[[NSImage alloc] initWithContentsOfFile:path]
  97.     autorelease];
  98.  
  99.   [self addImage:image];
  100. }
  101.  
  102. - (void)addImage:(NSImage*)image
  103. {
  104. //  NSRect rect = { NSZeroPoint, [image size] };
  105.  
  106.   [image lockFocus];
  107. //  PSsetgray(NSWhite);
  108. //  NSRectFill(rect);
  109.   [image compositeToPoint:NSZeroPoint operation:NSCompositeSourceOver];
  110.   [image unlockFocus];
  111.  
  112.   [images addObject:image];
  113. }
  114.  
  115. - (void)drawRect:(NSRect)frame
  116. {
  117.   [resultImage compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
  118. }
  119.  
  120. - (void)animateOneImage:(NSTimer*)timer
  121. {
  122.   id image = [images objectAtIndex:currentImageIndex];
  123.   NSRect rect;
  124.   NSPoint targetPoint;
  125.  
  126.   tickNo = tickNo * GENERATOR % (TILES * TILES + 1);
  127.   [resultImage lockFocus];
  128.   rect.origin.x = (tickNo % TILES) * xTile + SMALLMARG;
  129.   rect.origin.y = (tickNo / TILES) * yTile + BIGMARG;
  130.   rect.size.width = xTile;
  131.   rect.size.height = yTile;
  132.   targetPoint.x = point.x + rect.origin.x;
  133.   targetPoint.y = point.y + rect.origin.y;
  134.  
  135.   if(tickNo == 1) {
  136.     [timer invalidate];
  137.     tickNo = 0;
  138.     rect.origin = NSZeroPoint;
  139.     rect.size = [image size];
  140.     rect.origin = point;
  141.     PSsetgray(NSWhite);
  142.     NSRectFill(rect);
  143.     [image compositeToPoint:point operation:NSCompositeSourceOver];
  144.  
  145.     currentImageIndex++;
  146.     if (currentImageIndex == [images count])
  147.       currentImageIndex = 0;
  148.     imagesToDisplay--;
  149.   }
  150.   else
  151.     [image compositeToPoint:targetPoint
  152.                    fromRect:rect
  153.                   operation:NSCompositeSourceOver];
  154.  
  155.   [resultImage unlockFocus];
  156.   [self display];
  157. }
  158.  
  159. - (void)animateAllImages:(NSTimer*)timer
  160. {
  161.   if (tickNo)
  162.     [self animateOneImage:timer];
  163.  
  164.   /* Schedule a new timer if we still have to display parts of the current image. */
  165.   if (tickNo)
  166.     [NSTimer scheduledTimerWithTimeInterval:PERIOD
  167.                                      target:self
  168.                                    selector:@selector(animateAllImages:)
  169.                                    userInfo:nil
  170.                                     repeats:NO];
  171.   else if (imagesToDisplay) {
  172.     /* Setup a new timer after which we display the rest of images */
  173.     tickNo = 1;
  174.     [NSTimer scheduledTimerWithTimeInterval:1
  175.                                      target:self
  176.                                    selector:@selector(animateAllImages:)
  177.                                    userInfo:nil
  178.                                     repeats:NO];
  179.   }
  180. }
  181.  
  182. @end
  183.