1 /******************************************************************************
2 * $Id: FilePriorityCell.m 13162 2012-01-14 17:12:04Z livings124 $
4 * Copyright (c) 2007-2012 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 "FilePriorityCell.h"
26 #import "FileOutlineView.h"
27 #import "FileListNode.h"
28 #import "NSImageAdditions.h"
31 #define IMAGE_OVERLAP 1.0
33 @implementation FilePriorityCell
37 if ((self = [super init]))
39 [self setTrackingMode: NSSegmentSwitchTrackingSelectAny];
40 [self setControlSize: NSMiniControlSize];
41 [self setSegmentCount: 3];
43 for (NSInteger i = 0; i < [self segmentCount]; i++)
45 [self setLabel: @"" forSegment: i];
46 [self setWidth: 9.0f forSegment: i]; //9 is minimum size to get proper look
49 [self setImage: [NSImage imageNamed: @"PriorityControlLow.png"] forSegment: 0];
50 [self setImage: [NSImage imageNamed: @"PriorityControlNormal.png"] forSegment: 1];
51 [self setImage: [NSImage imageNamed: @"PriorityControlHigh.png"] forSegment: 2];
58 - (id) copyWithZone: (NSZone *) zone
60 id value = [super copyWithZone: zone];
61 [value setRepresentedObject: [self representedObject]];
65 - (void) setSelected: (BOOL) flag forSegment: (NSInteger) segment
67 [super setSelected: flag forSegment: segment];
69 //only for when clicking manually
74 priority = TR_PRI_LOW;
77 priority = TR_PRI_NORMAL;
80 priority = TR_PRI_HIGH;
84 Torrent * torrent = [(FileListNode *)[self representedObject] torrent];
85 [torrent setFilePriority: priority forIndexes: [(FileListNode *)[self representedObject] indexes]];
87 FileOutlineView * controlView = (FileOutlineView *)[self controlView];
88 [controlView setNeedsDisplay: YES];
91 - (void) addTrackingAreasForView: (NSView *) controlView inRect: (NSRect) cellFrame withUserInfo: (NSDictionary *) userInfo
92 mouseLocation: (NSPoint) mouseLocation
94 NSTrackingAreaOptions options = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
96 if (NSMouseInRect(mouseLocation, cellFrame, [controlView isFlipped]))
98 options |= NSTrackingAssumeInside;
99 [controlView setNeedsDisplayInRect: cellFrame];
102 NSTrackingArea * area = [[NSTrackingArea alloc] initWithRect: cellFrame options: options owner: controlView userInfo: userInfo];
103 [controlView addTrackingArea: area];
107 - (void) setHovered: (BOOL) hovered
112 - (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
114 FileListNode * node = [self representedObject];
115 Torrent * torrent = [node torrent];
116 NSSet * priorities = [torrent filePrioritiesForIndexes: [node indexes]];
118 const NSUInteger count = [priorities count];
119 if (fHoverRow && count > 0)
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];
125 [super drawWithFrame: cellFrame inView: controlView];
129 NSMutableArray * images = [NSMutableArray arrayWithCapacity: MAX(count, 1)];
134 //if ([self backgroundStyle] != NSBackgroundStyleDark)
136 NSImage * image = [[NSImage imageNamed: @"PriorityNormalTemplate.png"] imageWithColor: [NSColor lightGrayColor]];
137 [images addObject: image];
138 totalWidth = [image size].width;
143 NSColor * priorityColor = [self backgroundStyle] == NSBackgroundStyleDark ? [NSColor whiteColor] : [NSColor darkGrayColor];
146 if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_LOW]])
148 NSImage * image = [[NSImage imageNamed: @"PriorityLowTemplate.png"] imageWithColor: priorityColor];
149 [images addObject: image];
150 totalWidth += [image size].width;
152 if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_NORMAL]])
154 NSImage * image = [[NSImage imageNamed: @"PriorityNormalTemplate.png"] imageWithColor: priorityColor];
155 [images addObject: image];
156 totalWidth += [image size].width;
158 if ([priorities containsObject: [NSNumber numberWithInteger: TR_PRI_HIGH]])
160 NSImage * image = [[NSImage imageNamed: @"PriorityHighTemplate.png"] imageWithColor: priorityColor];
161 [images addObject: image];
162 totalWidth += [image size].width;
167 totalWidth -= IMAGE_OVERLAP * (count-1);
169 CGFloat currentWidth = floor(NSMidX(cellFrame) - totalWidth * 0.5);
171 for (NSImage * image in images)
173 const NSSize imageSize = [image size];
174 const NSRect imageRect = NSMakeRect(currentWidth, floor(NSMidY(cellFrame) - imageSize.height * 0.5), imageSize.width, imageSize.height);
176 [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0 respectFlipped: YES hints: nil];
178 currentWidth += imageSize.width - IMAGE_OVERLAP;