/
CRDServerList.m
614 lines (477 loc) · 16.2 KB
/
CRDServerList.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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/* Copyright (c) 2007-2012 Dorian Johnson <2011@dorianj.net>
This file is part of CoRD.
CoRD is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.
CoRD is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
CoRD; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
Fifth Floor, Boston, MA 02110-1301 USA
*/
/* Notes:
- In its current form, this isn't as abstracted as a view object should be.
The delegate is assumed to respond to messages, it delves into the controller
realm, etc.
- Some of this code is downright ugly/hacky - especially the drag and drop code
*/
#import "CRDServerList.h"
#import "CRDShared.h"
#import "AppController.h"
#import "CRDServerCell.h"
#import "CRDSession.h"
#define ANIMATION_DURATION 0.18
#pragma mark -
@interface CRDServerList (Private)
- (BOOL)pasteboardHasValidData:(NSPasteboard *)draggingPasteboard;
- (void)createNewRowOriginsAboveRow:(NSInteger)rowIndex;
- (void)startAnimation;
- (void)concludeDrag;
- (void)createConvolvedRowRects:(float)fraction;
- (CRDServerCell *)cellForRow:(NSInteger)row;
- (NSDragOperation)dragOperationForSource:(id <NSDraggingInfo>)info;
- (void)startDragForEvent:(NSEvent *)ev;
@end
#pragma mark -
@implementation CRDServerList
#pragma mark -
#pragma mark NSTableView
- (void)awakeFromNib
{
draggedRow = selectedRow = emptyRowIndex = -1;
[self setVerticalMotionCanBeginDrag:YES];
}
- (void)dealloc
{
[autoexpansionAnimation release];
[autoexpansionStartRowOrigins release];
[autoexpansionEndRowOrigins release];
[autoexpansionCurrentRowOrigins release];
[super dealloc];
}
// If this isn't overridden, it won't use the hightlightSelectionInClipRect method
- (id)_highlightColorForCell:(NSCell *)cell
{
return nil;
}
- (void)highlightSelectionInClipRect:(NSRect)clipRect
{
if (selectedRow == -1)
return;
NSRect drawRect = [self rectOfRow:selectedRow];
NSColor *fillColor;
if ([[NSApp mainWindow] isKeyWindow])
fillColor = [NSColor selectedControlColor];
else
fillColor = [[NSColor selectedControlColor] colorWithAlphaComponent:0.35];
[self lockFocus];
NSRectClip(drawRect);
[fillColor set];
NSRectFillUsingOperation(NSMakeRect(drawRect.origin.x, drawRect.origin.y, drawRect.size.width, drawRect.size.height), NSCompositeSourceOver);
[self unlockFocus];
}
- (void)drawRow:(NSInteger)rowIndex clipRect:(NSRect)clipRect
{
// Lightly highlight the visible server if not selected
if ([[self dataSource] tableView:self objectValueForTableColumn:nil row:rowIndex] == [g_appController viewedServer]
&& (selectedRow != rowIndex) && ([g_appController displayMode] == CRDDisplayUnified) )
{
[NSGraphicsContext saveGraphicsState];
[[[NSColor selectedControlColor] colorWithAlphaComponent:0.5] set];
[NSBezierPath fillRect:[self rectOfRow:rowIndex]];
[NSGraphicsContext restoreGraphicsState];
}
[[[[self tableColumns] objectAtIndex:0] dataCellForRow:rowIndex] setHighlighted:[self isRowSelected:rowIndex]];
[super drawRow:rowIndex clipRect:clipRect];
}
- (void)selectRowIndexes:(NSIndexSet *)indexes byExtendingSelection:(BOOL)extend
{
[self selectRow:[indexes firstIndex]];
}
- (void)selectRow:(NSInteger)index
{
selectedRow = [[self delegate] tableView:self shouldSelectRow:index] ? index : -1;
// Very hacky, but works better than calling super (this way, we control the notification)
[_selectedRows autorelease];
_selectedRows = [[NSMutableIndexSet indexSetWithIndex:selectedRow] retain];
[self setNeedsDisplay:YES];
[[NSNotificationCenter defaultCenter] postNotificationName:NSTableViewSelectionDidChangeNotification object:self];
}
- (void)deselectRow:(NSInteger)rowIndex
{
[self deselectAll:nil];
}
- (void)deselectAll:(id)sender
{
[self selectRow:-1];
}
- (BOOL)isRowSelected:(NSInteger)rowIndex
{
return rowIndex == selectedRow;
}
- (NSInteger)selectedRow
{
return selectedRow;
}
- (NSIndexSet *)selectedRowIndexes
{
return [NSIndexSet indexSetWithIndex:selectedRow];
}
- (NSInteger)numberOfSelectedRows
{
return ([self selectedRow] == -1) ? 0 : 1;
}
- (NSImage *)dragImageForRowsWithIndexes:(NSIndexSet *)dragRows tableColumns:(NSArray *)tableColumns
event:(NSEvent*)dragEvent offset:(NSPointPointer)dragImageOffset
{
NSInteger row = [dragRows firstIndex];
if ( (row < 0) || (row >= [self numberOfRows]))
return nil;
NSRect rowRect = [self rectOfRow:row];
NSImage *dragImage = [[[NSImage alloc] initWithSize:rowRect.size] autorelease];
[dragImage setFlipped:YES];
[dragImage lockFocus];
{
NSAffineTransform *xform = [NSAffineTransform transform];
[xform translateXBy:0.0 yBy:rowRect.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
[xform concat];
BOOL highlighted = [[self cellForRow:row] isHighlighted];
[[self cellForRow:row] setHighlighted:NO];
[[self cellForRow:row] drawWithFrame:CRDRectFromSize([dragImage size]) inView:nil];
[[self cellForRow:row] setHighlighted:highlighted];
} [dragImage unlockFocus];
// Get it into a 60% opaque image. xxx: This is ugly.
NSImage *dragImage2 = [[NSImage alloc] initWithSize:rowRect.size];
[dragImage2 lockFocus];
{
NSAffineTransform *xform = [NSAffineTransform transform];
[xform translateXBy:0.0 yBy:rowRect.size.height];
[xform scaleXBy:1.0 yBy:-1.0];
[xform concat];
[dragImage drawAtPoint:NSZeroPoint fromRect:(NSRect){NSZeroPoint, [dragImage size]} operation:NSCompositeCopy fraction:0.6];
} [dragImage2 unlockFocus];
return [dragImage2 autorelease];
}
- (BOOL)canDragRowsWithIndexes:(NSIndexSet *)rowIndexes atPoint:(NSPoint)mouseDownPoint
{
return [[self dataSource] tableView:self canDragRow:[rowIndexes firstIndex]];
}
- (id <CRDServerListDataSource>)dataSource
{
return (id)[super dataSource];
}
#pragma mark -
#pragma mark NSResponder
- (void)keyDown:(NSEvent *)ev
{
NSString *str = [ev charactersIgnoringModifiers];
if ([str length] == 1)
{
switch ([str characterAtIndex:0])
{
case NSDeleteFunctionKey:
case NSBackspaceCharacter:
case NSDeleteCharacter: /* backward delete */
[g_appController removeSelectedSavedServer:self];
return;
break;
case NSEnterCharacter: // return
case NSCarriageReturnCharacter: // numpad enter
[g_appController connect:self];
return;
break;
default:
break;
}
}
[super keyDown:ev];
}
- (void)mouseDown:(NSEvent *)ev
{
NSInteger row = [self rowAtPoint:[self convertPoint:[ev locationInWindow] fromView:nil]];
mouseDragStart = [ev locationInWindow];
if ([ev clickCount] == 2 && row == [self selectedRow])
{
[g_appController connect:self];
return;
}
[[self window] makeFirstResponder:self];
}
- (void)mouseUp:(NSEvent *)ev
{
NSInteger row = [self rowAtPoint:[self convertPoint:[ev locationInWindow] fromView:nil]];
if ([self selectedRow] != row)
{
[self selectRow:row];
}
}
- (void)mouseDragged:(NSEvent *)ev
{
if (POINT_DISTANCE(mouseDragStart, [ev locationInWindow]) < 4.0)
return;
[self startDragForEvent:ev];
}
// Assure that the row the right click is over is selected so that the context menu is correct
- (void)rightMouseDown:(NSEvent *)ev
{
NSInteger row = [self rowAtPoint:[self convertPoint:[ev locationInWindow] fromView:nil]];
if (row != -1)
[self selectRow:row];
if (([self selectedRow] != -1) && [[self menu] numberOfItems])
[NSMenu popUpContextMenu:[self menu] withEvent:ev forView:self];
}
- (NSRect)rectOfRow:(NSInteger)rowIndex
{
NSRect realRect = [super rectOfRow:rowIndex];
if ((rowIndex != -1) && (autoexpansionCurrentRowOrigins != nil) && ([autoexpansionCurrentRowOrigins count] > rowIndex))
{
realRect.origin = [[autoexpansionCurrentRowOrigins objectAtIndex:rowIndex] pointValue];
}
return realRect;
}
#pragma mark -
#pragma mark NSDraggingDestination
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
if ([sender draggingSource] == self)
{
return NSDragOperationMove;
}
else
return [self draggingUpdated:sender];
}
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
NSInteger row = [super rowAtPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
NSDragOperation retOperation;
BOOL innerListDrag = [sender draggingSource] == self;
inLiveDrag = YES;
if ([[self dataSource] tableView:self canDropAboveRow:row])
retOperation = innerListDrag ? NSDragOperationMove : NSDragOperationCopy;
else
return NSDragOperationNone;
if ( (row != -1) && (row != emptyRowIndex) )
{
[self createNewRowOriginsAboveRow:row];
[self startAnimation];
}
else if ( (row == -1) && (emptyRowIndex != [self numberOfRows]) )
{
[self createNewRowOriginsAboveRow:[self numberOfRows]];
[self startAnimation];
}
return retOperation;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
[self concludeDrag];
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
if ([self pasteboardHasValidData:[sender draggingPasteboard]])
return YES;
else
{
[self concludeDrag];
return NO;
}
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
if ([self pasteboardHasValidData:[sender draggingPasteboard]])
{
NSInteger row = [self rowAtPoint:[self convertPoint:[sender draggingLocation] fromView:nil]];
if (row == -1)
row = [self numberOfRows];
[[self dataSource] tableView:self acceptDrop:sender row:row dropOperation:[self dragOperationForSource:sender]];
[self createNewRowOriginsAboveRow:[self numberOfRows]];
[self createConvolvedRowRects:1.0];
return YES;
}
else
{
[self concludeDrag];
return NO;
}
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
[self concludeDrag];
}
- (BOOL)wantsPeriodicDraggingUpdates
{
return YES;
}
#pragma mark -
#pragma mark NSDraggingSource
- (void)draggedImage:(NSImage *)anImage endedAt:(NSPoint)aPoint operation:(NSDragOperation)operation
{
if ( (operation == NSDragOperationCopy) || (operation == NSDragOperationNone) )
{
[g_appController reinsertHeldSavedServer:-1];
}
[super draggedImage:anImage endedAt:aPoint operation:operation];
}
- (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)flag;
{
return flag ? NSDragOperationMove : NSDragOperationCopy;
}
#pragma mark -
#pragma mark Dragging internal use
- (NSString *)pasteboardDataType:(NSPasteboard *)draggingPasteboard
{
NSArray *supportedTypes = [NSArray arrayWithObjects:CRDRowIndexPboardType,
NSFilenamesPboardType, NSFilesPromisePboardType, nil];
return [draggingPasteboard availableTypeFromArray:supportedTypes];
}
- (BOOL)pasteboardHasValidData:(NSPasteboard *)draggingPasteboard
{
return [self pasteboardDataType:draggingPasteboard] != nil;
}
- (NSDragOperation)dragOperationForSource:(id <NSDraggingInfo>)info
{
NSString *type = [self pasteboardDataType:[info draggingPasteboard]];
if (type == nil)
return NSDragOperationNone;
if ([type isEqualToString:CRDRowIndexPboardType])
return NSDragOperationMove;
else
return NSDragOperationCopy;
}
- (void)startAnimation
{
if (autoexpansionAnimation != nil)
{
[autoexpansionAnimation stopAnimation];
[autoexpansionAnimation release];
}
float frameRate = 30.0;
autoexpansionAnimation = [[NSAnimation alloc] initWithDuration:ANIMATION_DURATION
animationCurve:NSAnimationLinear];
[autoexpansionAnimation setFrameRate:frameRate];
[autoexpansionAnimation setAnimationBlockingMode:NSAnimationNonblocking];
[autoexpansionAnimation setDelegate:self];
// Make sure animation:didReachProgressMark: gets called on each frame
for (NSInteger i = 0; i < (int)frameRate; i++)
[autoexpansionAnimation addProgressMark:(i / frameRate)];
[autoexpansionAnimation startAnimation];
}
- (void)createNewRowOriginsAboveRow:(NSInteger)rowIndex
{
if (emptyRowIndex == rowIndex || rowIndex == -1)
return;
if ([self numberOfRows] != [autoexpansionCurrentRowOrigins count])
{
[autoexpansionCurrentRowOrigins release];
autoexpansionCurrentRowOrigins = nil;
}
oldEmptyRowIndex = (emptyRowIndex == -1) ? [self numberOfRows] : emptyRowIndex;
emptyRowIndex = rowIndex;
NSInteger numRows = [self numberOfRows], i;
float delta = [super rectOfRow:rowIndex].size.height;
NSPoint startRowOrigin, endRowOrigin;
NSMutableArray *endBuilder = [NSMutableArray arrayWithCapacity:numRows-emptyRowIndex];
NSMutableArray *startBuilder = [NSMutableArray arrayWithCapacity:numRows-emptyRowIndex];
for (i = 0; i < numRows; i++)
{
startRowOrigin = [self rectOfRow:i].origin;
endRowOrigin = [super rectOfRow:i].origin;
if ( i >= emptyRowIndex)
endRowOrigin.y += delta;
[startBuilder addObject:[NSValue valueWithPoint:startRowOrigin]];
[endBuilder addObject:[NSValue valueWithPoint:endRowOrigin]];
}
[autoexpansionStartRowOrigins release];
[autoexpansionEndRowOrigins release];
autoexpansionStartRowOrigins = [startBuilder retain];
autoexpansionEndRowOrigins = [endBuilder retain];
}
- (void)concludeDrag
{
if (inLiveDrag)
{
if (emptyRowIndex != [self numberOfRows])
{
[self createNewRowOriginsAboveRow:[self numberOfRows]];
[self startAnimation];
}
inLiveDrag = NO;
}
else
{
[autoexpansionCurrentRowOrigins release];
[autoexpansionEndRowOrigins release];
[autoexpansionCurrentRowOrigins release];
autoexpansionCurrentRowOrigins = autoexpansionEndRowOrigins = autoexpansionStartRowOrigins = nil;
[autoexpansionAnimation stopAnimation];
[autoexpansionAnimation release];
autoexpansionAnimation = nil;
emptyRowIndex = oldEmptyRowIndex = -1;
}
[self setNeedsDisplay];
}
// Create a new autoexpansionCurrentRowOrigins by convolving the start origins to the end
- (void)createConvolvedRowRects:(float)fraction
{
NSMutableArray *currentPointsBuilder = [[NSMutableArray alloc] init];
NSEnumerator *startEnum = [autoexpansionStartRowOrigins objectEnumerator], *endEnum = [autoexpansionEndRowOrigins objectEnumerator];
id startValue, endValue;
NSPoint convolvedPoint, startOrigin, endOrigin;
while ( (startValue = [startEnum nextObject]) && (endValue = [endEnum nextObject]))
{
startOrigin = [startValue pointValue];
endOrigin = [endValue pointValue];
convolvedPoint = NSMakePoint(startOrigin.x * (1.0 - fraction) + endOrigin.x * fraction, startOrigin.y * (1.0 - fraction) + endOrigin.y * fraction);
[currentPointsBuilder addObject:[NSValue valueWithPoint:convolvedPoint]];
}
[autoexpansionCurrentRowOrigins release];
autoexpansionCurrentRowOrigins = [currentPointsBuilder retain];
}
- (void)startDragForEvent:(NSEvent *)ev
{
NSInteger row = [self rowAtPoint:[self convertPoint:mouseDragStart fromView:nil]];
if (![[self dataSource] tableView:self canDragRow:row])
return;
NSRect rowRect = [self rectOfRow:row];
NSIndexSet *index = [NSIndexSet indexSetWithIndex:row];
NSPoint offset = NSZeroPoint, imageStart = rowRect.origin;
imageStart.y += rowRect.size.height;
NSPasteboard *pboard;
pboard = [NSPasteboard pasteboardWithName:NSDragPboard];
[[self dataSource] tableView:self writeRowsWithIndexes:index toPasteboard:pboard];
NSImage *dragImage = [self dragImageForRowsWithIndexes:index tableColumns:nil event:ev offset:&offset];
[g_appController holdSavedServer:row];
[self deselectAll:self];
[self noteNumberOfRowsChanged];
draggedRow = row;
[self createNewRowOriginsAboveRow:draggedRow];
[autoexpansionStartRowOrigins release];
autoexpansionStartRowOrigins = [autoexpansionEndRowOrigins retain];
[self createConvolvedRowRects:1.0];
[self dragImage:dragImage at:imageStart offset:NSZeroSize event:ev pasteboard:pboard source:self slideBack:YES];
[autoexpansionCurrentRowOrigins release];
autoexpansionCurrentRowOrigins = nil;
}
#pragma mark -
#pragma mark NSAnimation delegate
- (void)animation:(NSAnimation*)animation didReachProgressMark:(NSAnimationProgress)progress
{
[self createConvolvedRowRects:[animation currentValue]];
[self setNeedsDisplay];
}
- (void)animationDidEnd:(NSAnimation*)animation
{
oldEmptyRowIndex = -1;
if (!inLiveDrag)
[self concludeDrag];
}
#pragma mark -
#pragma mark Internal use
- (CRDServerCell *)cellForRow:(NSInteger)row
{
return [[[self tableColumns] objectAtIndex:0] dataCellForRow:row];
}
@end