#include <gtk/gtk.h>

void info (GtkWidget *view)
{
   GdkRectangle rect;
   
   gtk_tree_view_get_visible_rect (GTK_TREE_VIEW(view), &rect);
   
   g_message ("Visible rect x = %d, y = %d, w = %d, h = %d", rect.x, rect.y, rect.width, rect.height);
   g_message ("Widget allocation width = %d, height = %d", view->allocation.width, view->allocation.height);
}

void scroll (GtkTreeView *view)
{
    GtkTreePath *path;
    GtkTreeSelection *selection;
    
    path = gtk_tree_path_new_from_string ("50");

    selection = gtk_tree_view_get_selection (view);
    gtk_tree_selection_select_path (selection, path);
    
    gtk_tree_view_scroll_to_cell (view, path, NULL, FALSE, 0, 0);
    gtk_tree_path_free (path);
    
    return;
}

GtkListStore *create_model ()
{
    GtkListStore *list_store;
    
    list_store = gtk_list_store_new (1, G_TYPE_STRING);
    
    return list_store;
}

void fill_model (GtkListStore *list_store)
{
    GtkTreeIter iter;
    gint i;
    gchar* buf;

    for (i = 0; i<100; i++)
    {
	buf = g_strdup_printf ("Hello %d", i);
	gtk_list_store_append (list_store, &iter);
	gtk_list_store_set (list_store, &iter, 0, buf, -1);
	g_free (buf);
    }

}

gboolean idle_cb (gpointer data)
{
    info (GTK_WIDGET(data));
    scroll (GTK_TREE_VIEW (data));
    gtk_widget_show (GTK_WIDGET(data));
    return FALSE;
}

int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
    GtkWidget *sw;
    GtkWidget *tree_view;
    GtkTreeModel *list_store;

    GtkCellRenderer *renderer;
    GtkTreeViewColumn *column;
    
    gtk_init (&argc, &argv);
    
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size (GTK_WINDOW(window), 100, 400);

    list_store = GTK_TREE_MODEL(create_model ());
    tree_view = gtk_tree_view_new_with_model (list_store);
    sw = gtk_scrolled_window_new (NULL, NULL);
    
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes ("Test",
						     renderer,
						     "text",
						     0,
						     NULL);
    gtk_tree_view_append_column (GTK_TREE_VIEW(tree_view), column);
    
    gtk_container_add (GTK_CONTAINER(window), sw);

    gtk_widget_show_all (window);
    
    gtk_container_add (GTK_CONTAINER(sw), tree_view);

    fill_model (GTK_LIST_STORE (list_store));
    
    g_idle_add (idle_cb, tree_view);
    
    gtk_main ();
    
    return 0;
}
