Fix NSButtonCell size calculations. Rename xDist and yDist.

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@19814 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
alexm 2004-08-02 14:36:09 +00:00
parent 6454346206
commit cb93530d65
5 changed files with 123 additions and 121 deletions

View file

@ -1,3 +1,14 @@
2004-08-02 16:32 Alexander Malmberg <alexander@malmberg.org>
* Headers/AppKit/NSCell.h, Source/NSMenuView.m: Rename xDist and
yDist to GSCellTextImageXDist and GSCellTextImageYDist.
* Source/NSMenuItemCell.m: Rename xDist and yDist.
(titleRectForBounds:): Reindent.
* Source/NSButtonCell.m (-drawInteriorWithFrame:inView:, -cellSize):
Adjust size calculations, make them more forgiving before clipping
text, and make -cellSize and -drawInteriorWithFrame:inView: match
each other again. Update xDist and yDist references.
2004-08-01 14:55 Alexander Malmberg <alexander@malmberg.org> 2004-08-01 14:55 Alexander Malmberg <alexander@malmberg.org>
* Source/GSStandardWindowDecorationView.m (-drawTitleBar): Fix * Source/GSStandardWindowDecorationView.m (-drawTitleBar): Fix

View file

@ -100,8 +100,8 @@ enum {
}; };
enum { enum {
xDist = 2, // horizontal distance between the text and image rects. GSCellTextImageXDist = 2, // horizontal distance between the text and image rects.
yDist = 2 // vertical distance between the text and image rects. GSCellTextImageYDist = 2 // vertical distance between the text and image rects.
}; };
/* /*

View file

@ -848,12 +848,12 @@
titleToDisplay = [self attributedTitle]; titleToDisplay = [self attributedTitle];
} }
if (imageToDisplay) if (imageToDisplay && ipos != NSNoImage)
{ {
imageSize = [imageToDisplay size]; imageSize = [imageToDisplay size];
} }
if (titleToDisplay && (ipos == NSImageAbove || ipos == NSImageBelow)) if (titleToDisplay && ipos != NSImageOnly)
{ {
titleSize = [titleToDisplay size]; titleSize = [titleToDisplay size];
} }
@ -870,11 +870,28 @@
} }
} }
/*
The size calculations here should be changed very carefully, and _must_ be
kept in sync with -cellSize. Changing the calculations to require more
space isn't OK; this breaks interfaces designed using the old sizes by
clipping away parts of the title.
The current size calculations ensure that for bordered or bezeled cells,
there's always at least a three point margin between the size returned by
-cellSize and the minimum size required not to clip text. (In other words,
the text can become three points wider (due to eg. font mismatches) before
you lose the last character.)
*/
switch (ipos) switch (ipos)
{ {
case NSNoImage: case NSNoImage:
imageToDisplay = nil; imageToDisplay = nil;
titleRect = cellFrame; titleRect = cellFrame;
if (titleSize.width + 6 <= titleRect.size.width)
{
titleRect.origin.x += 3;
titleRect.size.width -= 6;
}
break; break;
case NSImageOnly: case NSImageOnly:
@ -889,13 +906,14 @@
if (_cell.is_bordered || _cell.is_bezeled) if (_cell.is_bordered || _cell.is_bezeled)
{ {
imageRect.origin.x += 3; imageRect.origin.x += 3;
imageRect.size.height -= 2;
imageRect.origin.y += 1;
} }
titleRect = imageRect; titleRect = imageRect;
titleRect.origin.x += imageSize.width + xDist; titleRect.origin.x += imageSize.width + GSCellTextImageXDist;
titleRect.size.width = cellFrame.size.width - imageSize.width - xDist; titleRect.size.width = NSMaxX(cellFrame) - titleRect.origin.x;
titleRect.size.width -= 3; if (titleSize.width + 3 <= titleRect.size.width)
{
titleRect.size.width -= 3;
}
break; break;
case NSImageRight: case NSImageRight:
@ -906,14 +924,15 @@
if (_cell.is_bordered || _cell.is_bezeled) if (_cell.is_bordered || _cell.is_bezeled)
{ {
imageRect.origin.x -= 3; imageRect.origin.x -= 3;
imageRect.size.height -= 2;
imageRect.origin.y += 1;
} }
titleRect.origin = cellFrame.origin; titleRect.origin = cellFrame.origin;
titleRect.size.width = cellFrame.size.width - imageSize.width - xDist; titleRect.size.width = imageRect.origin.x - titleRect.origin.x - GSCellTextImageXDist;
titleRect.size.height = cellFrame.size.height; titleRect.size.height = cellFrame.size.height;
titleRect.origin.x += 3; if (titleSize.width + 3 <= titleRect.size.width)
titleRect.size.width -= 3; {
titleRect.origin.x += 3;
titleRect.size.width -= 3;
}
break; break;
case NSImageAbove: case NSImageAbove:
@ -923,26 +942,24 @@
* The drawing code below will then center the image in imageRect. * The drawing code below will then center the image in imageRect.
*/ */
titleRect.origin.x = cellFrame.origin.x; 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.width = cellFrame.size.width;
titleRect.size.height = titleSize.height; titleRect.size.height = titleSize.height;
imageRect.origin.x = cellFrame.origin.x; imageRect.origin.x = cellFrame.origin.x;
imageRect.origin.y = cellFrame.origin.y; imageRect.origin.y = NSMaxY(titleRect);
imageRect.origin.y += titleRect.size.height + yDist;
imageRect.size.width = cellFrame.size.width; imageRect.size.width = cellFrame.size.width;
imageRect.size.height = cellFrame.size.height; imageRect.size.height = NSMaxY(cellFrame) - imageRect.origin.y;
imageRect.size.height -= titleSize.height + yDist;
if (_cell.is_bordered || _cell.is_bezeled) if (_cell.is_bordered || _cell.is_bezeled)
{ {
imageRect.origin.x += 3; imageRect.origin.y -= 1;
imageRect.size.width -= 6; }
imageRect.size.height -= 1; if (titleSize.width + 6 <= titleRect.size.width)
{
titleRect.origin.x += 3;
titleRect.size.width -= 6;
} }
titleRect.origin.x += 3;
titleRect.origin.y += 4;
titleRect.size.width -= 6;
break; break;
case NSImageBelow: case NSImageBelow:
@ -952,33 +969,34 @@
* The drawing code below will then center the image in imageRect. * The drawing code below will then center the image in imageRect.
*/ */
titleRect.origin.x = cellFrame.origin.x; titleRect.origin.x = cellFrame.origin.x;
titleRect.origin.y = cellFrame.origin.y + cellFrame.size.height; titleRect.origin.y = NSMaxY(cellFrame) - titleSize.height;
titleRect.origin.y -= titleSize.height;
titleRect.size.width = cellFrame.size.width; titleRect.size.width = cellFrame.size.width;
titleRect.size.height = titleSize.height; titleRect.size.height = titleSize.height;
imageRect.origin.x = cellFrame.origin.x; imageRect.origin.x = cellFrame.origin.x;
imageRect.origin.y = cellFrame.origin.y; imageRect.origin.y = cellFrame.origin.y;
imageRect.size.width = cellFrame.size.width; imageRect.size.width = cellFrame.size.width;
imageRect.size.height = cellFrame.size.height; imageRect.size.height = titleRect.origin.y - GSCellTextImageYDist - imageRect.origin.y;
imageRect.size.height -= titleSize.height + yDist;
if (_cell.is_bordered || _cell.is_bezeled) if (_cell.is_bordered || _cell.is_bezeled)
{ {
imageRect.size.width -= 6; imageRect.origin.y += 1;
imageRect.origin.x += 3; }
imageRect.size.height -= 1; if (titleSize.width + 6 <= titleRect.size.width)
imageRect.origin.y += 1; {
titleRect.origin.x += 3;
titleRect.size.width -= 6;
} }
titleRect.size.width -= 6;
titleRect.origin.x += 3;
titleRect.size.height -= 4;
break; break;
case NSImageOverlaps: case NSImageOverlaps:
titleRect = cellFrame;
imageRect = cellFrame; imageRect = cellFrame;
// TODO: Add distance from border if needed titleRect = cellFrame;
if (titleSize.width + 6 <= titleRect.size.width)
{
titleRect.origin.x += 3;
titleRect.size.width -= 6;
}
break; break;
} }
@ -995,8 +1013,8 @@
NSPoint position; NSPoint position;
size = [imageToDisplay size]; size = [imageToDisplay size];
position.x = MAX(NSMidX(imageRect) - (size.width/2.),0.); position.x = MAX(NSMidX(imageRect) - (size.width / 2.), 0.);
position.y = MAX(NSMidY(imageRect) - (size.height/2.),0.); position.y = MAX(NSMidY(imageRect) - (size.height / 2.), 0.);
/* /*
* Images are always drawn with their bottom-left corner at the origin * 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. * so we must adjust the position to take account of a flipped view.
@ -1027,7 +1045,7 @@
if (_cell.shows_first_responder if (_cell.shows_first_responder
&& [[controlView window] firstResponder] == controlView) && [[controlView window] firstResponder] == controlView)
{ {
NSDottedFrameRect(cellFrame); NSDottedFrameRect(cellFrame);
} }
} }
@ -1038,24 +1056,23 @@
unsigned mask; unsigned mask;
NSImage *imageToDisplay; NSImage *imageToDisplay;
NSAttributedString *titleToDisplay; NSAttributedString *titleToDisplay;
NSSize imageSize; NSSize imageSize = NSZeroSize;
NSSize titleSize; NSSize titleSize = NSZeroSize;
/* /* The size calculations here must be kept in sync with
* The following code must be kept in sync with -drawInteriorWithFrame -drawInteriorWithFrame. */
*/
if (_cell.is_highlighted) if (_cell.is_highlighted)
{ {
mask = _highlightsByMask; mask = _highlightsByMask;
if (_cell.state)
mask &= ~_showAltStateMask;
} }
else if (_cell.state) else if (_cell.state)
{ mask = _showAltStateMask;
mask = _showAltStateMask;
}
else else
{ mask = NSNoCellMask;
mask = NSNoCellMask;
}
if (mask & NSContentsCellMask) if (mask & NSContentsCellMask)
{ {
@ -1080,19 +1097,11 @@
{ {
imageSize = [imageToDisplay size]; imageSize = [imageToDisplay size];
} }
else
{
imageSize = NSZeroSize;
}
if (titleToDisplay != nil) if (titleToDisplay != nil)
{ {
titleSize = [titleToDisplay size]; titleSize = [titleToDisplay size];
} }
else
{
titleSize = NSZeroSize;
}
switch (_cell.image_position) switch (_cell.image_position)
{ {
@ -1106,57 +1115,39 @@
case NSImageLeft: case NSImageLeft:
case NSImageRight: case NSImageRight:
s.width = imageSize.width + titleSize.width + xDist; s.width = imageSize.width + titleSize.width + GSCellTextImageXDist;
if (imageSize.height > titleSize.height) s.height = MAX(imageSize.height, titleSize.height);
s.height = imageSize.height;
else
s.height = titleSize.height;
break; break;
case NSImageBelow: case NSImageBelow:
case NSImageAbove: case NSImageAbove:
if (imageSize.width > titleSize.width) s.width = MAX(imageSize.width, titleSize.width);
s.width = imageSize.width; s.height = imageSize.height + titleSize.height + GSCellTextImageYDist;
else
s.width = titleSize.width;
s.height = imageSize.height + titleSize.height; // + yDist ??
break; break;
case NSImageOverlaps: case NSImageOverlaps:
if (imageSize.width > titleSize.width) s.width = MAX(imageSize.width, titleSize.width);
s.width = imageSize.width; s.height = MAX(imageSize.height, titleSize.height);
else
s.width = titleSize.width;
if (imageSize.height > titleSize.height)
s.height = imageSize.height;
else
s.height = titleSize.height;
break; break;
} }
// Get border size // Get border size
if (_cell.is_bordered) if (_cell.is_bordered)
// Buttons only have three paths for border (NeXT looks) // Buttons only have three paths for border (NeXT looks)
borderSize = NSMakeSize (1.5, 1.5); borderSize = NSMakeSize (3.0, 3.0);
else else
borderSize = NSZeroSize; borderSize = NSZeroSize;
if ((_cell.is_bordered || _cell.is_bezeled) if ((_cell.is_bordered && (_cell.image_position != NSImageOnly))
&& (_cell.image_position != NSImageOnly)) || _cell.is_bezeled)
{ {
borderSize.height += 1; borderSize.width += 6;
borderSize.width += 3; borderSize.height += 6;
/* Add some more space because it looks better. */
borderSize.height += 2;
borderSize.width += 3;
} }
// Add border size // Add border size
s.width += 2 * borderSize.width; s.width += borderSize.width;
s.height += 2 * borderSize.height; s.height += borderSize.height;
return s; return s;
} }

View file

@ -276,7 +276,7 @@ static NSImage *arrowImage = nil; /* Cache arrow image. */
break; break;
case NSImageRight: case NSImageRight:
cellFrame.origin.x += _titleWidth + xDist; cellFrame.origin.x += _titleWidth + GSCellTextImageXDist;
cellFrame.size.width = _imageWidth; cellFrame.size.width = _imageWidth;
break; break;
@ -313,40 +313,40 @@ static NSImage *arrowImage = nil; /* Cache arrow image. */
- (NSRect) titleRectForBounds:(NSRect)cellFrame - (NSRect) titleRectForBounds:(NSRect)cellFrame
{ {
// Calculate the image part of cell frame from NSMenuView // Calculate the image part of cell frame from NSMenuView
cellFrame.origin.x += [_menuView imageAndTitleOffset]; cellFrame.origin.x += [_menuView imageAndTitleOffset];
cellFrame.size.width = [_menuView imageAndTitleWidth]; cellFrame.size.width = [_menuView imageAndTitleWidth];
switch (_cell.image_position) switch (_cell.image_position)
{ {
case NSNoImage: case NSNoImage:
case NSImageOverlaps: case NSImageOverlaps:
break; break;
case NSImageOnly: case NSImageOnly:
cellFrame = NSZeroRect; cellFrame = NSZeroRect;
break; break;
case NSImageLeft: case NSImageLeft:
cellFrame.origin.x += _imageWidth + xDist; cellFrame.origin.x += _imageWidth + GSCellTextImageXDist;
cellFrame.size.width = _titleWidth; cellFrame.size.width = _titleWidth;
break; break;
case NSImageRight: case NSImageRight:
cellFrame.size.width = _titleWidth; cellFrame.size.width = _titleWidth;
break; break;
case NSImageBelow: case NSImageBelow:
cellFrame.size.height /= 2; cellFrame.size.height /= 2;
cellFrame.origin.y += cellFrame.size.height; cellFrame.origin.y += cellFrame.size.height;
break; break;
case NSImageAbove: case NSImageAbove:
cellFrame.size.height /= 2; cellFrame.size.height /= 2;
break; break;
} }
return cellFrame; return cellFrame;
} }
// //

View file

@ -555,7 +555,7 @@ _addLeftBorderOffsetToRect(NSRect aRect)
case NSImageLeft: case NSImageLeft:
case NSImageRight: case NSImageRight:
anImageAndTitleWidth = anImageWidth + aTitleWidth + xDist; anImageAndTitleWidth = anImageWidth + aTitleWidth + GSCellTextImageXDist;
break; break;
case NSImageBelow: case NSImageBelow: