/*
to compile an application showing warnings:
gcc -c -Wall -Wextra -pedantic -DSHOW_WARNS `pkg-config --cflags gtk+-3.0` main.c
gcc -o test `pkg-config --libs gtk+-3.0` main.o

otherwise:
gcc -c -Wall -Wextra -pedantic `pkg-config --cflags gtk+-3.0` main.c
gcc -o test `pkg-config --libs gtk+-3.0` main.o

*/

#include <gtk/gtk.h>

typedef struct
{
  const gint  col1;
  const gint  col2;
  const gint  col3;
}
Data;

enum
{
  COLUMN_1,
  COLUMN_2,
  COLUMN_3,
  NUM_COLUMNS
};

static Data data[] =
{
  { 2, 7, 6 },
  { 9, 5, 1 },
  { 4, 3, 8 }
};

static GtkTreeModel *
create_model (void)
{
  guint i = 0;
  GtkListStore *store;
  GtkTreeIter iter;

  /* create list store */
  store = gtk_list_store_new (NUM_COLUMNS,
                              G_TYPE_INT,
                              G_TYPE_INT,
                              G_TYPE_INT);

  /* add data to the list store */
  for (i = 0; i < G_N_ELEMENTS(data); ++i)
    {
      gtk_list_store_append (store, &iter);
      gtk_list_store_set (store, &iter,
                          COLUMN_1, data[i].col1,
                          COLUMN_2, data[i].col2,
                          COLUMN_3, data[i].col3,
                          -1);
    }

  return GTK_TREE_MODEL (store);
}

static void
add_columns (GtkTreeView *treeview)
{
  GtkCellRenderer   *renderer;
  GtkTreeViewColumn *column;
  guint i;
  gchar* label;

  for (i = 0; i < NUM_COLUMNS; ++i)
  {
    label = g_strdup_printf ("Col%d", i + 1);
    renderer = gtk_cell_renderer_text_new ();
    column = gtk_tree_view_column_new_with_attributes (label,
                                                       renderer,
                                                       "text",
                                                       COLUMN_1 + i,
                                                       NULL);
    gtk_tree_view_append_column (treeview, column);
    g_free (label);
  }
}

static gboolean
window_closed (GtkWidget *widget G_GNUC_UNUSED,
               GdkEvent  *event G_GNUC_UNUSED,
               gpointer   user_data)
{
  GtkTreeView       *tree_view;
  GtkTreeViewColumn *column;
  GList             *columns;
  GList             *iter;

  tree_view = GTK_TREE_VIEW (user_data);
  columns = gtk_tree_view_get_columns (tree_view);

#ifdef SHOW_WARNS
  for (iter = columns; iter; iter = iter->next)
#else
  for (iter = g_list_last (columns); iter; iter = iter->prev)
#endif
  {
    column = GTK_TREE_VIEW_COLUMN (iter->data);

    gtk_tree_view_remove_column (tree_view, column);
  }

  g_list_free (columns);
  return FALSE;
}

int
main (void)
{
  GtkWindow    *window = NULL;
  GtkTreeModel *model = NULL;
  GtkTreeView  *treeview = NULL;

  gtk_init (NULL, NULL);

  /* create tree model */
  model = create_model ();

  /* create tree view */
  treeview = GTK_TREE_VIEW (gtk_tree_view_new_with_model (model));

  /* create window, etc */
  window = GTK_WINDOW (gtk_window_new (GTK_WINDOW_TOPLEVEL));
  gtk_window_set_title (window, "Magic square");

  gtk_container_set_border_width (GTK_CONTAINER (window), 5);

  gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (treeview));

  g_object_unref (model);

  g_signal_connect (window, "delete-event",
                    G_CALLBACK (window_closed), treeview);
  g_signal_connect (window, "destroy",
                    G_CALLBACK (gtk_main_quit), NULL);

  /* add columns to the tree view */
  add_columns (treeview);

  /* finish & show */
  gtk_window_set_default_size (window, 100, 100);

  gtk_widget_show_all (GTK_WIDGET (window));

  gtk_main ();

  return 0;
}

