source: trunk/macosx/PiecesView.m @ 969

Last change on this file since 969 was 969, checked in by livings124, 16 years ago

Release box images on dealloc.

File size: 7.1 KB
Line 
1/******************************************************************************
2 * $Id$
3 *
4 * Copyright (c) 2005-2006 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 "PiecesView.h"
26
27#define MAX_ACROSS 18
28#define BETWEEN 1.0
29
30#define BLANK -99
31
32@implementation PiecesView
33
34- (id) init
35{
36    if ((self = [super init]))
37    {
38        fTorrent = nil;
39        int numPieces = MAX_ACROSS * MAX_ACROSS;
40        fPieces = malloc(numPieces);
41        int i;
42        for (i = 0; i < numPieces; i++)
43            fPieces[i] = BLANK;
44    }
45   
46    return self;
47}
48
49- (void) awakeFromNib
50{
51        NSSize size = [fImageView frame].size;
52        NSBezierPath * bp = [NSBezierPath bezierPathWithRect: [fImageView bounds]];
53       
54        //back image
55        fBack = [[NSImage alloc] initWithSize: size];
56       
57        [fBack lockFocus];
58        [[NSColor blackColor] set];
59        [bp fill];
60        [fBack unlockFocus];
61       
62        //white box image
63        fWhitePiece = [[NSImage alloc] initWithSize: size];
64       
65        [fWhitePiece lockFocus];
66        [[NSColor whiteColor] set];
67        [bp fill];
68        [fWhitePiece unlockFocus];
69       
70        //green box image
71        fGreenPiece = [[NSImage alloc] initWithSize: size];
72       
73        [fGreenPiece lockFocus];
74        [[NSColor colorWithCalibratedRed: 0.557 green: 0.992 blue: 0.639 alpha: 1.0] set];
75        [bp fill];
76        [fGreenPiece unlockFocus];
77       
78        //blue 1 box image
79        fBlue1Piece = [[NSImage alloc] initWithSize: size];
80       
81        [fBlue1Piece lockFocus];
82        [[NSColor colorWithCalibratedRed: 0.777 green: 0.906 blue: 1.0 alpha: 1.0] set];
83        [bp fill];
84        [fBlue1Piece unlockFocus];
85       
86        //blue 2 box image
87        fBlue2Piece = [[NSImage alloc] initWithSize: size];
88       
89        [fBlue2Piece lockFocus];
90        [[NSColor colorWithCalibratedRed: 0.682 green: 0.839 blue: 1.0 alpha: 1.0] set];
91        [bp fill];
92        [fBlue2Piece unlockFocus];
93       
94        //blue 3 box image
95        fBlue3Piece = [[NSImage alloc] initWithSize: size];
96       
97        [fBlue3Piece lockFocus];
98        [[NSColor colorWithCalibratedRed: 0.506 green: 0.745 blue: 1.0 alpha: 1.0] set];
99        [bp fill];
100        [fBlue3Piece unlockFocus];
101       
102        //actually draw the box
103        [self setTorrent: nil];
104}
105
106- (void) dealloc
107{
108    free(fPieces);
109   
110    [fBack release];
111    [fWhitePiece release];
112    [fGreenPiece release];
113    [fBlue1Piece release];
114    [fBlue2Piece release];
115    [fBlue3Piece release];
116   
117    if (fTorrent)
118        [fTorrent release];
119    [super dealloc];
120}
121
122- (void) setTorrent: (Torrent *) torrent
123{
124    if (fTorrent)
125    {
126        [fTorrent release];
127       
128        if (!torrent)
129            fTorrent = nil;
130    }
131   
132    if (torrent)
133    {
134        fTorrent = [torrent retain];
135       
136        //determine relevant values
137        fNumPieces = MAX_ACROSS * MAX_ACROSS;
138        if ([fTorrent pieceCount] < fNumPieces)
139        {
140            fNumPieces = [fTorrent pieceCount];
141           
142            fAcross = sqrt(fNumPieces);
143            if (fAcross * fAcross < fNumPieces)
144                fAcross++;
145        }
146        else
147            fAcross = MAX_ACROSS;
148       
149        fWidth = ([[fImageView image] size].width - (fAcross + 1) * BETWEEN) / fAcross;
150        fExtraBorder = ([[fImageView image] size].width - ((fWidth + BETWEEN) * fAcross + BETWEEN)) / 2;
151       
152        [self updateView: YES];
153    }
154   
155    [fImageView setHidden: torrent == nil];
156}
157
158- (void) updateView: (BOOL) first
159{
160    if (!fTorrent)
161        return;
162   
163    if (first)
164        [fImageView setImage: [[fBack copy] autorelease]];
165   
166    NSImage * image = [fImageView image];
167   
168    int8_t * pieces = malloc(fNumPieces);
169    [fTorrent getAvailability: pieces size: fNumPieces];
170   
171    int i, j, piece, index = -1;
172    NSPoint point;
173    NSRect rect = NSMakeRect(0, 0, fWidth, fWidth);
174    NSImage * pieceImage;
175    BOOL change = NO;
176       
177    for (i = 0; i < fAcross; i++)
178        for (j = 0; j < fAcross; j++)
179        {
180            index++;
181            if (index >= fNumPieces)
182                break;
183           
184            pieceImage = nil;
185           
186            piece = pieces[index];
187            if (piece < 0)
188            {
189                if (first || fPieces[index] != -1)
190                {
191                    fPieces[index] = -1;
192                    pieceImage = fGreenPiece;
193                }
194            }
195            else if (piece == 0)
196            {
197                if (first || fPieces[index] != 0)
198                {
199                    fPieces[index] = 0;
200                    pieceImage = fWhitePiece;
201                }
202            }
203            else if (piece == 1)
204            {
205                if (first || fPieces[index] != 1)
206                {
207                    fPieces[index] = 1;
208                    pieceImage = fBlue1Piece;
209                }
210            }
211            else if (piece == 2)
212            {
213                if (first || fPieces[index] != 2)
214                {
215                    fPieces[index] = 2;
216                    pieceImage = fBlue2Piece;
217                }
218            }
219            else
220            {
221                if (first || fPieces[index] != 3)
222                {
223                    fPieces[index] = 3;
224                    pieceImage = fBlue3Piece;
225                }
226            }
227           
228            if (pieceImage)
229            {
230                //drawing actually will occur, so figure out values
231                if (!change)
232                {
233                    [image lockFocus];
234                    change = YES;
235                }
236               
237                point = NSMakePoint(j * (fWidth + BETWEEN) + BETWEEN + fExtraBorder,
238                                    [[fImageView image] size].width - (i + 1) * (fWidth + BETWEEN) - fExtraBorder);
239                [pieceImage compositeToPoint: point fromRect: rect operation: NSCompositeSourceOver];
240            }
241        }
242   
243    if (change)
244    {
245        [image unlockFocus];
246        [fImageView setNeedsDisplay];
247    }
248   
249    free(pieces);
250}
251
252@end
Note: See TracBrowser for help on using the repository browser.