MenuItem.st
author ca
Fri, 23 May 1997 16:04:09 +0200
changeset 581 0080d12b5758
parent 580 5d407019526d
child 584 9a4c30f7586f
permissions -rw-r--r--
enabled can be something not only a boolean support of ResourceRetriver

Object subclass:#MenuItem
	instanceVariableNames:'enabled label value nameKey adornment'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Support'
!

Object subclass:#MenuItemAdornment
	instanceVariableNames:'color indication submenu shortcutKey labelText labelImage
		accessCharacterPosition'
	classVariableNames:''
	poolDictionaries:''
	privateIn:MenuItem
!

!MenuItem class methodsFor:'documentation'!

documentation
"
    not yet finished MenuItem class - this will eventually replace
    most of the MenuView and PopUpMenu stuff.
    (and hopefully be ST-80 compatible ...)

    For now, only a subset of the full protocol is implemented.

    [author:]
        Claus Gittinger

    [see also:]
        MenuItem
        PopUpMenu
"

! !

!MenuItem class methodsFor:'instance creation'!

labeled:aString
    ^ self new label:aString
! !

!MenuItem methodsFor:'accessing'!

accessCharacterPosition
    "get the index of the access character in the label text or string, or nil if none
    "
    adornment notNil ifTrue:[
        ^ adornment accessCharacterPosition
    ].
    ^ nil
!

accessCharacterPosition:index
    "set the index of the access character in the label text or string, or nil if none
    "
    (index isNil and:[adornment isNil]) ifFalse:[
        self adornment accessCharacterPosition:index
    ]
!

indication
    "not yet supported
    "
    ^ true
!

indication:anIndication
    "not yet supported
    "
!

label
    ^ label

    "Created: 25.2.1997 / 19:48:16 / cg"
!

label:aString
    label := aString

    "Created: 25.2.1997 / 19:55:16 / cg"
!

labelImage
    "gets the labelImage
    "
    adornment notNil ifTrue:[
        ^ adornment labelImage
    ].
  ^ nil
!

labelImage:aResourceRetriever
    "set the labelImage
    "
    aResourceRetriever notNil ifTrue:[
        self adornment labelImage:aResourceRetriever
    ]
!

nameKey
    ^ nameKey


!

nameKey:aNameKey
    nameKey := aNameKey.


!

rawLabel
    ^ label

    "Created: 25.2.1997 / 19:48:16 / cg"
!

rawLabel:aString
    label := aString

    "Created: 25.2.1997 / 19:11:02 / cg"
!

shortcutKeyCharacter
    "ignored for now"

    adornment notNil ifTrue:[
        ^ adornment shortcutKeyCharacter
    ].
    ^ nil
!

shortcutKeyCharacter:aKey
    "set the  key to press to select the menu item from the keyboard
    "
    (aKey isNil and:[adornment isNil]) ifFalse:[
        self adornment shortcutKeyCharacter:aKey
    ]
!

submenu
    adornment notNil ifTrue:[
        ^ adornment submenu
    ].
    ^ nil

    "Created: 25.2.1997 / 20:57:24 / cg"
!

submenu:aMenu
    self adornment submenu:aMenu

    "Created: 25.2.1997 / 20:56:20 / cg"
!

value
    ^ value

    "Created: 25.2.1997 / 19:50:14 / cg"
!

value:something
    value := something

    "Created: 25.2.1997 / 19:11:13 / cg"
! !

!MenuItem methodsFor:'accessing behavior'!

disable
    enabled := false

    "Created: 25.2.1997 / 19:39:09 / cg"
!

enable
    enabled := true

    "Created: 25.2.1997 / 19:39:00 / cg"
!

enabled
    "returns a boolean, valueHolder or block
    "
    ^ enabled
!

enabled:something
    "a boolean, valueHolder or block
    "
    enabled := something
! !

!MenuItem methodsFor:'accessing resource'!

findGuiResourcesIn:aResourceContainer
    "setup a resource container
    "
    |lblAndIcon|

    (lblAndIcon := self labelAndIcon) notNil ifTrue:[
        lblAndIcon findGuiResourcesIn:aResourceContainer
    ]
! !

!MenuItem methodsFor:'converting'!

