Platforms to show: All Mac Windows Linux Cross-Platform

/MacCocoa/NSSearchField control with menu


Required plugins for this example: MBS MacBase 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: /MacCocoa/NSSearchField control with menu

This example is the version from Tue, 19th Jan 2015.

Project "NSSearchField control with menu.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open() if not TargetCocoa then MsgBox "Please run this example with Cocoa." end if End EventHandler
End Class
Class Window1 Inherits Window
Control StaticText1 Inherits Label
ControlInstance StaticText1 Inherits Label
End Control
Control PushButton1 Inherits PushButton
ControlInstance PushButton1 Inherits PushButton
EventHandler Sub Action() 'StaticText1.text = n.StringValue StaticText1.text = NSSearchFieldControlMBS1.View.StringValue End EventHandler
End Control
Control Listbox1 Inherits Listbox
ControlInstance Listbox1 Inherits Listbox
End Control
Control NSSearchFieldControlMBS1 Inherits myNSSearchFieldControlMBS
ControlInstance NSSearchFieldControlMBS1 Inherits myNSSearchFieldControlMBS
EventHandler Sub Action() log CurrentMethodName End EventHandler
EventHandler Function KeyDown(Key As String) As Boolean log CurrentMethodName+": "+key End EventHandler
EventHandler Sub KeyUp(Key As String) log CurrentMethodName+": "+key If Asc(Key) = 13 Then Dim t As String = Me.View.StringValue Select Case Me.result Case SearchResult.BeginsWith t = t + "%" Case SearchResult.Contains t = "%" + t + "%" Case SearchResult.EndsWith t = "%" + t End Select StaticText1.Text = t End If End EventHandler
EventHandler Sub Open() Me.createMenu End EventHandler
EventHandler Sub TextDidBeginEditing(fieldEditor as NSTextMBS, notification as NSNotificationMBS) log CurrentMethodName End EventHandler
EventHandler Sub TextDidChange(fieldEditor as NSTextMBS, notification as NSNotificationMBS) log CurrentMethodName End EventHandler
EventHandler Sub TextDidEndEditing(fieldEditor as NSTextMBS, notification as NSNotificationMBS) log CurrentMethodName End EventHandler
EventHandler Function textShouldBeginEditing(fieldEditor as NSTextMBS) As boolean log CurrentMethodName Return true End EventHandler
EventHandler Function textShouldEndEditing(fieldEditor as NSTextMBS) As boolean log CurrentMethodName Return true End EventHandler
End Control
EventHandler Sub Open() if not TargetCocoa then MsgBox "This example requires Mac OS X 10.5 Cocoa." quit end if End EventHandler
Sub log(s as string) Listbox1.AddRow s Listbox1.ScrollPosition = Listbox1.ListCount End Sub
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 myNSSearchFieldControlMBS Inherits NSSearchFieldControlMBS
Sub DelegateForMenuAction(SelectedTag As Integer) window1.log CurrentMethodName+" "+str(SelectedTag) dim index as integer = searchmenu.indexOfItemWithTag(SelectedTag) if index >= 0 then dim item as NSMenuItemMBS = searchmenu.Item(index) Select case SelectedTag case 1 // Begins with result=SearchResult.BeginsWith case 3 // Ends with result=SearchResult.EndsWith case 2 // Contains result=SearchResult.Contains case 4 // cancel ' TODO: handle cancel search end Select // recreate searchbox's menu createMenu end if End Sub
Sub createMenu() n = me.View Redim items(-1) // Menu Dim m As ModSearch.SearchFieldMenuItem searchmenu=New NSMenuMBS("Search Menu") m=New ModSearch.SearchFieldMenuItem m.CreateMenuItem("Begins with") m.Enabled=True if result = SearchResult.BeginsWith then m.state = 1 else m.state = 0 end if m.tag=1 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m searchmenu.addItem m m=New ModSearch.SearchFieldMenuItem m.CreateMenuItem("Contains") m.Enabled=True if result = SearchResult.Contains then m.state = 1 else m.state = 0 end if m.tag=2 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m searchmenu.addItem m m=New ModSearch.SearchFieldMenuItem m.CreateMenuItem("Ends with") if result = SearchResult.EndsWith then m.state = 1 else m.state = 0 end if m.Enabled=True m.tag=3 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m searchmenu.addItem m m=New ModSearch.SearchFieldMenuItem m.CreateSeparator searchmenu.addItem m m=New ModSearch.SearchFieldMenuItem m.CreateMenuItem("Cancel") m.Enabled=True m.tag=4 m.ActionMethod=AddressOf DelegateForMenuAction items.Append m searchmenu.addItem m // ***** recent searches dim ClearItem as new NSMenuItemMBS ClearItem.CreateMenuItem("Clear") ClearItem.tag = NSSearchFieldMBS.NSSearchFieldClearRecentsMenuItemTag searchmenu.addItem ClearItem dim SepItem as new NSMenuItemMBS SepItem.CreateSeparator SepItem.tag = NSSearchFieldMBS.NSSearchFieldRecentsTitleMenuItemTag searchmenu.addItem SepItem dim RecentSearchesItem as new NSMenuItemMBS RecentSearchesItem.CreateMenuItem "Recent Searches" RecentSearchesItem.tag = NSSearchFieldMBS.NSSearchFieldRecentsTitleMenuItemTag searchmenu.addItem RecentSearchesItem dim RecentsItem as new NSMenuItemMBS RecentsItem.CreateMenuItem "Recents" RecentsItem.tag = NSSearchFieldMBS.NSSearchFieldRecentsMenuItemTag searchmenu.addItem RecentsItem n.searchMenuTemplate = searchmenu n.focusRingType = n.NSFocusRingTypeDefault n.maximumRecents = 10 n.setRecentSearches array("Hello", "World") End Sub
Property items() As ModSearch.SearchFieldMenuItem
Property n As NSSearchFieldMBS
Property result As SearchResult
Property searchmenu As NSMenuMBS
End Class
Module ModSearch
Delegate Sub SearchFieldMenuAction(SelectedTag As Integer)
Enum SearchResult BeginsWith Contains EndsWith End Enum
End Module
Class SearchFieldMenuItem Inherits NSMenuItemMBS
EventHandler Sub Action() if ActionMethod<>Nil then ActionMethod.Invoke( Self.tag ) end if End EventHandler
Property ActionMethod As SearchFieldMenuAction
End Class
End Project

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


The biggest plugin in space...