Merge pull request #10 from gunyarakun/fix-invalid-return
[cocotron.git] / AppKit / NSTabViewItem.m
blob1ab189021d8a7ca1ea78da89c2fda63c2d22acfc
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)];
27    }
29    if (length==[string length])
30        return string;
31    else
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;
44     
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];
51    }
52    else {
53     [NSException raise:NSInvalidArgumentException format:@"-[%@ %s] is not implemented for coder %@",isa,sel_getName(_cmd),coder];
54    }
56    return self;
59 -initWithIdentifier:identifier {
60    _identifier=[identifier retain];
61    _label=@"";
62    _view=nil;
63    _color=[[NSColor controlColor] retain];
64    _state=NSBackgroundTab;
65    return self;
68 -(void)dealloc {
69    [_identifier release];
70    [_label release];
71    [_view release];
72    [_color release];
73    [super dealloc];
76 -identifier {
77    return _identifier;
80 -(NSString *)label {
81    return _label;
84 -view {
85    return _view;
88 -(NSColor *)color {
89     return _color;
92 -(NSTabView *)tabView {
93     return _tabView;
96 -(NSTabState)tabState {
97     return _state;
100 -initialFirstResponder {
101     return _initialFirstResponder;
105 -(void)setLabel:(NSString *)label {
106    label=[label copy];
107    [_label release];
108    _label=label;
111 -(void)setView:(NSView *)view {
112    view=[view retain];
113    [_view release];
114    _view=view;
116    if (_initialFirstResponder==nil)
117        _initialFirstResponder=view;
118    [view removeFromSuperview];
119    [_tabView _itemViewDidChange:self];
122 -(void)setColor:(NSColor *)color {
123     color=[color retain];
124     [_color release];
125     _color=color;
128 // private; no retain
129 -(void)setTabView:(NSTabView *)tabView {
130     _tabView=tabView;
133 // private
134 -(void)setTabState:(NSTabState)tabState {
135     _state=tabState;
138 -(void)setInitialFirstResponder:responder {
139     responder=[responder retain];
140     [_initialFirstResponder release];
141     _initialFirstResponder=responder;
142 }   
144 // This is private Apple API, BGHUDAppKit uses it, override for custom color
145 -_labelColor {
146    return [NSColor blackColor];
149 -(NSDictionary *)labelAttributes {
150    return [NSDictionary dictionaryWithObjectsAndKeys:
151     [_tabView font],NSFontAttributeName,
152     [self _labelColor],NSForegroundColorAttributeName,
153     nil];
156 // our tabs draw too closely to the left mragin.
157 #define ORIGIN_PADDING 2.0
158 -(void)drawLabel:(BOOL)truncateLabel inRect:(NSRect)rect {
159     _lastRect = 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]];
164     }
165     else
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]];
178     }
179     size.width+=SIZE_PADDING;
181     return size;
184 @end