5
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
72
|
3 |
All Rights Reserved
|
5
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
0
|
12 |
|
|
13 |
Dictionary subclass:#ResourcePack
|
72
|
14 |
instanceVariableNames:'elements dependents packsClassName'
|
|
15 |
classVariableNames:'Packs'
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'System-Support'
|
0
|
18 |
!
|
|
19 |
|
5
|
20 |
ResourcePack comment:'
|
46
|
21 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
72
|
22 |
All Rights Reserved
|
54
|
23 |
|
72
|
24 |
$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.9 1994-10-10 02:33:00 claus Exp $
|
46
|
25 |
'!
|
5
|
26 |
|
46
|
27 |
!ResourcePack class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1993 by Claus Gittinger
|
72
|
32 |
All Rights Reserved
|
5
|
33 |
|
46
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
5
|
42 |
|
46
|
43 |
version
|
|
44 |
"
|
72
|
45 |
$Header: /cvs/stx/stx/libview/ResourcePack.st,v 1.9 1994-10-10 02:33:00 claus Exp $
|
46
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
to allow easy customization of smalltalk code (i.e. internationalization)
|
50
|
52 |
(replaces the obsolete Resource class).
|
54
|
53 |
ResourcePacks are class specific, meaning that every subclass of view
|
50
|
54 |
has an instance of ResourcePack (called resources) which is created when
|
|
55 |
the first instance of the view is created.
|
54
|
56 |
|
50
|
57 |
The resourcePack consists of a mapping from strings to values, which are
|
|
58 |
then used in labels, buttons, menus etc.
|
|
59 |
The resourcePack initializes itself from a file found in 'resources/<className>.rs',
|
|
60 |
where 'className' is built by the usual abbreviation mechanism (see abbrev-files).
|
|
61 |
Conditional mappings are possible, by including lines as:
|
72
|
62 |
#if <expression>
|
|
63 |
#endif
|
50
|
64 |
in the resourcefile. Example:
|
|
65 |
file 'foo.rs':
|
72
|
66 |
#if Language == #german
|
|
67 |
'abort' 'Abbruch'
|
|
68 |
#endif
|
|
69 |
#if Language == #french
|
|
70 |
'abort' 'canceller'
|
|
71 |
#endif
|
50
|
72 |
the corresponding resource-strings are accessed using:
|
72
|
73 |
resource string:'abort'
|
54
|
74 |
returning the mapped string (i.e. 'Abbruch' if the global Language is set
|
|
75 |
to #german)..
|
|
76 |
If no corresponding entry is found in the resources, the key is returned.
|
|
77 |
Translations can also include arguments, such as:
|
72
|
78 |
resources string:'really delete %1' with:fileName
|
46
|
79 |
"
|
|
80 |
! !
|
5
|
81 |
|
0
|
82 |
!ResourcePack class methodsFor:'initialization'!
|
|
83 |
|
|
84 |
initialize
|
24
|
85 |
Packs isNil ifTrue:[
|
72
|
86 |
Packs := WeakArray new:30
|
0
|
87 |
].
|
|
88 |
|
|
89 |
"ResourcePack initialize"
|
24
|
90 |
!
|
|
91 |
|
72
|
92 |
flushCachedResourcePacks
|
24
|
93 |
"forget all cached resources - needed after a style change"
|
|
94 |
|
|
95 |
Packs := nil.
|
|
96 |
self initialize
|
|
97 |
|
72
|
98 |
"ResourcePack flushCachedResourcePacks"
|
0
|
99 |
! !
|
|
100 |
|
54
|
101 |
!ResourcePack class methodsFor:'private'!
|
|
102 |
|
|
103 |
addToCache:aPack
|
|
104 |
|idx|
|
0
|
105 |
|
54
|
106 |
idx := Packs identityIndexOf:nil.
|
|
107 |
idx == 0 ifTrue:[
|
72
|
108 |
"
|
|
109 |
throw away oldest
|
|
110 |
"
|
|
111 |
idx := Packs size.
|
|
112 |
Packs replaceFrom:1 to:idx-1 with:Packs startingAt:2.
|
54
|
113 |
].
|
|
114 |
Packs at:idx put:aPack
|
|
115 |
!
|
0
|
116 |
|
54
|
117 |
searchCacheFor:aClassname
|
|
118 |
|sz|
|
0
|
119 |
|
24
|
120 |
Packs isNil ifTrue:[
|
72
|
121 |
self initialize.
|
|
122 |
^ nil
|
0
|
123 |
].
|
54
|
124 |
|
|
125 |
sz := Packs size.
|
|
126 |
1 to:sz do:[:idx |
|
72
|
127 |
|aPack|
|
54
|
128 |
|
72
|
129 |
aPack := Packs at:idx.
|
|
130 |
aPack notNil ifTrue:[
|
|
131 |
aClassname = aPack packsClassName ifTrue:[
|
|
132 |
"
|
|
133 |
bring to end for LRU
|
|
134 |
"
|
|
135 |
Packs replaceFrom:idx to:sz-1 with:Packs startingAt:idx+1.
|
|
136 |
Packs at:sz put:aPack.
|
|
137 |
^ aPack
|
|
138 |
]
|
|
139 |
]
|
0
|
140 |
].
|
54
|
141 |
^ nil
|
|
142 |
! !
|
|
143 |
|
|
144 |
!ResourcePack class methodsFor:'instance creation'!
|
|
145 |
|
|
146 |
fromFile:aFileName directory:dirName
|
|
147 |
"get the resource definitions from a file in a directory.
|
|
148 |
Uncached low-level entry."
|
|
149 |
|
72
|
150 |
|newPack|
|
0
|
151 |
|
2
|
152 |
newPack := self new.
|
72
|
153 |
newPack readFromFile:aFileName directory:dirName.
|
0
|
154 |
^ newPack
|
50
|
155 |
!
|
0
|
156 |
|
50
|
157 |
fromFile:aFileName
|
54
|
158 |
"get the resource definitions from a file in the default directory.
|
|
159 |
Uncached low-level entry."
|
50
|
160 |
|
|
161 |
^ self fromFile:aFileName directory:'resources'
|
|
162 |
|
|
163 |
"
|
|
164 |
ResourcePack fromFile:'SBrowser.rs'
|
|
165 |
ResourcePack fromFile:'FBrowser.rs'
|
|
166 |
ResourcePack fromFile:'Smalltalk.rs'
|
|
167 |
"
|
0
|
168 |
!
|
|
169 |
|
|
170 |
for:aClass
|
54
|
171 |
"get the full resource definitions for aClass (i.e. with super packs).
|
|
172 |
Also leave the resulting pack in the cache for faster access next time."
|
0
|
173 |
|
54
|
174 |
|nm pack superPack|
|
|
175 |
|
|
176 |
nm := aClass name.
|
|
177 |
pack := self searchCacheFor:nm.
|
|
178 |
pack notNil ifTrue:[^ pack].
|
0
|
179 |
|
54
|
180 |
pack := self fromFile:(Smalltalk fileNameForClass:nm) , '.rs'.
|
|
181 |
aClass == Object ifFalse:[
|
72
|
182 |
superPack := self for:(aClass superclass).
|
|
183 |
pack := pack merge:superPack
|
0
|
184 |
].
|
54
|
185 |
pack packsClassName:nm.
|
|
186 |
self addToCache:pack.
|
|
187 |
^ pack
|
0
|
188 |
! !
|
|
189 |
|
|
190 |
!ResourcePack methodsFor:'merging'!
|
|
191 |
|
|
192 |
merge:anotherPack
|
54
|
193 |
anotherPack keysAndValuesDo:[:key :value |
|
72
|
194 |
(self includesKey:key) ifFalse:[
|
|
195 |
self at:key put:value
|
|
196 |
]
|
0
|
197 |
]
|
|
198 |
! !
|
|
199 |
|
|
200 |
!ResourcePack methodsFor:'accessing'!
|
|
201 |
|
|
202 |
dependents:aCollection
|
|
203 |
dependents := aCollection
|
|
204 |
!
|
|
205 |
|
2
|
206 |
dependents
|
|
207 |
^ dependents
|
0
|
208 |
!
|
|
209 |
|
54
|
210 |
packsClassName
|
|
211 |
^ packsClassName
|
0
|
212 |
!
|
|
213 |
|
54
|
214 |
packsClassName:aString
|
|
215 |
packsClassName := aString
|
0
|
216 |
!
|
|
217 |
|
2
|
218 |
at:aKey
|
72
|
219 |
^ self at:aKey ifAbsent:aKey
|
0
|
220 |
!
|
|
221 |
|
|
222 |
at:aKey default:default
|
72
|
223 |
^ self at:aKey ifAbsent:default
|
0
|
224 |
!
|
|
225 |
|
2
|
226 |
name:aKey
|
72
|
227 |
^ self at:aKey ifAbsent:aKey
|
2
|
228 |
!
|
|
229 |
|
|
230 |
name:aKey default:default
|
72
|
231 |
^ self at:aKey ifAbsent:default
|
2
|
232 |
!
|
|
233 |
|
0
|
234 |
array:anArray
|
|
235 |
^ anArray collect:[:r | self at:r default:r]
|
|
236 |
!
|
|
237 |
|
|
238 |
string:s
|
72
|
239 |
^ self at:s ifAbsent:s
|
0
|
240 |
!
|
|
241 |
|
|
242 |
string:s with:arg
|
|
243 |
^ self string:s withArgs:(Array with:arg)
|
|
244 |
!
|
|
245 |
|
|
246 |
string:s with:arg1 with:arg2
|
|
247 |
^ self string:s withArgs:(Array with:arg1 with:arg2)
|
|
248 |
!
|
|
249 |
|
|
250 |
string:s withArgs:argArray
|
|
251 |
| template expandedString idx start stop next |
|
|
252 |
|
|
253 |
template := self at:s ifAbsent:[s].
|
|
254 |
expandedString := ''.
|
|
255 |
stop := template size.
|
|
256 |
start := 1.
|
|
257 |
[start < stop] whileTrue:[
|
72
|
258 |
idx := template indexOf:$% startingAt:start.
|
|
259 |
idx == 0 ifTrue:[
|
|
260 |
^ expandedString , (template copyFrom:start to:stop)
|
|
261 |
].
|
|
262 |
"found a %"
|
|
263 |
expandedString := expandedString , (template copyFrom:start to:(idx - 1)).
|
|
264 |
next := template at:(idx + 1).
|
|
265 |
(next == $%) ifTrue:[
|
|
266 |
expandedString := expandedString , '%'
|
|
267 |
] ifFalse:[
|
|
268 |
expandedString := expandedString , (argArray at:(next digitValue)) printString
|
|
269 |
].
|
|
270 |
start := idx + 2
|
2
|
271 |
].
|
|
272 |
^ expandedString
|
0
|
273 |
! !
|
|
274 |
|
|
275 |
!ResourcePack methodsFor:'file reading'!
|
|
276 |
|
72
|
277 |
readFromFile:fileName directory:dirName
|
|
278 |
|inStream|
|
|
279 |
|
|
280 |
inStream := Smalltalk systemFileStreamFor:(dirName , '/' , fileName).
|
|
281 |
inStream isNil ifTrue:[
|
|
282 |
"
|
|
283 |
an empty pack
|
|
284 |
"
|
|
285 |
^ nil
|
|
286 |
].
|
|
287 |
self readFromResourceStream:inStream in:dirName.
|
|
288 |
inStream close.
|
|
289 |
!
|
|
290 |
|
|
291 |
readFromResourceStream:inStream in:dirName
|
|
292 |
|lineString rest value ifLevel skipping l name first str macroName|
|
0
|
293 |
|
|
294 |
ifLevel := 0.
|
|
295 |
skipping := false.
|
|
296 |
[inStream atEnd] whileFalse:[
|
72
|
297 |
lineString := inStream nextLine.
|
|
298 |
(lineString notNil and:[lineString isEmpty not]) ifTrue:[
|
|
299 |
first := lineString at:1.
|
|
300 |
first == $; ifFalse:[
|
|
301 |
first == $# ifTrue:[
|
|
302 |
lineString := (lineString copyFrom:2) withoutSpaces.
|
|
303 |
|
|
304 |
(lineString startsWith:'if') ifTrue:[
|
|
305 |
skipping ifFalse:[
|
|
306 |
rest := lineString copyFrom:3.
|
|
307 |
value := Compiler evaluate:rest.
|
|
308 |
(value == #Error) ifTrue:[
|
|
309 |
Transcript show:('error in resource:' , lineString).
|
|
310 |
].
|
|
311 |
(value == false) ifTrue:[
|
|
312 |
skipping := true
|
|
313 |
]
|
|
314 |
].
|
|
315 |
ifLevel := ifLevel + 1
|
|
316 |
] ifFalse:[
|
|
317 |
(lineString startsWith:'endif') ifTrue:[
|
|
318 |
ifLevel := ifLevel - 1.
|
|
319 |
ifLevel == 0 ifTrue:[
|
|
320 |
skipping := false
|
|
321 |
]
|
|
322 |
] ifFalse:[
|
|
323 |
(lineString startsWith:'else') ifTrue:[
|
|
324 |
skipping := skipping not
|
|
325 |
] ifFalse:[
|
|
326 |
(lineString startsWith:'include') ifTrue:[
|
|
327 |
rest := lineString copyFrom:8.
|
|
328 |
value := Compiler evaluate:rest.
|
|
329 |
self readFromFile:value directory:dirName
|
|
330 |
]
|
|
331 |
]
|
|
332 |
]
|
|
333 |
].
|
|
334 |
] ifFalse:[
|
|
335 |
skipping ifFalse:[
|
|
336 |
lineString := lineString withoutSeparators.
|
|
337 |
name := nil.
|
|
338 |
(lineString at:1) == $' ifTrue:[
|
|
339 |
str := ReadStream on:lineString.
|
|
340 |
name := String readFrom:str.
|
|
341 |
l := str position.
|
|
342 |
|
|
343 |
"/ l := lineString indexOf:$' startingAt:2.
|
|
344 |
"/ l ~~ 0 ifTrue:[
|
|
345 |
"/ name := (lineString copyFrom:2 to:l-1).
|
|
346 |
"/ l := l + 1
|
|
347 |
"/ ]
|
|
348 |
] ifFalse:[
|
|
349 |
l := lineString indexOfSeparatorStartingAt:1.
|
|
350 |
l ~~ 0 ifTrue:[
|
|
351 |
name := lineString copyFrom:1 to:l-1.
|
|
352 |
]
|
|
353 |
].
|
|
354 |
name notNil ifTrue:[
|
|
355 |
rest := (lineString copyFrom:l) withoutSeparators.
|
|
356 |
(rest startsWith:'=') ifTrue:[
|
|
357 |
rest := rest copyFrom:2.
|
|
358 |
str := ReadStream on:rest.
|
|
359 |
macroName := str nextWord.
|
|
360 |
rest := str upToEnd.
|
|
361 |
value := self at:macroName.
|
|
362 |
value := Compiler evaluate:('self ' , rest)
|
|
363 |
receiver:value
|
|
364 |
notifying:nil.
|
|
365 |
] ifFalse:[
|
|
366 |
value := Compiler evaluate:rest.
|
|
367 |
(value == #Error) ifTrue:[
|
|
368 |
Transcript show:('error in resource:' , name).
|
|
369 |
]
|
|
370 |
].
|
|
371 |
self at:name put:value.
|
|
372 |
]
|
|
373 |
]
|
|
374 |
]
|
|
375 |
]
|
|
376 |
]
|
0
|
377 |
].
|
|
378 |
! !
|
|
379 |
|
|
380 |
ResourcePack initialize!
|