libs-gui/Source/NSToolbar.m

321 lines
7.3 KiB
Mathematica
Raw Normal View History

/*
<title>NSToolbar.m</title>
<abstract>The toolbar class.</abstract>
Copyright (C) 2002 Free Software Foundation, Inc.
Author: Gregory John Casamento <greg_casamento@yahoo.com>,
Fabien Vallon <fabien.vallon@fr.alcove.com>
Date: May 2002
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <Foundation/NSObject.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSException.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSUserDefaults.h>
#include "AppKit/NSToolbarItem.h"
#include "AppKit/NSToolbar.h"
#include "AppKit/NSView.h"
#include "AppKit/NSButton.h"
#include "AppKit/NSNibLoading.h"
#include "GNUstepGUI/GSToolbarView.h"
// internal
static NSNotificationCenter *nc = nil;
static const int current_version = 1;
@implementation GSToolbarView
- (id) initWithToolbar: (NSToolbar *)toolbar
{
if((self = [super init]) != nil)
{
ASSIGN(_toolbar,toolbar);
}
return self;
}
- (void) dealloc
{
RELEASE(_toolbar);
[super dealloc];
}
- (void) setToolbar: (NSToolbar *)toolbar
{
ASSIGN(_toolbar, toolbar);
}
- (NSToolbar *) toolbar
{
return _toolbar;
}
- (void) drawRect: (NSRect)aRect
{
NSArray *items = [_toolbar items];
NSEnumerator *en = [items objectEnumerator];
id item = nil;
float x = 0;
[super drawRect: aRect];
while((item = [en nextObject]) != nil)
{
NSView *itemView = [item view];
NSRect itemFrame = [itemView frame];
// now we need to draw the items...
itemFrame.origin.x = x; // start at x
itemFrame.origin.y = 0; // reset to top of view.
[itemView drawRect: itemFrame];
x += NSWidth(itemFrame) + 2; // move over by the frame width plus 2 pixels.
}
}
@end
@implementation NSToolbar (GNUstepPrivate)
- (id) _toolbarView
{
return _toolbarView;
}
@end
@implementation NSToolbar
// Initialize the class when it is loaded
+ (void) initialize
{
if (self == [NSToolbar class])
{
[self setVersion: current_version];
nc = [NSNotificationCenter defaultCenter];
}
}
// Instance methods
- (BOOL) allowsUserCustomization
{
return _allowsUserCustomization;
}
- (BOOL) autosavesConfiguration
{
return _autosavesConfiguration;
}
- (NSDictionary *) configurationDictionary
{
return _configurationDictionary;
}
- (BOOL) customizationPaletteIsRunning
{
return _customizationPaletteIsRunning;
}
- (id) delegate
{
return _delegate;
}
- (NSToolbarDisplayMode) displayMode
{
return _displayMode;
}
- (NSString *) identifier
{
return _identifier;
}
- (void) _loadConfig
{
if(_identifier != nil)
{
NSUserDefaults *defaults;
NSString *tableKey;
id config;
defaults = [NSUserDefaults standardUserDefaults];
tableKey = [NSString stringWithFormat: @"NSToolbar Config %@",
_identifier];
config = [defaults objectForKey: tableKey];
if (config != nil)
{
[self setConfigurationFromDictionary: config];
}
}
}
- (id) initWithIdentifier: (NSString*)identifier
{
[super init];
_customizationPaletteIsRunning = NO;
_allowsUserCustomization = NO;
_autosavesConfiguration = NO;
_configurationDictionary = nil;
_delegate = nil;
_displayMode = NSToolbarDisplayModeDefault;
_visible = YES;
_items = nil;
_visibleItems = nil;
ASSIGN(_identifier, identifier);
[self _loadConfig];
return self;
}
- (void) dealloc
{
DESTROY (_identifier);
DESTROY (_configurationDictionary);
if (_delegate != nil)
{
[nc removeObserver: _delegate name: nil object: self];
_delegate = nil;
}
[super dealloc];
}
- (void) insertItemWithItemIdentifier: (NSString *)itemIdentifier
atIndex: (int)index
{
NSToolbarItem *item = nil;
NSArray *allowedItems = [_delegate toolbarAllowedItemIdentifiers: self];
if([allowedItems containsObject: itemIdentifier])
{
item = [_delegate toolbar: self
itemForItemIdentifier: itemIdentifier
willBeInsertedIntoToolbar: YES];
[nc postNotificationName: NSToolbarWillAddItemNotification
object: self];
[_items insertObject: item atIndex: index];
}
}
- (BOOL) isVisible
{
return _visible;
}
- (NSArray *) items
{
return _items;
}
- (void) removeItemAtIndex: (int)index
{
id obj = [_items objectAtIndex: index];
[_items removeObjectAtIndex: index];
[_visibleItems removeObject: obj];
[nc postNotificationName: NSToolbarDidRemoveItemNotification
object: self];
}
- (void) runCustomizationPalette: (id)sender
{
_customizationPaletteIsRunning = [NSBundle loadNibNamed: @"GSToolbarCustomizationPalette"
owner: self];
if(!_customizationPaletteIsRunning)
{
NSLog(@"Failed to load gorm for GSToolbarCustomizationPalette");
}
}
- (void) setAllowsUserCustomization: (BOOL)flag
{
_allowsUserCustomization = flag;
}
- (void) setAutosavesConfiguration: (BOOL)flag
{
_autosavesConfiguration = flag;
}
- (void) setConfigurationFromDictionary: (NSDictionary *)configDict
{
ASSIGN(_configurationDictionary, configDict);
}
/**
* Sets the receivers delgate ... this is the object which will receive
* -toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:
* -toolbarAllowedItemIdentifiers: and -toolbarDefaultItemIdentifiers:
* messages.
*/
- (void) setDelegate: (id)delegate
{
#define CHECK_REQUIRED_METHOD(selector_name) \
if (![delegate respondsToSelector: @selector(selector_name)]) \
[NSException raise: NSInternalInconsistencyException \
format: @"delegate does not respond to %@",@#selector_name]
CHECK_REQUIRED_METHOD(toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:);
CHECK_REQUIRED_METHOD(toolbarAllowedItemIdentifiers:);
CHECK_REQUIRED_METHOD(toolbarDefaultItemIdentifiers:);
if (_delegate)
[nc removeObserver: _delegate name: nil object: self];
_delegate = delegate;
#define SET_DELEGATE_NOTIFICATION(notif_name) \
if ([_delegate respondsToSelector: @selector(toolbar##notif_name:)]) \
[nc addObserver: _delegate \
selector: @selector(toolbar##notif_name:) \
name: NSToolbar##notif_name##Notification object: self]
SET_DELEGATE_NOTIFICATION(DidRemoveItem);
SET_DELEGATE_NOTIFICATION(WillAddItem);
}
- (void) setDisplayMode: (NSToolbarDisplayMode)displayMode
{
_displayMode = displayMode;
}
- (void) setVisible: (BOOL)shown
{
_visible = shown;
}
- (void) validateVisibleItems
{
NSEnumerator *en = [_visibleItems objectEnumerator];
NSToolbarItem *item = nil;
while((item = [en nextObject]) != nil)
{
[item validate];
}
}
- (NSArray *) visibleItems
{
return _visibleItems;
}
@end /* interface of NSToolbar */