#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <OmniBase/OmniBase.h>
#import <OmniFoundation/OmniFoundation.h>
#import <OmniAppKit/OmniAppKit.h>
#import <OIF/OIF.h>

#import "OWImagingPreferences.h"

RCS_ID("$Header: /Network/Developer/Source/CVS/OmniGroup/OmniImage/Preferences.bproj/OWImagingPreferences.m,v 1.8 1998/01/19 09:38:59 wjs Exp $")

@interface OWImagingPreferences (Private)
- (void)updateAnimationMatrixAndField;
@end

@implementation OWImagingPreferences

- (void)updateUI;
{
    [minDrawIntervalField setFloatValue:[defaults floatForKey:@"OIImageProcessorDrawInterval"]];
    [minRowsIntervalField setIntValue:[defaults integerForKey:@"OIImageProcessorCheckTimeEveryNRows"]];
    [gifGammaCorrectionField setFloatValue:[defaults floatForKey:@"GifGammaCorrection"]];
    [jpegGammaCorrectionField setFloatValue:[defaults floatForKey:@"JPEGGammaCorrection"]];
    [colorGammaCorrectionField setFloatValue:[defaults floatForKey:@"ColorGammaCorrection"]];

    [self updateAnimationMatrixAndField];
}

- (void)setValueForSender:sender;
{
    if (sender == minDrawIntervalField) {
	if ([sender floatValue] > 0.0)
            [defaults setFloat:[sender floatValue] forKey:@"OIImageProcessorDrawInterval"];
	else
            [self resetFloatValueToDefaultNamed:@"OIImageProcessorDrawInterval" inTextField:sender];
    }
    else if (sender == minRowsIntervalField) {
	if ([sender intValue] > 0)
            [defaults setInteger:[sender intValue] forKey:@"OIImageProcessorCheckTimeEveryNRows"];
        else
            [self resetIntValueToDefaultNamed:@"OIImageProcessorCheckTimeEveryNRows" inTextField:sender];
    }
    else if (sender == gifGammaCorrectionField) {
	if ([sender floatValue] > 0 && [sender floatValue] < 10)
	    [defaults setFloat:[sender floatValue] forKey:@"GifGammaCorrection"];
	else
            [self resetFloatValueToDefaultNamed:@"GifGammaCorrection" inTextField:sender];
    }
    else if (sender == jpegGammaCorrectionField) {
        if ([sender floatValue] > 0 && [sender floatValue] < 10)
            [defaults setFloat:[sender floatValue] forKey:@"JPEGGammaCorrection"];
        else
            [self resetFloatValueToDefaultNamed:@"JPEGGammaCorrection" inTextField:sender];
    }
    else if (sender == colorGammaCorrectionField) {
        if ([sender floatValue] > 0 && [sender floatValue] < 10)
            [defaults setFloat:[sender floatValue] forKey:@"ColorGammaCorrection"];
        else
            [self resetFloatValueToDefaultNamed:@"ColorGammaCorrection" inTextField:sender];
    }
    else if (sender == animateCyclesField) {
        if ([sender intValue] >= 0)
            [defaults setInteger:[sender intValue] forKey:@"AnimationMaxLoops"];
        [self updateAnimationMatrixAndField];
    }
    else if (sender == animateBehaviorMatrix) {
        unsigned int animationMaxLoops;

        animationMaxLoops = [defaults integerForKey:@"AnimationMaxLoops"];

        switch ([sender selectedRow]) {
            case 0:
                [defaults setInteger:0 forKey:@"AnimationMaxLoops"];
                break;
            case 1:
                if (animationMaxLoops != 0 && animationMaxLoops != OIAnimationInfiniteLoopCount)
                    [defaults setInteger:animationMaxLoops forKey:@"AnimationMaxLoops"];
                else
                    [defaults setInteger:20 forKey:@"AnimationMaxLoops"];
                break;
            case 2:
                [defaults setInteger:OIAnimationInfiniteLoopCount forKey:@"AnimationMaxLoops"];
                break;
        }
        [self updateAnimationMatrixAndField];
    }
}

// NSNibAwaking informal protocol

- (void)awakeFromNib;
{
    [super awakeFromNib];
    
    // Ints
    [[minRowsIntervalField cell] setEntryType:NSPositiveIntType];
    [[animateCyclesField cell] setEntryType:NSPositiveIntType];

    // Floats
    [[minDrawIntervalField cell] setEntryType:NSPositiveFloatType];
    [[gifGammaCorrectionField cell] setEntryType:NSPositiveFloatType];
    [[jpegGammaCorrectionField cell] setEntryType:NSPositiveFloatType];
    [[colorGammaCorrectionField cell] setEntryType:NSPositiveFloatType];
        
    [jpegGammaCorrectionField setFloatingPointFormat:NO left:2 right:1];
    [gifGammaCorrectionField setFloatingPointFormat:NO left:2 right:1];
    [minDrawIntervalField setFloatingPointFormat:NO left:2 right:1];
    [colorGammaCorrectionField setFloatingPointFormat:NO left:2 right:1];
}

@end

@implementation OWImagingPreferences (Private)

- (void)updateAnimationMatrixAndField;
{
    unsigned int animationMaxLoops;
    
    // Animation
    animationMaxLoops = [defaults integerForKey:@"AnimationMaxLoops"];
    switch (animationMaxLoops) {
        case 0:
            [animateBehaviorMatrix selectCellAtRow:0 column:0];
            [animateCyclesField setStringValue:@""];
            break;
        default:
            [animateBehaviorMatrix selectCellAtRow:1 column:0];
            [animateCyclesField setIntValue:animationMaxLoops];
            break;
        case OIAnimationInfiniteLoopCount:
            [animateBehaviorMatrix selectCellAtRow:2 column:0];
            [animateCyclesField setStringValue:@""];
            break;
    }
}

@end
