#include <gtk/gtk.h>

#define NUMBER_OF_ITEMS   10

static void
fill_model (GtkTreeModel *model)
{
  GdkPixbuf *pixbuf;
  int i;
  char *str, *str2;
  GtkTreeIter iter;
  GtkListStore *store = GTK_LIST_STORE (model);
  gint32 size;

  pixbuf = gdk_pixbuf_new_from_file ("gnome-textfile.png", NULL);

  i = 0;

  gtk_list_store_prepend (store, &iter);

  gtk_list_store_set (store, &iter,
                      0, pixbuf,
                      1, "Really really\nreally really loooooooooong item name",
                      2, 0,
                      3, "This is a <b>Test</b> of <i>markup</i>",
                      4, TRUE,
                      -1);

  while (i < NUMBER_OF_ITEMS - 1)
    {
      GdkPixbuf *pb;
      size = g_random_int_range (20, 70);
      pb = gdk_pixbuf_scale_simple (pixbuf, size, size, GDK_INTERP_NEAREST);

      str = g_strdup_printf ("Icon %d", i);
      str2 = g_strdup_printf ("Icon <b>%d</b>", i);
      gtk_list_store_prepend (store, &iter);
      gtk_list_store_set (store, &iter,
                          0, pb,
                          1, str,
                          2, i,
                          3, str2,
                          4, TRUE,
                          -1);
      g_free (str);
      g_free (str2);
      i++;
    }
}

static GtkTreeModel *
create_model (void)
{
  GtkListStore *store;

  store = gtk_list_store_new (5, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_INT, G_TYPE_STRING, G_TYPE_BOOLEAN);

  return GTK_TREE_MODEL (store);
}

int
main (gint argc, gchar **argv)
{
  GtkWidget *window, *child;
  GtkTreeModel *model;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_widget_set_size_request (GTK_WIDGET (window), 675, -1);

  if (g_getenv ("ICONVIEW"))
    {
      child = gtk_icon_view_new ();
      gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (child), GTK_SELECTION_MULTIPLE);

      model = create_model ();
      gtk_icon_view_set_model (GTK_ICON_VIEW (child), model);
      fill_model (model);

      gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (child), 0);
      gtk_icon_view_set_text_column (GTK_ICON_VIEW (child), 1);
    }
  else
    {
      child = gtk_viewport_new (NULL, NULL);
      gtk_container_add (GTK_CONTAINER (child),
                         gtk_label_new ("1\n2\n3\n4\n5"));
    }

  if (g_getenv ("SCROLLEDWINDOW") == NULL)
    gtk_container_add (GTK_CONTAINER (window), child);
  else
    {
      GtkWidget *scrolled_window;
      scrolled_window = gtk_scrolled_window_new (NULL, NULL);
      gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                                      GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
      gtk_container_add (GTK_CONTAINER (window), scrolled_window);
      gtk_container_add (GTK_CONTAINER (scrolled_window), child);

      if (g_getenv ("MINHEIGHT"))
        g_object_set (scrolled_window, "min-content-height", 60, NULL);
    }

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}

