source: trunk/Cocoa/F-Script Anywhere/Source/FSAViewAssociationController.m @ 7

Last change on this file since 7 was 7, checked in by Nicholas Riley, 18 years ago

F-Script Anywhere 1.1.2a1

File size: 5.3 KB
Line 
1//
2//  FSAViewAssociationController.m
3//  F-Script Anywhere
4//
5//  Created by Nicholas Riley on Wed Jul 17 2002.
6//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
7//
8
9/*
10
11 F-Script Anywhere is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 F-Script Anywhere is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with F-Script Anywhere; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25*/
26
27#import "FSAViewAssociationController.h"
28
29@implementation FSAViewAssociationController
30
31- (id)initWithInterpreter:(FSInterpreter *)anInterpreter;
32{
33    self = [super initWithWindowNibName: @"FSAViewAssociationPanel"];
34
35    if (self != nil) {
36        interpreter = [anInterpreter retain];
37        [self window];
38        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(controlTextDidChange:) name: NSControlTextDidChangeNotification object: variableNameField];
39        [self update: nil];
40    }
41    return self;
42}
43
44- (void)dealloc;
45{
46    [viewHierarchyMenu release];
47    [selectedView release];
48    [interpreter release];
49    [[NSNotificationCenter defaultCenter] removeObserver: self];
50    [super dealloc];
51}
52
53- (IBAction)update:(id)sender;
54{
55    [associateButton setEnabled: selectedView != nil && [[variableNameField stringValue] length] != 0];
56    [variableNameField setEnabled: [captureButton state] == NSOffState];
57}
58
59- (void)stopCapturing;
60{
61    [captureButton setState: NSOffState];
62    [statusField setStringValue: @""];
63    [self update: nil];
64    [variableNameField becomeFirstResponder];
65}
66
67- (void)captureOneView;
68{
69    NSEvent *event;
70    NSView *view, *superView = nil, *contentView;
71    NSWindow *eventWindow;
72    NSMenuItem *item;
73
74    event = [NSApp nextEventMatchingMask: NSLeftMouseDownMask | NSRightMouseDownMask | NSKeyUpMask
75                               untilDate: [NSDate distantFuture]
76                                  inMode: NSEventTrackingRunLoopMode
77                                 dequeue: YES];
78    if ([event type] == NSKeyUp) {
79        [self stopCapturing];
80        return;
81    }
82    [viewHierarchyMenu release]; viewHierarchyMenu = nil;
83    viewHierarchyMenu = [[NSMenu alloc] initWithTitle: @""];
84    NS_DURING
85        eventWindow = [event window];
86        contentView = [eventWindow contentView];
87        view = [[contentView superview] hitTest: [event locationInWindow]];
88        if (view == captureButton) {
89            [self stopCapturing];
90            NS_VOIDRETURN;
91        }
92        do {
93            if (superView == nil) superView = view;
94            else superView = [superView superview];
95            if (superView == nil) break;
96            item = [viewHierarchyMenu addItemWithTitle: NSStringFromClass([superView class])
97                                                action: @selector(viewSelected:)
98                                         keyEquivalent: @""];
99            [item setTarget: self];
100            [item setRepresentedObject: superView];
101        } while (superView != contentView);
102        [viewHierarchyMenu addItem: [NSMenuItem separatorItem]];
103        item = [viewHierarchyMenu addItemWithTitle: NSStringFromClass([eventWindow class])
104                                            action: @selector(viewSelected:)
105                                     keyEquivalent: @""];
106        [item setTarget: self];
107        [item setRepresentedObject: eventWindow];
108    NS_HANDLER
109        [descriptionField setStringValue:
110            [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
111    NS_ENDHANDLER
112    [NSMenu popUpContextMenu: viewHierarchyMenu withEvent: event forView: view];
113}
114
115- (IBAction)captureView:(id)sender
116{
117    [statusField setStringValue: @"Click to select; press any key to cancel."];
118    [selectedView release]; selectedView = nil;
119    [self update: nil];
120    [self captureOneView];
121}
122
123- (void)viewSelected:(NSMenuItem *)sender;
124{
125    NSView *view = [sender representedObject];
126    NS_DURING
127        [descriptionField setStringValue: [view description]];
128        [selectedView release];
129        selectedView = [view retain];
130    NS_HANDLER
131        [descriptionField setStringValue:
132            [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
133    NS_ENDHANDLER
134    [viewHierarchyMenu release]; viewHierarchyMenu = nil;
135    [self captureOneView];
136}
137
138- (IBAction)defineVariable:(id)sender
139{
140    [statusField setStringValue: @"AssociatingÉ"];
141    NS_DURING
142        NSString *variableName = [variableNameField stringValue];
143        [interpreter setObject: selectedView forIdentifier: variableName];
144        [statusField setStringValue: [NSString stringWithFormat: @"Assigned variable Ò%@Ó", variableName]];
145    NS_HANDLER
146        [statusField setStringValue: [NSString stringWithFormat: @"Assocation failed: %@", localException]];
147    NS_ENDHANDLER
148}
149
150- (void)controlTextDidChange:(NSNotification *)notification;
151{
152    [self update: nil];
153}
154
155@end
Note: See TracBrowser for help on using the repository browser.