-
Notifications
You must be signed in to change notification settings - Fork 30
/
BlogAccountCell.m
270 lines (199 loc) · 7.6 KB
/
BlogAccountCell.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//
// BlocAccountCell.m
// Journler
//
// Created by Philip Dow on 4/3/07.
// Copyright 2007 Sprouted, Philip Dow. 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 the author nor the names of its 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 HOLDER 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.
*/
// Basically, you can use the code in your free, commercial, private and public projects
// as long as you include the above notice and attribute the code to Philip Dow / Sprouted
// If you use this code in an app send me a note. I'd love to know how the code is used.
// Please also note that this copyright does not supersede any other copyrights applicable to
// open source code used herein. While explicit credit has been given in the Journler about box,
// it may be lacking in some instances in the source code. I will remedy this in future commits,
// and if you notice any please point them out.
#import "BlogAccountCell.h"
#import <SproutedUtilities/SproutedUtilities.h>
/*
#import "NSBezierPath_AMShading.h"
#import "NSBezierPath_AMAdditons.h"
*/
@implementation BlogAccountCell
- (id)initWithCoder:(NSCoder *)decoder {
if ( self = [super initWithCoder:decoder] )
{
imageSize = NSMakeSize(32,32);
}
return self;
}
- (id)initTextCell:(NSString *)aString {
if ( self = [super initTextCell:aString] )
{
imageSize = NSMakeSize(32,32);
}
return self;
}
- (void)dealloc {
[image release];
image = nil;
if ( _paragraph ) [_paragraph release];
_paragraph = nil;
[super dealloc];
}
- copyWithZone:(NSZone *)zone {
BlogAccountCell *cell = (BlogAccountCell *)[super copyWithZone:zone];
cell->image = [image retain];
cell->_paragraph = [_paragraph retain];
cell->blogType = [blogType retain];
cell->imageSize = imageSize;
cell->selected = selected;
return cell;
}
- (NSSize) imageSize
{
return imageSize;
}
- (void) setImageSize:(NSSize)aSize
{
imageSize = aSize;
}
- (NSImage *)image
{
return image;
}
- (void)setImage:(NSImage *)anImage
{
if (anImage != image)
{
[image release];
image = [anImage retain];
}
}
- (NSString*) blogType
{
return blogType;
}
- (void) setBlogType:(NSString*)aString
{
if ( blogType != aString )
{
[blogType release];
blogType = [aString copyWithZone:[self zone]];
}
}
- (BOOL) isSelected
{
return selected;
}
- (void) setSelected:(BOOL)isSelected
{
selected = isSelected;
}
#pragma mark -
- (NSRect)imageFrameForCellFrame:(NSRect)cellFrame {
if (image != nil) {
NSRect imageFrame;
imageFrame.size = [self imageSize];
imageFrame.origin = cellFrame.origin;
imageFrame.origin.x += 3;
imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
return imageFrame;
}
else
return NSZeroRect;
}
- (NSSize)cellSize {
NSSize cellSize = [super cellSize];
cellSize.width += (image ? [self imageSize].width : 0) + 3;
return cellSize;
}
#pragma mark -
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
if (image != nil) {
NSSize myImageSize;
NSRect imageFrame;
myImageSize = [self imageSize];
NSDivideRect(cellFrame, &imageFrame, &cellFrame, 3 + myImageSize.width, NSMinXEdge);
imageFrame.origin.x += 3;
imageFrame.size = myImageSize;
if ([controlView isFlipped])
imageFrame.origin.y += ceil((cellFrame.size.height + imageFrame.size.height) / 2);
else
imageFrame.origin.y += ceil((cellFrame.size.height - imageFrame.size.height) / 2);
//[image compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
//#warning this seems like a lot of extra work just because a newly created item draws flipped
NSImage *imageCopy = [[[NSImage alloc] initWithSize:[self imageSize]] autorelease];
//[imageCopy setFlipped:[controlView isFlipped]];
[imageCopy lockFocus];
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[image drawInRect:NSMakeRect(0,0,[self imageSize].width,[self imageSize].height) fromRect:NSMakeRect(0,0,[image size].width,[image size].height)
operation:NSCompositeSourceOver fraction:1.0];
[[NSGraphicsContext currentContext] restoreGraphicsState];
[imageCopy unlockFocus];
//[imageCopy drawInRect:imageFrame fromRect:NSMakeRect(0,0,[imageCopy size].width,[imageCopy size].height) operation:NSCompositeSourceOver fraction:1.0];
[imageCopy compositeToPoint:imageFrame.origin operation:NSCompositeSourceOver];
}
[super drawWithFrame:cellFrame inView:controlView];
}
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
CGFloat textHeight;
NSRect inset = cellFrame;
NSMutableDictionary *attrs;
NSAttributedString *attrStringValue = [self attributedStringValue];
if ( attrStringValue != nil && [attrStringValue length] != 0)
attrs = [[[attrStringValue attributesAtIndex:0 effectiveRange:NULL] mutableCopyWithZone:[self zone]] autorelease];
else
attrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:11], NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName, nil];
// paragraph attribute
if ( _paragraph == nil ) {
_paragraph = [[NSParagraphStyle defaultParagraphStyle] mutableCopyWithZone:[self zone]];
[_paragraph setLineBreakMode:NSLineBreakByTruncatingTail];
}
[attrs setValue:_paragraph forKey:NSParagraphStyleAttributeName];
if ([self isSelected])
[attrs setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
else
[attrs setValue:[self textColor] forKey:NSForegroundColorAttributeName];
// modify the inset some
inset.origin.x += 2;
inset.size.width -= 4;
// center the text and take into account the required inset
textHeight = [[self stringValue] sizeWithAttributes:attrs].height;
inset.origin.y = inset.origin.y + (inset.size.height/2 - textHeight/2);
inset.size.height = textHeight;
NSRect titleRect = inset;
NSRect typeRect = inset;
titleRect.origin.y -= textHeight/2;
typeRect.origin.y += textHeight/2;
// actually draw the title
[[self stringValue] drawInRect:titleRect withAttributes:attrs];
if ([self isSelected])
[attrs setValue:[NSColor whiteColor] forKey:NSForegroundColorAttributeName];
else
[attrs setValue:[NSColor darkGrayColor] forKey:NSForegroundColorAttributeName];
[[self blogType] drawInRect:typeRect withAttributes:attrs];
}
@end