-
Notifications
You must be signed in to change notification settings - Fork 0
/
IconTextCell.m
147 lines (110 loc) · 4.26 KB
/
IconTextCell.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
//----------------------------------------------------------------------------------------
// IconTextCell.m - An NSTextFieldCell which displays both an icon & text.
//
// Copyright © Chris, 2007 - 2010. All rights reserved.
//----------------------------------------------------------------------------------------
#import "IconTextCell.h"
#import "CommonUtils.h"
enum {
kIconSize = 16,
kIconLeft = 0,
kIconRight = 4,
kIconWidth = kIconLeft + kIconSize + kIconRight,
kMiniIconSize = 12,
kMiniIconLeft = 3,
kMiniIconRight = 0,
kMiniIconWidth = kMiniIconLeft + kMiniIconSize + kMiniIconRight
};
#define fMini _tfFlags.mini
//----------------------------------------------------------------------------------------
@implementation IconTextCell
//----------------------------------------------------------------------------------------
// Set the IconRef for a standard size icon text cell.
- (void) setIconRef: (IconRef) iconRef
{
fMini = 0;
fIconRef = iconRef;
}
//----------------------------------------------------------------------------------------
- (void) editWithFrame: (NSRect) aRect
inView: (NSView*) controlView
editor: (NSText*) textObj
delegate: (id) anObject
event: (NSEvent*) theEvent
{
const GCoord width = fMini ? kMiniIconWidth : kIconWidth;
aRect.origin.x += width;
aRect.origin.y += 1;
aRect.size.width -= width;
aRect.size.height -= 2;
[super editWithFrame: aRect inView: controlView editor: textObj delegate: anObject event: theEvent];
}
//----------------------------------------------------------------------------------------
- (void) selectWithFrame: (NSRect) aRect
inView: (NSView*) controlView
editor: (NSText*) textObj
delegate: (id) anObject
start: (int) selStart
length: (int) selLength
{
const GCoord width = fMini ? kMiniIconWidth : kIconWidth;
aRect.origin.x += width;
aRect.size.width -= width;
[super selectWithFrame: aRect
inView: controlView
editor: textObj
delegate: anObject
start: selStart
length: selLength];
}
//----------------------------------------------------------------------------------------
- (void) drawWithFrame: (NSRect) cellFrame
inView: (NSView*) controlView
{
if (fIconRef != NULL)
{
CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
const BOOL mini = fMini;
const GCoord size = mini ? kMiniIconSize : kIconSize,
width = mini ? kMiniIconWidth : kIconWidth;
const CGRect rect = {{ mini ? kMiniIconLeft : kIconLeft, -size }, { size, size }};
CGAffineTransform mat = { 1, 0, 0, -1, cellFrame.origin.x,
cellFrame.origin.y + floor((cellFrame.size.height - size) * 0.5) };
CGContextSaveGState(ctx);
CGContextConcatCTM(ctx, mat);
WarnIf(PlotIconRefInContext(ctx, &rect, kAlignNone, kTransformNone,
NULL, kPlotIconRefNormalFlags, fIconRef));
CGContextRestoreGState(ctx);
cellFrame.origin.x += width;
cellFrame.origin.y += 1;
cellFrame.size.width -= width;
cellFrame.size.height -= 2;
}
[super drawWithFrame: cellFrame inView: controlView]; // Draws the text
}
//----------------------------------------------------------------------------------------
- (NSSize) cellSize
{
NSSize cellSize = [super cellSize];
const BOOL mini = fMini;
cellSize.width += fIconRef ? (mini ? kMiniIconWidth : kIconWidth)
: (mini ? kMiniIconLeft + kMiniIconRight : kIconLeft + kIconRight);
return cellSize;
}
//----------------------------------------------------------------------------------------
@end // IconTextCell
//----------------------------------------------------------------------------------------
#pragma mark -
//----------------------------------------------------------------------------------------
@implementation MiniIconTextCell
//----------------------------------------------------------------------------------------
// Set the IconRef for a mini size icon text cell.
- (void) setIconRef: (IconRef) iconRef
{
fMini = 1;
fIconRef = iconRef;
}
//----------------------------------------------------------------------------------------
@end // MiniIconTextCell
//----------------------------------------------------------------------------------------
// End of IconTextCell.m