/****************************************************************************
 *   Copyright (C) 2004 by Arboretum Systems, Inc.                          *
 *   Technical administrator: Alain Mazy, hav@mazy.be                       *
 *   Lead program designer: Georges Jaroslaw, gjaro@arboretum.com           *
 *                                                                          *
 *   HyperEngine-AV is a video, audio and text editor that can be used to   *
 *   record, import, edit, process and arrange together video, sound and    *
 *   text. These are all achieved in the program's free-form document       *
 *   window, with no limitations to the number of tracks.                   *
 *   The purpose of this program is to provide to users at all levels of    *
 *   expertise a readily accessible tool enabling them to create movies,    *
 *   slide shows or presentations.                                          *
 *                                                                          *
 *   HyperEngine-AV itself is free software. However, plug-ins that can be  *
 *   used as add-ons to HyperEngine-AV may or may not be free software, at  *
 *   the discretion of each plug-in developer and as specified by these     *
 *   plug-ins' respective licenses.                                         *
 *   You can redistribute HyperEngine-AV itself and/or modify it under the  *
 *   terms of the GNU General Public License as published by the Free       *
 *   Software Foundation; either version 2 of the License, or (at your      *
 *   option) any later version.                                             *
 *                                                                          *
 *   HyperEngine-AV is distributed in the hope that it will be useful, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for all applicable details.                 *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program; if not, write to:                             *
 *   Free Software Foundation, Inc.,                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
 ****************************************************************************/

#import "MIScreensView.h"
#import "MontageInterface.h"

@implementation MIScreensView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        fScreens = [NSScreen screens];
        
        // calculate desktop bounds and find main screen (our initial selection) index
        // assumes monitor layout won't change while this object exists, but that's safe I imagine
        NSEnumerator *enumerator = [fScreens objectEnumerator];
        NSScreen* screen;
        UInt32 index = 0;
        while ((screen = [enumerator nextObject]))
        {
            if (screen == [NSScreen mainScreen])
                fSelectedScreen = fMainScreen = index;
            
            fMainDesktop = NSUnionRect(fMainDesktop, [screen frame]);
 
            index++;
	}
    }
    return self;
}

- (void)drawRect:(NSRect)rect
{
    const short kMenuBarHeight = 8;
    const short kSelectionRectWidth = 4;
    
    fLocalDesktop = ScaleNSRectToNSRect(fMainDesktop, rect, true);

    NSEnumerator *enumerator = [fScreens objectEnumerator];
    NSScreen* screen;
    NSRect screenRect;
    UInt32 index = 0;
    while ((screen = [enumerator nextObject]))
    {
        screenRect = MapNSRect([screen frame], fMainDesktop,  fLocalDesktop);

        [[NSColor grayColor] set];
        [NSBezierPath setDefaultLineJoinStyle:NSRoundLineJoinStyle];
        if (index == fMainScreen)
            screenRect.size.height -= kMenuBarHeight;
        [NSBezierPath fillRect:screenRect];
        if (index == fMainScreen)
            screenRect.size.height += kMenuBarHeight;

        if (index == fSelectedScreen)
        {
            NSRect selectionRect = NSInsetRect(screenRect, kSelectionRectWidth / 2, kSelectionRectWidth / 2);
            [[NSColor blueColor] set];
            [NSBezierPath setDefaultLineWidth:kSelectionRectWidth];
            [NSBezierPath strokeRect:selectionRect];
        }
        
        [[NSColor blackColor] set];
        [NSBezierPath setDefaultLineWidth:1];
        [NSBezierPath strokeRect:screenRect];
		[NSBezierPath setDefaultLineWidth:0];
  
        index++;
    }
}

- (BOOL)acceptsFirstResponder
{
    return YES;
}

- (void)keyDown:(NSEvent*)theEvent
{
    char key = [[theEvent characters] characterAtIndex:0];
    if (key == '\t')
    {
        fSelectedScreen++;
        if (fSelectedScreen >= [fScreens count])
            fSelectedScreen = 0;
	[self setNeedsDisplay:TRUE];
    }
    else
        [[self nextResponder] keyDown:theEvent];
}

- (void)mouseUp:(NSEvent*)theEvent
{
    // the mouse button has been released

    // convert the mouse location of the event to the local coordinate system
    NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:nil];

    NSEnumerator *enumerator = [fScreens objectEnumerator];
    NSScreen* screen;
    NSRect screenRect;
    UInt32 index = 0;
    while ((screen = [enumerator nextObject]))
    {
        screenRect = MapNSRect([screen frame], fMainDesktop,  fLocalDesktop);
        
        if (IsNSPointInRect(mouseLoc, screenRect))
        {
            fSelectedScreen = index;
            [self setNeedsDisplay:TRUE];
            break;
        }

        index++;
    }
}

- (NSScreen*)getSelectedScreen
{
    return [fScreens objectAtIndex:fSelectedScreen];
}

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

@end
