//
//  shinyRulerController.m
//  ShinyRuler
//
//  Created by Matteo rattotti on 12/17/07.
//  Copyright 2007 www.shinyfrog.net. All rights reserved.
//

#import "shinyRulerController.h"


EventHandlerRef trackMouseGlobal;

OSStatus mouseActivated(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData)
{
   [[NSApp delegate] mouseMoved];
   return CallNextEventHandler(nextHandler, theEvent);
}

@implementation shinyRulerController

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
    //Add in a global handler
    EventTypeSpec globalEventType[2];
    globalEventType[0].eventClass = kEventClassMouse;
    globalEventType[0].eventKind = kEventMouseMoved;
    globalEventType[1].eventClass = kEventClassMouse;
    globalEventType[1].eventKind = kEventMouseDragged;
   
    EventHandlerUPP globalHandlerFunction = NewEventHandlerUPP(mouseActivated);
    /*OSStatus result = */ InstallEventHandler(GetEventMonitorTarget(), globalHandlerFunction, 2, globalEventType, NULL, &trackMouseGlobal);
}


- (void)mouseMoved{
    NSPoint mousePoint =[NSEvent mouseLocation];
    float markerOriz = -1;
    float markerVer = -1;

    if(mousePoint.x >= [orizontalRuler frame].origin.x && mousePoint.x <= [orizontalRuler frame].origin.x + [orizontalRuler frame].size.width){
       markerOriz = mousePoint.x - [orizontalRuler frame].origin.x;
       }
        
    if(mousePoint.y >= [verticalRuler frame].origin.y && mousePoint.y <= [verticalRuler frame].origin.y + [verticalRuler frame].size.height){
        markerVer = [verticalRuler frame].origin.y + [verticalRuler frame].size.height -mousePoint.y;
        }
    
    [self updateMakerX:markerOriz Y:markerVer];
}

- (void)awakeFromNib{
    // Getting screen bounds
    //NSScreen* mainScreen = [NSScreen mainScreen];
    //NSRect screenBounds = [mainScreen frame];

    /*SFTranFadeWindow *win = [[SFTranFadeWindow alloc] initWithContentRect:screenBounds
                                                        styleMask:NSBorderlessWindowMask
                                                        backing:NSBackingStoreBuffered
                                                        defer:NO];*/
                                                        
    /*win2 = [[SFTranFadeWindow alloc] initWithContentRect:NSMakeRect(400, 300, 400, 45)
                                                        styleMask:NSBorderlessWindowMask
                                                        backing:NSBackingStoreBuffered
                                                        defer:NO];    
    
    SFTranFadeWindow *win = [[SFTranFadeWindow alloc] initWithContentRect:NSMakeRect(400, 300, 45, 400)
                                                        styleMask:NSBorderlessWindowMask
                                                        backing:NSBackingStoreBuffered
                                                        defer:NO];
    

    [win setMinSize:NSMakeSize(45, 100)];
    [win setMaxSize:NSMakeSize(45, 10000)];
    
    
    [win2 setMinSize:NSMakeSize(100, 45)];
    [win2 setMaxSize:NSMakeSize(10000, 45)];*/
    
    //SFContentView *cView = [[SFContentView alloc]initWithFrame:[win frame]];
    //NSRect positions = [cView frame];            
    
    //NSRect rulerRect = NSMakeRect(0.0, 0.0, 200.0, 50.0);
    //SFRulerView *rw = [[SFRulerView alloc] initWithFrame:[self centerRect:rulerRect inRect:[cView frame]]];
    //SFRulerView *rw = [[SFRulerView alloc] initWithFrame:rulerRect];
    //SFRulerView *rw = [[SFRulerView alloc] initWithFrame:[win frame]];
    //SFRulerView *rw2 = [[SFRulerView alloc] initWithFrame:[win2 frame]];
    //NSRect position = [rw frame];            


    //[cView addSubview:rw];
    
    
    //[win setContentView:cView];
    /*[win setContentView:rw];

    [win makeKeyAndOrderFront:self];
    
    [win2 setContentView:rw2];

    [win2 makeKeyAndOrderFront:self];*/

    
}

- (void) updateMakerX:(float) markerX Y:(float) markerY{
    NSLog(@"x -> %f, y -> %f", markerX, markerY);
}

- (void)applicationWillTerminate:(NSNotification *)aNotification
{
   RemoveEventHandler(trackMouseGlobal);
}
@end
