author | Claus Gittinger <cg@exept.de> |
Wed, 26 Jun 2019 23:31:24 +0200 | |
changeset 6076 | e32451ae0577 |
parent 6057 | 8c54bab4666f |
child 6215 | 4480fe34e420 |
permissions | -rw-r--r-- |
6057 | 1 |
"{ Encoding: utf8 }" |
2 |
||
5478 | 3 |
" |
4 |
COPYRIGHT (c) 2009 by Claus Gittinger |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
3650 | 14 |
"{ Package: 'stx:libwidg2' }" |
15 |
||
4985 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
3650 | 18 |
Button subclass:#LinkButton |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
19 |
instanceVariableNames:'labelsAndActions' |
3662 | 20 |
classVariableNames:'DefaultLinkColor' |
3650 | 21 |
poolDictionaries:'' |
22 |
category:'Views-Layout' |
|
23 |
! |
|
24 |
||
25 |
!LinkButton class methodsFor:'documentation'! |
|
26 |
||
5478 | 27 |
copyright |
28 |
" |
|
29 |
COPYRIGHT (c) 2009 by Claus Gittinger |
|
30 |
All Rights Reserved |
|
31 |
||
32 |
This software is furnished under a license and may be used |
|
33 |
only in accordance with the terms of that license and with the |
|
34 |
inclusion of the above copyright notice. This software may not |
|
35 |
be provided or otherwise made available to, or used by, any |
|
36 |
other person. No title to or ownership of the software is |
|
37 |
hereby transferred. |
|
38 |
" |
|
39 |
! |
|
40 |
||
3650 | 41 |
documentation |
42 |
" |
|
43 |
Looks like a Label, but behaves like a button with individually clickable text components. |
|
5045 | 44 |
Can be used to create html-page-look-alike links in a view, |
5110 | 45 |
especially to make label-looking action buttons (as in the browser's info view). |
3650 | 46 |
|
47 |
[author:] |
|
48 |
cg (cg@CG-VOSTRO) |
|
49 |
||
50 |
[instance variables:] |
|
51 |
||
52 |
[class variables:] |
|
53 |
||
54 |
[see also:] |
|
55 |
||
56 |
" |
|
57 |
! |
|
58 |
||
59 |
examples |
|
60 |
" |
|
4071 | 61 |
[exBegin] |
3650 | 62 |
|v l| |
63 |
||
64 |
v := StandardSystemView new. |
|
65 |
l := LinkButton in:v. |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
66 |
l label: |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
67 |
(('Hello' actionForAll:[ Transcript showCR:'Hello Clicked']) |
4071 | 68 |
, ' ' |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
69 |
, ('World' actionForAll:[ Transcript showCR:'World Clicked'])). |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
70 |
|
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
71 |
v open |
4071 | 72 |
[exEnd] |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
73 |
|
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
74 |
|
4071 | 75 |
[exBegin] |
76 |
|v l| |
|
77 |
||
78 |
v := StandardSystemView new. |
|
79 |
l := LinkButton in:v. |
|
80 |
l label: |
|
81 |
((('Hello' actionForAll:[ Transcript showCR:'Hello Clicked']) colorizeAllWith:(Color blue)) |
|
82 |
, ' ' |
|
83 |
, ('World' actionForAll:[ Transcript showCR:'World Clicked'])). |
|
84 |
||
85 |
v open |
|
86 |
[exEnd] |
|
87 |
||
88 |
||
89 |
[exBegin] |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
90 |
|v l| |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
91 |
|
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
92 |
v := StandardSystemView new. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
93 |
l := LinkButton in:v. |
3650 | 94 |
l labelsAndActions:{ |
3662 | 95 |
'Hello' -> [ Transcript showCR:'Hello Clicked']. |
3650 | 96 |
' ' -> nil. |
3662 | 97 |
'World' -> [ Transcript showCR:'World Clicked']. |
3650 | 98 |
}. |
99 |
l foregroundColor:Color blue. |
|
100 |
v open |
|
4071 | 101 |
[exEnd] |
3668 | 102 |
|
103 |
||
4071 | 104 |
[exBegin] |
3668 | 105 |
|v l| |
106 |
||
107 |
v := StandardSystemView new. |
|
108 |
l := LinkButton in:v. |
|
109 |
l labelsAndActions:{ |
|
110 |
'Hello' -> nil. |
|
111 |
' ' -> nil. |
|
112 |
'World' -> [ Transcript showCR:'World Clicked']. |
|
113 |
}. |
|
114 |
l foregroundColor:Color blue. |
|
115 |
v open |
|
4071 | 116 |
[exEnd] |
4985 | 117 |
Dialog aboutToOpenBoxNotificationSignal handle:[:ex | |
118 |
|lbl| |
|
119 |
||
120 |
lbl := LinkButton label:(('XXX' |
|
121 |
colorizeAllWith:Color blue) |
|
122 |
actionForAll:[ Transcript showCR:'xxx' ]). |
|
123 |
ex box verticalPanel addComponent:lbl. |
|
124 |
] do:[ |
|
125 |
self warn:'Bla bla bla' |
|
126 |
]. |
|
127 |
||
3650 | 128 |
" |
129 |
! ! |
|
130 |
||
3662 | 131 |
!LinkButton class methodsFor:'defaults'! |
132 |
||
133 |
updateStyleCache |
|
134 |
"extract values from the styleSheet and cache them in class variables" |
|
135 |
||
136 |
<resource: #style ( #'linkButton.linkColor' )> |
|
137 |
||
138 |
DefaultLinkColor := StyleSheet colorAt:#'linkButton.linkColor' default:Color blue. |
|
139 |
! ! |
|
140 |
||
3650 | 141 |
!LinkButton methodsFor:'accessing'! |
142 |
||
5110 | 143 |
actionAt:aPoint |
6057 | 144 |
|pressAction emphasisAction emphasis pointInLabel| |
5110 | 145 |
|
5988 | 146 |
self enabled ifFalse:[ |
147 |
^ nil |
|
148 |
]. |
|
149 |
||
5110 | 150 |
pressAction := self pressAction ifNil:[ self releaseAction ]. |
151 |
pressAction isNil ifTrue:[ |
|
152 |
pressAction := self pressChannel ifNil:[ self releaseChannel ]. |
|
153 |
]. |
|
154 |
labelsAndActions isNil ifTrue:[ |
|
155 |
"take action from logo, which is normally a text with an action-emphasis" |
|
156 |
logo notNil ifTrue:[ |
|
5111 | 157 |
pointInLabel := (aPoint - (labelOriginX@labelOriginY)). |
158 |
logo isStringCollection ifTrue:[ |
|
159 |
|lineIndex line pointInLine| |
|
160 |
||
161 |
lineIndex := (pointInLabel y // self font height) + 1. |
|
162 |
line := logo at:lineIndex ifAbsent:nil. |
|
163 |
line notNil ifTrue:[ |
|
164 |
pointInLine := pointInLabel - (0 @ ((lineIndex - 1) * self font height)). |
|
165 |
emphasis := line emphasisAtPoint:pointInLine on:self. |
|
166 |
]. |
|
167 |
] ifFalse:[ |
|
168 |
emphasis := logo emphasisAtPoint:pointInLabel on:self. |
|
169 |
]. |
|
6057 | 170 |
(emphasisAction := Text actionBlockFromEmphasis:emphasis) notNil ifTrue:[ |
171 |
^ emphasisAction |
|
172 |
]. |
|
5110 | 173 |
]. |
174 |
^ pressAction. |
|
175 |
]. |
|
176 |
||
177 |
self labelsAndActionsWithPositionsDo:[:lbl :action :leftX :rightX | |
|
178 |
(aPoint x between:leftX and:rightX) ifTrue:[ |
|
179 |
^ labelsAndActions notNil ifTrue:action ifFalse:pressAction |
|
180 |
]. |
|
181 |
]. |
|
182 |
^ nil |
|
5988 | 183 |
|
184 |
"Modified: / 30-01-2019 / 15:42:31 / sr" |
|
6057 | 185 |
"Modified: / 28-05-2019 / 11:04:13 / Claus Gittinger" |
5110 | 186 |
! |
187 |
||
3650 | 188 |
labelsAndActions |
189 |
"returns the collection of label->action associations. |
|
190 |
For display, the label strings are drawn as one concatenated string (add separating spaces, if you have to). |
|
191 |
When clicked on a string, the corresponding action is called" |
|
192 |
||
193 |
^ labelsAndActions |
|
194 |
! |
|
195 |
||
196 |
labelsAndActions:aCollectionOfAssociations |
|
197 |
"set the collection of label->action associations. |
|
198 |
For display, the label strings are drawn as one concatenated string (add separating spaces, if you have to). |
|
199 |
When clicked on a string, the corresponding action is called" |
|
200 |
||
201 |
labelsAndActions := aCollectionOfAssociations. |
|
202 |
self label:((aCollectionOfAssociations collect:[:assoc | assoc key]) asStringWith:'') |
|
5045 | 203 |
! |
204 |
||
205 |
level:anInteger |
|
206 |
enterLevel := leaveLevel := onLevel := offLevel := level := anInteger. |
|
207 |
margin := level abs. |
|
3650 | 208 |
! ! |
209 |
||
210 |
!LinkButton methodsFor:'initialization'! |
|
211 |
||
4677 | 212 |
allViewBackground:something if:condition |
213 |
"set the viewBackground to something, a color, image or form, |
|
214 |
in myself and recursively in all of my subviews" |
|
215 |
||
216 |
self viewBackground:something if:condition. |
|
217 |
self backgroundColor:something. |
|
218 |
! |
|
219 |
||
3650 | 220 |
defaultControllerClass |
221 |
^ LinkButtonController |
|
3662 | 222 |
! |
223 |
||
224 |
initStyle |
|
225 |
super initStyle. |
|
226 |
||
227 |
level := enterLevel := leaveLevel := onLevel := offLevel := 0. |
|
4681 | 228 |
self borderWidth: 0. |
3662 | 229 |
DefaultLinkColor notNil ifTrue:[ |
4508 | 230 |
|color| |
4987 | 231 |
self paint:(color := DefaultLinkColor onDevice:device). |
4508 | 232 |
self foreground:color. |
3662 | 233 |
]. |
4681 | 234 |
enteredFgColor := nil. |
235 |
enteredBgColor := nil. |
|
4071 | 236 |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
237 |
"/ activeFgColor := enteredFgColor := foreground. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
238 |
"/ activeBgColor := enteredBgColor := viewBackground. |
4071 | 239 |
|
240 |
"Modified: / 07-09-2011 / 04:29:29 / cg" |
|
3662 | 241 |
! |
242 |
||
243 |
initialize |
|
244 |
super initialize. |
|
245 |
self enableMotionEvents |
|
246 |
! ! |
|
247 |
||
5110 | 248 |
!LinkButton methodsFor:'private'! |
3662 | 249 |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
250 |
actionEmphasisIn:aText atPoint:aPoint |
5605 | 251 |
"check for an actionBlock-emphasis in aText at aPoint. |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
252 |
Answer an Array with the whole emphasis and the actionBlock, |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
253 |
or nil" |
5605 | 254 |
|
6057 | 255 |
|emphasis action| |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
256 |
|
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
257 |
emphasis := aText emphasisAtPoint:aPoint on:self. |
6057 | 258 |
emphasis notNil ifTrue:[ |
259 |
action := Text actionBlockFromEmphasis:emphasis. |
|
260 |
action notNil ifTrue:[ |
|
261 |
^ { emphasis . action } |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
262 |
]. |
6057 | 263 |
]. |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
264 |
^ #(nil nil). |
5605 | 265 |
|
266 |
"Modified: / 13-09-2017 / 18:14:58 / mawalch" |
|
6057 | 267 |
"Modified: / 28-05-2019 / 11:03:05 / Claus Gittinger" |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
268 |
! |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
269 |
|
5110 | 270 |
labelsAndActionsWithPositionsDo:aFourArgBlock |
271 |
|leftX rightX w| |
|
272 |
||
273 |
leftX := labelOriginX. |
|
274 |
labelsAndActions isNil ifTrue:[ |
|
275 |
w := (self font widthOf:logo on:device). |
|
276 |
rightX := leftX + w-1. |
|
277 |
aFourArgBlock |
|
278 |
value:logo |
|
279 |
value:self pressAction |
|
280 |
value:leftX |
|
281 |
value:rightX. |
|
282 |
^ self |
|
283 |
]. |
|
284 |
||
285 |
labelsAndActions do:[:assoc | |
|
286 |
|lbl wEach| |
|
287 |
||
288 |
lbl := assoc key. |
|
289 |
wEach := (self font widthOf:lbl on:device). |
|
290 |
rightX := leftX + wEach-1. |
|
291 |
aFourArgBlock |
|
292 |
value:assoc key |
|
293 |
value:assoc value |
|
294 |
value:leftX |
|
295 |
value:rightX. |
|
296 |
||
297 |
leftX := rightX+1. |
|
298 |
]. |
|
299 |
^ nil |
|
300 |
! ! |
|
301 |
||
302 |
!LinkButton methodsFor:'redrawing'! |
|
303 |
||
4680 | 304 |
drawFocusFrame |
305 |
"/ intentionally ignored |
|
306 |
^ self |
|
307 |
! |
|
308 |
||
3662 | 309 |
drawStringLogo:aString x:x y:y |
5045 | 310 |
"redefined to draw any anchor under the mouse pointer with an underlined emphasis" |
3662 | 311 |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
312 |
|str entered mousePoint start len emphasis| |
3662 | 313 |
|
314 |
mousePoint := controller lastMousePoint. |
|
315 |
entered := controller entered. |
|
316 |
||
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
317 |
labelsAndActions isNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
318 |
(entered not or:[mousePoint isNil]) ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
319 |
self displayString:aString x:x y:y. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
320 |
^ self. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
321 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
322 |
|
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
323 |
str := aString. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
324 |
emphasis := (self actionEmphasisIn:aString atPoint:mousePoint) first. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
325 |
emphasis notNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
326 |
start := 1. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
327 |
aString emphasisCollection runsDo:[:eachLen :eachEmphasis| |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
328 |
len isNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
329 |
eachEmphasis == emphasis ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
330 |
len := eachLen. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
331 |
] ifFalse:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
332 |
start := start + eachLen. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
333 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
334 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
335 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
336 |
len notNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
337 |
str := str deepCopy. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
338 |
str emphasisFrom:start to:start+len-1 add:#underline. |
4071 | 339 |
"/ str emphasisFrom:start to:start+len-1 add:(#color -> foreground). |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
340 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
341 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
342 |
self displayString:str x:x y:y. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
343 |
^ self. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
344 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
345 |
|
3662 | 346 |
self labelsAndActionsWithPositionsDo:[:lbl :action :leftX :rightX | |
347 |
|l| |
|
3668 | 348 |
|
3662 | 349 |
l := lbl. |
3668 | 350 |
action notNil ifTrue:[ |
351 |
(entered and:[mousePoint notNil and:[mousePoint x between:leftX and:rightX]]) ifTrue:[ |
|
352 |
l := l allUnderlined |
|
353 |
]. |
|
3662 | 354 |
]. |
355 |
self displayString:l x:leftX y:y. |
|
356 |
]. |
|
4071 | 357 |
|
358 |
"Modified: / 07-09-2011 / 04:47:13 / cg" |
|
3662 | 359 |
! |
360 |
||
4681 | 361 |
is3D |
362 |
^ false. |
|
3650 | 363 |
! ! |
364 |
||
365 |
!LinkButton class methodsFor:'documentation'! |
|
366 |
||
367 |
version |
|
4985 | 368 |
^ '$Header$' |
4681 | 369 |
! |
370 |
||
371 |
version_CVS |
|
4985 | 372 |
^ '$Header$' |
3650 | 373 |
! ! |
4508 | 374 |