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

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

F-Script Anywhere 1.1.2

File size: 6.8 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#import "FSAController.h"
29#import "FSAnywhere.h"
30#import <FScript/FSInterpreter.h>
31#import <FScript/System.h>
32
33// XXX workaround for lack of identifier validation; should go away when F-Script adds (promised) direct support for this
34@interface Compiler
35+ (BOOL)isValidIndentifier:(NSString *)str;
36@end
37
38@implementation FSAViewAssociationController
39
40- (id)initWithFSAController:(FSAController *)fsa;
41{
42    self = [super initWithWindowNibName: @"FSAViewAssociationPanel"];
43
44    if (self != nil) {
45        interpreter = [[[fsa interpreterView] interpreter] retain];
46        system = [fsa system];
47        [self window];
48        [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(controlTextDidChange:) name: NSControlTextDidChangeNotification object: variableNameField];
49        [[captureButton cell] setShowsStateBy: NSContentsCellMask | NSChangeGrayCellMask];
50        [self update: nil];
51    }
52    return self;
53}
54
55- (void)dealloc;
56{
57    [viewHierarchyMenu release];
58    [selectedView release];
59    [interpreter release];
60    [[NSNotificationCenter defaultCenter] removeObserver: self];
61    [super dealloc];
62}
63
64- (IBAction)update:(id)sender;
65{
66    NSString *variableName = [variableNameField stringValue];
67    BOOL canAssignToVariable = NO;
68    [browseButton setEnabled: selectedView != nil];
69    [statusField setStringValue: @""];
70    if ([variableName length] != 0) {
71        if (![Compiler isValidIndentifier: variableName]) {
72            [statusField setStringValue: @"Invalid name: contains spaces, punctuation or non-ASCII characters"];
73        } else if (selectedView != nil) {
74            [statusField setStringValue: @"Click ÒAssociateÓ to assign to this variable"];
75            canAssignToVariable = YES;
76        }
77    }
78    [associateButton setEnabled: canAssignToVariable];
79    [variableNameField setEnabled: [captureButton state] == NSOffState];
80}
81
82- (void)stopCapturing;
83{
84    FSALog(@"stopping capture");
85    [captureButton setState: NSOffState];
86    [statusField setStringValue: @""];
87    [self update: nil];
88    [variableNameField becomeFirstResponder];
89}
90
91- (void)_addElementToMenu:(id)element;
92{
93    NSMenuItem *item;
94    if (element == nil) return;
95    item = [viewHierarchyMenu addItemWithTitle: [@"  "
96                stringByAppendingString: NSStringFromClass([element class])]
97                                        action: @selector(elementSelected:)
98                                 keyEquivalent: @""];
99    [item setTarget: self];
100    [item setRepresentedObject: element];
101}
102
103- (void)captureOneView;
104{
105    NSEvent *event;
106    NSView *view, *superView = nil, *contentView;
107    NSWindow *eventWindow;
108
109    FSALog(@"capturing one");
110    [captureButton setState: NSOnState];
111
112    event = [NSApp nextEventMatchingMask: NSLeftMouseDownMask | NSRightMouseDownMask | NSKeyUpMask
113                               untilDate: [NSDate distantFuture]
114                                  inMode: NSEventTrackingRunLoopMode
115                                 dequeue: YES];
116    if ([event type] == NSKeyUp) {
117        [self stopCapturing];
118        return;
119    }
120    [viewHierarchyMenu release]; viewHierarchyMenu = nil;
121    viewHierarchyMenu = [[NSMenu alloc] initWithTitle: @""];
122    NS_DURING
123        eventWindow = [event window];
124        contentView = [eventWindow contentView];
125        view = [[contentView superview] hitTest: [event locationInWindow]];
126        if (view == captureButton) {
127            [self stopCapturing];
128            NS_VOIDRETURN;
129        }
130        [viewHierarchyMenu addItemWithTitle: @"View" action: nil keyEquivalent: @""];
131        [self _addElementToMenu: view];
132        superView = view;
133        do {
134            superView = [superView superview];
135            if (superView == nil) break;
136            [self _addElementToMenu: superView];
137        } while (superView != contentView);
138        [viewHierarchyMenu addItem: [NSMenuItem separatorItem]];
139        [viewHierarchyMenu addItemWithTitle: @"Window" action: nil keyEquivalent: @""];
140        [self _addElementToMenu: eventWindow];
141    NS_HANDLER
142        [descriptionField setStringValue:
143            [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
144    NS_ENDHANDLER
145    [NSMenu popUpContextMenu: viewHierarchyMenu withEvent: event forView: view];
146    if ([captureButton state] == NSOnState)
147        [self captureOneView];
148}
149
150- (IBAction)captureView:(id)sender
151{
152    [statusField setStringValue: @"Click inside one of this applicationÕs windows to select."];
153    [selectedView release]; selectedView = nil;
154    [self update: nil];
155    [self captureOneView];
156}
157
158- (void)elementSelected:(NSMenuItem *)sender;
159{
160    NSView *element = [sender representedObject];
161    FSALog(@"element selected: %@", sender);
162    NS_DURING
163        [descriptionField setStringValue: [element description]];
164        [selectedView release];
165        selectedView = [element retain];
166    NS_HANDLER
167        [descriptionField setStringValue:
168            [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
169    NS_ENDHANDLER
170    [viewHierarchyMenu release]; viewHierarchyMenu = nil;
171    [self captureOneView];
172}
173
174- (IBAction)defineVariable:(id)sender;
175{
176#warning this should change when F-Script supports a public API for identifier validation
177    NS_DURING
178        NSString *variableName = [variableNameField stringValue];
179        [statusField setStringValue: @"AssociatingÉ"];
180        [interpreter setObject: selectedView forIdentifier: variableName];
181        [statusField setStringValue: [NSString stringWithFormat: @"Assigned variable Ò%@Ó", variableName]];
182    NS_HANDLER
183        [statusField setStringValue: [NSString stringWithFormat: @"Assocation failed: %@", localException]];
184    NS_ENDHANDLER
185}
186
187- (IBAction)viewInObjectBrowser:(id)sender;
188{
189    FSALog(@"system: %@", system);
190    [system browse: selectedView];
191    [statusField setStringValue: @"Opened object browser"];
192}
193
194- (void)controlTextDidChange:(NSNotification *)notification;
195{
196    [self update: nil];
197}
198
199@end
Note: See TracBrowser for help on using the repository browser.