1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 01:27:28 +00:00
transmission/macosx/TorrentTableView.m

233 lines
7.4 KiB
Mathematica
Raw Normal View History

2006-03-23 12:39:39 +00:00
/******************************************************************************
* $Id$
*
2006-03-23 12:39:39 +00:00
* Copyright (c) 2005-2006 Transmission authors and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#import "TorrentTableView.h"
#import "Controller.h"
2006-03-23 12:39:39 +00:00
#import "Torrent.h"
2006-01-12 18:29:20 +00:00
#define BUTTON_TO_TOP_REGULAR 33.5
2006-07-06 00:33:24 +00:00
#define BUTTON_TO_TOP_SMALL 20.0
#define BUTTON_WIDTH 14.0
#define DISTANCE_FROM_CENTER 2.5
//change BUTTONS_TOTAL_WIDTH when changing this
#define AREA_CENTER 21.0
2006-06-12 04:25:43 +00:00
@interface TorrentTableView (Private)
- (NSRect) pauseRectForRow: (int) row;
- (NSRect) revealRectForRow: (int) row;
- (BOOL) pointInPauseRect: (NSPoint) point;
- (BOOL) pointInRevealRect: (NSPoint) point;
- (BOOL) pointInIconRect: (NSPoint) point;
2006-06-12 04:25:43 +00:00
@end
2006-01-12 18:29:20 +00:00
@implementation TorrentTableView
- (id) initWithCoder: (NSCoder *) decoder
{
if ((self = [super initWithCoder: decoder]))
{
fResumeOnIcon = [NSImage imageNamed: @"ResumeOn.png"];
fResumeOffIcon = [NSImage imageNamed: @"ResumeOff.png"];
fPauseOnIcon = [NSImage imageNamed: @"PauseOn.png"];
fPauseOffIcon = [NSImage imageNamed: @"PauseOff.png"];
fRevealOnIcon = [NSImage imageNamed: @"RevealOn.png"];
fRevealOffIcon = [NSImage imageNamed: @"RevealOff.png"];
fClickPoint = NSZeroPoint;
fDefaults = [NSUserDefaults standardUserDefaults];
}
return self;
}
- (void) awakeFromNib
{
[fContextRow setTitle: @"Context"];
[fContextNoRow setTitle: @"Context"];
}
2006-03-23 12:39:39 +00:00
- (void) setTorrents: (NSArray *) torrents
2006-01-12 18:29:20 +00:00
{
2006-03-23 12:39:39 +00:00
fTorrents = torrents;
2006-01-12 18:29:20 +00:00
}
- (void) mouseDown: (NSEvent *) event
2006-03-23 12:39:39 +00:00
{
fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil];
2006-01-12 18:29:20 +00:00
if ([event modifierFlags] & NSAlternateKeyMask)
2006-01-12 18:29:20 +00:00
{
[fController toggleAdvancedBar: self];
fClickPoint = NSZeroPoint;
2006-01-12 18:29:20 +00:00
}
else if (![self pointInPauseRect: fClickPoint] && ![self pointInRevealRect: fClickPoint])
[super mouseDown: event];
2006-03-23 12:39:39 +00:00
else;
[self display];
}
- (void) mouseUp: (NSEvent *) event
2006-03-23 12:39:39 +00:00
{
NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
int row = [self rowAtPoint: point];
BOOL sameRow = row == [self rowAtPoint: fClickPoint];
2006-03-23 12:39:39 +00:00
if (sameRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint])
2006-01-12 18:33:20 +00:00
{
Torrent * torrent = [fTorrents objectAtIndex: row];
2006-04-07 13:09:19 +00:00
if ([torrent isPaused])
2006-07-09 15:22:20 +00:00
[fController resumeTorrents: [NSArray arrayWithObject: torrent]];
else if ([torrent isActive])
2006-07-09 15:22:20 +00:00
[fController stopTorrents: [NSArray arrayWithObject: torrent]];
2006-04-07 13:09:19 +00:00
else;
2006-01-12 18:33:20 +00:00
}
else if (sameRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint])
[[fTorrents objectAtIndex: row] revealData];
else if ([event clickCount] == 2)
{
if ([self pointInIconRect: point])
[[fTorrents objectAtIndex: row] revealData];
else
[fController showInfo: nil];
}
else;
2006-04-07 13:09:19 +00:00
[super mouseUp: event];
2006-01-12 18:29:20 +00:00
fClickPoint = NSZeroPoint;
[self display];
2006-01-12 18:29:20 +00:00
}
- (NSMenu *) menuForEvent: (NSEvent *) event
2006-01-12 18:29:20 +00:00
{
int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
2006-01-12 18:57:23 +00:00
2006-06-16 00:04:29 +00:00
if (row >= 0)
2006-03-23 12:39:39 +00:00
{
if (![self isRowSelected: row])
[self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
2006-03-23 12:39:39 +00:00
return fContextRow;
}
else
{
[self deselectAll: self];
return fContextNoRow;
}
2006-01-12 18:29:20 +00:00
}
- (void) drawRect: (NSRect) r
{
NSRect rect;
2006-03-23 12:39:39 +00:00
Torrent * torrent;
NSImage * image;
2006-01-12 18:29:20 +00:00
[super drawRect: r];
int i;
for (i = 0; i < [fTorrents count]; i++)
2006-01-12 18:29:20 +00:00
{
2006-03-23 12:39:39 +00:00
torrent = [fTorrents objectAtIndex: i];
2006-01-12 18:29:20 +00:00
rect = [self pauseRectForRow: i];
2006-03-23 12:39:39 +00:00
2006-06-16 01:50:10 +00:00
if ([torrent isPaused])
image = NSPointInRect(fClickPoint, rect) ? fResumeOnIcon : fResumeOffIcon;
else if ([torrent isActive])
image = NSPointInRect(fClickPoint, rect) ? fPauseOnIcon : fPauseOffIcon;
else
image = nil;
2006-03-23 12:39:39 +00:00
2006-06-16 01:50:10 +00:00
if (image)
2006-01-12 18:29:20 +00:00
{
2006-03-23 12:39:39 +00:00
[image setFlipped: YES];
[image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
operation: NSCompositeSourceOver fraction: 1.0];
2006-01-12 18:29:20 +00:00
}
2006-06-16 01:50:10 +00:00
rect = [self revealRectForRow: i];
image = NSPointInRect(fClickPoint, rect) ? fRevealOnIcon : fRevealOffIcon;
2006-03-23 12:39:39 +00:00
[image setFlipped: YES];
[image drawAtPoint: rect.origin fromRect: NSMakeRect(0, 0, BUTTON_WIDTH, BUTTON_WIDTH)
operation: NSCompositeSourceOver fraction: 1.0];
2006-01-12 18:29:20 +00:00
}
}
@end
2006-06-12 04:56:07 +00:00
@implementation TorrentTableView (Private)
- (NSRect) pauseRectForRow: (int) row
{
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
return NSMakeRect(cellRect.origin.x + cellRect.size.width - AREA_CENTER - DISTANCE_FROM_CENTER - BUTTON_WIDTH,
cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
2006-06-12 04:56:07 +00:00
}
- (NSRect) revealRectForRow: (int) row
{
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
2006-06-12 04:56:07 +00:00
float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
return NSMakeRect(cellRect.origin.x + cellRect.size.width - AREA_CENTER + DISTANCE_FROM_CENTER,
cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
2006-06-12 04:56:07 +00:00
}
- (BOOL) pointInIconRect: (NSPoint) point
{
int row = [self rowAtPoint: point];
2006-06-14 20:35:28 +00:00
if (row < 0)
return NO;
NSRect cellRect = [self frameOfCellAtColumn: [self columnWithIdentifier: @"Torrent"] row: row];
NSSize iconSize = [fDefaults boolForKey: @"SmallView"] ? [[[fTorrents objectAtIndex: row] iconSmall] size]
: [[[fTorrents objectAtIndex: row] iconFlipped] size];
NSRect iconRect = NSMakeRect(cellRect.origin.x + 3.0, cellRect.origin.y
+ (cellRect.size.height - iconSize.height) * 0.5, iconSize.width, iconSize.height);
return NSPointInRect(point, iconRect);
}
2006-06-12 04:56:07 +00:00
- (BOOL) pointInPauseRect: (NSPoint) point
{
2006-06-15 00:11:05 +00:00
return NSPointInRect(point, [self pauseRectForRow: [self rowAtPoint: point]]);
2006-06-12 04:56:07 +00:00
}
- (BOOL) pointInRevealRect: (NSPoint) point
{
2006-06-15 00:11:05 +00:00
return NSPointInRect(point, [self revealRectForRow: [self rowAtPoint: point]]);
2006-06-12 04:56:07 +00:00
}
@end