Page 1 of 1

Re: ListIconGadget column

Posted: Wed Jan 28, 2015 12:09 am
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

Re: ListIconGadget column

Posted: Wed Jan 28, 2015 7:51 am
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)

Re: ListIconGadget column

Posted: Wed Jan 28, 2015 8:01 am
by infratec

Re: ListIconGadget column

Posted: Wed Jan 28, 2015 12:16 pm
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?

Re: ListIconGadget column

Posted: Wed Jan 28, 2015 12:40 pm
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

Re: ListIconGadget column

Posted: Sun Dec 04, 2016 4:21 pm
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)