/* 
 * Short, Self Contained, Correct Example to demostrate Thunar GTK3 issue
 * related to shortcuts pane layout.
 * More details: https://github.com/andreldm/thunar/issues/40
 * Build:
 * gcc $(pkg-config --cflags gtk+-2.0) sscee.c -o sscee $(pkg-config --libs gtk+-2.0)
 * gcc $(pkg-config --cflags gtk+-3.0) sscee.c -o sscee $(pkg-config --libs gtk+-3.0)
 */

#include <gtk/gtk.h>



enum {
  MODEL_NAME = 0,
  MODEL_IS_ITEM,
  MODEL_IS_HEADER,
  MODEL_CAN_EJECT,
  N_COLUMNS
};



void
init_list (GtkWidget *list)
{
  GtkCellRenderer *renderer;
  GtkTreeViewColumn *column;
  GtkListStore *store;

  column = g_object_new (GTK_TYPE_TREE_VIEW_COLUMN,
               "reorderable", FALSE,
               "resizable", FALSE,
               "sizing", GTK_TREE_VIEW_COLUMN_AUTOSIZE,
               "spacing", 2,
               NULL);
  gtk_tree_view_append_column (GTK_TREE_VIEW (list), column);
  
  /* header */
  renderer = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
                           "weight", PANGO_WEIGHT_BOLD,
                           "xpad", 6,
                           "ypad", 6,
#if GTK_CHECK_VERSION(3, 0, 0)
                           "ellipsize", PANGO_ELLIPSIZE_END,
#endif
                           NULL);
   gtk_tree_view_column_pack_start (column, renderer, FALSE);
   gtk_tree_view_column_set_attributes (column, renderer,
                                        "text", MODEL_NAME,
                                        "visible", MODEL_IS_HEADER,
                                        NULL);

  /* separator for indent */
  renderer = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
                             "xpad", 6,
                             NULL);
  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xpad", 6, NULL);
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
  gtk_tree_view_column_set_attributes (column, renderer,
                                       "visible", MODEL_IS_ITEM,
                                       NULL);

  /* icon */
  renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
                           "icon-name", "drive-harddisk",
                           "xpad", 6,
                           NULL);
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
  gtk_tree_view_column_set_attributes (column, renderer,
                                       "visible", MODEL_IS_ITEM,
                                       NULL);

  /* text */
  renderer = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT,
                           "ellipsize", PANGO_ELLIPSIZE_END,
                           "cell-background", "lightblue",
                           "cell-background-set", TRUE,
                           NULL);
  gtk_tree_view_column_pack_start (column, renderer, TRUE);
  gtk_tree_view_column_set_attributes (column, renderer,
                                       "text", MODEL_NAME,
                                       "visible", MODEL_IS_ITEM,
                                       NULL);

  /* eject icon */
  renderer = g_object_new (GTK_TYPE_CELL_RENDERER_PIXBUF,
                         "icon-name", "media-eject",
                         "mode", GTK_CELL_RENDERER_MODE_ACTIVATABLE,
                         "cell-background", "red",
                         "cell-background-set", TRUE,
                         "xpad", 6,
                         NULL);
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
  gtk_tree_view_column_set_attributes (column, renderer,
                                       "visible", MODEL_CAN_EJECT,
                                       NULL);
  gtk_tree_view_column_set_max_width (column, 50);

  store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN);
  gtk_tree_view_set_model (GTK_TREE_VIEW (list), GTK_TREE_MODEL (store));
  g_object_unref (store);
}



void
add_to_list (GtkWidget *list, int pos, const gchar *str, gboolean isHeader, gboolean canEject)
{
  GtkListStore *store;
  GtkTreeIter iter;

  store = GTK_LIST_STORE (gtk_tree_view_get_model (GTK_TREE_VIEW (list)));

  if (pos == -1)
    gtk_list_store_append (store, &iter);
  else
    gtk_list_store_insert (store, &iter, pos);

  gtk_list_store_set (store, &iter,
                      MODEL_NAME, str,
                      MODEL_IS_HEADER, isHeader,
                      MODEL_IS_ITEM, !isHeader,
                      MODEL_CAN_EJECT, canEject, -1);
}



void
toggle_headers (GtkToggleButton *button_toggle_headers, GtkWidget* list)
{
  GtkTreeIter   iter;
  GtkTreeModel *model = gtk_tree_view_get_model (GTK_TREE_VIEW (list));

  if (gtk_toggle_button_get_active (button_toggle_headers))
  {
    add_to_list (list, 0, "DEVICES", TRUE, FALSE);
    add_to_list (list, 3, "PLACES", TRUE, FALSE);
    add_to_list (list, 8, "NETWORK", TRUE, FALSE);
  }
  else
  {
    gtk_tree_model_iter_nth_child (model, &iter, NULL, 8);
    gtk_list_store_remove (GTK_LIST_STORE (model), &iter);

    gtk_tree_model_iter_nth_child (model, &iter, NULL, 3);
    gtk_list_store_remove (GTK_LIST_STORE (model), &iter);

    gtk_tree_model_iter_nth_child (model, &iter, NULL, 0);
    gtk_list_store_remove (GTK_LIST_STORE (model), &iter);
  }
}



int
main (int argc, char *argv[])
{
  GtkWidget *window;
  GtkWidget *list;
  GtkWidget *pane;
  GtkWidget *box;
  GtkWidget *button_toggle_headers;

  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
#if GTK_CHECK_VERSION(3, 0, 0)
  gtk_window_set_title (GTK_WINDOW (window), "Thunar SCCEE - GTK3");
#else
  gtk_window_set_title (GTK_WINDOW (window), "Thunar SCCEE - GTK2");
#endif
  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_container_set_border_width (GTK_CONTAINER (window), 5);
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);

#if GTK_CHECK_VERSION(3, 0, 0)
  pane = gtk_paned_new (GTK_ORIENTATION_HORIZONTAL);
#else
  pane = gtk_hpaned_new ();
#endif
  gtk_container_add (GTK_CONTAINER (window), pane);

  list = gtk_tree_view_new ();
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (list), FALSE);
  gtk_widget_set_size_request (list, 200, -1);
  gtk_paned_pack1 (GTK_PANED (pane), list, FALSE, FALSE);

#if GTK_CHECK_VERSION(3, 0, 0)
  box = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE);
#else
  box = gtk_vbox_new (FALSE, 0);
#endif
  gtk_paned_pack2 (GTK_PANED (pane), box, TRUE, FALSE);

  button_toggle_headers = gtk_toggle_button_new_with_label ("Toggle Headers");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button_toggle_headers), TRUE);
  gtk_box_pack_start (GTK_BOX (box), button_toggle_headers, FALSE, FALSE, 0);

  init_list (list);
  add_to_list (list, -1, "DEVICES", TRUE, FALSE);
  add_to_list (list, -1, "File System", FALSE, FALSE);
  add_to_list (list, -1, "120 GB Volume", FALSE, TRUE);
  add_to_list (list, -1, "PLACES", TRUE, FALSE);
  add_to_list (list, -1, "Home", FALSE, FALSE);
  add_to_list (list, -1, "Dekstop", FALSE, FALSE);
  add_to_list (list, -1, "Trash", FALSE, FALSE);
  add_to_list (list, -1, "Downloads", FALSE, FALSE);
  add_to_list (list, -1, "NETWORK", TRUE, FALSE);
  add_to_list (list, -1, "Browse Network", FALSE, FALSE);

  g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);

  g_signal_connect (G_OBJECT(button_toggle_headers), "toggled", G_CALLBACK (toggle_headers), list);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
