Re: Cross-platform column justification of ListIconGadget
Posted: Tue Feb 09, 2016 9:42 am

http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
EnableExplicit
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
ImportC ""
g_object_set_double(*Object, Property.P-ASCII, Value.D,
Null) As "g_object_set"
gtk_cell_layout_get_cells(*CellLayout)
gtk_tree_view_column_set_alignment(*TreeColumn.GtkTreeViewColumn,
Alignment.F)
EndImport
CompilerEndSelect
Enumeration
#PB_ListIcon_JustifyColumnLeft
#PB_ListIcon_JustifyColumnCenter
#PB_ListIcon_JustifyColumnRight
EndEnumeration
Procedure SetListIconColumnJustification(ListIconID.I, Column.I, Alignment.I)
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Linux
Protected AlignmentFactor.F
Protected *CellRenderers
Protected *Column
Protected Count.I
Protected i.I
Select Alignment
Case #PB_ListIcon_JustifyColumnLeft
AlignmentFactor = 0.0
Case #PB_ListIcon_JustifyColumnCenter
AlignmentFactor = 0.5
Case #PB_ListIcon_JustifyColumnRight
AlignmentFactor = 1.0
EndSelect
*Column = gtk_tree_view_get_column_(GadgetID(ListIconID), Column)
If *Column
gtk_tree_view_column_set_alignment(*Column, AlignmentFactor)
*CellRenderers = gtk_cell_layout_get_cells(*Column)
If *CellRenderers
Count = g_list_length_(*CellRenderers)
For i = 0 To Count - 1
g_object_set_double(g_list_nth_data_(*CellRenderers, i), "xalign",
AlignmentFactor, #Null)
Next i
g_list_free_(*CellRenderers)
EndIf
EndIf
CompilerCase #PB_OS_MacOS
Protected ColumnHeaderCell.I
Protected ColumnObject.I
Protected ColumnObjectArray.I
Select Alignment
Case #PB_ListIcon_JustifyColumnLeft
Alignment = #NSLeftTextAlignment
Case #PB_ListIcon_JustifyColumnCenter
Alignment = #NSCenterTextAlignment
Case #PB_ListIcon_JustifyColumnRight
Alignment = #NSRightTextAlignment
EndSelect
; ----- Justify text of column cells
CocoaMessage(@ColumnObjectArray, GadgetID(ListIconID), "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(ListIconID), "reloadData")
CompilerCase #PB_OS_Windows
Protected ListIconColumn.LV_COLUMN
ListIconColumn\mask = #LVCF_FMT
Select Alignment
Case #PB_ListIcon_JustifyColumnLeft
ListIconColumn\fmt = #LVCFMT_LEFT
Case #PB_ListIcon_JustifyColumnCenter
ListIconColumn\fmt = #LVCFMT_CENTER
Case #PB_ListIcon_JustifyColumnRight
ListIconColumn\fmt = #LVCFMT_RIGHT
EndSelect
SendMessage_(GadgetID(ListIconID), #LVM_SETCOLUMN, Column, @ListIconColumn)
CompilerEndSelect
EndProcedure
OpenWindow(0, 200, 100, 445, 90, "Right justify 1st column")
ListIconGadget(0, 5, 5, 435, 80, "Name", 110)
AddGadgetColumn(0, 1, "Address", 300)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ + "12 Parliament Way, Battle Street, By the Bay")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity")
SetListIconColumnJustification(0, 0, #PB_ListIcon_JustifyColumnRight)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Select all
OpenWindow(0, 200, 100, 999, 350, "Show Listicon justifications")
ListIconGadget(0, 5, 5, 835, 80, "Name", 110)
AddGadgetColumn(0, 1, "Address", 350)
AddGadgetColumn(0, 2, "Phone", 200)
AddGadgetColumn(0, 3, "--", 20)
AddGadgetItem(0, -1, "Harry Rannit" + #LF$ + "12 Parliament Way, Battle Street, By the Bay" + #LF$ + "45678")
AddGadgetItem(0, -1, "Ginger Brokeit" + #LF$ + "130 PureBasic Road, BigTown, CodeCity" + #LF$ + "678 998")
SetListIconColumnJustification(0, 0, #PB_ListIcon_JustifyColumnRight)
SetListIconColumnJustification(0, 1, #PB_ListIcon_JustifyColumnCenter)
SetListIconColumnJustification(0, 2, #PB_ListIcon_JustifyColumnLeft)
ListIconGadget(1, 5, 100, 835, 80, "Who", 110)
AddGadgetColumn(1, 1, "Where", 350)
AddGadgetColumn(1, 2, "Number", 200)
AddGadgetColumn(1, 3, "--", 20)
AddGadgetItem(1, -1, "Roger Bodgedit" + #LF$ + "999 Duff Place, Codswallop" + #LF$ + "07777 45678")
AddGadgetItem(1, -1, "Fred Fixedit" + #LF$ + "White House, NotThatOne, Gleeful" + #LF$ + "Fortify 245")
SetListIconColumnJustification(1, 2, #PB_ListIcon_JustifyColumnRight)
SetListIconColumnJustification(1, 1, #PB_ListIcon_JustifyColumnCenter)
SetListIconColumnJustification(1, 0, #PB_ListIcon_JustifyColumnLeft)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
You are right. The problem is a wrong internal declaration in PureBasic of the GTK API function gtk_tree_view_column_set_alignment_(*TreeColumn, xalign). Alignment has to be declared as Float in order to work correctly.eck49 wrote:Thanks for this code, which is just what I want, except that the alignment of the headers seems to be displaced. Help!
You are welcome!pierre2 wrote:Late to the party, but thank you Shardik for your cross-platform alignment code (both here, and elsewhere for TextGadgets) - exactly what I needed. Good work !
Yes, you may include the code into your LIGEnhancemant module. But on MacOS the code currently doesn't work anymore in PB 6.0 and above because Fred changed internals of the ListIconGadget cells. I have almost finished a modified version for MacOS which works both on PB 5.73 and earlier and on PB 6.0 and newer.jacdelad wrote: Tue Feb 14, 2023 9:33 am @Shardik: Can I include your code from above into the next version of my LIGEnhancements-module (with a proper comment about its origin, of course)? The module itself only works for Windows right now, but who knows the future...
Thanks!Shardik wrote: Tue Feb 14, 2023 11:16 amYes, you may include the code into your LIGEnhancemant module. But on MacOS the code currently doesn't work anymore in PB 6.0 and above because Fred changed internals of the ListIconGadget cells. I have almost finished a modified version for MacOS which works both on PB 5.73 and earlier and on PB 6.0 and newer.jacdelad wrote: Tue Feb 14, 2023 9:33 am @Shardik: Can I include your code from above into the next version of my LIGEnhancements-module (with a proper comment about its origin, of course)? The module itself only works for Windows right now, but who knows the future...