#include <gtk/gtk.h>

static GtkTreeModel *create_model ();
static void add_columns (GtkTreeView *treeview);
static void add_columns_packed (GtkTreeView *treeview);


/* TreeItem structure */
typedef struct _TreeItem TreeItem;
struct _TreeItem
{
	const gchar *label;
	const gchar *page;
	TreeItem *children;
};

static TreeItem thirdlevel[] =
{
  { "3rd level short", "30", NULL },
  { "Third level with a very long title to test ellipsize", "long-page-number", NULL },
  { NULL }
};

static TreeItem secondlevel[] =
{
  { "A short title", "2", thirdlevel },
  { "A not short title", "200", thirdlevel },
  { "A very long title to test ellipsize", "cover", thirdlevel },
  { NULL }
};

static TreeItem toplevel[] =
{
  {"Introduction", "1", secondlevel},
  //{"A Very seconded discussion on the main topic", "front", secondlevel},
  {"Conclusions and future work", "900", secondlevel},
  {NULL}
};



static GtkTreeModel *
create_model ()
{
	GtkTreeStore *model;
	GtkTreeIter iter;
	TreeItem *top = toplevel;

	model = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_STRING);

	while (top->label) {
		TreeItem *second = top->children;

		gtk_tree_store_append (model, &iter, NULL);
		gtk_tree_store_set (model, &iter, 0, top->label,
			            1, top->page, -1);

		while (second->label) {
			GtkTreeIter siter;
			TreeItem *third = second->children;

			gtk_tree_store_append (model, &siter, &iter);
			gtk_tree_store_set (model, &siter,
				            0, second->label,
					    1, second->page,
					    -1);

			while (third->label) {
				GtkTreeIter titer;

				gtk_tree_store_append (model, &titer, &siter);
				gtk_tree_store_set (model, &titer,
					            0, third->label,
						    1, third->page,
						    -1);

				third++;
			}
			second++;
		}
		top++;
	}

	return GTK_TREE_MODEL (model);
}

static void
add_columns_packed (GtkTreeView *treeview)
{
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	const GdkRGBA red = { .7, 0.7, 0.7, 1.0 };

	column = gtk_tree_view_column_new();
	gtk_tree_view_append_column (treeview, column);

	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "xalign", 0.0,
		      "ellipsize", PANGO_ELLIPSIZE_END,
		      NULL);

	gtk_tree_view_column_pack_start (column, renderer, TRUE);
	gtk_tree_view_column_set_attributes (column, renderer, "text", 0, NULL);

	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "xalign", 1.0,
		      "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
		      "max-width-chars", 6,
		      "cell-background-rgba", &red,
		      NULL);
	gtk_tree_view_column_pack_end (column, renderer, FALSE);
	gtk_tree_view_column_set_attributes (column, renderer, "text", 1, NULL);
}
	
static void
add_columns (GtkTreeView *treeview)
{
	GtkCellRenderer *renderer;
	GtkTreeViewColumn *column;
	const GdkRGBA red = { 1.0, 0.0, 0.0, 1.0 };

	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "xalign", 0.0,
		      "ellipsize", PANGO_ELLIPSIZE_END,
		      NULL);

	column = gtk_tree_view_column_new_with_attributes ("Index",
			                                   renderer,
							   "text", 0,
							   NULL);
	gtk_tree_view_column_set_expand (column, TRUE);
	gtk_tree_view_append_column (treeview, column);

	renderer = gtk_cell_renderer_text_new ();
	g_object_set (renderer, "xalign", 1.0,
		      "ellipsize", PANGO_ELLIPSIZE_MIDDLE,
		      "width-chars", 6,
		      "cell-background-rgba", &red,
		      NULL);

	column = gtk_tree_view_column_new_with_attributes ("Page",
			                                   renderer,
							   "text", 1,
							   NULL);
	gtk_tree_view_append_column (treeview, column);
}

GtkWidget *
get_tree_view (GtkTreeModel *model)
{
	GtkWidget *sw;
	GtkWidget *treeview;

	sw = gtk_scrolled_window_new (NULL, NULL);
	gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW
					     (sw),
					     GTK_SHADOW_ETCHED_IN);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);

	treeview = gtk_tree_view_new_with_model (model);
	gtk_tree_view_set_rules_hint (GTK_TREE_VIEW (treeview),
				      TRUE);
	gtk_tree_selection_set_mode (gtk_tree_view_get_selection
				     (GTK_TREE_VIEW (treeview)),
				     GTK_SELECTION_MULTIPLE);
	gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview),
					   FALSE);
	gtk_container_add (GTK_CONTAINER (sw), treeview);

	return sw;
}

int
main (int argc, char *argv[])
{
	GtkWidget *window;
	GtkTreeModel *model;
	GtkWidget *box, *sw_left, *sw_right;
	GtkWidget *treeview_left, *treeview_right;

	gtk_init (&argc, &argv);

	model = create_model ();

	sw_left = get_tree_view (model);
	treeview_left = gtk_bin_get_child (GTK_BIN (sw_left));
	add_columns_packed (GTK_TREE_VIEW (treeview_left));

	sw_right = get_tree_view (model);
	treeview_right = gtk_bin_get_child (GTK_BIN (sw_right));
	add_columns (GTK_TREE_VIEW (treeview_right));

	g_object_unref (G_OBJECT (model));

	box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 8);
	gtk_container_set_border_width (GTK_CONTAINER (box), 8);
	gtk_box_pack_start (GTK_BOX (box), sw_left, TRUE, TRUE, 0);
	gtk_box_pack_start (GTK_BOX (box), sw_right, TRUE, TRUE, 0);

	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_window_set_default_size (GTK_WINDOW (window), 500, 300);
	gtk_container_add (GTK_CONTAINER (window), box);

	g_signal_connect (G_OBJECT (window), "delete-event",
			  G_CALLBACK (gtk_main_quit), &window);

	gtk_widget_show_all (window);
	gtk_main ();

	return 0;
}
