GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jul 30, 2014 11:15 pm

All times are UTC




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: GtkTreeRowReference and Tree updates
PostPosted: Mon Dec 17, 2012 10:51 pm 
Offline
Familiar Face

Joined: Thu Aug 23, 2012 4:50 am
Posts: 11
Hi,

I have a problem where a GtkTreeRowReference isn't being updated when I move the treenode to a parent node.

Below is some test code where I create 6 nodes on a parent and save a reference of each node to the TreeStore. My understanding is, each time I update, insert or delete a node, the references are updated so that when I read the reference of a node it'll show the correct path ( at the bottom of the window in this case).

But in my test code, when I move a node to different parent and then click on the node, I get a "assertion path != NULL" error. All the other nodes get updated correctly, just not the one that has been set to a new parent. So is there any reason for this?

What I'm trying to do is store a handle/pointer/reference/whatever of a particular node in 3D object. When I click on that object it should highlight the corresponding treenode no matter where it is in the TreeModel.

Any help would be appreciated. I'm using Gtk+ 2.24 on Windows.

Thanks
6
Code:
#include <gtk/gtk.h>

enum
{
  COLUMN = 0,
  REFERENCE,
  NUM_COLS
} ;

void  on_changed(GtkWidget *widget, gpointer statusbar)
{
  GtkTreeIter iter;
  GtkTreeModel *model;
  gchar *str;
  GtkTreeRowReference *ref;
  GtkTreePath *path;

  if (gtk_tree_selection_get_selected(GTK_TREE_SELECTION(widget), &model, &iter))
  {
    //Get the stored Reference from the node we clicked on, get the updated path and display it.
    gtk_tree_model_get(model, &iter,  REFERENCE, &ref, -1);
    path = gtk_tree_row_reference_get_path(ref);
    str = gtk_tree_path_to_string(path);
    gtk_statusbar_push(GTK_STATUSBAR(statusbar), gtk_statusbar_get_context_id(GTK_STATUSBAR(statusbar), str), str);
  }
}

static GtkTreeModel *
create_and_fill_model (void)
{
  GtkTreeStore *treestore;
  GtkTreePath *path;
  GtkTreeRowReference *ref;
  GtkTreeIter toplevel, child;

  treestore = gtk_tree_store_new(NUM_COLS,G_TYPE_STRING,G_TYPE_POINTER);

  //Create a new iter, get the path of it, make a reference and add it to the store

  gtk_tree_store_append(treestore, &toplevel, NULL);
  path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &toplevel);
  ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(treestore), path);
  gtk_tree_store_set(treestore, &toplevel, COLUMN, "languages", REFERENCE, ref,-1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &child);
  ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(treestore), path);
  gtk_tree_store_set(treestore, &child, COLUMN, "Python", REFERENCE, ref, -1);
     
  gtk_tree_store_append(treestore, &child, &toplevel);
  path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &child);
  ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(treestore), path);
  gtk_tree_store_set(treestore, &child,COLUMN, "Perl",REFERENCE, ref,-1);
   
  gtk_tree_store_append(treestore, &child, &toplevel);
  path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &child);
  ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(treestore), path);
  gtk_tree_store_set(treestore, &child,COLUMN, "PHP", REFERENCE, ref,-1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &child);
  ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(treestore), path);
  gtk_tree_store_set(treestore, &child, COLUMN, "C", REFERENCE, ref,-1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &child);
  ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(treestore), path);
  gtk_tree_store_set(treestore, &child, COLUMN, "C++", REFERENCE, ref,-1);

  gtk_tree_store_append(treestore, &child, &toplevel);
  path = gtk_tree_model_get_path(GTK_TREE_MODEL(treestore), &child);
  ref = gtk_tree_row_reference_new(GTK_TREE_MODEL(treestore), path);
  gtk_tree_store_set(treestore, &child, COLUMN, "Java", REFERENCE, ref,-1);
 
  return GTK_TREE_MODEL(treestore);
}

static GtkWidget *
create_view_and_model (void)
{
  GtkTreeViewColumn *col;
  GtkCellRenderer *renderer;
  GtkWidget *view;
  GtkTreeModel *model;
  GtkTreeSelection *select;
 
  view = gtk_tree_view_new();
  select = gtk_tree_view_get_selection (GTK_TREE_VIEW (view));
  gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);

  gtk_tree_view_set_reorderable(GTK_TREE_VIEW(view), TRUE);
 
  col = gtk_tree_view_column_new();
  gtk_tree_view_column_set_title(col, "Programming languages");
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_column_pack_start(col, renderer, TRUE);
  gtk_tree_view_column_add_attribute(col, renderer, "text", COLUMN);

  model = create_and_fill_model();
  gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
  //g_object_unref(model);

  return view;
}

int
main (int argc, char **argv)
{       
  GtkWidget *window;
  GtkWidget *view;
  GtkTreeSelection *selection;
  GtkWidget *vbox;
  GtkWidget *statusbar;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_title(GTK_WINDOW(window), "Tree View");
  gtk_widget_set_size_request (window, 350, 300);

  vbox = gtk_vbox_new(FALSE, 2);
  gtk_container_add(GTK_CONTAINER(window), vbox);

  view = create_view_and_model();
  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));

  gtk_box_pack_start(GTK_BOX(vbox), view, TRUE, TRUE, 1);

  statusbar = gtk_statusbar_new();
  gtk_box_pack_start(GTK_BOX(vbox), statusbar, FALSE, TRUE, 1);

  g_signal_connect(selection, "changed",
      G_CALLBACK(on_changed), statusbar);

  g_signal_connect (G_OBJECT (window), "destroy",
          G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group