From VisualWorks®, 7.6 of February 1, 2008 on February 1, 2008 at 12:49:14 pm (Dialog confirm: 'You are filing-in a Parcel source file!\\While this is possible it will not have\the same effect as loading the parcel.\None of the Parcel''s prerequisites will\be loaded and none of its load actions\will be performed.\\Are you sure you want to file-in?' withCRs) ifFalse: [self error: 'Parcel file-in abandoned. Choose terminate or close.'] <class> <name>MenuCommand2Example</name> <environment>Examples</environment> <super>UI.ApplicationModel</super> <private>false</private> none files <imports></imports> <category>Examples-Help</category> <attributes> <package>MenuCommand2-Example</package> </attributes> </class> <comment> Examples.MenuCommand2Example
This example class demonstrates how to implement a menu of commands (as opposed to a menu of values) in each of the three types of menu: menu bar, menu button and pop-up menu. In this variation, the menus are installed from a menu resource as opposed to being created programmatically. The old programmatic menu method of MenuCommandExample renamed from #fileMenu to #oldFileMenu has had the pragma <resource: #menu> added. With this pragma it served as source for the menu resource opened in and installed from the MenuEditor.
</comment> <methods> Examples.MenuCommand2Example <category>menu messages</category>
addFile "Prompt for a filename and add it to the list." | name list selectionInList | name := Dialog requestFileName: 'File to add' default: '' version: #mustBeOld. name isEmpty ifFalse: [name asFilename isDirectory ifTrue: [^Dialog warn: 'A directory cannot be added.' for: ScheduledControllers activeController view]. selectionInList := self files. list := selectionInList list. selectionInList selection isNil ifTrue: [list add: name] ifFalse: [list add: name before: selectionInList selection]. selectionInList list: list]
deleteFile "Remove the selected file from the list." | selectionInList list | selectionInList := self files. list := selectionInList list. list removeAtIndex: selectionInList selectionIndex. selectionInList list: list.
explainUsage "Tell how to use the example." Dialog warn: ' This example demonstrates a menu of commands that is accessible via a menu bar, a menu button or a pop-up menu. It can also be used to list your most-used text files for quick editing. '
openFile "Open a File Editor on the selected file." | selectedFile | selectedFile := self files selection. selectedFile asFilename edit.
</methods> <methods> Examples.MenuCommand2Example <category>testing</category>
anyFileSelected ^self files selection notNil
</methods> <methods> Examples.MenuCommand2Example <category>aspects</category>
files ^files
performer ^self
</methods> <methods> Examples.MenuCommand2Example <category>initialize-release</category>
initialize files := SelectionInList new.
</methods> <methods> Examples.MenuCommand2Example class <category>resources</category>
fileMenu "MenuEditor new openOnClass: self andSelector: #fileMenu" <resource: #menu> ^#(#{UI.Menu} #( #(#{UI.MenuItem} #rawLabel: '&File' #submenu: #(#{UI.Menu} #( #(#{UI.MenuItem} #rawLabel: 'Open' #value: #openFile #enablementSelector: #anyFileSelected #shortcutKeyCharacter: $O #shortcutModifiers: 8 ) #(#{UI.MenuItem} #rawLabel: 'Add' #value: #addFile #shortcutKeyCharacter: $A #shortcutModifiers: 8 ) #(#{UI.MenuItem} #rawLabel: 'Delete' #value: #deleteFile #enablementSelector: #anyFileSelected #shortcutKeyCharacter: $D #shortcutModifiers: 8 ) ) #(1 2 ) nil ) ) #(#{UI.MenuItem} #rawLabel: '&Help' #submenu: #(#{UI.Menu} #( #(#{UI.MenuItem} #rawLabel: '&Usage' #value: #explainUsage ) ) #(1 ) nil ) ) ) #(2 ) nil ) decodeAsLiteralArray
</methods> <methods> Examples.MenuCommand2Example class <category>old menus</category>
oldFileMenu "MenuEditor new openOnClass: self andSelector: #oldFileMenu" <resource: #menu> | mb menu submenu | mb := MenuBuilder new. mb beginSubMenuLabeled: 'File'; add: 'Open' -> #openFile; line; add: 'Add' -> #addFile; add: 'Delete' -> #deleteFile; endSubMenu. mb beginSubMenuLabeled: 'Help'; add: 'Usage' -> #explainUsage; endSubMenu. "Add shortcut keys." menu := mb menu. submenu := (menu menuItemLabeled: 'File') submenu. (submenu menuItemLabeled: 'Open') shortcutKeyCharacter: $O; enablement: #anyFileSelected. (submenu menuItemLabeled: 'Add') shortcutKeyCharacter: $A. (submenu menuItemLabeled: 'Delete') shortcutKeyCharacter: $D; enablement: #anyFileSelected. ^menu
</methods> <methods> Examples.MenuCommand2Example class <category>interface specs</category>
windowSpec "UIPainter new openOnClass: self andSelector: #windowSpec" <resource: #canvas> ^#(#{UI.FullSpec} #window: #(#{UI.WindowSpec} #label: 'MenuCommand2Example' #min: #(#{Core.Point} 233 200 ) #bounds: #(#{Graphics.Rectangle} 800 600 1033 800 ) #flags: 4 #menu: #fileMenu ) #component: #(#{UI.SpecCollection} #collection: #( #(#{UI.SequenceViewSpec} #layout: #(#{Graphics.LayoutFrame} 10 0 35 0 -10 1 -10 1 ) #model: #files #menu: #fileMenu ) #(#{UI.MenuButtonSpec} #layout: #(#{Graphics.LayoutFrame} -50 0.5 5 0 50 0.5 30 0 ) #model: #performer #label: 'Actions' #menu: #fileMenu ) ) ) )
</methods> "Imported Classes:" self error: 'Attempting to file-in parcel imports. Choose terminate or close' <class> <name>ApplicationModel</name> <environment>UI</environment> <super>UI.Model</super> <private>false</private> none builder uiSession eventHandlers savedWindowInformation <imports></imports> <category>UIBuilder-Framework</category> <attributes> <package>UIBuilder-Framework</package> </attributes> </class>