General clean up of GSToolbarView code.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@27501 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
fredkiefer 2009-01-02 14:05:39 +00:00
parent 2c07551e5d
commit 2358f07c43
5 changed files with 322 additions and 396 deletions

View file

@ -1,3 +1,11 @@
2009-01-02 Fred Kiefer <FredKiefer@gmx.de>
* Source/NSWindow+Toolbar.m,
* Source/NSToolbar.m: Rewrote interaction with GSToolbarView.
* Source/NSToolbar.m (_toolbarItemForIdentifier:): Fix memory leak.
* Headers/Additions/GNUstepGUI/GSToolbarView.h,
* Source/GSToolbarView.m: General cleanup of code.
2009-01-02 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSButtonCell.m: Fix to use the backgroundColor method when

View file

@ -55,21 +55,15 @@ typedef enum {
@interface GSToolbarView : NSView
{
NSToolbar *_toolbar;
NSClipView *_clipView, *_clipViewForEditMode;
NSClipView *_clipView;
GSToolbarClippedItemsButton *_clippedItemsMark;
NSMutableArray *_visibleBackViews;
BOOL _willBeVisible;
unsigned int _borderMask;
NSToolbarDisplayMode _displayMode;
NSToolbarSizeMode _sizeMode;
NSRect _rectAvailable;
float _heightFromLayout;
}
- (id) initWithFrame: (NSRect)frame;
- (id) initWithFrame: (NSRect)frame
displayMode: (NSToolbarDisplayMode)displayMode
sizeMode: (NSToolbarSizeMode)sizeMode;
// Accessors
- (NSToolbar *) toolbar;

View file

@ -124,6 +124,20 @@ static void initSystemExtensionsColors(void)
if (changed)
[SystemExtensionsColors writeToFile: nil];
}
/* Never released, but that's not a problem because the variables are
static and then will be deallocated with the class when the application
quits. */
StandardBackgroundColor =
[NSColor colorWithCalibratedRed: 0.8 green: 0.8 blue: 0.8 alpha: 1.0];
RETAIN(StandardBackgroundColor);
BackgroundColor =
[SystemExtensionsColors colorWithKey: @"toolbarBackgroundColor"];
BorderColor =
[SystemExtensionsColors colorWithKey: @"toolbarBorderColor"];
RETAIN(BackgroundColor);
RETAIN(BorderColor);
}
@implementation NSColor (GSToolbarViewAdditions)
@ -144,10 +158,14 @@ static void initSystemExtensionsColors(void)
@interface NSToolbar (GNUstepPrivate)
- (void) _build;
- (void) _concludeRemoveItem: (NSToolbarItem *)item atIndex: (int)index broadcast: (BOOL)broadcast;
- (void) _concludeRemoveItem: (NSToolbarItem *)item
atIndex: (int)index
broadcast: (BOOL)broadcast;
- (int) _indexOfItem: (NSToolbarItem *)item;
- (void) _insertPassivelyItem: (NSToolbarItem *)item atIndex: (int)newIndex;
- (void) _moveItemFromIndex: (int)index toIndex: (int)newIndex broadcast: (BOOL)broacast;
- (void) _moveItemFromIndex: (int)index
toIndex: (int)newIndex
broadcast: (BOOL)broacast;
- (void) _toolbarViewWillMoveToSuperview: (NSView *)newSuperview;
@ -184,8 +202,6 @@ static void initSystemExtensionsColors(void)
// Accessors
- (float) _heightFromLayout;
- (NSArray *) _visibleBackViews;
- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode;
- (NSToolbarSizeMode) _sizeMode;
- (BOOL) _usesStandardBackgroundColor;
- (void) _setUsesStandardBackgroundColor: (BOOL)standard;
@end
@ -206,13 +222,6 @@ static void initSystemExtensionsColors(void)
- (void) setToolbar: (NSToolbar *)toolbar;
@end
@interface GSToolbarClipView : NSClipView
{
}
@end
@implementation GSToolbarClippedItemsButton
- (id) init
{
@ -281,8 +290,6 @@ static void initSystemExtensionsColors(void)
id item;
NSArray *visibleItems;
AUTORELEASE(menu);
visibleItems = [_toolbar visibleItems];
e = [[_toolbar items] objectEnumerator];
@ -301,7 +308,7 @@ static void initSystemExtensionsColors(void)
}
}
return menu;
return AUTORELEASE(menu);
}
// Accessors
@ -315,105 +322,43 @@ static void initSystemExtensionsColors(void)
// ---
@implementation GSToolbarClipView
// Nothing here
@end
// Implementation GSToolbarView
@implementation GSToolbarView
+ (void) initialize
{
if (self != [GSToolbarView class])
return;
if (self == [GSToolbarView class])
initSystemExtensionsColors();
StandardBackgroundColor =
[NSColor colorWithCalibratedRed: 0.8 green: 0.8 blue: 0.8 alpha: 1.0];
RETAIN(StandardBackgroundColor);
/* Never released, but that's not a problem because the variable is static and
then will be deallocated with the class when the application quits. */
}
- (id) initWithFrame: (NSRect)frame
{
return [self initWithFrame: frame
displayMode: NSToolbarDisplayModeDefault
sizeMode: NSToolbarSizeModeDefault];
if ((self = [super initWithFrame: frame]) == nil)
{
return nil;
}
- (id) initWithFrame: (NSRect)frame
displayMode: (NSToolbarDisplayMode)displayMode
sizeMode: (NSToolbarSizeMode)sizeMode
{
if ((self = [super initWithFrame: frame]) != nil)
{
_displayMode = displayMode;
_sizeMode = sizeMode;
switch (_sizeMode)
{
case NSToolbarSizeModeDefault:
_heightFromLayout = ToolbarViewDefaultHeight;
break;
case NSToolbarSizeModeRegular:
_heightFromLayout = ToolbarViewRegularHeight;
break;
case NSToolbarSizeModeSmall:
_heightFromLayout = ToolbarViewSmallHeight;
break;
default:
// Raise exception
_heightFromLayout = 0;
}
[self setFrame: NSMakeRect(frame.origin.x, frame.origin.y,
frame.size.width, _heightFromLayout)];
// ---
_clipView =
[[GSToolbarClipView alloc] initWithFrame: NSMakeRect(0, 0, 100, 100)];
[_clipView setAutoresizingMask: (NSViewWidthSizable |
NSViewHeightSizable)];
_clipView = [[NSClipView alloc] initWithFrame:
NSMakeRect(0, 0, frame.size.width,
_heightFromLayout)];
[_clipView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
[self addSubview: _clipView];
// Adjust the clip view frame
[self setBorderMask: GSToolbarViewTopBorder | GSToolbarViewBottomBorder
| GSToolbarViewRightBorder | GSToolbarViewLeftBorder];
// Adjust the clip view frame
[self addSubview: _clipView];
_clippedItemsMark = [[GSToolbarClippedItemsButton alloc] init];
BackgroundColor =
[SystemExtensionsColors colorWithKey: @"toolbarBackgroundColor"];
BorderColor =
[SystemExtensionsColors colorWithKey: @"toolbarBorderColor"];
RETAIN(BackgroundColor);
RETAIN(BorderColor);
/* Never released, but that's not a problem because the variables are
static and then will be deallocated with the class when the application
quits. */
// ---
[self registerForDraggedTypes:
[NSArray arrayWithObject: GSMovableToolbarItemPboardType]];
return self;
}
return nil;
}
- (id) retain
{
return [super retain];
}
- (void) dealloc
{
//NSLog(@"Toolbar view dealloc");
@ -510,14 +455,13 @@ static void initSystemExtensionsColors(void)
- (void) drawRect: (NSRect)aRect
{
NSBezierPath *rect = [NSBezierPath bezierPathWithRect: aRect];
NSRect viewFrame = [self frame];
// We draw the background
if (![BackgroundColor isEqual: [NSColor clearColor]])
{
[BackgroundColor set];
[rect fill];
[NSBezierPath fillRect: aRect];
}
// We draw the border
@ -591,7 +535,7 @@ static void initSystemExtensionsColors(void)
object: _window];
}
// More methods... Accessors
// Accessors
- (unsigned int) borderMask
{
@ -606,29 +550,12 @@ static void initSystemExtensionsColors(void)
- (void) setBorderMask: (unsigned int)borderMask
{
NSRect toolbarViewFrame = [self frame];
NSRect rect = NSMakeRect(0, 0, toolbarViewFrame.size.width, 100);
NSRect rect = NSMakeRect(0, 0, toolbarViewFrame.size.width,
toolbarViewFrame.size.height);
_borderMask = borderMask;
// Take in account the size mode
switch (_sizeMode)
{
case NSToolbarSizeModeDefault:
rect.size.height = ToolbarViewDefaultHeight;
break;
case NSToolbarSizeModeRegular:
rect.size.height = ToolbarViewRegularHeight;
break;
case NSToolbarSizeModeSmall:
rect.size.height = ToolbarViewSmallHeight;
break;
default:
; // Invalid
}
// Take in account the border
if (_borderMask & GSToolbarViewBottomBorder)
{
rect = NSMakeRect(rect.origin.x, ++rect.origin.y, rect.size.width,
@ -658,9 +585,6 @@ static void initSystemExtensionsColors(void)
- (void) setToolbar: (NSToolbar *)toolbar
{
if ([toolbar sizeMode] != _sizeMode)
; // FIXME: Raise exception here
if (_toolbar == toolbar)
return;
@ -816,10 +740,10 @@ static void initSystemExtensionsColors(void)
[_clippedItemsMark removeFromSuperview];
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x,
clipViewFrame.origin.y, [self frame].size.width,
clipViewFrame.origin.y,
[self frame].size.width,
clipViewFrame.size.height)];
}
}
- (void) _reload
@ -955,16 +879,6 @@ static void initSystemExtensionsColors(void)
}
- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode
{
_sizeMode = sizeMode;
}
- (NSToolbarSizeMode) _sizeMode
{
return _sizeMode;
}
- (BOOL) _usesStandardBackgroundColor
{
return [BackgroundColor isEqual: [self standardBackgroundColor]];

View file

@ -520,15 +520,13 @@ static GSValidationCenter *vc = nil;
- (void) _reload;
// Accessors
- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode;
- (NSToolbarSizeMode) _sizeMode;
- (NSArray *) _visibleBackViews;
- (BOOL) _usesStandardBackgroundColor;
- (void) _setUsesStandardBackgroundColor: (BOOL)standard;
@end
@interface NSWindow (ToolbarPrivate)
- (void) _adjustToolbarView;
- (void) _adjustToolbarView: (GSToolbarView*)view;
@end
// ---
@ -1044,7 +1042,7 @@ static GSValidationCenter *vc = nil;
item = [[NSToolbarItem alloc] initWithItemIdentifier: itemIdent];
}
return item;
return AUTORELEASE(item);
}
@ -1216,8 +1214,11 @@ static GSValidationCenter *vc = nil;
{
_displayMode = displayMode;
if ([self isVisible])
{
[_toolbarView _reload];
[[_toolbarView window] _adjustToolbarView];
[[_toolbarView window] _adjustToolbarView: _toolbarView];
}
if (broadcast)
{
@ -1233,10 +1234,11 @@ static GSValidationCenter *vc = nil;
{
_sizeMode = sizeMode;
[_toolbarView _setSizeMode: _sizeMode];
if ([self isVisible])
{
[_toolbarView _reload];
[[_toolbarView window] _adjustToolbarView];
[[_toolbarView window] _adjustToolbarView: _toolbarView];
}
if (broadcast)
{

View file

@ -44,8 +44,9 @@
@end
@interface NSWindow (ToolbarPrivate)
- (void) _adjustToolbarView;
- (void) _toggleToolbarView;
- (void) _adjustToolbarView: (GSToolbarView*)view;
- (void) _addToolbarView: (GSToolbarView*)view;
- (void) _removeToolbarView: (GSToolbarView*)view;
- (NSView *) _contentViewWithoutToolbar;
@end
@ -59,24 +60,99 @@
- (void) toggleToolbarShown: (id)sender
{
NSToolbar *toolbar = [self toolbar];
BOOL isVisible = [toolbar isVisible];
if (!toolbar)
return;
[self _toggleToolbarView];
if (isVisible)
{
[self _removeToolbarView: [toolbar _toolbarView]];
}
else
{
[self _addToolbarView: [toolbar _toolbarView]];
}
// Important to set _visible after the toolbar view has been toggled because
// NSWindow method contentViewWithoutToolbar uses [NSToolbar visible]
// NSWindow method _contentViewWithoutToolbar uses [NSToolbar visible]
// when we toggle the toolbar
// example : the toolbar needs to be still known visible in order to hide
// it.
[toolbar setVisible: ![toolbar isVisible]];
[toolbar setVisible: !isVisible];
[self display];
}
// Accessors
- (NSToolbar *) toolbar
{
return _toolbar;
}
- (void) setToolbar: (NSToolbar*)toolbar
{
if (toolbar == _toolbar)
return;
if (_toolbar != nil)
{
GSToolbarView *toolbarView = [_toolbar _toolbarView];
// We throw the last toolbar out
if ([_toolbar isVisible])
{
[self _removeToolbarView: toolbarView];
}
[toolbarView setToolbar: nil];
// Release the toolbarView, this may release the toolbar
RELEASE(toolbarView);
}
ASSIGN(_toolbar, toolbar);
if (toolbar != nil)
{
GSToolbarView *toolbarView = [toolbar _toolbarView];
if (toolbarView != nil)
{
NSLog(@"Error: the new toolbar is still owned by a toolbar view");
}
else
{
// Instantiate the toolbar view
toolbarView = [[GSToolbarView alloc]
initWithFrame:
NSMakeRect(0, 0,
[NSWindow contentRectForFrameRect: [self frame]
styleMask: [self styleMask]].size.width, 100)];
// _toggleToolbarView method will set the toolbar view to the right
// frame
[toolbarView setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
[toolbarView setBorderMask: GSToolbarViewBottomBorder];
// Load the toolbar inside the toolbar view
// Will set the _toolbarView variable for the toolbar
[toolbarView setToolbar: toolbar];
}
// Make the toolbar view visible
if ([toolbar isVisible])
{
[self _addToolbarView: toolbarView];
}
}
// To show the changed toolbar
[self displayIfNeeded];
}
@end
// Private methods
@implementation NSWindow (ToolbarPrivate)
- (NSView *) _contentViewWithoutToolbar
{
NSToolbar *toolbar = [self toolbar];
@ -115,79 +191,8 @@
return [self contentView];
}
- (NSToolbar *) toolbar
- (void) _adjustToolbarView: (GSToolbarView *)toolbarView
{
return _toolbar;
}
- (void) setToolbar: (NSToolbar*)toolbar
{
if (toolbar == _toolbar)
return;
if (_toolbar != nil)
{
GSToolbarView *toolbarView = [_toolbar _toolbarView];
// We throw the last toolbar out
if ([_toolbar isVisible])
{
[self _toggleToolbarView];
}
[toolbarView setToolbar: nil];
// Release the toolbarView, this will release the toolbar
RELEASE(toolbarView);
}
ASSIGN(_toolbar, toolbar);
if (toolbar != nil)
{
GSToolbarView *toolbarView = [toolbar _toolbarView];
if (toolbarView != nil)
{
NSLog(@"Error: the new toolbar is still owned by a toolbar view");
}
else
{
// Instantiate the toolbar view
toolbarView = [[GSToolbarView alloc]
initWithFrame:
NSMakeRect(0, 0,
[NSWindow contentRectForFrameRect: [self frame]
styleMask: [self styleMask]].size.width, 100)];
// _toggleToolbarView method will set the toolbar view to the right
// frame
[toolbarView setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
[toolbarView setBorderMask: GSToolbarViewBottomBorder];
// Load the toolbar inside the toolbar view
// Will set the _toolbarView variable for the toolbar
[toolbarView setToolbar: toolbar];
}
// Make the toolbar view visible
if ([toolbar isVisible])
{
[self _toggleToolbarView];
}
}
// To show the changed toolbar
[self display];
}
// Private methods
- (void) _adjustToolbarView
{
NSToolbar *toolbar = [self toolbar];
if ([toolbar isVisible])
{
// Views
GSToolbarView *toolbarView = [toolbar _toolbarView];
// Frame and height
NSRect toolbarViewFrame = [toolbarView frame];
float toolbarViewHeight = toolbarViewFrame.size.height;
@ -220,12 +225,9 @@
}
}
}
- (void) _toggleToolbarView
- (void) _addToolbarView: (GSToolbarView*)toolbarView
{
// Views
GSToolbarView *toolbarView = [[self toolbar] _toolbarView];
NSView *contentViewWithoutToolbar;
// Frame
@ -233,8 +235,6 @@
= [NSWindow contentRectForFrameRect: [self frame]
styleMask: [self styleMask]];
if ([toolbarView superview] == nil)
{
float newToolbarViewHeight;
NSRect contentViewWithoutToolbarFrame;
@ -279,8 +279,17 @@
[_contentView addSubview: contentViewWithoutToolbar];
RELEASE(contentViewWithoutToolbar);
}
else
- (void) _removeToolbarView: (GSToolbarView *)toolbarView
{
// Views
NSView *contentViewWithoutToolbar;
// Frame
NSRect windowContentFrame
= [NSWindow contentRectForFrameRect: [self frame]
styleMask: [self styleMask]];
float toolbarViewHeight = [toolbarView frame].size.height;
contentViewWithoutToolbar = [self _contentViewWithoutToolbar];
@ -312,6 +321,5 @@
RELEASE(contentViewWithoutToolbar);
}
}
@end