(-initWithFrame:): Removed creation of titleView

([NSMenuView -setMenu:]): call update to handle new situation
([NSMenuView -update]): Removed call to _menu update, handle the
adding/removing of the title view.
([NSMenuView -sizeToFit]): reformatted to conform to coding standard,
handle missing titleView.
([NSMenuView -setWindowFrameForAttachingToRect:onScreen:preferredEdge:popUpSelectedItem:]):
Reformatted to conform to coding standard.  Removed the code that removed the titleView.
That is handled by update.
([NSMenuView -drawRect:]): Reformatted to conform to coding standards.
([NSMenuWindowTitleView -init]): Reformatted to conform to coding standards.
([NSMenuWindowTitleView -dealloc]): new method to prevent leaking the close button.
([NSMenuWindowTitleView -titleSize]): Reformatted to conform to coding standard
([NSMenuWindowTitleView -drawRect:]): idem
([NSMenuWindowTitleView -mouseDown:]): idem
([NSMenuWindowTitleView -createButton]): idem


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@16222 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
wim 2003-03-22 16:30:04 +00:00
parent 2f222d7aa5
commit d0b49085b6

View file

@ -147,11 +147,6 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
// Create an array to store our menu item cells. // Create an array to store our menu item cells.
_itemCells = [NSMutableArray new]; _itemCells = [NSMutableArray new];
// Create title view and add it. CHECKME, should we do this here?
_titleView = [[NSMenuWindowTitleView alloc] init];
[self addSubview: _titleView];
return self; return self;
} }
@ -215,7 +210,7 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
name: NSMenuDidRemoveItemNotification name: NSMenuDidRemoveItemNotification
object: _menu]; object: _menu];
[_titleView setMenu: _menu]; // WO CHECKME does this needs reorganizing? [self update];
// Force menu view's layout to be recalculated. // Force menu view's layout to be recalculated.
[self setNeedsSizing: YES]; [self setNeedsSizing: YES];
} }
@ -472,7 +467,24 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
*/ */
- (void) update - (void) update
{ {
[_menu update]; NSDebugLLog (@"NSMenu", @"update called on menu view");
if ([_menu _ownedByPopUp] && _titleView)
{
[_titleView removeFromSuperview];
_titleView = nil;
}
if (![_menu _ownedByPopUp] && !_titleView)
{
_titleView = [[NSMenuWindowTitleView alloc] init];
[_titleView setMenu: _menu];
[self addSubview: _titleView];
[_titleView release];
}
[_titleView setMenu: _menu];
if (_needsSizing)
[self sizeToFit];
if ([_menu isTornOff] && ![_menu isTransient]) if ([_menu isTornOff] && ![_menu isTransient])
{ {
@ -482,9 +494,6 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
{ {
[_titleView removeCloseButton]; [_titleView removeCloseButton];
} }
if (_needsSizing)
[self sizeToFit];
} }
- (void) setNeedsSizing: (BOOL)flag - (void) setNeedsSizing: (BOOL)flag
@ -499,148 +508,153 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
- (void) sizeToFit - (void) sizeToFit
{ {
unsigned i; unsigned i;
unsigned howMany = [_itemCells count]; unsigned howMany = [_itemCells count];
unsigned wideTitleView = 1; unsigned wideTitleView = 1;
float neededImageAndTitleWidth = [_titleView titleSize].width; float neededImageAndTitleWidth = 0.0;
float neededKeyEquivalentWidth = 0.0; float neededKeyEquivalentWidth = 0.0;
float neededStateImageWidth = 0.0; float neededStateImageWidth = 0.0;
float accumulatedOffset = 0.0; float accumulatedOffset = 0.0;
float popupImageWidth = 0.0; float popupImageWidth = 0.0;
float menuBarHeight = 0.0; float menuBarHeight = 0.0;
// Popup menu doesn't need title bar // Popup menu doesn't need title bar
if (![_menu _ownedByPopUp]) if (![_menu _ownedByPopUp] && _titleView)
menuBarHeight = [[self class] menuBarHeight]; {
else menuBarHeight = [[self class] menuBarHeight];
menuBarHeight += _leftBorderOffset; neededStateImageWidth = [_titleView titleSize].width;
}
// TODO: Optimize this loop. else
for (i = 0; i < howMany; i++) {
{ menuBarHeight += _leftBorderOffset;
float aStateImageWidth = 0.0; }
float aTitleWidth = 0.0;
float anImageWidth = 0.0; // TODO: Optimize this loop.
float anImageAndTitleWidth = 0.0; for (i = 0; i < howMany; i++)
float aKeyEquivalentWidth = 0.0; {
NSMenuItemCell *aCell = [_itemCells objectAtIndex: i]; float aStateImageWidth = 0.0;
float aTitleWidth = 0.0;
// State image area. float anImageWidth = 0.0;
aStateImageWidth = [aCell stateImageWidth]; float anImageAndTitleWidth = 0.0;
float aKeyEquivalentWidth = 0.0;
// Title and Image area. NSMenuItemCell *aCell = [_itemCells objectAtIndex: i];
aTitleWidth = [aCell titleWidth];
anImageWidth = [aCell imageWidth]; // State image area.
aStateImageWidth = [aCell stateImageWidth];
// Key equivalent area.
aKeyEquivalentWidth = [aCell keyEquivalentWidth]; // Title and Image area.
aTitleWidth = [aCell titleWidth];
switch ([aCell imagePosition]) anImageWidth = [aCell imageWidth];
{
case NSNoImage: // Key equivalent area.
anImageAndTitleWidth = aTitleWidth; aKeyEquivalentWidth = [aCell keyEquivalentWidth];
break;
switch ([aCell imagePosition])
case NSImageOnly: {
anImageAndTitleWidth = anImageWidth; case NSNoImage:
break; anImageAndTitleWidth = aTitleWidth;
break;
case NSImageLeft:
case NSImageRight: case NSImageOnly:
anImageAndTitleWidth = anImageWidth + aTitleWidth + xDist; anImageAndTitleWidth = anImageWidth;
break; break;
case NSImageBelow: case NSImageLeft:
case NSImageAbove: case NSImageRight:
case NSImageOverlaps: anImageAndTitleWidth = anImageWidth + aTitleWidth + xDist;
default: break;
if (aTitleWidth > anImageWidth)
anImageAndTitleWidth = aTitleWidth; case NSImageBelow:
else case NSImageAbove:
anImageAndTitleWidth = anImageWidth; case NSImageOverlaps:
break; default:
} if (aTitleWidth > anImageWidth)
anImageAndTitleWidth += aStateImageWidth; anImageAndTitleWidth = aTitleWidth;
else
if (aStateImageWidth > neededStateImageWidth) anImageAndTitleWidth = anImageWidth;
neededStateImageWidth = aStateImageWidth; break;
}
if (anImageAndTitleWidth > neededImageAndTitleWidth) anImageAndTitleWidth += aStateImageWidth;
{
neededImageAndTitleWidth = anImageAndTitleWidth; if (aStateImageWidth > neededStateImageWidth)
wideTitleView = 0; neededStateImageWidth = aStateImageWidth;
}
if (anImageAndTitleWidth > neededImageAndTitleWidth)
if (aKeyEquivalentWidth > neededKeyEquivalentWidth) {
neededKeyEquivalentWidth = aKeyEquivalentWidth; neededImageAndTitleWidth = anImageAndTitleWidth;
wideTitleView = 0;
// Popup menu has only one item with nibble image }
if (anImageWidth)
popupImageWidth = anImageWidth; if (aKeyEquivalentWidth > neededKeyEquivalentWidth)
} neededKeyEquivalentWidth = aKeyEquivalentWidth;
// Cache the needed widths. // Popup menu has only one item with nibble image
_stateImageWidth = neededStateImageWidth; if (anImageWidth)
_imageAndTitleWidth = neededImageAndTitleWidth; popupImageWidth = anImageWidth;
_keyEqWidth = neededKeyEquivalentWidth; }
if (howMany) // Cache the needed widths.
{ _stateImageWidth = neededStateImageWidth;
// Calculate the offsets and cache them. _imageAndTitleWidth = neededImageAndTitleWidth;
if (neededStateImageWidth) _keyEqWidth = neededKeyEquivalentWidth;
{
_stateImageOffset = accumulatedOffset += _horizontalEdgePad; if (howMany)
accumulatedOffset += neededStateImageWidth; {
} // Calculate the offsets and cache them.
if (neededStateImageWidth)
_imageAndTitleOffset = accumulatedOffset += _horizontalEdgePad; {
accumulatedOffset += neededImageAndTitleWidth; _stateImageOffset = accumulatedOffset += _horizontalEdgePad;
accumulatedOffset += neededStateImageWidth;
if (neededKeyEquivalentWidth) }
{
_keyEqOffset = accumulatedOffset += (2 * _horizontalEdgePad); _imageAndTitleOffset = accumulatedOffset += _horizontalEdgePad;
accumulatedOffset += neededKeyEquivalentWidth + _horizontalEdgePad; accumulatedOffset += neededImageAndTitleWidth;
}
else if (neededKeyEquivalentWidth)
{ {
if (wideTitleView && [_menu supermenu] != nil) _keyEqOffset = accumulatedOffset += (2 * _horizontalEdgePad);
accumulatedOffset += 15 + 3 + 2; accumulatedOffset += neededKeyEquivalentWidth + _horizontalEdgePad;
else }
accumulatedOffset += 3 * _horizontalEdgePad; else
} {
} if (wideTitleView && [_menu supermenu] != nil)
else accumulatedOffset += 15 + 3 + 2;
{ else
accumulatedOffset += 5 + neededImageAndTitleWidth + 3 + 2; accumulatedOffset += 3 * _horizontalEdgePad;
if (wideTitleView && [_menu supermenu] != nil) }
accumulatedOffset += 15; }
} else
{
// Calculate frame size. accumulatedOffset += 5 + neededImageAndTitleWidth + 3 + 2;
if (![_menu _ownedByPopUp]) if (wideTitleView && [_menu supermenu] != nil)
{ accumulatedOffset += 15;
// Add the border width: 1 for left, 2 for right sides }
_cellSize.width = accumulatedOffset + 3;
} // Calculate frame size.
else if (![_menu _ownedByPopUp])
{ {
_keyEqOffset = _cellSize.width - _keyEqWidth - popupImageWidth; // Add the border width: 1 for left, 2 for right sides
} _cellSize.width = accumulatedOffset + 3;
}
if (_horizontal == NO) else
{ {
[self setFrameSize: NSMakeSize(_cellSize.width + _leftBorderOffset, _keyEqOffset = _cellSize.width - _keyEqWidth - popupImageWidth;
(howMany * _cellSize.height) + menuBarHeight)]; }
[_titleView setFrame: NSMakeRect (0, howMany * _cellSize.height,
NSWidth (_bounds), menuBarHeight)]; if (_horizontal == NO)
} {
else [self setFrameSize: NSMakeSize(_cellSize.width + _leftBorderOffset,
{ (howMany * _cellSize.height) + menuBarHeight)];
[self setFrameSize: NSMakeSize((howMany * _cellSize.width), [_titleView setFrame: NSMakeRect (0, howMany * _cellSize.height,
_cellSize.height + _leftBorderOffset)]; NSWidth (_bounds), menuBarHeight)];
} }
else
_needsSizing = NO; {
[self setFrameSize: NSMakeSize((howMany * _cellSize.width),
_cellSize.height + _leftBorderOffset)];
}
_needsSizing = NO;
} }
- (float) stateImageOffset - (float) stateImageOffset
@ -845,69 +859,67 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
preferredEdge: (NSRectEdge)edge preferredEdge: (NSRectEdge)edge
popUpSelectedItem: (int)selectedItemIndex popUpSelectedItem: (int)selectedItemIndex
{ {
NSRect r; NSRect r;
NSRect cellFrame; NSRect cellFrame;
NSRect screenFrame; NSRect screenFrame;
int items = [_itemCells count]; int items = [_itemCells count];
[_titleView removeFromSuperview]; // Convert the screen rect to our view
cellFrame.size = screenRect.size;
// Convert the screen rect to our view cellFrame.origin = [_window convertScreenToBase: screenRect.origin];
cellFrame.size = screenRect.size; cellFrame = [self convertRect: cellFrame fromView: nil];
cellFrame.origin = [_window convertScreenToBase: screenRect.origin];
cellFrame = [self convertRect: cellFrame fromView: nil]; _cellSize = cellFrame.size;
[self sizeToFit];
_cellSize = cellFrame.size;
[self sizeToFit]; /*
* Compute the frame
/* */
* Compute the frame screenFrame = screenRect;
*/ if (items > 1)
screenFrame = screenRect; {
if (items > 1) float f;
{
float f; if (_horizontal == NO)
{
if (_horizontal == NO) f = screenRect.size.height * (items - 1);
{ screenFrame.size.height += f + _leftBorderOffset;
f = screenRect.size.height * (items - 1); screenFrame.origin.y -= f;
screenFrame.size.height += f + _leftBorderOffset; screenFrame.size.width += _leftBorderOffset;
screenFrame.origin.y -= f; screenFrame.origin.x -= _leftBorderOffset;
screenFrame.size.width += _leftBorderOffset; }
screenFrame.origin.x -= _leftBorderOffset; else
} {
else f = screenRect.size.width * (items - 1);
{ screenFrame.size.width += f;
f = screenRect.size.width * (items - 1); }
screenFrame.size.width += f; }
}
} // Move the menu window to screen?
// TODO
// Move the menu window to screen?
// TODO // Compute position for popups, if needed
if (selectedItemIndex != -1)
// Compute position for popups, if needed {
if (selectedItemIndex != -1) if (_horizontal == NO)
{ {
if (_horizontal == NO) screenFrame.origin.y += screenRect.size.height * selectedItemIndex;
{ }
screenFrame.origin.y += screenRect.size.height * selectedItemIndex; else
} {
else screenFrame.origin.x -= screenRect.size.width * selectedItemIndex;
{ }
screenFrame.origin.x -= screenRect.size.width * selectedItemIndex; }
}
} // Get the frameRect
r = [NSWindow frameRectForContentRect: screenFrame
// Get the frameRect
r = [NSWindow frameRectForContentRect: screenFrame
styleMask: [_window styleMask]]; styleMask: [_window styleMask]];
// Update position,if needed, using the preferredEdge; // Update position,if needed, using the preferredEdge;
// TODO // TODO
// Set the window frame // Set the window frame
[_window setFrame: r display: NO]; [_window setFrame: r display: NO];
} }
/* /*
@ -915,27 +927,27 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
*/ */
- (void) drawRect: (NSRect)rect - (void) drawRect: (NSRect)rect
{ {
int i; int i;
int howMany = [_itemCells count]; int howMany = [_itemCells count];
NSRectEdge sides[] = {NSMinXEdge, NSMaxYEdge}; NSRectEdge sides[] = {NSMinXEdge, NSMaxYEdge};
float grays[] = {NSDarkGray, NSDarkGray}; float grays[] = {NSDarkGray, NSDarkGray};
// Draw the dark gray upper left lines. // Draw the dark gray upper left lines.
NSDrawTiledRects(rect, rect, sides, grays, 2); NSDrawTiledRects(rect, rect, sides, grays, 2);
// Draw the menu cells. // Draw the menu cells.
for (i = 0; i < howMany; i++) for (i = 0; i < howMany; i++)
{ {
NSRect aRect; NSRect aRect;
NSMenuItemCell *aCell; NSMenuItemCell *aCell;
aRect = [self rectOfItemAtIndex: i]; aRect = [self rectOfItemAtIndex: i];
if (NSIntersectsRect(rect, aRect) == YES) if (NSIntersectsRect(rect, aRect) == YES)
{ {
aCell = [_itemCells objectAtIndex: i]; aCell = [_itemCells objectAtIndex: i];
[aCell drawWithFrame: aRect inView: self]; [aCell drawWithFrame: aRect inView: self];
} }
} }
} }
/* /*
@ -1430,11 +1442,17 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
- (id) init - (id) init
{ {
[super init]; self = [super init];
attr = nil;
return self;
}
attr = nil; - (void) dealloc
{
return self; RELEASE (button);
[super dealloc];
} }
- (BOOL) acceptsFirstMouse: (NSEvent *)theEvent - (BOOL) acceptsFirstMouse: (NSEvent *)theEvent
@ -1454,93 +1472,93 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
- (NSSize) titleSize - (NSSize) titleSize
{ {
if (attr == nil) if (attr == nil)
{ {
attr = [[NSDictionary alloc] initWithObjectsAndKeys: attr = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSFont boldSystemFontOfSize: 0], NSFontAttributeName, [NSFont boldSystemFontOfSize: 0], NSFontAttributeName,
[NSColor windowFrameTextColor], NSForegroundColorAttributeName, [NSColor windowFrameTextColor], NSForegroundColorAttributeName,
nil]; nil];
} }
return [[menu title] sizeWithAttributes: attr]; 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};
// 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);
// Draw the title box's button. // Draw the title box's button.
NSDrawButton(workRect, workRect); NSDrawButton(workRect, workRect);
// Paint it Black! // Paint it Black!
workRect.origin.x += 1; workRect.origin.x += 1;
workRect.origin.y += 2; workRect.origin.y += 2;
workRect.size.height -= 3; workRect.size.height -= 3;
workRect.size.width -= 3; workRect.size.width -= 3;
[[NSColor windowFrameColor] set]; [[NSColor windowFrameColor] set];
NSRectFill(workRect); NSRectFill(workRect);
// Draw the title // Draw the title
titleSize = [self titleSize]; titleSize = [self titleSize];
workRect.origin.x += 4; workRect.origin.x += 4;
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];
} }
- (void) mouseDown: (NSEvent*)theEvent - (void) mouseDown: (NSEvent*)theEvent
{ {
NSPoint lastLocation; NSPoint lastLocation;
NSPoint location; NSPoint location;
unsigned eventMask = NSLeftMouseUpMask | NSLeftMouseDraggedMask; unsigned eventMask = NSLeftMouseUpMask | NSLeftMouseDraggedMask;
BOOL done = NO; BOOL done = NO;
NSDate *theDistantFuture = [NSDate distantFuture]; NSDate *theDistantFuture = [NSDate distantFuture];
NSDebugLLog (@"NSMenu", @"Mouse down in title!"); NSDebugLLog (@"NSMenu", @"Mouse down in title!");
lastLocation = [theEvent locationInWindow]; lastLocation = [theEvent locationInWindow];
if (![menu isTornOff] && [menu supermenu]) if (![menu isTornOff] && [menu supermenu])
{ {
[menu setTornOff: YES]; [menu setTornOff: YES];
} }
while (!done) while (!done)
{ {
theEvent = [NSApp nextEventMatchingMask: eventMask theEvent = [NSApp nextEventMatchingMask: eventMask
untilDate: theDistantFuture untilDate: theDistantFuture
inMode: NSEventTrackingRunLoopMode inMode: NSEventTrackingRunLoopMode
dequeue: YES]; dequeue: YES];
switch ([theEvent type]) switch ([theEvent type])
{ {
case NSRightMouseUp: case NSRightMouseUp:
case NSLeftMouseUp: case NSLeftMouseUp:
done = YES; done = YES;
break; break;
case NSRightMouseDragged: case NSRightMouseDragged:
case NSLeftMouseDragged: case NSLeftMouseDragged:
location = [_window mouseLocationOutsideOfEventStream]; location = [_window mouseLocationOutsideOfEventStream];
if (NSEqualPoints(location, lastLocation) == NO) if (NSEqualPoints(location, lastLocation) == NO)
{ {
NSPoint origin = [_window frame].origin; NSPoint origin = [_window frame].origin;
origin.x += (location.x - lastLocation.x); origin.x += (location.x - lastLocation.x);
origin.y += (location.y - lastLocation.y); origin.y += (location.y - lastLocation.y);
[menu nestedSetFrameOrigin: origin]; [menu nestedSetFrameOrigin: origin];
} }
break; break;
default: default:
break; break;
} }
} }
} }
- (void) createButton - (void) createButton
@ -1550,10 +1568,10 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
NSImage *closeHImage = [NSImage imageNamed: @"common_CloseH"]; NSImage *closeHImage = [NSImage imageNamed: @"common_CloseH"];
NSSize imageSize = [closeImage size]; NSSize imageSize = [closeImage size];
NSRect rect = { NSRect rect = {
{ _frame.size.width - imageSize.width - 4, { _frame.size.width - imageSize.width - 4,
(_frame.size.height - imageSize.height) / 2 }, (_frame.size.height - imageSize.height) / 2 },
{ imageSize.width, imageSize.height } }; { imageSize.width, imageSize.height } };
button = [[NSButton alloc] initWithFrame: rect]; button = [[NSButton alloc] initWithFrame: rect];
[button setRefusesFirstResponder: YES]; [button setRefusesFirstResponder: YES];
[button setButtonType: NSMomentaryChangeButton]; [button setButtonType: NSMomentaryChangeButton];
@ -1566,7 +1584,7 @@ _addLeftBorderOffsetToRect(NSRect aRect, BOOL isHorizontal)
[button setAction: @selector(_performMenuClose:)]; [button setAction: @selector(_performMenuClose:)];
[self setAutoresizingMask: [self setAutoresizingMask:
NSViewMinXMargin | NSViewMinYMargin | NSViewMaxYMargin]; NSViewMinXMargin | NSViewMinYMargin | NSViewMaxYMargin];
} }
- (void) removeCloseButton - (void) removeCloseButton