#import "fontWindow.h"

@implementation fontWindow

- (id)initWithContentRect:(NSRect)contentRect 
                styleMask:(unsigned int)aStyle 
                  backing:(NSBackingStoreType)bufferingType 
                    defer:(BOOL)flag {
    
    if (self = [super initWithContentRect:contentRect 
                                        styleMask:NSBorderlessWindowMask 
                                          backing:NSBackingStoreBuffered 
                                   defer:NO]) {
        [self setLevel: NSStatusWindowLevel];
        //[self setLevel: NSNormalWindowLevel];
        //[self setLevel: kCGDesktopWindowLevel];// + 1];
        //[self setBackgroundColor: [NSColor clearColor]];
        [self setAlphaValue:1.0];
        //[self setOpaque:NO];
        //[self setHasShadow:NO];
        [self setMovableByWindowBackground:YES];
        //[self setIgnoresMouseEvents:YES];
        [self addCloseWidget];
        [self addMoveWidget];
        return self;
    }
    
    return nil;
}

- (void)addCloseWidget
{
    NSButton *closeButton = [[NSButton alloc] initWithFrame:NSMakeRect(3.0, [self frame].size.height - 28.0, 
                                                                       25.0, 25.0)];
    
    [[self contentView] addSubview:closeButton];
    [closeButton setBezelStyle:NSRoundedBezelStyle];
    [closeButton setButtonType:NSMomentaryChangeButton];
    [closeButton setBordered:NO];
    [closeButton setImage:[NSImage imageNamed:@"remove"]];
    [closeButton setTitle:@""];
    [closeButton setImagePosition:NSImageBelow];
    [closeButton setTarget:self];
    [closeButton setFocusRingType:NSFocusRingTypeNone];
    //[closeButton setAction:@selector(orderOut:)];
    [closeButton release];
}

- (void)addMoveWidget
{
    //NSButton *moveButton = [[NSButton alloc] initWithFrame:NSMakeRect(3.0, [self frame].size.height - 56.0, 
                                                                       //25.0, 25.0)];
    NSImageView *move = [[NSImageView alloc]initWithFrame:NSMakeRect(3.0, [self frame].size.height - 56.0, 
                                                                         25.0, 25.0)];
    [move setImage:[NSImage imageNamed:@"move"]];
    [[self contentView] addSubview:move];
    /*
    [[self contentView] addSubview:moveButton];
    [moveButton setBezelStyle:NSRoundedBezelStyle];
    [moveButton setButtonType:NSMomentaryChangeButton];
    [moveButton setBordered:NO];
    [moveButton setImage:[NSImage imageNamed:@"move"]];
    [moveButton setTitle:@""];
    [moveButton setImagePosition:NSImageBelow];
    [moveButton setTarget:self];
    [moveButton setFocusRingType:NSFocusRingTypeNone];
    //[moveButton setAction:@selector(orderOut:)];
    [moveButton release];*/
}


//- (void) printFont

- (BOOL) canBecomeKeyWindow
{
    return YES;
}


- (void)mouseDragged:(NSEvent *)theEvent
{
    NSPoint currentLocation;
    NSPoint newOrigin;
    NSRect  screenFrame = [[NSScreen mainScreen] frame];
    NSRect  windowFrame = [self frame];
    
    currentLocation = [self convertBaseToScreen:[self mouseLocationOutsideOfEventStream]];
    newOrigin.x = currentLocation.x - initialLocation.x;
    newOrigin.y = currentLocation.y - initialLocation.y;
    
    if( (newOrigin.y + windowFrame.size.height) > (NSMaxY(screenFrame) - [NSMenuView menuBarHeight]) ){
        // Prevent dragging into the menu bar area
	newOrigin.y = NSMaxY(screenFrame) - windowFrame.size.height - [NSMenuView menuBarHeight];
    }
    /*
    if (newOrigin.y < NSMinY(screenFrame)) {
        // Prevent dragging off bottom of screen
        newOrigin.y = NSMinY(screenFrame);
    }
    if (newOrigin.x < NSMinX(screenFrame)) {
        // Prevent dragging off left of screen
        newOrigin.x = NSMinX(screenFrame);
    }
    if (newOrigin.x > NSMaxX(screenFrame) - windowFrame.size.width) {
        // Prevent dragging off right of screen
        newOrigin.x = NSMaxX(screenFrame) - windowFrame.size.width;
    }
    */
    
    [self setFrameOrigin:newOrigin];
}


- (void)mouseDown:(NSEvent *)theEvent
{    
    //NSLog(@"click!");
    NSRect windowFrame = [self frame];
    
    // Get mouse location in global coordinates
    initialLocation = [self convertBaseToScreen:[theEvent locationInWindow]];
    initialLocation.x -= windowFrame.origin.x;
    initialLocation.y -= windowFrame.origin.y;
}


@end
