2002-06-30 05:14:21 +00:00
|
|
|
/*
|
|
|
|
<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>
|
2003-06-13 15:01:12 +00:00
|
|
|
#include "AppKit/NSToolbarItem.h"
|
|
|
|
#include "AppKit/NSToolbar.h"
|
|
|
|
#include "AppKit/NSView.h"
|
|
|
|
#include "AppKit/NSButton.h"
|
|
|
|
#include "AppKit/NSNibLoading.h"
|
2003-08-24 17:22:11 +00:00
|
|
|
#include "GNUstepGUI/GSToolbarView.h"
|
2002-06-30 05:14:21 +00:00
|
|
|
|
2002-12-29 05:38:29 +00:00
|
|
|
// internal
|
2002-06-30 05:14:21 +00:00
|
|
|
static NSNotificationCenter *nc = nil;
|
|
|
|
static const int current_version = 1;
|
|
|
|
|
|
|
|
@implementation GSToolbarView
|
2003-11-11 19:40:18 +00:00
|
|
|
- (id) initWithToolbar: (NSToolbar *)toolbar
|
|
|
|
{
|
|
|
|
if((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
ASSIGN(_toolbar,toolbar);
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2003-07-21 10:41:10 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_toolbar);
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) setToolbar: (NSToolbar *)toolbar
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
ASSIGN(_toolbar, toolbar);
|
2003-08-24 17:22:11 +00:00
|
|
|
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
2002-10-13 07:00:38 +00:00
|
|
|
- (NSToolbar *) toolbar
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _toolbar;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) drawRect: (NSRect)aRect
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
2003-08-24 17:22:11 +00:00
|
|
|
NSArray *items = [_toolbar items];
|
|
|
|
NSEnumerator *en = [items objectEnumerator];
|
|
|
|
id item = nil;
|
|
|
|
float x = 0;
|
|
|
|
|
2002-06-30 05:14:21 +00:00
|
|
|
[super drawRect: aRect];
|
2003-08-24 17:22:11 +00:00
|
|
|
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.
|
|
|
|
}
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSToolbar (GNUstepPrivate)
|
2003-08-24 17:22:11 +00:00
|
|
|
- (id) _toolbarView
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
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
|
2002-10-13 07:00:38 +00:00
|
|
|
- (BOOL) allowsUserCustomization
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _allowsUserCustomization;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (BOOL) autosavesConfiguration
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _autosavesConfiguration;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (NSDictionary *) configurationDictionary
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _configurationDictionary;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (BOOL) customizationPaletteIsRunning
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _customizationPaletteIsRunning;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (id) delegate
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _delegate;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (NSToolbarDisplayMode) displayMode
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _displayMode;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (NSString *) identifier
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
2002-12-29 05:38:29 +00:00
|
|
|
[self setConfigurationFromDictionary: config];
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (id) initWithIdentifier: (NSString*)identifier
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
[super init];
|
2003-09-13 20:59:10 +00:00
|
|
|
|
|
|
|
_customizationPaletteIsRunning = NO;
|
|
|
|
_allowsUserCustomization = NO;
|
|
|
|
_autosavesConfiguration = NO;
|
|
|
|
_configurationDictionary = nil;
|
|
|
|
_delegate = nil;
|
|
|
|
_displayMode = NSToolbarDisplayModeDefault;
|
|
|
|
_visible = YES;
|
|
|
|
_items = nil;
|
|
|
|
_visibleItems = nil;
|
2002-06-30 05:14:21 +00:00
|
|
|
ASSIGN(_identifier, identifier);
|
|
|
|
[self _loadConfig];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2002-09-24 01:33:36 +00:00
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
DESTROY (_identifier);
|
2003-07-21 10:41:10 +00:00
|
|
|
DESTROY (_configurationDictionary);
|
2002-09-24 01:33:36 +00:00
|
|
|
|
|
|
|
if (_delegate != nil)
|
|
|
|
{
|
|
|
|
[nc removeObserver: _delegate name: nil object: self];
|
|
|
|
_delegate = nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) insertItemWithItemIdentifier: (NSString *)itemIdentifier
|
2002-12-22 16:14:00 +00:00
|
|
|
atIndex: (int)index
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
2003-09-13 20:59:10 +00:00
|
|
|
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];
|
|
|
|
}
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (BOOL) isVisible
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _visible;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (NSArray *) items
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
return _items;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) removeItemAtIndex: (int)index
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
2002-12-22 16:14:00 +00:00
|
|
|
|
|
|
|
id obj = [_items objectAtIndex: index];
|
|
|
|
[_items removeObjectAtIndex: index];
|
|
|
|
[_visibleItems removeObject: obj];
|
|
|
|
[nc postNotificationName: NSToolbarDidRemoveItemNotification
|
|
|
|
object: self];
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) runCustomizationPalette: (id)sender
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
2002-12-29 05:38:29 +00:00
|
|
|
_customizationPaletteIsRunning = [NSBundle loadNibNamed: @"GSToolbarCustomizationPalette"
|
|
|
|
owner: self];
|
2003-09-13 20:59:10 +00:00
|
|
|
if(!_customizationPaletteIsRunning)
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to load gorm for GSToolbarCustomizationPalette");
|
|
|
|
}
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) setAllowsUserCustomization: (BOOL)flag
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
_allowsUserCustomization = flag;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) setAutosavesConfiguration: (BOOL)flag
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
_autosavesConfiguration = flag;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) setConfigurationFromDictionary: (NSDictionary *)configDict
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
ASSIGN(_configurationDictionary, configDict);
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
/**
|
|
|
|
* Sets the receivers delgate ... this is the object which will receive
|
|
|
|
* -toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:
|
|
|
|
* -toolbarAllowedItemIdentifiers: and -toolbarDefaultItemIdentifiers:
|
|
|
|
* messages.
|
|
|
|
*/
|
|
|
|
- (void) setDelegate: (id)delegate
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
#define CHECK_REQUIRED_METHOD(selector_name) \
|
2002-10-13 07:00:38 +00:00
|
|
|
if (![delegate respondsToSelector: @selector(selector_name)]) \
|
2002-06-30 05:14:21 +00:00
|
|
|
[NSException raise: NSInternalInconsistencyException \
|
2002-09-21 22:57:25 +00:00
|
|
|
format: @"delegate does not respond to %@",@#selector_name]
|
2002-06-30 05:14:21 +00:00
|
|
|
|
|
|
|
CHECK_REQUIRED_METHOD(toolbar:itemForItemIdentifier:willBeInsertedIntoToolbar:);
|
|
|
|
CHECK_REQUIRED_METHOD(toolbarAllowedItemIdentifiers:);
|
|
|
|
CHECK_REQUIRED_METHOD(toolbarDefaultItemIdentifiers:);
|
|
|
|
|
|
|
|
if (_delegate)
|
|
|
|
[nc removeObserver: _delegate name: nil object: self];
|
2002-10-13 07:00:38 +00:00
|
|
|
_delegate = delegate;
|
2002-06-30 05:14:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) setDisplayMode: (NSToolbarDisplayMode)displayMode
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
_displayMode = displayMode;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) setVisible: (BOOL)shown
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
|
|
|
_visible = shown;
|
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (void) validateVisibleItems
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
2002-12-29 05:38:29 +00:00
|
|
|
NSEnumerator *en = [_visibleItems objectEnumerator];
|
|
|
|
NSToolbarItem *item = nil;
|
|
|
|
|
|
|
|
while((item = [en nextObject]) != nil)
|
|
|
|
{
|
|
|
|
[item validate];
|
|
|
|
}
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
|
|
|
|
2002-10-13 07:00:38 +00:00
|
|
|
- (NSArray *) visibleItems
|
2002-06-30 05:14:21 +00:00
|
|
|
{
|
2002-12-29 05:38:29 +00:00
|
|
|
return _visibleItems;
|
2002-06-30 05:14:21 +00:00
|
|
|
}
|
|
|
|
@end /* interface of NSToolbar */
|