LabelAndIcon.st
author Claus Gittinger <cg@exept.de>
Wed, 29 Oct 1997 16:58:34 +0100
changeset 588 da5d228708c2
parent 578 72916ad0a5f3
child 629 4a2d83659f66
permissions -rw-r--r--
fixed icon-centering

"
 COPYRIGHT (c) 1996 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.
"


ListEntry subclass:#LabelAndIcon
	instanceVariableNames:'icon gap string'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Support'
!

!LabelAndIcon class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1996 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.
"

!

documentation
"
    an icon and a string, side by side.
    Usable as list entries in a listView (a fileList), as
    popUpMenuItems or as Label/Button image.

    This is an example class - currently not used by the system.

    Notice:
        This is different from ST-80's LabelAndIcon class, which
        inherits from Label. Therefore, things may change in the future.

    [author:]
        Claus Gittinger

    [see also:]
        ListEntry Text String Icon
        ListView SelectionInListView
"
!

examples
"
  in a listView:
                                                                        [exBegin]
    |top slv wrapper l fileImage dirImage|

    fileImage := Image 
                   width:16 
                   height:16
                   depth:1
                   fromArray:#[2r00000000 2r00000000
                               2r00000000 2r00000000
                               2r00011111 2r11100000
                               2r00010000 2r00100000
                               2r00010000 2r00011000
                               2r00010000 2r00001000
                               2r00010000 2r00001000
                               2r00010000 2r00001000
                               2r00010000 2r00001000
                               2r00010000 2r00001000
                               2r00010000 2r00001000
                               2r00010000 2r00001000
                               2r00010000 2r00001000
                               2r00011111 2r11111000
                               2r00000000 2r00000000
                               2r00000000 2r00000000].
    fileImage photometric:#whiteIs0.

    dirImage := Image 
                   width:16 
                   height:16
                   depth:1
                   fromArray:#[2r00000000 2r00000000
                               2r00000000 2r00000000
                               2r00000000 2r00000000
                               2r01111111 2r11110000
                               2r01000000 2r00001000
                               2r01000000 2r00000100
                               2r01000000 2r00000010
                               2r01000000 2r00000010
                               2r01000000 2r00000010
                               2r01000000 2r00000010
                               2r01000000 2r00000010
                               2r01000000 2r00000010
                               2r01111111 2r11111110
                               2r00000000 2r00000000
                               2r00000000 2r00000000
                               2r00000000 2r00000000].
    dirImage photometric:#whiteIs0.


    l := OrderedCollection new.
    Filename currentDirectory directoryContents do:[:s |
        s asFilename isDirectory ifTrue:[
            l add:(LabelAndIcon icon:dirImage string:s)
        ] ifFalse:[
            l add:(LabelAndIcon icon:fileImage string:s)
        ]
    ].

    slv := SelectionInListView new.
    slv list:l.
    wrapper := HVScrollableView forView:slv miniScrollerH:true.

    top := StandardSystemView extent:150@200.
    top add:wrapper in:(0.0@0.0 corner:1.0@1.0).
    top open.
                                                                        [exEnd]
  in a selectionInListView:
                                                                        [exBegin]
    |top slv wrapper l fileImage dirImage|

    dirImage := Image fromFile:'DirObj.xbm'.
    fileImage := Image fromFile:'FileObj.xbm'.


    l := OrderedCollection new.
    Filename currentDirectory directoryContents do:[:s |
        s asFilename isDirectory ifTrue:[
            l add:(LabelAndIcon icon:dirImage string:s)
        ] ifFalse:[
            l add:(LabelAndIcon icon:fileImage string:s)
        ]
    ].

    slv := SelectionInListView new.
    slv list:l.
    wrapper := HVScrollableView forView:slv miniScrollerH:true.

    top := StandardSystemView extent:150@200.
    top add:wrapper in:(0.0@0.0 corner:1.0@1.0).
    top open.
                                                                        [exEnd]
  in a menu:
                                                                        [exBegin]
    |top l fileImage dirImage|

    dirImage := Image fromFile:'DirObj.xbm'.
    fileImage := Image fromFile:'FileObj.xbm'.


    l := OrderedCollection new.
    l add:(LabelAndIcon icon:dirImage string:'create directory').
    l add:(LabelAndIcon icon:fileImage string:'create file').

    top := View new.

    top middleButtonMenu:(PopUpMenu labels:l
                        selectors:#(foo bar)).

    top open.
                                                                        [exEnd]
  in a button/label:
                                                                        [exBegin]
    |top l image|

    image := Image fromFile:'DirObj.xbm'.
    l := (LabelAndIcon icon:image string:'directory').

    top := Button label:l.
    top open.
                                                                        [exEnd]
"
! !

!LabelAndIcon class methodsFor:'instance creation'!

icon:anIcon string:aString
    ^ self new icon:anIcon string:aString

    "Created: 12.5.1996 / 20:00:58 / cg"
!

new
    ^ self basicNew initialize

    "Created: 12.5.1996 / 20:00:58 / cg"
! !

!LabelAndIcon methodsFor:'accessing'!

gap:pixels
    "set the spacing between the icon and the labelString.
     The default is 4."

    gap := pixels.

    "Created: 12.5.1996 / 20:00:52 / cg"
!

icon
    "return my icon part"

    ^ icon
!

icon:anIcon
    "set the icon image"

    icon := anIcon.

    "Created: 12.5.1996 / 20:00:52 / cg"
!

icon:anIcon string:aString
    "set both iconImage and the labelString"

    icon := anIcon.
    string := aString

    "Created: 12.5.1996 / 20:00:52 / cg"
!

string
    "return my string part"

    ^ string
!

string:aString
    "set the labelString"

    string := aString

    "Created: 12.5.1996 / 20:00:52 / cg"
! !

!LabelAndIcon methodsFor:'conversion'!

asString
    "return my string part"

    ^ string

    "Created: 12.5.1996 / 21:26:47 / cg"
! !

!LabelAndIcon methodsFor:'displaying'!

displayOn:aGC x:x y:y opaque:opaque
    "display the receiver on a GC"

    |yOffs yGap font fH iH fA yI xOffs|

    font := aGC font on:aGC device.
    fH := font height.
    icon isNil ifTrue:[
        iH := 0.
        yGap := 0.
        xOffs := 0.
    ] ifFalse:[
        yGap := (aGC device pixelPerMillimeter x rounded).
        iH := icon height + yGap.
        xOffs := icon width + gap.
    ].
    fA := font ascent.

    yOffs := 0.

    icon notNil ifTrue:[

"/ #ifdef OLD
"/        iH <= fA ifTrue:[
"/            "/ place the icon on the baseLine
"/            yI := y-fA.
"/ #else NEW

        iH <= fH ifTrue:[
            "/ place the icon centered
            yI := (fH-iH)//2 + y - fA + (yGap).
"/ #endif NEW
        ] ifFalse:[
            iH := iH - yGap. yGap := 0. 
            iH <= fH ifTrue:[
                "/ place the icon centered - no gap
                yI := (fH-iH)//2 + y - fA + (yGap).
            ] ifFalse:[
                yI := y-(fA+(font descent // 2)).
                yOffs := (iH - fH) // 2
            ]
        ].

        icon := icon onDevice:aGC device.

        (opaque 
        and:[icon mask isNil]) ifTrue:[
            aGC displayOpaqueForm:icon x:x y:yI.
        ] ifFalse:[
            aGC displayForm:icon x:x y:yI. 
        ].
    ].

    string 
        displayOn:aGC 
        x:x+xOffs
        y:y+yOffs.

    "Modified: / 29.10.1997 / 03:33:29 / cg"
! !

!LabelAndIcon methodsFor:'initialization'!

initialize
    gap := 4
! !

!LabelAndIcon methodsFor:'queries'!

heightOn:aGC
    "return the height of the receiver, if it is to be displayed on aGC"

    |iconHeight|

    icon notNil ifTrue:[
        iconHeight := icon height + (aGC device pixelPerMillimeter x rounded)
    ] ifFalse:[
        iconHeight := 0
    ].
    ^ iconHeight max:(string heightOn:aGC)

    "Created: 12.5.1996 / 20:26:20 / cg"
    "Modified: 27.2.1997 / 14:53:36 / cg"
!

widthOn:aGC
    "return the width of the receiver, if it is to be displayed on aGC"

    |iconWidth|

    icon notNil ifTrue:[
        iconWidth := icon width + gap
    ] ifFalse:[
        iconWidth := 0
    ].
    ^ iconWidth + (string widthOn:aGC)

    "Created: 12.5.1996 / 20:10:06 / cg"
    "Modified: 27.2.1997 / 14:53:58 / cg"
! !

!LabelAndIcon class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/LabelAndIcon.st,v 1.16 1997-10-29 15:58:34 cg Exp $'
! !