I guess that's the right...
Code: Select all
Global RootNode = CocoaMessage(0, 0, "NSTreeNode treeNodeWithRepresentedObject:$", @"Root node")
CocoaMessage(0, RootNode, "retain")
ChildNode1 = CocoaMessage(0, 0, "NSTreeNode treeNodeWithRepresentedObject:$", @"Child node 1")
ChildNode2 = CocoaMessage(0, 0, "NSTreeNode treeNodeWithRepresentedObject:$", @"Child node 2")
ChildNode1_1 = CocoaMessage(0, 0, "NSTreeNode treeNodeWithRepresentedObject:$", @"Child node 1.1")
ChildNode1_2 = CocoaMessage(0, 0, "NSTreeNode treeNodeWithRepresentedObject:$", @"Child node 1.2")
ChildNode1_3 = CocoaMessage(0, 0, "NSTreeNode treeNodeWithRepresentedObject:$", @"Child node 1.3")
ChildNodes = CocoaMessage(0, RootNode, "mutableChildNodes")
CocoaMessage(0, ChildNodes, "addObject:", ChildNode1)
CocoaMessage(0, ChildNodes, "addObject:", ChildNode2)
ChildNodes = CocoaMessage(0, ChildNode1, "mutableChildNodes")
CocoaMessage(0, ChildNodes, "addObject:", ChildNode1_1)
CocoaMessage(0, ChildNodes, "addObject:", ChildNode1_2)
CocoaMessage(0, ChildNodes, "addObject:", ChildNode1_3)
; dataSource => child of item
ProcedureC.i child_ofItem(obj, sel, outlineView, index, item)
If item = #nil : item = RootNode : EndIf
ProcedureReturn CocoaMessage(0, CocoaMessage(0, item, "mutableChildNodes"), "objectAtIndex:", index)
EndProcedure
; dataSource => is item expandable ?
ProcedureC.i isItemExpandable(obj, sel, outlineView, item)
If CocoaMessage(0, item, "isLeaf")
ProcedureReturn #NO
Else
ProcedureReturn #YES
EndIf
EndProcedure
; dataSource => number of children of item
ProcedureC numberOfChildrenOfItem(obj, sel, outlineView, item)
If item = #nil : item = RootNode : EndIf
ProcedureReturn CocoaMessage(0, CocoaMessage(0, item, "mutableChildNodes"), "count")
EndProcedure
; dataSource => object value
ProcedureC objectValueForTableColumn_byItem(obj, sel, outlineView, tableColumn, item)
ProcedureReturn CocoaMessage(0, item, "representedObject")
EndProcedure
; create class(es)
class = objc_allocateClassPair_(objc_getClass_("NSObject"), "MyDataSourceClass", 0)
class_addMethod_(class, sel_registerName_("outlineView:child:ofItem:"), @child_ofItem(), "@@:i@")
class_addMethod_(class, sel_registerName_("outlineView:isItemExpandable:"), @isItemExpandable(), "c@:@")
class_addMethod_(class, sel_registerName_("outlineView:numberOfChildrenOfItem:"), @numberOfChildrenOfItem(), "i@:@")
class_addMethod_(class, sel_registerName_("outlineView:objectValueForTableColumn:byItem:"), @objectValueForTableColumn_byItem(), "@@:@@")
objc_registerClassPair_(class)
; test
OpenWindow(0, 0, 0, 400, 340, "NSOutlineView test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TreeGadget(0, 10, 10, 380, 280)
DataSource = CocoaMessage(0, 0, "MyDataSourceClass new")
CocoaMessage(0, GadgetID(0), "setDataSource:", DataSource)
CocoaMessage(0, GadgetID(0), "setDelegate:", DataSource)
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow