/* gcc -o mouseselect mouseselect.c `pkg-config --libs --cflags gtk+-2.0`
 */

#include <stdio.h>
#include <unistd.h>
#include <gtk/gtk.h>

static void motion_cb (GtkWidget *widget, GdkEventMotion *event,
                                       gpointer user_data)
{
    GtkTreeViewColumn *col;
    GtkTreePath *path;
    GtkTreeSelection *select;
    gint cx, cy;
    if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW (widget),
                                       event->x, event->y, &path, &col,
                                       &cx, &cy)) {
        select = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
        gtk_tree_selection_select_path (select, path);
        gtk_tree_view_set_cursor (GTK_TREE_VIEW (widget), path, 0, FALSE);
        gtk_tree_path_free (path);
    }
}

int main (int argc, char **argv)
{
    GtkWidget *window, *vbox, *scrolled, *tree;
    GtkTreeStore *store;
    GdkColor color;
    int i;

    fprintf (stderr, "Please move your mouse around in a circle ...\n");
    sleep (3);

    gtk_init (&argc, &argv);
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    g_signal_connect (G_OBJECT (window), "destroy",
                      G_CALLBACK (gtk_main_quit), 0);
    g_signal_connect (G_OBJECT (window), "delete_event",
                      G_CALLBACK (gtk_main_quit), 0);
    gtk_widget_set_size_request (window, 800, 600);
    vbox = gtk_vbox_new (FALSE, 0);
    scrolled = gtk_scrolled_window_new (NULL, NULL);
    store = gtk_tree_store_new (1, G_TYPE_STRING);
    tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store));
    gtk_tree_view_append_column (GTK_TREE_VIEW (tree),
        gtk_tree_view_column_new_with_attributes("Column",
             gtk_cell_renderer_text_new (), "text", 0, 0));
    gtk_container_add (GTK_CONTAINER (scrolled), tree);
    gtk_container_add (GTK_CONTAINER (vbox), scrolled);
    gtk_container_add (GTK_CONTAINER (window), vbox);
    gtk_widget_set_size_request (tree, 600, 768);
    for (i = 0; i < 256; i++) {
        GtkTreeIter iter;
        gtk_tree_store_append (store, &iter, 0);
        gtk_tree_store_set (store, &iter, 0, "this is a row", -1);
    }
    gtk_widget_realize (tree);
    gtk_widget_modify_bg (tree, GTK_STATE_NORMAL, &color);
    g_signal_connect (G_OBJECT (tree), "motion-notify-event",
                      G_CALLBACK (motion_cb),
                      0);
    gtk_widget_show_all (window);
    gtk_main ();
    return 0;
}

