Private Shared Function setToolItemSearch(itemIdentifier As String) as Ptr
// 文字列を指定してクラスオブジェクト/セレクタを取得する。最初に一回宣言しておけばよい。
Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
Declare Function NSSelectorFromString Lib "Cocoa" (aSelName As CFStringRef) As Ptr
Declare Function alloc Lib "Cocoa" Selector "alloc" (receiver As Ptr) As Ptr
Dim search1 As Ptr = NSClassFromString("NSSearchField")
search1 = alloc(search1)
Declare Function initWithFrame Lib "Cocoa" Selector "initWithFrame:" (receiver As Ptr, identifier As NSRect) As Ptr
search1 = initWithFrame(search1, NSMakeRect(0,0,150,24))
Declare Sub setDelegate Lib "Cocoa" Selector "setDelegate:" (receiver As Ptr, id As Ptr)
setDelegate(search1, makeDelegateTextView(SearchToolbarItemIdentifier)) // Delegateの設定(retunキー、deleteキーが押された時をアクションとして認識するため)
Dim toolbarItem As Ptr = NSClassFromString("NSToolbarItem")
toolbarItem = alloc(toolbarItem)
Declare Function initWithItemIdentifier Lib "Cocoa" Selector "initWithItemIdentifier:" (receiver As Ptr, identifier As CFStringRef) As Ptr
toolbarItem = initWithItemIdentifier(toolbarItem, itemIdentifier)
Declare Sub setLabel Lib "Cocoa" Selector "setLabel:" (receiver As Ptr, label As CFStringRef)
setLabel(toolbarItem, "NSSearchField")
Declare Sub setPaletteLabel Lib "Cocoa" Selector "setPaletteLabel:" (receiver As Ptr, label As CFStringRef)
setPaletteLabel(toolbarItem, "Search")
Declare Sub setToolTip Lib "Cocoa" Selector "setToolTip:" (receiver As Ptr, text As CFStringRef)
setToolTip(toolbarItem, "Search Your Document")
Declare Sub setView Lib "Cocoa" Selector "setView:" (receiver As Ptr, actionTarget As Ptr)
setView(toolbarItem, search1)
// Target/Actionは、NSSearchFieldCellのsearchButtonCellに対して設定する。
Declare Function cell Lib "Cocoa" Selector "cell" (receiver As Ptr) As Ptr
Dim cell1 As Ptr = cell(search1)
Declare Sub setSendsWholeSearchString Lib "Cocoa" Selector "setSendsWholeSearchString:" (receiver As Ptr, flag As Boolean)
setSendsWholeSearchString(cell1, false) // false:虫眼鏡ボタンが有効に, true:虫眼鏡ボタンが無効に(この解釈でいいか?)
Declare Function searchButtonCell Lib "Cocoa" Selector "searchButtonCell" (receiver As Ptr) As Ptr
Dim searchButtonCell1 As Ptr = searchButtonCell(cell1)
Declare Sub setTarget Lib "Cocoa" Selector "setTarget:" (receiver As Ptr, actionTarget As Ptr)
setTarget(searchButtonCell1, makeTarget(itemIdentifier)) // Actionの受け口となるメソッドを定義
Declare Sub setAction Lib "Cocoa" Selector "setAction:" (receiver As Ptr, actionEvent As Ptr)
setAction(searchButtonCell1, NSSelectorFromString("action:"))
Declare Sub refusesFirstResponder Lib "Cocoa" Selector "setRefusesFirstResponder:" (receiver As Ptr, flg As Boolean) // NSControlのメソッド
refusesFirstResponder(search1,true)
return toolbarItem
End Function