2007-01-01 21:11:21 +00:00
|
|
|
/* Copyright (c) 2006-2007 Christopher J. W. Lloyd
|
2006-12-22 04:41:44 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
|
|
|
#import <AppKit/NSTabViewItem.h>
|
|
|
|
#import <AppKit/NSTabView.h>
|
|
|
|
#import <AppKit/NSView.h>
|
|
|
|
#import <AppKit/NSStringDrawing.h>
|
|
|
|
#import <AppKit/NSAttributedString.h>
|
|
|
|
#import <AppKit/NSFont.h>
|
|
|
|
#import <AppKit/NSColor.h>
|
|
|
|
#import <AppKit/NSStringDrawer.h>
|
2009-06-19 18:12:28 +00:00
|
|
|
#import <Foundation/NSKeyedArchiver.h>
|
2009-06-05 03:16:46 +00:00
|
|
|
#import <AppKit/NSRaise.h>
|
2006-12-22 04:41:44 +00:00
|
|
|
|
|
|
|
// not perfect; needs real ellipsis character, will fail to produce a small enough string if
|
|
|
|
// char. 1 + @"..." > rect.size.width
|
|
|
|
NSString *_NSTruncatedStringWithAttributesInRect(NSString *string, NSDictionary *attributes, NSRect rect) {
|
|
|
|
unsigned length=[string length];
|
|
|
|
|
|
|
|
while ([string length] > 1 && [string sizeWithAttributes:attributes].width > rect.size.width) {
|
|
|
|
string = [string substringWithRange:NSMakeRange(0, [string length]-1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length==[string length])
|
|
|
|
return string;
|
|
|
|
else
|
|
|
|
return [string stringByAppendingString:@"..."];
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation NSTabViewItem
|
|
|
|
|
|
|
|
-(void)encodeWithCoder:(NSCoder *)coder {
|
|
|
|
NSUnimplementedMethod();
|
|
|
|
}
|
|
|
|
|
|
|
|
-initWithCoder:(NSCoder *)coder {
|
2009-06-19 17:31:04 +00:00
|
|
|
if([coder allowsKeyedCoding]){
|
|
|
|
NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
|
2006-12-22 04:41:44 +00:00
|
|
|
|
|
|
|
_identifier=[[keyed decodeObjectForKey:@"NSIdentifier"] retain];
|
|
|
|
_label=[[keyed decodeObjectForKey:@"NSLabel"] retain];
|
|
|
|
_view=[[keyed decodeObjectForKey:@"NSView"] retain];
|
2010-03-16 20:38:14 +00:00
|
|
|
|
2007-11-19 14:49:00 +00:00
|
|
|
_tabView=[keyed decodeObjectForKey:@"NSTabView"];
|
2008-08-28 14:29:37 +00:00
|
|
|
_color=[[keyed decodeObjectForKey:@"NSColor"] retain];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-12-17 13:11:05 +00:00
|
|
|
[NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",[self class],sel_getName(_cmd),coder];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-initWithIdentifier:identifier {
|
|
|
|
_identifier=[identifier retain];
|
|
|
|
_label=@"";
|
|
|
|
_view=nil;
|
|
|
|
_color=[[NSColor controlColor] retain];
|
|
|
|
_state=NSBackgroundTab;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)dealloc {
|
|
|
|
[_identifier release];
|
|
|
|
[_label release];
|
|
|
|
[_view release];
|
|
|
|
[_color release];
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
-identifier {
|
|
|
|
return _identifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSString *)label {
|
|
|
|
return _label;
|
|
|
|
}
|
|
|
|
|
|
|
|
-view {
|
|
|
|
return _view;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSColor *)color {
|
|
|
|
return _color;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSTabView *)tabView {
|
|
|
|
return _tabView;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(NSTabState)tabState {
|
|
|
|
return _state;
|
|
|
|
}
|
|
|
|
|
|
|
|
-initialFirstResponder {
|
|
|
|
return _initialFirstResponder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
-(void)setLabel:(NSString *)label {
|
|
|
|
label=[label copy];
|
|
|
|
[_label release];
|
2008-04-30 18:40:25 +00:00
|
|
|
_label=label;
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setView:(NSView *)view {
|
|
|
|
view=[view retain];
|
|
|
|
[_view release];
|
|
|
|
_view=view;
|
|
|
|
|
|
|
|
if (_initialFirstResponder==nil)
|
|
|
|
_initialFirstResponder=view;
|
2008-07-12 19:26:13 +00:00
|
|
|
[view removeFromSuperview];
|
|
|
|
[_tabView _itemViewDidChange:self];
|
2006-12-22 04:41:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setColor:(NSColor *)color {
|
|
|
|
color=[color retain];
|
|
|
|
[_color release];
|
|
|
|
_color=color;
|
|
|
|
}
|
|
|
|
|
|
|
|
// private; no retain
|
|
|
|
-(void)setTabView:(NSTabView *)tabView {
|
|
|
|
_tabView=tabView;
|
|
|
|
}
|
|
|
|
|
|
|
|
// private
|
|
|
|
-(void)setTabState:(NSTabState)tabState {
|
|
|
|
_state=tabState;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void)setInitialFirstResponder:responder {
|
|
|
|
responder=[responder retain];
|
|
|
|
[_initialFirstResponder release];
|
|
|
|
_initialFirstResponder=responder;
|
|
|
|
}
|
|
|
|
|
2010-10-22 18:48:49 +00:00
|
|
|
// This is private Apple API, BGHUDAppKit uses it, override for custom color
|
|
|
|
-_labelColor {
|
|
|
|
return [NSColor blackColor];
|
|
|
|
}
|
|
|
|
|
2006-12-22 04:41:44 +00:00
|
|
|
-(NSDictionary *)labelAttributes {
|
|
|
|
return [NSDictionary dictionaryWithObjectsAndKeys:
|
|
|
|
[_tabView font],NSFontAttributeName,
|
2010-10-22 18:48:49 +00:00
|
|
|
[self _labelColor],NSForegroundColorAttributeName,
|
2006-12-22 04:41:44 +00:00
|
|
|
nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
// our tabs draw too closely to the left mragin.
|
|
|
|
#define ORIGIN_PADDING 2.0
|
|
|
|
-(void)drawLabel:(BOOL)truncateLabel inRect:(NSRect)rect {
|
|
|
|
_lastRect = rect;
|
|
|
|
rect.origin.x+=ORIGIN_PADDING;
|
|
|
|
if ([self sizeOfLabel:NO].width > rect.size.width && truncateLabel) {
|
|
|
|
NSString *truncatedLabel = _NSTruncatedStringWithAttributesInRect(_label, [self labelAttributes], rect);
|
|
|
|
[truncatedLabel _clipAndDrawInRect:rect withAttributes:[self labelAttributes]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
[_label _clipAndDrawInRect:rect withAttributes:[self labelAttributes]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// our tabs look too short.
|
|
|
|
#define SIZE_PADDING 8.0
|
|
|
|
-(NSSize)sizeOfLabel:(BOOL)truncateLabel {
|
|
|
|
NSSize size = [_label sizeWithAttributes:[self labelAttributes]];
|
|
|
|
|
|
|
|
// make sure that we don't return the uninitialized lastRect
|
|
|
|
if (truncateLabel && _lastRect.size.width > 0) {
|
|
|
|
NSString *truncatedLabel = _NSTruncatedStringWithAttributesInRect(_label, [self labelAttributes], _lastRect);
|
|
|
|
size = [truncatedLabel sizeWithAttributes:[self labelAttributes]];
|
|
|
|
}
|
|
|
|
size.width+=SIZE_PADDING;
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|