
/* Generated by Interface Builder */

#import "Inspector.h"

#import "HDraw.h"
#import "Graphic.h"
#import "GraphicView.h"
#import "DrawDocument.h"
#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/Form.h>
#import <appkit/NXColorWell.h>
#import <appkit/NXColorPanel.h>
#import <appkit/PopUpList.h>
#import <appkit/Slider.h>
#import <appkit/Text.h>
#import <stdlib.h>
#import <math.h> 
#import <appkit/color.h>

@implementation Inspector

- reloadGraphic:graphic
/*
 * Loads up the size fields if they have changed since last time
 * we loaded up the panel with this graphic.  This is used since we
 * know that none of the things controlled by the InspectorPanel can
 * change except the size from event to event.
 */
{
    NXRect bounds;

    if (!graphic) return self;
    [graphic getBounds:&bounds];
    if (lastSize.width != bounds.size.width) {
	[width setFloatValue:bounds.size.width];
	lastSize.width = bounds.size.width;
    }
    if (lastSize.height != bounds.size.height) {
	[height setFloatValue:bounds.size.height];
	lastSize.height = bounds.size.height;
    }

    return self;
}

static void setTitle(id button, int row)
{
    [button setTitle:[[[[button target] itemList] cellAt:row :0] title]];
}

- preset
{
    [fillColor setColor:NX_COLORCLEAR];
    [lineColor setColor:NX_COLORBLACK];
    [textColor setColor:NX_COLORCLEAR];
    return self;
}

- loadGraphic:graphic
/*
 * Loads up the InspectorPanel with a new graphic's attributes.
 */
{
    NXRect bounds;

    if (selectedGraphic == graphic) return [self reloadGraphic:graphic];
    selectedGraphic = graphic;
    if (!selectedGraphic) return self;

    [lineWidthField setFloatValue:[graphic lineWidth]];
    [lineWidthSlider setFloatValue:[graphic lineWidth]];
    [lineColor setColor:[graphic lineColor]];
    [fillColor setColor:[graphic fillColor]];
    [textColor setColor:[graphic textColor]];
    [graphic getBounds:&bounds];
    [width setFloatValue:bounds.size.width];
    [height setFloatValue:bounds.size.height];
    lastSize = bounds.size;
    if ([graphic fill]) {
	[filled setEnabled:YES];
	setTitle(filled, [graphic fill]);
    } else {
	setTitle(filled, 0);
	[filled setEnabled:NO];
    }
    setTitle(lineCap, [graphic lineCap]);
    setTitle(arrows, [graphic lineArrow]);
    setTitle(lineJoin, [graphic lineJoin]);
    if ([graphic wantsLineColor]) {
	[lineColor activate:YES];
	if (NXEqualColor([lineColor color], [graphic fillColor])) [fillColor activate:NO];
    } else if ([graphic wantsFillColor]) {
	[fillColor activate:YES];
	if (NXEqualColor([fillColor color], [graphic lineColor])) [lineColor activate:NO];
    } else if ([graphic wantsTextColor]) {
	[textColor activate:YES];
    }

    return self;
}

- load:view
/*
 * If the view has only one selected graphic, then the panel is loaded with it.
 */
{
    graphicView = view;
    [self loadGraphic:[view selectedGraphic]];
    return self;
}

- initializeGraphic:graphic
/*
 * Goes the opposite way of loadGraphic.  Gives the Graphic the attributes
 * which are in the InspectorPanel.
 */
{
    float value;
    const char *s;
    NXColor color;

    s = [lineWidthField stringValue];
    if (s && s[0] && (value = atof(s))) [graphic setLineWidth:&value];
    color = [lineColor color];
    [graphic setLineColor:&color];
    color = [fillColor color];
    [graphic setFillColor:&color];
    color = [textColor color];
    if ([graphic wantsTextColor]) {
	if (NXEqualColor(color, NX_COLORCLEAR)) {
	    color = NX_COLORBLACK;
	    [textColor setColor:color];
	}
    } else {
	if (!NXEqualColor(color, NX_COLORCLEAR)) {
	    color = NX_COLORCLEAR;
	    [textColor setColor:color];
	}
    }
    [graphic setGraphicTextColor:&color];
    [graphic setFill:[[[filled target] itemList] selectedRow]];
    [graphic setLineCap:[[[lineCap target] itemList] selectedRow]];
    [graphic setLineArrow:[[[arrows target] itemList] selectedRow]];
    [graphic setLineJoin:[[[lineJoin target] itemList] selectedRow]];
    if ([graphic wantsLineColor]) {
	[lineColor activate:YES];
	if (NXEqualColor([lineColor color], [graphic fillColor])) [fillColor activate:NO];
    } else if ([graphic wantsFillColor]) {
	[fillColor activate:YES];
	if (NXEqualColor([fillColor color], [graphic lineColor])) [lineColor activate:NO];
    } else if ([graphic wantsTextColor]) {
	[textColor activate:YES];
    }

    return self;
}

