Page 1 of 1

TreeGadget() no visible selection

Posted: Sun Mar 18, 2018 7:03 pm
by HeX0R
Run this code and press the button to see the effect.

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "", #PB_Window_SystemMenu)
TreeGadget(0, 5, 5, 290, 260)
ButtonGadget(1, 25, 270, 120, 24, "Add Item")

AddGadgetItem(0, -1, "Item 1", 0, 0)
AddGadgetItem(0, -1, "Item 2", 0, 0)
AddGadgetItem(0, -1, "Item 3", 0, 1)
AddGadgetItem(0, -1, "Item 4", 0, 1)

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

Repeat
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow
			Break
		Case #PB_Event_Gadget
			Select EventGadget()
				Case 1
					AddGadgetItem(0, 1, "Added #1", 0, 0)
					AddGadgetItem(0, 2, "Added #2", 0, 1)
					AddGadgetItem(0, 3, "Added #3", 0, 1)
					SetGadgetState(0, 1)
					SetGadgetItemState(0, 1, #PB_Tree_Expanded)
					If GetGadgetState(0) = 1
						Debug "Added #1 is selected, but you don't see it!"
						Debug "Try to select Added #1 with the mouse, you will not see anything"
					EndIf
			EndSelect
	EndSelect
ForEver

Re: TreeGadget() no visible selection

Posted: Sun Mar 18, 2018 7:48 pm
by Bisonte
Confirmed on Win 10 x64 with PB 5.62 (x86/x64)

Re: TreeGadget() no visible selection

Posted: Sun Mar 18, 2018 10:30 pm
by Demivec
I don't think it's a bug, though I can't rule that out.

I think the more proper code may be to write it this way (note the two changed and commented lines in 'Case 1'):

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "", #PB_Window_SystemMenu)
TreeGadget(0, 5, 5, 290, 260)
ButtonGadget(1, 25, 270, 120, 24, "Add Item")

AddGadgetItem(0, -1, "Item 1", 0, 0)
AddGadgetItem(0, -1, "Item 2", 0, 0)
AddGadgetItem(0, -1, "Item 3", 0, 1)
AddGadgetItem(0, -1, "Item 4", 0, 1)

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
         Break
      Case #PB_Event_Gadget
         Select EventGadget()
            Case 1
               AddGadgetItem(0, 1, "Added #1", 0, 0)
               AddGadgetItem(0, 2, "Added #2", 0, 1)
               AddGadgetItem(0, 3, "Added #3", 0, 1)
               SetGadgetState(0, 1)
               SetGadgetItemState(0, 1, #PB_Tree_Selected | #PB_Tree_Expanded) ;Formally select the item
               SetActiveGadget(0) ;and make the gadget active to show the selection
               If GetGadgetState(0) = 1
                  Debug "Added #1 is selected, but you don't see it!"
                  Debug "Try to select Added #1 with the mouse, you will not see anything"
               EndIf
         EndSelect
   EndSelect
ForEver

Re: TreeGadget() no visible selection

Posted: Mon Mar 19, 2018 8:27 pm
by HeX0R
Maybe it's the more proper way, anyway I would say it shouldn't happen at all, that you click on a tree item and it will not be (visual) selected, don't you think?

Re: TreeGadget() no visible selection

Posted: Mon Mar 19, 2018 9:26 pm
by mk-soft
I think it's a problem with the order the commands go to control.
Besides that a flag is missing

Code: Select all

OpenWindow(0, 0, 0, 300, 300, "", #PB_Window_SystemMenu)
TreeGadget(0, 5, 5, 290, 260, #PB_Tree_AlwaysShowSelection)
ButtonGadget(1, 25, 270, 120, 24, "Add Item")

AddGadgetItem(0, -1, "Item 1", 0, 0)
AddGadgetItem(0, -1, "Item 2", 0, 0)
AddGadgetItem(0, -1, "Item 3", 0, 1)
AddGadgetItem(0, -1, "Item 4", 0, 1)

SetGadgetItemState(0, 1, #PB_Tree_Expanded)

;SetWindowTheme_(GadgetID(0), @"Explorer", 0)
    
Repeat
   Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
         Break
      Case #PB_Event_Gadget
         Select EventGadget()
            Case 1
               AddGadgetItem(0, 1, "Added #1", 0, 0)
               AddGadgetItem(0, 2, "Added #2", 0, 1)
               AddGadgetItem(0, 3, "Added #3", 0, 1)
               SetGadgetItemState(0, 1, #PB_Tree_Expanded)
               SetGadgetState(0, 1)
               If GetGadgetState(0) = 1
                  Debug "Added #1 is selected, but you don't see it!"
                  Debug "Try to select Added #1 with the mouse, you will not see anything"
               EndIf
         EndSelect
   EndSelect
ForEver
So works with Window 7 and 10

P.S. Same on Linux and Mac
Works only if first expanded tree, then set item

Re: TreeGadget() no visible selection

Posted: Thu Apr 03, 2025 4:42 pm
by Fred
Not a bug, you need to use SetGadgetItemState(0, 1, #PB_Tree_Expanded | #PB_Tree_Selected) as SetGadgetItemState() changes the selected state as well. Also #PB_Tree_AlwaysShowSelection needs to be specified if the selection needs to be seen when the gadget doesn't have the focus.

Re: TreeGadget() no visible selection

Posted: Thu Apr 03, 2025 6:19 pm
by HeX0R
Boah, can't remember that one :lol: