Programatically set user's selection in ExplorerListGadget?

Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Programatically set user's selection in ExplorerListGadget?

Post by Quin »

My app uses an ExplorerListGadget, and I want to be able to programatically set the user's position to a certain position (e.g. SetGadgetState()). according to the docs and testing it myself though, SetGadgetState doesn't work on ExplorerListGadget. Is there some other way to do this?
Thanks.
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Programatically set user's selection in ExplorerListGadget?

Post by infratec »

SetGadgetState() is not listed in the help for ExplorerListGadget().

But you can select items with SetGadgetItemState() and #PB_Explorer_Selected.

The problem is, that you can not set it immediately after the ExplorerListGadget() call.
It needs a bit time of loading.

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "ExplorerListGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ExplorerListGadget(0, 10, 10, 380, 180, "*.*", #PB_Explorer_MultiSelect|#PB_Explorer_AlwaysShowSelection)
  
  SetGadgetItemState(0, 1, #PB_Explorer_Selected) ; not working, to fast
  
  Repeat
    Event = WaitWindowEvent()
    If Event = #PB_Event_Gadget
      If EventGadget() = 0
        If EventType() = #PB_EventType_Change
          Debug GetGadgetState(0)
          SetGadgetItemState(0, 1, #PB_Explorer_Selected) ; works, because the gadget is filled at this point
        EndIf
      EndIf
    EndIf
  Until Event = #PB_Event_CloseWindow
EndIf
Last edited by infratec on Sat Aug 17, 2024 6:16 pm, edited 2 times in total.
PeDe
Enthusiast
Enthusiast
Posts: 305
Joined: Sun Nov 26, 2017 3:13 pm

Re: Programatically set user's selection in ExplorerListGadget?

Post by PeDe »

With SetGadgetItemState() and Status #PB_Explorer_Selected you can select an entry in the list.
Perhaps the #PB_Explorer_AlwaysShowSelection flag with ExplorerListGadget() is also helpful.

Peter

Code: Select all

If OpenWindow(0, 0, 0, 400, 200, "ExplorerListGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ExplorerListGadget(0, 10, 10, 380, 180, "*.*", #PB_Explorer_AlwaysShowSelection)
  SetGadgetItemState(0, 2, #PB_Explorer_Selected)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Programatically set user's selection in ExplorerListGadget?

Post by Quin »

Thanks you both! Infratec is indeed correct, this doesn't seem to work until it's fully populated.
Can I tell when the gadget has fully populated and do it only then? My goal here is to try and work around the current behavior with ExplorerListGadget and screen readers (e.g. it doesn't say anything when changing directories), and I wanted to make it select and read the first item by default like a lot of file explorers do.
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Programatically set user's selection in ExplorerListGadget?

Post by Quin »

Update: SetGadgetItemState does seem to work right after creation of all my gadgets actually, so the first part of my problem is solved! Now I just need to make this logic apply when going into and out of folders...
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Programatically set user's selection in ExplorerListGadget?

Post by Quin »

Hi all
I'm back at this again. I'm trying this to both make the selection correct when I first create the gadget, but also whenever the user changes directories. However, I seem to be capturing the event too early, because whenever I press enter, it selects the first item in the list, for some reason? Any advice here would be appreciated!

Code: Select all

EnableExplicit

Enumeration Windows
#Window_Main
EndEnumeration

Enumeration Gadgets
#Gadget_ExplorerLabel
#Gadget_ExplorerList
EndEnumeration

Procedure GadgetEvents()
Select EventGadget()
Case #Gadget_ExplorerList
If EventType() = #PB_EventType_Change And GetGadgetState(#Gadget_ExplorerList) = -1
SetGadgetItemState(#Gadget_ExplorerList, 0, #PB_Explorer_Selected)
EndIf
EndSelect
EndProcedure

OpenWindow(#Window_Main, #PB_Ignore, #PB_Ignore, 450, 300, "Explorer test")
TextGadget(#Gadget_ExplorerLabel, 5, 5, 30, 5, "Files")
ExplorerListGadget(#Gadget_ExplorerList, 5, 15, 400, 250, "")
SetActiveGadget(#Gadget_ExplorerList)
SetGadgetItemState(#Gadget_ExplorerList, 0, #PB_Explorer_Selected)
BindEvent(#PB_Event_Gadget, @GadgetEvents())
Repeat : Until WaitWindowEvent(1) = #PB_Event_CloseWindow
Thanks!
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Programatically set user's selection in ExplorerListGadget?

Post by RASHAD »

Hi
Author Shardik :wink:

Code: Select all

EnableExplicit

Procedure SelectExplorerListRow(ExplorerListID.I, RowIndex.I)
  If RowIndex < CountGadgetItems(ExplorerListID)
    SetActiveGadget(ExplorerListID)

    CompilerSelect #PB_Compiler_OS
      CompilerCase #PB_OS_Linux
        Protected TreePath.I = gtk_tree_path_new_from_indices_(RowIndex, -1)
        gtk_tree_view_set_cursor_(GadgetID(ExplorerListID), TreePath, 0, #False)
      CompilerCase #PB_OS_MacOS
        Protected IndexSet.I
        IndexSet = CocoaMessage(0, CocoaMessage(0, 0, "NSIndexSet alloc"),
          "initWithIndex:", RowIndex)
        CocoaMessage(0, GadgetID(ExplorerListID),
          "selectRowIndexes:", IndexSet,
          "byExtendingSelection:", #NO)
        CocoaMessage(0, IndexSet, "release")
      CompilerCase #PB_OS_Windows
        Protected Item.LVITEM
        Item\mask = #LVIF_STATE
        Item\state = #LVIS_SELECTED
        Item\stateMask = #LVIS_SELECTED
        SendMessage_(GadgetID(ExplorerListID), #LVM_SETITEMSTATE, RowIndex, @Item)
    CompilerEndSelect
  EndIf
EndProcedure

OpenWindow(0, 270, 100, 400, 200, "ExplorerListGadget")
; ---- Attention: The flag #PB_Explorer_AlwaysShowSelection is necessary to
;      see a selected entry in Windows OS
ExplorerListGadget(0, 10, 10, 380, 180, "/", #PB_Explorer_AlwaysShowSelection)
SelectExplorerListRow(0, 1)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Egypt my love
Quin
Addict
Addict
Posts: 1135
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Programatically set user's selection in ExplorerListGadget?

Post by Quin »

Hi RASHAD,
That's a neat trick, but it sadly doesn't do what I want, I already pretty much have this functionality on Windows at least with SetGadgetItemState(). The problem I'm hitting now is when trying to automatically set the user's selection to the first item in the list of files every time they change directories, thus avoiding this weird accessibility problem where changing directories just says a random folder name :mrgreen:
Post Reply