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

#import "InspectData.h"

const char InspectData_h_rcsid[] = INSPECTDATA_H_ID;
const char InspectData_m_rcsid[] = "$Id: InspectData.m,v 1.12 1992/04/05 18:19:44 pfkeb Rel $";

#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/ButtonCell.h>
#import <appkit/Form.h>
#import <appkit/Matrix.h>
#import <appkit/NXBrowser.h>
#import <appkit/NXBrowserCell.h>

#import "DrawDocument.h"
#import "HDraw.h"
#import "HGraphicView.h"
#import "InspectTuple.h"
#import "NewInspector.h"
#import "Plot.h"

#import <strings.h>

#define LOW_RANGE 0
#define HIGH_RANGE 1
#define LOW_X 0
#define LOW_Y 1
#define HIGH_X 2
#define HIGH_Y 3
#define MAX_VALUE 0
#define MIN_VALUE 1
#define SUM_VALUE 2
#define AX_X 0
#define AX_Y 1
#define AX_W 2
#define AX_XE 2
#define AX_YE 3

@implementation InspectData

- initInspFor:aDraw
{    
    int		i;
    
    [super initInspFor:aDraw];
    
    [NXApp loadNibSection:"InspectData.nib" owner:self
           withNames:NO fromZone:[self zone]];
    [theInspector addView:[contentBox contentView]
                  withName:"Data Selection" withSupervisor:self];
    currentDim = 1;
    for ( i = 0; i < 4; i++ ) {
        [[tpAxisForm cellAt:i :0] setEditable:NO];
    }
    return self;
}
- tupleAxisChanged:sender
{
    int		currentButton;
    
    currentButton = [tpAxisButtons selectedRow];

    if (graphtype == XYPLOT && currentButton >= 2)  currentButton += 1;
    tpBrowserCells = [tpBrowser matrixInColumn:0];
    [tpBrowserCells selectCellAt:tpAxisIndex[currentButton] :0 ];
    [tpBrowserCells scrollCellToVisible:tpAxisIndex[currentButton]  :0];
    return self;
}
- (int) currentTupleCol
{
    tpBrowserCells = [tpBrowser matrixInColumn:0];
    return [tpBrowserCells selectedRow ];
}
- tupleSetAxisDataDim:sender
{
    int		currentTupleN, currentButton;
    static int previousTupleN = -1, previousButton = -1;
    drawtype_t          drawtype;

    tpBrowserCells = [tpBrowser matrixInColumn:0];
    currentTupleN = [tpBrowserCells selectedRow ];
    currentButton = [tpAxisButtons selectedRow];
    [tpAxisForm setStringValue:
    [[tpBrowserCells cellAt:currentTupleN :0] stringValue] at:currentButton];
    switch( currentButton ) {
    case AX_X:
	[graphicView graphicsPerform:@selector(bindAxisX:)
		with :(id)&currentTupleN andDraw:YES];
    	tpAxisIndex[0] = currentTupleN;
	break;
    case AX_Y:
	[graphicView graphicsPerform:@selector(bindAxisY:)
		with :(id)&currentTupleN andDraw:YES];
    	tpAxisIndex[1] = currentTupleN;
	break;
    case AX_W:
	if (graphtype == XYPLOT) {
		if (currentTupleN == previousTupleN && currentButton == previousButton) {
			currentTupleN = -1;
			drawtype = POINT;
			[tpAxisForm setStringValue:"" at:AX_XE];
		}
		else drawtype = POINT + ERRBAR;

		[ graphicView graphicsPerform:@selector(setDrawType:)
			with: (id)&drawtype andDraw:NO ];
		[graphicView graphicsPerform:@selector(bindAxisXE:)
			with :(id)&currentTupleN andDraw:YES];
    		tpAxisIndex[3] = currentTupleN;
	} else {
		if (currentTupleN == previousTupleN && currentButton == previousButton) 
		{
			currentTupleN = -1;
			[tpAxisForm setStringValue:"" at:AX_W];
		}
		[graphicView graphicsPerform:@selector(bindAxisW:)
			with :(id)&currentTupleN andDraw:YES];
    		tpAxisIndex[2] = currentTupleN;
	}
	previousTupleN = currentTupleN;
	previousButton = currentButton;
	break;
    case AX_YE:
	if (currentTupleN == previousTupleN && currentButton == previousButton) {
		currentTupleN = -1;
		drawtype = POINT;
		[tpAxisForm setStringValue:"" at:AX_YE];
	}
	else drawtype = POINT + ERRBAR;

	[ graphicView graphicsPerform:@selector(setDrawType:)
		with: (id)&drawtype andDraw:NO ];
	[graphicView graphicsPerform:@selector(bindAxisYE:)
		with :(id)&currentTupleN andDraw:YES];
    	tpAxisIndex[4] = currentTupleN;
	previousTupleN = currentTupleN;
	previousButton = currentButton;
	break;
    }
    [[graphicView window] flushWindow];
    return self;
}

 /* Methods for updating Inspector */
