source: releases/F-Script Anywhere/1.1.2a1/Source/FSAController.m@ 510

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

F-Script Anywhere 1.1.2a1

File size: 6.8 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
31@class ShellView;
32
33// XXX workaround for lack of focus on FSInterpreterView
34@interface CLIView : NSView
35- (ShellView *)shellView;
36@end
37
38@interface FSInterpreterView (Private)
39- (CLIView *)cliView;
40@end
41
42@implementation FSAController
43
44// derived from TETextWatcher.m in Mike Ferris's TextExtras
45+ (void)installMenu;
46{
47 static BOOL alreadyInstalled = NO;
48 NSMenu *mainMenu = nil;
49
50 if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) {
51 NSMenu *insertIntoMenu = nil;
52 NSMenuItem *item;
53 unsigned insertLoc = NSNotFound;
54 NSBundle *bundle = [NSBundle bundleForClass:self];
55 NSMenu * beforeSubmenu = [NSApp windowsMenu];
56 // Succeed or fail, we do not try again.
57 alreadyInstalled = YES;
58
59 // 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.)
60
61 if (!beforeSubmenu) {
62 beforeSubmenu = [NSApp servicesMenu];
63 }
64
65 insertIntoMenu = mainMenu;
66
67 if (beforeSubmenu) {
68 NSArray *itemArray = [insertIntoMenu itemArray];
69 unsigned i, c = [itemArray count];
70
71 // Default to end of menu
72 insertLoc = c;
73
74 for (i=0; i<c; i++) {
75 if ([[itemArray objectAtIndex:i] target] == beforeSubmenu) {
76 insertLoc = i;
77 break;
78 }
79 }
80 } else {
81 NSArray *itemArray = [insertIntoMenu itemArray];
82 unsigned i = [itemArray count];
83
84 // Default to end of menu
85 insertLoc = i;
86
87 while (i-- > 0) {
88 if ([[itemArray objectAtIndex:i] hasSubmenu]) {
89 insertLoc = i+1;
90 break;
91 }
92 }
93 }
94 if (insertIntoMenu) {
95 NSMenu *fsaMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:NSLocalizedStringFromTableInBundle(@"FSA", @"FSA", bundle, @"Title of F-Script Anywhere menu")];
96
97 item = [insertIntoMenu insertItemWithTitle:NSLocalizedStringFromTableInBundle(@"FSA", @"FSA", bundle, @"Title of F-Script Anywhere menu") action:NULL keyEquivalent:@"" atIndex:insertLoc];
98 [insertIntoMenu setSubmenu:fsaMenu forItem:item];
99 [fsaMenu release];
100
101 // Add the items for the commands.
102 item = [fsaMenu addItemWithTitle:NSLocalizedStringFromTableInBundle(@"New F-Script Workspace", @"FSA", bundle, @"Title of F-Script Workspace menu item") action:@selector(createInterpreterWindow:) keyEquivalent:@""];
103 [item setTarget: self];
104 [fsaMenu addItem: [NSMenuItem separatorItem]];
105 item = [fsaMenu addItemWithTitle:NSLocalizedStringFromTableInBundle(@"About F-Script AnywhereÉ", @"FSA", bundle, @"Title of Info Panel menu item") action:@selector(showInfo:) keyEquivalent:@""];
106 [item setTarget: self];
107 }
108 }
109
110}
111
112+ (BOOL)validateMenuItem:(id <NSMenuItem>)menuItem
113{
114 SEL sel;
115 NSAssert([menuItem target] == self, @"menu item does not target FSAController!");
116 sel = [menuItem action];
117 if (sel == @selector(showInfo:) || sel == @selector(createInterpreterWindow:)) return YES;
118 FSALog(@"-[FSAController validateMenuItem:] unknown menu item for validation: %@", menuItem);
119 return NO;
120}
121
122+ (void)createInterpreterWindow:(id)sender;
123{
124 [[self alloc] init];
125}
126
127+ (void)showInfo:(id)sender;
128{
129 int result = NSRunInformationalAlertPanel(@"About F-Script Anywhere", @"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);
130 if (result == NSAlertAlternateReturn) {
131 [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FSA_FScriptURL]];
132 }
133}
134
135+ (void)load;
136{
137 [self installMenu];
138}
139
140- (id)init {
141 self = [super initWithWindowNibName: @"FSAInterpreterPanel"];
142
143 if (self != nil) {
144 NSString *appName = nil;
145 NSWindow *window = [self window];
146
147 NSAssert(window != nil, @"Can't get interpreter window!");
148 NS_DURING
149 appName = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleName"];
150 NS_HANDLER
151 FSALog(@"Exception occurred while trying to obtain application name: %@", localException);
152 NS_ENDHANDLER
153 if (appName != nil) {
154 [window setTitle: [NSString stringWithFormat: @"%@: %@", [window title], appName]];
155 }
156
157 [window setLevel: NSNormalWindowLevel]; // XXX if set floating, it is globally floating!
158 [self showWindow: self];
159 [window makeKeyAndOrderFront: self];
160#warning this should go away when F-Script properly accepts firstResponder on the InterpreterView
161 [window makeFirstResponder: (NSView *)[[[self interpreterView] cliView] shellView]];
162 }
163
164 return self;
165}
166
167- (FSInterpreterView *)interpreterView;
168{
169 return interpreterView;
170}
171
172- (IBAction)setFloating:(id)sender;
173{
174 [[self window] setLevel: [sender state] == NSOnState ? NSFloatingWindowLevel : NSNormalWindowLevel];
175}
176
177- (IBAction)associateInterface:(id)sender;
178{
179 FSALog(@"associating interface");
180 NS_DURING
181 FSALog(@"controller?");
182 if (viewAssociationController == nil) {
183 NSLog(@"creating controller");
184 viewAssociationController = [[FSAViewAssociationController alloc] initWithInterpreter: [interpreterView interpreter]];
185 }
186 FSALog(@"got controller %@", viewAssociationController);
187 [viewAssociationController showWindow: self];
188 NS_HANDLER
189 FSALog(@"%@", localException);
190 NS_ENDHANDLER
191}
192
193@end
Note: See TracBrowser for help on using the repository browser.