1 | //
|
---|
2 | // ICeCoffEELabeledIconCell.m
|
---|
3 | // ICeCoffEE
|
---|
4 | //
|
---|
5 | // Created by Nicholas Riley on 6/8/07.
|
---|
6 | // Copyright 2007 Nicholas Riley. All rights reserved.
|
---|
7 | //
|
---|
8 |
|
---|
9 | #import "ICeCoffEELabeledIconCell.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | @implementation ICeCoffEELabeledIconCell
|
---|
13 |
|
---|
14 | const static float LEFT_PADDING = 3;
|
---|
15 | const static float PADDING = 5;
|
---|
16 | const static NSSize ICON_SIZE = {16, 16};
|
---|
17 |
|
---|
18 | - (ICeCoffEELabeledIconCell *)justCopySuper;
|
---|
19 | {
|
---|
20 | return [super copy];
|
---|
21 | }
|
---|
22 |
|
---|
23 | + (ICeCoffEELabeledIconCell *)copyFromTextFieldCell:(NSTextFieldCell *)cell;
|
---|
24 | {
|
---|
25 | ((struct objc_object *)cell)->isa = self;
|
---|
26 | ICeCoffEELabeledIconCell *copy = [(ICeCoffEELabeledIconCell *)cell justCopySuper];
|
---|
27 | copy->iconRef = NULL;
|
---|
28 | ((struct objc_object *)cell)->isa = [NSTextFieldCell class];
|
---|
29 | return [copy autorelease];
|
---|
30 | }
|
---|
31 |
|
---|
32 | - (void)dealloc;
|
---|
33 | {
|
---|
34 | if (iconRef != NULL) {
|
---|
35 | ReleaseIconRef(iconRef);
|
---|
36 | iconRef = NULL;
|
---|
37 | }
|
---|
38 | [super dealloc];
|
---|
39 | }
|
---|
40 |
|
---|
41 | - (id)copyWithZone:(NSZone *)zone;
|
---|
42 | {
|
---|
43 | ICeCoffEELabeledIconCell *cell = [super copyWithZone: zone];
|
---|
44 | cell->iconRef = iconRef;
|
---|
45 | if (iconRef != NULL)
|
---|
46 | AcquireIconRef(iconRef);
|
---|
47 | return cell;
|
---|
48 | }
|
---|
49 |
|
---|
50 | - (void)setIconRef:(IconRef)anIconRef;
|
---|
51 | {
|
---|
52 | if (anIconRef == iconRef)
|
---|
53 | return;
|
---|
54 | if (iconRef != NULL)
|
---|
55 | ReleaseIconRef(iconRef);
|
---|
56 | if (anIconRef != NULL)
|
---|
57 | AcquireIconRef(anIconRef);
|
---|
58 | iconRef = anIconRef;
|
---|
59 | }
|
---|
60 |
|
---|
61 | - (IconRef)iconRef;
|
---|
62 | {
|
---|
63 | return iconRef;
|
---|
64 | }
|
---|
65 |
|
---|
66 | - (NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
|
---|
67 | {
|
---|
68 | return nil;
|
---|
69 | }
|
---|
70 |
|
---|
71 | - (void)editWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent;
|
---|
72 | {
|
---|
73 | NSRect textFrame, imageFrame;
|
---|
74 | NSDivideRect(aRect, &imageFrame, &textFrame,
|
---|
75 | PADDING + ICON_SIZE.width, NSMinXEdge);
|
---|
76 | [super editWithFrame: textFrame inView: controlView
|
---|
77 | editor: textObj delegate: anObject event: theEvent];
|
---|
78 | }
|
---|
79 |
|
---|
80 | - (void)selectWithFrame:(NSRect)aRect inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(int)selStart length:(int)selLength;
|
---|
81 | {
|
---|
82 | NSRect textFrame, imageFrame;
|
---|
83 | NSDivideRect(aRect, &imageFrame, &textFrame, PADDING + ICON_SIZE.width,
|
---|
84 | NSMinXEdge);
|
---|
85 | [super selectWithFrame: textFrame inView: controlView editor:textObj delegate: anObject start: selStart length: selLength];
|
---|
86 | }
|
---|
87 |
|
---|
88 | - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView;
|
---|
89 | {
|
---|
90 | if (iconRef != NULL) {
|
---|
91 | NSRect imageFrame;
|
---|
92 | BOOL flipped = [controlView isFlipped];
|
---|
93 |
|
---|
94 | NSDivideRect(cellFrame, &imageFrame, &cellFrame, PADDING + ICON_SIZE.width, NSMinXEdge);
|
---|
95 | if ([self drawsBackground]) {
|
---|
96 | [[self backgroundColor] set];
|
---|
97 | NSRectFill(imageFrame);
|
---|
98 | }
|
---|
99 | imageFrame.origin.x += LEFT_PADDING;
|
---|
100 | imageFrame.size = ICON_SIZE;
|
---|
101 |
|
---|
102 | imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height + 2) / 2);
|
---|
103 |
|
---|
104 | NSRect imageRect = NSOffsetRect(imageFrame, -imageFrame.origin.x, -imageFrame.origin.y);
|
---|
105 | CGContextRef context = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
|
---|
106 | CGContextTranslateCTM(context, imageFrame.origin.x, imageFrame.origin.y);
|
---|
107 | if (flipped)
|
---|
108 | CGContextScaleCTM(context, 1, -1);
|
---|
109 | PlotIconRefInContext(context, (CGRect *)&imageRect, kAlignNone, kTransformNone,
|
---|
110 | NULL /*inLabelColor*/, kPlotIconRefNormalFlags, iconRef);
|
---|
111 | if (flipped)
|
---|
112 | CGContextScaleCTM(context, 1, -1);
|
---|
113 | CGContextTranslateCTM(context, -imageFrame.origin.x, -imageFrame.origin.y);
|
---|
114 | }
|
---|
115 | [super drawWithFrame: cellFrame inView: controlView];
|
---|
116 | }
|
---|
117 |
|
---|
118 | - (NSSize)cellSize;
|
---|
119 | {
|
---|
120 | NSSize cellSize = [super cellSize];
|
---|
121 | cellSize.width += (iconRef != NULL ? (ICON_SIZE.width + PADDING): 0);
|
---|
122 | return cellSize;
|
---|
123 | }
|
---|
124 |
|
---|
125 | @end
|
---|