/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ /* CarbonToolbar / Cocoa front-end / AbiWord Plug-in * 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 "AbiCarbonToolbar.h" #import "AbiCarbonToolbar_Configuration.h" #import "AbiCarbonToolbar_EditorController.h" #import "AbiCarbonToolbar_LogController.h" #import "AbiCarbonToolbar_Manager.h" #import "AbiCarbonToolbar_Window.h" @implementation AbiCarbonToolbar_LogController - (id)initWithAbiWord:(XAP_CocoaPlugin *)AbiWord { if (self = [super initWithWindowNibName:@"Main"]) { m_AbiWord = AbiWord; m_Configuration = 0; m_MenuAttached = NO; m_ToolbarWindows = [[NSMutableArray alloc] initWithCapacity:8]; } return self; } - (void)dealloc { [self unloadToolbars]; if (m_MenuAttached) { [m_AbiWord removeMenuItem:oCarbonToolbarItem]; m_MenuAttached = NO; } [m_ToolbarWindows release]; [super dealloc]; } - (void)windowDidLoad { NSMenu * menu = 0; if (menu = [oCarbonToolbarItem menu]) { [oCarbonToolbarItem retain]; [menu removeItem:oCarbonToolbarItem]; } [m_AbiWord appendMenuItem:oCarbonToolbarItem]; m_MenuAttached = YES; [self reloadToolbars]; } - (void)reloadToolbars { if (m_Configuration) { [self unloadToolbars]; } m_Configuration = [[AbiCarbonToolbar_Configuration alloc] initWithAbiWord:m_AbiWord controller:self]; if (m_Configuration) { unsigned count; unsigned i; NSArray * toolbars = 0; NSString * config_path = 0; NSRect screenFrame = [[NSScreen mainScreen] visibleFrame]; NSRect frame; frame.origin.x = screenFrame.origin.x; frame.size.width = 500.0f; frame.size.height = 30.0f; if (config_path = [m_Configuration configurationFile]) { [m_Configuration loadConfiguration:config_path]; } /* create toolbars */ toolbars = [m_Configuration toolbars]; count = [toolbars count]; for (i = 0; i < count; i++) { AbiCarbonToolbar_ConfigurationToolbar * toolbar = (AbiCarbonToolbar_ConfigurationToolbar *) [toolbars objectAtIndex:i]; NSArray * tools = [toolbar tools]; unsigned tool_count = [tools count]; unsigned j; AbiCarbonToolbar_Window * window = 0; AbiCarbonToolbar_Manager * manager = 0; frame.origin.y = screenFrame.origin.y + screenFrame.size.height - (float) (30 * (i + 1)); window = [[AbiCarbonToolbar_Window alloc] initWithTitle:[toolbar name] frame:frame]; manager = [window manager]; /* add tools to toolbar view manager */ for (j = 0; j < tool_count; j++) { AbiCarbonToolbar_ConfigurationTool * tool = (AbiCarbonToolbar_ConfigurationTool *) [tools objectAtIndex:j]; id provider = [tool provider]; NSString * identifier = [tool identifier]; [manager addTool:[provider toolWithIdentifier:identifier]]; } /* TODO: add menu item to Panels menu */ [window toggle:self]; /* TODO: need a preference settings for size and visibility */ [m_ToolbarWindows addObject:window]; } } } - (void)unloadToolbars { /* destroy toolbars */ unsigned count; unsigned i; count = [m_ToolbarWindows count]; for (i = 0; i < count; i++) { AbiCarbonToolbar_Window * window = (AbiCarbonToolbar_Window *) [m_ToolbarWindows objectAtIndex:i]; AbiCarbonToolbar_Manager * manager = [window manager]; [manager removeAllTools]; /* TODO: remove menu item from Panels menu */ [window close]; /* TODO: save size and visibility preferences */ } [m_ToolbarWindows removeAllObjects]; if (m_Configuration) { [m_Configuration release]; m_Configuration = 0; } } - (IBAction)aEditor:(id)sender { AbiCarbonToolbar_EditorController * editor = [[AbiCarbonToolbar_EditorController alloc] initWithAbiWord:m_AbiWord controller:self]; if (editor) { BOOL bCancelled; [NSApp runModalForWindow:[editor window]]; bCancelled = [editor editorWasCancelled]; [editor close]; [editor release]; if (!bCancelled) { [self reloadToolbars]; } } } - (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 * AbiCarbonToolbar_Bundle = [NSBundle bundleForClass:[self class]]; NSString * HelpIndex_Path = [[AbiCarbonToolbar_Bundle resourcePath] stringByAppendingString:@"/help/index.html"]; [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:HelpIndex_Path]]; } - (BOOL)validateMenuItem:(id )menuItem { if ([menuItem tag] == 3) { 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]; } @end