ListIconGadget column

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: ListIconGadget column

Post by Shardik »

Mindphazer wrote:Though I wish you could find a cross platform way :-)
Please try my following true cross-platform solution which displays the row and column of a clicked cell. I have tested it successfully on these operating systems:
- MacOS 10.6.8 (Snow Leopard) with PB 5.31 x86 and x64
- Ubuntu 14.04 x64 Unity with PB 5.31 x64
- Windows XP SP3 with PB 5.31 x86
- Windows 7 x64 SP1 with PB 5.31 x86 and x64
falsam wrote:The only option is THE CallBack.
Not quite true: the Windows code is taken from this old code example from srod and doesn't require a callback... :wink:

Code: Select all

EnableExplicit

Procedure.I GetSelectedColumn(WindowID.I, ListIconID.I)
  Protected SelectedColumn.I

  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux ; ------------------------------------------------
      Protected ColumnList.I
      Protected ColumnObject.I
      Protected Iter.I
      Protected Path.I
      Protected TreeStore.I

      If gtk_tree_view_get_cursor_(GadgetID(ListIconID), @Path, @ColumnObject)
        TreeStore = gtk_tree_view_get_model_(GadgetID(ListIconID))
        
        If gtk_tree_model_get_iter_(TreeStore, @Iter, Path)
          If Path <> 0 And ColumnObject <> 0
            ColumnList = gtk_tree_view_get_columns_(GadgetID(ListIconID))
            SelectedColumn = g_list_index_(ColumnList, ColumnObject)
            g_list_free_(ColumnList)
          EndIf
        EndIf
      EndIf
    CompilerCase #PB_OS_MacOS ; ------------------------------------------------
      Protected CursorLocation.NSPoint

      CursorLocation\x = WindowMouseX(WindowID)
      CursorLocation\y = WindowHeight(WindowID) - WindowMouseY(WindowID)
      CocoaMessage(@CursorLocation, GadgetID(ListIconID),
        "convertPoint:@", @CursorLocation, "fromView:", 0)
      SelectedColumn = CocoaMessage(0, GadgetID(ListIconID),
        "columnAtPoint:@", @CursorLocation)
    CompilerCase #PB_OS_Windows ; ----------------------------------------------
      Protected CursorLocation.POINT
      Protected HitInfo.LVHITTESTINFO

      GetCursorPos_(CursorLocation)
      MapWindowPoints_(0, GadgetID(ListIconID), CursorLocation, 1)             
      Hitinfo\pt\x = CursorLocation\x
      HitInfo\pt\y = CursorLocation\y
      SendMessage_(GadgetID(ListIconID), #LVM_SUBITEMHITTEST, 0, HitInfo)
      SelectedColumn = HitInfo\iSubItem                       
  CompilerEndSelect

  ProcedureReturn SelectedColumn
EndProcedure

OpenWindow(0, 200, 100, 304, 107, "Detect clicked cell")
ListIconGadget(0, 5, 5, WindowWidth(0) - 10, WindowHeight(0) - 10, "Column 0",
  95, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect)
AddGadgetColumn(0, 1, "Column 1", 95)
AddGadgetColumn(0, 2, "Column 2", 95)
AddGadgetItem(0, -1, "0,0" + #LF$ + "0,1" + #LF$ + "0,2")
AddGadgetItem(0, -1, "1,0" + #LF$ + "1,1" + #LF$ + "1,2")
AddGadgetItem(0, -1, "2,0" + #LF$ + "2,1" + #LF$ + "2,2")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
        Debug "Clicked cell: " + GetGadgetState(0) + "," + GetSelectedColumn(0, 0)
      EndIf
  EndSelect
ForEver
Last edited by Shardik on Wed Jan 28, 2015 1:32 pm, edited 2 times in total.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 456
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: ListIconGadget column

Post by Mindphazer »

Thank you very much Shardik !!
Your code works very well (I confirm it works on OS X 10.10.1 too :P :P)
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
infratec
Always Here
Always Here
Posts: 7583
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: ListIconGadget column

Post by infratec »

epog10
User
User
Posts: 93
Joined: Sat May 29, 2010 11:46 am
Location: UK

Re: ListIconGadget column

Post by epog10 »

Ouch!

Over 50 lines of code against a one-line function?

(True that the enthusiasm of those who decided to 'crack it' does show through.)

I would think that a Feature Request is in order. Does anybody know if it has already been previously submitted?
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: ListIconGadget column

Post by Shardik »

epog10 wrote:Ouch!

Over 50 lines of code against a one-line function?
It's a bit unfair to speak of over 50 lines for a cross-platform solution. If you consider my Windows-only part it's only 10 lines of code for PB's missing function GetSelectedColumn()... :wink:

But you are right in that it would be nice to have a native PB function to obtain the selected column.

Code: Select all

EnableExplicit

Procedure.I GetSelectedColumn(ListIconID.I)
  Protected CursorLocation.POINT
  Protected HitInfo.LVHITTESTINFO
  GetCursorPos_(CursorLocation)
  MapWindowPoints_(0, GadgetID(ListIconID), CursorLocation, 1)             
  Hitinfo\pt\x = CursorLocation\x
  HitInfo\pt\y = CursorLocation\y
  SendMessage_(GadgetID(ListIconID), #LVM_SUBITEMHITTEST, 0, HitInfo)
  ProcedureReturn HitInfo\iSubItem  
EndProcedure

OpenWindow(0, 200, 100, 304, 80, "Detect clicked cell")
ListIconGadget(0, 5, 5, WindowWidth(0) - 10, WindowHeight(0) - 10, "Column 0",
  95, #PB_ListIcon_GridLines | #PB_ListIcon_FullRowSelect)
AddGadgetColumn(0, 1, "Column 1", 95)
AddGadgetColumn(0, 2, "Column 2", 95)
AddGadgetItem(0, -1, "0,0" + #LF$ + "0,1" + #LF$ + "0,2")
AddGadgetItem(0, -1, "1,0" + #LF$ + "1,1" + #LF$ + "1,2")
AddGadgetItem(0, -1, "2,0" + #LF$ + "2,1" + #LF$ + "2,2")

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      If EventGadget() = 0 And EventType() = #PB_EventType_LeftClick
        Debug "Clicked cell: " + GetGadgetState(0) + "," + GetSelectedColumn(0)
      EndIf
  EndSelect
ForEver
GenRabbit
Enthusiast
Enthusiast
Posts: 151
Joined: Wed Dec 31, 2014 5:41 pm

Re: ListIconGadget column

Post by GenRabbit »

Nice program. How would I go about it if I wanted to be able to write new stuff into cell x.1? (Digits only)
Post Reply