-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIconTextFieldCell.m
More file actions
executable file
·119 lines (96 loc) · 3.33 KB
/
IconTextFieldCell.m
File metadata and controls
executable file
·119 lines (96 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// IconTextFieldCell.m
//
// Created by John R Chang on Mon Jan 12 2004.
// This code is Creative Commons Public Domain. You may use it for any purpose whatsoever.
// http://creativecommons.org/licenses/publicdomain/
//
#import "IconTextFieldCell.h"
//! An NSTextFieldCell with left justified image.
/*!
Used in the table view to display score for a given item.
*/
@implementation IconTextFieldCell
//! Initializes IconTextFieldCell with no image.
- (id) init
{
self = [super init];
_image = nil;
return self;
}
//! Creates copy which shares the original image.
- (id)copyWithZone:(NSZone *)zone
{
IconTextFieldCell * newCell = [[IconTextFieldCell alloc] init];
newCell->_image = [_image retain];
return newCell;
}
//! Releases _image and frees up memory.
- (void) dealloc
{
[_image release];
[super dealloc];
}
//! _image setter.
- (void)setImage:(NSImage *)image
{
[_image release];
_image = [image retain];
}
//! _image getter.
- (NSImage *)image
{
return _image;
}
//! Draws icon if any and then next to it the field text.
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if (_image)
{
NSRect imageFrame = [self imageRectForBounds:cellFrame];
//[[NSColor blueColor] set]; NSFrameRect(imageFrame); // DEBUG
NSPoint point = imageFrame.origin;
if ([controlView isFlipped])
point.y += imageFrame.size.height;
[_image compositeToPoint:point operation:NSCompositeSourceOver];
}
NSRect titleRect = [self titleRectForBounds:cellFrame];
[super drawInteriorWithFrame:titleRect inView:controlView];
}
//! Calculates position of title origin based on image width.
- (NSRect)titleRectForBounds:(NSRect)theRect
{
NSRect titleRect = [super titleRectForBounds:theRect];
if (_image)
{
NSRect imageRect = [self imageRectForBounds:theRect];
const float kHorizontalMarginBetweenIconAndText = 3.0;
float offsetForImageRectX = imageRect.size.width + kHorizontalMarginBetweenIconAndText;
titleRect.origin.x += offsetForImageRectX;
titleRect.size.width -= offsetForImageRectX;
}
return titleRect;
}
//! Calculates position of image origin relative to title origin.
- (NSRect)imageRectForBounds:(NSRect)theRect
{
// Superclass returns a centered image position
// We need to left align it, i.e. NSImageLeft
NSRect imageRect = [super imageRectForBounds:theRect];
NSRect titleRect = [super titleRectForBounds:theRect];
imageRect.origin.x = titleRect.origin.x + 2.0;
return imageRect;
}
//! Shorten up the editor rectangle to accomodate our icon
- (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent
{
NSRect textFrame = [self titleRectForBounds:aRect];
[super editWithFrame:textFrame inView:controlView editor:textObj delegate:anObject event:theEvent];
}
//! Shorten up the editor to reflect our icon.
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength
{
NSRect textFrame = [self titleRectForBounds:aRect];
[super selectWithFrame:textFrame inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}
@end