Added display mode and size mode support to the toolbar implementation

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@19085 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Quentin Mathe 2004-04-13 13:12:53 +00:00
parent 49bc2ab40b
commit 972d2de54c
9 changed files with 535 additions and 207 deletions

View file

@ -1,3 +1,16 @@
2004-04-13 Quentin Mathe <qmathe@club-internet.fr>
* Source/NSToolbarItem.m:
* Source/NSToolbar.m:
* Headers/AppKit/NSToolbar.h:
* Source/GSToolbar.m:
* Headers/GNUstepGUI/GSToolbar.h:
* Source/GSToolbarView.m:
* Headers/GNUstepGUI/GSToolbarView.h:
* Source/NSWindow+Toolbar.m:
* Headers/AppKit/NSWindow+Toolbar.h:
Added support for display mode and size mode and other minor changes.
2004-04-09 Kazunobu Kuriyama <kazunobu.kuriyama@nifty.com>
* Documentation/GuiUser/LanguageSetup.gsdoc: Added Korean stuff.

View file

@ -91,6 +91,7 @@ APPKIT_EXPORT NSString *NSToolbarWillAddItemNotification;
- (void) insertItemWithItemIdentifier: (NSString*)itemIdentifier atIndex: (int)index;
- (void) removeItemAtIndex: (int)index;
- (void) runCustomizationPalette: (id)sender;
- (void) validateVisibleItems;
// Accessors
- (BOOL) allowsUserCustomization;
@ -98,6 +99,7 @@ APPKIT_EXPORT NSString *NSToolbarWillAddItemNotification;
- (NSDictionary*) configurationDictionary;
- (BOOL) customizationPaletteIsRunning;
- (id) delegate;
- (NSToolbarDisplayMode) displayMode;
- (NSString*) identifier;
- (NSArray*) items;
- (NSString *) selectedItemIdentifier;
@ -107,7 +109,7 @@ APPKIT_EXPORT NSString *NSToolbarWillAddItemNotification;
- (void) setConfigurationFromDictionary: (NSDictionary*)configDict;
- (void) setDelegate: (id)delegate;
- (void) setSelectedItemIdentifier: (NSString *) identifier;
- (void) validateVisibleItems;
- (NSToolbarSizeMode) sizeMode;
@end /* interface of NSToolbar */

View file

