//
//  FSAController.m
//  F-Script Anywhere
//
//  Created by Nicholas Riley on Fri Feb 01 2002.
//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
//

/*

 F-Script Anywhere is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 F-Script Anywhere is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with F-Script Anywhere; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

#import "FSAController.h"
#import "FSAViewAssociationController.h"
#import "FSAnywhere.h"
#import "FSAWindowManager.h"
#import <FScript/FSInterpreter.h>

@class ShellView;

// XXX workaround for lack of focus on FSInterpreterView
@interface CLIView : NSView
- (ShellView *)shellView;
@end

@interface FSInterpreterView (FSAWorkaround)
- (CLIView *)cliView;
@end

@interface NSWindow (RoundedCorners)
- (void)setBottomCornerRounded:(BOOL)yn;
@end

@implementation FSAController

// derived from TETextWatcher.m in Mike Ferris's TextExtras
+ (void)installMenu;
{
    static BOOL alreadyInstalled = NO;
    NSMenu *mainMenu = nil;
    
    if (!alreadyInstalled && ((mainMenu = [NSApp mainMenu]) != nil)) {
        NSMenu *insertIntoMenu = nil;
        NSMenuItem *item;
        unsigned insertLoc = NSNotFound;
        NSBundle *bundle = [NSBundle bundleForClass: self];
        NSMenu * beforeSubmenu = [NSApp windowsMenu];
        // Succeed or fail, we do not try again.
        alreadyInstalled = YES;

        // 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.)

        if (!beforeSubmenu) {
            beforeSubmenu = [NSApp servicesMenu];
        }

        insertIntoMenu = mainMenu;

        if (beforeSubmenu) {
            NSArray *itemArray = [insertIntoMenu itemArray];
            unsigned i, c = [itemArray count];

            // Default to end of menu
            insertLoc = c;

            for (i=0; i<c; i++) {
                if ([[itemArray objectAtIndex:i] target] == beforeSubmenu) {
                    insertLoc = i;
                    break;
                }
            }
        } else {
            NSArray *itemArray = [insertIntoMenu itemArray];
            unsigned i = [itemArray count];

            // Default to end of menu
            insertLoc = i;

            while (i-- > 0) {
                if ([[itemArray objectAtIndex:i] hasSubmenu]) {
                    insertLoc = i+1;
                    break;
                }
            }
        }
        if (insertIntoMenu) {
            NSMenu *fsaMenu = [[NSMenu allocWithZone: [NSMenu menuZone]] initWithTitle:NSLocalizedStringFromTableInBundle(@"FSA", @"FSA", bundle, @"Title of F-Script Anywhere menu")];

            item = [insertIntoMenu insertItemWithTitle: NSLocalizedStringFromTableInBundle(@"FSA", @"FSA", bundle, @"Title of F-Script Anywhere menu") action:NULL keyEquivalent:@"" atIndex:insertLoc];
            [insertIntoMenu setSubmenu:fsaMenu forItem:item];
            [fsaMenu release];

            // Add the items for the commands.
            item = [fsaMenu addItemWithTitle: NSLocalizedStringFromTableInBundle(@"New F-Script Workspace", @"FSA", bundle, @"Title of F-Script Workspace menu item") action:@selector(createInterpreterWindow:) keyEquivalent: @""];
            [item setTarget: self];
            item = [fsaMenu addItemWithTitle: NSLocalizedStringFromTableInBundle(@"Browser for Target...", @"FSA", bundle, @"Title of F-Script Workspace menu item") action:@selector(createBrowserForSelection:) keyEquivalent: @""];
            [item setTarget: self];
            [fsaMenu addItem: [NSMenuItem separatorItem]];
            item = [fsaMenu addItemWithTitle: NSLocalizedStringFromTableInBundle(@"About F-Script Anywhere", @"FSA", bundle, @"Title of Info Panel menu item") action:@selector(showInfo:) keyEquivalent: @""];
            [item setTarget: self];
            [[FSAWindowManager sharedManager] setWindowMenu: fsaMenu];
        }
    }
    
    // Open up a workspace if the user wants one
    if ([[[[NSUserDefaults standardUserDefaults] persistentDomainForName: @"net.sabi.FScriptAnywhereApp"] objectForKey:@"OpenWorkspaceOnInstall"] boolValue]) {
	[FSAController createInterpreterWindow: nil];
    }
}

+ (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
    SEL sel;
    NSAssert([menuItem target] == self, @"menu item does not target FSAController!");
    sel = [menuItem action];
    if (sel == @selector(showInfo:) || sel == @selector(createInterpreterWindow:) || sel == @selector(createBrowserForSelection:)) return YES;
    FSALog(@"+[FSAController validateMenuItem:] unknown menu item for validation: %@", menuItem);
    return NO;
}

+ (void)createInterpreterWindow:(id)sender;
{
    [[self alloc] initShowingInterpreter:YES];
}

+ (void)createBrowserForSelection:(id)sender;
{
    [[[self alloc] initShowingInterpreter:NO] FSA_associateAndOpenBrowser:sender];
}

+ (void)showInfo:(id)sender;
{
    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);
    if (result == NSAlertAlternateReturn) {
        [[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: FSA_FScriptURL]];
    }
}

- (id)initShowingInterpreter:(BOOL)showInterpreter {
    self = [super initWithWindowNibName: @"FSAInterpreterPanel"];
    
    if (self != nil) {
        NSWindow *window = [self window];
        NSString *label;
        static unsigned numInterpWindows = 0;
	
	if (showInterpreter) {
	    NSAssert(window != nil, @"Can't get interpreter window!");
	    if ([window respondsToSelector: @selector(setBottomCornerRounded:)])
		[window setBottomCornerRounded: NO]; // XXX otherwise we get black areas because the window isn't rounded
	    [window setContentBorderThickness: 30 forEdge: NSMinYEdge];
	    // XXX why oh why is text not vertically centered in standard-sized buttons?
	    [openBrowserButton setFrameSize: NSMakeSize([openBrowserButton frame].size.width, 19)];
	    [associateWithInterfaceButton setFrameSize: NSMakeSize([associateWithInterfaceButton frame].size.width, 19)];
	    [floatButtonCell setBackgroundStyle: NSBackgroundStyleRaised];
	    if (interpreterNum == 0) interpreterNum = ++numInterpWindows;
	    if ( (label = [self interpreterLabel]) != nil) {
		[window setTitle: [NSString stringWithFormat: @"%@: %@", [window title], label]];
	    }
	    
	    [window setLevel: NSNormalWindowLevel]; // XXX if set floating, it is globally floating!
	    [self showWindow: self];
	    [window makeKeyAndOrderFront: self];
#warning this should go away when F-Script properly accepts firstResponder on the InterpreterView
	    [window makeFirstResponder: (NSView *)[[[self interpreterView] cliView] shellView]];
	    [[FSAWindowManager sharedManager] registerWindow: window];
	}
	system = [[[self interpreterView] interpreter] objectForIdentifier: @"sys" found: NULL];
	[system retain];
	NSAssert1([system isKindOfClass: [System class]], @"Initial value bound to identifier 'sys' is not a System object, but %@", system);
    }
    
    return self;
}

- (void)dealloc;
{
    [system release];
    [super dealloc];
}

- (NSString *)interpreterLabel;
{
    static NSString *appName = nil;
    static BOOL retrievedAppName = NO;
    
    if (appName == nil) {
        if (retrievedAppName) return nil;
        NS_DURING
            appName = [[[NSBundle mainBundle] infoDictionary] objectForKey: @"CFBundleName"];
        NS_HANDLER
            FSALog(@"Exception occurred while trying to obtain application name: %@", localException);
        NS_ENDHANDLER
        retrievedAppName = YES;
    }
    if (interpreterNum == 1) return appName;
    return [NSString stringWithFormat: @"%@ [%u]", appName, interpreterNum];
}

- (FSInterpreterView *)interpreterView;
{
    return interpreterView;
}

- (System *)system;
{
    return system;
}

- (IBAction)setFloating:(id)sender;
{
    [[self window] setLevel: [sender state] == NSOnState ? NSFloatingWindowLevel : NSNormalWindowLevel];
}

- (IBAction)FSA_associateAndOpenBrowser:(id)sender
{
    if (viewAssociationController == nil) {
	viewAssociationController = [[FSAViewAssociationController alloc] initWithFSAController: self];
    }
    [viewAssociationController captureOneView];
}

@end

@implementation FSAController (NSWindowDelegate)

- (BOOL)windowShouldClose:(id)sender;
{
    NSEvent *e = [NSApp currentEvent];
    
    if ([e type] == NSKeyDown && [e keyCode] == 53) // Escape
	return NO;
    
    return YES;
}

@end
