fixes to sizeToFit, NSMenuTitleView's drawRect, added titleSize

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@16198 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
stoyan 2003-03-18 16:24:31 +00:00
parent ed81b1248e
commit efa8df36c4

View file

@ -57,8 +57,10 @@
{ {
NSMenu *menu; NSMenu *menu;
NSButton *button; NSButton *button;
NSDictionary *attr;
} }
- (NSSize) titleSize;
- (void) addCloseButton; - (void) addCloseButton;
- (void) removeCloseButton; - (void) removeCloseButton;
- (void) createButton; - (void) createButton;
@ -110,6 +112,7 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
{ {
return YES; return YES;
} }
// We do not want to popup menus in this menu. // We do not want to popup menus in this menu.
- (id) menuForEvent: (NSEvent*) theEvent - (id) menuForEvent: (NSEvent*) theEvent
{ {
@ -117,7 +120,6 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
return nil; return nil;
} }
/* /*
* Init methods. * Init methods.
*/ */
@ -499,7 +501,8 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
{ {
unsigned i; unsigned i;
unsigned howMany = [_itemCells count]; unsigned howMany = [_itemCells count];
float neededImageAndTitleWidth = [_font widthOfString: [_menu title]]; unsigned wideTitleView = 1;
float neededImageAndTitleWidth = [_titleView titleSize].width;
float neededKeyEquivalentWidth = 0.0; float neededKeyEquivalentWidth = 0.0;
float neededStateImageWidth = 0.0; float neededStateImageWidth = 0.0;
float accumulatedOffset = 0.0; float accumulatedOffset = 0.0;
@ -515,19 +518,14 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
if ([_menu _ownedByPopUp]) if ([_menu _ownedByPopUp])
_leftBorderOffset = 0; _leftBorderOffset = 0;
if ([_menu supermenu] != nil)
neededImageAndTitleWidth = [_font widthOfString: [_menu title]] + 15;
else
neededImageAndTitleWidth = [_font widthOfString: [_menu title]];
// TODO: Optimize this loop. // TODO: Optimize this loop.
for (i = 0; i < howMany; i++) for (i = 0; i < howMany; i++)
{ {
float anImageAndTitleWidth = 0.0;
float anImageWidth = 0.0;
float aKeyEquivalentWidth = 0.0;
float aStateImageWidth = 0.0; float aStateImageWidth = 0.0;
float aTitleWidth = 0.0; float aTitleWidth = 0.0;
float anImageWidth = 0.0;
float anImageAndTitleWidth = 0.0;
float aKeyEquivalentWidth = 0.0;
NSMenuItemCell *aCell = [_itemCells objectAtIndex: i]; NSMenuItemCell *aCell = [_itemCells objectAtIndex: i];
// State image area. // State image area.
@ -571,7 +569,10 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
neededStateImageWidth = aStateImageWidth; neededStateImageWidth = aStateImageWidth;
if (anImageAndTitleWidth > neededImageAndTitleWidth) if (anImageAndTitleWidth > neededImageAndTitleWidth)
{
neededImageAndTitleWidth = anImageAndTitleWidth; neededImageAndTitleWidth = anImageAndTitleWidth;
wideTitleView = 0;
}
if (aKeyEquivalentWidth > neededKeyEquivalentWidth) if (aKeyEquivalentWidth > neededKeyEquivalentWidth)
neededKeyEquivalentWidth = aKeyEquivalentWidth; neededKeyEquivalentWidth = aKeyEquivalentWidth;
@ -582,18 +583,43 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
_imageAndTitleWidth = neededImageAndTitleWidth; _imageAndTitleWidth = neededImageAndTitleWidth;
_keyEqWidth = neededKeyEquivalentWidth; _keyEqWidth = neededKeyEquivalentWidth;
if (howMany)
{
// Calculate the offsets and cache them. // Calculate the offsets and cache them.
_stateImageOffset = _imageAndTitleOffset = accumulatedOffset if (neededStateImageWidth)
= _horizontalEdgePad; {
_stateImageOffset = accumulatedOffset += _horizontalEdgePad;
accumulatedOffset += neededStateImageWidth;
}
_imageAndTitleOffset = accumulatedOffset += _horizontalEdgePad;
accumulatedOffset += neededImageAndTitleWidth; accumulatedOffset += neededImageAndTitleWidth;
_keyEqOffset = accumulatedOffset += 2 * _horizontalEdgePad; if (neededKeyEquivalentWidth)
{
_keyEqOffset = accumulatedOffset += (2 * _horizontalEdgePad);
accumulatedOffset += neededKeyEquivalentWidth + _horizontalEdgePad; accumulatedOffset += neededKeyEquivalentWidth + _horizontalEdgePad;
}
else
{
if (wideTitleView && [_menu supermenu] != nil)
accumulatedOffset += 15 + 3 + 2;
else
accumulatedOffset += 3 * _horizontalEdgePad;
}
}
else
{
accumulatedOffset += 5 + neededImageAndTitleWidth + 3 + 2;
if (wideTitleView && [_menu supermenu] != nil)
accumulatedOffset += 15;
}
// Calculate frame size. // Calculate frame size.
if (![_menu _ownedByPopUp]) if (![_menu _ownedByPopUp])
{ {
_cellSize.width = accumulatedOffset + 3; // Add the border width // Add the border width: 1 for left, 2 for right sides
_cellSize.width = accumulatedOffset + 3;
} }
if (_horizontal == NO) if (_horizontal == NO)
@ -1414,6 +1440,15 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
@implementation NSMenuWindowTitleView @implementation NSMenuWindowTitleView
- (id) init
{
[super init];
attr = nil;
return self;
}
- (BOOL) acceptsFirstMouse: (NSEvent *)theEvent - (BOOL) acceptsFirstMouse: (NSEvent *)theEvent
{ {
return YES; return YES;
@ -1429,14 +1464,25 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
return menu; return menu;
} }
- (NSSize) titleSize
{
if (attr == nil)
{
attr = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont boldSystemFontOfSize: 0], NSFontAttributeName,
[NSColor windowFrameTextColor], NSForegroundColorAttributeName,
nil];
}
return [[menu title] sizeWithAttributes: attr];
}
- (void) drawRect: (NSRect)rect - (void) drawRect: (NSRect)rect
{ {
NSRect workRect = [self bounds]; NSRect workRect = [self bounds];
NSSize titleSize; NSSize titleSize;
NSRectEdge sides[] = {NSMinXEdge, NSMaxYEdge}; NSRectEdge sides[] = {NSMinXEdge, NSMaxYEdge};
float grays[] = {NSDarkGray, NSDarkGray}; float grays[] = {NSDarkGray, NSDarkGray};
/* Cache the title attributes */
static NSDictionary *attr = nil;
// Draw the dark gray upper left lines. // Draw the dark gray upper left lines.
workRect = NSDrawTiledRects(workRect, workRect, sides, grays, 2); workRect = NSDrawTiledRects(workRect, workRect, sides, grays, 2);
@ -1453,17 +1499,8 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
NSRectFill(workRect); NSRectFill(workRect);
// Draw the title // Draw the title
if (attr == nil) titleSize = [self titleSize];
{ workRect.origin.x += 4;
attr = [[NSDictionary alloc]
initWithObjectsAndKeys:
[NSFont boldSystemFontOfSize: 0], NSFontAttributeName,
[NSColor windowFrameTextColor], NSForegroundColorAttributeName,
nil];
}
titleSize = [[menu title] sizeWithAttributes: attr];
workRect.origin.x += 5;
workRect.origin.y = NSMidY (workRect) - titleSize.height / 2; workRect.origin.y = NSMidY (workRect) - titleSize.height / 2;
workRect.size.height = titleSize.height; workRect.size.height = titleSize.height;
[[menu title] drawInRect: workRect withAttributes: attr]; [[menu title] drawInRect: workRect withAttributes: attr];
@ -1524,9 +1561,10 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
NSImage *closeImage = [NSImage imageNamed: @"common_Close"]; NSImage *closeImage = [NSImage imageNamed: @"common_Close"];
NSImage *closeHImage = [NSImage imageNamed: @"common_CloseH"]; NSImage *closeHImage = [NSImage imageNamed: @"common_CloseH"];
NSSize imageSize = [closeImage size]; NSSize imageSize = [closeImage size];
NSRect rect = { { _frame.size.width - imageSize.width - 4, NSRect rect = {
{ _frame.size.width - imageSize.width - 4,
(_frame.size.height - imageSize.height) / 2 }, (_frame.size.height - imageSize.height) / 2 },
{ imageSize.height, imageSize.width } }; { imageSize.width, imageSize.height } };
button = [[NSButton alloc] initWithFrame: rect]; button = [[NSButton alloc] initWithFrame: rect];
[button setRefusesFirstResponder: YES]; [button setRefusesFirstResponder: YES];
@ -1556,7 +1594,6 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
[self setNeedsDisplay: YES]; [self setNeedsDisplay: YES];
} }
- (void) rightMouseDown: (NSEvent*)theEvent - (void) rightMouseDown: (NSEvent*)theEvent
{ {
} }