1/******************************************************************************
2 * Copyright (c) 2007-2012 Transmission authors and contributors
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#import "InfoWindowController.h"
24#import "FileListNode.h"
25#import "FileNameCell.h"
26#import "FileOutlineView.h"
27#import "FilePriorityCell.h"
28#import "Torrent.h"
29
30@implementation FileOutlineView
31
32- (void) awakeFromNib
33{
34    FileNameCell * nameCell = [[FileNameCell alloc] init];
35    [[self tableColumnWithIdentifier: @"Name"] setDataCell: nameCell];
36
37    FilePriorityCell * priorityCell = [[FilePriorityCell alloc] init];
38    [[self tableColumnWithIdentifier: @"Priority"] setDataCell: priorityCell];
39
40    [self setAutoresizesOutlineColumn: NO];
41    [self setIndentationPerLevel: 14.0];
42
43    fMouseRow = -1;
44}
45
46
47- (void) mouseDown: (NSEvent *) event
48{
49    [[self window] makeKeyWindow];
50    [super mouseDown: event];
51}
52
53- (NSMenu *) menuForEvent: (NSEvent *) event
54{
55    const NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
56
57    if (row >= 0)
58    {
59        if (![self isRowSelected: row])
60            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
61    }
62    else
63        [self deselectAll: self];
64
65    return [self menu];
66}
67
68- (NSRect) iconRectForRow: (int) row
69{
70    FileNameCell * cell = (FileNameCell *)[self preparedCellAtColumn: [self columnWithIdentifier: @"Name"] row: row];
71    NSRect iconRect = [cell imageRectForBounds: [self rectOfRow: row]];
72
73    iconRect.origin.x += [self indentationPerLevel] * (CGFloat)([self levelForRow: row] + 1);
74    return iconRect;
75}
76
77- (void) updateTrackingAreas
78{
79    [super updateTrackingAreas];
80
81    for (NSTrackingArea * area in [self trackingAreas])
82    {
83        if ([area owner] == self && [area userInfo][@"Row"])
84            [self removeTrackingArea: area];
85    }
86
87    NSRange visibleRows = [self rowsInRect: [self visibleRect]];
88    if (visibleRows.length == 0)
89        return;
90
91    NSPoint mouseLocation = [self convertPoint: [[self window] mouseLocationOutsideOfEventStream] fromView: nil];
92
93    for (NSInteger row = visibleRows.location, col = [self columnWithIdentifier: @"Priority"]; (NSUInteger)row < NSMaxRange(visibleRows); row++)
94    {
95        FilePriorityCell * cell = (FilePriorityCell *)[self preparedCellAtColumn: col row: row];
96
97        NSDictionary * userInfo = @{@"Row": @(row)};
98        [cell addTrackingAreasForView: self inRect: [self frameOfCellAtColumn: col row: row] withUserInfo: userInfo
99                mouseLocation: mouseLocation];
100    }
101}
102
103- (NSInteger) hoveredRow
104{
105    return fMouseRow;
106}
107
108- (void) mouseEntered: (NSEvent *) event
109{
110    NSNumber * row;
111    if ((row = ((NSDictionary *)[event userData])[@"Row"]))
112    {
113        fMouseRow = [row intValue];
114        [self setNeedsDisplayInRect: [self frameOfCellAtColumn: [self columnWithIdentifier: @"Priority"] row: fMouseRow]];
115    }
116}
117
118- (void) mouseExited: (NSEvent *) event
119{
120    NSNumber * row;
121    if ((row = ((NSDictionary *)[event userData])[@"Row"]))
122    {
123        [self setNeedsDisplayInRect: [self frameOfCellAtColumn: [self columnWithIdentifier: @"Priority"] row: [row intValue]]];
124        fMouseRow = -1;
125    }
126}
127
128@end
129