/*	
 *    Copyright (C) 1993  Raymond Lutz.
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; version 1.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * This program illustrates the features of the CubicSlider.
 */

#import "Controler.h"
#import "CubicSlider.h"
#import "CubicSliderCell.h"
#import "ProgressView.h"
#import "SwitchBox.h"
#import <appkit/Button.h>
#import <appkit/TextField.h>
#import <appkit/Form.h>
#import <appkit/graphics.h>
#import <math.h>
#import <strings.h>

#define UPTIME (float)0.7

@implementation Controler

-appDidInit:sender
{
    [sample setUpTarget:self action: @selector(upMouseMethod)];
    [[[sample setCubic: NO] setMinValue:0] setMaxValue: 1];
    [[sample setMinEditable: NO] setMaxEditable:NO];
    [aboutView switchTo:0];
    [[valForm cellAt:0:0] setFloatingPointFormat:NO left:1 right:6];
    [[valForm cellAt:1:0] setFloatingPointFormat:NO left:1 right:6];
    [self resetSliderValuesDisplay];
    panel = 0;
    fastCubic = YES;
    return self;
}

void soon(teNum, now, theControl) 
DPSTimedEntry teNum;
double now;
id theControl;
{
    [[theControl upToDown] stopTimedEntry]; 
    return;   
}


- stopTimedEntry
{
    	if (teNum)
         DPSRemoveTimedEntry (teNum);
    	teNum = (DPSTimedEntry)0;
	return self;
}

- upToDown
{
    [upTargetBut setState: 0];
    [upTargetText setTextGray: NX_DKGRAY];
    return self;
}

- upMouseMethod
{
    [upTargetBut setState: 1];
    [upTargetText setTextGray: NX_BLACK];
    if (!teNum) teNum = DPSAddTimedEntry(UPTIME, &soon, self, NX_BASETHRESHOLD);
    return self;
}



- setPlainSample:sender
{
    [[[[sample setCubic: NO] setMinValue:0] setMaxValue: 1] setFloatValue:0];
    [minText setFloatValue: 0];
    [maxText setFloatValue: 1];
    [[sample setMinEditable: NO] setMaxEditable:NO];
    [self resetSliderValuesDisplay];
    panel = [[sender selectedCell] tag];
    [aboutView switchTo: panel];
    return self;
}

- setMouseSample:sender
{
    [[[[sample setCubic: NO] setMinValue:0] setMaxValue: 1] setFloatValue:0];
    [minText setFloatValue: 0];
    [maxText setFloatValue: 1];
    [[sample setMinEditable: NO] setMaxEditable:NO];
    [self resetSliderValuesDisplay];
    panel = [[sender selectedCell] tag];
    [aboutView switchTo: panel];
    return self;
}

- setMinLockedSample:sender
{
    [[[[sample setCubic: NO] setMinValue:0] setMaxValue: 1] setFloatValue:0];
    [minText setFloatValue: 0];
    [maxText setFloatValue: 1];
    [[sample setMinEditable: NO] setMaxEditable:YES];
    [[sample clearAbsoluteMinValue] clearAbsoluteMaxValue];
    panel = [[sender selectedCell] tag];
    [aboutView switchTo: panel];
    [[[barView reset] setMin: 0] setMax: 10];
    [self clearLabels];
    [labelMin setStringValue:"0"];
    [label2MaxEdit setStringValue:"1"];
    [labelMax setStringValue:"10"];
    [barView setPosition: 0];
    return self;
}
- setCubicSample:sender
{
    [[[[sample setCubic: YES] setMinValue:0] setMaxValue: 1] setFloatValue:0];
    [minText setFloatValue: 0];
    [maxText setFloatValue: 1];
    [[sample setMinEditable: NO] setMaxEditable:NO];
    [self resetSliderValuesDisplay];
    panel = [[sender selectedCell] tag];
    [aboutView switchTo: panel];
    return self;
}

- setMatriceSamples:sender;
{
    [self resetSliderValuesDisplay];
    panel = [[sender selectedCell] tag];
    [[sample setMinEditable: NO] setMaxEditable:NO];
    [aboutView switchTo: panel];
    return self;
}


- setAbsRangeSample:sender
{
    [[[[sample setCubic: NO] setMinValue:0] setMaxValue: 1] setFloatValue:0];
    [minText setFloatValue: 0];
    [maxText setFloatValue: 1];
    [[sample setMinEditable: YES] setMaxEditable:YES];
    [[sample setAbsoluteMinValue: -4] setAbsoluteMaxValue: 5];
    panel = [[sender selectedCell] tag];
    [aboutView switchTo: panel];
    [[[barView reset] setMin: -4] setMax: 5];
    [self clearLabels];
    [labelMin setStringValue:"-4"];
    [label1Abs setStringValue:"0"];
    [label2Abs setStringValue:"1"];
    [labelMax setStringValue:"5"];
    [barView setPosition: 0];
    return self;
}

