forked from amaxwell/tlutility
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTLMMirrorCell.m
201 lines (170 loc) · 6.52 KB
/
TLMMirrorCell.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
201
//
// TLMMirrorCell.m
// TeX Live Utility
//
// Created by Adam R. Maxwell on 11/20/10.
/*
This software is Copyright (c) 2010-2016
Adam Maxwell. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
- Neither the name of Adam Maxwell nor the names of any
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "TLMMirrorCell.h"
#import <WebKit/WebKit.h>
@implementation TLMMirrorCell
#define FAVICON_INSET ((NSSize) { 2, 2 })
@synthesize icon = _icon;
- (id)initTextCell:(NSString *)aString
{
self = [super initTextCell:aString];
if (self) {
[self setScrollable:YES];
[self setLineBreakMode:NSLineBreakByTruncatingTail];
}
return self;
}
- (id)copyWithZone:(NSZone *)zone
{
TLMMirrorCell *copy = [super copyWithZone:zone];
[copy->_icon retain];
return copy;
}
- (void)dealloc
{
[_icon release];
[super dealloc];
}
- (void)iconCache:(TLMFaviconCache *)cache downloadedIcon:(NSImage *)anIcon forURL:(NSURL *)aURL;
{
[self setIcon:anIcon];
[[self controlView] setNeedsDisplay:YES];
}
- (NSImage *)_iconForURL:(NSURL *)aURL
{
return aURL ? [[TLMFaviconCache sharedCache] downloadIconForURL:aURL delegate:self] : nil;
}
- (void)setObjectValue:(id <NSCopying>)obj
{
NSImage *icon = nil;
if ([(id)obj isKindOfClass:[NSURL class]])
icon = [self _iconForURL:(NSURL *)obj];
else if ([(id)obj isKindOfClass:[NSString class]] && [(NSString *)obj isEqualToString:@""] == NO)
icon = [self _iconForURL:[NSURL URLWithString:(NSString *)obj]];
[self setIcon:icon];
[super setObjectValue:obj];
}
- (void)setStringValue:(NSString *)aString
{
NSURL *aURL = nil;
if (aString && [aString isEqualToString:@""] == NO)
aURL = [NSURL URLWithString:aString];
[self setIcon:[self _iconForURL:aURL]];
[super setStringValue:aString];
}
- (NSRect)drawingRectForBounds:(NSRect)theRect
{
NSRect drawingRect = [super drawingRectForBounds:theRect];
NSSize cellSize = [self cellSizeForBounds:theRect];
CGFloat offset = NSHeight(drawingRect) - cellSize.height;
if (offset > 0.5) {
drawingRect.size.height -= offset;
drawingRect.origin.y += (offset / 2);
}
return drawingRect;
}
- (NSRect)iconRectForBounds:(NSRect)cellFrame
{
NSRect iconRect = cellFrame;
iconRect.size.width = NSHeight(cellFrame);
return iconRect;
}
- (NSRect)textRectForBounds:(NSRect)cellFrame
{
NSRect iconRect = [self iconRectForBounds:cellFrame];
cellFrame.origin.x = NSMaxX(iconRect);
cellFrame.size.width -= NSWidth(iconRect);
return cellFrame;
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if ([self drawsBackground]) {
[NSGraphicsContext saveGraphicsState];
[[self backgroundColor] setFill];
NSRectFillUsingOperation(cellFrame, NSCompositeSourceOver);
[NSGraphicsContext restoreGraphicsState];
}
if ([self icon]) {
NSRect iconRect = [self iconRectForBounds:cellFrame];
CGContextRef ctxt = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(ctxt);
CGContextSetInterpolationQuality(ctxt, kCGInterpolationHigh);
CGContextSetShouldAntialias(ctxt, true);
if ([controlView isFlipped]) {
CGContextTranslateCTM(ctxt, 0, NSMaxY(iconRect));
CGContextScaleCTM(ctxt, 1, -1);
iconRect.origin.y = 0;
}
[[self icon] drawInRect:NSInsetRect(iconRect, FAVICON_INSET.width, FAVICON_INSET.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
CGContextRestoreGState(ctxt);
}
[super drawInteriorWithFrame:[self textRectForBounds:cellFrame] inView:controlView];
}
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
[super drawWithFrame:cellFrame inView:controlView];
if ([self showsFirstResponder]) {
[NSGraphicsContext saveGraphicsState];
NSSetFocusRingStyle(NSFocusRingAbove);
NSRectFill([self textRectForBounds:cellFrame]);
[NSGraphicsContext restoreGraphicsState];
}
}
- (NSFocusRingType)focusRingType { return NSFocusRingTypeNone; }
- (NSSize)cellSize;
{
NSSize cellSize = [super cellSize];
// cellSize.height approximates the icon size
cellSize.width += cellSize.height;
return cellSize;
}
#if 0
- (NSRect)expansionFrameWithFrame:(NSRect)cellFrame inView:(NSView *)view;
{
NSRect expansionFrame = [super expansionFrameWithFrame:cellFrame inView:view];
if (NSEqualRects(expansionFrame, NSZeroRect) == NO) {
expansionFrame.size = [self cellSize];
expansionFrame.size.height = NSHeight(cellFrame);
}
return expansionFrame;
}
#endif
- (void)editWithFrame:(NSRect)cellFrame inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject event:(NSEvent *)theEvent;
{
[super editWithFrame:[self textRectForBounds:cellFrame] inView:controlView editor:textObj delegate:anObject event:theEvent];
}
- (void)selectWithFrame:(NSRect)cellFrame inView:(NSView *)controlView editor:(NSText *)textObj delegate:(id)anObject start:(NSInteger)selStart length:(NSInteger)selLength;
{
[super selectWithFrame:[self textRectForBounds:cellFrame] inView:controlView editor:textObj delegate:anObject start:selStart length:selLength];
}
@end