Page 1 of 1

Change grid parameters of ListIconGadget

Posted: Sun Jan 31, 2016 7:36 pm
by Shardik
The following code example demonstrates how to change the grid appearance of the ListIconGadget. It allows to modify the following grid parameters:
- Display of grid lines:
  • + No grid lines
    + Only vertical grid lines
    + Only horizontal grid lines
    + Both horizontal and vertical grid lines
- Line pattern:
  • + Dotted (default)
    + Dashed
    + Solid
- Line thickness
  • + 1 pixel (default)
    + 2 pixels
    + 3 pixels
The code example works equally well with GTK2 and GTK3 and was successfully tested with PB 5.31 x64 and 5.41 x64 (ASCII and Unicode mode) on Ubuntu 14.04 x64 with these desktop managers:
- Enlightenment E17
- KDE
- Unity

Code: Select all

EnableExplicit

#DQ$ = #DQUOTE$

ImportC ""
  gtk_tree_view_set_grid_lines(*TreeView.GtkTreeView, GridLineState.I)
EndImport

CompilerIf (#PB_Compiler_Version < 540 And Subsystem("gtk3") = #True) Or
  (#PB_Compiler_Version >= 540 And Subsystem("gtk2") = #False)
  ImportC ""
    gtk_css_provider_load_from_data(*CSSProvider, CSSData.P-UTF8, Length.I,
      *Error.GError)
    gtk_css_provider_new()
    gtk_style_context_add_provider_for_screen(*Screen.GdkScreen, *StyleProvider,
      Priority.I)
  EndImport
  
  #GTK_STYLE_PROVIDER_PRIORITY_APPLICATION = 600
CompilerEndIf

Enumeration
  #Window
EndEnumeration

Enumeration GtkTreeViewGridLines
  #GTK_TREE_VIEW_GRID_LINES_NONE
  #GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
  #GTK_TREE_VIEW_GRID_LINES_VERTICAL
  #GTK_TREE_VIEW_GRID_LINES_BOTH
EndEnumeration

Enumeration
  #OptionNoGridlines
  #OptionOnlyVertical
  #OptionOnlyHorizontal
  #OptionBoth
  #OptionDottedLine
  #OptionDashedLine
  #OptionSolidLine
  #Option1Px
  #Option2Px
  #Option3Px
  #ListIcon
  #FrameGridLineSelection
  #FrameGridPattern
  #FrameLineThickness
EndEnumeration

Procedure CreateListIconGadget()
  If IsGadget(#ListIcon)
    FreeGadget(#ListIcon)
  EndIf
  
  ListIconGadget(#ListIcon, 10, 10, 430, 110, "Name", 130,
    #PB_ListIcon_GridLines)
  AddGadgetColumn(#ListIcon, 1, "Address", 242)
  AddGadgetItem(#ListIcon, -1, "Harry Rannit" + #LF$ +
    "12 Parliament Way, Battle Street, By the Bay")
  AddGadgetItem(#ListIcon, -1, "Ginger Brokeit" + #LF$ +
    "130 PureBasic Road, BigTown, CodeCity")
  AddGadgetItem(#ListIcon, -1, "Didi Foundit" + #LF$ +
    "321 Logo Drive, Mouse House, Downtown")
EndProcedure

Procedure EnableGridLineOptions(State.I)
  DisableGadget(#Option1Px, State ! 1)
  DisableGadget(#Option2Px, State ! 1)
  DisableGadget(#Option3Px, State ! 1)
  DisableGadget(#OptionDottedLine, State ! 1)
  DisableGadget(#OptionDashedLine, State ! 1)
  DisableGadget(#OptionSolidLine, State ! 1)
EndProcedure

Procedure SetGridParameters(GridDisplay.I, GridPattern.S, LineThickness.I)
  CompilerIf (#PB_Compiler_Version < 540 And Subsystem("gtk3") = #True) Or
    (#PB_Compiler_Version >= 540 And Subsystem("gtk2") = #False)
    Protected CSSProvider.I
    Protected GridPatternCSS.S
    Protected Screen.I
  
    GridPatternCSS.S = "" +
      "GtkTreeView {" + #LF$ +
      "    -GtkTreeView-grid-line-pattern: " + #DQUOTE$ + GridPattern + #DQUOTE$ + ";" + #LF$ +
      "    -GtkTreeView-grid-line-width: " + Str(LineThickness) + ";" + #LF$ +
      "  }" + #LF$
  
    CSSProvider= gtk_css_provider_new()
    gtk_css_provider_load_from_data(CSSProvider, GridPatternCSS, -1, 0)
    Screen = gdk_display_get_default_screen_(gdk_display_get_default_())
    gtk_style_context_add_provider_for_screen(Screen, CSSProvider,
      #GTK_STYLE_PROVIDER_PRIORITY_APPLICATION)
    g_object_unref_(CSSProvider)
  CompilerElse
    Protected RCString.S

    RCString.S = "" +
      "style " + #DQ$ + "ModifiedGridLines" + #DQ$ + #LF$ +
      "{" + #LF$ +
      " GtkTreeView::grid-line-pattern = " + #DQ$ + GridPattern + #DQ$ + #LF$ +
      " GtkTreeView::grid-line-width = " + Str(LineThickness) + #LF$ +
      "}" + #LF$ +
      #LF$ +
      "class " + #DQ$ + "GtkTreeView" + #DQ$ + " style " + #DQ$ + "ModifiedGridLines" + #DQ$ + #LF$

    gtk_rc_parse_string_(RCString)
  CompilerEndIf

  CreateListIconGadget()
  gtk_tree_view_set_grid_lines(GadgetID(#ListIcon), GridDisplay)
EndProcedure

Define EventGadget.I
Define GridDisplay.I = #GTK_TREE_VIEW_GRID_LINES_BOTH
Define GridPattern.S = "\1\1"
Define LineThickness.I = 1

OpenWindow(#Window, 100, 100, 450, 430, "Change grid line parameters")

CreateListIconGadget()

FrameGadget(#FrameGridPattern, 10, 150, 200, 110, "Grid line pattern:")
OptionGadget(#OptionDottedLine, 30, 175, 136, 24, "Dotted line")
OptionGadget(#OptionDashedLine, 30, 200, 136, 24, "Dashed line")
OptionGadget(#OptionSolidLine, 30, 225, 136, 24, "Solid line")
SetGadgetState(#OptionDottedLine, #True)

FrameGadget(#FrameLineThickness, 240, 150, 200, 110, "Grid line thickness:")
OptionGadget(#Option1Px, 256, 175, 166, 20, "1 pixel")
OptionGadget(#Option2Px, 256, 200, 172, 20, "2 pixels")
OptionGadget(#Option3Px, 256, 225, 172, 20, "3 pixels")

FrameGadget(#FrameGridLineSelection, 120, 275, 200, 140, "Grid lines to draw:")
OptionGadget(#OptionNoGridlines, 140, 300, 166, 20, "No grid lines")
OptionGadget(#OptionOnlyVertical, 140, 325, 172, 20, "Only vertical")
OptionGadget(#OptionOnlyHorizontal, 140, 350, 172, 20, "Only horizontal")
OptionGadget(#OptionBoth, 140, 375, 172, 20, "Horizontal and vertical")
SetGadgetState(#OptionBoth, #True)

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_CloseWindow
      Break
    Case #PB_Event_Gadget
      EventGadget = EventGadget()
      
      Select EventGadget
        Case #OptionNoGridlines
          GridDisplay = #GTK_TREE_VIEW_GRID_LINES_NONE
        Case #OptionOnlyHorizontal
          GridDisplay = #GTK_TREE_VIEW_GRID_LINES_HORIZONTAL
        Case #OptionOnlyVertical
          GridDisplay = #GTK_TREE_VIEW_GRID_LINES_VERTICAL
        Case #OptionBoth
          GridDisplay = #GTK_TREE_VIEW_GRID_LINES_BOTH
        Case #OptionDottedLine
          GridPattern = "\1\1"
        Case #OptionDashedLine
          GridPattern = "\3\3"
        Case #OptionSolidLine
          GridPattern = "\0\0"
        Case #Option1Px
          LineThickness = 1
        Case #Option2Px
          LineThickness = 2
        Case #Option3Px
          LineThickness = 3
      EndSelect
      
      If EventGadget <= #OptionBoth
        gtk_tree_view_set_grid_lines(GadgetID(#ListIcon), GridDisplay)
        
        If EventGadget = #OptionNoGridlines
          EnableGridLineOptions(#False)
        Else
          EnableGridLineOptions(#True)
        EndIf
      ElseIf EventGadget <= #Option3Px          
        SetGridParameters(GridDisplay, GridPattern, LineThickness)
      EndIf
  EndSelect
ForEver

Re: Change grid parameters of ListIconGadget

Posted: Mon Feb 01, 2016 9:00 pm
by Zebuddi123
Hi Shardik thanks for the example :) I had actually seen the RCString in a C example just could`nt wrap my head around it and have`nt had the time (a week or so i would have needed lol) I`ve made into an easy proc to call. Works perfectly :D

Zebuddi. :)

Code: Select all

Procedure GTK_ListView_SolidLine() ;Requires GTK2
	CompilerIf (#PB_Compiler_Version < 540 And Subsystem("gtk3") = #True) Or  #PB_Compiler_Version >= 540 And Subsystem("gtk2") = #False
		MessageRequester("Program aborted", "Sorry, but this example needs GTK2 to work correctly!")
		End
	CompilerEndIf
	Define RCString.S  = "" +
	                     "style " + #DQUOTE$+ "SolidGridLines" + #DQUOTE$+ #LF$ +
	                     "{" + #LF$ +
	                     " GtkTreeView::grid-line-pattern = " + #DQUOTE$+ "\0\0" + #DQUOTE$+ #LF$ +
	                     "}" + #LF$ +
	                     #LF$ +
	                     "class " + #DQUOTE$+ "GtkTreeView" + #DQUOTE$+ " style " + #DQUOTE$+ "SolidGridLines" + #DQUOTE$+ #LF$
	gtk_rc_parse_string_(RCString)
EndProcedure