forked from iMediaSandboxing/iMedia
/
IMBImageBrowserView.m
472 lines (337 loc) · 13.8 KB
/
IMBImageBrowserView.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
iMedia Browser Framework <http://karelia.com/imedia/>
Copyright (c) 2005-2011 by Karelia Software et al.
iMedia Browser is based on code originally developed by Jason Terhorst,
further developed for Sandvox by Greg Hulands, Dan Wood, and Terrence Talbot.
The new architecture for version 2.0 was developed by Peter Baumgartner.
Contributions have also been made by Matt Gough, Martin Wennerberg and others
as indicated in source files.
The iMedia Browser Framework is licensed under the following terms:
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in all or substantial portions of the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following
conditions:
Redistributions of source code must retain the original terms stated here,
including this list of conditions, the disclaimer noted below, and the
following copyright notice: Copyright (c) 2005-2011 by Karelia Software et al.
Redistributions in binary form must include, in an end-user-visible manner,
e.g., About window, Acknowledgments window, or similar, either a) the original
terms stated here, including this list of conditions, the disclaimer noted
below, and the aforementioned copyright notice, or b) the aforementioned
copyright notice and a link to karelia.com/imedia.
Neither the name of Karelia Software, nor Sandvox, nor the names of
contributors to iMedia Browser may be used to endorse or promote products
derived from the Software without prior and express written permission from
Karelia Software or individual contributors, as appropriate.
Disclaimer: THE SOFTWARE IS PROVIDED BY THE COPYRIGHT OWNER AND CONTRIBUTORS
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION WITH, THE
SOFTWARE OR THE USE OF, OR OTHER DEALINGS IN, THE SOFTWARE.
*/
// Author: Peter Baumgartner, Dan Wood, Daniel Jalkut
//----------------------------------------------------------------------------------------------------------------------
#pragma mark HEADERS
#import "IMBImageBrowserView.h"
#import "IMBImageBrowserCell.h"
#import "IMBObject.h"
#import "IMBButtonObject.h"
#import "IMBObjectViewController.h"
#import "IMBObjectFifoCache.h"
#import "IMBParser.h"
#import "IMBQLPreviewPanel.h"
#import <Carbon/Carbon.h>
#import "IMBPanelController.h"
#import "IMBConfig.h"
//----------------------------------------------------------------------------------------------------------------------
#pragma mark CONSTANTS
enum IMBMouseOperation
{
kMouseOperationNone,
kMouseOperationButtonClick,
kMouseOperationDragSelection
};
//----------------------------------------------------------------------------------------------------------------------
// Declare internal methods of superclass to shut up the compiler...
@interface IKImageBrowserView ()
- (NSImage*) draggedImage;
@end
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
@implementation IMBImageBrowserView
@synthesize mouseOperation = _mouseOperation;
@synthesize clickedObjectIndex = _clickedObjectIndex;
@synthesize clickedObject = _clickedObject;
//----------------------------------------------------------------------------------------------------------------------
- (void) _init // support method shared by both
{
_mouseOperation = kMouseOperationNone;
_clickedObjectIndex = NSNotFound;
_clickedObject = nil;
[[NSNotificationCenter defaultCenter] // Unload parsers before we quit, so that custom have
addObserver:self // a chance to clean up (e.g. remove callbacks, etc...)
selector:@selector(showTitlesStateChanged:)
name:kIMBImageBrowserShowTitlesNotification
object:nil];
// Set up initial value
NSString* filenames = [IMBConfig prefsValueForKey:@"prefersFilenamesInPhotoBasedBrowsers"];
BOOL showTitle = (nil == filenames) ? YES : [filenames boolValue];
int mask = showTitle ? IKCellsStyleTitled : IKCellsStyleNone;
[self setCellsStyleMask: mask];
}
- (id)initWithCoder:(NSCoder *)aDecoder;
{
if (self = [super initWithCoder:aDecoder])
{
[self _init];
}
return self;
}
- (id) initWithFrame:(NSRect) frame;
{
if (self = [super initWithFrame:frame])
{
[self _init];
}
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[NSObject cancelPreviousPerformRequestsWithTarget:self];
IMBRelease(_clickedObject);
[super dealloc];
}
//----------------------------------------------------------------------------------------------------------------------
- (void) showTitlesStateChanged:(NSNotification *)aNotification
{
id obj = [aNotification object];
BOOL showTitle = [obj boolValue];
int mask = showTitle ? IKCellsStyleTitled : IKCellsStyleNone;
[self setCellsStyleMask: mask];
}
//----------------------------------------------------------------------------------------------------------------------
// If the IKImageBrowserView asked for a custom cell class, then pass on the request to the library's delegate.
// That way the application is given a chance to customize the look of the browser...
- (Class) _cellClass
{
Class cellClass = nil;
id delegate = self.delegate;
if (delegate)
{
if ([delegate respondsToSelector:@selector(imageBrowserCellClassForController:)])
{
cellClass = [delegate imageBrowserCellClassForController:nil];
}
}
// Please note that we check for the existence of the base class before creating the subclass,
// as the baseclass is an undocumented internal class on 10.5. In 10.6 it is always there...
if (cellClass == nil)
{
if (NSClassFromString(@"IKImageBrowserCell") != nil)
{
cellClass = [IMBImageBrowserCell class];
}
}
return cellClass;
}
//----------------------------------------------------------------------------------------------------------------------
// Make the browser use our own custom cell class...
- (void) awakeFromNib
{
_cellClass = [self _cellClass];
if ([self respondsToSelector:@selector(setCellClass:)])
{
[self performSelector:@selector(setCellClass:) withObject:_cellClass];
}
[self setConstrainsToOriginalSize:NO];
// [self setValue:attributes forKey:IKImageBrowserCellsHighlightedTitleAttributesKey];
// [self setCellSize:NSMakeSize(44.0,22.0)];
// [self setIntercellSpacing:NSMakeSize(8.0,12.0)];
// [self setAnimates:NO];
// [self setWantsLayer:NO];
// NSColor* selectionColor = [NSColor selectedTextBackgroundColor];
// [self setValue:selectionColor forKey:IKImageBrowserSelectionColorKey];
}
// This method is for 10.6 only. Create and return a cell. Please note that we must not autorelease here!
- (IKImageBrowserCell*) newCellForRepresentedItem:(id)inCell
{
return [[_cellClass alloc] init];
}
//----------------------------------------------------------------------------------------------------------------------
// This makes sure that dragging to external applications works...
- (NSDragOperation) draggingSourceOperationMaskForLocal:(BOOL)inLocal
{
return NSDragOperationCopy;
}
// When creating the drag image first give the delegate of our controller a chance to create a custom drag image.
// If it declines, then simply return the default drag image created by Apple. Please note that we supply nil
// arguments to the delegate method here. These will be filled in by the controller...
- (NSImage*) draggedImage
{
NSImage* image = nil;
id delegate = self.delegate;
if (delegate)
{
if ([delegate respondsToSelector:@selector(draggedImageForController:draggedObjects:)])
{
image = [delegate draggedImageForController:nil draggedObjects:nil];
}
}
if (image == nil)
{
image = [super draggedImage];
}
return image;
}
//----------------------------------------------------------------------------------------------------------------------
// Disable background loading. This is an undocumented internal method. It was suggested to override this method
// by a developer on StackOverflow.com. otool -oV reveals that this method is available on 10.5 and 10.6, but
// we do not yet know whether the behavior is the same...
- (BOOL) _shouldProcessLongTasks
{
// If there still is plenty of space left in the cache we can allow some background loading, but do not
// fill it up all the way. Try to leave some space for when it is needed...
return [IMBObjectFifoCache count] < [IMBObjectFifoCache size]-64;
}
//----------------------------------------------------------------------------------------------------------------------
- (void) mouseDown:(NSEvent*)inEvent
{
// Find the clicked object...
NSPoint mouse = [self convertPoint:[inEvent locationInWindow] fromView:nil];
_clickedObjectIndex = [self indexOfItemAtPoint:mouse];
if (_clickedObjectIndex != NSNotFound && [self.dataSource respondsToSelector:@selector(imageBrowser:itemAtIndex:)])
{
self.clickedObject = [self.dataSource imageBrowser:self itemAtIndex:_clickedObjectIndex];
}
// If it was a button, then handle the click...
if ([_clickedObject isKindOfClass:[IMBButtonObject class]])
{
_mouseOperation = kMouseOperationButtonClick;
[(IMBButtonObject*)_clickedObject setImageRepresentationForState:YES];
[self setNeedsDisplayInRect:[self itemFrameAtIndex:_clickedObjectIndex]];
}
// In case of a normal object start dragging or selection...
else if (_clickedObject != nil && _clickedObject.isSelectable)
{
_mouseOperation = kMouseOperationDragSelection;
}
else
{
_mouseOperation = kMouseOperationNone;
}
// For parity with TableView; not really needed since selection is changed anyhow
IMBObjectViewController* controller = (IMBObjectViewController*) self.delegate;
[controller setClickedObject:self.clickedObject];
[controller setClickedObjectIndex:self.clickedObjectIndex];
[super mouseDown:inEvent];
}
- (void) mouseDragged:(NSEvent*)inEvent;
{
// If a button was clicked then track that button and highlight it when inside...
if (_mouseOperation == kMouseOperationButtonClick)
{
NSPoint mouse = [self convertPoint:[inEvent locationInWindow] fromView:nil];
BOOL highlighted = [self indexOfItemAtPoint: mouse] == _clickedObjectIndex;
[(IMBButtonObject*)_clickedObject setImageRepresentationForState:highlighted];
[self setNeedsDisplayInRect:[self itemFrameAtIndex:_clickedObjectIndex]];
}
// If the user clicked on an object, then this will start a drag. Ignore the drag if the object is
// not supposed to be draggable...
else if (_clickedObject)
{
[self.delegate setDropDestinationURL:nil]; // initialize to nil so we know drag has just started
if ([_clickedObject isDraggable])
{
[super mouseDragged:inEvent];
return;
}
}
// Let the superclass handle other events...
else
{
[super mouseDragged:inEvent];
}
}
- (void) mouseUp:(NSEvent*)inEvent
{
// If a button was clicked the perform the click action and remove the highlight...
if (_mouseOperation == kMouseOperationButtonClick)
{
NSPoint mouse = [self convertPoint:[inEvent locationInWindow] fromView:nil];
NSInteger objectIndex = [self indexOfItemAtPoint: mouse];
if (objectIndex == _clickedObjectIndex)
{
[(IMBButtonObject*)_clickedObject sendClickAction];
}
[(IMBButtonObject*)_clickedObject setImageRepresentationForState:NO];
[self setNeedsDisplayInRect:[self itemFrameAtIndex:_clickedObjectIndex]];
}
// Let the superclass handle other events...
else
{
[super mouseUp:inEvent];
}
// Cleanup...
_mouseOperation = kMouseOperationNone;
_dragSelectInProgress = NO;
self.clickedObject = nil;
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Dragging Promise Support
- (NSArray*) namesOfPromisedFilesDroppedAtDestination:(NSURL*)inDropDestination
{
return [self.delegate namesOfPromisedFilesDroppedAtDestination:inDropDestination];
}
- (void) draggedImage:(NSImage*)inImage endedAt:(NSPoint)inScreenPoint operation:(NSDragOperation)inOperation
{
[self.delegate draggedImage:inImage endedAt:inScreenPoint operation:inOperation];
}
//----------------------------------------------------------------------------------------------------------------------
#pragma mark
#pragma mark Quicklook
- (void) keyDown:(NSEvent*)inEvent
{
IMBObjectViewController* controller = (IMBObjectViewController*) self.delegate;
NSString* key = [inEvent charactersIgnoringModifiers];
if([key isEqual:@" "])
{
[controller quicklook:self];
}
else
{
[super keyDown:inEvent];
}
}
- (BOOL) acceptsPreviewPanelControl:(QLPreviewPanel*)inPanel
{
return YES;
}
- (void) beginPreviewPanelControl:(QLPreviewPanel*)inPanel
{
IMBObjectViewController* controller = (IMBObjectViewController*) self.delegate;
inPanel.delegate = controller;
inPanel.dataSource = controller;
if ([controller respondsToSelector:@selector(beginPreviewPanelControl:)])
{
[controller performSelector:@selector(beginPreviewPanelControl:) withObject:inPanel];
}
}
- (void) endPreviewPanelControl:(QLPreviewPanel*)inPanel
{
IMBObjectViewController* controller = (IMBObjectViewController*) self.delegate;
if ([controller respondsToSelector:@selector(endPreviewPanelControl:)])
{
[controller performSelector:@selector(endPreviewPanelControl:) withObject:inPanel];
}
inPanel.delegate = nil;
inPanel.dataSource = nil;
}
//----------------------------------------------------------------------------------------------------------------------
@end