/* TSShelfDragView.m created by tsengel on Sat 16-Aug-1997 */

#import "TSShelfDragView.h"

#import "TSAppManager.h"
#import "TSShelfWindow.h"
#import "TSShelfItem.h"


@implementation TSShelfDragView

/*"
Used for dragging the shelf up and down and doing all the stuff that should be done somewhere else.
This class is a quick and dirty hack that keeps us from having to subclass the NSMatrix which holds our tabs. This makes it easier to set up the NIB in IB and gets the "job" done faster...while less elegant..sure.
"*/

- (void)establishTrackingRect
/*"
    To be called after the view has been set to final size and final location
"*/
{
    // <<HACK>> Ok..the clean way would be to register for all teh notifiaciton and overwrite all the size methods
    // to properly update our tacking rect...maybe someday...

    NSRect 	trackRect;

    trackRect = [self frame];

    magicY = trackRect.size.height / 2;

    trackRect.origin.x = 0;
    trackRect.origin.y = 2;
    trackRect.size.height -= 2;
    [self addTrackingRect:trackRect
                    owner:self
                 userData:nil
             assumeInside:NO];
}

- (void)mouseExited:(NSEvent *)theEvent
/*"
    Causes the shelf to hide or raise depending on where we left the window.
"*/
{
    NSPoint 	mouseLocation = [self convertPoint:[theEvent locationInWindow] fromView:nil];

    if( mouseLocation.y > magicY )
        [(TSShelfWindow *)[self window] shouldAutoHide];
    else
        [(TSShelfWindow *)[self window] shouldAutoRaise];
}

- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
{
    return YES;
}

- (void)mouseDown:(NSEvent *)theEvent
{
    NSPoint 	mouseLocation, curLocation, offset;
    BOOL		buttonDown;
    NSRect		theFrame;
    id			ourWindow;

    int			row, column;
    BOOL		wasSeriousDrag = YES;

    // Lets check what should happen to our window. The Alt key causes
    // it to switch tier's.

    ourWindow = [self window];
    theFrame = [ourWindow frame];

    if( [theEvent modifierFlags] & NSAlternateKeyMask )
    {
        [ourWindow dropToLowerLevel];
        return;
    }

    // A double click gets us to a "rename" session.

    if( [theEvent clickCount] > 1 )
    {
        mouseLocation = [theEvent locationInWindow];
        mouseLocation = [tabMatrix convertPoint:mouseLocation fromView:nil];
        [tabMatrix getRow:&row column:&column forPoint:mouseLocation];
        [tabMatrix selectCellAtRow:row column:column];
        [tabMatrix sendAction];

        [[NSApp delegate] performSelector:@selector(showRenamingPanel:)
                               withObject:self
                               afterDelay:0.5];
        return;
    }

    // Maybe we should drag the shelf path ??

    mouseLocation = [theEvent locationInWindow];
    curLocation = [self convertPoint:mouseLocation fromView:nil];
    if( curLocation.x > ( [self frame].size.width - 25) )
    {
        // find the current item..save it so that we can be sure that data is valid...and then drag the path
        // reference around.

        id		anItem = [[tabMatrix target] selectedSwapViewItem];
        id		aPath = [anItem path];

        if( [anItem contentDidChange] ) [anItem save];

        if( [[NSFileManager defaultManager] fileExistsAtPath:aPath] )
            [self dragFile:aPath
                  fromRect:NSMakeRect( curLocation.x - 24, curLocation.y - 24, 48, 48 )
                 slideBack:YES
                     event:theEvent];
        return;
    }

    // Seems like it is a drag up and down action...
    // Since we drag the window we can't really take the mosue locations from the event sicne teh coordinate system of
    // the window might have changed since teh event was produced.
    // We will also eat up all MouseExite events to prevent the shelf from popping down or up right after we release
    // the tab view.

    mouseLocation = [ourWindow convertBaseToScreen:mouseLocation];
    offset.y = theFrame.origin.y - mouseLocation.y;

    theEvent = [ourWindow nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask|NSMouseExitedMask];

    while( [theEvent type] != NSLeftMouseUp )
    {
        curLocation = [ourWindow mouseLocationOutsideOfEventStream];
        curLocation = [ourWindow convertBaseToScreen:curLocation];

        theFrame.origin.y = curLocation.y + offset.y;

        // Now check if the mouse button is really still down

        buttonDown = YES;

        if( buttonDown )
            [ourWindow setFrame:theFrame display:YES];
        else	break;

        theEvent = [ourWindow nextEventMatchingMask:NSLeftMouseDraggedMask|NSLeftMouseUpMask|NSMouseExitedMask];
    }

    // If we did not really do a serious drag operation then we should consider
    // activating the new tab so that we can drag & select in the same area.
    // While from the UI point of view..

    if( wasSeriousDrag )
    {
        mouseLocation = [theEvent locationInWindow];
        mouseLocation = [tabMatrix convertPoint:mouseLocation fromView:nil];
        [tabMatrix getRow:&row column:&column forPoint:mouseLocation];
        [tabMatrix selectCellAtRow:row column:column];
        [tabMatrix sendAction];
    }
}

@end
