1 /* Copyright (c) 2006-2007 Christopher J. W. Lloyd
3 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:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 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. */
9 #import <AppKit/NSTabViewItem.h>
10 #import <AppKit/NSTabView.h>
11 #import <AppKit/NSView.h>
12 #import <AppKit/NSStringDrawing.h>
13 #import <AppKit/NSAttributedString.h>
14 #import <AppKit/NSFont.h>
15 #import <AppKit/NSColor.h>
16 #import <AppKit/NSStringDrawer.h>
17 #import <Foundation/NSKeyedArchiver.h>
18 #import <AppKit/NSRaise.h>
20 // not perfect; needs real ellipsis character, will fail to produce a small enough string if
21 // char. 1 + @"..." > rect.size.width
22 NSString *_NSTruncatedStringWithAttributesInRect(NSString *string, NSDictionary *attributes, NSRect rect) {
23 unsigned length=[string length];
25 while ([string length] > 1 && [string sizeWithAttributes:attributes].width > rect.size.width) {
26 string = [string substringWithRange:NSMakeRange(0, [string length]-1)];
29 if (length==[string length])
32 return [string stringByAppendingString:@"..."];
35 @implementation NSTabViewItem
37 -(void)encodeWithCoder:(NSCoder *)coder {
38 NSUnimplementedMethod();
41 -initWithCoder:(NSCoder *)coder {
42 if([coder allowsKeyedCoding]){
43 NSKeyedUnarchiver *keyed=(NSKeyedUnarchiver *)coder;
45 _identifier=[[keyed decodeObjectForKey:@"NSIdentifier"] retain];
46 _label=[[keyed decodeObjectForKey:@"NSLabel"] retain];
47 _view=[[keyed decodeObjectForKey:@"NSView"] retain];
49 _tabView=[keyed decodeObjectForKey:@"NSTabView"];
50 _color=[[keyed decodeObjectForKey:@"NSColor"] retain];
53 [NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
59 -initWithIdentifier:identifier {
60 _identifier=[identifier retain];
63 _color=[[NSColor controlColor] retain];
64 _state=NSBackgroundTab;
69 [_identifier release];
92 -(NSTabView *)tabView {
96 -(NSTabState)tabState {
100 -initialFirstResponder {
101 return _initialFirstResponder;
105 -(void)setLabel:(NSString *)label {
111 -(void)setView:(NSView *)view {
116 if (_initialFirstResponder==nil)
117 _initialFirstResponder=view;
118 [view removeFromSuperview];
119 [_tabView _itemViewDidChange:self];
122 -(void)setColor:(NSColor *)color {
123 color=[color retain];
128 // private; no retain
129 -(void)setTabView:(NSTabView *)tabView {
134 -(void)setTabState:(NSTabState)tabState {
138 -(void)setInitialFirstResponder:responder {
139 responder=[responder retain];
140 [_initialFirstResponder release];
141 _initialFirstResponder=responder;
144 // This is private Apple API, BGHUDAppKit uses it, override for custom color
146 return [NSColor blackColor];
149 -(NSDictionary *)labelAttributes {
150 return [NSDictionary dictionaryWithObjectsAndKeys:
151 [_tabView font],NSFontAttributeName,
152 [self _labelColor],NSForegroundColorAttributeName,
156 // our tabs draw too closely to the left mragin.
157 #define ORIGIN_PADDING 2.0
158 -(void)drawLabel:(BOOL)truncateLabel inRect:(NSRect)rect {
160 rect.origin.x+=ORIGIN_PADDING;
161 if ([self sizeOfLabel:NO].width > rect.size.width && truncateLabel) {
162 NSString *truncatedLabel = _NSTruncatedStringWithAttributesInRect(_label, [self labelAttributes], rect);
163 [truncatedLabel _clipAndDrawInRect:rect withAttributes:[self labelAttributes]];
166 [_label _clipAndDrawInRect:rect withAttributes:[self labelAttributes]];
169 // our tabs look too short.
170 #define SIZE_PADDING 8.0
171 -(NSSize)sizeOfLabel:(BOOL)truncateLabel {
172 NSSize size = [_label sizeWithAttributes:[self labelAttributes]];
174 // make sure that we don't return the uninitialized lastRect
175 if (truncateLabel && _lastRect.size.width > 0) {
176 NSString *truncatedLabel = _NSTruncatedStringWithAttributesInRect(_label, [self labelAttributes], _lastRect);
177 size = [truncatedLabel sizeWithAttributes:[self labelAttributes]];
179 size.width+=SIZE_PADDING;