LabelAndIcon.st
author Claus Gittinger <cg@exept.de>
Thu, 27 May 2004 16:26:48 +0200
changeset 2710 fcfa161492ae
parent 2674 e877ecb7d8b7
child 2773 ba1b2f93afee
permissions -rw-r--r--
added selectedFg/selectedBG attributes

"
 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.
"


"{ Package: 'stx:libwidg2' }"

ModelListEntry subclass:#LabelAndIcon
	instanceVariableNames:'icon gap image offset'
	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'!

form:aForm image:anImage

    ^self new form:aForm image:anImage string:''
!

form:aForm image:anImage string:aString

    ^self new form:aForm image:anImage string:aString
!

icon:anIcon string:aString

    ^ self new icon:anIcon string:aString

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

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

new
    ^ self basicNew initialize

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

string:aString

    ^ self new icon:nil string:aString

    "Created: / 21.6.1998 / 04:57:28 / cg"
! !

!LabelAndIcon methodsFor:'accessing'!

beCheckMark
    "/ self icon:(CheckToggle checkFormOn:Screen current)
    self icon:((CheckToggle smallCheckImageForStyle:#borderedFatcross) onDevice:Screen current)

    "Created: / 21.6.1998 / 05:01:01 / cg"
    "Modified: / 21.6.1998 / 05:03:23 / cg"
!

form:aForm image:anImage string:aString

    icon := aForm.
    image:= anImage.
    string := aString. 
!

gap
    "return the spacing between the icon and the labelString.
     The default is 4"

    ^ gap
!

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"
!

image
    "return my image"

    ^ image
!

image:anImage
    "set the image"

    image := anImage
!

offset:pixels
    "set the left offset (i.e. spacing to the left of the icon).
     The default is 0."

    offset := pixels.

    "Created: / 21.6.1998 / 03:11:03 / cg"
! !

!LabelAndIcon methodsFor:'comparing'!

= aStringOrLabelAndIcon

    aStringOrLabelAndIcon isNil ifTrue:[^ false].

    self hasIcon == aStringOrLabelAndIcon hasIcon ifFalse:[^ false].
    aStringOrLabelAndIcon hasIcon ifTrue:[
        icon = aStringOrLabelAndIcon icon ifFalse:[^ false].
    ].
    self hasImage == aStringOrLabelAndIcon hasImage ifFalse:[^ false].
    aStringOrLabelAndIcon hasImage ifTrue:[
        image = aStringOrLabelAndIcon image ifFalse:[^ false].
    ].
    ^ super = aStringOrLabelAndIcon
!

sameStringAndEmphasisAs:someStringOrLabelAndIcon
    someStringOrLabelAndIcon class == self class ifTrue:[
        icon = someStringOrLabelAndIcon icon ifFalse:[^ false].
        image = someStringOrLabelAndIcon image ifFalse:[^ false].
        (string sameStringAndEmphasisAs:someStringOrLabelAndIcon string) ifFalse:[ ^ false].
        ^ true.        
    ].

    (string sameStringAndEmphasisAs:someStringOrLabelAndIcon) ifFalse:[ ^ false].
    (icon isNil and:[image isNil]) ifTrue:[^ true].
    ^ false
! !

!LabelAndIcon methodsFor:'displaying'!

ascentOn:aGC
    ^ aGC font ascentOn:aGC device.
!

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

    |y1 x1 iconHeight imageHeight stringHeight maxHeight gapY asc|

    gapY := (aGC device pixelPerMillimeter x) rounded.
    "/ gapY := 0.
    asc  := (aGC font onDevice:aGC device) ascent.

    iconHeight   := icon notNil     ifTrue:[icon   height]       ifFalse:[0].
    imageHeight  := image notNil    ifTrue:[image  height]       ifFalse:[0].
    stringHeight := string size > 0 ifTrue:[string heightOn:aGC] ifFalse:[0].

    maxHeight := ((iconHeight max: imageHeight) + gapY) max: stringHeight.
    x1 := x + offset.

    icon notNil ifTrue:[
        y1 := y - asc + (maxHeight - iconHeight + 1 // 2).

        icon := icon onDevice:aGC device.

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

        x1 := x1 + icon width + gap
    ].

    image notNil ifTrue:[
        y1 := y - asc + (maxHeight - imageHeight + 1 // 2).
        image := image onDevice:aGC device.

        (opaque and:[image mask isNil]) ifTrue:[aGC displayOpaqueForm:image x:x1 y:y1]
                                       ifFalse:[aGC displayForm:image x:x1 y:y1].

        x1 := x1 + image width + gap
    ].

    stringHeight ~~ 0 ifTrue:[
        y1 := y + (maxHeight - stringHeight + 1 // 2).
        string displayOn:aGC x:x1 y:y1 opaque:opaque
    ]

    "Modified: / 21.6.1998 / 03:52:12 / cg"
!

on:aDevice
    "return a new image on a device
    "
    ^ self onDevice:aDevice

!

onDevice:aDevice
    "return a new image on a device
    "
    |form img|

    aDevice isNil ifTrue:[^ self].
    icon  notNil ifTrue:[form := icon  onDevice:aDevice].
    image notNil ifTrue:[img  := image onDevice:aDevice].

    ^ LabelAndIcon form:form image:img string:string



! !

!LabelAndIcon methodsFor:'initialization'!

initialize
    gap := 4.
    offset := 0.

    "Modified: / 21.6.1998 / 03:10:19 / cg"
! !

!LabelAndIcon methodsFor:'queries'!

hasIcon
    ^ icon notNil
!

hasImage
    ^ image notNil
!

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

    |iconHeight imageHeight stringHeight oneMillimeter|

    oneMillimeter := aGC device pixelPerMillimeter x rounded.
    "/ oneMillimeter := 0.

    icon notNil ifTrue:[
        iconHeight := icon height + oneMillimeter
    ] ifFalse:[
        iconHeight := 0
    ].
    image notNil ifTrue:[
        imageHeight := image height + oneMillimeter
    ] ifFalse:[
        imageHeight := 0   
    ].
    string size > 0 ifTrue:[
        stringHeight := string heightOn:aGC
    ] ifFalse:[
        stringHeight := 0   
    ].
    ^ ((iconHeight max: imageHeight) max: stringHeight)
!

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

    |width|

    width := offset.
    icon notNil ifTrue:[
        width := width + icon width
    ].
    image notNil ifTrue:[
        width := width + gap + image width
    ].
    string size > 0 ifTrue:[
        width := width + gap + (string widthOn:aGC)
    ].  
    ^width

    "Modified: / 21.6.1998 / 03:11:14 / cg"
! !

!LabelAndIcon class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libwidg2/LabelAndIcon.st,v 1.35 2004-03-08 13:54:46 stefan Exp $'
! !