PopUpList.st
author claus
Sun, 07 Aug 1994 15:23:42 +0200
changeset 38 4b9b70b2cc87
parent 20 0d9c61aacaa4
child 59 450ce95a72a4
permissions -rw-r--r--
2.10.3 pre-final version

"
 COPYRIGHT (c) 1994 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Button subclass:#PopUpList
         instanceVariableNames:'menu menuAction'
         classVariableNames:''
         poolDictionaries:''
         category:'Views-Interactors'
!

PopUpList comment:'
COPYRIGHT (c) 1994 by Claus Gittinger
              All Rights Reserved

$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.2 1994-08-07 13:23:07 claus Exp $
'!

!PopUpList class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1994 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libwidg/PopUpList.st,v 1.2 1994-08-07 13:23:07 claus Exp $
"
!

documentation
"
    a PopUpList is basically a button with a popup menu.
    The PopUpLists label is showing the current selection from the
    list.

    example use:

     |p|
     p := PopUpList label:'healthy fruit'.
     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').

     p open


    with an initial selection:

     |p|
     p := PopUpList label:'dummy'.
     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
     p selection:'apples'.
     p open


    with separating lines:

     |p|
     p := PopUpList label:'dummy'.
     p list:#('apples' 'bananas' 'grape' 'lemon' '-' 'margarithas').
     p selection:'apples'.
     p open
"
! !

!PopUpList methodsFor:'drawing'!

drawWith:fgColor and:bgColor
    |mmH mmV mW mH|

    pressed ifTrue:[
        super drawWith:enteredFgColor and:enteredBgColor
    ] ifFalse:[
        super drawWith:fgColor and:bgColor.
    ].
    mmH := (device horizontalPixelPerMillimeter * 1) rounded.
    mmV := (device verticalPixelPerMillimeter * 1) rounded.
    mW := (device horizontalPixelPerMillimeter * 2.5) rounded.
    mH := (device verticalPixelPerMillimeter * 1.5) rounded.

    self drawEdgesForX:(width - mW - (hSpace*2)) y:(height - mmV // 2)
                 width:mW height:mH level:2
! !

!PopUpList methodsFor:'events'!

buttonPress:button x:x y:y
    |org mv|

    ((button == 1) or:[button == #select]) ifTrue:[
        menu notNil ifTrue:[
            menu font:font.
"
            menu width:self width + (margin * 2).
"
            mv := menu menuView.
            mv width:(self width - (2 * menu margin) - (menu borderWidth*2)).
            mv level:0; borderWidth:0.
            mv fixSize.
            org := device translatePoint:0@0 
                                    from:(self id)
                                      to:(DisplayRootView new id).
        
            menu showAt:org "resizing:false"
        ]
    ].
! !

!PopUpList methodsFor:'initialization'!

initialize
    super initialize.
    self adjust:#left
! !

!PopUpList methodsFor:'accessing'!

action:aOneArgBlock
    "set the action to be performed on selection changes;
     the argument, aOneArgBlock will be evaluated with the
     selection-value as argument"

    menuAction := aOneArgBlock
!

list
    "return the list - i.e. the values shown in the pop-up list"

    ^ menu labels
!

list:aList
    "set the list - i.e. the values shown in the pop-up list"

    menu := PopUpMenu
                  labels:aList
               selectors:(Array new:(aList size) withAll:#select:)
                    args:aList
                receiver:self
                     for:self.
    realized ifTrue:[
        self computeLabelSize
    ]

    "
     |p|
     p := PopUpList label:'fruit ?'.
     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
     p open
    "
!

selection:indexOrString
    "set (force) a selection - usually done to set
     an initial selection"

    |index|

    index := menu labels indexOf:indexOrString.
    index == 0 ifTrue:[
        indexOrString isNumber ifTrue:[
            index := indexOrString
        ] ifFalse:[
            ^ self
        ]
    ].
    self label:(menu labels at:index)

    "
     |p|
     p := PopUpList label:'what fruit ?'.
     p list:#('apples' 'bananas' 'grape' 'lemon' 'margarithas').
     p selection:'apples'.
     p open
    "
! !

!PopUpList methodsFor:'private'!

computeLabelSize
    "compute the extent needed to hold the label plus the mark"

    |mmH mmV savedLogo longest longestWidth|

    menu isNil ifTrue:[
        super computeLabelSize
    ] ifFalse:[
        "hack: simulate logo change to longest menu entry"

        font := font on:device.
        longest := logo.
        longestWidth := font widthOf:logo.
        menu labels do:[:entry |
            |this|

            this := font widthOf:entry printString.
            this > longestWidth ifTrue:[
                longest := entry.
                longestWidth := this
            ].
        ].
        savedLogo := logo.
        logo := longest.
        super computeLabelSize.
        logo := savedLogo.
"self halt.     "
    ].
    mmH := device horizontalPixelPerMillimeter.
    mmV := device verticalPixelPerMillimeter.
    labelWidth := labelWidth + hSpace + (mmH * 2.5) rounded + hSpace.
    labelHeight := labelHeight max: (mmV * 2) rounded
! !

!PopUpList methodsFor:'user actions'!

select:anEntry
"/ 'selected:' print. anEntry printNewline.
    menuAction notNil ifTrue:[
        menuAction value:anEntry.
    ].
    self sizeFixed:true.
    self label:anEntry printString.
    (model notNil and:[changeSymbol notNil]) ifTrue:[
        model perform:changeSymbol with:anEntry
    ].
! !