Changing the pop-up menu
One useful thing you can do with this edit part is add your own items to the part's pop-up menu. As a convenient alternative to the settings view, let's add a menu choice to the pop-up menu to change the shape attribute. The two methods below add a cascaded menu, with graphical choices for each shape. In MyShapeEditPart, add the following instance method:
addOwnItemsToPopUpMenu: aPopUpMenu
"Add the items for the receiver to the popup menu. This popup
menu is called during the creation of the popup for an object
if the object is the only selection."
| cascadeButton cascadeMenu labelString mnemonicControlChar
hasPages numPages |
mnemonicControlChar := AbtEditStrings::AbtCharMnemonic.
hasPages := ((numPages := self parentPart components size) > 0).
cascadeMenu := aPopUpMenu
createPulldownMenu: 'ChangeShape'
argBlock: nil.
labelString := 'Change ~Shape'.
cascadeButton :=
(aPopUpMenu
createCascadeButton: (labelString
abtWithoutMnemonic: mnemonicControlChar)
argBlock: [:w |
w mnemonic: (labelString
abtMnemonic: mnemonicControlChar
ifAbsent: [$S]);
subMenuId: cascadeMenu]) manageChild.
#('circle' 'rectangle' 'triangle') do: [:each |
labelString := each.
(cascadeMenu
createPushButton: labelString
argBlock: [:w | w labelType: XmPIXMAP.
w labelPixmap: (self pixmap: each)])
addCallback: XmNactivateCallback
receiver: self
selector: #changeShape:clientData:callData:
clientData: each;
manageChild].
In MyShape, add the following instance method:
abtAddOwnItemsToPopUpMenu: aPopUpMenu for: anEditPart
"Add a popup menu."
super abtAddOwnItemsToPopUpMenu: aPopUpMenu for: anEditPart.
anEditPart addOwnItemsToPopUpMenu: aPopUpMenu
The method addOwnItemsToPopUpMenu: uses two other methods: changeShape:clientData:callData: and pixmap:. Both of these methods go in MyShapeEditPart. The first method changes the part's shape attribute by using an update operation.
Here is the changeShape:clientData:callData: method:
changeShape: aWidget clientData: clientData callData: callData
"Change the shape attribute from the pop-up menu."
| operation |
operation := self partBuilder customSettingsOperation
stackHolder: self stackHolder;
defaultValuesSource: self part;
target: self partBuilder.
operation target attributeSettingNamed: #shape put: clientData.
operation execute.
The first four statements are the messages that are sent when the settings is opened for a part; the only difference is that the settings view is not displayed. The next line sets the attribute value to the new shape value, and the last line applies the changes.
Here is the pixmap: method, which returns a bitmap to display on the cascaded menu:
pixmap: aString
"Answer the icon for the pop-up menu."
| descriptor id |
id := 9904. "default to a circle icon"
aString = 'rectangle' ifTrue: [id := 9905].
aString = 'triangle' ifTrue: id := 9906].
descriptor := (AbtIconDescriptor new
moduleName: 'abtico50';
id: id).
^descriptor pixmap
In another application, test your work by adding a new shape part to a Window part, displaying the pop-up menu for the part, and selecting Change Shape. The pop-up menu should look like the following:
Pop-up menu
Last modified date: 06/11/2018