Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS MacBase Plugin, MBS MacFrameworks Plugin, MBS MacCocoa Plugin, MBS MacControls Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacFrameworks/TokenField
This example is the version from Sat, 5th Oct 2012.
Project "TokenField.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
End Class
Class TestWindow Inherits Window
Control CocoaControlMBS1 Inherits CocoaControlMBS
ControlInstance CocoaControlMBS1 Inherits CocoaControlMBS
EventHandler Function GetView() As NSViewMBS
n=new MyNSTokenFieldMBS(0,0,me.Width,me.Height)
n.tokenStyle = n.NSRoundedTokenStyle
// tell it how the resize is going
n.autoresizingMask=n.NSViewHeightSizable+n.NSViewWidthSizable
Return n
End EventHandler
End Control
Control Listbox1 Inherits Listbox
ControlInstance Listbox1 Inherits Listbox
End Control
Control BevelButton1 Inherits BevelButton
ControlInstance BevelButton1 Inherits BevelButton
EventHandler Sub Action()
MsgBox n.StringValue
End EventHandler
End Control
Control BevelButton2 Inherits BevelButton
ControlInstance BevelButton2 Inherits BevelButton
EventHandler Sub Action()
dim v() as Variant = n.objects
dim a() as string
for each vv as Variant in v
a.Append vv
next
MsgBox Join(a,EndOfLine)
End EventHandler
End Control
Control BevelButton3 Inherits BevelButton
ControlInstance BevelButton3 Inherits BevelButton
EventHandler Sub Action()
dim v() as Variant
v.Append 2
v.Append 4
n.setObjects v
End EventHandler
End Control
Control BevelButton4 Inherits BevelButton
ControlInstance BevelButton4 Inherits BevelButton
EventHandler Sub Action()
n.StringValue = ""
End EventHandler
End Control
EventHandler Sub Open()
if not CocoaControlMBS1.Available then
MsgBox "This example requires Mac OS X 10.5."
quit
end if
End EventHandler
Property n As NSTokenFieldMBS
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyNSTokenFieldMBS Inherits NSTokenFieldMBS
EventHandler Sub Action()
// this event does nothing in NSTokenFieldMBS
End EventHandler
EventHandler Function completionsForSubstring(substring as string, tokenIndex as integer, byref selectedIndex as integer) As variant()
dim answers() as Variant
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if instr(list.Cell(i,1), substring)>0 or instr(list.Cell(i,2), substring)>0 then
answers.Append list.Cell(i,2)
end if
next
Return answers
End EventHandler
EventHandler Function displayStringForRepresentedObject(representedObject as variant) As string
dim id as string = representedObject
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if list.Cell(i,0) = id then
Return list.Cell(i,1)
end if
next
End EventHandler
EventHandler Function editingStringForRepresentedObject(representedObject as variant) As string
dim id as string = representedObject
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if list.Cell(i,0) = id then
Return list.Cell(i,2)
end if
next
End EventHandler
EventHandler Function hasMenuForRepresentedObject(representedObject as variant) As boolean
Return representedObject.IntegerValue = 1
End EventHandler
EventHandler Function menuForRepresentedObject(representedObject as variant) As NSMenuMBS
if representedObject.IntegerValue = 1 then
LastMenu = new NSMenuMBS
LastMenuItem = new MyNSMenuItemMBS
LastMenuItem.CreateMenuItem "Hello World"
LastMenu.addItem LastMenuItem
Return LastMenu
end if
End EventHandler
EventHandler Function representedObjectForEditingString(editingString as string) As variant
dim list as listbox = TestWindow.Listbox1
dim u as integer = list.ListCount-1
for i as integer = 0 to u
if list.Cell(i,1) = editingString or list.Cell(i,2) = editingString then
Return list.Cell(i,0)
end if
next
End EventHandler
EventHandler Function shouldAddObjects(tokens() as variant, index as Integer) As variant()
// add all
Return tokens
End EventHandler
EventHandler Function textShouldBeginEditing(fieldEditor as NSTextMBS) As boolean
// this event does nothing in NSTokenFieldMBS
End EventHandler
EventHandler Function textShouldEndEditing(fieldEditor as NSTextMBS) As boolean
// this event does nothing in NSTokenFieldMBS
End EventHandler
EventHandler Sub tokenFieldAction()
System.DebugLog CurrentMethodName
End EventHandler
EventHandler Function tokenFieldTextShouldBeginEditing(fieldEditor as NSTextMBS) As boolean
System.DebugLog CurrentMethodName
Return true
End EventHandler
EventHandler Function tokenFieldTextShouldEndEditing(fieldEditor as NSTextMBS) As boolean
System.DebugLog CurrentMethodName
Return true
End EventHandler
Property LastMenu As NSMenuMBS
Property LastMenuItem As MyNSMenuItemMBS
End Class
Class MyNSMenuItemMBS Inherits NSMenuItemMBS
EventHandler Sub Action()
MsgBox "You selected menu item."
End EventHandler
End Class
End Project
The items on this page are in the following plugins: MBS MacFrameworks Plugin.