Search code examples
c#gtk3gtk#

GTK TreeView checkbox left to the text?


I am using GTK# with .NET Core. How can I add a checkbox left to a text, not a separate column? Ubuntu's update dialogue box uses it so it must be possible, but I don't know how.

enter image description here

I tried changing the renderer type to CellRenderToggle() but it only showed the checkbox. I think the type of the column should be something that contains both a boolean value and a string, but what is that?

enter image description here

var st = new Gtk.TreeStore(typeof(string), typeof(string), typeof(string));
var iter = st.AppendValues("hello", "world", "gtk");

st.AppendValues(iter, "damn", "you", "ebay");
st.AppendValues(iter, "damn", "you", "ebay");
st.AppendValues(iter, "damn", "you", "ebay");

mytree.Model = st;
mytree.ExpandAll();

var r1 = new Gtk.CellRendererToggle();
var r2 = new Gtk.CellRendererText();

mytree.Columns[0].PackStart(r1, true);
mytree.Columns[1].PackStart(r2, true);
mytree.Columns[2].PackStart(r2, true);

mytree.Columns[0].AddAttribute(r1, "text", 0);
mytree.Columns[1].AddAttribute(r2, "text", 1);
mytree.Columns[2].AddAttribute(r2, "text", 2);

Solution

  • You have to add two renderers to the same Treeview column, reflecting two different column values in the Store. Since I don't know C#, here is an MCVE in Python:

    #!/usr/bin/env python
    
    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk
    
    window = Gtk.Window()
    window.set_default_size(500, 500)
    tree = Gtk.TreeView()
    window.add(tree)
    store = Gtk.TreeStore(bool, str, str)
    tree.set_model(store)
    iter = store.append(None,[True, "world", "gtk"])
    store.append(iter, [False, "you", "ebay"])
    # create a column
    column = Gtk.TreeViewColumn()
    tree.append_column(column)
    # add a toggle render
    toggle = Gtk.CellRendererToggle()
    column.pack_start(toggle, True)
    column.add_attribute(toggle, "active", 0)
    # and add a text renderer to the same column
    text_ren = Gtk.CellRendererText()
    column.pack_start(text_ren, True)
    column.add_attribute(text_ren, "text", 1)
    # now add a column using the default method
    column = Gtk.TreeViewColumn("column", text_ren, text = 2)
    tree.append_column(column)
    window.show_all()
    
    Gtk.main()
    

    Your code would probably look similar to:

    mytree.Columns[0].PackStart(r1, true);
    mytree.Columns[0].PackStart(r2, true);
    mytree.Columns[2].PackStart(r2, true);
    
    mytree.Columns[0].AddAttribute(r1, "text", 0);
    mytree.Columns[0].AddAttribute(r2, "text", 1);
    mytree.Columns[2].AddAttribute(r2, "text", 2);