[Mono-docs-list] Miguel asked me to pass this to you.

George Farris farrisg@shaw.ca
Fri, 28 Feb 2003 18:46:01 -0800


--Boundary_(ID_NI1B5cFUpVBiK8LFr18u7Q)
Content-type: text/plain
Content-transfer-encoding: 7BIT

I have written some very preliminary docs for the TreeView widget. 
Please feel free to include them in the docs for GTK#.  I will expand
the documentation to include selections and also make what I provide
here much better over the long run but this should allow people to get
stated.

Let me know what you think.

-- 
George Farris <farrisg@shaw.ca>

--Boundary_(ID_NI1B5cFUpVBiK8LFr18u7Q)
Content-type: text/html; name=treeview.html; charset=ISO-8859-1
Content-transfer-encoding: 7BIT
Content-disposition: attachment; filename=treeview.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
	<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
	<TITLE>The Mono Handbook - GNOME.NET - GTK#</TITLE>
</HEAD>
<BODY LANG="en-US">
<P><BR>
</P>
<H1>GTK#</H1>
<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. 
</P>
<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 text, pixbuf or toggle
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><BR><BR>
</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="margin-bottom: 0.5cm">ListStore store = null;</PRE><P>
Next we create a new TreeView widget and associate our newly created
ListStore with the TreeView.</P>
<PRE STYLE="margin-bottom: 0.5cm">TreeView tv = new TreeView (store);</PRE><P>
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="margin-bottom: 0.5cm">tv.HeadersVisible = 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
text to add to our column. 
</P>
<P><BR><BR>
</P>
<PRE>TreeViewColumn NameCol = new TreeViewColumn ();
CellRenderer NameRenderer = new CellRendererText ();
NameCol.Title = &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. 
</P>
<PRE>mywidget.Add (tv);</PRE><P>
mywidget.ShowAll ();</P>
<P><BR><BR>
</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 string. 
</P>
<P>store = 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 bool and one of type
string.</P>
<PRE>store = new ListStore ((int)TypeFundamentals.TypeBool,(int)TypeFundamentals.TypeString);

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

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

Great now lets put four rows of text data into the list.  The ListStore and TreeStore actually 
store a Glib.Value not a text string so first we must store our string in a Glib.Value then 
we can set the value in the ListStore.  Note the 0 in the SetValue call is acually the 
column we are writing data to.
<PRE>
for (int i = 0; i &lt; 4; i++) {
  GLib.Value value = new Glib.Value(data +i.ToString());
  store.Append (out iter);
  store.SetValue (iter, 0, value);
}
</PRE>
<H2>Example</H2>
<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 = null;
                                                
                        Application.Init ();


                        store = new TreeStore ((int)TypeFundamentals.TypeString,
                                               (int)TypeFundamentals.TypeString);


                        TreeIter iter = new TreeIter ();
                        
                        for (int i=0; i&lt;10; i++)
                        {
                                GLib.Value Name = new GLib.Value (&quot;Demo &quot; +
i.ToString());
                                GLib.Value Type = new GLib.Value (&quot;Data &quot; +
i.ToString());
                                store.Append (out iter);
                                store.SetValue (iter, 0, Name);
                                store.SetValue (iter, 1, Type);
                        }
                        
                        Window win = new Window (&quot;TreeView List Demo&quot;);
                        win.DeleteEvent += new DeleteEventHandler (delete_cb);
                        win.DefaultSize = new Size (400,250);


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


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


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


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


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


                private static void delete_cb (System.Object o, DeleteEventArgs
args)
                {
                        Application.Quit ();
                        args.RetVal = true;
                }
        }
}</PRE>
</BODY>
</HTML>

--Boundary_(ID_NI1B5cFUpVBiK8LFr18u7Q)--