-
Notifications
You must be signed in to change notification settings - Fork 30
/
CollectionsSourceList.m
547 lines (406 loc) · 18.3 KB
/
CollectionsSourceList.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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
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 "CollectionsSourceList.h"
#import "Definitions.h"
#import "JournlerCollection.h"
#import "FoldersController.h"
//#import "BrowseArrayController.h"
#import "EntriesTableView.h"
#import <SproutedUtilities/SproutedUtilities.h>
#import <SproutedInterface/SproutedInterface.h>
/*
#import "NSOutlineView_Extensions.h"
#import "NSOutlineView_ProxyAdditions.h"
#import "NSBezierPath_AMShading.h"
#import "NSBezierPath_AMAdditons.h"
#import "NSImage_PDCategories.h"
#import "NSColor_JournlerAdditions.h"
#import "NSUserDefaults+PDDefaultsAdditions.h"
#import "JournlerGradientView.h"
static NSString *kMailMessagePboardType = @"MV Super-secret message transfer pasteboard type";
*/
#define kSearchIntervalDelay 1.5
@implementation CollectionsSourceList
- (void) awakeFromNib {
[self setIntercellSpacing:NSMakeSize(0.0,0.0)];
_searchString = [[NSMutableString alloc] init];
[self setAutoresizesOutlineColumn:NO];
// appearance bindings
[self bind:@"font" toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.FoldersTableFont" options:[NSDictionary dictionaryWithObjectsAndKeys:
@"NSUnarchiveFromData", NSValueTransformerNameBindingOption,
[NSFont controlContentFontOfSize:11], NSNullPlaceholderBindingOption, nil]];
[self bind:@"backgroundColor" toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.FolderBackgroundColor" options:[NSDictionary dictionaryWithObjectsAndKeys:
@"NSUnarchiveFromData", NSValueTransformerNameBindingOption,
[NSColor colorWithCalibratedHue:234.0/400.0 saturation:1.0/100.0 brightness:97.0/100.0 alpha:1.0], NSNullPlaceholderBindingOption, nil]];
// drag types
NSArray *draggedTypes = [NSArray arrayWithObjects: PDFolderIDPboardType, PDEntryIDPboardType,
NSFilenamesPboardType, WebURLsWithTitlesPboardType, NSURLPboardType, NSRTFDPboardType, NSRTFPboardType,
NSStringPboardType, NSTIFFPboardType, NSPICTPboardType, kMailMessagePboardType, nil];
[self registerForDraggedTypes:draggedTypes];
[self setDraggingSourceOperationMask:NSDragOperationCopy|NSDragOperationMove forLocal:YES];
[self setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
}
- (void) dealloc
{
#ifdef __DEBUG__
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
[self unregisterDraggedTypes];
[_searchString release];
[super dealloc];
}
- (void)keyDown:(NSEvent *)event
{
static unichar kUnicharKeyReturn = '\r';
static unichar kUnicharKeyNewline = '\n';
unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
if (key == NSDeleteCharacter && [self numberOfRows] > 0 && [self selectedRow] != -1)
{
// request a delete from the delegate
if ( [[self delegate] respondsToSelector:@selector(deleteSelectedFolder:)] )
[[self delegate] performSelector:@selector(deleteSelectedFolder:) withObject:self];
}
//#warning - what about opening into a new tab (but not new window)
else if ( ( key == kUnicharKeyReturn || key == kUnicharKeyNewline ) && ( [event modifierFlags] & NSControlKeyMask ) )
[super keyDown:event];
//else if ( key == NSRightArrowFunctionKey )
//{
// [[self window] makeFirstResponder: [[[self dataSource] browseController] entriesTable] ];
//}
// perform a search
else
{
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
if ( currentTime - _searchInterval > kSearchIntervalDelay )
[_searchString setString:@""];
_searchInterval = currentTime;
NSString *new_characters = [event characters];
if ( new_characters && [new_characters length] > 0 )
{
unichar a_char = [new_characters characterAtIndex:0];
if ( a_char >= 0xF700 && a_char <= 0xF8FF )
{
[super keyDown:event];
}
else
{
NSInteger i;
NSArray *objects = [[self dataSource] allObjects];
[_searchString appendString:new_characters];
for ( i = 0; i < [objects count]; i++ )
{
if ( [[[objects objectAtIndex:i] valueForKey:@"title"]
rangeOfString:_searchString options:NSCaseInsensitiveSearch].location == 0 )
{
[[self dataSource] selectCollection:[objects objectAtIndex:i] byExtendingSelection:NO];
[self scrollRowToVisible:i];
break;
}
}
}
}
else
{
[super keyDown:event];
}
}
}
- (void) textDidEndEditing: (NSNotification *) notification
{
NSDictionary *userInfo;
userInfo = [notification userInfo];
NSNumber *textMovement;
textMovement = [userInfo objectForKey: @"NSTextMovement"];
NSInteger movementCode;
movementCode = [textMovement integerValue];
// see if this a 'pressed-return' instance
if (movementCode == NSReturnTextMovement) {
// hijack the notification and pass a different textMovement
// value
textMovement = [NSNumber numberWithInteger: NSIllegalTextMovement];
NSDictionary *newUserInfo;
newUserInfo = [NSDictionary dictionaryWithObject: textMovement
forKey: @"NSTextMovement"];
notification = [NSNotification notificationWithName:
[notification name]
object: [notification object]
userInfo: newUserInfo];
}
[super textDidEndEditing: notification];
} // textDidEndEditing
-(void)_drawDropHighlightOnRow:(NSInteger)rowIndex
{
static float widthOffset = 5.0;
static float heightOffset = 3.0;
[self lockFocus];
NSRect drawRect = [self rectOfRow:rowIndex];
drawRect.size.width -= widthOffset;
drawRect.origin.x += widthOffset/2.0;
drawRect.size.height -= heightOffset;
drawRect.origin.y += heightOffset/2.0;
//[[[NSColor blueColor]colorWithAlphaComponent:0.2]set];
[[NSColor colorWithCalibratedRed:31.0/255.0 green:100.0/255.0 blue:205.0/255.0 alpha:0.2] set];
[NSBezierPath fillRoundRectInRect:drawRect radius:4.0];
//[[[NSColor blueColor]colorWithAlphaComponent:0.8]set];
[[NSColor colorWithCalibratedRed:0.0/255.0 green:64.0/255.0 blue:188.0/255.0 alpha:0.96] set];
[NSBezierPath setDefaultLineWidth:2.0];
[NSBezierPath strokeRoundRectInRect:drawRect radius:4.0];
[self unlockFocus];
}
- (id)_highlightColorForCell:(NSCell *)cell
{
return [self backgroundColor];
//return [NSColor colorWithCalibratedRed:210.0/255.0 green:216.0/255.0 blue:225.0/255.0 alpha:1.0];
//if(([[self window] firstResponder] == self) && [[self window] isMainWindow] && [[self window] isKeyWindow])
// return [NSColor colorWithCalibratedRed:61.0/256.0 green:128.0/256.0 blue:223.0/256.0 alpha:1.0];
//else
// return [NSColor secondarySelectedControlColor];
}
- (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect {
JournlerCollection *actualItem;
id anObject = [self itemAtRow:rowIndex];
// 10.4 hack, 10.5 compatible
if ( [anObject respondsToSelector:@selector(representedObject)] )
actualItem = [anObject representedObject];
else if ( [anObject respondsToSelector:@selector(observedObject)] )
actualItem = [anObject observedObject];
else
actualItem = anObject;
// ask the data source for the entry's label
NSNumber *labelColorVal = [actualItem valueForKey:@"label"];
// grab the draw rect
NSRect targetRect = [self rectOfRow:rowIndex];
if ( [[self selectedRowIndexes] containsIndex:rowIndex] )
{
NSRect gradientRect = targetRect;
gradientRect.size.height--;
[self _drawSelectionInRect:gradientRect
highlight: (([[self window] firstResponder] == self) && [[self window] isMainWindow] && [[self window] isKeyWindow]) ];
}
else if ( labelColorVal && [labelColorVal integerValue] != 0 )
{
NSColor *gradientStart = [NSColor colorForLabel:[labelColorVal integerValue] gradientEnd:NO];
NSColor *gradientEnd = [NSColor colorForLabel:[labelColorVal integerValue] gradientEnd:YES];
if ( gradientStart != nil && gradientEnd != nil )
{
targetRect.origin.x+=2.0;
targetRect.size.width-=3.0;
targetRect.size.height-=1.0;
[self lockFocus];
[[NSBezierPath bezierPathWithRoundedRect:targetRect cornerRadius:7.3]
linearGradientFillWithStartColor:gradientStart endColor:gradientEnd];
[self unlockFocus];
}
}
[super drawRow:rowIndex clipRect:clipRect];
}
- (void) _drawSelectionInRect:(NSRect)aRect highlight:(BOOL)highlight
{
//[JournlerGradientView drawGradientInView:self rect:aRect highlight:highlight];
NSColor *gradientStart, *gradientEnd;
if ( highlight )
{
gradientStart = [[NSColor colorWithCalibratedRed:136.0/255.0 green:165.0/255.0 blue:212.0/255.0 alpha:1.0]
blendedColorWithFraction:0.5 ofColor:[NSColor colorForControlTint:[NSColor currentControlTint]]];
gradientEnd = [[NSColor colorWithCalibratedRed:102.0/255.0 green:133.0/255.0 blue:183.0/255.0 alpha:1.0]
blendedColorWithFraction:0.5 ofColor:[NSColor colorForControlTint:[NSColor currentControlTint]]];
}
else
{
gradientStart = [[NSColor colorWithCalibratedRed:172.0/255.0 green:186.0/255.0 blue:207.0/255.0 alpha:0.9]
blendedColorWithFraction:0.5 ofColor:[NSColor colorForControlTint:[NSColor currentControlTint]]];
gradientEnd = [[NSColor colorWithCalibratedRed:152.0/255.0 green:170.0/255.0 blue:196.0/255.0 alpha:0.9]
blendedColorWithFraction:0.5 ofColor:[NSColor colorForControlTint:[NSColor currentControlTint]]];
}
[[NSBezierPath bezierPathWithRect:aRect] linearGradientFillWithStartColor:gradientStart endColor:gradientEnd];
}
/*
- (void) mouseDown:(NSEvent*)theEvent
{
NSPoint window_point = [theEvent locationInWindow];
NSPoint table_point = [self convertPoint:window_point fromView:nil];
NSUInteger row_selection = [self rowAtPoint:table_point];
if ( [theEvent clickCount] == 1 && row_selection == [self selectedRow]
&& [[self delegate] respondsToSelector:@selector(sourceList:didSelectRowAlreadySelected:event:)] )
{
if ( ![[self delegate] sourceList:self didSelectRowAlreadySelected:row_selection event:theEvent] )
// pass the event on
[super mouseDown:theEvent];
}
else
{
// pass the event on
[super mouseDown:theEvent];
}
}
*/
#pragma mark -
#pragma mark State Information
- (NSArray*) stateArray
{
NSMutableArray *state_array = [[[NSMutableArray alloc] initWithCapacity:[self numberOfRows]] autorelease];
NSInteger i;
for ( i = 0; i < [self numberOfRows]; i++ )
[state_array addObject:[NSNumber numberWithBool:[self isItemExpanded:[self itemAtRow:i]]]];
return state_array;
}
- (void) restoreStateFromArray:(NSArray*)anArray
{
if ( anArray == nil )
return;
NSInteger i;
for ( i = 0; i < [anArray count]; i++ )
{
if ( [[anArray objectAtIndex:i] boolValue] && i < [self numberOfRows] )
[self expandItem:[self itemAtRow:i] expandChildren:NO];
}
}
#pragma mark -
/*
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
NSLog(@"%s",__PRETTY_FUNCTION__);
return [sender draggingSourceOperationMask];
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
NSLog(@"%s",__PRETTY_FUNCTION__);
}
*/
#pragma mark -
//- (NSUInteger)draggingSourceOperationMaskForLocal:(BOOL)isLocal
//{
// NSUInteger mask;
// mask = ( isLocal ? NSDragOperationCopy | NSDragOperationMove : NSDragOperationCopy );
// return mask;
//}
- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows
tableColumns:(NSArray *)tableColumns
event:(NSEvent*)dragEvent
offset:(NSPointPointer)dragImageOffset
{
JournlerCollection *collection = [self originalItemAtRow:[dragRows firstIndex]];
if ( !collection ) return [super dragImageForRowsWithIndexes:dragRows
tableColumns:tableColumns event:dragEvent offset:dragImageOffset];
NSString *title = [collection valueForKey:@"title"];
if ( !title )
return [super dragImageForRowsWithIndexes:dragRows tableColumns:tableColumns event:dragEvent offset:dragImageOffset];
NSImage *icon = [[collection valueForKey:@"icon"] imageWithWidth:32 height:32 inset:0];
if ( !icon )
return [super dragImageForRowsWithIndexes:dragRows tableColumns:tableColumns event:dragEvent offset:dragImageOffset];
// if more than one row is being dragged, determine which dragBadge to use
NSImage *dragBadge = nil;
if ( [dragRows count] > 1 && [dragRows count] < 100 )
dragBadge = [[[NSImage imageNamed:@"dragBadge1.png"] copyWithZone:[self zone]] autorelease];
else if ( [dragRows count] >= 100 && [dragRows count] < 1000 )
dragBadge = [[[NSImage imageNamed:@"dragBadge2.png"] copyWithZone:[self zone]] autorelease];
else if ( [dragRows count] >= 1000 )
dragBadge = [[[NSImage imageNamed:@"dragBadge3.png"] copyWithZone:[self zone]] autorelease];
// Get the size of this thing
if ( dragBadge )
{
// draw the count if necessary
NSDictionary *countAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:[NSFont smallSystemFontSize]], NSFontAttributeName,
[NSColor colorWithCalibratedWhite:1.0 alpha:1.0], NSForegroundColorAttributeName, nil];
NSString *countString = [[NSNumber numberWithInteger:[dragRows count]] stringValue];
NSSize countSize = [countString sizeWithAttributes:countAttributes];
[dragBadge lockFocus];
[countString drawInRect:NSMakeRect( [dragBadge size].width/2 - countSize.width/2,
[dragBadge size].height/2 - countSize.height/2,
countSize.width, countSize.height ) withAttributes:countAttributes];
[dragBadge unlockFocus];
}
NSImage *returnImage;
NSShadow *shadow = [[[NSShadow alloc] init] autorelease];
[shadow setShadowColor:[NSColor colorWithCalibratedWhite:1.0 alpha:0.96]];
[shadow setShadowOffset:NSMakeSize(1,-1)];
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSFont systemFontOfSize:11], NSFontAttributeName,
[NSColor colorWithCalibratedWhite:0.01 alpha:1.0], NSForegroundColorAttributeName,
shadow, NSShadowAttributeName, nil];
NSSize iconSize = [icon size];
NSSize stringSize = [title sizeWithAttributes:attributes];
float myWidth = [self bounds].size.width;
float proposedWidth = iconSize.width+stringSize.width+20;
returnImage = [[NSImage alloc] initWithSize:NSMakeSize( ( myWidth > proposedWidth ? myWidth : proposedWidth ) ,
(iconSize.height >= stringSize.height ? iconSize.height : stringSize.height) + 6)];
[returnImage lockFocus];
//NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
//[graphicsContext saveGraphicsState];
//[shadow set];
[[NSColor colorWithCalibratedWhite:0.25 alpha:0.3] set];
//NSRectFill(NSMakeRect(0,6,[returnImage size].width,[returnImage size].height-6));
[[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,6,[returnImage size].width,[returnImage size].height-6) cornerRadius:10.0] fill];
[[NSColor colorWithCalibratedWhite:0.5 alpha:0.3] set];
//NSFrameRect(NSMakeRect(0,6,[returnImage size].width,[returnImage size].height-6));
[[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(0,6,[returnImage size].width,[returnImage size].height-6) cornerRadius:10.0] stroke];
//[graphicsContext restoreGraphicsState];
[icon compositeToPoint:NSMakePoint(6,6) operation:NSCompositeSourceOver fraction:1.0];
[title drawAtPoint:NSMakePoint(iconSize.width+7,8) withAttributes:attributes];
if ( dragBadge )
{
[dragBadge compositeToPoint:NSMakePoint(iconSize.width+7-[dragBadge size].width, 0) operation:NSCompositeSourceOver fraction:1.0];
}
[returnImage unlockFocus];
return [returnImage autorelease];
}
#pragma mark -
- (IBAction) copy:(id)sender
{
[[self dataSource] outlineView:self writeItems:[self allSelectedItems] toPasteboard:[NSPasteboard generalPasteboard]];
}
- (BOOL)becomeFirstResponder
{
NSMenuItem *copyItem = [[[[NSApp mainMenu] itemWithTag:2] submenu] itemWithTag:99];
[copyItem setTitle:NSLocalizedString(@"folder link menu item",@"")];
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder
{
NSMenuItem *copyItem = [[[[NSApp mainMenu] itemWithTag:2] submenu] itemWithTag:99];
[copyItem setTitle:NSLocalizedString(@"copy menu item",@"")];
return [super resignFirstResponder];
}
#pragma mark -
- (void)setFont:(NSFont *)fontObject
{
// overridden to pass the message onto the individual cells and change the row height
float lineHeight = [[[[NSLayoutManager alloc] init] autorelease] defaultLineHeightForFont:fontObject];
[[self valueForKeyPath:@"tableColumns.dataCell"] setValue:fontObject forKey:@"font"];
[self setRowHeight:lineHeight];
if ( [[self delegate] respondsToSelector:@selector(adjustRowHeightsFromFontSize:) ] )
[[self delegate] adjustRowHeightsFromFontSize:lineHeight];
[self setNeedsDisplay:YES];
}
- (IBAction) changeColor:(id)sender
{
[[NSUserDefaults standardUserDefaults] setColor:[sender color] forKey:@"FolderBackgroundColor"];
}
@end