/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ /* AbiSource Application Framework * Copyright (C) 2005 Francis James Franklin * * This program 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. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #import #import "AbiSite.h" #import "AbiSite_Controller.h" #import "AbiSite_SiteController.h" @implementation AbiSite_Controller - (id)initWithAbiWord:(XAP_CocoaPlugin *)AbiWord { if (self = [super initWithWindowNibName:@"Main"]) { m_AbiWord = AbiWord; m_SiteList = [[NSMutableArray alloc] initWithCapacity:16]; m_UntitledNumber = 0; m_MenuAttached = NO; } return self; } - (void)dealloc { if (m_MenuAttached) { [m_AbiWord removeMenuItem:oSiteManagerItem]; m_MenuAttached = NO; } if (m_SiteList) { [m_SiteList release]; m_SiteList = 0; } [super dealloc]; } - (void)windowDidLoad { NSMenu * menu = 0; if (menu = [oSiteManagerItem menu]) { [oSiteManagerItem retain]; [menu removeItem:oSiteManagerItem]; } [m_AbiWord appendMenuItem:oSiteManagerItem]; m_MenuAttached = YES; } - (NSString *)chooseDirectory { NSString * directory = nil; NSOpenPanel * panel = [NSOpenPanel openPanel]; [panel setCanChooseDirectories:YES]; [panel setCanChooseFiles:NO]; [panel setAllowsMultipleSelection:NO]; if ([panel runModalForTypes:nil] == NSOKButton) { NSArray * paths = [panel filenames]; if ([paths count] == 1) { directory = (NSString *) [paths objectAtIndex:0]; } } return directory; } - (IBAction)aNewSite:(id)sender { NSString * path = [self chooseDirectory]; if (path) { AbiSite_SiteController * site = [[AbiSite_SiteController alloc] initWithAbiWord:m_AbiWord withController:self]; [m_SiteList addObject:site]; m_UntitledNumber++; [site setUntitled:m_UntitledNumber]; [site setSourceRoot:path]; [site release]; } } - (IBAction)aOpenSite:(id)sender { // TODO } - (IBAction)aLog:(id)sender { NSPanel * panel = (NSPanel *) [self window]; if ([panel isVisible]) [panel orderOut:self]; else [panel orderFront:self]; } - (IBAction)aAbout:(id)sender { // TODO } - (IBAction)aHelp:(id)sender { NSBundle * AbiSite_Bundle = [NSBundle bundleForClass:[self class]]; NSString * HelpIndex_Path = [[AbiSite_Bundle resourcePath] stringByAppendingString:@"/help/index.html"]; [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:HelpIndex_Path]]; } - (BOOL)validateMenuItem:(id )menuItem { if ([menuItem tag] == 4) { NSPanel * panel = (NSPanel *) [self window]; [menuItem setState:([panel isVisible] ? NSOnState : NSOffState)]; } return YES; } - (void)appendToLog:(id)aString { [oLog setEditable:YES]; [oLog setSelectedRange:NSMakeRange(0,0)]; [oLog moveToEndOfDocument:self]; [oLog insertText:aString]; [oLog setEditable:NO]; } - (void)logEntry:(NSString *)aString forMethod:(NSString *)method { NSString * method_name = [NSString stringWithFormat:@"(%@) ", method]; NSDictionary * attr = [NSDictionary dictionaryWithObject:[NSColor darkGrayColor] forKey:NSForegroundColorAttributeName]; NSAttributedString * str = [[NSAttributedString alloc] initWithString:method_name attributes:attr]; [self appendToLog:str]; [str release]; attr = [NSDictionary dictionaryWithObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName]; str = [[NSAttributedString alloc] initWithString:aString attributes:attr]; [self appendToLog:str]; [str release]; [self appendToLog:@"\n"]; } - (void)logError:(NSString *)aString forMethod:(NSString *)method { NSDictionary * attr = [NSDictionary dictionaryWithObject:[NSColor redColor] forKey:NSForegroundColorAttributeName]; NSAttributedString * str = [[NSAttributedString alloc] initWithString:@"Error: " attributes:attr]; [self appendToLog:str]; [str release]; [self logEntry:aString forMethod:method]; } /* Plugin should attempt to save all files, and return NO *only* if the user has answered * "Cancel" to an alert about an unsaved document. */ - (BOOL)pluginCanDeactivate { return YES; } @end