- updateView
{
    id		inspectTuple;
    
    if ( firstPlot ) {
        [self setTuple:[firstPlot ntuple] ];
	[self showBinding];
    } else {
        inspectTuple = [hDraw inspectTuple];
	[self setTuple:[inspectTuple currentTuple]];
    }
    return self;
}
- setTuple:(ntuple) atuple
{    
        if ( selectedTuple == atuple ) return self;
    	selectedTuple = atuple;
	if ( selectedTuple == NULL ) return self;
        [tpBrowser loadColumnZero];
	if ( firstPlot ) {
	    [firstPlot getDispType:&graphtype];
	    if ( graphtype == XYPLOT ) {
	        [tpAxisButtons selectCellAt:AX_Y :0];
	    }
	}
	[tpAxisForm setStringValue:h_getNtLabel(selectedTuple,0) at:AX_X];
    	return self;
}
- showBinding
  /*
   * called when we click on a different plot -
   * load up the options panel to correspond to the selected plot
   */
{
    int 	row;
    display adisplay = [firstPlot histDisplay];
    
    graphtype = h_getDispType(adisplay);
    if (graphtype == HISTOGRAM)
	 currentDim = 1;
    else
	 currentDim = 2;
    
  /*
   * get the current bindings
   */
    tpAxisIndex[0] = h_getBinding(adisplay, XAXIS);
    tpAxisIndex[1] = h_getBinding(adisplay, YAXIS);
    tpAxisIndex[2] = h_getBinding(adisplay, WEIGHT);
    tpAxisIndex[3] = h_getBinding(adisplay, XERROR);
    tpAxisIndex[4] = h_getBinding(adisplay, YERROR);
	
 /*
  * enable buttons and show titles
  */
    [self setButtonsAndTitles];
 /*
  * click on the X Axis button as a starting default
  */
    row = [ tpAxisButtons selectedRow ];
    if ( row < 0 ) {
	[tpAxisButtons selectCellAt:AX_X :0];
    }
    if ( ![[tpAxisButtons cellAt:row :0] isEnabled] ) {
	[tpAxisButtons selectCellAt:AX_X :0];
    }
    
    [self tupleAxisChanged:self];
    return self;
}

- setButtonsAndTitles
{
    int                 tupleN;

 /*
  * clear all highlights 
  */
    tpBrowserCells = [tpBrowser matrixInColumn:0];

 /*
  * enable X cell always 
  */
    [[tpAxisButtons cellAt:AX_X :0] setEnabled:YES];
    [[tpAxisForm cellAt:AX_X :0] setTitle:"X Axis"];
    tupleN = tpAxisIndex[0];
    if ( selectedTuple && (tupleN >= 0) )
	[tpAxisForm setStringValue:h_getNtLabel(selectedTuple, tupleN)
		                at:AX_X];
    else
	[tpAxisForm setStringValue:"" at:AX_X];
    [tpAxisForm drawCellAt:AX_X :0];

    switch (currentDim) {
    case 1:
    /*
     * disable Y cell and remove form title 
     */
	[[tpAxisButtons cellAt:AX_Y :0] setEnabled:NO];
	[[tpAxisForm cellAt:AX_Y :0] setTitle:"      "];
	[tpAxisForm setStringValue:"" at:AX_Y];
	[tpAxisForm drawCellAt:AX_Y :0];
    /*
     * enable weight cell and add title (if tuple has >1 dimension) 
     */
	if (selectedTuple) {
	    if (h_getNtDim(selectedTuple) > 1) {
		[[tpAxisButtons cellAt:AX_W :0] setEnabled:YES];
		[[tpAxisForm cellAt:AX_W :0] setTitle:"Weight"];
		tupleN = tpAxisIndex[2];
		if (tupleN >= 0)
		    [tpAxisForm 
		        setStringValue:h_getNtLabel(selectedTuple, tupleN)
		                    at:AX_W];
		else
		    [tpAxisForm setStringValue:"" at:AX_W];
		[tpAxisForm drawCellAt:AX_W :0];


	    } else {
		[[tpAxisButtons cellAt:AX_W :0] setEnabled:NO];
		[[tpAxisForm cellAt:AX_W :0] setTitle:"      "];
		[tpAxisForm setStringValue:"" at:AX_W];
		[tpAxisForm drawCellAt:AX_W :0];
	    }
	} else {
	    [[tpAxisButtons cellAt:AX_W :0] setEnabled:NO];
	    [[tpAxisForm cellAt:AX_W :0] setTitle:"      "];
	    [tpAxisForm setStringValue:"" at:AX_W];
	    [tpAxisForm drawCellAt:AX_W :0];
	}
	    
    /*
     * disable Y error cell and remove title 
     */
	[[tpAxisButtons cellAt:AX_YE :0] setEnabled:NO];
	[[tpAxisForm cellAt:AX_YE :0] setTitle:"       "];
	[tpAxisForm setStringValue:"" at:AX_YE];
	[tpAxisForm drawCellAt:AX_YE :0];
	break;

    case 2:
    /*
     * enable Y cell and add form title 
     */
	[[tpAxisButtons cellAt:AX_Y :0] setEnabled:YES];
	[[tpAxisForm cellAt:AX_Y :0] setTitle:"Y Axis"];
	tupleN = tpAxisIndex[1];
	if ( selectedTuple && (tupleN >= 0) )
	    [tpAxisForm setStringValue:h_getNtLabel(selectedTuple, tupleN)
	     			    at:AX_Y];
	else
	    [tpAxisForm setStringValue:"" at:AX_Y];
	[tpAxisForm drawCellAt:AX_Y :0];

    /*
     * for the XYPLOT, we enable X Errors and Y Errors 
     */
	if (graphtype == XYPLOT || graphtype == STRIPCHART) {
	    [[tpAxisButtons cellAt:AX_XE :0] setEnabled:YES];
	    [[tpAxisForm cellAt:AX_XE :0] setTitle:"X Error"];
	    tupleN = tpAxisIndex[3];
	    if ( selectedTuple && (tupleN >= 0) )
		[tpAxisForm setStringValue:h_getNtLabel(selectedTuple, tupleN)
		 			at:AX_XE];
	    else
		[tpAxisForm setStringValue:"" at:AX_XE];
	    [tpAxisForm drawCellAt:AX_XE :0];


	    [[tpAxisButtons cellAt:AX_YE :0] setEnabled:YES];
	    [[tpAxisForm cellAt:AX_YE :0] setTitle:"Y Error"];
	    tupleN = tpAxisIndex[4];
	    if ( selectedTuple && (tupleN >= 0) )
		[tpAxisForm setStringValue:h_getNtLabel(selectedTuple, tupleN)
		 			at:AX_YE];
	    else
		[tpAxisForm setStringValue:"" at:AX_YE];
	    [tpAxisForm drawCellAt:AX_YE :0];
	}
    /*
     * for SCATTER plots we disable X Errors and Y Errors 
     */
	else if (graphtype == SCATTERPLOT) {
	    [[tpAxisButtons cellAt:AX_XE :0] setEnabled:NO];
	    [[tpAxisForm cellAt:AX_XE :0] setTitle:"       "];
	    [tpAxisForm setStringValue:"" at:AX_XE];
	    [tpAxisForm drawCellAt:AX_XE :0];

	    [[tpAxisButtons cellAt:AX_YE :0] setEnabled:NO];
	    [[tpAxisForm cellAt:AX_YE :0] setTitle:"       "];
	    [tpAxisForm setStringValue:"" at:AX_YE];
	    [tpAxisForm drawCellAt:AX_YE :0];
	}
    /*
     * for binned plots, we enable Weights and disable Y Errors 
     */
	else {
	    [[tpAxisButtons cellAt:AX_W :0] setEnabled:YES];
	    [[tpAxisForm cellAt:AX_W :0] setTitle:"Weight"];
	    tupleN = tpAxisIndex[2];
	    if ( selectedTuple && (tupleN >= 0) )
		[tpAxisForm setStringValue:h_getNtLabel(selectedTuple, tupleN)
		 			at:AX_W];
	    else
		[tpAxisForm setStringValue:"" at:AX_W];
	    [tpAxisForm drawCellAt:AX_W :0];

	    [[tpAxisButtons cellAt:AX_YE :0] setEnabled:NO];
	    [[tpAxisForm cellAt:AX_YE :0] setTitle:"       "];
	    [tpAxisForm setStringValue:"" at:AX_YE];
	    [tpAxisForm drawCellAt:AX_YE :0];
	}

	break;
    default:
	break;
    }
    return self;
}
    
/* Delegate Methods for NXBrowsers */
- (int) browser: sender fillMatrix: matrix inColumn: (int) column
{
    id                  aCell;
    int                 i, nrows;

    if ( selectedTuple ) {
        nrows = h_getNtDim(selectedTuple);
    } else { 
        nrows = 0;
    }
    for (i = 0; i < nrows; i++) {
	[matrix insertRowAt:i];
	aCell = [matrix cellAt:i :0];
	[aCell setStringValue:h_getNtLabel(selectedTuple, i)];
	[aCell setLeaf:YES];
	[aCell setLoaded:YES];
    }
    return nrows;
}
@end
