GtkTree remove/add problem



Hi ALL,

I'm working on a program in which a gtktree must be frequently updated
by a timeout function. In order to keep this thing simple, I only want
to clear all tree items and create them again. So, if new data is read,
they simply appear on the gtktree when I reconstruct it. And exactly
there I'm having a problem: once I've cleared all tree items, I'm not
able to insert items on the tree anymore. Surely I'm overlooking
something. Here an excerpt of my code (the code constructs the tree
correctly the first time it's realized):

void
on_tree_realize (GtkWidget *widget, gpointer user_data)
{
...

  /* If is as rebuild, clear all tree items */
  if(rebuild_tree) {
    gtk_tree_clear_items (tree, 0, -1);
    rebuild_tree = FALSE;
  }

  /* Call function to build the tree */
  populate_tree (GTK_TREE(widget));
}

void
populate_tree(GtkTree *tree)
{
  GList *item_list = NULL, *swap_list = NULL, *node;
  GtkWidget *subtree, *root_item, *root_hbox;
  GtkWidget *root_pixmap, *root_label;

  /* Create the root item */
  root_item = gtk_tree_item_new ();

  /* Create the root hbox which will contain the 
   * root_item label and pixmap 
   */
  root_hbox = gtk_hbox_new (FALSE, 2);

  /* Show the root hbox */
  gtk_widget_show (root_hbox);

  /* Pack the root hbox into the tree root item */
  gtk_container_add (GTK_CONTAINER (root_item), root_hbox);

  /* Create the root pixmap */
  root_pixmap = create_pixmap (gtk_widget_get_toplevel\
                              (GTK_WIDGET(tree)),
                               "root.xpm");

  /* Set root_pixmap alignment properly */
  gtk_misc_set_alignment (GTK_MISC (root_pixmap), 0.0, 0.5);

  /* Show root_pixmap */
  gtk_widget_show (root_pixmap);

  /* Pack the root pixmap into root_hbox */
  gtk_box_pack_start (GTK_BOX (root_hbox), root_pixmap,
                      FALSE, FALSE, 0);

  /* Create root_label */
  root_label = gtk_label_new ("Root");

  /* Set root_label alignment properly */
  gtk_misc_set_alignment (GTK_MISC (root_label), 0.0, 0.5);

  /* Show the root label*/
  gtk_widget_show (root_label);

  /* Pack root label into the root hbox */
  gtk_box_pack_start (GTK_BOX (root_hbox), root_label,
                      FALSE, FALSE, 0);

  /* Add root item to the tree root */
  gtk_tree_append (GTK_TREE(tree), root_item);

  /* Show item */
  gtk_widget_show (root_item);

  /* Read the list of available items */
  swap_list = read_item_list("list\n");

  /* Copying all the the items from swap_list into item_list */
  foreach(swap_list, node) {
    item_list = g_list_insert(item_list,
                              g_strdup((gchar *) node->data), 0);
  }

  /* Revert the glist and remove the NULL pointer allocated to
   * initialize the GList in the function read_item_list()
   */
  item_list = g_list_reverse (item_list);
  g_list_remove(item_list, NULL);

  /* Create the subtree (it's a subtree of root)  */
  subtree = gtk_tree_new();

  /* Set the item subtree - note that you cannot do this until
   * the root item has been added to its parent tree!
   */
  gtk_tree_item_set_subtree (GTK_TREE_ITEM(root_item), subtree);

  /* Populate the tree */
  foreach(item_list, node) {
    GtkWidget *item, *item_hbox, *item_pixmap, *item_label;

    /* Create a tree item */
    item = gtk_tree_item_new ();
    /* Create a hbox which will contain the item label and pixmap */
    item_hbox = gtk_hbox_new (FALSE, 2);
    gtk_widget_ref (item_hbox);
    /* Show the hbox */
    gtk_widget_show (item_hbox);
    /* Pack the hbox into the tree item */
    gtk_container_add (GTK_CONTAINER (item), item_hbox);
    /* Create the item pixmap */
    item_pixmap = create_pixmap (gtk_widget_get_toplevel\
                                (GTK_WIDGET(tree)),
                                "item.xpm");
    /* Set pixmap alignment properly */
    gtk_misc_set_alignment (GTK_MISC (item_pixmap), 0.0, 0.5);
    /* Show the item pixmap */
    gtk_widget_show (item_pixmap);
    /* Pack the pixmap into the hbox */
    gtk_box_pack_start (GTK_BOX (item_hbox), item_pixmap,
                        FALSE, FALSE, 0);
    /* Create the item label */
    item_label = gtk_label_new ((gchar *) node->data);
    /* Set label alignment properly */
    gtk_misc_set_alignment (GTK_MISC (item_label), 0.0, 0.5);
    /* Show the item label*/
    gtk_widget_show (item_label);
    /* Pack item label into the hbox */
    gtk_box_pack_start (GTK_BOX (item_hbox), item_label,
                        FALSE, FALSE, 0);
    /* Connect select signal to handle item selections */
    gtk_signal_connect (GTK_OBJECT(item), "select",
                        GTK_SIGNAL_FUNC(on_tree_select_subitem),
                        item_label);
    /* Add item to the item tree */
    gtk_tree_append (GTK_TREE(subtree), item);
    /* Show item */
    gtk_widget_show (item);
  }

  /* Free glist */
  g_list_free(item_list);
  g_list_free(swap_list);
  g_list_free(node);
}

Does anyone have a clue, what am I doing wrong? I've already tried to
increase the reference count of the gtktree, but I know that the tree
wasn't removed. Just it's children pointer is NULL after the items are
cleared. But cause I create them again, this should not be a problem at
all, should it?

Thanks in advance for the help!

Greetings,

Rafael
P.S. ('foreach' is a macro that iterates through all items of a GList.)
-- 
rafael peregrino innominate com
software engineer                                        innominate AG
server appliances                                 the linux architects
tel: +49.30.308806-81  fax: -77              http://www.innominate.com





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