Page 1 of 1

Display double-clicked row at top of list in ListIconGadget

Posted: Thu Dec 26, 2013 12:04 pm
by Shardik
On request of a user of this forum I have put together the following cross-platform example code which demonstrates how to move a double-clicked row to the top of a list in a ListIconGadget. I have tested it successfully with PB 5.21 on these operating systems:
- MacOS 10.6.8 (Snow Leopard) x86 and x64
- Ubuntu 12.04 LTS x64 with KDE
- Windows XP SP3
- Windows 7 SP1 x64

Code: Select all

EnableExplicit

CompilerSelect #PB_Compiler_OS
  CompilerCase #PB_OS_Linux
    ImportC ""
      gtk_tree_view_scroll_to_cell(*TreeView.GtkTreeView, *TreePath,
        *TreeColumn.GtkTreeViewColumn, UseAlign.I, RowAlign.F, ColumnAlign.F)
    EndImport
    
    Procedure MoveSelectedRowToTop(ListIconID.I)
      Protected FocusColumn.I
      Protected TreePath.I
      
      gtk_tree_view_get_cursor_(GadgetID(ListIconID), @TreePath, @FocusColumn)
      gtk_tree_view_scroll_to_cell(GadgetID(ListIconID), TreePath, 0, #True,
        0, 0)
    EndProcedure
  CompilerCase #PB_OS_MacOS
    Procedure.I GetVisibleRows(ListIconID.I)
      Protected ContentView.I
      Protected EnclosingScrollView.I
      Protected VisibleRange.NSRange
      Protected VisibleRect.NSRect
      
      ; ----- Get scroll view inside of ListIconGadget
      EnclosingScrollView = CocoaMessage(0, GadgetID(ListIconID),
        "enclosingScrollView")
      
      If EnclosingScrollView
        ContentView = CocoaMessage(0, EnclosingScrollView, "contentView")
        ; ----- Get visible area
        ;       (automatically subtract horizontal scrollbar if shown) 
        CocoaMessage(@VisibleRect, ContentView, "documentVisibleRect")
        ; ----- Subtract border width
        If CocoaMessage(0, EnclosingScrollView, "borderType") > 0
          VisibleRect\size\height - 5
        EndIf
        ; ----- Get number of rows visible
        CocoaMessage(@VisibleRange, GadgetID(ListIconID), "rowsInRect:@",
          @VisibleRect)
        ProcedureReturn Int(VisibleRange\length)
      EndIf
    EndProcedure
    
    Procedure MoveSelectedRowToTop(ListIconID.I)
      Protected LastVisibleRow.I
      Protected RowTotal.I
      Protected SelectedRow.I
      Protected VisibleRows.I

      SelectedRow = GetGadgetState(ListIconID)
      VisibleRows = GetVisibleRows(ListIconID)
      LastVisibleRow = SelectedRow + VisibleRows - 1
      RowTotal = CountGadgetItems(ListIconID)

      If LastVisibleRow > RowTotal - 1
        LastVisibleRow = RowTotal - 1
      EndIf

      ; ----- Display last visible row with selected row at top
      CocoaMessage(0, GadgetID(ListIconID), "scrollRowToVisible:",
        LastVisibleRow)
      ; ----- Ensure that the selected row at top is fully visible
      CocoaMessage(0, GadgetID(ListIconID), "scrollRowToVisible:",
        SelectedRow)
    EndProcedure
  CompilerCase #PB_OS_Windows
    Procedure MoveSelectedRowToTop(ListIconID.I)
      RowHeight = SendMessage_(GadgetID(ListIconID), #LVM_GETITEMSPACING,
        #True, 0) >> 16
      SelectedRow = GetGadgetState(ListIconID)
      SetGadgetState(ListIconID, 0)
      SendMessage_(GadgetID(ListIconID), #LVM_SCROLL, 0,
        SelectedRow * RowHeight)
    EndProcedure
CompilerEndSelect

OpenWindow(0, 270, 100, 290, 200, "Move double-clicked line to top")
ListIconGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20, "Item",
  WindowWidth(0) - 41)

Define i.I

For i= 1 To 100
  AddGadgetItem(0, -1, "Line " + Str(i))
Next i

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_LeftDoubleClick
        MoveSelectedRowToTop(0)
      EndIf
  EndSelect
ForEver

Re: Display double-clicked row at top of list in ListIconGad

Posted: Thu Dec 26, 2013 3:47 pm
by Kwai chang caine
Works great
Thanks for sharing 8)

Re: Display double-clicked row at top of list in ListIconGad

Posted: Thu Dec 26, 2013 7:01 pm
by davido
Instructive.

Thank you for sharing. :D