Since the TreeGadget can change the status of each item without changing the selection, I created my own event. For this the last state of the item is stored in item data.
Code: Select all
;-TOP
; New EventType for TreeGadgets by mk-soft, v1.04.1, Update 28.02.2021
EnableExplicit
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
;- MacOS SetNotificationCallback
ImportC ""
sel_getName(selector)
sel_registerName(str.p-utf8)
class_addMethod(class, selector, imp, types.p-utf8)
EndImport
; *** required variables ***
Global notificationCenter = CocoaMessage(0, 0, "NSNotificationCenter defaultCenter")
Global appDelegate = CocoaMessage(0, CocoaMessage(0, 0, "NSApplication sharedApplication"), "delegate")
Global delegateClass = CocoaMessage(0, appDelegate, "class")
Structure udtCallbackEntry
Window.i
Gadget.i
EndStructure
Global NewMap CallbackEntry.udtCallbackEntry()
; *** Callback (Base by Shardik) ***
Procedure SetGadgetCallback(GadgetID.I, GadgetCallback.I, Method.S)
Protected PlaceholderList.S
PlaceholderList = LSet("@:", CountString(Method, ":"), "@")
class_addMethod_(delegateClass, sel_registerName_(Method), GadgetCallback, PlaceholderList)
CocoaMessage(0, GadgetID(GadgetID), "setDelegate:", appDelegate)
EndProcedure
; *** Notification (Base by Shardik) ***
Procedure SetNotificationCallback(Window, Gadget, *Callback, Notification.s)
Protected selector
AddMapElement(CallbackEntry(), Str(GadgetID(Gadget)))
CallbackEntry()\Window = Window
CallbackEntry()\Gadget = Gadget
selector = sel_registerName("CB_" + Str(*Callback))
class_addMethod(delegateClass, selector, *Callback, "v@:@")
CocoaMessage(0, notificationCenter,
"addObserver:", appDelegate,
"selector:", selector,
"name:$", @Notification,
"object:", GadgetID(Gadget))
EndProcedure
Procedure RemoveNotificationCallback(Window, Gadget, *Callback, Notification.s)
CocoaMessage(0, notificationCenter,
"removeObserver:", appDelegate,
"name:$", @Notification,
"object:", GadgetID(Gadget))
EndProcedure
; *** Cocoa String Helper ***
Macro CocoaString(NSString)
PeekS(CocoaMessage(0, NSString, "UTF8String"), -1, #PB_UTF8)
EndMacro
CompilerEndIf
; ********
Enumeration #PB_EventType_FirstCustomValue
#MyEventType_Tree_Expanded
#MyEventType_Tree_Collapsed
EndEnumeration
Procedure InitTreeGadgetData(Gadget)
Protected item, cnt
cnt = CountGadgetItems(Gadget) - 1
For item = 0 To cnt
SetGadgetItemData(Gadget, item, GetGadgetItemState(Gadget, item))
Next
EndProcedure
Procedure CheckTreeGadget(Gadget)
Protected item, cnt, state, last_state
If GadgetType(Gadget) = #PB_GadgetType_Tree
cnt = CountGadgetItems(Gadget) - 1
For item = 0 To cnt
state = GetGadgetItemState(Gadget, item)
last_state = GetGadgetItemData(Gadget, item)
SetGadgetItemData(Gadget, item, state)
; New EventTypes
If (state & #PB_Tree_Expanded) And Not (last_state & #PB_Tree_Expanded)
PostEvent(#PB_Event_Gadget, EventWindow(), Gadget, #MyEventType_Tree_Expanded, item)
EndIf
If (state & #PB_Tree_Collapsed) And Not (last_state & #PB_Tree_Collapsed)
PostEvent(#PB_Event_Gadget, EventWindow(), Gadget, #MyEventType_Tree_Collapsed, item)
EndIf
Next
EndIf
EndProcedure
Procedure DoGadgetEvent()
Protected Gadget
Protected item, cnt, state, last_state
Gadget = EventGadget()
If GadgetType(Gadget) = #PB_GadgetType_Tree
CheckTreeGadget(gadget)
EndIf
EndProcedure
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
Procedure NotificationCallback(*Object, Selector, Notification)
Protected Notify.s, GadgetID, Gadget
GadgetID = CocoaMessage(0, Notification, "object")
Notify.s = CocoaString(CocoaMessage(0, Notification, "name"))
Gadget = CallbackEntry(Str(GadgetID))\Gadget
;Debug "Selector " + PeekS(sel_getName(Selector), -1, #PB_UTF8)
;Debug "Gadget " + Gadget
;Debug "Notify " + Notify
Select Notify
Case "NSOutlineViewItemDidExpandNotification"
CheckTreeGadget(Gadget)
Case "NSOutlineViewItemDidCollapseNotification"
CheckTreeGadget(Gadget)
EndSelect
EndProcedure
CompilerEndIf
; *****
CompilerIf #PB_Compiler_IsMainFile
Procedure UpdateTreeGadget(Gadget, Item)
Protected count, i
count = CountGadgetItems(Gadget) - 1
For i = 0 To count
If i <> item
If GetGadgetItemState(Gadget, i) & #PB_Tree_Expanded
SetGadgetItemState(Gadget, i, #PB_Tree_Collapsed)
EndIf
EndIf
Next
EndProcedure
Define ID, a, cnt
If OpenWindow(0, 0, 0, 600, 500, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
TreeGadget(0, 5, 5, 290, 490) ; TreeGadget standard
TreeGadget(1, 300, 5, 290, 490, #PB_Tree_CheckBoxes | #PB_Tree_NoLines); TreeGadget with Checkboxes + NoLines
For ID = 0 To 1
For a = 0 To 10
AddGadgetItem (ID, -1, "Normal Item "+Str(a), 0, 0) ; if you want to add an image, use
AddGadgetItem (ID, -1, "Node "+Str(a), 0, 0) ; ImageID(x) as 4th parameter
AddGadgetItem(ID, -1, "Sub-Item 1", 0, 1) ; These are on the 1st sublevel
AddGadgetItem(ID, -1, "Sub-Item 2", 0, 1)
AddGadgetItem(ID, -1, "Sub-Item 3", 0, 1)
AddGadgetItem(ID, -1, "Sub-Item 4", 0, 1)
AddGadgetItem (ID, -1, "File "+Str(a), 0, 0) ; sublevel 0 again
Next
Next
InitTreeGadgetData(0)
InitTreeGadgetData(1)
CompilerIf #PB_Compiler_OS = #PB_OS_Windows
BindEvent(#PB_Event_Gadget, @DoGadgetEvent())
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_Linux
BindEvent(#PB_Event_Gadget, @DoGadgetEvent())
CompilerEndIf
CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
SetNotificationCallback(0, 0, @NotificationCallback(), "NSOutlineViewItemDidExpandNotification")
SetNotificationCallback(0, 0, @NotificationCallback(), "NSOutlineViewItemDidCollapseNotification")
SetNotificationCallback(0, 1, @NotificationCallback(), "NSOutlineViewItemDidExpandNotification")
SetNotificationCallback(0, 1, @NotificationCallback(), "NSOutlineViewItemDidCollapseNotification")
CompilerEndIf
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
Case #PB_Event_Gadget
Select EventGadget()
Case 0, 1
Select EventType()
Case #MyEventType_Tree_Expanded
Debug "Gadget " + EventGadget() + " Item " + EventData() + " Expanded"
UpdateTreeGadget(EventGadget(), EventData())
Case #MyEventType_Tree_Collapsed
Debug "Gadget " + EventGadget() + " Item " + EventData() + " Collapsed"
Case #PB_EventType_Change
; Debug "Gadget " + EventGadget() + " Item " + GetGadgetState(EventGadget()) + " Selected"
EndSelect
EndSelect
Default
EndSelect
ForEver
EndIf
CompilerEndIf