GtkTreeView cell_data_func requesting non-visible cells.



Am I right in thinking that GtkTreeView shouldn't call the cell_data
callback for cells that aren't visible?

The attached test case shows it being called for every row (even if only
a few are visible in the GtkScrolledWindow), and then called again for
the visible rows. This is not so nice when I must retrieve the data (in
my custom model) from a database.

-- 
Murray Cumming
murrayc murrayc com
www.murrayc.com
www.openismus.com
#include <gtk/gtk.h>

enum
{
  COL_TEST = 0,
  NUM_COLS
} ;

static GtkTreeModel *
create_and_fill_model (void)
{
  GtkTreeStore  *treestore;
  GtkTreeIter    child;
  unsigned long i = 0;

  treestore = gtk_tree_store_new(NUM_COLS,
                                 G_TYPE_ULONG);

  /* Append a second top level row, and fill it with some data */
  for(i = 0; i < 1000; ++i)
  {
    gtk_tree_store_append(treestore, &child, NULL);
    gtk_tree_store_set(treestore, &child,
                     COL_TEST, i,
                     -1);
  }

  return GTK_TREE_MODEL(treestore);
}

void
on_cell_data_func (GtkTreeViewColumn *col,
                    GtkCellRenderer   *renderer,
                    GtkTreeModel      *model,
                    GtkTreeIter       *iter,
                    gpointer           user_data)
{
  gchar  buf[64];
  gulong test = 0;

  gtk_tree_model_get(model, iter, COL_TEST, &test , -1);

  printf("debug: test=%d\n", test);

  g_snprintf(buf, sizeof(buf), "test: %d", test);

  g_object_set(renderer, "text", buf, NULL);
}


static GtkWidget *
create_view_and_model (void)
{
  GtkTreeViewColumn   *col;
  GtkCellRenderer     *renderer;
  GtkWidget           *view;
  GtkTreeModel        *model;

  view = gtk_tree_view_new();

  /* --- Column --- */

  col = gtk_tree_view_column_new();

  gtk_tree_view_column_set_title(col, "Age");

  /* pack tree view column into tree view */
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

  renderer = gtk_cell_renderer_text_new();

  /* pack cell renderer into tree view column */
  gtk_tree_view_column_pack_start(col, renderer, TRUE);

  /* connect a cell data function */
  gtk_tree_view_column_set_cell_data_func(col, renderer, on_cell_data_func, NULL, NULL);


  model = create_and_fill_model();

  gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);

  g_object_unref(model); /* destroy model automatically with view */

  gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(view)),
                              GTK_SELECTION_NONE);

  return view;
}


int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *view;
  GtkWidget *scrolledwindow;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  g_signal_connect(window, "delete_event", gtk_main_quit, NULL); /* dirty */

  view = create_view_and_model();

  scrolledwindow = gtk_scrolled_window_new(NULL, NULL);
  gtk_container_add(GTK_CONTAINER(scrolledwindow), view);

  gtk_window_set_default_size(GTK_WINDOW(window), 200, 200);
  gtk_container_add(GTK_CONTAINER(window), scrolledwindow);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;
}



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