Page 1 of 2

Column alignment in ListIconGadget

Posted: Sun May 08, 2011 9:08 am
by csk
An optional parameter in the AddGadgetColumn command for individual coulmn alignment, cross-platform enabled.

This is quite commonly used since certain texts are more professionally presented when right-justified, for example prices range from $1.00 to $1,000.50 or quantity range from 0 to like 1000, or whatever large number.

Something similar to the TextGadget like:
#PB_Text_Center : The text is centered in the gadget.
#PB_Text_Right : The text is right aligned.

Re: Column alignment in ListIconGadget

Posted: Sun May 08, 2011 1:32 pm
by IdeasVacuum
+1

Just hit this in my current project. Mostly, my values have the same number of digits so it does not look too bad but right-justify would be a great option.

Re: Column alignment in ListIconGadget

Posted: Wed Jun 08, 2011 5:25 pm
by liam
+2
i was also looking for this 'simple' feature

Re: Column alignment in ListIconGadget

Posted: Wed Jun 08, 2011 7:16 pm
by USCode
+1
I requested the same thing awhile back with a slightly different implementation but ultimately the same goal:
http://www.purebasic.fr/english/viewtop ... =alignment

Re: Column alignment in ListIconGadget

Posted: Tue Feb 21, 2012 9:52 pm
by Shardik
I was just able to present a cross-platform solution for Windows,
Linux and MacOS X: :P
http://www.purebasic.fr/english/viewtop ... 31&start=8

Re: Column alignment in ListIconGadget

Posted: Fri Feb 22, 2013 3:28 pm
by Michael Vogel
To define the alignment by using PB internal commands would be fine, this would need to allow to use flags for both gadget command, AddGadgetColumn and ListIconGadget...

Code: Select all

