/* -*- 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_Controller.h" #import "AbiSite_SiteController.h" #import "AbiSite_Toolbar.h" enum AbiSite_ToolbarButtonTag { AMMTag_Site_New = 1, AMMTag_Site_Open, AMMTag_Site_Save, AMMTag_Site_SaveAs, AMMTag_Site_Close, AMMTag_Site_Rescan, AMMTag_Site_Build }; @interface AbiSite_ToolbarButton : NSButton { NSMenu * m_menu; AbiSite_Toolbar * m_toolbar; NSPoint ButtonMenuPoint[3]; } - (id)initWithFrame:(NSRect)frameRect; - (void)dealloc; - (void)setMenu:(NSMenu *)menu forToolbar:(AbiSite_Toolbar *)toolbar; - (void)mouseDown:(NSEvent *)theEvent; - (void)drawRect:(NSRect)aRect; @end @implementation AbiSite_ToolbarButton - (id)initWithFrame:(NSRect)frameRect { if (self = [super initWithFrame:frameRect]) { m_menu = 0; m_toolbar = 0; ButtonMenuPoint[0].x = frameRect.size.width - 1.0f; ButtonMenuPoint[0].y = frameRect.size.height - 6.0f; ButtonMenuPoint[1].x = frameRect.size.width - 7.0f; ButtonMenuPoint[1].y = frameRect.size.height - 6.0f; ButtonMenuPoint[2].x = frameRect.size.width - 4.0f; ButtonMenuPoint[2].y = frameRect.size.height - 1.0f; } return self; } - (void)dealloc { if (m_menu) { [m_menu release]; m_menu = 0; } [super dealloc]; } - (void)setMenu:(NSMenu *)menu forToolbar:(AbiSite_Toolbar *)toolbar { if (m_menu) { [m_menu release]; m_menu = 0; } if (menu) { m_menu = menu; [m_menu retain]; } m_toolbar = toolbar; } - (void)mouseDown:(NSEvent *)theEvent { if (m_menu && [self isEnabled]) { [self highlight:YES]; NSRect button_frame = [self frame]; NSPoint new_location; new_location.x = 6.0f; new_location.y = button_frame.size.height + 6.0f; new_location = [self convertPoint:new_location toView:[[self window] contentView]]; NSEvent * event = [NSEvent mouseEventWithType:[theEvent type] location:new_location modifierFlags:[theEvent modifierFlags] timestamp:[theEvent timestamp] windowNumber:[theEvent windowNumber] context:[theEvent context] eventNumber:[theEvent eventNumber] clickCount:[theEvent clickCount] pressure:[theEvent pressure]]; [NSMenu popUpContextMenu:m_menu withEvent:event forView:self]; [self highlight:NO]; } else { [super mouseDown:theEvent]; } } - (void)drawRect:(NSRect)aRect { [super drawRect:aRect]; if (m_menu) { NSBezierPath * path = [NSBezierPath bezierPath]; [path moveToPoint:ButtonMenuPoint[0]]; [path lineToPoint:ButtonMenuPoint[1]]; [path lineToPoint:ButtonMenuPoint[2]]; [path closePath]; // [[NSColor whiteColor] set]; // [path stroke]; [[NSColor blackColor] set]; [path fill]; } } @end @implementation AbiSite_Toolbar - (id)initWithController:(AbiSite_Controller *)controller withSite:(AbiSite_SiteController *)site { if (self = [super init]) { m_Controller = controller; m_Site = site; m_Toolbar = [[NSToolbar alloc] initWithIdentifier:@"AbiSite_Toolbar"]; m_TI_Site = [[NSToolbarItem alloc] initWithItemIdentifier:@"AbiSite_Toolbar_Site"]; [self createButtonWithMenu:@"Site" forToolbarItem:m_TI_Site ]; m_Identifiers = [NSArray arrayWithObjects: @"AbiSite_Toolbar_Site", nil]; [m_Identifiers retain]; [m_TI_Site setToolTip:@"Create a new site, open an existing one, or save this."]; NSString * resourcePath = [[NSBundle bundleForClass:[self class]] resourcePath]; NSImage * image_Site = [[NSImage alloc] initWithContentsOfFile:[resourcePath stringByAppendingString:@"/ToolBarImages/Site.png" ]]; [m_TI_Site setImage:image_Site]; [image_Site release]; [m_Toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel]; [m_Toolbar setAllowsUserCustomization:NO]; [m_Toolbar setSizeMode:NSToolbarSizeModeRegular]; [m_Toolbar setVisible:YES]; [m_Toolbar setDelegate:self]; [[m_Site window] setToolbar:m_Toolbar]; } return self; } - (void)dealloc { [m_Toolbar release]; [m_TI_Site release]; [m_Identifiers release]; [super dealloc]; } - (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag { NSToolbarItem * item = m_TI_Site; if ([itemIdentifier isEqualToString:@"AbiSite_Toolbar_Site"]) { item = m_TI_Site; } return item; } - (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar { return m_Identifiers; } - (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar; { return m_Identifiers; } #if 0 - (void)toolbarDidRemoveItem:(NSNotification *)notification // optional { // } - (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar // optional (10.3) { return m_Identifiers; } - (void)toolbarWillAddItem:(NSNotification *)notification // optional { // } #endif - (void)aClick:(id)sender { switch ([sender tag]) { case AMMTag_Site_New: break; case AMMTag_Site_Open: break; case AMMTag_Site_Save: break; case AMMTag_Site_SaveAs: break; case AMMTag_Site_Close: break; case AMMTag_Site_Rescan: break; case AMMTag_Site_Build: break; default: // ?? break; } } - (BOOL)validateMenuItem:(id )menuItem { BOOL bEnabled = YES; switch ([menuItem tag]) { case AMMTag_Site_New: case AMMTag_Site_Open: case AMMTag_Site_Save: case AMMTag_Site_SaveAs: case AMMTag_Site_Close: case AMMTag_Site_Rescan: case AMMTag_Site_Build: break; default: // ?? shouldn't happen... bEnabled = NO; break; } return bEnabled; } - (void)createButtonWithMenu:(NSString *)menuName forToolbarItem:(NSToolbarItem *)toolbarItem { AbiSite_ToolbarButton * button = nil; NSMenu * menu = [self createMenu:menuName]; if (menu) { NSRect frame; frame.origin.x = 0.0f; frame.origin.y = 0.0f; frame.size.width = 48.0f; // bit of a hack, this :-( frame.size.height = 32.0f; button = [[AbiSite_ToolbarButton alloc] initWithFrame:frame]; [button setButtonType:NSMomentaryPushInButton]; [button setImagePosition:NSImageOnly]; [button setBordered:NO]; [button setMenu:menu forToolbar:self]; [toolbarItem setView:button]; [toolbarItem setMinSize:(frame.size)]; [toolbarItem setMaxSize:(frame.size)]; [button release]; } [toolbarItem setLabel:menuName]; [toolbarItem setPaletteLabel:menuName]; } - (NSMenu *)createMenu:(NSString *)menuName { NSMenu * menu = nil; if ([menuName isEqualToString:@"Site"]) { menu = [[NSMenu alloc] initWithTitle:menuName]; [menu addItem:[self createMenuItem:@"New" withTag:((int) AMMTag_Site_New) ]]; [menu addItem:[self createMenuItem:@"Open..." withTag:((int) AMMTag_Site_Open) ]]; [menu addItem:[self createMenuItem:@"Save" withTag:((int) AMMTag_Site_Save) ]]; [menu addItem:[self createMenuItem:@"Save As..." withTag:((int) AMMTag_Site_SaveAs)]]; [menu addItem:[self createMenuItem:@"Close" withTag:((int) AMMTag_Site_Close) ]]; [menu addItem:[NSMenuItem separatorItem]]; [menu addItem:[self createMenuItem:@"Rescan Source" withTag:((int) AMMTag_Site_Rescan)]]; [menu addItem:[self createMenuItem:@"Build Destination" withTag:((int) AMMTag_Site_Build) ]]; [menu autorelease]; } return menu; } - (NSMenuItem *)createMenuItem:(NSString *)itemName withTag:(int)tag { NSMenuItem * item = [[NSMenuItem alloc] initWithTitle:itemName action:@selector(aClick:) keyEquivalent:@""]; [item setTag:tag]; [item setTarget:self]; [item autorelease]; return item; } @end