/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

#define N_COLUMNS 3
#define ADD_ROWS_TIMEOUT_MS 250

#include <config.h>
#include <string.h>
#include <gtk/gtk.h>

static gboolean
add_rows (GtkTreeModel *model)
{
  static gint  n = 0;
  GtkTreeIter  iter;
  gchar       *prepend_string;
  gchar       *append_string;

  prepend_string = g_strdup_printf ("Prependage #%d", n);
  append_string = g_strdup_printf ("Appendage #%d", n);
  n++;

  gtk_list_store_prepend (GTK_LIST_STORE (model), &iter);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                      0, prepend_string,
                      1, prepend_string,
                      2, prepend_string,
                      -1);

  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
  gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                      0, append_string,
                      1, append_string,
                      2, append_string,
                      -1);

  return TRUE;
}

static GtkWidget *
construct_tree_view (void)
{
  GtkTreeModel      *model;
  GtkTreeModel      *model_sort;
  GtkTreeView       *view;
  GtkScrolledWindow *scrolled_window;
  gint               i;

  model = GTK_TREE_MODEL (gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING));
  model_sort = gtk_tree_model_sort_new_with_model (model);
  view = GTK_TREE_VIEW (gtk_tree_view_new_with_model (model_sort));
  scrolled_window = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new (NULL, NULL));
  gtk_container_add (GTK_CONTAINER (scrolled_window), GTK_WIDGET (view));

  for (i = 0; i < N_COLUMNS; i++)
    {
      GtkCellRenderer   *renderer;
      GtkTreeViewColumn *column;
      gchar             *name;

      name = g_strdup_printf ("%d", i);

      renderer = gtk_cell_renderer_text_new ();
      column = gtk_tree_view_column_new_with_attributes (name,
                                                         renderer,
                                                         "text", i,
                                                         NULL);

      g_object_set (column,
                    "expand", TRUE,
                    NULL);

      gtk_tree_view_column_set_sort_column_id (column, i);
      gtk_tree_view_append_column (view, column);

      g_free (name);
    }

  gtk_tree_view_set_rules_hint (view, TRUE);

  g_timeout_add (ADD_ROWS_TIMEOUT_MS, (GSourceFunc) add_rows, model);

  return GTK_WIDGET (scrolled_window);
}

gint
main (gint argc, gchar *argv [])
{
  GtkWidget *tree_view_box;
  GtkWindow *window;

  gtk_init (&argc, &argv);

  window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  gtk_window_set_default_size (window, 400, 400);

  tree_view_box = construct_tree_view ();
  gtk_container_add (GTK_CONTAINER (window), tree_view_box);

  gtk_widget_show_all (GTK_WIDGET (window));

  gtk_main ();
  return 0;
}
