Home > Mobile >  Gtk# TreeView, not using certail columns for non-leaf nodes?
Gtk# TreeView, not using certail columns for non-leaf nodes?

Time:05-03

The code below shows the second column for non-leaf nodes. But that is not what I wanted. Can I make only leaf nodes have the second column?

enter image description here

        var languages = new TreeViewColumn();
        languages.Title = "Programming languages";

        var efficiency = new TreeViewColumn();
        efficiency.Title = "Efficiency";

        var cell = new CellRendererText();
        languages.PackStart(cell, true);
        languages.AddAttribute(cell, "text", 0);

        var cell2 = new CellRendererProgress();
        efficiency.PackStart(cell2, true);
        efficiency.AddAttribute(cell2, "value", 1);

        var treestore = new TreeStore(typeof(string), typeof(int));

        var iter = treestore.AppendValues("scripting languages");
        treestore.AppendValues(iter, "Python", 10);

        iter = treestore.AppendValues("compiling");
        treestore.AppendValues(iter, "C#", 20);

        tree.AppendColumn(languages);
        tree.AppendColumn(efficiency);
        tree.Model = treestore;
        tree.ExpandAll();

CodePudding user response:

After tens of minutes of searching, trial & error (because Gtk# lacks any documentation and there are not many examples on line), I got it working. I'm not sure if this is the most efficient way, but at least it is working. If someone finds a better way, please post it; I will mark that as the answer.

The hint was enter image description here

CodePudding user response:

I haven't worked with "C#" in a while, but researching how to hide a specific renderer cell, I came across the following link.

"https://stackoverflow.com/questions/35057446/python3-gtk3-how-to-hide-a-cell-of-a-row"

The information to build on here is as follows:

  • Include a hidden column in your tree store that can hold a boolean value.
  • Add a "visibility" attribute to the progress bar cell that references the value that will be placed within hidden column cell for each row.
  • For those cells that should display the progress bar, set the associated hidden column cell to a value of "TRUE".
  • For those cells that should not display the progress bar, set the associated hidden column cell to a value of "FALSE".

Using this information, I built a sample program that had a tree view with one of the columns being a progress bar. I won't list out the whole program here, but I will paste in some of the key bits. I realize this will be C code, but one should be able to interpret what the equivalent code would be in C#.

First off, I define the columns that I want in the tree view (two viewable columns, and a hidden column).

enum
{
    COLUMN = 0,
    PROG = 1,
    VIS = 2,
    N_COLS          /* Used as a terminator/counter */
}   

Then, I create a view and model. Following is a snippet of the code to set up the cell renderers for the two visible columns.

renderer    = gtk_cell_renderer_text_new();
renderer2   = gtk_cell_renderer_progress_new();

gtk_tree_view_column_set_title(col, "Programming Languages");
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
gtk_tree_view_column_pack_start(col, renderer, TRUE);    /* This will be visible on the tree view */
gtk_tree_view_column_add_attribute(col, renderer, "text", COLUMN);

gtk_tree_view_column_set_title(col2, "Percentage");
gtk_tree_view_append_column(GTK_TREE_VIEW(view), col2);
gtk_tree_view_column_pack_start(col2, renderer2, TRUE);  /* This will be visible on the tree view */
gtk_tree_view_column_add_attribute(col2, renderer2, "value", PROG);
gtk_tree_view_column_add_attribute(col2, renderer2, "visible", VIS);

The last line of code above is the additional bit I think you are looking for to control the visibility of the progress bar cell..

Next, I define that the tree view will have three columns (the last one will be hidden).

treestore = gtk_tree_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_INT, G_TYPE_BOOLEAN);

gtk_tree_store_append(treestore, &toplevel, NULL);
gtk_tree_store_set(treestore, &toplevel, COLUMN, "Scripting Languages", VIS, FALSE, -1);
gtk_tree_store_append(treestore, &child, &toplevel);
gtk_tree_store_set(treestore, &child, COLUMN, "Python", PROG, 62, VIS, TRUE, -1);
/* And so forth for other rows */

The result of this produces a tree view with conditioned display of the progress bar cell.

Sample tree view

As noted, I built this example in C, but the method of adding a visibility attribute to the progress bar cell and making its visibility dependent upon the value in a hidden boolean cell/column should be easily interpreted to C#.

  • Related