#include <stdio.h>
#include <string.h>

#include "gtk/gtk.h"
#include "gdk/gdk.h"
#include <glib.h>

GtkWidget* window;
GtkWidget* hbox;
GtkWidget* vbox;
GtkWidget* scrolledwindow;
GtkWidget* list1;
GtkWidget* list2;
GtkTreeModel* model1;
GtkTreeModel* model2;
GtkCellRenderer* renderer;

GtkWidget* button;
GtkRcStyle* rc_style;
GdkColor color;

GtkTreeIter treeiter;

gint i;
gchar* buffer;

gboolean useViewPort;
gboolean showHeader;
gboolean bgInList;
gboolean bgInWindow;

GValue value = { 0, };

/* DND call backs */

const GtkTargetEntry dropTypes[2] =
    {
      { target: "import",           flags: GTK_TARGET_SAME_APP, info: 0 },
      { target: "export",           flags: GTK_TARGET_SAME_APP, info: 0 }
    };

GtkTreePath *drag_source;
GtkTreePath *drag_dest;
GdkDragContext *drag_context;

gboolean rowIsDraggable (GtkTreeDragSource *drag_source, GtkTreePath *path)
{
 return TRUE;
}
 
gboolean rowIsDroppable (GtkTreeDragDest *drag_dest, GtkTreePath *path, GtkSelectionData* selection_data)
{
  return TRUE;
}

void dnd_begin (GtkWidget *widget, GdkDragContext* context, gpointer user_data)
{
  gint x;
  gint y;
  GdkModifierType mask;

  GtkTreePath *path;
  GtkTreeViewColumn *column;


  fprintf (stderr, "DEBUG: dnd started\n");
  gdk_window_get_pointer (gtk_tree_view_get_bin_window(GTK_TREE_VIEW(widget)), &x, &y, &mask);
  fprintf (stderr, "DEBUG: dnd started %d %d\n", x, y);
  if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW(widget), x, y, &path, &column, NULL, NULL))
    drag_source = path;
  else
    {
      fprintf (stderr, "DEBUG: dnd started: no row at %d, %d\n", x, y);
      drag_source = path;
    }
  drag_context = context;
  fprintf (stderr, "DEBUG: dnd started <<\n");
}

gboolean dnd_motion (GtkWidget* widget, GdkDragContext *context, gint x, gint y, guint time, gpointer user_data)
{
  GdkModifierType mask;
  gint x2;
  gint y2;

  GtkTreePath *path;
  GtkTreeViewColumn *column;

  fprintf (stderr, "DEBUG: dnd motion\n");
  gdk_window_get_pointer (gtk_tree_view_get_bin_window(GTK_TREE_VIEW(widget)), &x2, &y2, &mask);
  if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW(widget), x2, y2, &path, &column, NULL, NULL))
    {
      if (rowIsDroppable (GTK_TREE_DRAG_DEST(widget), path, NULL))
	gdk_drag_status (drag_context, 0, 0);
      else 
	gdk_drag_status (drag_context, 2, 0);
      gtk_tree_path_free(path);
    }
  return FALSE;
}

void dnd_end (GtkWidget *widget, GdkDragContext *context, gpointer user_data)
{
  gint x;
  gint y;
  GdkModifierType mask;

  GtkTreePath *path;
  GtkTreeViewColumn *column;

  fprintf (stderr, "DEBUG: dnd end\n");
  gdk_window_get_pointer (gtk_tree_view_get_bin_window(GTK_TREE_VIEW(list2)), &x, &y, &mask);
  if (gtk_tree_view_get_path_at_pos (GTK_TREE_VIEW(list2), x, y, &path, &column, NULL, NULL))
    drag_dest = path;

  if (context->action != 0)
    {
      g_object_set_data(G_OBJECT(list1), gtk_tree_path_to_string(drag_source), (gpointer) gtk_tree_row_reference_new(model2, drag_dest));
    }

    gtk_tree_path_free (drag_source);
    drag_source =  NULL;
    gtk_tree_path_free (drag_dest);
    drag_dest =  NULL;
    drag_context = NULL;
}

void def_target_drag_data_get (GtkWidget* widget,
		               GdkDragContext*     context,
		               GtkSelectionData*     selection_data,
		               guint     info,
		               guint     time,
                               gpointer     data)
{
  gchar* ref;

  fprintf (stderr, "DEBUG: drag data get: %ld\n", (long) drag_source);
  ref = gtk_tree_path_to_string(drag_source);
  gtk_selection_data_set (selection_data,
			  selection_data->target,
			  8, (guchar*) ref, strlen(ref));
}
#if 0
PROCEDURE def_target_drag_data_received  (widget             : PGtkWidget;
			                  context            : PGdkDragContext;
			                  x                  : gint;
			                  y                  : gint;
			                  data               : PGtkSelectionData;
			                  info               : guint;
			                  time               : guint;
                                          user_data          : gpointer); cdecl;
  BEGIN
writeln ('DND: dnd_data_received: ', gpointer_to_size(widget), ' data = ', gpointer_to_size(user_data), ' sel data = ', pchar(data^.data));
    if ((data^.length >= 0) AND (data^.format = 8)) 
    then begin
           gtk_drag_finish (context, TRUE, FALSE, time);
         end
    else gtk_drag_finish (context, FALSE, FALSE, time);
  END;
#endif

/* call backs for the middle pane with settings */


void output_state_reorderable (GtkToggleButton* togglebutton, gpointer user_data)
{
  if (gtk_toggle_button_get_active (togglebutton))
    gtk_tree_view_set_reorderable (GTK_TREE_VIEW(user_data), TRUE);
  else
    {
      gtk_tree_view_set_reorderable (GTK_TREE_VIEW(user_data), FALSE);
  
      /* enable dragging again */
      gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW(list1), &(dropTypes[0]), 1, GDK_ACTION_COPY | GDK_ACTION_MOVE);

      gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW(list1), GDK_BUTTON1_MASK |GDK_BUTTON3_MASK, &dropTypes[0], 1, GDK_ACTION_COPY | GDK_ACTION_MOVE);
    }
}

void output_state_tlines (GtkToggleButton* togglebutton, gpointer user_data)
{
  if (gtk_toggle_button_get_active (togglebutton))
    gtk_tree_view_set_enable_tree_lines (GTK_TREE_VIEW(user_data), TRUE);
  else
    gtk_tree_view_set_enable_tree_lines (GTK_TREE_VIEW(user_data), FALSE);
}

void output_state_tlines_value (GtkToggleButton* togglebutton, gpointer user_data)
{
  if (gtk_toggle_button_get_active (togglebutton))
    gtk_tree_view_set_level_indentation (GTK_TREE_VIEW(user_data), 40);
  else
    gtk_tree_view_set_level_indentation (GTK_TREE_VIEW(user_data), 0);
}

void output_state_grid (GtkToggleButton* togglebutton, gpointer user_data)
{
  if (gtk_toggle_button_get_active (togglebutton))
    gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(user_data), GTK_TREE_VIEW_GRID_LINES_BOTH);
  else
    gtk_tree_view_set_grid_lines (GTK_TREE_VIEW(user_data), GTK_TREE_VIEW_GRID_LINES_NONE);
}

void output_state_rules (GtkToggleButton* togglebutton, gpointer user_data)
{
  if (gtk_toggle_button_get_active (togglebutton))
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(user_data), TRUE);
  else
    gtk_tree_view_set_rules_hint (GTK_TREE_VIEW(user_data), FALSE);
}

void output_state_header (GtkToggleButton* togglebutton, gpointer user_data)
{
    showHeader = gtk_toggle_button_get_active (togglebutton);

    gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(user_data), showHeader);
}

void output_state_expander (GtkToggleButton* togglebutton, gpointer user_data)
{
    if (gtk_toggle_button_get_active (togglebutton))
      {
	gtk_tree_view_set_expander_column (GTK_TREE_VIEW(user_data), NULL);
	gtk_tree_view_set_show_expanders (GTK_TREE_VIEW(user_data), TRUE);
      }
    else
      gtk_tree_view_set_show_expanders (GTK_TREE_VIEW(user_data), FALSE);
}

void output_state_hscroll (GtkToggleButton* togglebutton, gpointer user_data)
  {
    if (gtk_toggle_button_get_active (togglebutton))
      {
	gtk_tree_view_set_bg_scrolling (GTK_TREE_VIEW(user_data), GTK_TREE_VIEW_BG_SCROLL_MOVING, GTK_TREE_VIEW_BG_SCROLL_NONE);
      }
    else
      gtk_tree_view_set_bg_scrolling (GTK_TREE_VIEW(user_data), GTK_TREE_VIEW_BG_SCROLL_DEFAULT, GTK_TREE_VIEW_BG_SCROLL_NONE);
  }

void output_state_vscroll (GtkToggleButton* togglebutton, gpointer user_data)
 {
    if (gtk_toggle_button_get_active (togglebutton))
      {
	gtk_tree_view_set_bg_scrolling (GTK_TREE_VIEW(user_data), GTK_TREE_VIEW_BG_SCROLL_NONE, GTK_TREE_VIEW_BG_SCROLL_MOVING);
      }
    else
      gtk_tree_view_set_bg_scrolling (GTK_TREE_VIEW(user_data), GTK_TREE_VIEW_BG_SCROLL_NONE, GTK_TREE_VIEW_BG_SCROLL_DEFAULT);
 }

 int main(int argc, char** argv)
{
  /* Initialize i18n support */
  gtk_set_locale();

  /* Initialize the widget set */
  gtk_init (&argc, &argv);

  showHeader = TRUE;
  useViewPort = FALSE;
  bgInWindow = TRUE;
  bgInList = TRUE;

  if (useViewPort)
    bgInList = TRUE;
  if (bgInList) 
    bgInWindow = FALSE;

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  hbox = gtk_hbox_new(FALSE, 2);
  vbox = gtk_vbox_new(FALSE, 2);

  gtk_container_add(GTK_CONTAINER(window), hbox);

  scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  model1 = (GtkTreeModel*) gtk_tree_store_new (5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
  list1 = gtk_tree_view_new_with_model(model1);
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(list1), showHeader);
  gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(list1), TRUE);
  gtk_tree_view_set_search_column (GTK_TREE_VIEW(list1), 1);

  gtk_tree_selection_set_mode( gtk_tree_view_get_selection(GTK_TREE_VIEW(list1)), GTK_SELECTION_MULTIPLE);

  if (1)  gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW(list1), TRUE);

  g_value_init (&value, G_TYPE_BOOLEAN);
  g_value_set_boolean(&value, TRUE);

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list1),
                                                      -1, "column header 1",
                                                      renderer,
						       "text", 1, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list1), 0);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 1);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
     gtk_tree_view_column_set_reorderable (treecolumn, TRUE);
    }

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  g_object_set_property (G_OBJECT(renderer), "editable", &value);  
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list1),
                                                      -1, "column header 2",
                                                      renderer,
						       "text", 2, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list1), 1);
      //      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(model1), 2, GTK_SORT_ASCENDING);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 2);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
     gtk_tree_view_column_set_reorderable (treecolumn, TRUE);
    }

  renderer = gtk_cell_renderer_text_new ();

  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list1),
                                                      -1, "column header 3",
                                                      renderer,
						       "text", 3, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list1), 2);
      //      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(model1), 3, GTK_SORT_ASCENDING);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 3);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
     gtk_tree_view_column_set_reorderable (treecolumn, TRUE);
    }

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list1),
                                                      -1, "column header 4",
                                                      renderer,
						       "text", 4, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list1), 3);
      //      gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(model1), 4, GTK_SORT_ASCENDING);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 4);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
     gtk_tree_view_column_set_reorderable (treecolumn, TRUE);
    }

  button = gtk_check_button_new_with_label ("display headers");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), showHeader);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_header), (gpointer)list1);

  button = gtk_check_button_new_with_label ("set expander");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), TRUE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_expander), (gpointer)list1);

  button = gtk_check_button_new_with_label ("set tree lines");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), FALSE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_tlines), (gpointer)list1);

  button = gtk_check_button_new_with_label ("indent size 10");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), FALSE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_tlines_value), (gpointer)list1);

  button = gtk_check_button_new_with_label ("set hor bg scrolling");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), FALSE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_hscroll), (gpointer)list1);

  button = gtk_check_button_new_with_label ("set ver bg scrolling");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), FALSE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_vscroll), (gpointer)list1);

  button = gtk_check_button_new_with_label ("set rules hint");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), FALSE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_rules), (gpointer)list1);

  button = gtk_check_button_new_with_label ("set grid lines");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), FALSE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_grid), (gpointer)list1);

  button = gtk_check_button_new_with_label ("set reorderable");
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(button), FALSE);
  gtk_box_pack_start (GTK_BOX(vbox), button, FALSE, FALSE, 0);
  g_signal_connect (button, "toggled", G_CALLBACK(output_state_reorderable), (gpointer)list1);

  gtk_box_pack_start (GTK_BOX(hbox), scrolledwindow, TRUE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX(hbox), vbox, FALSE, FALSE, 0);

  if (useViewPort) 
    gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scrolledwindow), list1);
  else
    gtk_container_add(GTK_CONTAINER(scrolledwindow), list1);

  gtk_tree_view_set_bg_scrolling (GTK_TREE_VIEW(list1), GTK_TREE_VIEW_BG_SCROLL_FIXED, GTK_TREE_VIEW_BG_SCROLL_FIXED);

#if 1
  /* dragging */
  g_signal_connect (GTK_OBJECT (list1), "drag-begin", G_CALLBACK (dnd_begin), NULL);
  g_signal_connect (GTK_OBJECT (list1), "drag-end", G_CALLBACK (dnd_end), NULL);
  gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW(list1), &(dropTypes[0]), 1, GDK_ACTION_COPY |  GDK_ACTION_MOVE);

  gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW(list1), GDK_BUTTON1_MASK |GDK_BUTTON3_MASK, &dropTypes[0], 1, GDK_ACTION_COPY |  GDK_ACTION_MOVE);
  g_signal_connect (GTK_OBJECT (list1), "drag-data-get", G_CALLBACK (def_target_drag_data_get), NULL);


#if 0
  treeinterface1 = GTK_TREE_DRAG_SOURCE_GET_IFACE(model1);
  treeinterface1->row_draggable = &rowIsDraggable;
#endif
  //  GTK_TREE_DRAG_SOURCE_GET_IFACE(model1)->row_draggable = &rowIsDraggable;
  GTK_TREE_DRAG_SOURCE_GET_IFACE(model1)->row_draggable = NULL;


  gtk_tree_view_set_dragwindow_anchor (GTK_TREE_VIEW(list1), GTK_ANCHOR_NORTH_WEST);
  gtk_tree_view_set_enable_dragwindow_anchor (GTK_TREE_VIEW(list1), TRUE);
