1/** <title>NSTabViewItem</title>
2
3   Copyright (C) 2000-2016 Free Software Foundation, Inc.
4
5   Author: Michael Hanni <mhanni@sprintmail.com>
6   Date: 1999
7
8   This file is part of the GNUstep GUI Library.
9
10   This library is free software; you can redistribute it and/or
11   modify it under the terms of the GNU Lesser General Public
12   License as published by the Free Software Foundation; either
13   version 2 of the License, or (at your option) any later version.
14
15   This library is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public
21   License along with this library; see the file COPYING.LIB.
22   If not, see <http://www.gnu.org/licenses/> or write to the
23   Free Software Foundation, 51 Franklin Street, Fifth Floor,
24   Boston, MA 02110-1301, USA.
25*/
26
27#import "AppKit/NSAttributedString.h"
28#import "AppKit/NSColor.h"
29#import "AppKit/NSFont.h"
30#import "AppKit/NSGraphics.h"
31#import "AppKit/NSImage.h"
32#import "AppKit/NSStringDrawing.h"
33#import "AppKit/NSTabView.h"
34#import "AppKit/NSTabViewItem.h"
35#import "AppKit/PSOperators.h"
36
37@implementation NSTabViewItem
38
39- (id) init
40{
41  return [self initWithIdentifier: @""];
42}
43
44- (id) initWithIdentifier: (id)identifier
45{
46  self = [super init];
47
48  if (self)
49    {
50      ASSIGN(_ident, identifier);
51      _state = NSBackgroundTab;
52      _view = [NSView new];
53      // Use the window background colour as default, not the control background colour.
54      [self setColor: [NSColor windowBackgroundColor]];
55    }
56
57  return self;
58}
59
60- (void) dealloc
61{
62  TEST_RELEASE(_ident);
63  RELEASE(_label);
64  RELEASE(_view);
65  RELEASE(_color);
66  [super dealloc];
67}
68
69- (NSString*) description
70{
71  return [NSString stringWithFormat: @"%@: %@ (ident: %@)",
72		   NSStringFromClass([self class]), _label, _ident];
73}
74
75// Set identifier.
76
77- (void) setIdentifier: (id)identifier
78{
79  ASSIGN(_ident, identifier);
80}
81
82- (id) identifier
83{
84  return _ident;
85}
86
87// Set label for item.
88
89- (void) setLabel: (NSString*)label
90{
91  ASSIGN(_label, label);
92}
93
94- (NSString *) label
95{
96  return _label;
97}
98
99- (NSSize) sizeOfLabel: (BOOL)shouldTruncateLabel
100{
101  NSDictionary *  attr;
102  NSString *string;
103  NSSize rSize;
104
105  if (nil == _label)
106    return NSZeroSize;
107
108  attr = [[NSDictionary alloc] initWithObjectsAndKeys:
109	       [_tabview font], NSFontAttributeName,
110	       nil];
111
112  if (shouldTruncateLabel)
113    {
114      string = [self _truncatedLabel];
115    }
116  else
117    {
118      string = _label;
119    }
120
121  rSize = [string sizeWithAttributes: attr];
122  RELEASE(attr);
123  return rSize;
124}
125
126// Set view to display when item is clicked.
127
128- (void) setView: (NSView*)view
129{
130  ASSIGN(_view, view);
131}
132
133- (NSView*) view
134{
135  return _view;
136}
137
138// Set color of tab surface.
139
140- (void) setColor: (NSColor*)color
141{
142  ASSIGN(_color, color);
143}
144
145- (NSColor*) color
146{
147  return _color;
148}
149
150// tab state
151
152- (NSTabState) tabState
153{
154  return _state;
155}
156
157
158// Tab view, this is the "super" view.
159
160- (NSTabView*) tabView
161{
162  return _tabview;
163}
164
165// First responder.
166
167- (void) setInitialFirstResponder: (NSView*)view
168{
169  // We don't retain this.
170  _first_responder = view;
171}
172
173- (id) initialFirstResponder
174{
175  return _first_responder;
176}
177
178// Draw item.
179
180- (void) drawLabel: (BOOL)shouldTruncateLabel
181	    inRect: (NSRect)tabRect
182{
183  NSDictionary *attr;
184  NSString *string;
185
186  if (nil == _label)
187    return;
188
189  _rect = tabRect;
190
191  if (shouldTruncateLabel)
192    {
193      string = [self _truncatedLabel];
194    }
195  else
196    {
197      string = _label;
198    }
199
200  attr = [[NSDictionary alloc] initWithObjectsAndKeys:
201			       [_tabview font], NSFontAttributeName,
202			       [NSColor controlTextColor], NSForegroundColorAttributeName,
203			       nil];
204
205  {
206    NSSize size = [string sizeWithAttributes: attr];
207    NSRect labelRect = tabRect;
208
209    labelRect.origin.y = tabRect.origin.y + ((tabRect.size.height - size.height) / 2);
210    labelRect.size.height = size.height;
211
212    [string drawInRect: labelRect withAttributes: attr];
213  }
214  RELEASE(attr);
215}
216
217- (void) setToolTip: (NSString*)toolTip
218{
219  ASSIGN(_toolTip, toolTip);
220}
221
222- (NSString*) toolTip
223{
224  return _toolTip;
225}
226
227// NSCoding protocol.
228
229- (void) encodeWithCoder: (NSCoder*)aCoder
230{
231  if ([aCoder allowsKeyedCoding])
232    {
233      [aCoder encodeObject: _ident forKey: @"NSIdentifier"];
234      [aCoder encodeObject: _label forKey: @"NSLabel"];
235      [aCoder encodeObject: _view forKey: @"NSView"];
236      [aCoder encodeObject: _color forKey: @"NSColor"];
237      [aCoder encodeObject: _tabview forKey: @"NSTabView"];
238    }
239  else
240    {
241      [aCoder encodeObject:_ident];
242      [aCoder encodeObject:_label];
243      [aCoder encodeObject:_view];
244      [aCoder encodeObject:_color];
245      [aCoder encodeValueOfObjCType: @encode(int) at: &_state];
246      [aCoder encodeObject:_first_responder];
247      [aCoder encodeObject:_tabview];
248    }
249}
250
251- (id) initWithCoder: (NSCoder*)aDecoder
252{
253  if ([aDecoder allowsKeyedCoding])
254    {
255      id identifier = [aDecoder decodeObjectForKey: @"NSIdentifier"];
256
257      self = [self initWithIdentifier: identifier];
258      [self setLabel: [aDecoder decodeObjectForKey: @"NSLabel"]];
259      [self setView: [aDecoder decodeObjectForKey: @"NSView"]];
260      [self setColor: [aDecoder decodeObjectForKey: @"NSColor"]];
261      [self _setTabView: [aDecoder decodeObjectForKey: @"NSTabView"]];
262    }
263  else
264    {
265      [aDecoder decodeValueOfObjCType: @encode(id) at: &_ident];
266      [aDecoder decodeValueOfObjCType: @encode(id) at: &_label];
267      [aDecoder decodeValueOfObjCType: @encode(id) at: &_view];
268      [aDecoder decodeValueOfObjCType: @encode(id) at: &_color];
269      [aDecoder decodeValueOfObjCType: @encode(int) at:&_state];
270      [aDecoder decodeValueOfObjCType: @encode(id) at: &_first_responder];
271      AUTORELEASE(_first_responder);
272      [aDecoder decodeValueOfObjCType: @encode(id) at: &_tabview];
273      AUTORELEASE(_tabview);
274    }
275
276  return self;
277}
278@end
279
280@implementation NSTabViewItem (GNUstep)
281
282// Non spec
283
284- (NSRect) _tabRect
285{
286  return _rect;
287}
288
289- (void) _setTabState: (NSTabState)tabState
290{
291  _state = tabState;
292}
293
294- (void) _setTabView: (NSTabView*)tabView
295{
296  _tabview = tabView;
297}
298
299- (NSString*) _truncatedLabel
300{
301  // FIXME: What is the algo to truncate?
302  return _label;
303}
304
305@end
306