//
//  MHSplitView.m
//  Hootenanny
//
//  Created by Noah Sorscher on 12/4/04.
//  Copyright 2004 __MyCompanyName__. All rights reserved.
//

#import "MHSplitView.h"


@implementation MHSplitView

- (void) awakeFromNib
{
    autosaveName = nil;
    active = NO;
    [self setDelegate:self];
    [self performSelector:@selector(activate) withObject:nil afterDelay:0];
    [self performSelector:@selector(restoreAutosave) withObject:nil afterDelay:0];
}

- (void)activate
{
    active = YES;
}

- (void) restoreAutosave
{
    if (!autosaveName) return;
    NSArray *sizes = [[NSUserDefaults standardUserDefaults] objectForKey:[NSString stringWithFormat:@"%@ %@", [self className], autosaveName]];
    if (!sizes) return;
    int i, n = [[self subviews] count];
    for (i=0;i<n && i < [sizes count];i++) {
        NSRect frame = [[[self subviews] objectAtIndex:i] frame];
        if ([self isVertical])
            frame.size.width = [[sizes objectAtIndex:i] floatValue];
        else
            frame.size.height = [[sizes objectAtIndex:i] floatValue];
        [[[self subviews] objectAtIndex:i] setFrame:frame];
    }
    [self adjustSubviews];
    [self setNeedsDisplay:YES];
}

- (void) setAutosaveName:(NSString *)s
{
    [autosaveName release];
    autosaveName = [s retain];
}

- (NSString *)autosaveName
{
    return autosaveName;
}

- (void)splitViewDidResizeSubviews:(NSNotification *)aNotification
{
    if (!active || !autosaveName) return;
    NSMutableArray *sizes = [NSMutableArray array];
    int i, n = [[self subviews] count];
    for (i=0;i<n;i++) {
        NSRect frame = [[[self subviews] objectAtIndex:i] frame];
        if ([self isVertical])
            [sizes addObject:[NSNumber numberWithFloat:frame.size.width]];
        else
            [sizes addObject:[NSNumber numberWithFloat:frame.size.height]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:sizes forKey:[NSString stringWithFormat:@"%@ %@", [self className], autosaveName]];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
