source: trunk/macosx/TrackerTableView.m @ 9228

Last change on this file since 9228 was 9228, checked in by livings124, 13 years ago

allowing pasting into the tracker table on 10.5

  • Property svn:keywords set to Date Rev Author Id
File size: 6.9 KB
Line 
1/******************************************************************************
2 * $Id: TrackerTableView.m 9228 2009-10-01 02:24:58Z livings124 $
3 *
4 * Copyright (c) 2008-2009 Transmission authors and contributors
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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 *****************************************************************************/
24
25#import "TrackerTableView.h"
26#import "NSApplicationAdditions.h"
27#import "Torrent.h"
28#import "TrackerNode.h"
29
30@implementation TrackerTableView
31
32- (void) mouseDown: (NSEvent *) event
33{
34    [[self window] makeKeyWindow];
35    [super mouseDown: event];
36}
37
38- (void) setTorrent: (Torrent *) torrent
39{
40    fTorrent = torrent;
41}
42
43- (void) setTrackers: (NSArray *) trackers
44{
45    fTrackers = trackers;
46}
47
48- (void) copy: (id) sender
49{
50    NSMutableArray * addresses = [NSMutableArray arrayWithCapacity: [fTrackers count]];
51    NSIndexSet * indexes = [self selectedRowIndexes];
52    for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
53    {
54        id item = [fTrackers objectAtIndex: i];
55        if ([item isKindOfClass: [NSNumber class]])
56        {
57            for (++i; i < [fTrackers count] && ![[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]]; ++i)
58                [addresses addObject: [(TrackerNode *)[fTrackers objectAtIndex: i] fullAnnounceAddress]];
59            --i;
60        }
61        else
62            [addresses addObject: [(TrackerNode *)item fullAnnounceAddress]];
63    }
64   
65    NSString * text = [addresses componentsJoinedByString: @"\n"];
66   
67    NSPasteboard * pb = [NSPasteboard generalPasteboard];
68    if ([NSApp isOnSnowLeopardOrBetter])
69    {
70        [pb clearContents];
71        [pb writeObjects: [NSArray arrayWithObject: text]];
72    }
73    else
74    {
75        [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
76        [pb setString: text forType: NSStringPboardType];
77    }
78}
79
80- (void) paste: (id) sender
81{
82    NSAssert(fTorrent != nil, @"no torrent but trying to paste; should not be able to call this method");
83   
84    BOOL added = NO;
85   
86    if ([NSApp isOnSnowLeopardOrBetter])
87    {
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");
91       
92        for (NSString * pbItem in items)
93        {
94            for (NSString * item in [pbItem componentsSeparatedByString: @"\n"])
95                if ([fTorrent addTrackerToNewTier: item])
96                    added = YES;
97        }
98    }
99    else
100    {
101        NSString * pbItem =[[NSPasteboard generalPasteboard] stringForType: NSStringPboardType];
102        NSAssert(pbItem != nil, @"no string items to paste; should not be able to call this method");
103       
104        for (NSString * item in [pbItem componentsSeparatedByString: @"\n"])
105            if ([fTorrent addTrackerToNewTier: item])
106                added = YES;
107    }
108   
109    //none added
110    if (!added)
111        NSBeep();
112}
113
114- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
115{
116    const SEL action = [menuItem action];
117   
118    if (action == @selector(copy:))
119        return [self numberOfSelectedRows] > 0;
120   
121    if (action == @selector(paste:))
122    {
123        return fTorrent && ([NSApp isOnSnowLeopardOrBetter]
124                ? [[NSPasteboard generalPasteboard] canReadObjectForClasses: [NSArray arrayWithObject: [NSString class]] options: nil]
125                : [[NSPasteboard generalPasteboard] availableTypeFromArray: [NSArray arrayWithObject: NSStringPboardType]] != nil);
126    }
127   
128    return YES;
129}
130
131//alternating rows - first row after group row is white
132- (void) highlightSelectionInClipRect: (NSRect) clipRect
133{
134    NSRect visibleRect = clipRect;
135    NSRange rows = [self rowsInRect: visibleRect];
136    BOOL start = YES;
137   
138    const CGFloat totalRowHeight = [self rowHeight] + [self intercellSpacing].height;
139   
140    NSRect gridRects[(NSInteger)(ceil(visibleRect.size.height / totalRowHeight / 2.0)) + 1]; //add one if partial rows at top and bottom
141    NSInteger rectNum = 0;
142   
143    if (rows.length > 0)
144    {
145        //determine what the first row color should be
146        if (![[fTrackers objectAtIndex: rows.location] isKindOfClass: [NSNumber class]])
147        {
148            for (NSInteger i = rows.location-1; i>=0; i--)
149            {
150                if ([[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]])
151                    break;
152                start = !start;
153            }
154        }
155        else
156        {
157            rows.location++;
158            rows.length--;
159        }
160       
161        NSInteger i;
162        for (i = rows.location; i < NSMaxRange(rows); i++)
163        {
164            if ([[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]])
165            {
166                start = YES;
167                continue;
168            }
169           
170            if (!start && ![self isRowSelected: i])
171                gridRects[rectNum++] = [self rectOfRow: i];
172           
173            start = !start;
174        }
175       
176        const CGFloat newY = NSMaxY([self rectOfRow: i-1]);
177        visibleRect.size.height -= newY - visibleRect.origin.y;
178        visibleRect.origin.y = newY;
179    }
180   
181    const NSInteger numberBlankRows = ceil(visibleRect.size.height / totalRowHeight);
182   
183    //remaining visible rows continue alternating
184    visibleRect.size.height = totalRowHeight;
185    if (start)
186        visibleRect.origin.y += totalRowHeight;
187   
188    for (NSInteger i = start ? 1 : 0; i < numberBlankRows; i += 2)
189    {
190        gridRects[rectNum++] = visibleRect;
191        visibleRect.origin.y += 2.0 * totalRowHeight;
192    }
193   
194    NSAssert([[NSColor controlAlternatingRowBackgroundColors] count] >= 2, @"There should be 2 alternating row colors");
195   
196    [[[NSColor controlAlternatingRowBackgroundColors] objectAtIndex: 1] set];
197    NSRectFillList(gridRects, rectNum);
198   
199    [super highlightSelectionInClipRect: clipRect];
200}
201
202@end
Note: See TracBrowser for help on using the repository browser.