forked from pieter/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PBGitRevisionCell.m
200 lines (156 loc) · 6.26 KB
/
PBGitRevisionCell.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
//
// PBGitRevisionCell.m
// GitX
//
// Created by Pieter de Bie on 17-06-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBGitRevisionCell.h"
@implementation NSBezierPath (RoundedRectangle)
+ (NSBezierPath *)bezierPathWithRoundedRect: (NSRect) aRect cornerRadius: (double) cRadius
{
double left = aRect.origin.x, bottom = aRect.origin.y, width = aRect.size.width, height = aRect.size.height;
//now, crop the radius so we don't get weird effects
double lesserDim = width < height ? width : height;
if ( cRadius > lesserDim / 2 )
{
cRadius = lesserDim / 2;
}
//these points describe the rectangle as start and stop points of the
//arcs making up its corners --points c, e, & g are implicit endpoints of arcs
//and are unnecessary
NSPoint a = NSMakePoint( 0, cRadius ), b = NSMakePoint( 0, height - cRadius ),
d = NSMakePoint( width - cRadius, height ), f = NSMakePoint( width, cRadius ),
h = NSMakePoint( cRadius, 0 );
//these points describe the center points of the corner arcs
NSPoint cA = NSMakePoint( cRadius, height - cRadius ),
cB = NSMakePoint( width - cRadius, height - cRadius ),
cC = NSMakePoint( width - cRadius, cRadius ),
cD = NSMakePoint( cRadius, cRadius );
//start
NSBezierPath *bp = [NSBezierPath bezierPath];
[bp moveToPoint: a ];
[bp lineToPoint: b ];
[bp appendBezierPathWithArcWithCenter: cA radius: cRadius startAngle:180 endAngle:90 clockwise: YES];
[bp lineToPoint: d ];
[bp appendBezierPathWithArcWithCenter: cB radius: cRadius startAngle:90 endAngle:0 clockwise: YES];
[bp lineToPoint: f ];
[bp appendBezierPathWithArcWithCenter: cC radius: cRadius startAngle:0 endAngle:270 clockwise: YES];
[bp lineToPoint: h ];
[bp appendBezierPathWithArcWithCenter: cD radius: cRadius startAngle:270 endAngle:180 clockwise: YES];
[bp closePath];
//Transform path to rectangle's origin
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy: left yBy: bottom];
[bp transformUsingAffineTransform: transform];
return bp; //it's already been autoreleased
}
@end
@implementation PBGitRevisionCell
@synthesize cellInfo;
-(void) setCellInfo: (PBGraphCellInfo*) info
{
isReady = YES;
cellInfo = info;
}
- (id) initWithCoder: (id) coder
{
self = [super initWithCoder:coder];
if (self != nil) {
isReady = NO;
}
return self;
}
- (NSArray*) colors
{
return [NSArray arrayWithObjects:
[NSColor colorWithCalibratedRed: 0X4e/256.0 green:0X9A/256.0 blue: 0X06/256.0 alpha: 1.0],
[NSColor colorWithCalibratedRed: 0X20/256.0 green:0X4A/256.0 blue: 0X87/256.0 alpha: 1.0],
[NSColor colorWithCalibratedRed: 0XC4/256.0 green:0XA0/256.0 blue: 0 alpha: 1.0],
[NSColor colorWithCalibratedRed: 0X5C/256.0 green:0X35/256.0 blue: 0X66/256.0 alpha: 1.0],
[NSColor colorWithCalibratedRed: 0XA4/256.0 green:0X00/256.0 blue: 0X00/256.0 alpha: 1.0],
[NSColor colorWithCalibratedRed: 0XCE/256.0 green:0X5C/256.0 blue: 0 alpha: 1.0],
nil];
}
- (void) drawLineFromColumn: (int) from toColumn: (int) to inRect: (NSRect) r offset: (int) offset color: (int) c
{
int columnWidth = 10;
NSPoint origin = r.origin;
NSPoint source = NSMakePoint(origin.x + columnWidth* from, origin.y + offset);
NSPoint center = NSMakePoint( origin.x + columnWidth * to, origin.y + r.size.height * 0.5);
// Just use red for now.
NSArray* colors = [self colors];
[[colors objectAtIndex: c % [colors count]] set];
NSBezierPath * path = [NSBezierPath bezierPath];
[path setLineWidth:2];
[path moveToPoint: source];
[path lineToPoint: center];
[path stroke];
}
- (void) drawCircleForColumn: (int) c inRect: (NSRect) r
{
if (!cellInfo.refs)
[[NSColor blackColor] set];
else
[[NSColor redColor] set];
int columnWidth = 10;
NSPoint origin = r.origin;
NSPoint columnOrigin = { origin.x + columnWidth * c, origin.y};
NSRect oval = { columnOrigin.x - 5, columnOrigin.y + r.size.height * 0.5 - 5, 10, 10};
NSBezierPath * path = [NSBezierPath bezierPath];
path = [NSBezierPath bezierPathWithOvalInRect:oval];
//[[col objectAtIndex:cellInfo.columns[c].color] set];
[path fill];
NSRect smallOval = { columnOrigin.x - 3, columnOrigin.y + r.size.height * 0.5 - 3, 6, 6};
[[NSColor whiteColor] set];
path = [NSBezierPath bezierPathWithOvalInRect:smallOval];
[path fill];
}
- (void) drawRefsInRect: (NSRect*) rect
{
static const float ref_padding = 10.0f;
static const float ref_spacing = 2.0f;
NSRect refRect = (NSRect){rect->origin, rect->size};
if([self isHighlighted])
[[NSColor whiteColor] setStroke];
else
[[NSColor blackColor] setStroke];
int index;
for (index = 0; index < [cellInfo.refs count]; ++index) {
NSString* ref = [cellInfo.refs objectAtIndex:index];
NSString* newRef = [[ref componentsSeparatedByString:@"/"] lastObject];
NSSize refSize = [newRef sizeWithAttributes:nil];
refRect.size.width = refSize.width + ref_padding;
NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
NSMutableParagraphStyle* style = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
[style setAlignment:NSCenterTextAlignment];
[attributes setObject:style forKey:NSParagraphStyleAttributeName];
if([self isHighlighted])
[attributes setObject:[NSColor alternateSelectedControlTextColor] forKey:NSForegroundColorAttributeName];
[newRef drawInRect:refRect withAttributes:attributes];
[[NSBezierPath bezierPathWithRoundedRect:refRect cornerRadius:2.0f] stroke];
refRect.origin.x += refRect.size.width + ref_spacing;
}
rect->size.width -= refRect.origin.x - rect->origin.x;
rect->origin.x = refRect.origin.x;
}
- (void) drawWithFrame: (NSRect) rect inView:(NSView *)view
{
if (!isReady)
return [super drawWithFrame:rect inView:view];
float pathWidth = 10 + 10 * cellInfo.numColumns;
NSRect ownRect;
NSDivideRect(rect, &ownRect, &rect, pathWidth, NSMinXEdge);
for (PBGitGraphLine* line in cellInfo.lines) {
if (line.upper == 0)
[self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: ownRect.size.height color: line.colorIndex];
else
[self drawLineFromColumn: line.from toColumn: line.to inRect:ownRect offset: 0 color:line.colorIndex];
}
[self drawCircleForColumn: cellInfo.position inRect: ownRect];
if (cellInfo.refs)
[self drawRefsInRect:&rect];
[super drawWithFrame:rect inView:view];
isReady = NO;
}
@end