/* InspectStat.h		by Paul Kunz	December 1991
 * Controls binding of the plot axes with the columns of the tuple.
 *
 * Copyright (C)  1991  The Board of Trustees of
 * The Leland Stanford Junior University.  All Rights Reserved.
 */

#import "InspectStat.h"

const char InspectStat_h_rcsid[] = INSPECTSTAT_H_ID;
const char InspectStat_m_rcsid[] = "$Id: InspectStat.m,v 1.10 1992/04/21 00:53:13 pfkeb Rel $";

#import <appkit/Application.h>
#import <appkit/Box.h>
#import <appkit/Matrix.h>
#import <appkit/TextField.h>

#import "HGraphicView.h"
#import "NewInspector.h"
#import "Plot.h"

#import "hippo.h"

@implementation InspectStat

- initInspFor:aDraw
{    
    [super initInspFor:aDraw];
    
    [NXApp loadNibSection:"InspectStat.nib" owner:self
           withNames:NO fromZone:[self zone]];
    [theInspector addView:[contentBox contentView]
                  withName:"Plot Statistics" withSupervisor:self];
    return self;
}

- createImage:sender
{
    id		statView;
    NXStream 	*s;
    NXRect	rect, prect;
    
    statView = [statBox contentView];
    
    s = NXOpenMemory(NULL, 0, NX_READWRITE);
    if (s) {
    	[statView getFrame:&rect];
    	[statView copyPSCodeInside:&rect to:s]; 
    }
    NXSeek(s, 0L, NX_FROMSTART);
    [selectedPlot getBounds:&prect];
    [graphicView loadImageFromStream:s at:&prect.origin allowAlpha:YES];
    NXCloseMemory(s, NX_FREEBUFFER);
    return self;
}
/* Methods for updating Inspector */
- updateView
{    
    [self showStats];
    return self;
}
- updateEmptySelection
{
    [self clearStats];
    return self;
}
- updateMultiSelection
{
    selectedPlot = nil;
    [self clearStats];
    return self;
}
- clearStats
{
    int		c, r;
    
    for( c = 0; c < 3; c++ ) {
        for( r = 0; r < 3; r++ ) {
	    [[statMatrix cellAt:r :c] setStringValue:""];
	}
    }
    [statMatrix setEnabled:NO];
    return self;
}
- showStats
{
    id		aCell;
    display	disp;
    graphtype_t	graphType;
    int		c, r;

    selectedPlot = [graphicView firstPlot];
    if ( !selectedPlot ) {
        [self clearStats];
        return self;
    }
    disp = [selectedPlot histDisplay];
    graphType = h_getDispType( disp );

    [statMatrix setEnabled:YES];
    if (graphType == HISTOGRAM)
    {
	 for ( r = 0; r < 3; r++ ) {
		for ( c = 0; c < 3; c++ ) {
		    aCell = [statMatrix cellAt:r :c ];
		    if ( r == 1 ) {
		        [aCell setFloatValue:h_getTotal(disp,c,0) ];
		    } else {
		        [aCell setStringValue:""];
		    }
		}
	}
    }
    else
    {
	 for ( c = 0; c < 3; c++ )
	 {
	      for ( r = 0; r < 3; r++ )
	      {
		   aCell = [statMatrix cellAt:r :c];
		   [aCell setFloatValue:h_getTotal(disp,c,2-r) ];
	      }
	 }
    }

    return self;
}
    
@end
