Listview Multiselect GetGadgetText()

Just starting out? Need help? Post your questions and find answers here.
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Listview Multiselect GetGadgetText()

Post by Shardik »

The code example below is true cross-platform while using the fast platform-specific API functions of Linux, MacOS X and Windows to get the indices of all selected rows in a ListViewGadget without having to iterate over each row when using solely PureBasic functions.

I have tested the example successfully on these operating systems:
- MacOS X 10.6.8 (Snow Leopard) with PB 5.41 x86 and x64
- Ubuntu 14.04 x64 with KDE and PB 5.41 x64
- Windows 7 SP1 with PB 5.41 x86 and x64

Code: Select all

EnableExplicit

#RowCount = 10

; ----- Create an array that will contain the indices of the selected items

CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
  Dim RowIndex.I(#RowCount - 1) ; Has to be Long in x86 and Quad in x64!
CompilerElse
  Dim RowIndex.L(#RowCount - 1)
CompilerEndIf

Procedure.I GetSelectedRows(GadgetID.I)
  Protected SelectedRowsCount.I

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Shared RowIndex.L()

      Protected i.I
      Protected IndexList.I
      Protected Path.I
      Protected Selection.I

      Selection = gtk_tree_view_get_selection_(GadgetID(GadgetID))

      If Selection
        IndexList = gtk_tree_selection_get_selected_rows_(Selection, 0)

        If IndexList
          SelectedRowsCount = g_list_length_(IndexList)

          For i = 0 To SelectedRowsCount - 1
            Path = g_list_nth_data_(IndexList, i)
            RowIndex(i) = PeekI(gtk_tree_path_get_indices_(Path))
          Next i

          g_list_free_(IndexList)
        EndIf
      EndIf
    CompilerCase #PB_OS_MacOS
      Shared RowIndex.I()

      Protected IndexSet.I

      IndexSet = CocoaMessage(0, GadgetID(GadgetID), "selectedRowIndexes")
      SelectedRowsCount = CocoaMessage(0, IndexSet,
        "getIndexes:", @RowIndex(),
        "maxCount:", ArraySize(RowIndex()) + 1,
        "inIndexRange:", 0)
    CompilerCase #PB_OS_Windows
      Shared RowIndex.L()

      SelectedRowsCount = SendMessage_(GadgetID(0), #LB_GETSELCOUNT, 0, 0)

      If SelectedRowsCount > 0
        SendMessage_(GadgetID(GadgetID), #LB_GETSELITEMS, SelectedRowsCount,
          @RowIndex())
      EndIf
  CompilerEndSelect

  ProcedureReturn SelectedRowsCount
EndProcedure

Define i.I
Define SelectedRowsCount.I

OpenWindow(0, 100, 100, 270, 195, "Detect multiselected items")
ListViewGadget(0, 10, 10, 250, 135, #PB_ListView_MultiSelect)
ButtonGadget(1, 60, 155, 140, 25, "List selected items")

For i = 1 To #RowCount
  AddGadgetItem (0, -1, "Row " + Str(i))
Next

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 1
        SelectedRowsCount = GetSelectedRows(0)
        Debug "Number of selected rows: " + SelectedRowsCount

        If SelectedRowsCount > 0
          For i = 0 To SelectedRowsCount - 1
            Debug RowIndex(i) + 1
          Next i
        EndIf
      EndIf
  EndSelect
ForEver
mark5009
User
User
Posts: 22
Joined: Sun Oct 19, 2014 10:47 pm

Re: Listview Multiselect GetGadgetText()

Post by mark5009 »

Shardik wrote:
mark5009 wrote:I am using OSX
If you would have posted your OS in your signature, I would have posted the API code for OS X... :wink:

I have converted my example from above using Windows API functions to an example using the necessary MacOS API functions.
Wow! Many thanks, Shardik. Fantastic stuff.

.. mark.
Post Reply