Page 1 of 1

ListIconGadget Scroll Left and Right with Keyboard (Solution)

Posted: Fri Aug 25, 2023 3:13 pm
by mk-soft
After requesting a solution to move the columns of ListIconGadget with the left and right keys.

Update v1.01.6

Code: Select all

;-TOP by mk-soft; v1.01.6; 29.08.2023

; Link: https://www.purebasic.fr/english/viewtopic.php?t=82305

#ProgramTitle = "ListIconGadget with Left and Right Keys"
#ProgramVersion = "v1.01"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
  #MainMenuKeyLeft
  #MainMenuKeyRight
EndEnumeration

Enumeration Gadgets
  #MainList
  #MainEdit
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

; ----

Procedure FillList(Gadget)
  Protected txt.s, i, c
  RemoveGadgetColumn(Gadget, #PB_All)
  ; Add columns
  AddGadgetColumn(Gadget, 0, "Column 1", 200)
  AddGadgetColumn(Gadget, 1, "Column 2", 200)
  AddGadgetColumn(Gadget, 2, "Column 3", 200)
  AddGadgetColumn(Gadget, 3, "Column 4", 200)
  AddGadgetColumn(Gadget, 4, "Column 5", 200)
  AddGadgetColumn(Gadget, 5, "Column 6", 200)
  AddGadgetColumn(Gadget, 6, "Column 7", 200)
  AddGadgetColumn(Gadget, 7, "Column 8", 200)
  ; Add Items
  For i = 1 To 1000
    txt = ""
    For c = 1 To 8
      txt + "Item " + Str(i) + "." + Str(c) + #LF$
    Next
    AddGadgetItem(Gadget, -1, txt)
  Next  
EndProcedure

; ----

Procedure DoGadgetEventListIcon()
  Static gadget, lastgadget = -1, IsSetKey
  gadget = EventGadget()
  If gadget <> lastgadget
    lastgadget = gadget
    If GadgetType(gadget) = #PB_GadgetType_ListIcon
      If Not IsSetKey
        IsSetKey = #True
        AddKeyboardShortcut(#Main, #PB_Shortcut_Left, #MainMenuKeyLeft)
        AddKeyboardShortcut(#Main, #PB_Shortcut_Right, #MainMenuKeyRight)
      EndIf
    Else
      If IsSetKey
        IsSetKey = #False
        RemoveKeyboardShortcut(#Main, #PB_Shortcut_Left)
        RemoveKeyboardShortcut(#Main, #PB_Shortcut_Right)
      EndIf
    EndIf  
  EndIf
EndProcedure

; ----
Procedure DoMenuEventListIcon()
  Protected Rect.NSRect, Point.NSPoint, gadget, nslistview, nsheaderview, column, height.d
    
  Select EventMenu()
    Case #MainMenuKeyLeft
      gadget = GetActiveGadget()
      If GadgetType(gadget) = #PB_GadgetType_ListIcon
        nslistview = GadgetID(gadget)
        CocoaMessage(Rect, nslistview, "visibleRect")
        Point\x = Rect\origin\x
        Point\y = Rect\origin\y
        column = CocoaMessage(0, nslistview, "columnAtPoint:@", Point)
        CocoaMessage(Rect, nslistview, "rectOfColumn:", column)
        If column > 0 And Point\x <= (Rect\origin\x + 10) 
          column - 1
        EndIf
        CocoaMessage(Rect, nslistview, "rectOfColumn:", column)
        Point\x = Rect\origin\x
        If Point\x < 0.0
          Point\x = 0.0
        EndIf
        CocoaMessage(@height, nslistview, "rowHeight")
        If Point\y = 0.0
          nsheaderview = CocoaMessage(0, nslistview, "headerView")
          CocoaMessage(Rect, nsheaderview, "headerRectOfColumn:", 0)
          Point\y - Rect\size\height
        EndIf
        CocoaMessage(0, nslistview, "scrollPoint:@", Point)
      EndIf
        
    Case #MainMenuKeyRight
      gadget = GetActiveGadget()
      If GadgetType(gadget) = #PB_GadgetType_ListIcon
        nslistview = GadgetID(gadget)
        CocoaMessage(Rect, nslistview, "visibleRect")
        Point\x = Rect\origin\x
        Point\y = Rect\origin\y
        column = CocoaMessage(0, nslistview, "columnAtPoint:@", Point)
        If column < GetGadgetAttribute(gadget, #PB_ListIcon_ColumnCount)
          column + 1
        EndIf
        CocoaMessage(Rect, nslistview, "rectOfColumn:", column)
        Point\x = Rect\origin\x
        If Point\y = 0.0
          nsheaderview = CocoaMessage(0, nslistview, "headerView")
          CocoaMessage(Rect, nsheaderview, "headerRectOfColumn:", 0)
          point\y - Rect\size\height
        EndIf
        CocoaMessage(0, nslistview, "scrollPoint:@", Point)
      EndIf
      
  EndSelect
EndProcedure
      
; ----

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainList, 0, 0, dx, dy - 100)
  ResizeGadget(#MainEdit, 0, dy - 100, dx, 100)
  
EndProcedure

Procedure Main()
  Protected dx, dy
  
  #MainStyle = #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_MinimizeGadget
  
  If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 800, 600, #ProgramTitle , #MainStyle)
    ; Menu
    CreateMenu(#MainMenu, WindowID(#Main))
    MenuTitle("&File")
    CompilerIf #PB_Compiler_OS = #PB_OS_MacOS
      MenuItem(#PB_Menu_About, "")
    CompilerElse
      MenuItem(#MainMenuAbout, "About")
    CompilerEndIf
    ; Menu File Items
    
    CompilerIf Not #PB_Compiler_OS = #PB_OS_MacOS
      MenuBar()
      MenuItem(#MainMenuExit, "E&xit")
    CompilerEndIf
    
    ; StatusBar
    CreateStatusBar(#MainStatusBar, WindowID(#Main))
    AddStatusBarField(#PB_Ignore)
    
    ; Gadgets
    dx = WindowWidth(#Main)
    dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
    ListIconGadget(#MainList, 0, 0, dx, dy - 100, "", 0)
    EditorGadget(#MainEdit, 0, dy - 100, dx, 100)
    FillList(#MainList)
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    ; Bind Events - ListIconGadget
    BindEvent(#PB_Event_Gadget, @DoGadgetEventListIcon())
    BindEvent(#PB_Event_Menu, @DoMenuEventListIcon())
    
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Select EventWindow()
            Case #Main
              Break
              
          EndSelect
          
        Case #PB_Event_Menu
          Select EventMenu()
            CompilerIf #PB_Compiler_OS = #PB_OS_MacOS   
              Case #PB_Menu_About
                PostEvent(#PB_Event_Menu, #Main, #MainMenuAbout)
                
              Case #PB_Menu_Preferences
                
              Case #PB_Menu_Quit
                PostEvent(#PB_Event_CloseWindow, #Main, #Null)
                
            CompilerEndIf
              
            Case #MainMenuExit
              PostEvent(#PB_Event_CloseWindow, #Main, #Null)
              
            Case #MainMenuAbout
              MessageRequester("About", #ProgramTitle + #LF$ + #ProgramVersion, #PB_MessageRequester_Info)
              
          EndSelect
          
          
        Case #PB_Event_Gadget
          Select EventGadget()
            Case #MainList
              Select EventType()
                Case #PB_EventType_Change
                  ;
              EndSelect
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()

Re: Solution ListIconGadget Scroll Left and Right with Keyboard

Posted: Fri Aug 25, 2023 3:33 pm
by DayDreamer
Beautiful!

Re: Solution ListIconGadget Scroll Left and Right with Keyboard

Posted: Sat Aug 26, 2023 9:06 pm
by Piero
You are too gentle/helpful mk:
I will ask you something futile that will make you go to psychiatric hospital to solve, in the near future.
Be afraid.

Re: Solution ListIconGadget Scroll Left and Right with Keyboard

Posted: Sun Aug 27, 2023 1:26 am
by mk-soft
Piero wrote: Sat Aug 26, 2023 9:06 pm You are too gentle/helpful mk:
I will ask you something futile that will make you go to psychiatric hospital to solve, in the near future.
Be afraid.
I already know the answer ...

... { 42 } ...


;)

Re: Solution ListIconGadget Scroll Left and Right with Keyboard

Posted: Tue Aug 29, 2023 4:27 pm
by Piero
mk-soft wrote: Sun Aug 27, 2023 1:26 amI already know the answer ...
"It is Not Mind, It is Not Buddha, It is Not Things." 🤔

PS: And doesn't need a towel :wink: