forked from gonzoua/AudioBookBinder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoverImageView.m
396 lines (327 loc) · 11.6 KB
/
CoverImageView.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
// CoverImageView.m
// AudioBookBinder
//
// Created by Oleksandr Tymoshenko on 10-05-07.
// Copyright 2010 Bluezbox Software. All rights reserved.
//
#import "CoverImageView.h"
@implementation CoverImageView
@synthesize coverImageFilename;
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
coverImage = nil;
highlighted = NO;
highlightedColor = [[NSColor blackColor] retain];
normalColor = [[NSColor colorWithCalibratedWhite:0.4 alpha:1] retain];
[self prepareAttributes];
string = NSLocalizedString(@"⌘ + I\nor\nDrag Image Here", nil);
[self registerForDraggedTypes:[NSArray arrayWithObjects:NSTIFFPboardType,
NSFilenamesPboardType, nil]];
coverImageFilename = nil;
}
return self;
}
- (void) dealloc {
[attributes release];
[super dealloc];
}
- (void) prepareAttributes {
attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:24]
forKey:NSFontAttributeName];
NSMutableParagraphStyle *centeredStyle =
[[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[centeredStyle setAlignment:NSCenterTextAlignment];
[attributes setObject:[[centeredStyle copy] autorelease]
forKey:NSParagraphStyleAttributeName];
[centeredStyle release];
}
- (void) resetImage
{
self.coverImageFilename = nil;
self.coverImage = nil;
}
- (BOOL) haveCover
{
return (coverImage != nil);
}
- (BOOL) shouldConvert
{
NSString *ext;
char ext_temp;
unsigned int ch;
// we care only about filename. If image was brough by dragging
// picture - it's converted to PNG
if (coverImageFilename == nil)
return YES;
for (ch = [coverImageFilename length];
((ext_temp = [coverImageFilename characterAtIndex:(ch - 1)]) != '.') && (ch >= 0); ch--)
;
ext = [[coverImageFilename lowercaseString] substringFromIndex:ch];
if ([ext isEqualToString:@"jpg"] || [ext isEqualToString:@"jpeg"])
return NO;
else if ([ext isEqualToString:@"png"])
return NO;
else // none of the above
return YES;
}
- (NSImage *) coverImage
{
return [coverImage retain];
}
- (void) setCoverImageFilename:(NSString *)imagePath
{
if (coverImageFilename) {
[coverImageFilename release];
coverImageFilename = nil;
}
if (imagePath) {
NSImage *img = [[NSImage alloc] initWithContentsOfFile:imagePath];
self.coverImage = img;
[img release];
// invalid image, do not set image path
if (img == nil)
return;
coverImageFilename = [imagePath retain];
}
}
- (void) setCoverImage:(NSImage *)image
{
[coverImage release];
[scaledImage release];
if (image == nil)
{
coverImage = nil;
scaledImage = nil;
return;
}
coverImage = [image copy];
NSImageRep *rep = [[coverImage representations] objectAtIndex:0];
[coverImage setScalesWhenResized:YES];
[coverImage setSize:NSMakeSize([rep pixelsWide], [rep pixelsHigh])];
NSSize origSize = NSMakeSize([rep pixelsWide], [rep pixelsHigh]);
if ((origSize.width > ITUNES_COVER_SIZE) || (origSize.height > ITUNES_COVER_SIZE)) {
NSSize scaledSize;
if (origSize.width > origSize.height) {
scaledSize.width = ITUNES_COVER_SIZE;
scaledSize.height = origSize.height * ITUNES_COVER_SIZE/origSize.width;
}
else {
scaledSize.height = ITUNES_COVER_SIZE;
scaledSize.width = origSize.width * ITUNES_COVER_SIZE/origSize.height;
}
scaledImage = [[[NSImage alloc] initWithSize:scaledSize] retain];
// Composite image appropriately
[scaledImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[coverImage drawInRect:NSMakeRect(0, 0, scaledSize.width, scaledSize.height)
fromRect:NSMakeRect(0, 0, origSize.width, origSize.height)
operation:NSCompositeSourceOver
fraction:1.0];
[scaledImage unlockFocus];
}
else {
scaledImage = [coverImage copy];
}
[self setNeedsDisplay:YES];
}
- (void)drawStringCenteredIn: (NSRect)r
{
NSSize strSize = [string sizeWithAttributes:attributes];
NSRect strRect;
strRect.origin.x = r.origin.x + (r.size.width - strSize.width)/2;
strRect.origin.y = r.origin.y + (r.size.height - strSize.height)/2;
strRect.size = strSize;
[string drawInRect:strRect withAttributes:attributes];
}
- (void)drawRect: (NSRect)dirtyRect
{
if (scaledImage == nil) {
NSColor *bgColor;
if(highlighted) {
bgColor = highlightedColor;
[attributes setObject:highlightedColor
forKey:NSForegroundColorAttributeName];
}
else {
bgColor = normalColor;
[attributes setObject:normalColor
forKey:NSForegroundColorAttributeName];
}
float borderWidth = 4.0;
NSRect boxRect = [self bounds];
NSRect bgRect = boxRect;
bgRect = NSInsetRect(boxRect, borderWidth / 2.0, borderWidth / 2.0);
bgRect = NSIntegralRect(bgRect);
bgRect.origin.x += 0.5;
bgRect.origin.y += 0.5;
int minX = NSMinX(bgRect);
int midX = NSMidX(bgRect);
int maxX = NSMaxX(bgRect);
int minY = NSMinY(bgRect);
int midY = NSMidY(bgRect);
int maxY = NSMaxY(bgRect);
float radius = 25.0;
NSBezierPath *bgPath = [NSBezierPath bezierPath];
// Bottom edge and bottom-right curve
[bgPath moveToPoint:NSMakePoint(midX, minY)];
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY)
toPoint:NSMakePoint(maxX, midY)
radius:radius];
// Right edge and top-right curve
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, maxY)
toPoint:NSMakePoint(midX, maxY)
radius:radius];
// Top edge and top-left curve
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY)
toPoint:NSMakePoint(minX, midY)
radius:radius];
// Left edge and bottom-left curve
[bgPath appendBezierPathWithArcFromPoint:bgRect.origin
toPoint:NSMakePoint(midX, minY)
radius:radius];
[bgPath closePath];
// [bgPath fill];
[bgColor set];
[bgPath setLineWidth:borderWidth];
CGFloat arr[2];
arr[0] = 5.0;
arr[1] = 2.0;
[bgPath setLineDash:arr count:2 phase:0.0];
[bgPath setLineCapStyle:NSRoundLineCapStyle];
[bgPath stroke];
[self drawStringCenteredIn:[self bounds]];
}
else {
NSRect viewSize = [self bounds];
NSSize imageSize = [scaledImage size];
NSPoint orig;
orig.x = (viewSize.size.width - imageSize.width) / 2;
orig.y = (viewSize.size.height - imageSize.height) / 2;
[scaledImage compositeToPoint:orig operation:NSCompositeSourceOver];
}
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
if ([sender draggingSource] == self) {
return NSDragOperationNone;
}
NSPasteboard *paste = [sender draggingPasteboard];
//gets the dragging-specific pasteboard from the sender
NSArray *types = [NSArray arrayWithObjects:NSTIFFPboardType,
NSFilenamesPboardType, nil];
//a list of types that we can accept
NSString *desiredType = [paste availableTypeFromArray:types];
NSData *carriedData = [paste dataForType:desiredType];
if (nil == carriedData)
return NSDragOperationNone;
if ([desiredType isEqualToString:NSFilenamesPboardType])
{
//we have a list of file names in an NSData object
NSArray *fileArray = [paste propertyListForType:@"NSFilenamesPboardType"];
if ([fileArray count] > 1) {
NSLog(@"multiple files");
return NSDragOperationNone;
}
}
highlighted = YES;
[self setNeedsDisplay:YES];
NSLog(@"draggingEntered: %d", highlighted);
return NSDragOperationGeneric;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
NSLog(@"draggingExited:");
highlighted = NO;
[self setNeedsDisplay:YES];
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
NSLog(@"performDragOperation");
NSPasteboard *paste = [sender draggingPasteboard];
//gets the dragging-specific pasteboard from the sender
NSArray *types = [NSArray arrayWithObjects:NSTIFFPboardType,
NSFilenamesPboardType, nil];
//a list of types that we can accept
NSString *desiredType = [paste availableTypeFromArray:types];
NSData *carriedData = [paste dataForType:desiredType];
if (nil == carriedData)
{
//the operation failed for some reason
NSRunAlertPanel(@"Paste Error", @"Sorry, but the past operation failed",
nil, nil, nil);
return NO;
}
else
{
NSImage *newImage = nil;
//the pasteboard was able to give us some meaningful data
if ([desiredType isEqualToString:NSTIFFPboardType])
{
[self resetImage];
//we have TIFF bitmap data in the NSData object
newImage = [[NSImage alloc] initWithData:carriedData];
self.coverImage = newImage;
[newImage release];
if (newImage == nil)
return NO;
}
else if ([desiredType isEqualToString:NSFilenamesPboardType])
{
//we have a list of file names in an NSData object
NSArray *fileArray = [paste propertyListForType:@"NSFilenamesPboardType"];
//be caseful since this method returns id.
//We just happen to know that it will be an array.
NSString *path = [fileArray objectAtIndex:0];
//assume that we can ignore all but the first path in the list
self.coverImageFilename = path;
if (coverImageFilename == nil)
return NO;
}
else
{
//this can't happen
NSAssert(NO, @"This can't happen");
return NO;
}
}
return YES;
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
NSLog(@"concludeDragOperation:");
highlighted = NO;
[self setNeedsDisplay:YES];
}
- (void)keyDown:(NSEvent*)event_ {
BOOL isDeleteKey = FALSE;
NSString *eventCharacters = [event_ characters];
if ([eventCharacters length]) {
switch ([eventCharacters characterAtIndex:0]) {
case NSDeleteFunctionKey:
case NSDeleteCharFunctionKey:
case NSDeleteCharacter:
isDeleteKey = YES;
break;
default:
break;
}
}
if (isDeleteKey) {
[self resetImage];
[self setNeedsDisplay:YES];
} else {
[super keyDown:event_];
}
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
@end