@ -35,8 +35,10 @@
#include <Foundation/NSGeometry.h>
#include <AppKit/NSView.h>
#include "GNUstepGUI/GSToolbar.h"
// Necessary for NSToolbarDisplayMode and NSToolbarSizeMode
@class NSMutableArray;
@class GSToolbar;
@class NSToolbarItem;
@class NSClipView;
@class GSToolbarClippedItemsButton;
@ -49,13 +51,19 @@ enum {
GSToolbarViewBottomBorder = 16
};
typedef enum _ItemBackViewHeight {
typedef enum {
_ToolbarViewDefaultHeight = 61,
_ToolbarViewRegularHeight = 61,
_ToolbarViewSmallHeight = 51
} _ToolbarViewHeight;
typedef enum {
_ItemBackViewDefaultHeight = 60,
_ItemBackViewRegularHeight = 60,
_ItemBackViewSmallHeight = 50
} _ItemBackViewHeight;
typedef enum _ItemViewWidth {
typedef enum {
_ItemBackViewDefaultWidth = 60,
_ItemBackViewRegularWidth = 60,
_ItemBackViewSmallWidth = 50
@ -78,9 +86,15 @@ static const int _ClippedItemsViewWidth = 28;
NSMutableArray *_visibleBackViews;
BOOL _willBeVisible;
unsigned int _borderMask;
NSToolbarDisplayMode _displayMode;
NSToolbarSizeMode _sizeMode;
unsigned int _heightFromLayout;
}
- (id) initWithFrame: (NSRect)frame;
- (id) initWithFrame: (NSRect)frame;
- (id) initWithFrame: (NSRect)frame
displayMode: (NSToolbarDisplayMode)displayMode
sizeMode: (NSToolbarSizeMode)sizeMode;
// Accessors
- (GSToolbar *) toolbar;

View file

@ -39,12 +39,11 @@
}
// Accessors
- (NSToolbarDisplayMode) displayMode;
- (BOOL) isVisible;
- (void) setDisplayMode: (NSToolbarDisplayMode)displayMode;
- (void) setSizeMode: (NSToolbarSizeMode)sizeMode;
- (void) setVisible: (BOOL)shown;
- (NSToolbarSizeMode) sizeMode;
@end /* interface of NSToolbar */

View file

@ -176,8 +176,6 @@ static NSMutableArray *toolbars;
return nil;
ASSIGN(_identifier, identifier);
_displayMode = displayMode;
_sizeMode = sizeMode;
_items = [[NSMutableArray alloc] init];
@ -189,7 +187,13 @@ static NSMutableArray *toolbars;
_allowsUserCustomization = [toolbarModel allowsUserCustomization];
_autosavesConfiguration = [toolbarModel autosavesConfiguration];
ASSIGN(_configurationDictionary, [toolbarModel configurationDictionary]);
if ([toolbarModel displayMode] != displayMode
&& [toolbarModel sizeMode] != sizeMode)
{
// raise an exception.
}
//[self _loadConfig];
[self _setDelegate: [toolbarModel delegate] broadcast: NO];
@ -205,6 +209,9 @@ static NSMutableArray *toolbars;
_delegate = nil;
}
_displayMode = displayMode;
_sizeMode = sizeMode;
[toolbars addObject: self];
@ -287,6 +294,11 @@ static NSMutableArray *toolbars;
return _delegate;
}
- (NSToolbarDisplayMode) displayMode
{
return _displayMode;
}
- (NSString *) identifier
{
return _identifier;
@ -304,7 +316,14 @@ static NSMutableArray *toolbars;
- (NSArray *) visibleItems
{
return [[_toolbarView _visibleBackViews] valueForKey: @"toolbarItem"];
if ([_toolbarView superview] == nil)
{
return nil;
}
else
{
return [[_toolbarView _visibleBackViews] valueForKey: @"toolbarItem"];
}
}
- (void) setAllowsUserCustomization: (BOOL)flag
@ -341,7 +360,7 @@ static NSMutableArray *toolbars;
- (NSToolbarSizeMode) sizeMode
{
return 0;
return _sizeMode;
}
// Private methods

View file

@ -38,7 +38,6 @@
#include "AppKit/NSMenu.h"
#include "AppKit/NSEvent.h"
#include "AppKit/NSWindow.h"
#include "GNUstepGUI/GSToolbar.h"
#include "GNUstepGUI/GSToolbarView.h"
// internal
@ -48,6 +47,7 @@ static const int current_version = 1;
@interface GSToolbar (GNUstepPrivate)
- (void) _build;
- (void) _setToolbarView: (GSToolbarView *)toolbarView;
- (GSToolbarView *) _toolbarView;
@end
@interface NSToolbarItem (GNUstepPrivate)
@ -58,13 +58,14 @@ static const int current_version = 1;
@end
@interface GSToolbarView (GNUstepPrivate)
- (void) _handleViewsFrame;
- (void) _handleBackViewsFrame;
- (void) _handleViewsVisibility;
- (void) _reload;
- (void) _setToolbar: (GSToolbar *)toolbar;
- (void) _takeInAccountFlexibleSpaces;
// Accessors
- (float) _heightFromLayout;
- (NSArray *) _visibleBackViews;
- (void) _setWillBeVisible: (BOOL)willBeVisible;
- (BOOL) _willBeVisible;
@ -83,11 +84,12 @@ static const int current_version = 1;
// this method cannot be called "menu" otherwise it would override NSResponder
// method with the same name
- (void)setToolbar: (GSToolbar *)toolbar;
- (void) layout;
- (void) setToolbar: (GSToolbar *)toolbar;
@end
@implementation GSToolbarClippedItemsButton
- (id)init
- (id) init
{
NSImage *image = [NSImage imageNamed: @"common_ToolbarClippedItemsMark"];
@ -97,17 +99,22 @@ static const int current_version = 1;
[self setBordered: NO];
[[self cell] setHighlightsBy: NSChangeGrayCellMask
| NSChangeBackgroundCellMask];
[self setAutoresizingMask: NSViewNotSizable | NSViewMinXMargin];
[self setAutoresizingMask: NSViewNotSizable];
[self setImagePosition: NSImageOnly];
[image setScalesWhenResized: YES];
[image setSize: NSMakeSize(20, 20)];
//[image setSize: NSMakeSize(20, 20)];
[self setImage: image];
return self;
}
return nil;
}
- (void)mouseDown: (NSEvent *)event {
- (void) layout {
[self setFrameSize: NSMakeSize([self frame].size.width,
[[_toolbar _toolbarView] _heightFromLayout])];
}
- (void) mouseDown: (NSEvent *)event {
NSMenu *clippedItemsMenu = [self menuForEvent:event];
[super highlight: YES];
@ -121,7 +128,7 @@ static const int current_version = 1;
[super highlight: NO];
}
- (NSMenu *)menuForEvent: (NSEvent *)event {
- (NSMenu *) menuForEvent: (NSEvent *)event {
if ([event type] == NSLeftMouseDown)
{
return [self returnMenu];
@ -129,7 +136,7 @@ static const int current_version = 1;
return nil;
}
- (NSMenu *)returnMenu
- (NSMenu *) returnMenu
{
// this method cannot be called menu otherwise it would
// override NSResponder method with the same name
@ -160,7 +167,7 @@ static const int current_version = 1;
// Accessors
- (void)setToolbar: (GSToolbar *)toolbar
- (void) setToolbar: (GSToolbar *)toolbar
{
ASSIGN(_toolbar, toolbar);
}
@ -170,11 +177,49 @@ static const int current_version = 1;
@implementation GSToolbarView
- (id) initWithFrame: (NSRect)frame
{
return [self initWithFrame: frame
displayMode: NSToolbarDisplayModeDefault
sizeMode: NSToolbarSizeModeDefault];
}
- (id) initWithFrame: (NSRect)frame
displayMode: (NSToolbarDisplayMode)displayMode
sizeMode: (NSToolbarSizeMode)sizeMode
{
if((self = [super initWithFrame: frame]) != nil)
{
float toolbarViewHeight;
_displayMode = displayMode;
_sizeMode = sizeMode;
switch (_sizeMode)
{
case NSToolbarSizeModeDefault:
toolbarViewHeight = _ToolbarViewDefaultHeight;
break;
case NSToolbarSizeModeRegular:
toolbarViewHeight = _ToolbarViewRegularHeight;
break;
case NSToolbarSizeModeSmall:
toolbarViewHeight = _ToolbarViewSmallHeight;
break;
default:
// raise exception
toolbarViewHeight = 0;
}
[self setFrame: NSMakeRect(
frame.origin.x,
frame.origin.y,
frame.size.width,
toolbarViewHeight)];
// ---
_clipView = [[NSClipView alloc] initWithFrame:
NSMakeRect(0, 1, frame.size.width, frame.size.height)];
NSMakeRect(0, 1, frame.size.width, toolbarViewHeight - 1)];
[_clipView setAutoresizingMask: (NSViewWidthSizable |
NSViewHeightSizable)];
@ -185,9 +230,12 @@ static const int current_version = 1;
_borderMask = GSToolbarViewTopBorder | GSToolbarViewBottomBorder
| GSToolbarViewRightBorder | GSToolbarViewLeftBorder;
// ---
return self;
}
return nil;
}
@ -250,7 +298,7 @@ static const int current_version = 1;
if ([self superview] == nil)
return;
[self _handleViewsVisibility];
[self _reload];
}
- (void) viewDidMoveToSuperview
@ -259,7 +307,7 @@ static const int current_version = 1;
if (_toolbar != nil)
{
[self _handleViewsVisibility];
//[self _reload];
}
}
@ -275,7 +323,7 @@ static const int current_version = 1;
[nc addObserver: self selector: @selector(windowDidResize:)
name: NSWindowDidResizeNotification object: nil];
[self viewDidMoveToSuperview];
//[self viewDidMoveToSuperview];
}
// More methods... Accessors
@ -308,7 +356,7 @@ static const int current_version = 1;
// Private methods
- (void) _handleViewsFrame
- (void) _handleBackViewsFrame
{
NSEnumerator *e = [[_toolbar items] objectEnumerator];
NSToolbarItem *item;
@ -317,6 +365,8 @@ static const int current_version = 1;
float x = 0;
// ---
NSArray *subviews = [self subviews];
_heightFromLayout = 100;
while ((item = [e nextObject]) != nil)
{
@ -333,10 +383,13 @@ static const int current_version = 1;
itemBackViewFrame = [itemBackView frame];
[itemBackView setFrame: NSMakeRect(x,
itemBackViewFrame.origin.y,
itemBackViewFrame.size.width,
itemBackViewFrame.size.height)];
itemBackViewFrame.origin.y,
itemBackViewFrame.size.width,
itemBackViewFrame.size.height)];
x += [itemBackView frame].size.width;
if (itemBackViewFrame.size.height < _heightFromLayout)
_heightFromLayout = itemBackViewFrame.size.height;
}
}
@ -347,102 +400,119 @@ static const int current_version = 1;
// 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 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]
NSArray *backViews = [items valueForKey: @"_backView"];
// The back views which will be visible in the toolbar view (when
// _handleViewsVisibility will be terminated)
NSArray *willBeVisibleItemBackViews;
NSArray *visibleBackViews;
NSArray *subviews;
NSEnumerator *e;
NSView *itemBackView;
NSView *backView;
NSRect clipViewFrame;
// First, we resize
[self _handleViewsFrame];
[self _handleBackViewsFrame];
[self _takeInAccountFlexibleSpaces];
// Then we retrieve the back views which should be visible now that the resize
// process has been taken in account
willBeVisibleItemBackViews = [self _visibleBackViews];
visibleBackViews = [self _visibleBackViews];
// We remove the back view associated with the removed or not visible toolbar
// items
// ---
// We remove each back view associated with a removed toolbar item
e = [visibleItemBackViews objectEnumerator];
e = [[_clipView subviews] objectEnumerator];
while ((itemBackView = [e nextObject]) != nil)
while ((backView = [e nextObject]) != nil)
{
if (![itemBackViews containsObject: itemBackView]
|| ![willBeVisibleItemBackViews containsObject: itemBackView])
if (![backViews containsObject: backView])
{
if ([itemBackView superview] != nil)
[itemBackView removeFromSuperview];
if ([backView superview] != nil)
[backView removeFromSuperview];
}
}
// We add the backView associated with the added toolbar item when it should
// become visible
// We add each backView associated with an added toolbar item
e = [willBeVisibleItemBackViews objectEnumerator];
e = [backViews objectEnumerator];
subviews = [_clipView subviews];
while ((itemBackView = [e nextObject]) != nil)
while ((backView = [e nextObject]) != nil)
{
if (![visibleItemBackViews containsObject: itemBackView])
if (![subviews containsObject: backView])
{
[_clipView addSubview: itemBackView];
[_clipView addSubview: backView];
}
}
// ---
// We manage the clipped items view in the case it should become visible or
// invisible
clipViewFrame = [_clipView frame];
if (([_clippedItemsMark superview] == nil)
&& ([willBeVisibleItemBackViews count] < [itemBackViews count]))
if ([visibleBackViews count] < [backViews count])
{
NSView *lastVisibleBackView = [visibleBackViews lastObject];
float width = 0;
// Resize the clip view
if (lastVisibleBackView != nil)
width = NSMaxX([lastVisibleBackView frame]);
[_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)];
[self addSubview: _clippedItemsMark];
clipViewFrame.origin.y,
width,
clipViewFrame.size.height)];
// Adjust the clipped items mark
// Frame handling
[_clippedItemsMark layout];
clipViewFrame = [_clipView frame]; // We get the new _clipView frame
[_clippedItemsMark setFrameOrigin: NSMakePoint(
[self frame].size.width - _ClippedItemsViewWidth, clipViewFrame.origin.y)];
// ---
if ([_clippedItemsMark superview] == nil)
[self addSubview: _clippedItemsMark];
}
else if (([_clippedItemsMark superview] != nil)
&& ([willBeVisibleItemBackViews count] >= [itemBackViews count]))
{
&& ([visibleBackViews count] >= [backViews count]))
{
[_clippedItemsMark removeFromSuperview];
[_clipView setFrame: NSMakeRect(clipViewFrame.origin.x,
clipViewFrame.origin.y,
clipViewFrame.size.width + _ClippedItemsViewWidth,
clipViewFrame.size.height)];
clipViewFrame.origin.y,
[self frame].size.width,
clipViewFrame.size.height)];
}
[self setNeedsDisplay: YES];
}
- (void) _reload
{
[self _handleViewsVisibility];
[self setNeedsDisplay: YES];
}
- (void) _setToolbar: (GSToolbar *)toolbar
{
[_toolbar _setToolbarView: nil];
if ([toolbar sizeMode] != _sizeMode)
; // FIXME : raise exception here
[toolbar _setToolbarView: self]; // We set the toolbar view on the new toolbar
[_toolbar _setToolbarView: nil]; // We unset the toolbar view from the previous toolbar
ASSIGN(_toolbar, toolbar);
[_clippedItemsMark setToolbar: _toolbar];
[_toolbar _setToolbarView: self];
[_clippedItemsMark setToolbar: _toolbar];
[self _reload]; // Load the toolbar in the toolbar view
}
@ -452,10 +522,10 @@ static const int current_version = 1;
NSArray *items = [_toolbar items];
NSEnumerator *e = [items objectEnumerator];
NSToolbarItem *item;
NSView *itemBackView;
NSRect lastItemBackViewFrame = [[[items lastObject] _backView] frame];
NSView *backView;
NSRect lastBackViewFrame = [[[items lastObject] _backView] frame];
float lengthAvailable = [self frame].size.width -
NSMaxX(lastItemBackViewFrame);
NSMaxX(lastBackViewFrame);
unsigned int flexibleSpaceItemsNumber = 0;
BOOL mustAdjustNext = NO;
float x = 0;
@ -477,57 +547,60 @@ static const int current_version = 1;
e = [items objectEnumerator];
while ((item = [e nextObject]) != nil)
{
itemBackView = [item _backView];
backView = [item _backView];
if ([item _isFlexibleSpace])
{
NSRect itemBackViewFrame = [itemBackView frame];
NSRect backViewFrame = [backView frame];
[itemBackView setFrame: NSMakeRect(x, itemBackViewFrame.origin.y,
[backView setFrame: NSMakeRect(x, backViewFrame.origin.y,
lengthAvailable / flexibleSpaceItemsNumber,
itemBackViewFrame.size.height)];
backViewFrame.size.height)];
mustAdjustNext = YES;
}
else if (mustAdjustNext)
{
NSRect itemBackViewFrame = [itemBackView frame];
NSRect backViewFrame = [backView frame];
[itemBackView setFrame: NSMakeRect(x, itemBackViewFrame.origin.y,
itemBackViewFrame.size.width, itemBackViewFrame.size.height)];
[backView setFrame: NSMakeRect(x, backViewFrame.origin.y,
backViewFrame.size.width, backViewFrame.size.height)];
}
x += [itemBackView frame].size.width;
x += [backView frame].size.width;
}
}
// Accessors private methods
- (float) _heightFromLayout
{
return _heightFromLayout;
}
- (NSArray *) _visibleBackViews
{
NSArray *items = [_toolbar items];
NSView *itemBackView;
NSView *backView;
int i, n = [items count];
float itemBackViewsWidth = 0, toolbarWidth;
float backViewsWidth = 0, toolbarWidth = [self frame].size.width;
/*
// _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];
_visibleBackViews = [[NSMutableArray alloc] init];
toolbarWidth = [self frame].size.width;;
for (i = 0; i < n; i++)
{
itemBackView = [[items objectAtIndex:i] _backView];
backView = [[items objectAtIndex:i] _backView];
itemBackViewsWidth += [itemBackView frame].size.width;
backViewsWidth += [backView frame].size.width;
if ((itemBackViewsWidth + _ClippedItemsViewWidth <= toolbarWidth)
|| (i == n - 1 && itemBackViewsWidth <= toolbarWidth))
if ((backViewsWidth + _ClippedItemsViewWidth <= toolbarWidth)
|| (i == n - 1 && backViewsWidth <= toolbarWidth))
{
[_visibleBackViews addObject: itemBackView];
[_visibleBackViews addObject: backView];
}
}

View file

@ -36,6 +36,7 @@
#include "AppKit/NSToolbarItem.h"
#include "AppKit/NSView.h"
#include "AppKit/NSWindow.h"
#include "AppKit/NSWindow+Toolbar.h"
#include "GNUstepGUI/GSToolbarView.h"
#include "GNUstepGUI/GSToolbar.h"
#include "AppKit/NSToolbar.h"
@ -77,6 +78,10 @@ static const int current_version = 1;
- (void) _setWillBeVisible: (BOOL)willBeVisible;
@end
@interface NSWindow (ToolbarPrivate)
- (void) _adjustToolbarView;
@end
// ---
@implementation NSToolbar
@ -134,16 +139,6 @@ static const int current_version = 1;
// Accessors
- (NSToolbarDisplayMode) displayMode
{
return _displayMode;
}
- (NSToolbarSizeMode) sizeMode
{
return _sizeMode;
}
- (BOOL) isVisible
{
return _visible;
@ -196,7 +191,8 @@ static const int current_version = 1;
{
_displayMode = displayMode;
// do more
[_toolbarView _reload];
[_window _adjustToolbarView];
if (broadcast)
{
@ -204,12 +200,13 @@ static const int current_version = 1;
}
}
- (void) setSizeMode: (NSToolbarSizeMode)sizeMode
broadcast: (BOOL)broadcast
- (void) _setSizeMode: (NSToolbarSizeMode)sizeMode
broadcast: (BOOL)broadcast
{
_sizeMode = sizeMode;
// do more
[_toolbarView _reload];
[_window _adjustToolbarView];
if (broadcast)
{

View file

@ -6,7 +6,8 @@
Copyright (C) 2002 Free Software Foundation, Inc.
Author: Gregory John Casamento <greg_casamento@yahoo.com>,
Fabien Vallon <fabien.vallon@fr.alcove.com>
Fabien Vallon <fabien.vallon@fr.alcove.com>,
Quentin Mathe <qmathe@club-internet.fr>
Date: May 2002
This file is part of the GNUstep GUI Library.
@ -32,13 +33,13 @@
#include <Foundation/NSDebug.h>
#include "AppKit/NSToolbarItem.h"
#include "AppKit/NSToolbar.h"
#include "AppKit/NSMenu.h"
#include "AppKit/NSMenuItem.h"
#include "AppKit/NSImage.h"
#include "AppKit/NSButton.h"
#include "AppKit/NSFont.h"
#include "AppKit/NSEvent.h"
#include "GNUstepGUI/GSToolbar.h"
#include "GNUstepGUI/GSToolbarView.h"
/*
@ -55,16 +56,16 @@
* with the item identifier.
*/
@interface NSToolbar (GNUstepPrivate)
@interface GSToolbar (GNUstepPrivate)
- (GSToolbarView *) _toolbarView;
@end
@interface NSToolbarItem (GNUstepPrivate)
- (void) _layout;
- (NSView *) _backView;
- (BOOL) _isUpdated;
- (BOOL) _isModified;
- (BOOL) _isFlexibleSpace;
- (void) _setToolbar: (NSToolbar *)toolbar;
- (void) _setToolbar: (GSToolbar *)toolbar;
@end
@interface GSToolbarView (GNUstepPrivate)
@ -97,25 +98,76 @@
- (void) layout
{
float textWidth, layoutedWidth;
float textWidth, layoutedWidth = -1, layoutedHeight = -1;
NSAttributedString *attrStr;
NSDictionary *attr;
NSFont *font =[NSFont systemFontOfSize: 11]; // [NSFont smallSystemFontSize] or better should NSControlContentFontSize
NSFont *font = [NSFont systemFontOfSize: 11]; // [NSFont smallSystemFontSize] or better should NSControlContentFontSize
// Adjust the layout in accordance with NSToolbarSizeMode
switch ([[_toolbarItem toolbar] sizeMode])
{
case NSToolbarSizeModeDefault:
layoutedWidth = _ItemBackViewDefaultWidth;
layoutedHeight = _ItemBackViewDefaultHeight;
[[_toolbarItem image] setSize: NSMakeSize(32, 32)];
break;
case NSToolbarSizeModeRegular:
layoutedWidth = _ItemBackViewRegularWidth;
layoutedHeight = _ItemBackViewRegularHeight;
[[_toolbarItem image] setSize: NSMakeSize(32, 32)];
break;
case NSToolbarSizeModeSmall:
layoutedWidth = _ItemBackViewSmallWidth;
layoutedHeight = _ItemBackViewSmallHeight;
[[_toolbarItem image] setSize: NSMakeSize(24, 24)];
// Not use [self image] here because it can return nil, when image position is
// set to NSNoImage. Even if NSToolbarDisplayModeTextOnly is not true anymore
// -setImagePosition: is only called below, then [self image] can still returns
// nil.
font = [NSFont systemFontOfSize: 9];
break;
default:
; // invalid
}
[[self cell] setFont: font];
// Adjust the layout in accordance with the label
attr = [NSDictionary dictionaryWithObject: font forKey: @"NSFontAttributeName"];
attrStr = [[NSAttributedString alloc] initWithString: [_toolbarItem label] attributes: attr];
textWidth = [attrStr size].width + 2 * _InsetItemTextX;
if (textWidth > _ItemBackViewDefaultWidth)
{
if (layoutedWidth != -1 && textWidth > layoutedWidth)
layoutedWidth = textWidth;
}
else
{
layoutedWidth = _ItemBackViewDefaultWidth;
}
// Adjust the layout in accordance with NSToolbarDisplayMode
switch ([[_toolbarItem toolbar] displayMode])
{
case NSToolbarDisplayModeDefault:
[self setImagePosition: NSImageAbove];
break;
case NSToolbarDisplayModeIconAndLabel:
[self setImagePosition: NSImageAbove];
break;
case NSToolbarDisplayModeIconOnly:
[self setImagePosition: NSImageOnly];
layoutedHeight -= [attrStr size].height + _InsetItemTextY;
layoutedWidth -= [attrStr size].height + _InsetItemTextY;
break;
case NSToolbarDisplayModeLabelOnly:
[self setImagePosition: NSNoImage];
layoutedHeight = [attrStr size].height + _InsetItemTextY * 2;
break;
default:
; // invalid
}
[self setFrameSize: NSMakeSize(layoutedWidth, _ItemBackViewDefaultHeight)];
// Set the frame size to use the new layout
[self setFrameSize: NSMakeSize(layoutedWidth, layoutedHeight)];
}
@ -132,6 +184,8 @@
{
NSToolbarItem *_toolbarItem;
BOOL _enabled;
BOOL _showLabel;
NSFont *_font;
}
- (id) initWithToolbarItem: (NSToolbarItem *)toolbarItem;
@ -158,57 +212,142 @@
{
NSAttributedString *attrString;
NSDictionary *attr;
NSFont *font = [NSFont systemFontOfSize: 11]; // [NSFont smallSystemFontSize] or better should be NSControlContentFontSize
NSColor *color;
int textX;
[super drawRect: rect]; // We draw _view which is a subview
if (_enabled)
{
[[NSColor blackColor] set];
color = [NSColor blackColor];
}
else
{
[[NSColor grayColor] set];
color = [NSColor disabledControlTextColor];
}
if (_showLabel)
{
// we draw the label
attr = [NSDictionary dictionaryWithObjectsAndKeys: _font,
@"NSFontAttributeName",
color,
@"NSForegroundColorAttributeName",
nil];
attrString = [[NSAttributedString alloc] initWithString: [_toolbarItem label] attributes: attr];
textX = (([self frame].size.width - _InsetItemTextX) - [attrString size].width) / 2;
[attrString drawAtPoint: NSMakePoint(textX, _InsetItemTextY)];
}
attr = [NSDictionary dictionaryWithObject: font forKey: @"NSFontAttributeName"];
attrString = [[NSAttributedString alloc] initWithString: [_toolbarItem label] attributes: attr]; // we draw the label
textX = (([self frame].size.width - _InsetItemTextX) - [attrString size].width) / 2;
[attrString drawAtPoint: NSMakePoint(textX, _InsetItemTextY)];
}
- (void) layout
{
NSView *view;
float textWidth;
NSView *view = [_toolbarItem view];
float insetItemViewY;
float textWidth, layoutedWidth = -1, layoutedHeight = -1;
NSAttributedString *attrStr;
NSDictionary *attr;
NSFont *font = [NSFont systemFontOfSize: 11]; // [NSFont smallSystemFontSize] or better should be NSControlContentFontSize
view = [_toolbarItem view];
if ([view frame].size.height <= _ItemBackViewDefaultHeight)
{
[view setFrameOrigin: NSMakePoint(_InsetItemViewX, _InsetItemViewY)];
[self addSubview: view];
}
else
{
[view removeFromSuperview];
}
_font = [NSFont systemFontOfSize: 11]; // [NSFont smallSystemFontSize] or better should be NSControlContentFontSize
[self setFrameSize: NSMakeSize([view frame].size.width + 2 * _InsetItemViewX, _ItemBackViewDefaultHeight)];
if ([view superview] == nil) // Show the view to eventually hide it later
[self addSubview: view];
// Adjust the layout in accordance with NSToolbarSizeMode
switch ([[_toolbarItem toolbar] sizeMode])
{
case NSToolbarSizeModeDefault:
layoutedWidth = _ItemBackViewDefaultWidth;
layoutedHeight = _ItemBackViewDefaultHeight;
if ([view frame].size.height > 32)
[view removeFromSuperview];
break;
case NSToolbarSizeModeRegular:
layoutedWidth = _ItemBackViewRegularWidth;
layoutedHeight = _ItemBackViewRegularHeight;
if ([view frame].size.height > 32)
[view removeFromSuperview];
break;
case NSToolbarSizeModeSmall:
layoutedWidth = _ItemBackViewSmallWidth;
layoutedHeight = _ItemBackViewSmallHeight;
_font = [NSFont systemFontOfSize: 9];
if ([view frame].size.height > 24)
[view removeFromSuperview];
break;
default:
NSLog(@"Invalid NSToolbarSizeMode"); // invalid
}
// Adjust the layout in accordance with the label
attr = [NSDictionary dictionaryWithObject: font forKey: @"NSFontAttributeName"];
attr = [NSDictionary dictionaryWithObject: _font forKey: @"NSFontAttributeName"];
attrStr = [[NSAttributedString alloc] initWithString: [_toolbarItem label] attributes: attr];
textWidth = [attrStr size].width + 2 * _InsetItemTextX;
if (textWidth > [self frame].size.width)
if (textWidth > layoutedWidth)
layoutedWidth = textWidth;
// Adjust the layout in accordance with NSToolbarDisplayMode
_enabled = YES;
_showLabel = YES;
// this boolean variable is used to known when it's needed to draw the label in the -drawRect:
// method.
switch ([[_toolbarItem toolbar] displayMode])
{
[self setFrameSize: NSMakeSize(textWidth, _ItemBackViewDefaultHeight)];
[view setFrameOrigin: NSMakePoint((textWidth - [view frame].size.width) / 2, _InsetItemViewY)];
}
case NSToolbarDisplayModeDefault:
break; // Nothing to do
case NSToolbarDisplayModeIconAndLabel:
break; // Nothing to do
case NSToolbarDisplayModeIconOnly:
_showLabel = NO;
layoutedHeight -= [attrStr size].height + _InsetItemTextY;
break;
case NSToolbarDisplayModeLabelOnly:
_enabled = NO;
layoutedHeight = [attrStr size].height + _InsetItemTextY * 2;
if ([view superview] != nil)
[view removeFromSuperview];
break;
default:
; // invalid
}
// If the view is visible...
// Adjust the layout in accordance with the view width in the case it is needed
if ([view superview] != nil)
{
if (layoutedWidth < [view frame].size.width + 2 * _InsetItemViewX)
layoutedWidth = [view frame].size.width + 2 * _InsetItemViewX;
}
// Set the frame size to use the new layout
[self setFrameSize: NSMakeSize(layoutedWidth, layoutedHeight)];
// If the view is visible...
// Adjust the view position in accordance with the new layout
if ([view superview] != nil)
{
if (_showLabel)
{
insetItemViewY = ([self frame].size.height
- [view frame].size.height - [attrStr size].height - _InsetItemTextX) / 2
+ [attrStr size].height + _InsetItemTextX;
}
else
{
insetItemViewY = ([self frame].size.height - [view frame].size.height) / 2;
}
[view setFrameOrigin:
NSMakePoint((layoutedWidth - [view frame].size.width) / 2, insetItemViewY)];
}
}
- (NSToolbarItem *)toolbarItem
@ -244,14 +383,14 @@
@implementation GSToolbarSeparatorItem
- (id) initWithItemIdentifier: (NSString *)itemIdentifier
{
NSImage *image = [NSImage imageNamed: @"common_ToolbarSeparatorItem"];
NSImage *image = [NSImage imageNamed: @"common_ToolbarSeperatorItem"];
self = [super initWithItemIdentifier: itemIdentifier];
[(NSButton *)[self _backView] setImagePosition: NSImageOnly];
[(NSButton *)[self _backView] setImage: image];
// We bypass the toolbar item accessor to set the image in order to have it (48 * 48) not resized
[[self _backView] setFrameSize: NSMakeSize(15, _ItemBackViewDefaultHeight)];
[[self _backView] setFrameSize: NSMakeSize(30, _ItemBackViewDefaultHeight)];
return self;
}
@ -427,7 +566,7 @@
{
if(_flags._image)
{
return [(id)_backView image];
return _image;
}
return nil;
}
@ -613,7 +752,7 @@
ASSIGN(_image, image);
[_image setScalesWhenResized: YES];
[_image setSize: NSMakeSize(32, 32)];
//[_image setSize: NSMakeSize(32, 32)];
if ([_backView isKindOfClass: [NSButton class]])
[(NSButton *)_backView setImage: _image];
@ -627,7 +766,7 @@
if ([_backView isKindOfClass: [NSButton class]])
[(NSButton *)_backView setTitle:_label];
_updated = YES;
_modified = YES;
if (_toolbar != nil)
[[_toolbar _toolbarView] _reload];
}
@ -720,7 +859,7 @@
return _toolTip;
}
- (NSToolbar *)toolbar
- (GSToolbar *)toolbar
{
return _toolbar;
}
@ -748,9 +887,9 @@
[(id)_backView layout];
}
- (BOOL)_isUpdated
- (BOOL)_isModified
{
return _updated;
return _modified;
}
- (BOOL)_isFlexibleSpace
@ -758,7 +897,7 @@
return [self isKindOfClass: [GSToolbarFlexibleSpaceItem class]];
}
- (void) _setToolbar: (NSToolbar *)toolbar
- (void) _setToolbar: (GSToolbar *)toolbar
{
ASSIGN(_toolbar, toolbar);
}

