mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-05-31 18:00:48 +00:00
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:
parent
ed81b1248e
commit
efa8df36c4
1 changed files with 217 additions and 180 deletions
|
@ -57,8 +57,10 @@
|
|||
{
|
||||
NSMenu *menu;
|
||||
NSButton *button;
|
||||
NSDictionary *attr;
|
||||
}
|
||||
|
||||
- (NSSize) titleSize;
|
||||
- (void) addCloseButton;
|
||||
- (void) removeCloseButton;
|
||||
- (void) createButton;
|
||||
|
@ -110,6 +112,7 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
// We do not want to popup menus in this menu.
|
||||
- (id) menuForEvent: (NSEvent*) theEvent
|
||||
{
|
||||
|
@ -117,7 +120,6 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
return nil;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Init methods.
|
||||
*/
|
||||
|
@ -499,7 +501,8 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
{
|
||||
unsigned i;
|
||||
unsigned howMany = [_itemCells count];
|
||||
float neededImageAndTitleWidth = [_font widthOfString: [_menu title]];
|
||||
unsigned wideTitleView = 1;
|
||||
float neededImageAndTitleWidth = [_titleView titleSize].width;
|
||||
float neededKeyEquivalentWidth = 0.0;
|
||||
float neededStateImageWidth = 0.0;
|
||||
float accumulatedOffset = 0.0;
|
||||
|
@ -515,19 +518,14 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
if ([_menu _ownedByPopUp])
|
||||
_leftBorderOffset = 0;
|
||||
|
||||
if ([_menu supermenu] != nil)
|
||||
neededImageAndTitleWidth = [_font widthOfString: [_menu title]] + 15;
|
||||
else
|
||||
neededImageAndTitleWidth = [_font widthOfString: [_menu title]];
|
||||
|
||||
// TODO: Optimize this loop.
|
||||
for (i = 0; i < howMany; i++)
|
||||
{
|
||||
float anImageAndTitleWidth = 0.0;
|
||||
float anImageWidth = 0.0;
|
||||
float aKeyEquivalentWidth = 0.0;
|
||||
float aStateImageWidth = 0.0;
|
||||
float aTitleWidth = 0.0;
|
||||
float anImageWidth = 0.0;
|
||||
float anImageAndTitleWidth = 0.0;
|
||||
float aKeyEquivalentWidth = 0.0;
|
||||
NSMenuItemCell *aCell = [_itemCells objectAtIndex: i];
|
||||
|
||||
// State image area.
|
||||
|
@ -571,7 +569,10 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
neededStateImageWidth = aStateImageWidth;
|
||||
|
||||
if (anImageAndTitleWidth > neededImageAndTitleWidth)
|
||||
{
|
||||
neededImageAndTitleWidth = anImageAndTitleWidth;
|
||||
wideTitleView = 0;
|
||||
}
|
||||
|
||||
if (aKeyEquivalentWidth > neededKeyEquivalentWidth)
|
||||
neededKeyEquivalentWidth = aKeyEquivalentWidth;
|
||||
|
@ -582,18 +583,43 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
_imageAndTitleWidth = neededImageAndTitleWidth;
|
||||
_keyEqWidth = neededKeyEquivalentWidth;
|
||||
|
||||
if (howMany)
|
||||
{
|
||||
// Calculate the offsets and cache them.
|
||||
_stateImageOffset = _imageAndTitleOffset = accumulatedOffset
|
||||
= _horizontalEdgePad;
|
||||
if (neededStateImageWidth)
|
||||
{
|
||||
_stateImageOffset = accumulatedOffset += _horizontalEdgePad;
|
||||
accumulatedOffset += neededStateImageWidth;
|
||||
}
|
||||
|
||||
_imageAndTitleOffset = accumulatedOffset += _horizontalEdgePad;
|
||||
accumulatedOffset += neededImageAndTitleWidth;
|
||||
|
||||
_keyEqOffset = accumulatedOffset += 2 * _horizontalEdgePad;
|
||||
if (neededKeyEquivalentWidth)
|
||||
{
|
||||
_keyEqOffset = accumulatedOffset += (2 * _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.
|
||||
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)
|
||||
|
@ -1414,6 +1440,15 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
|
||||
@implementation NSMenuWindowTitleView
|
||||
|
||||
- (id) init
|
||||
{
|
||||
[super init];
|
||||
|
||||
attr = nil;
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL) acceptsFirstMouse: (NSEvent *)theEvent
|
||||
{
|
||||
return YES;
|
||||
|
@ -1429,14 +1464,25 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
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
|
||||
{
|
||||
NSRect workRect = [self bounds];
|
||||
NSSize titleSize;
|
||||
NSRectEdge sides[] = {NSMinXEdge, NSMaxYEdge};
|
||||
float grays[] = {NSDarkGray, NSDarkGray};
|
||||
/* Cache the title attributes */
|
||||
static NSDictionary *attr = nil;
|
||||
|
||||
// Draw the dark gray upper left lines.
|
||||
workRect = NSDrawTiledRects(workRect, workRect, sides, grays, 2);
|
||||
|
@ -1453,17 +1499,8 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
NSRectFill(workRect);
|
||||
|
||||
// Draw the title
|
||||
if (attr == nil)
|
||||
{
|
||||
attr = [[NSDictionary alloc]
|
||||
initWithObjectsAndKeys:
|
||||
[NSFont boldSystemFontOfSize: 0], NSFontAttributeName,
|
||||
[NSColor windowFrameTextColor], NSForegroundColorAttributeName,
|
||||
nil];
|
||||
}
|
||||
|
||||
titleSize = [[menu title] sizeWithAttributes: attr];
|
||||
workRect.origin.x += 5;
|
||||
titleSize = [self titleSize];
|
||||
workRect.origin.x += 4;
|
||||
workRect.origin.y = NSMidY (workRect) - titleSize.height / 2;
|
||||
workRect.size.height = titleSize.height;
|
||||
[[menu title] drawInRect: workRect withAttributes: attr];
|
||||
|
@ -1521,12 +1558,13 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
- (void) createButton
|
||||
{
|
||||
// create the menu's close button
|
||||
NSImage* closeImage = [NSImage imageNamed: @"common_Close"];
|
||||
NSImage* closeHImage = [NSImage imageNamed: @"common_CloseH"];
|
||||
NSImage *closeImage = [NSImage imageNamed: @"common_Close"];
|
||||
NSImage *closeHImage = [NSImage imageNamed: @"common_CloseH"];
|
||||
NSSize imageSize = [closeImage size];
|
||||
NSRect rect = { { _frame.size.width - imageSize.width - 4,
|
||||
(_frame.size.height - imageSize.height) / 2},
|
||||
{ imageSize.height, imageSize.width } };
|
||||
NSRect rect = {
|
||||
{ _frame.size.width - imageSize.width - 4,
|
||||
(_frame.size.height - imageSize.height) / 2 },
|
||||
{ imageSize.width, imageSize.height } };
|
||||
|
||||
button = [[NSButton alloc] initWithFrame: rect];
|
||||
[button setRefusesFirstResponder: YES];
|
||||
|
@ -1556,7 +1594,6 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
|
|||
[self setNeedsDisplay: YES];
|
||||
}
|
||||
|
||||
|
||||
- (void) rightMouseDown: (NSEvent*)theEvent
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue