libs-gui/Source/NSButtonCell.m

793 lines
19 KiB
Mathematica
Raw Normal View History

/*
NSButtonCell.m
The button cell class
Copyright (C) 1996-1999 Free Software Foundation, Inc.
Author: Scott Christley <scottc@net-community.com>
Ovidiu Predescu <ovidiu@net-community.com>
Date: 1996
* NSApplication.h added windows_need_update as autodisplay ivar. * NSApplication.m in init set default for main_menu, windows_need_update. * NSApplication.m in run added support for OPENSTEP autodisplay mechanism. * NSApplication.m _eventMatchingMask: reformat. * NSApplication.m nextEventMatchingMask:untilDate:inMode:dequeue: move calls to _flushWindows to XDPS backend. * NSApplication.m implemented updateWindows. * NSApplication.m implemented setWindowsNeedUpdate:. * NSApplication.m setMainMenu: test for menu prior to release. * NSActionCell.m removed setState: method. * NSActionCell.m copyWithZone: optimized ivar copying. * NSButtonCell.m added _init and moved common defaults to this method. * NSButtonCell.m initImageCell: and initTextCell: minor optimizations. * NSButtonCell.m setAlternateTitle: optimize and use ASSIGN macro. * NSButtonCell.m setKeyEquivalent: remove [copy release]. * NSButtonCell.m setKeyEquivalentFont:size: optimize. * NSButtonCell.m copyWithZone: optimized ivar copying where possible. * NSCell.m _init removed call to [super init]. * NSCell.m initTextCell: removed duplicate default initializers. * NSCell.m setImage: rewrote to use ASSIGN macro. * NSCell.m setDoubleValue: rewrote to use ASSIGN macro. * NSCell.m setFloatValue: rewrote to use ASSIGN macro. * NSCell.m setIntValue: rewrote to use ASSIGN macro. * NSCell.m setStringValue: rewrote to use ASSIGN macro. * NSCell.m setFont: rewrote to use ASSIGN macro. * NSCell.m setRepresentedObject: rewrote to use ASSIGN macro. * NSCell.m copyWithZone: optimized ivar copying where possible. * NSClipView.m viewBoundsChanged: minor optimization. * NSClipView.m viewFrameChanged: minor optimization. * NSClipView.m scaleUnitSquareToSize: minor optimization. * NSClipView.m setBoundsOrigin: minor optimization. * NSClipView.m setBoundsSize: minor optimization. * NSClipView.m setFrameSize: minor optimization. * NSClipView.m setFrameOrigin: minor optimization. * NSClipView.m setFrame: minor optimization. * NSClipView.m translateOriginToPoint: minor optimization. * NSMatrix.m eliminate retain/release of selected cell via ASSIGN. * NSMatrix.m initWithFrame: remove duplicate setting of selected row and column. * NSMatrix.m removeColumn: rewrite so that col is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m removeRow: rewrite so that row is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m deselectAllCells per OS spec try to select a cell at end if empty selection is not allowed. * NSMatrix.m deselectSelectedCell set default selected row/col to 0. * NSMatrix.m mouseDown: optimize for new drawing behaviour. * NSMenu.m setSelectedCell: eliminate use of retain/release of selected cell via ASSIGN. * NSMenuItem.m copyWithZone: optimized ivar copying where possible. * NSMenuItem.m dealloc minor optimization. * NSMenuItem.m setTarget: minor optimization. * NSScroller.m trackScrollButtons: heavily optimized. * NSScroller.m sendAction: reimplemented to use sendAction: per spec also optimized. * NSText.m implemented init method. * NSText.m initWithFrame: fixed initialization of ivars which are released to retain. * NSText.m dealloc add release of retained ivars. * NSView.m dealloc add release of retained ivars. * NSView.m displayRect: and _addSubviewForNeedingDisplay reformatted code and rewrote comments. * NSWindow.m setFrame:display: rewrote for clarity. * NSWindow.m implemented update method per OPENSTEP spec. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@2941 72102866-910b-0410-8b05-ffd578937521
1998-08-30 16:06:47 +00:00
Author: Felipe A. Rodriguez <far@ix.netcom.com>
Date: August 1998
Modified: Richard Frith-Macdonald <richard@brainstorm.co.uk>
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <gnustep/gui/config.h>
#include <Foundation/NSLock.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSString.h>
#include <Foundation/NSException.h>
#include <AppKit/NSButtonCell.h>
#include <AppKit/NSButton.h>
#include <AppKit/NSWindow.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSApplication.h>
#include <AppKit/NSFont.h>
#include <AppKit/NSImage.h>
#include <AppKit/NSColor.h>
#include <AppKit/NSGraphics.h>
#include <AppKit/PSOperators.h>
@implementation NSButtonCell
//
// Class methods
//
+ (void) initialize
{
if (self == [NSButtonCell class])
[self setVersion: 1];
}
//
// Instance methods
//
- (id) _init
* NSApplication.h added windows_need_update as autodisplay ivar. * NSApplication.m in init set default for main_menu, windows_need_update. * NSApplication.m in run added support for OPENSTEP autodisplay mechanism. * NSApplication.m _eventMatchingMask: reformat. * NSApplication.m nextEventMatchingMask:untilDate:inMode:dequeue: move calls to _flushWindows to XDPS backend. * NSApplication.m implemented updateWindows. * NSApplication.m implemented setWindowsNeedUpdate:. * NSApplication.m setMainMenu: test for menu prior to release. * NSActionCell.m removed setState: method. * NSActionCell.m copyWithZone: optimized ivar copying. * NSButtonCell.m added _init and moved common defaults to this method. * NSButtonCell.m initImageCell: and initTextCell: minor optimizations. * NSButtonCell.m setAlternateTitle: optimize and use ASSIGN macro. * NSButtonCell.m setKeyEquivalent: remove [copy release]. * NSButtonCell.m setKeyEquivalentFont:size: optimize. * NSButtonCell.m copyWithZone: optimized ivar copying where possible. * NSCell.m _init removed call to [super init]. * NSCell.m initTextCell: removed duplicate default initializers. * NSCell.m setImage: rewrote to use ASSIGN macro. * NSCell.m setDoubleValue: rewrote to use ASSIGN macro. * NSCell.m setFloatValue: rewrote to use ASSIGN macro. * NSCell.m setIntValue: rewrote to use ASSIGN macro. * NSCell.m setStringValue: rewrote to use ASSIGN macro. * NSCell.m setFont: rewrote to use ASSIGN macro. * NSCell.m setRepresentedObject: rewrote to use ASSIGN macro. * NSCell.m copyWithZone: optimized ivar copying where possible. * NSClipView.m viewBoundsChanged: minor optimization. * NSClipView.m viewFrameChanged: minor optimization. * NSClipView.m scaleUnitSquareToSize: minor optimization. * NSClipView.m setBoundsOrigin: minor optimization. * NSClipView.m setBoundsSize: minor optimization. * NSClipView.m setFrameSize: minor optimization. * NSClipView.m setFrameOrigin: minor optimization. * NSClipView.m setFrame: minor optimization. * NSClipView.m translateOriginToPoint: minor optimization. * NSMatrix.m eliminate retain/release of selected cell via ASSIGN. * NSMatrix.m initWithFrame: remove duplicate setting of selected row and column. * NSMatrix.m removeColumn: rewrite so that col is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m removeRow: rewrite so that row is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m deselectAllCells per OS spec try to select a cell at end if empty selection is not allowed. * NSMatrix.m deselectSelectedCell set default selected row/col to 0. * NSMatrix.m mouseDown: optimize for new drawing behaviour. * NSMenu.m setSelectedCell: eliminate use of retain/release of selected cell via ASSIGN. * NSMenuItem.m copyWithZone: optimized ivar copying where possible. * NSMenuItem.m dealloc minor optimization. * NSMenuItem.m setTarget: minor optimization. * NSScroller.m trackScrollButtons: heavily optimized. * NSScroller.m sendAction: reimplemented to use sendAction: per spec also optimized. * NSText.m implemented init method. * NSText.m initWithFrame: fixed initialization of ivars which are released to retain. * NSText.m dealloc add release of retained ivars. * NSView.m dealloc add release of retained ivars. * NSView.m displayRect: and _addSubviewForNeedingDisplay reformatted code and rewrote comments. * NSWindow.m setFrame:display: rewrote for clarity. * NSWindow.m implemented update method per OPENSTEP spec. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@2941 72102866-910b-0410-8b05-ffd578937521
1998-08-30 16:06:47 +00:00
{
_cell.is_enabled = YES;
_buttoncell_is_transparent = NO;
_cell.is_bordered = YES;
_showAltStateMask = NSNoCellMask; // configure as a NSMomentaryPushButton
_highlightsByMask = NSPushInCellMask | NSChangeGrayCellMask;
_delayInterval = 0.4;
_repeatInterval = 0.075;
_altContents = nil;
_keyEquivalentModifierMask = NSCommandKeyMask;
return self;
* NSApplication.h added windows_need_update as autodisplay ivar. * NSApplication.m in init set default for main_menu, windows_need_update. * NSApplication.m in run added support for OPENSTEP autodisplay mechanism. * NSApplication.m _eventMatchingMask: reformat. * NSApplication.m nextEventMatchingMask:untilDate:inMode:dequeue: move calls to _flushWindows to XDPS backend. * NSApplication.m implemented updateWindows. * NSApplication.m implemented setWindowsNeedUpdate:. * NSApplication.m setMainMenu: test for menu prior to release. * NSActionCell.m removed setState: method. * NSActionCell.m copyWithZone: optimized ivar copying. * NSButtonCell.m added _init and moved common defaults to this method. * NSButtonCell.m initImageCell: and initTextCell: minor optimizations. * NSButtonCell.m setAlternateTitle: optimize and use ASSIGN macro. * NSButtonCell.m setKeyEquivalent: remove [copy release]. * NSButtonCell.m setKeyEquivalentFont:size: optimize. * NSButtonCell.m copyWithZone: optimized ivar copying where possible. * NSCell.m _init removed call to [super init]. * NSCell.m initTextCell: removed duplicate default initializers. * NSCell.m setImage: rewrote to use ASSIGN macro. * NSCell.m setDoubleValue: rewrote to use ASSIGN macro. * NSCell.m setFloatValue: rewrote to use ASSIGN macro. * NSCell.m setIntValue: rewrote to use ASSIGN macro. * NSCell.m setStringValue: rewrote to use ASSIGN macro. * NSCell.m setFont: rewrote to use ASSIGN macro. * NSCell.m setRepresentedObject: rewrote to use ASSIGN macro. * NSCell.m copyWithZone: optimized ivar copying where possible. * NSClipView.m viewBoundsChanged: minor optimization. * NSClipView.m viewFrameChanged: minor optimization. * NSClipView.m scaleUnitSquareToSize: minor optimization. * NSClipView.m setBoundsOrigin: minor optimization. * NSClipView.m setBoundsSize: minor optimization. * NSClipView.m setFrameSize: minor optimization. * NSClipView.m setFrameOrigin: minor optimization. * NSClipView.m setFrame: minor optimization. * NSClipView.m translateOriginToPoint: minor optimization. * NSMatrix.m eliminate retain/release of selected cell via ASSIGN. * NSMatrix.m initWithFrame: remove duplicate setting of selected row and column. * NSMatrix.m removeColumn: rewrite so that col is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m removeRow: rewrite so that row is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m deselectAllCells per OS spec try to select a cell at end if empty selection is not allowed. * NSMatrix.m deselectSelectedCell set default selected row/col to 0. * NSMatrix.m mouseDown: optimize for new drawing behaviour. * NSMenu.m setSelectedCell: eliminate use of retain/release of selected cell via ASSIGN. * NSMenuItem.m copyWithZone: optimized ivar copying where possible. * NSMenuItem.m dealloc minor optimization. * NSMenuItem.m setTarget: minor optimization. * NSScroller.m trackScrollButtons: heavily optimized. * NSScroller.m sendAction: reimplemented to use sendAction: per spec also optimized. * NSText.m implemented init method. * NSText.m initWithFrame: fixed initialization of ivars which are released to retain. * NSText.m dealloc add release of retained ivars. * NSView.m dealloc add release of retained ivars. * NSView.m displayRect: and _addSubviewForNeedingDisplay reformatted code and rewrote comments. * NSWindow.m setFrame:display: rewrote for clarity. * NSWindow.m implemented update method per OPENSTEP spec. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@2941 72102866-910b-0410-8b05-ffd578937521
1998-08-30 16:06:47 +00:00
}
- (id) init
{
[self initTextCell: @"Button"];
return self;
}
- (id) initImageCell: (NSImage *)anImage
{
[super initImageCell: anImage];
_contents = nil;
return [self _init];
}
- (id) initTextCell: (NSString *)aString
{
[super initTextCell: aString];
return [self _init];
}
- (void) dealloc
{
[_altContents release];
[_altImage release];
[_keyEquivalent release];
[_keyEquivalentFont release];
[super dealloc];
}
//
// Setting the Titles
//
- (NSString *) title
{
return [self stringValue];
}
- (NSString *) alternateTitle
{
return _altContents;
}
- (void) setFont: (NSFont *)fontObject
{
[super setFont: fontObject];
}
- (void) setTitle: (NSString *)aString
{
[self setStringValue: aString];
}
- (void) setAlternateTitle: (NSString *)aString
{
NSString* string = [aString copy];
* NSApplication.h added windows_need_update as autodisplay ivar. * NSApplication.m in init set default for main_menu, windows_need_update. * NSApplication.m in run added support for OPENSTEP autodisplay mechanism. * NSApplication.m _eventMatchingMask: reformat. * NSApplication.m nextEventMatchingMask:untilDate:inMode:dequeue: move calls to _flushWindows to XDPS backend. * NSApplication.m implemented updateWindows. * NSApplication.m implemented setWindowsNeedUpdate:. * NSApplication.m setMainMenu: test for menu prior to release. * NSActionCell.m removed setState: method. * NSActionCell.m copyWithZone: optimized ivar copying. * NSButtonCell.m added _init and moved common defaults to this method. * NSButtonCell.m initImageCell: and initTextCell: minor optimizations. * NSButtonCell.m setAlternateTitle: optimize and use ASSIGN macro. * NSButtonCell.m setKeyEquivalent: remove [copy release]. * NSButtonCell.m setKeyEquivalentFont:size: optimize. * NSButtonCell.m copyWithZone: optimized ivar copying where possible. * NSCell.m _init removed call to [super init]. * NSCell.m initTextCell: removed duplicate default initializers. * NSCell.m setImage: rewrote to use ASSIGN macro. * NSCell.m setDoubleValue: rewrote to use ASSIGN macro. * NSCell.m setFloatValue: rewrote to use ASSIGN macro. * NSCell.m setIntValue: rewrote to use ASSIGN macro. * NSCell.m setStringValue: rewrote to use ASSIGN macro. * NSCell.m setFont: rewrote to use ASSIGN macro. * NSCell.m setRepresentedObject: rewrote to use ASSIGN macro. * NSCell.m copyWithZone: optimized ivar copying where possible. * NSClipView.m viewBoundsChanged: minor optimization. * NSClipView.m viewFrameChanged: minor optimization. * NSClipView.m scaleUnitSquareToSize: minor optimization. * NSClipView.m setBoundsOrigin: minor optimization. * NSClipView.m setBoundsSize: minor optimization. * NSClipView.m setFrameSize: minor optimization. * NSClipView.m setFrameOrigin: minor optimization. * NSClipView.m setFrame: minor optimization. * NSClipView.m translateOriginToPoint: minor optimization. * NSMatrix.m eliminate retain/release of selected cell via ASSIGN. * NSMatrix.m initWithFrame: remove duplicate setting of selected row and column. * NSMatrix.m removeColumn: rewrite so that col is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m removeRow: rewrite so that row is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m deselectAllCells per OS spec try to select a cell at end if empty selection is not allowed. * NSMatrix.m deselectSelectedCell set default selected row/col to 0. * NSMatrix.m mouseDown: optimize for new drawing behaviour. * NSMenu.m setSelectedCell: eliminate use of retain/release of selected cell via ASSIGN. * NSMenuItem.m copyWithZone: optimized ivar copying where possible. * NSMenuItem.m dealloc minor optimization. * NSMenuItem.m setTarget: minor optimization. * NSScroller.m trackScrollButtons: heavily optimized. * NSScroller.m sendAction: reimplemented to use sendAction: per spec also optimized. * NSText.m implemented init method. * NSText.m initWithFrame: fixed initialization of ivars which are released to retain. * NSText.m dealloc add release of retained ivars. * NSView.m dealloc add release of retained ivars. * NSView.m displayRect: and _addSubviewForNeedingDisplay reformatted code and rewrote comments. * NSWindow.m setFrame:display: rewrote for clarity. * NSWindow.m implemented update method per OPENSTEP spec. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@2941 72102866-910b-0410-8b05-ffd578937521
1998-08-30 16:06:47 +00:00
ASSIGN(_altContents, string);
[string release];
if (_control_view)
if ([_control_view isKindOfClass: [NSControl class]])
[(NSControl *)_control_view updateCell: self];
}
//
// Setting the Images
//
- (NSImage *) alternateImage
{
return _altImage;
}
- (NSCellImagePosition) imagePosition
{
return _cell.image_position;
}
- (void) setAlternateImage: (NSImage *)anImage
{
ASSIGN(_altImage, anImage);
}
- (void) setImagePosition: (NSCellImagePosition)aPosition
{
_cell.image_position = aPosition;
}
//
// Setting the Repeat Interval
//
- (void) getPeriodicDelay: (float *)delay interval: (float *)interval
{
*delay = _delayInterval;
*interval = _repeatInterval;
}
- (void) setPeriodicDelay: (float)delay interval: (float)interval
{
_delayInterval = delay;
_repeatInterval = interval;
[self setContinuous: YES];
}
//
// Setting the Key Equivalent
//
- (NSString*) keyEquivalent
{
return _keyEquivalent;
}
- (NSFont*) keyEquivalentFont
{
return _keyEquivalentFont;
}
- (unsigned int) keyEquivalentModifierMask
{
return _keyEquivalentModifierMask;
}
- (void) setKeyEquivalent: (NSString*)key
{
if (_keyEquivalent != key)
{
[_keyEquivalent release];
_keyEquivalent = [key copy];
}
}
- (void) setKeyEquivalentModifierMask: (unsigned int)mask
{
_keyEquivalentModifierMask = mask;
}
- (void) setKeyEquivalentFont: (NSFont*)fontObj
{
ASSIGN(_keyEquivalentFont, fontObj);
}
- (void) setKeyEquivalentFont: (NSString*)fontName size: (float)fontSize
{
ASSIGN(_keyEquivalentFont, [NSFont fontWithName: fontName size: fontSize]);
}
//
// Modifying Graphic Attributes
//
- (BOOL) isTransparent
{
return _buttoncell_is_transparent;
}
- (void) setTransparent: (BOOL)flag
{
_buttoncell_is_transparent = flag;
}
- (BOOL) isOpaque
{
// MacOS-X says we should return !transparent && [self isBordered],
// but that's wrong in our case, since if there is no border,
// we draw the interior of the cell to fill completely the bounds.
// They are likely to draw differently.
return !_buttoncell_is_transparent;
}
//
// Modifying Graphic Attributes
//
- (int) highlightsBy
{
return _highlightsByMask;
}
- (void) setHighlightsBy: (int)mask
{
_highlightsByMask = mask;
}
- (void) setShowsStateBy: (int)mask
{
_showAltStateMask = mask;
}
- (void) setButtonType: (NSButtonType)buttonType
{
_cell.type = buttonType;
switch (buttonType)
{
case NSMomentaryLight:
[self setHighlightsBy: NSChangeBackgroundCellMask];
[self setShowsStateBy: NSNoCellMask];
break;
case NSMomentaryPushButton:
[self setHighlightsBy: NSPushInCellMask | NSChangeGrayCellMask];
[self setShowsStateBy: NSNoCellMask];
break;
case NSMomentaryChangeButton:
[self setHighlightsBy: NSContentsCellMask];
[self setShowsStateBy: NSNoCellMask];
break;
case NSPushOnPushOffButton:
[self setHighlightsBy: NSPushInCellMask | NSChangeGrayCellMask];
[self setShowsStateBy: NSChangeBackgroundCellMask];
break;
case NSOnOffButton:
[self setHighlightsBy: NSChangeBackgroundCellMask];
[self setShowsStateBy: NSChangeBackgroundCellMask];
break;
case NSToggleButton:
[self setHighlightsBy: NSPushInCellMask | NSContentsCellMask];
[self setShowsStateBy: NSContentsCellMask];
break;
case NSSwitchButton:
[self setHighlightsBy: NSContentsCellMask];
[self setShowsStateBy: NSContentsCellMask];
[self setImage: [NSImage imageNamed: @"common_SwitchOff"]];
[self setAlternateImage: [NSImage imageNamed: @"common_SwitchOn"]];
[self setImagePosition: NSImageLeft];
[self setAlignment: NSLeftTextAlignment];
break;
case NSRadioButton:
[self setHighlightsBy: NSContentsCellMask];
[self setShowsStateBy: NSContentsCellMask];
[self setImage: [NSImage imageNamed: @"common_RadioOff"]];
[self setAlternateImage: [NSImage imageNamed: @"common_RadioOn"]];
[self setImagePosition: NSImageLeft];
[self setAlignment: NSLeftTextAlignment];
break;
}
}
- (int) showsStateBy
{
return _showAltStateMask;
}
- (void) setIntValue: (int)anInt
{
[self setState: (anInt != 0)];
}
- (void) setFloatValue: (float)aFloat
{
[self setState: (aFloat != 0)];
}
- (void) setDoubleValue: (double)aDouble
{
[self setState: (aDouble != 0)];
}
- (int) intValue
{
return _cell.state;
}
- (float) floatValue
{
return _cell.state;
}
- (double) doubleValue
{
return _cell.state;
}
//
// Displaying
//
- (NSColor *) textColor
{
if (_cell.is_enabled == NO)
return [NSColor disabledControlTextColor];
if ((_cell.state && (_showAltStateMask & NSChangeGrayCellMask))
|| (_cell.is_highlighted && (_highlightsByMask & NSChangeGrayCellMask)))
return [NSColor selectedControlTextColor];
return [NSColor controlTextColor];
}
- (void) drawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
// Save last view drawn to
if (_control_view != controlView)
_control_view = controlView;
// transparent buttons never draw
if (_buttoncell_is_transparent)
return;
// do nothing if cell's frame rect is zero
if (NSIsEmptyRect(cellFrame))
return;
// draw the border if needed
if (_cell.is_bordered)
{
[controlView lockFocus];
if (_cell.is_highlighted && (_highlightsByMask & NSPushInCellMask))
{
NSDrawGrayBezel(cellFrame, NSZeroRect);
}
else
{
NSDrawButton(cellFrame, NSZeroRect);
}
[controlView unlockFocus];
}
[self drawInteriorWithFrame: cellFrame inView: controlView];
}
- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
BOOL showAlternate = NO;
unsigned mask;
NSImage *imageToDisplay;
NSRect imageRect;
NSString *titleToDisplay;
NSRect titleRect;
NSSize imageSize = {0, 0};
NSColor *backgroundColor = nil;
BOOL flippedView = [controlView isFlipped];
// transparent buttons never draw
if (_buttoncell_is_transparent)
return;
_control_view = controlView;
cellFrame = [self drawingRectForBounds: cellFrame];
[controlView lockFocus];
// pushed in buttons contents are displaced to the bottom right 1px
if (_cell.is_bordered && _cell.is_highlighted
&& (_highlightsByMask & NSPushInCellMask))
{
cellFrame = NSOffsetRect(cellFrame, 1., flippedView ? 1. : -1.);
}
// determine the background color
if (_cell.state)
{
if (_showAltStateMask
& (NSChangeGrayCellMask | NSChangeBackgroundCellMask))
{
backgroundColor = [NSColor selectedControlColor];
}
}
if (_cell.is_highlighted)
{
if (_highlightsByMask
& (NSChangeGrayCellMask | NSChangeBackgroundCellMask))
{
backgroundColor = [NSColor selectedControlColor];
}
}
if (backgroundColor == nil)
backgroundColor = [NSColor controlBackgroundColor];
// set cell's background color
[backgroundColor set];
NSRectFill(cellFrame);
/*
* Determine the image and the title that will be
* displayed. If the NSContentsCellMask is set the
* image and title are swapped only if state is 1 or
* if highlighting is set (when a button is pushed it's
* content is changed to the face of reversed state).
*/
if (_cell.is_highlighted)
mask = _highlightsByMask;
else
mask = _showAltStateMask;
if (mask & NSContentsCellMask)
showAlternate = _cell.state;
if (showAlternate || _cell.is_highlighted)
{
imageToDisplay = _altImage;
if (!imageToDisplay)
imageToDisplay = _cell_image;
titleToDisplay = _altContents;
if (titleToDisplay == nil || [titleToDisplay isEqual: @""])
titleToDisplay = _contents;
}
else
{
imageToDisplay = _cell_image;
titleToDisplay = _contents;
}
if (imageToDisplay)
{
imageSize = [imageToDisplay size];
[imageToDisplay setBackgroundColor: backgroundColor];
}
switch (_cell.image_position)
{
case NSNoImage:
imageToDisplay = nil;
titleRect = cellFrame;
break;
case NSImageOnly:
titleToDisplay = nil;
imageRect = cellFrame;
break;
case NSImageLeft:
imageRect.origin = cellFrame.origin;
imageRect.size.width = imageSize.width;
imageRect.size.height = cellFrame.size.height;
titleRect = imageRect;
titleRect.origin.x += imageSize.width + xDist;
titleRect.size.width = cellFrame.size.width - imageSize.width - xDist;
break;
case NSImageRight:
imageRect.origin.x = NSMaxX(cellFrame) - imageSize.width;
imageRect.origin.y = cellFrame.origin.y;
imageRect.size.width = imageSize.width;
imageRect.size.height = cellFrame.size.height;
titleRect.origin = cellFrame.origin;
titleRect.size.width = cellFrame.size.width - imageSize.width - xDist;
titleRect.size.height = cellFrame.size.height;
break;
case NSImageAbove:
imageRect.origin.x = cellFrame.origin.x;
imageRect.size.width = cellFrame.size.width;
imageRect.size.height = imageSize.height;
titleRect.origin.x = cellFrame.origin.x;
titleRect.size.width = cellFrame.size.width;
titleRect.size.height = cellFrame.size.height - imageSize.height
- yDist;
if (flippedView)
{
imageRect.origin.y = NSMinY(cellFrame);
titleRect.origin.y = NSMaxY(cellFrame) - titleRect.size.height;
}
else
{
imageRect.origin.y = NSMaxY(cellFrame) - imageRect.size.height;
titleRect.origin.y = NSMinY(cellFrame);
}
break;
case NSImageBelow:
imageRect.origin.x = cellFrame.origin.x;
imageRect.size.width = cellFrame.size.width;
imageRect.size.height = imageSize.height;
titleRect.origin.x = cellFrame.origin.x;
titleRect.size.width = cellFrame.size.width;
titleRect.size.height = cellFrame.size.height - imageSize.height
- yDist;
if (flippedView)
{
imageRect.origin.y = NSMaxY(cellFrame) - imageRect.size.height;
titleRect.origin.y = NSMinY(cellFrame);
}
else
{
imageRect.origin.y = NSMinY(cellFrame);
titleRect.origin.y = NSMaxY(cellFrame) - titleRect.size.height;
}
break;
case NSImageOverlaps:
titleRect = cellFrame;
imageRect = cellFrame;
break;
}
if (imageToDisplay != nil)
{
NSSize size;
NSPoint position;
size = [imageToDisplay size];
position.x = MAX(NSMidX(imageRect) - (size.width/2.),0.);
position.y = MAX(NSMidY(imageRect) - (size.height/2.),0.);
/*
* 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.
*/
if (flippedView)
{
position.y += size.height;
}
[imageToDisplay compositeToPoint: position operation: NSCompositeCopy];
}
if (titleToDisplay != nil)
{
[self _drawText: titleToDisplay inFrame: titleRect];
}
[controlView unlockFocus];
}
- (NSSize) cellSize
{
NSSize s;
NSSize borderSize;
BOOL showAlternate = NO;
unsigned mask;
NSImage *imageToDisplay;
NSString *titleToDisplay;
NSSize imageSize;
NSSize titleSize;
//
// The following code must be kept in sync with -drawInteriorWithFrame
//
if (_cell.is_highlighted)
mask = _highlightsByMask;
else
mask = _showAltStateMask;
if (mask & NSContentsCellMask)
showAlternate = _cell.state;
if (showAlternate || _cell.is_highlighted)
{
imageToDisplay = _altImage;
if (!imageToDisplay)
imageToDisplay = _cell_image;
titleToDisplay = _altContents;
if (titleToDisplay == nil || [titleToDisplay isEqual: @""])
titleToDisplay = _contents;
}
else
{
imageToDisplay = _cell_image;
titleToDisplay = _contents;
}
if (imageToDisplay)
imageSize = [imageToDisplay size];
else
imageSize = NSZeroSize;
if (titleToDisplay)
titleSize = NSMakeSize ([_cell_font widthOfString: titleToDisplay],
[_cell_font pointSize]);
else
titleSize = NSZeroSize;
switch (_cell.image_position)
{
case NSNoImage:
s = titleSize;
break;
case NSImageOnly:
s = imageSize;
break;
case NSImageLeft:
case NSImageRight:
s.width = imageSize.width + titleSize.width + xDist;
if (imageSize.height > titleSize.height)
s.height = imageSize.height;
else
s.height = titleSize.height;
break;
case NSImageBelow:
case NSImageAbove:
if (imageSize.width > titleSize.width)
s.height = imageSize.width;
else
s.width = titleSize.width;
s.height = imageSize.height + titleSize.height; // + yDist ??
break;
case NSImageOverlaps:
if (imageSize.width > titleSize.width)
s.width = imageSize.width;
else
s.width = titleSize.width;
if (imageSize.height > titleSize.height)
s.height = imageSize.height;
else
s.height = titleSize.height;
break;
}
// Add some spacing between text/image and border
// if there is text in the button
if (titleToDisplay) {
s.width += 2 * xDist;
s.height += 2 * yDist;
}
// Get border size
if (_cell.is_bordered)
// Buttons only have three paths for border (NeXT looks)
borderSize = NSMakeSize (1.5, 1.5);
else
borderSize = NSZeroSize;
// Add border size
s.width += 2 * borderSize.width;
s.height += 2 * borderSize.height;
return s;
}
- (NSRect) drawingRectForBounds: (NSRect)theRect
{
if (_cell.is_bordered)
{
// Special case: Buttons have only three different paths for border.
// One white path at the top left corner, one black path at the
// bottom right and another in dark gray at the inner bottom right.
float yDelta = [_control_view isFlipped] ? 1. : 2.;
return NSMakeRect (theRect.origin.x + 1.,
theRect.origin.y + yDelta,
theRect.size.width - 3.,
theRect.size.height - 3.);
}
else
return theRect;
}
//
// NSCopying protocol
//
- (id) copyWithZone: (NSZone*)zone
{
NSButtonCell *c = [super copyWithZone: zone];
c->_altContents = [_altContents copyWithZone: zone];
if (_altImage)
c->_altImage = [_altImage retain];
c->_keyEquivalent = [_keyEquivalent copyWithZone: zone];
if (_keyEquivalentFont)
c->_keyEquivalentFont = [_keyEquivalentFont retain];
c->_keyEquivalentModifierMask = _keyEquivalentModifierMask;
c->_buttoncell_is_transparent = _buttoncell_is_transparent;
c->_highlightsByMask = _highlightsByMask;
c->_showAltStateMask = _showAltStateMask;
return c;
}
//
// NSCoding protocol
//
- (void) encodeWithCoder: (NSCoder*)aCoder
{
BOOL tmp;
[super encodeWithCoder: aCoder];
NSDebugLog(@"NSButtonCell: start encoding\n");
[aCoder encodeObject: _keyEquivalent];
[aCoder encodeObject: _keyEquivalentFont];
[aCoder encodeObject: _altContents];
[aCoder encodeObject: _altImage];
tmp = _buttoncell_is_transparent;
[aCoder encodeValueOfObjCType: @encode(BOOL)
at: &tmp];
[aCoder encodeValueOfObjCType: @encode(unsigned int)
at: &_keyEquivalentModifierMask];
[aCoder encodeValueOfObjCType: @encode(unsigned int)
at: &_highlightsByMask];
[aCoder encodeValueOfObjCType: @encode(unsigned int)
at: &_showAltStateMask];
NSDebugLog(@"NSButtonCell: finish encoding\n");
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
BOOL tmp;
[super initWithCoder: aDecoder];
NSDebugLog(@"NSButtonCell: start decoding\n");
[aDecoder decodeValueOfObjCType: @encode(id) at: &_keyEquivalent];
[aDecoder decodeValueOfObjCType: @encode(id) at: &_keyEquivalentFont];
[aDecoder decodeValueOfObjCType: @encode(id) at: &_altContents];
[aDecoder decodeValueOfObjCType: @encode(id) at: &_altImage];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &tmp];
_buttoncell_is_transparent = tmp;
[aDecoder decodeValueOfObjCType: @encode(unsigned int)
at: &_keyEquivalentModifierMask];
[aDecoder decodeValueOfObjCType: @encode(unsigned int)
at: &_highlightsByMask];
[aDecoder decodeValueOfObjCType: @encode(unsigned int)
at: &_showAltStateMask];
NSDebugLog(@"NSButtonCell: finish decoding\n");
* NSApplication.h added windows_need_update as autodisplay ivar. * NSApplication.m in init set default for main_menu, windows_need_update. * NSApplication.m in run added support for OPENSTEP autodisplay mechanism. * NSApplication.m _eventMatchingMask: reformat. * NSApplication.m nextEventMatchingMask:untilDate:inMode:dequeue: move calls to _flushWindows to XDPS backend. * NSApplication.m implemented updateWindows. * NSApplication.m implemented setWindowsNeedUpdate:. * NSApplication.m setMainMenu: test for menu prior to release. * NSActionCell.m removed setState: method. * NSActionCell.m copyWithZone: optimized ivar copying. * NSButtonCell.m added _init and moved common defaults to this method. * NSButtonCell.m initImageCell: and initTextCell: minor optimizations. * NSButtonCell.m setAlternateTitle: optimize and use ASSIGN macro. * NSButtonCell.m setKeyEquivalent: remove [copy release]. * NSButtonCell.m setKeyEquivalentFont:size: optimize. * NSButtonCell.m copyWithZone: optimized ivar copying where possible. * NSCell.m _init removed call to [super init]. * NSCell.m initTextCell: removed duplicate default initializers. * NSCell.m setImage: rewrote to use ASSIGN macro. * NSCell.m setDoubleValue: rewrote to use ASSIGN macro. * NSCell.m setFloatValue: rewrote to use ASSIGN macro. * NSCell.m setIntValue: rewrote to use ASSIGN macro. * NSCell.m setStringValue: rewrote to use ASSIGN macro. * NSCell.m setFont: rewrote to use ASSIGN macro. * NSCell.m setRepresentedObject: rewrote to use ASSIGN macro. * NSCell.m copyWithZone: optimized ivar copying where possible. * NSClipView.m viewBoundsChanged: minor optimization. * NSClipView.m viewFrameChanged: minor optimization. * NSClipView.m scaleUnitSquareToSize: minor optimization. * NSClipView.m setBoundsOrigin: minor optimization. * NSClipView.m setBoundsSize: minor optimization. * NSClipView.m setFrameSize: minor optimization. * NSClipView.m setFrameOrigin: minor optimization. * NSClipView.m setFrame: minor optimization. * NSClipView.m translateOriginToPoint: minor optimization. * NSMatrix.m eliminate retain/release of selected cell via ASSIGN. * NSMatrix.m initWithFrame: remove duplicate setting of selected row and column. * NSMatrix.m removeColumn: rewrite so that col is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m removeRow: rewrite so that row is removed before new selection is attempted (needed because selected cell is not retained). * NSMatrix.m deselectAllCells per OS spec try to select a cell at end if empty selection is not allowed. * NSMatrix.m deselectSelectedCell set default selected row/col to 0. * NSMatrix.m mouseDown: optimize for new drawing behaviour. * NSMenu.m setSelectedCell: eliminate use of retain/release of selected cell via ASSIGN. * NSMenuItem.m copyWithZone: optimized ivar copying where possible. * NSMenuItem.m dealloc minor optimization. * NSMenuItem.m setTarget: minor optimization. * NSScroller.m trackScrollButtons: heavily optimized. * NSScroller.m sendAction: reimplemented to use sendAction: per spec also optimized. * NSText.m implemented init method. * NSText.m initWithFrame: fixed initialization of ivars which are released to retain. * NSText.m dealloc add release of retained ivars. * NSView.m dealloc add release of retained ivars. * NSView.m displayRect: and _addSubviewForNeedingDisplay reformatted code and rewrote comments. * NSWindow.m setFrame:display: rewrote for clarity. * NSWindow.m implemented update method per OPENSTEP spec. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@2941 72102866-910b-0410-8b05-ffd578937521
1998-08-30 16:06:47 +00:00
return self;
}
@end