5 // Created by Pieter de Bie on 17-06-08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "PBGitRevisionCell.h"
11 #import "RoundedRectangle.h"
13 @implementation PBGitRevisionCell
16 - (id) initWithCoder: (id) coder
18 self = [super initWithCoder:coder];
19 textCell = [[NSTextFieldCell alloc] initWithCoder:coder];
25 return [NSArray arrayWithObjects:
26 [NSColor colorWithCalibratedRed: 0X4e/256.0 green:0X9A/256.0 blue: 0X06/256.0 alpha: 1.0],
27 [NSColor colorWithCalibratedRed: 0X20/256.0 green:0X4A/256.0 blue: 0X87/256.0 alpha: 1.0],
28 [NSColor colorWithCalibratedRed: 0XC4/256.0 green:0XA0/256.0 blue: 0 alpha: 1.0],
29 [NSColor colorWithCalibratedRed: 0X5C/256.0 green:0X35/256.0 blue: 0X66/256.0 alpha: 1.0],
30 [NSColor colorWithCalibratedRed: 0XA4/256.0 green:0X00/256.0 blue: 0X00/256.0 alpha: 1.0],
31 [NSColor colorWithCalibratedRed: 0XCE/256.0 green:0X5C/256.0 blue: 0 alpha: 1.0],
35 - (void) drawLineFromColumn: (int) from toColumn: (int) to inRect: (NSRect) r offset: (int) offset color: (int) c
39 NSPoint origin = r.origin;
41 NSPoint source = NSMakePoint(origin.x + columnWidth* from, origin.y + offset);
42 NSPoint center = NSMakePoint( origin.x + columnWidth * to, origin.y + r.size.height * 0.5 + 0.5);
44 // Just use red for now.
45 NSArray* colors = [self colors];
46 [[colors objectAtIndex: c % [colors count]] set];
48 NSBezierPath * path = [NSBezierPath bezierPath];
49 [path setLineWidth:2];
51 [path moveToPoint: source];
52 [path lineToPoint: center];
57 - (void) drawCircleInRect: (NSRect) r
60 int c = cellInfo.position;
62 NSPoint origin = r.origin;
63 NSPoint columnOrigin = { origin.x + columnWidth * c, origin.y};
65 NSRect oval = { columnOrigin.x - 5, columnOrigin.y + r.size.height * 0.5 - 5, 10, 10};
68 NSBezierPath * path = [NSBezierPath bezierPathWithOvalInRect:oval];
70 [[NSColor blackColor] set];
73 NSRect smallOval = { columnOrigin.x - 3, columnOrigin.y + r.size.height * 0.5 - 3, 6, 6};
74 [[NSColor whiteColor] set];
75 path = [NSBezierPath bezierPathWithOvalInRect:smallOval];
79 - (void) drawTriangleInRect: (NSRect) r sign: (char) sign
81 int c = cellInfo.position;
82 int columnHeight = 10;
87 top.x = round(r.origin.x) + 10 * c + 4;
89 top.x = round(r.origin.x) + 10 * c - 4;
92 top.y = r.origin.y + (r.size.height - columnHeight) / 2;
94 NSBezierPath * path = [NSBezierPath bezierPath];
96 [path moveToPoint: NSMakePoint(top.x, top.y)];
98 [path lineToPoint: NSMakePoint(top.x, top.y + columnHeight)];
100 [path lineToPoint: NSMakePoint(top.x - columnWidth, top.y + columnHeight / 2)];
104 [[NSColor whiteColor] set];
106 [[NSColor blackColor] set];
107 [path setLineWidth: 2];
111 - (NSMutableDictionary*) attributesForRefLabelSelected: (BOOL) selected isCurrentBranch: (BOOL) curBranch
113 NSMutableDictionary *attributes = [[[NSMutableDictionary alloc] initWithCapacity:2] autorelease];
114 NSMutableParagraphStyle* style = [[[NSParagraphStyle defaultParagraphStyle] mutableCopy] autorelease];
118 fontName = @"Helvetica-Bold";
120 fontName = @"Helvetica";
122 [style setAlignment:NSCenterTextAlignment];
123 [attributes setObject:style forKey:NSParagraphStyleAttributeName];
124 [attributes setObject:[NSFont fontWithName:fontName size:9] forKey:NSFontAttributeName];
127 // [attributes setObject:[NSColor alternateSelectedControlTextColor] forKey:NSForegroundColorAttributeName];
132 - (NSColor*) colorForRef: (PBGitRef*) ref
134 NSString* type = [ref type];
135 if ([type isEqualToString:@"head"])
136 return [NSColor colorWithCalibratedRed: 0Xaa/256.0 green:0Xf2/256.0 blue: 0X54/256.0 alpha: 1.0];
137 else if ([type isEqualToString:@"remote"])
138 return [NSColor colorWithCalibratedRed: 0xb2/256.0 green:0Xdf/256.0 blue: 0Xff/256.0 alpha: 1.0];
139 else if ([type isEqualToString:@"tag"])
140 return [NSColor colorWithCalibratedRed: 0Xfc/256.0 green:0Xed/256.0 blue: 0X4f/256.0 alpha: 1.0];
142 return [NSColor yellowColor];
145 -(NSArray *)rectsForRefsinRect:(NSRect) rect;
147 NSMutableArray *array = [NSMutableArray array];
149 static const int ref_padding = 10;
150 static const int ref_spacing = 2;
152 NSRect lastRect = rect;
153 lastRect.origin.x = round(lastRect.origin.x) - 0.5;
154 lastRect.origin.y = round(lastRect.origin.y) - 0.5;
156 for (PBGitRef *ref in self.objectValue.refs) {
157 NSMutableDictionary* attributes = [self attributesForRefLabelSelected:NO isCurrentBranch:NO];
158 NSSize textSize = [[ref shortName] sizeWithAttributes:attributes];
160 NSRect newRect = lastRect;
161 newRect.size.width = textSize.width + ref_padding;
162 newRect.size.height = textSize.height;
163 newRect.origin.y = rect.origin.y + (rect.size.height - newRect.size.height) / 2;
165 [array addObject:[NSValue valueWithRect:newRect]];
167 lastRect.origin.x += (int)lastRect.size.width + ref_spacing;
173 - (void) drawLabelAtIndex:(int)index inRect:(NSRect)rect
175 NSArray *refs = self.objectValue.refs;
176 PBGitRef *ref = [refs objectAtIndex:index];
177 BOOL isCurBranch = [ref.ref isEqualToString:[[[controller repository] headRef] simpleRef]];
179 NSMutableDictionary* attributes = [self attributesForRefLabelSelected:[self isHighlighted]
180 isCurrentBranch:isCurBranch];
181 NSBezierPath *border = [NSBezierPath bezierPathWithRoundedRect:rect cornerRadius: 2.0];
182 [[self colorForRef:ref] set];
185 [[ref shortName] drawInRect:rect withAttributes:attributes];
189 - (void) drawRefsInRect: (NSRect *)refRect
191 [[NSColor blackColor] setStroke];
195 for (NSValue *rectValue in [self rectsForRefsinRect:*refRect])
197 NSRect rect = [rectValue rectValue];
198 [self drawLabelAtIndex:index inRect:rect];
202 refRect->size.width -= lastRect.origin.x - refRect->origin.x + lastRect.size.width;
203 refRect->origin.x = lastRect.origin.x + lastRect.size.width;
206 - (void) drawWithFrame: (NSRect) rect inView:(NSView *)view
208 cellInfo = [self.objectValue lineInfo];
210 if (cellInfo && ![controller hasNonlinearPath]) {
211 float pathWidth = 10 + 10 * cellInfo.numColumns;
214 NSDivideRect(rect, &ownRect, &rect, pathWidth, NSMinXEdge);
217 struct PBGitGraphLine *lines = cellInfo.lines;
218 for (i = 0; i < cellInfo.nLines; i++) {
219 if (lines[i].upper == 0)
220 [self drawLineFromColumn: lines[i].from toColumn: lines[i].to inRect:ownRect offset: ownRect.size.height color: lines[i].colorIndex];
222 [self drawLineFromColumn: lines[i].from toColumn: lines[i].to inRect:ownRect offset: 0 color:lines[i].colorIndex];
225 if (cellInfo.sign == '<' || cellInfo.sign == '>')
226 [self drawTriangleInRect: ownRect sign: cellInfo.sign];
228 [self drawCircleInRect: ownRect];
232 if ([self.objectValue refs] && [[self.objectValue refs] count])
233 [self drawRefsInRect:&rect];
235 // Still use this superclass because of hilighting differences
236 //_contents = [self.objectValue subject];
237 //[super drawWithFrame:rect inView:view];
238 [textCell setObjectValue: [self.objectValue subject]];
239 [textCell setHighlighted: [self isHighlighted]];
240 [textCell drawWithFrame:rect inView: view];
243 - (void) setObjectValue: (PBGitCommit*)object {
244 [super setObjectValue:[NSValue valueWithNonretainedObject:object]];
247 - (PBGitCommit*) objectValue {
248 return [[super objectValue] nonretainedObjectValue];
251 - (int) indexAtX:(float)x
253 cellInfo = [self.objectValue lineInfo];
255 if (cellInfo && ![controller hasNonlinearPath])
256 pathWidth = 10 + 10 * cellInfo.numColumns;
259 NSRect refRect = NSMakeRect(pathWidth, 0, 1000, 10000);
260 for (NSValue *rectValue in [self rectsForRefsinRect:refRect])
262 NSRect rect = [rectValue rectValue];
263 if (x >= rect.origin.x && x <= (rect.origin.x + rect.size.width))
271 - (NSRect) rectAtIndex:(int)index
273 cellInfo = [self.objectValue lineInfo];
275 if (cellInfo && ![controller hasNonlinearPath])
276 pathWidth = 10 + 10 * cellInfo.numColumns;
277 NSRect refRect = NSMakeRect(pathWidth, 0, 1000, 10000);
279 return [[[self rectsForRefsinRect:refRect] objectAtIndex:index] rectValue];
282 # pragma mark context menu delegate methods
284 - (NSMenu *) menuForEvent:(NSEvent *)event inRect:(NSRect)rect ofView:(NSView *)view
286 if (!contextMenuDelegate)
289 int i = [self indexAtX:[view convertPointFromBase:[event locationInWindow]].x];
293 id ref = [[[self objectValue] refs] objectAtIndex:i];
297 NSArray *items = [contextMenuDelegate menuItemsForRef:ref commit:[self objectValue]];
298 NSMenu *menu = [[NSMenu alloc] init];
299 for (NSMenuItem *item in items)