//
//  FSAViewAssociationController.m
//  F-Script Anywhere
//
//  Created by Nicholas Riley on Wed Jul 17 2002.
//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
//

/*

 F-Script Anywhere is free software; you can redistribute it 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.

 F-Script Anywhere 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 more details.

 You should have received a copy of the GNU General Public License
 along with F-Script Anywhere; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#import "FSAViewAssociationController.h"

@implementation FSAViewAssociationController

- (id)initWithInterpreter:(FSInterpreter *)anInterpreter;
{
    self = [super initWithWindowNibName: @"FSAViewAssociationPanel"];

    if (self != nil) {
        interpreter = [anInterpreter retain];
        [self window];
        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(controlTextDidChange:) name: NSControlTextDidChangeNotification object: variableNameField];
        [self update: nil];
    }
    return self;
}

- (void)dealloc;
{
    [viewHierarchyMenu release];
    [selectedView release];
    [interpreter release];
    [[NSNotificationCenter defaultCenter] removeObserver: self];
    [super dealloc];
}

- (IBAction)update:(id)sender;
{
    [associateButton setEnabled: selectedView != nil && [[variableNameField stringValue] length] != 0];
    [variableNameField setEnabled: [captureButton state] == NSOffState];
}

- (void)stopCapturing;
{
    [captureButton setState: NSOffState];
    [statusField setStringValue: @""];
    [self update: nil];
    [variableNameField becomeFirstResponder];
}

- (void)captureOneView;
{
    NSEvent *event;
    NSView *view, *superView = nil, *contentView;
    NSWindow *eventWindow;
    NSMenuItem *item;

    event = [NSApp nextEventMatchingMask: NSLeftMouseDownMask | NSRightMouseDownMask | NSKeyUpMask
                               untilDate: [NSDate distantFuture]
                                  inMode: NSEventTrackingRunLoopMode
                                 dequeue: YES];
    if ([event type] == NSKeyUp) {
        [self stopCapturing];
        return;
    }
    [viewHierarchyMenu release]; viewHierarchyMenu = nil;
    viewHierarchyMenu = [[NSMenu alloc] initWithTitle: @""];
    NS_DURING
        eventWindow = [event window];
        contentView = [eventWindow contentView];
        view = [[contentView superview] hitTest: [event locationInWindow]];
        if (view == captureButton) {
            [self stopCapturing];
            NS_VOIDRETURN;
        }
        do {
            if (superView == nil) superView = view;
            else superView = [superView superview];
            if (superView == nil) break;
            item = [viewHierarchyMenu addItemWithTitle: NSStringFromClass([superView class])
                                                action: @selector(viewSelected:)
                                         keyEquivalent: @""];
            [item setTarget: self];
            [item setRepresentedObject: superView];
        } while (superView != contentView);
        [viewHierarchyMenu addItem: [NSMenuItem separatorItem]];
        item = [viewHierarchyMenu addItemWithTitle: NSStringFromClass([eventWindow class])
                                            action: @selector(viewSelected:)
                                     keyEquivalent: @""];
        [item setTarget: self];
        [item setRepresentedObject: eventWindow];
    NS_HANDLER
        [descriptionField setStringValue:
            [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
    NS_ENDHANDLER
    [NSMenu popUpContextMenu: viewHierarchyMenu withEvent: event forView: view];
}

- (IBAction)captureView:(id)sender
{
    [statusField setStringValue: @"Click to select; press any key to cancel."];
    [selectedView release]; selectedView = nil;
    [self update: nil];
    [self captureOneView];
}

- (void)viewSelected:(NSMenuItem *)sender;
{
    NSView *view = [sender representedObject];
    NS_DURING
        [descriptionField setStringValue: [view description]];
        [selectedView release];
        selectedView = [view retain];
    NS_HANDLER
        [descriptionField setStringValue:
            [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
    NS_ENDHANDLER
    [viewHierarchyMenu release]; viewHierarchyMenu = nil;
    [self captureOneView];
}

- (IBAction)defineVariable:(id)sender
{
    [statusField setStringValue: @"AssociatingÉ"];
    NS_DURING
        NSString *variableName = [variableNameField stringValue];
        [interpreter setObject: selectedView forIdentifier: variableName];
        [statusField setStringValue: [NSString stringWithFormat: @"Assigned variable Ò%@Ó", variableName]];
    NS_HANDLER
        [statusField setStringValue: [NSString stringWithFormat: @"Assocation failed: %@", localException]];
    NS_ENDHANDLER
}

- (void)controlTextDidChange:(NSNotification *)notification;
{
    [self update: nil];
}

@end
