Fixed the toolbar layout problem introduced by NSButtonCell changes

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@19941 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
qmathe 2004-08-30 21:33:17 +00:00
parent 0238dd7e11
commit 9c44969b67
5 changed files with 244 additions and 80 deletions

View file

@ -55,6 +55,12 @@
#include <math.h>
@interface NSButtonCell (Private)
// Overriden private internal method
- (void) _drawImage: (NSImage *)anImage inFrame: (NSRect)aRect
isFlipped: (BOOL)flipped;
@end
@implementation NSButtonCell
/*
@ -942,12 +948,12 @@
* The drawing code below will then center the image in imageRect.
*/
titleRect.origin.x = cellFrame.origin.x;
titleRect.origin.y = cellFrame.origin.y;
titleRect.origin.y = cellFrame.origin.y + GSCellTextImageYDist;
titleRect.size.width = cellFrame.size.width;
titleRect.size.height = titleSize.height;
imageRect.origin.x = cellFrame.origin.x;
imageRect.origin.y = NSMaxY(titleRect) + GSCellTextImageYDist;
imageRect.origin.y = NSMaxY(titleRect);
imageRect.size.width = cellFrame.size.width;
imageRect.size.height = NSMaxY(cellFrame) - imageRect.origin.y;
@ -1009,30 +1015,7 @@
// Draw image
if (imageToDisplay != nil)
{
NSSize size;
NSPoint position;
size = [imageToDisplay size];
position.x = MAX(NSMidX(imageRect) - (size.width / 2.), 0.);
position.y = MAX(NSMidY(imageRect) - (size.height / 2.), 0.);
/*
* Images are always drawn with their bottom-left corner at the origin
* so we must adjust the position to take account of a flipped view.
*/
if (flippedView)
{
position.y += size.height;
}
if (_cell.is_disabled && _image_dims_when_disabled)
{
[imageToDisplay dissolveToPoint: position fraction: 0.5];
}
else
{
[imageToDisplay compositeToPoint: position
operation: NSCompositeSourceOver];
}
[self _drawImage: imageToDisplay inFrame: imageRect isFlipped: flippedView];
}
// Draw title
@ -1327,3 +1310,40 @@
}
@end
@implementation NSButtonCell (Private)
// Overriden private internal method
- (void) _drawImage: (NSImage *)anImage inFrame: (NSRect)aRect
isFlipped: (BOOL)flipped
{
// To keep partially in sync with the NSCell overriden method
NSSize size;
NSPoint position;
size = [anImage size];
position.x = MAX(NSMidX(aRect) - (size.width / 2.), 0.);
position.y = MAX(NSMidY(aRect) - (size.height / 2.), 0.);
/*
* Images are always drawn with their bottom-left corner at the origin
* so we must adjust the position to take account of a flipped view.
*/
if (flipped)
{
position.y += size.height;
}
if (_cell.is_disabled && _image_dims_when_disabled)
{
[anImage dissolveToPoint: position fraction: 0.5];
}
else
{
[anImage compositeToPoint: position
operation: NSCompositeSourceOver];
}
}
@end

View file