#endif

  /* set style for window */
  rc_style = gtk_rc_style_new ();
  rc_style->color_flags[GTK_STATE_NORMAL] = GTK_RC_BG;
  if (bgInWindow)
    {
      rc_style->bg_pixmap_name[GTK_STATE_NORMAL] = strdup("/home/genea/gava/themes/cotswolds/side_left2.xpm");
      gtk_widget_modify_style (GTK_WIDGET(window), rc_style);
    }

  /* set style for treeview */
  rc_style = gtk_rc_style_new ();
  gdk_color_parse ("#4d4d4d", &color);
  rc_style->text[GTK_STATE_NORMAL] = color;
  if (bgInList) 
    {
      rc_style->bg_pixmap_name[GTK_STATE_NORMAL] = strdup("/home/genea/gava/themes/cotswolds/side_left2.xpm");
    }
  else 
    {
      rc_style->bg_pixmap_name[GTK_STATE_NORMAL] = strdup("<parent>");
    }
  rc_style->color_flags[GTK_STATE_NORMAL] = GTK_RC_BG | GTK_RC_TEXT;
  gtk_widget_modify_style (GTK_WIDGET(list1), rc_style);
  
  buffer = g_strdup ("test me 000x");
  for (i=1;i<=100;i++)
    {
      GtkTreeIter treeiter2;

      gtk_tree_store_append (GTK_TREE_STORE(model1), &treeiter, NULL);
      g_snprintf (buffer, 12, "test me %03d", (gint)i);
      gtk_tree_store_set(GTK_TREE_STORE(model1), &treeiter, 1, buffer, 2, "extra text", 3, "more text", 4, "last column text", -1);

        gtk_tree_store_append (GTK_TREE_STORE(model1), &treeiter2, &treeiter);
        gtk_tree_store_set(GTK_TREE_STORE(model1), &treeiter2, 1, buffer, 2, "sub text", 3, "sub text", 4, "sub column text", -1);
    }


  /* create second treeview on the right */
  scrolledwindow = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW(scrolledwindow), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

  model2 = (GtkTreeModel*) gtk_tree_store_new (5, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
  list2 = gtk_tree_view_new_with_model(model2);
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW(list2), showHeader);
  gtk_tree_view_set_headers_clickable (GTK_TREE_VIEW(list2), TRUE);

  if (1)  gtk_tree_view_set_rubber_banding (GTK_TREE_VIEW(list2), TRUE);

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list2),
                                                      -1, "column header 1",
                                                      renderer,
						       "text", 1, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list2), 0);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 1);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
    }

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list2),
                                                      -1, "column header 2",
                                                      renderer,
						       "text", 2, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list2), 1);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 2);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
    }

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list2),
                                                      -1, "column header 3",
                                                      renderer,
						       "text", 3, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list2), 2);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 3);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
    }

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (G_OBJECT (renderer), "xalign", (double)0.0, NULL);
  if (0 < gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW(list2),
                                                      -1, "column header 4",
                                                      renderer,
						       "text", 4, NULL))
    {
     GtkTreeViewColumn *treecolumn = gtk_tree_view_get_column (GTK_TREE_VIEW(list2), 3);
     gtk_tree_view_column_set_sort_column_id (treecolumn, 4);
     gtk_tree_view_column_set_resizable (treecolumn, TRUE);
    }
  gtk_box_pack_start (GTK_BOX(hbox), scrolledwindow, TRUE, TRUE, 0);
  gtk_container_add(GTK_CONTAINER(scrolledwindow), list2);

#if 1
  /* dragging */
  g_signal_connect (GTK_OBJECT (list2), "drag-begin", G_CALLBACK (dnd_begin), NULL);
  g_signal_connect (GTK_OBJECT (list2), "drag-end", G_CALLBACK (dnd_end), NULL);
  gtk_tree_view_enable_model_drag_dest (GTK_TREE_VIEW(list2), &(dropTypes[0]), 1, GDK_ACTION_COPY |  GDK_ACTION_MOVE);

  gtk_tree_view_enable_model_drag_source (GTK_TREE_VIEW(list2), GDK_BUTTON1_MASK |GDK_BUTTON3_MASK, &dropTypes[0], 1, GDK_ACTION_COPY |  GDK_ACTION_MOVE);
  g_signal_connect (GTK_OBJECT (list2), "drag-data-get", G_CALLBACK (def_target_drag_data_get), NULL);


#if 0
  treeinterface1 = GTK_TREE_DRAG_SOURCE_GET_IFACE(model2);
  treeinterface1->row_draggable = &rowIsDraggable;
#endif
  //  GTK_TREE_DRAG_SOURCE_GET_IFACE(model2)->row_draggable = &rowIsDraggable;
  GTK_TREE_DRAG_SOURCE_GET_IFACE(model2)->row_draggable = NULL;
#endif


  rc_style = gtk_rc_style_new ();
  gdk_color_parse ("#eeeeee", &color);
  rc_style->text[GTK_STATE_NORMAL] = color;
  rc_style->color_flags[GTK_STATE_NORMAL] = GTK_RC_TEXT;
  gtk_widget_modify_style (GTK_WIDGET(list2), rc_style);
 
  buffer = g_strdup ("dummy 000x");
  for (i=1;i<=10;i++)
    {
      gtk_tree_store_append (GTK_TREE_STORE(model2), &treeiter, NULL);
      g_snprintf (buffer, 10, "dummy %03d", (gint)i);
      gtk_tree_store_set(GTK_TREE_STORE(model2), &treeiter, 1, buffer, 2, "C2", 3, "C3", 4, "C4", -1);
    }

  /* start the program ... */


  gtk_widget_show_all (window);

  g_object_connect (G_OBJECT(window), "signal::destroy",
                        gtk_main_quit,
		    NULL, NULL);

  gtk_main();

  return 0;
}