- setInspectableSample:sender
{
    panel = [[sender selectedCell] tag];
    [aboutView switchTo: panel];
    [self updateInspector];
    return self;
}

- resetSliderValuesDisplay
{
    [[[barView reset] setMin: 0] setMax: 1];
    [self clearLabels];
    return self;
}

- clearLabels
{
    [labelMin setStringValue:""];
    [label1Abs setStringValue:""];
    [label2Abs setStringValue:""];
    [label2MaxEdit setStringValue:""];
    [labelMax setStringValue:""];
    return self;
}

- takeCubicSliderStatusFrom:sender
{
    [minText setDoubleValue: [sender minValue]];
    [maxText setDoubleValue: [sender maxValue]];
    [self updateInspector];
    return self;
}

- cubicSpeed:sender;
{
    fastCubic = [sender state];
    [valForm setEnabled: 1 - fastCubic];
    return self;
}

- sampleMoved:sender
{
    float val = [sender floatValue];
    [mouse setState: 1 - [sender mouseIsUp]];
    [barView setPosition: val];
    if ( panel == 2 && !fastCubic) {
	 [valForm setFloatValue:val at:0];
	 [valForm setFloatValue: fabs(val - oldValue) at:1];
	 oldValue = val;}
    return self;
}

- ok:sender
{

    if (strlen([absoluteValuesForm stringValueAt:0]) != 0)  
         [sample setAbsoluteMinValue:[absoluteValuesForm doubleValueAt:0]];
    else [sample clearAbsoluteMinValue];	    
    if (strlen([absoluteValuesForm stringValueAt:1]) != 0)  
         [sample setAbsoluteMaxValue:[absoluteValuesForm doubleValueAt:1]];
    else [sample clearAbsoluteMaxValue];	    
    [sample setMinEditable: 1-[[userEditableSwitches cellAt: 0:0] state]] ;
    [sample setMaxEditable: 1-[[userEditableSwitches cellAt: 1:0] state]] ;
    [sample setCubic: 1 - [functionMatrix selectedRow] ] ;
    [sample setMaxValue:[extremumValuesForm doubleValueAt:1]];
    [sample setMinValue:[extremumValuesForm doubleValueAt:0]];
    [self updateInspector];
    return self;
}

- updateInspector
{
    if ([sample absoluteMinValueSet])  
         [absoluteValuesForm setDoubleValue: [sample absoluteMinValue] at:0];
    else [absoluteValuesForm setStringValue: "" at:0];	    
    if ([sample absoluteMaxValueSet]) 
         [absoluteValuesForm setDoubleValue: [sample absoluteMaxValue] at:1];
    else [absoluteValuesForm setStringValue: "" at:1];	    
    [userEditableSwitches setState: 1- [sample minIsEditable] at:0:0];
    [userEditableSwitches setState: 1- [sample maxIsEditable] at:1:0];
    [functionMatrix selectCellAt: 1 - [sample isCubic]:0];
    [extremumValuesForm setDoubleValue: [sample minValue] at:0];
    [extremumValuesForm setDoubleValue: [sample maxValue] at:1];
    [minText setFloatValue: [sample minValue]];
    [maxText setFloatValue: [sample maxValue]];
//    [[barView setMin: [sample minValue]] setMax: [sample maxValue]];
    return self;
}

- minChg:sender
{
    [sample setMinValue:[sender doubleValue]];
    [self updateInspector];
    return self;
}

- maxChg:sender
{
    [sample setMaxValue:[sender doubleValue]];
    [self updateInspector];
    return self;
}
- absMinChg:sender
{
    if (strlen([sender stringValueAt:0]) != 0)  
         [sample setAbsoluteMinValue:[sender doubleValueAt:0]];
    else [sample clearAbsoluteMinValue];	    
    [self updateInspector];
    return self;
}

- absMaxChg:sender
{
    if (strlen([sender stringValueAt:1]) != 0)  
         [sample setAbsoluteMaxValue:[sender doubleValueAt:1]];
    else [sample clearAbsoluteMaxValue];	    
    [self updateInspector];
    return self;
}

- minEditChg:sender
{
   if ([[sender selectedCell] state]) 
          [absoluteValuesForm setFloatValue: [sample minValue] at:0];
   else [absoluteValuesForm setStringValue: "" at:0];
   [sample setMinEditable: 1-[[sender cellAt: 0:0] state]] ;
   return self;
}

- maxEditChg:sender
{
   if ([[sender selectedCell] state]) 
           [absoluteValuesForm setFloatValue: [sample maxValue] at:1];
   else [absoluteValuesForm setStringValue: "" at:1];
   [sample setMaxEditable: 1-[[sender cellAt: 1:0] state]] ;
   return self;
}

- fctChg:sender
{
    [sample setCubic: 1 - [sender selectedRow] ] ;
    return self;
}




@end
