home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.0 / NeXTSTEP3.0.iso / NextDeveloper / Examples / AppKit / BusyBox / AnimalView.m < prev    next >
Text File  |  1992-05-28  |  4KB  |  139 lines

  1.  
  2. #import "AnimalView.h"
  3. #import <appkit/NXBitmapImageRep.h>
  4. #import <appkit/Control.h>
  5. #import <sys/param.h>
  6.  
  7. /* 
  8.  * AnimalView.m, a simple view to display (%XBitmapImageRep.  
  9.  * Subclass of view that draws an NXBitmapImageRep.  Knows how 
  10.  * to flip and rotate itself in response to IB controls.    
  11.  * NXBitmapImageRep provides the functionality to deal with images 
  12.  * that are stored as bitmaps.  The instances of this class are created by 
  13.  * loading TIFF data.  In 1.0, I would have read bitmap data (from TIFF 
  14.  * file or Mach-O) and imaged it using NXImageBitmap(). It works out much 
  15.  * cleaner using NXBitmapImageRep.
  16.  *
  17.  * Author: Julie Zelenski, NeXT Developer Support
  18.  * You may freely copy, distribute and reuse the code in this example.  
  19.  * NeXT disclaims any warranty of any kind, expressed or implied, as to 
  20.  * its fitness for any particular use.
  21.  */
  22.  
  23. @implementation AnimalView:View
  24.  
  25. /* INIT/FREE METHODS */
  26.  
  27. - initFrame:(NXRect *)frameRect;
  28. /* Init method for newly created animal view. It initializes the scaling
  29.  * factors to normal, and then grabs the duck for the initial image.
  30.  */
  31. {
  32.     [super initFrame:frameRect];
  33.     scaleFactor.x = scaleFactor.y = 1.0;
  34.     [self setImageToAnimal:"Duck"];
  35.     return self;
  36. }
  37.    
  38. - free;
  39. /* Free the image on our way out.
  40.  */
  41. {
  42.     [image free];
  43.     return [super free];
  44. }
  45.  
  46.  
  47. /* TARGET/ACTION METHODS */
  48.  
  49. - rotateLeft:sender
  50. /* Rotates the view 90 degrees to the left and redisplays.
  51.  */
  52. {  
  53.     [self rotate:(scaleFactor.x*scaleFactor.y)*90.0];
  54.     [self display];
  55.     return self;
  56. }
  57.  
  58. - rotateRight:sender
  59. /* Rotates the view 90 degrees to the right and redisplays.
  60.  */
  61. {
  62.     [self rotate:(scaleFactor.x*scaleFactor.y)*-90.0];
  63.     [self display];
  64.     return self;
  65. }
  66.  
  67. - flipHorizontal:sender
  68. /* Mirrors the view across vertical axis, and updates scale factor to 
  69.  * indicate that.  Since rotation can change what is currently vertical, 
  70.  * need to rotate to zero, do the scaling, and restore rotation.
  71.  */ 
  72. {   
  73.     float angle;
  74.     
  75.     angle = [self boundsAngle];
  76.     scaleFactor.x *= -1;
  77.     [self rotate:-1*angle];
  78.     [self scale:-1.0 :1.0];
  79.     [self rotate:angle];
  80.     [self display];
  81.     return self;
  82. }
  83.  
  84. - flipVertical:sender
  85. /* Mirrors the view across horizontal axis, and updates scale factor to 
  86.  * indicate that.  Since rotation can change what is currently horizontal, 
  87.  * need to rotate to zero, do the scaling, and restore rotation.
  88.  */ 
  89. {
  90.     float angle;
  91.     
  92.     angle = [self boundsAngle];
  93.     scaleFactor.y *= -1;
  94.     [self r(&e:-1*angle];
  95.     [self scale:1.0 :-1.0];
  96.     [self rotate:angle];
  97.     [self display];
  98.     return self;
  99. }
  100.  
  101. - changeAnimal:sender
  102. /* Target/Action for IB control.  Changes to display image with name of
  103.  * cell title.
  104.  */
  105. {   
  106.     [self setImageToAnimal:[[sender selectedCell] title]];
  107.     return self;
  108. }
  109.  
  110.  
  111.  
  112. /* PRIVATE METHODS */
  113.  
  114. - setImageToAnimal:(const char *)imageName;
  115. /* Find the bitmap image rep for the specified name, sets the scaling
  116.  * and rotation back to "normal" and displays new image.
  117.  */
  118. {   char filename[MAXPATHLEN];
  119.  
  120.     sprintf(filename,"%s.tiff",imageName);
  121.     if (image) [image free];
  122.     image = [[NXBitmapImageRep alloc] initFromSection:filename];
  123.     [self setDrawRotation:0.0];
  124.     [self scale:scaleFactor.x :scaleFactor.y];
  125.     scaleFactor.x = scaleFactor.y = 1.0;
  126.     [self display];
  127.     return self;
  128. }
  129.  
  130. - drawSelf:(NXRect *)rects :(int)rectCount;
  131. /* Clears the background and has the image draw itself 
  132.  */
  133. {
  134.    NXEraseRect(&bounds);  /* to be sure to clear background */
  135.    [image drawIn:&bounds];
  136.    return self;
  137. }
  138.  
  139. @end