View file

@ -33,7 +33,6 @@
#include "AppKit/NSToolbar.h"
#include "GNUstepGUI/GSToolbarView.h"
static const int ToolbarHeight = 61;
@interface NSToolbar (GNUstepPrivate)
+ (NSArray *) _toolbars;
@ -44,11 +43,14 @@ static const int ToolbarHeight = 61;
@interface GSToolbarView (GNUstepPrivate)
- (void) _setToolbar: (NSToolbar *)toolbar;
- (void) _handleViewsVisibility;
- (float) _heightFromLayout;
- (void) _reload;
- (void) _setWillBeVisible: (BOOL)willBeVisible;
@end
@interface NSWindow (ToolbarPrivate)
- (void) _toggleToolbarView: (GSToolbarView *)toolbarView display: (BOOL)flag;
- (void) _adjustToolbarView;
- (void) _toggleToolbarViewWithDisplay: (BOOL)flag;
@end
@implementation NSWindow (Toolbar)
@ -61,17 +63,26 @@ static const int ToolbarHeight = 61;
- (void) toggleToolbarShown: (id)sender
{
NSToolbar *toolbar = [self toolbar];
BOOL isVisible = [toolbar isVisible];
GSToolbarView *toolbarView = [toolbar _toolbarView];
if ([sender isEqual: toolbar])
// we can enter this branch when the toolbar class has called
// We can enter this branch when the toolbar class has called
// toggleToolbarShown:
{
[self _toggleToolbarView: [toolbar _toolbarView] display: YES];
if (!isVisible) // In the case : not visible when the method has been called
{
[toolbarView setFrameSize: NSMakeSize([self frame].size.width, 100)];
// -toogleToolbarViewWithDisplay: will reset the toolbarView frame
[toolbarView _reload]; // Will recalculate the layout
}
[self _toggleToolbarViewWithDisplay: YES];
}
else
{
// we call the toolbar class letting it call back on toggleToolbarShown:
[toolbar setVisible: ![toolbar isVisible]];
// We call the toolbar class letting it call back on toggleToolbarShown:
[toolbar setVisible: !isVisible];
}
}
@ -85,7 +96,7 @@ static const int ToolbarHeight = 61;
if (toolbar != nil && [toolbar isVisible])
{
NSArray *subviews = [_contentView subviews];
id subview;
NSView *subview;
int i, n = [subviews count];
GSToolbarView *toolbarView = [toolbar _toolbarView];
@ -146,60 +157,114 @@ static const int ToolbarHeight = 61;
- (void) setToolbar: (NSToolbar*)toolbar
{
NSToolbar *lastToolbar = [self toolbar];
GSToolbarView *toolbarView = nil;
// ---
GSToolbarView *toolbarView = nil;
if (lastToolbar != nil)
{
// we throw the last toolbar out
// We throw the last toolbar out
[self _toggleToolbarView : [lastToolbar _toolbarView] display: NO];
[self _toggleToolbarViewWithDisplay: NO];
[lastToolbar _setWindow: nil];
}
// when there is no new toolbar
// When there is no new toolbar
if (toolbar == nil)
{
[self display]; // to show we have toggle the previous toolbar view
[self display]; // To show we have toggle the previous toolbar view
return;
}
// -----
// ELSE
// the window want to know which toolbar is binded
// -----
// The window want to know which toolbar is binded
[toolbar _setWindow : self];
// insert the toolbar view (we create this view when the toolbar hasn't such
// Instantiate or retrieve the toolbar view (we create this view when the toolbar hasn't such
// view)...
toolbarView = [toolbar _toolbarView];
if (toolbarView == nil)
{
toolbarView = [[GSToolbarView alloc] initWithFrame: NSMakeRect(0, 0, 0, 0)];
toolbarView = [[GSToolbarView alloc] initWithFrame: NSMakeRect(0, 0,
[self frame].size.width, 100)];
// _toggleToolbarView:display: method will set the toolbar view to the right
// frame
[toolbarView setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
}
[toolbarView setBorderMask: GSToolbarViewBottomBorder];
[self _toggleToolbarView: toolbarView display: YES];
// load the toolbar inside the toolbar view
// Load the toolbar inside the toolbar view
[toolbarView _setToolbar: toolbar];
[toolbarView _setWillBeVisible: YES];
[toolbarView _setToolbar: toolbar]; // Will set the _toolbarView variable for the toolbar
[toolbarView _setWillBeVisible: NO];
// Make the toolbar view visible
// We do that in this way...
// because _toggleToolbarViewWithDisplay: will call -toolbarView method for the toolbar
[self _toggleToolbarViewWithDisplay: YES];
}
// Private methods
- (void) _toggleToolbarView: (GSToolbarView *)toolbarView display: (BOOL)flag {
NSRect windowFrame;
- (void) _adjustToolbarView
{
NSToolbar *toolbar = [self toolbar];
// Views
GSToolbarView *toolbarView = [toolbar _toolbarView];
NSView *contentViewWithoutToolbar = [self contentViewWithoutToolbar];
// Frame and height
NSRect windowFrame = [self frame];
NSRect toolbarViewFrame = [toolbarView frame];
float toolbarViewHeight = toolbarViewFrame.size.height;
float newToolbarViewHeight = [toolbarView _heightFromLayout];
[toolbarView setFrame: NSMakeRect(
toolbarViewFrame.origin.x,
toolbarViewFrame.origin.y + (toolbarViewHeight - newToolbarViewHeight),
toolbarViewFrame.size.width,
newToolbarViewHeight)];
if ([toolbar isVisible])
{
// 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) _toggleToolbarViewWithDisplay: (BOOL)flag {
// Views
GSToolbarView *toolbarView = [[self toolbar] _toolbarView];
NSView *contentViewWithoutToolbar;
// Frame
NSRect windowFrame = [self frame];
if ([toolbarView superview] == nil)
{
NSView *contentViewWithoutToolbar;
float newToolbarViewHeight = [toolbarView _heightFromLayout];
NSRect contentViewWithoutToolbarFrame;
contentViewWithoutToolbar = _contentView;
// Switch the content view
@ -210,20 +275,23 @@ static const int ToolbarHeight = 61;
// Resize the window
windowFrame = [self frame];
[self setFrame: NSMakeRect(windowFrame.origin.x,
windowFrame.origin.y - ToolbarHeight, windowFrame.size.width,
windowFrame.size.height + ToolbarHeight) display: flag];
[self setFrame: NSMakeRect(
windowFrame.origin.x,
windowFrame.origin.y - newToolbarViewHeight,
windowFrame.size.width,
windowFrame.size.height + newToolbarViewHeight) display: NO];
// Plug the toolbar view
contentViewWithoutToolbarFrame = [contentViewWithoutToolbar frame];
[toolbarView setFrame: NSMakeRect(0,contentViewWithoutToolbarFrame.size.height,
contentViewWithoutToolbarFrame.size.width, ToolbarHeight)];
[toolbarView setFrame: NSMakeRect(
0,
contentViewWithoutToolbarFrame.size.height,
contentViewWithoutToolbarFrame.size.width,
newToolbarViewHeight)];
[_contentView addSubview: toolbarView];
[toolbarView _handleViewsVisibility];
[toolbarView setNextResponder: self];
// Insert the previous content view
@ -231,9 +299,9 @@ static const int ToolbarHeight = 61;
RELEASE(contentViewWithoutToolbar);
}
else
{
NSView *contentViewWithoutToolbar;
{
float toolbarViewHeight = [toolbarView frame].size.height;
contentViewWithoutToolbar = [self contentViewWithoutToolbar];
// Unplug the toolbar view
@ -244,10 +312,11 @@ static const int ToolbarHeight = 61;
[contentViewWithoutToolbar setAutoresizingMask: NSViewMaxYMargin];
windowFrame = [self frame];
[self setFrame: NSMakeRect(windowFrame.origin.x,
windowFrame.origin.y + ToolbarHeight, windowFrame.size.width,
windowFrame.size.height - ToolbarHeight) display: flag];
[self setFrame: NSMakeRect(
windowFrame.origin.x,
windowFrame.origin.y + toolbarViewHeight,
windowFrame.size.width,
windowFrame.size.height - toolbarViewHeight) display: NO];
[contentViewWithoutToolbar setAutoresizingMask: NSViewWidthSizable
| NSViewHeightSizable];
@ -256,13 +325,16 @@ static const int ToolbarHeight = 61;
// Switch the content view
RETAIN(contentViewWithoutToolbar);
// because setContentView: will release the parent view and their subviews
// because setContentView: will release the parent view (aka _contentView) and
// their subviews and actually contentViewWithoutToolbar is a subview of _contentView
[self setContentView: contentViewWithoutToolbar];
RELEASE(contentViewWithoutToolbar);
}
if (flag)
[self display];
}
@end