/* LineAndCharacterRange.m created by ovidiu on Sat 29-Mar-1997 */

#import <AppKit/AppKit.h>
#import "Document.h"
#import "LineAndCharacterRange.h"

@implementation LineAndCharacterRange

// Class variables
static id sharedInstance = nil;

+ sharedInstance
{
    if (!sharedInstance) {
        sharedInstance = [self new];
        if (![NSBundle loadNibNamed:@"LineRange" owner:sharedInstance]) {
            NSLog(@"Failed to load LineRange.nib");
            [sharedInstance release];
            sharedInstance = nil;
            return nil;
        }
    }

    return sharedInstance;
}

- init
{
    [super init];

    if (!sharedInstance)
        sharedInstance = self;
    else
        [self release];

    return sharedInstance;
}

- (void)awakeFromNib
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    useLineRange = [defaults boolForKey:@"LineRange"];
    autoupdate = [defaults boolForKey:@"AutoUpdate"];
    [lineCharacterMatrix selectCellAtRow:!useLineRange column:0];
    [autoUpdateButton setState:autoupdate];
    [[rangeField window] setFrameAutosaveName:@"Line Range Window"];
}

- (void)buttonsChanged:sender
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setBool:![lineCharacterMatrix selectedRow] forKey:@"LineRange"];
    [defaults setBool:[autoUpdateButton state] forKey:@"AutoUpdate"];
}

- (void)select:sender
{
    Document* document = [Document currentDocument];
    NSArray* selectionArray = [[rangeField stringValue]
                                componentsSeparatedByString:@":"];
    int a = -1, b = -1, count = [selectionArray count];

    a = [[selectionArray objectAtIndex:0] intValue];
    if (count == 2)
        b = [[selectionArray objectAtIndex:1] intValue];

    if (b == -1)
        b = a;
    else if (a > b) {
        int tmp = a;
        a = b;
        b = tmp;
    }

    if (a < 0 && b < 0) {
        [[lineCharacterMatrix window] orderOut:nil];
        return;
    }

    if (![lineCharacterMatrix selectedRow])
        [document selectFromLine:a toEndLine:b];
    else
        [document selectFromChar:a toEndChar:b];

    [[lineCharacterMatrix window] orderOut:nil];
}

- (void)updateInfo
{
    Document* document;

    document = [Document currentDocument];

    if (![lineCharacterMatrix selectedRow]) {
        int startLine, endLine;
        [document getStartLine:&startLine endLine:&endLine];
        if (startLine != endLine)
            [rangeField setStringValue:
                [NSString stringWithFormat:@"%d:%d", startLine, endLine]];
        else
            [rangeField setStringValue:
                [NSString stringWithFormat:@"%d", startLine]];
    }
    else {
        NSRange range = [document selectionRange];

        if (range.length)
            [rangeField setStringValue:
                [NSString stringWithFormat:@"%d:%d",
                    range.location, range.location + range.length]];
        else
            [rangeField setStringValue:
                [NSString stringWithFormat:@"%d", range.location]];
    }
}

- (void)showPanel
{
    [self updateInfo];
    [[rangeField window] makeFirstResponder:rangeField];
    [[lineCharacterMatrix window] makeKeyAndOrderFront:nil];
}

- (BOOL)useLineRange		{ return useLineRange; }
- (BOOL)autoupdate		{ return autoupdate; }

@end
