forked from phildow/Journler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIndexOutlineView.m
363 lines (272 loc) · 12.6 KB
/
IndexOutlineView.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
//
// IndexOutlineView.m
// Journler
//
// Created by Philip Dow on 2/7/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 "IndexOutlineView.h"
#import "IndexNode.h"
#import "IndexColumn.h"
#import <SproutedUtilities/SproutedUtilities.h>
/*
#import "NSBezierPath_AMShading.h"
#import "NSBezierPath_AMAdditons.h"
#import "NSOutlineView_Extensions.h"
#import "NSOutlineView_ProxyAdditions.h"
*/
#define kSearchIntervalDelay 1.5
@implementation IndexOutlineView
- (void) awakeFromNib
{
_searchString = [[NSMutableString alloc] init];
}
- (void) dealloc
{
[_searchString release];
[super dealloc];
}
- (IndexColumn*) indexColumn
{
return indexColumn;
}
#pragma mark -
- (id)_highlightColorForCell:(NSCell *)cell
{
return [self backgroundColor];
}
- (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(indexOutlineView:deleteSelectedRows:)] )
[[self delegate] indexOutlineView:self deleteSelectedRows:[self selectedRowIndexes]];
}
else
{
// perform a search
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 = [rootController arrangedObjects];
[_searchString appendString:new_characters];
for ( i = 0; i < [objects count]; i++ )
{
if ( [[[objects objectAtIndex:i] valueForKey:@"title"]
rangeOfString:_searchString options:NSCaseInsensitiveSearch].location == 0 )
{
[self scrollRowToVisible:i];
[self selectRowIndexes:[NSIndexSet indexSetWithIndex:i] byExtendingSelection:NO];
break;
}
}
}
}
else
{
[super keyDown:event];
}
}
}
- (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect {
// 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]) ];
}
[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];
}
#pragma mark -
- (void)copy:(id)sender
{
// copy the strings onto the pasteboard
[[self dataSource] outlineView:self writeItems:[self allSelectedItems] toPasteboard:[NSPasteboard generalPasteboard]];
}
#pragma mark -
/*
- (void)keyDown:(NSEvent *)event
{
unichar key = [[event charactersIgnoringModifiers] characterAtIndex:0];
NSUInteger modifierFlags = [event modifierFlags];
if ( key == NSLeftArrowFunctionKey && [[self delegate] respondsToSelector:@selector(outlineView:leftKeyDown:)] && ( modifierFlags & NSCommandKeyMask ) )
[[self delegate] outlineView:self leftKeyDown:event];
else if ( key == NSRightArrowFunctionKey && [[self delegate] respondsToSelector:@selector(outlineView:rightKeyDown:)] && ( modifierFlags & NSCommandKeyMask ) )
[[self delegate] outlineView:self rightKeyDown:event];
else
[super keyDown:event];
}
*/
- (NSUInteger)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
if ( isLocal )
return NSDragOperationLink;
else
return NSDragOperationCopy;
}
- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows
tableColumns:(NSArray *)tableColumns event:(NSEvent*)dragEvent offset:(NSPointPointer)dragImageOffset
{
// it doesn't matter what kind of object we're dealing with, as long as it responds to title and icon
id anObject = [[self originalItemAtRow:[dragRows firstIndex]] representedObject];
if ( !anObject ) return [super dragImageForRowsWithIndexes:dragRows tableColumns:tableColumns event:dragEvent offset:dragImageOffset];
if ( ![anObject respondsToSelector:@selector(title)] || ![anObject respondsToSelector:@selector(icon)] )
return [super dragImageForRowsWithIndexes:dragRows tableColumns:tableColumns event:dragEvent offset:dragImageOffset];
NSString *title = [anObject valueForKey:@"title"];
if ( !title ) return [super dragImageForRowsWithIndexes:dragRows tableColumns:tableColumns event:dragEvent offset:dragImageOffset];
NSImage *icon = [self resizedImage:[anObject valueForKey:@"icon"] width: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];
}
- (BOOL)becomeFirstResponder
{
// let the delegate know that we become first responder
#ifdef __DEBUG__
NSLog(@"%s",__PRETTY_FUNCTION__);
#endif
BOOL didBecome = [super becomeFirstResponder];
if ( didBecome && [[self delegate] respondsToSelector:@selector(outlineViewDidBecomeFirstResponder:)] )
[[self delegate] outlineViewDidBecomeFirstResponder:self];
return didBecome;
}
@end
@implementation IndexOutlineView (AdditionalUISupport)
- (NSImage*) resizedImage:(NSImage*)anImage width:(float)width height:(float)height inset:(float)inset
{
// returns a WxH version of the image but with the image inset on all sides
NSImage *image = [[NSImage alloc] initWithSize:NSMakeSize(width,height)];
NSRect dRect = NSMakeRect(inset,inset,width-inset*2,height-inset*2);
[image lockFocus];
[[NSGraphicsContext currentContext] saveGraphicsState];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[anImage drawInRect:dRect fromRect:NSMakeRect(0,0,[anImage size].width,[anImage size].height)
operation:NSCompositeSourceOver fraction:1.0];
[[NSGraphicsContext currentContext] restoreGraphicsState];
[image unlockFocus];
return [image autorelease];
}
@end