source: trunk/Cocoa/F-Script Anywhere/Source/FSAController.m @ 224

Last change on this file since 224 was 224, checked in by rchin, 14 years ago

Object For Target... button now works as it should.

File size: 8.2 KB
Line 
1//
2//  FSAController.m
3//  F-Script Anywhere
4//
5//  Created by Nicholas Riley on Fri Feb 01 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 "FSAController.h"
28#import "FSAViewAssociationController.h"
29#import "FSAnywhere.h"
30#import "FSAWindowManager.h"
31#import <FScript/FSInterpreter.h>
32
33@class ShellView;
34
35// XXX workaround for lack of focus on FSInterpreterView
36@interface CLIView : NSView
37- (ShellView *)shellView;
38@end
39
40@interface FSInterpreterView (FSAWorkaround)
41- (CLIView *)cliView;
42@end
43
44@implementation FSAController
45
46// derived from TETextWatcher.m in Mike Ferris's TextExtras
47+ (void)installMenu;
48{
49    static BOOL alreadyInstalled = NO;
50    NSMenu *mainMenu = nil;
51   
52    if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) {
53        NSMenu *insertIntoMenu = nil;
54        id<NSMenuItem> item;
55        unsigned insertLoc = NSNotFound;
56        NSBundle *bundle = [NSBundle bundleForClass: self];
57        NSMenu * beforeSubmenu = [NSApp windowsMenu];
58        // Succeed or fail, we do not try again.
59        alreadyInstalled = YES;
60
61        // Add it to the main menu.  We try to put it right before the Windows menu if there is one, or right before the Services menu if there is one, and if there's neither we put it right before the the last submenu item (ie above Quit and Hide on Mach, at the end on Windows.)
62
63        if (!beforeSubmenu) {
64            beforeSubmenu = [NSApp servicesMenu];
65        }
66
67        insertIntoMenu = mainMenu;
68
69        if (beforeSubmenu) {
70            NSArray *itemArray = [insertIntoMenu itemArray];
71            unsigned i, c = [itemArray count];
72
73            // Default to end of menu
74            insertLoc = c;
75
76            for (i=0; i<c; i++) {
77                if ([[itemArray objectAtIndex:i] target] == beforeSubmenu) {
78                    insertLoc = i;
79                    break;
80                }
81            }
82        } else {
83            NSArray *itemArray = [insertIntoMenu itemArray];
84            unsigned i = [itemArray count];
85
86            // Default to end of menu
87            insertLoc = i;
88
89            while (i-- > 0) {
90                if ([[itemArray objectAtIndex:i] hasSubmenu]) {
91                    insertLoc = i+1;
92                    break;
93                }
94            }
95        }
96        if (insertIntoMenu) {
97            NSMenu *fsaMenu = [[NSMenu allocWithZone: [NSMenu menuZone]] initWithTitle:NSLocalizedStringFromTableInBundle(@"FSA", @"FSA", bundle, @"Title of F-Script Anywhere menu")];
98
99            item = [insertIntoMenu insertItemWithTitle: NSLocalizedStringFromTableInBundle(@"FSA", @"FSA", bundle, @"Title of F-Script Anywhere menu") action:NULL keyEquivalent:@"" atIndex:insertLoc];
100            [insertIntoMenu setSubmenu:fsaMenu forItem:item];
101            [fsaMenu release];
102
103            // Add the items for the commands.
104            item = [fsaMenu addItemWithTitle: NSLocalizedStringFromTableInBundle(@"New F-Script Workspace", @"FSA", bundle, @"Title of F-Script Workspace menu item") action:@selector(createInterpreterWindow:) keyEquivalent: @""];
105            [item setTarget: self];
106            [fsaMenu addItem: [NSMenuItem separatorItem]];
107            item = [fsaMenu addItemWithTitle: NSLocalizedStringFromTableInBundle(@"About F-Script Anywhere...", @"FSA", bundle, @"Title of Info Panel menu item") action:@selector(showInfo:) keyEquivalent: @""];
108            [item setTarget: self];
109            [[FSAWindowManager sharedManager] setWindowMenu: fsaMenu];
110        }
111    }
112
113}
114
115+ (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
116{
117    SEL sel;
118    NSAssert([menuItem target] == self, @"menu item does not target FSAController!");
119    sel = [menuItem action];
120    if (sel == @selector(showInfo:) || sel == @selector(createInterpreterWindow:)) return YES;
121    FSALog(@"+[FSAController validateMenuItem:] unknown menu item for validation: %@", menuItem);
122    return NO;
123}
124
125+ (void)createInterpreterWindow:(id)sender;
126{
127    [[self alloc] init];
128}
129
130+ (void)showInfo:(id)sender;
131{
132    int result = NSRunInformationalAlertPanel([NSString stringWithFormat: @"About F-Script Anywhere (version %s)", FSA_VERSION], @"F-Script Anywhere lets you embed a F-Script interpreter in a Cocoa application while it is running.\n\nF-Script Anywhere is currently installed in this application.  To remove it, quit this application.\n\nFor more information about F-Script, please visit its Web site %@.", @"OK", @"Visit Web Site", nil, FSA_FScriptURL);
133    if (result == NSAlertAlternateReturn) {
134        [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FSA_FScriptURL]];
135    }
136}
137
138- (id)init {
139    self = [super initWithWindowNibName: @"FSAInterpreterPanel"];
140
141    if (self != nil) {
142        NSWindow *window = [self window];
143        NSString *label;
144        static unsigned numInterpWindows = 0;
145
146        NSAssert(window != nil, @"Can't get interpreter window!");
147        if (interpreterNum == 0) interpreterNum = ++numInterpWindows;
148        if ( (label = [self interpreterLabel]) != nil) {
149            [window setTitle: [NSString stringWithFormat: @"%@: %@", [window title], label]];
150        }
151
152        [window setLevel: NSNormalWindowLevel]; // XXX if set floating, it is globally floating!
153        [self showWindow: self];
154        [window makeKeyAndOrderFront: self];
155#warning this should go away when F-Script properly accepts firstResponder on the InterpreterView
156        [window makeFirstResponder: (NSView *)[[[self interpreterView] cliView] shellView]];
157        [[FSAWindowManager sharedManager] registerWindow: window];
158        system = [[[self interpreterView] interpreter] objectForIdentifier: @"sys" found: NULL];
159        [system retain];
160        NSAssert1([system isKindOfClass: [System class]], @"Initial value bound to identifier 'sys' is not a System object, but %@", system);
161    }
162   
163    return self;
164}
165
166- (void)dealloc;
167{
168    [system release];
169    [super dealloc];
170}
171
172- (NSString *)interpreterLabel;
173{
174    static NSString *appName = nil;
175    static BOOL retrievedAppName = NO;
176   
177    if (appName == nil) {
178        if (retrievedAppName) return nil;
179        NS_DURING
180            appName = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleName"];
181        NS_HANDLER
182            FSALog(@"Exception occurred while trying to obtain application name: %@", localException);
183        NS_ENDHANDLER
184        retrievedAppName = YES;
185    }
186    if (interpreterNum == 1) return appName;
187    return [NSString stringWithFormat: @"%@ [%u]", appName, interpreterNum];
188}
189
190- (FSInterpreterView *)interpreterView;
191{
192    return interpreterView;
193}
194
195- (System *)system;
196{
197    return system;
198}
199
200- (IBAction)setFloating:(id)sender;
201{
202    [[self window] setLevel: [sender state] == NSOnState ? NSFloatingWindowLevel : NSNormalWindowLevel];
203}
204
205- (IBAction)FSA_associateAndOpenBrowser:(id)sender
206{
207    if (viewAssociationController == nil) {
208        viewAssociationController = [[FSAViewAssociationController alloc] initWithFSAController: self];
209    }
210    [viewAssociationController captureOneView];
211}
212
213- (IBAction)FSA_associateWithInterface:(id)sender;
214{
215    NS_DURING
216        FSAWindowManager *wm = [FSAWindowManager sharedManager];
217        if (viewAssociationController == nil) {
218            viewAssociationController = [[FSAViewAssociationController alloc] initWithFSAController: self];
219        }
220        [viewAssociationController showWindow: self];
221        if (![wm windowIsRegistered: [viewAssociationController window]]) {
222            [wm registerSubordinateWindow: [viewAssociationController window]
223                                forWindow: [self window]];
224        }
225    NS_HANDLER
226        FSALog(@"%@", localException);
227    NS_ENDHANDLER
228}
229
230@end
Note: See TracBrowser for help on using the repository browser.