ListIconGadget(0,  10,  25, 300, 70, "Column 1", 100,#PB_Text_Center)
AddGadgetColumn(0, 1, "Address", 250,#PB_Text_Right)

Re: Column alignment in ListIconGadget

Posted: Wed Apr 10, 2013 11:46 am
by CONVERT
I use the good Shardik's code http://www.purebasic.fr/english/viewtop ... 22#p410422 (Tue Feb 21, 2012 22:37), but I agree.

It would be fine to have a column alignment in ListIconGadget.

Re: Column alignment in ListIconGadget

Posted: Wed Apr 10, 2013 2:50 pm
by davido
+1 for native Text alignment in individual columns in ListIconGadget.

Re: Column alignment in ListIconGadget

Posted: Sun Jun 16, 2013 12:31 pm
by zxtunes.com
+3

Re: Column alignment in ListIconGadget

Posted: Sun Jun 16, 2013 5:38 pm
by uwekel
+1

Re: Column alignment in ListIconGadget

Posted: Tue Jun 18, 2013 12:56 pm
by thierry94
+5

Re: Column alignment in ListIconGadget

Posted: Tue Jan 31, 2017 10:02 am
by Torp
Fred, It's time to go ! :mrgreen:

Re: Column alignment in ListIconGadget

Posted: Wed Feb 01, 2017 8:42 am
by Lebostein
+100

In my case I use that forum snippet, but I have no idea how it works and if it correct - but it works on Windows and Mac OS:

Code: Select all

Procedure SetListIconColumnAlignment(ListIconID.i, ColumnIndex.i, Alignment.i)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    Protected Aligment.LV_COLUMN
    Aligment\mask = #LVCF_FMT
    Aligment\fmt = Alignment
    SendMessage_(GadgetID(ListIconID), #LVM_SETCOLUMN, ColumnIndex, @Aligment)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
    Protected ColumnHeaderCell.i
    Protected ColumnObject.i
    Protected ColumnObjectArray.i
    CocoaMessage(@ColumnObjectArray, GadgetID(ListIconID), "tableColumns")
    CocoaMessage(@ColumnObject, ColumnObjectArray, "objectAtIndex:", ColumnIndex)
    CocoaMessage(0, CocoaMessage(0, ColumnObject, "dataCell"), "setAlignment:", Alignment)
    CocoaMessage(@ColumnHeaderCell, ColumnObject, "headerCell")
    CocoaMessage(0, ColumnHeaderCell, "setAlignment:", Alignment)
  CompilerEndIf
EndProcedure
And don't forget the ColumnHeaderClick-Event!!! If you want to implement a sortable list (a list without sorting possibility is no list), then you need the ColumnHeaderClick-Event. There are some OS-specific workarounds here in the forum, but it would be nice to have some basic functionalities build in PureBasic. Thanks!

Re: Column alignment in ListIconGadget

Posted: Wed Feb 01, 2017 12:18 pm
by mestnyi
+1
My two cents :)

Code: Select all

; Windows Ok
; Linux Ok but #PB_ListIcon_TextItemAutoSize
; Mac OS no tested

DeclareModule ListIconText
  EnableExplicit
  
  #PB_ListIcon_TextLeft = 1<<1
  #PB_ListIcon_TextCenter = 1<<2
  #PB_ListIcon_TextRight = 1<<3
  #PB_ListIcon_TextItemAutoSize = 1<<4
  #PB_ListIcon_TextColumnAutoSize = 1<<5
  
  Declare Alignment(Gadget, Column, Alignment)
EndDeclareModule

Module ListIconText
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux ;- OS_Linux
      ImportC ""
        g_object_set_double(*Object, Property.p-ascii, Value.D, Null) As "g_object_set"
        gtk_cell_layout_get_cells(*cell_layout)
        gtk_tree_view_column_set_alignment(*tree_column, xalign.f);             replaced PureBasic's V5.* buggy: gtk_tree_view_column_set_alignment_(*Column, Alignment)
      EndImport
      
      ProcedureC Alignment(Gadget, Column, Alignment)
        Protected AlignmentFactor.D
        Protected *CellRenderers
        Protected *Column
        Protected Count
        Protected i
        
        If ((Alignment & #PB_ListIcon_TextLeft) = #PB_ListIcon_TextLeft)
          AlignmentFactor = 0.0
        ElseIf ((Alignment & #PB_ListIcon_TextCenter) = #PB_ListIcon_TextCenter)
          AlignmentFactor = 0.5
        ElseIf ((Alignment & #PB_ListIcon_TextRight) = #PB_ListIcon_TextRight)
          AlignmentFactor = 1.0
        EndIf
        
        *Column = gtk_tree_view_get_column_(GadgetID(Gadget), Column)
        
        If *Column
          ;gtk_tree_view_column_set_alignment_(*Column, AlignmentFactor) ; bug
          gtk_tree_view_column_set_alignment(*Column, AlignmentFactor);               title-alignment - fixed with ImportC
          
          If ((Alignment & #PB_ListIcon_TextColumnAutoSize) = #PB_ListIcon_TextColumnAutoSize)
            gtk_tree_view_column_set_sizing_(*Column, #GTK_TREE_VIEW_COLUMN_AUTOSIZE)
          EndIf
          
          ;         *CellRenderers = gtk_tree_view_column_get_cell_renderers_(*Column)
          *CellRenderers = gtk_cell_layout_get_cells(*Column)
          
          If *CellRenderers
            Count = g_list_length_(*CellRenderers)    ; number of columns
            
            For i = 0 To Count - 1
              ;g_object_set_double(g_list_nth_data_(*CellRenderers, i), "xalign", AlignmentFactor, #Null)
              Protected *Cellule.GtkCellRenderer = g_list_nth_data_(*CellRenderers, i) : *Cellule\xalign=AlignmentFactor
            Next i         
            
            g_list_free_(*CellRenderers)
          EndIf
          
          gtk_widget_queue_draw_(GadgetID(Gadget));                             force Redraw
        EndIf
      EndProcedure
      
    CompilerCase #PB_OS_MacOS ;- OS_MacOS
      #kControlUseJustMask = $0040
      #teCenter = 1
      #teFlushRight = -1
      #teFlushLeft = -2
      
      ImportC ""
        GetDataBrowserListViewHeaderDesc(DataBrowserRef, ColumnID, *HeaderDesc)
        GetDataBrowserTableViewColumnProperty(DataBrowserRef, Column, *ColumnID)
        HiliteControl(ControlRef, ControlPart)
        SetControlVisibility(ControlRef, IsVisible, DoDraw)
        SetDataBrowserListViewHeaderDesc(DataBrowserRef, ColumnID, *HeaderDesc)
      EndImport
      
      Structure RGBColor
        Red.U
        Green.U
        Blue.U
      EndStructure
      
      Structure ControlFontStyleRec
        Flags.W
        Font.W
        Size.W
        Style.W
        Mode.W
        Just.W
        ForeColor.RGBColor
        BackColor.RGBColor
      EndStructure
      
      Structure DataBrowserListViewHeaderDesc
        Version.L
        MinimumColumnWidth.U
        MaximumColumnWidth.U
        TitleOffset.W
        CFTitleString.L
        InitialSortOrder.U
        FontStyle.ControlFontStyleRec
        IconInfo.L
      EndStructure
      
      ProcedureC Alignment(Gadget, Column, Alignment)
        ;         Protected ColumnID.L
        ;         Protected HeaderDesc.DataBrowserListViewHeaderDesc
        ;         
        ;         If GetDataBrowserTableViewColumnProperty(GadgetID(Gadget), Column, @ColumnID) = 0
        ;           If GetDataBrowserListViewHeaderDesc(GadgetID(Gadget), ColumnID, @HeaderDesc) = 0
        ;             HeaderDesc\FontStyle\Flags = #kControlUseJustMask
        ;             
        ;             If ((Alignment & #PB_ListIcon_TextLeft) = #PB_ListIcon_TextLeft)
        ;               HeaderDesc\FontStyle\Just = #teFlushLeft
        ;             ElseIf ((Alignment & #PB_ListIcon_TextCenter) = #PB_ListIcon_TextCenter)
        ;               HeaderDesc\FontStyle\Just = #teCenter
        ;             ElseIf ((Alignment & #PB_ListIcon_TextRight) = #PB_ListIcon_TextRight)
        ;               HeaderDesc\FontStyle\Just = #teFlushRight
        ;             EndIf
        ;             
        ;             SetDataBrowserListViewHeaderDesc(GadgetID(Gadget), ColumnID, @HeaderDesc)
        ;           EndIf
        ;         EndIf
        Protected ColumnHeaderCell.I
        Protected ColumnObject.I
        Protected ColumnObjectArray.I
        
        If ((Alignment & #PB_ListIcon_TextLeft) = #PB_ListIcon_TextLeft)
          Alignment = 0
        ElseIf ((Alignment & #PB_ListIcon_TextCenter) = #PB_ListIcon_TextCenter)
          Alignment = 2
        ElseIf ((Alignment & #PB_ListIcon_TextRight) = #PB_ListIcon_TextRight)
          Alignment = 1
        EndIf
        
        ; ----- Justify text of column cells
        CocoaMessage(@ColumnObjectArray, GadgetID(Gadget), "tableColumns")
        CocoaMessage(@ColumnObject, ColumnObjectArray, "objectAtIndex:", Column)
        CocoaMessage(0, CocoaMessage(0, ColumnObject, "dataCell"), "setAlignment:", Alignment)
        
        ; ----- Justify text of column header
        CocoaMessage(@ColumnHeaderCell, ColumnObject, "headerCell")
        CocoaMessage(0, ColumnHeaderCell, "setAlignment:", Alignment)
        
        ; ----- Redraw ListIcon contents to see change
        CocoaMessage(0, GadgetID(Gadget), "reloadData")
        
      EndProcedure
      
    CompilerCase #PB_OS_Windows ;- OS_Windows
      Procedure Alignment(Gadget, Column, Alignment)
        Protected ListIconColumn.LV_COLUMN
        ListIconColumn\mask = #LVCF_FMT
        
        If ((Alignment & #PB_ListIcon_TextLeft) = #PB_ListIcon_TextLeft)
          ListIconColumn\fmt = #LVCFMT_LEFT
        ElseIf ((Alignment & #PB_ListIcon_TextCenter) = #PB_ListIcon_TextCenter)
          ListIconColumn\fmt = #LVCFMT_CENTER
        ElseIf ((Alignment & #PB_ListIcon_TextRight) = #PB_ListIcon_TextRight)
          ListIconColumn\fmt = #LVCFMT_RIGHT
        EndIf
        
        SendMessage_(GadgetID(Gadget), #LVM_SETCOLUMN, Column, @ListIconColumn)
        
        If ((Alignment & #PB_ListIcon_TextItemAutoSize) = #PB_ListIcon_TextItemAutoSize)
          SendMessage_(GadgetID(Gadget),#LVM_SETCOLUMNWIDTH,Column,#LVSCW_AUTOSIZE)
        ElseIf ((Alignment & #PB_ListIcon_TextColumnAutoSize) = #PB_ListIcon_TextColumnAutoSize)
          SendMessage_(GadgetID(Gadget),#LVM_SETCOLUMNWIDTH,Column,#LVSCW_AUTOSIZE_USEHEADER)
        EndIf
        
        InvalidateRect_(GadgetID(Gadget), 0, #True)
        
      EndProcedure
      
  CompilerEndSelect
EndModule


Enumeration Window
  #Window_0
EndEnumeration

Enumeration Gadget
  #Window_0_Gadget_0
EndEnumeration

Procedure Resize()
  ResizeGadget(#Window_0_Gadget_0,#PB_Ignore,#PB_Ignore,WindowWidth(EventWindow()),WindowHeight(EventWindow()))
EndProcedure

If OpenWindow(#Window_0,0,0,1000,100, "",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget) 
  ListIconGadget(#Window_0_Gadget_0,0,0,1000,100, "Text left", 100, #PB_ListIcon_GridLines|#PB_ListIcon_FullRowSelect) 
  AddGadgetColumn(#Window_0_Gadget_0, 1, "Text center", 100) 
  AddGadgetColumn(#Window_0_Gadget_0, 2, "Text right", 100) 
  
  AddGadgetColumn(#Window_0_Gadget_0, 3, "Text column auto size and text center", 200) 
  AddGadgetColumn(#Window_0_Gadget_0, 4, "Text column auto size", 200) 
  AddGadgetColumn(#Window_0_Gadget_0, 5, "Text center", 250) 
  AddGadgetColumn(#Window_0_Gadget_0, 6, "Text right", 150) 
  
  For i =0 To 3
    AddGadgetItem(#Window_0_Gadget_0,i,"(standart)" + Chr(10) + "text center" + Chr(10) + "text right" + Chr(10) + "text center" + Chr(10) + "text right" + Chr(10) + "text item auto size" + Chr(10) + "text item auto size") 
  Next
  
  ListIconText::Alignment(#Window_0_Gadget_0,0,ListIconText::#PB_ListIcon_TextLeft)
  ListIconText::Alignment(#Window_0_Gadget_0,1,ListIconText::#PB_ListIcon_TextCenter)
  ListIconText::Alignment(#Window_0_Gadget_0,2,ListIconText::#PB_ListIcon_TextRight)
  
  ListIconText::Alignment(#Window_0_Gadget_0,3,ListIconText::#PB_ListIcon_TextColumnAutoSize|ListIconText::#PB_ListIcon_TextCenter)
  ListIconText::Alignment(#Window_0_Gadget_0,4,ListIconText::#PB_ListIcon_TextColumnAutoSize|ListIconText::#PB_ListIcon_TextRight)
  ListIconText::Alignment(#Window_0_Gadget_0,5,ListIconText::#PB_ListIcon_TextItemAutoSize|ListIconText::#PB_ListIcon_TextCenter)
  ListIconText::Alignment(#Window_0_Gadget_0,6,ListIconText::#PB_ListIcon_TextItemAutoSize|ListIconText::#PB_ListIcon_TextRight)
  
  BindEvent(#PB_Event_SizeWindow, @Resize(), #Window_0)
  
  While IsWindow(#Window_0)
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        CloseWindow(EventWindow())
    EndSelect
  Wend
EndIf 
End

Re: Column alignment in ListIconGadget

Posted: Thu Feb 02, 2017 10:30 pm
by Shardik
mestnyi wrote:+1
My two cents :)
Your example will work on MacOS only up to PB 5.11 with subsystem Carbon. Starting with version 5.00, PureBasic has changed its utilized framework on MacOS from Carbon to Cocoa.

You have taken the MacOS code part from my old code example from 2012. But you seem to have overseen my link in that thread to a solution using the Cocoa framework which also works in current PB versions. Therefore I have posted in that thread now a modified version in which I replaced the Carbon part by the Cocoa part, so that my modified example is now also working with current PB versions on MacOS.