Menu.st
author ca
Mon, 03 Mar 1997 11:14:14 +0100
changeset 473 9ef922391c70
parent 457 19f2c28e4adb
child 489 264ba86c52c1
permissions -rw-r--r--
nil aspect

Object subclass:#Menu
	instanceVariableNames:'items groupSizes'
	classVariableNames:''
	poolDictionaries:''
	category:'Views-Support'
!

!Menu class methodsFor:'documentation'!

documentation
"
    not yet finished Menu 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
"
! !

!Menu methodsFor:'accessing'!

labels
    "return a collection of labels from my items"

    ^ items collect:[:anItem | anItem label]

    "Created: 25.2.1997 / 19:47:53 / cg"
!

menuItems:aCollectionOfMenuItems groupsSizes:sizes values:values
    |idx newItems nItems|

    items := aCollectionOfMenuItems.
    values notNil ifTrue:[
        items with:values do:[:anItem :aValue |
            anItem value:aValue 
        ]
    ].

    "/ add empty items for group-separating lines.

    groupSizes := sizes.
    groupSizes notNil ifTrue:[
        idx := 1.
        nItems := items size.
        newItems := OrderedCollection new:(nItems + groupSizes size).
        groupSizes do:[:aGroupSize |
            items from:idx to:idx+aGroupSize-1 do:[:l | newItems add:l].
            idx := idx + aGroupSize.
            idx <= nItems ifTrue:[
                newItems add:(MenuItem new label:'-').
            ]
        ].
        items from:idx to:nItems do:[:l | newItems add:l].
        items := newItems.
    ].

    "Created: 25.2.1997 / 20:28:12 / cg"
    "Modified: 25.2.1997 / 21:03:33 / cg"
!

valueAt:index
    "return a collection of values from my items"

    ^ (items at:index) value

    "Created: 25.2.1997 / 19:49:41 / cg"
!

values
    "return a collection of values from my items"

    ^ items collect:[:anItem | anItem value]

    "Created: 25.2.1997 / 19:49:29 / cg"
! !

!Menu methodsFor:'converting'!

fromLiteralArrayEncoding:aLiteralEncodedArray
    "read my contents from a aLiteralEncodedArray"

    |items groups values|

    items := (aLiteralEncodedArray at:2) collect:[:item | item decodeAsLiteralArray].
    groups := aLiteralEncodedArray at:3.
    values := aLiteralEncodedArray at:4.
    self menuItems:items groupsSizes:groups values:values.

    "extract from PD folder.st:
     #(#Menu #(
                #(#MenuItem 
                        #rawLabel: 'left' 
                        #value: #left ) 
                #(#MenuItem 
                        #rawLabel: 'center' 
                        #value: #center ) 
                #(#MenuItem 
                        #rawLabel: 'right' 
                        #value: #right ) 
              ) 
             #(3 ) 
             nil 
       ) decodeAsLiteralArray
    "
    "
     #(#Menu #(
                #(#MenuItem 
                        #label: 'Straighten Up' ) 
                #(#MenuItem 
                        #label: 'Inspect' ) 
                #(#MenuItem 
                        #label: 'Coredump' ) 
              ) 
             #(3 ) 
            #(#straightenUp #inspect #halt ) 
       ) decodeAsLiteralArray startUp  
    "

    "extract from iconicBrowser.st:
     #(#Menu #(
                #(#MenuItem 
                        #label: 'Straighten Up' ) 
                #(#MenuItem 
                        #label: 'Inspect' ) 
                #(#MenuItem 
                        #label: 'Coredump' ) 
              ) 
             #(3 ) 
             #(1 2 3 )
       ) decodeAsLiteralArray startUp  
    "

    "extract from refactory213.st:
     #(#Menu #(
                #(#MenuItem 
                    #label: 'File List' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'File Editor...' 
                    #accessCharacterPosition: 6 ) 
                #(#MenuItem #label: 'Refactoring Tool...' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'Workspace' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'New Canvas' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'Palette' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'Canvas Tool' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'Image Editor' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'Menu Editor' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'Advanced' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'DLL and C Connect' 
                    #accessCharacterPosition: 1 ) 
                #(#MenuItem #label: 'System Transcript' 
                    #accessCharacterPosition: 8 ) 
              ) 
              #(4 5 2 1 ) 
              #(#openFileList #openFileEditor #openRefactoringTool #toolsNewWorkspace #toolsNewCanvas #toolsPalette #toolsCanvasTool #toolsMaskEditor #toolsMenuEditor nil #openExternalFinder #toggleSystemTranscript ) 
        ) decodeAsLiteralArray startUp
    "

    "submenus:
     #(#Menu #(
                #(#MenuItem 
                        #label: 'Foo' 
                        #submenu: #(#Menu #(
                                            #(#MenuItem #label: 'foo 1')     
                                            #(#MenuItem #label: 'foo 2')     
                                          )
                                          nil
                                          #(11 22)
                                   )     
                 ) 
                #(#MenuItem 
                        #label: 'Inspect' ) 
                #(#MenuItem 
                        #label: 'Coredump' ) 
              ) 
             #(3 ) 
             #(1 2 3 )
       ) decodeAsLiteralArray startUp  
    "

    "Modified: 25.2.1997 / 21:05:06 / cg"
! !

!Menu methodsFor:'startup'!

createPopUpMenu
    "create a popUpMenu for the receiver"

    |menuView val index labels |

    "/ for now, use PopUpMenu ....
    "/ ... eventually, MenuView will handle me directly

    labels := self labels.

    menuView := PopUpMenu labels:labels.

    "/ manually convert subMenus ...

    items keysAndValuesDo:[:idx :anItem |
        |sub m|

        (sub := anItem submenu) notNil ifTrue:[
            m := sub createPopUpMenu.
            menuView subMenuAt:idx put:m
        ]
    ].

    ^ menuView

    "Created: 25.2.1997 / 21:02:21 / cg"
!

startUp
    "display the menu as a popUp; return the value associated with the
     selected item, 0 if none was selected"

    |menuView val index |

    "/ for now, use PopUpMenu ....
    "/ ... eventually, MenuView will handle me directly

    menuView := self createPopUpMenu.
    index := menuView startUp. 
    menuView destroy.

    (index notNil and:[index ~~ 0]) ifTrue:[
        val := self valueAt:index.
        val notNil ifTrue:[
            ^ val
        ]
    ].
    ^ 0

    "   
        |m|
        m := #(#Menu #(
                        #(#MenuItem 
                                #rawLabel: 'left' 
                                #value: #left ) 
                        #(#MenuItem 
                                #rawLabel: 'center' 
                                #value: #center ) 
                        #(#MenuItem 
                                #rawLabel: 'right' 
                                #value: #right ) ) 
                 #(3) 
                nil 
        ) decodeAsLiteralArray.
        m startUp        
    "

    "Modified: 26.2.1997 / 10:41:37 / cg"
! !

!Menu class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libview2/Menu.st,v 1.5 1997-02-26 09:42:03 cg Exp $'
! !