home *** CD-ROM | disk | FTP | other *** search
/ Nebula / nebula.bin / SourceCode / MiniExamples / Rotato / MyImage.m < prev    next >
Text File  |  1991-05-12  |  3KB  |  116 lines

  1. /*  
  2.  * 
  3.  * MyImage.m    -- How to rotate an NXImage
  4.  * 
  5.  * This is a subclass of NXImage that supports rotation by overriding 
  6.  * drawRepresentation:inRect:
  7.  *
  8.  * You may freely copy, distribute, and reuse the code in this example.
  9.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  10.  * fitness for any particular use.
  11.  *
  12.  * Written by Henry Krempel -- NeXT Developer Support
  13.  *
  14.  * Wed Apr 10 17:39:50 1991
  15.  */
  16. #import "MyImage.h"
  17. #import <appkit/NXImage.h>
  18. #import <appkit/View.h>
  19. #import <dpsclient/wraps.h>
  20. #import <math.h>
  21. #import "wraps.h"
  22.  
  23.  
  24. @implementation MyImage
  25.  
  26.  
  27. - initFromFile:(const char *)fileName
  28. {
  29.     [super initFromFile:fileName];
  30.     [self getSize:&origSize];
  31.     rotSize.width=origSize.height;
  32.     rotSize.height=origSize.width;
  33.     currentSize=&origSize;
  34.     flipped=0;
  35.     return self;
  36. }
  37.  
  38. - initFromSection:(const char *)imageName
  39. {
  40.     [super initFromSection:imageName];
  41.     [self getSize:&origSize];
  42.     rotSize.width=origSize.height;
  43.     rotSize.height=origSize.width;
  44.     currentSize=&origSize;
  45.     flipped=0;
  46.     return self;
  47. }
  48.  
  49. /*
  50.  *
  51.  * drawRepresentation:inRect: override.  Any changes to the PostScript 
  52.  * transformation matrix can take effect here. Compositing normally ignores
  53.  * these changes,  but we are doing the transformation before the offscreen 
  54.  * NXImage is rendered,  then compositing this into our visible window.
  55.  *
  56.  */
  57.  
  58. - (BOOL)drawRepresentation:(NXImageRep *)imageRep inRect:(const NXRect *)rect
  59. {
  60.     if (NXDrawingStatus == NX_DRAWING) {    // Don't do this when printing
  61.     PSgsave();
  62.     NXSetColor (NX_COLORCLEAR);
  63.     NXRectFill (rect);
  64.     PSgrestore();
  65.     }
  66.     
  67.   /*
  68.    * The wrap below translates and rotates to the appropriate starting point,
  69.    * based on the rotation state (0 1 2 3) and the angle
  70.    */
  71.      
  72.     PSW_transformToFit(rotation, rotation*90, origSize.width, origSize.height);
  73.     
  74.     if (flipped) PSW_flipit(origSize.width, origSize.height);
  75.             
  76.     return [super drawRepresentation:imageRep inRect:rect];
  77. }
  78.  
  79. /*
  80.  * Set the rotation of the image.  This will be one of 4 values 0..3 for the 
  81.  * four rotations we do here.  We only support these four since the size of
  82.  * these rectangles is easy to calculate.
  83.  *
  84.  */
  85.  
  86. - setRotation:(int)anInt
  87. {
  88.     NXSize *size;
  89.     rotation = anInt;
  90.     
  91.   /*
  92.    * Handles 2 rectangles, the original and the sideways (rotSize).
  93.    */
  94.      
  95.     if ( (rotation % 2) == 0 ) size=&origSize;
  96.     else size=&rotSize;
  97.     
  98.     if (currentSize != size) {
  99.         [self setSize:size];
  100.         [self recache];
  101.         currentSize=size;
  102.         }
  103.     
  104.     return self;
  105. }
  106.  
  107. - flip:sender
  108. {
  109.     flipped = (flipped+1) % 2;
  110.     [self recache];
  111.     return sender;
  112. }
  113.  
  114. @end
  115.  
  116.