Re: still on treeview



On Sat, 2002-12-28 at 17:41, Ottavio Campana wrote:
> I've created the treeview with all the columns I need.
> 
> Now I  need to  know how to  add rows to  it e  how to remove  them. The
> tutorial has got just an empty page for it....
> 
> Do you have got an example to understand how to do it?
> 

I am attaching a test program I wrote to try and understand the treeview
widget better myself. The treeview is a pretty complex widget to figure
out at first but you should be able to get a start on things with whats
there.

-- 
Stephen Witkop <switkop charter net>
#include <gtk/gtk.h>

typedef struct
{
  const gchar *note;
  const gint   number;
  const gchar  operator;
} Row;

typedef struct 
{
  GtkWidget   *tree;
  GtkTreeIter  current_row;
  gint         last_row;
  
  void (* append_row) (GtkWidget *widget, gpointer data);
  void (* delete_row) (GtkWidget *widget, gpointer data);
} ListView;

enum
  {
    COLUMN_NOTE,
    COLUMN_NUMBER,
    COLUMN_OPERATOR,
    N_COLUMNS
  };

void
delete_event (GtkWidget *widget, GdkEvent *event, gpointer data) {
  gtk_main_quit ();
}

/* Scroll to th specified cell */
void scroll_to_cell (GtkTreeView *tree, GtkTreeModel *model, GtkTreeIter *iter)
{
  GtkTreePath *row_path;
  
  row_path = gtk_tree_model_get_path (model, iter);
  gtk_tree_view_scroll_to_cell (tree, row_path,
				NULL, TRUE, 1, 0);
}

void 
add_row (GtkWidget *widget,  gpointer data) {
  GtkTreeModel *model;
  GtkTreeIter iter;
  gint count = 2; /* start at 2, initial row is #1, this is the next row */
  ListView *lv =data;
  
  model = gtk_tree_view_get_model (GTK_TREE_VIEW(lv->tree));
  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      while (gtk_tree_model_iter_next (model, &iter))
	{
	  count++;
	}
      lv->last_row = count;
      
      gtk_list_store_insert (GTK_LIST_STORE(model), &iter, count);
      gtk_list_store_set (GTK_LIST_STORE(model), &iter,
			  COLUMN_NOTE, "A New Note",
			  COLUMN_NUMBER, lv->last_row,
			  COLUMN_OPERATOR, "-", -1);
      
      scroll_to_cell (GTK_TREE_VIEW(lv->tree), model, &iter);
    }
  
  g_message ("Add, last_row is: %d", lv->last_row);
  
}

void
remove_row (GtkWidget *widget, gpointer data) 
{
  GtkTreeModel *model;
  GtkTreeIter  iter;
  gint count = 0;
  ListView *lv = data;
  
  model = gtk_tree_view_get_model (GTK_TREE_VIEW(lv->tree));
  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      GtkTreeIter prev_iter;
      while (gtk_tree_model_iter_next (model, &iter))
	{
	  prev_iter = iter;
	  count++;
	}
      lv->last_row = count;
      gtk_list_store_remove (GTK_LIST_STORE(model), &lv->current_row);
    }
  
  g_message ("Remove, last row is %d", lv->last_row);
  
}

void
cell_changed (GtkCellRendererText *cell, 
	      gchar *arg1, gchar *arg2, 
	      gpointer data)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  ListView *lv = data;
  
  model = gtk_tree_view_get_model (GTK_TREE_VIEW(lv->tree));
  
  if (gtk_tree_model_get_iter_from_string (model, &iter, arg1)) {
    gtk_list_store_set (GTK_LIST_STORE(model), &iter,
			COLUMN_NOTE, arg2, -1);
  }
  
  g_message ("cell_changed called");
}

void
cell_op_changed (GtkCellRendererText *cell, 
		 gchar *arg1, gchar *arg2, 
		 gpointer data)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  ListView *lv = data;
  
  model = gtk_tree_view_get_model (GTK_TREE_VIEW(lv->tree));
  
  if (gtk_tree_model_get_iter_from_string (model, &iter, arg1)) {
    gtk_list_store_set (GTK_LIST_STORE(model), &iter,
	              COLUMN_OPERATOR, arg2, -1);
  }
  
  g_message ("cell_op_changed called");
}

void
selection_changed (GtkTreeSelection *select, gpointer data) 
{
  GtkTreeIter iter;
  GtkTreeModel *model;
  GtkTreeViewColumn *column;
  GList *list;
  GdkColor color;
  gchar *string;
  ListView *lv = data;
  
  if (gtk_tree_selection_get_selected (select, &model, &iter))
    {
      gtk_tree_model_get (model, &iter, 
			  COLUMN_NOTE, &string, -1);
      
      g_print ("The Note was: %s\n", string);
      g_free (string);
      
      lv->current_row = iter;
    }
  column = gtk_tree_view_get_column (GTK_TREE_VIEW(lv->tree), 1);
  list = gtk_tree_view_column_get_cell_renderers (column);
  //g_object_set (G_OBJECT(list->data), "background", "black", NULL);
}

void
tree_row_activated (GtkTreeView *tree, GtkTreePath *path, 
		    GtkTreeViewColumn *column, gpointer data)
{
  ListView *lv = data;
  
  lv->append_row (lv->tree, lv);
  g_message ("row-activated called");
}

void editing_done (GtkCellEditable *edit_cell, gpointer data)
{
  ListView *lv = data;
  
  g_message ("editing_done called");
}

static GtkTreeModel *
make_model (void) {
  GtkListStore *store;
  GtkTreeIter  iter;
  gint i;
  
  store = gtk_list_store_new (N_COLUMNS, 
			      G_TYPE_STRING, 
			      G_TYPE_INT, 
			      G_TYPE_STRING);
  for (i = 0; i < 10; i++)
    {
      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
			  COLUMN_NOTE, "A Demo Note",
			  COLUMN_NUMBER, i,
			  COLUMN_OPERATOR, "+",
			  -1);
    }
  return GTK_TREE_MODEL(store);
}

static void
make_columns (ListView *lv) {
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  
  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT(renderer), "editable", TRUE, 
		"foreground", "red", "background", "black", NULL);
  g_signal_connect (G_OBJECT(renderer), "edited", 
		    G_CALLBACK(cell_changed), lv);
  
  column = gtk_tree_view_column_new_with_attributes ("Notes", renderer, 
						     "text", COLUMN_NOTE, 
						     NULL);
  
  g_object_set (G_OBJECT(column), "resizable", TRUE, NULL);
  
  gtk_tree_view_column_set_min_width (column, 200);
  gtk_tree_view_append_column (GTK_TREE_VIEW(lv->tree), column);
  
  renderer = gtk_cell_renderer_text_new ();
  column = gtk_tree_view_column_new_with_attributes ("Numbers", renderer,
						     "text", COLUMN_NUMBER, 
						     NULL);
  
  g_object_set (G_OBJECT(renderer), "xalign", 1.0, NULL);
  g_object_set (G_OBJECT(column), "resizable", TRUE, NULL);
  /*gtk_tree_view_column_set_max_width (column, 70); */
  gtk_tree_view_append_column (GTK_TREE_VIEW(lv->tree), column);
  
  
  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT(renderer), "editable", TRUE, "xpad", 10, NULL);
  g_signal_connect (G_OBJECT(renderer), "edited", 
		    G_CALLBACK(cell_op_changed), lv);
  column = gtk_tree_view_column_new_with_attributes ("Op", renderer, 
						     "text", COLUMN_OPERATOR, 
						     NULL);
  /*gtk_tree_view_column_set_sizing (column, GTK_TREE_VIEW_COLUMN_FIXED); */
  /*gtk_tree_view_column_set_max_width (column, 20);*/
  gtk_tree_view_append_column (GTK_TREE_VIEW(lv->tree), column);
}


int 
main (int argc, char *argv[]) {
  
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *scrolled;
  GtkWidget *tree;
  GtkWidget *toolbar;
  GtkWidget *button;
  GtkTreeModel *model;
  GtkTreeSelection *select;
  ListView      *lv;
  
  gtk_init (&argc, &argv);
  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (GTK_OBJECT(window), "delete_event",
		    G_CALLBACK(gtk_main_quit), NULL);
  
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER(window), vbox);
  gtk_widget_show (vbox);
  
  model = make_model ();
  
  lv = g_new0 (ListView, 1);
  
  lv->append_row = add_row;
  lv->delete_row = remove_row;
  
  
  /* toolbar */
  toolbar = gtk_toolbar_new ();
  gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
  gtk_widget_show (toolbar);
  
  button = gtk_toolbar_insert_stock (GTK_TOOLBAR(toolbar), "gtk-add",
				     "Add A Row",
				     NULL, G_CALLBACK(lv->append_row), 
				     lv, -1);
  gtk_widget_show (button);
  button = gtk_toolbar_insert_stock (GTK_TOOLBAR(toolbar), "gtk-remove",
				     "Remove A Row",
				     NULL, G_CALLBACK(lv->delete_row), 
				     lv, -1);
  gtk_widget_show (button);
  button = gtk_toolbar_insert_stock (GTK_TOOLBAR(toolbar), "gtk-quit",
				     "Quit The Application",
				     NULL, G_CALLBACK(delete_event), 
				     NULL, -1);
    gtk_widget_show (button);
    
    /* setup the tree */
    scrolled = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW(scrolled),
					 GTK_SHADOW_ETCHED_IN);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrolled),
				    GTK_POLICY_AUTOMATIC, 
				    GTK_POLICY_AUTOMATIC);
    gtk_box_pack_start (GTK_BOX(vbox), scrolled, TRUE, TRUE, 0);
    gtk_widget_show (scrolled);
    
    lv->tree = gtk_tree_view_new_with_model (model);
    
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(lv->tree), TRUE);
    g_signal_connect (G_OBJECT(lv->tree), "row-activated", 
		      G_CALLBACK(tree_row_activated), 
		      lv);
    
    select = gtk_tree_view_get_selection (GTK_TREE_VIEW(lv->tree));
    gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
    g_signal_connect (G_OBJECT(select), "changed",
		      G_CALLBACK(selection_changed),
		      lv);
    
    g_object_unref (G_OBJECT(model));
    
    gtk_container_add (GTK_CONTAINER(scrolled), lv->tree);
    
    gtk_widget_show (lv->tree);
    
    make_columns (lv);
    
    gtk_window_set_default_size (GTK_WINDOW(window), 400, 300);
    
    gtk_widget_show (window);
    
    gtk_main ();
    
    g_free (lv);
    
    return (0);
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]