Reworked toolbar implementation

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@18840 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
qmathe 2004-03-12 22:55:02 +00:00
parent 2f8484ff24
commit 141d738af8
10 changed files with 1045 additions and 654 deletions

View file

@ -1,11 +1,11 @@
/*
<title>GSToolbarView.m</title>
<Title>GSToolbarView.m</title>
<abstract>The toolbar view class.</abstract>
Copyright (C) 2004 Free Software Foundation, Inc.
Author: Quentin Mathé <qmathe@club-internet.fr>
Author: Quentin Mathe <qmathe@club-internet.fr>
Date: January 2004
This file is part of the GNUstep GUI Library.
@ -29,9 +29,7 @@
#include <Foundation/NSObject.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSException.h>
#include "AppKit/NSToolbarItem.h"
#include "AppKit/NSToolbar.h"
#include "AppKit/NSView.h"
#include "AppKit/NSClipView.h"
#include "AppKit/NSButton.h"
@ -40,13 +38,14 @@
#include "AppKit/NSMenu.h"
#include "AppKit/NSEvent.h"
#include "AppKit/NSWindow.h"
#include "GNUstepGUI/GSToolbar.h"
#include "GNUstepGUI/GSToolbarView.h"
// internal
static const int current_version = 1;
@interface NSToolbar (GNUstepPrivate)
@interface GSToolbar (GNUstepPrivate)
- (void) _build;
- (void) _setToolbarView: (GSToolbarView *)toolbarView;
@end
@ -58,24 +57,11 @@ static const int current_version = 1;
- (void) _layout;
@end
@interface GSToolbarClippedItemsButton : NSButton
{
NSToolbar *_toolbar;
}
- (id) init;
// Accessors
- (NSMenu *) returnMenu; // this method cannot be called menu otherwise
// it would override NSResponder method with the same name
- (void)setToolbar: (NSToolbar *)toolbar;
@end
@interface GSToolbarView (GNUstepPrivate)
- (void) _handleViewsSize;
- (void) _handleViewsOrigin;
- (void) _handleViewsFrame;
- (void) _handleViewsVisibility;
- (void) _reload;
- (void) _setToolbar: (GSToolbar *)toolbar;
- (void) _takeInAccountFlexibleSpaces;
// Accessors
@ -84,19 +70,37 @@ static const int current_version = 1;
- (BOOL) _willBeVisible;
@end
@interface GSToolbarClippedItemsButton : NSButton
{
GSToolbar *_toolbar;
}
- (id) init;
// Accessors
- (NSMenu *) returnMenu;
// this method cannot be called "menu" otherwise it would override NSResponder
// method with the same name
- (void)setToolbar: (GSToolbar *)toolbar;
@end
@implementation GSToolbarClippedItemsButton
- (id)init
{
NSImage *image = [NSImage imageNamed: @"common_ToolbarClippedItemsMark"];
if ((self = [super initWithFrame: NSMakeRect(0, 0, _ClippedItemsViewWidth, _ItemBackViewDefaultHeight)]) != nil)
if ((self = [super initWithFrame: NSMakeRect(0, 0, _ClippedItemsViewWidth,
_ItemBackViewDefaultHeight)]) != nil)
{
[self setBordered: NO];
[[self cell] setHighlightsBy: NSChangeGrayCellMask | NSChangeBackgroundCellMask];
[self setAutoresizingMask: (NSViewNotSizable | NSViewMinXMargin)];
[[self cell] setHighlightsBy: NSChangeGrayCellMask
| NSChangeBackgroundCellMask];
[self setAutoresizingMask: NSViewNotSizable | NSViewMinXMargin];
[self setImagePosition: NSImageOnly];
[image setScalesWhenResized: YES];
[image setSize: NSMakeSize(20., 20.)];
[image setSize: NSMakeSize(20, 20)];
[self setImage: image];
return self;
}
@ -106,10 +110,15 @@ static const int current_version = 1;
- (void)mouseDown: (NSEvent *)event {
NSMenu *clippedItemsMenu = [self menuForEvent:event];
[super highlight: YES];
if (clippedItemsMenu != nil)
{
[NSMenu popUpContextMenu: clippedItemsMenu withEvent: event forView: self];
[NSMenu popUpContextMenu: clippedItemsMenu withEvent: event
forView: self];
}
[super highlight: NO];
}
- (NSMenu *)menuForEvent: (NSEvent *)event {
@ -121,8 +130,9 @@ static const int current_version = 1;
}
- (NSMenu *)returnMenu
// this method cannot be called menu otherwise it would override NSResponder method with the same name
{
// this method cannot be called menu otherwise it would
// override NSResponder method with the same name
NSMenu *menu = [[NSMenu alloc] initWithTitle:@""];
NSEnumerator *e;
id item;
@ -150,7 +160,7 @@ static const int current_version = 1;
// Accessors
- (void)setToolbar: (NSToolbar *)toolbar
- (void)setToolbar: (GSToolbar *)toolbar
{
ASSIGN(_toolbar, toolbar);
}
@ -163,15 +173,22 @@ static const int current_version = 1;
{
if((self = [super initWithFrame: frame]) != nil)
{
_clipView = [[NSClipView alloc] initWithFrame: NSMakeRect(0, 1, frame.size.width, frame.size.height)];
[_clipView setAutoresizingMask: (NSViewWidthSizable | NSViewHeightSizable)];
_clipView = [[NSClipView alloc] initWithFrame:
NSMakeRect(0, 1, frame.size.width, frame.size.height)];
[_clipView setAutoresizingMask: (NSViewWidthSizable |
NSViewHeightSizable)];
[self addSubview: _clipView];
_clippedItemsMark = [[GSToolbarClippedItemsButton alloc] init];
_borderMask = GSToolbarViewTopBorder | GSToolbarViewBottomBorder | GSToolbarViewRightBorder | GSToolbarViewLeftBorder;
_borderMask = GSToolbarViewTopBorder | GSToolbarViewBottomBorder
| GSToolbarViewRightBorder | GSToolbarViewLeftBorder;
return self;
}
return self;
return nil;
}
- (void) dealloc
@ -204,7 +221,8 @@ static const int current_version = 1;
if (_borderMask & GSToolbarViewTopBorder)
{
[NSBezierPath strokeLineFromPoint: NSMakePoint(0, viewFrame.size.height - 0.5)
toPoint: NSMakePoint(viewFrame.size.width, viewFrame.size.height - 0.5)];
toPoint: NSMakePoint(viewFrame.size.width,
viewFrame.size.height - 0.5)];
}
if (_borderMask & GSToolbarViewLeftBorder)
{
@ -213,8 +231,9 @@ static const int current_version = 1;
}
if (_borderMask & GSToolbarViewRightBorder)
{
[NSBezierPath strokeLineFromPoint: NSMakePoint(viewFrame.size.width - 0.5, 0)
toPoint: NSMakePoint(viewFrame.size.width - 0.5, viewFrame.size.height)];
[NSBezierPath strokeLineFromPoint: NSMakePoint(viewFrame.size.width - 0.5,0)
toPoint: NSMakePoint(viewFrame.size.width - 0.5,
viewFrame.size.height)];
}
[super drawRect: aRect];
@ -228,7 +247,8 @@ static const int current_version = 1;
- (void) windowDidResize: (NSNotification *)notification
{
if (![_toolbar isVisible]) return;
if ([self superview] == nil)
return;
[self _handleViewsVisibility];
}
@ -247,11 +267,13 @@ static const int current_version = 1;
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
// NSView method called when a view is moved to a window (NSView has a variable _window)
// NSView method called when a view is moved to a window (NSView has a
// variable _window)
[super viewDidMoveToWindow];
[nc removeObserver: self name: NSWindowDidResizeNotification object: _window];
[nc addObserver: self selector: @selector(windowDidResize:) name: NSWindowDidResizeNotification object: nil];
[nc addObserver: self selector: @selector(windowDidResize:)
name: NSWindowDidResizeNotification object: nil];
[self viewDidMoveToSuperview];
}
@ -263,7 +285,7 @@ static const int current_version = 1;
return _borderMask;
}
- (NSToolbar *) toolbar
- (GSToolbar *) toolbar
{
return _toolbar;
}
@ -273,63 +295,48 @@ static const int current_version = 1;
_borderMask = borderMask;
}
- (void) setToolbar: (NSToolbar *)toolbar
- (void) setToolbar: (GSToolbar *)toolbar
{
ASSIGN(_toolbar, toolbar);
[_clippedItemsMark setToolbar: _toolbar];
if ([toolbar isKindOfClass: [NSToolbar class]])
[NSException raise: NSInvalidArgumentException
format: @"NSToolbar instance can't be attached directly to a \
toolbar view, setToolbar: from the NSWindow toolbar \
category must be used."];
[_toolbar _build]; // Use the delegate to build the toolbar
[_toolbar _setToolbarView: self];
[self _reload]; // Load the toolbar in the toolbar view
[self _setToolbar: toolbar];
}
// Private methods
- (void) _handleViewsOrigin
- (void) _handleViewsFrame
{
NSEnumerator *e = [[_toolbar items] objectEnumerator];
NSToolbarItem *item;
NSView *itemView;
NSRect itemViewFrame;
float x = 0.;
while ((item = [e nextObject]) != nil)
{
itemView = [item _backView];
if ([item _isFlexibleSpace])
{
[item _layout];
}
itemViewFrame = [itemView frame];
[itemView setFrame: NSMakeRect(x, itemViewFrame.origin.y, itemViewFrame.size.width, itemViewFrame.size.height)];
x += [itemView frame].size.width;
}
}
- (void) _handleViewsSize
{
NSEnumerator *e = [[_toolbar items] objectEnumerator];
NSToolbarItem *item;
NSView *itemView, *itemViewEdited;
NSView *itemBackView;
NSRect itemBackViewFrame;
float x = 0;
// ---
NSArray *subviews = [self subviews];
while ((item = [e nextObject]) != nil)
{
itemView = [item _backView];
if (![subviews containsObject: itemView] || [item _isModified])
itemBackView = [item _backView];
if (![subviews containsObject: itemBackView]
|| [item _isModified]
|| [item _isFlexibleSpace])
{
itemViewEdited = itemView;
// 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
// item back view
[item _layout];
}
// Example : when a label is changed, _isModified returns YES
// to let us known we must recalculate the text lenght and
// then the item view size with the subsequent arrangement
if (itemViewEdited != nil)
[item _layout];
itemBackViewFrame = [itemBackView frame];
[itemBackView setFrame: NSMakeRect(x,
itemBackViewFrame.origin.y,
itemBackViewFrame.size.width,
itemBackViewFrame.size.height)];
x += [itemBackView frame].size.width;
}
}
@ -338,79 +345,106 @@ static const int current_version = 1;
{
NSArray *items = [_toolbar items];
// the backViews which are associated with the toolbar items now (the toolbar items doesn't
// reflect the toolbar view content)
// The back views which are associated with each toolbar item (the toolbar
// items doesn't reflect the toolbar view content)
NSArray *itemBackViews = [items valueForKey: @"_backView"];
// the backViews which will be visible in the toolbar view (when _handleViewsVisibility will be terminated)
NSArray *visibleItemBackViews = [self _visibleBackViews];
// the backViews which are visible in the toolbar view now
NSArray *currentItemBackViews = [_clipView subviews];
// The back views which are visible in the toolbar view now (before taking the
// last user action in account)
NSArray *visibleItemBackViews = [_clipView subviews];
// more efficient than [self _visibleBackViews]
// The back views which will be visible in the toolbar view (when
// _handleViewsVisibility will be terminated)
NSArray *willBeVisibleItemBackViews;
NSEnumerator *e;
NSView *itemBackView;
NSRect clipViewFrame;
[self _handleViewsOrigin];
// First, we resize
[self _handleViewsFrame];
[self _takeInAccountFlexibleSpaces];
// We remove the _backView (itemBackView variable) associated with the removed or not visible
// toolbar items
// Then we retrieve the back views which should be visible now that the resize
// process has been taken in account
willBeVisibleItemBackViews = [self _visibleBackViews];
// We remove the back view associated with the removed or not visible toolbar
// items
e = [currentItemBackViews objectEnumerator];
e = [visibleItemBackViews objectEnumerator];
while ((itemBackView = [e nextObject]) != nil)
{
if (![itemBackViews containsObject: itemBackView] || ![visibleItemBackViews containsObject: itemBackView])
if (![itemBackViews containsObject: itemBackView]
|| ![willBeVisibleItemBackViews containsObject: itemBackView])
{
if ([itemBackView superview] != nil)
[itemBackView removeFromSuperview];
}
}
// We add the _backView (itemBackView variable) associated with the added toolbar item when it
// is visible
// We add the backView associated with the added toolbar item when it should
// become visible
e = [visibleItemBackViews objectEnumerator];
e = [willBeVisibleItemBackViews objectEnumerator];
while ((itemBackView = [e nextObject]) != nil)
{
if (![currentItemBackViews containsObject: itemBackView])
if (![visibleItemBackViews containsObject: itemBackView])
{
[_clipView addSubview: itemBackView];
}
}
// We manage the clipped items view in the case it should become visible or
// invisible
clipViewFrame = [_clipView frame];
if (([_clippedItemsMark superview] == nil) && ([visibleItemBackViews count] < [itemBackViews count]))
if (([_clippedItemsMark superview] == nil)
&& ([willBeVisibleItemBackViews count] < [itemBackViews count]))
{
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x, clipViewFrame.origin.y,
clipViewFrame.size.width - _ClippedItemsViewWidth,
clipViewFrame.size.height)];
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x,
clipViewFrame.origin.y,
clipViewFrame.size.width - _ClippedItemsViewWidth,
clipViewFrame.size.height)];
clipViewFrame = [_clipView frame]; // we get the new _clipView frame
[_clippedItemsMark setFrameOrigin: NSMakePoint(clipViewFrame.size.width,
clipViewFrame.origin.y)];
[_clippedItemsMark setFrameOrigin: NSMakePoint(clipViewFrame.size.width,
clipViewFrame.origin.y)];
[self addSubview: _clippedItemsMark];
}
else if (([_clippedItemsMark superview] != nil) && ([visibleItemBackViews count] >=
[itemBackViews count]))
else if (([_clippedItemsMark superview] != nil)
&& ([willBeVisibleItemBackViews count] >= [itemBackViews count]))
{
[_clippedItemsMark removeFromSuperview];
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x, clipViewFrame.origin.y,
clipViewFrame.size.width + _ClippedItemsViewWidth, clipViewFrame.size.height)];
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x,
clipViewFrame.origin.y,
clipViewFrame.size.width + _ClippedItemsViewWidth,
clipViewFrame.size.height)];
}
[self setNeedsDisplay: YES];
}
- (void) _reload {
[self _handleViewsSize];
- (void) _reload
{
[self _handleViewsVisibility];
}
- (void) _setToolbar: (GSToolbar *)toolbar
{
[_toolbar _setToolbarView: nil];
ASSIGN(_toolbar, toolbar);
[_clippedItemsMark setToolbar: _toolbar];
[_toolbar _setToolbarView: self];
[self _reload]; // Load the toolbar in the toolbar view
}
- (void) _takeInAccountFlexibleSpaces
@ -418,13 +452,13 @@ static const int current_version = 1;
NSArray *items = [_toolbar items];
NSEnumerator *e = [items objectEnumerator];
NSToolbarItem *item;
NSView *itemView;
NSRect lastItemViewFrame = [[[items lastObject] _backView] frame];
float lengthAvailable = [self frame].size.width - (lastItemViewFrame.origin.x +
lastItemViewFrame.size.width);
int flexibleSpaceItemsNumber = 0;
NSView *itemBackView;
NSRect lastItemBackViewFrame = [[[items lastObject] _backView] frame];
float lengthAvailable = [self frame].size.width -
NSMaxX(lastItemBackViewFrame);
unsigned int flexibleSpaceItemsNumber = 0;
BOOL mustAdjustNext = NO;
float x = 0.;
float x = 0;
if (lengthAvailable < 1)
return;
@ -443,24 +477,24 @@ static const int current_version = 1;
e = [items objectEnumerator];
while ((item = [e nextObject]) != nil)
{
itemView = [item _backView];
itemBackView = [item _backView];
if ([item _isFlexibleSpace])
{
NSRect itemViewFrame = [itemView frame];
NSRect itemBackViewFrame = [itemBackView frame];
[itemView setFrame:
NSMakeRect(x, itemViewFrame.origin.y, lengthAvailable / flexibleSpaceItemsNumber,
itemViewFrame.size.height)];
[itemBackView setFrame: NSMakeRect(x, itemBackViewFrame.origin.y,
lengthAvailable / flexibleSpaceItemsNumber,
itemBackViewFrame.size.height)];
mustAdjustNext = YES;
}
else if (mustAdjustNext)
{
NSRect itemViewFrame = [itemView frame];
NSRect itemBackViewFrame = [itemBackView frame];
[itemView setFrame: NSMakeRect(x, itemViewFrame.origin.y, itemViewFrame.size.width,
itemViewFrame.size.height)];
[itemBackView setFrame: NSMakeRect(x, itemBackViewFrame.origin.y,
itemBackViewFrame.size.width, itemBackViewFrame.size.height)];
}
x += [itemView frame].size.width;
x += [itemBackView frame].size.width;
}
}
@ -470,11 +504,13 @@ static const int current_version = 1;
- (NSArray *) _visibleBackViews
{
NSArray *items = [_toolbar items];
NSView *itemView;
NSView *itemBackView;
int i, n = [items count];
float totalWidth = 0, toolbarWidth;
float itemBackViewsWidth = 0, toolbarWidth;
if (![_toolbar isVisible] && ![self _willBeVisible])
// _willBeVisible indicates that the toolbar view previously hidden is in
// process to become visible again before the end of current the event loop.
if ([self superview] == nil && ![self _willBeVisible])
return nil;
[_visibleBackViews release];
@ -484,14 +520,14 @@ static const int current_version = 1;
for (i = 0; i < n; i++)
{
itemView = [[items objectAtIndex:i] _backView];
itemBackView = [[items objectAtIndex:i] _backView];
totalWidth += [itemView frame].size.width;
itemBackViewsWidth += [itemBackView frame].size.width;
if ((totalWidth + _ClippedItemsViewWidth <= toolbarWidth) || (i == n - 1 && totalWidth <=
toolbarWidth))
if ((itemBackViewsWidth + _ClippedItemsViewWidth <= toolbarWidth)
|| (i == n - 1 && itemBackViewsWidth <= toolbarWidth))
{
[_visibleBackViews addObject: itemView];
[_visibleBackViews addObject: itemBackView];
}
}