
/*
 * AIView.m
 *
 * AIView is a subclass of View responsible for managing the NXEPSImageRep
 * of the ai file and also updating the information fields on the display.
 *
 *
 * You may freely copy, distribute, and reuse the code in this example.
 * Both Terrence Talbot and Digital Tool Works disclaim any warranty
 * of any kind, expressed or implied, as to its fitness for any particular use.
 *
 * Written by:  Terrence Talbot 
 *    Created:  Oct/92
 *
 */

#import "AIView.h"
 
@implementation AIView:View

/* INIT/FREE METHODS */

- free;
/*
 * Free the image on our way out.
 */
{
    [image free];
    return [super free];
}

/* PRIVATE METHODS */

- setImageToFilename:(const char *)filename;
{
    if (image) [image free];
    image = [[NXEPSImageRep alloc] initFromSection:filename];
    [self display];
    
    return self;
}

- display
{
   char   aiScale[32];
   
   NXSetRect(&aiRect,0.0,0.0,0.0,0.0);
   aspectRatio = 0.0;
   imageScale = 0;
   
   [super display]; // calls drawSelf:: which sets aiRect and aspectRatio properly

   [widthField setFloatValue:aiRect.size.width];
   [heightField setFloatValue:aiRect.size.height];
   
   imageScale = floor(aspectRatio * 100);
   sprintf(aiScale,"%i%%",imageScale);
   [scaleField setStringValue:aiScale];

   return self;   
}
   
- drawSelf:(NXRect *)rects :(int)rectCount;
{
   NXRect imageRect;
   
   [image getBoundingBox:&aiRect];
   
   // here's a lot of stuff to preserve the aspect ratio and calculate the scale
   
   // if it's bigger than the bounds, bring it back to the bounds
   if ( (aiRect.size.width > bounds.size.width) && 
        (aiRect.size.height > bounds.size.height) ) {
      if ( (bounds.size.width / aiRect.size.width) < 
           (bounds.size.height / aiRect.size.height) ) {
         aspectRatio = (bounds.size.width / aiRect.size.width);
         NXSetRect(&imageRect,
            bounds.origin.x,
	    ((bounds.size.height - (aiRect.size.height * aspectRatio)) / 2),
	    bounds.size.width,
	    (aiRect.size.height * aspectRatio));
      }
      else {
         aspectRatio = (bounds.size.height / aiRect.size.height);
         NXSetRect(&imageRect,
            ((bounds.size.width - (aiRect.size.width * aspectRatio)) /2),
	    bounds.origin.y,
	    (aiRect.size.width * aspectRatio),
	    bounds.size.height);
      }
   }
   else // the width has to be brought back to the bounds and the height scaled accordingly
      if ( aiRect.size.width > bounds.size.width ) {
         aspectRatio = (bounds.size.width / aiRect.size.width);
         NXSetRect(&imageRect,
	    bounds.origin.x,
	    ((bounds.size.height - (aiRect.size.height * aspectRatio)) / 2),
	    bounds.size.width,
	    (aiRect.size.height * aspectRatio));
      }
      else // the height has to be brought back to the bounds and the width scaled accordingly
         if ( aiRect.size.height > bounds.size.height ) {
	    aspectRatio = (bounds.size.height / aiRect.size.height);
            NXSetRect(&imageRect,
               ((bounds.size.width - (aiRect.size.width * aspectRatio)) /2),
	       bounds.origin.y,
	       (aiRect.size.width * aspectRatio),
	       bounds.size.height);
	 }
	 else { // it's smaller than the bounds, so center the thing
	    aspectRatio = 1.0;
            NXSetRect(&imageRect,
	       ((bounds.size.width - aiRect.size.width)/2),
	       ((bounds.size.height - aiRect.size.height)/2),
	       aiRect.size.width,
	       aiRect.size.height);
	 }
      
   NXEraseRect(&bounds);  /* to be sure to clear background */

   [image drawIn:&imageRect];

   return self;
}

@end
