1 /******************************************************************************
2 * $Id: TrackerTableView.m 11617 2011-01-01 20:42:14Z livings124 $
4 * Copyright (c) 2008-2011 Transmission authors and contributors
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
25 #import "TrackerTableView.h"
26 #import "NSApplicationAdditions.h"
28 #import "TrackerNode.h"
30 @implementation TrackerTableView
32 - (void) mouseDown: (NSEvent *) event
34 [[self window] makeKeyWindow];
35 [super mouseDown: event];
38 - (void) setTorrent: (Torrent *) torrent
43 - (void) setTrackers: (NSArray *) trackers
48 - (void) copy: (id) sender
50 NSMutableArray * addresses = [NSMutableArray arrayWithCapacity: [fTrackers count]];
51 NSIndexSet * indexes = [self selectedRowIndexes];
52 for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
54 id item = [fTrackers objectAtIndex: i];
55 if (![item isKindOfClass: [TrackerNode class]])
57 for (++i; i < [fTrackers count] && [[fTrackers objectAtIndex: i] isKindOfClass: [TrackerNode class]]; ++i)
58 [addresses addObject: [(TrackerNode *)[fTrackers objectAtIndex: i] fullAnnounceAddress]];
62 [addresses addObject: [(TrackerNode *)item fullAnnounceAddress]];
65 NSString * text = [addresses componentsJoinedByString: @"\n"];
67 NSPasteboard * pb = [NSPasteboard generalPasteboard];
68 if ([NSApp isOnSnowLeopardOrBetter])
71 [pb writeObjects: [NSArray arrayWithObject: text]];
75 [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
76 [pb setString: text forType: NSStringPboardType];
80 - (void) paste: (id) sender
82 NSAssert(fTorrent != nil, @"no torrent but trying to paste; should not be able to call this method");
86 if ([NSApp isOnSnowLeopardOrBetter])
88 NSArray * items = [[NSPasteboard generalPasteboard] readObjectsForClasses:
89 [NSArray arrayWithObject: [NSString class]] options: nil];
90 NSAssert(items != nil, @"no string items to paste; should not be able to call this method");
92 for (NSString * pbItem in items)
94 for (NSString * item in [pbItem componentsSeparatedByString: @"\n"])
95 if ([fTorrent addTrackerToNewTier: item])
101 NSString * pbItem =[[NSPasteboard generalPasteboard] stringForType: NSStringPboardType];
102 NSAssert(pbItem != nil, @"no string items to paste; should not be able to call this method");
104 for (NSString * item in [pbItem componentsSeparatedByString: @"\n"])
105 if ([fTorrent addTrackerToNewTier: item])
114 - (BOOL) validateMenuItem: (NSMenuItem *) menuItem
116 const SEL action = [menuItem action];
118 if (action == @selector(copy:))
119 return [self numberOfSelectedRows] > 0;
121 if (action == @selector(paste:))
122 return fTorrent && ([NSApp isOnSnowLeopardOrBetter]
123 ? [[NSPasteboard generalPasteboard] canReadObjectForClasses: [NSArray arrayWithObject: [NSString class]] options: nil]
124 : [[NSPasteboard generalPasteboard] availableTypeFromArray: [NSArray arrayWithObject: NSStringPboardType]] != nil);
129 //alternating rows - first row after group row is white
130 - (void) highlightSelectionInClipRect: (NSRect) clipRect
132 NSRect visibleRect = clipRect;
133 NSRange rows = [self rowsInRect: visibleRect];
136 const CGFloat totalRowHeight = [self rowHeight] + [self intercellSpacing].height;
138 NSRect gridRects[(NSInteger)(ceil(NSHeight(visibleRect) / totalRowHeight / 2.0)) + 1]; //add one if partial rows at top and bottom
139 NSInteger rectNum = 0;
143 //determine what the first row color should be
144 if ([[fTrackers objectAtIndex: rows.location] isKindOfClass: [TrackerNode class]] || [self editedRow] == rows.location)
146 for (NSInteger i = rows.location-1; i>=0; i--)
148 if (![[fTrackers objectAtIndex: i] isKindOfClass: [TrackerNode class]])
160 for (i = rows.location; i < NSMaxRange(rows); i++)
162 if (![[fTrackers objectAtIndex: i] isKindOfClass: [TrackerNode class]] && [self editedRow] != i)
168 if (!start && ![self isRowSelected: i])
169 gridRects[rectNum++] = [self rectOfRow: i];
174 const CGFloat newY = NSMaxY([self rectOfRow: i-1]);
175 visibleRect.size.height -= newY - visibleRect.origin.y;
176 visibleRect.origin.y = newY;
179 const NSInteger numberBlankRows = ceil(visibleRect.size.height / totalRowHeight);
181 //remaining visible rows continue alternating
182 visibleRect.size.height = totalRowHeight;
184 visibleRect.origin.y += totalRowHeight;
186 for (NSInteger i = start ? 1 : 0; i < numberBlankRows; i += 2)
188 gridRects[rectNum++] = visibleRect;
189 visibleRect.origin.y += 2.0 * totalRowHeight;
192 NSAssert([[NSColor controlAlternatingRowBackgroundColors] count] >= 2, @"There should be 2 alternating row colors");
194 [[[NSColor controlAlternatingRowBackgroundColors] objectAtIndex: 1] set];
195 NSRectFillList(gridRects, rectNum);
197 [super highlightSelectionInClipRect: clipRect];