fromLiteralArrayEncoding:aLiteralEncodedArray
    "read my contents from a aLiteralEncodedArray"

    2 to:aLiteralEncodedArray size by:2 do:[:i |
        |selector value|

        selector := aLiteralEncodedArray at:i.
        value    := (aLiteralEncodedArray at:i+1) decodeAsLiteralArray.
        self perform:selector with:value
    ].

    "
     #( #MenuItem #rawLabel: 'left' #nameKey: 'identifier'  #value: #left )
         decodeAsLiteralArray
    "

    "Modified: 25.2.1997 / 19:24:22 / cg"
!

literalArrayEncoding
    "return myself encoded as a literal array"

    |coll something|

    coll := OrderedCollection new.
    coll add:#MenuItem.

    label notNil ifTrue:[
        coll add:#label: ; add:(label literalArrayEncoding)
    ].

    nameKey notNil ifTrue:[
        coll add:#nameKey: ; add:(nameKey literalArrayEncoding)
    ].

    (something := self enabled) ifFalse:[
        coll add:#enabled: ; add:(something literalArrayEncoding).
    ].

    value notNil ifTrue:[
        coll add:#value: ; add:(value literalArrayEncoding).
    ].

    (something := self shortcutKeyCharacter) notNil ifTrue:[
        coll add:#shortcutKeyCharacter: ; add:(something literalArrayEncoding)
    ].

    (something := self accessCharacterPosition) notNil ifTrue:[
        coll add:#accessCharacterPosition: ; add:(something literalArrayEncoding)
    ].

    (something := self labelImage) notNil ifTrue:[
        coll add:#labelImage: ; add:(something literalArrayEncoding)
    ].
  ^ coll asArray
! !

!MenuItem methodsFor:'private - accessing'!

adornment
    adornment isNil ifTrue:[
        adornment := MenuItemAdornment new
    ].
    ^ adornment

    "Created: 25.2.1997 / 20:57:05 / cg"
! !

!MenuItem methodsFor:'queries'!

hasSubmenu
    ^ self subMenu notNil

    "Created: 25.2.1997 / 20:56:20 / cg"
!

isEnabled
    ^ enabled value ? true

    "Created: 25.2.1997 / 19:39:17 / cg"
! !

!MenuItem::MenuItemAdornment methodsFor:'accessing'!

accessCharacterPosition
    "return the value of the instance variable 'accessCharacterPosition' (automatically generated)"

    ^ accessCharacterPosition!

accessCharacterPosition:something
    "set the value of the instance variable 'accessCharacterPosition' (automatically generated)"

    accessCharacterPosition := something.!

color
    "return the value of the instance variable 'color' (automatically generated)"

    ^ color

    "Created: 25.2.1997 / 20:59:28 / cg"
!

color:something
    "set the value of the instance variable 'color' (automatically generated)"

    color := something.

    "Created: 25.2.1997 / 20:59:28 / cg"
!

indication
    "return the value of the instance variable 'indication' (automatically generated)"

    ^ indication

    "Created: 25.2.1997 / 20:59:28 / cg"
!

indication:something
    "set the value of the instance variable 'indication' (automatically generated)"

    indication := something.

    "Created: 25.2.1997 / 20:59:28 / cg"
!

labelImage
    "return the value of the instance variable 'labelImage' (automatically generated)"

    ^ labelImage!

labelImage:something
    "set the value of the instance variable 'labelImage' (automatically generated)"

    labelImage := something.!

labelText
    "get the text appear as the menu label
    "
    ^ labelText
!

labelText:something
    "set the text appear as the menu label
    "
    labelText := something.
!

shortcutKeyCharacter
    "get the  key to press to select the menu item from the keyboard
    "
    ^ shortcutKey
!

shortcutKeyCharacter:something
    "set the  key to press to select the menu item from the keyboard
    "
    shortcutKey := something.
!

submenu
    "get the submenu or nil
    "
    ^ submenu

    "Created: 25.2.1997 / 20:59:28 / cg"
!

submenu:something
    "set the submenu or nil
    "
    submenu := something.

    "Created: 25.2.1997 / 20:59:28 / cg"
! !

!MenuItem class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/MenuItem.st,v 1.9 1997-05-23 14:04:09 ca Exp $'
! !