/* WindowController.m created by andrew on Sat 05-Jul-1997 */


#import "WindowController.h"

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>


@implementation WindowController

#define DOCUMENT_START_X	(150.0)
#define DOCUMENT_START_Y	( 20.0)
#define DOCUMENT_DELTA_X	( 20.0)
#define DOCUMENT_DELTA_Y	( 25.0)

static NSString *saveString = nil;
static NSString *abandonChangesString = nil;
static NSString *cancelString = nil;
static NSString *closingUnsavedWindowString = nil;

+ (void)initialize;
{
    static BOOL initialized = NO;

    [super initialize];

    if (initialized)
        return;

    initialized = YES;

    saveString = NSLocalizedString (@"Save", save prompt);
    abandonChangesString = NSLocalizedString (@"Don't Save", abandon changes prompt);
    cancelString = NSLocalizedString (@"Cancel", cancel prompt);
    closingUnsavedWindowString = NSLocalizedString (@"Save changes to %@?", alert user that window is closing and it has unsaved changes);
}

+ (NSPoint)locationForWindow:(NSWindow *)docWindow;
{
    static int count  = 0;
    NSRect windowFrame;
    NSRect mainScreenFrame;
    NSPoint docPoint;

    mainScreenFrame = [[NSScreen mainScreen] frame];

    windowFrame = [docWindow frame];

    docPoint.x = DOCUMENT_START_X + (DOCUMENT_DELTA_X * count);
    docPoint.y = mainScreenFrame.size.height - (windowFrame.size.height + DOCUMENT_START_Y + DOCUMENT_DELTA_Y * count);

    if (docPoint.x + windowFrame.size.width > mainScreenFrame.size.width || docPoint.y < 0.0) {
        count = 0;
        docPoint.x = mainScreenFrame.size.width + DOCUMENT_START_X;
        docPoint.y = mainScreenFrame.size.height - (windowFrame.size.height + DOCUMENT_START_Y);
    }
    count++;

    return docPoint;
}

+ (NSString *)nibName;
{
    return @"ResolverWindow";
}

+ (NSString *)miniwindowImageName;
{
    return @"ResolverWindow";
}

- init;
{
    [self initLazily];
    [self loadIfNecessary];
    
    return self;
}

- initLazily;
{
    [super init];
    
    return self;
}

- (void)dealloc;
{
    [window release];
    [miniwindowImage release];
    [super dealloc];
}

- (void)load;
{
    NSPoint aPoint;
    NSString *imageName;

    [NSBundle loadNibNamed:[[self class] nibName] owner:self];
    aPoint = [[self class] locationForWindow:window];
    [window setFrameOrigin:aPoint];
    [window setDelegate:self];

    imageName = [[self class] miniwindowImageName];
    if (imageName)
        miniwindowImage = [[NSImage imageNamed:imageName] retain];
}

- (void)loadIfNecessary;
{
    if (!window)
        [self load];
}

- (void)display;
{
    [self loadIfNecessary];
    [window makeKeyAndOrderFront:nil];
}

- (BOOL)shouldClose;
{
    return [self windowShouldClose:window];
}

- (void)close;
{
    [window close];
}

// save support

- (void)prepareToSaveChanges;
{
}

- (void)saveChanges;
{
}

- (void)finishedSavingChanges;
{
    [window setDocumentEdited:NO];
}

- (void)save;
{
    NS_DURING {
        [self prepareToSaveChanges];
        [self saveChanges];
        [self finishedSavingChanges];
    } NS_HANDLER {
        NSLog (@"Save failed: %@", [localException reason]);
    } NS_ENDHANDLER;
}

- (void)save:sender;
{
    NS_DURING {
        [self save];
    } NS_HANDLER {
    } NS_ENDHANDLER;
}

// revert support

- (void)prepareToRevertChanges;
{
}

- (void)revertChanges;
{
}

- (void)finishedRevertingChanges;
{
    [window setDocumentEdited:NO];
}

- (void)revert;
{
    [self prepareToRevertChanges];
    [self revertChanges];
    [self finishedRevertingChanges];
}

- (void)revert:sender;
{
    NS_DURING {
        [self revert];
    } NS_HANDLER {
    } NS_ENDHANDLER;
}

@end


@implementation WindowController (NSControlDelegate)

- (void)controlTextDidBeginEditing:(NSNotification *)notification;
{
    [window setDocumentEdited:YES];
}

@end


@implementation WindowController (NSWindowDelegate)

- (BOOL)windowShouldClose:sender;
{
    BOOL shouldClose;

    shouldClose = YES;

    [window makeFirstResponder:nil];
    if ([window isDocumentEdited]) {
        [window makeKeyAndOrderFront:nil];
        switch (NSRunAlertPanel (nil, closingUnsavedWindowString, saveString, abandonChangesString, cancelString, [window title])) {
            
            case NSAlertDefaultReturn:
                NS_DURING {
                    [self save];
                } NS_HANDLER {
                    shouldClose = NO;
                } NS_ENDHANDLER;
                break;
                
            case NSAlertAlternateReturn:
                break;
                
            default:	// cancel
                shouldClose = NO;
                break;
        }
    }
            
    return shouldClose;
}

- (void)windowWillClose:(NSNotification *)notification;
{
}

@end
