Page 1 of 1

Calculate Y coordinate of TreeGadget item

Posted: Wed Mar 01, 2023 9:12 pm
by wombats
How would I calculate the Y position of a tree item by iterating over the items? Or is there a simple formula I could use? I think there are API functions to do this on Windows, but I'm trying to program my own with drag and drop since the Drag & Drop library doesn't allow that on macOS, so I need to know how to determine which item the user clicked on. This code only has the PB TreeGadget for simplicity's sake.

Code: Select all

EnableExplicit

OpenWindow(0, 100, 100, 400, 300, "", #PB_Window_SystemMenu)

TreeGadget(0, 0, 0, 400, 300)
AddGadgetItem(0, -1, "Item 1")
AddGadgetItem(0, -1, "Item 2")
AddGadgetItem(0, -1, "Item 2.1", 0, 1)
AddGadgetItem(0, -1, "Item 2.2", 0, 1)
AddGadgetItem(0, -1, "Item 2.2.1", 0, 2)
AddGadgetItem(0, -1, "Item 3")
AddGadgetItem(0, -1, "Item 4")

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

Define y, item, i, itemHeight

itemHeight = 25

item = 5

y = 0

For i = 0 To CountGadgetItems(0) - 1
  If GetGadgetItemState(0, i) & #PB_Tree_Collapsed
    ; skip children?
  EndIf
  If i = item
    Break
  EndIf
  y + itemHeight
Next

Debug y

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Calculate Y coordinate of TreeGadget item

Posted: Wed Mar 01, 2023 10:25 pm
by Shardik
I had already posted this cross-platform example on how to detect a clicked cell in a ListIconGadget. This method also works on MacOS in a TreeGadget. I have modified your example for MacOS to detect the clicked item, display its name and the cursor's y-position of the cursor above the item:

Code: Select all

EnableExplicit

Define CursorLocation.NSPoint
Define y.I

OpenWindow(0, 100, 100, 400, 300, "", #PB_Window_SystemMenu)

TreeGadget(0, 0, 0, 400, 300)
AddGadgetItem(0, -1, "Item 1")
AddGadgetItem(0, -1, "Item 2")
AddGadgetItem(0, -1, "Item 2.1", 0, 1)
AddGadgetItem(0, -1, "Item 2.2", 0, 1)
AddGadgetItem(0, -1, "Item 2.2.1", 0, 2)
AddGadgetItem(0, -1, "Item 3")
AddGadgetItem(0, -1, "Item 4")

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
        CursorLocation\x = WindowMouseX(0)
        CursorLocation\y = WindowHeight(0) - WindowMouseY(0)
        CocoaMessage(@CursorLocation, GadgetID(0),
          "convertPoint:@", @CursorLocation, "fromView:", 0)
        Debug "Left click on " + GetGadgetText(0) + ", y = " + CursorLocation\y
      EndIf
  EndSelect
ForEver

Re: Calculate Y coordinate of TreeGadget item

Posted: Wed Mar 01, 2023 11:28 pm
by RASHAD
Another hit from Shardik
Thanks Shardik
Next Maybe cross platform, who knows :D
Tested with PB 6.xx x86 Windows 11 x64

Code: Select all

If OpenWindow(0, 0, 0, 400, 600, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TreeGadget(0, 10, 10, 380, 580)
  For a = 0 To 10
    AddGadgetItem (0, -1, "Normal Item "+Str(a), 0, 0)
    AddGadgetItem (0, -1, "Node "+Str(a), 0, 0)
    AddGadgetItem(0, -1, "Sub-Item 1", 0, 1)
    AddGadgetItem(0, -1, "Sub-Item 2", 0, 1)
    AddGadgetItem(0, -1, "Sub-Item 3", 0, 1)
    AddGadgetItem(0, -1, "Sub-Item 4", 0, 1)
    AddGadgetItem (0, -1, "File "+Str(a), 0, 0)
  Next
;   LoadFont(0,"tahoma",14)
;   SetGadgetFont(0,FontID(0))
;   StartDrawing(WindowOutput(0))
;     DrawingFont(FontID(0))
;     rowheight = TextHeight("Q")+6
;   StopDrawing()
;   Debug rowheight
  rowheight = 22 ;Default Font
  Repeat
    Select WaitWindowEvent()
        
      Case #PB_Event_CloseWindow
        Quit = 1
        
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 0
            Select EventType()
              Case #PB_EventType_LeftClick
                yy = WindowMouseY(0)
                trim = Mod(yy,rowheight)
                If trim > rowheight/2
                  y = yy-trim
                Else
                  y = yy-trim-rowheight
                EndIf
                Debug y
            EndSelect
        EndSelect
        
    EndSelect
  Until Quit = 1
EndIf

For Windows :

Code: Select all

If OpenWindow(0, 0, 0, 400, 600, "TreeGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
   TreeGadget(0, 10, 10, 380, 580)
  For a = 0 To 10
    AddGadgetItem (0, -1, "Normal Item "+Str(a), 0, 0)
    AddGadgetItem (0, -1, "Node "+Str(a), 0, 0)
    AddGadgetItem(0, -1, "Sub-Item 1", 0, 1)
    AddGadgetItem(0, -1, "Sub-Item 2", 0, 1)
    AddGadgetItem(0, -1, "Sub-Item 3", 0, 1)
    AddGadgetItem(0, -1, "Sub-Item 4", 0, 1)
    AddGadgetItem (0, -1, "File "+Str(a), 0, 0)
  Next
      
r.RECT
Repeat
  Select WaitWindowEvent()     
    Case #PB_Event_CloseWindow
      Quit = 1
                       
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 0
        Select EventType()
          Case #PB_EventType_LeftClick
            r\left = GadgetItemID(0, GetGadgetState(0))
            SendMessage_(GadgetID(0),#TVM_GETITEMRECT,1,@r)
            Debug r\top
        EndSelect
      EndSelect
  EndSelect
Until Quit = 1
EndIf

Re: Calculate Y coordinate of TreeGadget item

Posted: Tue Mar 07, 2023 10:42 pm
by wombats
Thank you for the replies. :)