Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gtk# NodeView colored rows

I use NodeView object to output data to user in this way (Gtk# tutorial):

    [Gtk.TreeNode (ListOnly=true)]
    public class MyTreeNode : Gtk.TreeNode {

            string song_title;

            public MyTreeNode (string artist, string song_title)
            {
                    Artist = artist;
                    this.song_title = song_title; 
            }

            [Gtk.TreeNodeValue (Column=0)]
            public string Artist;

            [Gtk.TreeNodeValue (Column=1)]
            public string SongTitle {get { return song_title; } }
    }

Gtk.NodeStore store;
    Gtk.NodeStore Store 
    {
        get {
                if (store == null)
                {
                    store = new Gtk.NodeStore (typeof(MyTreeNode));
                    store.AddNode (new MyTreeNode ("The Beatles", "Yesterday"));
                    store.AddNode (new MyTreeNode ("Peter Gabriel", "In Your Eyes"));
                    store.AddNode (new MyTreeNode ("Rush", "Fly By Night"));
                }
            return store;
            }
    }

protected void OnButton1Clicked (object sender, EventArgs e)
{
    // Create a column with title Artist and bind its renderer to model column 0
    nodeview1.AppendColumn ("Artist", new Gtk.CellRendererText (), "text", 0);

    // Create a column with title 'Song Title' and bind its renderer to model column 1
    nodeview1.AppendColumn ("Song Title", new Gtk.CellRendererText (), "text", 1);
        nodeview1.ShowAll ();

    nodeview1.NodeStore=Store;

}

But how can I color some rows of NodeView ("The Beatles" - "Yesterday", for example)? I tried to do it by changing NodeView Style's: Backgrounds, BaseColors, Foregrounds and other but it's doesn't work.

Edit: I just realized, that I can change color of the column this way:

protected void OnButton1Clicked (object sender, EventArgs e)
{
    // Create a column with title Artist and bind its renderer to model column 0
    nodeview1.AppendColumn ("Artist", new Gtk.CellRendererText (), "text", 0);



    // Create a column with title 'Song Title' and bind its renderer to model column 1
    nodeview1.AppendColumn ("Song Title", new Gtk.CellRendererText (), "text", 1);
        nodeview1.ShowAll ();

    nodeview1.NodeStore=Store;
    nodeview1.Columns[0].Cells[0].CellBackgroundGdk=new Gdk.Color(0,255,0);
}

but how can I change the color of a specific cell?

like image 659
Mixim Avatar asked Nov 03 '22 07:11

Mixim


1 Answers

To change rendering attributes on per-row basis you have two possibilities

  1. define the attributes in your model and then references them when you create the cell renderers; or
  2. write your own TreeCellDataFunc and bind it to the cell renderer to make sure you change its attributes before the rendering.

Option 1 is faster but limits you to statically defined values; 2 is better when the rendering depends on more than just one variable and you have some logic involved. I'll show you how to do 1 and refer to Mono documentation for 2.

If you want to change the foreground color you just need to add a new column to your model:

[Gtk.TreeNode (ListOnly=true)]
public class MyTreeNode : Gtk.TreeNode 
{
    public MyTreeNode (string artist, string title, string color)
    {
        Artist = artist;
        Title = title; 
        Color = color;
    }

    [Gtk.TreeNodeValue (Column=0)]
    public string Artist;

    [Gtk.TreeNodeValue (Column=1)]
    public string Title;

    [Gtk.TreeNodeValue (Column=2)]
    public string Color;
}

passing a valid Gdk color representation, like "red" or "#ff0000" to the row constructor:

store.AddNode(new MyTreeNode("The Beatles", "Yesterday", "red"));
store.AddNode(new MyTreeNode("Peter Gabriel", "In Your Eyes", "black"));

Then when you build your view you just need to bind that model column (2) to the "foreground" property of the cell renderer:

nodeview1.AppendColumn("Artist", new Gtk.CellRendererText(), 
                       "text", 0, "foreground", 2);

nodeview1.AppendColumn("Song Title", new Gtk.CellRendererText(),
                       "text", 1, "foreground", 2);

That's all.

like image 143
fog Avatar answered Nov 13 '22 23:11

fog