-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBGImageAndTextCell.m
171 lines (140 loc) · 5.07 KB
/
PBGImageAndTextCell.m
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// PBGImageAndTextCell.m
// Neurosis
//
// Created by Patrick B. Gibson on 02/04/08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGImageAndTextCell.h"
#import "PBGTreeNode.h"
@implementation PBGImageAndTextCell
#define kIconImageSize 16.0
#define kImageOriginXOffset 3
#define kImageOriginYOffset 1
#define kTextOriginXOffset 2
#define kTextOriginYOffset 2
#define kTextHeightAdjust 4
- (id)init
{
self = [super init];
[self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
return self;
}
// -------------------------------------------------------------------------------
// copyWithZone:zone
// -------------------------------------------------------------------------------
/*- (id)copyWithZone:(NSZone*)zone
{
ImageAndTextCell *cell = (ImageAndTextCell*)[super copyWithZone:zone];
cell->image = [image retain];
return cell;
}
*/
- (void)setImage:(NSImage*)anImage
{
if (anImage != image)
{
[image release];
image = anImage;
[anImage retain];
[image setSize:NSMakeSize(kIconImageSize, kIconImageSize)];
}
}
- (NSImage*)image
{
return image;
}
- (BOOL)isGroupCell
{
return ([self image] == nil && [[self title] length] > 0);
}
// -------------------------------------------------------------------------------
// titleRectForBounds:cellRect
//
// Returns the proper bound for the cell's title while being edited
// -------------------------------------------------------------------------------
- (NSRect)titleRectForBounds:(NSRect)cellRect
{
// the cell has an image: draw the normal item cell
NSSize imageSize;
NSRect imageFrame;
imageSize = [image size];
NSDivideRect(cellRect, &imageFrame, &cellRect, 3 + imageSize.width, NSMinXEdge);
imageFrame.origin.x += kImageOriginXOffset;
imageFrame.origin.y -= kImageOriginYOffset;
imageFrame.size = imageSize;
imageFrame.origin.y += ceil((cellRect.size.height - imageFrame.size.height) / 2);
NSRect newFrame = cellRect;
newFrame.origin.x += kTextOriginXOffset;
newFrame.origin.y += kTextOriginYOffset;
newFrame.size.height -= kTextHeightAdjust;
return newFrame;
}
// -------------------------------------------------------------------------------
// editWithFrame:inView:editor:delegate:event
// -------------------------------------------------------------------------------
- (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];
}
// -------------------------------------------------------------------------------
// selectWithFrame:inView:editor:delegate:event:start:length
// -------------------------------------------------------------------------------
- (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];
}
// -------------------------------------------------------------------------------
// drawWithFrame:cellFrame:controlView:
// -------------------------------------------------------------------------------
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
{
if (image != nil)
{
// the cell has an image: draw the normal item cell
NSSize imageSize;
NSRect imageFrame;
imageSize = [image size];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + imageSize.width, NSMinXEdge);
imageFrame.origin.x += kImageOriginXOffset;
imageFrame.origin.y -= kImageOriginYOffset;
imageFrame.size = imageSize;
if ([controlView isFlipped])
imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
else
imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
[image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
NSRect newFrame = cellFrame;
newFrame.origin.x += kTextOriginXOffset;
newFrame.origin.y += kTextOriginYOffset;
newFrame.size.height -= kTextHeightAdjust;
[super drawWithFrame:newFrame inView:controlView];
}
else
{
if ([self isGroupCell])
{
// Center the text in the cellFrame, and call super to do thew ork of actually drawing.
CGFloat yOffset = floor((NSHeight(cellFrame) - [[self attributedStringValue] size].height) / 2.0);
cellFrame.origin.y += yOffset;
cellFrame.size.height -= (kTextOriginYOffset*yOffset);
[super drawWithFrame:cellFrame inView:controlView];
}
}
}
// -------------------------------------------------------------------------------
// cellSize:
// -------------------------------------------------------------------------------
- (NSSize)cellSize
{
NSSize cellSize = [super cellSize];
cellSize.width += (image ? [image size].width : 0) + 3;
return cellSize;
}
- (void)dealloc
{
[super dealloc];
}
@end