snej / Murky
A GUI client app for the Mercurial version-control system. Written for Mac OS X, in Objective-C.
| commit 146: | a6d76f31c381 |
| parent 145: | 56680411fdd9 |
| branch: | default |
adding a new icon/text cell class, this one sets the text to the vertical center off the cell
Changed (Δ3.1 KB):
raw changeset »
Source/IconTextCell.h (20 lines added, 0 lines removed)
Source/IconTextCell.m (107 lines added, 0 lines removed)
Up to file-list Source/IconTextCell.h:
1 |
// |
|
2 |
// IconTextCell.h |
|
3 |
// Murky |
|
4 |
// |
|
5 |
// Created by David Keegan on 1/5/10. |
|
6 |
// Copyright 2010 InScopeApps{+}. All rights reserved. |
|
7 |
// |
|
8 |
// By: Jim McGowan |
|
9 |
// From: http://www.cocoadev.com/index.pl?VerticallyCenteringTableViewItems |
|
10 |
// |
|
11 |
||
12 |
#import <Cocoa/Cocoa.h> |
|
13 |
||
14 |
@interface IconTextCell : NSTextFieldCell { |
|
15 |
NSImage *icon; |
|
16 |
} |
|
17 |
||
18 |
- (void)setIcon:(NSImage *)newIcon; |
|
19 |
||
20 |
@end |
Up to file-list Source/IconTextCell.m:
1 |
// |
|
2 |
// IconTextCell.m |
|
3 |
// Murky |
|
4 |
// |
|
5 |
// Created by David Keegan on 1/5/10. |
|
6 |
// Copyright 2010 InScopeApps{+}. All rights reserved. |
|
7 |
// |
|
8 |
||
9 |
#import "IconTextCell.h" |
|
10 |
||
11 |
void defineTextAndIconRects(NSRect cellRect, NSRect *imageRect, NSRect *stringRect, float stringHeight); |
|
12 |
||
13 |
@implementation IconTextCell |
|
14 |
||
15 |
- (id)init { |
|
16 |
self = [super initTextCell:@""]; |
|
17 |
||
18 |
icon = nil; |
|
19 |
||
20 |
[self setEditable:YES]; |
|
21 |
[self setScrollable:YES]; |
|
22 |
[self setLineBreakMode:NSLineBreakByTruncatingTail]; |
|
23 |
||
24 |
return self; |
|
25 |
} |
|
26 |
||
27 |
- (id)copyWithZone:(NSZone *)zone { |
|
28 |
IconTextCell *copy = [[IconTextCell allocWithZone:zone] init]; |
|
29 |
[copy setAttributedStringValue:[self attributedStringValue]]; |
|
30 |
[copy setIcon:icon]; |
|
31 |
return copy; |
|
32 |
} |
|
33 |
||
34 |
- (NSColor *)textColor { |
|
35 |
if([self isHighlighted]) |
|
36 |
{ |
|
37 |
return [NSColor selectedMenuItemTextColor]; |
|
38 |
} |
|
39 |
else |
|
40 |
{ |
|
41 |
return [NSColor controlTextColor]; |
|
42 |
} |
|
43 |
} |
|
44 |
||
45 |
- (void)drawInteriorWithFrame:(NSRect)aRect inView:(NSView *)controlView { |
|
46 |
NSRect imgRect, strRect; |
|
47 |
float strHeight = [[self attributedStringValue] size].height; |
|
48 |
defineTextAndIconRects(aRect, &imgRect, &strRect, strHeight); |
|
49 |
||
50 |
if([[self controlView] isFlipped]) |
|
51 |
{ |
|
52 |
[icon setFlipped:YES]; |
|
53 |
} |
|
54 |
[icon drawInRect:imgRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0f]; |
|
55 |
||
56 |
[[self attributedStringValue] drawInRect:strRect]; |
|
57 |
} |
|
58 |
||
59 |
- (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength { |
|
60 |
NSRect imgRect, strRect; |
|
61 |
float strHeight = [[self attributedStringValue] size].height; |
|
62 |
defineTextAndIconRects(aRect, &imgRect, &strRect, strHeight); |
|
63 |
||
64 |
[super selectWithFrame:strRect inView:controlView editor:textObj delegate:anObject start:selStart length:selLength]; |
|
65 |
[textObj setTextColor:[NSColor controlTextColor]]; |
|
66 |
} |
|
67 |
||
68 |
- (void)setObjectValue:(id <NSCopying, NSObject>)object { |
|
69 |
// change this part to suit your own use... |
|
70 |
if([object isKindOfClass:[NSDictionary class]]) |
|
71 |
{ |
|
72 |
[self setStringValue:[(NSDictionary *)object valueForKey:@"name"]]; |
|
73 |
[self setIcon:[NSUnarchiver unarchiveObjectWithData:[(NSDictionary *)object valueForKey:@"image"]]]; |
|
74 |
} |
|
75 |
else |
|
76 |
{ |
|
77 |
[super setObjectValue:object]; |
|
78 |
} |
|
79 |
||
80 |
} |
|
81 |
||
82 |
- (void)setIcon:(NSImage *)newIcon { |
|
83 |
if(newIcon != icon) |
|
84 |
{ |
|
85 |
[icon release]; |
|
86 |
icon = [newIcon copy]; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
- (void)dealloc { |
|
91 |
[icon release]; |
|
92 |
[super dealloc]; |
|
93 |
} |
|
94 |
||
95 |
@end |
|
96 |
||
97 |
void defineTextAndIconRects(NSRect cellRect, NSRect *imageRect, NSRect *stringRect, float stringHeight) { |
|
98 |
float cellHeight = cellRect.size.height; |
|
99 |
||
100 |
NSDivideRect(cellRect, imageRect, stringRect, cellHeight, NSMinXEdge); |
|
101 |
||
102 |
*imageRect = NSInsetRect(*imageRect, 2.0f, 2.0f); |
|
103 |
*stringRect = NSInsetRect(*stringRect, 2.0f, 0.0f); |
|
104 |
||
105 |
stringRect -> origin.y += (stringRect -> size.height - stringHeight) / 2.0f; |
|
106 |
stringRect -> size.height = stringHeight; |
|
107 |
} |