@ -1664,34 +1664,21 @@ static NSColor *shadowCol;
switch (_cell.type)
{
case NSTextCellType:
{
[self _drawAttributedText: [self attributedStringValue]
inFrame: cellFrame];
}
[self _drawAttributedText: [self attributedStringValue]
inFrame: cellFrame];
break;
case NSImageCellType:
if (_cell_image)
{
NSSize size;
NSPoint position;
size = [_cell_image size];
position.x = MAX(NSMidX(cellFrame) - (size.width/2.),0.);
position.y = MAX(NSMidY(cellFrame) - (size.height/2.),0.);
/*
* Images are always drawn with their bottom-left corner
* at the origin so we must adjust the position to take
* account of a flipped view.
*/
if ([controlView isFlipped])
position.y += size.height;
[_cell_image compositeToPoint: position operation: NSCompositeSourceOver];
}
break;
[self _drawImage: _cell_image
inFrame: cellFrame
isFlipped: [controlView isFlipped]];
}
break;
case NSNullCellType:
break;
break;
}
if (_cell.shows_first_responder)
@ -2206,6 +2193,27 @@ static NSColor *shadowCol;
RELEASE (attributes);
}
- (void) _drawImage: (NSImage *)anImage inFrame: (NSRect)aRect
isFlipped: (BOOL)flipped
{
NSSize size;
NSPoint position;
size = [anImage size];
position.x = MAX(NSMidX(aRect) - (size.width/2.),0.);
position.y = MAX(NSMidY(aRect) - (size.height/2.),0.);
/*
* Images are always drawn with their bottom-left corner
* at the origin so we must adjust the position to take
* account of a flipped view.
*/
if (flipped)
position.y += size.height;
[anImage compositeToPoint: position operation: NSCompositeSourceOver];
}
@end
/*

View file

@ -38,8 +38,10 @@
#include "AppKit/NSMenuItem.h"
#include "AppKit/NSImage.h"
#include "AppKit/NSButton.h"
#include "AppKit/NSButtonCell.h"
#include "AppKit/NSFont.h"
#include "AppKit/NSEvent.h"
#include "AppKit/NSParagraphStyle.h"
#include "GNUstepGUI/GSToolbar.h"
#include "GNUstepGUI/GSToolbarView.h"
@ -122,16 +124,35 @@ static NSFont *SmallFont = nil;
- (void) setToolbarItemAction: (SEL)action;
@end
@interface GSToolbarButtonCell : NSButtonCell
{
NSRect titleRect;
NSRect imageRect;
}
@end
// ---
@implementation GSToolbarButton
+ (void) initialize
{
if (self == [GSToolbarButton class])
[GSToolbarButton setCellClass: [GSToolbarButtonCell class]];
}
- (id) initWithToolbarItem: (NSToolbarItem *)toolbarItem
{
self = [super initWithFrame: NSMakeRect(ItemBackViewX, ItemBackViewY, ItemBackViewDefaultWidth, ItemBackViewDefaultHeight)];
self = [super initWithFrame: NSMakeRect(ItemBackViewX, ItemBackViewY,
ItemBackViewDefaultWidth, ItemBackViewDefaultHeight)];
// Frame will be reset by the layout method
if (self != nil)
{
// Don't do an ASSIGN here, the toolbar item itself retains us.
_toolbarItem = toolbarItem;
//[self setCell: [[GSToolbarButtonCell alloc] init]];
}
return self;
}
@ -280,6 +301,84 @@ static NSFont *SmallFont = nil;
@end
@implementation GSToolbarButtonCell
// Overriden NSButtonCell method
- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
NSSize titleSize = [[self attributedTitle] size];
// We ignore alternateAttributedTitle, it is not needed
// We store the values we need to customize the drawing into titleRect and imageRect
titleRect.origin.x = cellFrame.origin.x;
titleRect.origin.y = cellFrame.origin.y + InsetItemTextY;
titleRect.size.width = cellFrame.size.width;
titleRect.size.height = titleSize.height;
imageRect.origin.x = cellFrame.origin.x;
imageRect.origin.y = cellFrame.origin.y;
if ([self imagePosition] != NSImageOnly)
imageRect.origin.y += titleRect.size.height;
imageRect.size.width = cellFrame.size.width;
imageRect.size.height = cellFrame.size.height;
if ([self imagePosition] != NSImageOnly)
imageRect.size.height -= titleRect.size.height;
[super drawInteriorWithFrame: cellFrame inView: controlView];
}
// Overriden NSCell method
- (void) _drawAttributedText: (NSAttributedString*)aString
inFrame: (NSRect)aRect
{
if (aString == nil)
return;
/** Important: text should always be vertically centered without
* considering descender [as if descender did not exist].
* This is particularly important for single line texts.
* Please make sure the output remains always correct.
*/
// We ignore aRect value
[aString drawInRect: titleRect];
}
// Overriden NSButtonCell method
- (void) _drawImage: (NSImage *)anImage inFrame: (NSRect)aRect isFlipped: (BOOL)flipped
{
NSSize size;
NSPoint position;
// We ignore aRect value
size = [anImage size];
position.x = MAX(NSMidX(imageRect) - (size.width / 2.), 0.);
position.y = MAX(NSMidY(imageRect) - (size.height / 2.), 0.);
/*
* Images are always drawn with their bottom-left corner at the origin
* so we must adjust the position to take account of a flipped view.
*/
if (flipped)
{
position.y += size.height;
}
if (_cell.is_disabled && _image_dims_when_disabled)
{
[anImage dissolveToPoint: position fraction: 0.5];
}
else
{
[anImage compositeToPoint: position
operation: NSCompositeSourceOver];
}
}
@end
/*
* Back view used to enclose toolbar item's custom view
*/
@ -322,37 +421,48 @@ static NSFont *SmallFont = nil;
[super dealloc];
}
- (void)drawRect: (NSRect)rect
{
NSAttributedString *attrString;
NSDictionary *attr;
NSColor *color;
float textX;
- (void) drawRect: (NSRect)rect
{
[super drawRect: rect]; // We draw _view which is a subview
if (_enabled)
{
color = [NSColor blackColor];
}
else
{
color = [NSColor disabledControlTextColor];
}
if (_showLabel)
{
NSAttributedString *attrString;
NSDictionary *attr;
NSColor *color;
NSMutableParagraphStyle *pStyle = [NSMutableParagraphStyle defaultParagraphStyle];
NSRect titleRect;
NSRect viewBounds = [self bounds];
if (_enabled)
{
color = [NSColor blackColor];
}
else
{
color = [NSColor disabledControlTextColor];
}
[pStyle setAlignment: NSCenterTextAlignment];
// We draw the label
attr = [NSDictionary dictionaryWithObjectsAndKeys: _font,
NSFontAttributeName,
color,
NSForegroundColorAttributeName,
pStyle,
NSParagraphStyleAttributeName,
nil];
attrString = [[NSAttributedString alloc] initWithString: [_toolbarItem label] attributes: attr];
textX = (([self frame].size.width - InsetItemTextX) - [attrString size].width) / 2;
[attrString drawAtPoint: NSMakePoint(textX, InsetItemTextY)];
titleRect.origin.x = viewBounds.origin.x;
titleRect.origin.y = viewBounds.origin.y + InsetItemTextY;
titleRect.size.width = viewBounds.size.width;
titleRect.size.height = [attrString size].height;
[attrString drawInRect: titleRect];
DESTROY(attrString);
}
}
}
- (void) layout