/** NSBrowserCell Cell class for the NSBrowser Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc. Author: Scott Christley Date: 1996 Author: Nicola Pero Date: December 1999 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 Lesser 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; see the file COPYING.LIB. If not, see or write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #import "config.h" #import #import "AppKit/NSBrowserCell.h" #import "AppKit/NSColor.h" #import "AppKit/NSFont.h" #import "AppKit/NSImage.h" #import "AppKit/NSGraphics.h" #import "AppKit/NSEvent.h" #import "AppKit/NSWindow.h" #import "GSGuiPrivate.h" /* * Class variables */ static NSImage *_branch_image; static NSImage *_highlight_image; static Class _colorClass; // GNUstep user default to have NSBrowserCell in bold if non leaf static BOOL _gsFontifyCells = NO; static NSFont *_nonLeafFont; static NSFont *_leafFont; /**

TODO Description

*/ @implementation NSBrowserCell /* * Class methods */ + (void) initialize { if (self == [NSBrowserCell class]) { [self setVersion: 1]; ASSIGN(_branch_image, [NSImage imageNamed: @"common_3DArrowRight"]); ASSIGN(_highlight_image, [NSImage imageNamed: @"common_3DArrowRightH"]); /* * Cache classes to avoid overheads of poor compiler implementation. */ _colorClass = [NSColor class]; // A GNUstep experimental feature if ([[NSUserDefaults standardUserDefaults] boolForKey: @"GSBrowserCellFontify"]) { _gsFontifyCells = YES; _nonLeafFont = RETAIN ([NSFont boldSystemFontOfSize: 0]); _leafFont = RETAIN ([NSFont systemFontOfSize: 0]); } } } /**

Returns the default branch image. Currently, it's an arrow.

*

See Also: +highlightedBranchImage

*/ + (NSImage*) branchImage { return _branch_image; } /**

Returns the default hightlited branch image

*

See Also: +branchImage

*/ + (NSImage*) highlightedBranchImage { return _highlight_image; } /* * Instance methods */ - (id) initTextCell: (NSString *)aString { self = [super initTextCell: aString]; if (nil == self) return self; //_alternateImage = nil; //_browsercell_is_leaf = NO; //_browsercell_is_loaded = NO; [self setLineBreakMode: NSLineBreakByTruncatingTail]; if (_gsFontifyCells) [self setFont: _nonLeafFont]; return self; } - (id) initImageCell: (NSImage *)anImage { self = [super initImageCell: anImage]; if (nil == self) return self; //_alternateImage = nil; //_browsercell_is_leaf = NO; //_browsercell_is_loaded = NO; [self setLineBreakMode: NSLineBreakByTruncatingTail]; if (_gsFontifyCells) [self setFont: _nonLeafFont]; return self; } - (void) dealloc { TEST_RELEASE (_alternateImage); [super dealloc]; } - (id) copyWithZone: (NSZone*)zone { NSBrowserCell *c = [super copyWithZone: zone]; _alternateImage = TEST_RETAIN (_alternateImage); //c->_browsercell_is_leaf = _browsercell_is_leaf; //c->_browsercell_is_loaded = _browsercell_is_loaded; return c; } /**

Returns the alternate image in the NSBrowserCell (used when the cell is highlighted)

See Also: -setAlternateImage:

*/ - (NSImage*) alternateImage { return _alternateImage; } /**

Sets the altenate image (used when the cell is highlighted) to anImage *

See Also: -alternateImage

*/ - (void) setAlternateImage: (NSImage *)anImage { ASSIGN(_alternateImage, anImage); } - (NSColor *)highlightColorInView: (NSView *)controlView { return [_colorClass selectedControlColor]; } /**

Returns whether the browserCell is a leaf. A leaf cell has usually no * image. This method is used by NSBrowser in several methods, for example * to know if a column should be added; when the user selects a browser cell * (a branch or a leaf)

*

See Also: -setLeaf:

*/ - (BOOL) isLeaf { return _browsercell_is_leaf; } /**

Sets whether the cell is a leaf. This method is usally used in the * NSBrowser's delegate methods

*

See Also: -isLeaf

*/ - (void) setLeaf: (BOOL)flag { if (_browsercell_is_leaf == flag) return; _browsercell_is_leaf = flag; if (_gsFontifyCells) { if (_browsercell_is_leaf) { [self setFont: _leafFont]; } else { [self setFont: _nonLeafFont]; } } } /**

Returns whether the NSBrowseCell state is set and is ready for display

See Also: -setLoaded:

*/ - (BOOL) isLoaded { return _browsercell_is_loaded; } /**

Sets whether the NSBrowseCell state is set and is ready for display

*

See Also: -isLoaded

*/ - (void) setLoaded: (BOOL)flag { _browsercell_is_loaded = flag; } /** *

Unhighlights the cell and sets the cell's state to NO

*

See Also: -set

*/ - (void) reset { _cell.is_highlighted = NO; _cell.state = NO; } /**

Highlights the cell and sets the cell's state to YES

*

See Also: -reset

*/ - (void) set { _cell.is_highlighted = YES; _cell.state = YES; } - (void) setType: (NSCellType)aType { /* We do nothing here (we match the Mac OS X behavior) because with * NSBrowserCell GNUstep implementation the cell may contain an image * and text at the same time. */ } /* * Displaying */ - (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView *)controlView { NSRect title_rect = cellFrame; NSImage *branch_image = nil; NSImage *cell_image = nil; NSColor *backColor; NSWindow *cvWin = [controlView window]; if (_cell.is_highlighted || _cell.state) { backColor = [self highlightColorInView: controlView]; [backColor set]; if (!_browsercell_is_leaf) branch_image = [isa highlightedBranchImage]; cell_image = [self alternateImage]; } else { backColor = [cvWin backgroundColor]; [backColor set]; if (!_browsercell_is_leaf) branch_image = [isa branchImage]; cell_image = [self image]; } // Clear the background NSRectFill(cellFrame); // Draw the branch image if there is one if (branch_image) { NSSize size; NSPoint position; size = [branch_image size]; position.x = MAX(NSMaxX(title_rect) - size.width - 4.0, 0.); position.y = MAX(NSMidY(title_rect) - (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 ([controlView isFlipped]) position.y += size.height; [branch_image compositeToPoint: position operation: NSCompositeSourceOver]; title_rect.size.width -= size.width + 8; } // Skip 2 points from the left border title_rect.origin.x += 2; title_rect.size.width -= 2; // Draw the cell image if there is one if (cell_image) { NSSize size; NSPoint position; size = [cell_image size]; position.x = NSMinX(title_rect); position.y = MAX(NSMidY(title_rect) - (size.height/2.),0.); if ([controlView isFlipped]) position.y += size.height; [cell_image compositeToPoint: position operation: NSCompositeSourceOver]; title_rect.origin.x += size.width + 4; title_rect.size.width -= size.width + 4; } // Draw the body of the cell [self _drawAttributedText: [self attributedStringValue] inFrame: title_rect]; } - (BOOL) isOpaque { return YES; } /* * NSCoding protocol */ - (void) encodeWithCoder: (NSCoder*)aCoder { [super encodeWithCoder: aCoder]; if ([aCoder allowsKeyedCoding]) { // simply encodes prescence... } else { BOOL tmp; tmp = _browsercell_is_leaf; [aCoder encodeValueOfObjCType: @encode(BOOL) at: &tmp]; tmp = _browsercell_is_loaded; [aCoder encodeValueOfObjCType: @encode(BOOL) at: &tmp]; [aCoder encodeObject: _alternateImage]; } } - (id) initWithCoder: (NSCoder*)aDecoder { self = [super initWithCoder: aDecoder]; if (nil == self) return self; if ([aDecoder allowsKeyedCoding]) { // Nothing to decode... } else { BOOL tmp; [aDecoder decodeValueOfObjCType: @encode(BOOL) at: &tmp]; [self setLeaf: tmp]; [aDecoder decodeValueOfObjCType: @encode(BOOL) at: &tmp]; [self setLoaded: tmp]; [aDecoder decodeValueOfObjCType: @encode(id) at: &_alternateImage]; } return self; } @end