WIP: Now : how to display in our window (/panel) what was stored in these models ?
This commit is contained in:
parent
96bb834615
commit
9898be565d
177
src/ui/tree.c
177
src/ui/tree.c
|
@ -29,7 +29,8 @@
|
||||||
#include "../../include/ui.h"
|
#include "../../include/ui.h"
|
||||||
|
|
||||||
GtkListStore *list_store;
|
GtkListStore *list_store;
|
||||||
GtkTreeIter iter;
|
GtkTreeStore *tree_store;
|
||||||
|
GtkTreeIter iter, child;
|
||||||
|
|
||||||
/***********************************************************
|
/***********************************************************
|
||||||
* *
|
* *
|
||||||
|
@ -40,7 +41,6 @@ GtkTreeIter iter;
|
||||||
void
|
void
|
||||||
traverse_list_store (GtkListStore *liststore)
|
traverse_list_store (GtkListStore *liststore)
|
||||||
{
|
{
|
||||||
GtkTreeIter iter;
|
|
||||||
gboolean valid;
|
gboolean valid;
|
||||||
|
|
||||||
g_return_if_fail ( liststore != NULL );
|
g_return_if_fail ( liststore != NULL );
|
||||||
|
@ -74,7 +74,6 @@ void
|
||||||
onTreeViewRowActivated (GtkTreeView *view, GtkTreePath *path,
|
onTreeViewRowActivated (GtkTreeView *view, GtkTreePath *path,
|
||||||
GtkTreeViewColumn *col, gpointer userdata)
|
GtkTreeViewColumn *col, gpointer userdata)
|
||||||
{
|
{
|
||||||
GtkTreeIter iter;
|
|
||||||
GtkTreeModel *model;
|
GtkTreeModel *model;
|
||||||
|
|
||||||
model = gtk_tree_view_get_model(view);
|
model = gtk_tree_view_get_model(view);
|
||||||
|
@ -101,12 +100,176 @@ tree_c_printing_test(void)
|
||||||
gtk_list_store_append(list_store, &iter);
|
gtk_list_store_append(list_store, &iter);
|
||||||
gtk_list_store_append(list_store, &iter);
|
gtk_list_store_append(list_store, &iter);
|
||||||
|
|
||||||
|
tree_store = gtk_tree_store_new(1, G_TYPE_STRING);
|
||||||
|
|
||||||
|
/* Append an empty top-level row to the tree store.
|
||||||
|
* Iter will point to the new row */
|
||||||
|
gtk_tree_store_append(tree_store, &iter, NULL);
|
||||||
|
|
||||||
|
/* Append another empty top-level row to the tree store.
|
||||||
|
* Iter will point to the new row */
|
||||||
|
gtk_tree_store_append(tree_store, &iter, NULL);
|
||||||
|
|
||||||
|
/* Append a child to the row we just added.
|
||||||
|
* Child will point to the new row */
|
||||||
|
gtk_tree_store_append(tree_store, &child, &iter);
|
||||||
|
|
||||||
|
/* Get the first row, and add a child to it as well (could have been done
|
||||||
|
* right away earlier of course, this is just for demonstration purposes) */
|
||||||
|
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(tree_store), &iter))
|
||||||
|
{
|
||||||
|
/* Child will point to new row */
|
||||||
|
gtk_tree_store_append(tree_store, &child, &iter);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
g_error("Oops, we should have a first row in the tree store!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
printf("tree_c_printing_test() \
|
printf("tree_c_printing_test() \
|
||||||
---------------------------------------------------------\n\n\
|
---------------------------------------------------------\n\n\
|
||||||
(https://en.wikibooks.org/wiki/GTK (modulo) 2B_By_Example/Tree_View/Tree_Models)\n\n\
|
(https://en.wikibooks.org/wiki/GTK (modulo) 2B_By_Example/Tree_View/Tree_Models)\n\n\
|
||||||
cf. tuto : gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_UINT) - (cf. line 69)\n\
|
cf. tuto : gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_UINT); - (cf. line 69)\n\
|
||||||
creates a new list store with two columns.\n\
|
creates a new list store with two columns.\n\n\
|
||||||
Column 0 stores a string and column 1 stores an unsigned integer for each row.\n\
|
gtk_tree_store_new(1, G_TYPE_STRING); - (cf. line 108)\n\
|
||||||
Three empty lines have been added to the model - (cf. line 100).\n");
|
creates a new tree store with one column.\n\n\
|
||||||
|
Now : how to display in our window (/panel) what was stored in these models ?.\n\
|
||||||
|
(see the commented code at the end)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
https://stackoverflow.com/questions/51002454/how-to-list-all-the-rows-of-a-gtk-treeview
|
||||||
|
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using Gtk;
|
||||||
|
|
||||||
|
public class Actress
|
||||||
|
{
|
||||||
|
public string Name;
|
||||||
|
public string Place;
|
||||||
|
public int Year;
|
||||||
|
|
||||||
|
public Actress(string name, string place, int year)
|
||||||
|
{
|
||||||
|
Name = name;
|
||||||
|
Place = place;
|
||||||
|
Year = year;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SharpApp : Window
|
||||||
|
{
|
||||||
|
ListStore store;
|
||||||
|
Statusbar statusbar;
|
||||||
|
|
||||||
|
enum Column
|
||||||
|
{
|
||||||
|
Name,
|
||||||
|
Place,
|
||||||
|
Year
|
||||||
|
}
|
||||||
|
|
||||||
|
Actress[] actresses =
|
||||||
|
{
|
||||||
|
new Actress("Jessica Alba", "Pomona", 1981),
|
||||||
|
new Actress("Sigourney Weaver", "New York", 1949),
|
||||||
|
new Actress("Angelina Jolie", "Los Angeles", 1975),
|
||||||
|
new Actress("Natalie Portman", "Jerusalem", 1981),
|
||||||
|
new Actress("Rachel Weissz", "London", 1971),
|
||||||
|
new Actress("Scarlett Johansson", "New York", 1984)
|
||||||
|
};
|
||||||
|
|
||||||
|
public SharpApp() : base ("ListView")
|
||||||
|
{
|
||||||
|
BorderWidth = 8;
|
||||||
|
|
||||||
|
SetDefaultSize(350, 250);
|
||||||
|
SetPosition(WindowPosition.Center);
|
||||||
|
DeleteEvent += delegate { Application.Quit(); };
|
||||||
|
|
||||||
|
VBox vbox = new VBox(false, 8);
|
||||||
|
|
||||||
|
|
||||||
|
ScrolledWindow sw = new ScrolledWindow();
|
||||||
|
sw.ShadowType = ShadowType.EtchedIn;
|
||||||
|
sw.SetPolicy(PolicyType.Automatic, PolicyType.Automatic);
|
||||||
|
vbox.PackStart(sw, true, true, 0);
|
||||||
|
|
||||||
|
store = CreateModel();
|
||||||
|
|
||||||
|
TreeView treeView = new TreeView(store);
|
||||||
|
treeView.RulesHint = true;
|
||||||
|
treeView.RowActivated += OnRowActivated;
|
||||||
|
sw.Add(treeView);
|
||||||
|
|
||||||
|
AddColumns(treeView);
|
||||||
|
|
||||||
|
statusbar = new Statusbar();
|
||||||
|
|
||||||
|
vbox.PackStart(statusbar, false, false, 0);
|
||||||
|
|
||||||
|
Add(vbox);
|
||||||
|
ShowAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
void OnRowActivated (object sender, RowActivatedArgs args) {
|
||||||
|
|
||||||
|
TreeIter iter;
|
||||||
|
TreeView view = (TreeView) sender;
|
||||||
|
|
||||||
|
if (view.Model.GetIter(out iter, args.Path)) {
|
||||||
|
string row = (string) view.Model.GetValue(iter, (int) Column.Name );
|
||||||
|
row += ", " + (string) view.Model.GetValue(iter, (int) Column.Place );
|
||||||
|
row += ", " + view.Model.GetValue(iter, (int) Column.Year );
|
||||||
|
statusbar.Push(0, row);
|
||||||
|
}
|
||||||
|
// *** if I can dump treeview to the console here I'll be happy ***
|
||||||
|
// *** I'd prefer a foreach or do/while ***
|
||||||
|
}
|
||||||
|
|
||||||
|
void AddColumns(TreeView treeView)
|
||||||
|
{
|
||||||
|
CellRendererText rendererText = new CellRendererText();
|
||||||
|
TreeViewColumn column = new TreeViewColumn("Name", rendererText,
|
||||||
|
"text", Column.Name);
|
||||||
|
column.SortColumnId = (int) Column.Name;
|
||||||
|
treeView.AppendColumn(column);
|
||||||
|
|
||||||
|
rendererText = new CellRendererText();
|
||||||
|
column = new TreeViewColumn("Place", rendererText,
|
||||||
|
"text", Column.Place);
|
||||||
|
column.SortColumnId = (int) Column.Place;
|
||||||
|
treeView.AppendColumn(column);
|
||||||
|
|
||||||
|
rendererText = new CellRendererText();
|
||||||
|
column = new TreeViewColumn("Year", rendererText,
|
||||||
|
"text", Column.Year);
|
||||||
|
column.SortColumnId = (int) Column.Year;
|
||||||
|
treeView.AppendColumn(column);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
ListStore CreateModel()
|
||||||
|
{
|
||||||
|
ListStore store = new ListStore( typeof(string),
|
||||||
|
typeof(string), typeof(int) );
|
||||||
|
|
||||||
|
foreach (Actress act in actresses) {
|
||||||
|
store.AppendValues(act.Name, act.Place, act.Year );
|
||||||
|
}
|
||||||
|
|
||||||
|
return store;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Main()
|
||||||
|
{
|
||||||
|
Application.Init();
|
||||||
|
new SharpApp();
|
||||||
|
Application.Run();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue