Listicon bug or something else?

Just starting out? Need help? Post your questions and find answers here.
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Listicon bug or something else?

Post by tua »

Seems to only work as desired when clicking the first column ...

Code: Select all

EnableExplicit

#Win  = 1
#Grid = 2 

Procedure GridClick()
  Protected row, column
  
  row = GetGadgetState(#Grid)
  column = GetGadgetItemAttribute(#Grid, row, #PB_ListIcon_ClickedColumn)
  MessageRequester("Info", "Row: " + Str(row) + ", Column: " + Str(column))
EndProcedure

Procedure Main()
  Protected i
  
  If OpenWindow(#Win, 100, 100, 400, 300, "ListIconGadget Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    ListIconGadget(#Grid, 10, 10, 380, 280, "Column 1", 100)
    AddGadgetColumn(#Grid, 1, "Column 2", 100)
    AddGadgetColumn(#Grid, 2, "Column 3", 100)
    ;
    For i = 0 To 9
      AddGadgetItem(#Grid, -1, "Row " + Str(i) + #LF$ + "Item 1" + #LF$ + "Item 2")
    Next
    BindGadgetEvent(#Grid, @GridClick(), #PB_EventType_LeftClick)
  EndIf  
EndProcedure    

Main()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Listicon bug or something else?

Post by Marc56us »

#PB_ListIcon_ClickedColumn is not available for ListIconGadget

See Remarks in help (GetGadgetItemAttribute())
Only #PB_ListIcon_ColumnWidth is available

However, you can find the clicked column by using the column width and the X position (GadgetX() /#PB_Gadget_WindowCoordinate) of the gadget in the window.
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Re: Listicon bug or something else?

Post by tua »

What about -1 being returned by GetGadgetState() when clicking on any column other than 0?
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: Listicon bug or something else?

Post by PBJim »

Hi Tua, you could use some of the code in Rashad's grid — where pInfo\iSubItem gives you the column number. You would need to remove all reference to the stringinput, which in this code serves as a cell input. Try the full code in this link first and then you'll see what's possible before adapting it.

https://www.purebasic.fr/english/viewto ... 50#p630250

The section of code you need is this, minus the lines I've commented out.

Code: Select all

Case #WM_LBUTTONDOWN
      SetGadgetState(ListIcon,-1)
      ; MoveWindow_(GadgetID(2),0,0,0,0,1)
      ; If editflag = 1 And GetGadgetText(2) <> ""
      ;   SetGadgetItemText(ListIcon, Oldr,GetGadgetText(2),Oldc)
      ; EndIf
      pInfo.LVHITTESTINFO
      pInfo\pt\x = (lParam & $FFFF) 
      pInfo\pt\y = (lParam>> 16 & $FFFF)                    
      
      SendMessage_(GadgetID(ListIcon),#LVM_SUBITEMHITTEST,0,@pInfo)
      SetGadgetItemColor(ListIcon,OLdr,#PB_Gadget_FrontColor,#Black ,Oldc)
      SetGadgetItemColor(ListIcon,Oldr, #PB_Gadget_BackColor, LIColor,Oldc)
      SetGadgetItemColor(ListIcon,pInfo\iItem,#PB_Gadget_FrontColor,#White ,pInfo\iSubItem)
      SetGadgetItemColor(ListIcon,pInfo\iItem, #PB_Gadget_BackColor,GetSysColor_(#COLOR_HIGHLIGHT),pInfo\iSubItem)

      Oldr = pInfo\iItem
      Oldc = pInfo\iSubItem

      Debug oldc
PBJim
Enthusiast
Enthusiast
Posts: 294
Joined: Fri Jan 19, 2024 11:56 pm

Re: Listicon bug or something else?

Post by PBJim »

This is the grid code adapted and stripped down to the basics, with your own code. It's working code.

Code: Select all

EnableExplicit

#Win  = 1
#Grid = 2 

Global oldCallback,LIColor,Oldr,Oldc,editflag

Procedure LIcallback(hwnd, msg, wparam, lparam) 
  
  Define result.i
  ; Define *NMHDR.NMHDR
  ; Define *phdn.NMHEADER
  ; Define r.RECT
  Define pInfo.LVHITTESTINFO
  Define col, row
  
  result = CallWindowProc_(oldCallback, hwnd, msg, wparam, lparam)
  Select msg
      
    Case #WM_LBUTTONDOWN
      SetGadgetState(#Grid,-1)
      
      pInfo.LVHITTESTINFO
      pInfo\pt\x = (lParam & $FFFF) 
      pInfo\pt\y = (lParam>> 16 & $FFFF)                    
      
      SendMessage_(GadgetID(#Grid),#LVM_SUBITEMHITTEST,0,@pInfo)
      SetGadgetItemColor(#Grid,OLdr,#PB_Gadget_FrontColor,#Black ,Oldc)
      SetGadgetItemColor(#Grid,Oldr, #PB_Gadget_BackColor, LIColor,Oldc)
      SetGadgetItemColor(#Grid,pInfo\iItem,#PB_Gadget_FrontColor,#White ,pInfo\iSubItem)
      SetGadgetItemColor(#Grid,pInfo\iItem, #PB_Gadget_BackColor,GetSysColor_(#COLOR_HIGHLIGHT),pInfo\iSubItem)

      Oldr = pInfo\iItem
      Oldc = pInfo\iSubItem
      
      Debug "Row = " + Oldr + ", Col = " + Oldc
      
  EndSelect
  
  ProcedureReturn result
  
EndProcedure


Procedure Main()
  Protected i
  
  If OpenWindow(#Win, 100, 100, 400, 300, "#GridGadget Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    LoadFont(0,"Tahoma",10) 
    
    ListIconGadget(#Grid, 10, 10, 380, 280, "Column 1", 100)
    ; **
    ; **  Set callback for grid cell selection
    ; **
    oldCallback = SetWindowLongPtr_(GadgetID(#Grid), #GWL_WNDPROC, @LIcallback())
    
    SetGadgetFont(#Grid,FontID(0))
    SetGadgetFont(#Grid,FontID(0))
    If GetGadgetColor(#Grid, #PB_Gadget_BackColor) < 0 
      LIColor = #White
    Else
      LIColor =  GetGadgetColor(#Grid, #PB_Gadget_BackColor)
    EndIf

    AddGadgetColumn(#Grid, 1, "Column 2", 100)
    AddGadgetColumn(#Grid, 2, "Column 3", 100)
    ;
    For i = 0 To 9
      AddGadgetItem(#Grid, -1, "Row " + Str(i) + #LF$ + "Item 1" + #LF$ + "Item 2")
    Next
  EndIf  
EndProcedure    

Main()

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
User avatar
mk-soft
Always Here
Always Here
Posts: 6209
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Listicon bug or something else?

Post by mk-soft »

#PB_ListIcon_ClickedColumn work only with event type #PB_EventType_ColumnClick
You have to read the PB-Help. Me too first ;)

Code: Select all

;-TOP

#ProgramTitle = "Main Window"
#ProgramVersion = "v1.01.2"

Enumeration Windows
  #Main
EndEnumeration

Enumeration MenuBar
  #MainMenu
EndEnumeration

Enumeration MenuItems
  #MainMenuAbout
  #MainMenuExit
EndEnumeration

Enumeration Gadgets
  #MainList
EndEnumeration

Enumeration StatusBar
  #MainStatusBar
EndEnumeration

Procedure UpdateWindow()
  Protected dx, dy
  dx = WindowWidth(#Main)
  dy = WindowHeight(#Main) - StatusBarHeight(#MainStatusBar) - MenuHeight()
  ; Resize gadgets
  ResizeGadget(#MainList, 0, 0, dx, dy)
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()
    ;ListViewGadget(#MainList, 0, 0, dx, dy)
    ListIconGadget(#MainList, 0, 0, dx, dy, "Column 0", 200, #PB_ListIcon_FullRowSelect)
    AddGadgetColumn(#MainList, 1, "Column 1", 200)
    AddGadgetColumn(#MainList, 2, "Column 2", 200)
    
    For i = 0 To 25
      AddGadgetItem(#MainList, -1, "Item " + i)
    Next
    
    ; Bind Events
    BindEvent(#PB_Event_SizeWindow, @UpdateWindow(), #Main)
    
    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_ColumnClick
                  column = GetGadgetAttribute(#MainList, #PB_ListIcon_ClickedColumn)
                  Debug "Last Clicked Columm " + column
                  
                Case #PB_EventType_Change
                  ;
              EndSelect
              
          EndSelect
          
      EndSelect
    ForEver
    
  EndIf
  
EndProcedure : Main()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Listicon bug or something else?

Post by Marc56us »

#PB_ListIcon_ClickedColumn work only with event type #PB_EventType_ColumnClick
Yes, works only on header line.
tua
User
User
Posts: 68
Joined: Sun Jul 23, 2023 8:49 pm
Location: BC, Canada

Re: Listicon bug or something else?

Post by tua »

I just came back to this post of mine and realized that I have been remiss in thanking everyone for their help with this - it was very helpful and much appreciated!
Post Reply