Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
How to implement a List Morph
Last updated at 3:28 pm UTC on 14 August 2014

Problem

You want to present a list of items in Morphic. It uses PluggableListMorph.

Solution

Below is a simple class that can be filed-in to create a list in Morphic. To learn about "filing in" a Changeset, see Goodies and Changesets.

It has been validated to work in Squeak 4.5 #13663.


'From Squeak2.7 of 5 January 2000 [latest update: #1762] on 9 January 2000 at 11:55:01 am'!
Object subclass: #AnExampleOfLists
instanceVariableNames: 'list selectedIndex '
classVariableNames: ''
poolDictionaries: ''
category: 'Morphic-Windows'!

!AnExampleOfLists commentStamp: 'RAA 1/9/2000 11:54' prior: 0!
I am a very simple example of how to use the PluggableListMorph. To see me in action, evaluate:

AnExampleOfLists new initialize openAViewOnMe!

!AnExampleOfLists methodsFor: 'as yet unclassified' stamp: 'RAA 1/9/2000 11:33'!
initialize

list _ (Collection withAllSubclasses collect: [ :each | each name])
asSortedCollection: [ :a :b | a = b].
selectedIndex _ 1.! !

!AnExampleOfLists methodsFor: 'as yet unclassified' stamp: 'RAA 1/9/2000 11:26'!
list

^list! !

!AnExampleOfLists methodsFor: 'as yet unclassified' stamp: 'RAA 1/9/2000 11:30'!
listIndex

^selectedIndex! !

!AnExampleOfLists methodsFor: 'as yet unclassified' stamp: 'RAA 1/9/2000 11:27'!
listIndex: anInteger

selectedIndex _ anInteger.
self changed: #listIndex. "signal my acceptance of the change"! !

!AnExampleOfLists methodsFor: 'as yet unclassified' stamp: 'RAA 1/9/2000 11:51'!
listMenu: aMenu

| targetClass differentMenu className |

className _ list
at: selectedIndex
ifAbsent: [
^aMenu add: 'nothing selected' target: self selector: #beep
].
targetClass _ Smalltalk
at: className
ifAbsent: [
^aMenu add: 'that class is history!!' target: self selector: #beep
].
differentMenu _ DumberMenuMorph new. "avoid the retargeting business"
differentMenu
add: 'browse' target: targetClass selector: #browse;
add: 'inspect' target: targetClass selector: #inspect;
add: 'explore' target: targetClass selector: #explore.
^ differentMenu
! !

!AnExampleOfLists methodsFor: 'as yet unclassified' stamp: 'RAA 1/9/2000 11:32'!
openAViewOnMe

| window aListMorph |

aListMorph _ PluggableListMorph
on: self "I am the model that has a list to display"
list: #list "This is how the morph gets the list from me"
selected: #listIndex "This is how the morph knows which item to highlight"
changeSelected: #listIndex: "This is how the morph informs me of a user selection"
menu: #listMenu:. "This is how the morph requests a menu for the list"
aListMorph color: Color white.

window _ SystemWindow labelled: 'An example of a list'.
window
color: Color blue;
addMorph: aListMorph
frame: (0@0 corner: 1@1).
^ window openInWorldExtent: 380@220! !