Platforms to show: All Mac Windows Linux Cross-Platform

/MacControls/Toolbar/Toolbar buttons/Toolbar buttons


Required plugins for this example: MBS MacBase Plugin, MBS MacControls Plugin, MBS MacCocoa Plugin, MBS Main Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacControls/Toolbar/Toolbar buttons/Toolbar buttons

This example is the version from Tue, 3rd Aug 2015.

Project "Toolbar buttons.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
Control Label1 Inherits Label
ControlInstance Label1 Inherits Label
End Control
Control Label2 Inherits Label
ControlInstance Label2 Inherits Label
End Control
EventHandler Sub Open() MainToolbar=New NSToolbarMain dim w as NSWindowMBS = self.NSWindowMBS w.toolbar = MainToolbar MainToolbar.visible = true End EventHandler
Property MainToolbar As NSToolbarMain
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Module Module1
Const MainToolbarNSButtonHeight = 23
Const MainToolbarNSSearchFieldHeight = 23
Const MainToolbarNSSegmentHeight = 24
Delegate Sub MainToolbarFlagMenuAction(SelectedTag As Integer)
Delegate Sub MainToolbarSearchFieldMenuAction(SelectedTag As Integer)
End Module
Class NSToolbarMain Inherits CustomNSToolbarMBS
EventHandler Function itemForItemIdentifier(identifier as string, willBeInsertedIntoToolbar as boolean) As NSToolbarItemMBS Select case identifier case "admin" // items with NSSegmentedControls must be recreated here. // Workaround for a Cocoa bug. Only needed if allowsUserCustomization=True. dim BtAdmin as New NSToolbarAdminSegment // toolbar item dim BtAdminItem as New NSToolbarItemMBS("admin") BtAdminItem.label="Administration" BtAdminItem.paletteLabel="Administration" BtAdminItem.view=BtAdmin myItems.Append BtAdminItem myItems.Append BtAdmin Return BtAdminItem case "display" dim BtDisplay as New NSToolbarDisplaySegment dim BtDisplayItem as New NSToolbarItemMBS("display") BtDisplayItem.label="Display options" BtDisplayItem.paletteLabel="Display options" BtDisplayItem.view=BtDisplay myItems.Append BtDisplayItem myItems.Append BtDisplay Return BtDisplayItem case "options" dim BtOption as New NSToolbarOptionPopup // toolbar item dim BtOptionItem as New NSToolbarItemMBS("options") BtOptionItem.label="Options" BtOptionItem.paletteLabel="Options" BtOptionItem.view = BtOption myItems.Append BtOptionItem myItems.Append BtOption Return BtOptionItem case "searchfield" dim SearchField as New NSToolbarSearchField // toolbar item dim SearchFieldItem as New NSToolbarItemMBS("searchfield") SearchFieldItem.label="Search title" SearchFieldItem.paletteLabel="Search title" SearchFieldItem.minSize=New NSSizeMBS( 140, MainToolbarNSSearchFieldHeight ) SearchFieldItem.maxSize=New NSSizeMBS( 300, MainToolbarNSSearchFieldHeight ) SearchFieldItem.view = SearchField myItems.Append SearchField myItems.Append SearchFieldItem Return SearchFieldItem case "editor" dim BtEditor as New NSToolbarEditorSegment dim BtEditorItem as New NSToolbarItemMBS("editor") BtEditorItem.label="Editor" BtEditorItem.paletteLabel="Editor" BtEditorItem.view=BtEditor myItems.Append BtEditorItem myItems.Append BtEditor self.BtEditor = BtEditor Return BtEditorItem case "flags" dim BtFlags as New NSToolbarFlagPopupSegment dim BtFlagsItem as New NSToolbarItemMBS("flags") BtFlagsItem.label="Flags" BtFlagsItem.paletteLabel="Flags" BtFlagsItem.view=BtFlags myItems.Append BtFlagsItem myItems.Append BtFlags Return BtFlagsItem case "play" dim BtPlay as New NSToolbarPlayButton // toolbar item dim BtPlayItem as New NSToolbarItemMBS("play") BtPlayItem.label="Play" BtPlayItem.paletteLabel="Play" BtPlayItem.view = BtPlay myItems.Append BtPlayItem myItems.Append BtPlay Return BtPlayItem end Select End EventHandler
EventHandler Function toolbarAllowedItemIdentifiers() As string() Return AllowEdit End EventHandler
EventHandler Function toolbarDefaultItemIdentifiers() As string() Return ItemIdentifiers End EventHandler
Sub BtEditorAddEnabled(Assigns value As Boolean) // A example of method that can make buttons more accessibles. // Use like this: Window1.MainToolbar.BtEditorAddEnabled=True BtEditor.isEnabledForSegment( 0 )=value End Sub
Sub Constructor() Super.Constructor("my_application_mainToolbar") Self.allowsUserCustomization = true Self.displayMode = NSToolbarMBS.NSToolbarDisplayModeIconOnly Self.sizeMode = NSToolbarMBS.NSToolbarSizeModeRegular //———————————————— // Segment button : administration //———————————————— AllowEdit.Append "admin" ItemIdentifiers.Append "admin" //——————— // Flexible space //——————— ItemIdentifiers.Append NSToolbaritemMBS.NSToolbarFlexibleSpaceItemIdentifier AllowEdit.Append NSToolbaritemMBS.NSToolbarFlexibleSpaceItemIdentifier //———————————————— // Segment button : display options //———————————————— AllowEdit.Append "display" ItemIdentifiers.Append "display" //—————————————— // Popup button : option popup //—————————————— AllowEdit.Append "options" ItemIdentifiers.Append "options" //—————— // SearchField //—————— AllowEdit.Append "searchfield" ItemIdentifiers.Append "searchfield" //——————— // Flexible space //——————— ItemIdentifiers.Append NSToolbaritemMBS.NSToolbarFlexibleSpaceItemIdentifier AllowEdit.Append NSToolbaritemMBS.NSToolbarFlexibleSpaceItemIdentifier //——————————————— // Segment button : Edit and Add //——————————————— AllowEdit.Append "editor" ItemIdentifiers.Append "editor" //——————————————— // Segment button : Flag popup //——————————————— AllowEdit.Append "flags" ItemIdentifiers.Append "flags" //—————————— // Single button : Play //—————————— AllowEdit.Append "play" ItemIdentifiers.Append "play" End Sub
Note "Credits"
David Della Rocca
Property Protected AllowEdit() As String
Property BtEditor As NSSegmentedControlMBS
Property Protected ItemIdentifiers() As String
Property myItems() As Variant
This array stores all items and views we use to keep them alive
End Class
Class NSToolbarOptionPopup Inherits NSPopupButtonMBS
EventHandler Sub Action() dim n As Integer=indexOfSelectedItem Window1.Label1.Text="Menu selected="+Str(n) End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor( 0,0,40,MainToolbarNSButtonHeight ) bezelStyle=NSButtonMBS.NSTexturedRoundedBezelStyle pullsDown=true addItemsWithTitles( Array( " ","menu1","menu2","menu3","menu4" ) ) // first item has no text " " dim m As NSMenuItemMBS=itemAtIndex(0) // we add an image to the first item m.Image=New NSImageMBS( ActionGearTemplate ) m.Image.setSize( 14,14 ) m.Image.IsTemplate=true End Sub
End Class
Class NSToolbarPlayButton Inherits NSButtonMBS
EventHandler Sub Action() Window1.Label1.Text="Play button" End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor( 0,0,52,MainToolbarNSButtonHeight ) bezelStyle=NSButtonMBS.NSTexturedRoundedBezelStyle Image=New NSImageMBS( MediaPlayButtonTemplate ) Image.setSize( 15,15 ) Image.isTemplate=true End Sub
End Class
Class NSToolbarAdminSegment Inherits NSSegmentedControlMBS
EventHandler Sub Action() Select case selectedSegment case 0 Window1.Label1.Text="Admin button=0" case 1 Window1.Label1.Text="Admin button=1" case 2 Window1.Label1.Text="Admin button=2" End Select End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor( 0,0,104,MainToolbarNSSegmentHeight ) // size is calculated like this: 3px + segmentWidth + 1px + segmentWidth + 3px segmentCount=3 segmentStyle=NSSegmentedControlMBS.NSSegmentStyleRoundRect trackingMode=NSSegmentedControlMBS.NSSegmentSwitchTrackingMomentary // Segments dim p As NSImageMBS p=New NSImageMBS( PreferencesButtonTemplate ) p.setSize( 18,18 ) p.isTemplate=true imageForSegment(0)=p tagForSegment(0)=0 widthForSegment(0)=32 p=New NSImageMBS( UserButtonTemplate ) p.setSize( 20,18 ) p.isTemplate=true imageForSegment(1)=p tagForSegment(1)=1 widthForSegment(1)=32 p=New NSImageMBS( SystemNewFolderTemplate ) p.setSize( 20,18 ) p.isTemplate=true imageForSegment(2)=p tagForSegment(2)=2 widthForSegment(2)=32 End Sub
End Class
Class NSToolbarDisplaySegment Inherits NSSegmentedControlMBS
EventHandler Sub Action() Select case selectedSegment case 0 Window1.Label1.Text="Display button=0" case 1 Window1.Label1.Text="Display button=1" case 2 Window1.Label1.Text="Display button=2" case 3 Window1.Label1.Text="Display button=3" End Select End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor( 0,0,137,MainToolbarNSSegmentHeight ) // size is calculated like this: 3px + segmentWidth + 1px + segmentWidth + 3px segmentCount=4 segmentStyle=NSSegmentedControlMBS.NSSegmentStyleRoundRect trackingMode=NSSegmentedControlMBS.NSSegmentSwitchTrackingSelectOne selectedSegment=0 // Segments dim p As NSImageMBS p=New NSImageMBS( PresentationTwoListesTemplate ) p.setSize( 20,18 ) p.isTemplate=true imageForSegment(0)=p tagForSegment(0)=0 widthForSegment(0)=32 p=New NSImageMBS( PresentationListAndPreviewTemplate ) p.setSize( 20,18 ) p.isTemplate=true imageForSegment(1)=p tagForSegment(1)=1 widthForSegment(1)=32 p=New NSImageMBS( PresentationGridViewTemplate ) p.setSize( 20,18 ) p.isTemplate=true imageForSegment(2)=p tagForSegment(2)=2 widthForSegment(2)=32 p=New NSImageMBS( PresentationSettingsTemplate ) p.setSize( 20,18 ) p.isTemplate=true imageForSegment(3)=p tagForSegment(3)=3 widthForSegment(3)=32 End Sub
End Class
Class NSToolbarEditorSegment Inherits NSSegmentedControlMBS
EventHandler Sub Action() Select case selectedSegment case 0 Window1.Label1.Text="Editor button=0" case 1 Window1.Label1.Text="Editor button=1" End Select End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor( 0,0,71,MainToolbarNSSegmentHeight ) // size is calculated like this: 3px + segmentWidth + 1px + segmentWidth + 3px segmentCount=2 segmentStyle=NSSegmentedControlMBS.NSSegmentStyleRoundRect trackingMode=NSSegmentedControlMBS.NSSegmentSwitchTrackingMomentary // Segments dim p As NSImageMBS p=New NSImageMBS( AddButtonTemplate ) p.setSize( 14,14 ) p.IsTemplate=true imageForSegment(0)=p tagForSegment(0)=0 widthForSegment(0)=32 p=New NSImageMBS( EditButtonSmallTemplate ) p.setSize( 20,18 ) p.IsTemplate=true imageForSegment(1)=p tagForSegment(1)=1 widthForSegment(1)=32 End Sub
End Class
Class NSToolbarFlagPopupSegment Inherits NSSegmentedControlMBS
EventHandler Sub Action() if selectedSegment=0 then Window1.Label1.Text="Flag button=0" end if End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor( 0,0,51,MainToolbarNSSegmentHeight ) // size is calculated like this: // 3px + segmentWidth + 1px + segmentWidth + 3px segmentCount=2 segmentStyle=NSSegmentedControlMBS.NSSegmentStyleRoundRect trackingMode=NSSegmentedControlMBS.NSSegmentSwitchTrackingMomentary // Segments imageForSegment(0)=New NSImageMBS( Flags01 ) tagForSegment(0)=0 widthForSegment(0)=28 dim p As New NSImageMBS( MenuGlyphTemplate ) p.setSize( 9,9 ) p.isTemplate=true imageForSegment(1)=p tagForSegment(1)=1 widthForSegment(1)=16 // Menu for segment 1 dim m As NSFlagMenuItem FlagMenu=New NSMenuMBS("Flag Menu") m=New NSFlagMenuItem m.CreateMenuItem("Red") m.Image=New NSImageMBS( Flags01 ) m.Enabled=true m.tag=1 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m FlagMenu.addItem m m=New NSFlagMenuItem m.CreateMenuItem("Orange") m.Image=New NSImageMBS( Flags02 ) m.Enabled=true m.tag=2 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m FlagMenu.addItem m m=New NSFlagMenuItem m.CreateMenuItem("Yellow") m.Image=New NSImageMBS( Flags03 ) m.Enabled=true m.tag=3 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m FlagMenu.addItem m m=New NSFlagMenuItem m.CreateMenuItem("Green") m.Image=New NSImageMBS( Flags04 ) m.Enabled=true m.tag=4 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m FlagMenu.addItem m m=New NSFlagMenuItem m.CreateMenuItem("Blue") m.Image=New NSImageMBS( Flags05 ) m.Enabled=true m.tag=5 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m FlagMenu.addItem m // Add the menu to segment 1 menuForSegment(1)=FlagMenu End Sub
Sub DelegateForMenuAction(SelectedTag As Integer) for i As Integer=0 to UBound( items ) if items(i).tag=SelectedTag then imageForSegment(0)=items(i).Image Window1.Label1.Text="Flag menu="+items(i).Title Exit end if next End Sub
Property Protected FlagMenu As NSMenuMBS
Property Protected items() As NSFlagMenuItem
End Class
Class NSFlagMenuItem Inherits NSMenuItemMBS
EventHandler Sub Action() if ActionMethod<>Nil then ActionMethod.Invoke( Self.tag ) end if End EventHandler
Property ActionMethod As MainToolbarFlagMenuAction
End Class
Class NSToolbarSearchField Inherits NSSearchFieldMBS
EventHandler Sub Action() Window1.Label1.Text="SearchField text="+Self.StringValue End EventHandler
Sub Constructor() // Calling the overridden superclass constructor. Super.Constructor( 0,0,200,MainToolbarNSSearchFieldHeight ) // Menu dim m As NSSearchFieldMenuItem SearchMenu=New NSMenuMBS("Search Menu") m=New NSSearchFieldMenuItem m.CreateMenuItem("Title begins with") m.Enabled=true m.tag=1 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m SearchMenu.addItem m m=New NSSearchFieldMenuItem m.CreateMenuItem("Title contains") m.Enabled=true m.tag=2 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m SearchMenu.addItem m m=New NSSearchFieldMenuItem m.CreateMenuItem("Title ends with") m.Enabled=true m.tag=3 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m SearchMenu.addItem m // Add the menu to the searchfield searchMenuTemplate = SearchMenu End Sub
Sub DelegateForMenuAction(SelectedTag As Integer) for i As Integer=0 to UBound( items ) if items(i).tag=SelectedTag then Window1.Label1.Text="SearchField menu="+items(i).Title Exit end if next End Sub
Property Protected SearchMenu As NSMenuMBS
Property Protected items() As NSSearchFieldMenuItem
End Class
Class NSSearchFieldMenuItem Inherits NSMenuItemMBS
EventHandler Sub Action() if ActionMethod<>Nil then ActionMethod.Invoke( Self.tag ) end if End EventHandler
Property ActionMethod As MainToolbarSearchFieldMenuAction
End Class
ExternalFile Flags01
End ExternalFile
ExternalFile Flags02
End ExternalFile
ExternalFile Flags03
End ExternalFile
ExternalFile Flags04
End ExternalFile
ExternalFile Flags05
End ExternalFile
ExternalFile ActionGearTemplate
End ExternalFile
ExternalFile AddButtonTemplate
End ExternalFile
ExternalFile EditButtonSmallTemplate
End ExternalFile
ExternalFile MediaPlayButtonTemplate
End ExternalFile
ExternalFile MenuGlyphTemplate
End ExternalFile
ExternalFile PreferencesButtonTemplate
End ExternalFile
ExternalFile PresentationGridViewTemplate
End ExternalFile
ExternalFile PresentationListAndPreviewTemplate
End ExternalFile
ExternalFile PresentationSettingsTemplate
End ExternalFile
ExternalFile PresentationTwoListesTemplate
End ExternalFile
ExternalFile SystemNewFolderTemplate
End ExternalFile
ExternalFile UserButtonTemplate
End ExternalFile
End Project

See also:

The items on this page are in the following plugins: MBS MacControls Plugin.


The biggest plugin in space...