1/****************************************************************************** 2 * $Id: FilePriorityCell.m 13340 2012-06-10 02:35:58Z livings124 $ 3 * 4 * Copyright (c) 2007-2012 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 "FilePriorityCell.h" 26#import "FileOutlineView.h" 27#import "FileListNode.h" 28#import "NSImageAdditions.h" 29#import "Torrent.h" 30 31#define IMAGE_OVERLAP 1.0 32 33@implementation FilePriorityCell 34 35- (id) init 36{ 37 if ((self = [super init])) 38 { 39 [self setTrackingMode: NSSegmentSwitchTrackingSelectAny]; 40 [self setControlSize: NSMiniControlSize]; 41 [self setSegmentCount: 3]; 42 43 for (NSInteger i = 0; i < [self segmentCount]; i++) 44 { 45 [self setLabel: @"" forSegment: i]; 46 [self setWidth: 9.0f forSegment: i]; //9 is minimum size to get proper look 47 } 48 49 [self setImage: [NSImage imageNamed: @"PriorityControlLow"] forSegment: 0]; 50 [self setImage: [NSImage imageNamed: @"PriorityControlNormal"] forSegment: 1]; 51 [self setImage: [NSImage imageNamed: @"PriorityControlHigh"] forSegment: 2]; 52 53 fHoverRow = NO; 54 } 55 return self; 56} 57 58- (id) copyWithZone: (NSZone *) zone 59{ 60 id value = [super copyWithZone: zone]; 61 [value setRepresentedObject: [self representedObject]]; 62 return value; 63} 64 65- (void) setSelected: (BOOL) flag forSegment: (NSInteger) segment 66{ 67 [super setSelected: flag forSegment: segment]; 68 69 //only for when clicking manually 70 NSInteger priority; 71 switch (segment) 72 { 73 case 0: 74 priority = TR_PRI_LOW; 75 break; 76 case 1: 77 priority = TR_PRI_NORMAL; 78 break; 79 case 2: 80 priority = TR_PRI_HIGH; 81 break; 82 } 83 84 Torrent * torrent = [(FileListNode *)[self representedObject] torrent]; 85 [torrent setFilePriority: priority forIndexes: [(FileListNode *)[self representedObject] indexes]]; 86 87 FileOutlineView * controlView = (FileOutlineView *)[self controlView]; 88 [controlView setNeedsDisplay: YES]; 89} 90 91- (void) addTrackingAreasForView: (NSView *) controlView inRect: (NSRect) cellFrame withUserInfo: (NSDictionary *) userInfo 92 mouseLocation: (NSPoint) mouseLocation 93{ 94 NSTrackingAreaOptions options = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways; 95 96 if (NSMouseInRect(mouseLocation, cellFrame, [controlView isFlipped])) 97 { 98 options |= NSTrackingAssumeInside; 99 [controlView setNeedsDisplayInRect: cellFrame]; 100 } 101 102 NSTrackingArea * area = [[NSTrackingArea alloc] initWithRect: cellFrame options: options owner: controlView userInfo: userInfo]; 103 [controlView addTrackingArea: area]; 104 [area release]; 105} 106 107- (void) setHovered: (BOOL) hovered 108{ 109 fHoverRow = hovered; 110} 111 112- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView 113{ 114 FileListNode * node = [self representedObject]; 115 Torrent * torrent = [node torrent]; 116 NSSet * priorities = [torrent filePrioritiesForIndexes: [node indexes]]; 117 118 const NSUInteger count = [priorities count]; 119 if (fHoverRow && count > 0) 120 { 121 [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_LOW]] forSegment: 0]; 122 [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_NORMAL]] forSegment: 1]; 123 [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_HIGH]] forSegment: 2]; 124 125 [super drawWithFrame: cellFrame inView: controlView]; 126 } 127 else 128 { 129 NSMutableArray * images = [NSMutableArray arrayWithCapacity: MAX(count, 1)]; 130 CGFloat totalWidth; 131 132 if (count == 0) 133 { 134 //if ([self backgroundStyle] != NSBackgroundStyleDark) 135 { 136 NSImage * image = [[NSImage imageNamed: @"PriorityNormalTemplate"] imageWithColor: [NSColor lightGrayColor]]; 137 [images addObject: image]; 138 totalWidth = [image size].width; 139 } 140 } 141 else 142 { 143 NSColor * priorityColor = [self backgroundStyle] == NSBackgroundStyleDark ? [NSColor whiteColor] : [NSColor darkGrayColor]; 144 145 totalWidth = 0.0; 146 if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_LOW]]) 147 { 148 NSImage * image = [[NSImage imageNamed: @"PriorityLowTemplate"] imageWithColor: priorityColor]; 149 [images addObject: image]; 150 totalWidth += [image size].width; 151 } 152 if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_NORMAL]]) 153 { 154 NSImage * image = [[NSImage imageNamed: @"PriorityNormalTemplate"] imageWithColor: priorityColor]; 155 [images addObject: image]; 156 totalWidth += [image size].width; 157 } 158 if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_HIGH]]) 159 { 160 NSImage * image = [[NSImage imageNamed: @"PriorityHighTemplate"] imageWithColor: priorityColor]; 161 [images addObject: image]; 162 totalWidth += [image size].width; 163 } 164 } 165 166 if (count > 1) 167 totalWidth -= IMAGE_OVERLAP * (count-1); 168 169 CGFloat currentWidth = floor(NSMidX(cellFrame) - totalWidth * 0.5); 170 171 for (NSImage * image in images) 172 { 173 const NSSize imageSize = [image size]; 174 const NSRect imageRect = NSMakeRect(currentWidth, floor(NSMidY(cellFrame) - imageSize.height * 0.5), imageSize.width, imageSize.height); 175 176 [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0 respectFlipped: YES hints: nil]; 177 178 currentWidth += imageSize.width - IMAGE_OVERLAP; 179 } 180 } 181} 182 183@end 184