/* InspectPlot.h		by Paul Kunz	June 1991
 * Controls various options for displaying the plot.
 *
 * Copyright (C)  1991  The Board of Trustees of
 * The Leland Stanford Junior University.  All Rights Reserved.
 */

#import "InspectPlot.h"

const char InspectPlot_h_rcsid[] = INSPECTPLOT_H_ID;
const char InspectPlot_m_rcsid[] = "$Id: InspectPlot.m,v 1.55 1992/04/24 20:51:14 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 <appkit/SavePanel.h>
#import <appkit/Slider.h>

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

#import <c.h>
#import <string.h>
#import <float.h>

/* Tags on the Graph Type radio Buttons defined in IB 
 * must be same as graphtype_t enumerated type in hippo.h 
 * Here we define the rows index of grey and color bins buttons
 * which both have tag of COLORPLOT
 */
#define GREY_ROW 2
#define COLOR_ROW 3 

/* Tags on the draw options switches defined in IB */
#define BOX_TAG 0
#define PTS_TAG 1
#define JOIN_TAG 2
#define ERR_TAG 3
#define TLT_TAG 4
#define AXES_TAG 5


@implementation InspectPlot

- initInspFor:aDraw
{
    [super initInspFor:aDraw];
    [NXApp loadNibSection:"InspectPlot.nib" owner:self
    	   withNames:NO fromZone:[self zone]];
    [theInspector addView:[contentBox contentView]
                 withName:"Graph Options" withSupervisor:self];
    currentDim = 1;
    return self;
}
- drawOption:sender
{
    drawtype_t          drawtype;
    int			titlesFlag, axesFlag;

    drawtype = NONE;
    if ([[drawOptMatrix findCellWithTag:BOX_TAG] state] == 1) 
        drawtype |= BOX;
    if ([[drawOptMatrix findCellWithTag:PTS_TAG] state] == 1)
        drawtype |= POINT;
    if ([[drawOptMatrix findCellWithTag:JOIN_TAG] state] == 1)
	drawtype |= LINE;
    if ([[drawOptMatrix findCellWithTag:ERR_TAG] state] == 1)
	drawtype |= ERRBAR;

    [graphicView graphicsPerform:@selector(setDrawType:)
                           with :(id)&drawtype andDraw:NO];
    
    titlesFlag = [[drawOptMatrix findCellWithTag:TLT_TAG] state ];
    [graphicView graphicsPerform:@selector(setTitlesFlag:)
                           with :(id)&titlesFlag andDraw:NO];
    
    axesFlag = [[drawOptMatrix findCellWithTag:AXES_TAG] state ];
    [graphicView graphicsPerform:@selector(setAxesFlag:)
                           with :(id)&axesFlag andDraw:YES];
    
    [[graphicView window] flushWindow];
    return self;
}

- graphOption:sender
{
    	int		color;
	drawtype_t	dt;
	
	graphtype = [[graphTypMatrix selectedCell] tag]; 
	color = 0;
	if (                   ( graphtype == COLORPLOT) &&
	     ([graphTypMatrix selectedRow] == COLOR_ROW)    ) {
	    color = 1;
	}
	
	if (graphtype == XYPLOT || graphtype == STRIPCHART)
	{
	     [selectedPlot getDrawType: &dt];
	     if (! (dt & (LINE+POINT+ERRBAR)) )
	     {
		  dt += POINT;
		  [graphicView graphicsPerform:@selector(setDrawType:)
 		       with :(id)&dt andDraw:NO];
	     }
	}
	
	[graphicView graphicsPerform:@selector(setDispType:)
	    with :(id)&graphtype andDraw:NO];
	[graphicView graphicsPerform:@selector(setColorType:)
		with :(id)&color andDraw:YES];
    	[[graphicView window] flushWindow];

        [self updateView];

    	return self;
}

