/* TSShelfWindow.m created by tsengel on Thu 14-Aug-1997 */

#import "TSShelfWindow.h"
#import <AppKit/psopsNeXT.h>

@implementation TSShelfWindow

- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)aStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag
{
    self = [super initWithContentRect:contentRect
                            styleMask:NSBorderlessWindowMask
                              backing:bufferingType
                                defer:NO];

    if( !self ) return nil;

    [self awake];
    return self;
}

- (void)awake
{
    _maxSize = [[[[self screen] deviceDescription] objectForKey:NSDeviceSize] sizeValue];
    _maxSize.height /= 3;
    [self setGrabBorder:20];
}

- (void)awakeFromNib
{
    NSLog( @"Awaking instance or what ??..." );
    [self awake];
}

- (id)awakeAfterUsingCoder:(NSCoder *)aCoder
{
    NSRect		newRect;
    id			realWindow;

    NSLog( @"Replace ourself if the styleMask is not ok..." );

    if( [self styleMask] != NSBorderlessWindowMask )
    {
        newRect = [TSShelfWindow contentRectForFrameRect:[self frame]
                                               styleMask:NSBorderlessWindowMask];

        realWindow = [[[self class] alloc] initWithContentRect:newRect
                                                     styleMask:NSBorderlessWindowMask
                                                       backing:[self backingType]
                                                         defer:NO];
        [self autorelease];
    }
    else
    {
        realWindow = self;
    }
    return realWindow;
}

- (void)setGrabBorder:(float)height
/*"
    Use this method to set the height of the broder whcih must remain visible on screen. This typically should be the height of the tab view.
"*/
{
    grabBorderHeight = height;
   _lowestYOrigin = - [self frame].size.height + grabBorderHeight;

    // Now try to reset our current frame...we propably might have to readjust our position due to this change

    [self setFrame:[self frame] display:[self isVisible]];

    // Mkae us "owner" less... so no more hidden shelf !!

    // PSsetowner( -20, [self windowNumber] );
}

- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
/*"
    This method will ensure that we will always keep our frame in the proper region. This currently means that we will ensure that the window always sticks to the lower screen border.
"*/
{
    fullyHidden = NO;
    fullyVisible = NO;

    frameRect.size = _maxSize;
    frameRect.origin.x = 0;
    if( frameRect.origin.y >= 0 )
    {
        frameRect.origin.y = 0;
        fullyVisible = YES;
    }
    if( frameRect.origin.y <= _lowestYOrigin )
    {
        frameRect.origin.y = _lowestYOrigin;
        fullyHidden = YES;
    }
    [super setFrame:frameRect display:flag];
}

- (void)setAutoRaiseEnabled:(BOOL)flag
{
    doesAutoraise = flag;
}

- (BOOL)isAutoRaiseEnabled
{
    return doesAutoraise;
}

- (void)shouldAutoRaise
{
    NSRect		newFrame;

    if( doesAutoraise && fullyHidden )
    {
        newFrame = [self frame];
        newFrame.origin.y = 0;
        [self setFrame:newFrame display:YES];
        [self orderFrontRegardless];
    }
}

- (void)shouldAutoHide
{
    NSRect		newFrame;

    if( doesAutoraise && fullyVisible )
    {
        newFrame = [self frame];
        newFrame.origin.y = _lowestYOrigin;
        [self setFrame:newFrame display:YES];
    }
}

- (void)dropToLowerLevel
{
    int		currentLevel = [self level];

    switch( currentLevel )
    {
        case TSShelfForegroundWindowLevel:
            [self setLevel:TSShelfNormalWindowLevel];
            break;
        case TSShelfNormalWindowLevel:
            [self setLevel:TSShelfBackgroundWindowLevel];
            break;
        case TSShelfBackgroundWindowLevel:
            [self setLevel:TSShelfForegroundWindowLevel];
            break;

            // To catch other none standard settings we do this...

        default:
            [self setLevel:TSShelfNormalWindowLevel];
    }
    [self orderBack:self];
    [[NSUserDefaults standardUserDefaults] setInteger:[self level] forKey:@"ShelfWindowLevel"];
}

- (void)raiseToHigherLevel
{
    int		currentLevel = [self level];

    switch( currentLevel )
    {
        case TSShelfBackgroundWindowLevel:
            [self setLevel:TSShelfNormalWindowLevel];
            break;
        case TSShelfNormalWindowLevel:
            [self setLevel:TSShelfForegroundWindowLevel];
            break;
        case TSShelfForegroundWindowLevel:
            [self setLevel:TSShelfBackgroundWindowLevel];
            break;

            // To catch other none standard settings we do this...

        default:
            [self setLevel:TSShelfNormalWindowLevel];
    }
    [self orderFront:self];
    [[NSUserDefaults standardUserDefaults] setInteger:[self level] forKey:@"ShelfWindowLevel"];
}

@end
