diff --git a/ChangeLog b/ChangeLog index 9097c6cb3..47805e32a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,17 @@ 2009-01-02 Fred Kiefer - * Source/NSWindow+Toolbar.m, + * Headers/Additions/GNUstepGUI/GSToolbarView.h: Remove now + unneeded include of NSToolbar.h + * Source/NSToolbarFrameworkPrivate.h: New header file with shared + toolbar classes extensions. + * Source/NSWindow+Toolbar.m, + * Source/NSToolbar.m + * Source/NSToolbarItem.m + * Source/GSToolbarView.m: Use this new header file. + +2009-01-02 Fred Kiefer + + * Source/NSWindow+Toolbar.m, * Source/NSToolbar.m: Rewrote interaction with GSToolbarView. * Source/NSToolbar.m (_toolbarItemForIdentifier:): Fix memory leak. * Headers/Additions/GNUstepGUI/GSToolbarView.h, diff --git a/Headers/Additions/GNUstepGUI/GSToolbarView.h b/Headers/Additions/GNUstepGUI/GSToolbarView.h index 39f2565ba..0f6ea0a13 100644 --- a/Headers/Additions/GNUstepGUI/GSToolbarView.h +++ b/Headers/Additions/GNUstepGUI/GSToolbarView.h @@ -33,14 +33,12 @@ #define _GSToolbarView_h_INCLUDE #include -// Necessary for NSToolbarDisplayMode and NSToolbarSizeMode -#include #include #include - @class NSMutableArray; @class NSClipView; +@class NSToolbar; @class NSToolbarItem; @class GSToolbarClippedItemsButton; diff --git a/Source/GSToolbarView.m b/Source/GSToolbarView.m index c26822700..d4cc957ed 100644 --- a/Source/GSToolbarView.m +++ b/Source/GSToolbarView.m @@ -50,6 +50,8 @@ #include "AppKit/NSWindow.h" #include "GNUstepGUI/GSToolbarView.h" +#include "NSToolbarFrameworkPrivate.h" + typedef enum { ToolbarViewDefaultHeight = 62, ToolbarViewRegularHeight = 62, @@ -155,34 +157,6 @@ static void initSystemExtensionsColors(void) /* * Toolbar related code */ -@interface NSToolbar (GNUstepPrivate) -- (void) _build; - -- (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) _toolbarViewWillMoveToSuperview: (NSView *)newSuperview; - -// Accessors -- (void) _setToolbarView: (GSToolbarView *)toolbarView; -- (GSToolbarView *) _toolbarView; -@end - -@interface NSToolbarItem (GNUstepPrivate) -- (void) _layout; - -// Accessors -- (NSView *) _backView; -- (NSMenuItem *) _defaultMenuFormRepresentation; -- (BOOL) _isModified; -- (BOOL) _isFlexibleSpace; -@end @interface GSToolbarButton - (NSToolbarItem *) toolbarItem; @@ -192,20 +166,6 @@ static void initSystemExtensionsColors(void) - (NSToolbarItem *) toolbarItem; @end -@interface GSToolbarView (GNUstepPrivate) -- (void) _handleBackViewsFrame; -- (void) _handleViewsVisibility; -- (void) _reload; -- (void) _takeInAccountFlexibleSpaces; -- (int) _insertionIndexAtPoint: (NSPoint)location; - -// Accessors -- (float) _heightFromLayout; -- (NSArray *) _visibleBackViews; -- (BOOL) _usesStandardBackgroundColor; -- (void) _setUsesStandardBackgroundColor: (BOOL)standard; -@end - @interface GSToolbarClippedItemsButton : NSButton { NSToolbar *_toolbar; @@ -374,6 +334,25 @@ static void initSystemExtensionsColors(void) // Dragging related methods +- (int) _insertionIndexAtPoint: (NSPoint)location +{ + id hitView = [self hitTest: location]; + NSRect hitViewFrame = [hitView frame]; + int index; + + if ((hitView != nil) + && ([hitView isKindOfClass: NSClassFromString(@"GSToolbarButton")] + || [hitView isKindOfClass: NSClassFromString(@"GSToolbarBackView")])) + { + index = [_toolbar _indexOfItem: [hitView toolbarItem]]; + if (location.x - hitViewFrame.origin.x > hitViewFrame.size.width / 2) + index++; + + return index; + } + return NSNotFound; +} + - (NSDragOperation) draggingEntered: (id )info { NSToolbar *toolbar = [self toolbar]; @@ -646,6 +625,63 @@ static void initSystemExtensionsColors(void) _heightFromLayout = newHeight; } +- (void) _takeInAccountFlexibleSpaces +{ + NSArray *items = [_toolbar items]; + NSEnumerator *e = [items objectEnumerator]; + NSToolbarItem *item; + NSView *backView; + NSRect lastBackViewFrame; + float lengthAvailable; + unsigned int flexibleSpaceItemsNumber = 0; + BOOL mustAdjustNext = NO; + float x = 0; + + if ([items count] == 0) + return; + + lastBackViewFrame = [[[items lastObject] _backView] frame]; + lengthAvailable = [self frame].size.width - NSMaxX(lastBackViewFrame); + + if (lengthAvailable < 1) + return; + + while ((item = [e nextObject]) != nil) + { + if ([item _isFlexibleSpace]) + { + flexibleSpaceItemsNumber++; + } + } + + if (lengthAvailable < flexibleSpaceItemsNumber) + return; + + e = [items objectEnumerator]; + while ((item = [e nextObject]) != nil) + { + backView = [item _backView]; + if ([item _isFlexibleSpace]) + { + NSRect backViewFrame = [backView frame]; + + [backView setFrame: NSMakeRect(x, backViewFrame.origin.y, + lengthAvailable / flexibleSpaceItemsNumber, + backViewFrame.size.height)]; + mustAdjustNext = YES; + } + else if (mustAdjustNext) + { + NSRect backViewFrame = [backView frame]; + + [backView setFrame: NSMakeRect(x, backViewFrame.origin.y, + backViewFrame.size.width, backViewFrame.size.height)]; + } + x += [backView frame].size.width; + } + +} + - (void) _handleViewsVisibility { NSArray *items = [_toolbar items]; @@ -752,82 +788,6 @@ static void initSystemExtensionsColors(void) [self setNeedsDisplay: YES]; } -- (void) _takeInAccountFlexibleSpaces -{ - NSArray *items = [_toolbar items]; - NSEnumerator *e = [items objectEnumerator]; - NSToolbarItem *item; - NSView *backView; - NSRect lastBackViewFrame; - float lengthAvailable; - unsigned int flexibleSpaceItemsNumber = 0; - BOOL mustAdjustNext = NO; - float x = 0; - - if ([items count] == 0) - return; - - lastBackViewFrame = [[[items lastObject] _backView] frame]; - lengthAvailable = [self frame].size.width - NSMaxX(lastBackViewFrame); - - if (lengthAvailable < 1) - return; - - while ((item = [e nextObject]) != nil) - { - if ([item _isFlexibleSpace]) - { - flexibleSpaceItemsNumber++; - } - } - - if (lengthAvailable < flexibleSpaceItemsNumber) - return; - - e = [items objectEnumerator]; - while ((item = [e nextObject]) != nil) - { - backView = [item _backView]; - if ([item _isFlexibleSpace]) - { - NSRect backViewFrame = [backView frame]; - - [backView setFrame: NSMakeRect(x, backViewFrame.origin.y, - lengthAvailable / flexibleSpaceItemsNumber, - backViewFrame.size.height)]; - mustAdjustNext = YES; - } - else if (mustAdjustNext) - { - NSRect backViewFrame = [backView frame]; - - [backView setFrame: NSMakeRect(x, backViewFrame.origin.y, - backViewFrame.size.width, backViewFrame.size.height)]; - } - x += [backView frame].size.width; - } - -} - -- (int) _insertionIndexAtPoint: (NSPoint)location -{ - id hitView = [self hitTest: location]; - NSRect hitViewFrame = [hitView frame]; - int index; - - if ((hitView != nil) - && ([hitView isKindOfClass: NSClassFromString(@"GSToolbarButton")] - || [hitView isKindOfClass: NSClassFromString(@"GSToolbarBackView")])) - { - index = [_toolbar _indexOfItem: [hitView toolbarItem]]; - if (location.x - hitViewFrame.origin.x > hitViewFrame.size.width / 2) - index++; - - return index; - } - return NSNotFound; -} - // Accessors private methods - (float) _heightFromLayout diff --git a/Source/NSToolbar.m b/Source/NSToolbar.m index abbbe6632..19dec2e9b 100644 --- a/Source/NSToolbar.m +++ b/Source/NSToolbar.m @@ -52,6 +52,8 @@ #include "GNUstepGUI/GSToolbarView.h" #include "AppKit/NSToolbar.h" +#include "NSToolbarFrameworkPrivate.h" + // internal static NSNotificationCenter *nc = nil; static NSMutableArray *toolbars = nil; @@ -467,68 +469,6 @@ static GSValidationCenter *vc = nil; @end -@interface NSToolbar (GNUstepPrivate) -// Private class method -+ (NSArray *) _toolbarsWithIdentifier: (NSString *)identifier; - -// Private methods with broadcast support -- (void) _insertItemWithItemIdentifier: (NSString *)itemIdentifier - atIndex: (int)index - broadcast: (BOOL)broadcast; -- (void) _removeItemAtIndex: (int)index broadcast: (BOOL)broadcast; -- (void) _setAllowsUserCustomization: (BOOL)flag broadcast: (BOOL)broadcast; -- (void) _setAutosavesConfiguration: (BOOL)flag broadcast: (BOOL)broadcast; -- (void) _setConfigurationFromDictionary: (NSDictionary *)configDict - broadcast: (BOOL)broadcast; -- (void) _moveItemFromIndex: (int)index toIndex: (int)newIndex broadcast: (BOOL)broadcast; -- (void) _setDisplayMode: (NSToolbarDisplayMode)displayMode - broadcast: (BOOL)broadcast; -- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode - broadcast: (BOOL)broadcast; -- (void) _setVisible: (BOOL)shown broadcast: (BOOL)broadcast; - -// Few other private methods -- (void) _build; -- (int) _indexOfItem: (NSToolbarItem *)item; -- (void) _insertPassivelyItem: (NSToolbarItem *)item atIndex: (int)newIndex; -- (void) _performRemoveItem: (NSToolbarItem *)item; -- (void) _concludeRemoveItem: (NSToolbarItem *)item atIndex: (int)index broadcast: (BOOL)broadcast; -- (void) _loadConfig; -- (NSToolbarItem *) _toolbarItemForIdentifier: (NSString *)itemIdent; -- (NSToolbar *) _toolbarModel; -- (void) _validate: (NSWindow *)observedWindow; -- (void) _toolbarViewWillMoveToSuperview: (NSView *)newSuperview; - -// Accessors -- (void) _setToolbarView: (GSToolbarView *)toolbarView; -- (GSToolbarView *) _toolbarView; - -- (void) setUsesStandardBackgroundColor: (BOOL)standard; -- (BOOL) usesStandardBackgroundColor; - -@end - -@interface NSToolbarItem (GNUstepPrivate) -- (BOOL) _selectable; -- (void) _setSelectable: (BOOL)selectable; -- (BOOL) _selected; -- (void) _setSelected: (BOOL)selected; -- (void) _setToolbar: (NSToolbar *)toolbar; -@end - -@interface GSToolbarView (GNUstepPrivate) -- (void) _reload; - -// Accessors -- (NSArray *) _visibleBackViews; -- (BOOL) _usesStandardBackgroundColor; -- (void) _setUsesStandardBackgroundColor: (BOOL)standard; -@end - -@interface NSWindow (ToolbarPrivate) -- (void) _adjustToolbarView: (GSToolbarView*)view; -@end - // --- @implementation NSToolbar diff --git a/Source/NSToolbarFrameworkPrivate.h b/Source/NSToolbarFrameworkPrivate.h new file mode 100644 index 000000000..a1853ea2f --- /dev/null +++ b/Source/NSToolbarFrameworkPrivate.h @@ -0,0 +1,118 @@ +/* + NSToolbarFrameworkPrivate.h + + Private methods used throughout the toolbar classes. + + Copyright (C) 2009 Free Software Foundation, Inc. + + Author: Fred Kiefer + Date: January 2009 + + 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 Lesser 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; see the file COPYING.LIB. + If not, see or write to the + Free Software Foundation, 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef _NSToolbarFrameworkPrivate_h_INCLUDE +#define _NSToolbarFrameworkPrivate_h_INCLUDE + +#include "AppKit/NSToolbar.h" +#include "AppKit/NSToolbarItem.h" +#include "AppKit/NSWindow+Toolbar.h" +#include "GNUstepGUI/GSToolbarView.h" + +@interface GSToolbarView (GNUstepPrivate) +- (void) _reload; + +// Accessors +- (float) _heightFromLayout; +- (NSArray *) _visibleBackViews; + +- (BOOL) _usesStandardBackgroundColor; +- (void) _setUsesStandardBackgroundColor: (BOOL)standard; +@end + +@interface NSToolbarItem (GNUstepPrivate) +- (void) _layout; +- (void) _computeFlags; + +// Accessors +- (NSView *) _backView; +- (NSMenuItem *) _defaultMenuFormRepresentation; +- (BOOL) _isModified; +- (BOOL) _isFlexibleSpace; +- (BOOL) _selectable; +- (void) _setSelectable: (BOOL)selectable; +- (BOOL) _selected; +- (void) _setSelected: (BOOL)selected; +- (void) _setToolbar: (NSToolbar *)toolbar; +@end + +@interface NSToolbar (GNUstepPrivate) +// Private class method ++ (NSArray *) _toolbarsWithIdentifier: (NSString *)identifier; + +// Private methods with broadcast support +- (void) _insertItemWithItemIdentifier: (NSString *)itemIdentifier + atIndex: (int)index + broadcast: (BOOL)broadcast; +- (void) _removeItemAtIndex: (int)index broadcast: (BOOL)broadcast; +- (void) _setAllowsUserCustomization: (BOOL)flag broadcast: (BOOL)broadcast; +- (void) _setAutosavesConfiguration: (BOOL)flag broadcast: (BOOL)broadcast; +- (void) _setConfigurationFromDictionary: (NSDictionary *)configDict + broadcast: (BOOL)broadcast; +- (void) _moveItemFromIndex: (int)index toIndex: (int)newIndex broadcast: (BOOL)broadcast; +- (void) _setDisplayMode: (NSToolbarDisplayMode)displayMode + broadcast: (BOOL)broadcast; +- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode + broadcast: (BOOL)broadcast; +- (void) _setVisible: (BOOL)shown broadcast: (BOOL)broadcast; + +// Few other private methods +- (void) _build; + +- (int) _indexOfItem: (NSToolbarItem *)item; +- (void) _concludeRemoveItem: (NSToolbarItem *)item + atIndex: (int)index + broadcast: (BOOL)broadcast; +- (void) _insertPassivelyItem: (NSToolbarItem *)item atIndex: (int)newIndex; +- (void) _moveItemFromIndex: (int)index + toIndex: (int)newIndex + broadcast: (BOOL)broacast; +- (void) _performRemoveItem: (NSToolbarItem *)item; // Used by drag setup +- (void) _loadConfig; +- (NSToolbarItem *) _toolbarItemForIdentifier: (NSString *)itemIdent; +- (NSToolbar *) _toolbarModel; +- (void) _validate: (NSWindow *)observedWindow; +- (void) _toolbarViewWillMoveToSuperview: (NSView *)newSuperview; + +// Accessors +- (void) _setToolbarView: (GSToolbarView *)toolbarView; +- (GSToolbarView *) _toolbarView; +- (void) setUsesStandardBackgroundColor: (BOOL)standard; +- (BOOL) usesStandardBackgroundColor; +@end + +@interface NSWindow (ToolbarPrivate) +- (void) _adjustToolbarView: (GSToolbarView*)view; +- (void) _addToolbarView: (GSToolbarView*)view; +- (void) _removeToolbarView: (GSToolbarView*)view; +- (NSView *) _contentViewWithoutToolbar; +@end + + +#endif // _NSToolbarFrameworkPrivate_h_INCLUDE diff --git a/Source/NSToolbarItem.m b/Source/NSToolbarItem.m index 799b6d2e2..06d4d0f17 100644 --- a/Source/NSToolbarItem.m +++ b/Source/NSToolbarItem.m @@ -50,6 +50,8 @@ #include "GNUstepGUI/GSToolbarView.h" #include "AppKit/NSToolbarItem.h" +#include "NSToolbarFrameworkPrivate.h" + /* * Each NSToolbarItem object are coupled with a backView which is their * representation on the screen. @@ -89,32 +91,6 @@ static NSFont *SmallFont = nil; NSString *GSMovableToolbarItemPboardType = @"GSMovableToolbarItemPboardType"; -@interface NSToolbar (GNUstepPrivate) -- (GSToolbarView *) _toolbarView; -- (int) _indexOfItem: (NSToolbarItem *)item; // Used by drag setup - -- (void) _performRemoveItem: (NSToolbarItem *)item; // Used by drag setup -@end - -@interface NSToolbarItem (GNUstepPrivate) -- (void) _layout; -// --- -- (void) _computeFlags; -- (NSView *) _backView; -- (NSMenuItem *) _defaultMenuFormRepresentation; -- (BOOL) _isFlexibleSpace; -- (BOOL) _isModified; -- (BOOL) _selectable; -- (void) _setSelectable: (BOOL)selectable; -- (BOOL) _selected; -- (void) _setSelected: (BOOL)selected; -- (void) _setToolbar: (NSToolbar *)toolbar; -@end - -@interface GSToolbarView (GNUstepPrivate) -- (void) _reload; -@end - /* * NSButton subclass is the toolbar buttons _backView */ diff --git a/Source/NSWindow+Toolbar.m b/Source/NSWindow+Toolbar.m index 73b0bfac1..2cb9ae959 100644 --- a/Source/NSWindow+Toolbar.m +++ b/Source/NSWindow+Toolbar.m @@ -34,21 +34,7 @@ #include "AppKit/NSToolbar.h" #include "GNUstepGUI/GSToolbarView.h" -@interface NSToolbar (GNUstepPrivate) -- (GSToolbarView *) _toolbarView; -@end - -@interface GSToolbarView (GNUstepPrivate) -- (float) _heightFromLayout; -- (void) _reload; -@end - -@interface NSWindow (ToolbarPrivate) -- (void) _adjustToolbarView: (GSToolbarView*)view; -- (void) _addToolbarView: (GSToolbarView*)view; -- (void) _removeToolbarView: (GSToolbarView*)view; -- (NSView *) _contentViewWithoutToolbar; -@end +#include "NSToolbarFrameworkPrivate.h" @implementation NSWindow (Toolbar) @@ -123,6 +109,8 @@ else { // Instantiate the toolbar view + // FIXME: Currently this is reatined until the toolbar + // gets removed from the window. toolbarView = [[GSToolbarView alloc] initWithFrame: NSMakeRect(0, 0, @@ -267,7 +255,6 @@ contentViewWithoutToolbarFrame.size.width, newToolbarViewHeight)]; [_contentView addSubview: toolbarView]; - RELEASE(toolbarView); // Insert the previous content view /* We want contentViewWithoutToolbarFrame at the origin of our new @@ -295,7 +282,6 @@ contentViewWithoutToolbar = [self _contentViewWithoutToolbar]; // Unplug the toolbar view - RETAIN(toolbarView); [toolbarView removeFromSuperviewWithoutNeedingDisplay]; // Resize the window