/* Overridden from superclass */

- windowDidUpdate:sender
/*
 * Called each time an event occurs.  Loads up the panel.
 */
{
    if (hDraw == nil ) {
        hDraw = NXGetNamedObject("HDrawInstance", NXApp);
    }
    [self load:[[hDraw currentDocument] view]];
    return self;
}

- changeContinuous:sender
{
    [[NXColorPanel sharedInstance:NO] setContinuous:([sender intValue] ? YES : NO)];
    return self;
}

/* Target/Action methods */

- changeFilled:sender
{
    [graphicView takeFillValueFrom:[[filled target] itemList]];
    if ([[filled target] itemList] && ![[[filled target] itemList] selectedRow]) {
	[fillColor deactivate];
	[fillColor setColor:NX_COLORCLEAR];
	[filled setEnabled:NO];
    }
    return self;
}

- changeLineCap:sender
{
    [graphicView takeLineCapFrom:[[lineCap target] itemList]];
    return self;
}

- changeArrows:sender
{
    [graphicView takeLineArrowFrom:[[arrows target] itemList]];
    return self;
}

- changeLineJoin:sender
{
    [graphicView takeLineJoinFrom:[[lineJoin target] itemList]];
    [[graphicView window] makeKeyWindow];
    return self;
}

- changeLineWidth:sender
{
    float linewidth;

    linewidth = [sender floatValue];
    if (sender == lineWidthSlider) {
	if ([NXApp currentEvent]->type == NX_MOUSEDRAGGED) {
	    [graphicView graphicsPerform:@selector(deselect) andDraw:NO];
	} else {
	    [graphicView graphicsPerform:@selector(select) andDraw:NO];
	}
	[lineWidthField setFloatValue:linewidth];
    } else {
	if ([lineWidthSlider maxValue] < linewidth) {
	    [lineWidthSlider setMaxValue:linewidth];
	}
	[lineWidthSlider setFloatValue:linewidth];
	[[graphicView window] makeKeyWindow];
    }
    [graphicView takeLineWidthFrom:lineWidthField];
    return self;
}

- changeTextColor:sender
{
    id fe;

    [graphicView takeTextColorFrom:sender];
    fe = [[graphicView window] getFieldEditor:NO for:self];
    [fe setSelColor:[sender color]];

    return self;
}

- changeLineColor:sender
{
    [graphicView takeLineColorFrom:sender];
    return self;
}

- changeFillColor:sender
{
    [graphicView takeFillColorFrom:sender];
    if (NXAlphaComponent([sender color]) == NX_NOALPHA ||
	NXAlphaComponent([sender color]) == 1.0) {
	[filled setEnabled:YES];
	if (![[[filled target] itemList] selectedRow]) {
	    [[[filled target] itemList] selectCellAt:1 :0];
	    setTitle(filled, 1);
	    [self changeFilled:[filled target]];
	}
    } else {
	if ([[[filled target] itemList] selectedRow]) {
	    [[[filled target] itemList] selectCellAt:0 :0];
	    setTitle(filled, 0);
	    [self changeFilled:[filled target]];
	}
    }
    return self;
}


- changeDimensions:sender
{
    NXSize size;

    size.width = [width floatValue];
    size.height = [height floatValue];
    [graphicView graphicsPerform:@selector(sizeTo:) with:(id)&size andDraw:YES];
    [[[graphicView window] flushWindow] makeKeyWindow];

    return self;
}

- setFilled:anObject
{
    id matrix = [[anObject target] itemList];
    [matrix setTarget:self];
    [matrix setAction:@selector(changeFilled:)];
    [matrix selectCellAt:0 :0];
    filled = anObject;
    return self;
}

- setLineJoin:anObject
{
    id matrix = [[anObject target] itemList];
    [matrix setTarget:self];
    [matrix setAction:@selector(changeLineJoin:)];
    [matrix selectCellAt:0 :0];
    lineJoin = anObject;
    return self;
}

- setLineCap:anObject
{
    id matrix = [[anObject target] itemList];
    [matrix setTarget:self];
    [matrix setAction:@selector(changeLineCap:)];
    [matrix selectCellAt:0 :0];
    lineCap = anObject;
    return self;
}

- setArrows:anObject
{
    id matrix = [[anObject target] itemList];
    [matrix setTarget:self];
    [matrix setAction:@selector(changeArrows:)];
    [matrix selectCellAt:0 :0];
    arrows = anObject;
    return self;
}

@end
