50
|
1 |
'From Smalltalk/X, Version:2.10.5 on 30-apr-1995 at 8:48:16 am'!
|
|
2 |
|
|
3 |
View subclass:#ProgressIndicator
|
|
4 |
instanceVariableNames:'percentage showPercentage fgColor'
|
|
5 |
classVariableNames:''
|
|
6 |
poolDictionaries:''
|
|
7 |
category:'Views-Misc'
|
|
8 |
!
|
|
9 |
|
|
10 |
!ProgressIndicator class methodsFor:'documentation'!
|
|
11 |
|
|
12 |
version
|
|
13 |
"
|
|
14 |
$Header: /cvs/stx/stx/libwidg2/ProgressIndicator.st,v 1.1 1995-05-03 01:11:23 claus Exp $
|
|
15 |
"
|
|
16 |
!
|
|
17 |
|
|
18 |
documentation
|
|
19 |
"
|
|
20 |
a view showing a rectangle filled according the percentage value.
|
|
21 |
Can be used as a progress indicator a la MSwindows.
|
|
22 |
"
|
|
23 |
!
|
|
24 |
|
|
25 |
examples
|
|
26 |
"
|
|
27 |
typical progress indicator:
|
|
28 |
|
|
29 |
|top p h|
|
|
30 |
|
|
31 |
top := StandardSystemView new.
|
|
32 |
top extent:300@100.
|
|
33 |
top label:'Progress'.
|
|
34 |
p := ProgressIndicator in:top.
|
|
35 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
36 |
p level:-1.
|
|
37 |
h := p preferedExtent y.
|
|
38 |
p topInset:(h // 2) negated;
|
|
39 |
bottomInset:(h // 2) negated;
|
|
40 |
leftInset:5;
|
|
41 |
rightInset:5.
|
|
42 |
top open.
|
|
43 |
|
|
44 |
[
|
|
45 |
1 to:100 do:[:val |
|
|
46 |
(Delay forSeconds:0.05) wait.
|
|
47 |
p percentage:val
|
|
48 |
]
|
|
49 |
] fork
|
|
50 |
|
|
51 |
|
|
52 |
changing colors, turning percentage display off:
|
|
53 |
|
|
54 |
|top p h|
|
|
55 |
|
|
56 |
top := StandardSystemView new.
|
|
57 |
top extent:300@100.
|
|
58 |
top label:'Progress'.
|
|
59 |
p := ProgressIndicator in:top.
|
|
60 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
61 |
p level:-1.
|
|
62 |
p showPercentage:false.
|
|
63 |
p foregroundColor:(Color red).
|
|
64 |
h := 10.
|
|
65 |
p topInset:(h // 2) negated;
|
|
66 |
bottomInset:(h // 2) negated;
|
|
67 |
leftInset:5;
|
|
68 |
rightInset:5.
|
|
69 |
top open.
|
|
70 |
[
|
|
71 |
1 to:100 do:[:val |
|
|
72 |
(Delay forSeconds:0.05) wait.
|
|
73 |
p percentage:val
|
|
74 |
]
|
|
75 |
] fork
|
|
76 |
|
|
77 |
with border (2D look):
|
|
78 |
|
|
79 |
|top p h|
|
|
80 |
|
|
81 |
top := StandardSystemView new.
|
|
82 |
top extent:300@100.
|
|
83 |
top label:'Progress'.
|
|
84 |
p := ProgressIndicator in:top.
|
|
85 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
86 |
p borderWidth:1.
|
|
87 |
h := p preferedExtent y.
|
|
88 |
p topInset:(h // 2) negated;
|
|
89 |
bottomInset:(h // 2) negated;
|
|
90 |
leftInset:5;
|
|
91 |
rightInset:5.
|
|
92 |
top open.
|
|
93 |
[
|
|
94 |
1 to:100 do:[:val |
|
|
95 |
(Delay forSeconds:0.05) wait.
|
|
96 |
p percentage:val
|
|
97 |
]
|
|
98 |
] fork
|
|
99 |
|
|
100 |
|
|
101 |
getting progress from a model:
|
|
102 |
|
|
103 |
|model top p h|
|
|
104 |
|
|
105 |
model := 0 asValue.
|
|
106 |
|
|
107 |
top := StandardSystemView new.
|
|
108 |
top extent:300@100.
|
|
109 |
top label:'Progress'.
|
|
110 |
p := ProgressIndicator in:top.
|
|
111 |
p model:model.
|
|
112 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
113 |
p level:-1.
|
|
114 |
h := p preferedExtent y.
|
|
115 |
p topInset:(h // 2) negated;
|
|
116 |
bottomInset:(h // 2) negated;
|
|
117 |
leftInset:5;
|
|
118 |
rightInset:5.
|
|
119 |
top open.
|
|
120 |
|
|
121 |
[
|
|
122 |
1 to:100 do:[:val |
|
|
123 |
(Delay forSeconds:0.05) wait.
|
|
124 |
model value:val
|
|
125 |
]
|
|
126 |
] fork
|
|
127 |
|
|
128 |
concrete example:
|
|
129 |
search all files in the source directory for a string
|
|
130 |
using grep. Show progress while doing so.
|
|
131 |
|
|
132 |
|top p h names done|
|
|
133 |
|
|
134 |
top := StandardSystemView new.
|
|
135 |
top extent:300@100.
|
|
136 |
top label:'Searching ...'.
|
|
137 |
p := ProgressIndicator in:top.
|
|
138 |
p origin:(0.0@0.5) corner:(1.0@0.5).
|
|
139 |
p level:-1.
|
|
140 |
h := p preferedExtent y.
|
|
141 |
p topInset:(h // 2) negated;
|
|
142 |
bottomInset:(h // 2) negated;
|
|
143 |
leftInset:5;
|
|
144 |
rightInset:5.
|
|
145 |
top openWithPriority:(Processor activePriority + 1).
|
|
146 |
|
|
147 |
names := 'source' asFilename directoryContents.
|
|
148 |
done := 0.
|
|
149 |
names do:[:aName |
|
|
150 |
|stream line|
|
|
151 |
|
|
152 |
p percentage:(done / names size * 100).
|
|
153 |
stream := ('source/' , aName) asFilename readStream.
|
|
154 |
[stream atEnd] whileFalse:[
|
|
155 |
line := stream nextLine.
|
|
156 |
(line findString:'subclass:') ~~ 0 ifTrue:[
|
|
157 |
Transcript showCr:line
|
|
158 |
].
|
|
159 |
].
|
|
160 |
stream close.
|
|
161 |
done := done + 1
|
|
162 |
].
|
|
163 |
|
|
164 |
top destroy
|
|
165 |
"
|
|
166 |
! !
|
|
167 |
|
|
168 |
!ProgressIndicator methodsFor:'drawing'!
|
|
169 |
|
|
170 |
redraw
|
|
171 |
|s rx sx sy sw m w h|
|
|
172 |
|
|
173 |
m := margin + 1.
|
|
174 |
w := width - (m*2).
|
|
175 |
h := height - (m*2).
|
|
176 |
|
|
177 |
s := percentage printString , ' %'.
|
|
178 |
sw := font widthOf:s.
|
|
179 |
sx := (width - sw) // 2.
|
|
180 |
sy := height // 2 + font descent + 2.
|
|
181 |
|
|
182 |
rx := (w * percentage / 100) rounded.
|
|
183 |
|
|
184 |
self paint:Color white.
|
|
185 |
self fillRectangleX:m y:m width:w height:h.
|
|
186 |
|
|
187 |
showPercentage ifTrue:[
|
|
188 |
rx <= (sx+sw) ifTrue:[
|
|
189 |
self paint:Color black.
|
|
190 |
self displayString:s x:sx y:sy.
|
|
191 |
]
|
|
192 |
].
|
|
193 |
|
|
194 |
self paint:fgColor.
|
|
195 |
self fillRectangleX:m y:m width:rx height:h.
|
|
196 |
|
|
197 |
showPercentage ifTrue:[
|
|
198 |
rx >= sx ifTrue:[
|
|
199 |
self clipRect:(m@m corner:rx+1 @ h).
|
|
200 |
self paint:Color white.
|
|
201 |
self displayString:s x:sx y:sy.
|
|
202 |
self clipRect:nil
|
|
203 |
]
|
|
204 |
]
|
|
205 |
! !
|
|
206 |
|
|
207 |
!ProgressIndicator methodsFor:'initialization'!
|
|
208 |
|
|
209 |
initialize
|
|
210 |
super initialize.
|
|
211 |
viewBackground := StyleSheet colorAt:'progressIndicatorViewBackground' default:Color white.
|
|
212 |
fgColor := StyleSheet colorAt:'progressIndicatorForegroundColor' default:Color blue.
|
|
213 |
percentage := 0.
|
|
214 |
showPercentage := true.
|
|
215 |
! !
|
|
216 |
|
|
217 |
!ProgressIndicator methodsFor:'accessing'!
|
|
218 |
|
|
219 |
percentage:aNumber
|
|
220 |
|newPercentage|
|
|
221 |
|
|
222 |
newPercentage := ((aNumber max:0) min:100) rounded.
|
|
223 |
newPercentage ~~ percentage ifTrue:[
|
|
224 |
percentage := newPercentage.
|
|
225 |
shown ifTrue:[self redraw].
|
|
226 |
]
|
|
227 |
!
|
|
228 |
|
|
229 |
showPercentage:aBoolean
|
|
230 |
showPercentage := aBoolean
|
|
231 |
!
|
|
232 |
|
|
233 |
foregroundColor:aColor
|
|
234 |
fgColor := aColor
|
|
235 |
! !
|
|
236 |
|
|
237 |
!ProgressIndicator methodsFor:'queries'!
|
|
238 |
|
|
239 |
preferedExtent
|
|
240 |
^ 100 @ (font height + font descent + ((margin + 1) * 2))
|
|
241 |
! !
|
|
242 |
|
|
243 |
!ProgressIndicator methodsFor:'change & update'!
|
|
244 |
|
|
245 |
update:aspect with:aParameter from:changedObject
|
|
246 |
(aspect == aspectMsg
|
|
247 |
and:[changedObject == model]) ifTrue:[
|
|
248 |
self percentage:(model perform:aspectMsg).
|
|
249 |
^ self
|
|
250 |
].
|
|
251 |
^ super update:aspect with:aParameter from:changedObject
|
|
252 |
! !
|
|
253 |
|