[Gtk-sharp-list] Row background color

Vincent Arnoux vincent.arnoux@rfo.atmel.com
Mon, 28 Feb 2005 11:34:29 +0100


Christian Rudh a =E9crit :

>Hi
>
>I have an application where it makes some text in a treeview bold, but
>it should be possible to use for setting the background too:
>
>This is the column I set up in the beginning:
>
>TreeViewColumn displayCol =3D new TreeViewColumn ();
>CellRendererText displayRenderer =3D new CellRendererText ();
>displayRenderer.Xpad =3D 10;
>displayCol.Title =3D "Filedisplay";
>displayCol.PackStart (displayRenderer, true);
>displayCol.SetCellDataFunc (displayRenderer, new TreeCellDataFunc
>(MarkUpSongName));
>playList.AppendColumn (displayCol);
>
>
>If you look at the SetCellDataFunc, it calls MarkUpSongName when there
>is a redraw.
>
>This is the method:
>
>
>private void MarkUpSongName (TreeViewColumn treeColumn, CellRenderer
>cellRenderer, TreeModel treeModel, TreeIter treeIter)
>{
>	GLib.Value valx =3D new GLib.Value ();
>	store.GetValue (treeIter, 1, ref valx);
>	string completeName =3D (string) valx.Val;
>	=09
>	GLib.Value valy =3D new GLib.Value ();
>	store.GetValue (treeIter, 0, ref valy);
>	string hiddenName =3D (string) valy.Val;
>		=09
>	CellRendererText textCellRenderer =3D (CellRendererText) cellRenderer;
>
>	if (hiddenName =3D=3D null)
>	{
>		store.SetValue (treeIter, 0, "x");
>		textCellRenderer.Markup =3D "";
>	}
>	else if (treeIter.Equals (nextToPlay))
>	{
>		textCellRenderer.Markup =3D "<b>" + completeName + "</b>";
>	}
>	else
>	{
>		textCellRenderer.Markup =3D completeName;
>	}
>}
>
>You should be able to change this function to set
>textCellRenderer.Background instead of textCellRenderer.Markup if the
>columns contain the correct text (red). Then you don't have to have a
>function going through the rows, you simply wait for a redraw or call it
>yourself with the QueueDraw method in the treeview.
>
>I tried the background setting in my code and it worked fine.
>
>/Christian
>
Thanks for your answer. I received an e-mail advising me to have a look=20
at the source of http://veldstra.co.uk/sharplist/. Thanks to this code,=20
I wrote the following :

        // Build the List
        jobsStore =3D new ListStore (typeof (string), typeof (string),=20
typeof (string), typeof (int), typeof (int), typeof(string),=20
typeof(string));
        tv =3D new TreeView ();
        tv.Model =3D jobsStore;
        tv.HeadersVisible =3D true;
        tv.RulesHint =3D true;
        tv.Reorderable =3D true;

        // Category column
        CellRendererText crt1 =3D new CellRendererText();
        TreeViewColumn col1 =3D new TreeViewColumn ("Categorie", crt1,=20
"text", 0);       =20
        col1.AddAttribute (crt1, "weight", 3);
        col1.AddAttribute (crt1, "style", 4);
        col1.AddAttribute (crt1, "foreground", 5);
        col1.AddAttribute (crt1, "background", 6);
        tv.AppendColumn(col1);
       =20
        // Job column
        CellRendererText crt2 =3D new CellRendererText();
        TreeViewColumn col2 =3D new TreeViewColumn ("Job", crt2, "text", =
1);
        col2.AddAttribute (crt2, "weight", 3);
        col2.AddAttribute (crt2, "style", 4);
        col2.AddAttribute (crt2, "foreground", 5);
        col2.AddAttribute (crt2, "background", 6);
        tv.AppendColumn(col2);
       =20
        // State column
        CellRendererText crt3 =3D new CellRendererText();
        TreeViewColumn col3 =3D new TreeViewColumn ("Etat", crt3, "text",=
 2);
        col3.AddAttribute (crt3, "weight", 3);
        col3.AddAttribute (crt3, "style", 4);
        col3.AddAttribute (crt3, "foreground", 5);
        col3.AddAttribute (crt3, "background", 6);
        tv.AppendColumn(col3);

        tv.Selection.Changed +=3D new EventHandler (OnSelectionChanged);
        sw.Add(tv);
        tv.Show();

And then to browse and change one of the lines parameter :

        TreeIter iter;
        TreeModel model;
        ArrayList jobsToDownloadAgain =3D new ArrayList();
        if (jobsStore.GetIterFirst(out iter))
        {
            do
            {
                    // Job has not been copied, its state is NOK and bgd =
red
                    jobsStore.SetValue (iter, 2, "Done");
                    jobsStore.SetValue (iter, 6, "red");
            }
            while (jobsStore.IterNext(ref iter));
        }

But in my case, I needed to browse the rows, where in your code you don't=
=2E..

Thanks a lot,
Vincent