Cross-platform column justification of ListIconGadget

Just starting out? Need help? Post your questions and find answers here.
csk
User
User
Posts: 39
Joined: Tue May 03, 2011 5:52 pm
Location: Little Red Dot

Cross-platform column justification of ListIconGadget

Post by csk »

Hi,

I bought PureBasic a few months ago and am only now starting to learn the language. I come from VB6 and will re-write my programs in PureBasic to run Cross-Platform.

I am trying to right-justify some columns in the ListIconGadget. I search the forum and found samples that goes like:

Code: Select all

lvc.LV_COLUMN  ; set up structure for global use to format ListIconGadget columns
lvc\mask = #LVCF_FMT
lvc\fmt  = #LVCFMT_RIGHT
.
.
.
.
SendMessage_(GadgetID(i), #LVM_SETCOLUMN, 2, @lvc)  ; Right justify column 2
SendMessage_(GadgetID(i), #LVM_SETCOLUMN, 3, @lvc)  ; Right justify column 3
I don't have a Mac yet so I am unable to test this code on a Mac. However, the above SendMessage method sure looks like Windows API syntax that I doubt works in Mac and Linux.

Is there a PureBasic method that works cross-plaform?
Last edited by csk on Wed May 04, 2011 8:50 am, edited 1 time in total.
User avatar
OldSkoolGamer
Enthusiast
Enthusiast
Posts: 148
Joined: Mon Dec 15, 2008 11:15 pm
Location: Nashville, TN
Contact:

Re: Beginner's questions

Post by OldSkoolGamer »

You are correct, the SendMessage_ is a Windows API command so it will not work on Mac or Linux. I am not aware of an easy cross-platform way to do what you are asking, but hopefully someone in the know better than I can answer that. I unfortunately only use PB for Windows currently. :(
csk
User
User
Posts: 39
Joined: Tue May 03, 2011 5:52 pm
Location: Little Red Dot

Re: Beginner's questions

Post by csk »

I am looking for the equivalent of what are available to TextGadget:

#PB_Text_Center : The text is centered in the gadget.
#PB_Text_Right : The text is right aligned.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Beginner's questions

Post by Shardik »

Try this procedure which works in Windows and Linux. If time allows I will try to supplement
the code for MacOS this evening. :wink:

@csk,
one small demand: please change the topic text from "Beginner's questions" to something
more informative, for example "Cross-platform justification of ListIconGadget columns". If
someone later tries to find a solution to this problem he probably won't look for a topic
"Beginner's questions" :wink:

Code: Select all

EnableExplicit

CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  ImportC ""
    g_object_set_double(*Object, Property.P-ASCII, Value.D, Null) As "g_object_set"
  EndImport
CompilerEndIf

Enumeration
  #PB_ListIcon_JustifyColumnLeft
  #PB_ListIcon_JustifyColumnCenter
  #PB_ListIcon_JustifyColumnRight
EndEnumeration

Procedure SetListIconColumnJustification(ListIconID, Column, Alignment)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected AlignmentFactor.D
      Protected *CellRenderers
      Protected *Column
      Protected Count
      Protected 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_tree_view_column_get_cell_renderers_(*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_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, 430, 85, "Change column justification")
ListIconGadget(0, 5, 5, 420, 75, "Name", 100)
AddGadgetColumn(0, 1, "Address", 310)
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
Update 1: Sorry, I forgot to include an Import statement for the Linux version.
I have added that and for clarity I changed the constants from a simple #Right
etc. to #PB_ListIcon_JustifyColumnRight etc. and changed the procedure name
from SetColumnAlignment() to SetListIconColumnJustification(). Furthermore I
added a simple example so that this code is runnable at once without change... :P
Last edited by Shardik on Wed May 04, 2011 3:11 pm, edited 2 times in total.
csk
User
User
Posts: 39
Joined: Tue May 03, 2011 5:52 pm
Location: Little Red Dot

Re: Cross-platform column justification of ListIconGadget

Post by csk »

Hi Shardik,

Thanks.

I changed the Topic heading to "Cross-platform column justification of ListIconGadget".
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Cross-platform column justification of ListIconGadget

Post by Shardik »

Shardik wrote:If time allows I will try to supplement the code for MacOS this evening. :wink:
Sorry, but I seem to have been overly optimistic. Until now I didn't succeed
in finding a Mac solution. At least it should be possible to change the
justification of the column header by changing the "just" field in the
ControlFontStyleRec structure of the DataBrowserListViewHeaderDesc.
Although all API function calls return 0 (noErr), the justification change
doesn't take place. Probably it's only possible to define the column
justification during creation of the DataBrowser control (ListIconGadget),
but not during runtime... :evil:

Code: Select all

; ATTENTION: Doesn't work currently!
EnableExplicit

#teCenter = 1
#teFlushRight = -1
#teFlushLeft = -2

ImportC ""
  GetDataBrowserListViewHeaderDesc(DataBrowserRef, ColumnID, *HeaderDesc)
  GetDataBrowserTableViewColumnProperty(DataBrowserRef, Column, *ColumnID)
  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.L
  FontStyle.ControlFontStyleRec
  IconInfo.L
EndStructure

Enumeration
  #PB_ListIcon_JustifyColumnLeft
  #PB_ListIcon_JustifyColumnCenter
  #PB_ListIcon_JustifyColumnRight
EndEnumeration

Procedure SetListIconColumnJustification(ListIconID, Column, Alignment)
  Protected ColumnID
  Protected HeaderDesc.DataBrowserListViewHeaderDesc
  Protected Justification
  
  Select Alignment
    Case #PB_ListIcon_JustifyColumnLeft
      Justification = #teFlushLeft
    Case #PB_ListIcon_JustifyColumnCenter
      Justification = #teCenter
    Case #PB_ListIcon_JustifyColumnRight
      Justification = #teFlushRight
  EndSelect
  
  If GetDataBrowserTableViewColumnProperty(GadgetID(0), Column, @ColumnID) = 0
    If GetDataBrowserListViewHeaderDesc(GadgetID(0), ColumnID, @HeaderDesc) = 0
      HeaderDesc\FontStyle\just = Justification
      SetDataBrowserListViewHeaderDesc(GadgetID(0), ColumnID, @HeaderDesc)
    EndIf
  EndIf
EndProcedure

OpenWindow(0, 200, 100, 445, 90, "Change column justification")
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
User avatar
USCode
Addict
Addict
Posts: 912
Joined: Wed Mar 24, 2004 11:04 pm
Location: Seattle, USA

Re: Cross-platform column justification of ListIconGadget

Post by USCode »

Shardik wrote:... Probably it's only possible to define the column justification during creation of the DataBrowser control (ListIconGadget), but not during runtime...
I don't know about the OP, but I could live with that. Perhaps a new optional parameter for AddGadgetColumn to set the justification, with left being the default ?
Or are you saying the same justification (left, right, center) would have to apply to ALL columns and be set at ListIconGadget creation time?
csk
User
User
Posts: 39
Joined: Tue May 03, 2011 5:52 pm
Location: Little Red Dot

Re: Cross-platform column justification of ListIconGadget

Post by csk »

Defining the column justification during the ListIconGadget creation is okay. Actually that is what I am doing in my VB6 codes.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Cross-platform column justification of ListIconGadget

Post by Shardik »

After countless hours of trying to find a Mac solution I finally
succeeded. I simply had declared the variable InitialSortOrder in
the structure DataBrowserListViewHeaderDesc wrongly as Long
Integer instead of Unsigned Word... :evil:

This is the final cross-platform code example (Windows, Linux and
MacOS X) to change the column justification in a ListIconGadget
during runtime.

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"
    EndImport
  CompilerCase #PB_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
CompilerEndSelect

Enumeration
  #PB_ListIcon_JustifyColumnLeft
  #PB_ListIcon_JustifyColumnCenter
  #PB_ListIcon_JustifyColumnRight
EndEnumeration

Procedure SetListIconColumnJustification(ListIconID, Column, Alignment)
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Linux
      Protected AlignmentFactor.D
      Protected *CellRenderers
      Protected *Column
      Protected Count
      Protected 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_tree_view_column_get_cell_renderers_(*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 ColumnID.L
      Protected HeaderDesc.DataBrowserListViewHeaderDesc

      If GetDataBrowserTableViewColumnProperty(GadgetID(0), Column, @ColumnID) = 0
        If GetDataBrowserListViewHeaderDesc(GadgetID(0), ColumnID, @HeaderDesc) = 0
          HeaderDesc\FontStyle\Flags = #kControlUseJustMask
          
          Select Alignment
            Case #PB_ListIcon_JustifyColumnLeft
              HeaderDesc\FontStyle\Just = #teFlushLeft
            Case #PB_ListIcon_JustifyColumnCenter
              HeaderDesc\FontStyle\Just = #teCenter
            Case #PB_ListIcon_JustifyColumnRight
              HeaderDesc\FontStyle\Just = #teFlushRight
          EndSelect
          
          SetDataBrowserListViewHeaderDesc(GadgetID(0), ColumnID, @HeaderDesc)
        EndIf
      EndIf
    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
ozzie
Enthusiast
Enthusiast
Posts: 429
Joined: Sun Apr 06, 2008 12:54 pm
Location: Brisbane, Qld, Australia
Contact:

Re: Cross-platform column justification of ListIconGadget

Post by ozzie »

Many thanks, Shardik.
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Cross-platform column justification of ListIconGadget

Post by Shardik »

In my above cross-platform example the Mac part is for PB's carbon implementation (PB 4.61 and older or PB 5.00 with subsystem Carbon). Beginning with PB 5.00 PureBasic is based on the cocoa framework with a totally different API. Therefore I have programmed a separate solution for cocoa:
http://www.purebasic.fr/english/viewtop ... 5&start=28

To use this example code in your own cross-platform programs in PB 5.00 you have to download wilbert's CocoaMessage userlib (link in 1st posting of the above thread) and to save the lib (available for 32 and 64 bit) in your PB installation's userlib folder!
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: Cross-platform column justification of ListIconGadget

Post by CONVERT »

Shardik, you are so great!

Thank you very much.

Jean.
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
Oma
Enthusiast
Enthusiast
Posts: 312
Joined: Thu Jun 26, 2014 9:17 am
Location: Germany

Re: Cross-platform column justification of ListIconGadget

Post by Oma »

After adding this line to shardiks code...

Code: Select all

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(*cell_layout)
       ;<<<
    EndImport
and replace the following disabled line with the following...

Code: Select all

;         *CellRenderers = gtk_tree_view_column_get_cell_renderers_(*Column)
        *CellRenderers = gtk_cell_layout_get_cells(*Column)
the code example still works on Linux with gtk2 and gtk3.

Regards, Charly
PureBasic 5.4-5.7, Linux: (X/L/K)Ubuntus+Mint - Windows XP (32Bit)
PureBasic Linux-API-Library & Viewer: http://www.chabba.de
User avatar
CONVERT
Enthusiast
Enthusiast
Posts: 127
Joined: Fri May 02, 2003 12:19 pm
Location: France

Re: Cross-platform column justification of ListIconGadget

Post by CONVERT »

Thanks a lot, Charly.
PureBasic 6.01 LTS 64 bit | Windows 10 Pro x64 | Intel(R) Core(TM) i7-8700 CPU @ 3.20Ghz 16 GB RAM, SSD 500 GB, PC locally assembled
User avatar
Keya
Addict
Addict
Posts: 1891
Joined: Thu Jun 04, 2015 7:10 am

Re: Cross-platform column justification of ListIconGadget

Post by Keya »

amazing work! I never would've thought this possible (especially cross-OS!) without a custom control :)
Post Reply