Clean up toolbar item resizing, this now allows for resizable item other than flexible space.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@30371 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
fredkiefer 2010-05-12 23:06:02 +00:00
parent c78d98f976
commit ad422833d4
2 changed files with 136 additions and 114 deletions

View file

@ -27,32 +27,32 @@
Boston, MA 02110-1301, USA. Boston, MA 02110-1301, USA.
*/ */
#include <Foundation/NSObject.h> #import <Foundation/NSObject.h>
#include <Foundation/NSArray.h> #import <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h> #import <Foundation/NSDictionary.h>
#include <Foundation/NSEnumerator.h> #import <Foundation/NSEnumerator.h>
#include <Foundation/NSException.h> #import <Foundation/NSException.h>
#include <Foundation/NSNotification.h> #import <Foundation/NSNotification.h>
#include <Foundation/NSUserDefaults.h> #import <Foundation/NSUserDefaults.h>
#include <Foundation/NSString.h> #import <Foundation/NSString.h>
#include "AppKit/NSButton.h" #import "AppKit/NSButton.h"
#include "AppKit/NSClipView.h" #import "AppKit/NSClipView.h"
#include "AppKit/NSColor.h" #import "AppKit/NSColor.h"
#include "AppKit/NSColorList.h" #import "AppKit/NSColorList.h"
#include "AppKit/NSDragging.h" #import "AppKit/NSDragging.h"
#include "AppKit/NSEvent.h" #import "AppKit/NSEvent.h"
#include "AppKit/NSImage.h" #import "AppKit/NSImage.h"
#include "AppKit/NSMenu.h" #import "AppKit/NSMenu.h"
#include "AppKit/NSPasteboard.h" #import "AppKit/NSPasteboard.h"
// It contains GSMovableToolbarItemPboardType declaration // It contains GSMovableToolbarItemPboardType declaration
#include "AppKit/NSToolbarItem.h" #import "AppKit/NSToolbarItem.h"
#include "AppKit/NSView.h" #import "AppKit/NSView.h"
#include "AppKit/NSWindow.h" #import "AppKit/NSWindow.h"
#include "GNUstepGUI/GSTheme.h" #import "GNUstepGUI/GSTheme.h"
#include "GNUstepGUI/GSToolbarView.h" #import "GNUstepGUI/GSToolbarView.h"
#include "NSToolbarFrameworkPrivate.h" #import "NSToolbarFrameworkPrivate.h"
typedef enum { typedef enum {
ToolbarViewDefaultHeight = 62, ToolbarViewDefaultHeight = 62,
@ -457,12 +457,12 @@ static void initSystemExtensionsColors(void)
- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>)info - (NSDragOperation) draggingEntered: (id <NSDraggingInfo>)info
{ {
return [self updateItemWhileDragging:info exited:NO]; return [self updateItemWhileDragging: info exited: NO];
} }
- (NSDragOperation) draggingUpdated: (id <NSDraggingInfo>)info - (NSDragOperation) draggingUpdated: (id <NSDraggingInfo>)info
{ {
return [self updateItemWhileDragging:info exited:NO]; return [self updateItemWhileDragging: info exited: NO];
} }
- (void) draggingEnded: (id <NSDraggingInfo>)info - (void) draggingEnded: (id <NSDraggingInfo>)info
@ -472,7 +472,7 @@ static void initSystemExtensionsColors(void)
- (void) draggingExited: (id <NSDraggingInfo>)info - (void) draggingExited: (id <NSDraggingInfo>)info
{ {
[self updateItemWhileDragging:info exited:YES]; [self updateItemWhileDragging: info exited: YES];
} }
- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>)info - (BOOL) prepareForDragOperation: (id <NSDraggingInfo>)info
@ -484,7 +484,7 @@ static void initSystemExtensionsColors(void)
{ {
NSToolbar *toolbar = [self toolbar]; NSToolbar *toolbar = [self toolbar];
[self updateItemWhileDragging:info exited:NO]; [self updateItemWhileDragging: info exited: NO];
draggedItemIndex = NSNotFound; draggedItemIndex = NSNotFound;
@ -614,19 +614,17 @@ static void initSystemExtensionsColors(void)
- (void) _handleBackViewsFrame - (void) _handleBackViewsFrame
{ {
NSEnumerator *e = [[_toolbar items] objectEnumerator];
NSToolbarItem *item;
NSView *itemBackView;
NSRect itemBackViewFrame;
float x = 0; float x = 0;
float newHeight = 0; float newHeight = 0;
// ---
NSArray *subviews = [_clipView subviews]; NSArray *subviews = [_clipView subviews];
NSEnumerator *e = [[_toolbar items] objectEnumerator];
//_heightFromLayout = 0; NSToolbarItem *item;
while ((item = [e nextObject]) != nil) while ((item = [e nextObject]) != nil)
{ {
NSView *itemBackView;
NSRect itemBackViewFrame;
itemBackView = [item _backView]; itemBackView = [item _backView];
if ([subviews containsObject: itemBackView] == NO if ([subviews containsObject: itemBackView] == NO
|| [item _isModified] || [item _isModified]
@ -662,7 +660,8 @@ static void initSystemExtensionsColors(void)
float lengthAvailable; float lengthAvailable;
unsigned int flexibleSpaceItemsNumber = 0; unsigned int flexibleSpaceItemsNumber = 0;
BOOL mustAdjustNext = NO; BOOL mustAdjustNext = NO;
float x = 0; CGFloat x = 0.0;
CGFloat maxX = 0.0;
if ([items count] == 0) if ([items count] == 0)
return; return;
@ -679,8 +678,11 @@ static void initSystemExtensionsColors(void)
{ {
flexibleSpaceItemsNumber++; flexibleSpaceItemsNumber++;
} }
maxX += [item maxSize].width;
} }
if (flexibleSpaceItemsNumber > 0)
{
if (lengthAvailable < flexibleSpaceItemsNumber) if (lengthAvailable < flexibleSpaceItemsNumber)
return; return;
@ -706,40 +708,55 @@ static void initSystemExtensionsColors(void)
} }
x += [backView frame].size.width; x += [backView frame].size.width;
} }
}
else
{
// No flexible space
// Could the items fill more space?
if (maxX >= NSMaxX(lastBackViewFrame))
{
CGFloat rel = lengthAvailable / (maxX - NSMaxX(lastBackViewFrame));
e = [items objectEnumerator];
while ((item = [e nextObject]) != nil)
{
CGFloat diff = [item maxSize].width - [item minSize].width;
backView = [item _backView];
if (diff > 0)
{
NSRect backViewFrame = [backView frame];
[backView setFrame: NSMakeRect(x, backViewFrame.origin.y,
(rel * diff) + [item minSize].width,
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 - (void) _handleViewsVisibility
{ {
NSArray *items = [_toolbar items]; NSArray *backViews;
/* The back views which are associated with each toolbar item (the toolbar
items doesn't reflect the toolbar view content) */
NSArray *backViews = [items valueForKey: @"_backView"];
/* The back views which will be visible in the toolbar view (when
_handleViewsVisibility will be terminated). */
NSArray *visibleBackViews;
NSArray *subviews; NSArray *subviews;
NSEnumerator *e; NSEnumerator *e;
NSView *backView; NSView *backView;
NSRect clipViewFrame;
// First, we resize /* The back views which are associated with each toolbar item (the toolbar
[self _handleBackViewsFrame]; items doesn't reflect the toolbar view content) */
[self _takeInAccountFlexibleSpaces]; backViews = [[_toolbar items] valueForKey: @"_backView"];
// Then we retrieve the back views which should be visible now that the resize
// process has been taken in account
visibleBackViews = [self _visibleBackViews];
// ---
// We remove each back view associated with a removed toolbar item // We remove each back view associated with a removed toolbar item
e = [[_clipView subviews] objectEnumerator]; e = [[_clipView subviews] objectEnumerator];
while ((backView = [e nextObject]) != nil) while ((backView = [e nextObject]) != nil)
{ {
if ([backViews containsObject: backView] == NO) if ([backViews containsObject: backView] == NO)
@ -750,10 +767,8 @@ static void initSystemExtensionsColors(void)
} }
// We add each backView associated with an added toolbar item // We add each backView associated with an added toolbar item
e = [backViews objectEnumerator];
subviews = [_clipView subviews]; subviews = [_clipView subviews];
e = [backViews objectEnumerator];
while ((backView = [e nextObject]) != nil) while ((backView = [e nextObject]) != nil)
{ {
if ([subviews containsObject: backView] == NO) if ([subviews containsObject: backView] == NO)
@ -761,21 +776,22 @@ static void initSystemExtensionsColors(void)
[_clipView addSubview: backView]; [_clipView addSubview: backView];
} }
} }
}
// --- - (void) _manageClipView
{
NSRect clipViewFrame = [_clipView frame];
int count = [[_toolbar items] count];
// Retrieve the back views which should be visible now that the resize
// process has been taken in account
NSArray *visibleBackViews = [self _visibleBackViews];
/* We manage the clipped items view in the case it should become visible or if ([visibleBackViews count] < count)
invisible */
clipViewFrame = [_clipView frame];
if ([visibleBackViews count] < [backViews count])
{ {
NSView *lastVisibleBackView = [visibleBackViews lastObject]; NSView *lastVisibleBackView = [visibleBackViews lastObject];
float width = 0; float width = 0;
// Resize the clip view // Resize the clip view
if (lastVisibleBackView != nil) if (lastVisibleBackView != nil)
width = NSMaxX([lastVisibleBackView frame]); width = NSMaxX([lastVisibleBackView frame]);
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x, [_clipView setFrame: NSMakeRect(clipViewFrame.origin.x,
@ -784,21 +800,19 @@ static void initSystemExtensionsColors(void)
clipViewFrame.size.height)]; clipViewFrame.size.height)];
// Adjust the clipped items mark frame handling // Adjust the clipped items mark frame handling
[_clippedItemsMark layout]; [_clippedItemsMark layout];
clipViewFrame = [_clipView frame]; // We get the new _clipView frame // We get the new _clipView frame
clipViewFrame = [_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];
} }
else if (([_clippedItemsMark superview] != nil) else if (([_clippedItemsMark superview] != nil)
&& ([visibleBackViews count] == [backViews count])) && ([visibleBackViews count] == count))
{ {
[_clippedItemsMark removeFromSuperview]; [_clippedItemsMark removeFromSuperview];
@ -811,7 +825,15 @@ static void initSystemExtensionsColors(void)
- (void) _reload - (void) _reload
{ {
// First, we resize
[self _handleBackViewsFrame];
[self _takeInAccountFlexibleSpaces];
[self _handleViewsVisibility]; [self _handleViewsVisibility];
/* We manage the clipped items view in the case it should become visible or
invisible */
[self _manageClipView];
[self setNeedsDisplay: YES]; [self setNeedsDisplay: YES];
} }
@ -846,8 +868,7 @@ static void initSystemExtensionsColors(void)
int i, n = [items count]; int i, n = [items count];
float backViewsWidth = 0, toolbarWidth = [self frame].size.width; float backViewsWidth = 0, toolbarWidth = [self frame].size.width;
//[_visibleBackViews release]; NSMutableArray *visibleBackViews = [NSMutableArray array];
NSMutableArray *_visibleBackViews = [NSMutableArray array];
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
{ {
@ -858,12 +879,11 @@ static void initSystemExtensionsColors(void)
if ((backViewsWidth + ClippedItemsViewWidth <= toolbarWidth) if ((backViewsWidth + ClippedItemsViewWidth <= toolbarWidth)
|| (i == n - 1 && backViewsWidth <= toolbarWidth)) || (i == n - 1 && backViewsWidth <= toolbarWidth))
{ {
[_visibleBackViews addObject: backView]; [visibleBackViews addObject: backView];
} }
} }
return _visibleBackViews; return visibleBackViews;
} }
- (NSColor *) standardBackgroundColor - (NSColor *) standardBackgroundColor

View file

@ -613,18 +613,20 @@ NSString *GSMovableToolbarItemPboardType = @"GSMovableToolbarItemPboardType";
if ([view superview] == nil) // Show the view to eventually hide it later if ([view superview] == nil) // Show the view to eventually hide it later
[self addSubview: view]; [self addSubview: view];
if([view respondsToSelector:@selector(sizeToFit)]) if ([view respondsToSelector: @selector(sizeToFit)])
{ {
NSSize newSize, minSize = [_toolbarItem minSize]; NSSize newSize, minSize = [_toolbarItem minSize];
[view performSelector:@selector(sizeToFit)];
[(id)view sizeToFit];
newSize = [view frame].size; newSize = [view frame].size;
if (newSize.width < minSize.width || newSize.height < minSize.height) if (newSize.width < minSize.width || newSize.height < minSize.height)
{ {
newSize.width = MAX(newSize.width, minSize.width); newSize.width = MAX(newSize.width, minSize.width);
newSize.height = MAX(newSize.height, minSize.height); newSize.height = MAX(newSize.height, minSize.height);
[view setFrameSize:newSize]; [view setFrameSize: newSize];
} }
} }
// Adjust the layout in accordance with NSToolbarSizeMode // Adjust the layout in accordance with NSToolbarSizeMode
switch ([[_toolbarItem toolbar] sizeMode]) switch ([[_toolbarItem toolbar] sizeMode])
{ {
@ -1495,7 +1497,7 @@ NSString *GSMovableToolbarItemPboardType = @"GSMovableToolbarItemPboardType";
- (BOOL) _isFlexibleSpace - (BOOL) _isFlexibleSpace
{ {
return [self isKindOfClass:[GSToolbarFlexibleSpaceItem class]]; return NO;
} }
- (BOOL) _selectable - (BOOL) _selectable