Re: Passing Glists to callbacks



On Monday 05 December 2005 21:20, Tristan Van Berkom wrote:
Adam wrote:
Hi all,

[...]

Any suggestions are of value.

Hi,
     Its hard to make out what exactly it is your doing...
it would be helpfull if you posted a short example
of code that doesn't work.

See below:


typedef struct instsdata
{
        GList *instruments;
        GtkWidget *hbox;
}instsdata;


static gboolean
cell_clicked(GtkTreeSelection *selection,
             GtkTreeModel     *model,
             GtkTreePath      *path,
             gboolean          path_currently_selected,
             gpointer          user_data)
{
  gboolean ret = FALSE;
  g_print("In cell_clicked callback\n");
  instsdata *cbdata = (instsdata *)user_data;
  GtkWidget *list;
  GtkListStore *list_store;
  GtkTreeIter iter;
  
  GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
  GList *instruments = cbdata->instruments;
  list_store = gtk_list_store_new (1, G_TYPE_STRING);   /* label */

  list = gtk_tree_view_new_with_model (GTK_TREE_MODEL (list_store));
  gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (list),
                                               0, "Instrument",
                                               renderer,
                                               "text", 0, NULL);


  if (gtk_tree_model_get_iter(model, &iter, path))
    {
      gchar *name;

      gtk_tree_model_get(model, &iter, 0, &name, -1);
                        
      if (!path_currently_selected)
      { 
        InstrumentType type = lookuptype(name);
        g_print("Name %s,  Type %d\n", name, type);
        g_print("Entire Instruments List length %d\t",                  
                g_list_length(instruments));
        for(GList *tmp=instruments; tmp; tmp=tmp->next)
        {
                g_print("Individual List type %d \t ",  ((InstrumentList *)     
                instruments->data)->type);
                if(type == ((InstrumentList *) (instruments)->data)->type)
                {
                        g_print("Individual List length %d", 
                g_list_length(((InstrumentList *) (instruments)->data)->instruments));
                        for(GList *inst = ((InstrumentList *)
(instruments)->data)->instruments; inst; inst=inst->next)
                {  
                                                        
                  gtk_list_store_append (list_store, &iter);
                gtk_list_store_set (list_store, &iter,  0,
                _(((InstrumentConfig *)inst->data)->name->str), -1);
                                                        
                }
                break;
                }
        }
        
      }
     

      g_free(name);
    }
 
        gtk_box_pack_start (GTK_BOX (cbdata->hbox), list, TRUE, TRUE, 0);
        gtk_widget_show_all(cbdata->hbox);
        
        return ret;
}

A common mistake when passing lists in as arguments to
a callback *that modifies the list* is when people pass
in the head node itself instead of its address, since
you almost always want to re-assign the head node after
any list operations.

i.e. the callback should look like:

gint
my_hash_foreach_list_accum (gpointer   key,
                             gpointer   value,
                             GList    **accum)
{
     if (frobnicate (key, value)) {
         *accum = g_list_prepend (*accum, value);
     }
}

I'll give it a try

Adam



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