// You may freely copy, distribute and reuse the code in this example.
// NeXT disclaims any warranty of any kind, expressed or implied,
// as to its fitness for any particular use.

// Controller and ColorTest.app written by Keith Bernstein.

#import <appkit/appkit.h>
#import "Controller.h"
#import "colorAsAscii.h"

#define ATTACH_LIST1   0
#define ATTACH_LIST2   1
#define ATTACH_LIST3   2
#define DETACH_LIST1   3
#define DETACH_LIST2   4
#define DETACH_LIST3   5

#define INSERT_BEFORE_WHEELMODE               0
#define INSERT_BEFORE_SLIDERMODE              1
#define INSERT_BEFORE_CUSTOM_PALETTEMODE      2
#define INSERT_BEFORE_LISTMODE                3
#define INSERT_AFTER_LISTMODE                 4


float insertionPoint = 0.0;

@implementation Controller

- useAccessoryView:sender {
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (colorPanel) {
        [colorPanel setAccessoryView:[accessoryViewSwitch state] ? accessoryView : nil];
    } 
    return self;
}

- attachMatrixClicked:sender
{
    char appWrapperDir[MAXPATHLEN+1];
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (!attachedList1) {
        strcpy(appWrapperDir, [[NXBundle mainBundle] directory]);
	strcat(appWrapperDir, "/");
	strcat(appWrapperDir, "attach1.clr");
        attachedList1 = [[NXColorList allocFromZone:[self zone]]
                                            initWithName:"attach1" 
	     fromFile:appWrapperDir];
        strcpy(appWrapperDir, [[NXBundle mainBundle] directory]);
	strcat(appWrapperDir, "/");
	strcat(appWrapperDir, "attach2.clr");
        attachedList2 = [[NXColorList allocFromZone:[self zone]]
                                            initWithName:"attach2" 
	 fromFile:appWrapperDir];
        strcpy(appWrapperDir, [[NXBundle mainBundle] directory]);
	strcat(appWrapperDir, "/");
	strcat(appWrapperDir, "attach3.clr");
        attachedList3 = [[NXColorList allocFromZone:[self zone]]
                                            initWithName:"attach3" 
	     fromFile:appWrapperDir];
    }
    switch([[sender selectedCell] tag]) {
        case ATTACH_LIST1:
	    [colorPanel attachColorList:attachedList1];
	    [addRedButton setEnabled:YES];
	    break;
    &case ATTACH_LIST2:
	    [colorPanel attachColorList:attachedList2];
	    break;
        case ATTACH_LIST3:
	    [colorPanel attachColorList:attachedList3];
	    break;
        case DETACH_LIST1:
	    [colorPanel detachColorList:attachedList1];
	    [addRedButton setEnabled:NO];
	    break;
        case DETACH_LIST2:
	    [colorPanel detachColorList:attachedList2];
	    break;
        case DETACH_LIST3:
	    [colorPanel detachColorList:attachedList3];
	    break;
    }
    return self;
}

// This method demonstrates how to add a color to an attached ColorList...
- addRed:sender
{
    char nameBuf[30];
    static int redCount = 1;
    
    sprintf(nameBuf, "New Red%d", redCount++);
    [attachedList1 setColorNamed:nameBuf color:NX_COLORRED];
    
    // This method will update the currently viewed ColorList.
    [[NXColorPanel sharedInstance:NO] updateCustomColorList];

    return(self);
}

- insertCustomPicker:sender
{
    switch ([[sender selectedCell] tag]) {
        case INSERT_BEFORE_WHEELMODE:
	    insertionPoint = NX_WHEEL_INSERTION - 0.001;
	    break;
        case INSERT_BEFORE_SLIDERMODE:
	    insertionPoint = NX_SLIDERS_INSERTION - 0.001;
	    break;
        case INSERT_BEFORE_CUSTOM_PALETTEMODE:
	    insertionPoint = NX_CUSTOMPALETTE_INSERTION - 0.001;
	    break;
        case INSERT_BEFORE_LISTMODE:
	    insertionPoint = NX_LIST_INSERTION - 0.001;
	    break;
        case INSERT_AFTER_LISTMODE:
	    insertionPoint = NX_LIST_INSERTION + 0.001;
	    break;
    }
    return self;
}

- colorPanelOpacityOnOff:sender
{
    int selectedCellTag = [[sender selectedCell] tag];
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (colorPanel = [NXColorPanel sharedInstance:NO]) {
	[colorPanel setShowAlpha:selectedCellTag];
	[appAlphaMatrix selectCellWithTag:selectedCellTag];
    } else {
        // If ColorPanel is not instantiated, "setImportAlpha" should be
	// used.  In fact, usually "setImportAlpha" is all that is ever
	// really needed.
        if ([NXApp doesImportAlpha]) {
	    [colorPanelAlphaMatrix selectCellWithTag:1];
	} else {
	    [colorPanelAlphaMatrix selectCellWithTag:0];
	}
    }
    return self;
}

- appOpacityOnOff:sender
{
    int selectedCellTag = [[sender selectedCell] tag];
    
    [NXApp setImportAlpha:selectedCellTag];
    [colorPanelAlphaMatrix selectCellWit':selectedCellTag];
    return self;
}

- continuousOnOff:sender
{
    [[NXColorPanel sharedInstance:NO] setContinuous:[[sender selectedCell] tag]];
    return self;
}

- isContinuous:sender
{
    BOOL continuous = YES;
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (colorPanel) {
	continuous = [colorPanel isContinuous];
    } 
    [continuousSwitch setState:continuous];
    return self;
}

- showCurrColor:sender
{
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (colorPanel) {
        [sampleWell setColor:[colorPanel color]];
    }
    return self;
}

- alphaValue:sender
{
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (colorPanel) [alphaText setFloatValue:[colorPanel alpha]];
    return self;
}

- queryMode:sender
{
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (colorPanel) [currModeText setIntValue:[colorPanel mode]];

    return self;
}

- modeMatrixClicked:sender
{
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:NO];
    
    if (colorPanel) {
        [colorPanel setMode:[[sender selectedCell] tag]];
    } else {
        [NXColorPanel setPickerMode:[[sender selectedCell] tag]];
    }
    return self;
}

- maskMatrixClicked:sender 
{
    int pickerMask = 0;
    int i;
    ButtonCell *aCell;
    
    for (i = [sender cellCount] - 1; i >=0; --i) {
        aCell = [sender cellAt:i :0];
	if ([aCell state]) {
            pickerMask |= [aCell tag];
	}
    }
    [NXColorPanel setPickerMask:pickerMask];
   
    return self;
}

- showColorPanel:sender
{
    NXColorPanel *colorPanel = [NXColorPanel sharedInstance:YES];
    if ([accessoryViewSwitch state]) {
        [colorPanel setAccessoryView:accessoryView];
    }
    [colorPanel setContinuous:[[continuousOnOffMatrix selectedCell] tag]];
    [NXApp orderFrontColorPanel:NXApp];
    return self;
}

// This method gets called when the .nib is being loaded. The argument is
// a pointer to the main color well in the window. If we've stored
// a color away from the previous invocation of the app, we set the color
// well color. Otherwise we leave the default value (determined in IB)
// alone. The color is saved out to the defaults whenever the app terminates.
// (We could also write it out everytime it changes...)

- setTheWell:anObject
{
 (XColor color;

    theWell = anObject;	// First set the outlet
    
    if (readColorFromDefaults ("Color", &color)) {
	[theWell setColor:color];
    }        

    return self;
}

- appWillTerminate:sender
{
    writeColorToDefaults ([theWell color], "Color");
    return self;
}

@end
