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> 2009-01-02 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSButtonCell.m: Fix to use the backgroundColor method when * Source/NSButtonCell.m: Fix to use the backgroundColor method when

View file

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

View file

@ -78,22 +78,22 @@ static void initSystemExtensionsColors(void)
colors as keys and with colors in string format as values. */ colors as keys and with colors in string format as values. */
toolbarBorderColor = [NSColor colorWithCalibratedRed: 0.5 toolbarBorderColor = [NSColor colorWithCalibratedRed: 0.5
green: 0.5 green: 0.5
blue: 0.5 blue: 0.5
alpha: 1.0]; alpha: 1.0];
// Window background color by tranparency // Window background color by tranparency
toolbarBackgroundColor = [NSColor clearColor]; toolbarBackgroundColor = [NSColor clearColor];
// Window backgound color hardcoded // Window backgound color hardcoded
/* toolbarBackgroundColor = [NSColor colorWithCalibratedRed: 0.8 /* toolbarBackgroundColor = [NSColor colorWithCalibratedRed: 0.8
green: 0.8 green: 0.8
blue: 0.8 blue: 0.8
alpha: 1.0]; */ alpha: 1.0]; */
colors = [[NSDictionary alloc] initWithObjectsAndKeys: colors = [[NSDictionary alloc] initWithObjectsAndKeys:
toolbarBackgroundColor, @"toolbarBackgroundColor",toolbarBorderColor, toolbarBackgroundColor, @"toolbarBackgroundColor",toolbarBorderColor,
@"toolbarBorderColor", nil]; @"toolbarBorderColor", nil];
SystemExtensionsColors = [NSColorList colorListNamed: @"System extensions"]; SystemExtensionsColors = [NSColorList colorListNamed: @"System extensions"];
if (SystemExtensionsColors == nil) if (SystemExtensionsColors == nil)
{ {
@ -111,19 +111,33 @@ static void initSystemExtensionsColors(void)
e = [colors keyEnumerator]; e = [colors keyEnumerator];
while ((colorKey = (NSString *)[e nextObject])) while ((colorKey = (NSString *)[e nextObject]))
{ {
if ([SystemExtensionsColors colorWithKey: colorKey]) if ([SystemExtensionsColors colorWithKey: colorKey])
continue; continue;
color = [colors objectForKey: colorKey]; color = [colors objectForKey: colorKey];
[SystemExtensionsColors setColor: color forKey: colorKey]; [SystemExtensionsColors setColor: color forKey: colorKey];
changed = YES; changed = YES;
} }
if (changed) if (changed)
[SystemExtensionsColors writeToFile: nil]; [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) @implementation NSColor (GSToolbarViewAdditions)
@ -144,10 +158,14 @@ static void initSystemExtensionsColors(void)
@interface NSToolbar (GNUstepPrivate) @interface NSToolbar (GNUstepPrivate)
- (void) _build; - (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; - (int) _indexOfItem: (NSToolbarItem *)item;
- (void) _insertPassivelyItem: (NSToolbarItem *)item atIndex: (int)newIndex; - (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; - (void) _toolbarViewWillMoveToSuperview: (NSView *)newSuperview;
@ -184,8 +202,6 @@ static void initSystemExtensionsColors(void)
// Accessors // Accessors
- (float) _heightFromLayout; - (float) _heightFromLayout;
- (NSArray *) _visibleBackViews; - (NSArray *) _visibleBackViews;
- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode;
- (NSToolbarSizeMode) _sizeMode;
- (BOOL) _usesStandardBackgroundColor; - (BOOL) _usesStandardBackgroundColor;
- (void) _setUsesStandardBackgroundColor: (BOOL)standard; - (void) _setUsesStandardBackgroundColor: (BOOL)standard;
@end @end
@ -206,13 +222,6 @@ static void initSystemExtensionsColors(void)
- (void) setToolbar: (NSToolbar *)toolbar; - (void) setToolbar: (NSToolbar *)toolbar;
@end @end
@interface GSToolbarClipView : NSClipView
{
}
@end
@implementation GSToolbarClippedItemsButton @implementation GSToolbarClippedItemsButton
- (id) init - (id) init
{ {
@ -249,17 +258,17 @@ static void initSystemExtensionsColors(void)
- (void) mouseDown: (NSEvent *)event - (void) mouseDown: (NSEvent *)event
{ {
NSMenu *clippedItemsMenu = [self menuForEvent: event]; NSMenu *clippedItemsMenu = [self menuForEvent: event];
[super highlight: YES]; [super highlight: YES];
if (clippedItemsMenu != nil) if (clippedItemsMenu != nil)
{ {
[NSMenu popUpContextMenu: clippedItemsMenu withEvent: event [NSMenu popUpContextMenu: clippedItemsMenu withEvent: event
forView: self]; forView: self];
} }
[super highlight: NO]; [super highlight: NO];
} }
- (NSMenu *) menuForEvent: (NSEvent *)event - (NSMenu *) menuForEvent: (NSEvent *)event
@ -281,8 +290,6 @@ static void initSystemExtensionsColors(void)
id item; id item;
NSArray *visibleItems; NSArray *visibleItems;
AUTORELEASE(menu);
visibleItems = [_toolbar visibleItems]; visibleItems = [_toolbar visibleItems];
e = [[_toolbar items] objectEnumerator]; e = [[_toolbar items] objectEnumerator];
@ -292,16 +299,16 @@ static void initSystemExtensionsColors(void)
{ {
id menuItem; id menuItem;
menuItem = [item menuFormRepresentation]; menuItem = [item menuFormRepresentation];
if (menuItem == nil) if (menuItem == nil)
menuItem = [item _defaultMenuFormRepresentation]; menuItem = [item _defaultMenuFormRepresentation];
if (menuItem != nil) if (menuItem != nil)
[menu addItem: menuItem]; [menu addItem: menuItem];
} }
} }
return menu; return AUTORELEASE(menu);
} }
// Accessors // Accessors
@ -315,103 +322,41 @@ static void initSystemExtensionsColors(void)
// --- // ---
@implementation GSToolbarClipView
// Nothing here
@end
// Implementation GSToolbarView // Implementation GSToolbarView
@implementation GSToolbarView @implementation GSToolbarView
+ (void) initialize + (void) initialize
{ {
if (self != [GSToolbarView class]) if (self == [GSToolbarView class])
return; initSystemExtensionsColors();
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 - (id) initWithFrame: (NSRect)frame
{ {
return [self initWithFrame: frame if ((self = [super initWithFrame: frame]) == nil)
displayMode: NSToolbarDisplayModeDefault
sizeMode: NSToolbarSizeModeDefault];
}
- (id) initWithFrame: (NSRect)frame
displayMode: (NSToolbarDisplayMode)displayMode
sizeMode: (NSToolbarSizeMode)sizeMode
{
if ((self = [super initWithFrame: frame]) != nil)
{ {
_displayMode = displayMode; return nil;
_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)];
[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; _heightFromLayout = ToolbarViewDefaultHeight;
} [self setFrame: NSMakeRect(frame.origin.x, frame.origin.y,
frame.size.width, _heightFromLayout)];
- (id) retain
{ _clipView = [[NSClipView alloc] initWithFrame:
return [super retain]; 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];
_clippedItemsMark = [[GSToolbarClippedItemsButton alloc] init];
[self registerForDraggedTypes:
[NSArray arrayWithObject: GSMovableToolbarItemPboardType]];
return self;
} }
- (void) dealloc - (void) dealloc
@ -440,9 +385,9 @@ static void initSystemExtensionsColors(void)
if ([self _insertionIndexAtPoint: [info draggingLocation]] != NSNotFound if ([self _insertionIndexAtPoint: [info draggingLocation]] != NSNotFound
&& [allowedItemIdentifiers containsObject: itemIdentifier]); && [allowedItemIdentifiers containsObject: itemIdentifier]);
{ {
return NSDragOperationGeneric; return NSDragOperationGeneric;
} }
return NSDragOperationNone; return NSDragOperationNone;
} }
@ -457,9 +402,9 @@ static void initSystemExtensionsColors(void)
if ([self _insertionIndexAtPoint: [info draggingLocation]] != NSNotFound if ([self _insertionIndexAtPoint: [info draggingLocation]] != NSNotFound
&& [allowedItemIdentifiers containsObject: itemIdentifier]); && [allowedItemIdentifiers containsObject: itemIdentifier]);
{ {
return NSDragOperationGeneric; return NSDragOperationGeneric;
} }
return NSDragOperationNone; return NSDragOperationNone;
} }
@ -510,41 +455,40 @@ static void initSystemExtensionsColors(void)
- (void) drawRect: (NSRect)aRect - (void) drawRect: (NSRect)aRect
{ {
NSBezierPath *rect = [NSBezierPath bezierPathWithRect: aRect];
NSRect viewFrame = [self frame]; NSRect viewFrame = [self frame];
// We draw the background // We draw the background
if (![BackgroundColor isEqual: [NSColor clearColor]]) if (![BackgroundColor isEqual: [NSColor clearColor]])
{ {
[BackgroundColor set]; [BackgroundColor set];
[rect fill]; [NSBezierPath fillRect: aRect];
} }
// We draw the border // We draw the border
[BorderColor set]; [BorderColor set];
if (_borderMask & GSToolbarViewBottomBorder) if (_borderMask & GSToolbarViewBottomBorder)
{ {
[NSBezierPath strokeLineFromPoint: NSMakePoint(0, 0.5) [NSBezierPath strokeLineFromPoint: NSMakePoint(0, 0.5)
toPoint: NSMakePoint(viewFrame.size.width, 0.5)]; toPoint: NSMakePoint(viewFrame.size.width, 0.5)];
} }
if (_borderMask & GSToolbarViewTopBorder) if (_borderMask & GSToolbarViewTopBorder)
{ {
[NSBezierPath strokeLineFromPoint: NSMakePoint(0, [NSBezierPath strokeLineFromPoint: NSMakePoint(0,
viewFrame.size.height - 0.5) viewFrame.size.height - 0.5)
toPoint: NSMakePoint(viewFrame.size.width, toPoint: NSMakePoint(viewFrame.size.width,
viewFrame.size.height - 0.5)]; viewFrame.size.height - 0.5)];
} }
if (_borderMask & GSToolbarViewLeftBorder) if (_borderMask & GSToolbarViewLeftBorder)
{ {
[NSBezierPath strokeLineFromPoint: NSMakePoint(0.5, 0) [NSBezierPath strokeLineFromPoint: NSMakePoint(0.5, 0)
toPoint: NSMakePoint(0.5, viewFrame.size.height)]; toPoint: NSMakePoint(0.5, viewFrame.size.height)];
} }
if (_borderMask & GSToolbarViewRightBorder) if (_borderMask & GSToolbarViewRightBorder)
{ {
[NSBezierPath strokeLineFromPoint: NSMakePoint(viewFrame.size.width - 0.5,0) [NSBezierPath strokeLineFromPoint: NSMakePoint(viewFrame.size.width - 0.5,0)
toPoint: NSMakePoint(viewFrame.size.width - 0.5, toPoint: NSMakePoint(viewFrame.size.width - 0.5,
viewFrame.size.height)]; viewFrame.size.height)];
} }
[super drawRect: aRect]; [super drawRect: aRect];
} }
@ -588,10 +532,10 @@ static void initSystemExtensionsColors(void)
[nc removeObserver: self name: NSWindowDidResizeNotification object: nil]; [nc removeObserver: self name: NSWindowDidResizeNotification object: nil];
[nc addObserver: self selector: @selector(windowDidResize:) [nc addObserver: self selector: @selector(windowDidResize:)
name: NSWindowDidResizeNotification name: NSWindowDidResizeNotification
object: _window]; object: _window];
} }
// More methods... Accessors // Accessors
- (unsigned int) borderMask - (unsigned int) borderMask
{ {
@ -606,51 +550,34 @@ static void initSystemExtensionsColors(void)
- (void) setBorderMask: (unsigned int)borderMask - (void) setBorderMask: (unsigned int)borderMask
{ {
NSRect toolbarViewFrame = [self frame]; 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; _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 // Take in account the border
if (_borderMask & GSToolbarViewBottomBorder) if (_borderMask & GSToolbarViewBottomBorder)
{ {
rect = NSMakeRect(rect.origin.x, ++rect.origin.y, rect.size.width, rect = NSMakeRect(rect.origin.x, ++rect.origin.y, rect.size.width,
--rect.size.height); --rect.size.height);
} }
if (_borderMask & GSToolbarViewTopBorder) if (_borderMask & GSToolbarViewTopBorder)
{ {
rect = NSMakeRect(rect.origin.x, rect.origin.y, rect.size.width, rect = NSMakeRect(rect.origin.x, rect.origin.y, rect.size.width,
--rect.size.height); --rect.size.height);
} }
if (_borderMask & GSToolbarViewLeftBorder) if (_borderMask & GSToolbarViewLeftBorder)
{ {
rect = NSMakeRect(++rect.origin.x, rect.origin.y, --rect.size.width, rect = NSMakeRect(++rect.origin.x, rect.origin.y, --rect.size.width,
rect.size.height); rect.size.height);
} }
if (_borderMask & GSToolbarViewRightBorder) if (_borderMask & GSToolbarViewRightBorder)
{ {
rect = NSMakeRect(rect.origin.x, rect.origin.y, --rect.size.width, rect = NSMakeRect(rect.origin.x, rect.origin.y, --rect.size.width,
rect.size.height); rect.size.height);
} }
[_clipView setFrame: rect]; [_clipView setFrame: rect];
@ -658,9 +585,6 @@ static void initSystemExtensionsColors(void)
- (void) setToolbar: (NSToolbar *)toolbar - (void) setToolbar: (NSToolbar *)toolbar
{ {
if ([toolbar sizeMode] != _sizeMode)
; // FIXME: Raise exception here
if (_toolbar == toolbar) if (_toolbar == toolbar)
return; return;
@ -702,16 +626,16 @@ static void initSystemExtensionsColors(void)
|| [item _isModified] || [item _isModified]
|| [item _isFlexibleSpace]) || [item _isFlexibleSpace])
{ {
// When a label is changed, _isModified returns YES to let us known we // When a label is changed, _isModified returns YES to let us known we
// must recalculate the text length and then the size for the edited // must recalculate the text length and then the size for the edited
// item back view // item back view
[item _layout]; [item _layout];
} }
itemBackViewFrame = [itemBackView frame]; itemBackViewFrame = [itemBackView frame];
[itemBackView setFrame: NSMakeRect(x, itemBackViewFrame.origin.y, [itemBackView setFrame: NSMakeRect(x, itemBackViewFrame.origin.y,
itemBackViewFrame.size.width, itemBackViewFrame.size.height)]; itemBackViewFrame.size.width, itemBackViewFrame.size.height)];
x += [itemBackView frame].size.width; x += [itemBackView frame].size.width;
if (itemBackViewFrame.size.height > newHeight) if (itemBackViewFrame.size.height > newHeight)
@ -795,7 +719,7 @@ static void initSystemExtensionsColors(void)
clipViewFrame.origin.y, clipViewFrame.origin.y,
width, width,
clipViewFrame.size.height)]; clipViewFrame.size.height)];
// Adjust the clipped items mark frame handling // Adjust the clipped items mark frame handling
[_clippedItemsMark layout]; [_clippedItemsMark layout];
@ -803,9 +727,9 @@ static void initSystemExtensionsColors(void)
clipViewFrame = [_clipView frame]; // We get the new _clipView frame clipViewFrame = [_clipView frame]; // We get the new _clipView frame
[_clippedItemsMark setFrameOrigin: NSMakePoint( [_clippedItemsMark setFrameOrigin: NSMakePoint(
[self frame].size.width - ClippedItemsViewWidth, clipViewFrame.origin.y)]; [self frame].size.width - ClippedItemsViewWidth, clipViewFrame.origin.y)];
// --- // ---
if ([_clippedItemsMark superview] == nil) if ([_clippedItemsMark superview] == nil)
[self addSubview: _clippedItemsMark]; [self addSubview: _clippedItemsMark];
@ -816,10 +740,10 @@ static void initSystemExtensionsColors(void)
[_clippedItemsMark removeFromSuperview]; [_clippedItemsMark removeFromSuperview];
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x, [_clipView setFrame: NSMakeRect(clipViewFrame.origin.x,
clipViewFrame.origin.y, [self frame].size.width, clipViewFrame.origin.y,
clipViewFrame.size.height)]; [self frame].size.width,
clipViewFrame.size.height)];
} }
} }
- (void) _reload - (void) _reload
@ -870,7 +794,7 @@ static void initSystemExtensionsColors(void)
[backView setFrame: NSMakeRect(x, backViewFrame.origin.y, [backView setFrame: NSMakeRect(x, backViewFrame.origin.y,
lengthAvailable / flexibleSpaceItemsNumber, lengthAvailable / flexibleSpaceItemsNumber,
backViewFrame.size.height)]; backViewFrame.size.height)];
mustAdjustNext = YES; mustAdjustNext = YES;
} }
else if (mustAdjustNext) else if (mustAdjustNext)
@ -955,16 +879,6 @@ static void initSystemExtensionsColors(void)
} }
- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode
{
_sizeMode = sizeMode;
}
- (NSToolbarSizeMode) _sizeMode
{
return _sizeMode;
}
- (BOOL) _usesStandardBackgroundColor - (BOOL) _usesStandardBackgroundColor
{ {
return [BackgroundColor isEqual: [self standardBackgroundColor]]; return [BackgroundColor isEqual: [self standardBackgroundColor]];

View file

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

View file

@ -44,8 +44,9 @@
@end @end
@interface NSWindow (ToolbarPrivate) @interface NSWindow (ToolbarPrivate)
- (void) _adjustToolbarView; - (void) _adjustToolbarView: (GSToolbarView*)view;
- (void) _toggleToolbarView; - (void) _addToolbarView: (GSToolbarView*)view;
- (void) _removeToolbarView: (GSToolbarView*)view;
- (NSView *) _contentViewWithoutToolbar; - (NSView *) _contentViewWithoutToolbar;
@end @end
@ -59,24 +60,99 @@
- (void) toggleToolbarShown: (id)sender - (void) toggleToolbarShown: (id)sender
{ {
NSToolbar *toolbar = [self toolbar]; NSToolbar *toolbar = [self toolbar];
BOOL isVisible = [toolbar isVisible];
if (!toolbar) if (!toolbar)
return; 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 // 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 // when we toggle the toolbar
// example : the toolbar needs to be still known visible in order to hide // example : the toolbar needs to be still known visible in order to hide
// it. // it.
[toolbar setVisible: ![toolbar isVisible]]; [toolbar setVisible: !isVisible];
[self display]; [self display];
} }
// Accessors // 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 - (NSView *) _contentViewWithoutToolbar
{ {
NSToolbar *toolbar = [self toolbar]; NSToolbar *toolbar = [self toolbar];
@ -115,117 +191,43 @@
return [self contentView]; return [self contentView];
} }
- (NSToolbar *) toolbar - (void) _adjustToolbarView: (GSToolbarView *)toolbarView
{ {
return _toolbar; // Frame and height
} NSRect toolbarViewFrame = [toolbarView frame];
float toolbarViewHeight = toolbarViewFrame.size.height;
- (void) setToolbar: (NSToolbar*)toolbar float newToolbarViewHeight = [toolbarView _heightFromLayout];
{
if (toolbar == _toolbar) if (toolbarViewHeight != newToolbarViewHeight)
return;
if (_toolbar != nil)
{ {
GSToolbarView *toolbarView = [_toolbar _toolbarView]; NSView *contentViewWithoutToolbar = [self _contentViewWithoutToolbar];
NSRect windowFrame = [self frame];
// We throw the last toolbar out
if ([_toolbar isVisible]) [toolbarView setFrame: NSMakeRect(
{ toolbarViewFrame.origin.x,
[self _toggleToolbarView]; toolbarViewFrame.origin.y + (toolbarViewHeight - newToolbarViewHeight),
} toolbarViewFrame.size.width,
[toolbarView setToolbar: nil]; newToolbarViewHeight)];
// Release the toolbarView, this will release the toolbar
RELEASE(toolbarView); // Resize the window
[contentViewWithoutToolbar setAutoresizingMask: NSViewNotSizable];
[self setFrame: NSMakeRect(
windowFrame.origin.x,
windowFrame.origin.y + (toolbarViewHeight - newToolbarViewHeight),
windowFrame.size.width,
windowFrame.size.height - (toolbarViewHeight - newToolbarViewHeight))
display: NO];
[contentViewWithoutToolbar setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
[self display];
} }
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) _addToolbarView: (GSToolbarView*)toolbarView
- (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;
float newToolbarViewHeight = [toolbarView _heightFromLayout];
if (toolbarViewHeight != newToolbarViewHeight)
{
NSView *contentViewWithoutToolbar = [self _contentViewWithoutToolbar];
NSRect windowFrame = [self frame];
[toolbarView setFrame: NSMakeRect(
toolbarViewFrame.origin.x,
toolbarViewFrame.origin.y + (toolbarViewHeight - newToolbarViewHeight),
toolbarViewFrame.size.width,
newToolbarViewHeight)];
// Resize the window
[contentViewWithoutToolbar setAutoresizingMask: NSViewNotSizable];
[self setFrame: NSMakeRect(
windowFrame.origin.x,
windowFrame.origin.y + (toolbarViewHeight - newToolbarViewHeight),
windowFrame.size.width,
windowFrame.size.height - (toolbarViewHeight - newToolbarViewHeight))
display: NO];
[contentViewWithoutToolbar setAutoresizingMask: NSViewHeightSizable | NSViewWidthSizable];
[self display];
}
}
}
- (void) _toggleToolbarView
{ {
// Views // Views
GSToolbarView *toolbarView = [[self toolbar] _toolbarView];
NSView *contentViewWithoutToolbar; NSView *contentViewWithoutToolbar;
// Frame // Frame
@ -233,85 +235,91 @@
= [NSWindow contentRectForFrameRect: [self frame] = [NSWindow contentRectForFrameRect: [self frame]
styleMask: [self styleMask]]; styleMask: [self styleMask]];
if ([toolbarView superview] == nil) float newToolbarViewHeight;
{ NSRect contentViewWithoutToolbarFrame;
float newToolbarViewHeight;
NSRect contentViewWithoutToolbarFrame;
[toolbarView setFrameSize: [toolbarView setFrameSize:
NSMakeSize(windowContentFrame.size.width, 100)]; NSMakeSize(windowContentFrame.size.width, 100)];
// Will recalculate the layout // Will recalculate the layout
[toolbarView _reload]; [toolbarView _reload];
newToolbarViewHeight = [toolbarView _heightFromLayout]; newToolbarViewHeight = [toolbarView _heightFromLayout];
contentViewWithoutToolbar = _contentView;
contentViewWithoutToolbarFrame = [contentViewWithoutToolbar frame];
// Switch the content view
RETAIN(contentViewWithoutToolbar);
[self setContentView:
AUTORELEASE([[NSView alloc] initWithFrame:
contentViewWithoutToolbarFrame])];
// Resize the window
windowContentFrame.origin.y -= newToolbarViewHeight;
windowContentFrame.size.height += newToolbarViewHeight;
[self setFrame: [NSWindow frameRectForContentRect: windowContentFrame
styleMask: [self styleMask]]
display: NO];
// Plug the toolbar view contentViewWithoutToolbar = _contentView;
[toolbarView setFrame: NSMakeRect( contentViewWithoutToolbarFrame = [contentViewWithoutToolbar frame];
0,
contentViewWithoutToolbarFrame.size.height, // Switch the content view
contentViewWithoutToolbarFrame.size.width, RETAIN(contentViewWithoutToolbar);
[self setContentView:
AUTORELEASE([[NSView alloc] initWithFrame:
contentViewWithoutToolbarFrame])];
// Resize the window
windowContentFrame.origin.y -= newToolbarViewHeight;
windowContentFrame.size.height += newToolbarViewHeight;
[self setFrame: [NSWindow frameRectForContentRect: windowContentFrame
styleMask: [self styleMask]]
display: NO];
// Plug the toolbar view
[toolbarView setFrame: NSMakeRect(
0,
contentViewWithoutToolbarFrame.size.height,
contentViewWithoutToolbarFrame.size.width,
newToolbarViewHeight)]; newToolbarViewHeight)];
[_contentView addSubview: toolbarView]; [_contentView addSubview: toolbarView];
RELEASE(toolbarView); RELEASE(toolbarView);
// Insert the previous content view
/* We want contentViewWithoutToolbarFrame at the origin of our new
content view. There's no guarantee that the old position was (0,0). */
contentViewWithoutToolbarFrame.origin.x = 0;
contentViewWithoutToolbarFrame.origin.y = 0;
[contentViewWithoutToolbar setFrame: contentViewWithoutToolbarFrame];
[_contentView addSubview: contentViewWithoutToolbar];
RELEASE(contentViewWithoutToolbar);
}
else
{
float toolbarViewHeight = [toolbarView frame].size.height;
contentViewWithoutToolbar = [self _contentViewWithoutToolbar];
// Unplug the toolbar view
RETAIN(toolbarView);
[toolbarView removeFromSuperviewWithoutNeedingDisplay];
// Resize the window
[contentViewWithoutToolbar setAutoresizingMask: NSViewMaxYMargin];
windowContentFrame.origin.y += toolbarViewHeight; // Insert the previous content view
windowContentFrame.size.height -= toolbarViewHeight; /* We want contentViewWithoutToolbarFrame at the origin of our new
[self setFrame: [NSWindow frameRectForContentRect: windowContentFrame content view. There's no guarantee that the old position was (0,0). */
styleMask: [self styleMask]] contentViewWithoutToolbarFrame.origin.x = 0;
display: NO]; contentViewWithoutToolbarFrame.origin.y = 0;
[contentViewWithoutToolbar setFrame: contentViewWithoutToolbarFrame];
[contentViewWithoutToolbar setAutoresizingMask: NSViewWidthSizable
| NSViewHeightSizable]; [_contentView addSubview: contentViewWithoutToolbar];
// Autoresizing mask will be set again by the setContentView: method RELEASE(contentViewWithoutToolbar);
}
// Switch the content view
RETAIN(contentViewWithoutToolbar);
// Because setContentView: will release the parent view (aka _contentView) and
// its subviews and actually contentViewWithoutToolbar is a subview of _contentView
[contentViewWithoutToolbar removeFromSuperviewWithoutNeedingDisplay]; - (void) _removeToolbarView: (GSToolbarView *)toolbarView
[self setContentView: contentViewWithoutToolbar]; {
// Views
RELEASE(contentViewWithoutToolbar); NSView *contentViewWithoutToolbar;
}
// Frame
NSRect windowContentFrame
= [NSWindow contentRectForFrameRect: [self frame]
styleMask: [self styleMask]];
float toolbarViewHeight = [toolbarView frame].size.height;
contentViewWithoutToolbar = [self _contentViewWithoutToolbar];
// Unplug the toolbar view
RETAIN(toolbarView);
[toolbarView removeFromSuperviewWithoutNeedingDisplay];
// Resize the window
[contentViewWithoutToolbar setAutoresizingMask: NSViewMaxYMargin];
windowContentFrame.origin.y += toolbarViewHeight;
windowContentFrame.size.height -= toolbarViewHeight;
[self setFrame: [NSWindow frameRectForContentRect: windowContentFrame
styleMask: [self styleMask]]
display: NO];
[contentViewWithoutToolbar setAutoresizingMask: NSViewWidthSizable
| NSViewHeightSizable];
// Autoresizing mask will be set again by the setContentView: method
// Switch the content view
RETAIN(contentViewWithoutToolbar);
// Because setContentView: will release the parent view (aka _contentView) and
// its subviews and actually contentViewWithoutToolbar is a subview of _contentView
[contentViewWithoutToolbar removeFromSuperviewWithoutNeedingDisplay];
[self setContentView: contentViewWithoutToolbar];
RELEASE(contentViewWithoutToolbar);
} }
@end @end