[comp.lang.smalltalk] GOODIE - CollectionMenu.st

pieter@prls.UUCP (Pieter van der Meulen) (07/11/90)

Here is another goodie I had still lying around. It is possible
that there are several similar implementations already around,
so this one is for anyone that hasn't got something like it.
Please post your better/other/superior goodies !

Have fun, Pieter.
 

Try : (FileDirectory filesMatching: '*') menuWithAbort
	startUp: #anyButton
	withHeading: (Text string: ' Select a file/dir : ' emphasis: 2)

'-----------------------------------cut here----------------------------------'!

'These sources allow you to select an object from a collection
via an HierarchicalMenu. A method for limiting the vertical size
of this menu is included, and may be also useful in other situations.

	Copyright (C) 1990, Pieter S. van der Meulen.
	This program is placed in the public domain.
	You may use and alter this program freely
	for non-commercial purposes as long as you
	leave this message intact.  Neither I nor
	my company will recognize any responsibility
	for damages arising from use of this program.'!

!Object methodsFor: 'printing'!
asString
	"Now all objects repond to asString."

	^self printString! !


!Character methodsFor: 'printing'!
asString
	"Return a String with one character (e.g. 'c' in stead of '$c')."

	^String with: self! !


!HierarchicalMenu methodsFor: 'accessing'!
maxHeight: anInteger
	"Limit the vertical size of the menu to anInteger number of pixels."

	frame height: ((anInteger // font height * font height + 4) min: frame height)! !


!Collection methodsFor: 'accessing'!
menuWithAbort
	"Generate a menu with descriptions for the elements I am holding.
	If I am strarted up, return nil if I am empty, ABORT (or nothing) is
	selected. Otherwise, return the selected entry (not the index).
	Written by Pieter van der Meulen."

	"(FileDirectory filesMatching: '*') menuWithAbort
		startUp: #anyButton
		withHeading: (Text string: ' Select a file/dir : ' emphasis: 2)"
	"(Smalltalk select: [:c | c isKindOf: Behavior]) menuWithAbort
		startUp: #anyButton
		withHeading: (Text string: ' Select a class : ' emphasis: 2)"
	"'Try this scrolling example too' menuWithAbort
		startUp: #anyButton
		withHeading: (Text string: ' Select a character : ' emphasis: 2)"

	| menuString nItems menuCollect menu |
	(nItems _ self size) = 0 ifTrue: [^nil].
	menuCollect _ OrderedCollection new: nItems + 1.
	self do: [:entry | menuCollect add: entry].
	menuString _ ''.
	menuCollect do: [:entry |
		menuString _ menuString ,entry asString, (String with: Character cr)].
	menuCollect add: nil.
	menuString _ menuString , 'ABORT'.
	menu _ HierarchicalMenu labels: menuString lines: (Array with: nItems).
	menu prefix: 'prefix not used' items: menuCollect.
	menu maxHeight: 200.
	^menu! !
-- 
---------------------------------------------
P.S. van der Meulen, MS 02        prls!pieter
PRLS, Signetics div. of NAPC      -----------
811 E.Arques Avenue, Sunnyvale, CA 94088-3409