- titleForm:sender
{
    const char		*string;
    
    string = [titleForm stringValueAt:0];
    [graphicView graphicsPerform:@selector(setTitle:)
	                    with:(id)string andDraw:NO];
    string = [titleForm stringValueAt:1];
    [graphicView graphicsPerform:@selector(NameAxisX:)
	                    with:(id)string andDraw:NO];
    string = [titleForm stringValueAt:2];
    [graphicView graphicsPerform:@selector(NameAxisY:)
	                    with:(id)string andDraw:YES];
    [[graphicView window] flushWindow];
    return self;
}
- resetTitle:sender
{
    const char	*string;
    char	disp_string[80];
    ntuple	nt;
    display	disp;
    int		index;
    
    if ( !selectedPlot ) {
        return self;
    }
    nt = [selectedPlot ntuple];
    if ( !nt ) {
        return self;
    }
    index = [sender selectedCol];
    switch (index) {
        case 0 :
	    string = h_getNtTitle(nt);
	    [titleForm setStringValue:string at:0];
	    [graphicView graphicsPerform:@selector(setTitle:)
				    with:(id)string andDraw:YES];
	    break;
	case 1 :
	    disp = [selectedPlot histDisplay];
	    index = h_getBinding(disp, XAXIS);
	    string = h_getNtLabel( nt, index );
	    [titleForm setStringValue:string at:1];
	    [graphicView graphicsPerform:@selector(NameAxisX:)
				    with:(id)string andDraw:YES];
	    break;
	case 2 :
	    disp = [selectedPlot histDisplay];
	    if ( h_getDispType(disp) == HISTOGRAM ) {
	        string = "Entries / %dx bin";
	    } else {
		index = h_getBinding( disp, YAXIS );
		string = h_getNtLabel( nt, index );
	    }
	    [graphicView graphicsPerform:@selector(NameAxisY:)
				    with:(id)string andDraw:YES];
	    h_expandLabel( disp_string, string, 80, disp );
	    [titleForm setStringValue:disp_string at:2];
	    break;
    }
    [[graphicView window] flushWindow];
    return self;
}
/* Methods to support updating the inspector's View */
- updateView
{
    int                 pos;
    drawtype_t          drawtype;
    char string[80];

    if ( !selectedPlot ) {
        return self;
    }
    [selectedPlot getDispType:&graphtype];
    if (graphtype == HISTOGRAM)
	 currentDim = 1;
    else 
	 currentDim = 2;
    

    pos = graphtype;
    if ( graphtype != COLORPLOT ) {
        [graphTypMatrix selectCellWithTag:pos];
    } else {
	[selectedPlot getDrawType:(drawtype_t *) &drawtype];
        if ( drawtype & COLOR ) {
	    [graphTypMatrix selectCellAt:COLOR_ROW :0 ];
	} else {
	    [graphTypMatrix selectCellAt:GREY_ROW :0];
	}
    }
    if ( [selectedPlot isCutPlot] ) {
        [graphTypMatrix setEnabled:NO];
    } else {
        [graphTypMatrix setEnabled:YES];
    }
    [graphTypMatrix display];
    
    if (graphtype == HISTOGRAM || graphtype == XYPLOT ||
	graphtype == STRIPCHART) 
    {
	[selectedPlot getDrawType:(drawtype_t *) & drawtype];
	pos = drawtype & BOX;
	if (pos > 0)
	    pos = 1;
	[[drawOptMatrix findCellWithTag:BOX_TAG] setState:pos];
	[[drawOptMatrix findCellWithTag:BOX_TAG] setEnabled:YES];
	
	pos = drawtype & POINT;
	if (pos > 0)
	    pos = 1;
	[[drawOptMatrix findCellWithTag:PTS_TAG] setState:pos];
	[[drawOptMatrix findCellWithTag:PTS_TAG] setEnabled:YES];
	
	pos = drawtype & LINE;
	if (pos > 0)
	    pos = 1;
	[[drawOptMatrix findCellWithTag:JOIN_TAG] setState:pos];
	[[drawOptMatrix findCellWithTag:JOIN_TAG] setEnabled:YES];
	
	pos = drawtype & ERRBAR;
	if (pos > 0)
	    pos = 1;
	[[drawOptMatrix findCellWithTag:ERR_TAG] setState:pos];
	[[drawOptMatrix findCellWithTag:ERR_TAG] setEnabled:YES];
    } else {
	[[drawOptMatrix findCellWithTag:BOX_TAG] setEnabled:NO];
	[[drawOptMatrix findCellWithTag:PTS_TAG] setEnabled:NO];
	[[drawOptMatrix findCellWithTag:JOIN_TAG] setEnabled:NO];
	[[drawOptMatrix findCellWithTag:ERR_TAG] setEnabled:NO];
    }
    [[drawOptMatrix findCellWithTag:TLT_TAG] 
         setState:[selectedPlot titlesFlag]];
    [[drawOptMatrix findCellWithTag:AXES_TAG] 
         setState:[selectedPlot axesFlag]];
	 
    [drawOptMatrix display];
    
    [titleForm setStringValue:[selectedPlot title]      at:0];
    [titleForm setStringValue:[selectedPlot axisLabelX] at:1];
    h_expandLabel( string, [selectedPlot axisLabelY], 80,
                           [selectedPlot histDisplay] );
    [titleForm setStringValue:string at:2];
    
    return self;
}
@end
