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

Last change on this file since 103 was 19, checked in by Nicholas Riley, 22 years ago

F-Script Anywhere 1.1.6d1

File size: 11.4 KB
RevLine 
[7]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"
[14]28#import "FSAController.h"
[19]29#import "FSAWindowManager.h"
[14]30#import "FSAnywhere.h"
31#import <FScript/FSInterpreter.h>
32#import <FScript/System.h>
[7]33
[14]34// XXX workaround for lack of identifier validation; should go away when F-Script adds (promised) direct support for this
35@interface Compiler
36+ (BOOL)isValidIndentifier:(NSString *)str;
37@end
38
[7]39@implementation FSAViewAssociationController
40
[14]41- (id)initWithFSAController:(FSAController *)fsa;
[7]42{
43 self = [super initWithWindowNibName: @"FSAViewAssociationPanel"];
44
45 if (self != nil) {
[19]46 NSImage *bullseyeImage = [[NSImage alloc] initByReferencingFile: [[NSBundle bundleForClass: [self class]] pathForResource: @"Bullseye menu cursor" ofType: @"tiff"]];
47 NSString *label = [fsa interpreterLabel];
48
[14]49 interpreter = [[[fsa interpreterView] interpreter] retain];
50 system = [fsa system];
[19]51 [[self window] setResizeIncrements: NSMakeSize(1, 12)];
52 if (label != nil) [[self window] setTitle: [NSString stringWithFormat: @"%@: %@", [[self window] title], label]];
53 bullseyeCursor = [[NSCursor alloc] initWithImage: bullseyeImage hotSpot: NSMakePoint(6, 7)];
[7]54 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(controlTextDidChange:) name: NSControlTextDidChangeNotification object: variableNameField];
[14]55 [[captureButton cell] setShowsStateBy: NSContentsCellMask | NSChangeGrayCellMask];
[19]56 [captureButton setState: NSOffState];
[7]57 [self update: nil];
58 }
59 return self;
60}
61
62- (void)dealloc;
63{
64 [viewHierarchyMenu release];
[19]65 [selectedElement release];
[7]66 [interpreter release];
[19]67 [bullseyeCursor release];
[7]68 [[NSNotificationCenter defaultCenter] removeObserver: self];
69 [super dealloc];
70}
71
72- (IBAction)update:(id)sender;
73{
[14]74 NSString *variableName = [variableNameField stringValue];
75 BOOL canAssignToVariable = NO;
[19]76 [browseButton setEnabled: selectedElement != nil];
[14]77 [statusField setStringValue: @""];
78 if ([variableName length] != 0) {
79 if (![Compiler isValidIndentifier: variableName]) {
80 [statusField setStringValue: @"Invalid name: contains spaces, punctuation or non-ASCII characters"];
[19]81 } else if (selectedElement != nil) {
[14]82 [statusField setStringValue: @"Click ÒAssociateÓ to assign to this variable"];
83 canAssignToVariable = YES;
84 }
85 }
86 [associateButton setEnabled: canAssignToVariable];
[7]87 [variableNameField setEnabled: [captureButton state] == NSOffState];
88}
89
[19]90- (void)stopCapturingVoluntarily:(BOOL)voluntary;
[7]91{
[14]92 FSALog(@"stopping capture");
[19]93 [[NSNotificationCenter defaultCenter] removeObserver: self name: NSMenuWillSendActionNotification object: nil];
[7]94 [captureButton setState: NSOffState];
95 [self update: nil];
[19]96 if (voluntary) {
97 FSALog(@"voluntary!");
98 [[self window] makeKeyAndOrderFront: self];
99 [bullseyeCursor pop];
100 [variableNameField becomeFirstResponder];
101 }
[7]102}
103
[19]104- (void)_addElement:(id)element withLabel:(NSString *)label toSubmenuForItem:(NSMenuItem *)item;
105{
106 NSMenu *submenu = [item submenu];
107 NSMenuItem *subItem;
108 if (submenu == nil) {
109 id superElement = [item representedObject];
110 submenu = [[NSMenu alloc] initWithTitle: @""];
111 subItem = [submenu addItemWithTitle: NSStringFromClass([superElement class])
112 action: @selector(elementSelected:)
113 keyEquivalent: @""];
114 [subItem setTarget: self];
115 [subItem setRepresentedObject: superElement];
116 [item setSubmenu: submenu];
117 [submenu release];
118 }
119 [submenu addItem: [NSMenuItem separatorItem]];
120 [submenu addItemWithTitle: label action: nil keyEquivalent: @""];
121 subItem = [submenu addItemWithTitle: [@" "
122 stringByAppendingString: NSStringFromClass([element class])]
123 action: @selector(elementSelected:)
124 keyEquivalent: @""];
125 [subItem setTarget: self];
126 [subItem setRepresentedObject: element];
127}
128
129- (void)_addValueForSelector:(SEL)sel withLabel:(NSString *)label toSubmenuForItem:(NSMenuItem *)item;
130{
131 id obj = [item representedObject];
132 if ([obj respondsToSelector: sel]) {
133 id value = [obj performSelector: sel];
134 if (value == nil) return;
135 [self _addElement: value withLabel: label toSubmenuForItem: item];
136 }
137}
138
[14]139- (void)_addElementToMenu:(id)element;
140{
141 NSMenuItem *item;
142 if (element == nil) return;
143 item = [viewHierarchyMenu addItemWithTitle: [@" "
144 stringByAppendingString: NSStringFromClass([element class])]
145 action: @selector(elementSelected:)
146 keyEquivalent: @""];
147 [item setTarget: self];
148 [item setRepresentedObject: element];
[19]149 [self _addValueForSelector: @selector(windowController) withLabel: @"Window Controller" toSubmenuForItem: item];
150 [self _addValueForSelector: @selector(delegate) withLabel: @"Delegate" toSubmenuForItem: item];
151 [self _addValueForSelector: @selector(dataSource) withLabel: @"Data Source" toSubmenuForItem: item];
152 [self _addValueForSelector: @selector(target) withLabel: @"Target" toSubmenuForItem: item];
153 [self _addValueForSelector: @selector(cell) withLabel: @"Cell" toSubmenuForItem: item];
[14]154}
155
[7]156- (void)captureOneView;
157{
158 NSEvent *event;
159 NSView *view, *superView = nil, *contentView;
160 NSWindow *eventWindow;
[19]161 static unsigned captureCount = 0;
162 unsigned capture = captureCount++;
163
164 FSALog(@"%4u>capturing one", capture);
[14]165 [captureButton setState: NSOnState];
[19]166 [bullseyeCursor push];
[14]167
[19]168captureElement:
169 [bullseyeCursor set];
170 FSALog(@"%4u waiting for event...", capture);
171 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(menuWillSendAction:) name: NSMenuWillSendActionNotification object: nil];
172 event = [NSApp nextEventMatchingMask: NSLeftMouseDownMask | NSRightMouseDownMask | NSOtherMouseDownMask | NSKeyUpMask | NSAppKitDefinedMask
[7]173 untilDate: [NSDate distantFuture]
174 inMode: NSEventTrackingRunLoopMode
175 dequeue: YES];
[19]176 FSALog(@"%4u got %@", capture, event);
177 [[NSNotificationCenter defaultCenter] removeObserver: self name: NSMenuWillSendActionNotification object: nil];
178 if ([event type] == NSAppKitDefined) {
179 if ([event subtype] == NSApplicationDeactivatedEventType) {
180 [NSApp discardEventsMatchingMask: NSAnyEventMask beforeEvent: event];
181 [self stopCapturingVoluntarily: NO];
182 [NSApp sendEvent: event];
183 return;
184 }
185 goto captureElement;
186 }
[7]187 if ([event type] == NSKeyUp) {
[19]188 [NSApp discardEventsMatchingMask: NSAnyEventMask & ~NSKeyUpMask beforeEvent: event];
189 FSALog(@"%4u<stop capture [key up]", capture);
190 [self stopCapturingVoluntarily: YES];
[7]191 return;
192 }
193 [viewHierarchyMenu release]; viewHierarchyMenu = nil;
194 viewHierarchyMenu = [[NSMenu alloc] initWithTitle: @""];
195 NS_DURING
196 eventWindow = [event window];
197 contentView = [eventWindow contentView];
198 view = [[contentView superview] hitTest: [event locationInWindow]];
199 if (view == captureButton) {
[19]200 [NSApp discardEventsMatchingMask: NSAnyEventMask & ~NSKeyUpMask beforeEvent: event];
201 FSALog(@"%4u<stop capture [capture button]", capture);
202 [self stopCapturingVoluntarily: YES];
[7]203 NS_VOIDRETURN;
204 }
[19]205 if (view == nil) {
206 [self captureOneView];
207 NS_VOIDRETURN;
208 }
[14]209 [viewHierarchyMenu addItemWithTitle: @"View" action: nil keyEquivalent: @""];
210 [self _addElementToMenu: view];
211 superView = view;
[7]212 do {
[14]213 superView = [superView superview];
[7]214 if (superView == nil) break;
[14]215 [self _addElementToMenu: superView];
[7]216 } while (superView != contentView);
217 [viewHierarchyMenu addItem: [NSMenuItem separatorItem]];
[14]218 [viewHierarchyMenu addItemWithTitle: @"Window" action: nil keyEquivalent: @""];
219 [self _addElementToMenu: eventWindow];
[7]220 NS_HANDLER
221 [descriptionField setStringValue:
222 [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
223 NS_ENDHANDLER
224 [NSMenu popUpContextMenu: viewHierarchyMenu withEvent: event forView: view];
[19]225 if ([captureButton state] == NSOnState) goto captureElement;
226 FSALog(@"%4u<stop capture [fell through to end]", capture);
[7]227}
228
229- (IBAction)captureView:(id)sender
230{
[14]231 [statusField setStringValue: @"Click inside one of this applicationÕs windows to select."];
[19]232 [selectedElement release]; selectedElement = nil;
[7]233 [self update: nil];
234 [self captureOneView];
235}
236
[19]237- (void)setSelectedElement:(id)element;
[7]238{
[19]239 FSALog(@"element selected: %@", element);
[7]240 NS_DURING
[14]241 [descriptionField setStringValue: [element description]];
[19]242 [selectedElement release];
243 selectedElement = [element retain];
244 [[self window] orderFront: self];
[7]245 NS_HANDLER
246 [descriptionField setStringValue:
247 [NSString stringWithFormat: @"Çan exception occurred: %@È", localException]];
248 NS_ENDHANDLER
249 [viewHierarchyMenu release]; viewHierarchyMenu = nil;
[19]250}
251
252- (void)elementSelected:(NSMenuItem *)sender;
253{
254 [self setSelectedElement: [sender representedObject]];
[7]255 [self captureOneView];
256}
257
[19]258- (void)menuWillSendAction:(NSNotification *)notification;
259{
260 NSMenuItem *item = [[notification userInfo] objectForKey: @"MenuItem"];
261 [self setSelectedElement: item];
262 [NSApp discardEventsMatchingMask: NSAnyEventMask beforeEvent: [NSApp currentEvent]];
263 // we're already capturing, don't do it again
264}
265
266- (void)controlTextDidChange:(NSNotification *)notification;
267{
268 [self update: nil];
269}
270
[14]271- (IBAction)defineVariable:(id)sender;
[7]272{
[14]273#warning this should change when F-Script supports a public API for identifier validation
[7]274 NS_DURING
275 NSString *variableName = [variableNameField stringValue];
[14]276 [statusField setStringValue: @"AssociatingÉ"];
[19]277 [interpreter setObject: selectedElement forIdentifier: variableName];
[7]278 [statusField setStringValue: [NSString stringWithFormat: @"Assigned variable Ò%@Ó", variableName]];
279 NS_HANDLER
280 [statusField setStringValue: [NSString stringWithFormat: @"Assocation failed: %@", localException]];
281 NS_ENDHANDLER
282}
283
[14]284- (IBAction)viewInObjectBrowser:(id)sender;
285{
286 FSALog(@"system: %@", system);
[19]287 [system browse: selectedElement];
[14]288 [statusField setStringValue: @"Opened object browser"];
289}
290
[19]291@end
Note: See TracBrowser for help on using the repository browser.