[Gtk-sharp-list] Using ListViews/TreeViews in Gtk#?

George Farris farrisg@mala.bc.ca
04 Mar 2003 07:59:40 -0800

Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

I did some preliminary docs for TreeView with a ListStore model and sent
them to the mono-doc mail list, have heard a peep????  I'll included it
here for you.  I haven't written the selection information yet though I
do have examples.

On Tue, 2003-03-04 at 07:39, Charles-Louis wrote:
> Le mar 04/03/2003 =E0 16:33, Jorge De Gante a =E9crit :
> > Hi Charles
> >=20
> >=20
> > Check the sample that comes with gtk-sharp, the file is TreeViewDemo.cs
> > or the source of the doc browser browser.cs, both samples are about
> > treeviews.
> I already looked at the TreeViewDemo.cs (it seems I don't have the doc
> browser? which package should I download?), but it's using an iterator
> to populate the treeview with an XML file.
> I'm writing a LogViewer, and I have to be able to add items line per
> line (eg. when clicking on a Button)
> Aren't there any methods like TreeView.Add(item)?
> >=20
> > El mar, 04-03-2003 a las 07:29, Charles-Louis escribi=F3:
> > > Hello,
> > >=20
> > > I'm pretty new to programming with Gtk and Glade. I've designed an
> > > application with Glade that should use ListViews (but I found out tha=
> > > ListViews were deprecated... so I've used a TreeView instead).
> > >=20
> > > I wanted to know how I can populate these Views=20
> > >=20
> > > eg. ---------------------------------------
> > >     |First Name   |Last Name    | Info    |
> > >     ---------------------------------------
> > >     |Chuck        |Berry        | Artist  |
> > >     ---------------------------------------
> > >     |John         |Johnson      | Baker   |
> > >     ---------------------------------------
> > >=20
> > >=20
> > > Regards
> > > --=20
> > > Charles-Louis <charlouis.mono@wanadoo.be>
> > >=20
> > > _______________________________________________
> > > Gtk-sharp-list maillist  -  Gtk-sharp-list@lists.ximian.com
> > > http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Content-Disposition: attachment; filename=treeview.html
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; name=treeview.html; charset=ANSI_X3.4-1968

	<META HTTP-EQUIV=3D"CONTENT-TYPE" CONTENT=3D"text/html; charset=3Dutf-8">
	<TITLE>The Mono Handbook - GNOME.NET - GTK#</TITLE>
<H2>Miscellaneous Widgets</H2>
<H3>Tree and List Widget</H3>
<P>The TreeView widget is one of the more complex GTK widgets. It is
well worth while spending some time studing it carefully.=20
<P>These widgets are designed around a <I>Model/View/Controller</I>
design and consists of four major parts:</P>
<P><B>GtkTreeView :</B> the tree view widget</P>
<P><B>GtkTreeViewColumn</B> : the view column</P>
<P><B>GtkCellRenderer</B> : the cell renderers</P>
<P><B>GtkTreeModel</B> : the model interface</P>
<P>A quick look at the situation boils down to this:</P>
<P>CellRender -----&gt; TreeViewColumn -----&gt; TreeView</P>
<P>A cell (CellRenderer) of type =E2=80=9Ctext=E2=80=9D, =E2=80=9Cpixbuf=E2=
=80=9D or =E2=80=9Ctoggle=E2=80=9D
is created and packed into a newly created column (TreeViewColumn).
The column in turn is appended to the tree (TreeView). Data is added
to the tree model (TreeStore or ListStore) and the model is
associated with the newly created tree.</P>
<P>Lets start with a simple example and explain as we go:</P>
<P>We'll create a simple list view with one column and text data
stored in it. We could of course create multiple columns of different
types but for our purposes, simple is better.</P>
<P>Lets create the TreeView widget first. We'll assume that the
parent widget that we are going to pack the TreeView widget into has
already been created.</P>
<P>First we create a ListStore for holding the data that our list
widget is going to display.</P>
<P>The ListStore object is a list model for use with a TreeView
widget. It implements the TreeModel interface, and consequentialy,
can use all of the methods available there. It also implements the
TreeSortable interface so it can be sorted by the view. Finally, it
also implements the tree interfaces.</P>
<PRE STYLE=3D"margin-bottom: 0.5cm">ListStore store =3D null;</PRE><P>
Next we create a new TreeView widget and associate our newly created
ListStore with the TreeView.</P>
<PRE STYLE=3D"margin-bottom: 0.5cm">TreeView tv =3D new TreeView (store);</=
Here we assign attributes to the TreeView such as making sure headers
are on. Headers refer to the column headers that appear as buttons
and may be tied to such functions as sorting the view of column data.</P>
<PRE STYLE=3D"margin-bottom: 0.5cm">tv.HeadersVisible =3D true;</PRE><P>
Now we need to add columns to our TreeView. We are going to add one
column. We get a new TreeViewColumn and a new CellRenderer of type
=E2=80=9Ctext=E2=80=9D to add to our column.=20
<PRE>TreeViewColumn NameCol =3D new TreeViewColumn ();
CellRenderer NameRenderer =3D new CellRendererText ();
NameCol.Title =3D &quot;Name&quot;;
NameCol.PackStart (NameRenderer, true);
NameCol.AddAttribute (NameRenderer, &quot;text&quot;, 0);
tv.AppendColumn (NameCol);</PRE><P>
Since we aren't going to add any more columns to our TreeView we can
finish by packing it into whatever parent widget we have.=20
<PRE>mywidget.Add (tv);</PRE><P>
mywidget.ShowAll ();</P>
<P>Our next task is to to populate the TreeView with data. Data is
stored in the ListStore that was created as the first step above.
When a ListStore or TreeStore is created we tell it what type of data
the store is holding for each column . In the example below we have
one column of type =E2=80=9Cstring=E2=80=9D.=20
<P>store =3D new ListStore ((int)TypeFundamentals.TypeString);</P>
<P>If we had more than one column we would expand the creation of the
ListStore to include those columns and their type. Here is an example
that creates two columns, one of type =E2=80=9Cbool=E2=80=9D and one of typ=
<PRE>store =3D new ListStore ((int)TypeFundamentals.TypeBool,(int)TypeFunda=

Right now lets put some data in our list.  We need to get a new TreeIter.  =
A TreeIter is used=20
to store the location where the data will be stored.  The TreeIter is updat=
ed automatically=20
here bye a call to store.Append, we don't actually set the TreeIter.  We ne=
ed to=20
append a TreeIter to the store to before we can store data in it.

<PRE>TreeIter iter =3D new TreeIter ();</PRE>

Great now lets put four rows of text data into the list.  The ListStore and=
 TreeStore actually=20
store a Glib.Value not a text string so first we must store our string in a=
 Glib.Value then=20
we can set the value in the ListStore.  Note the =E2=80=9C0=E2=80=9D in the=
 SetValue call is acually the=20
column we are writing data to.
for (int i =3D 0; i &lt; 4; i++) {
  GLib.Value value =3D new Glib.Value(=E2=80=9Cdata =E2=80=9D+i.ToString())=
  store.Append (out iter);
  store.SetValue (iter, 0, value);
<P>Here is a complete example of what we have just done above
complete with toplevel window.</P>
<PRE>namespace Samples {
        using System;
        using System.Drawing;
        using GLib;
        using Gtk;
        using GtkSharp;

        public class TreeView {
                public static void Main (string[] args)
                        TreeStore store =3D null;
                        Application.Init ();

                        store =3D new TreeStore ((int)TypeFundamentals.Type=

                        TreeIter iter =3D new TreeIter ();
                        for (int i=3D0; i&lt;10; i++)
                                GLib.Value Name =3D new GLib.Value (&quot;D=
emo &quot; +
                                GLib.Value Type =3D new GLib.Value (&quot;D=
ata &quot; +
                                store.Append (out iter);
                                store.SetValue (iter, 0, Name);
                                store.SetValue (iter, 1, Type);
                        Window win =3D new Window (&quot;TreeView List Demo=
                        win.DeleteEvent +=3D new DeleteEventHandler (delete=
                        win.DefaultSize =3D new Size (400,250);

                        ScrolledWindow sw =3D new ScrolledWindow ();
                        win.Add (sw);

                        TreeView tv =3D new TreeView (store);
                        tv.HeadersVisible =3D true;

                        TreeViewColumn DemoCol =3D new TreeViewColumn ();
                        CellRenderer DemoRenderer =3D new CellRendererText =
                        DemoCol.Title =3D &quot;Demo&quot;;
                        DemoCol.PackStart (DemoRenderer, true);
                        DemoCol.AddAttribute (DemoRenderer, &quot;text&quot=
;, 0);
                        tv.AppendColumn (DemoCol);

                        TreeViewColumn DataCol =3D new TreeViewColumn ();
                        CellRenderer DataRenderer =3D new CellRendererText =
                        DataCol.Title =3D &quot;Data&quot;;
                        DataCol.PackStart (DataRenderer, false);
                        DataCol.AddAttribute (DataRenderer, &quot;text&quot=
;, 1);
                        tv.AppendColumn (DataCol);

                        sw.Add (tv);
                        win.ShowAll ();
                        Application.Run ();

                private static void delete_cb (System.Object o, DeleteEvent=
                        Application.Quit ();
                        args.RetVal =3D true;