Re: TreeView/ListStore reordering by DND



Il Sat, 01 Mar 2014 19:35:08 +0100 Stefan Salewski <mail ssalewski de> scrisse:

On Sat, 2014-03-01 at 02:08 +0100, Stefan Salewski wrote:
[...]
Here is my complete C code:

http://www.ssalewski.de/tmp/test.c

I inserted these lines:

void on_row_inserted(GtkTreeModel *tree_model, GtkTreePath *path,
GtkTreeIter *iter, gpointer user_data)
{
  char *value;
  printf("row_inserted\n");
  gtk_tree_model_get(tree_model, iter, LIST_ITEM, &value, -1);
  g_printf("value = %s\n", value);
  gint *i = gtk_tree_path_get_indices(path);
  g_printf("row ins = %d\n", i[0]);
}

void on_row_deleted(GtkTreeModel *tree_model, GtkTreePath *path, gpointer user_data)
{
  printf("row_deleted\n");
  gint *i = gtk_tree_path_get_indices(path);
  g_printf("row del = %d\n", i[0]);
}

First question:
When I exchange rows by drag and drop, I get

value = (null)

similar to my test from Ruby yesterday. Is this intended, or is my code
wrong (I have not much practice with using GTK from plain C...)

Hi,

this is intended behavior. The problem is the insertion is done in two
steps: (1) an empty row is created (and your on_row_inserted() callback
is called) and (2) that row is filled with data.

If you want 'value' to be meaningful you must postpone your callback, e.g.
by using g_idle_add(). Try to substitute your on_row_inserted() with the
following code:


typedef struct {
    GtkTreeModel *model;
    GtkTreePath *path;
} CallbackData;

static gboolean
real_row_inserted(CallbackData *data)
{
    GtkTreeIter iter;

    if (gtk_tree_model_get_iter(data->model, &iter, data->path)) {
        gchar *value;
        gtk_tree_model_get(data->model, &iter, LIST_ITEM, &value, -1);
        g_print("Row inserted: value = '%s'\n", value);
        g_free(value);
    }

    gtk_tree_path_free(data->path);
    g_free(data);
    return FALSE;
}

static void
on_row_inserted(GtkTreeModel *tree_model, GtkTreePath *path,
        GtkTreeIter *iter, gpointer user_data)
{
    CallbackData *data = g_new(CallbackData, 1);
    data->model = tree_model;
    data->path = gtk_tree_path_copy(path);
    g_idle_add((GSourceFunc) real_row_inserted, data);
}


Ciao.
-- 
Nicola


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