author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 13:15:17 +0200 | |
changeset 6243 | 949681aaf3e4 |
parent 6216 | 9c9487e63400 |
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 |
6215 | 144 |
"if aPoint is over a clickable (anchor-) link, return its helptext. |
145 |
Otherwise, return nil" |
|
146 |
||
6057 | 147 |
|pressAction emphasisAction emphasis pointInLabel| |
5110 | 148 |
|
5988 | 149 |
self enabled ifFalse:[ |
150 |
^ nil |
|
151 |
]. |
|
152 |
||
5110 | 153 |
pressAction := self pressAction ifNil:[ self releaseAction ]. |
154 |
pressAction isNil ifTrue:[ |
|
155 |
pressAction := self pressChannel ifNil:[ self releaseChannel ]. |
|
156 |
]. |
|
157 |
labelsAndActions isNil ifTrue:[ |
|
158 |
"take action from logo, which is normally a text with an action-emphasis" |
|
159 |
logo notNil ifTrue:[ |
|
5111 | 160 |
pointInLabel := (aPoint - (labelOriginX@labelOriginY)). |
161 |
logo isStringCollection ifTrue:[ |
|
162 |
|lineIndex line pointInLine| |
|
163 |
||
164 |
lineIndex := (pointInLabel y // self font height) + 1. |
|
165 |
line := logo at:lineIndex ifAbsent:nil. |
|
166 |
line notNil ifTrue:[ |
|
167 |
pointInLine := pointInLabel - (0 @ ((lineIndex - 1) * self font height)). |
|
6216 | 168 |
emphasis := line emphasisAtX:pointInLine x on:self. |
5111 | 169 |
]. |
170 |
] ifFalse:[ |
|
6216 | 171 |
emphasis := logo emphasisAtX:pointInLabel x on:self. |
5111 | 172 |
]. |
6216 | 173 |
emphasis notNil ifTrue:[ |
174 |
(emphasisAction := Text actionBlockFromEmphasis:emphasis) notNil ifTrue:[ |
|
175 |
^ emphasisAction |
|
176 |
]. |
|
6057 | 177 |
]. |
5110 | 178 |
]. |
179 |
^ pressAction. |
|
180 |
]. |
|
181 |
||
182 |
self labelsAndActionsWithPositionsDo:[:lbl :action :leftX :rightX | |
|
183 |
(aPoint x between:leftX and:rightX) ifTrue:[ |
|
184 |
^ labelsAndActions notNil ifTrue:action ifFalse:pressAction |
|
185 |
]. |
|
186 |
]. |
|
187 |
^ nil |
|
5988 | 188 |
|
189 |
"Modified: / 30-01-2019 / 15:42:31 / sr" |
|
6057 | 190 |
"Modified: / 28-05-2019 / 11:04:13 / Claus Gittinger" |
5110 | 191 |
! |
192 |
||
3650 | 193 |
labelsAndActions |
194 |
"returns the collection of label->action associations. |
|
195 |
For display, the label strings are drawn as one concatenated string (add separating spaces, if you have to). |
|
196 |
When clicked on a string, the corresponding action is called" |
|
197 |
||
198 |
^ labelsAndActions |
|
199 |
! |
|
200 |
||
201 |
labelsAndActions:aCollectionOfAssociations |
|
202 |
"set the collection of label->action associations. |
|
203 |
For display, the label strings are drawn as one concatenated string (add separating spaces, if you have to). |
|
204 |
When clicked on a string, the corresponding action is called" |
|
205 |
||
206 |
labelsAndActions := aCollectionOfAssociations. |
|
207 |
self label:((aCollectionOfAssociations collect:[:assoc | assoc key]) asStringWith:'') |
|
5045 | 208 |
! |
209 |
||
210 |
level:anInteger |
|
211 |
enterLevel := leaveLevel := onLevel := offLevel := level := anInteger. |
|
212 |
margin := level abs. |
|
3650 | 213 |
! ! |
214 |
||
215 |
!LinkButton methodsFor:'initialization'! |
|
216 |
||
4677 | 217 |
allViewBackground:something if:condition |
218 |
"set the viewBackground to something, a color, image or form, |
|
219 |
in myself and recursively in all of my subviews" |
|
220 |
||
221 |
self viewBackground:something if:condition. |
|
222 |
self backgroundColor:something. |
|
223 |
! |
|
224 |
||
3650 | 225 |
defaultControllerClass |
226 |
^ LinkButtonController |
|
3662 | 227 |
! |
228 |
||
229 |
initStyle |
|
230 |
super initStyle. |
|
231 |
||
232 |
level := enterLevel := leaveLevel := onLevel := offLevel := 0. |
|
4681 | 233 |
self borderWidth: 0. |
3662 | 234 |
DefaultLinkColor notNil ifTrue:[ |
4508 | 235 |
|color| |
4987 | 236 |
self paint:(color := DefaultLinkColor onDevice:device). |
4508 | 237 |
self foreground:color. |
3662 | 238 |
]. |
4681 | 239 |
enteredFgColor := nil. |
240 |
enteredBgColor := nil. |
|
4071 | 241 |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
242 |
"/ activeFgColor := enteredFgColor := foreground. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
243 |
"/ activeBgColor := enteredBgColor := viewBackground. |
4071 | 244 |
|
245 |
"Modified: / 07-09-2011 / 04:29:29 / cg" |
|
3662 | 246 |
! |
247 |
||
248 |
initialize |
|
249 |
super initialize. |
|
250 |
self enableMotionEvents |
|
251 |
! ! |
|
252 |
||
5110 | 253 |
!LinkButton methodsFor:'private'! |
3662 | 254 |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
255 |
actionEmphasisIn:aText atPoint:aPoint |
5605 | 256 |
"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
|
257 |
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
|
258 |
or nil" |
5605 | 259 |
|
6216 | 260 |
|emphasis emphasisAction| |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
261 |
|
6216 | 262 |
emphasis := aText emphasisAtX:aPoint x on:self. |
6057 | 263 |
emphasis notNil ifTrue:[ |
6216 | 264 |
emphasisAction := Text actionBlockFromEmphasis:emphasis. |
265 |
emphasisAction notNil ifTrue:[ |
|
266 |
^ { emphasis . emphasisAction } |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
267 |
]. |
6057 | 268 |
]. |
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
269 |
^ #(nil nil). |
5605 | 270 |
|
271 |
"Modified: / 13-09-2017 / 18:14:58 / mawalch" |
|
6057 | 272 |
"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
|
273 |
! |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
274 |
|
5110 | 275 |
labelsAndActionsWithPositionsDo:aFourArgBlock |
276 |
|leftX rightX w| |
|
277 |
||
278 |
leftX := labelOriginX. |
|
279 |
labelsAndActions isNil ifTrue:[ |
|
280 |
w := (self font widthOf:logo on:device). |
|
281 |
rightX := leftX + w-1. |
|
282 |
aFourArgBlock |
|
283 |
value:logo |
|
284 |
value:self pressAction |
|
285 |
value:leftX |
|
286 |
value:rightX. |
|
287 |
^ self |
|
288 |
]. |
|
289 |
||
290 |
labelsAndActions do:[:assoc | |
|
291 |
|lbl wEach| |
|
292 |
||
293 |
lbl := assoc key. |
|
294 |
wEach := (self font widthOf:lbl on:device). |
|
295 |
rightX := leftX + wEach-1. |
|
296 |
aFourArgBlock |
|
297 |
value:assoc key |
|
298 |
value:assoc value |
|
299 |
value:leftX |
|
300 |
value:rightX. |
|
301 |
||
302 |
leftX := rightX+1. |
|
303 |
]. |
|
304 |
^ nil |
|
305 |
! ! |
|
306 |
||
307 |
!LinkButton methodsFor:'redrawing'! |
|
308 |
||
4680 | 309 |
drawFocusFrame |
310 |
"/ intentionally ignored |
|
311 |
^ self |
|
312 |
! |
|
313 |
||
3662 | 314 |
drawStringLogo:aString x:x y:y |
5045 | 315 |
"redefined to draw any anchor under the mouse pointer with an underlined emphasis" |
3662 | 316 |
|
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
317 |
|str entered mousePoint start len emphasis| |
3662 | 318 |
|
319 |
mousePoint := controller lastMousePoint. |
|
320 |
entered := controller entered. |
|
321 |
||
3670
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
322 |
labelsAndActions isNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
323 |
(entered not or:[mousePoint isNil]) ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
324 |
self displayString:aString x:x y:y. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
325 |
^ self. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
326 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
327 |
|
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
328 |
str := aString. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
329 |
emphasis := (self actionEmphasisIn:aString atPoint:mousePoint) first. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
330 |
emphasis notNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
331 |
start := 1. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
332 |
aString emphasisCollection runsDo:[:eachLen :eachEmphasis| |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
333 |
len isNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
334 |
eachEmphasis == emphasis ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
335 |
len := eachLen. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
336 |
] ifFalse:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
337 |
start := start + eachLen. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
338 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
339 |
]. |
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 |
len notNil ifTrue:[ |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
342 |
str := str deepCopy. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
343 |
str emphasisFrom:start to:start+len-1 add:#underline. |
4071 | 344 |
"/ 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
|
345 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
346 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
347 |
self displayString:str x:x y:y. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
348 |
^ self. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
349 |
]. |
ccfd04be9736
Now works with Text/actionBlocks
Stefan Vogel <sv@exept.de>
parents:
3668
diff
changeset
|
350 |
|
3662 | 351 |
self labelsAndActionsWithPositionsDo:[:lbl :action :leftX :rightX | |
352 |
|l| |
|
3668 | 353 |
|
3662 | 354 |
l := lbl. |
3668 | 355 |
action notNil ifTrue:[ |
356 |
(entered and:[mousePoint notNil and:[mousePoint x between:leftX and:rightX]]) ifTrue:[ |
|
357 |
l := l allUnderlined |
|
358 |
]. |
|
3662 | 359 |
]. |
360 |
self displayString:l x:leftX y:y. |
|
361 |
]. |
|
4071 | 362 |
|
363 |
"Modified: / 07-09-2011 / 04:47:13 / cg" |
|
3662 | 364 |
! |
365 |
||
4681 | 366 |
is3D |
367 |
^ false. |
|
3650 | 368 |
! ! |
369 |
||
370 |
!LinkButton class methodsFor:'documentation'! |
|
371 |
||
372 |
version |
|
4985 | 373 |
^ '$Header$' |
4681 | 374 |
! |
375 |
||
376 |
version_CVS |
|
4985 | 377 |
^ '$Header$' |
3650 | 378 |
! ! |
4508 | 379 |