(-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;
}
else
{
menuBarHeight += _leftBorderOffset;
}
// TODO: Optimize this loop. // TODO: Optimize this loop.
for (i = 0; i < howMany; i++) for (i = 0; i < howMany; i++)
{ {
float aStateImageWidth = 0.0; float aStateImageWidth = 0.0;
float aTitleWidth = 0.0; float aTitleWidth = 0.0;
float anImageWidth = 0.0; float anImageWidth = 0.0;
float anImageAndTitleWidth = 0.0; float anImageAndTitleWidth = 0.0;
float aKeyEquivalentWidth = 0.0; float aKeyEquivalentWidth = 0.0;
NSMenuItemCell *aCell = [_itemCells objectAtIndex: i]; NSMenuItemCell *aCell = [_itemCells objectAtIndex: i];
// State image area. // State image area.
aStateImageWidth = [aCell stateImageWidth]; aStateImageWidth = [aCell stateImageWidth];
// Title and Image area. // Title and Image area.
aTitleWidth = [aCell titleWidth]; aTitleWidth = [aCell titleWidth];
anImageWidth = [aCell imageWidth]; anImageWidth = [aCell imageWidth];
// Key equivalent area. // Key equivalent area.
aKeyEquivalentWidth = [aCell keyEquivalentWidth]; aKeyEquivalentWidth = [aCell keyEquivalentWidth];
switch ([aCell imagePosition]) switch ([aCell imagePosition])
{ {
case NSNoImage: case NSNoImage:
anImageAndTitleWidth = aTitleWidth; anImageAndTitleWidth = aTitleWidth;
break; break;
case NSImageOnly: case NSImageOnly:
anImageAndTitleWidth = anImageWidth; anImageAndTitleWidth = anImageWidth;
break; break;
case NSImageLeft: case NSImageLeft:
case NSImageRight: case NSImageRight:
anImageAndTitleWidth = anImageWidth + aTitleWidth + xDist; anImageAndTitleWidth = anImageWidth + aTitleWidth + xDist;
break; break;
case NSImageBelow: case NSImageBelow:
case NSImageAbove: case NSImageAbove:
case NSImageOverlaps: case NSImageOverlaps:
default: default:
if (aTitleWidth > anImageWidth) if (aTitleWidth > anImageWidth)
anImageAndTitleWidth = aTitleWidth; anImageAndTitleWidth = aTitleWidth;
else else
anImageAndTitleWidth = anImageWidth; anImageAndTitleWidth = anImageWidth;
break; break;
} }
anImageAndTitleWidth += aStateImageWidth; anImageAndTitleWidth += aStateImageWidth;
if (aStateImageWidth > neededStateImageWidth) if (aStateImageWidth > neededStateImageWidth)
neededStateImageWidth = aStateImageWidth; neededStateImageWidth = aStateImageWidth;
if (anImageAndTitleWidth > neededImageAndTitleWidth) if (anImageAndTitleWidth > neededImageAndTitleWidth)
{ {
neededImageAndTitleWidth = anImageAndTitleWidth; neededImageAndTitleWidth = anImageAndTitleWidth;
wideTitleView = 0; wideTitleView = 0;
} }
if (aKeyEquivalentWidth > neededKeyEquivalentWidth) if (aKeyEquivalentWidth > neededKeyEquivalentWidth)
neededKeyEquivalentWidth = aKeyEquivalentWidth; neededKeyEquivalentWidth = aKeyEquivalentWidth;
// Popup menu has only one item with nibble image // Popup menu has only one item with nibble image
if (anImageWidth) if (anImageWidth)
popupImageWidth = anImageWidth; popupImageWidth = anImageWidth;
} }
// Cache the needed widths. // Cache the needed widths.
_stateImageWidth = neededStateImageWidth; _stateImageWidth = neededStateImageWidth;
_imageAndTitleWidth = neededImageAndTitleWidth; _imageAndTitleWidth = neededImageAndTitleWidth;
_keyEqWidth = neededKeyEquivalentWidth; _keyEqWidth = neededKeyEquivalentWidth;
if (howMany) if (howMany)
{ {
// Calculate the offsets and cache them. // Calculate the offsets and cache them.
if (neededStateImageWidth) if (neededStateImageWidth)
{ {
_stateImageOffset = accumulatedOffset += _horizontalEdgePad; _stateImageOffset = accumulatedOffset += _horizontalEdgePad;
accumulatedOffset += neededStateImageWidth; accumulatedOffset += neededStateImageWidth;
} }
_imageAndTitleOffset = accumulatedOffset += _horizontalEdgePad; _imageAndTitleOffset = accumulatedOffset += _horizontalEdgePad;
accumulatedOffset += neededImageAndTitleWidth; accumulatedOffset += neededImageAndTitleWidth;
if (neededKeyEquivalentWidth) if (neededKeyEquivalentWidth)
{ {
_keyEqOffset = accumulatedOffset += (2 * _horizontalEdgePad); _keyEqOffset = accumulatedOffset += (2 * _horizontalEdgePad);
accumulatedOffset += neededKeyEquivalentWidth + _horizontalEdgePad; accumulatedOffset += neededKeyEquivalentWidth + _horizontalEdgePad;
} }
else else
{ {
if (wideTitleView && [_menu supermenu] != nil) if (wideTitleView && [_menu supermenu] != nil)
accumulatedOffset += 15 + 3 + 2; accumulatedOffset += 15 + 3 + 2;
else else
accumulatedOffset += 3 * _horizontalEdgePad; accumulatedOffset += 3 * _horizontalEdgePad;
} }
} }
else else
{ {
accumulatedOffset += 5 + neededImageAndTitleWidth + 3 + 2; accumulatedOffset += 5 + neededImageAndTitleWidth + 3 + 2;
if (wideTitleView && [_menu supermenu] != nil) if (wideTitleView && [_menu supermenu] != nil)
accumulatedOffset += 15; accumulatedOffset += 15;
} }
// Calculate frame size. // Calculate frame size.
if (![_menu _ownedByPopUp]) if (![_menu _ownedByPopUp])
{ {
// Add the border width: 1 for left, 2 for right sides // Add the border width: 1 for left, 2 for right sides
_cellSize.width = accumulatedOffset + 3; _cellSize.width = accumulatedOffset + 3;
} }
else else
{ {
_keyEqOffset = _cellSize.width - _keyEqWidth - popupImageWidth; _keyEqOffset = _cellSize.width - _keyEqWidth - popupImageWidth;
} }
if (_horizontal == NO) if (_horizontal == NO)
{ {
[self setFrameSize: NSMakeSize(_cellSize.width + _leftBorderOffset, [self setFrameSize: NSMakeSize(_cellSize.width + _leftBorderOffset,
(howMany * _cellSize.height) + menuBarHeight)]; (howMany * _cellSize.height) + menuBarHeight)];
[_titleView setFrame: NSMakeRect (0, howMany * _cellSize.height, [_titleView setFrame: NSMakeRect (0, howMany * _cellSize.height,
NSWidth (_bounds), menuBarHeight)]; NSWidth (_bounds), menuBarHeight)];
} }
else else
{ {
[self setFrameSize: NSMakeSize((howMany * _cellSize.width), [self setFrameSize: NSMakeSize((howMany * _cellSize.width),
_cellSize.height + _leftBorderOffset)]; _cellSize.height + _leftBorderOffset)];
} }
_needsSizing = NO; _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;
cellFrame.origin = [_window convertScreenToBase: screenRect.origin];
cellFrame = [self convertRect: cellFrame fromView: nil];
// Convert the screen rect to our view _cellSize = cellFrame.size;
cellFrame.size = screenRect.size; [self sizeToFit];
cellFrame.origin = [_window convertScreenToBase: screenRect.origin];
cellFrame = [self convertRect: cellFrame fromView: nil];
_cellSize = cellFrame.size; /*
[self sizeToFit]; * Compute the frame
*/
screenFrame = screenRect;
if (items > 1)
{
float f;
/* if (_horizontal == NO)
* Compute the frame {
*/ f = screenRect.size.height * (items - 1);
screenFrame = screenRect; screenFrame.size.height += f + _leftBorderOffset;
if (items > 1) screenFrame.origin.y -= f;
{ screenFrame.size.width += _leftBorderOffset;
float f; screenFrame.origin.x -= _leftBorderOffset;
}
else
{
f = screenRect.size.width * (items - 1);
screenFrame.size.width += f;
}
}
if (_horizontal == NO) // Move the menu window to screen?
{ // TODO
f = screenRect.size.height * (items - 1);
screenFrame.size.height += f + _leftBorderOffset;
screenFrame.origin.y -= f;
screenFrame.size.width += _leftBorderOffset;
screenFrame.origin.x -= _leftBorderOffset;
}
else
{
f = screenRect.size.width * (items - 1);
screenFrame.size.width += f;
}
}
// Move the menu window to screen? // Compute position for popups, if needed
// TODO if (selectedItemIndex != -1)
{
if (_horizontal == NO)
{
screenFrame.origin.y += screenRect.size.height * selectedItemIndex;
}
else
{
screenFrame.origin.x -= screenRect.size.width * selectedItemIndex;
}
}
// Compute position for popups, if needed // Get the frameRect
if (selectedItemIndex != -1) r = [NSWindow frameRectForContentRect: screenFrame
{
if (_horizontal == NO)
{
screenFrame.origin.y += screenRect.size.height * selectedItemIndex;
}
else
{
screenFrame.origin.x -= screenRect.size.width * selectedItemIndex;
}
}
// 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; attr = nil;
return self; return self;
}
- (void) dealloc
{
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,9 +1568,9 @@ _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];
@ -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