forked from ViennaRSS/vienna-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgressTextCell.m
149 lines (132 loc) · 5.07 KB
/
ProgressTextCell.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//
// ProgressTextCell.m
// Vienna
//
// Created by Curtis Faith on Mon Mar 15, 2010 based on ImageAndTextCell.m
// Copyright (c) 2004-2005 Steve Palmer. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "ProgressTextCell.h"
#define PROGRESS_INDICATOR_LEFT_MARGIN 2
#define PROGRESS_INDICATOR_DIMENSION 16
#define NO_ROW -1
/* This code originally taken from public stuff published
* by Apple and put into ImageAndTextCell. The code was modified to
* handle the case where we are only interested in the progress
* indicator and we don't have an associated TreeNode item to
* handle the indicator for us like we do in our ImageAndTextCell class
* used for the folder/feed view.
*/
@implementation ProgressTextCell
/* init
* Initialise a default instance of our cell.
*/
-(id)init
{
if ((self = [super init]) != nil)
{
inProgress = NO;
progressRow = NO_ROW;
currentRow = NO_ROW;
progressIndicator = nil;
}
return self;
}
-copyWithZone:(NSZone *)zone\
{
ProgressTextCell *cell = (ProgressTextCell *)[super copyWithZone:zone];
cell->inProgress = inProgress;
cell->progressRow = progressRow;
cell->currentRow = currentRow;
cell->progressIndicator = progressIndicator;
return cell;
}
/* setInProgress
* Set whether an active progress indicator should be shown for the row. This
* is used in drawInteriorWithFrame: to show and hide the progress indicators.
* We need to remember the row because we only want to turn off the progress
* indicator if we are redrawing the progress row. This method is generally
* called in an implementation of NSTableView's willDisplayCell:.
*/
-(void)setInProgress:(BOOL)newInProgress forRow:(NSInteger)row;
{
inProgress = newInProgress;
if (inProgress)
progressRow = row;
currentRow = row;
}
/* drawInteriorWithFrame:inView:
* Add or remove the progress indicator, if appropriate, and then have the
* superclass draw the cell.
*/
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
// If this row is the current progress row.
if (currentRow == progressRow)
{
// If the cell has a progress indicator, ensure it's framed properly
// and then reduce cellFrame to keep from overlapping it.
if (inProgress)
{
if (!progressIndicator)
{
// Allocate and initialize the spinning progress indicator.
NSRect progressRect = NSMakeRect(0, 0, PROGRESS_INDICATOR_DIMENSION, PROGRESS_INDICATOR_DIMENSION);
progressIndicator = [[NSProgressIndicator alloc] initWithFrame:progressRect];
[progressIndicator setControlSize:NSSmallControlSize];
[progressIndicator setStyle:NSProgressIndicatorSpinningStyle];
[progressIndicator setDisplayedWhenStopped:YES];
[progressIndicator setUsesThreadedAnimation:YES];
// Start the animation.
[progressIndicator startAnimation:self];
}
// Recompute the new cell frame taking out space on the right for the
// progress indicator.
NSRect progressIndicatorFrame;
NSInteger cellHeight = cellFrame.size.height;
NSInteger progressIndicatorSize = cellHeight < PROGRESS_INDICATOR_DIMENSION ? cellHeight : PROGRESS_INDICATOR_DIMENSION;
NSInteger progressOffset = (cellHeight - progressIndicatorSize) / 2;
NSInteger progressWidth = progressIndicatorSize + PROGRESS_INDICATOR_LEFT_MARGIN + progressOffset;
NSDivideRect(cellFrame, &progressIndicatorFrame, &cellFrame, progressWidth, NSMaxXEdge);
// Set the size for the progress indicator frame and add the margin.
progressIndicatorFrame.size = NSMakeSize(progressIndicatorSize, progressIndicatorSize);
progressIndicatorFrame.origin.x += PROGRESS_INDICATOR_LEFT_MARGIN;
progressIndicatorFrame.origin.y += progressOffset;
// Add the progress indicator as a subview of the controlView if
// it is not already one.
if ([progressIndicator superview] != controlView)
[controlView addSubview:progressIndicator];
// Set the progress indicator frame.
if (!NSEqualRects([progressIndicator frame], progressIndicatorFrame))
[progressIndicator setFrame:progressIndicatorFrame];
}
else
{
// Stop the animation and remove from the superview.
[progressIndicator setDisplayedWhenStopped:NO];
[progressIndicator stopAnimation:self];
[[progressIndicator superview] setNeedsDisplayInRect:[progressIndicator frame]];
[progressIndicator removeFromSuperviewWithoutNeedingDisplay];
// Release the progress indicator.
[progressIndicator release];
progressIndicator = nil;
}
}
// Draw the text
cellFrame.origin.y += 1;
cellFrame.origin.x += 2;
cellFrame.size.height -= 1;
[super drawInteriorWithFrame:cellFrame inView:controlView];
}
@end