LinkButton.st
author Stefan Vogel <sv@exept.de>
Fri, 13 Mar 2009 14:57:48 +0100
changeset 3666 4a4e9dae4017
parent 3664 b49c21d9aac4
child 3668 e1888e938d68
permissions -rw-r--r--
changed #initialize - link is left-aligned by default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3650
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"{ Package: 'stx:libwidg2' }"
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
Button subclass:#LinkButton
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
     4
	instanceVariableNames:'labelsAndActions mouseOverLabelIndex'
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
     5
	classVariableNames:'DefaultLinkColor'
3650
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
	poolDictionaries:''
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
	category:'Views-Layout'
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
!
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
!LinkButton class methodsFor:'documentation'!
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
documentation
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
"
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
    Looks like a Label, but behaves like a button with individually clickable text components.
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
    Can be used to create htmp-page-look-alike links in a view.
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
    [author:]
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
        cg (cg@CG-VOSTRO)
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
    [instance variables:]
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
    [class variables:]
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
    [see also:]
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
"
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
!
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
examples
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
"
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
    |v l|
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
    v := StandardSystemView new.
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
    l := LinkButton in:v.
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
    l labelsAndActions:{ 
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    36
                        'Hello' -> [ Transcript showCR:'Hello Clicked'].
3650
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
                        ' ' -> nil.
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    38
                        'World' -> [ Transcript showCR:'World Clicked'].
3650
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
                       }.
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
    l foregroundColor:Color blue.
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
    v open
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
"
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
! !
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    45
!LinkButton class methodsFor:'defaults'!
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    46
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    47
updateStyleCache
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    48
    "extract values from the styleSheet and cache them in class variables"
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    49
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    50
    <resource: #style ( #'linkButton.linkColor' )>
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    51
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    52
    DefaultLinkColor := StyleSheet colorAt:#'linkButton.linkColor' default:Color blue.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    53
! !
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    54
3650
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
!LinkButton methodsFor:'accessing'!
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
labelsAndActions
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
    "returns the collection of label->action associations. 
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
     For display, the label strings are drawn as one concatenated string (add separating spaces, if you have to).
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
     When clicked on a string, the corresponding action is called"
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
    ^ labelsAndActions
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
!
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
labelsAndActions:aCollectionOfAssociations
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
    "set the collection of label->action associations. 
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
     For display, the label strings are drawn as one concatenated string (add separating spaces, if you have to).
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
     When clicked on a string, the corresponding action is called"
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
    labelsAndActions := aCollectionOfAssociations.
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
    self label:((aCollectionOfAssociations collect:[:assoc | assoc key]) asStringWith:'')
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
! !
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
!LinkButton methodsFor:'initialization'!
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
defaultControllerClass
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
    ^ LinkButtonController
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    78
!
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    79
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    80
initStyle
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    81
    super initStyle.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    82
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    83
    level := enterLevel := leaveLevel := onLevel := offLevel := 0.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    84
    DefaultLinkColor notNil ifTrue:[
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    85
        foreground := DefaultLinkColor onDevice:device.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    86
    ].
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    87
    activeFgColor := enteredFgColor := foreground.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    88
    activeBgColor := enteredBgColor := viewBackground.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    89
!
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    90
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    91
initialize
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    92
    super initialize.
3666
4a4e9dae4017 changed #initialize - link is left-aligned by default
Stefan Vogel <sv@exept.de>
parents: 3664
diff changeset
    93
    adjust := #left.
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    94
    self enableMotionEvents
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    95
! !
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    96
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    97
!LinkButton methodsFor:'redrawing'!
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    98
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
    99
actionAt:aPoint
3664
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   100
    |pressAction|
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   101
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   102
    pressAction := self pressAction ? self releaseAction.
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   103
    self labelsAndActionsWithPositionsDo:[:lbl :action :leftX :rightX |
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   104
        (aPoint x between:leftX and:rightX) ifTrue:[
3664
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   105
            ^ labelsAndActions notNil ifTrue:action ifFalse:pressAction
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   106
        ].
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   107
    ].
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   108
    ^ nil
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   109
!
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   110
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   111
drawStringLogo:aString x:x y:y
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   112
    "redefined to draw the part under the mouse pointer with an underined emphasis"
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   113
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   114
    |entered mousePoint|
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   115
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   116
    mousePoint := controller lastMousePoint.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   117
    entered := controller entered.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   118
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   119
    self labelsAndActionsWithPositionsDo:[:lbl :action :leftX :rightX |
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   120
        |l|
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   121
        l := lbl.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   122
        (entered and:[mousePoint notNil and:[mousePoint x between:leftX and:rightX]]) ifTrue:[
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   123
            l := l allUnderlined
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   124
        ].
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   125
        self displayString:l x:leftX y:y.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   126
    ].
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   127
!
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   128
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   129
labelsAndActionsWithPositionsDo:aFourArgBlock
3664
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   130
    |leftX rightX w|
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   131
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   132
    leftX := labelOriginX.
3664
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   133
    labelsAndActions isNil ifTrue:[
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   134
        w := (self font widthOf:logo on:device).
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   135
        rightX := leftX + w-1.
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   136
        aFourArgBlock 
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   137
            value:logo
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   138
            value:self pressAction
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   139
            value:leftX
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   140
            value:rightX.
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   141
        ^ self
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   142
    ].
b49c21d9aac4 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3662
diff changeset
   143
3662
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   144
    labelsAndActions do:[:assoc | 
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   145
        |lbl wEach|
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   146
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   147
        lbl := assoc key.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   148
        wEach := (self font widthOf:lbl on:device).
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   149
        rightX := leftX + wEach-1.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   150
        aFourArgBlock 
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   151
            value:assoc key
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   152
            value:assoc value
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   153
            value:leftX
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   154
            value:rightX.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   155
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   156
        leftX := rightX+1.
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   157
    ].
fb3778b66ce3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 3650
diff changeset
   158
    ^ nil
3650
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
! !
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
!LinkButton class methodsFor:'documentation'!
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
version
3666
4a4e9dae4017 changed #initialize - link is left-aligned by default
Stefan Vogel <sv@exept.de>
parents: 3664
diff changeset
   164
    ^ '$Header: /cvs/stx/stx/libwidg2/LinkButton.st,v 1.4 2009-03-13 13:57:48 stefan Exp $'
3650
60a95483b993 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
! !