#include <gtk/gtk.h>

#define NOT_FOCUSED "TreeView not focused"
#define FOCUSED "<b>TreeView focused!</b>"

static void
tree_view_has_focus_notify_cb (GObject *object, GParamSpec *pspec, gpointer data)
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (object);
  GtkLabel *label = GTK_LABEL (data);
  const char *str;

  if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
    str = FOCUSED;
  else
    str = NOT_FOCUSED;

  gtk_label_set_markup (label, str);
}

static void
button_clicked_cb (GtkButton *button, gpointer data)
{
  GtkTreeView *tree_view = GTK_TREE_VIEW (data);
  GtkTreeModel *model;
  GtkListStore *store;

  model = gtk_tree_view_get_model (tree_view);
  g_assert (GTK_IS_LIST_STORE (model));

  store = GTK_LIST_STORE (model);

  gtk_list_store_insert_with_values (store, NULL, 0,
				     0, "Roses are red",
				     -1);
  gtk_list_store_insert_with_values (store, NULL, 1,
				     0, "widgets are blue",
				     -1);
  gtk_list_store_insert_with_values (store, NULL, 2,
				     0, "If you change the theme",
				     -1);
  gtk_list_store_insert_with_values (store, NULL, 3,
				     0, "then I will hate you.",
				     -1);
}

static void
window_destroy_cb (GtkWidget *widget, gpointer data)
{
  gtk_main_quit ();
}

int
main (int argc, char **argv)
{
  GtkWidget *window;
  GtkWidget *box;
  GtkWidget *button;
  GtkWidget *entry;
  GtkWidget *label;
  GtkWidget *scroll;
  GtkWidget *tree_view;
  GtkTreeSelection *selection;
  GtkListStore *store;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (window, "destroy",
		    G_CALLBACK (window_destroy_cb),
		    NULL);
  gtk_widget_set_size_request (window, 400, 400);

  gtk_window_set_title (GTK_WINDOW (window), "Test treeview cursor");
  gtk_container_set_border_width (GTK_CONTAINER (window), 12);

  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
  gtk_container_add (GTK_CONTAINER (window), box);

  button = gtk_button_new_with_label ("Populate the tree");
  gtk_widget_set_can_focus (button, FALSE); /* unfocusable so clicking it won't steal focus from the treeview */
  gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0);

  entry = gtk_entry_new ();
  gtk_entry_set_text (GTK_ENTRY (entry), "Random focusable widget");
  gtk_box_pack_start (GTK_BOX (box), entry, FALSE, FALSE, 0);

  label = gtk_label_new (NOT_FOCUSED);
  gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
  gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);

  scroll = gtk_scrolled_window_new (NULL, NULL);
  gtk_box_pack_start (GTK_BOX (box), scroll, TRUE, TRUE, 0);

  tree_view = gtk_tree_view_new ();
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view));
  gtk_tree_selection_set_mode (selection, GTK_SELECTION_SINGLE);
  gtk_tree_view_append_column (GTK_TREE_VIEW (tree_view),
			       gtk_tree_view_column_new_with_attributes ("Verses",
									 gtk_cell_renderer_text_new (),
									 "text", 0,
									 NULL));
  gtk_container_add (GTK_CONTAINER (scroll), tree_view);

  store = gtk_list_store_new (1, G_TYPE_STRING);
  gtk_tree_view_set_model (GTK_TREE_VIEW (tree_view), GTK_TREE_MODEL (store));

  g_signal_connect (tree_view, "notify::has-focus",
		    G_CALLBACK (tree_view_has_focus_notify_cb),
		    label);

  g_signal_connect (button, "clicked",
		    G_CALLBACK (button_clicked_cb), tree_view);

  gtk_widget_show_all (window);
  gtk_main ();

  return 0;
}
