Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post bugreports for the Windows version here
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

Seems there is an issue with both SetGadgetState() and SetGadgetItemState() on Windows when using them on ListViewGadget, ListIconGadget and TreeGadget.
Linux looks to be fine, but could not test this on Mac or RPi.

ListViewGadget

Code: Select all

#win = 0
#ListView = 0
If OpenWindow(#win, 0, 0, 320, 140, "ListViewGadget SetGadgetState / SetGadgetItemState", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListViewGadget(#ListView, 10, 10, 300, 120)
  For a = 0 To 9
    AddGadgetItem (#ListView, -1, "Item " + Str(a) + " of the ListView")
  Next
  
  ; *** SetGadgetState() ***
  ; Works okay on Windows
  ; Works okay on Linux GTK2
  ; Works okay on Linux GTK3
  ; Works okay on Linux Qt
  SetGadgetState(#ListView, 1)
  
  ; *** SetGadgetItemState() ***
  ; Doesn't do anything on Windows
  ; Works okay on Linux GTK2
  ; Works okay on Linux GTK3
  ; Works okay on Linux Qt
  ;SetGadgetItemState(#ListView, 2, #True)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
ListIconGadget

Code: Select all

#win = 0
#ListIcon = 0
If OpenWindow(#win, 0, 0, 320, 140, "ListIconGadget SetGadgetState / SetGadgetItemState", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ListIconGadget(#ListIcon, 10, 10, 300, 120, "Column-0", 200)  ;, #PB_ListIcon_AlwaysShowSelection)  ; this flag doens't make any difference
  For a = 0 To 9
    AddGadgetItem (#ListIcon, -1, "Item " + Str(a) + " of the ListIcon")
  Next
  
  ; *** SetGadgetState() ***
  ; Doesn't do anything on Windows
  ; Works okay on Linux GTK2
  ; Works okay on Linux GTK3
  ; Works okay on Linux Qt
  SetGadgetState(#ListIcon, 1)
  
  ; *** SetGadgetItemState() ***
  ; Doesn't do anything on Windows
  ; Works okay on Linux GTK2
  ; Works okay on Linux GTK3
  ; Works okay on Linux Qt
  ;SetGadgetItemState(#ListIcon, 2, #PB_ListIcon_Selected)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
TreeGadget

Code: Select all

#win = 0
#Tree = 0
If OpenWindow(#win, 0, 0, 320, 140, "TreeGadget SetGadgetState / SetGadgetItemState", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  TreeGadget(#Tree, 10, 10, 300, 120)  ; , #PB_Tree_AlwaysShowSelection)  ; this flag doens't make any difference
  For a = 0 To 9
    AddGadgetItem (#Tree, -1, "Item " + Str(a) + " of the Tree")
  Next
  
  ; *** SetGadgetState() ***
  ; Doesn't do anything on Windows
  ; Works okay on Linux GTK2
  ; Works okay on Linux GTK3
  ; Works okay on Linux Qt
  SetGadgetState(#Tree, 1)
  
  ; *** SetGadgetItemState() ***
  ; Doesn't do anything on Windows
  ; Works okay on Linux GTK2
  ; Works okay on Linux GTK3
  ; Works okay on Linux Qt
  ;SetGadgetItemState(#Tree, 2, #PB_Tree_Selected)
  
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Fred
Administrator
Administrator
Posts: 18465
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by Fred »

Seems to work as expected here, can anybody else confirm ?
Axolotl
Addict
Addict
Posts: 921
Joined: Wed Dec 31, 2008 3:36 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by Axolotl »

I'm not sure if the behavior is as described in the help section.
I tested the first two examples ListViewGadget() and ListIconGadget() with PB 630 b6.
In all of these cases (see below), the commands did not select any entries.

Code: Select all

  ; The examples from above and acc. to help text for SetGadgetState() and SetGadgetItemState() 
  ; Doesn't do anything on Windows
  SetGadgetItemState(#ListView, 2, #True)   ; <= no selection on ListView ! 

  ; Doesn't do anything on Windows
  SetGadgetState(#ListIcon, 1)  ; <= selection shown with SetActiveGadget(#ListIcon) 

  ; Doesn't do anything on Windows
  SetGadgetItemState(#ListIcon, 2, #PB_ListIcon_Selected)  ; <= selection with Activate..
  ; BUT: this works with flag: #PB_ListIcon_AlwaysShowSelection 


=> It looks to me, that the inconsistency is on ListViewGadget and SetGadgetItemState()
Last edited by Axolotl on Fri Dec 19, 2025 4:35 pm, edited 1 time in total.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Little John
Addict
Addict
Posts: 4833
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by Little John »

I can confirm all problems reported above by bal, tested with PB 6.30 beta 5 (x64) on Windows 11.
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

Hey guys, thanks for looking into this.

Seems this has been broken for a long time. Just went back to 4.61 on Windows and I can reproduce the same issue.
I created most of my GUI apps on Linux, that's why I've never noticed this bug.
User avatar
Bisonte
Addict
Addict
Posts: 1329
Joined: Tue Oct 09, 2007 2:15 am

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by Bisonte »

bal wrote: Fri Dec 19, 2025 5:36 pm Hey guys, thanks for looking into this.

Seems this has been broken for a long time. Just went back to 4.61 on Windows and I can reproduce the same issue.
I created most of my GUI apps on Linux, that's why I've never noticed this bug.
I think you misunderstood the logic behind SetGadgetItemState...

I only check on Windows, but here all works like a charm.
SetGadgetItemState on a ListView is more or less useless, you can only check if this Item is selected by the user.

On ListIcon, it is only to handle the checkboxes if there are any. And SetgadgetState is completly working.
It selects an item (the whole row). You wrote in the commented line the Flag #PB_ListIcon_AlwaysShowSelection has no effect... but there is one...
you have to activate the ListIcon (it should have the focus) to see the marked item... if it is not active it is very hard to see the light darker bar)

The same at Treegadget, the Itemstate is for expand or collapse or a Checkbox at this item... (and the SetGadgetState() thing from the ListIcon)

I don't try it on the beta, but till 6.21 is on windows all ok ;)

To check this out, Read the help section of SetGadgetItemState() with the specific Gadget.
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
ChrisR
Addict
Addict
Posts: 1552
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by ChrisR »

Thanks for the explanation, Bisonte. I didn't understand the logic at first either
On a ListView, it works if #PB_ListView_Multiselect or #PB_ListView_ClickSelect is used.
On ListIcon with the example, we can use #PB_ListIcon_MultiSelect | #PB_ListIcon_AlwaysShowSelection to see the result
Well, the help could be more specific
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

I know what you mean, however, my Linux apps use this command a lot (with checkboxed ListIcon items) and they are working fine. (Yes, it can still be me :) )

The flag values on Windows and Linux:

Code: Select all

Debug #PB_ListIcon_Selected                         ; = 1
Debug #PB_ListIcon_Checked                          ; = 2
Debug #PB_ListIcon_Checked|#PB_ListIcon_Selected    ; = 3
Debug #PB_ListIcon_Inbetween                        ; = 4
Debug #PB_ListIcon_Inbetween|#PB_ListIcon_Selected  ; = 5
Where using the values with SetGadgetItemState() "Selected" means the item selection, not the checkbox.
To affect the checkboxes, the flags need to be combined as above.

It also can be that the Linux version of the command is the culprit, but the command is definitely not doing the same across the platforms.

Would be also great to see how this behaves on a Mac.
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

Okay, my bad.
While putting together this next example, found that #PB_ListIcon_AlwaysShowSelection needs to be ON for ListIconGadget, otherwise the selection is not shown for an inactive gadget.
...and I always use this flag, except when I report its absence as a bug :)

So, when you run the demo, right after the start click on the "ListIcon1 Active" button to see that actually item 2 is selected, just not highlighted due to the missing #PB_ListIcon_AlwaysShowSelection flag.

Feel free to click the other buttons to see how item and item-checkbox selection work.

Code: Select all

Enumeration
  #btnActive
  #btnSet0
  #btnSet1
  #btnSet2
  #btnSet3
  #btnSet4
  #btnSet5
  #ListIcon1
  #ListIcon2
EndEnumeration

#win = 0
If OpenWindow(#win, 0, 0, 400, 240, "ListIconGadget SetGadgetState / SetGadgetItemState", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(#btnActive, 10, 10, 100, 25, "ListIcon1 Active")
  
  ButtonGadget(#btnSet0, 10,  50, 100, 25, "Set Item-0 to 0")
  ButtonGadget(#btnSet1, 10,  80, 100, 25, "Set Item-0 to 1") : GadgetToolTip(#btnSet1, "#PB_ListIcon_Selected = 1")
  ButtonGadget(#btnSet2, 10, 110, 100, 25, "Set Item-0 to 2") : GadgetToolTip(#btnSet2, "#PB_ListIcon_Checked = 2")
  ButtonGadget(#btnSet3, 10, 140, 100, 25, "Set Item-0 to 3") : GadgetToolTip(#btnSet3, "#PB_ListIcon_Checked|#PB_ListIcon_Selected = 3")
  ButtonGadget(#btnSet4, 10, 170, 100, 25, "Set Item-0 to 4") : GadgetToolTip(#btnSet4, "#PB_ListIcon_Inbetween = 4")
  ButtonGadget(#btnSet5, 10, 200, 100, 25, "Set Item-0 to 5") : GadgetToolTip(#btnSet5, "#PB_ListIcon_Inbetween|#PB_ListIcon_Selected = 5")
  
  
  ListIconGadget(#ListIcon1, 120,  10, 270, 100, "AlwaysShowSelection OFF", 200, #PB_ListIcon_CheckBoxes|#PB_ListIcon_ThreeState)
  ListIconGadget(#ListIcon2, 120, 120, 270, 100, "AlwaysShowSelection ON", 200, #PB_ListIcon_CheckBoxes|#PB_ListIcon_ThreeState|#PB_ListIcon_AlwaysShowSelection)
  For a = 0 To 4
    AddGadgetItem (#ListIcon1, -1, "Item " + Str(a) + " of the ListIcon")
    AddGadgetItem (#ListIcon2, -1, "Item " + Str(a) + " of the ListIcon")
  Next
  
  ;SetGadgetState(#ListIcon, 1)
  SetGadgetItemState(#ListIcon1, 2, #PB_ListIcon_Selected)
  SetGadgetItemState(#ListIcon2, 2, #PB_ListIcon_Selected)
  
  Repeat
    wEvent = WaitWindowEvent()
    
    If wEvent = #PB_Event_Gadget
      Select EventGadget()
        Case #btnSet0
          SetGadgetItemState(#ListIcon1, 0, 0)
          SetGadgetItemState(#ListIcon2, 0, 0)
        Case #btnSet1
          SetGadgetItemState(#ListIcon1, 0, 1)  ; #PB_ListIcon_Selected                        = 1
          SetGadgetItemState(#ListIcon2, 0, 1)  ; #PB_ListIcon_Selected                        = 1
        Case #btnSet2
          SetGadgetItemState(#ListIcon1, 0, 2)  ; #PB_ListIcon_Checked                         = 2
          SetGadgetItemState(#ListIcon2, 0, 2)  ; #PB_ListIcon_Checked                         = 2
        Case #btnSet3
          SetGadgetItemState(#ListIcon1, 0, 3)  ; #PB_ListIcon_Checked|#PB_ListIcon_Selected   = 3
          SetGadgetItemState(#ListIcon2, 0, 3)  ; #PB_ListIcon_Checked|#PB_ListIcon_Selected   = 3
        Case #btnSet4
          SetGadgetItemState(#ListIcon1, 0, 4)  ; #PB_ListIcon_Inbetween                       = 4
          SetGadgetItemState(#ListIcon2, 0, 4)  ; #PB_ListIcon_Inbetween                       = 4
        Case #btnSet5
          SetGadgetItemState(#ListIcon1, 0, 5)  ; #PB_ListIcon_Inbetween|#PB_ListIcon_Selected = 5
          SetGadgetItemState(#ListIcon2, 0, 5)  ; #PB_ListIcon_Inbetween|#PB_ListIcon_Selected = 5
          
        Case #btnActive
          SetActiveGadget(#ListIcon1)
          
      EndSelect
    EndIf
  Until wEvent = #PB_Event_CloseWindow
EndIf
ListView however, doesn't have this flag, and the Linux behavior looks correct to me.

Bear with me, need to check how TreeGadget behaves, and I'll also double check ListView.
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

ListViewGadget:

So, here is a new test program for ListViewGadget only.
Apologies for the extra-wide buttons, the captions on Linux need much more space.

Tested on Windows and Linux (3 subsystems).
Windows: definitely does not select an item using SetGadgetItemState() and I think it should, same way as it does on Linux.
Linux: has mostly issues when using Qt. When no item is selected it returns 0 instead of -1, and other things. See the code for comments.
The GTK2/3 warnings could be suppressed I think.

Will check ListIconGadget and TreeGadget in a separate post. It takes time.

Thanks,

Code: Select all

Enumeration
  #btnSetItem0
  #btnSetItem1
  #btnSetItem2
  #txtGetItem1
  #txtGetItem2
  #txtGetItem3
  #txtGetItem4
  #btnGetItem
  #ListView
EndEnumeration

#win = 0
#NumItems = 4

Procedure _UpdateInfo()
  Protected item = GetGadgetState(#ListView)
  SetGadgetText(#txtGetItem2, Str(item))
  SetGadgetText(#txtGetItem3, "GetGadgetItemState(#LV, " + Str(item) + "):")
  SetGadgetText(#txtGetItem4, Str(GetGadgetItemState(#ListView, item)))
EndProcedure

If OpenWindow(#win, 0, 0, 500, 250, "ListViewGadget SetGadgetState / SetGadgetItemState", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ButtonGadget(#btnSetItem0,  10,  10, 220, 25, "SetGadgetState to -1")
  ButtonGadget(#btnSetItem1,  10,  40, 220, 25, "SetGadgetState Random")
  ButtonGadget(#btnSetItem2,  10,  70, 220, 25, "SetGadgetItemState Random")
  
  TextGadget(#txtGetItem1,  240, 10, 220, 25, "GetGadgetState(): ", #PB_Text_Border)
  TextGadget(#txtGetItem2,  460, 10,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  
  TextGadget(#txtGetItem3,  240, 40, 220, 25, "GetGadgetItemState(#LV, -1): ", #PB_Text_Border)
  TextGadget(#txtGetItem4,  460, 40,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  
  ButtonGadget(#btnGetItem, 300, 70, 190, 25, "MsgBox Selected Item...")
  
  ListViewGadget(#ListView,   10, 100, 480, 140)
  For a = 0 To #NumItems
    AddGadgetItem (#ListView, -1, "Item " + Str(a) + " of the ListView")
  Next
  
  SetGadgetState(#ListView, -1)  ; Safe to call, this works okay on Windows and all Linux subsystems
  
  ; *** SetGadgetState() ***
  ; Windows:    Works okay
  ; Linux GTK2: Works okay
  ; Linux GTK3: Works okay
  ; Linux Qt:   Works okay
  
  ; *** SetGadgetItemState() ***
  ; Windows:    Doesn't do anything on Windows.
  ; Linux GTK2: Works okay
  ; Linux GTK3: Works okay
  ; Linux Qt:   Selection works but items look to be 1-based??? 0 = none selected.
  ;             Also, if item is set with this command (SetGadgetItemState()), GetGadgetItemState() keeps returning the state of the last manually selected item.
  
  ; *** GetGadgetState() ***
  ; Windows:    If no item is active: returns -1     Safely returns -1 if called on non-existing item: -1
  ; Linux GTK2: If no item is active: returns -1     Returns 0 and a GTK Warning if called on non-existing item: -1
  ; Linux GTK3: If no item is active: returns -1     Returns 0 and a GTK Warning if called on non-existing item: -1
  ; Linux Qt:   If no item is active: returns 0     <- Qt items are 1-based???
  
  ; *** GetGadgetItemState() ***
  ; Windows:    Works okay, returns 0 for item "-1"
  ; Linux GTK2: Works okay, returns 0 for item "-1" and a WARNING: [WARNING] Gtk (CRITICAL): IA__gtk_tree_model_iter_nth_child: assertion 'n >= 0' failed
  ; Linux GTK3: Works okay, returns 0 for item "-1" and a WARNING: [WARNING] Gtk (CRITICAL): gtk_tree_model_iter_nth_child: assertion 'n >= 0' failed
  ; Linux Qt:   Works okay
  
  Debug "GetGadgetState(#ListView): " + GetGadgetState(#ListView)
  Repeat
    wEvent = WaitWindowEvent()
    
    If wEvent = #PB_Event_Gadget
      Select EventGadget()
        Case #btnSetItem0
          SetGadgetState(#ListView, -1)
          SetActiveGadget(-1)
          _UpdateInfo()
          
        Case #btnSetItem1
          SetGadgetState(#ListView, Random(#NumItems))
          _UpdateInfo()
          
        Case #btnSetItem2
          SetGadgetItemState(#ListView, Random(#NumItems), 1)
          _UpdateInfo()
          
        Case #btnGetItem
          item = GetGadgetState(#ListView)
          msg$ = "GetGadgetState(#ListView): " + Str(item) + #LF$
          msg$ + "GetGadgetItemState(#ListView, "+Str(item)+"): " + Str(GetGadgetItemState(#ListView, item))
          MessageRequester("Selected item", msg$)
          
        Case #ListView
          If EventType() = #PB_EventType_LeftClick
            _UpdateInfo()
          EndIf
      EndSelect
    EndIf
    
  Until wEvent = #PB_Event_CloseWindow
EndIf
Last edited by bal on Tue Dec 23, 2025 4:34 am, edited 2 times in total.
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

ListIconGadget:

On Windows, it seems to be working okay.
It's Linux that makes the active item selected under all 3 subsystems, even if flag #PB_ListIcon_AlwaysShowSelection is not being used.

I'll open a new topic for this under Liunx bugs.

Code: Select all

Enumeration
  #btnActive1
  #btnActive2
  #btnSetItem0
  #btnSetItem1
  #btnSetItem2
  #txtGetItem1
  #txtGetItem2
  #txtGetItem3
  #txtGetItem4
  #txtGetItem11
  #txtGetItem12
  #txtGetItem13
  #txtGetItem14
  #btnMsgBox
  #ListIcon1
  #ListIcon2
EndEnumeration

#win = 0
#NumItems = 4

Procedure _UpdateInfo()
  Protected item = GetGadgetState(#ListIcon1)
  SetGadgetText(#txtGetItem2, Str(item))
  SetGadgetText(#txtGetItem3, "GetGadgetItemState(#ListIcon1, " + Str(item) + "):")
  SetGadgetText(#txtGetItem4, Str(GetGadgetItemState(#ListIcon1, item) & #PB_ListIcon_Selected))
  
  item = GetGadgetState(#ListIcon2)
  SetGadgetText(#txtGetItem12, Str(item))
  SetGadgetText(#txtGetItem13, "GetGadgetItemState(#ListIcon2, " + Str(item) + "):")
  SetGadgetText(#txtGetItem14, Str(GetGadgetItemState(#ListIcon2, item) & #PB_ListIcon_Selected))
EndProcedure

If OpenWindow(#win, 0, 0, 550, 470, "ListIconGadget SetGadgetState / SetGadgetItemState", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ButtonGadget(#btnSetItem0,  10,  10, 230, 25, "SetGadgetState to -1")
  ButtonGadget(#btnSetItem1,  10,  40, 230, 25, "SetGadgetState Random")
  ButtonGadget(#btnSetItem2,  10,  70, 230, 25, "SetGadgetItemState Random")
  
  ButtonGadget(#btnMsgBox,    10, 100, 230, 25, "MsgBox Selected Items...")
  
  TextGadget(#txtGetItem1,  250, 10, 260, 25, "GetGadgetState(#ListIcon1): ", #PB_Text_Border)
  TextGadget(#txtGetItem2,  510, 10,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  TextGadget(#txtGetItem3,  250, 35, 260, 25, "GetGadgetItemState(#ListIcon1, -1): ", #PB_Text_Border)
  TextGadget(#txtGetItem4,  510, 35,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  
  TextGadget(#txtGetItem11,  250, 70, 260, 25, "GetGadgetState(#ListIcon2): ", #PB_Text_Border)
  TextGadget(#txtGetItem12,  510, 70,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  TextGadget(#txtGetItem13,  250, 95, 260, 25, "GetGadgetItemState(#ListIcon2, -1): ", #PB_Text_Border)
  TextGadget(#txtGetItem14,  510, 95,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  
  ButtonGadget(#btnActive1,  10,  130, 150, 25, "Active #ListIcon1")
  ButtonGadget(#btnActive2,  10,  300, 150, 25, "Active #ListIcon2")
  
  ListIconGadget(#ListIcon1, 170, 130, 370, 160, "#ListIcon1: AlwaysShowSelection OFF", 320)
  ListIconGadget(#ListIcon2, 170, 300, 370, 160, "#ListIcon2: AlwaysShowSelection ON", 320, #PB_ListIcon_AlwaysShowSelection)
  
  For a = 0 To #NumItems
    AddGadgetItem (#ListIcon1, -1, "Item " + Str(a) + " of #ListIcon1")
    AddGadgetItem (#ListIcon2, -1, "Item " + Str(a) + " of #ListIcon2  [AlwaysShowSelection ON]")
  Next
  
  _UpdateInfo()
  
  ; *** SetGadgetState() ***
  ; Windows:    Works okay (#PB_ListIcon_AlwaysShowSelection on/off works as expected)
  ; Linux GTK2: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ; Linux GTK3: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ; Linux Qt:   #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  
  ; *** SetGadgetItemState() ***
  ; Windows:    Works okay (#PB_ListIcon_AlwaysShowSelection on/off works as expected)
  ; Linux GTK2: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ; Linux GTK3: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ;             Also, it seems at program start item-0 is "active" but not hightlighted:
  ;               click on "Active #ListIcon1" or "Active #ListIcon2" after program start to see that they are "active".
  ; Linux Qt:   #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  
  ; *** GetGadgetState() ***
  ; Windows:    Works okay: if no item is active: returns -1
  ; Linux GTK2: Works okay: if no item is active: returns -1
  ; Linux GTK3: Works okay: if no item is active: returns -1
  ; Linux Qt:   Works okay: if no item is active: returns -1
  
  ; *** GetGadgetItemState() ***
  ; Windows:    Works okay, querying item "-1" returns 0
  ; Linux GTK2: Works okay, querying item "-1" returns 0 and a WARNING: [WARNING] Gtk (CRITICAL): IA__gtk_tree_model_iter_nth_child: assertion 'n >= 0' failed
  ; Linux GTK3: Works okay, querying item "-1" returns 0 and a WARNING: [WARNING] Gtk (CRITICAL): gtk_tree_model_iter_nth_child: assertion 'n >= 0' failed
  ; Linux Qt:   Works okay, querying item "-1" returns 0
  
  
  Repeat
    wEvent = WaitWindowEvent()
    
    If wEvent = #PB_Event_Gadget
      Select EventGadget()
        Case #btnActive1
          SetActiveGadget(#ListIcon1)
        Case #btnActive2
          SetActiveGadget(#ListIcon2)
        
        Case #btnSetItem0
          SetGadgetState(#ListIcon1, -1)
          SetGadgetState(#ListIcon2, -1)
          SetActiveGadget(-1)
          _UpdateInfo()
          
        Case #btnSetItem1
          item = Random(#NumItems)
          SetGadgetState(#ListIcon1, item)
          SetGadgetState(#ListIcon2, item)
          _UpdateInfo()
          
        Case #btnSetItem2
          item = Random(#NumItems)
          SetGadgetItemState(#ListIcon1, item, 1)
          SetGadgetItemState(#ListIcon2, item, 1)
          _UpdateInfo()
          
        Case #btnMsgBox
         item = GetGadgetState(#ListIcon1)
         msg$ = "GetGadgetState(#ListIcon1): " + Str(item) + #LF$
         msg$ + "GetGadgetItemState(#ListIcon1, "+Str(item)+"): " + Str(GetGadgetItemState(#ListIcon1, item) & #PB_ListIcon_Selected) + #LF$
         msg$ + #LF$
         item = GetGadgetState(#ListIcon2)
         msg$ + "GetGadgetState(#ListIcon2): " + Str(item) + #LF$
         msg$ + "GetGadgetItemState(#ListIcon2, "+Str(item)+"): " + Str(GetGadgetItemState(#ListIcon1, item) & #PB_ListIcon_Selected)
         MessageRequester("Selected items", msg$)
          
        Case #ListIcon1
          If EventType() = #PB_EventType_LeftClick
            _UpdateInfo()
          EndIf
        Case #ListIcon2
          If EventType() = #PB_EventType_LeftClick
            _UpdateInfo()
          EndIf
        
      EndSelect
    EndIf
    
  Until wEvent = #PB_Event_CloseWindow
EndIf
Last edited by bal on Tue Dec 23, 2025 4:34 am, edited 2 times in total.
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

TreeGadget:

On Windows: selected item is not highlighted, even if flag #PB_ListIcon_AlwaysShowSelection is used
On Linux: selected item is always highlighted, even if flag #PB_ListIcon_AlwaysShowSelection is not used

Code: Select all

Enumeration
  #btnActive1
  #btnActive2
  #btnSetItem0
  #btnSetItem1
  #btnSetItem2
  #txtGetItem1
  #txtGetItem2
  #txtGetItem3
  #txtGetItem4
  #txtGetItem11
  #txtGetItem12
  #txtGetItem13
  #txtGetItem14
  #btnMsgBox
  #Tree1
  #Tree2
EndEnumeration

#win = 0
#NumItems = 4

Procedure _UpdateInfo()
  Protected item = GetGadgetState(#Tree1)
  SetGadgetText(#txtGetItem2, Str(item))
  SetGadgetText(#txtGetItem3, "GetGadgetItemState(#Tree1, " + Str(item) + "):")
  SetGadgetText(#txtGetItem4, Str( GetGadgetItemState(#Tree1, item) & #PB_Tree_Selected))
  
  item = GetGadgetState(#Tree2)
  SetGadgetText(#txtGetItem12, Str(item))
  SetGadgetText(#txtGetItem13, "GetGadgetItemState(#Tree2, " + Str(item) + "):")
  SetGadgetText(#txtGetItem14, Str(GetGadgetItemState(#Tree2, item) & #PB_Tree_Selected))
EndProcedure

If OpenWindow(#win, 0, 0, 550, 470, "TreeGadget SetGadgetState / SetGadgetItemState", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ButtonGadget(#btnSetItem0,  10,  10, 230, 25, "SetGadgetState to -1")
  ButtonGadget(#btnSetItem1,  10,  40, 230, 25, "SetGadgetState Random")
  ButtonGadget(#btnSetItem2,  10,  70, 230, 25, "SetGadgetItemState Random")
  
  ButtonGadget(#btnMsgBox,    10, 100, 230, 25, "MsgBox Selected Items...")
  
  TextGadget(#txtGetItem1,  250, 10, 260, 25, "GetGadgetState(#Tree1): ", #PB_Text_Border)
  TextGadget(#txtGetItem2,  510, 10,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  TextGadget(#txtGetItem3,  250, 35, 260, 25, "GetGadgetItemState(#Tree1, -1): ", #PB_Text_Border)
  TextGadget(#txtGetItem4,  510, 35,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  
  TextGadget(#txtGetItem11,  250, 70, 260, 25, "GetGadgetState(#Tree2): ", #PB_Text_Border)
  TextGadget(#txtGetItem12,  510, 70,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  TextGadget(#txtGetItem13,  250, 95, 260, 25, "GetGadgetItemState(#Tree2, -1): ", #PB_Text_Border)
  TextGadget(#txtGetItem14,  510, 95,  30, 25, "x", #PB_Text_Border|#PB_Text_Center)
  
  ButtonGadget(#btnActive1,  10,  130, 150, 25, "Active #Tree1")
  ButtonGadget(#btnActive2,  10,  300, 150, 25, "Active #Tree2")
  
  TreeGadget(#Tree1, 170, 130, 370, 160)
  TreeGadget(#Tree2, 170, 300, 370, 160, #PB_ListIcon_AlwaysShowSelection)
  
  For a = 0 To #NumItems
    AddGadgetItem (#Tree1, -1, "Item " + Str(a) + " of #Tree1")
    AddGadgetItem (#Tree2, -1, "Item " + Str(a) + " of #Tree2  [AlwaysShowSelection ON]")
  Next
  
  _UpdateInfo()
  
  ; *** SetGadgetState() ***
  ; Windows:    Selection works, but selected item is NOT HIGHLIGHTED, even if using flag #PB_ListIcon_AlwaysShowSelection
  ; Linux GTK2: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ; Linux GTK3: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ; Linux Qt:   #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  
  ; *** SetGadgetItemState() ***
  ; Windows:    Doesn't select or highlight an item on Windows.
  ;               BUT: keep clicking on "SetGadgetItemState Random" and sometimes the last item selected by SetGadgetState() will be selected (but never highlighted).
  ; Linux GTK2: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ; Linux GTK3: #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  ;               Also, it seems at program start item-0 is "active" but not hightlighted:
  ;               click on "Active #ListIcon1" or "Active #ListIcon2" after program start to see that they are "active".
  ; Linux Qt:   #PB_ListIcon_AlwaysShowSelection seems to be ALWAYS ON. Selected item is always hightlighted.
  
  ; *** GetGadgetState() ***
  ; Windows:    Works okay: if no item is active: returns -1
  ; Linux GTK2: Works okay: if no item is active: returns -1
  ; Linux GTK3: Works okay: if no item is active: returns -1
  ; Linux Qt:   Works okay: if no item is active: returns -1
  
  ; *** GetGadgetItemState() ***
  ; Windows:    Works okay, querying item "-1" returns 0
  ; Linux GTK2: Works okay, querying item "-1" returns 0
  ; Linux GTK3: Works okay, querying item "-1" returns 0
  ; Linux Qt:   Works okay, querying item "-1" returns 0
  
  
  Repeat
    wEvent = WaitWindowEvent()
    
    If wEvent = #PB_Event_Gadget
      Select EventGadget()
        Case #btnActive1
          SetActiveGadget(#Tree1)
        Case #btnActive2
          SetActiveGadget(#Tree2)
        
        Case #btnSetItem0
          SetGadgetState(#Tree1, -1)
          SetGadgetState(#Tree2, -1)
          SetActiveGadget(-1)
          _UpdateInfo()
          
        Case #btnSetItem1
          item = Random(#NumItems)
          SetGadgetState(#Tree1, item)
          SetGadgetState(#Tree2, item)
          _UpdateInfo()
          
        Case #btnSetItem2
          item = Random(#NumItems)
          SetGadgetItemState(#Tree1, item, 1)
          SetGadgetItemState(#Tree2, item, 1)
          _UpdateInfo()
          
        Case #btnMsgBox
         item = GetGadgetState(#Tree1)
         msg$ = "GetGadgetState(#Tree1): " + Str(item) + #LF$
         msg$ + "GetGadgetItemState(#Tree1, "+Str(item)+"): " + Str( GetGadgetItemState(#Tree1, item) & #PB_Tree_Selected) + #LF$
         msg$ + #LF$
         item = GetGadgetState(#Tree2)
         msg$ + "GetGadgetState(#Tree2): " + Str(item) + #LF$
         msg$ + "GetGadgetItemState(#Tree2, "+Str(item)+"): " + Str( GetGadgetItemState(#Tree1, item) & #PB_Tree_Selected)
         MessageRequester("Selected items", msg$)
          
        Case #Tree1
          If EventType() = #PB_EventType_LeftClick
            _UpdateInfo()
          EndIf
        Case #Tree2
          If EventType() = #PB_EventType_LeftClick
            _UpdateInfo()
          EndIf
        
      EndSelect
    EndIf
    
  Until wEvent = #PB_Event_CloseWindow
EndIf
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

Please take a look at the above 3 posts. Item selection doesn't work on ListViewGadget, ListIconGadget and TreeGadget as it should.
There is a more elaborate example for each gadget type.
Tested on Windows and Linux GTK2/GTK3/Qt.
Thank you.
User avatar
Bisonte
Addict
Addict
Posts: 1329
Joined: Tue Oct 09, 2007 2:15 am

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by Bisonte »

I'll gladly say it again. Get/SetGadgetItemState() is not intended for visibly selecting an item.
It is there to query or set the status of an item. On Listview for multiselect, on ListIcon for
multiselect and checkbox, and on Tree for the checkboxes and whether it is expanded or collapsed.

Look at Checkbox ... you see there the same Flags ...

If your statement about a bug were correct, several people would have noticed this since 4.xx,
including me, as I use it very frequently, for example. ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
bal
User
User
Posts: 18
Joined: Fri Sep 03, 2010 6:15 pm

Re: Issue with SetGadgetState() and SetGadgetItemState() on Windows

Post by bal »

Bisonte wrote: Tue Dec 23, 2025 7:49 am I'll gladly say it again. Get/SetGadgetItemState() is not intended for visibly selecting an item.
It is there to query or set the status of an item. On Listview for multiselect, on ListIcon for
multiselect and checkbox, and on Tree for the checkboxes and whether it is expanded or collapsed.

Look at Checkbox ... you see there the same Flags ...

If your statement about a bug were correct, several people would have noticed this since 4.xx,
including me, as I use it very frequently, for example. ;)
Have you tried the examples on Linux, please?

Even the documentation mentions (on TreeGadget only), that the flags can be / need to be combined.
By combining the flags the item can be put in any state using SeteGadgetItemState() only.
Otherwise we should use SetGadgetState() to select the item first, then SetGadgetItemState() to set the desired state of the checkbox. But that's not the case.

Using only Set/GetGadgetItemState() any item state can be set and checked by combining the flags.

In either case, Windows and Linux versions of these commands don't work the same way. One of them needs to be fixed.

This is how items are set with SetGadgetItemState():

Code: Select all

Enumeration
  #btnSetState0
  #btnSetState1
  #btnSetState2
  #btnSetState3
  #btnSetState4
  #btnSetState5
  #ListIcon
EndEnumeration

#win = 0
#NumItems = 4

If OpenWindow(#win, 0, 0, 550, 200, "ListIconGadget SetGadgetItemState()", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ButtonGadget(#btnSetState0,  10,  10, 230, 25, "None")
  ButtonGadget(#btnSetState1,  10,  40, 230, 25, "#PB_ListIcon_Selected")
  ButtonGadget(#btnSetState2,  10,  70, 230, 25, "#PB_ListIcon_Checked")
  ButtonGadget(#btnSetState3,  10, 100, 230, 25, "#PB_ListIcon_Selected | Checked")
  ButtonGadget(#btnSetState4,  10, 130, 230, 25, "#PB_ListIcon_Inbetween")
  ButtonGadget(#btnSetState5,  10, 160, 230, 25, "#PB_ListIcon_Selected | Inbetween")
  
  ListIconGadget(#ListIcon, 250, 10, 290, 170, "#ListIcon", 270, #PB_ListIcon_AlwaysShowSelection|#PB_ListIcon_CheckBoxes|#PB_ListIcon_ThreeState)
  
  For a = 0 To #NumItems
    AddGadgetItem (#ListIcon, -1, "Item " + Str(a) + " of #ListIcon")
  Next
  
  Repeat
    wEvent = WaitWindowEvent()
    
    If wEvent = #PB_Event_Gadget
      Select EventGadget()
        
        Case #btnSetState0
          SetGadgetItemState(#ListIcon, 1, 0)
          SetActiveGadget(#ListIcon)
        Case #btnSetState1
          SetGadgetItemState(#ListIcon, 1, #PB_ListIcon_Selected)  ; #PB_ListIcon_Selected = 1
          SetActiveGadget(#ListIcon)
        Case #btnSetState2
          SetGadgetItemState(#ListIcon, 1, #PB_ListIcon_Checked)   ; #PB_ListIcon_Checked = 2
          SetActiveGadget(#ListIcon)
        Case #btnSetState3
          SetGadgetItemState(#ListIcon, 1, #PB_ListIcon_Selected|#PB_ListIcon_Checked)  ; 1 + 2 = 3
          SetActiveGadget(#ListIcon)
        Case #btnSetState4
          SetGadgetItemState(#ListIcon, 1, #PB_ListIcon_Inbetween)  ; #PB_ListIcon_Inbetween = 4
          SetActiveGadget(#ListIcon)
        Case #btnSetState5
          SetGadgetItemState(#ListIcon, 1, #PB_ListIcon_Selected|#PB_ListIcon_Inbetween)  ; 1 + 4 = 5
          SetActiveGadget(#ListIcon)
        
      EndSelect
    EndIf
    
  Until wEvent = #PB_Event_CloseWindow
EndIf
Post Reply