source: trunk/macosx/FilePriorityCell.m @ 12445

Last change on this file since 12445 was 12445, checked in by livings124, 11 years ago

implement missing copyWithZone in FilePriorityCell?

  • Property Id set to /Users/mitchell/Desktop/Transmission/macosx/ActionMenuRatioToDisplayRatioTransformer.h
  • Property svn:keywords set to Date Rev Author Id
File size: 6.0 KB
Line 
1/******************************************************************************
2 * $Id: FilePriorityCell.m 12445 2011-05-19 22:04:46Z livings124 $
3 *
4 * Copyright (c) 2007-2011 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 "NSApplicationAdditions.h"
29#import "Torrent.h"
30
31@implementation FilePriorityCell
32
33- (id) init
34{
35    if ((self = [super init]))
36    {
37        [self setTrackingMode: NSSegmentSwitchTrackingSelectAny];
38        [self setControlSize: NSMiniControlSize];
39        [self setSegmentCount: 3];
40       
41        for (NSInteger i = 0; i < [self segmentCount]; i++)
42        {
43            [self setLabel: @"" forSegment: i];
44            [self setWidth: 9.0f forSegment: i]; //9 is minimum size to get proper look
45        }
46       
47        [self setImage: [NSImage imageNamed: @"PriorityControlLow.png"] forSegment: 0];
48        [self setImage: [NSImage imageNamed: @"PriorityControlNormal.png"] forSegment: 1];
49        [self setImage: [NSImage imageNamed: @"PriorityControlHigh.png"] forSegment: 2];
50       
51        fHoverRow = NO;
52    }
53    return self;
54}
55
56- (id) copyWithZone: (NSZone *) zone
57{
58    id value = [super copyWithZone: zone];
59    [value setRepresentedObject: [self representedObject]];
60    return value;
61}
62
63- (void) setSelected: (BOOL) flag forSegment: (NSInteger) segment
64{
65    [super setSelected: flag forSegment: segment];
66   
67    //only for when clicking manually
68    NSInteger priority;
69    switch (segment)
70    {
71        case 0:
72            priority = TR_PRI_LOW;
73            break;
74        case 1:
75            priority = TR_PRI_NORMAL;
76            break;
77        case 2:
78            priority = TR_PRI_HIGH;
79            break;
80    }
81   
82    FileOutlineView * controlView = (FileOutlineView *)[self controlView];
83    Torrent * torrent = [controlView torrent];
84    [torrent setFilePriority: priority forIndexes: [(FileListNode *)[self representedObject] indexes]];
85    [controlView reloadData];
86}
87
88- (void) addTrackingAreasForView: (NSView *) controlView inRect: (NSRect) cellFrame withUserInfo: (NSDictionary *) userInfo
89            mouseLocation: (NSPoint) mouseLocation
90{
91    NSTrackingAreaOptions options = NSTrackingEnabledDuringMouseDrag | NSTrackingMouseEnteredAndExited | NSTrackingActiveAlways;
92   
93    if (NSMouseInRect(mouseLocation, cellFrame, [controlView isFlipped]))
94    {
95        options |= NSTrackingAssumeInside;
96        [controlView setNeedsDisplayInRect: cellFrame];
97    }
98   
99    NSTrackingArea * area = [[NSTrackingArea alloc] initWithRect: cellFrame options: options owner: controlView userInfo: userInfo];
100    [controlView addTrackingArea: area];
101    [area release];
102}
103
104- (void) setHovered: (BOOL) hovered
105{
106    fHoverRow = hovered;
107}
108
109- (void) drawWithFrame: (NSRect) cellFrame inView: (NSView *) controlView
110{
111    Torrent * torrent = [(FileOutlineView *)controlView torrent];
112    FileListNode * node = [self representedObject];
113    NSSet * priorities = [torrent filePrioritiesForIndexes: [node indexes]];
114   
115    const NSUInteger count = [priorities count];
116    if (fHoverRow && count > 0)
117    {
118        [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_LOW]] forSegment: 0];
119        [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_NORMAL]] forSegment: 1];
120        [super setSelected: [priorities containsObject: [NSNumber numberWithInteger: TR_PRI_HIGH]] forSegment: 2];
121       
122        [super drawWithFrame: cellFrame inView: controlView];
123    }
124    else
125    {
126        NSImage * image;
127        if (count == 0)
128            image = [NSImage imageNamed: @"PriorityNone.png"];
129        else if (count > 1)
130            image = [NSImage imageNamed: @"PriorityMixed.png"];
131        else
132        {
133            switch ([[priorities anyObject] integerValue])
134            {
135                case TR_PRI_NORMAL:
136                    image = [NSImage imageNamed: @"PriorityNormal.png"];
137                    break;
138                case TR_PRI_LOW:
139                    image = [NSImage imageNamed: @"PriorityLow.png"];
140                    break;
141                case TR_PRI_HIGH:
142                    image = [NSImage imageNamed: @"PriorityHigh.png"];
143                    break;
144            }
145        }
146       
147        NSSize imageSize = [image size];
148        NSRect imageRect = NSMakeRect(NSMidX(cellFrame) - imageSize.width * 0.5, NSMidY(cellFrame) - imageSize.height * 0.5,
149                                        imageSize.width, imageSize.height);
150       
151        if ([NSApp isOnSnowLeopardOrBetter])
152            [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0
153                respectFlipped: YES hints: nil];
154        else
155        {
156            image = [image copy];
157            [image setFlipped: YES];
158            [image drawInRect: imageRect fromRect: NSZeroRect operation: NSCompositeSourceOver fraction: 1.0];
159            [image release];
160        }
161    }
162}
163
164@end
Note: See TracBrowser for help on using the repository browser.